xref: /titanic_44/usr/src/lib/libpool/common/pool_xml.c (revision 5ad42b1b1469908fabc0099764182e9ecbc04dda)
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
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * 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*5ad42b1bSSurya Prakki  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <errno.h>
277c478bd9Sstevel@tonic-gate #include <fcntl.h>
287c478bd9Sstevel@tonic-gate #include <limits.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <thread.h>
337c478bd9Sstevel@tonic-gate #include <time.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/mman.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/time.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <libxml/debugXML.h>
437c478bd9Sstevel@tonic-gate #include <libxml/parser.h>
447c478bd9Sstevel@tonic-gate #include <libxml/tree.h>
457c478bd9Sstevel@tonic-gate #include <libxml/xmlerror.h>
467c478bd9Sstevel@tonic-gate #include <libxml/xpath.h>
477c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <pool.h>
507c478bd9Sstevel@tonic-gate #include "pool_internal.h"
517c478bd9Sstevel@tonic-gate #include "pool_impl.h"
527c478bd9Sstevel@tonic-gate #include "pool_xml_impl.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * libpool XML Manipulation Routines
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * pool_xml.c implements the XML manipulation routines used by the libpool
587c478bd9Sstevel@tonic-gate  * XML datastore. The functions are grouped into the following logical areas
597c478bd9Sstevel@tonic-gate  * - Result Sets
607c478bd9Sstevel@tonic-gate  * The XPath API is used to search the XML document represented by a
617c478bd9Sstevel@tonic-gate  * configuration. The results of XPath queries are represented through
627c478bd9Sstevel@tonic-gate  * pool_result_set_t structures as part of the abstraction of the datastore
637c478bd9Sstevel@tonic-gate  * representation. (see pool.c comment for more details)
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * - Property Manipulation
667c478bd9Sstevel@tonic-gate  * Validated XML (XML associated with a DTD) does not allow the introduction
677c478bd9Sstevel@tonic-gate  * of attributes which are not recognised by the DTD. This is a limitation
687c478bd9Sstevel@tonic-gate  * since we want to allow libpool to associate an arbitrary number of
697c478bd9Sstevel@tonic-gate  * properties with an element. The property manipulation code overcomes this
707c478bd9Sstevel@tonic-gate  * limitation by allowing property sub-elements to be created and manipulated
717c478bd9Sstevel@tonic-gate  * through a single API so that they are indistinguishable from attributes
727c478bd9Sstevel@tonic-gate  * to the libpool user.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * - XML Element/Attribute Manipulation
757c478bd9Sstevel@tonic-gate  * These routines manipulate XML elements and attributes and are the routines
767c478bd9Sstevel@tonic-gate  * which interact most directly with libxml.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  * - File Processing/IO
797c478bd9Sstevel@tonic-gate  * Since libpool must present its data in a consistent fashion, we have to
807c478bd9Sstevel@tonic-gate  * implement file locking above libxml. These routines allow us to lock files
817c478bd9Sstevel@tonic-gate  * during processing and maintain data integrity between processes. Note
827c478bd9Sstevel@tonic-gate  * that locks are at the process scope and are advisory (see fcntl).
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  * - Utilities
857c478bd9Sstevel@tonic-gate  * Sundry utility functions that aren't easily categorised.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	MAX_PROP_SIZE	1024	/* Size of property buffer */
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * The PAGE_READ_SIZE value is used to determine the size of the input buffer
917c478bd9Sstevel@tonic-gate  * used to parse XML files.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate #define	PAGE_READ_SIZE	8192
947c478bd9Sstevel@tonic-gate #define	ELEM_TYPE_COUNT	6	/* Count of Element types */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate typedef struct dtype_tbl
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	xmlChar *dt_name;
997c478bd9Sstevel@tonic-gate 	int dt_type;
1007c478bd9Sstevel@tonic-gate } dtype_tbl_t;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate typedef struct elem_type_tbl
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	xmlChar *ett_elem;
1057c478bd9Sstevel@tonic-gate 	dtype_tbl_t (*ett_dtype)[];
1067c478bd9Sstevel@tonic-gate } elem_type_tbl_t;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate extern int xmlDoValidityCheckingDefaultValue;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * The _xml_lock is used to lock the state of libpool during
1127c478bd9Sstevel@tonic-gate  * xml initialisation operations.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate static mutex_t _xml_lock;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate const char *element_class_tags[] = {
1177c478bd9Sstevel@tonic-gate 	"any",
1187c478bd9Sstevel@tonic-gate 	"system",
1197c478bd9Sstevel@tonic-gate 	"pool",
1207c478bd9Sstevel@tonic-gate 	"res_comp",
1217c478bd9Sstevel@tonic-gate 	"res_agg",
1227c478bd9Sstevel@tonic-gate 	"comp",
1237c478bd9Sstevel@tonic-gate 	NULL
1247c478bd9Sstevel@tonic-gate };
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static const char *data_type_tags[] = {
1277c478bd9Sstevel@tonic-gate 	"uint",
1287c478bd9Sstevel@tonic-gate 	"int",
1297c478bd9Sstevel@tonic-gate 	"float",
1307c478bd9Sstevel@tonic-gate 	"boolean",
1317c478bd9Sstevel@tonic-gate 	"string"
1327c478bd9Sstevel@tonic-gate };
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate const char *dtd_location = "file:///usr/share/lib/xml/dtd/rm_pool.dtd.1";
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static elem_type_tbl_t elem_tbl[ELEM_TYPE_COUNT] = {0};
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /* libpool initialisation indicator */
1397c478bd9Sstevel@tonic-gate static int _libpool_xml_initialised = PO_FALSE;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * Utility functions
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * Those functions which are not static are shared with pool_kernel.c
1467c478bd9Sstevel@tonic-gate  * They provide the required XML support for exporting a kernel
1477c478bd9Sstevel@tonic-gate  * configuration as an XML document.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate void xml_init(void);
1507c478bd9Sstevel@tonic-gate static int create_shadow(xmlNodePtr node);
1517c478bd9Sstevel@tonic-gate static int pool_xml_free_doc(pool_conf_t *conf);
1527c478bd9Sstevel@tonic-gate static int prop_sort(const void *a, const void *b);
1537c478bd9Sstevel@tonic-gate static int dtd_exists(const char *path);
1547c478bd9Sstevel@tonic-gate static void build_dtype_accelerator(void);
1557c478bd9Sstevel@tonic-gate static dtype_tbl_t (*build_dtype_tbl(const xmlChar *rawdata))[];
1567c478bd9Sstevel@tonic-gate static int get_fast_dtype(xmlNodePtr node, xmlChar *name);
1577c478bd9Sstevel@tonic-gate static int pool_assoc_default_resource_type(pool_t *,
1587c478bd9Sstevel@tonic-gate     pool_resource_elem_class_t);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * XML Data access and navigation APIs
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate static int pool_build_xpath_buf(pool_xml_connection_t *, const pool_elem_t *,
1647c478bd9Sstevel@tonic-gate     pool_elem_class_t, pool_value_t **, char_buf_t *, int);
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * SHARED WITH pool_kernel.c for XML export support
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate xmlNodePtr node_create(xmlNodePtr parent, const xmlChar *name);
1697c478bd9Sstevel@tonic-gate static xmlNodePtr node_create_with_id(xmlNodePtr parent, const xmlChar *name);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /* Configuration */
1727c478bd9Sstevel@tonic-gate static int pool_xml_close(pool_conf_t *);
1737c478bd9Sstevel@tonic-gate static int pool_xml_validate(const pool_conf_t *, pool_valid_level_t);
1747c478bd9Sstevel@tonic-gate static int pool_xml_commit(pool_conf_t *conf);
1757c478bd9Sstevel@tonic-gate static int pool_xml_export(const pool_conf_t *conf, const char *location,
1767c478bd9Sstevel@tonic-gate     pool_export_format_t fmt);
1777c478bd9Sstevel@tonic-gate static int pool_xml_rollback(pool_conf_t *conf);
1787c478bd9Sstevel@tonic-gate static pool_result_set_t *pool_xml_exec_query(const pool_conf_t *conf,
1797c478bd9Sstevel@tonic-gate     const pool_elem_t *src, const char *src_attr,
1807c478bd9Sstevel@tonic-gate     pool_elem_class_t classes, pool_value_t **props);
1817c478bd9Sstevel@tonic-gate static int pool_xml_remove(pool_conf_t *conf);
1827c478bd9Sstevel@tonic-gate static int pool_xml_res_transfer(pool_resource_t *, pool_resource_t *,
1837c478bd9Sstevel@tonic-gate     uint64_t);
1847c478bd9Sstevel@tonic-gate static int pool_xml_res_xtransfer(pool_resource_t *, pool_resource_t *,
1857c478bd9Sstevel@tonic-gate     pool_component_t **);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /* Connections */
1887c478bd9Sstevel@tonic-gate static void pool_xml_connection_free(pool_xml_connection_t *prov);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /* Result Sets */
1917c478bd9Sstevel@tonic-gate static pool_xml_result_set_t *pool_xml_result_set_alloc(const pool_conf_t *);
1927c478bd9Sstevel@tonic-gate static void pool_xml_result_set_free(pool_xml_result_set_t *rs);
1937c478bd9Sstevel@tonic-gate static pool_elem_t *pool_xml_rs_next(pool_result_set_t *set);
1947c478bd9Sstevel@tonic-gate static pool_elem_t *pool_xml_rs_prev(pool_result_set_t *set);
1957c478bd9Sstevel@tonic-gate static pool_elem_t *pool_xml_rs_first(pool_result_set_t *set);
1967c478bd9Sstevel@tonic-gate static pool_elem_t *pool_xml_rs_last(pool_result_set_t *set);
1977c478bd9Sstevel@tonic-gate static int pool_xml_rs_set_index(pool_result_set_t *set, int index);
1987c478bd9Sstevel@tonic-gate static int pool_xml_rs_get_index(pool_result_set_t *set);
1997c478bd9Sstevel@tonic-gate static int pool_xml_rs_count(pool_result_set_t *set);
2007c478bd9Sstevel@tonic-gate static int pool_xml_rs_close(pool_result_set_t *set);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /* Element (and sub-type) */
2037c478bd9Sstevel@tonic-gate static void pool_xml_elem_init(pool_conf_t *conf, pool_xml_elem_t *elem,
2047c478bd9Sstevel@tonic-gate     pool_elem_class_t, pool_resource_elem_class_t, pool_component_elem_class_t);
2057c478bd9Sstevel@tonic-gate static int pool_xml_elem_wrap(xmlNodePtr node, pool_elem_class_t class,
2067c478bd9Sstevel@tonic-gate     pool_resource_elem_class_t, pool_component_elem_class_t);
2077c478bd9Sstevel@tonic-gate static pool_elem_t *pool_xml_elem_create(pool_conf_t *, pool_elem_class_t,
2087c478bd9Sstevel@tonic-gate     pool_resource_elem_class_t, pool_component_elem_class_t);
2097c478bd9Sstevel@tonic-gate static int pool_xml_elem_remove(pool_elem_t *pe);
2107c478bd9Sstevel@tonic-gate static int pool_xml_set_container(pool_elem_t *, pool_elem_t *);
2117c478bd9Sstevel@tonic-gate static pool_elem_t *pool_xml_get_container(const pool_elem_t *);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * Pool element specific
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate static int pool_xml_pool_associate(pool_t *, const pool_resource_t *);
2177c478bd9Sstevel@tonic-gate static int pool_xml_pool_dissociate(pool_t *, const pool_resource_t *);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * Resource elements specific
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate static int pool_xml_resource_is_system(const pool_resource_t *);
2237c478bd9Sstevel@tonic-gate static int pool_xml_resource_can_associate(const pool_resource_t *);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /* Properties */
2267c478bd9Sstevel@tonic-gate static pool_value_class_t pool_xml_get_property(const pool_elem_t *,
2277c478bd9Sstevel@tonic-gate     const char *, pool_value_t *);
2287c478bd9Sstevel@tonic-gate static int pool_xml_put_property(pool_elem_t *, const char *,
2297c478bd9Sstevel@tonic-gate     const pool_value_t *);
2307c478bd9Sstevel@tonic-gate static int pool_xml_rm_property(pool_elem_t *, const char *);
2317c478bd9Sstevel@tonic-gate static xmlNodePtr property_create(xmlNodePtr, const char *,
2327c478bd9Sstevel@tonic-gate     pool_value_class_t);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /* Internal Attribute/Property manipulation */
2357c478bd9Sstevel@tonic-gate static int pool_is_xml_attr(xmlDocPtr, const char *, const char *);
2367c478bd9Sstevel@tonic-gate static pool_value_class_t pool_xml_get_attr(xmlNodePtr node, xmlChar *name,
2377c478bd9Sstevel@tonic-gate     pool_value_t *value);
2387c478bd9Sstevel@tonic-gate int pool_xml_set_attr(xmlNodePtr node, xmlChar *name,
2397c478bd9Sstevel@tonic-gate     const pool_value_t *value);
2407c478bd9Sstevel@tonic-gate static pool_value_class_t pool_xml_get_prop(xmlNodePtr node, xmlChar *name,
2417c478bd9Sstevel@tonic-gate     pool_value_t *value);
2427c478bd9Sstevel@tonic-gate int pool_xml_set_prop(xmlNodePtr node, xmlChar *name,
2437c478bd9Sstevel@tonic-gate     const pool_value_t *value);
2447c478bd9Sstevel@tonic-gate static pool_value_t **pool_xml_get_properties(const pool_elem_t *, uint_t *);
2457c478bd9Sstevel@tonic-gate /* XML Error handling */
2467c478bd9Sstevel@tonic-gate void pool_error_func(void *ctx, const char *msg, ...);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /* XML File Input Processing */
2497c478bd9Sstevel@tonic-gate static int pool_xml_open_file(pool_conf_t *conf);
2507c478bd9Sstevel@tonic-gate static int pool_xml_parse_document(pool_conf_t *);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * Initialise this module
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate void
xml_init()2567c478bd9Sstevel@tonic-gate xml_init()
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&_xml_lock);
2597c478bd9Sstevel@tonic-gate 	if (_libpool_xml_initialised == PO_TRUE) {
2607c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&_xml_lock);
2617c478bd9Sstevel@tonic-gate 		return;
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 	xmlInitParser();
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * DTD validation, with line numbers.
2677c478bd9Sstevel@tonic-gate 	 */
268*5ad42b1bSSurya Prakki 	(void) xmlLineNumbersDefault(1);
2697c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
2707c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
2717c478bd9Sstevel@tonic-gate 	/* Try to improve indentation and readability */
272*5ad42b1bSSurya Prakki 	(void) xmlKeepBlanksDefault(0);
2737c478bd9Sstevel@tonic-gate 	/* Send all XML errors to our debug handler */
2747c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, pool_error_func);
2757c478bd9Sstevel@tonic-gate 	/* Load up DTD element a-dtype data to improve performance */
2767c478bd9Sstevel@tonic-gate 	build_dtype_accelerator();
2777c478bd9Sstevel@tonic-gate 	_libpool_xml_initialised = PO_TRUE;
2787c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&_xml_lock);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate  * Get the next ID for this configuration
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate static int
get_unique_id(xmlNodePtr node,char * id)2857c478bd9Sstevel@tonic-gate get_unique_id(xmlNodePtr node, char *id)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
2887c478bd9Sstevel@tonic-gate 	uint64_t nid = 0;
2897c478bd9Sstevel@tonic-gate 	if (node->doc->_private) {
2907c478bd9Sstevel@tonic-gate 		if (pool_get_ns_property(
2917c478bd9Sstevel@tonic-gate 		    pool_conf_to_elem((pool_conf_t *)node->doc->_private),
2927c478bd9Sstevel@tonic-gate 		    "_next_id", &val) == POC_UINT)
2937c478bd9Sstevel@tonic-gate 			(void) pool_value_get_uint64(&val, &nid);
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 	if (snprintf(id, KEY_BUFFER_SIZE, "id_%llx", nid) > KEY_BUFFER_SIZE) {
2967c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
2977c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	pool_value_set_uint64(&val, ++nid);
3007c478bd9Sstevel@tonic-gate 	return (pool_put_ns_property(
3017c478bd9Sstevel@tonic-gate 	    pool_conf_to_elem((pool_conf_t *)node->doc->_private), "_next_id",
3027c478bd9Sstevel@tonic-gate 	    &val));
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /* Document building functions */
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate  * node_create() creates a child node of type name of the supplied parent in
3097c478bd9Sstevel@tonic-gate  * the supplied document. If the parent or document is NULL, create the node
3107c478bd9Sstevel@tonic-gate  * but do not associate it with a parent or document.
3117c478bd9Sstevel@tonic-gate  */
3127c478bd9Sstevel@tonic-gate xmlNodePtr
node_create(xmlNodePtr parent,const xmlChar * name)3137c478bd9Sstevel@tonic-gate node_create(xmlNodePtr parent, const xmlChar *name)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	xmlNodePtr node;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (parent == NULL)
3187c478bd9Sstevel@tonic-gate 		node = xmlNewNode(NULL, name);
3197c478bd9Sstevel@tonic-gate 	else
3207c478bd9Sstevel@tonic-gate 		node = xmlNewChild(parent, NULL, name, NULL);
3217c478bd9Sstevel@tonic-gate 	return (node);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate /*
3257c478bd9Sstevel@tonic-gate  * node_create_with_id() creates a child node of type name of the supplied
3267c478bd9Sstevel@tonic-gate  * parent with the ref_id generated by get_unique_id(). Actual node creation
3277c478bd9Sstevel@tonic-gate  * is performed by node_create() and this function just sets the ref_id
3287c478bd9Sstevel@tonic-gate  * property to the value of the id.
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate static xmlNodePtr
node_create_with_id(xmlNodePtr parent,const xmlChar * name)3317c478bd9Sstevel@tonic-gate node_create_with_id(xmlNodePtr parent, const xmlChar *name)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	char id[KEY_BUFFER_SIZE]; /* Must be big enough for key below */
3347c478bd9Sstevel@tonic-gate 	xmlNodePtr node = node_create(parent, name);
3357c478bd9Sstevel@tonic-gate 	if (node != NULL) {
3367c478bd9Sstevel@tonic-gate 		if (get_unique_id(node, id) != PO_SUCCESS) {
3377c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(node);
3387c478bd9Sstevel@tonic-gate 			xmlFreeNode(node); /* recurses all children */
3397c478bd9Sstevel@tonic-gate 			pool_seterror(POE_DATASTORE);
3407c478bd9Sstevel@tonic-gate 			return (NULL);
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 		if (xmlSetProp(node, BAD_CAST c_ref_id, BAD_CAST id) == NULL) {
3437c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(node);
3447c478bd9Sstevel@tonic-gate 			xmlFreeNode(node); /* recurses all children */
3457c478bd9Sstevel@tonic-gate 			pool_seterror(POE_DATASTORE);
3467c478bd9Sstevel@tonic-gate 			return (NULL);
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 	return (node);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /* Supporting Data Conversion Routines */
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /* XML Parser Utility Functions */
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate  * Handler for XML Errors. Called by libxml at libxml Error.
3587c478bd9Sstevel@tonic-gate  */
3597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3607c478bd9Sstevel@tonic-gate void
pool_error_func(void * ctx,const char * msg,...)3617c478bd9Sstevel@tonic-gate pool_error_func(void *ctx, const char *msg, ...)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	va_list ap;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	va_start(ap, msg);
3667c478bd9Sstevel@tonic-gate 	do_dprintf(msg, ap);
3677c478bd9Sstevel@tonic-gate 	va_end(ap);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate  * Free the shadowed elements from within the supplied document and then
3727c478bd9Sstevel@tonic-gate  * free the document. This function should always be called when freeing
3737c478bd9Sstevel@tonic-gate  * a pool document to ensure that all "shadow" resources are reclaimed.
3747c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
3757c478bd9Sstevel@tonic-gate  */
3767c478bd9Sstevel@tonic-gate static int
pool_xml_free_doc(pool_conf_t * conf)3777c478bd9Sstevel@tonic-gate pool_xml_free_doc(pool_conf_t *conf)
3787c478bd9Sstevel@tonic-gate {
3797c478bd9Sstevel@tonic-gate 	/* Only do any of this if there is a document */
3807c478bd9Sstevel@tonic-gate 	if (((pool_xml_connection_t *)conf->pc_prov)->pxc_doc != NULL) {
3817c478bd9Sstevel@tonic-gate 		pool_elem_t *pe;
3827c478bd9Sstevel@tonic-gate 		pool_result_set_t *rs;
3837c478bd9Sstevel@tonic-gate 		/* Delete all the "shadowed" children of the doc */
3847c478bd9Sstevel@tonic-gate 		rs = pool_exec_query(conf, NULL, NULL, PEC_QRY_ANY, NULL);
3857c478bd9Sstevel@tonic-gate 		if (rs == NULL) {
3867c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
3877c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 		for (pe = rs->prs_next(rs); pe != NULL; pe = rs->prs_next(rs)) {
3907c478bd9Sstevel@tonic-gate 			/*
3917c478bd9Sstevel@tonic-gate 			 * Work out the element type and free the elem
3927c478bd9Sstevel@tonic-gate 			 */
3937c478bd9Sstevel@tonic-gate 			free(pe);
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 		(void) pool_rs_close(rs);
3967c478bd9Sstevel@tonic-gate 		xmlFreeDoc(((pool_xml_connection_t *)conf->pc_prov)->pxc_doc);
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 	((pool_xml_connection_t *)conf->pc_prov)->pxc_doc = NULL;
3997c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Remove an element from the document. Note that only three types of elements
4047c478bd9Sstevel@tonic-gate  * can be removed, res, comp and pools. comp are moved around to the
4057c478bd9Sstevel@tonic-gate  * default res when a res is deleted.
4067c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate static int
pool_xml_elem_remove(pool_elem_t * pe)4097c478bd9Sstevel@tonic-gate pool_xml_elem_remove(pool_elem_t *pe)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *pxe = (pool_xml_elem_t *)pe;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/*
4147c478bd9Sstevel@tonic-gate 	 * You can only destroy three elements: pools, resources and
4157c478bd9Sstevel@tonic-gate 	 * components.
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 	switch (pe->pe_class) {
4187c478bd9Sstevel@tonic-gate 	case PEC_POOL:
4197c478bd9Sstevel@tonic-gate 	case PEC_RES_COMP:
4207c478bd9Sstevel@tonic-gate 	case PEC_RES_AGG:
4217c478bd9Sstevel@tonic-gate 	case PEC_COMP:
4227c478bd9Sstevel@tonic-gate 		if (pxe->pxe_node) {
4237c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(pxe->pxe_node);
4247c478bd9Sstevel@tonic-gate 			xmlFreeNode(pxe->pxe_node); /* recurses all children */
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 		free(pxe);
4277c478bd9Sstevel@tonic-gate 		break;
4287c478bd9Sstevel@tonic-gate 	default:
4297c478bd9Sstevel@tonic-gate 		break;
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate /*
4357c478bd9Sstevel@tonic-gate  * Create a property element.
4367c478bd9Sstevel@tonic-gate  */
4377c478bd9Sstevel@tonic-gate static xmlNodePtr
property_create(xmlNodePtr parent,const char * name,pool_value_class_t type)4387c478bd9Sstevel@tonic-gate property_create(xmlNodePtr parent, const char *name, pool_value_class_t type)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	xmlNodePtr element;
4427c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if ((element = node_create(parent, BAD_CAST "property")) == NULL) {
4457c478bd9Sstevel@tonic-gate 		pool_seterror(POE_DATASTORE);
4467c478bd9Sstevel@tonic-gate 		return (NULL);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	if (pool_value_set_string(&val, name) != PO_SUCCESS) {
4497c478bd9Sstevel@tonic-gate 		xmlFree(element);
4507c478bd9Sstevel@tonic-gate 		return (NULL);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	(void) pool_xml_set_attr(element, BAD_CAST c_name, &val);
4537c478bd9Sstevel@tonic-gate 	if (pool_value_set_string(&val, data_type_tags[type]) != PO_SUCCESS) {
4547c478bd9Sstevel@tonic-gate 		xmlFree(element);
4557c478bd9Sstevel@tonic-gate 		return (NULL);
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 	(void) pool_xml_set_attr(element, BAD_CAST c_type, &val);
4587c478bd9Sstevel@tonic-gate 	return (element);
4597c478bd9Sstevel@tonic-gate }
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate /*
4627c478bd9Sstevel@tonic-gate  * External clients need to be able to put/get properties and this is the
4637c478bd9Sstevel@tonic-gate  * way to do it.
4647c478bd9Sstevel@tonic-gate  * This function is an interceptor, since it will *always* try to manipulate
4657c478bd9Sstevel@tonic-gate  * an attribute first. If the attribute doesn't exist, then it will treat
4667c478bd9Sstevel@tonic-gate  * the request as a property request.
4677c478bd9Sstevel@tonic-gate  */
4687c478bd9Sstevel@tonic-gate static pool_value_class_t
pool_xml_get_property(const pool_elem_t * pe,const char * name,pool_value_t * val)4697c478bd9Sstevel@tonic-gate pool_xml_get_property(const pool_elem_t *pe, const char *name,
4707c478bd9Sstevel@tonic-gate     pool_value_t *val)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	pool_value_class_t type;
4737c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *pxe = (pool_xml_elem_t *)pe;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * "type" is a special attribute which is not visible ever outside of
4777c478bd9Sstevel@tonic-gate 	 * libpool. Use the specific type accessor function.
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 	if (strcmp(name, c_type) == 0) {
4807c478bd9Sstevel@tonic-gate 		return (pool_xml_get_attr(pxe->pxe_node, BAD_CAST name,
4817c478bd9Sstevel@tonic-gate 		    val));
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 	if (is_ns_property(pe, name) != NULL) {	/* in ns */
4847c478bd9Sstevel@tonic-gate 		if ((type = pool_xml_get_attr(pxe->pxe_node,
4857c478bd9Sstevel@tonic-gate 		    BAD_CAST property_name_minus_ns(pe, name), val))
4867c478bd9Sstevel@tonic-gate 		    == POC_INVAL)
4877c478bd9Sstevel@tonic-gate 			return (pool_xml_get_prop(pxe->pxe_node, BAD_CAST name,
4887c478bd9Sstevel@tonic-gate 			    val));
4897c478bd9Sstevel@tonic-gate 	} else
4907c478bd9Sstevel@tonic-gate 		return (pool_xml_get_prop(pxe->pxe_node, BAD_CAST name, val));
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	return (type);
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * Put a property on an element. Check if the property is an attribute,
4977c478bd9Sstevel@tonic-gate  * if it is update that value. If not add a property element.
4987c478bd9Sstevel@tonic-gate  *
4997c478bd9Sstevel@tonic-gate  * There are three possible conditions here:
5007c478bd9Sstevel@tonic-gate  * - the name is a ns
5017c478bd9Sstevel@tonic-gate  *	- the name is an attribute
5027c478bd9Sstevel@tonic-gate  *	- the name isn't an attribute
5037c478bd9Sstevel@tonic-gate  * - the name is not a ns
5047c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate static int
pool_xml_put_property(pool_elem_t * pe,const char * name,const pool_value_t * val)5077c478bd9Sstevel@tonic-gate pool_xml_put_property(pool_elem_t *pe, const char *name,
5087c478bd9Sstevel@tonic-gate     const pool_value_t *val)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *pxe = (pool_xml_elem_t *)pe;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * "type" is a special attribute which is not visible ever outside of
5147c478bd9Sstevel@tonic-gate 	 * libpool. Use the specific type accessor function.
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	if (strcmp(name, c_type) == 0) {
5177c478bd9Sstevel@tonic-gate 		return (pool_xml_set_attr(pxe->pxe_node, BAD_CAST name,
5187c478bd9Sstevel@tonic-gate 		    val));
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 	if (is_ns_property(pe, name) != NULL) {	/* in ns */
5217c478bd9Sstevel@tonic-gate 		if (pool_xml_set_attr(pxe->pxe_node,
5227c478bd9Sstevel@tonic-gate 		    BAD_CAST property_name_minus_ns(pe, name), val) == PO_FAIL)
5237c478bd9Sstevel@tonic-gate 			return (pool_xml_set_prop(pxe->pxe_node, BAD_CAST name,
5247c478bd9Sstevel@tonic-gate 			    val));
5257c478bd9Sstevel@tonic-gate 	} else
5267c478bd9Sstevel@tonic-gate 		return (pool_xml_set_prop(pxe->pxe_node, BAD_CAST name, val));
5277c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate  * Remove a property from an element. Check if the property is an attribute,
5327c478bd9Sstevel@tonic-gate  * if it is fail. Otherwise remove the property subelement.
5337c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
5347c478bd9Sstevel@tonic-gate  */
5357c478bd9Sstevel@tonic-gate static int
pool_xml_rm_property(pool_elem_t * pe,const char * name)5367c478bd9Sstevel@tonic-gate pool_xml_rm_property(pool_elem_t *pe, const char *name)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *pxe = (pool_xml_elem_t *)pe;
5397c478bd9Sstevel@tonic-gate 	xmlXPathContextPtr ctx;
5407c478bd9Sstevel@tonic-gate 	xmlXPathObjectPtr path;
5417c478bd9Sstevel@tonic-gate 	char buf[MAX_PROP_SIZE];
5427c478bd9Sstevel@tonic-gate 	int ret;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (xmlHasProp(pxe->pxe_node, BAD_CAST name) != NULL) {
5457c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
5467c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/* use xpath to find the node with the appropriate value for name */
5507c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "property[@name=\"%s\"]", name);
5517c478bd9Sstevel@tonic-gate 	if ((ctx = xmlXPathNewContext(pxe->pxe_node->doc)) == NULL) {
5527c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
5537c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 	ctx->node = pxe->pxe_node;
5567c478bd9Sstevel@tonic-gate 	path = xmlXPathEval(BAD_CAST buf, ctx);
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	if (path && (path->type == XPATH_NODESET) &&
5597c478bd9Sstevel@tonic-gate 	    (path->nodesetval->nodeNr == 1)) {
5607c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(path->nodesetval->nodeTab[0]);
5617c478bd9Sstevel@tonic-gate 		xmlFreeNode(path->nodesetval->nodeTab[0]);
5627c478bd9Sstevel@tonic-gate 		ret = PO_SUCCESS;
5637c478bd9Sstevel@tonic-gate 	} else {
5647c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
5657c478bd9Sstevel@tonic-gate 		ret = PO_FAIL;
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	xmlXPathFreeObject(path);
5687c478bd9Sstevel@tonic-gate 	xmlXPathFreeContext(ctx);
5697c478bd9Sstevel@tonic-gate 	return (ret);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*
5737c478bd9Sstevel@tonic-gate  * Get the data type for an attribute name from the element node. The data
5747c478bd9Sstevel@tonic-gate  * type is returned and the value of the attribute updates the supplied value
5757c478bd9Sstevel@tonic-gate  * pointer.
5767c478bd9Sstevel@tonic-gate  */
5777c478bd9Sstevel@tonic-gate static pool_value_class_t
pool_xml_get_attr(xmlNodePtr node,xmlChar * name,pool_value_t * value)5787c478bd9Sstevel@tonic-gate pool_xml_get_attr(xmlNodePtr node, xmlChar *name, pool_value_t *value)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	pool_value_class_t data_type;
5817c478bd9Sstevel@tonic-gate 	xmlChar *data;
5827c478bd9Sstevel@tonic-gate 	uint64_t uval;
5837c478bd9Sstevel@tonic-gate 	int64_t ival;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (xmlHasProp(node, name) == NULL && pool_is_xml_attr(node->doc,
5867c478bd9Sstevel@tonic-gate 	    (const char *) node->name, (const char *) name) == PO_FALSE) {
5877c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
5887c478bd9Sstevel@tonic-gate 		return (POC_INVAL);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	if (xmlHasProp(node, BAD_CAST c_a_dtype) == NULL) {
5917c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
5927c478bd9Sstevel@tonic-gate 		return (POC_INVAL);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 	data = xmlGetProp(node, name);
5957c478bd9Sstevel@tonic-gate 	data_type = get_fast_dtype(node, name);
5967c478bd9Sstevel@tonic-gate 	if (data_type != POC_STRING && data == NULL) {
5977c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
5987c478bd9Sstevel@tonic-gate 		return (POC_INVAL);
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 	switch (data_type) {
6017c478bd9Sstevel@tonic-gate 	case POC_UINT:
6027c478bd9Sstevel@tonic-gate 		errno = 0;
6037c478bd9Sstevel@tonic-gate 		uval = strtoull((char *)data, NULL, 0);
6047c478bd9Sstevel@tonic-gate 		if (errno != 0) {
6057c478bd9Sstevel@tonic-gate 			data_type =  POC_INVAL;
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 		else
6087c478bd9Sstevel@tonic-gate 			pool_value_set_uint64(value, uval);
6097c478bd9Sstevel@tonic-gate 		break;
6107c478bd9Sstevel@tonic-gate 	case POC_INT:
6117c478bd9Sstevel@tonic-gate 		errno = 0;
6127c478bd9Sstevel@tonic-gate 		ival = strtoll((char *)data, NULL, 0);
6137c478bd9Sstevel@tonic-gate 		if (errno != 0) {
6147c478bd9Sstevel@tonic-gate 			data_type =  POC_INVAL;
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 		else
6177c478bd9Sstevel@tonic-gate 			pool_value_set_int64(value, ival);
6187c478bd9Sstevel@tonic-gate 		break;
6197c478bd9Sstevel@tonic-gate 	case POC_DOUBLE:
6207c478bd9Sstevel@tonic-gate 		pool_value_set_double(value, atof((const char *)data));
6217c478bd9Sstevel@tonic-gate 		break;
6227c478bd9Sstevel@tonic-gate 	case POC_BOOL:
6237c478bd9Sstevel@tonic-gate 		if (strcmp((const char *)data, "true") == 0)
6247c478bd9Sstevel@tonic-gate 			pool_value_set_bool(value, PO_TRUE);
6257c478bd9Sstevel@tonic-gate 		else
6267c478bd9Sstevel@tonic-gate 			pool_value_set_bool(value, PO_FALSE);
6277c478bd9Sstevel@tonic-gate 		break;
6287c478bd9Sstevel@tonic-gate 	case POC_STRING:
6297c478bd9Sstevel@tonic-gate 		if (pool_value_set_string(value, data ?
6307c478bd9Sstevel@tonic-gate 		    (const char *)data : "") != PO_SUCCESS) {
6317c478bd9Sstevel@tonic-gate 			xmlFree(data);
6327c478bd9Sstevel@tonic-gate 			return (POC_INVAL);
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 		break;
6357c478bd9Sstevel@tonic-gate 	case POC_INVAL:
6367c478bd9Sstevel@tonic-gate 	default:
6377c478bd9Sstevel@tonic-gate 		break;
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 	xmlFree(data);
6407c478bd9Sstevel@tonic-gate 	return (data_type);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate /*
6447c478bd9Sstevel@tonic-gate  * Set the data type for an attribute name from the element node. The
6457c478bd9Sstevel@tonic-gate  * supplied value is used to update the designated name using the data
6467c478bd9Sstevel@tonic-gate  * type supplied.
6477c478bd9Sstevel@tonic-gate  */
6487c478bd9Sstevel@tonic-gate int
pool_xml_set_attr(xmlNodePtr node,xmlChar * name,const pool_value_t * value)6497c478bd9Sstevel@tonic-gate pool_xml_set_attr(xmlNodePtr node, xmlChar *name, const pool_value_t *value)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	xmlChar buf[MAX_PROP_SIZE] = {0};
6527c478bd9Sstevel@tonic-gate 	uint64_t ures;
6537c478bd9Sstevel@tonic-gate 	int64_t ires;
6547c478bd9Sstevel@tonic-gate 	uchar_t bres;
6557c478bd9Sstevel@tonic-gate 	double dres;
6567c478bd9Sstevel@tonic-gate 	const char *sres;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	pool_value_class_t data_type;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if (xmlHasProp(node, name) == NULL && pool_is_xml_attr(node->doc,
6617c478bd9Sstevel@tonic-gate 	    (const char *) node->name, (const char *) name) == PO_FALSE) {
6627c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
6637c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (xmlHasProp(node, BAD_CAST c_a_dtype) == NULL) {
6677c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
6687c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 	data_type = get_fast_dtype(node, name);
6717c478bd9Sstevel@tonic-gate 	if (data_type != value->pv_class) {
6727c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
6737c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 	switch (value->pv_class) {
6767c478bd9Sstevel@tonic-gate 	case POC_UINT:
6777c478bd9Sstevel@tonic-gate 		(void) pool_value_get_uint64(value, &ures);
6787c478bd9Sstevel@tonic-gate 		(void) snprintf((char *)buf, sizeof (buf), "%llu",
6797c478bd9Sstevel@tonic-gate 		    (u_longlong_t)ures);
6807c478bd9Sstevel@tonic-gate 		break;
6817c478bd9Sstevel@tonic-gate 	case POC_INT:
6827c478bd9Sstevel@tonic-gate 		(void) pool_value_get_int64(value, &ires);
6837c478bd9Sstevel@tonic-gate 		(void) snprintf((char *)buf, sizeof (buf), "%lld",
6847c478bd9Sstevel@tonic-gate 		    (longlong_t)ires);
6857c478bd9Sstevel@tonic-gate 		break;
6867c478bd9Sstevel@tonic-gate 	case POC_DOUBLE:
6877c478bd9Sstevel@tonic-gate 		(void) pool_value_get_double(value, &dres);
6887c478bd9Sstevel@tonic-gate 		(void) snprintf((char *)buf, sizeof (buf), "%f", dres);
6897c478bd9Sstevel@tonic-gate 		break;
6907c478bd9Sstevel@tonic-gate 	case POC_BOOL:
6917c478bd9Sstevel@tonic-gate 		(void) pool_value_get_bool(value, &bres);
6927c478bd9Sstevel@tonic-gate 		if (bres == PO_FALSE)
6937c478bd9Sstevel@tonic-gate 			(void) snprintf((char *)buf, sizeof (buf),
6947c478bd9Sstevel@tonic-gate 			    "false");
6957c478bd9Sstevel@tonic-gate 		else
6967c478bd9Sstevel@tonic-gate 			(void) snprintf((char *)buf, sizeof (buf),
6977c478bd9Sstevel@tonic-gate 			    "true");
6987c478bd9Sstevel@tonic-gate 		break;
6997c478bd9Sstevel@tonic-gate 	case POC_STRING:
7007c478bd9Sstevel@tonic-gate 		(void) pool_value_get_string(value, &sres);
7017c478bd9Sstevel@tonic-gate 		if (sres != NULL)
7027c478bd9Sstevel@tonic-gate 			(void) snprintf((char *)buf, sizeof (buf), "%s",
7037c478bd9Sstevel@tonic-gate 			    sres);
7047c478bd9Sstevel@tonic-gate 		break;
7057c478bd9Sstevel@tonic-gate 	case POC_INVAL:
7067c478bd9Sstevel@tonic-gate 	default:
7077c478bd9Sstevel@tonic-gate 		break;
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 	if (xmlSetProp(node, name, buf) == NULL) {
7107c478bd9Sstevel@tonic-gate 		pool_seterror(POE_DATASTORE);
7117c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate /*
7177c478bd9Sstevel@tonic-gate  * Get the data type for a property name from the element node. The data
7187c478bd9Sstevel@tonic-gate  * type is returned and the value of the property updates the supplied value
7197c478bd9Sstevel@tonic-gate  * pointer. The user is responsible for freeing the memory associated with
7207c478bd9Sstevel@tonic-gate  * a string.
7217c478bd9Sstevel@tonic-gate  */
7227c478bd9Sstevel@tonic-gate static pool_value_class_t
pool_xml_get_prop(xmlNodePtr node,xmlChar * name,pool_value_t * value)7237c478bd9Sstevel@tonic-gate pool_xml_get_prop(xmlNodePtr node, xmlChar *name, pool_value_t *value)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	pool_value_class_t data_type;
7267c478bd9Sstevel@tonic-gate 	xmlChar *data, *node_data;
7277c478bd9Sstevel@tonic-gate 	xmlXPathContextPtr ctx;
7287c478bd9Sstevel@tonic-gate 	xmlXPathObjectPtr path;
7297c478bd9Sstevel@tonic-gate 	char buf[MAX_PROP_SIZE];
7307c478bd9Sstevel@tonic-gate 	int64_t uval;
7317c478bd9Sstevel@tonic-gate 	int64_t ival;
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	/* use xpath to find the node with the appropriate value for name */
7347c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "property[@name=\"%s\"]", name);
7357c478bd9Sstevel@tonic-gate 	if ((ctx = xmlXPathNewContext(node->doc)) == NULL) {
7367c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
7377c478bd9Sstevel@tonic-gate 		return (POC_INVAL);
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 	ctx->node = node;
7407c478bd9Sstevel@tonic-gate 	path = xmlXPathEval(BAD_CAST buf, ctx);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (path && (path->type == XPATH_NODESET) &&
7437c478bd9Sstevel@tonic-gate 	    (path->nodesetval->nodeNr == 1)) {
7447c478bd9Sstevel@tonic-gate 		int i;
7457c478bd9Sstevel@tonic-gate 		if (xmlHasProp(path->nodesetval->nodeTab[0],
7467c478bd9Sstevel@tonic-gate 		    BAD_CAST c_type) == NULL) {
7477c478bd9Sstevel@tonic-gate 			xmlXPathFreeObject(path);
7487c478bd9Sstevel@tonic-gate 			xmlXPathFreeContext(ctx);
7497c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
7507c478bd9Sstevel@tonic-gate 			return (POC_INVAL);
7517c478bd9Sstevel@tonic-gate 		}
7527c478bd9Sstevel@tonic-gate 		/* type is a string representation of the type */
7537c478bd9Sstevel@tonic-gate 		data = xmlGetProp(path->nodesetval->nodeTab[0],
7547c478bd9Sstevel@tonic-gate 		    BAD_CAST c_type);
7557c478bd9Sstevel@tonic-gate 		node_data = xmlNodeGetContent(path->nodesetval->nodeTab[0]);
7567c478bd9Sstevel@tonic-gate 		data_type = POC_INVAL;
7577c478bd9Sstevel@tonic-gate 		for (i = 0; i < (sizeof (data_type_tags) /
7587c478bd9Sstevel@tonic-gate 		    sizeof (data_type_tags[0])); i++) {
7597c478bd9Sstevel@tonic-gate 			if (strcmp((char *)data, data_type_tags[i]) == 0) {
7607c478bd9Sstevel@tonic-gate 				data_type = i;
7617c478bd9Sstevel@tonic-gate 				break;
7627c478bd9Sstevel@tonic-gate 			}
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 		switch (data_type) {
7657c478bd9Sstevel@tonic-gate 		case POC_UINT:
7667c478bd9Sstevel@tonic-gate 			errno = 0;
7677c478bd9Sstevel@tonic-gate 			uval = strtoull((char *)node_data, NULL, 0);
7687c478bd9Sstevel@tonic-gate 			if (errno != 0)
7697c478bd9Sstevel@tonic-gate 				data_type =  POC_INVAL;
7707c478bd9Sstevel@tonic-gate 			else
7717c478bd9Sstevel@tonic-gate 				pool_value_set_uint64(value, uval);
7727c478bd9Sstevel@tonic-gate 			break;
7737c478bd9Sstevel@tonic-gate 		case POC_INT:
7747c478bd9Sstevel@tonic-gate 			errno = 0;
7757c478bd9Sstevel@tonic-gate 			ival = strtoll((char *)node_data, NULL, 0);
7767c478bd9Sstevel@tonic-gate 			if (errno != 0)
7777c478bd9Sstevel@tonic-gate 				data_type =  POC_INVAL;
7787c478bd9Sstevel@tonic-gate 			else
7797c478bd9Sstevel@tonic-gate 				pool_value_set_int64(value, ival);
7807c478bd9Sstevel@tonic-gate 			break;
7817c478bd9Sstevel@tonic-gate 		case POC_DOUBLE:
7827c478bd9Sstevel@tonic-gate 			pool_value_set_double(value,
7837c478bd9Sstevel@tonic-gate 			    atof((const char *)node_data));
7847c478bd9Sstevel@tonic-gate 			break;
7857c478bd9Sstevel@tonic-gate 		case POC_BOOL:
7867c478bd9Sstevel@tonic-gate 			if (strcmp((const char *)node_data, "true")
7877c478bd9Sstevel@tonic-gate 			    == 0)
7887c478bd9Sstevel@tonic-gate 				pool_value_set_bool(value, PO_TRUE);
7897c478bd9Sstevel@tonic-gate 			else
7907c478bd9Sstevel@tonic-gate 				pool_value_set_bool(value, PO_FALSE);
7917c478bd9Sstevel@tonic-gate 			break;
7927c478bd9Sstevel@tonic-gate 		case POC_STRING:
7937c478bd9Sstevel@tonic-gate 			if (pool_value_set_string(value,
7947c478bd9Sstevel@tonic-gate 			    (const char *)node_data) != PO_SUCCESS) {
7957c478bd9Sstevel@tonic-gate 				data_type = POC_INVAL;
7967c478bd9Sstevel@tonic-gate 				break;
7977c478bd9Sstevel@tonic-gate 			}
7987c478bd9Sstevel@tonic-gate 			break;
7997c478bd9Sstevel@tonic-gate 		case POC_INVAL:
8007c478bd9Sstevel@tonic-gate 		default:
8017c478bd9Sstevel@tonic-gate 			break;
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 		xmlFree(data);
8047c478bd9Sstevel@tonic-gate 		xmlFree(node_data);
8057c478bd9Sstevel@tonic-gate 		xmlXPathFreeObject(path);
8067c478bd9Sstevel@tonic-gate 		xmlXPathFreeContext(ctx);
8077c478bd9Sstevel@tonic-gate 		return (data_type);
8087c478bd9Sstevel@tonic-gate 	} else { /* No property exists, clean up and return */
8097c478bd9Sstevel@tonic-gate 		xmlXPathFreeObject(path);
8107c478bd9Sstevel@tonic-gate 		xmlXPathFreeContext(ctx);
8117c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
8127c478bd9Sstevel@tonic-gate 		return (POC_INVAL);
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate /*
8177c478bd9Sstevel@tonic-gate  * Set the data type for a property name from the element node. The
8187c478bd9Sstevel@tonic-gate  * supplied value is used to update the designated name using the data
8197c478bd9Sstevel@tonic-gate  * type supplied.
8207c478bd9Sstevel@tonic-gate  */
8217c478bd9Sstevel@tonic-gate int
pool_xml_set_prop(xmlNodePtr node,xmlChar * name,const pool_value_t * value)8227c478bd9Sstevel@tonic-gate pool_xml_set_prop(xmlNodePtr node, xmlChar *name, const pool_value_t *value)
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate /* First check if we have a property with this name (and type???). */
8257c478bd9Sstevel@tonic-gate 	xmlXPathContextPtr ctx;
8267c478bd9Sstevel@tonic-gate 	xmlXPathObjectPtr path;
8277c478bd9Sstevel@tonic-gate 	xmlChar buf[MAX_PROP_SIZE];
8287c478bd9Sstevel@tonic-gate 	xmlNodePtr element;
8297c478bd9Sstevel@tonic-gate 	uint64_t ures;
8307c478bd9Sstevel@tonic-gate 	int64_t ires;
8317c478bd9Sstevel@tonic-gate 	uchar_t bres;
8327c478bd9Sstevel@tonic-gate 	double dres;
8337c478bd9Sstevel@tonic-gate 	const char *sres;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	/* use xpath to find the node with the appropriate value for name */
8367c478bd9Sstevel@tonic-gate 	(void) snprintf((char *)buf, sizeof (buf), "property[@name=\"%s\"]",
8377c478bd9Sstevel@tonic-gate 	    name);
8387c478bd9Sstevel@tonic-gate 	if ((ctx = xmlXPathNewContext(node->doc)) == NULL) {
8397c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
8407c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 	ctx->node = node;
8437c478bd9Sstevel@tonic-gate 	path = xmlXPathEval(buf, ctx);
8447c478bd9Sstevel@tonic-gate 	if (path == NULL || path->type != XPATH_NODESET) {
8457c478bd9Sstevel@tonic-gate 		xmlXPathFreeObject(path);
8467c478bd9Sstevel@tonic-gate 		xmlXPathFreeContext(ctx);
8477c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
8487c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
8497c478bd9Sstevel@tonic-gate 	} else {
8507c478bd9Sstevel@tonic-gate 		if (path->nodesetval->nodeNr == 0)
8517c478bd9Sstevel@tonic-gate 			element = property_create
8527c478bd9Sstevel@tonic-gate 			    (node, (const char *)name, value->pv_class);
8537c478bd9Sstevel@tonic-gate 		else if (path->nodesetval->nodeNr == 1) {
8547c478bd9Sstevel@tonic-gate 			int i;
8557c478bd9Sstevel@tonic-gate 			xmlChar *data;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 			element = path->nodesetval->nodeTab[0];
8587c478bd9Sstevel@tonic-gate 			if (xmlHasProp(element, BAD_CAST c_type) == NULL) {
8597c478bd9Sstevel@tonic-gate 				xmlXPathFreeObject(path);
8607c478bd9Sstevel@tonic-gate 				xmlXPathFreeContext(ctx);
8617c478bd9Sstevel@tonic-gate 				pool_seterror(POE_INVALID_CONF);
8627c478bd9Sstevel@tonic-gate 				return (PO_FAIL);
8637c478bd9Sstevel@tonic-gate 			}
8647c478bd9Sstevel@tonic-gate 			data = xmlGetProp(element, BAD_CAST c_type);
8657c478bd9Sstevel@tonic-gate 			for (i = 0; i < (sizeof (data_type_tags) /
8667c478bd9Sstevel@tonic-gate 			    sizeof (data_type_tags[0])); i++)
8677c478bd9Sstevel@tonic-gate 				if (strcmp((char *)data, data_type_tags[i])
8687c478bd9Sstevel@tonic-gate 				    == 0) {
8697c478bd9Sstevel@tonic-gate 					break;
8707c478bd9Sstevel@tonic-gate 				}
8717c478bd9Sstevel@tonic-gate 			xmlFree(data);
8727c478bd9Sstevel@tonic-gate 			if (value->pv_class != i) {
8737c478bd9Sstevel@tonic-gate 				xmlXPathFreeObject(path);
8747c478bd9Sstevel@tonic-gate 				xmlXPathFreeContext(ctx);
8757c478bd9Sstevel@tonic-gate 				pool_seterror(POE_BADPARAM);
8767c478bd9Sstevel@tonic-gate 				return (PO_FAIL);
8777c478bd9Sstevel@tonic-gate 			}
8787c478bd9Sstevel@tonic-gate 		} else {
8797c478bd9Sstevel@tonic-gate 			xmlXPathFreeObject(path);
8807c478bd9Sstevel@tonic-gate 			xmlXPathFreeContext(ctx);
8817c478bd9Sstevel@tonic-gate 			pool_seterror(POE_BADPARAM);
8827c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	switch (value->pv_class) {
8877c478bd9Sstevel@tonic-gate 	case POC_UINT:
8887c478bd9Sstevel@tonic-gate 		(void) pool_value_get_uint64(value, &ures);
8897c478bd9Sstevel@tonic-gate 		(void) snprintf((char *)buf, sizeof (buf), "%llu",
8907c478bd9Sstevel@tonic-gate 		    (u_longlong_t)ures);
8917c478bd9Sstevel@tonic-gate 		break;
8927c478bd9Sstevel@tonic-gate 	case POC_INT:
8937c478bd9Sstevel@tonic-gate 		(void) pool_value_get_int64(value, &ires);
8947c478bd9Sstevel@tonic-gate 		(void) snprintf((char *)buf, sizeof (buf), "%lld",
8957c478bd9Sstevel@tonic-gate 		    (longlong_t)ires);
8967c478bd9Sstevel@tonic-gate 		break;
8977c478bd9Sstevel@tonic-gate 	case POC_DOUBLE:
8987c478bd9Sstevel@tonic-gate 		(void) pool_value_get_double(value, &dres);
8997c478bd9Sstevel@tonic-gate 		(void) snprintf((char *)buf, sizeof (buf), "%f", dres);
9007c478bd9Sstevel@tonic-gate 		break;
9017c478bd9Sstevel@tonic-gate 	case POC_BOOL:
9027c478bd9Sstevel@tonic-gate 		(void) pool_value_get_bool(value, &bres);
9037c478bd9Sstevel@tonic-gate 		if (bres == PO_FALSE)
9047c478bd9Sstevel@tonic-gate 			(void) snprintf((char *)buf, sizeof (buf),
9057c478bd9Sstevel@tonic-gate 			    "false");
9067c478bd9Sstevel@tonic-gate 		else
9077c478bd9Sstevel@tonic-gate 			(void) snprintf((char *)buf, sizeof (buf),
9087c478bd9Sstevel@tonic-gate 			    "true");
9097c478bd9Sstevel@tonic-gate 		break;
9107c478bd9Sstevel@tonic-gate 	case POC_STRING:
9117c478bd9Sstevel@tonic-gate 		(void) pool_value_get_string(value, &sres);
9127c478bd9Sstevel@tonic-gate 		(void) snprintf((char *)buf, sizeof (buf), "%s", sres);
9137c478bd9Sstevel@tonic-gate 		break;
9147c478bd9Sstevel@tonic-gate 	case POC_INVAL:
9157c478bd9Sstevel@tonic-gate 	default:
9167c478bd9Sstevel@tonic-gate 		break;
9177c478bd9Sstevel@tonic-gate 	}
9187c478bd9Sstevel@tonic-gate 	xmlNodeSetContent(element, buf);
9197c478bd9Sstevel@tonic-gate 	xmlXPathFreeObject(path);
9207c478bd9Sstevel@tonic-gate 	xmlXPathFreeContext(ctx);
9217c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate /*
9257c478bd9Sstevel@tonic-gate  * Return a NULL terminated array of pool_value_t which represents all
9267c478bd9Sstevel@tonic-gate  * of the properties stored for an element
9277c478bd9Sstevel@tonic-gate  *
9287c478bd9Sstevel@tonic-gate  * Return NULL on failure. It is the caller's responsibility to free
9297c478bd9Sstevel@tonic-gate  * the returned array of values.
9307c478bd9Sstevel@tonic-gate  */
9317c478bd9Sstevel@tonic-gate pool_value_t **
pool_xml_get_properties(const pool_elem_t * pe,uint_t * nprops)9327c478bd9Sstevel@tonic-gate pool_xml_get_properties(const pool_elem_t *pe, uint_t *nprops)
9337c478bd9Sstevel@tonic-gate {
9347c478bd9Sstevel@tonic-gate 	pool_value_t **result;
9357c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *pxe = (pool_xml_elem_t *)pe;
9367c478bd9Sstevel@tonic-gate 	int i, j;
9377c478bd9Sstevel@tonic-gate 	pool_conf_t *conf = TO_CONF(pe);
9387c478bd9Sstevel@tonic-gate 	xmlElementPtr elemDTD;
9397c478bd9Sstevel@tonic-gate 	xmlAttributePtr attr;
9407c478bd9Sstevel@tonic-gate 	xmlXPathContextPtr ctx;
9417c478bd9Sstevel@tonic-gate 	xmlXPathObjectPtr path;
9427c478bd9Sstevel@tonic-gate 	char_buf_t *cb = NULL;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	*nprops = 0;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	elemDTD = xmlGetDtdElementDesc(pxe->pxe_node->doc->extSubset,
9477c478bd9Sstevel@tonic-gate 	    pxe->pxe_node->name);
9487c478bd9Sstevel@tonic-gate 	for (attr = elemDTD->attributes; attr != NULL; attr = attr->nexth) {
9497c478bd9Sstevel@tonic-gate 		if (strcmp((const char *)attr->name, c_a_dtype) != 0 ||
9507c478bd9Sstevel@tonic-gate 		    strcmp((const char *)attr->name, c_type) != 0)
9517c478bd9Sstevel@tonic-gate 			(*nprops)++;
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	if ((ctx = xmlXPathNewContext(
9547c478bd9Sstevel@tonic-gate 	    ((pool_xml_connection_t *)conf->pc_prov)->pxc_doc)) == NULL) {
9557c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
9567c478bd9Sstevel@tonic-gate 		return (NULL);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 	ctx->node = pxe->pxe_node;
9597c478bd9Sstevel@tonic-gate 	path = xmlXPathEval(BAD_CAST "property", ctx);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	if (path != NULL && path->type == XPATH_NODESET &&
9627c478bd9Sstevel@tonic-gate 	    path->nodesetval != NULL)
9637c478bd9Sstevel@tonic-gate 		(*nprops) += path->nodesetval->nodeNr;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if ((result = calloc(*nprops + 1, sizeof (pool_value_t *))) == NULL) {
9667c478bd9Sstevel@tonic-gate 		xmlXPathFreeObject(path);
9677c478bd9Sstevel@tonic-gate 		xmlXPathFreeContext(ctx);
9687c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
9697c478bd9Sstevel@tonic-gate 		return (NULL);
9707c478bd9Sstevel@tonic-gate 	}
9717c478bd9Sstevel@tonic-gate 	if ((cb = alloc_char_buf(CB_DEFAULT_LEN)) == NULL) {
9727c478bd9Sstevel@tonic-gate 		xmlXPathFreeObject(path);
9737c478bd9Sstevel@tonic-gate 		xmlXPathFreeContext(ctx);
9747c478bd9Sstevel@tonic-gate 		free(result);
9757c478bd9Sstevel@tonic-gate 		return (NULL);
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 	/*
9787c478bd9Sstevel@tonic-gate 	 * Now store our attributes and properties in result
9797c478bd9Sstevel@tonic-gate 	 */
9807c478bd9Sstevel@tonic-gate 	for (i = 0, attr = elemDTD->attributes; attr != NULL;
9817c478bd9Sstevel@tonic-gate 	    attr = attr->nexth, i++) {
9827c478bd9Sstevel@tonic-gate 		if (strcmp((const char *)attr->name, c_a_dtype) == 0 ||
9837c478bd9Sstevel@tonic-gate 		    strcmp((const char *)attr->name, c_type) == 0) {
9847c478bd9Sstevel@tonic-gate 			i--;
9857c478bd9Sstevel@tonic-gate 			continue;
9867c478bd9Sstevel@tonic-gate 		}
9877c478bd9Sstevel@tonic-gate 		result[i] = pool_value_alloc();
9887c478bd9Sstevel@tonic-gate 		if (pool_xml_get_attr(pxe->pxe_node,
9897c478bd9Sstevel@tonic-gate 		    BAD_CAST attr->name, result[i]) == POC_INVAL) {
9907c478bd9Sstevel@tonic-gate 			xmlXPathFreeObject(path);
9917c478bd9Sstevel@tonic-gate 			xmlXPathFreeContext(ctx);
9927c478bd9Sstevel@tonic-gate 			while (i-- >= 0)
9937c478bd9Sstevel@tonic-gate 				pool_value_free(result[i]);
9947c478bd9Sstevel@tonic-gate 			free(result);
9957c478bd9Sstevel@tonic-gate 			free_char_buf(cb);
9967c478bd9Sstevel@tonic-gate 			return (NULL);
9977c478bd9Sstevel@tonic-gate 		}
9987c478bd9Sstevel@tonic-gate 		if (strcmp((const char *)attr->name, c_type) != 0) {
9997c478bd9Sstevel@tonic-gate 			if (set_char_buf(cb, "%s.%s",
10007c478bd9Sstevel@tonic-gate 			    pool_elem_class_string(pe), attr->name) !=
10017c478bd9Sstevel@tonic-gate 			    PO_SUCCESS) {
10027c478bd9Sstevel@tonic-gate 				xmlXPathFreeObject(path);
10037c478bd9Sstevel@tonic-gate 				xmlXPathFreeContext(ctx);
10047c478bd9Sstevel@tonic-gate 				while (i-- >= 0)
10057c478bd9Sstevel@tonic-gate 					pool_value_free(result[i]);
10067c478bd9Sstevel@tonic-gate 				free(result);
10077c478bd9Sstevel@tonic-gate 				free_char_buf(cb);
10087c478bd9Sstevel@tonic-gate 				return (NULL);
10097c478bd9Sstevel@tonic-gate 			}
10107c478bd9Sstevel@tonic-gate 			if (pool_value_set_name(result[i], cb->cb_buf) !=
10117c478bd9Sstevel@tonic-gate 			    PO_SUCCESS) {
10127c478bd9Sstevel@tonic-gate 				xmlXPathFreeObject(path);
10137c478bd9Sstevel@tonic-gate 				xmlXPathFreeContext(ctx);
10147c478bd9Sstevel@tonic-gate 				while (i-- >= 0)
10157c478bd9Sstevel@tonic-gate 					pool_value_free(result[i]);
10167c478bd9Sstevel@tonic-gate 				free(result);
10177c478bd9Sstevel@tonic-gate 				free_char_buf(cb);
10187c478bd9Sstevel@tonic-gate 				return (NULL);
10197c478bd9Sstevel@tonic-gate 			}
10207c478bd9Sstevel@tonic-gate 		} else {
10217c478bd9Sstevel@tonic-gate 			if (pool_value_set_name(result[i],
10227c478bd9Sstevel@tonic-gate 			    (const char *)attr->name) != PO_SUCCESS) {
10237c478bd9Sstevel@tonic-gate 				xmlXPathFreeObject(path);
10247c478bd9Sstevel@tonic-gate 				xmlXPathFreeContext(ctx);
10257c478bd9Sstevel@tonic-gate 				while (i-- >= 0)
10267c478bd9Sstevel@tonic-gate 					pool_value_free(result[i]);
10277c478bd9Sstevel@tonic-gate 				free(result);
10287c478bd9Sstevel@tonic-gate 				free_char_buf(cb);
10297c478bd9Sstevel@tonic-gate 				return (NULL);
10307c478bd9Sstevel@tonic-gate 			}
10317c478bd9Sstevel@tonic-gate 		}
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 	free_char_buf(cb);
10347c478bd9Sstevel@tonic-gate 	for (j = 0; j < path->nodesetval->nodeNr; j++, i++) {
10357c478bd9Sstevel@tonic-gate 		xmlChar *name = xmlGetProp(path->nodesetval->nodeTab[j],
10367c478bd9Sstevel@tonic-gate 		    BAD_CAST c_name);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 		result[i] = pool_value_alloc();
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 		if (pool_xml_get_prop(pxe->pxe_node, name, result[i]) ==
10417c478bd9Sstevel@tonic-gate 		    POC_INVAL) {
10427c478bd9Sstevel@tonic-gate 			xmlFree(name);
10437c478bd9Sstevel@tonic-gate 			xmlXPathFreeObject(path);
10447c478bd9Sstevel@tonic-gate 			xmlXPathFreeContext(ctx);
10457c478bd9Sstevel@tonic-gate 			while (i-- >= 0)
10467c478bd9Sstevel@tonic-gate 				pool_value_free(result[i]);
10477c478bd9Sstevel@tonic-gate 			free(result);
10487c478bd9Sstevel@tonic-gate 			return (NULL);
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 		if (pool_value_set_name(result[i], (const char *)name) !=
10517c478bd9Sstevel@tonic-gate 		    PO_SUCCESS) {
10527c478bd9Sstevel@tonic-gate 			xmlFree(name);
10537c478bd9Sstevel@tonic-gate 			xmlXPathFreeObject(path);
10547c478bd9Sstevel@tonic-gate 			xmlXPathFreeContext(ctx);
10557c478bd9Sstevel@tonic-gate 			while (i-- >= 0)
10567c478bd9Sstevel@tonic-gate 				pool_value_free(result[i]);
10577c478bd9Sstevel@tonic-gate 			free(result);
10587c478bd9Sstevel@tonic-gate 			return (NULL);
10597c478bd9Sstevel@tonic-gate 		}
10607c478bd9Sstevel@tonic-gate 		xmlFree(name);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 	xmlXPathFreeObject(path);
10637c478bd9Sstevel@tonic-gate 	xmlXPathFreeContext(ctx);
10647c478bd9Sstevel@tonic-gate 	return (result);
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate /*
10687c478bd9Sstevel@tonic-gate  * Store a pointer to one of our data types in the _private member of each
10697c478bd9Sstevel@tonic-gate  * XML data node contained within the passed node. Note this function is
10707c478bd9Sstevel@tonic-gate  * recursive and so all sub-nodes are also shadowed. Only shadow the nodes
10717c478bd9Sstevel@tonic-gate  * which we are interested in, i.e. system, pool, res and comp
10727c478bd9Sstevel@tonic-gate  */
10737c478bd9Sstevel@tonic-gate static int
create_shadow(xmlNodePtr node)10747c478bd9Sstevel@tonic-gate create_shadow(xmlNodePtr node)
10757c478bd9Sstevel@tonic-gate {
10767c478bd9Sstevel@tonic-gate 	xmlNodePtr sib;
10777c478bd9Sstevel@tonic-gate 	int ret = PO_SUCCESS;
10787c478bd9Sstevel@tonic-gate 	/* Create a data structure of the appropriate type */
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (0 == (xmlStrcmp(node->name,
10817c478bd9Sstevel@tonic-gate 	    BAD_CAST element_class_tags[PEC_SYSTEM]))) {
10827c478bd9Sstevel@tonic-gate 		ret = pool_xml_elem_wrap(node, PEC_SYSTEM, PREC_INVALID,
10837c478bd9Sstevel@tonic-gate 		    PCEC_INVALID);
10847c478bd9Sstevel@tonic-gate 	} else if (0 == (xmlStrcmp(node->name,
10857c478bd9Sstevel@tonic-gate 	    BAD_CAST element_class_tags[PEC_POOL]))) {
10867c478bd9Sstevel@tonic-gate 		ret = pool_xml_elem_wrap(node, PEC_POOL, PREC_INVALID,
10877c478bd9Sstevel@tonic-gate 		    PCEC_INVALID);
10887c478bd9Sstevel@tonic-gate 	} else if (0 == (xmlStrcmp(node->name,
10897c478bd9Sstevel@tonic-gate 	    BAD_CAST element_class_tags[PEC_RES_COMP]))) {
10907c478bd9Sstevel@tonic-gate 		xmlChar *data;
10917c478bd9Sstevel@tonic-gate 		pool_resource_elem_class_t res_class;
10927c478bd9Sstevel@tonic-gate 		data = xmlGetProp(node, BAD_CAST c_type);
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 		res_class = pool_resource_elem_class_from_string((char *)data);
10957c478bd9Sstevel@tonic-gate 		xmlFree(data);
10967c478bd9Sstevel@tonic-gate 		ret = pool_xml_elem_wrap(node, PEC_RES_COMP, res_class,
10977c478bd9Sstevel@tonic-gate 		    PCEC_INVALID);
10987c478bd9Sstevel@tonic-gate 	} else if (0 == (xmlStrcmp(node->name,
10997c478bd9Sstevel@tonic-gate 	    BAD_CAST element_class_tags[PEC_RES_AGG]))) {
11007c478bd9Sstevel@tonic-gate 		xmlChar *data;
11017c478bd9Sstevel@tonic-gate 		pool_resource_elem_class_t res_class;
11027c478bd9Sstevel@tonic-gate 		data = xmlGetProp(node, BAD_CAST c_type);
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 		res_class = pool_resource_elem_class_from_string((char *)data);
11057c478bd9Sstevel@tonic-gate 		xmlFree(data);
11067c478bd9Sstevel@tonic-gate 		ret = pool_xml_elem_wrap(node, PEC_RES_AGG, res_class,
11077c478bd9Sstevel@tonic-gate 		    PCEC_INVALID);
11087c478bd9Sstevel@tonic-gate 	} else if (0 == (xmlStrcmp(node->name,
11097c478bd9Sstevel@tonic-gate 	    BAD_CAST element_class_tags[PEC_COMP]))) {
11107c478bd9Sstevel@tonic-gate 		xmlChar *data;
11117c478bd9Sstevel@tonic-gate 		pool_component_elem_class_t comp_class;
11127c478bd9Sstevel@tonic-gate 		data = xmlGetProp(node, BAD_CAST c_type);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 		comp_class = pool_component_elem_class_from_string(
11157c478bd9Sstevel@tonic-gate 		    (char *)data);
11167c478bd9Sstevel@tonic-gate 		xmlFree(data);
11177c478bd9Sstevel@tonic-gate 		ret = pool_xml_elem_wrap(node, PEC_COMP, PREC_INVALID,
11187c478bd9Sstevel@tonic-gate 		    comp_class);
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 	/* Have to shadow all children and all siblings */
11217c478bd9Sstevel@tonic-gate 	for (sib = node->children; sib != NULL; sib = sib->next) {
11227c478bd9Sstevel@tonic-gate 		if ((ret = create_shadow(sib)) != PO_SUCCESS)
11237c478bd9Sstevel@tonic-gate 			break;
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 	return (ret);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate /*
11307c478bd9Sstevel@tonic-gate  * XML Data access and navigation APIs
11317c478bd9Sstevel@tonic-gate  */
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate /*
11347c478bd9Sstevel@tonic-gate  * Close the configuration. There are a few steps to closing a configuration:
11357c478bd9Sstevel@tonic-gate  * - Unlock the backing file (if there is one)
11367c478bd9Sstevel@tonic-gate  * - Close the file (if there is one)
11377c478bd9Sstevel@tonic-gate  * - Free the shadow memory	}Done in pool_xml_free_doc
11387c478bd9Sstevel@tonic-gate  * - Free the document		}
11397c478bd9Sstevel@tonic-gate  * - Free the data provider for this configuration
11407c478bd9Sstevel@tonic-gate  * - Free the configuration location specifier
11417c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
11427c478bd9Sstevel@tonic-gate  */
11437c478bd9Sstevel@tonic-gate static int
pool_xml_close(pool_conf_t * conf)11447c478bd9Sstevel@tonic-gate pool_xml_close(pool_conf_t *conf)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	pool_xml_connection_t *pxc = (pool_xml_connection_t *)conf->pc_prov;
11477c478bd9Sstevel@tonic-gate 	int ret = PO_SUCCESS;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	if (pxc->pxc_file != NULL) {
11507c478bd9Sstevel@tonic-gate 		/* Close (and implicitly) unlock the file */
11517c478bd9Sstevel@tonic-gate 		if (fclose(pxc->pxc_file) != 0) {
11527c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
11537c478bd9Sstevel@tonic-gate 			ret = PO_FAIL;
11547c478bd9Sstevel@tonic-gate 		}
11557c478bd9Sstevel@tonic-gate 		pxc->pxc_file = NULL;
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate 	/* Close the xml specific parts */
11587c478bd9Sstevel@tonic-gate 	(void) pool_xml_free_doc(conf);
11597c478bd9Sstevel@tonic-gate 	pool_xml_connection_free((pool_xml_connection_t *)conf->pc_prov);
11607c478bd9Sstevel@tonic-gate 	return (ret);
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate /*
11647c478bd9Sstevel@tonic-gate  * Remove the configuration from the backing store. In XML terms delete
11657c478bd9Sstevel@tonic-gate  * the file backing the configuration. You need a copy of the location
11667c478bd9Sstevel@tonic-gate  * since the pool_conf_close function, frees the location.
11677c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
11687c478bd9Sstevel@tonic-gate  */
11697c478bd9Sstevel@tonic-gate static int
pool_xml_remove(pool_conf_t * conf)11707c478bd9Sstevel@tonic-gate pool_xml_remove(pool_conf_t *conf)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	if (pool_conf_location(conf) != NULL) {
11737c478bd9Sstevel@tonic-gate 		/* First unlink the file, to prevent races on open */
11747c478bd9Sstevel@tonic-gate 		if (unlink(pool_conf_location(conf)) != 0) {
11757c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
11767c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
11777c478bd9Sstevel@tonic-gate 		}
11787c478bd9Sstevel@tonic-gate 		/* Now close the configuration */
11797c478bd9Sstevel@tonic-gate 		(void) pool_conf_close(conf);
11807c478bd9Sstevel@tonic-gate 		return (PO_SUCCESS);
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	return (PO_FAIL);
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate /*
11867c478bd9Sstevel@tonic-gate  * Validate the configuration. There are three levels of validation, loose,
11877c478bd9Sstevel@tonic-gate  * strict and runtime. In this, XML, implementation, loose is mapped to XML
11887c478bd9Sstevel@tonic-gate  * validation, strict implements additional application level validation
11897c478bd9Sstevel@tonic-gate  * checks, e.g. all pools must have unique names, runtime ensures that this
11907c478bd9Sstevel@tonic-gate  * configuration would instantiate on the current system.
11917c478bd9Sstevel@tonic-gate  *
11927c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
11937c478bd9Sstevel@tonic-gate  */
11947c478bd9Sstevel@tonic-gate static int
pool_xml_validate(const pool_conf_t * conf,pool_valid_level_t level)11957c478bd9Sstevel@tonic-gate pool_xml_validate(const pool_conf_t *conf, pool_valid_level_t level)
11967c478bd9Sstevel@tonic-gate {
11977c478bd9Sstevel@tonic-gate 	pool_xml_connection_t *pxc = (pool_xml_connection_t *)conf->pc_prov;
11987c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL) {
12017c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
12027c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate 	cvp->error    = pool_error_func;
12057c478bd9Sstevel@tonic-gate 	cvp->warning  = pool_error_func;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	if (xmlValidateDocument(cvp, pxc->pxc_doc) == 0) {
12087c478bd9Sstevel@tonic-gate 		xmlFreeValidCtxt(cvp);
12097c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
12107c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
12117c478bd9Sstevel@tonic-gate 	}
12127c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	if (level >= POV_RUNTIME) {
12157c478bd9Sstevel@tonic-gate 		/*
12167c478bd9Sstevel@tonic-gate 		 * Note: This is resource specific.
12177c478bd9Sstevel@tonic-gate 		 */
12187c478bd9Sstevel@tonic-gate 		return (((pool_validate_resource(conf, "pset", c_min_prop, 0) ==
12197c478bd9Sstevel@tonic-gate 		    PO_SUCCESS) &&
12207c478bd9Sstevel@tonic-gate 		    (pool_validate_resource(conf, "pset", c_max_prop, 0) ==
12217c478bd9Sstevel@tonic-gate 		    PO_SUCCESS)) ? PO_SUCCESS : PO_FAIL);
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate /*
12277c478bd9Sstevel@tonic-gate  * Commit the configuration to the backing store. In XML terms this means
12287c478bd9Sstevel@tonic-gate  * write the changes to the backing file. Read the comments below for details
12297c478bd9Sstevel@tonic-gate  * on exactly how this operation is performed.
12307c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
12317c478bd9Sstevel@tonic-gate  */
12327c478bd9Sstevel@tonic-gate static int
pool_xml_commit(pool_conf_t * conf)12337c478bd9Sstevel@tonic-gate pool_xml_commit(pool_conf_t *conf)
12347c478bd9Sstevel@tonic-gate {
12357c478bd9Sstevel@tonic-gate 	pool_xml_connection_t *prov = (pool_xml_connection_t *)conf->pc_prov;
12367c478bd9Sstevel@tonic-gate 	xmlOutputBufferPtr buf;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	/*
12397c478bd9Sstevel@tonic-gate 	 * Ensure that the configuration file has no contents
12407c478bd9Sstevel@tonic-gate 	 */
12417c478bd9Sstevel@tonic-gate 	if (fseek(prov->pxc_file, 0, SEEK_SET) != 0) {
12427c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
12437c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
12447c478bd9Sstevel@tonic-gate 	}
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	if (ftruncate(fileno(prov->pxc_file), 0) == -1) {
12477c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
12487c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 	/*
12517c478bd9Sstevel@tonic-gate 	 * Create an XML output buffer and write out the contents of the
12527c478bd9Sstevel@tonic-gate 	 * configuration to the file.
12537c478bd9Sstevel@tonic-gate 	 */
12547c478bd9Sstevel@tonic-gate 	if ((buf = xmlOutputBufferCreateFile(prov->pxc_file, NULL)) == NULL) {
12557c478bd9Sstevel@tonic-gate 		pool_seterror(POE_DATASTORE);
12567c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFileTo(buf, prov->pxc_doc, NULL, 1) == -1) {
12607c478bd9Sstevel@tonic-gate 		pool_seterror(POE_DATASTORE);
12617c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
12657c478bd9Sstevel@tonic-gate }
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate /*
12687c478bd9Sstevel@tonic-gate  * Export the configuration in the specified format to the specified location.
12697c478bd9Sstevel@tonic-gate  * The only format implemented now is the native format, which saves the
12707c478bd9Sstevel@tonic-gate  * active configuration to the supplied location.
12717c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
12727c478bd9Sstevel@tonic-gate  */
12737c478bd9Sstevel@tonic-gate static int
pool_xml_export(const pool_conf_t * conf,const char * location,pool_export_format_t fmt)12747c478bd9Sstevel@tonic-gate pool_xml_export(const pool_conf_t *conf, const char *location,
12757c478bd9Sstevel@tonic-gate     pool_export_format_t fmt)
12767c478bd9Sstevel@tonic-gate {
12777c478bd9Sstevel@tonic-gate 	int ret;
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	switch (fmt) {
12807c478bd9Sstevel@tonic-gate 	case POX_NATIVE:
12817c478bd9Sstevel@tonic-gate 		ret = xmlSaveFormatFile(location,
12827c478bd9Sstevel@tonic-gate 		    ((pool_xml_connection_t *)conf->pc_prov)->pxc_doc,
12837c478bd9Sstevel@tonic-gate 		    1);
12847c478bd9Sstevel@tonic-gate 		if (ret == -1) {
12857c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
12867c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
12877c478bd9Sstevel@tonic-gate 		} else
12887c478bd9Sstevel@tonic-gate 			return (PO_SUCCESS);
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	default:
12917c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
12927c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate }
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate /*
12977c478bd9Sstevel@tonic-gate  * Discard the configuration and restore the configuration to the values
12987c478bd9Sstevel@tonic-gate  * specified in the configuration location.
12997c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
13007c478bd9Sstevel@tonic-gate  */
13017c478bd9Sstevel@tonic-gate static int
pool_xml_rollback(pool_conf_t * conf)13027c478bd9Sstevel@tonic-gate pool_xml_rollback(pool_conf_t *conf)
13037c478bd9Sstevel@tonic-gate {
13047c478bd9Sstevel@tonic-gate 	pool_xml_connection_t *prov = (pool_xml_connection_t *)conf->pc_prov;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	/* Rollback the file pointer ready for the reparse */
13077c478bd9Sstevel@tonic-gate 	if (fseek(prov->pxc_file, 0, SEEK_SET) != 0) {
13087c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
13097c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 	/* Reparse the document */
13127c478bd9Sstevel@tonic-gate 	/* In XML terms this means, discard and reparse the document */
13137c478bd9Sstevel@tonic-gate 	(void) pool_xml_free_doc(conf);
13147c478bd9Sstevel@tonic-gate 	if (pool_xml_parse_document(conf) == PO_FAIL)
13157c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
13167c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate /*
13207c478bd9Sstevel@tonic-gate  * Allocate a new pool_elem_t in the supplied configuration of the specified
13217c478bd9Sstevel@tonic-gate  * class.
13227c478bd9Sstevel@tonic-gate  * Returns element pointer/NULL
13237c478bd9Sstevel@tonic-gate  */
13247c478bd9Sstevel@tonic-gate static void
pool_xml_elem_init(pool_conf_t * conf,pool_xml_elem_t * elem,pool_elem_class_t class,pool_resource_elem_class_t res_class,pool_component_elem_class_t comp_class)13257c478bd9Sstevel@tonic-gate pool_xml_elem_init(pool_conf_t *conf, pool_xml_elem_t *elem,
13267c478bd9Sstevel@tonic-gate     pool_elem_class_t class, pool_resource_elem_class_t res_class,
13277c478bd9Sstevel@tonic-gate     pool_component_elem_class_t comp_class)
13287c478bd9Sstevel@tonic-gate {
13297c478bd9Sstevel@tonic-gate 	pool_elem_t *pe = TO_ELEM(elem);
13307c478bd9Sstevel@tonic-gate 	pe->pe_conf = conf;
13317c478bd9Sstevel@tonic-gate 	pe->pe_class = class;
13327c478bd9Sstevel@tonic-gate 	pe->pe_resource_class = res_class;
13337c478bd9Sstevel@tonic-gate 	pe->pe_component_class = comp_class;
13347c478bd9Sstevel@tonic-gate 	/* Set up the function pointers for element manipulation */
13357c478bd9Sstevel@tonic-gate 	pe->pe_get_prop = pool_xml_get_property;
13367c478bd9Sstevel@tonic-gate 	pe->pe_put_prop = pool_xml_put_property;
13377c478bd9Sstevel@tonic-gate 	pe->pe_rm_prop = pool_xml_rm_property;
13387c478bd9Sstevel@tonic-gate 	pe->pe_get_props = pool_xml_get_properties;
13397c478bd9Sstevel@tonic-gate 	pe->pe_remove = pool_xml_elem_remove;
13407c478bd9Sstevel@tonic-gate 	pe->pe_get_container = pool_xml_get_container;
13417c478bd9Sstevel@tonic-gate 	pe->pe_set_container = pool_xml_set_container;
13427c478bd9Sstevel@tonic-gate 	/*
13437c478bd9Sstevel@tonic-gate 	 * Specific initialisation for different types of element
13447c478bd9Sstevel@tonic-gate 	 */
13457c478bd9Sstevel@tonic-gate 	if (class == PEC_POOL) {
13467c478bd9Sstevel@tonic-gate 		pool_xml_pool_t *pp = (pool_xml_pool_t *)elem;
13477c478bd9Sstevel@tonic-gate 		pp->pp_associate = pool_xml_pool_associate;
13487c478bd9Sstevel@tonic-gate 		pp->pp_dissociate = pool_xml_pool_dissociate;
13497c478bd9Sstevel@tonic-gate 	}
13507c478bd9Sstevel@tonic-gate 	if (class == PEC_RES_COMP || class == PEC_RES_AGG) {
13517c478bd9Sstevel@tonic-gate 		pool_xml_resource_t *pr = (pool_xml_resource_t *)elem;
13527c478bd9Sstevel@tonic-gate 		pr->pr_is_system = pool_xml_resource_is_system;
13537c478bd9Sstevel@tonic-gate 		pr->pr_can_associate = pool_xml_resource_can_associate;
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate }
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate /*
13587c478bd9Sstevel@tonic-gate  * "Wrap" a suplied XML node with a pool_elem_t sub-type of the supplied
13597c478bd9Sstevel@tonic-gate  * class.
13607c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
13617c478bd9Sstevel@tonic-gate  */
13627c478bd9Sstevel@tonic-gate static int
pool_xml_elem_wrap(xmlNodePtr node,pool_elem_class_t class,pool_resource_elem_class_t res_class,pool_component_elem_class_t comp_class)13637c478bd9Sstevel@tonic-gate pool_xml_elem_wrap(xmlNodePtr node, pool_elem_class_t class,
13647c478bd9Sstevel@tonic-gate     pool_resource_elem_class_t res_class,
13657c478bd9Sstevel@tonic-gate     pool_component_elem_class_t comp_class)
13667c478bd9Sstevel@tonic-gate {
13677c478bd9Sstevel@tonic-gate 	pool_conf_t *conf = node->doc->_private;
13687c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *elem;
13697c478bd9Sstevel@tonic-gate 	/* Need to do some messing about to support SubTypes */
13707c478bd9Sstevel@tonic-gate 	switch (class) {
13717c478bd9Sstevel@tonic-gate 	case PEC_SYSTEM:
13727c478bd9Sstevel@tonic-gate 		if ((elem = malloc(sizeof (pool_xml_system_t))) == NULL) {
13737c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
13747c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
13757c478bd9Sstevel@tonic-gate 		}
13767c478bd9Sstevel@tonic-gate 		(void) memset(elem, 0, sizeof (pool_xml_system_t));
13777c478bd9Sstevel@tonic-gate 		break;
13787c478bd9Sstevel@tonic-gate 	case PEC_POOL:
13797c478bd9Sstevel@tonic-gate 		if ((elem = malloc(sizeof (pool_xml_pool_t))) == NULL) {
13807c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
13817c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 		(void) memset(elem, 0, sizeof (pool_xml_pool_t));
13847c478bd9Sstevel@tonic-gate 		break;
13857c478bd9Sstevel@tonic-gate 	case PEC_RES_COMP:
13867c478bd9Sstevel@tonic-gate 	case PEC_RES_AGG:
13877c478bd9Sstevel@tonic-gate 		if ((elem = malloc(sizeof (pool_xml_resource_t))) == NULL) {
13887c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
13897c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
13907c478bd9Sstevel@tonic-gate 		}
13917c478bd9Sstevel@tonic-gate 		(void) memset(elem, 0, sizeof (pool_xml_resource_t));
13927c478bd9Sstevel@tonic-gate 		break;
13937c478bd9Sstevel@tonic-gate 	case PEC_COMP:
13947c478bd9Sstevel@tonic-gate 		if ((elem = malloc(sizeof (pool_xml_component_t))) == NULL) {
13957c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
13967c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
13977c478bd9Sstevel@tonic-gate 		}
13987c478bd9Sstevel@tonic-gate 		(void) memset(elem, 0, sizeof (pool_xml_component_t));
13997c478bd9Sstevel@tonic-gate 		break;
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 	pool_xml_elem_init(conf, elem, class, res_class, comp_class);
14027c478bd9Sstevel@tonic-gate 	node->_private = elem;
14037c478bd9Sstevel@tonic-gate 	elem->pxe_node = node;
14047c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate /*
14087c478bd9Sstevel@tonic-gate  * Associate a pool to the default resource for the supplied resource
14097c478bd9Sstevel@tonic-gate  * type.
14107c478bd9Sstevel@tonic-gate  */
14117c478bd9Sstevel@tonic-gate int
pool_assoc_default_resource_type(pool_t * pool,pool_resource_elem_class_t type)14127c478bd9Sstevel@tonic-gate pool_assoc_default_resource_type(pool_t *pool, pool_resource_elem_class_t type)
14137c478bd9Sstevel@tonic-gate {
14147c478bd9Sstevel@tonic-gate 	pool_value_t *props[] = { NULL, NULL, NULL };
14157c478bd9Sstevel@tonic-gate 	uint_t rl_size;
14167c478bd9Sstevel@tonic-gate 	pool_resource_t **rsl;
14177c478bd9Sstevel@tonic-gate 	pool_conf_t *conf = TO_ELEM(pool)->pe_conf;
14187c478bd9Sstevel@tonic-gate 	char_buf_t *cb = NULL;
14197c478bd9Sstevel@tonic-gate 	pool_value_t val0 = POOL_VALUE_INITIALIZER;
14207c478bd9Sstevel@tonic-gate 	pool_value_t val1 = POOL_VALUE_INITIALIZER;
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	props[0] = &val0;
14237c478bd9Sstevel@tonic-gate 	props[1] = &val1;
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	if (pool_value_set_string(props[0], pool_resource_type_string(type)) !=
14277c478bd9Sstevel@tonic-gate 	    PO_SUCCESS ||
14287c478bd9Sstevel@tonic-gate 	    pool_value_set_name(props[0], c_type) != PO_SUCCESS) {
14297c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
14307c478bd9Sstevel@tonic-gate 	}
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	if ((cb = alloc_char_buf(CB_DEFAULT_LEN)) == NULL) {
14337c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	if (set_char_buf(cb, "%s.default",
14377c478bd9Sstevel@tonic-gate 	    pool_resource_type_string(type)) !=
14387c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
14397c478bd9Sstevel@tonic-gate 		free_char_buf(cb);
14407c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
14417c478bd9Sstevel@tonic-gate 	}
14427c478bd9Sstevel@tonic-gate 	if (pool_value_set_name(props[1], cb->cb_buf) != PO_SUCCESS) {
14437c478bd9Sstevel@tonic-gate 		free_char_buf(cb);
14447c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
14457c478bd9Sstevel@tonic-gate 	}
14467c478bd9Sstevel@tonic-gate 	pool_value_set_bool(props[1], PO_TRUE);
14477c478bd9Sstevel@tonic-gate 	free_char_buf(cb);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	if ((rsl = pool_query_resources(conf, &rl_size, props)) == NULL) {
14507c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
14517c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
14527c478bd9Sstevel@tonic-gate 	}
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	/*
14557c478bd9Sstevel@tonic-gate 	 * One default resource set per type
14567c478bd9Sstevel@tonic-gate 	 */
14577c478bd9Sstevel@tonic-gate 	if (rl_size != 1) {
14587c478bd9Sstevel@tonic-gate 		free(rsl);
14597c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
14607c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
14617c478bd9Sstevel@tonic-gate 	}
14627c478bd9Sstevel@tonic-gate 	if (pool_associate(conf, pool, rsl[0])  < 0) {
14637c478bd9Sstevel@tonic-gate 		free(rsl);
14647c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
14657c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 	free(rsl);
14687c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate /*
14727c478bd9Sstevel@tonic-gate  * Create an XML node in the supplied configuration with a pool_elem_t
14737c478bd9Sstevel@tonic-gate  * sub-type of the supplied class.
14747c478bd9Sstevel@tonic-gate  * Returns pool_elem_t pointer/NULL
14757c478bd9Sstevel@tonic-gate  */
14767c478bd9Sstevel@tonic-gate static pool_elem_t *
pool_xml_elem_create(pool_conf_t * conf,pool_elem_class_t class,pool_resource_elem_class_t res_class,pool_component_elem_class_t comp_class)14777c478bd9Sstevel@tonic-gate pool_xml_elem_create(pool_conf_t *conf, pool_elem_class_t class,
14787c478bd9Sstevel@tonic-gate     pool_resource_elem_class_t res_class,
14797c478bd9Sstevel@tonic-gate     pool_component_elem_class_t comp_class)
14807c478bd9Sstevel@tonic-gate {
14817c478bd9Sstevel@tonic-gate 	/* In XML terms, create an element of the appropriate class */
14827c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *elem;
14837c478bd9Sstevel@tonic-gate 	pool_elem_t *parent;
14847c478bd9Sstevel@tonic-gate 	pool_system_t *parent_system;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	if (class == PEC_INVALID) {
14877c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
14887c478bd9Sstevel@tonic-gate 		return (NULL);
14897c478bd9Sstevel@tonic-gate 	}
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	/* Now create the XML component and add to it's parent */
14927c478bd9Sstevel@tonic-gate 	/*
14937c478bd9Sstevel@tonic-gate 	 * If we know the class of an element, we know it's parent.
14947c478bd9Sstevel@tonic-gate 	 * PEC_POOL, the parent must be the system node
14957c478bd9Sstevel@tonic-gate 	 * PEC_RES, treat as pool.
14967c478bd9Sstevel@tonic-gate 	 * PEC_COMP, we don't know the parent, leave this up to the
14977c478bd9Sstevel@tonic-gate 	 * create_comp function.
14987c478bd9Sstevel@tonic-gate 	 */
14997c478bd9Sstevel@tonic-gate 	/* Since we know the subtype we can create and populate the sub-type */
15007c478bd9Sstevel@tonic-gate 	switch (class) {
15017c478bd9Sstevel@tonic-gate 	case PEC_POOL:
15027c478bd9Sstevel@tonic-gate 		if ((parent_system = pool_conf_system(conf)) == NULL) {
15037c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
15047c478bd9Sstevel@tonic-gate 			return (NULL);
15057c478bd9Sstevel@tonic-gate 		}
15067c478bd9Sstevel@tonic-gate 		if ((parent = pool_system_elem(parent_system)) == NULL) {
15077c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
15087c478bd9Sstevel@tonic-gate 			return (NULL);
15097c478bd9Sstevel@tonic-gate 		}
15107c478bd9Sstevel@tonic-gate 		if ((elem = malloc(sizeof (pool_xml_system_t))) == NULL) {
15117c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
15127c478bd9Sstevel@tonic-gate 			return (NULL);
15137c478bd9Sstevel@tonic-gate 		}
15147c478bd9Sstevel@tonic-gate 		(void) memset(elem, 0, sizeof (pool_xml_system_t));
15157c478bd9Sstevel@tonic-gate 		if ((elem->pxe_node = node_create_with_id(
15167c478bd9Sstevel@tonic-gate 		    ((pool_xml_elem_t *)parent)->pxe_node,
15177c478bd9Sstevel@tonic-gate 		    BAD_CAST element_class_tags[class])) == NULL) {
15187c478bd9Sstevel@tonic-gate 			pool_seterror(POE_DATASTORE);
15197c478bd9Sstevel@tonic-gate 			(void) pool_xml_elem_remove((pool_elem_t *)elem);
15207c478bd9Sstevel@tonic-gate 			return (NULL);
15217c478bd9Sstevel@tonic-gate 		}
15227c478bd9Sstevel@tonic-gate 		break;
15237c478bd9Sstevel@tonic-gate 	case PEC_RES_COMP:
15247c478bd9Sstevel@tonic-gate 	case PEC_RES_AGG:
15257c478bd9Sstevel@tonic-gate 		if ((parent_system = pool_conf_system(conf)) == NULL) {
15267c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
15277c478bd9Sstevel@tonic-gate 			return (NULL);
15287c478bd9Sstevel@tonic-gate 		}
15297c478bd9Sstevel@tonic-gate 		if ((parent = pool_system_elem(parent_system)) == NULL) {
15307c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
15317c478bd9Sstevel@tonic-gate 			return (NULL);
15327c478bd9Sstevel@tonic-gate 		}
15337c478bd9Sstevel@tonic-gate 		if ((elem = malloc(sizeof (pool_xml_resource_t))) == NULL) {
15347c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
15357c478bd9Sstevel@tonic-gate 			return (NULL);
15367c478bd9Sstevel@tonic-gate 		}
15377c478bd9Sstevel@tonic-gate 		(void) memset(elem, 0, sizeof (pool_xml_resource_t));
15387c478bd9Sstevel@tonic-gate 		if ((elem->pxe_node = node_create_with_id
15397c478bd9Sstevel@tonic-gate 		    (((pool_xml_elem_t *)parent)->pxe_node,
15407c478bd9Sstevel@tonic-gate 		    BAD_CAST element_class_tags[class])) == NULL) {
15417c478bd9Sstevel@tonic-gate 			pool_seterror(POE_DATASTORE);
15427c478bd9Sstevel@tonic-gate 			(void) pool_xml_elem_remove((pool_elem_t *)elem);
15437c478bd9Sstevel@tonic-gate 			return (NULL);
15447c478bd9Sstevel@tonic-gate 		}
15457c478bd9Sstevel@tonic-gate 		break;
15467c478bd9Sstevel@tonic-gate 	case PEC_COMP:
15477c478bd9Sstevel@tonic-gate 		if ((elem = malloc(sizeof (pool_xml_component_t))) == NULL) {
15487c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
15497c478bd9Sstevel@tonic-gate 			return (NULL);
15507c478bd9Sstevel@tonic-gate 		}
15517c478bd9Sstevel@tonic-gate 		(void) memset(elem, 0, sizeof (pool_xml_component_t));
15527c478bd9Sstevel@tonic-gate 		if ((elem->pxe_node = node_create(NULL,
15537c478bd9Sstevel@tonic-gate 		    BAD_CAST element_class_tags[class])) == NULL) {
15547c478bd9Sstevel@tonic-gate 			pool_seterror(POE_DATASTORE);
15557c478bd9Sstevel@tonic-gate 			(void) pool_xml_elem_remove((pool_elem_t *)elem);
15567c478bd9Sstevel@tonic-gate 			return (NULL);
15577c478bd9Sstevel@tonic-gate 		}
15587c478bd9Sstevel@tonic-gate 		break;
15597c478bd9Sstevel@tonic-gate 	default:
15607c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
15617c478bd9Sstevel@tonic-gate 		return (NULL);
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate 	pool_xml_elem_init(conf, elem, class, res_class, comp_class);
15647c478bd9Sstevel@tonic-gate 	elem->pxe_node->_private = elem;
15657c478bd9Sstevel@tonic-gate 	if (class == PEC_RES_COMP || class == PEC_RES_AGG ||
15667c478bd9Sstevel@tonic-gate 	    class == PEC_COMP) {
15677c478bd9Sstevel@tonic-gate 		/*
15687c478bd9Sstevel@tonic-gate 		 * Put the type and an invalid sys_id on the node.
15697c478bd9Sstevel@tonic-gate 		 */
15707c478bd9Sstevel@tonic-gate 		if (xmlSetProp(elem->pxe_node, BAD_CAST c_sys_prop,
15717c478bd9Sstevel@tonic-gate 		    BAD_CAST POOL_SYSID_BAD_STRING) == NULL) {
15727c478bd9Sstevel@tonic-gate 			pool_seterror(POE_DATASTORE);
15737c478bd9Sstevel@tonic-gate 			(void) pool_xml_elem_remove((pool_elem_t *)elem);
15747c478bd9Sstevel@tonic-gate 			return (NULL);
15757c478bd9Sstevel@tonic-gate 		}
15767c478bd9Sstevel@tonic-gate 		if (xmlSetProp(elem->pxe_node, BAD_CAST c_type,
15777c478bd9Sstevel@tonic-gate 		    BAD_CAST pool_elem_class_string(
15787c478bd9Sstevel@tonic-gate 		    (pool_elem_t *)elem)) == NULL) {
15797c478bd9Sstevel@tonic-gate 			pool_seterror(POE_DATASTORE);
15807c478bd9Sstevel@tonic-gate 			(void) pool_xml_elem_remove((pool_elem_t *)elem);
15817c478bd9Sstevel@tonic-gate 			return (NULL);
15827c478bd9Sstevel@tonic-gate 		}
15837c478bd9Sstevel@tonic-gate 	}
15847c478bd9Sstevel@tonic-gate 	if (class == PEC_POOL) {
15857c478bd9Sstevel@tonic-gate 		/*
15867c478bd9Sstevel@tonic-gate 		 * Note: This is resource specific.
15877c478bd9Sstevel@tonic-gate 		 */
15887c478bd9Sstevel@tonic-gate 		if (pool_assoc_default_resource_type(pool_elem_pool(
15897c478bd9Sstevel@tonic-gate 		    (pool_elem_t *)elem), PREC_PSET) == PO_FAIL) {
15907c478bd9Sstevel@tonic-gate 			(void) pool_xml_elem_remove((pool_elem_t *)elem);
15917c478bd9Sstevel@tonic-gate 			return (NULL);
15927c478bd9Sstevel@tonic-gate 		}
15937c478bd9Sstevel@tonic-gate 	}
15947c478bd9Sstevel@tonic-gate 	return ((pool_elem_t *)elem);
15957c478bd9Sstevel@tonic-gate }
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate /*
15987c478bd9Sstevel@tonic-gate  * Allocate a data provider for the supplied configuration and optionally
15997c478bd9Sstevel@tonic-gate  * discover resources.
16007c478bd9Sstevel@tonic-gate  * The data provider is the cross over point from the "abstract" configuration
16017c478bd9Sstevel@tonic-gate  * functions into the data representation specific manipulation routines.
16027c478bd9Sstevel@tonic-gate  * This function sets up all the required pointers to create an XML aware
16037c478bd9Sstevel@tonic-gate  * data provider.
16047c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
16057c478bd9Sstevel@tonic-gate  */
16067c478bd9Sstevel@tonic-gate int
pool_xml_connection_alloc(pool_conf_t * conf,int oflags)16077c478bd9Sstevel@tonic-gate pool_xml_connection_alloc(pool_conf_t *conf, int oflags)
16087c478bd9Sstevel@tonic-gate {
16097c478bd9Sstevel@tonic-gate 	pool_xml_connection_t *prov;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	xml_init();
16127c478bd9Sstevel@tonic-gate 	if ((prov = malloc(sizeof (pool_xml_connection_t))) == NULL) {
16137c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
16147c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 	(void) memset(prov, 0, sizeof (pool_xml_connection_t));
16177c478bd9Sstevel@tonic-gate 	/*
16187c478bd9Sstevel@tonic-gate 	 * Initialise data members
16197c478bd9Sstevel@tonic-gate 	 */
16207c478bd9Sstevel@tonic-gate 	prov->pc_name = strdup("LIBXML 2.4.0");
16217c478bd9Sstevel@tonic-gate 	prov->pc_store_type = XML_DATA_STORE;
16227c478bd9Sstevel@tonic-gate 	prov->pc_oflags = oflags;
16237c478bd9Sstevel@tonic-gate 	/*
16247c478bd9Sstevel@tonic-gate 	 * Initialise function pointers
16257c478bd9Sstevel@tonic-gate 	 */
16267c478bd9Sstevel@tonic-gate 	prov->pc_close = pool_xml_close;
16277c478bd9Sstevel@tonic-gate 	prov->pc_validate = pool_xml_validate;
16287c478bd9Sstevel@tonic-gate 	prov->pc_commit = pool_xml_commit;
16297c478bd9Sstevel@tonic-gate 	prov->pc_export = pool_xml_export;
16307c478bd9Sstevel@tonic-gate 	prov->pc_rollback = pool_xml_rollback;
16317c478bd9Sstevel@tonic-gate 	prov->pc_exec_query = pool_xml_exec_query;
16327c478bd9Sstevel@tonic-gate 	prov->pc_elem_create = pool_xml_elem_create;
16337c478bd9Sstevel@tonic-gate 	prov->pc_remove = pool_xml_remove;
16347c478bd9Sstevel@tonic-gate 	prov->pc_res_xfer = pool_xml_res_transfer;
16357c478bd9Sstevel@tonic-gate 	prov->pc_res_xxfer = pool_xml_res_xtransfer;
16367c478bd9Sstevel@tonic-gate 	/*
16377c478bd9Sstevel@tonic-gate 	 * End of common initialisation
16387c478bd9Sstevel@tonic-gate 	 */
16397c478bd9Sstevel@tonic-gate 	/*
16407c478bd9Sstevel@tonic-gate 	 * Associate the provider to it's configuration
16417c478bd9Sstevel@tonic-gate 	 */
16427c478bd9Sstevel@tonic-gate 	conf->pc_prov = (pool_connection_t *)prov;
16437c478bd9Sstevel@tonic-gate 	/*
16447c478bd9Sstevel@tonic-gate 	 * At this point the configuration provider has been initialized,
16457c478bd9Sstevel@tonic-gate 	 * mark the configuration as valid so that the various routines
16467c478bd9Sstevel@tonic-gate 	 * which rely on a valid configuration will work correctly.
16477c478bd9Sstevel@tonic-gate 	 */
16487c478bd9Sstevel@tonic-gate 	conf->pc_state = POF_VALID;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	if ((oflags & PO_CREAT) != 0) {
16517c478bd9Sstevel@tonic-gate 		pool_conf_t *dyn;
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 		if ((dyn = pool_conf_alloc()) == NULL)
16547c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 		if (pool_conf_open(dyn, pool_dynamic_location(),
16577c478bd9Sstevel@tonic-gate 		    PO_RDONLY) != PO_SUCCESS) {
16587c478bd9Sstevel@tonic-gate 			pool_conf_free(dyn);
16597c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
16607c478bd9Sstevel@tonic-gate 		}
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 		if (pool_conf_export(dyn, conf->pc_location,
16637c478bd9Sstevel@tonic-gate 		    POX_NATIVE) != PO_SUCCESS) {
16647c478bd9Sstevel@tonic-gate 			(void) pool_conf_close(dyn);
16657c478bd9Sstevel@tonic-gate 			pool_conf_free(dyn);
16667c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
16677c478bd9Sstevel@tonic-gate 		}
16687c478bd9Sstevel@tonic-gate 		(void) pool_conf_close(dyn);
16697c478bd9Sstevel@tonic-gate 		pool_conf_free(dyn);
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	if (pool_xml_open_file(conf) == PO_FAIL) {
16737c478bd9Sstevel@tonic-gate 		(void) pool_xml_close(conf);
16747c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
16757c478bd9Sstevel@tonic-gate 	}
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
16787c478bd9Sstevel@tonic-gate }
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate /*
16817c478bd9Sstevel@tonic-gate  * Free the resources for an XML data provider.
16827c478bd9Sstevel@tonic-gate  */
16837c478bd9Sstevel@tonic-gate static void
pool_xml_connection_free(pool_xml_connection_t * prov)16847c478bd9Sstevel@tonic-gate pool_xml_connection_free(pool_xml_connection_t *prov)
16857c478bd9Sstevel@tonic-gate {
16867c478bd9Sstevel@tonic-gate 	free((void *)prov->pc_name);
16877c478bd9Sstevel@tonic-gate 	free(prov);
16887c478bd9Sstevel@tonic-gate }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate /*
16917c478bd9Sstevel@tonic-gate  * Allocate a result set. The Result Set stores the result of an XPath
16927c478bd9Sstevel@tonic-gate  * query along with the parameters used to create the result set (for
16937c478bd9Sstevel@tonic-gate  * debugging purposes).
16947c478bd9Sstevel@tonic-gate  * Returns pool_xml_result_set_t pointer/NULL
16957c478bd9Sstevel@tonic-gate  */
16967c478bd9Sstevel@tonic-gate static pool_xml_result_set_t *
pool_xml_result_set_alloc(const pool_conf_t * conf)16977c478bd9Sstevel@tonic-gate pool_xml_result_set_alloc(const pool_conf_t *conf)
16987c478bd9Sstevel@tonic-gate {
16997c478bd9Sstevel@tonic-gate 	pool_xml_result_set_t *rs;
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	if ((rs = malloc(sizeof (pool_xml_result_set_t))) == NULL) {
17027c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
17037c478bd9Sstevel@tonic-gate 		return (NULL);
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 	(void) memset(rs, 0, sizeof (pool_xml_result_set_t));
17067c478bd9Sstevel@tonic-gate 	rs->prs_conf = conf;
17077c478bd9Sstevel@tonic-gate 	rs->prs_index = -1;
17087c478bd9Sstevel@tonic-gate 	rs->prs_active = PO_TRUE;
17097c478bd9Sstevel@tonic-gate 	/* Fix up the result set accessor functions to the xml specfic ones */
17107c478bd9Sstevel@tonic-gate 	rs->prs_next = pool_xml_rs_next;
17117c478bd9Sstevel@tonic-gate 	rs->prs_prev = pool_xml_rs_prev;
17127c478bd9Sstevel@tonic-gate 	rs->prs_first = pool_xml_rs_first;
17137c478bd9Sstevel@tonic-gate 	rs->prs_last = pool_xml_rs_last;
17147c478bd9Sstevel@tonic-gate 	rs->prs_get_index = pool_xml_rs_get_index;
17157c478bd9Sstevel@tonic-gate 	rs->prs_set_index = pool_xml_rs_set_index;
17167c478bd9Sstevel@tonic-gate 	rs->prs_close = pool_xml_rs_close;
17177c478bd9Sstevel@tonic-gate 	rs->prs_count = pool_xml_rs_count;
17187c478bd9Sstevel@tonic-gate 	return (rs);
17197c478bd9Sstevel@tonic-gate }
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate /*
17227c478bd9Sstevel@tonic-gate  * Free a result set. Ensure that the resources are all released at this point.
17237c478bd9Sstevel@tonic-gate  */
17247c478bd9Sstevel@tonic-gate static void
pool_xml_result_set_free(pool_xml_result_set_t * rs)17257c478bd9Sstevel@tonic-gate pool_xml_result_set_free(pool_xml_result_set_t *rs)
17267c478bd9Sstevel@tonic-gate {
17277c478bd9Sstevel@tonic-gate 	if (rs->pxr_path != NULL)
17287c478bd9Sstevel@tonic-gate 		xmlXPathFreeObject(rs->pxr_path);
17297c478bd9Sstevel@tonic-gate 	if (rs->pxr_ctx != NULL)
17307c478bd9Sstevel@tonic-gate 		xmlXPathFreeContext(rs->pxr_ctx);
17317c478bd9Sstevel@tonic-gate 	free(rs);
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate /*
17357c478bd9Sstevel@tonic-gate  * Transfer size from one resource to another.
17367c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
17377c478bd9Sstevel@tonic-gate  */
17387c478bd9Sstevel@tonic-gate /* ARGSUSED */
17397c478bd9Sstevel@tonic-gate int
pool_xml_res_transfer(pool_resource_t * src,pool_resource_t * tgt,uint64_t size)17407c478bd9Sstevel@tonic-gate pool_xml_res_transfer(pool_resource_t *src, pool_resource_t *tgt, uint64_t size)
17417c478bd9Sstevel@tonic-gate {
17427c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
17437c478bd9Sstevel@tonic-gate }
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate /*
17467c478bd9Sstevel@tonic-gate  * Transfer components rl from one resource to another.
17477c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
17487c478bd9Sstevel@tonic-gate  */
17497c478bd9Sstevel@tonic-gate /* ARGSUSED */
17507c478bd9Sstevel@tonic-gate int
pool_xml_res_xtransfer(pool_resource_t * src,pool_resource_t * tgt,pool_component_t ** rl)17517c478bd9Sstevel@tonic-gate pool_xml_res_xtransfer(pool_resource_t *src, pool_resource_t *tgt,
17527c478bd9Sstevel@tonic-gate     pool_component_t **rl) {
17537c478bd9Sstevel@tonic-gate 	int i;
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	/*
17567c478bd9Sstevel@tonic-gate 	 * Walk the Result Set and move the resource components
17577c478bd9Sstevel@tonic-gate 	 */
17587c478bd9Sstevel@tonic-gate 	for (i = 0; rl[i] != NULL; i++) {
17597c478bd9Sstevel@tonic-gate 		if (pool_set_container(TO_ELEM(tgt), TO_ELEM(rl[i])) ==
17607c478bd9Sstevel@tonic-gate 		    PO_FAIL) {
17617c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
17627c478bd9Sstevel@tonic-gate 		}
17637c478bd9Sstevel@tonic-gate 	}
17647c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
17657c478bd9Sstevel@tonic-gate }
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate /*
17687c478bd9Sstevel@tonic-gate  * Return the next element in a result set.
17697c478bd9Sstevel@tonic-gate  * Returns pool_elem_t pointer/NULL
17707c478bd9Sstevel@tonic-gate  */
17717c478bd9Sstevel@tonic-gate static pool_elem_t *
pool_xml_rs_next(pool_result_set_t * set)17727c478bd9Sstevel@tonic-gate pool_xml_rs_next(pool_result_set_t *set)
17737c478bd9Sstevel@tonic-gate {
17747c478bd9Sstevel@tonic-gate 	pool_elem_t *next;
17757c478bd9Sstevel@tonic-gate 	/* Since I know this is an XML result set */
17767c478bd9Sstevel@tonic-gate 	pool_xml_result_set_t *xset = (pool_xml_result_set_t *)set;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	/* Update the context node */
17797c478bd9Sstevel@tonic-gate 	if (xset->prs_index == xset->pxr_path->nodesetval->nodeNr - 1)
17807c478bd9Sstevel@tonic-gate 		return (NULL);
17817c478bd9Sstevel@tonic-gate 	next =
17827c478bd9Sstevel@tonic-gate 	    xset->pxr_path->nodesetval->nodeTab[++xset->prs_index]->_private;
17837c478bd9Sstevel@tonic-gate 	return (next);
17847c478bd9Sstevel@tonic-gate }
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate /*
17877c478bd9Sstevel@tonic-gate  * Return the previous element in a result set.
17887c478bd9Sstevel@tonic-gate  * Returns pool_elem_t pointer/NULL
17897c478bd9Sstevel@tonic-gate  */
17907c478bd9Sstevel@tonic-gate static pool_elem_t *
pool_xml_rs_prev(pool_result_set_t * set)17917c478bd9Sstevel@tonic-gate pool_xml_rs_prev(pool_result_set_t *set)
17927c478bd9Sstevel@tonic-gate {
17937c478bd9Sstevel@tonic-gate 	pool_elem_t *prev;
17947c478bd9Sstevel@tonic-gate 	/* Since I know this is an XML result set */
17957c478bd9Sstevel@tonic-gate 	pool_xml_result_set_t *xset = (pool_xml_result_set_t *)set;
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	/* Update the context node */
17987c478bd9Sstevel@tonic-gate 	if (xset->prs_index < 0)
17997c478bd9Sstevel@tonic-gate 		return (NULL);
18007c478bd9Sstevel@tonic-gate 	prev =
18017c478bd9Sstevel@tonic-gate 	    xset->pxr_path->nodesetval->nodeTab[xset->prs_index--]->_private;
18027c478bd9Sstevel@tonic-gate 	return (prev);
18037c478bd9Sstevel@tonic-gate }
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate /*
18067c478bd9Sstevel@tonic-gate  * Sets the current index in a result set.
18077c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
18087c478bd9Sstevel@tonic-gate  */
18097c478bd9Sstevel@tonic-gate static int
pool_xml_rs_set_index(pool_result_set_t * set,int index)18107c478bd9Sstevel@tonic-gate pool_xml_rs_set_index(pool_result_set_t *set, int index)
18117c478bd9Sstevel@tonic-gate {
18127c478bd9Sstevel@tonic-gate 	/* Since I know this is an XML result set */
18137c478bd9Sstevel@tonic-gate 	pool_xml_result_set_t *xset = (pool_xml_result_set_t *)set;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	if (index < 0 || index >= xset->pxr_path->nodesetval->nodeNr) {
18167c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
18177c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 	xset->prs_index = index;
18207c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
18217c478bd9Sstevel@tonic-gate }
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate /*
18247c478bd9Sstevel@tonic-gate  * Return the current index in a result set.
18257c478bd9Sstevel@tonic-gate  * Returns current index
18267c478bd9Sstevel@tonic-gate  */
18277c478bd9Sstevel@tonic-gate static int
pool_xml_rs_get_index(pool_result_set_t * set)18287c478bd9Sstevel@tonic-gate pool_xml_rs_get_index(pool_result_set_t *set)
18297c478bd9Sstevel@tonic-gate {
18307c478bd9Sstevel@tonic-gate 	/* Since I know this is an XML result set */
18317c478bd9Sstevel@tonic-gate 	pool_xml_result_set_t *xset = (pool_xml_result_set_t *)set;
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	return (xset->prs_index);
18347c478bd9Sstevel@tonic-gate }
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate /*
18377c478bd9Sstevel@tonic-gate  * Return the first element in a result set.
18387c478bd9Sstevel@tonic-gate  * Returns pool_elem_t pointer/NULL
18397c478bd9Sstevel@tonic-gate  */
18407c478bd9Sstevel@tonic-gate static pool_elem_t *
pool_xml_rs_first(pool_result_set_t * set)18417c478bd9Sstevel@tonic-gate pool_xml_rs_first(pool_result_set_t *set)
18427c478bd9Sstevel@tonic-gate {
18437c478bd9Sstevel@tonic-gate 	/* Since I know this is an XML result set */
18447c478bd9Sstevel@tonic-gate 	pool_xml_result_set_t *xset = (pool_xml_result_set_t *)set;
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	/* Update the context node */
18477c478bd9Sstevel@tonic-gate 	return (xset->pxr_path->nodesetval->nodeTab[0]->_private);
18487c478bd9Sstevel@tonic-gate }
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate /*
18517c478bd9Sstevel@tonic-gate  * Return the last element in a result set.
18527c478bd9Sstevel@tonic-gate  * Returns pool_elem_t pointer/NULL
18537c478bd9Sstevel@tonic-gate  */
18547c478bd9Sstevel@tonic-gate static pool_elem_t *
pool_xml_rs_last(pool_result_set_t * set)18557c478bd9Sstevel@tonic-gate pool_xml_rs_last(pool_result_set_t *set)
18567c478bd9Sstevel@tonic-gate {
18577c478bd9Sstevel@tonic-gate 	/* Since I know this is an XML result set */
18587c478bd9Sstevel@tonic-gate 	pool_xml_result_set_t *xset = (pool_xml_result_set_t *)set;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	/* Update the context node */
18617c478bd9Sstevel@tonic-gate 	return (xset->pxr_path->nodesetval->
18627c478bd9Sstevel@tonic-gate 	    nodeTab[xset->pxr_path->nodesetval->nodeNr-1]->_private);
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate /*
18667c478bd9Sstevel@tonic-gate  * Return the number of results in a result set.
18677c478bd9Sstevel@tonic-gate  * Returns result count
18687c478bd9Sstevel@tonic-gate  */
18697c478bd9Sstevel@tonic-gate static int
pool_xml_rs_count(pool_result_set_t * set)18707c478bd9Sstevel@tonic-gate pool_xml_rs_count(pool_result_set_t *set)
18717c478bd9Sstevel@tonic-gate {
18727c478bd9Sstevel@tonic-gate 	pool_xml_result_set_t *xset = (pool_xml_result_set_t *)set;
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	return (xset->pxr_path->nodesetval->nodeNr);
18757c478bd9Sstevel@tonic-gate }
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate /*
18797c478bd9Sstevel@tonic-gate  * Close a result set. Remove this result set from the list of results and
18807c478bd9Sstevel@tonic-gate  * free the resources
18817c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
18827c478bd9Sstevel@tonic-gate  */
18837c478bd9Sstevel@tonic-gate static int
pool_xml_rs_close(pool_result_set_t * set)18847c478bd9Sstevel@tonic-gate pool_xml_rs_close(pool_result_set_t *set)
18857c478bd9Sstevel@tonic-gate {
18867c478bd9Sstevel@tonic-gate 	pool_xml_result_set_t *xset = (pool_xml_result_set_t *)set;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	pool_xml_result_set_free(xset);
18897c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
18907c478bd9Sstevel@tonic-gate }
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate /*
18937c478bd9Sstevel@tonic-gate  * Set the container for a node.
18947c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
18957c478bd9Sstevel@tonic-gate  */
18967c478bd9Sstevel@tonic-gate static int
pool_xml_set_container(pool_elem_t * pp,pool_elem_t * pc)18977c478bd9Sstevel@tonic-gate pool_xml_set_container(pool_elem_t *pp, pool_elem_t *pc)
18987c478bd9Sstevel@tonic-gate {
18997c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *pxp;
19007c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *pxc;
19017c478bd9Sstevel@tonic-gate 	xmlNodePtr parent;
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	pxp = (pool_xml_elem_t *)pp;
19047c478bd9Sstevel@tonic-gate 	pxc = (pool_xml_elem_t *)pc;
19057c478bd9Sstevel@tonic-gate 	parent = pxc->pxe_node->parent;
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	xmlUnlinkNode(pxc->pxe_node);
19087c478bd9Sstevel@tonic-gate 	if (xmlAddChild(pxp->pxe_node, pxc->pxe_node) == NULL) {
1909*5ad42b1bSSurya Prakki 		/* Try to move back */
1910*5ad42b1bSSurya Prakki 		(void) xmlAddChild(parent, pxc->pxe_node);
19117c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
19127c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
19137c478bd9Sstevel@tonic-gate 	}
19147c478bd9Sstevel@tonic-gate 	pc->pe_conf = pp->pe_conf;
19157c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate /*
19187c478bd9Sstevel@tonic-gate  * Get the container for a node.
19197c478bd9Sstevel@tonic-gate  * Returns Container/NULL
19207c478bd9Sstevel@tonic-gate  */
19217c478bd9Sstevel@tonic-gate static pool_elem_t *
pool_xml_get_container(const pool_elem_t * pc)19227c478bd9Sstevel@tonic-gate pool_xml_get_container(const pool_elem_t *pc)
19237c478bd9Sstevel@tonic-gate {
19247c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *pxc = (pool_xml_elem_t *)pc;
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	return ((pool_elem_t *)pxc->pxe_node->parent->_private);
19277c478bd9Sstevel@tonic-gate }
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate /*
19307c478bd9Sstevel@tonic-gate  * Note: This function is resource specific, needs extending for other
19317c478bd9Sstevel@tonic-gate  * resource types.
19327c478bd9Sstevel@tonic-gate  */
19337c478bd9Sstevel@tonic-gate int
pool_xml_resource_is_system(const pool_resource_t * pr)19347c478bd9Sstevel@tonic-gate pool_xml_resource_is_system(const pool_resource_t *pr)
19357c478bd9Sstevel@tonic-gate {
19367c478bd9Sstevel@tonic-gate 	switch (pool_resource_elem_class(TO_ELEM(pr))) {
19377c478bd9Sstevel@tonic-gate 	case PREC_PSET:
19387c478bd9Sstevel@tonic-gate 		return (PSID_IS_SYSSET(
19397c478bd9Sstevel@tonic-gate 		    elem_get_sysid(TO_ELEM(pr))));
19407c478bd9Sstevel@tonic-gate 	default:
19417c478bd9Sstevel@tonic-gate 		return (PO_FALSE);
19427c478bd9Sstevel@tonic-gate 	}
19437c478bd9Sstevel@tonic-gate }
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate /*
19467c478bd9Sstevel@tonic-gate  * Note: This function is resource specific, needs extending for other
19477c478bd9Sstevel@tonic-gate  * resource types.
19487c478bd9Sstevel@tonic-gate  */
19497c478bd9Sstevel@tonic-gate int
pool_xml_resource_can_associate(const pool_resource_t * pr)19507c478bd9Sstevel@tonic-gate pool_xml_resource_can_associate(const pool_resource_t *pr)
19517c478bd9Sstevel@tonic-gate {
19527c478bd9Sstevel@tonic-gate 	switch (pool_resource_elem_class(TO_ELEM(pr))) {
19537c478bd9Sstevel@tonic-gate 	case PREC_PSET:
19547c478bd9Sstevel@tonic-gate 		return (PO_TRUE);
19557c478bd9Sstevel@tonic-gate 	default:
19567c478bd9Sstevel@tonic-gate 		return (PO_FALSE);
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate }
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate /*
19617c478bd9Sstevel@tonic-gate  * Note: This function is resource specific. It must be extended to support
19627c478bd9Sstevel@tonic-gate  * multiple resource types.
19637c478bd9Sstevel@tonic-gate  */
19647c478bd9Sstevel@tonic-gate int
pool_xml_pool_associate(pool_t * pool,const pool_resource_t * pr)19657c478bd9Sstevel@tonic-gate pool_xml_pool_associate(pool_t *pool, const pool_resource_t *pr)
19667c478bd9Sstevel@tonic-gate {
19677c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	if (pool_xml_get_property(TO_ELEM(pr),
19707c478bd9Sstevel@tonic-gate 	    "pset.ref_id", &val) != POC_STRING)
19717c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
19727c478bd9Sstevel@tonic-gate 	if (pool_xml_put_property(TO_ELEM(pool), "pool.res", &val) !=
19737c478bd9Sstevel@tonic-gate 	    PO_SUCCESS)
19747c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
19757c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
19767c478bd9Sstevel@tonic-gate }
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate /*
19797c478bd9Sstevel@tonic-gate  * pool_xml_pool_dissociate() simply finds the default resource for
19807c478bd9Sstevel@tonic-gate  * the type of resource being dissociated and then calls
19817c478bd9Sstevel@tonic-gate  * pool_xml_pool_associate() to associate to the default resource.
19827c478bd9Sstevel@tonic-gate  */
19837c478bd9Sstevel@tonic-gate int
pool_xml_pool_dissociate(pool_t * pool,const pool_resource_t * pr)19847c478bd9Sstevel@tonic-gate pool_xml_pool_dissociate(pool_t *pool, const pool_resource_t *pr)
19857c478bd9Sstevel@tonic-gate {
19867c478bd9Sstevel@tonic-gate 	const pool_resource_t *default_res;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	if ((default_res = get_default_resource(pr)) == NULL)
19897c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
19907c478bd9Sstevel@tonic-gate 	if (default_res == pr)
19917c478bd9Sstevel@tonic-gate 		return (PO_SUCCESS);
19927c478bd9Sstevel@tonic-gate 	return (pool_xml_pool_associate(pool, default_res));
19937c478bd9Sstevel@tonic-gate }
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate /*
19967c478bd9Sstevel@tonic-gate  * pool_xml_open_file() opens a file for a configuration. This establishes
19977c478bd9Sstevel@tonic-gate  * the locks required to ensure data integrity when manipulating a
19987c478bd9Sstevel@tonic-gate  * configuration.
19997c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
20007c478bd9Sstevel@tonic-gate  */
20017c478bd9Sstevel@tonic-gate static int
pool_xml_open_file(pool_conf_t * conf)20027c478bd9Sstevel@tonic-gate pool_xml_open_file(pool_conf_t *conf)
20037c478bd9Sstevel@tonic-gate {
20047c478bd9Sstevel@tonic-gate 	struct flock lock;
20057c478bd9Sstevel@tonic-gate 	struct stat s;
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	pool_xml_connection_t *prov = (pool_xml_connection_t *)conf->pc_prov;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	/*
20107c478bd9Sstevel@tonic-gate 	 * Always close the pxc_file in case there was a previously failed open
20117c478bd9Sstevel@tonic-gate 	 */
20127c478bd9Sstevel@tonic-gate 	if (prov->pxc_file != NULL) {
20137c478bd9Sstevel@tonic-gate 		(void) fclose(prov->pxc_file);
20147c478bd9Sstevel@tonic-gate 		prov->pxc_file = NULL;
20157c478bd9Sstevel@tonic-gate 	}
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	/*
20187c478bd9Sstevel@tonic-gate 	 * Check that the DTD required for this operation is present.
20197c478bd9Sstevel@tonic-gate 	 * If it isn't fail
20207c478bd9Sstevel@tonic-gate 	 */
20217c478bd9Sstevel@tonic-gate 	if (dtd_exists(dtd_location) == PO_FALSE) {
20227c478bd9Sstevel@tonic-gate 		pool_seterror(POE_DATASTORE);
20237c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
20247c478bd9Sstevel@tonic-gate 	}
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	if ((prov->pc_oflags & PO_RDWR) != 0)
2027004388ebScasper 		prov->pxc_file = fopen(conf->pc_location, "r+F");
20287c478bd9Sstevel@tonic-gate 	else /* Assume opening PO_RDONLY */
2029004388ebScasper 		prov->pxc_file = fopen(conf->pc_location, "rF");
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	if (prov->pxc_file == NULL) {
20327c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
20337c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
20347c478bd9Sstevel@tonic-gate 	}
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	/*
20377c478bd9Sstevel@tonic-gate 	 * Setup the lock for the file
20387c478bd9Sstevel@tonic-gate 	 */
20397c478bd9Sstevel@tonic-gate 	lock.l_type = (prov->pc_oflags & PO_RDWR) ? F_WRLCK : F_RDLCK;
20407c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
20417c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
20427c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
20437c478bd9Sstevel@tonic-gate 	if (fcntl(fileno(prov->pxc_file), F_SETLKW, &lock) == -1) {
20447c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
20457c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
20467c478bd9Sstevel@tonic-gate 	}
20477c478bd9Sstevel@tonic-gate 	/*
20487c478bd9Sstevel@tonic-gate 	 * Check to see if the document was removed whilst waiting for
20497c478bd9Sstevel@tonic-gate 	 * the lock. If it was return an error.
20507c478bd9Sstevel@tonic-gate 	 */
20517c478bd9Sstevel@tonic-gate 	if (stat(conf->pc_location, &s) == -1) {
20527c478bd9Sstevel@tonic-gate 		(void) fclose(prov->pxc_file);
20537c478bd9Sstevel@tonic-gate 		prov->pxc_file = NULL;
20547c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
20557c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
20567c478bd9Sstevel@tonic-gate 	}
20577c478bd9Sstevel@tonic-gate 	/* Parse the document */
20587c478bd9Sstevel@tonic-gate 	if (pool_xml_parse_document(conf) != PO_SUCCESS)
20597c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
20607c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
20617c478bd9Sstevel@tonic-gate }
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate /*
20647c478bd9Sstevel@tonic-gate  * Try to work out if an element contains an attribute of the supplied name.
20657c478bd9Sstevel@tonic-gate  * Search the internal subset first and then the external subset.
20667c478bd9Sstevel@tonic-gate  * Return PO_TRUE if there is an attribute of that name declared for that
20677c478bd9Sstevel@tonic-gate  * element.
20687c478bd9Sstevel@tonic-gate  */
20697c478bd9Sstevel@tonic-gate int
pool_is_xml_attr(xmlDocPtr doc,const char * elem,const char * attr)20707c478bd9Sstevel@tonic-gate pool_is_xml_attr(xmlDocPtr doc, const char *elem, const char *attr)
20717c478bd9Sstevel@tonic-gate {
20727c478bd9Sstevel@tonic-gate 	xmlDtdPtr internal = xmlGetIntSubset(doc);
20737c478bd9Sstevel@tonic-gate 	xmlDtdPtr external = doc->extSubset;
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	if (xmlGetDtdAttrDesc(internal, BAD_CAST elem, BAD_CAST attr) == NULL)
20767c478bd9Sstevel@tonic-gate 		if (xmlGetDtdAttrDesc(external,
20777c478bd9Sstevel@tonic-gate 		    BAD_CAST elem, BAD_CAST attr) == NULL)
20787c478bd9Sstevel@tonic-gate 			return (PO_FALSE);
20797c478bd9Sstevel@tonic-gate 	return (PO_TRUE);
20807c478bd9Sstevel@tonic-gate }
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate /*
20837c478bd9Sstevel@tonic-gate  * Execute the specified query using XPath. This complex function relies on
20847c478bd9Sstevel@tonic-gate  * a couple of helpers to build up an XPath query, pool_build_xpath_buf in
20857c478bd9Sstevel@tonic-gate  * particular.
20867c478bd9Sstevel@tonic-gate  * conf - the pool configuration being manipulated
20877c478bd9Sstevel@tonic-gate  * src - the root of the search, if NULL that means whole document
20887c478bd9Sstevel@tonic-gate  * src_attr - if supplied means an IDREF(S) search on this attribute
20897c478bd9Sstevel@tonic-gate  * classes - target classes
20907c478bd9Sstevel@tonic-gate  * props - target properties
20917c478bd9Sstevel@tonic-gate  * Returns pool_result_set_t pointer/NULL
20927c478bd9Sstevel@tonic-gate  */
20937c478bd9Sstevel@tonic-gate pool_result_set_t *
pool_xml_exec_query(const pool_conf_t * conf,const pool_elem_t * src,const char * src_attr,pool_elem_class_t classes,pool_value_t ** props)20947c478bd9Sstevel@tonic-gate pool_xml_exec_query(const pool_conf_t *conf, const pool_elem_t *src,
20957c478bd9Sstevel@tonic-gate     const char *src_attr, pool_elem_class_t classes, pool_value_t **props)
20967c478bd9Sstevel@tonic-gate {
20977c478bd9Sstevel@tonic-gate 	char *buf = NULL;
20987c478bd9Sstevel@tonic-gate 	char_buf_t *cb = NULL;
20997c478bd9Sstevel@tonic-gate 	pool_xml_result_set_t *rs;
21007c478bd9Sstevel@tonic-gate 	pool_xml_elem_t *pxe = (pool_xml_elem_t *)src;
21017c478bd9Sstevel@tonic-gate 	pool_xml_connection_t *prov = (pool_xml_connection_t *)conf->pc_prov;
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	if ((cb = alloc_char_buf(CB_DEFAULT_LEN)) == NULL)
21047c478bd9Sstevel@tonic-gate 		return (NULL);
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	/*
21077c478bd9Sstevel@tonic-gate 	 * Prior to building up the complex XPath query, check to see if
21087c478bd9Sstevel@tonic-gate 	 * src_attr is an IDREF(S). If it is use the IDREF(S) information
21097c478bd9Sstevel@tonic-gate 	 * to generate the query rather than the other data
21107c478bd9Sstevel@tonic-gate 	 */
21117c478bd9Sstevel@tonic-gate 	if (src_attr != NULL) {
21127c478bd9Sstevel@tonic-gate 		char *tok;
21137c478bd9Sstevel@tonic-gate 		char *lasts;
21147c478bd9Sstevel@tonic-gate 		char *or = "";
21157c478bd9Sstevel@tonic-gate 		xmlChar *id;
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 		/*
21187c478bd9Sstevel@tonic-gate 		 * Check the arguments for consistency
21197c478bd9Sstevel@tonic-gate 		 */
21207c478bd9Sstevel@tonic-gate 		if (pool_is_xml_attr(prov->pxc_doc,
21217c478bd9Sstevel@tonic-gate 		    element_class_tags[src->pe_class], src_attr) != PO_TRUE) {
21227c478bd9Sstevel@tonic-gate 			free_char_buf(cb);
21237c478bd9Sstevel@tonic-gate 			pool_seterror(POE_BADPARAM);
21247c478bd9Sstevel@tonic-gate 			return (NULL);
21257c478bd9Sstevel@tonic-gate 		}
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 		if ((id = xmlGetProp(pxe->pxe_node, BAD_CAST src_attr))
21287c478bd9Sstevel@tonic-gate 		    == NULL) {
21297c478bd9Sstevel@tonic-gate 			free_char_buf(cb);
21307c478bd9Sstevel@tonic-gate 			pool_seterror(POE_DATASTORE);
21317c478bd9Sstevel@tonic-gate 			return (NULL);
21327c478bd9Sstevel@tonic-gate 		}
21337c478bd9Sstevel@tonic-gate 		for (tok = strtok_r((char *)id, "	 ", &lasts);
21347c478bd9Sstevel@tonic-gate 		    tok != NULL; tok = strtok_r(NULL, "	 ", &lasts)) {
21357c478bd9Sstevel@tonic-gate 			(void) append_char_buf(cb, "%s//*[@ref_id=\"%s\"]",
21367c478bd9Sstevel@tonic-gate 			    or, tok);
21377c478bd9Sstevel@tonic-gate 			or = " | ";
21387c478bd9Sstevel@tonic-gate 			if ((classes & PEC_QRY_SYSTEM) != 0) {
21397c478bd9Sstevel@tonic-gate 				if (pool_build_xpath_buf(prov, src, PEC_SYSTEM,
21407c478bd9Sstevel@tonic-gate 				    props, cb, PO_TRUE) == PO_FAIL) {
21417c478bd9Sstevel@tonic-gate 					free_char_buf(cb);
21427c478bd9Sstevel@tonic-gate 					return (NULL);
21437c478bd9Sstevel@tonic-gate 				}
21447c478bd9Sstevel@tonic-gate 			}
21457c478bd9Sstevel@tonic-gate 			if ((classes & PEC_QRY_POOL) != 0) {
21467c478bd9Sstevel@tonic-gate 				if (pool_build_xpath_buf(prov, src, PEC_POOL,
21477c478bd9Sstevel@tonic-gate 				    props, cb, PO_TRUE) == PO_FAIL) {
21487c478bd9Sstevel@tonic-gate 					free_char_buf(cb);
21497c478bd9Sstevel@tonic-gate 					return (NULL);
21507c478bd9Sstevel@tonic-gate 				}
21517c478bd9Sstevel@tonic-gate 			}
21527c478bd9Sstevel@tonic-gate 			if ((classes & PEC_QRY_RES_COMP) != 0) {
21537c478bd9Sstevel@tonic-gate 				if (pool_build_xpath_buf(prov, src,
21547c478bd9Sstevel@tonic-gate 				    PEC_RES_COMP, props, cb, PO_TRUE)
21557c478bd9Sstevel@tonic-gate 				    == PO_FAIL) {
21567c478bd9Sstevel@tonic-gate 					free_char_buf(cb);
21577c478bd9Sstevel@tonic-gate 					return (NULL);
21587c478bd9Sstevel@tonic-gate 				}
21597c478bd9Sstevel@tonic-gate 			} else if ((classes & PEC_QRY_RES_AGG) != 0) {
21607c478bd9Sstevel@tonic-gate 				if (pool_build_xpath_buf(prov, src,
21617c478bd9Sstevel@tonic-gate 				    PEC_RES_AGG, props, cb, PO_TRUE)
21627c478bd9Sstevel@tonic-gate 				    == PO_FAIL) {
21637c478bd9Sstevel@tonic-gate 					free_char_buf(cb);
21647c478bd9Sstevel@tonic-gate 					return (NULL);
21657c478bd9Sstevel@tonic-gate 				}
21667c478bd9Sstevel@tonic-gate 			}
21677c478bd9Sstevel@tonic-gate 		}
21687c478bd9Sstevel@tonic-gate 		xmlFree(id);
21697c478bd9Sstevel@tonic-gate 	} else {
21707c478bd9Sstevel@tonic-gate 		/*
21717c478bd9Sstevel@tonic-gate 		 * Build up an XPath query using the supplied parameters.
21727c478bd9Sstevel@tonic-gate 		 * The basic logic is to:
21737c478bd9Sstevel@tonic-gate 		 * - Identify which classes are the targets of the query
21747c478bd9Sstevel@tonic-gate 		 * - For each class work out if the props are attributes or not
21757c478bd9Sstevel@tonic-gate 		 * - Build up a piece of XPath for each class
21767c478bd9Sstevel@tonic-gate 		 * - Combine the results into one large XPath query.
21777c478bd9Sstevel@tonic-gate 		 * - Execute the query.
21787c478bd9Sstevel@tonic-gate 		 */
21797c478bd9Sstevel@tonic-gate 		if ((classes & PEC_QRY_SYSTEM) != 0) {
21807c478bd9Sstevel@tonic-gate 			if (pool_build_xpath_buf(prov, src, PEC_SYSTEM, props,
21817c478bd9Sstevel@tonic-gate 			    cb, PO_FALSE) == PO_FAIL) {
21827c478bd9Sstevel@tonic-gate 				free_char_buf(cb);
21837c478bd9Sstevel@tonic-gate 				return (NULL);
21847c478bd9Sstevel@tonic-gate 			}
21857c478bd9Sstevel@tonic-gate 		}
21867c478bd9Sstevel@tonic-gate 		if ((classes & PEC_QRY_POOL) != 0) {
21877c478bd9Sstevel@tonic-gate 			if (pool_build_xpath_buf(prov, src, PEC_POOL, props,
21887c478bd9Sstevel@tonic-gate 			    cb, PO_FALSE) == PO_FAIL) {
21897c478bd9Sstevel@tonic-gate 				free_char_buf(cb);
21907c478bd9Sstevel@tonic-gate 				return (NULL);
21917c478bd9Sstevel@tonic-gate 			}
21927c478bd9Sstevel@tonic-gate 		}
21937c478bd9Sstevel@tonic-gate 		if ((classes & PEC_QRY_RES_COMP) != 0) {
21947c478bd9Sstevel@tonic-gate 			if (pool_build_xpath_buf(prov, src, PEC_RES_COMP, props,
21957c478bd9Sstevel@tonic-gate 			    cb, PO_FALSE) == PO_FAIL) {
21967c478bd9Sstevel@tonic-gate 				free_char_buf(cb);
21977c478bd9Sstevel@tonic-gate 				return (NULL);
21987c478bd9Sstevel@tonic-gate 			}
21997c478bd9Sstevel@tonic-gate 		}
22007c478bd9Sstevel@tonic-gate 		if ((classes & PEC_QRY_RES_AGG) != 0) {
22017c478bd9Sstevel@tonic-gate 			if (pool_build_xpath_buf(prov, src, PEC_RES_AGG, props,
22027c478bd9Sstevel@tonic-gate 			    cb, PO_FALSE) == PO_FAIL) {
22037c478bd9Sstevel@tonic-gate 				free_char_buf(cb);
22047c478bd9Sstevel@tonic-gate 				return (NULL);
22057c478bd9Sstevel@tonic-gate 			}
22067c478bd9Sstevel@tonic-gate 		}
22077c478bd9Sstevel@tonic-gate 		if ((classes & PEC_QRY_COMP) != 0) {
22087c478bd9Sstevel@tonic-gate 			if (pool_build_xpath_buf(prov, src, PEC_COMP, props,
22097c478bd9Sstevel@tonic-gate 			    cb, PO_FALSE) == PO_FAIL) {
22107c478bd9Sstevel@tonic-gate 				free_char_buf(cb);
22117c478bd9Sstevel@tonic-gate 				return (NULL);
22127c478bd9Sstevel@tonic-gate 			}
22137c478bd9Sstevel@tonic-gate 		}
22147c478bd9Sstevel@tonic-gate 	}
22157c478bd9Sstevel@tonic-gate 	buf = strdup(cb->cb_buf);
22167c478bd9Sstevel@tonic-gate 	free_char_buf(cb);
22177c478bd9Sstevel@tonic-gate 	/*
22187c478bd9Sstevel@tonic-gate 	 * Have a buffer at this point, that we can use
22197c478bd9Sstevel@tonic-gate 	 */
22207c478bd9Sstevel@tonic-gate 	if ((rs = pool_xml_result_set_alloc(conf)) == NULL) {
22217c478bd9Sstevel@tonic-gate 		free(buf);
22227c478bd9Sstevel@tonic-gate 		return (NULL);
22237c478bd9Sstevel@tonic-gate 	}
22247c478bd9Sstevel@tonic-gate 	/*
22257c478bd9Sstevel@tonic-gate 	 * Set up the XPath Query
22267c478bd9Sstevel@tonic-gate 	 */
22277c478bd9Sstevel@tonic-gate 	if ((rs->pxr_ctx = xmlXPathNewContext(
22287c478bd9Sstevel@tonic-gate 	    ((pool_xml_connection_t *)conf->pc_prov)->pxc_doc)) == NULL) {
22297c478bd9Sstevel@tonic-gate 		free(buf);
22307c478bd9Sstevel@tonic-gate 		(void) pool_xml_rs_close((pool_result_set_t *)rs);
22317c478bd9Sstevel@tonic-gate 		pool_seterror(POE_DATASTORE);
22327c478bd9Sstevel@tonic-gate 		return (NULL);
22337c478bd9Sstevel@tonic-gate 	}
22347c478bd9Sstevel@tonic-gate 	if (src == NULL)
22357c478bd9Sstevel@tonic-gate 		rs->pxr_ctx->node = xmlDocGetRootElement
22367c478bd9Sstevel@tonic-gate 		    (((pool_xml_connection_t *)conf->pc_prov)->pxc_doc);
22377c478bd9Sstevel@tonic-gate 	else
22387c478bd9Sstevel@tonic-gate 		rs->pxr_ctx->node = pxe->pxe_node;
22397c478bd9Sstevel@tonic-gate 	/*
22407c478bd9Sstevel@tonic-gate 	 * Select
22417c478bd9Sstevel@tonic-gate 	 */
22427c478bd9Sstevel@tonic-gate 	rs->pxr_path = xmlXPathEval(BAD_CAST buf, rs->pxr_ctx);
22437c478bd9Sstevel@tonic-gate 	free(buf);
22447c478bd9Sstevel@tonic-gate 	/*
22457c478bd9Sstevel@tonic-gate 	 * Generate the result set and wrap the results as pool_elem_t
22467c478bd9Sstevel@tonic-gate 	 */
224726d8ba22Sgarypen 	if (rs->pxr_path->nodesetval->nodeNr == 0)
224826d8ba22Sgarypen 		pool_seterror(POE_INVALID_SEARCH);
22497c478bd9Sstevel@tonic-gate 	return ((pool_result_set_t *)rs);
22507c478bd9Sstevel@tonic-gate }
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate /*
22537c478bd9Sstevel@tonic-gate  * Build an XPath query buffer. This is complex and a little fragile, but
22547c478bd9Sstevel@tonic-gate  * I'm trying to accomplish something complex with as little code as possible.
22557c478bd9Sstevel@tonic-gate  * I wait the implementation of XMLQuery with baited breath...
22567c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
22577c478bd9Sstevel@tonic-gate  */
22587c478bd9Sstevel@tonic-gate static int
pool_build_xpath_buf(pool_xml_connection_t * prov,const pool_elem_t * src,pool_elem_class_t class,pool_value_t * props[],char_buf_t * cb,int is_ref)22597c478bd9Sstevel@tonic-gate pool_build_xpath_buf(pool_xml_connection_t *prov, const pool_elem_t *src,
22607c478bd9Sstevel@tonic-gate     pool_elem_class_t class, pool_value_t *props[], char_buf_t *cb, int is_ref)
22617c478bd9Sstevel@tonic-gate {
22627c478bd9Sstevel@tonic-gate 	int i;
22637c478bd9Sstevel@tonic-gate 	const char *ATTR_FMTS[] = {
22647c478bd9Sstevel@tonic-gate 		"[ @%s=\"%llu\" ]",	/* POC_UINT */
22657c478bd9Sstevel@tonic-gate 		"[ @%s=\"%lld\" ]",	/* POC_INT */
22667c478bd9Sstevel@tonic-gate 		"[ @%s=\"%f\" ]",	/* POC_DOUBLE */
22677c478bd9Sstevel@tonic-gate 		"[ @%s=\"%s\" ]",	/* POC_BOOL */
22687c478bd9Sstevel@tonic-gate 		"[ @%s=\"%s\" ]",	/* POC_STRING */
22697c478bd9Sstevel@tonic-gate 	};
22707c478bd9Sstevel@tonic-gate 	const char *PROP_FMTS[] = {
22717c478bd9Sstevel@tonic-gate 		"[ property[@name=\"%s\"][text()=\"%llu\"] ]",	/* POC_UINT */
22727c478bd9Sstevel@tonic-gate 		"[ property[@name=\"%s\"][text()=\"%lld\"] ]",	/* POC_INT */
22737c478bd9Sstevel@tonic-gate 		"[ property[@name=\"%s\"][text()=\"%f\"] ]",	/* POC_DOUBLE */
22747c478bd9Sstevel@tonic-gate 		"[ property[@name=\"%s\"][text()=\"%s\"] ]",	/* POC_BOOL */
22757c478bd9Sstevel@tonic-gate 		"[ property[@name=\"%s\"][text()=\"%s\"] ]"	/* POC_STRING */
22767c478bd9Sstevel@tonic-gate 	};
22777c478bd9Sstevel@tonic-gate 	const char **fmts;
22787c478bd9Sstevel@tonic-gate 	int nprop;
22797c478bd9Sstevel@tonic-gate 	const char *last_prop_name = NULL;
22807c478bd9Sstevel@tonic-gate 	char *type_prefix = NULL;
22817c478bd9Sstevel@tonic-gate 	int has_type = PO_FALSE;
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	if (is_ref == PO_FALSE) {
22847c478bd9Sstevel@tonic-gate 		if (cb->cb_buf != NULL && strlen(cb->cb_buf) > 0)
22857c478bd9Sstevel@tonic-gate 			(void) append_char_buf(cb, " |");
22867c478bd9Sstevel@tonic-gate 		if (src != NULL)
22877c478bd9Sstevel@tonic-gate 			(void) append_char_buf(cb, " ./");
22887c478bd9Sstevel@tonic-gate 		else
22897c478bd9Sstevel@tonic-gate 			(void) append_char_buf(cb, "//");
22907c478bd9Sstevel@tonic-gate 		(void) append_char_buf(cb, element_class_tags[class]);
22917c478bd9Sstevel@tonic-gate 	}
22927c478bd9Sstevel@tonic-gate 	if (props == NULL || props[0] == NULL)
22937c478bd9Sstevel@tonic-gate 		return (PO_SUCCESS);
22947c478bd9Sstevel@tonic-gate 	for (nprop = 0; props[nprop] != NULL; nprop++)
22957c478bd9Sstevel@tonic-gate 		/* Count properties */;
22967c478bd9Sstevel@tonic-gate 	/*
22977c478bd9Sstevel@tonic-gate 	 * Sort the attributes and properties by name.
22987c478bd9Sstevel@tonic-gate 	 */
22997c478bd9Sstevel@tonic-gate 	qsort(props, nprop, sizeof (pool_value_t *), prop_sort);
23007c478bd9Sstevel@tonic-gate 	for (i = 0; i < nprop; i++) {
23017c478bd9Sstevel@tonic-gate 		int is_attr = 0;
23027c478bd9Sstevel@tonic-gate 		const char *prefix;
23037c478bd9Sstevel@tonic-gate 		const char *prop_name;
23047c478bd9Sstevel@tonic-gate 		uint64_t uval;
23057c478bd9Sstevel@tonic-gate 		int64_t ival;
23067c478bd9Sstevel@tonic-gate 		double dval;
23077c478bd9Sstevel@tonic-gate 		uchar_t bval;
23087c478bd9Sstevel@tonic-gate 		const char *sval;
23097c478bd9Sstevel@tonic-gate 		pool_value_class_t pvc;
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 		prop_name = pool_value_get_name(props[i]);
23127c478bd9Sstevel@tonic-gate 		if ((prefix = is_a_known_prefix(class, prop_name)) != NULL) {
23137c478bd9Sstevel@tonic-gate 			const char *attr_name;
23147c478bd9Sstevel@tonic-gate 			/*
23157c478bd9Sstevel@tonic-gate 			 * Possibly an attribute. Strip off the prefix.
23167c478bd9Sstevel@tonic-gate 			 */
23177c478bd9Sstevel@tonic-gate 			if (strcmp(prop_name, c_type) == 0) {
23187c478bd9Sstevel@tonic-gate 				has_type = PO_TRUE;
23197c478bd9Sstevel@tonic-gate 				attr_name = prop_name;
23207c478bd9Sstevel@tonic-gate 			} else
23217c478bd9Sstevel@tonic-gate 				attr_name = prop_name + strlen(prefix) + 1;
23227c478bd9Sstevel@tonic-gate 			if (pool_is_xml_attr(prov->pxc_doc,
23237c478bd9Sstevel@tonic-gate 			    element_class_tags[class], attr_name)) {
23247c478bd9Sstevel@tonic-gate 				is_attr = 1;
23257c478bd9Sstevel@tonic-gate 				prop_name = attr_name;
23267c478bd9Sstevel@tonic-gate 				if (class == PEC_RES_COMP ||
23277c478bd9Sstevel@tonic-gate 				    class == PEC_RES_AGG ||
23287c478bd9Sstevel@tonic-gate 				    class == PEC_COMP) {
23297c478bd9Sstevel@tonic-gate 					if (type_prefix != NULL)
23307c478bd9Sstevel@tonic-gate 						free(type_prefix);
23317c478bd9Sstevel@tonic-gate 					type_prefix = strdup(prefix);
23327c478bd9Sstevel@tonic-gate 				}
23337c478bd9Sstevel@tonic-gate 			}
23347c478bd9Sstevel@tonic-gate 		}
23357c478bd9Sstevel@tonic-gate 		if (is_attr)  {
23367c478bd9Sstevel@tonic-gate 			fmts = ATTR_FMTS;
23377c478bd9Sstevel@tonic-gate 		} else {
23387c478bd9Sstevel@tonic-gate 			fmts = PROP_FMTS;
23397c478bd9Sstevel@tonic-gate 		}
23407c478bd9Sstevel@tonic-gate 		/*
23417c478bd9Sstevel@tonic-gate 		 * Add attributes/properties to the search buffer
23427c478bd9Sstevel@tonic-gate 		 */
23437c478bd9Sstevel@tonic-gate 		switch ((pvc = pool_value_get_type(props[i]))) {
23447c478bd9Sstevel@tonic-gate 		case POC_UINT:
23457c478bd9Sstevel@tonic-gate 			(void) pool_value_get_uint64(props[i], &uval);
23467c478bd9Sstevel@tonic-gate 			if (append_char_buf(cb, fmts[pvc], prop_name, uval)
23477c478bd9Sstevel@tonic-gate 			    == PO_FAIL) {
23487c478bd9Sstevel@tonic-gate 				free(type_prefix);
23497c478bd9Sstevel@tonic-gate 				return (PO_FAIL);
23507c478bd9Sstevel@tonic-gate 			}
23517c478bd9Sstevel@tonic-gate 			break;
23527c478bd9Sstevel@tonic-gate 		case POC_INT:
23537c478bd9Sstevel@tonic-gate 			(void) pool_value_get_int64(props[i], &ival);
23547c478bd9Sstevel@tonic-gate 			if (append_char_buf(cb, fmts[pvc], prop_name, ival)
23557c478bd9Sstevel@tonic-gate 			    == PO_FAIL) {
23567c478bd9Sstevel@tonic-gate 				free(type_prefix);
23577c478bd9Sstevel@tonic-gate 				return (PO_FAIL);
23587c478bd9Sstevel@tonic-gate 			}
23597c478bd9Sstevel@tonic-gate 			break;
23607c478bd9Sstevel@tonic-gate 		case POC_DOUBLE:
23617c478bd9Sstevel@tonic-gate 			(void) pool_value_get_double(props[i], &dval);
23627c478bd9Sstevel@tonic-gate 			if (append_char_buf(cb, fmts[pvc], prop_name, dval)
23637c478bd9Sstevel@tonic-gate 			    == PO_FAIL) {
23647c478bd9Sstevel@tonic-gate 				free(type_prefix);
23657c478bd9Sstevel@tonic-gate 				return (PO_FAIL);
23667c478bd9Sstevel@tonic-gate 			}
23677c478bd9Sstevel@tonic-gate 			break;
23687c478bd9Sstevel@tonic-gate 		case POC_BOOL:
23697c478bd9Sstevel@tonic-gate 			(void) pool_value_get_bool(props[i], &bval);
23707c478bd9Sstevel@tonic-gate 			if (append_char_buf(cb, fmts[pvc], prop_name,
23717c478bd9Sstevel@tonic-gate 			    bval ? "true" : "false") == PO_FAIL) {
23727c478bd9Sstevel@tonic-gate 				free(type_prefix);
23737c478bd9Sstevel@tonic-gate 				return (PO_FAIL);
23747c478bd9Sstevel@tonic-gate 			}
23757c478bd9Sstevel@tonic-gate 			break;
23767c478bd9Sstevel@tonic-gate 		case POC_STRING:
23777c478bd9Sstevel@tonic-gate 			(void) pool_value_get_string(props[i], &sval);
23787c478bd9Sstevel@tonic-gate 			if (append_char_buf(cb, fmts[pvc], prop_name, sval)
23797c478bd9Sstevel@tonic-gate 			    == PO_FAIL) {
23807c478bd9Sstevel@tonic-gate 				free(type_prefix);
23817c478bd9Sstevel@tonic-gate 				return (PO_FAIL);
23827c478bd9Sstevel@tonic-gate 			}
23837c478bd9Sstevel@tonic-gate 			break;
23847c478bd9Sstevel@tonic-gate 		default:
23857c478bd9Sstevel@tonic-gate 			free(type_prefix);
238626d8ba22Sgarypen 			pool_seterror(POE_INVALID_SEARCH);
23877c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
23887c478bd9Sstevel@tonic-gate 		}
23897c478bd9Sstevel@tonic-gate 		if (last_prop_name != NULL) {
23907c478bd9Sstevel@tonic-gate 			const char *suffix1, *suffix2;
23917c478bd9Sstevel@tonic-gate 			/*
23927c478bd9Sstevel@tonic-gate 			 * Extra fiddling for namespaces
23937c478bd9Sstevel@tonic-gate 			 */
23947c478bd9Sstevel@tonic-gate 			suffix1 = strrchr(prop_name, '.');
23957c478bd9Sstevel@tonic-gate 			suffix2 = strrchr(last_prop_name, '.');
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate 			if (suffix1 != NULL || suffix2 != NULL) {
23987c478bd9Sstevel@tonic-gate 				if (suffix1 == NULL)
23997c478bd9Sstevel@tonic-gate 					suffix1 = prop_name;
24007c478bd9Sstevel@tonic-gate 				else
24017c478bd9Sstevel@tonic-gate 					suffix1++;
24027c478bd9Sstevel@tonic-gate 				if (suffix2 == NULL)
24037c478bd9Sstevel@tonic-gate 					suffix2 = last_prop_name;
24047c478bd9Sstevel@tonic-gate 				else
24057c478bd9Sstevel@tonic-gate 					suffix2++;
24067c478bd9Sstevel@tonic-gate 			} else {
24077c478bd9Sstevel@tonic-gate 				suffix1 = prop_name;
24087c478bd9Sstevel@tonic-gate 				suffix2 = last_prop_name;
24097c478bd9Sstevel@tonic-gate 			}
24107c478bd9Sstevel@tonic-gate 			if (strcmp(suffix1, suffix2) == 0) {
24117c478bd9Sstevel@tonic-gate 				char *where = strrchr(cb->cb_buf, '[');
24127c478bd9Sstevel@tonic-gate 				if (is_attr != PO_TRUE) {
24137c478bd9Sstevel@tonic-gate 					/* repeat */
2414*5ad42b1bSSurya Prakki 					while (*--where != '[')
2415*5ad42b1bSSurya Prakki 						;
2416*5ad42b1bSSurya Prakki 					while (*--where != '[')
2417*5ad42b1bSSurya Prakki 						;
24187c478bd9Sstevel@tonic-gate 				}
24197c478bd9Sstevel@tonic-gate 				*(where - 1) = 'o';
24207c478bd9Sstevel@tonic-gate 				*where = 'r';
24217c478bd9Sstevel@tonic-gate 			}
24227c478bd9Sstevel@tonic-gate 		}
24237c478bd9Sstevel@tonic-gate 		last_prop_name = prop_name;
24247c478bd9Sstevel@tonic-gate 	}
24257c478bd9Sstevel@tonic-gate 	if (has_type == PO_FALSE) {
24267c478bd9Sstevel@tonic-gate 		if (type_prefix) {
24277c478bd9Sstevel@tonic-gate 			if (append_char_buf(cb, ATTR_FMTS[POC_STRING],
24287c478bd9Sstevel@tonic-gate 			    c_type, type_prefix) == PO_FAIL) {
24297c478bd9Sstevel@tonic-gate 				free(type_prefix);
24307c478bd9Sstevel@tonic-gate 				return (PO_FAIL);
24317c478bd9Sstevel@tonic-gate 			}
24327c478bd9Sstevel@tonic-gate 		}
24337c478bd9Sstevel@tonic-gate 	}
24347c478bd9Sstevel@tonic-gate 	free(type_prefix);
24357c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
24367c478bd9Sstevel@tonic-gate }
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate /*
24397c478bd9Sstevel@tonic-gate  * Utility routine for use by quicksort. Assumes that the supplied data
24407c478bd9Sstevel@tonic-gate  * are pool values and compares the names of the two pool values.
24417c478bd9Sstevel@tonic-gate  * Returns an integer greater than, equal to, or less than 0.
24427c478bd9Sstevel@tonic-gate  */
24437c478bd9Sstevel@tonic-gate static int
prop_sort(const void * a,const void * b)24447c478bd9Sstevel@tonic-gate prop_sort(const void *a, const void *b)
24457c478bd9Sstevel@tonic-gate {
24467c478bd9Sstevel@tonic-gate 	pool_value_t **prop_a = (pool_value_t **)a;
24477c478bd9Sstevel@tonic-gate 	pool_value_t **prop_b = (pool_value_t **)b;
24487c478bd9Sstevel@tonic-gate 	const char *str_a;
24497c478bd9Sstevel@tonic-gate 	const char *str_b;
24507c478bd9Sstevel@tonic-gate 	const char *suffix1, *suffix2;
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	str_a = pool_value_get_name(*prop_a);
24537c478bd9Sstevel@tonic-gate 	str_b = pool_value_get_name(*prop_b);
24547c478bd9Sstevel@tonic-gate 	/*
24557c478bd9Sstevel@tonic-gate 	 * Extra fiddling for namespaces
24567c478bd9Sstevel@tonic-gate 	 */
24577c478bd9Sstevel@tonic-gate 	suffix1 = strrchr(str_a, '.');
24587c478bd9Sstevel@tonic-gate 	suffix2 = strrchr(str_b, '.');
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	if (suffix1 != NULL || suffix2 != NULL) {
24617c478bd9Sstevel@tonic-gate 		if (suffix1 == NULL)
24627c478bd9Sstevel@tonic-gate 			suffix1 = str_a;
24637c478bd9Sstevel@tonic-gate 		else
24647c478bd9Sstevel@tonic-gate 			suffix1++;
24657c478bd9Sstevel@tonic-gate 		if (suffix2 == NULL)
24667c478bd9Sstevel@tonic-gate 			suffix2 = str_b;
24677c478bd9Sstevel@tonic-gate 		else
24687c478bd9Sstevel@tonic-gate 			suffix2++;
24697c478bd9Sstevel@tonic-gate 	} else {
24707c478bd9Sstevel@tonic-gate 		suffix1 = str_a;
24717c478bd9Sstevel@tonic-gate 		suffix2 = str_b;
24727c478bd9Sstevel@tonic-gate 	}
24737c478bd9Sstevel@tonic-gate 	return (strcmp(suffix1, suffix2));
24747c478bd9Sstevel@tonic-gate }
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate /*
24777c478bd9Sstevel@tonic-gate  * Order the elements by (ref_id)
24787c478bd9Sstevel@tonic-gate  */
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate /*
24817c478bd9Sstevel@tonic-gate  * Returns PO_TRUE/PO_FALSE to indicate whether the supplied path exists on the
24827c478bd9Sstevel@tonic-gate  * system. It is assumed that the supplied path is in URL format and represents
24837c478bd9Sstevel@tonic-gate  * a file and so file:// is stripped from the start of the search.
24847c478bd9Sstevel@tonic-gate  */
24857c478bd9Sstevel@tonic-gate static int
dtd_exists(const char * path)24867c478bd9Sstevel@tonic-gate dtd_exists(const char *path)
24877c478bd9Sstevel@tonic-gate {
24887c478bd9Sstevel@tonic-gate 	struct stat buf;
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	if (strstr(path, "file://") != path)
24917c478bd9Sstevel@tonic-gate 		return (PO_FALSE);
24927c478bd9Sstevel@tonic-gate 
24937c478bd9Sstevel@tonic-gate 	if (path[7] == 0)
24947c478bd9Sstevel@tonic-gate 		return (PO_FALSE);
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 	if (stat(&path[7], &buf) == 0)
24977c478bd9Sstevel@tonic-gate 		return (PO_TRUE);
24987c478bd9Sstevel@tonic-gate 	return (PO_FALSE);
24997c478bd9Sstevel@tonic-gate }
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate /*
25027c478bd9Sstevel@tonic-gate  * Build the dtype structures to accelerate data type lookup operations. The
25037c478bd9Sstevel@tonic-gate  * purpose is to avoid expensive XML manipulations on data which will not
25047c478bd9Sstevel@tonic-gate  * change over the life of a library invocation. It is designed to be invoked
25057c478bd9Sstevel@tonic-gate  * once from the library init function.
25067c478bd9Sstevel@tonic-gate  */
25077c478bd9Sstevel@tonic-gate static void
build_dtype_accelerator(void)25087c478bd9Sstevel@tonic-gate build_dtype_accelerator(void)
25097c478bd9Sstevel@tonic-gate {
25107c478bd9Sstevel@tonic-gate 	xmlDtdPtr dtd;
25117c478bd9Sstevel@tonic-gate 	const xmlChar *elem_list[ELEM_TYPE_COUNT] = {
25127c478bd9Sstevel@tonic-gate 		BAD_CAST "res_comp",
25137c478bd9Sstevel@tonic-gate 		BAD_CAST "res_agg",
25147c478bd9Sstevel@tonic-gate 		BAD_CAST "comp",
25157c478bd9Sstevel@tonic-gate 		BAD_CAST "pool",
25167c478bd9Sstevel@tonic-gate 		BAD_CAST "property",
25177c478bd9Sstevel@tonic-gate 		BAD_CAST "system" };
25187c478bd9Sstevel@tonic-gate 	int i;
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	if (_libpool_xml_initialised == PO_TRUE)
25217c478bd9Sstevel@tonic-gate 		return;
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	/* Load up the d-type data for each element */
25247c478bd9Sstevel@tonic-gate 	/*
25257c478bd9Sstevel@tonic-gate 	 * Store data type information in nested lists
25267c478bd9Sstevel@tonic-gate 	 * Top level list contains attribute declaration pointers which
25277c478bd9Sstevel@tonic-gate 	 * can be used to match with supplied nodes.
25287c478bd9Sstevel@tonic-gate 	 * Second level list contains attribute type information for each
25297c478bd9Sstevel@tonic-gate 	 * element declaration
25307c478bd9Sstevel@tonic-gate 	 */
25317c478bd9Sstevel@tonic-gate 	/*
25327c478bd9Sstevel@tonic-gate 	 * Unfortunately, there's no easy way to get a list of all DTD
25337c478bd9Sstevel@tonic-gate 	 * element descriptions as there is no libxml API to do this (they
25347c478bd9Sstevel@tonic-gate 	 * are stored in a hash, which I guess is why). Explicitly seek
25357c478bd9Sstevel@tonic-gate 	 * for descriptions for elements that are known to hold an a-dtype
25367c478bd9Sstevel@tonic-gate 	 * attribute and build accelerators for those elements.
25377c478bd9Sstevel@tonic-gate 	 * If the DTD changes, the library may have to change as well now,
25387c478bd9Sstevel@tonic-gate 	 * since this code makes explicit assumptions about which elements
25397c478bd9Sstevel@tonic-gate 	 * contain a-dtype information.
25407c478bd9Sstevel@tonic-gate 	 */
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	if ((dtd = xmlParseDTD(BAD_CAST "-//Sun Microsystems Inc//DTD Resource"
25437c478bd9Sstevel@tonic-gate 	    " Management All//EN", BAD_CAST dtd_location)) == NULL)
25447c478bd9Sstevel@tonic-gate 		return;
25457c478bd9Sstevel@tonic-gate 	for (i = 0; i < ELEM_TYPE_COUNT; i++) {
25467c478bd9Sstevel@tonic-gate 		xmlElementPtr elem;
25477c478bd9Sstevel@tonic-gate 		xmlAttributePtr attr;
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 		if ((elem = xmlGetDtdElementDesc(dtd, elem_list[i])) == NULL)
25507c478bd9Sstevel@tonic-gate 			return;
25517c478bd9Sstevel@tonic-gate 		elem_tbl[i].ett_elem = xmlStrdup(elem->name);
25527c478bd9Sstevel@tonic-gate 		/* Walk the list of attributes looking for a-dtype */
25537c478bd9Sstevel@tonic-gate 		for (attr = elem->attributes; attr != NULL;
25547c478bd9Sstevel@tonic-gate 		    attr = attr->nexth) {
25557c478bd9Sstevel@tonic-gate 			if (strcmp((const char *)attr->name, c_a_dtype) == 0) {
25567c478bd9Sstevel@tonic-gate 				/*
25577c478bd9Sstevel@tonic-gate 				 * Allocate a dtype_tbl_t
25587c478bd9Sstevel@tonic-gate 				 */
25597c478bd9Sstevel@tonic-gate 				elem_tbl[i].ett_dtype =
25607c478bd9Sstevel@tonic-gate 				    build_dtype_tbl(attr->defaultValue);
25617c478bd9Sstevel@tonic-gate 				/* This could have returned NULL */
25627c478bd9Sstevel@tonic-gate 			}
25637c478bd9Sstevel@tonic-gate 		}
25647c478bd9Sstevel@tonic-gate 	}
25657c478bd9Sstevel@tonic-gate 	xmlFreeDtd(dtd);
25667c478bd9Sstevel@tonic-gate }
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate /*
25697c478bd9Sstevel@tonic-gate  * build_dtype_tbl() parses the supplied data and returns an array (max size
25707c478bd9Sstevel@tonic-gate  * of 10, increase if required) of dtype_tbl_t structures holding data type
25717c478bd9Sstevel@tonic-gate  * information for an element. The supplied data is assumed to be in "a-dtype"
25727c478bd9Sstevel@tonic-gate  * format. The dtype_tbl_t array is NULL terminated, which is why space for
25737c478bd9Sstevel@tonic-gate  * 11 members is allocated.
25747c478bd9Sstevel@tonic-gate  */
25757c478bd9Sstevel@tonic-gate static dtype_tbl_t
build_dtype_tbl(const xmlChar * rawdata)25767c478bd9Sstevel@tonic-gate (*build_dtype_tbl(const xmlChar *rawdata))[]
25777c478bd9Sstevel@tonic-gate {
25787c478bd9Sstevel@tonic-gate 	char *tok;
25797c478bd9Sstevel@tonic-gate 	char *lasts;
25807c478bd9Sstevel@tonic-gate 	dtype_tbl_t (*tbl)[];
25817c478bd9Sstevel@tonic-gate 	int j = 0;
25827c478bd9Sstevel@tonic-gate 	xmlChar *data;
25837c478bd9Sstevel@tonic-gate 	const int max_attr = 11; /* Not more than 10 types per element */
25847c478bd9Sstevel@tonic-gate 
25857c478bd9Sstevel@tonic-gate 	/*
25867c478bd9Sstevel@tonic-gate 	 * Parse the supplied data, assumed to be in a-dtype format, and
25877c478bd9Sstevel@tonic-gate 	 * generate a lookup table which is indexed by the name and contains
25887c478bd9Sstevel@tonic-gate 	 * the data type
25897c478bd9Sstevel@tonic-gate 	 */
25907c478bd9Sstevel@tonic-gate 	if (rawdata == NULL)
25917c478bd9Sstevel@tonic-gate 	return (NULL);
25927c478bd9Sstevel@tonic-gate 	if ((data = xmlStrdup(rawdata)) == NULL)
25937c478bd9Sstevel@tonic-gate 	return (NULL);
25947c478bd9Sstevel@tonic-gate 	if ((tbl = calloc(max_attr, sizeof (dtype_tbl_t))) == NULL) {
25957c478bd9Sstevel@tonic-gate 		xmlFree(data);
25967c478bd9Sstevel@tonic-gate 		return (NULL);
25977c478bd9Sstevel@tonic-gate 	}
25987c478bd9Sstevel@tonic-gate 	for (tok = strtok_r((char *)data, "	 ", &lasts); tok != NULL;
25997c478bd9Sstevel@tonic-gate 	    tok = strtok_r(NULL, "	 ", &lasts)) {
26007c478bd9Sstevel@tonic-gate 		    int i;
26017c478bd9Sstevel@tonic-gate 		    (*tbl)[j].dt_name  = xmlStrdup(BAD_CAST tok);
26027c478bd9Sstevel@tonic-gate 		    if ((tok = strtok_r(NULL, "	 ", &lasts)) == NULL) {
26037c478bd9Sstevel@tonic-gate 			    int k = j;
26047c478bd9Sstevel@tonic-gate 			    for (j = 0; j < k; j++)
26057c478bd9Sstevel@tonic-gate 				    free((*tbl)[j].dt_name);
26067c478bd9Sstevel@tonic-gate 			    pool_seterror(POE_DATASTORE);
26077c478bd9Sstevel@tonic-gate 			    xmlFree(data);
26087c478bd9Sstevel@tonic-gate 			    free(tbl);
26097c478bd9Sstevel@tonic-gate 			    return (NULL);
26107c478bd9Sstevel@tonic-gate 		    }
26117c478bd9Sstevel@tonic-gate 		    for (i = 0; i < (sizeof (data_type_tags) /
26127c478bd9Sstevel@tonic-gate 			sizeof (data_type_tags[0])); i++) {
26137c478bd9Sstevel@tonic-gate 				if (strcmp(tok, data_type_tags[i]) == 0)
26147c478bd9Sstevel@tonic-gate 				(*tbl)[j++].dt_type = i;
26157c478bd9Sstevel@tonic-gate 			}
26167c478bd9Sstevel@tonic-gate 		    if (j == max_attr) { /* too many attributes, bail out */
26177c478bd9Sstevel@tonic-gate 			    for (j = 0; j < max_attr; j++)
26187c478bd9Sstevel@tonic-gate 			    free((*tbl)[j].dt_name);
26197c478bd9Sstevel@tonic-gate 			    free(tbl);
26207c478bd9Sstevel@tonic-gate 			    xmlFree(data);
26217c478bd9Sstevel@tonic-gate 			    return (NULL);
26227c478bd9Sstevel@tonic-gate 		    }
26237c478bd9Sstevel@tonic-gate 	    }
26247c478bd9Sstevel@tonic-gate 	(*tbl)[j].dt_name = NULL; /* Terminate the table */
26257c478bd9Sstevel@tonic-gate 	xmlFree(data);
26267c478bd9Sstevel@tonic-gate 	return (tbl);
26277c478bd9Sstevel@tonic-gate }
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate /*
26307c478bd9Sstevel@tonic-gate  * get_fast_dtype() finds the data type for a supplied attribute name on a
26317c478bd9Sstevel@tonic-gate  * supplied node. This is called get_fast_dtype() because it uses the cached
26327c478bd9Sstevel@tonic-gate  * data type information created at library initialisation.
26337c478bd9Sstevel@tonic-gate  */
26347c478bd9Sstevel@tonic-gate static int
get_fast_dtype(xmlNodePtr node,xmlChar * name)26357c478bd9Sstevel@tonic-gate get_fast_dtype(xmlNodePtr node, xmlChar *name)
26367c478bd9Sstevel@tonic-gate {
26377c478bd9Sstevel@tonic-gate 	int i;
26387c478bd9Sstevel@tonic-gate 	xmlElementPtr elem;
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 	if ((elem = xmlGetDtdElementDesc(node->doc->extSubset, node->name))
26417c478bd9Sstevel@tonic-gate 	    == NULL) {
26427c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
26437c478bd9Sstevel@tonic-gate 		return (POC_INVAL);
26447c478bd9Sstevel@tonic-gate 	}
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 	for (i = 0; i < ELEM_TYPE_COUNT; i++) {
26477c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(elem_tbl[i].ett_elem, elem->name) == 0) {
26487c478bd9Sstevel@tonic-gate 			dtype_tbl_t (*tbl)[] = elem_tbl[i].ett_dtype;
26497c478bd9Sstevel@tonic-gate 			int j = 0;
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 			if (tbl == NULL)
26527c478bd9Sstevel@tonic-gate 				break;
26537c478bd9Sstevel@tonic-gate 			for (j = 0; (*tbl)[j].dt_name != NULL; j++)
26547c478bd9Sstevel@tonic-gate 				if (xmlStrcmp(name, (*tbl)[j].dt_name) == 0)
26557c478bd9Sstevel@tonic-gate 					return ((*tbl)[j].dt_type); /* found */
26567c478bd9Sstevel@tonic-gate 			break; /* if we didn't find it in the elem, break */
26577c478bd9Sstevel@tonic-gate 		}
26587c478bd9Sstevel@tonic-gate 	}
26597c478bd9Sstevel@tonic-gate 	/* If we can't find it, say it's a string */
26607c478bd9Sstevel@tonic-gate 	return (POC_STRING);
26617c478bd9Sstevel@tonic-gate }
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate /*
26647c478bd9Sstevel@tonic-gate  * pool_xml_parse_document() parses the file associated with a supplied
26657c478bd9Sstevel@tonic-gate  * configuration to regenerate the runtime representation. The supplied
26667c478bd9Sstevel@tonic-gate  * configuration must reference an already opened file and this is used
26677c478bd9Sstevel@tonic-gate  * to generate the XML representation via the configuration provider's
26687c478bd9Sstevel@tonic-gate  * pxc_doc member.
26697c478bd9Sstevel@tonic-gate  * size must be >=4 in order for "content encoding detection" to work.
26707c478bd9Sstevel@tonic-gate  */
26717c478bd9Sstevel@tonic-gate static int
pool_xml_parse_document(pool_conf_t * conf)26727c478bd9Sstevel@tonic-gate pool_xml_parse_document(pool_conf_t *conf)
26737c478bd9Sstevel@tonic-gate {
26747c478bd9Sstevel@tonic-gate 	int res;
26757c478bd9Sstevel@tonic-gate 	char chars[PAGE_READ_SIZE];
26767c478bd9Sstevel@tonic-gate 	struct stat f_stat;
26777c478bd9Sstevel@tonic-gate 	xmlParserCtxtPtr ctxt;
26787c478bd9Sstevel@tonic-gate 	size_t size;
26797c478bd9Sstevel@tonic-gate 	pool_xml_connection_t *prov = (pool_xml_connection_t *)conf->pc_prov;
26807c478bd9Sstevel@tonic-gate 	xmlNodePtr root;
26817c478bd9Sstevel@tonic-gate 	pool_resource_t **rsl;
26827c478bd9Sstevel@tonic-gate 	uint_t nelem;
26837c478bd9Sstevel@tonic-gate 	int i;
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate 	if (fstat(fileno(prov->pxc_file), &f_stat) == -1) {
26867c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
26877c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
26887c478bd9Sstevel@tonic-gate 	}
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	if (f_stat.st_size == 0) {
26917c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
26927c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
26937c478bd9Sstevel@tonic-gate 	} else
26947c478bd9Sstevel@tonic-gate 		size = f_stat.st_size < 4 ? 4 : PAGE_READ_SIZE;
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 	res = fread(chars, 1, size, prov->pxc_file);
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	if (res >= 4) {
26997c478bd9Sstevel@tonic-gate 		xmlValidCtxtPtr cvp;
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 		if ((ctxt = xmlCreatePushParserCtxt(NULL, NULL,
27027c478bd9Sstevel@tonic-gate 		    chars, res, conf->pc_location)) == NULL) {
27037c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
27047c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
27057c478bd9Sstevel@tonic-gate 		}
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 		while ((res = fread(chars, 1, size, prov->pxc_file)) > 0) {
27087c478bd9Sstevel@tonic-gate 			if (xmlParseChunk(ctxt, chars, res, 0) != 0) {
27097c478bd9Sstevel@tonic-gate 				xmlFreeParserCtxt(ctxt);
27107c478bd9Sstevel@tonic-gate 				pool_seterror(POE_INVALID_CONF);
27117c478bd9Sstevel@tonic-gate 				return (PO_FAIL);
27127c478bd9Sstevel@tonic-gate 			}
27137c478bd9Sstevel@tonic-gate 		}
27147c478bd9Sstevel@tonic-gate 		if (xmlParseChunk(ctxt, chars, 0, 1) != 0) {
27157c478bd9Sstevel@tonic-gate 			xmlFreeParserCtxt(ctxt);
27167c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
27177c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
27187c478bd9Sstevel@tonic-gate 		}
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 		if ((cvp = xmlNewValidCtxt()) == NULL) {
27217c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
27227c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
27237c478bd9Sstevel@tonic-gate 		}
27247c478bd9Sstevel@tonic-gate 		cvp->error    = pool_error_func;
27257c478bd9Sstevel@tonic-gate 		cvp->warning  = pool_error_func;
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 		if (xmlValidateDocument(cvp, ctxt->myDoc) == 0) {
27287c478bd9Sstevel@tonic-gate 			xmlFreeValidCtxt(cvp);
27297c478bd9Sstevel@tonic-gate 			xmlFreeParserCtxt(ctxt);
27307c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
27317c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
27327c478bd9Sstevel@tonic-gate 		}
27337c478bd9Sstevel@tonic-gate 		prov->pxc_doc = ctxt->myDoc;
27347c478bd9Sstevel@tonic-gate 		xmlFreeValidCtxt(cvp);
27357c478bd9Sstevel@tonic-gate 		xmlFreeParserCtxt(ctxt);
27367c478bd9Sstevel@tonic-gate 	}
27377c478bd9Sstevel@tonic-gate 	if (prov->pxc_doc == NULL) {
27387c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
27397c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
27407c478bd9Sstevel@tonic-gate 	}
27417c478bd9Sstevel@tonic-gate 	prov->pxc_doc->_private = conf;
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	/* Get the root element */
27447c478bd9Sstevel@tonic-gate 	if ((root = xmlDocGetRootElement(prov->pxc_doc)) == NULL) {
27457c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
27467c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
27477c478bd9Sstevel@tonic-gate 	}
27487c478bd9Sstevel@tonic-gate 	/*
27497c478bd9Sstevel@tonic-gate 	 * Ensure that the parsed tree has been contained within
27507c478bd9Sstevel@tonic-gate 	 * our shadow tree.
27517c478bd9Sstevel@tonic-gate 	 */
27527c478bd9Sstevel@tonic-gate 	if (create_shadow(root) != PO_SUCCESS) {
27537c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
27547c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
27557c478bd9Sstevel@tonic-gate 	}
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	if (pool_xml_validate(conf, POV_STRICT) != PO_SUCCESS) {
27587c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
27597c478bd9Sstevel@tonic-gate 	}
27607c478bd9Sstevel@tonic-gate 	/*
27617c478bd9Sstevel@tonic-gate 	 * For backwards compatibility with S9, make sure that all
27627c478bd9Sstevel@tonic-gate 	 * resources have a size and that it is correct.
27637c478bd9Sstevel@tonic-gate 	 */
27647c478bd9Sstevel@tonic-gate 	if ((rsl = pool_query_resources(conf, &nelem, NULL)) != NULL) {
27657c478bd9Sstevel@tonic-gate 		pool_value_t val = POOL_VALUE_INITIALIZER;
27667c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
27677c478bd9Sstevel@tonic-gate 			if (pool_get_ns_property(TO_ELEM(rsl[i]), c_size_prop,
27687c478bd9Sstevel@tonic-gate 			    &val) != POC_UINT) {
27697c478bd9Sstevel@tonic-gate 				pool_component_t **cs;
27707c478bd9Sstevel@tonic-gate 				uint_t size;
27717c478bd9Sstevel@tonic-gate 				if ((cs = pool_query_resource_components(conf,
27727c478bd9Sstevel@tonic-gate 				    rsl[i], &size, NULL)) != NULL) {
27737c478bd9Sstevel@tonic-gate 					free(cs);
27747c478bd9Sstevel@tonic-gate 					pool_value_set_uint64(&val, size);
27757c478bd9Sstevel@tonic-gate 				} else
27767c478bd9Sstevel@tonic-gate 					pool_value_set_uint64(&val, 0);
27777c478bd9Sstevel@tonic-gate 				if (pool_put_any_ns_property(TO_ELEM(rsl[i]),
27787c478bd9Sstevel@tonic-gate 				    c_size_prop, &val)  != PO_SUCCESS) {
27797c478bd9Sstevel@tonic-gate 					free(rsl);
27807c478bd9Sstevel@tonic-gate 					return (PO_FAIL);
27817c478bd9Sstevel@tonic-gate 				}
27827c478bd9Sstevel@tonic-gate 			}
27837c478bd9Sstevel@tonic-gate 		}
27847c478bd9Sstevel@tonic-gate 		free(rsl);
27857c478bd9Sstevel@tonic-gate 	}
27867c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
27877c478bd9Sstevel@tonic-gate }
2788