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
5103b2b15Sgww  * Common Development and Distribution License (the "License").
6103b2b15Sgww  * 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  */
21*a574db85Sraf 
227c478bd9Sstevel@tonic-gate /*
235b7f77adStw  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * This is the main implementation file for the low-level repository
317c478bd9Sstevel@tonic-gate  * interface.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "lowlevel_impl.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "repcache_protocol.h"
377c478bd9Sstevel@tonic-gate #include "scf_type.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <assert.h>
407c478bd9Sstevel@tonic-gate #include <alloca.h>
417c478bd9Sstevel@tonic-gate #include <door.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <fnmatch.h>
457c478bd9Sstevel@tonic-gate #include <libuutil.h>
467c478bd9Sstevel@tonic-gate #include <poll.h>
477c478bd9Sstevel@tonic-gate #include <pthread.h>
487c478bd9Sstevel@tonic-gate #include <stddef.h>
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <stdlib.h>
517c478bd9Sstevel@tonic-gate #include <string.h>
527c478bd9Sstevel@tonic-gate #include <sys/mman.h>
537c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
547c478bd9Sstevel@tonic-gate #include <unistd.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	ENV_SCF_DEBUG		"LIBSCF_DEBUG"
577c478bd9Sstevel@tonic-gate #define	ENV_SCF_DOORPATH	"LIBSCF_DOORPATH"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static uint32_t default_debug = 0;
607c478bd9Sstevel@tonic-gate static const char *default_door_path = REPOSITORY_DOOR_NAME;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	CALL_FAILED		-1
637c478bd9Sstevel@tonic-gate #define	RESULT_TOO_BIG		-2
647c478bd9Sstevel@tonic-gate #define	NOT_BOUND		-3
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static pthread_mutex_t	lowlevel_init_lock;
677c478bd9Sstevel@tonic-gate static int32_t		lowlevel_inited;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static uu_list_pool_t	*tran_entry_pool;
707c478bd9Sstevel@tonic-gate static uu_list_pool_t	*datael_pool;
717c478bd9Sstevel@tonic-gate static uu_list_pool_t	*iter_pool;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * We want MUTEX_HELD, but we also want pthreads.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate struct _lwp_mutex;
777c478bd9Sstevel@tonic-gate extern int _mutex_held(struct _lwp_mutex *);
787c478bd9Sstevel@tonic-gate #define	MUTEX_HELD(m)		_mutex_held((struct _lwp_mutex *)(m))
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #ifdef lint
817c478bd9Sstevel@tonic-gate #define	assert_nolint(x) (void)0
827c478bd9Sstevel@tonic-gate #else
837c478bd9Sstevel@tonic-gate #define	assert_nolint(x) assert(x)
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static void scf_iter_reset_locked(scf_iter_t *iter);
877c478bd9Sstevel@tonic-gate static void scf_value_reset_locked(scf_value_t *val, int and_destroy);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #define	TYPE_VALUE	(-100)
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Hold and release subhandles.  We only allow one thread access to the
937c478bd9Sstevel@tonic-gate  * subhandles at a time, and he can use any subset, grabbing and releasing
947c478bd9Sstevel@tonic-gate  * them in any order.  The only restrictions are that you cannot hold an
957c478bd9Sstevel@tonic-gate  * already-held subhandle, and all subhandles must be released before
967c478bd9Sstevel@tonic-gate  * returning to the original caller.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate static void
997c478bd9Sstevel@tonic-gate handle_hold_subhandles(scf_handle_t *h, int mask)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	assert(mask != 0 && (mask & ~RH_HOLD_ALL) == 0);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
104*a574db85Sraf 	while (h->rh_hold_flags != 0 && h->rh_holder != pthread_self()) {
105*a574db85Sraf 		int cancel_state;
106*a574db85Sraf 
107*a574db85Sraf 		(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
108*a574db85Sraf 		    &cancel_state);
109*a574db85Sraf 		(void) pthread_cond_wait(&h->rh_cv, &h->rh_lock);
110*a574db85Sraf 		(void) pthread_setcancelstate(cancel_state, NULL);
111*a574db85Sraf 	}
1127c478bd9Sstevel@tonic-gate 	if (h->rh_hold_flags == 0)
1137c478bd9Sstevel@tonic-gate 		h->rh_holder = pthread_self();
1147c478bd9Sstevel@tonic-gate 	assert(!(h->rh_hold_flags & mask));
1157c478bd9Sstevel@tonic-gate 	h->rh_hold_flags |= mask;
1167c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static void
1207c478bd9Sstevel@tonic-gate handle_rele_subhandles(scf_handle_t *h, int mask)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	assert(mask != 0 && (mask & ~RH_HOLD_ALL) == 0);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
1257c478bd9Sstevel@tonic-gate 	assert(h->rh_holder == pthread_self());
1267c478bd9Sstevel@tonic-gate 	assert((h->rh_hold_flags & mask));
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	h->rh_hold_flags &= ~mask;
1297c478bd9Sstevel@tonic-gate 	if (h->rh_hold_flags == 0)
1307c478bd9Sstevel@tonic-gate 		(void) pthread_cond_signal(&h->rh_cv);
1317c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #define	HOLD_HANDLE(h, flag, field) \
1357c478bd9Sstevel@tonic-gate 	(handle_hold_subhandles((h), (flag)), (h)->field)
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #define	RELE_HANDLE(h, flag) \
1387c478bd9Sstevel@tonic-gate 	(handle_rele_subhandles((h), (flag)))
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * convenience macros, for functions that only need a one or two handles at
1427c478bd9Sstevel@tonic-gate  * any given time
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate #define	HANDLE_HOLD_ITER(h)	HOLD_HANDLE((h), RH_HOLD_ITER, rh_iter)
1457c478bd9Sstevel@tonic-gate #define	HANDLE_HOLD_SCOPE(h)	HOLD_HANDLE((h), RH_HOLD_SCOPE, rh_scope)
1467c478bd9Sstevel@tonic-gate #define	HANDLE_HOLD_SERVICE(h)	HOLD_HANDLE((h), RH_HOLD_SERVICE, rh_service)
1477c478bd9Sstevel@tonic-gate #define	HANDLE_HOLD_INSTANCE(h)	HOLD_HANDLE((h), RH_HOLD_INSTANCE, rh_instance)
1487c478bd9Sstevel@tonic-gate #define	HANDLE_HOLD_SNAPSHOT(h)	HOLD_HANDLE((h), RH_HOLD_SNAPSHOT, rh_snapshot)
1497c478bd9Sstevel@tonic-gate #define	HANDLE_HOLD_SNAPLVL(h)	HOLD_HANDLE((h), RH_HOLD_SNAPLVL, rh_snaplvl)
1507c478bd9Sstevel@tonic-gate #define	HANDLE_HOLD_PG(h)	HOLD_HANDLE((h), RH_HOLD_PG, rh_pg)
1517c478bd9Sstevel@tonic-gate #define	HANDLE_HOLD_PROPERTY(h)	HOLD_HANDLE((h), RH_HOLD_PROPERTY, rh_property)
1527c478bd9Sstevel@tonic-gate #define	HANDLE_HOLD_VALUE(h)	HOLD_HANDLE((h), RH_HOLD_VALUE, rh_value)
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #define	HANDLE_RELE_ITER(h)	RELE_HANDLE((h), RH_HOLD_ITER)
1557c478bd9Sstevel@tonic-gate #define	HANDLE_RELE_SCOPE(h)	RELE_HANDLE((h), RH_HOLD_SCOPE)
1567c478bd9Sstevel@tonic-gate #define	HANDLE_RELE_SERVICE(h)	RELE_HANDLE((h), RH_HOLD_SERVICE)
1577c478bd9Sstevel@tonic-gate #define	HANDLE_RELE_INSTANCE(h)	RELE_HANDLE((h), RH_HOLD_INSTANCE)
1587c478bd9Sstevel@tonic-gate #define	HANDLE_RELE_SNAPSHOT(h)	RELE_HANDLE((h), RH_HOLD_SNAPSHOT)
1597c478bd9Sstevel@tonic-gate #define	HANDLE_RELE_SNAPLVL(h)	RELE_HANDLE((h), RH_HOLD_SNAPLVL)
1607c478bd9Sstevel@tonic-gate #define	HANDLE_RELE_PG(h)	RELE_HANDLE((h), RH_HOLD_PG)
1617c478bd9Sstevel@tonic-gate #define	HANDLE_RELE_PROPERTY(h)	RELE_HANDLE((h), RH_HOLD_PROPERTY)
1627c478bd9Sstevel@tonic-gate #define	HANDLE_RELE_VALUE(h)	RELE_HANDLE((h), RH_HOLD_VALUE)
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1657c478bd9Sstevel@tonic-gate static int
1667c478bd9Sstevel@tonic-gate transaction_entry_compare(const void *l_arg, const void *r_arg, void *private)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	const char *l_prop =
1697c478bd9Sstevel@tonic-gate 	    ((scf_transaction_entry_t *)l_arg)->entry_property;
1707c478bd9Sstevel@tonic-gate 	const char *r_prop =
1717c478bd9Sstevel@tonic-gate 	    ((scf_transaction_entry_t *)r_arg)->entry_property;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	int ret;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	ret = strcmp(l_prop, r_prop);
1767c478bd9Sstevel@tonic-gate 	if (ret > 0)
1777c478bd9Sstevel@tonic-gate 		return (1);
1788918dff3Sjwadams 	if (ret < 0)
1798918dff3Sjwadams 		return (-1);
1808918dff3Sjwadams 	return (0);
1818918dff3Sjwadams }
1828918dff3Sjwadams 
1838918dff3Sjwadams static int
1848918dff3Sjwadams datael_compare(const void *l_arg, const void *r_arg, void *private)
1858918dff3Sjwadams {
1868918dff3Sjwadams 	uint32_t l_id = ((scf_datael_t *)l_arg)->rd_entity;
1878918dff3Sjwadams 	uint32_t r_id = (r_arg != NULL) ? ((scf_datael_t *)r_arg)->rd_entity :
1888918dff3Sjwadams 	    *(uint32_t *)private;
1898918dff3Sjwadams 
1908918dff3Sjwadams 	if (l_id > r_id)
1918918dff3Sjwadams 		return (1);
1928918dff3Sjwadams 	if (l_id < r_id)
1938918dff3Sjwadams 		return (-1);
1948918dff3Sjwadams 	return (0);
1958918dff3Sjwadams }
1968918dff3Sjwadams 
1978918dff3Sjwadams static int
1988918dff3Sjwadams iter_compare(const void *l_arg, const void *r_arg, void *private)
1998918dff3Sjwadams {
2008918dff3Sjwadams 	uint32_t l_id = ((scf_iter_t *)l_arg)->iter_id;
2018918dff3Sjwadams 	uint32_t r_id = (r_arg != NULL) ? ((scf_iter_t *)r_arg)->iter_id :
2028918dff3Sjwadams 	    *(uint32_t *)private;
2038918dff3Sjwadams 
2048918dff3Sjwadams 	if (l_id > r_id)
2058918dff3Sjwadams 		return (1);
2068918dff3Sjwadams 	if (l_id < r_id)
2077c478bd9Sstevel@tonic-gate 		return (-1);
2087c478bd9Sstevel@tonic-gate 	return (0);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static int
2127c478bd9Sstevel@tonic-gate lowlevel_init(void)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	const char *debug;
2157c478bd9Sstevel@tonic-gate 	const char *door_path;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&lowlevel_init_lock);
2187c478bd9Sstevel@tonic-gate 	if (lowlevel_inited == 0) {
2197c478bd9Sstevel@tonic-gate 		if (!issetugid() &&
2207c478bd9Sstevel@tonic-gate 		    (debug = getenv(ENV_SCF_DEBUG)) != NULL && debug[0] != 0 &&
2217c478bd9Sstevel@tonic-gate 		    uu_strtoint(debug, &default_debug, sizeof (default_debug),
2227c478bd9Sstevel@tonic-gate 		    0, 0, 0) == -1) {
2237c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "LIBSCF: $%s (%s): %s",
2247c478bd9Sstevel@tonic-gate 			    ENV_SCF_DEBUG, debug,
2257c478bd9Sstevel@tonic-gate 			    uu_strerror(uu_error()));
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		if (!issetugid() &&
2297c478bd9Sstevel@tonic-gate 		    (door_path = getenv(ENV_SCF_DOORPATH)) != NULL &&
2307c478bd9Sstevel@tonic-gate 		    door_path[0] != 0) {
2317c478bd9Sstevel@tonic-gate 			default_door_path = strdup(door_path);
2327c478bd9Sstevel@tonic-gate 			if (default_door_path == NULL)
2337c478bd9Sstevel@tonic-gate 				default_door_path = door_path;
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		datael_pool = uu_list_pool_create("SUNW,libscf_datael",
2377c478bd9Sstevel@tonic-gate 		    sizeof (scf_datael_t), offsetof(scf_datael_t, rd_node),
2388918dff3Sjwadams 		    datael_compare, UU_LIST_POOL_DEBUG);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		iter_pool = uu_list_pool_create("SUNW,libscf_iter",
2417c478bd9Sstevel@tonic-gate 		    sizeof (scf_iter_t), offsetof(scf_iter_t, iter_node),
2428918dff3Sjwadams 		    iter_compare, UU_LIST_POOL_DEBUG);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		assert_nolint(offsetof(scf_transaction_entry_t,
2457c478bd9Sstevel@tonic-gate 		    entry_property) == 0);
2467c478bd9Sstevel@tonic-gate 		tran_entry_pool = uu_list_pool_create(
2477c478bd9Sstevel@tonic-gate 		    "SUNW,libscf_transaction_entity",
2487c478bd9Sstevel@tonic-gate 		    sizeof (scf_transaction_entry_t),
2497c478bd9Sstevel@tonic-gate 		    offsetof(scf_transaction_entry_t, entry_link),
2507c478bd9Sstevel@tonic-gate 		    transaction_entry_compare, UU_LIST_POOL_DEBUG);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		if (datael_pool == NULL || iter_pool == NULL ||
2537c478bd9Sstevel@tonic-gate 		    tran_entry_pool == NULL) {
2547c478bd9Sstevel@tonic-gate 			lowlevel_inited = -1;
2557c478bd9Sstevel@tonic-gate 			goto end;
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		if (!scf_setup_error()) {
2597c478bd9Sstevel@tonic-gate 			lowlevel_inited = -1;
2607c478bd9Sstevel@tonic-gate 			goto end;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 		lowlevel_inited = 1;
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate end:
2657c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&lowlevel_init_lock);
2667c478bd9Sstevel@tonic-gate 	if (lowlevel_inited > 0)
2677c478bd9Sstevel@tonic-gate 		return (1);
2687c478bd9Sstevel@tonic-gate 	return (0);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate static const struct {
2727c478bd9Sstevel@tonic-gate 	scf_type_t ti_type;
2737c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t ti_proto_type;
2747c478bd9Sstevel@tonic-gate 	const char *ti_name;
2757c478bd9Sstevel@tonic-gate } scf_type_info[] = {
2767c478bd9Sstevel@tonic-gate 	{SCF_TYPE_BOOLEAN,	REP_PROTOCOL_TYPE_BOOLEAN,	"boolean"},
2777c478bd9Sstevel@tonic-gate 	{SCF_TYPE_COUNT,	REP_PROTOCOL_TYPE_COUNT,	"count"},
2787c478bd9Sstevel@tonic-gate 	{SCF_TYPE_INTEGER,	REP_PROTOCOL_TYPE_INTEGER,	"integer"},
2797c478bd9Sstevel@tonic-gate 	{SCF_TYPE_TIME,		REP_PROTOCOL_TYPE_TIME,		"time"},
2807c478bd9Sstevel@tonic-gate 	{SCF_TYPE_ASTRING,	REP_PROTOCOL_TYPE_STRING,	"astring"},
2817c478bd9Sstevel@tonic-gate 	{SCF_TYPE_OPAQUE,	REP_PROTOCOL_TYPE_OPAQUE,	"opaque"},
2827c478bd9Sstevel@tonic-gate 	{SCF_TYPE_USTRING,	REP_PROTOCOL_SUBTYPE_USTRING,	"ustring"},
2837c478bd9Sstevel@tonic-gate 	{SCF_TYPE_URI,		REP_PROTOCOL_SUBTYPE_URI,	"uri"},
2847c478bd9Sstevel@tonic-gate 	{SCF_TYPE_FMRI,		REP_PROTOCOL_SUBTYPE_FMRI,	"fmri"},
2857c478bd9Sstevel@tonic-gate 	{SCF_TYPE_HOST,		REP_PROTOCOL_SUBTYPE_HOST,	"host"},
2867c478bd9Sstevel@tonic-gate 	{SCF_TYPE_HOSTNAME,	REP_PROTOCOL_SUBTYPE_HOSTNAME,	"hostname"},
2877c478bd9Sstevel@tonic-gate 	{SCF_TYPE_NET_ADDR_V4,	REP_PROTOCOL_SUBTYPE_NETADDR_V4,
2887c478bd9Sstevel@tonic-gate 	    "net_address_v4"},
2897c478bd9Sstevel@tonic-gate 	{SCF_TYPE_NET_ADDR_V6,	REP_PROTOCOL_SUBTYPE_NETADDR_V6,
2907c478bd9Sstevel@tonic-gate 	    "net_address_v6"}
2917c478bd9Sstevel@tonic-gate };
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate #define	SCF_TYPE_INFO_COUNT (sizeof (scf_type_info) / sizeof (*scf_type_info))
2947c478bd9Sstevel@tonic-gate static rep_protocol_value_type_t
2957c478bd9Sstevel@tonic-gate scf_type_to_protocol_type(scf_type_t t)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	int i;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCF_TYPE_INFO_COUNT; i++)
3007c478bd9Sstevel@tonic-gate 		if (scf_type_info[i].ti_type == t)
3017c478bd9Sstevel@tonic-gate 			return (scf_type_info[i].ti_proto_type);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_TYPE_INVALID);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static scf_type_t
3077c478bd9Sstevel@tonic-gate scf_protocol_type_to_type(rep_protocol_value_type_t t)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	int i;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCF_TYPE_INFO_COUNT; i++)
3127c478bd9Sstevel@tonic-gate 		if (scf_type_info[i].ti_proto_type == t)
3137c478bd9Sstevel@tonic-gate 			return (scf_type_info[i].ti_type);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	return (SCF_TYPE_INVALID);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate const char *
3197c478bd9Sstevel@tonic-gate scf_type_to_string(scf_type_t ty)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	int i;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	for (i = 0; i < SCF_TYPE_INFO_COUNT; i++)
3247c478bd9Sstevel@tonic-gate 		if (scf_type_info[i].ti_type == ty)
3257c478bd9Sstevel@tonic-gate 			return (scf_type_info[i].ti_name);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	return ("unknown");
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate scf_type_t
3317c478bd9Sstevel@tonic-gate scf_string_to_type(const char *name)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	int i;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (scf_type_info) / sizeof (*scf_type_info); i++)
3367c478bd9Sstevel@tonic-gate 		if (strcmp(scf_type_info[i].ti_name, name) == 0)
3377c478bd9Sstevel@tonic-gate 			return (scf_type_info[i].ti_type);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	return (SCF_TYPE_INVALID);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate int
3437c478bd9Sstevel@tonic-gate scf_type_base_type(scf_type_t type, scf_type_t *out)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t t = scf_type_to_protocol_type(type);
3467c478bd9Sstevel@tonic-gate 	if (t == REP_PROTOCOL_TYPE_INVALID)
3477c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	*out = scf_protocol_type_to_type(scf_proto_underlying_type(t));
3507c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * Convert a protocol error code into an SCF_ERROR_* code.
3557c478bd9Sstevel@tonic-gate  */
3567c478bd9Sstevel@tonic-gate static scf_error_t
3577c478bd9Sstevel@tonic-gate proto_error(rep_protocol_responseid_t e)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	switch (e) {
3607c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_MISORDERED:
3617c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_UNKNOWN_ID:
3627c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_INVALID_TYPE:
3637c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_TRUNCATED:
3647c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_TYPE_MISMATCH:
3657c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_NOT_APPLICABLE:
3667c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_UNKNOWN:
3677c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_INTERNAL);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_BAD_TX:
3707c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_INVALID_ARGUMENT);
3717c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_BAD_REQUEST:
3727c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_INVALID_ARGUMENT);
3737c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_NO_RESOURCES:
3747c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_NO_RESOURCES);
3757c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_NOT_FOUND:
3767c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_NOT_FOUND);
3777c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_DELETED:
3787c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_DELETED);
3797c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_NOT_SET:
3807c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_NOT_SET);
3817c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_EXISTS:
3827c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_EXISTS);
3837c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_DUPLICATE_ID:
3847c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_EXISTS);
3857c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_PERMISSION_DENIED:
3867c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_PERMISSION_DENIED);
3877c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_BACKEND_ACCESS:
3887c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_BACKEND_ACCESS);
3897c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_BACKEND_READONLY:
3907c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_BACKEND_READONLY);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_SUCCESS:
3937c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_DONE:
3947c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_FAIL_NOT_LATEST:	/* TX code should handle this */
3957c478bd9Sstevel@tonic-gate 	default:
3967c478bd9Sstevel@tonic-gate #ifndef NDEBUG
3977c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Bad error code %d passed to proto_error().\n",
3987c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__, e);
3997c478bd9Sstevel@tonic-gate #endif
4007c478bd9Sstevel@tonic-gate 		abort();
4017c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate ssize_t
4067c478bd9Sstevel@tonic-gate scf_limit(uint32_t limit)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	switch (limit) {
4097c478bd9Sstevel@tonic-gate 	case SCF_LIMIT_MAX_NAME_LENGTH:
4107c478bd9Sstevel@tonic-gate 	case SCF_LIMIT_MAX_PG_TYPE_LENGTH:
4117c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_NAME_LEN - 1);
4127c478bd9Sstevel@tonic-gate 	case SCF_LIMIT_MAX_VALUE_LENGTH:
4137c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_VALUE_LEN - 1);
4147c478bd9Sstevel@tonic-gate 	case SCF_LIMIT_MAX_FMRI_LENGTH:
4157c478bd9Sstevel@tonic-gate 		return (SCF_FMRI_PREFIX_MAX_LEN +
4167c478bd9Sstevel@tonic-gate 		    sizeof (SCF_FMRI_SCOPE_PREFIX) - 1 +
4177c478bd9Sstevel@tonic-gate 		    sizeof (SCF_FMRI_SCOPE_SUFFIX) - 1 +
4187c478bd9Sstevel@tonic-gate 		    sizeof (SCF_FMRI_SERVICE_PREFIX) - 1 +
4197c478bd9Sstevel@tonic-gate 		    sizeof (SCF_FMRI_INSTANCE_PREFIX) - 1 +
4207c478bd9Sstevel@tonic-gate 		    sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
4217c478bd9Sstevel@tonic-gate 		    sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
4227c478bd9Sstevel@tonic-gate 		    5 * (REP_PROTOCOL_NAME_LEN - 1));
4237c478bd9Sstevel@tonic-gate 	default:
4247c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate static size_t
4297c478bd9Sstevel@tonic-gate scf_opaque_decode(char *out_arg, const char *in, size_t max_out)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	char a, b;
4327c478bd9Sstevel@tonic-gate 	char *out = out_arg;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	while (max_out > 0 && (a = in[0]) != 0 && (b = in[1]) != 0) {
4357c478bd9Sstevel@tonic-gate 		in += 2;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 		if (a >= '0' && a <= '9')
4387c478bd9Sstevel@tonic-gate 			a -= '0';
4397c478bd9Sstevel@tonic-gate 		else if (a >= 'a' && a <= 'f')
4407c478bd9Sstevel@tonic-gate 			a = a - 'a' + 10;
4417c478bd9Sstevel@tonic-gate 		else if (a >= 'A' && a <= 'F')
4427c478bd9Sstevel@tonic-gate 			a = a - 'A' + 10;
4437c478bd9Sstevel@tonic-gate 		else
4447c478bd9Sstevel@tonic-gate 			break;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		if (b >= '0' && b <= '9')
4477c478bd9Sstevel@tonic-gate 			b -= '0';
4487c478bd9Sstevel@tonic-gate 		else if (b >= 'a' && b <= 'f')
4497c478bd9Sstevel@tonic-gate 			b = b - 'a' + 10;
4507c478bd9Sstevel@tonic-gate 		else if (b >= 'A' && b <= 'F')
4517c478bd9Sstevel@tonic-gate 			b = b - 'A' + 10;
4527c478bd9Sstevel@tonic-gate 		else
4537c478bd9Sstevel@tonic-gate 			break;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		*out++ = (a << 4) | b;
4567c478bd9Sstevel@tonic-gate 		max_out--;
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	return (out - out_arg);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate static size_t
4637c478bd9Sstevel@tonic-gate scf_opaque_encode(char *out_arg, const char *in_arg, size_t in_sz)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	uint8_t *in = (uint8_t *)in_arg;
4667c478bd9Sstevel@tonic-gate 	uint8_t *end = in + in_sz;
4677c478bd9Sstevel@tonic-gate 	char *out = out_arg;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (out == NULL)
4707c478bd9Sstevel@tonic-gate 		return (2 * in_sz);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	while (in < end) {
4737c478bd9Sstevel@tonic-gate 		uint8_t c = *in++;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		uint8_t a = (c & 0xf0) >> 4;
4767c478bd9Sstevel@tonic-gate 		uint8_t b = (c & 0x0f);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 		if (a <= 9)
4797c478bd9Sstevel@tonic-gate 			*out++ = a + '0';
4807c478bd9Sstevel@tonic-gate 		else
4817c478bd9Sstevel@tonic-gate 			*out++ = a + 'a' - 10;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		if (b <= 9)
4847c478bd9Sstevel@tonic-gate 			*out++ = b + '0';
4857c478bd9Sstevel@tonic-gate 		else
4867c478bd9Sstevel@tonic-gate 			*out++ = b + 'a' - 10;
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	*out = 0;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	return (out - out_arg);
4927c478bd9Sstevel@tonic-gate }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate static void
4957c478bd9Sstevel@tonic-gate handle_do_close(scf_handle_t *h)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
4987c478bd9Sstevel@tonic-gate 	assert(h->rh_doorfd != -1);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	/*
5017c478bd9Sstevel@tonic-gate 	 * if there are any active FD users, we just move the FD over
5027c478bd9Sstevel@tonic-gate 	 * to rh_doorfd_old -- they'll close it when they finish.
5037c478bd9Sstevel@tonic-gate 	 */
5047c478bd9Sstevel@tonic-gate 	if (h->rh_fd_users > 0) {
5057c478bd9Sstevel@tonic-gate 		h->rh_doorfd_old = h->rh_doorfd;
5067c478bd9Sstevel@tonic-gate 		h->rh_doorfd = -1;
5077c478bd9Sstevel@tonic-gate 	} else {
5087c478bd9Sstevel@tonic-gate 		assert(h->rh_doorfd_old == -1);
5097c478bd9Sstevel@tonic-gate 		(void) close(h->rh_doorfd);
5107c478bd9Sstevel@tonic-gate 		h->rh_doorfd = -1;
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate  * Check if a handle is currently bound.  fork()ing implicitly unbinds
5167c478bd9Sstevel@tonic-gate  * the handle in the child.
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate static int
5197c478bd9Sstevel@tonic-gate handle_is_bound(scf_handle_t *h)
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if (h->rh_doorfd == -1)
5247c478bd9Sstevel@tonic-gate 		return (0);
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (getpid() == h->rh_doorpid)
5277c478bd9Sstevel@tonic-gate 		return (1);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/* forked since our last bind -- initiate handle close */
5307c478bd9Sstevel@tonic-gate 	handle_do_close(h);
5317c478bd9Sstevel@tonic-gate 	return (0);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate static int
5358918dff3Sjwadams handle_has_server_locked(scf_handle_t *h)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	door_info_t i;
5388918dff3Sjwadams 	assert(MUTEX_HELD(&h->rh_lock));
5398918dff3Sjwadams 
5408918dff3Sjwadams 	return (handle_is_bound(h) && door_info(h->rh_doorfd, &i) != -1 &&
5418918dff3Sjwadams 	    i.di_target != -1);
5428918dff3Sjwadams }
5438918dff3Sjwadams 
5448918dff3Sjwadams static int
5458918dff3Sjwadams handle_has_server(scf_handle_t *h)
5468918dff3Sjwadams {
5477c478bd9Sstevel@tonic-gate 	int ret;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
5508918dff3Sjwadams 	ret = handle_has_server_locked(h);
5517c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	return (ret);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * This makes a door request on the client door associated with handle h.
5587c478bd9Sstevel@tonic-gate  * It will automatically retry calls which fail on EINTR.  If h is not bound,
5597c478bd9Sstevel@tonic-gate  * returns NOT_BOUND.  If the door call fails or the server response is too
5607c478bd9Sstevel@tonic-gate  * small, returns CALL_FAILED.  If the server response is too big, truncates the
5617c478bd9Sstevel@tonic-gate  * response and returns RESULT_TOO_BIG.  Otherwise, the size of the result is
5627c478bd9Sstevel@tonic-gate  * returned.
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate static ssize_t
5657c478bd9Sstevel@tonic-gate make_door_call(scf_handle_t *h, const void *req, size_t req_sz,
5667c478bd9Sstevel@tonic-gate     void *res, size_t res_sz)
5677c478bd9Sstevel@tonic-gate {
5687c478bd9Sstevel@tonic-gate 	door_arg_t arg;
5697c478bd9Sstevel@tonic-gate 	int r;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	if (!handle_is_bound(h)) {
5747c478bd9Sstevel@tonic-gate 		return (NOT_BOUND);
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	arg.data_ptr = (void *)req;
5787c478bd9Sstevel@tonic-gate 	arg.data_size = req_sz;
5797c478bd9Sstevel@tonic-gate 	arg.desc_ptr = NULL;
5807c478bd9Sstevel@tonic-gate 	arg.desc_num = 0;
5817c478bd9Sstevel@tonic-gate 	arg.rbuf = res;
5827c478bd9Sstevel@tonic-gate 	arg.rsize = res_sz;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	while ((r = door_call(h->rh_doorfd, &arg)) < 0) {
5857c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
5867c478bd9Sstevel@tonic-gate 			break;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (r < 0) {
5907c478bd9Sstevel@tonic-gate 		return (CALL_FAILED);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	if (arg.desc_num > 0) {
5947c478bd9Sstevel@tonic-gate 		while (arg.desc_num > 0) {
5957c478bd9Sstevel@tonic-gate 			if (arg.desc_ptr->d_attributes & DOOR_DESCRIPTOR) {
5967c478bd9Sstevel@tonic-gate 				int cfd = arg.desc_ptr->d_data.d_desc.d_id;
5977c478bd9Sstevel@tonic-gate 				(void) close(cfd);
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 			arg.desc_ptr++;
6007c478bd9Sstevel@tonic-gate 			arg.desc_num--;
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 	if (arg.data_ptr != res && arg.data_size > 0)
6047c478bd9Sstevel@tonic-gate 		(void) memmove(res, arg.data_ptr, MIN(arg.data_size, res_sz));
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (arg.rbuf != res)
6077c478bd9Sstevel@tonic-gate 		(void) munmap(arg.rbuf, arg.rsize);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (arg.data_size > res_sz)
6107c478bd9Sstevel@tonic-gate 		return (RESULT_TOO_BIG);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if (arg.data_size < sizeof (uint32_t))
6137c478bd9Sstevel@tonic-gate 		return (CALL_FAILED);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	return (arg.data_size);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * Should only be used when r < 0.
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate #define	DOOR_ERRORS_BLOCK(r)	{					\
6227c478bd9Sstevel@tonic-gate 	switch (r) {							\
6237c478bd9Sstevel@tonic-gate 	case NOT_BOUND:							\
6247c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_BOUND));		\
6257c478bd9Sstevel@tonic-gate 									\
6267c478bd9Sstevel@tonic-gate 	case CALL_FAILED:						\
6277c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN));	\
6287c478bd9Sstevel@tonic-gate 									\
6297c478bd9Sstevel@tonic-gate 	case RESULT_TOO_BIG:						\
6307c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INTERNAL));		\
6317c478bd9Sstevel@tonic-gate 									\
6327c478bd9Sstevel@tonic-gate 	default:							\
6337c478bd9Sstevel@tonic-gate 		assert(r == NOT_BOUND || r == CALL_FAILED ||		\
6347c478bd9Sstevel@tonic-gate 		    r == RESULT_TOO_BIG);				\
6357c478bd9Sstevel@tonic-gate 		abort();						\
6367c478bd9Sstevel@tonic-gate 	}								\
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * Like make_door_call(), but takes an fd instead of a handle, and expects
6417c478bd9Sstevel@tonic-gate  * a single file descriptor, returned via res_fd.
6427c478bd9Sstevel@tonic-gate  *
6437c478bd9Sstevel@tonic-gate  * If no file descriptor is returned, *res_fd == -1.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate static int
6467c478bd9Sstevel@tonic-gate make_door_call_retfd(int fd, const void *req, size_t req_sz, void *res,
6477c478bd9Sstevel@tonic-gate     size_t res_sz, int *res_fd)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	door_arg_t arg;
6507c478bd9Sstevel@tonic-gate 	int r;
6517c478bd9Sstevel@tonic-gate 	char rbuf[256];
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	*res_fd = -1;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	if (fd == -1)
6567c478bd9Sstevel@tonic-gate 		return (NOT_BOUND);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	arg.data_ptr = (void *)req;
6597c478bd9Sstevel@tonic-gate 	arg.data_size = req_sz;
6607c478bd9Sstevel@tonic-gate 	arg.desc_ptr = NULL;
6617c478bd9Sstevel@tonic-gate 	arg.desc_num = 0;
6627c478bd9Sstevel@tonic-gate 	arg.rbuf = rbuf;
6637c478bd9Sstevel@tonic-gate 	arg.rsize = sizeof (rbuf);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	while ((r = door_call(fd, &arg)) < 0) {
6667c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
6677c478bd9Sstevel@tonic-gate 			break;
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	if (r < 0)
6717c478bd9Sstevel@tonic-gate 		return (CALL_FAILED);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if (arg.desc_num > 1) {
6747c478bd9Sstevel@tonic-gate 		while (arg.desc_num > 0) {
6757c478bd9Sstevel@tonic-gate 			if (arg.desc_ptr->d_attributes & DOOR_DESCRIPTOR) {
6767c478bd9Sstevel@tonic-gate 				int cfd =
6777c478bd9Sstevel@tonic-gate 				    arg.desc_ptr->d_data.d_desc.d_descriptor;
6787c478bd9Sstevel@tonic-gate 				(void) close(cfd);
6797c478bd9Sstevel@tonic-gate 			}
6807c478bd9Sstevel@tonic-gate 			arg.desc_ptr++;
6817c478bd9Sstevel@tonic-gate 			arg.desc_num--;
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 	if (arg.desc_num == 1 && arg.desc_ptr->d_attributes & DOOR_DESCRIPTOR)
6857c478bd9Sstevel@tonic-gate 		*res_fd = arg.desc_ptr->d_data.d_desc.d_descriptor;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if (arg.data_size > 0)
6887c478bd9Sstevel@tonic-gate 		(void) memmove(res, arg.data_ptr, MIN(arg.data_size, res_sz));
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	if (arg.rbuf != rbuf)
6917c478bd9Sstevel@tonic-gate 		(void) munmap(arg.rbuf, arg.rsize);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	if (arg.data_size > res_sz)
6947c478bd9Sstevel@tonic-gate 		return (RESULT_TOO_BIG);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if (arg.data_size < sizeof (uint32_t))
6977c478bd9Sstevel@tonic-gate 		return (CALL_FAILED);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	return (arg.data_size);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate /*
7037c478bd9Sstevel@tonic-gate  * Fails with
7047c478bd9Sstevel@tonic-gate  *   _VERSION_MISMATCH
7057c478bd9Sstevel@tonic-gate  *   _NO_MEMORY
7067c478bd9Sstevel@tonic-gate  */
7077c478bd9Sstevel@tonic-gate scf_handle_t *
7087c478bd9Sstevel@tonic-gate scf_handle_create(scf_version_t v)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	scf_handle_t *ret;
7117c478bd9Sstevel@tonic-gate 	int failed;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	/*
7147c478bd9Sstevel@tonic-gate 	 * This will need to be revisited when we bump SCF_VERSION
7157c478bd9Sstevel@tonic-gate 	 */
7167c478bd9Sstevel@tonic-gate 	if (v != SCF_VERSION) {
7177c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_VERSION_MISMATCH);
7187c478bd9Sstevel@tonic-gate 		return (NULL);
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if (!lowlevel_init()) {
7227c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
7237c478bd9Sstevel@tonic-gate 		return (NULL);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
7277c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
7287c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
7297c478bd9Sstevel@tonic-gate 		return (NULL);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	ret->rh_dataels = uu_list_create(datael_pool, ret, 0);
7337c478bd9Sstevel@tonic-gate 	ret->rh_iters = uu_list_create(iter_pool, ret, 0);
7347c478bd9Sstevel@tonic-gate 	if (ret->rh_dataels == NULL || ret->rh_iters == NULL) {
7357c478bd9Sstevel@tonic-gate 		if (ret->rh_dataels != NULL)
7367c478bd9Sstevel@tonic-gate 			uu_list_destroy(ret->rh_dataels);
7377c478bd9Sstevel@tonic-gate 		if (ret->rh_iters != NULL)
7387c478bd9Sstevel@tonic-gate 			uu_list_destroy(ret->rh_iters);
7397c478bd9Sstevel@tonic-gate 		uu_free(ret);
7407c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
7417c478bd9Sstevel@tonic-gate 		return (NULL);
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	ret->rh_doorfd = -1;
7457c478bd9Sstevel@tonic-gate 	ret->rh_doorfd_old = -1;
7467c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&ret->rh_lock, NULL);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	handle_hold_subhandles(ret, RH_HOLD_ALL);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	failed = ((ret->rh_iter = scf_iter_create(ret)) == NULL ||
7517c478bd9Sstevel@tonic-gate 	    (ret->rh_scope = scf_scope_create(ret)) == NULL ||
7527c478bd9Sstevel@tonic-gate 	    (ret->rh_service = scf_service_create(ret)) == NULL ||
7537c478bd9Sstevel@tonic-gate 	    (ret->rh_instance = scf_instance_create(ret)) == NULL ||
7547c478bd9Sstevel@tonic-gate 	    (ret->rh_snapshot = scf_snapshot_create(ret)) == NULL ||
7557c478bd9Sstevel@tonic-gate 	    (ret->rh_snaplvl = scf_snaplevel_create(ret)) == NULL ||
7567c478bd9Sstevel@tonic-gate 	    (ret->rh_pg = scf_pg_create(ret)) == NULL ||
7577c478bd9Sstevel@tonic-gate 	    (ret->rh_property = scf_property_create(ret)) == NULL ||
7587c478bd9Sstevel@tonic-gate 	    (ret->rh_value = scf_value_create(ret)) == NULL);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	/*
7617c478bd9Sstevel@tonic-gate 	 * these subhandles count as internal references, not external ones.
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 	ret->rh_intrefs = ret->rh_extrefs;
7647c478bd9Sstevel@tonic-gate 	ret->rh_extrefs = 0;
7657c478bd9Sstevel@tonic-gate 	handle_rele_subhandles(ret, RH_HOLD_ALL);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (failed) {
7687c478bd9Sstevel@tonic-gate 		scf_handle_destroy(ret);
7697c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
7707c478bd9Sstevel@tonic-gate 		return (NULL);
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	scf_value_set_count(ret->rh_value, default_debug);
7747c478bd9Sstevel@tonic-gate 	(void) scf_handle_decorate(ret, "debug", ret->rh_value);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	return (ret);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate int
7807c478bd9Sstevel@tonic-gate scf_handle_decorate(scf_handle_t *handle, const char *name, scf_value_t *v)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	if (v != SCF_DECORATE_CLEAR && handle != v->value_handle)
7837c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&handle->rh_lock);
7867c478bd9Sstevel@tonic-gate 	if (handle_is_bound(handle)) {
7877c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&handle->rh_lock);
7887c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_IN_USE));
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&handle->rh_lock);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if (strcmp(name, "debug") == 0) {
7937c478bd9Sstevel@tonic-gate 		if (v == SCF_DECORATE_CLEAR) {
7947c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&handle->rh_lock);
7957c478bd9Sstevel@tonic-gate 			handle->rh_debug = 0;
7967c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&handle->rh_lock);
7977c478bd9Sstevel@tonic-gate 		} else {
7987c478bd9Sstevel@tonic-gate 			uint64_t val;
7997c478bd9Sstevel@tonic-gate 			if (scf_value_get_count(v, &val) < 0)
8007c478bd9Sstevel@tonic-gate 				return (-1);		/* error already set */
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&handle->rh_lock);
8037c478bd9Sstevel@tonic-gate 			handle->rh_debug = (uid_t)val;
8047c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&handle->rh_lock);
8057c478bd9Sstevel@tonic-gate 		}
8067c478bd9Sstevel@tonic-gate 		return (0);
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 	if (strcmp(name, "door_path") == 0) {
8097c478bd9Sstevel@tonic-gate 		char name[sizeof (handle->rh_doorpath)];
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 		if (v == SCF_DECORATE_CLEAR) {
8127c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&handle->rh_lock);
8137c478bd9Sstevel@tonic-gate 			handle->rh_doorpath[0] = 0;
8147c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&handle->rh_lock);
8157c478bd9Sstevel@tonic-gate 		} else {
8167c478bd9Sstevel@tonic-gate 			ssize_t len;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 			if ((len = scf_value_get_astring(v, name,
8197c478bd9Sstevel@tonic-gate 			    sizeof (name))) < 0) {
8207c478bd9Sstevel@tonic-gate 				return (-1);		/* error already set */
8217c478bd9Sstevel@tonic-gate 			}
8227c478bd9Sstevel@tonic-gate 			if (len == 0 || len >= sizeof (name)) {
8237c478bd9Sstevel@tonic-gate 				return (scf_set_error(
8247c478bd9Sstevel@tonic-gate 				    SCF_ERROR_INVALID_ARGUMENT));
8257c478bd9Sstevel@tonic-gate 			}
8267c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&handle->rh_lock);
8277c478bd9Sstevel@tonic-gate 			(void) strlcpy(handle->rh_doorpath, name,
8287c478bd9Sstevel@tonic-gate 			    sizeof (handle->rh_doorpath));
8297c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&handle->rh_lock);
8307c478bd9Sstevel@tonic-gate 		}
8317c478bd9Sstevel@tonic-gate 		return (0);
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 	return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate /*
8377c478bd9Sstevel@tonic-gate  * fails with INVALID_ARGUMENT and HANDLE_MISMATCH.
8387c478bd9Sstevel@tonic-gate  */
8397c478bd9Sstevel@tonic-gate int
8407c478bd9Sstevel@tonic-gate _scf_handle_decorations(scf_handle_t *handle, scf_decoration_func *f,
8417c478bd9Sstevel@tonic-gate     scf_value_t *v, void *data)
8427c478bd9Sstevel@tonic-gate {
8437c478bd9Sstevel@tonic-gate 	scf_decoration_info_t i;
8447c478bd9Sstevel@tonic-gate 	char name[sizeof (handle->rh_doorpath)];
8457c478bd9Sstevel@tonic-gate 	uint64_t debug;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	if (f == NULL || v == NULL)
8487c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if (v->value_handle != handle)
8517c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	i.sdi_name = (const char *)"debug";
8547c478bd9Sstevel@tonic-gate 	i.sdi_type = SCF_TYPE_COUNT;
8557c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&handle->rh_lock);
8567c478bd9Sstevel@tonic-gate 	debug = handle->rh_debug;
8577c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&handle->rh_lock);
8587c478bd9Sstevel@tonic-gate 	if (debug != 0) {
8597c478bd9Sstevel@tonic-gate 		scf_value_set_count(v, debug);
8607c478bd9Sstevel@tonic-gate 		i.sdi_value = v;
8617c478bd9Sstevel@tonic-gate 	} else {
8627c478bd9Sstevel@tonic-gate 		i.sdi_value = SCF_DECORATE_CLEAR;
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	if ((*f)(&i, data) == 0)
8667c478bd9Sstevel@tonic-gate 		return (0);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	i.sdi_name = (const char *)"door_path";
8697c478bd9Sstevel@tonic-gate 	i.sdi_type = SCF_TYPE_ASTRING;
8707c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&handle->rh_lock);
8717c478bd9Sstevel@tonic-gate 	(void) strlcpy(name, handle->rh_doorpath, sizeof (name));
8727c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&handle->rh_lock);
8737c478bd9Sstevel@tonic-gate 	if (name[0] != 0) {
8747c478bd9Sstevel@tonic-gate 		(void) scf_value_set_astring(v, name);
8757c478bd9Sstevel@tonic-gate 		i.sdi_value = v;
8767c478bd9Sstevel@tonic-gate 	} else {
8777c478bd9Sstevel@tonic-gate 		i.sdi_value = SCF_DECORATE_CLEAR;
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	if ((*f)(&i, data) == 0)
8817c478bd9Sstevel@tonic-gate 		return (0);
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	return (1);
8847c478bd9Sstevel@tonic-gate }
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate /*
8877c478bd9Sstevel@tonic-gate  * Fails if handle is not bound.
8887c478bd9Sstevel@tonic-gate  */
8897c478bd9Sstevel@tonic-gate static int
8907c478bd9Sstevel@tonic-gate handle_unbind_unlocked(scf_handle_t *handle)
8917c478bd9Sstevel@tonic-gate {
8927c478bd9Sstevel@tonic-gate 	rep_protocol_request_t request;
8937c478bd9Sstevel@tonic-gate 	rep_protocol_response_t response;
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	if (!handle_is_bound(handle))
8967c478bd9Sstevel@tonic-gate 		return (-1);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_CLOSE;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	(void) make_door_call(handle, &request, sizeof (request),
9017c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	handle_do_close(handle);
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate /*
9097c478bd9Sstevel@tonic-gate  * Fails with
9107c478bd9Sstevel@tonic-gate  *   _HANDLE_DESTROYED - dp's handle has been destroyed
9117c478bd9Sstevel@tonic-gate  *   _INTERNAL - server response too big
9127c478bd9Sstevel@tonic-gate  *		 entity already set up with different type
9137c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - server out of memory
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate static int
9167c478bd9Sstevel@tonic-gate datael_attach(scf_datael_t *dp)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_setup request;
9217c478bd9Sstevel@tonic-gate 	rep_protocol_response_t response;
9227c478bd9Sstevel@tonic-gate 	ssize_t r;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	dp->rd_reset = 0;		/* setup implicitly resets */
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (h->rh_flags & HANDLE_DEAD)
9297c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_DESTROYED));
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	if (!handle_is_bound(h))
9327c478bd9Sstevel@tonic-gate 		return (SCF_SUCCESS);		/* nothing to do */
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_SETUP;
9357c478bd9Sstevel@tonic-gate 	request.rpr_entityid = dp->rd_entity;
9367c478bd9Sstevel@tonic-gate 	request.rpr_entitytype = dp->rd_type;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
9397c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (r == NOT_BOUND || r == CALL_FAILED)
9427c478bd9Sstevel@tonic-gate 		return (SCF_SUCCESS);
9437c478bd9Sstevel@tonic-gate 	if (r == RESULT_TOO_BIG)
9447c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INTERNAL));
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
9477c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate /*
9537c478bd9Sstevel@tonic-gate  * Fails with
9547c478bd9Sstevel@tonic-gate  *   _HANDLE_DESTROYED - iter's handle has been destroyed
9557c478bd9Sstevel@tonic-gate  *   _INTERNAL - server response too big
9567c478bd9Sstevel@tonic-gate  *		 iter already existed
9577c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES
9587c478bd9Sstevel@tonic-gate  */
9597c478bd9Sstevel@tonic-gate static int
9607c478bd9Sstevel@tonic-gate iter_attach(scf_iter_t *iter)
9617c478bd9Sstevel@tonic-gate {
9627c478bd9Sstevel@tonic-gate 	scf_handle_t *h = iter->iter_handle;
9637c478bd9Sstevel@tonic-gate 	struct rep_protocol_iter_request request;
9647c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
9657c478bd9Sstevel@tonic-gate 	int r;
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (h->rh_flags & HANDLE_DEAD)
9707c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_DESTROYED));
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	if (!handle_is_bound(h))
9737c478bd9Sstevel@tonic-gate 		return (SCF_SUCCESS);		/* nothing to do */
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ITER_SETUP;
9767c478bd9Sstevel@tonic-gate 	request.rpr_iterid = iter->iter_id;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
9797c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	if (r == NOT_BOUND || r == CALL_FAILED)
9827c478bd9Sstevel@tonic-gate 		return (SCF_SUCCESS);
9837c478bd9Sstevel@tonic-gate 	if (r == RESULT_TOO_BIG)
9847c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INTERNAL));
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
9877c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate /*
9937c478bd9Sstevel@tonic-gate  * Fails with
9947c478bd9Sstevel@tonic-gate  *   _IN_USE - handle already bound
9957c478bd9Sstevel@tonic-gate  *   _NO_SERVER - server door could not be open()ed
9967c478bd9Sstevel@tonic-gate  *		  door call failed
9977c478bd9Sstevel@tonic-gate  *		  door_info() failed
9987c478bd9Sstevel@tonic-gate  *   _VERSION_MISMATCH - server returned bad file descriptor
9997c478bd9Sstevel@tonic-gate  *			 server claimed bad request
10007c478bd9Sstevel@tonic-gate  *			 server reported version mismatch
10017c478bd9Sstevel@tonic-gate  *			 server refused with unknown reason
10027c478bd9Sstevel@tonic-gate  *   _INVALID_ARGUMENT
10037c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES - server is out of memory
10047c478bd9Sstevel@tonic-gate  *   _PERMISSION_DENIED
10057c478bd9Sstevel@tonic-gate  *   _INTERNAL - could not set up entities or iters
10067c478bd9Sstevel@tonic-gate  *		 server response too big
10077c478bd9Sstevel@tonic-gate  *
10087c478bd9Sstevel@tonic-gate  * perhaps this should try multiple times.
10097c478bd9Sstevel@tonic-gate  */
10107c478bd9Sstevel@tonic-gate int
10117c478bd9Sstevel@tonic-gate scf_handle_bind(scf_handle_t *handle)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	scf_datael_t *el;
10147c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	pid_t pid;
10177c478bd9Sstevel@tonic-gate 	int fd;
10187c478bd9Sstevel@tonic-gate 	int res;
10197c478bd9Sstevel@tonic-gate 	door_info_t info;
10207c478bd9Sstevel@tonic-gate 	repository_door_request_t request;
10217c478bd9Sstevel@tonic-gate 	repository_door_response_t response;
10227c478bd9Sstevel@tonic-gate 	const char *door_name = default_door_path;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&handle->rh_lock);
10257c478bd9Sstevel@tonic-gate 	if (handle_is_bound(handle)) {
10267c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&handle->rh_lock);
10277c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_IN_USE));
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	/* wait until any active fd users have cleared out */
1031*a574db85Sraf 	while (handle->rh_fd_users > 0) {
1032*a574db85Sraf 		int cancel_state;
1033*a574db85Sraf 
1034*a574db85Sraf 		(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,
1035*a574db85Sraf 		    &cancel_state);
1036*a574db85Sraf 		(void) pthread_cond_wait(&handle->rh_cv, &handle->rh_lock);
1037*a574db85Sraf 		(void) pthread_setcancelstate(cancel_state, NULL);
1038*a574db85Sraf 	}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/* check again, since we had to drop the lock */
10417c478bd9Sstevel@tonic-gate 	if (handle_is_bound(handle)) {
10427c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&handle->rh_lock);
10437c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_IN_USE));
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	assert(handle->rh_doorfd == -1 && handle->rh_doorfd_old == -1);
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	if (handle->rh_doorpath[0] != 0)
10497c478bd9Sstevel@tonic-gate 		door_name = handle->rh_doorpath;
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	fd = open(door_name, O_RDONLY, 0);
10527c478bd9Sstevel@tonic-gate 	if (fd == -1) {
10537c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&handle->rh_lock);
10547c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NO_SERVER));
10557c478bd9Sstevel@tonic-gate 	}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	request.rdr_version = REPOSITORY_DOOR_VERSION;
10587c478bd9Sstevel@tonic-gate 	request.rdr_request = REPOSITORY_DOOR_REQUEST_CONNECT;
10597c478bd9Sstevel@tonic-gate 	request.rdr_flags = handle->rh_flags;
10607c478bd9Sstevel@tonic-gate 	request.rdr_debug = handle->rh_debug;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	pid = getpid();
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	res = make_door_call_retfd(fd, &request, sizeof (request),
10657c478bd9Sstevel@tonic-gate 	    &response, sizeof (response), &handle->rh_doorfd);
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	(void) close(fd);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	if (res < 0) {
10707c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&handle->rh_lock);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 		assert(res != NOT_BOUND);
10737c478bd9Sstevel@tonic-gate 		if (res == CALL_FAILED)
10747c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_NO_SERVER));
10757c478bd9Sstevel@tonic-gate 		assert(res == RESULT_TOO_BIG);
10767c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INTERNAL));
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	if (handle->rh_doorfd < 0) {
10807c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&handle->rh_lock);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 		switch (response.rdr_status) {
10837c478bd9Sstevel@tonic-gate 		case REPOSITORY_DOOR_SUCCESS:
10847c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_VERSION_MISMATCH));
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 		case REPOSITORY_DOOR_FAIL_BAD_REQUEST:
10877c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_VERSION_MISMATCH));
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 		case REPOSITORY_DOOR_FAIL_VERSION_MISMATCH:
10907c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_VERSION_MISMATCH));
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 		case REPOSITORY_DOOR_FAIL_BAD_FLAG:
10937c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 		case REPOSITORY_DOOR_FAIL_NO_RESOURCES:
10967c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_NO_RESOURCES));
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 		case REPOSITORY_DOOR_FAIL_PERMISSION_DENIED:
10997c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_PERMISSION_DENIED));
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 		default:
11027c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_VERSION_MISMATCH));
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 	}
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	(void) fcntl(handle->rh_doorfd, F_SETFD, FD_CLOEXEC);
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if (door_info(handle->rh_doorfd, &info) < 0) {
11097c478bd9Sstevel@tonic-gate 		(void) close(handle->rh_doorfd);
11107c478bd9Sstevel@tonic-gate 		handle->rh_doorfd = -1;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&handle->rh_lock);
11137c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NO_SERVER));
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	handle->rh_doorpid = pid;
11177c478bd9Sstevel@tonic-gate 	handle->rh_doorid = info.di_uniquifier;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	/*
11207c478bd9Sstevel@tonic-gate 	 * Now, re-attach everything
11217c478bd9Sstevel@tonic-gate 	 */
11227c478bd9Sstevel@tonic-gate 	for (el = uu_list_first(handle->rh_dataels); el != NULL;
11237c478bd9Sstevel@tonic-gate 	    el = uu_list_next(handle->rh_dataels, el)) {
11247c478bd9Sstevel@tonic-gate 		if (datael_attach(el) == -1) {
11257c478bd9Sstevel@tonic-gate 			assert(scf_error() != SCF_ERROR_HANDLE_DESTROYED);
11267c478bd9Sstevel@tonic-gate 			(void) handle_unbind_unlocked(handle);
11277c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&handle->rh_lock);
11287c478bd9Sstevel@tonic-gate 			return (-1);
11297c478bd9Sstevel@tonic-gate 		}
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	for (iter = uu_list_first(handle->rh_iters); iter != NULL;
11337c478bd9Sstevel@tonic-gate 	    iter = uu_list_next(handle->rh_iters, iter)) {
11347c478bd9Sstevel@tonic-gate 		if (iter_attach(iter) == -1) {
11357c478bd9Sstevel@tonic-gate 			assert(scf_error() != SCF_ERROR_HANDLE_DESTROYED);
11367c478bd9Sstevel@tonic-gate 			(void) handle_unbind_unlocked(handle);
11377c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&handle->rh_lock);
11387c478bd9Sstevel@tonic-gate 			return (-1);
11397c478bd9Sstevel@tonic-gate 		}
11407c478bd9Sstevel@tonic-gate 	}
11417c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&handle->rh_lock);
11427c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate int
11467c478bd9Sstevel@tonic-gate scf_handle_unbind(scf_handle_t *handle)
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate 	int ret;
11497c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&handle->rh_lock);
11507c478bd9Sstevel@tonic-gate 	ret = handle_unbind_unlocked(handle);
11517c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&handle->rh_lock);
11527c478bd9Sstevel@tonic-gate 	return (ret == SCF_SUCCESS ? ret : scf_set_error(SCF_ERROR_NOT_BOUND));
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate static scf_handle_t *
11567c478bd9Sstevel@tonic-gate handle_get(scf_handle_t *h)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
11597c478bd9Sstevel@tonic-gate 	if (h->rh_flags & HANDLE_DEAD) {
11607c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
11617c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_HANDLE_DESTROYED);
11627c478bd9Sstevel@tonic-gate 		return (NULL);
11637c478bd9Sstevel@tonic-gate 	}
11647c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
11657c478bd9Sstevel@tonic-gate 	return (h);
11667c478bd9Sstevel@tonic-gate }
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate /*
11697c478bd9Sstevel@tonic-gate  * Called when an object is removed from the handle.  On the last remove,
11707c478bd9Sstevel@tonic-gate  * cleans up and frees the handle.
11717c478bd9Sstevel@tonic-gate  */
11727c478bd9Sstevel@tonic-gate static void
11737c478bd9Sstevel@tonic-gate handle_unrefed(scf_handle_t *handle)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
11767c478bd9Sstevel@tonic-gate 	scf_value_t *v;
11777c478bd9Sstevel@tonic-gate 	scf_scope_t *sc;
11787c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
11797c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
11807c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
11817c478bd9Sstevel@tonic-gate 	scf_snaplevel_t *snaplvl;
11827c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
11837c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&handle->rh_lock));
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	/*
11887c478bd9Sstevel@tonic-gate 	 * Don't do anything if the handle has not yet been destroyed, there
11897c478bd9Sstevel@tonic-gate 	 * are still external references, or we're already doing unrefed
11907c478bd9Sstevel@tonic-gate 	 * handling.
11917c478bd9Sstevel@tonic-gate 	 */
11927c478bd9Sstevel@tonic-gate 	if (!(handle->rh_flags & HANDLE_DEAD) ||
11937c478bd9Sstevel@tonic-gate 	    handle->rh_extrefs > 0 ||
11947c478bd9Sstevel@tonic-gate 	    handle->rh_fd_users > 0 ||
11957c478bd9Sstevel@tonic-gate 	    (handle->rh_flags & HANDLE_UNREFED)) {
11967c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&handle->rh_lock);
11977c478bd9Sstevel@tonic-gate 		return;
11987c478bd9Sstevel@tonic-gate 	}
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	handle->rh_flags |= HANDLE_UNREFED;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	/*
12037c478bd9Sstevel@tonic-gate 	 * Now that we know that there are no external references, and the
12047c478bd9Sstevel@tonic-gate 	 * HANDLE_DEAD flag keeps new ones from appearing, we can clean up
12057c478bd9Sstevel@tonic-gate 	 * our subhandles and destroy the handle completely.
12067c478bd9Sstevel@tonic-gate 	 */
12077c478bd9Sstevel@tonic-gate 	assert(handle->rh_intrefs >= 0);
12087c478bd9Sstevel@tonic-gate 	handle->rh_extrefs = handle->rh_intrefs;
12097c478bd9Sstevel@tonic-gate 	handle->rh_intrefs = 0;
12107c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&handle->rh_lock);
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	handle_hold_subhandles(handle, RH_HOLD_ALL);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	iter = handle->rh_iter;
12157c478bd9Sstevel@tonic-gate 	sc = handle->rh_scope;
12167c478bd9Sstevel@tonic-gate 	svc = handle->rh_service;
12177c478bd9Sstevel@tonic-gate 	inst = handle->rh_instance;
12187c478bd9Sstevel@tonic-gate 	snap = handle->rh_snapshot;
12197c478bd9Sstevel@tonic-gate 	snaplvl = handle->rh_snaplvl;
12207c478bd9Sstevel@tonic-gate 	pg = handle->rh_pg;
12217c478bd9Sstevel@tonic-gate 	prop = handle->rh_property;
12227c478bd9Sstevel@tonic-gate 	v = handle->rh_value;
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	handle->rh_iter = NULL;
12257c478bd9Sstevel@tonic-gate 	handle->rh_scope = NULL;
12267c478bd9Sstevel@tonic-gate 	handle->rh_service = NULL;
12277c478bd9Sstevel@tonic-gate 	handle->rh_instance = NULL;
12287c478bd9Sstevel@tonic-gate 	handle->rh_snapshot = NULL;
12297c478bd9Sstevel@tonic-gate 	handle->rh_snaplvl = NULL;
12307c478bd9Sstevel@tonic-gate 	handle->rh_pg = NULL;
12317c478bd9Sstevel@tonic-gate 	handle->rh_property = NULL;
12327c478bd9Sstevel@tonic-gate 	handle->rh_value = NULL;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	if (iter != NULL)
12357c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
12367c478bd9Sstevel@tonic-gate 	if (sc != NULL)
12377c478bd9Sstevel@tonic-gate 		scf_scope_destroy(sc);
12387c478bd9Sstevel@tonic-gate 	if (svc != NULL)
12397c478bd9Sstevel@tonic-gate 		scf_service_destroy(svc);
12407c478bd9Sstevel@tonic-gate 	if (inst != NULL)
12417c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
12427c478bd9Sstevel@tonic-gate 	if (snap != NULL)
12437c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
12447c478bd9Sstevel@tonic-gate 	if (snaplvl != NULL)
12457c478bd9Sstevel@tonic-gate 		scf_snaplevel_destroy(snaplvl);
12467c478bd9Sstevel@tonic-gate 	if (pg != NULL)
12477c478bd9Sstevel@tonic-gate 		scf_pg_destroy(pg);
12487c478bd9Sstevel@tonic-gate 	if (prop != NULL)
12497c478bd9Sstevel@tonic-gate 		scf_property_destroy(prop);
12507c478bd9Sstevel@tonic-gate 	if (v != NULL)
12517c478bd9Sstevel@tonic-gate 		scf_value_destroy(v);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&handle->rh_lock);
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	/* there should be no outstanding children at this point */
12567c478bd9Sstevel@tonic-gate 	assert(handle->rh_extrefs == 0);
12577c478bd9Sstevel@tonic-gate 	assert(handle->rh_intrefs == 0);
12587c478bd9Sstevel@tonic-gate 	assert(handle->rh_values == 0);
12597c478bd9Sstevel@tonic-gate 	assert(handle->rh_entries == 0);
12607c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(handle->rh_dataels) == 0);
12617c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(handle->rh_iters) == 0);
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	uu_list_destroy(handle->rh_dataels);
12647c478bd9Sstevel@tonic-gate 	uu_list_destroy(handle->rh_iters);
12657c478bd9Sstevel@tonic-gate 	handle->rh_dataels = NULL;
12667c478bd9Sstevel@tonic-gate 	handle->rh_iters = NULL;
12677c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&handle->rh_lock);
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&handle->rh_lock);
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	uu_free(handle);
12727c478bd9Sstevel@tonic-gate }
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate void
12757c478bd9Sstevel@tonic-gate scf_handle_destroy(scf_handle_t *handle)
12767c478bd9Sstevel@tonic-gate {
12777c478bd9Sstevel@tonic-gate 	if (handle == NULL)
12787c478bd9Sstevel@tonic-gate 		return;
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&handle->rh_lock);
12817c478bd9Sstevel@tonic-gate 	if (handle->rh_flags & HANDLE_DEAD) {
12827c478bd9Sstevel@tonic-gate 		/*
12837c478bd9Sstevel@tonic-gate 		 * This is an error (you are not allowed to reference the
12847c478bd9Sstevel@tonic-gate 		 * handle after it is destroyed), but we can't report it.
12857c478bd9Sstevel@tonic-gate 		 */
12867c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&handle->rh_lock);
12877c478bd9Sstevel@tonic-gate 		return;
12887c478bd9Sstevel@tonic-gate 	}
12897c478bd9Sstevel@tonic-gate 	handle->rh_flags |= HANDLE_DEAD;
12907c478bd9Sstevel@tonic-gate 	(void) handle_unbind_unlocked(handle);
12917c478bd9Sstevel@tonic-gate 	handle_unrefed(handle);
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate ssize_t
12957c478bd9Sstevel@tonic-gate scf_myname(scf_handle_t *h, char *out, size_t len)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	char *cp;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	if (!handle_has_server(h))
13007c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN));
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	cp = getenv("SMF_FMRI");
13037c478bd9Sstevel@tonic-gate 	if (cp == NULL)
13047c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	return (strlcpy(out, cp, len));
13077c478bd9Sstevel@tonic-gate }
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate static uint32_t
13108918dff3Sjwadams handle_alloc_entityid(scf_handle_t *h)
13117c478bd9Sstevel@tonic-gate {
13128918dff3Sjwadams 	uint32_t nextid;
13138918dff3Sjwadams 
13148918dff3Sjwadams 	assert(MUTEX_HELD(&h->rh_lock));
13158918dff3Sjwadams 
13168918dff3Sjwadams 	if (uu_list_numnodes(h->rh_dataels) == UINT32_MAX)
13178918dff3Sjwadams 		return (0);		/* no ids available */
13188918dff3Sjwadams 
13198918dff3Sjwadams 	/*
13208918dff3Sjwadams 	 * The following loop assumes that there are not a huge number of
13218918dff3Sjwadams 	 * outstanding entities when we've wrapped.  If that ends up not
13228918dff3Sjwadams 	 * being the case, the O(N^2) nature of this search will hurt a lot,
13238918dff3Sjwadams 	 * and the data structure should be switched to an AVL tree.
13248918dff3Sjwadams 	 */
13258918dff3Sjwadams 	nextid = h->rh_nextentity + 1;
13268918dff3Sjwadams 	for (;;) {
13278918dff3Sjwadams 		scf_datael_t *cur;
13288918dff3Sjwadams 
13298918dff3Sjwadams 		if (nextid == 0) {
13308918dff3Sjwadams 			nextid++;
13318918dff3Sjwadams 			h->rh_flags |= HANDLE_WRAPPED_ENTITY;
13328918dff3Sjwadams 		}
13338918dff3Sjwadams 		if (!(h->rh_flags & HANDLE_WRAPPED_ENTITY))
13348918dff3Sjwadams 			break;
13358918dff3Sjwadams 
13368918dff3Sjwadams 		cur = uu_list_find(h->rh_dataels, NULL, &nextid, NULL);
13378918dff3Sjwadams 		if (cur == NULL)
13388918dff3Sjwadams 			break;		/* not in use */
13398918dff3Sjwadams 
13408918dff3Sjwadams 		if (nextid == h->rh_nextentity)
13418918dff3Sjwadams 			return (0);	/* wrapped around; no ids available */
13428918dff3Sjwadams 		nextid++;
13438918dff3Sjwadams 	}
13448918dff3Sjwadams 
13458918dff3Sjwadams 	h->rh_nextentity = nextid;
13468918dff3Sjwadams 	return (nextid);
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate static uint32_t
13508918dff3Sjwadams handle_alloc_iterid(scf_handle_t *h)
13517c478bd9Sstevel@tonic-gate {
13528918dff3Sjwadams 	uint32_t nextid;
13538918dff3Sjwadams 
13548918dff3Sjwadams 	assert(MUTEX_HELD(&h->rh_lock));
13558918dff3Sjwadams 
13568918dff3Sjwadams 	if (uu_list_numnodes(h->rh_iters) == UINT32_MAX)
13578918dff3Sjwadams 		return (0);		/* no ids available */
13588918dff3Sjwadams 
13598918dff3Sjwadams 	/* see the comment in handle_alloc_entityid */
13608918dff3Sjwadams 	nextid = h->rh_nextiter + 1;
13618918dff3Sjwadams 	for (;;) {
13628918dff3Sjwadams 		scf_iter_t *cur;
13638918dff3Sjwadams 
13648918dff3Sjwadams 		if (nextid == 0) {
13658918dff3Sjwadams 			nextid++;
13668918dff3Sjwadams 			h->rh_flags |= HANDLE_WRAPPED_ITER;
13678918dff3Sjwadams 		}
13688918dff3Sjwadams 		if (!(h->rh_flags & HANDLE_WRAPPED_ITER))
13698918dff3Sjwadams 			break;			/* not yet wrapped */
13708918dff3Sjwadams 
13718918dff3Sjwadams 		cur = uu_list_find(h->rh_iters, NULL, &nextid, NULL);
13728918dff3Sjwadams 		if (cur == NULL)
13738918dff3Sjwadams 			break;		/* not in use */
13748918dff3Sjwadams 
13758918dff3Sjwadams 		if (nextid == h->rh_nextiter)
13768918dff3Sjwadams 			return (0);	/* wrapped around; no ids available */
13778918dff3Sjwadams 		nextid++;
13788918dff3Sjwadams 	}
13798918dff3Sjwadams 
13808918dff3Sjwadams 	h->rh_nextiter = nextid;
13818918dff3Sjwadams 	return (nextid);
13827c478bd9Sstevel@tonic-gate }
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate static uint32_t
13858918dff3Sjwadams handle_next_changeid(scf_handle_t *handle)
13867c478bd9Sstevel@tonic-gate {
13878918dff3Sjwadams 	uint32_t nextid;
13888918dff3Sjwadams 
13897c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&handle->rh_lock));
13908918dff3Sjwadams 
13918918dff3Sjwadams 	nextid = ++handle->rh_nextchangeid;
13928918dff3Sjwadams 	if (nextid == 0)
13938918dff3Sjwadams 		nextid = ++handle->rh_nextchangeid;
13948918dff3Sjwadams 	return (nextid);
13957c478bd9Sstevel@tonic-gate }
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate /*
13987c478bd9Sstevel@tonic-gate  * Fails with
13997c478bd9Sstevel@tonic-gate  *   _INVALID_ARGUMENT - h is NULL
14007c478bd9Sstevel@tonic-gate  *   _HANDLE_DESTROYED
14017c478bd9Sstevel@tonic-gate  *   _INTERNAL - server response too big
14027c478bd9Sstevel@tonic-gate  *		 entity already set up with different type
14037c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES
14047c478bd9Sstevel@tonic-gate  */
14057c478bd9Sstevel@tonic-gate static int
14067c478bd9Sstevel@tonic-gate datael_init(scf_datael_t *dp, scf_handle_t *h, uint32_t type)
14077c478bd9Sstevel@tonic-gate {
14087c478bd9Sstevel@tonic-gate 	int ret;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	if (h == NULL)
14117c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	uu_list_node_init(dp, &dp->rd_node, datael_pool);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	dp->rd_handle = h;
14167c478bd9Sstevel@tonic-gate 	dp->rd_type = type;
14177c478bd9Sstevel@tonic-gate 	dp->rd_reset = 0;
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
14207c478bd9Sstevel@tonic-gate 	if (h->rh_flags & HANDLE_DEAD) {
14217c478bd9Sstevel@tonic-gate 		/*
14227c478bd9Sstevel@tonic-gate 		 * we're in undefined territory (the user cannot use a handle
14237c478bd9Sstevel@tonic-gate 		 * directly after it has been destroyed), but we don't want
14247c478bd9Sstevel@tonic-gate 		 * to allow any new references to happen, so we fail here.
14257c478bd9Sstevel@tonic-gate 		 */
14267c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
14277c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_DESTROYED));
14287c478bd9Sstevel@tonic-gate 	}
14297c478bd9Sstevel@tonic-gate 	dp->rd_entity = handle_alloc_entityid(h);
14308918dff3Sjwadams 	if (dp->rd_entity == 0) {
14318918dff3Sjwadams 		(void) pthread_mutex_unlock(&h->rh_lock);
14328918dff3Sjwadams 		uu_list_node_fini(dp, &dp->rd_node, datael_pool);
14338918dff3Sjwadams 		return (scf_set_error(SCF_ERROR_NO_MEMORY));
14348918dff3Sjwadams 	}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	ret = datael_attach(dp);
14377c478bd9Sstevel@tonic-gate 	if (ret == 0) {
14387c478bd9Sstevel@tonic-gate 		(void) uu_list_insert_before(h->rh_dataels, NULL, dp);
14397c478bd9Sstevel@tonic-gate 		h->rh_extrefs++;
14407c478bd9Sstevel@tonic-gate 	} else {
14417c478bd9Sstevel@tonic-gate 		uu_list_node_fini(dp, &dp->rd_node, datael_pool);
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	return (ret);
14467c478bd9Sstevel@tonic-gate }
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate static void
14497c478bd9Sstevel@tonic-gate datael_destroy(scf_datael_t *dp)
14507c478bd9Sstevel@tonic-gate {
14517c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_teardown request;
14547c478bd9Sstevel@tonic-gate 	rep_protocol_response_t response;
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
14577c478bd9Sstevel@tonic-gate 	uu_list_remove(h->rh_dataels, dp);
14587c478bd9Sstevel@tonic-gate 	--h->rh_extrefs;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	if (handle_is_bound(h)) {
14617c478bd9Sstevel@tonic-gate 		request.rpr_request = REP_PROTOCOL_ENTITY_TEARDOWN;
14627c478bd9Sstevel@tonic-gate 		request.rpr_entityid = dp->rd_entity;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 		(void) make_door_call(h, &request, sizeof (request),
14657c478bd9Sstevel@tonic-gate 		    &response, sizeof (response));
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 	handle_unrefed(h);			/* drops h->rh_lock */
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	dp->rd_handle = NULL;
14707c478bd9Sstevel@tonic-gate }
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate static scf_handle_t *
14737c478bd9Sstevel@tonic-gate datael_handle(const scf_datael_t *dp)
14747c478bd9Sstevel@tonic-gate {
14757c478bd9Sstevel@tonic-gate 	return (handle_get(dp->rd_handle));
14767c478bd9Sstevel@tonic-gate }
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate /*
14797c478bd9Sstevel@tonic-gate  * We delay ENTITY_RESETs until right before the entity is used.  By doing
14807c478bd9Sstevel@tonic-gate  * them lazily, we remove quite a few unnecessary calls.
14817c478bd9Sstevel@tonic-gate  */
14827c478bd9Sstevel@tonic-gate static void
14837c478bd9Sstevel@tonic-gate datael_do_reset_locked(scf_datael_t *dp)
14847c478bd9Sstevel@tonic-gate {
14857c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_reset request;
14887c478bd9Sstevel@tonic-gate 	rep_protocol_response_t response;
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_RESET;
14937c478bd9Sstevel@tonic-gate 	request.rpr_entityid = dp->rd_entity;
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	(void) make_door_call(h, &request, sizeof (request),
14967c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	dp->rd_reset = 0;
14997c478bd9Sstevel@tonic-gate }
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate static void
15027c478bd9Sstevel@tonic-gate datael_reset_locked(scf_datael_t *dp)
15037c478bd9Sstevel@tonic-gate {
15047c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&dp->rd_handle->rh_lock));
15057c478bd9Sstevel@tonic-gate 	dp->rd_reset = 1;
15067c478bd9Sstevel@tonic-gate }
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate static void
15097c478bd9Sstevel@tonic-gate datael_reset(scf_datael_t *dp)
15107c478bd9Sstevel@tonic-gate {
15117c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
15147c478bd9Sstevel@tonic-gate 	dp->rd_reset = 1;
15157c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate static void
15197c478bd9Sstevel@tonic-gate datael_finish_reset(const scf_datael_t *dp_arg)
15207c478bd9Sstevel@tonic-gate {
15217c478bd9Sstevel@tonic-gate 	scf_datael_t *dp = (scf_datael_t *)dp_arg;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	if (dp->rd_reset)
15247c478bd9Sstevel@tonic-gate 		datael_do_reset_locked(dp);
15257c478bd9Sstevel@tonic-gate }
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate /*
15287c478bd9Sstevel@tonic-gate  * Fails with _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response too
15297c478bd9Sstevel@tonic-gate  * big, bad entity id, request not applicable to entity, name too long for
15307c478bd9Sstevel@tonic-gate  * buffer), _NOT_SET, _DELETED, or _CONSTRAINT_VIOLATED (snaplevel is not of an
15317c478bd9Sstevel@tonic-gate  * instance).
15327c478bd9Sstevel@tonic-gate  */
15337c478bd9Sstevel@tonic-gate static ssize_t
15347c478bd9Sstevel@tonic-gate datael_get_name(const scf_datael_t *dp, char *buf, size_t size, uint32_t type)
15357c478bd9Sstevel@tonic-gate {
15367c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_name request;
15397c478bd9Sstevel@tonic-gate 	struct rep_protocol_name_response response;
15407c478bd9Sstevel@tonic-gate 	ssize_t r;
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
15437c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_NAME;
15447c478bd9Sstevel@tonic-gate 	request.rpr_entityid = dp->rd_entity;
15457c478bd9Sstevel@tonic-gate 	request.rpr_answertype = type;
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	datael_finish_reset(dp);
15487c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
15497c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
15507c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	if (r < 0)
15537c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
15567c478bd9Sstevel@tonic-gate 		assert(response.rpr_response != REP_PROTOCOL_FAIL_BAD_REQUEST);
15577c478bd9Sstevel@tonic-gate 		if (response.rpr_response == REP_PROTOCOL_FAIL_NOT_FOUND)
15587c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
15597c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
15607c478bd9Sstevel@tonic-gate 	}
15617c478bd9Sstevel@tonic-gate 	return (strlcpy(buf, response.rpr_name, size));
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate /*
15657c478bd9Sstevel@tonic-gate  * Fails with _HANDLE_MISMATCH, _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL
15667c478bd9Sstevel@tonic-gate  * (server response too big, bad element id), _EXISTS (elements have same id),
15677c478bd9Sstevel@tonic-gate  * _NOT_SET, _DELETED, _CONSTRAINT_VIOLATED, _NOT_FOUND (scope has no parent),
15687c478bd9Sstevel@tonic-gate  * or _SUCCESS.
15697c478bd9Sstevel@tonic-gate  */
15707c478bd9Sstevel@tonic-gate static int
15717c478bd9Sstevel@tonic-gate datael_get_parent(const scf_datael_t *dp, scf_datael_t *pp)
15727c478bd9Sstevel@tonic-gate {
15737c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_parent request;
15767c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	ssize_t r;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	if (h != pp->rd_handle)
15817c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
15847c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_GET_PARENT;
15857c478bd9Sstevel@tonic-gate 	request.rpr_entityid = dp->rd_entity;
15867c478bd9Sstevel@tonic-gate 	request.rpr_outid = pp->rd_entity;
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	datael_finish_reset(dp);
15897c478bd9Sstevel@tonic-gate 	datael_finish_reset(pp);
15907c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
15917c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
15927c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	if (r < 0)
15957c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
15987c478bd9Sstevel@tonic-gate 		if (response.rpr_response == REP_PROTOCOL_FAIL_TYPE_MISMATCH)
15997c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
16007c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
16017c478bd9Sstevel@tonic-gate 	}
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
16047c478bd9Sstevel@tonic-gate }
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate /*
16077c478bd9Sstevel@tonic-gate  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT (out does not have type type,
16087c478bd9Sstevel@tonic-gate  * name is invalid), _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response
16097c478bd9Sstevel@tonic-gate  * too big, bad id, iter already exists, element cannot have children of type,
16107c478bd9Sstevel@tonic-gate  * type is invalid, iter was reset, sequence was bad, iter walks values, iter
16117c478bd9Sstevel@tonic-gate  * does not walk type entities), _NOT_SET, _DELETED, _NO_RESOURCES,
161276cf44abSjeanm  * _BACKEND_ACCESS, _NOT_FOUND.
16137c478bd9Sstevel@tonic-gate  */
16147c478bd9Sstevel@tonic-gate static int
16157c478bd9Sstevel@tonic-gate datael_get_child_composed_locked(const scf_datael_t *dp, const char *name,
16167c478bd9Sstevel@tonic-gate     uint32_t type, scf_datael_t *out, scf_iter_t *iter)
16177c478bd9Sstevel@tonic-gate {
16187c478bd9Sstevel@tonic-gate 	struct rep_protocol_iter_start request;
16197c478bd9Sstevel@tonic-gate 	struct rep_protocol_iter_read read_request;
16207c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
16237c478bd9Sstevel@tonic-gate 	ssize_t r;
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	if (h != out->rd_handle)
16267c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	if (out->rd_type != type)
16297c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
16327c478bd9Sstevel@tonic-gate 	assert(iter != NULL);
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	scf_iter_reset_locked(iter);
16357c478bd9Sstevel@tonic-gate 	iter->iter_type = type;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ITER_START;
16387c478bd9Sstevel@tonic-gate 	request.rpr_iterid = iter->iter_id;
16397c478bd9Sstevel@tonic-gate 	request.rpr_entity = dp->rd_entity;
16407c478bd9Sstevel@tonic-gate 	request.rpr_itertype = type;
16417c478bd9Sstevel@tonic-gate 	request.rpr_flags = RP_ITER_START_EXACT | RP_ITER_START_COMPOSED;
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	if (name == NULL || strlcpy(request.rpr_pattern, name,
16447c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_pattern)) >= sizeof (request.rpr_pattern)) {
16457c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
16467c478bd9Sstevel@tonic-gate 	}
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	datael_finish_reset(dp);
16497c478bd9Sstevel@tonic-gate 	datael_finish_reset(out);
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	/*
16527c478bd9Sstevel@tonic-gate 	 * We hold the handle lock across both door calls, so that they
16537c478bd9Sstevel@tonic-gate 	 * appear atomic.
16547c478bd9Sstevel@tonic-gate 	 */
16557c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
16567c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	if (r < 0)
16597c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
16627c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	iter->iter_sequence++;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	read_request.rpr_request = REP_PROTOCOL_ITER_READ;
16677c478bd9Sstevel@tonic-gate 	read_request.rpr_iterid = iter->iter_id;
16687c478bd9Sstevel@tonic-gate 	read_request.rpr_sequence = iter->iter_sequence;
16697c478bd9Sstevel@tonic-gate 	read_request.rpr_entityid = out->rd_entity;
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &read_request, sizeof (read_request),
16727c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	scf_iter_reset_locked(iter);
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	if (r < 0)
16777c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	if (response.rpr_response == REP_PROTOCOL_DONE) {
16807c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_FOUND));
16817c478bd9Sstevel@tonic-gate 	}
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
16847c478bd9Sstevel@tonic-gate 		if (response.rpr_response == REP_PROTOCOL_FAIL_NOT_SET ||
16857c478bd9Sstevel@tonic-gate 		    response.rpr_response == REP_PROTOCOL_FAIL_BAD_REQUEST)
16867c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INTERNAL));
16877c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
16887c478bd9Sstevel@tonic-gate 	}
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	return (0);
16917c478bd9Sstevel@tonic-gate }
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate /*
16947c478bd9Sstevel@tonic-gate  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT (out does not have type type,
16957c478bd9Sstevel@tonic-gate  * name is invalid), _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response
16967c478bd9Sstevel@tonic-gate  * too big, bad id, element cannot have children of type, type is invalid),
16977c478bd9Sstevel@tonic-gate  * _NOT_SET, _DELETED, _NO_RESOURCES, _BACKEND_ACCESS.
16987c478bd9Sstevel@tonic-gate  */
16997c478bd9Sstevel@tonic-gate static int
17007c478bd9Sstevel@tonic-gate datael_get_child_locked(const scf_datael_t *dp, const char *name,
17017c478bd9Sstevel@tonic-gate     uint32_t type, scf_datael_t *out)
17027c478bd9Sstevel@tonic-gate {
17037c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_get_child request;
17047c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
17077c478bd9Sstevel@tonic-gate 	ssize_t r;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	if (h != out->rd_handle)
17107c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	if (out->rd_type != type)
17137c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_GET_CHILD;
17187c478bd9Sstevel@tonic-gate 	request.rpr_entityid = dp->rd_entity;
17197c478bd9Sstevel@tonic-gate 	request.rpr_childid = out->rd_entity;
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	if (name == NULL || strlcpy(request.rpr_name, name,
17227c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_name)) >= sizeof (request.rpr_name)) {
17237c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	datael_finish_reset(dp);
17277c478bd9Sstevel@tonic-gate 	datael_finish_reset(out);
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
17307c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 	if (r < 0)
17337c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
17367c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
17377c478bd9Sstevel@tonic-gate 	return (0);
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate 
174076cf44abSjeanm /*
174176cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT (out does not have type type,
174276cf44abSjeanm  * name is invalid), _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response
174376cf44abSjeanm  * too big, bad id, iter already exists, element cannot have children of type,
174476cf44abSjeanm  * type is invalid, iter was reset, sequence was bad, iter walks values, iter
174576cf44abSjeanm  * does not walk type entities), _NOT_SET, _DELETED, _NO_RESOURCES,
174676cf44abSjeanm  * _BACKEND_ACCESS, _NOT_FOUND.
174776cf44abSjeanm  */
17487c478bd9Sstevel@tonic-gate static int
17497c478bd9Sstevel@tonic-gate datael_get_child(const scf_datael_t *dp, const char *name, uint32_t type,
17507c478bd9Sstevel@tonic-gate     scf_datael_t *out, boolean_t composed)
17517c478bd9Sstevel@tonic-gate {
17527c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
17537c478bd9Sstevel@tonic-gate 	uint32_t held = 0;
17547c478bd9Sstevel@tonic-gate 	int ret;
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	scf_iter_t *iter = NULL;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	if (composed)
17597c478bd9Sstevel@tonic-gate 		iter = HANDLE_HOLD_ITER(h);
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	if (out == NULL) {
17627c478bd9Sstevel@tonic-gate 		switch (type) {
17637c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_ENTITY_SERVICE:
17647c478bd9Sstevel@tonic-gate 			out = &HANDLE_HOLD_SERVICE(h)->rd_d;
17657c478bd9Sstevel@tonic-gate 			held = RH_HOLD_SERVICE;
17667c478bd9Sstevel@tonic-gate 			break;
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_ENTITY_INSTANCE:
17697c478bd9Sstevel@tonic-gate 			out = &HANDLE_HOLD_INSTANCE(h)->rd_d;
17707c478bd9Sstevel@tonic-gate 			held = RH_HOLD_INSTANCE;
17717c478bd9Sstevel@tonic-gate 			break;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_ENTITY_SNAPSHOT:
17747c478bd9Sstevel@tonic-gate 			out = &HANDLE_HOLD_SNAPSHOT(h)->rd_d;
17757c478bd9Sstevel@tonic-gate 			held = RH_HOLD_SNAPSHOT;
17767c478bd9Sstevel@tonic-gate 			break;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_ENTITY_SNAPLEVEL:
17797c478bd9Sstevel@tonic-gate 			out = &HANDLE_HOLD_SNAPLVL(h)->rd_d;
17807c478bd9Sstevel@tonic-gate 			held = RH_HOLD_SNAPLVL;
17817c478bd9Sstevel@tonic-gate 			break;
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_ENTITY_PROPERTYGRP:
17847c478bd9Sstevel@tonic-gate 			out = &HANDLE_HOLD_PG(h)->rd_d;
17857c478bd9Sstevel@tonic-gate 			held = RH_HOLD_PG;
17867c478bd9Sstevel@tonic-gate 			break;
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_ENTITY_PROPERTY:
17897c478bd9Sstevel@tonic-gate 			out = &HANDLE_HOLD_PROPERTY(h)->rd_d;
17907c478bd9Sstevel@tonic-gate 			held = RH_HOLD_PROPERTY;
17917c478bd9Sstevel@tonic-gate 			break;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 		default:
17947c478bd9Sstevel@tonic-gate 			assert(0);
17957c478bd9Sstevel@tonic-gate 			abort();
17967c478bd9Sstevel@tonic-gate 		}
17977c478bd9Sstevel@tonic-gate 	}
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
18007c478bd9Sstevel@tonic-gate 	if (composed)
18017c478bd9Sstevel@tonic-gate 		ret = datael_get_child_composed_locked(dp, name, type, out,
18027c478bd9Sstevel@tonic-gate 		    iter);
18037c478bd9Sstevel@tonic-gate 	else
18047c478bd9Sstevel@tonic-gate 		ret = datael_get_child_locked(dp, name, type, out);
18057c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	if (composed)
18087c478bd9Sstevel@tonic-gate 		HANDLE_RELE_ITER(h);
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	if (held)
18117c478bd9Sstevel@tonic-gate 		handle_rele_subhandles(h, held);
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	return (ret);
18147c478bd9Sstevel@tonic-gate }
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate /*
18177c478bd9Sstevel@tonic-gate  * Fails with
18187c478bd9Sstevel@tonic-gate  *   _HANDLE_MISMATCH
18197c478bd9Sstevel@tonic-gate  *   _INVALID_ARGUMENT - name is too long
18207c478bd9Sstevel@tonic-gate  *			 invalid changeid
18217c478bd9Sstevel@tonic-gate  *			 name is invalid
18227c478bd9Sstevel@tonic-gate  *			 cannot create children for dp's type of node
18237c478bd9Sstevel@tonic-gate  *   _NOT_BOUND - handle is not bound
18247c478bd9Sstevel@tonic-gate  *   _CONNECTION_BROKEN - server is not reachable
18257c478bd9Sstevel@tonic-gate  *   _INTERNAL - server response too big
18267c478bd9Sstevel@tonic-gate  *		 dp or cp has unknown id
18277c478bd9Sstevel@tonic-gate  *		 type is _PROPERTYGRP
18287c478bd9Sstevel@tonic-gate  *		 type is invalid
18297c478bd9Sstevel@tonic-gate  *		 dp cannot have children of type type
18307c478bd9Sstevel@tonic-gate  *		 database is corrupt
18317c478bd9Sstevel@tonic-gate  *   _EXISTS - dp & cp have the same id
18327c478bd9Sstevel@tonic-gate  *   _EXISTS - child already exists
18337c478bd9Sstevel@tonic-gate  *   _DELETED - dp has been deleted
18347c478bd9Sstevel@tonic-gate  *   _NOT_SET - dp is reset
18357c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES
18367c478bd9Sstevel@tonic-gate  *   _PERMISSION_DENIED
18377c478bd9Sstevel@tonic-gate  *   _BACKEND_ACCESS
18387c478bd9Sstevel@tonic-gate  *   _BACKEND_READONLY
18397c478bd9Sstevel@tonic-gate  */
18407c478bd9Sstevel@tonic-gate static int
18417c478bd9Sstevel@tonic-gate datael_add_child(const scf_datael_t *dp, const char *name, uint32_t type,
18427c478bd9Sstevel@tonic-gate     scf_datael_t *cp)
18437c478bd9Sstevel@tonic-gate {
18447c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_create_child request;
18477c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
18487c478bd9Sstevel@tonic-gate 	ssize_t r;
18497c478bd9Sstevel@tonic-gate 	uint32_t held = 0;
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
18527c478bd9Sstevel@tonic-gate 		switch (type) {
18537c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_ENTITY_SCOPE:
18547c478bd9Sstevel@tonic-gate 			cp = &HANDLE_HOLD_SCOPE(h)->rd_d;
18557c478bd9Sstevel@tonic-gate 			held = RH_HOLD_SCOPE;
18567c478bd9Sstevel@tonic-gate 			break;
18577c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_ENTITY_SERVICE:
18587c478bd9Sstevel@tonic-gate 			cp = &HANDLE_HOLD_SERVICE(h)->rd_d;
18597c478bd9Sstevel@tonic-gate 			held = RH_HOLD_SERVICE;
18607c478bd9Sstevel@tonic-gate 			break;
18617c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_ENTITY_INSTANCE:
18627c478bd9Sstevel@tonic-gate 			cp = &HANDLE_HOLD_INSTANCE(h)->rd_d;
18637c478bd9Sstevel@tonic-gate 			held = RH_HOLD_INSTANCE;
18647c478bd9Sstevel@tonic-gate 			break;
18657c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_ENTITY_SNAPSHOT:
18667c478bd9Sstevel@tonic-gate 		default:
18677c478bd9Sstevel@tonic-gate 			assert(0);
18687c478bd9Sstevel@tonic-gate 			abort();
18697c478bd9Sstevel@tonic-gate 		}
18707c478bd9Sstevel@tonic-gate 		assert(h == cp->rd_handle);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	} else if (h != cp->rd_handle) {
18737c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	if (strlcpy(request.rpr_name, name, sizeof (request.rpr_name)) >=
18777c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_name)) {
18787c478bd9Sstevel@tonic-gate 		r = scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
18797c478bd9Sstevel@tonic-gate 		goto err;
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
18837c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_CREATE_CHILD;
18847c478bd9Sstevel@tonic-gate 	request.rpr_entityid = dp->rd_entity;
18857c478bd9Sstevel@tonic-gate 	request.rpr_childtype = type;
18867c478bd9Sstevel@tonic-gate 	request.rpr_childid = cp->rd_entity;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	datael_finish_reset(dp);
18898918dff3Sjwadams 	request.rpr_changeid = handle_next_changeid(h);
18907c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
18917c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
18927c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	if (held)
18957c478bd9Sstevel@tonic-gate 		handle_rele_subhandles(h, held);
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	if (r < 0)
18987c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
19017c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate err:
19067c478bd9Sstevel@tonic-gate 	if (held)
19077c478bd9Sstevel@tonic-gate 		handle_rele_subhandles(h, held);
19087c478bd9Sstevel@tonic-gate 	return (r);
19097c478bd9Sstevel@tonic-gate }
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate static int
19127c478bd9Sstevel@tonic-gate datael_add_pg(const scf_datael_t *dp, const char *name, const char *type,
19137c478bd9Sstevel@tonic-gate     uint32_t flags, scf_datael_t *cp)
19147c478bd9Sstevel@tonic-gate {
19157c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_create_pg request;
19187c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
19197c478bd9Sstevel@tonic-gate 	ssize_t r;
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	int holding_els = 0;
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	if (cp == NULL) {
19247c478bd9Sstevel@tonic-gate 		holding_els = 1;
19257c478bd9Sstevel@tonic-gate 		cp = &HANDLE_HOLD_PG(h)->rd_d;
19267c478bd9Sstevel@tonic-gate 		assert(h == cp->rd_handle);
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	} else if (h != cp->rd_handle) {
19297c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
19307c478bd9Sstevel@tonic-gate 	}
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_CREATE_PG;
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	if (name == NULL || strlcpy(request.rpr_name, name,
19357c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_name)) > sizeof (request.rpr_name)) {
19367c478bd9Sstevel@tonic-gate 		r = scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
19377c478bd9Sstevel@tonic-gate 		goto err;
19387c478bd9Sstevel@tonic-gate 	}
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	if (type == NULL || strlcpy(request.rpr_type, type,
19417c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_type)) > sizeof (request.rpr_type)) {
19427c478bd9Sstevel@tonic-gate 		r = scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
19437c478bd9Sstevel@tonic-gate 		goto err;
19447c478bd9Sstevel@tonic-gate 	}
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
19477c478bd9Sstevel@tonic-gate 	request.rpr_entityid = dp->rd_entity;
19487c478bd9Sstevel@tonic-gate 	request.rpr_childid = cp->rd_entity;
19497c478bd9Sstevel@tonic-gate 	request.rpr_flags = flags;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	datael_finish_reset(dp);
19527c478bd9Sstevel@tonic-gate 	datael_finish_reset(cp);
19538918dff3Sjwadams 	request.rpr_changeid = handle_next_changeid(h);
19547c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
19557c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
19567c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	if (holding_els)
19597c478bd9Sstevel@tonic-gate 		HANDLE_RELE_PG(h);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if (r < 0)
19627c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
19657c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate err:
19707c478bd9Sstevel@tonic-gate 	if (holding_els)
19717c478bd9Sstevel@tonic-gate 		HANDLE_RELE_PG(h);
19727c478bd9Sstevel@tonic-gate 	return (r);
19737c478bd9Sstevel@tonic-gate }
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate static int
19767c478bd9Sstevel@tonic-gate datael_delete(const scf_datael_t *dp)
19777c478bd9Sstevel@tonic-gate {
19787c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_delete request;
19817c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
19827c478bd9Sstevel@tonic-gate 	ssize_t r;
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
19857c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_DELETE;
19867c478bd9Sstevel@tonic-gate 	request.rpr_entityid = dp->rd_entity;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	datael_finish_reset(dp);
19898918dff3Sjwadams 	request.rpr_changeid = handle_next_changeid(h);
19907c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
19917c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
19927c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 	if (r < 0)
19957c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
19987c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
20017c478bd9Sstevel@tonic-gate }
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate /*
20047c478bd9Sstevel@tonic-gate  * Fails with
20057c478bd9Sstevel@tonic-gate  *   _INVALID_ARGUMENT - h is NULL
20067c478bd9Sstevel@tonic-gate  *   _NO_MEMORY
20077c478bd9Sstevel@tonic-gate  *   _HANDLE_DESTROYED - h has been destroyed
20087c478bd9Sstevel@tonic-gate  *   _INTERNAL - server response too big
20097c478bd9Sstevel@tonic-gate  *		 iter already exists
20107c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES
20117c478bd9Sstevel@tonic-gate  */
20127c478bd9Sstevel@tonic-gate scf_iter_t *
20137c478bd9Sstevel@tonic-gate scf_iter_create(scf_handle_t *h)
20147c478bd9Sstevel@tonic-gate {
20157c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	if (h == NULL) {
20187c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
20197c478bd9Sstevel@tonic-gate 		return (NULL);
20207c478bd9Sstevel@tonic-gate 	}
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	iter = uu_zalloc(sizeof (*iter));
20237c478bd9Sstevel@tonic-gate 	if (iter == NULL) {
20247c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
20257c478bd9Sstevel@tonic-gate 		return (NULL);
20267c478bd9Sstevel@tonic-gate 	}
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	uu_list_node_init(iter, &iter->iter_node, iter_pool);
20297c478bd9Sstevel@tonic-gate 	iter->iter_handle = h;
20307c478bd9Sstevel@tonic-gate 	iter->iter_sequence = 1;
20317c478bd9Sstevel@tonic-gate 	iter->iter_type = REP_PROTOCOL_ENTITY_NONE;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
20347c478bd9Sstevel@tonic-gate 	iter->iter_id = handle_alloc_iterid(h);
20358918dff3Sjwadams 	if (iter->iter_id == 0) {
20368918dff3Sjwadams 		(void) pthread_mutex_unlock(&h->rh_lock);
20378918dff3Sjwadams 		uu_list_node_fini(iter, &iter->iter_node, iter_pool);
20388918dff3Sjwadams 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
20398918dff3Sjwadams 		return (NULL);
20408918dff3Sjwadams 	}
20417c478bd9Sstevel@tonic-gate 	if (iter_attach(iter) == -1) {
20427c478bd9Sstevel@tonic-gate 		uu_list_node_fini(iter, &iter->iter_node, iter_pool);
20437c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
20447c478bd9Sstevel@tonic-gate 		uu_free(iter);
20457c478bd9Sstevel@tonic-gate 		return (NULL);
20467c478bd9Sstevel@tonic-gate 	}
20477c478bd9Sstevel@tonic-gate 	(void) uu_list_insert_before(h->rh_iters, NULL, iter);
20487c478bd9Sstevel@tonic-gate 	h->rh_extrefs++;
20497c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
20507c478bd9Sstevel@tonic-gate 	return (iter);
20517c478bd9Sstevel@tonic-gate }
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate scf_handle_t *
20547c478bd9Sstevel@tonic-gate scf_iter_handle(const scf_iter_t *iter)
20557c478bd9Sstevel@tonic-gate {
20567c478bd9Sstevel@tonic-gate 	return (handle_get(iter->iter_handle));
20577c478bd9Sstevel@tonic-gate }
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate static void
20607c478bd9Sstevel@tonic-gate scf_iter_reset_locked(scf_iter_t *iter)
20617c478bd9Sstevel@tonic-gate {
20627c478bd9Sstevel@tonic-gate 	struct rep_protocol_iter_request request;
20637c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ITER_RESET;
20667c478bd9Sstevel@tonic-gate 	request.rpr_iterid = iter->iter_id;
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&iter->iter_handle->rh_lock));
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	(void) make_door_call(iter->iter_handle,
20717c478bd9Sstevel@tonic-gate 	    &request, sizeof (request), &response, sizeof (response));
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 	iter->iter_type = REP_PROTOCOL_ENTITY_NONE;
20747c478bd9Sstevel@tonic-gate 	iter->iter_sequence = 1;
20757c478bd9Sstevel@tonic-gate }
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate void
20787c478bd9Sstevel@tonic-gate scf_iter_reset(scf_iter_t *iter)
20797c478bd9Sstevel@tonic-gate {
20807c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&iter->iter_handle->rh_lock);
20817c478bd9Sstevel@tonic-gate 	scf_iter_reset_locked(iter);
20827c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&iter->iter_handle->rh_lock);
20837c478bd9Sstevel@tonic-gate }
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate void
20867c478bd9Sstevel@tonic-gate scf_iter_destroy(scf_iter_t *iter)
20877c478bd9Sstevel@tonic-gate {
20887c478bd9Sstevel@tonic-gate 	scf_handle_t *handle;
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	struct rep_protocol_iter_request request;
20917c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if (iter == NULL)
20947c478bd9Sstevel@tonic-gate 		return;
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	handle = iter->iter_handle;
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&handle->rh_lock);
20997c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ITER_TEARDOWN;
21007c478bd9Sstevel@tonic-gate 	request.rpr_iterid = iter->iter_id;
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	(void) make_door_call(handle, &request, sizeof (request),
21037c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	uu_list_remove(handle->rh_iters, iter);
21067c478bd9Sstevel@tonic-gate 	--handle->rh_extrefs;
21077c478bd9Sstevel@tonic-gate 	handle_unrefed(handle);			/* drops h->rh_lock */
21087c478bd9Sstevel@tonic-gate 	iter->iter_handle = NULL;
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	uu_list_node_fini(iter, &iter->iter_node, iter_pool);
21117c478bd9Sstevel@tonic-gate 	uu_free(iter);
21127c478bd9Sstevel@tonic-gate }
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate static int
21157c478bd9Sstevel@tonic-gate handle_get_local_scope_locked(scf_handle_t *handle, scf_scope_t *out)
21167c478bd9Sstevel@tonic-gate {
21177c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_get request;
21187c478bd9Sstevel@tonic-gate 	struct rep_protocol_name_response response;
21197c478bd9Sstevel@tonic-gate 	ssize_t r;
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&handle->rh_lock));
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	if (handle != out->rd_d.rd_handle)
21247c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_GET;
21277c478bd9Sstevel@tonic-gate 	request.rpr_entityid = out->rd_d.rd_entity;
21287c478bd9Sstevel@tonic-gate 	request.rpr_object = RP_ENTITY_GET_MOST_LOCAL_SCOPE;
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	datael_finish_reset(&out->rd_d);
21317c478bd9Sstevel@tonic-gate 	r = make_door_call(handle, &request, sizeof (request),
21327c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	if (r < 0)
21357c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
21387c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
21417c478bd9Sstevel@tonic-gate }
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate int
21447c478bd9Sstevel@tonic-gate scf_iter_handle_scopes(scf_iter_t *iter, const scf_handle_t *handle)
21457c478bd9Sstevel@tonic-gate {
21467c478bd9Sstevel@tonic-gate 	scf_handle_t *h = iter->iter_handle;
21477c478bd9Sstevel@tonic-gate 	if (h != handle)
21487c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
21517c478bd9Sstevel@tonic-gate 	scf_iter_reset_locked(iter);
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	if (!handle_is_bound(h)) {
21547c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
21557c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_BOUND));
21567c478bd9Sstevel@tonic-gate 	}
21577c478bd9Sstevel@tonic-gate 
21588918dff3Sjwadams 	if (!handle_has_server_locked(h)) {
21597c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
21607c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN));
21617c478bd9Sstevel@tonic-gate 	}
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	iter->iter_type = REP_PROTOCOL_ENTITY_SCOPE;
21647c478bd9Sstevel@tonic-gate 	iter->iter_sequence = 1;
21657c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
21667c478bd9Sstevel@tonic-gate 	return (0);
21677c478bd9Sstevel@tonic-gate }
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate int
21707c478bd9Sstevel@tonic-gate scf_iter_next_scope(scf_iter_t *iter, scf_scope_t *out)
21717c478bd9Sstevel@tonic-gate {
21727c478bd9Sstevel@tonic-gate 	int ret;
21737c478bd9Sstevel@tonic-gate 	scf_handle_t *h = iter->iter_handle;
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	if (h != out->rd_d.rd_handle)
21767c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
21797c478bd9Sstevel@tonic-gate 	if (iter->iter_type == REP_PROTOCOL_ENTITY_NONE) {
21807c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
21817c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
21827c478bd9Sstevel@tonic-gate 	}
21837c478bd9Sstevel@tonic-gate 	if (iter->iter_type != REP_PROTOCOL_ENTITY_SCOPE) {
21847c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
21857c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 	if (iter->iter_sequence == 1) {
21887c478bd9Sstevel@tonic-gate 		if ((ret = handle_get_local_scope_locked(h, out)) ==
21897c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS) {
21907c478bd9Sstevel@tonic-gate 			iter->iter_sequence++;
21917c478bd9Sstevel@tonic-gate 			ret = 1;
21927c478bd9Sstevel@tonic-gate 		}
21937c478bd9Sstevel@tonic-gate 	} else {
21947c478bd9Sstevel@tonic-gate 		datael_reset_locked(&out->rd_d);
21957c478bd9Sstevel@tonic-gate 		ret = 0;
21967c478bd9Sstevel@tonic-gate 	}
21977c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
21987c478bd9Sstevel@tonic-gate 	return (ret);
21997c478bd9Sstevel@tonic-gate }
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate int
22027c478bd9Sstevel@tonic-gate scf_handle_get_scope(scf_handle_t *h, const char *name, scf_scope_t *out)
22037c478bd9Sstevel@tonic-gate {
22047c478bd9Sstevel@tonic-gate 	int ret;
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	if (h != out->rd_d.rd_handle)
22077c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
22107c478bd9Sstevel@tonic-gate 	if (strcmp(name, SCF_SCOPE_LOCAL) == 0) {
22117c478bd9Sstevel@tonic-gate 		ret = handle_get_local_scope_locked(h, out);
22127c478bd9Sstevel@tonic-gate 	} else {
22137c478bd9Sstevel@tonic-gate 		datael_reset_locked(&out->rd_d);
22147c478bd9Sstevel@tonic-gate 		if (uu_check_name(name, 0) == -1)
22157c478bd9Sstevel@tonic-gate 			ret = scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
22167c478bd9Sstevel@tonic-gate 		else
22177c478bd9Sstevel@tonic-gate 			ret = scf_set_error(SCF_ERROR_NOT_FOUND);
22187c478bd9Sstevel@tonic-gate 	}
22197c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
22207c478bd9Sstevel@tonic-gate 	return (ret);
22217c478bd9Sstevel@tonic-gate }
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate static int
22247c478bd9Sstevel@tonic-gate datael_setup_iter(scf_iter_t *iter, const scf_datael_t *dp, uint32_t res_type,
22257c478bd9Sstevel@tonic-gate     boolean_t composed)
22267c478bd9Sstevel@tonic-gate {
22277c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	struct rep_protocol_iter_start request;
22307c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	ssize_t r;
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 	if (h != iter->iter_handle)
22357c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
22387c478bd9Sstevel@tonic-gate 	scf_iter_reset_locked(iter);
22397c478bd9Sstevel@tonic-gate 	iter->iter_type = res_type;
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ITER_START;
22427c478bd9Sstevel@tonic-gate 	request.rpr_iterid = iter->iter_id;
22437c478bd9Sstevel@tonic-gate 	request.rpr_entity = dp->rd_entity;
22447c478bd9Sstevel@tonic-gate 	request.rpr_itertype = res_type;
22457c478bd9Sstevel@tonic-gate 	request.rpr_flags = RP_ITER_START_ALL |
22467c478bd9Sstevel@tonic-gate 	    (composed ? RP_ITER_START_COMPOSED : 0);
22477c478bd9Sstevel@tonic-gate 	request.rpr_pattern[0] = 0;
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 	datael_finish_reset(dp);
22507c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
22517c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	if (r < 0) {
22547c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
22557c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
22567c478bd9Sstevel@tonic-gate 	}
22577c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
22587c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
22597c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
22607c478bd9Sstevel@tonic-gate 	}
22617c478bd9Sstevel@tonic-gate 	iter->iter_sequence++;
22627c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
22637c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
22647c478bd9Sstevel@tonic-gate }
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate static int
22677c478bd9Sstevel@tonic-gate datael_setup_iter_pgtyped(scf_iter_t *iter, const scf_datael_t *dp,
22687c478bd9Sstevel@tonic-gate     const char *pgtype, boolean_t composed)
22697c478bd9Sstevel@tonic-gate {
22707c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	struct rep_protocol_iter_start request;
22737c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	ssize_t r;
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	if (h != iter->iter_handle)
22787c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	if (pgtype == NULL || strlcpy(request.rpr_pattern, pgtype,
22817c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_pattern)) >= sizeof (request.rpr_pattern)) {
22827c478bd9Sstevel@tonic-gate 		scf_iter_reset(iter);
22837c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
22847c478bd9Sstevel@tonic-gate 	}
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
22877c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ITER_START;
22887c478bd9Sstevel@tonic-gate 	request.rpr_iterid = iter->iter_id;
22897c478bd9Sstevel@tonic-gate 	request.rpr_entity = dp->rd_entity;
22907c478bd9Sstevel@tonic-gate 	request.rpr_itertype = REP_PROTOCOL_ENTITY_PROPERTYGRP;
22917c478bd9Sstevel@tonic-gate 	request.rpr_flags = RP_ITER_START_PGTYPE |
22927c478bd9Sstevel@tonic-gate 	    (composed ? RP_ITER_START_COMPOSED : 0);
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	datael_finish_reset(dp);
22957c478bd9Sstevel@tonic-gate 	scf_iter_reset_locked(iter);
22967c478bd9Sstevel@tonic-gate 	iter->iter_type = REP_PROTOCOL_ENTITY_PROPERTYGRP;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
22997c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	if (r < 0) {
23027c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
23057c478bd9Sstevel@tonic-gate 	}
23067c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
23077c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
23087c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
23097c478bd9Sstevel@tonic-gate 	}
23107c478bd9Sstevel@tonic-gate 	iter->iter_sequence++;
23117c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
23127c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
23137c478bd9Sstevel@tonic-gate }
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate static int
23167c478bd9Sstevel@tonic-gate datael_iter_next(scf_iter_t *iter, scf_datael_t *out)
23177c478bd9Sstevel@tonic-gate {
23187c478bd9Sstevel@tonic-gate 	scf_handle_t *h = iter->iter_handle;
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 	struct rep_protocol_iter_read request;
23217c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
23227c478bd9Sstevel@tonic-gate 	ssize_t r;
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	if (h != out->rd_handle)
23257c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
23287c478bd9Sstevel@tonic-gate 	if (iter->iter_type == REP_PROTOCOL_ENTITY_NONE ||
23297c478bd9Sstevel@tonic-gate 	    iter->iter_sequence == 1) {
23307c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
23317c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
23327c478bd9Sstevel@tonic-gate 	}
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	if (out->rd_type != iter->iter_type) {
23357c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
23367c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
23377c478bd9Sstevel@tonic-gate 	}
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ITER_READ;
23407c478bd9Sstevel@tonic-gate 	request.rpr_iterid = iter->iter_id;
23417c478bd9Sstevel@tonic-gate 	request.rpr_sequence = iter->iter_sequence;
23427c478bd9Sstevel@tonic-gate 	request.rpr_entityid = out->rd_entity;
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	datael_finish_reset(out);
23457c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
23467c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	if (r < 0) {
23497c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
23507c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
23517c478bd9Sstevel@tonic-gate 	}
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	if (response.rpr_response == REP_PROTOCOL_DONE) {
23547c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
23557c478bd9Sstevel@tonic-gate 		return (0);
23567c478bd9Sstevel@tonic-gate 	}
23577c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
23587c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
23597c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
23607c478bd9Sstevel@tonic-gate 	}
23617c478bd9Sstevel@tonic-gate 	iter->iter_sequence++;
23627c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	return (1);
23657c478bd9Sstevel@tonic-gate }
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate int
23687c478bd9Sstevel@tonic-gate scf_iter_scope_services(scf_iter_t *iter, const scf_scope_t *s)
23697c478bd9Sstevel@tonic-gate {
23707c478bd9Sstevel@tonic-gate 	return (datael_setup_iter(iter, &s->rd_d,
23717c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_SERVICE, 0));
23727c478bd9Sstevel@tonic-gate }
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate int
23757c478bd9Sstevel@tonic-gate scf_iter_next_service(scf_iter_t *iter, scf_service_t *out)
23767c478bd9Sstevel@tonic-gate {
23777c478bd9Sstevel@tonic-gate 	return (datael_iter_next(iter, &out->rd_d));
23787c478bd9Sstevel@tonic-gate }
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate int
23817c478bd9Sstevel@tonic-gate scf_iter_service_instances(scf_iter_t *iter, const scf_service_t *svc)
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate 	return (datael_setup_iter(iter, &svc->rd_d,
23847c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_INSTANCE, 0));
23857c478bd9Sstevel@tonic-gate }
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate int
23887c478bd9Sstevel@tonic-gate scf_iter_next_instance(scf_iter_t *iter, scf_instance_t *out)
23897c478bd9Sstevel@tonic-gate {
23907c478bd9Sstevel@tonic-gate 	return (datael_iter_next(iter, &out->rd_d));
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate int
23947c478bd9Sstevel@tonic-gate scf_iter_service_pgs(scf_iter_t *iter, const scf_service_t *svc)
23957c478bd9Sstevel@tonic-gate {
23967c478bd9Sstevel@tonic-gate 	return (datael_setup_iter(iter, &svc->rd_d,
23977c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, 0));
23987c478bd9Sstevel@tonic-gate }
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate int
24017c478bd9Sstevel@tonic-gate scf_iter_service_pgs_typed(scf_iter_t *iter, const scf_service_t *svc,
24027c478bd9Sstevel@tonic-gate     const char *type)
24037c478bd9Sstevel@tonic-gate {
24047c478bd9Sstevel@tonic-gate 	return (datael_setup_iter_pgtyped(iter, &svc->rd_d, type, 0));
24057c478bd9Sstevel@tonic-gate }
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate int
24087c478bd9Sstevel@tonic-gate scf_iter_instance_snapshots(scf_iter_t *iter, const scf_instance_t *inst)
24097c478bd9Sstevel@tonic-gate {
24107c478bd9Sstevel@tonic-gate 	return (datael_setup_iter(iter, &inst->rd_d,
24117c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_SNAPSHOT, 0));
24127c478bd9Sstevel@tonic-gate }
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate int
24157c478bd9Sstevel@tonic-gate scf_iter_next_snapshot(scf_iter_t *iter, scf_snapshot_t *out)
24167c478bd9Sstevel@tonic-gate {
24177c478bd9Sstevel@tonic-gate 	return (datael_iter_next(iter, &out->rd_d));
24187c478bd9Sstevel@tonic-gate }
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate int
24217c478bd9Sstevel@tonic-gate scf_iter_instance_pgs(scf_iter_t *iter, const scf_instance_t *inst)
24227c478bd9Sstevel@tonic-gate {
24237c478bd9Sstevel@tonic-gate 	return (datael_setup_iter(iter, &inst->rd_d,
24247c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, 0));
24257c478bd9Sstevel@tonic-gate }
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate int
24287c478bd9Sstevel@tonic-gate scf_iter_instance_pgs_typed(scf_iter_t *iter, const scf_instance_t *inst,
24297c478bd9Sstevel@tonic-gate     const char *type)
24307c478bd9Sstevel@tonic-gate {
24317c478bd9Sstevel@tonic-gate 	return (datael_setup_iter_pgtyped(iter, &inst->rd_d, type, 0));
24327c478bd9Sstevel@tonic-gate }
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate int
24357c478bd9Sstevel@tonic-gate scf_iter_instance_pgs_composed(scf_iter_t *iter, const scf_instance_t *inst,
24367c478bd9Sstevel@tonic-gate     const scf_snapshot_t *snap)
24377c478bd9Sstevel@tonic-gate {
24387c478bd9Sstevel@tonic-gate 	if (snap != NULL && inst->rd_d.rd_handle != snap->rd_d.rd_handle)
24397c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	return (datael_setup_iter(iter, snap ? &snap->rd_d : &inst->rd_d,
24427c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, 1));
24437c478bd9Sstevel@tonic-gate }
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate int
24467c478bd9Sstevel@tonic-gate scf_iter_instance_pgs_typed_composed(scf_iter_t *iter,
24477c478bd9Sstevel@tonic-gate     const scf_instance_t *inst, const scf_snapshot_t *snap, const char *type)
24487c478bd9Sstevel@tonic-gate {
24497c478bd9Sstevel@tonic-gate 	if (snap != NULL && inst->rd_d.rd_handle != snap->rd_d.rd_handle)
24507c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	return (datael_setup_iter_pgtyped(iter,
24537c478bd9Sstevel@tonic-gate 	    snap ? &snap->rd_d : &inst->rd_d, type, 1));
24547c478bd9Sstevel@tonic-gate }
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate int
24577c478bd9Sstevel@tonic-gate scf_iter_snaplevel_pgs(scf_iter_t *iter, const scf_snaplevel_t *inst)
24587c478bd9Sstevel@tonic-gate {
24597c478bd9Sstevel@tonic-gate 	return (datael_setup_iter(iter, &inst->rd_d,
24607c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, 0));
24617c478bd9Sstevel@tonic-gate }
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate int
24647c478bd9Sstevel@tonic-gate scf_iter_snaplevel_pgs_typed(scf_iter_t *iter, const scf_snaplevel_t *inst,
24657c478bd9Sstevel@tonic-gate     const char *type)
24667c478bd9Sstevel@tonic-gate {
24677c478bd9Sstevel@tonic-gate 	return (datael_setup_iter_pgtyped(iter, &inst->rd_d, type, 0));
24687c478bd9Sstevel@tonic-gate }
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate int
24717c478bd9Sstevel@tonic-gate scf_iter_next_pg(scf_iter_t *iter, scf_propertygroup_t *out)
24727c478bd9Sstevel@tonic-gate {
24737c478bd9Sstevel@tonic-gate 	return (datael_iter_next(iter, &out->rd_d));
24747c478bd9Sstevel@tonic-gate }
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate int
24777c478bd9Sstevel@tonic-gate scf_iter_pg_properties(scf_iter_t *iter, const scf_propertygroup_t *pg)
24787c478bd9Sstevel@tonic-gate {
24797c478bd9Sstevel@tonic-gate 	return (datael_setup_iter(iter, &pg->rd_d,
24807c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTY, 0));
24817c478bd9Sstevel@tonic-gate }
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate int
24847c478bd9Sstevel@tonic-gate scf_iter_next_property(scf_iter_t *iter, scf_property_t *out)
24857c478bd9Sstevel@tonic-gate {
24867c478bd9Sstevel@tonic-gate 	return (datael_iter_next(iter, &out->rd_d));
24877c478bd9Sstevel@tonic-gate }
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate /*
24907c478bd9Sstevel@tonic-gate  * Fails with
24917c478bd9Sstevel@tonic-gate  *   _INVALID_ARGUMENT - handle is NULL
24927c478bd9Sstevel@tonic-gate  *   _INTERNAL - server response too big
24937c478bd9Sstevel@tonic-gate  *		 entity already set up with different type
24947c478bd9Sstevel@tonic-gate  *   _NO_RESOURCES
24957c478bd9Sstevel@tonic-gate  *   _NO_MEMORY
24967c478bd9Sstevel@tonic-gate  */
24977c478bd9Sstevel@tonic-gate scf_scope_t *
24987c478bd9Sstevel@tonic-gate scf_scope_create(scf_handle_t *handle)
24997c478bd9Sstevel@tonic-gate {
25007c478bd9Sstevel@tonic-gate 	scf_scope_t *ret;
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
25037c478bd9Sstevel@tonic-gate 	if (ret != NULL) {
25047c478bd9Sstevel@tonic-gate 		if (datael_init(&ret->rd_d, handle,
25057c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_ENTITY_SCOPE) == -1) {
25067c478bd9Sstevel@tonic-gate 			uu_free(ret);
25077c478bd9Sstevel@tonic-gate 			return (NULL);
25087c478bd9Sstevel@tonic-gate 		}
25097c478bd9Sstevel@tonic-gate 	} else {
25107c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
25117c478bd9Sstevel@tonic-gate 	}
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate 	return (ret);
25147c478bd9Sstevel@tonic-gate }
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate scf_handle_t *
25177c478bd9Sstevel@tonic-gate scf_scope_handle(const scf_scope_t *val)
25187c478bd9Sstevel@tonic-gate {
25197c478bd9Sstevel@tonic-gate 	return (datael_handle(&val->rd_d));
25207c478bd9Sstevel@tonic-gate }
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate void
25237c478bd9Sstevel@tonic-gate scf_scope_destroy(scf_scope_t *val)
25247c478bd9Sstevel@tonic-gate {
25257c478bd9Sstevel@tonic-gate 	if (val == NULL)
25267c478bd9Sstevel@tonic-gate 		return;
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	datael_destroy(&val->rd_d);
25297c478bd9Sstevel@tonic-gate 	uu_free(val);
25307c478bd9Sstevel@tonic-gate }
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate ssize_t
25337c478bd9Sstevel@tonic-gate scf_scope_get_name(const scf_scope_t *rep, char *out, size_t len)
25347c478bd9Sstevel@tonic-gate {
25357c478bd9Sstevel@tonic-gate 	return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME));
25367c478bd9Sstevel@tonic-gate }
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
25397c478bd9Sstevel@tonic-gate int
25407c478bd9Sstevel@tonic-gate scf_scope_get_parent(const scf_scope_t *child, scf_scope_t *parent)
25417c478bd9Sstevel@tonic-gate {
25427c478bd9Sstevel@tonic-gate 	char name[1];
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	/* fake up the side-effects */
25457c478bd9Sstevel@tonic-gate 	datael_reset(&parent->rd_d);
25467c478bd9Sstevel@tonic-gate 	if (scf_scope_get_name(child, name, sizeof (name)) < 0)
25477c478bd9Sstevel@tonic-gate 		return (-1);
25487c478bd9Sstevel@tonic-gate 	return (scf_set_error(SCF_ERROR_NOT_FOUND));
25497c478bd9Sstevel@tonic-gate }
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate /*
25527c478bd9Sstevel@tonic-gate  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
25537c478bd9Sstevel@tonic-gate  * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY.
25547c478bd9Sstevel@tonic-gate  */
25557c478bd9Sstevel@tonic-gate scf_service_t *
25567c478bd9Sstevel@tonic-gate scf_service_create(scf_handle_t *handle)
25577c478bd9Sstevel@tonic-gate {
25587c478bd9Sstevel@tonic-gate 	scf_service_t *ret;
25597c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
25607c478bd9Sstevel@tonic-gate 	if (ret != NULL) {
25617c478bd9Sstevel@tonic-gate 		if (datael_init(&ret->rd_d, handle,
25627c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_ENTITY_SERVICE) == -1) {
25637c478bd9Sstevel@tonic-gate 			uu_free(ret);
25647c478bd9Sstevel@tonic-gate 			return (NULL);
25657c478bd9Sstevel@tonic-gate 		}
25667c478bd9Sstevel@tonic-gate 	} else {
25677c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
25687c478bd9Sstevel@tonic-gate 	}
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	return (ret);
25717c478bd9Sstevel@tonic-gate }
25727c478bd9Sstevel@tonic-gate 
257376cf44abSjeanm 
257476cf44abSjeanm /*
257576cf44abSjeanm  * Fails with
257676cf44abSjeanm  *   _HANDLE_MISMATCH
257776cf44abSjeanm  *   _INVALID_ARGUMENT
257876cf44abSjeanm  *   _NOT_BOUND
257976cf44abSjeanm  *   _CONNECTION_BROKEN
258076cf44abSjeanm  *   _INTERNAL
258176cf44abSjeanm  *   _EXISTS
258276cf44abSjeanm  *   _DELETED
258376cf44abSjeanm  *   _NOT_SET
258476cf44abSjeanm  *   _NO_RESOURCES
258576cf44abSjeanm  *   _PERMISSION_DENIED
258676cf44abSjeanm  *   _BACKEND_ACCESS
258776cf44abSjeanm  *   _BACKEND_READONLY
258876cf44abSjeanm  */
25897c478bd9Sstevel@tonic-gate int
25907c478bd9Sstevel@tonic-gate scf_scope_add_service(const scf_scope_t *scope, const char *name,
25917c478bd9Sstevel@tonic-gate     scf_service_t *svc)
25927c478bd9Sstevel@tonic-gate {
25937c478bd9Sstevel@tonic-gate 	return (datael_add_child(&scope->rd_d, name,
25947c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_SERVICE, (svc != NULL)? &svc->rd_d : NULL));
25957c478bd9Sstevel@tonic-gate }
25967c478bd9Sstevel@tonic-gate 
259776cf44abSjeanm /*
259876cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
259976cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
260076cf44abSjeanm  * _BACKEND_ACCESS, _NOT_FOUND.
260176cf44abSjeanm  */
26027c478bd9Sstevel@tonic-gate int
26037c478bd9Sstevel@tonic-gate scf_scope_get_service(const scf_scope_t *s, const char *name,
26047c478bd9Sstevel@tonic-gate     scf_service_t *svc)
26057c478bd9Sstevel@tonic-gate {
26067c478bd9Sstevel@tonic-gate 	return (datael_get_child(&s->rd_d, name, REP_PROTOCOL_ENTITY_SERVICE,
26077c478bd9Sstevel@tonic-gate 	    svc ? &svc->rd_d : NULL, 0));
26087c478bd9Sstevel@tonic-gate }
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate scf_handle_t *
26117c478bd9Sstevel@tonic-gate scf_service_handle(const scf_service_t *val)
26127c478bd9Sstevel@tonic-gate {
26137c478bd9Sstevel@tonic-gate 	return (datael_handle(&val->rd_d));
26147c478bd9Sstevel@tonic-gate }
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate int
26177c478bd9Sstevel@tonic-gate scf_service_delete(scf_service_t *svc)
26187c478bd9Sstevel@tonic-gate {
26197c478bd9Sstevel@tonic-gate 	return (datael_delete(&svc->rd_d));
26207c478bd9Sstevel@tonic-gate }
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate int
26237c478bd9Sstevel@tonic-gate scf_instance_delete(scf_instance_t *inst)
26247c478bd9Sstevel@tonic-gate {
26257c478bd9Sstevel@tonic-gate 	return (datael_delete(&inst->rd_d));
26267c478bd9Sstevel@tonic-gate }
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate int
26297c478bd9Sstevel@tonic-gate scf_pg_delete(scf_propertygroup_t *pg)
26307c478bd9Sstevel@tonic-gate {
26317c478bd9Sstevel@tonic-gate 	return (datael_delete(&pg->rd_d));
26327c478bd9Sstevel@tonic-gate }
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate int
26357c478bd9Sstevel@tonic-gate _scf_snapshot_delete(scf_snapshot_t *snap)
26367c478bd9Sstevel@tonic-gate {
26377c478bd9Sstevel@tonic-gate 	return (datael_delete(&snap->rd_d));
26387c478bd9Sstevel@tonic-gate }
26397c478bd9Sstevel@tonic-gate 
264076cf44abSjeanm /*
264176cf44abSjeanm  * Fails with
264276cf44abSjeanm  *   _HANDLE_MISMATCH
264376cf44abSjeanm  *   _INVALID_ARGUMENT
264476cf44abSjeanm  *   _NOT_BOUND
264576cf44abSjeanm  *   _CONNECTION_BROKEN
264676cf44abSjeanm  *   _INTERNAL
264776cf44abSjeanm  *   _EXISTS
264876cf44abSjeanm  *   _DELETED
264976cf44abSjeanm  *   _NOT_SET
265076cf44abSjeanm  *   _NO_RESOURCES
265176cf44abSjeanm  *   _PERMISSION_DENIED
265276cf44abSjeanm  *   _BACKEND_ACCESS
265376cf44abSjeanm  *   _BACKEND_READONLY
265476cf44abSjeanm  */
26557c478bd9Sstevel@tonic-gate int
26567c478bd9Sstevel@tonic-gate scf_service_add_instance(const scf_service_t *svc, const char *name,
26577c478bd9Sstevel@tonic-gate     scf_instance_t *instance)
26587c478bd9Sstevel@tonic-gate {
26597c478bd9Sstevel@tonic-gate 	return (datael_add_child(&svc->rd_d, name,
26607c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_INSTANCE,
26617c478bd9Sstevel@tonic-gate 	    (instance != NULL)? &instance->rd_d : NULL));
26627c478bd9Sstevel@tonic-gate }
26637c478bd9Sstevel@tonic-gate 
266476cf44abSjeanm 
266576cf44abSjeanm /*
266676cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
266776cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
266876cf44abSjeanm  * _BACKEND_ACCESS, _NOT_FOUND.
266976cf44abSjeanm  */
26707c478bd9Sstevel@tonic-gate int
26717c478bd9Sstevel@tonic-gate scf_service_get_instance(const scf_service_t *svc, const char *name,
26727c478bd9Sstevel@tonic-gate     scf_instance_t *inst)
26737c478bd9Sstevel@tonic-gate {
26747c478bd9Sstevel@tonic-gate 	return (datael_get_child(&svc->rd_d, name, REP_PROTOCOL_ENTITY_INSTANCE,
26757c478bd9Sstevel@tonic-gate 	    inst ? &inst->rd_d : NULL, 0));
26767c478bd9Sstevel@tonic-gate }
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate int
26797c478bd9Sstevel@tonic-gate scf_service_add_pg(const scf_service_t *svc, const char *name,
26807c478bd9Sstevel@tonic-gate     const char *type, uint32_t flags, scf_propertygroup_t *pg)
26817c478bd9Sstevel@tonic-gate {
26827c478bd9Sstevel@tonic-gate 	return (datael_add_pg(&svc->rd_d, name, type, flags,
26837c478bd9Sstevel@tonic-gate 	    (pg != NULL)?&pg->rd_d : NULL));
26847c478bd9Sstevel@tonic-gate }
26857c478bd9Sstevel@tonic-gate 
268676cf44abSjeanm /*
268776cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
268876cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
268976cf44abSjeanm  * _BACKEND_ACCESS, _NOT_FOUND.
269076cf44abSjeanm  */
26917c478bd9Sstevel@tonic-gate int
26927c478bd9Sstevel@tonic-gate scf_service_get_pg(const scf_service_t *svc, const char *name,
26937c478bd9Sstevel@tonic-gate     scf_propertygroup_t *pg)
26947c478bd9Sstevel@tonic-gate {
26957c478bd9Sstevel@tonic-gate 	return (datael_get_child(&svc->rd_d, name,
26967c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 0));
26977c478bd9Sstevel@tonic-gate }
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate int
27007c478bd9Sstevel@tonic-gate scf_instance_add_pg(const scf_instance_t *inst, const char *name,
27017c478bd9Sstevel@tonic-gate     const char *type, uint32_t flags, scf_propertygroup_t *pg)
27027c478bd9Sstevel@tonic-gate {
27037c478bd9Sstevel@tonic-gate 	return (datael_add_pg(&inst->rd_d, name, type, flags,
27047c478bd9Sstevel@tonic-gate 	    (pg != NULL)?&pg->rd_d : NULL));
27057c478bd9Sstevel@tonic-gate }
27067c478bd9Sstevel@tonic-gate 
270776cf44abSjeanm /*
270876cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
270976cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
271076cf44abSjeanm  * _BACKEND_ACCESS, _NOT_FOUND.
271176cf44abSjeanm  */
27127c478bd9Sstevel@tonic-gate int
27137c478bd9Sstevel@tonic-gate scf_instance_get_snapshot(const scf_instance_t *inst, const char *name,
27147c478bd9Sstevel@tonic-gate     scf_snapshot_t *pg)
27157c478bd9Sstevel@tonic-gate {
27167c478bd9Sstevel@tonic-gate 	return (datael_get_child(&inst->rd_d, name,
27177c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_SNAPSHOT, pg ? &pg->rd_d : NULL, 0));
27187c478bd9Sstevel@tonic-gate }
27197c478bd9Sstevel@tonic-gate 
272076cf44abSjeanm /*
272176cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
272276cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
272376cf44abSjeanm  * _BACKEND_ACCESS, _NOT_FOUND.
272476cf44abSjeanm  */
27257c478bd9Sstevel@tonic-gate int
27267c478bd9Sstevel@tonic-gate scf_instance_get_pg(const scf_instance_t *inst, const char *name,
27277c478bd9Sstevel@tonic-gate     scf_propertygroup_t *pg)
27287c478bd9Sstevel@tonic-gate {
27297c478bd9Sstevel@tonic-gate 	return (datael_get_child(&inst->rd_d, name,
27307c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 0));
27317c478bd9Sstevel@tonic-gate }
27327c478bd9Sstevel@tonic-gate 
273376cf44abSjeanm /*
273476cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
273576cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
273676cf44abSjeanm  * _BACKEND_ACCESS, _NOT_FOUND.
273776cf44abSjeanm  */
27387c478bd9Sstevel@tonic-gate int
27397c478bd9Sstevel@tonic-gate scf_instance_get_pg_composed(const scf_instance_t *inst,
27407c478bd9Sstevel@tonic-gate     const scf_snapshot_t *snap, const char *name, scf_propertygroup_t *pg)
27417c478bd9Sstevel@tonic-gate {
27427c478bd9Sstevel@tonic-gate 	if (snap != NULL && inst->rd_d.rd_handle != snap->rd_d.rd_handle)
27437c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 	return (datael_get_child(snap ? &snap->rd_d : &inst->rd_d, name,
27467c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 1));
27477c478bd9Sstevel@tonic-gate }
27487c478bd9Sstevel@tonic-gate 
274976cf44abSjeanm /*
275076cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
275176cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
275276cf44abSjeanm  * _BACKEND_ACCESS, _NOT_FOUND.
275376cf44abSjeanm  */
27547c478bd9Sstevel@tonic-gate int
27557c478bd9Sstevel@tonic-gate scf_pg_get_property(const scf_propertygroup_t *pg, const char *name,
27567c478bd9Sstevel@tonic-gate     scf_property_t *prop)
27577c478bd9Sstevel@tonic-gate {
27587c478bd9Sstevel@tonic-gate 	return (datael_get_child(&pg->rd_d, name, REP_PROTOCOL_ENTITY_PROPERTY,
27597c478bd9Sstevel@tonic-gate 	    prop ? &prop->rd_d : NULL, 0));
27607c478bd9Sstevel@tonic-gate }
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate void
27637c478bd9Sstevel@tonic-gate scf_service_destroy(scf_service_t *val)
27647c478bd9Sstevel@tonic-gate {
27657c478bd9Sstevel@tonic-gate 	if (val == NULL)
27667c478bd9Sstevel@tonic-gate 		return;
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 	datael_destroy(&val->rd_d);
27697c478bd9Sstevel@tonic-gate 	uu_free(val);
27707c478bd9Sstevel@tonic-gate }
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate ssize_t
27737c478bd9Sstevel@tonic-gate scf_service_get_name(const scf_service_t *rep, char *out, size_t len)
27747c478bd9Sstevel@tonic-gate {
27757c478bd9Sstevel@tonic-gate 	return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME));
27767c478bd9Sstevel@tonic-gate }
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate /*
27797c478bd9Sstevel@tonic-gate  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
27807c478bd9Sstevel@tonic-gate  * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY.
27817c478bd9Sstevel@tonic-gate  */
27827c478bd9Sstevel@tonic-gate scf_instance_t *
27837c478bd9Sstevel@tonic-gate scf_instance_create(scf_handle_t *handle)
27847c478bd9Sstevel@tonic-gate {
27857c478bd9Sstevel@tonic-gate 	scf_instance_t *ret;
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
27887c478bd9Sstevel@tonic-gate 	if (ret != NULL) {
27897c478bd9Sstevel@tonic-gate 		if (datael_init(&ret->rd_d, handle,
27907c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_ENTITY_INSTANCE) == -1) {
27917c478bd9Sstevel@tonic-gate 			uu_free(ret);
27927c478bd9Sstevel@tonic-gate 			return (NULL);
27937c478bd9Sstevel@tonic-gate 		}
27947c478bd9Sstevel@tonic-gate 	} else {
27957c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
27967c478bd9Sstevel@tonic-gate 	}
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	return (ret);
27997c478bd9Sstevel@tonic-gate }
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate scf_handle_t *
28027c478bd9Sstevel@tonic-gate scf_instance_handle(const scf_instance_t *val)
28037c478bd9Sstevel@tonic-gate {
28047c478bd9Sstevel@tonic-gate 	return (datael_handle(&val->rd_d));
28057c478bd9Sstevel@tonic-gate }
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate void
28087c478bd9Sstevel@tonic-gate scf_instance_destroy(scf_instance_t *val)
28097c478bd9Sstevel@tonic-gate {
28107c478bd9Sstevel@tonic-gate 	if (val == NULL)
28117c478bd9Sstevel@tonic-gate 		return;
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	datael_destroy(&val->rd_d);
28147c478bd9Sstevel@tonic-gate 	uu_free(val);
28157c478bd9Sstevel@tonic-gate }
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate ssize_t
28187c478bd9Sstevel@tonic-gate scf_instance_get_name(const scf_instance_t *rep, char *out, size_t len)
28197c478bd9Sstevel@tonic-gate {
28207c478bd9Sstevel@tonic-gate 	return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME));
28217c478bd9Sstevel@tonic-gate }
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate /*
28247c478bd9Sstevel@tonic-gate  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
28257c478bd9Sstevel@tonic-gate  * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY.
28267c478bd9Sstevel@tonic-gate  */
28277c478bd9Sstevel@tonic-gate scf_snapshot_t *
28287c478bd9Sstevel@tonic-gate scf_snapshot_create(scf_handle_t *handle)
28297c478bd9Sstevel@tonic-gate {
28307c478bd9Sstevel@tonic-gate 	scf_snapshot_t *ret;
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
28337c478bd9Sstevel@tonic-gate 	if (ret != NULL) {
28347c478bd9Sstevel@tonic-gate 		if (datael_init(&ret->rd_d, handle,
28357c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_ENTITY_SNAPSHOT) == -1) {
28367c478bd9Sstevel@tonic-gate 			uu_free(ret);
28377c478bd9Sstevel@tonic-gate 			return (NULL);
28387c478bd9Sstevel@tonic-gate 		}
28397c478bd9Sstevel@tonic-gate 	} else {
28407c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
28417c478bd9Sstevel@tonic-gate 	}
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	return (ret);
28447c478bd9Sstevel@tonic-gate }
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate scf_handle_t *
28477c478bd9Sstevel@tonic-gate scf_snapshot_handle(const scf_snapshot_t *val)
28487c478bd9Sstevel@tonic-gate {
28497c478bd9Sstevel@tonic-gate 	return (datael_handle(&val->rd_d));
28507c478bd9Sstevel@tonic-gate }
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate void
28537c478bd9Sstevel@tonic-gate scf_snapshot_destroy(scf_snapshot_t *val)
28547c478bd9Sstevel@tonic-gate {
28557c478bd9Sstevel@tonic-gate 	if (val == NULL)
28567c478bd9Sstevel@tonic-gate 		return;
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 	datael_destroy(&val->rd_d);
28597c478bd9Sstevel@tonic-gate 	uu_free(val);
28607c478bd9Sstevel@tonic-gate }
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate ssize_t
28637c478bd9Sstevel@tonic-gate scf_snapshot_get_name(const scf_snapshot_t *rep, char *out, size_t len)
28647c478bd9Sstevel@tonic-gate {
28657c478bd9Sstevel@tonic-gate 	return (datael_get_name(&rep->rd_d, out, len, RP_ENTITY_NAME_NAME));
28667c478bd9Sstevel@tonic-gate }
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate /*
28697c478bd9Sstevel@tonic-gate  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
28707c478bd9Sstevel@tonic-gate  * (bad server response or id in use), _NO_RESOURCES, _NO_MEMORY.
28717c478bd9Sstevel@tonic-gate  */
28727c478bd9Sstevel@tonic-gate scf_snaplevel_t *
28737c478bd9Sstevel@tonic-gate scf_snaplevel_create(scf_handle_t *handle)
28747c478bd9Sstevel@tonic-gate {
28757c478bd9Sstevel@tonic-gate 	scf_snaplevel_t *ret;
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
28787c478bd9Sstevel@tonic-gate 	if (ret != NULL) {
28797c478bd9Sstevel@tonic-gate 		if (datael_init(&ret->rd_d, handle,
28807c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_ENTITY_SNAPLEVEL) == -1) {
28817c478bd9Sstevel@tonic-gate 			uu_free(ret);
28827c478bd9Sstevel@tonic-gate 			return (NULL);
28837c478bd9Sstevel@tonic-gate 		}
28847c478bd9Sstevel@tonic-gate 	} else {
28857c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
28867c478bd9Sstevel@tonic-gate 	}
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 	return (ret);
28897c478bd9Sstevel@tonic-gate }
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate scf_handle_t *
28927c478bd9Sstevel@tonic-gate scf_snaplevel_handle(const scf_snaplevel_t *val)
28937c478bd9Sstevel@tonic-gate {
28947c478bd9Sstevel@tonic-gate 	return (datael_handle(&val->rd_d));
28957c478bd9Sstevel@tonic-gate }
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate void
28987c478bd9Sstevel@tonic-gate scf_snaplevel_destroy(scf_snaplevel_t *val)
28997c478bd9Sstevel@tonic-gate {
29007c478bd9Sstevel@tonic-gate 	if (val == NULL)
29017c478bd9Sstevel@tonic-gate 		return;
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 	datael_destroy(&val->rd_d);
29047c478bd9Sstevel@tonic-gate 	uu_free(val);
29057c478bd9Sstevel@tonic-gate }
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate ssize_t
29087c478bd9Sstevel@tonic-gate scf_snaplevel_get_scope_name(const scf_snaplevel_t *rep, char *out, size_t len)
29097c478bd9Sstevel@tonic-gate {
29107c478bd9Sstevel@tonic-gate 	return (datael_get_name(&rep->rd_d, out, len,
29117c478bd9Sstevel@tonic-gate 	    RP_ENTITY_NAME_SNAPLEVEL_SCOPE));
29127c478bd9Sstevel@tonic-gate }
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate ssize_t
29157c478bd9Sstevel@tonic-gate scf_snaplevel_get_service_name(const scf_snaplevel_t *rep, char *out,
29167c478bd9Sstevel@tonic-gate     size_t len)
29177c478bd9Sstevel@tonic-gate {
29187c478bd9Sstevel@tonic-gate 	return (datael_get_name(&rep->rd_d, out, len,
29197c478bd9Sstevel@tonic-gate 	    RP_ENTITY_NAME_SNAPLEVEL_SERVICE));
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate ssize_t
29237c478bd9Sstevel@tonic-gate scf_snaplevel_get_instance_name(const scf_snaplevel_t *rep, char *out,
29247c478bd9Sstevel@tonic-gate     size_t len)
29257c478bd9Sstevel@tonic-gate {
29267c478bd9Sstevel@tonic-gate 	return (datael_get_name(&rep->rd_d, out, len,
29277c478bd9Sstevel@tonic-gate 	    RP_ENTITY_NAME_SNAPLEVEL_INSTANCE));
29287c478bd9Sstevel@tonic-gate }
29297c478bd9Sstevel@tonic-gate 
293076cf44abSjeanm /*
293176cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
293276cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
293376cf44abSjeanm  * _BACKEND_ACCESS, _NOT_FOUND.
293476cf44abSjeanm  */
29357c478bd9Sstevel@tonic-gate int
29367c478bd9Sstevel@tonic-gate scf_snaplevel_get_pg(const scf_snaplevel_t *snap, const char *name,
29377c478bd9Sstevel@tonic-gate     scf_propertygroup_t *pg)
29387c478bd9Sstevel@tonic-gate {
29397c478bd9Sstevel@tonic-gate 	return (datael_get_child(&snap->rd_d, name,
29407c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTYGRP, pg ? &pg->rd_d : NULL, 0));
29417c478bd9Sstevel@tonic-gate }
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate static int
29447c478bd9Sstevel@tonic-gate snaplevel_next(const scf_datael_t *src, scf_snaplevel_t *dst_arg)
29457c478bd9Sstevel@tonic-gate {
29467c478bd9Sstevel@tonic-gate 	scf_handle_t *h = src->rd_handle;
29477c478bd9Sstevel@tonic-gate 	scf_snaplevel_t *dst = dst_arg;
29487c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_pair request;
29497c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
29507c478bd9Sstevel@tonic-gate 	int r;
29517c478bd9Sstevel@tonic-gate 	int dups = 0;
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	if (h != dst->rd_d.rd_handle)
29547c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
29557c478bd9Sstevel@tonic-gate 
29567c478bd9Sstevel@tonic-gate 	if (src == &dst->rd_d) {
29577c478bd9Sstevel@tonic-gate 		dups = 1;
29587c478bd9Sstevel@tonic-gate 		dst = HANDLE_HOLD_SNAPLVL(h);
29597c478bd9Sstevel@tonic-gate 	}
29607c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
29617c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_NEXT_SNAPLEVEL;
29627c478bd9Sstevel@tonic-gate 	request.rpr_entity_src = src->rd_entity;
29637c478bd9Sstevel@tonic-gate 	request.rpr_entity_dst = dst->rd_d.rd_entity;
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 	datael_finish_reset(src);
29667c478bd9Sstevel@tonic-gate 	datael_finish_reset(&dst->rd_d);
29677c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
29687c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
29697c478bd9Sstevel@tonic-gate 	/*
29707c478bd9Sstevel@tonic-gate 	 * if we succeeded, we need to swap dst and dst_arg's identity.  We
29717c478bd9Sstevel@tonic-gate 	 * take advantage of the fact that the only in-library knowledge is
29727c478bd9Sstevel@tonic-gate 	 * their entity ids.
29737c478bd9Sstevel@tonic-gate 	 */
29747c478bd9Sstevel@tonic-gate 	if (dups && r >= 0 &&
29757c478bd9Sstevel@tonic-gate 	    (response.rpr_response == REP_PROTOCOL_SUCCESS ||
29767c478bd9Sstevel@tonic-gate 	    response.rpr_response == REP_PROTOCOL_DONE)) {
29777c478bd9Sstevel@tonic-gate 		int entity = dst->rd_d.rd_entity;
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 		dst->rd_d.rd_entity = dst_arg->rd_d.rd_entity;
29807c478bd9Sstevel@tonic-gate 		dst_arg->rd_d.rd_entity = entity;
29817c478bd9Sstevel@tonic-gate 	}
29827c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 	if (dups)
29857c478bd9Sstevel@tonic-gate 		HANDLE_RELE_SNAPLVL(h);
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 	if (r < 0)
29887c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS &&
29917c478bd9Sstevel@tonic-gate 	    response.rpr_response != REP_PROTOCOL_DONE) {
29927c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
29937c478bd9Sstevel@tonic-gate 	}
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate 	return (response.rpr_response == REP_PROTOCOL_SUCCESS) ?
29967c478bd9Sstevel@tonic-gate 	    SCF_SUCCESS : SCF_COMPLETE;
29977c478bd9Sstevel@tonic-gate }
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate int scf_snapshot_get_base_snaplevel(const scf_snapshot_t *base,
30007c478bd9Sstevel@tonic-gate     scf_snaplevel_t *out)
30017c478bd9Sstevel@tonic-gate {
30027c478bd9Sstevel@tonic-gate 	return (snaplevel_next(&base->rd_d, out));
30037c478bd9Sstevel@tonic-gate }
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate int scf_snaplevel_get_next_snaplevel(const scf_snaplevel_t *base,
30067c478bd9Sstevel@tonic-gate     scf_snaplevel_t *out)
30077c478bd9Sstevel@tonic-gate {
30087c478bd9Sstevel@tonic-gate 	return (snaplevel_next(&base->rd_d, out));
30097c478bd9Sstevel@tonic-gate }
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate /*
30127c478bd9Sstevel@tonic-gate  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
30137c478bd9Sstevel@tonic-gate  * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY.
30147c478bd9Sstevel@tonic-gate  */
30157c478bd9Sstevel@tonic-gate scf_propertygroup_t *
30167c478bd9Sstevel@tonic-gate scf_pg_create(scf_handle_t *handle)
30177c478bd9Sstevel@tonic-gate {
30187c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *ret;
30197c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
30207c478bd9Sstevel@tonic-gate 	if (ret != NULL) {
30217c478bd9Sstevel@tonic-gate 		if (datael_init(&ret->rd_d, handle,
30227c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_ENTITY_PROPERTYGRP) == -1) {
30237c478bd9Sstevel@tonic-gate 			uu_free(ret);
30247c478bd9Sstevel@tonic-gate 			return (NULL);
30257c478bd9Sstevel@tonic-gate 		}
30267c478bd9Sstevel@tonic-gate 	} else {
30277c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
30287c478bd9Sstevel@tonic-gate 	}
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	return (ret);
30317c478bd9Sstevel@tonic-gate }
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate scf_handle_t *
30347c478bd9Sstevel@tonic-gate scf_pg_handle(const scf_propertygroup_t *val)
30357c478bd9Sstevel@tonic-gate {
30367c478bd9Sstevel@tonic-gate 	return (datael_handle(&val->rd_d));
30377c478bd9Sstevel@tonic-gate }
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate void
30407c478bd9Sstevel@tonic-gate scf_pg_destroy(scf_propertygroup_t *val)
30417c478bd9Sstevel@tonic-gate {
30427c478bd9Sstevel@tonic-gate 	if (val == NULL)
30437c478bd9Sstevel@tonic-gate 		return;
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate 	datael_destroy(&val->rd_d);
30467c478bd9Sstevel@tonic-gate 	uu_free(val);
30477c478bd9Sstevel@tonic-gate }
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate ssize_t
30507c478bd9Sstevel@tonic-gate scf_pg_get_name(const scf_propertygroup_t *pg,  char *out, size_t len)
30517c478bd9Sstevel@tonic-gate {
30527c478bd9Sstevel@tonic-gate 	return (datael_get_name(&pg->rd_d, out, len, RP_ENTITY_NAME_NAME));
30537c478bd9Sstevel@tonic-gate }
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate ssize_t
30567c478bd9Sstevel@tonic-gate scf_pg_get_type(const scf_propertygroup_t *pg,  char *out, size_t len)
30577c478bd9Sstevel@tonic-gate {
30587c478bd9Sstevel@tonic-gate 	return (datael_get_name(&pg->rd_d, out, len, RP_ENTITY_NAME_PGTYPE));
30597c478bd9Sstevel@tonic-gate }
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate int
30627c478bd9Sstevel@tonic-gate scf_pg_get_flags(const scf_propertygroup_t *pg, uint32_t *out)
30637c478bd9Sstevel@tonic-gate {
30647c478bd9Sstevel@tonic-gate 	char buf[REP_PROTOCOL_NAME_LEN];
30657c478bd9Sstevel@tonic-gate 	ssize_t res;
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate 	res = datael_get_name(&pg->rd_d, buf, sizeof (buf),
30687c478bd9Sstevel@tonic-gate 	    RP_ENTITY_NAME_PGFLAGS);
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 	if (res == -1)
30717c478bd9Sstevel@tonic-gate 		return (-1);
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	if (uu_strtouint(buf, out, sizeof (*out), 0, 0, UINT32_MAX) == -1)
30747c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INTERNAL));
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	return (0);
30777c478bd9Sstevel@tonic-gate }
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate static int
30807c478bd9Sstevel@tonic-gate datael_update(scf_datael_t *dp)
30817c478bd9Sstevel@tonic-gate {
30827c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dp->rd_handle;
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_update request;
30857c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	int r;
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
30907c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_UPDATE;
30917c478bd9Sstevel@tonic-gate 	request.rpr_entityid = dp->rd_entity;
30927c478bd9Sstevel@tonic-gate 
30937c478bd9Sstevel@tonic-gate 	datael_finish_reset(dp);
30948918dff3Sjwadams 	request.rpr_changeid = handle_next_changeid(h);
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
30977c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
30987c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	if (r < 0)
31017c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
31027c478bd9Sstevel@tonic-gate 
310376cf44abSjeanm 	/*
310476cf44abSjeanm 	 * This should never happen but if it does something has
310576cf44abSjeanm 	 * gone terribly wrong and we should abort.
310676cf44abSjeanm 	 */
310776cf44abSjeanm 	if (response.rpr_response == REP_PROTOCOL_FAIL_BAD_REQUEST)
310876cf44abSjeanm 		abort();
310976cf44abSjeanm 
31107c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS &&
31117c478bd9Sstevel@tonic-gate 	    response.rpr_response != REP_PROTOCOL_DONE) {
31127c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
31137c478bd9Sstevel@tonic-gate 	}
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate 	return (response.rpr_response == REP_PROTOCOL_SUCCESS) ?
31167c478bd9Sstevel@tonic-gate 	    SCF_SUCCESS : SCF_COMPLETE;
31177c478bd9Sstevel@tonic-gate }
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate int
31207c478bd9Sstevel@tonic-gate scf_pg_update(scf_propertygroup_t *pg)
31217c478bd9Sstevel@tonic-gate {
31227c478bd9Sstevel@tonic-gate 	return (datael_update(&pg->rd_d));
31237c478bd9Sstevel@tonic-gate }
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate int
31267c478bd9Sstevel@tonic-gate scf_snapshot_update(scf_snapshot_t *snap)
31277c478bd9Sstevel@tonic-gate {
31287c478bd9Sstevel@tonic-gate 	return (datael_update(&snap->rd_d));
31297c478bd9Sstevel@tonic-gate }
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate int
31327c478bd9Sstevel@tonic-gate _scf_pg_wait(scf_propertygroup_t *pg, int timeout)
31337c478bd9Sstevel@tonic-gate {
31347c478bd9Sstevel@tonic-gate 	scf_handle_t *h = pg->rd_d.rd_handle;
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 	struct rep_protocol_propertygrp_request request;
31377c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	struct pollfd pollfd;
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate 	int r;
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
31447c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_PROPERTYGRP_SETUP_WAIT;
31457c478bd9Sstevel@tonic-gate 	request.rpr_entityid = pg->rd_d.rd_entity;
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 	datael_finish_reset(&pg->rd_d);
31487c478bd9Sstevel@tonic-gate 	if (!handle_is_bound(h)) {
31497c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
31507c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN));
31517c478bd9Sstevel@tonic-gate 	}
31527c478bd9Sstevel@tonic-gate 	r = make_door_call_retfd(h->rh_doorfd, &request, sizeof (request),
31537c478bd9Sstevel@tonic-gate 	    &response, sizeof (response), &pollfd.fd);
31547c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	if (r < 0)
31577c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 	assert((response.rpr_response == REP_PROTOCOL_SUCCESS) ==
31607c478bd9Sstevel@tonic-gate 	    (pollfd.fd != -1));
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	if (response.rpr_response == REP_PROTOCOL_FAIL_NOT_LATEST)
31637c478bd9Sstevel@tonic-gate 		return (SCF_SUCCESS);
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
31667c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	pollfd.events = 0;
31697c478bd9Sstevel@tonic-gate 	pollfd.revents = 0;
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate 	r = poll(&pollfd, 1, timeout * MILLISEC);
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 	(void) close(pollfd.fd);
31747c478bd9Sstevel@tonic-gate 	return (pollfd.revents ? SCF_SUCCESS : SCF_COMPLETE);
31757c478bd9Sstevel@tonic-gate }
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate static int
31787c478bd9Sstevel@tonic-gate scf_notify_add_pattern(scf_handle_t *h, int type, const char *name)
31797c478bd9Sstevel@tonic-gate {
31807c478bd9Sstevel@tonic-gate 	struct rep_protocol_notify_request request;
31817c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
31827c478bd9Sstevel@tonic-gate 	int r;
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
31857c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_CLIENT_ADD_NOTIFY;
31867c478bd9Sstevel@tonic-gate 	request.rpr_type = type;
31877c478bd9Sstevel@tonic-gate 	(void) strlcpy(request.rpr_pattern, name, sizeof (request.rpr_pattern));
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
31907c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
31917c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 	if (r < 0)
31947c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
31977c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
32007c478bd9Sstevel@tonic-gate }
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate int
32037c478bd9Sstevel@tonic-gate _scf_notify_add_pgname(scf_handle_t *h, const char *name)
32047c478bd9Sstevel@tonic-gate {
32057c478bd9Sstevel@tonic-gate 	return (scf_notify_add_pattern(h, REP_PROTOCOL_NOTIFY_PGNAME, name));
32067c478bd9Sstevel@tonic-gate }
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate int
32097c478bd9Sstevel@tonic-gate _scf_notify_add_pgtype(scf_handle_t *h, const char *type)
32107c478bd9Sstevel@tonic-gate {
32117c478bd9Sstevel@tonic-gate 	return (scf_notify_add_pattern(h, REP_PROTOCOL_NOTIFY_PGTYPE, type));
32127c478bd9Sstevel@tonic-gate }
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate int
32157c478bd9Sstevel@tonic-gate _scf_notify_wait(scf_propertygroup_t *pg, char *out, size_t sz)
32167c478bd9Sstevel@tonic-gate {
32177c478bd9Sstevel@tonic-gate 	struct rep_protocol_wait_request request;
32187c478bd9Sstevel@tonic-gate 	struct rep_protocol_fmri_response response;
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 	scf_handle_t *h = pg->rd_d.rd_handle;
32217c478bd9Sstevel@tonic-gate 	int dummy;
32227c478bd9Sstevel@tonic-gate 	int fd;
32237c478bd9Sstevel@tonic-gate 	int r;
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
32267c478bd9Sstevel@tonic-gate 	datael_finish_reset(&pg->rd_d);
32277c478bd9Sstevel@tonic-gate 	if (!handle_is_bound(h)) {
32287c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
32297c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_CONNECTION_BROKEN));
32307c478bd9Sstevel@tonic-gate 	}
32317c478bd9Sstevel@tonic-gate 	fd = h->rh_doorfd;
32327c478bd9Sstevel@tonic-gate 	++h->rh_fd_users;
32337c478bd9Sstevel@tonic-gate 	assert(h->rh_fd_users > 0);
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_CLIENT_WAIT;
32367c478bd9Sstevel@tonic-gate 	request.rpr_entityid = pg->rd_d.rd_entity;
32377c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 	r = make_door_call_retfd(fd, &request, sizeof (request),
32407c478bd9Sstevel@tonic-gate 	    &response, sizeof (response), &dummy);
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
32437c478bd9Sstevel@tonic-gate 	assert(h->rh_fd_users > 0);
32447c478bd9Sstevel@tonic-gate 	if (--h->rh_fd_users == 0) {
32457c478bd9Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&h->rh_cv);
32467c478bd9Sstevel@tonic-gate 		/*
32477c478bd9Sstevel@tonic-gate 		 * check for a delayed close, now that there are no other
32487c478bd9Sstevel@tonic-gate 		 * users.
32497c478bd9Sstevel@tonic-gate 		 */
32507c478bd9Sstevel@tonic-gate 		if (h->rh_doorfd_old != -1) {
32517c478bd9Sstevel@tonic-gate 			assert(h->rh_doorfd == -1);
32527c478bd9Sstevel@tonic-gate 			assert(fd == h->rh_doorfd_old);
32537c478bd9Sstevel@tonic-gate 			(void) close(h->rh_doorfd_old);
32547c478bd9Sstevel@tonic-gate 			h->rh_doorfd_old = -1;
32557c478bd9Sstevel@tonic-gate 		}
32567c478bd9Sstevel@tonic-gate 	}
32577c478bd9Sstevel@tonic-gate 	handle_unrefed(h);			/* drops h->rh_lock */
32587c478bd9Sstevel@tonic-gate 
32597c478bd9Sstevel@tonic-gate 	if (r < 0)
32607c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	if (response.rpr_response == REP_PROTOCOL_DONE)
32637c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
32667c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
32677c478bd9Sstevel@tonic-gate 
32687c478bd9Sstevel@tonic-gate 	/* the following will be non-zero for delete notifications */
32697c478bd9Sstevel@tonic-gate 	return (strlcpy(out, response.rpr_fmri, sz));
32707c478bd9Sstevel@tonic-gate }
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate static int
32737c478bd9Sstevel@tonic-gate _scf_snapshot_take(scf_instance_t *inst, const char *name,
32747c478bd9Sstevel@tonic-gate     scf_snapshot_t *snap, int flags)
32757c478bd9Sstevel@tonic-gate {
32767c478bd9Sstevel@tonic-gate 	scf_handle_t *h = inst->rd_d.rd_handle;
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate 	struct rep_protocol_snapshot_take request;
32797c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 	int r;
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 	if (h != snap->rd_d.rd_handle)
32847c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 	if (strlcpy(request.rpr_name, (name != NULL)? name : "",
32877c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_name)) >= sizeof (request.rpr_name))
32887c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
32917c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_SNAPSHOT_TAKE;
32927c478bd9Sstevel@tonic-gate 	request.rpr_entityid_src = inst->rd_d.rd_entity;
32937c478bd9Sstevel@tonic-gate 	request.rpr_entityid_dest = snap->rd_d.rd_entity;
32947c478bd9Sstevel@tonic-gate 	request.rpr_flags = flags;
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	datael_finish_reset(&inst->rd_d);
32977c478bd9Sstevel@tonic-gate 	datael_finish_reset(&snap->rd_d);
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
33007c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
33017c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 	if (r < 0)
33047c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
33077c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
33087c478bd9Sstevel@tonic-gate 
33097c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
33107c478bd9Sstevel@tonic-gate }
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate int
33137c478bd9Sstevel@tonic-gate _scf_snapshot_take_new_named(scf_instance_t *inst,
33147c478bd9Sstevel@tonic-gate     const char *svcname, const char *instname, const char *snapname,
33157c478bd9Sstevel@tonic-gate     scf_snapshot_t *snap)
33167c478bd9Sstevel@tonic-gate {
33177c478bd9Sstevel@tonic-gate 	scf_handle_t *h = inst->rd_d.rd_handle;
33187c478bd9Sstevel@tonic-gate 
33197c478bd9Sstevel@tonic-gate 	struct rep_protocol_snapshot_take_named request;
33207c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate 	int r;
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate 	if (h != snap->rd_d.rd_handle)
33257c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate 	if (strlcpy(request.rpr_svcname, svcname,
33287c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_svcname)) >= sizeof (request.rpr_svcname))
33297c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	if (strlcpy(request.rpr_instname, instname,
33327c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_instname)) >= sizeof (request.rpr_instname))
33337c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate 	if (strlcpy(request.rpr_name, snapname,
33367c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_name)) >= sizeof (request.rpr_name))
33377c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
33407c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_SNAPSHOT_TAKE_NAMED;
33417c478bd9Sstevel@tonic-gate 	request.rpr_entityid_src = inst->rd_d.rd_entity;
33427c478bd9Sstevel@tonic-gate 	request.rpr_entityid_dest = snap->rd_d.rd_entity;
33437c478bd9Sstevel@tonic-gate 
33447c478bd9Sstevel@tonic-gate 	datael_finish_reset(&inst->rd_d);
33457c478bd9Sstevel@tonic-gate 	datael_finish_reset(&snap->rd_d);
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
33487c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
33497c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
33507c478bd9Sstevel@tonic-gate 
33517c478bd9Sstevel@tonic-gate 	if (r < 0)
33527c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
33557c478bd9Sstevel@tonic-gate 		assert(response.rpr_response !=
33567c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_FAIL_TYPE_MISMATCH);
33577c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
33587c478bd9Sstevel@tonic-gate 	}
33597c478bd9Sstevel@tonic-gate 
33607c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
33617c478bd9Sstevel@tonic-gate }
33627c478bd9Sstevel@tonic-gate 
33637c478bd9Sstevel@tonic-gate int
33647c478bd9Sstevel@tonic-gate _scf_snapshot_take_new(scf_instance_t *inst, const char *name,
33657c478bd9Sstevel@tonic-gate     scf_snapshot_t *snap)
33667c478bd9Sstevel@tonic-gate {
33677c478bd9Sstevel@tonic-gate 	return (_scf_snapshot_take(inst, name, snap, REP_SNAPSHOT_NEW));
33687c478bd9Sstevel@tonic-gate }
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate int
33717c478bd9Sstevel@tonic-gate _scf_snapshot_take_attach(scf_instance_t *inst, scf_snapshot_t *snap)
33727c478bd9Sstevel@tonic-gate {
33737c478bd9Sstevel@tonic-gate 	return (_scf_snapshot_take(inst, NULL, snap, REP_SNAPSHOT_ATTACH));
33747c478bd9Sstevel@tonic-gate }
33757c478bd9Sstevel@tonic-gate 
33767c478bd9Sstevel@tonic-gate int
33777c478bd9Sstevel@tonic-gate _scf_snapshot_attach(scf_snapshot_t *src, scf_snapshot_t *dest)
33787c478bd9Sstevel@tonic-gate {
33797c478bd9Sstevel@tonic-gate 	scf_handle_t *h = dest->rd_d.rd_handle;
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 	struct rep_protocol_snapshot_attach request;
33827c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 	int r;
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 	if (h != src->rd_d.rd_handle)
33877c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
33887c478bd9Sstevel@tonic-gate 
33897c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
33907c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_SNAPSHOT_ATTACH;
33917c478bd9Sstevel@tonic-gate 	request.rpr_entityid_src = src->rd_d.rd_entity;
33927c478bd9Sstevel@tonic-gate 	request.rpr_entityid_dest = dest->rd_d.rd_entity;
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate 	datael_finish_reset(&src->rd_d);
33957c478bd9Sstevel@tonic-gate 	datael_finish_reset(&dest->rd_d);
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
33987c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
33997c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate 	if (r < 0)
34027c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
34057c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
34087c478bd9Sstevel@tonic-gate }
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate /*
34117c478bd9Sstevel@tonic-gate  * Fails with _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED, _INTERNAL
34127c478bd9Sstevel@tonic-gate  * (bad server response or id in use), _NO_RESOURCES, or _NO_MEMORY.
34137c478bd9Sstevel@tonic-gate  */
34147c478bd9Sstevel@tonic-gate scf_property_t *
34157c478bd9Sstevel@tonic-gate scf_property_create(scf_handle_t *handle)
34167c478bd9Sstevel@tonic-gate {
34177c478bd9Sstevel@tonic-gate 	scf_property_t *ret;
34187c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
34197c478bd9Sstevel@tonic-gate 	if (ret != NULL) {
34207c478bd9Sstevel@tonic-gate 		if (datael_init(&ret->rd_d, handle,
34217c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_ENTITY_PROPERTY) == -1) {
34227c478bd9Sstevel@tonic-gate 			uu_free(ret);
34237c478bd9Sstevel@tonic-gate 			return (NULL);
34247c478bd9Sstevel@tonic-gate 		}
34257c478bd9Sstevel@tonic-gate 	} else {
34267c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
34277c478bd9Sstevel@tonic-gate 	}
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 	return (ret);
34307c478bd9Sstevel@tonic-gate }
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate scf_handle_t *
34337c478bd9Sstevel@tonic-gate scf_property_handle(const scf_property_t *val)
34347c478bd9Sstevel@tonic-gate {
34357c478bd9Sstevel@tonic-gate 	return (datael_handle(&val->rd_d));
34367c478bd9Sstevel@tonic-gate }
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate void
34397c478bd9Sstevel@tonic-gate scf_property_destroy(scf_property_t *val)
34407c478bd9Sstevel@tonic-gate {
34417c478bd9Sstevel@tonic-gate 	if (val == NULL)
34427c478bd9Sstevel@tonic-gate 		return;
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate 	datael_destroy(&val->rd_d);
34457c478bd9Sstevel@tonic-gate 	uu_free(val);
34467c478bd9Sstevel@tonic-gate }
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate static int
34497c478bd9Sstevel@tonic-gate property_type_locked(const scf_property_t *prop,
34507c478bd9Sstevel@tonic-gate     rep_protocol_value_type_t *out)
34517c478bd9Sstevel@tonic-gate {
34527c478bd9Sstevel@tonic-gate 	scf_handle_t *h = prop->rd_d.rd_handle;
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate 	struct rep_protocol_property_request request;
34557c478bd9Sstevel@tonic-gate 	struct rep_protocol_integer_response response;
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 	int r;
34587c478bd9Sstevel@tonic-gate 
34597c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_PROPERTY_GET_TYPE;
34627c478bd9Sstevel@tonic-gate 	request.rpr_entityid = prop->rd_d.rd_entity;
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	datael_finish_reset(&prop->rd_d);
34657c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
34667c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 	if (r < 0)
34697c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS ||
34727c478bd9Sstevel@tonic-gate 	    r < sizeof (response)) {
34737c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
34747c478bd9Sstevel@tonic-gate 	}
34757c478bd9Sstevel@tonic-gate 	*out = response.rpr_value;
34767c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
34777c478bd9Sstevel@tonic-gate }
34787c478bd9Sstevel@tonic-gate 
34797c478bd9Sstevel@tonic-gate int
34807c478bd9Sstevel@tonic-gate scf_property_type(const scf_property_t *prop, scf_type_t *out)
34817c478bd9Sstevel@tonic-gate {
34827c478bd9Sstevel@tonic-gate 	scf_handle_t *h = prop->rd_d.rd_handle;
34837c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t out_raw;
34847c478bd9Sstevel@tonic-gate 	int ret;
34857c478bd9Sstevel@tonic-gate 
34867c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
34877c478bd9Sstevel@tonic-gate 	ret = property_type_locked(prop, &out_raw);
34887c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate 	if (ret == SCF_SUCCESS)
34917c478bd9Sstevel@tonic-gate 		*out = scf_protocol_type_to_type(out_raw);
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate 	return (ret);
34947c478bd9Sstevel@tonic-gate }
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate int
34977c478bd9Sstevel@tonic-gate scf_property_is_type(const scf_property_t *prop, scf_type_t base_arg)
34987c478bd9Sstevel@tonic-gate {
34997c478bd9Sstevel@tonic-gate 	scf_handle_t *h = prop->rd_d.rd_handle;
35007c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t base = scf_type_to_protocol_type(base_arg);
35017c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t type;
35027c478bd9Sstevel@tonic-gate 	int ret;
35037c478bd9Sstevel@tonic-gate 
35047c478bd9Sstevel@tonic-gate 	if (base == REP_PROTOCOL_TYPE_INVALID)
35057c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
35087c478bd9Sstevel@tonic-gate 	ret = property_type_locked(prop, &type);
35097c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate 	if (ret == SCF_SUCCESS) {
35127c478bd9Sstevel@tonic-gate 		if (!scf_is_compatible_type(base, type))
35137c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_TYPE_MISMATCH));
35147c478bd9Sstevel@tonic-gate 	}
35157c478bd9Sstevel@tonic-gate 	return (ret);
35167c478bd9Sstevel@tonic-gate }
35177c478bd9Sstevel@tonic-gate 
35187c478bd9Sstevel@tonic-gate ssize_t
35197c478bd9Sstevel@tonic-gate scf_property_get_name(const scf_property_t *prop, char *out, size_t len)
35207c478bd9Sstevel@tonic-gate {
35217c478bd9Sstevel@tonic-gate 	return (datael_get_name(&prop->rd_d, out, len, RP_ENTITY_NAME_NAME));
35227c478bd9Sstevel@tonic-gate }
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate /*
35257c478bd9Sstevel@tonic-gate  * transaction functions
35267c478bd9Sstevel@tonic-gate  */
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate /*
35297c478bd9Sstevel@tonic-gate  * Fails with _NO_MEMORY, _INVALID_ARGUMENT (handle is NULL), _HANDLE_DESTROYED,
35307c478bd9Sstevel@tonic-gate  * _INTERNAL (bad server response or id in use), or _NO_RESOURCES.
35317c478bd9Sstevel@tonic-gate  */
35327c478bd9Sstevel@tonic-gate scf_transaction_t *
35337c478bd9Sstevel@tonic-gate scf_transaction_create(scf_handle_t *handle)
35347c478bd9Sstevel@tonic-gate {
35357c478bd9Sstevel@tonic-gate 	scf_transaction_t *ret;
35367c478bd9Sstevel@tonic-gate 
35377c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (scf_transaction_t));
35387c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
35397c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
35407c478bd9Sstevel@tonic-gate 		return (NULL);
35417c478bd9Sstevel@tonic-gate 	}
35427c478bd9Sstevel@tonic-gate 	if (datael_init(&ret->tran_pg.rd_d, handle,
35437c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTYGRP) == -1) {
35447c478bd9Sstevel@tonic-gate 		uu_free(ret);
35457c478bd9Sstevel@tonic-gate 		return (NULL);			/* error already set */
35467c478bd9Sstevel@tonic-gate 	}
35477c478bd9Sstevel@tonic-gate 	ret->tran_state = TRAN_STATE_NEW;
35487c478bd9Sstevel@tonic-gate 	ret->tran_props = uu_list_create(tran_entry_pool, ret, UU_LIST_SORTED);
35497c478bd9Sstevel@tonic-gate 	if (ret->tran_props == NULL) {
35507c478bd9Sstevel@tonic-gate 		datael_destroy(&ret->tran_pg.rd_d);
35517c478bd9Sstevel@tonic-gate 		uu_free(ret);
35527c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
35537c478bd9Sstevel@tonic-gate 		return (NULL);
35547c478bd9Sstevel@tonic-gate 	}
35557c478bd9Sstevel@tonic-gate 
35567c478bd9Sstevel@tonic-gate 	return (ret);
35577c478bd9Sstevel@tonic-gate }
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate scf_handle_t *
35607c478bd9Sstevel@tonic-gate scf_transaction_handle(const scf_transaction_t *val)
35617c478bd9Sstevel@tonic-gate {
35627c478bd9Sstevel@tonic-gate 	return (handle_get(val->tran_pg.rd_d.rd_handle));
35637c478bd9Sstevel@tonic-gate }
35647c478bd9Sstevel@tonic-gate 
35657c478bd9Sstevel@tonic-gate int
35667c478bd9Sstevel@tonic-gate scf_transaction_start(scf_transaction_t *tran, scf_propertygroup_t *pg)
35677c478bd9Sstevel@tonic-gate {
35687c478bd9Sstevel@tonic-gate 	scf_handle_t *h = tran->tran_pg.rd_d.rd_handle;
35697c478bd9Sstevel@tonic-gate 
35707c478bd9Sstevel@tonic-gate 	struct rep_protocol_transaction_start request;
35717c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
35727c478bd9Sstevel@tonic-gate 	int r;
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	if (h != pg->rd_d.rd_handle)
35757c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
35787c478bd9Sstevel@tonic-gate 	if (tran->tran_state != TRAN_STATE_NEW) {
35797c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
35807c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_IN_USE));
35817c478bd9Sstevel@tonic-gate 	}
35827c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_PROPERTYGRP_TX_START;
35837c478bd9Sstevel@tonic-gate 	request.rpr_entityid_tx = tran->tran_pg.rd_d.rd_entity;
35847c478bd9Sstevel@tonic-gate 	request.rpr_entityid = pg->rd_d.rd_entity;
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate 	datael_finish_reset(&tran->tran_pg.rd_d);
35877c478bd9Sstevel@tonic-gate 	datael_finish_reset(&pg->rd_d);
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
35907c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate 	if (r < 0) {
35937c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
35947c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
35957c478bd9Sstevel@tonic-gate 	}
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate 	/* r < sizeof (response) cannot happen because sizeof (response) == 4 */
35987c478bd9Sstevel@tonic-gate 
35997c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS ||
36007c478bd9Sstevel@tonic-gate 	    r < sizeof (response)) {
36017c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
36027c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
36037c478bd9Sstevel@tonic-gate 	}
36047c478bd9Sstevel@tonic-gate 
36057c478bd9Sstevel@tonic-gate 	tran->tran_state = TRAN_STATE_SETUP;
36067c478bd9Sstevel@tonic-gate 	tran->tran_invalid = 0;
36077c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
36087c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
36097c478bd9Sstevel@tonic-gate }
36107c478bd9Sstevel@tonic-gate 
36117c478bd9Sstevel@tonic-gate static void
36127c478bd9Sstevel@tonic-gate entry_invalidate(scf_transaction_entry_t *cur, int and_destroy,
36137c478bd9Sstevel@tonic-gate     int and_reset_value)
36147c478bd9Sstevel@tonic-gate {
36157c478bd9Sstevel@tonic-gate 	scf_value_t *v, *next;
36167c478bd9Sstevel@tonic-gate 	scf_transaction_t *tx;
36177c478bd9Sstevel@tonic-gate 	scf_handle_t *h = cur->entry_handle;
36187c478bd9Sstevel@tonic-gate 
36197c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	if ((tx = cur->entry_tx) != NULL) {
36227c478bd9Sstevel@tonic-gate 		tx->tran_invalid = 1;
36237c478bd9Sstevel@tonic-gate 		uu_list_remove(tx->tran_props, cur);
36247c478bd9Sstevel@tonic-gate 		cur->entry_tx = NULL;
36257c478bd9Sstevel@tonic-gate 	}
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate 	cur->entry_property = NULL;
36287c478bd9Sstevel@tonic-gate 	cur->entry_state = ENTRY_STATE_INVALID;
36297c478bd9Sstevel@tonic-gate 	cur->entry_action = REP_PROTOCOL_TX_ENTRY_INVALID;
36307c478bd9Sstevel@tonic-gate 	cur->entry_type = REP_PROTOCOL_TYPE_INVALID;
36317c478bd9Sstevel@tonic-gate 
36327c478bd9Sstevel@tonic-gate 	for (v = cur->entry_head; v != NULL; v = next) {
36337c478bd9Sstevel@tonic-gate 		next = v->value_next;
36347c478bd9Sstevel@tonic-gate 		v->value_tx = NULL;
36357c478bd9Sstevel@tonic-gate 		v->value_next = NULL;
36367c478bd9Sstevel@tonic-gate 		if (and_destroy || and_reset_value)
36377c478bd9Sstevel@tonic-gate 			scf_value_reset_locked(v, and_destroy);
36387c478bd9Sstevel@tonic-gate 	}
36397c478bd9Sstevel@tonic-gate 	cur->entry_head = NULL;
36407c478bd9Sstevel@tonic-gate }
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate static void
36437c478bd9Sstevel@tonic-gate entry_destroy_locked(scf_transaction_entry_t *entry)
36447c478bd9Sstevel@tonic-gate {
36457c478bd9Sstevel@tonic-gate 	scf_handle_t *h = entry->entry_handle;
36467c478bd9Sstevel@tonic-gate 
36477c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
36487c478bd9Sstevel@tonic-gate 
36497c478bd9Sstevel@tonic-gate 	entry_invalidate(entry, 0, 0);
36507c478bd9Sstevel@tonic-gate 
36517c478bd9Sstevel@tonic-gate 	entry->entry_handle = NULL;
36527c478bd9Sstevel@tonic-gate 	assert(h->rh_entries > 0);
36537c478bd9Sstevel@tonic-gate 	--h->rh_entries;
36547c478bd9Sstevel@tonic-gate 	--h->rh_extrefs;
36557c478bd9Sstevel@tonic-gate 	uu_list_node_fini(entry, &entry->entry_link, tran_entry_pool);
36567c478bd9Sstevel@tonic-gate 	uu_free(entry);
36577c478bd9Sstevel@tonic-gate }
36587c478bd9Sstevel@tonic-gate 
365976cf44abSjeanm /*
366076cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
366176cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
366276cf44abSjeanm  * _BACKEND_ACCESS, _IN_USE, _NOT_FOUND, _EXISTS, _TYPE_MISMATCH.
366376cf44abSjeanm  */
36647c478bd9Sstevel@tonic-gate static int
36657c478bd9Sstevel@tonic-gate transaction_add(scf_transaction_t *tran, scf_transaction_entry_t *entry,
36667c478bd9Sstevel@tonic-gate     enum rep_protocol_transaction_action action,
36677c478bd9Sstevel@tonic-gate     const char *prop, rep_protocol_value_type_t type)
36687c478bd9Sstevel@tonic-gate {
36697c478bd9Sstevel@tonic-gate 	scf_handle_t *h = tran->tran_pg.rd_d.rd_handle;
36707c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *old;
36717c478bd9Sstevel@tonic-gate 	scf_property_t *prop_p;
36727c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t oldtype;
36737c478bd9Sstevel@tonic-gate 	scf_error_t error = SCF_ERROR_NONE;
36747c478bd9Sstevel@tonic-gate 	int ret;
36757c478bd9Sstevel@tonic-gate 	uu_list_index_t idx;
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate 	if (h != entry->entry_handle)
36787c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
36797c478bd9Sstevel@tonic-gate 
36807c478bd9Sstevel@tonic-gate 	if (action == REP_PROTOCOL_TX_ENTRY_DELETE)
36817c478bd9Sstevel@tonic-gate 		assert(type == REP_PROTOCOL_TYPE_INVALID);
36827c478bd9Sstevel@tonic-gate 	else if (type == REP_PROTOCOL_TYPE_INVALID)
36837c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 	prop_p = HANDLE_HOLD_PROPERTY(h);
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
36887c478bd9Sstevel@tonic-gate 	if (tran->tran_state != TRAN_STATE_SETUP) {
36897c478bd9Sstevel@tonic-gate 		error = SCF_ERROR_NOT_SET;
36907c478bd9Sstevel@tonic-gate 		goto error;
36917c478bd9Sstevel@tonic-gate 	}
36927c478bd9Sstevel@tonic-gate 	if (tran->tran_invalid) {
36937c478bd9Sstevel@tonic-gate 		error = SCF_ERROR_NOT_SET;
36947c478bd9Sstevel@tonic-gate 		goto error;
36957c478bd9Sstevel@tonic-gate 	}
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate 	if (entry->entry_state != ENTRY_STATE_INVALID)
36987c478bd9Sstevel@tonic-gate 		entry_invalidate(entry, 0, 0);
36997c478bd9Sstevel@tonic-gate 
37007c478bd9Sstevel@tonic-gate 	old = uu_list_find(tran->tran_props, &prop, NULL, &idx);
37017c478bd9Sstevel@tonic-gate 	if (old != NULL) {
37027c478bd9Sstevel@tonic-gate 		error = SCF_ERROR_IN_USE;
37037c478bd9Sstevel@tonic-gate 		goto error;
37047c478bd9Sstevel@tonic-gate 	}
37057c478bd9Sstevel@tonic-gate 
37067c478bd9Sstevel@tonic-gate 	ret = datael_get_child_locked(&tran->tran_pg.rd_d, prop,
37077c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_PROPERTY, &prop_p->rd_d);
37087c478bd9Sstevel@tonic-gate 	if (ret == -1 && (error = scf_error()) != SCF_ERROR_NOT_FOUND) {
37097c478bd9Sstevel@tonic-gate 		goto error;
37107c478bd9Sstevel@tonic-gate 	}
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 	switch (action) {
37137c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TX_ENTRY_DELETE:
37147c478bd9Sstevel@tonic-gate 		if (ret == -1) {
37157c478bd9Sstevel@tonic-gate 			error = SCF_ERROR_NOT_FOUND;
37167c478bd9Sstevel@tonic-gate 			goto error;
37177c478bd9Sstevel@tonic-gate 		}
37187c478bd9Sstevel@tonic-gate 		break;
37197c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TX_ENTRY_NEW:
37207c478bd9Sstevel@tonic-gate 		if (ret != -1) {
37217c478bd9Sstevel@tonic-gate 			error = SCF_ERROR_EXISTS;
37227c478bd9Sstevel@tonic-gate 			goto error;
37237c478bd9Sstevel@tonic-gate 		}
37247c478bd9Sstevel@tonic-gate 		break;
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TX_ENTRY_CLEAR:
37277c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TX_ENTRY_REPLACE:
37287c478bd9Sstevel@tonic-gate 		if (ret == -1) {
37297c478bd9Sstevel@tonic-gate 			error = SCF_ERROR_NOT_FOUND;
37307c478bd9Sstevel@tonic-gate 			goto error;
37317c478bd9Sstevel@tonic-gate 		}
37327c478bd9Sstevel@tonic-gate 		if (action == REP_PROTOCOL_TX_ENTRY_CLEAR) {
37337c478bd9Sstevel@tonic-gate 			if (property_type_locked(prop_p, &oldtype) == -1) {
37347c478bd9Sstevel@tonic-gate 				error = scf_error();
37357c478bd9Sstevel@tonic-gate 				goto error;
37367c478bd9Sstevel@tonic-gate 			}
37377c478bd9Sstevel@tonic-gate 			if (oldtype != type) {
37387c478bd9Sstevel@tonic-gate 				error = SCF_ERROR_TYPE_MISMATCH;
37397c478bd9Sstevel@tonic-gate 				goto error;
37407c478bd9Sstevel@tonic-gate 			}
37417c478bd9Sstevel@tonic-gate 		}
37427c478bd9Sstevel@tonic-gate 		break;
37437c478bd9Sstevel@tonic-gate 	default:
37447c478bd9Sstevel@tonic-gate 		assert(0);
37457c478bd9Sstevel@tonic-gate 		abort();
37467c478bd9Sstevel@tonic-gate 	}
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 	(void) strlcpy(entry->entry_namebuf, prop,
37497c478bd9Sstevel@tonic-gate 	    sizeof (entry->entry_namebuf));
37507c478bd9Sstevel@tonic-gate 	entry->entry_property = entry->entry_namebuf;
37517c478bd9Sstevel@tonic-gate 	entry->entry_action = action;
37527c478bd9Sstevel@tonic-gate 	entry->entry_type = type;
37537c478bd9Sstevel@tonic-gate 
37547c478bd9Sstevel@tonic-gate 	entry->entry_state = ENTRY_STATE_IN_TX_ACTION;
37557c478bd9Sstevel@tonic-gate 	entry->entry_tx = tran;
37567c478bd9Sstevel@tonic-gate 	uu_list_insert(tran->tran_props, entry, idx);
37577c478bd9Sstevel@tonic-gate 
37587c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
37597c478bd9Sstevel@tonic-gate 
37607c478bd9Sstevel@tonic-gate 	HANDLE_RELE_PROPERTY(h);
37617c478bd9Sstevel@tonic-gate 
37627c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
37637c478bd9Sstevel@tonic-gate 
37647c478bd9Sstevel@tonic-gate error:
37657c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 	HANDLE_RELE_PROPERTY(h);
37687c478bd9Sstevel@tonic-gate 
37697c478bd9Sstevel@tonic-gate 	return (scf_set_error(error));
37707c478bd9Sstevel@tonic-gate }
37717c478bd9Sstevel@tonic-gate 
377276cf44abSjeanm /*
377376cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
377476cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
377576cf44abSjeanm  * _BACKEND_ACCESS, _IN_USE, _NOT_FOUND, _EXISTS, _TYPE_MISMATCH.
377676cf44abSjeanm  */
37777c478bd9Sstevel@tonic-gate int
37787c478bd9Sstevel@tonic-gate scf_transaction_property_new(scf_transaction_t *tx,
37797c478bd9Sstevel@tonic-gate     scf_transaction_entry_t *entry, const char *prop, scf_type_t type)
37807c478bd9Sstevel@tonic-gate {
37817c478bd9Sstevel@tonic-gate 	return (transaction_add(tx, entry, REP_PROTOCOL_TX_ENTRY_NEW,
37827c478bd9Sstevel@tonic-gate 	    prop, scf_type_to_protocol_type(type)));
37837c478bd9Sstevel@tonic-gate }
37847c478bd9Sstevel@tonic-gate 
378576cf44abSjeanm /*
378676cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
378776cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
378876cf44abSjeanm  * _BACKEND_ACCESS, _IN_USE, _NOT_FOUND, _EXISTS, _TYPE_MISMATCH.
378976cf44abSjeanm  */
37907c478bd9Sstevel@tonic-gate int
37917c478bd9Sstevel@tonic-gate scf_transaction_property_change(scf_transaction_t *tx,
37927c478bd9Sstevel@tonic-gate     scf_transaction_entry_t *entry, const char *prop, scf_type_t type)
37937c478bd9Sstevel@tonic-gate {
37947c478bd9Sstevel@tonic-gate 	return (transaction_add(tx, entry, REP_PROTOCOL_TX_ENTRY_CLEAR,
37957c478bd9Sstevel@tonic-gate 	    prop, scf_type_to_protocol_type(type)));
37967c478bd9Sstevel@tonic-gate }
37977c478bd9Sstevel@tonic-gate 
379876cf44abSjeanm /*
379976cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
380076cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
380176cf44abSjeanm  * _BACKEND_ACCESS, _IN_USE, _NOT_FOUND, _EXISTS, _TYPE_MISMATCH.
380276cf44abSjeanm  */
38037c478bd9Sstevel@tonic-gate int
38047c478bd9Sstevel@tonic-gate scf_transaction_property_change_type(scf_transaction_t *tx,
38057c478bd9Sstevel@tonic-gate     scf_transaction_entry_t *entry, const char *prop, scf_type_t type)
38067c478bd9Sstevel@tonic-gate {
38077c478bd9Sstevel@tonic-gate 	return (transaction_add(tx, entry, REP_PROTOCOL_TX_ENTRY_REPLACE,
38087c478bd9Sstevel@tonic-gate 	    prop, scf_type_to_protocol_type(type)));
38097c478bd9Sstevel@tonic-gate }
38107c478bd9Sstevel@tonic-gate 
381176cf44abSjeanm /*
381276cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _NOT_BOUND,
381376cf44abSjeanm  * _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED, _NO_RESOURCES,
381476cf44abSjeanm  * _BACKEND_ACCESS, _IN_USE, _NOT_FOUND, _EXISTS, _TYPE_MISMATCH.
381576cf44abSjeanm  */
38167c478bd9Sstevel@tonic-gate int
38177c478bd9Sstevel@tonic-gate scf_transaction_property_delete(scf_transaction_t *tx,
38187c478bd9Sstevel@tonic-gate     scf_transaction_entry_t *entry, const char *prop)
38197c478bd9Sstevel@tonic-gate {
38207c478bd9Sstevel@tonic-gate 	return (transaction_add(tx, entry, REP_PROTOCOL_TX_ENTRY_DELETE,
38217c478bd9Sstevel@tonic-gate 	    prop, REP_PROTOCOL_TYPE_INVALID));
38227c478bd9Sstevel@tonic-gate }
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate #define	BAD_SIZE (-1UL)
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate static size_t
38277c478bd9Sstevel@tonic-gate commit_value(caddr_t data, scf_value_t *val, rep_protocol_value_type_t t)
38287c478bd9Sstevel@tonic-gate {
38297c478bd9Sstevel@tonic-gate 	size_t len;
38307c478bd9Sstevel@tonic-gate 
38317c478bd9Sstevel@tonic-gate 	assert(val->value_type == t);
38327c478bd9Sstevel@tonic-gate 
38337c478bd9Sstevel@tonic-gate 	if (t == REP_PROTOCOL_TYPE_OPAQUE) {
38347c478bd9Sstevel@tonic-gate 		len = scf_opaque_encode(data, val->value_value,
38357c478bd9Sstevel@tonic-gate 		    val->value_size);
38367c478bd9Sstevel@tonic-gate 	} else {
38377c478bd9Sstevel@tonic-gate 		if (data != NULL)
38387c478bd9Sstevel@tonic-gate 			len = strlcpy(data, val->value_value,
38397c478bd9Sstevel@tonic-gate 			    REP_PROTOCOL_VALUE_LEN);
38407c478bd9Sstevel@tonic-gate 		else
38417c478bd9Sstevel@tonic-gate 			len = strlen(val->value_value);
38427c478bd9Sstevel@tonic-gate 		if (len >= REP_PROTOCOL_VALUE_LEN)
38437c478bd9Sstevel@tonic-gate 			return (BAD_SIZE);
38447c478bd9Sstevel@tonic-gate 	}
38457c478bd9Sstevel@tonic-gate 	return (len + 1);	/* count the '\0' */
38467c478bd9Sstevel@tonic-gate }
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate static size_t
38497c478bd9Sstevel@tonic-gate commit_process(scf_transaction_entry_t *cur,
38507c478bd9Sstevel@tonic-gate     struct rep_protocol_transaction_cmd *out)
38517c478bd9Sstevel@tonic-gate {
38527c478bd9Sstevel@tonic-gate 	scf_value_t *child;
38537c478bd9Sstevel@tonic-gate 	size_t sz = 0;
38547c478bd9Sstevel@tonic-gate 	size_t len;
38557c478bd9Sstevel@tonic-gate 	caddr_t data = (caddr_t)out->rptc_data;
38567c478bd9Sstevel@tonic-gate 	caddr_t val_data;
38577c478bd9Sstevel@tonic-gate 
38587c478bd9Sstevel@tonic-gate 	if (out != NULL) {
38597c478bd9Sstevel@tonic-gate 		len = strlcpy(data, cur->entry_property, REP_PROTOCOL_NAME_LEN);
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate 		out->rptc_action = cur->entry_action;
38627c478bd9Sstevel@tonic-gate 		out->rptc_type = cur->entry_type;
38637c478bd9Sstevel@tonic-gate 		out->rptc_name_len = len + 1;
38647c478bd9Sstevel@tonic-gate 	} else {
38657c478bd9Sstevel@tonic-gate 		len = strlen(cur->entry_property);
38667c478bd9Sstevel@tonic-gate 	}
38677c478bd9Sstevel@tonic-gate 
38687c478bd9Sstevel@tonic-gate 	if (len >= REP_PROTOCOL_NAME_LEN)
38697c478bd9Sstevel@tonic-gate 		return (BAD_SIZE);
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate 	len = TX_SIZE(len + 1);
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 	sz += len;
38747c478bd9Sstevel@tonic-gate 	val_data = data + len;
38757c478bd9Sstevel@tonic-gate 
38767c478bd9Sstevel@tonic-gate 	for (child = cur->entry_head; child != NULL;
38777c478bd9Sstevel@tonic-gate 	    child = child->value_next) {
38787c478bd9Sstevel@tonic-gate 		assert(cur->entry_action != REP_PROTOCOL_TX_ENTRY_DELETE);
38797c478bd9Sstevel@tonic-gate 		if (out != NULL) {
38807c478bd9Sstevel@tonic-gate 			len = commit_value(val_data + sizeof (uint32_t), child,
38817c478bd9Sstevel@tonic-gate 			    cur->entry_type);
38827c478bd9Sstevel@tonic-gate 			/* LINTED alignment */
38837c478bd9Sstevel@tonic-gate 			*(uint32_t *)val_data = len;
38847c478bd9Sstevel@tonic-gate 		} else
38857c478bd9Sstevel@tonic-gate 			len = commit_value(NULL, child, cur->entry_type);
38867c478bd9Sstevel@tonic-gate 
38877c478bd9Sstevel@tonic-gate 		if (len == BAD_SIZE)
38887c478bd9Sstevel@tonic-gate 			return (BAD_SIZE);
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate 		len += sizeof (uint32_t);
38917c478bd9Sstevel@tonic-gate 		len = TX_SIZE(len);
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate 		sz += len;
38947c478bd9Sstevel@tonic-gate 		val_data += len;
38957c478bd9Sstevel@tonic-gate 	}
38967c478bd9Sstevel@tonic-gate 
38977c478bd9Sstevel@tonic-gate 	assert(val_data - data == sz);
38987c478bd9Sstevel@tonic-gate 
38997c478bd9Sstevel@tonic-gate 	if (out != NULL)
39007c478bd9Sstevel@tonic-gate 		out->rptc_size = REP_PROTOCOL_TRANSACTION_CMD_SIZE(sz);
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_TRANSACTION_CMD_SIZE(sz));
39037c478bd9Sstevel@tonic-gate }
39047c478bd9Sstevel@tonic-gate 
39057c478bd9Sstevel@tonic-gate int
39067c478bd9Sstevel@tonic-gate scf_transaction_commit(scf_transaction_t *tran)
39077c478bd9Sstevel@tonic-gate {
39087c478bd9Sstevel@tonic-gate 	scf_handle_t *h = tran->tran_pg.rd_d.rd_handle;
39097c478bd9Sstevel@tonic-gate 
39107c478bd9Sstevel@tonic-gate 	struct rep_protocol_transaction_commit *request;
39117c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
39127c478bd9Sstevel@tonic-gate 	uintptr_t cmd;
39137c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *cur;
39147c478bd9Sstevel@tonic-gate 	size_t total, size;
39157c478bd9Sstevel@tonic-gate 	size_t request_size;
39167c478bd9Sstevel@tonic-gate 	size_t new_total;
39177c478bd9Sstevel@tonic-gate 	int r;
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
39207c478bd9Sstevel@tonic-gate 	if (tran->tran_state != TRAN_STATE_SETUP ||
39217c478bd9Sstevel@tonic-gate 	    tran->tran_invalid) {
39227c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
39237c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
39247c478bd9Sstevel@tonic-gate 	}
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 	total = 0;
39277c478bd9Sstevel@tonic-gate 	for (cur = uu_list_first(tran->tran_props); cur != NULL;
39287c478bd9Sstevel@tonic-gate 	    cur = uu_list_next(tran->tran_props, cur)) {
39297c478bd9Sstevel@tonic-gate 		size = commit_process(cur, NULL);
39307c478bd9Sstevel@tonic-gate 		if (size == BAD_SIZE) {
39317c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&h->rh_lock);
39327c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INTERNAL));
39337c478bd9Sstevel@tonic-gate 		}
39347c478bd9Sstevel@tonic-gate 		assert(TX_SIZE(size) == size);
39357c478bd9Sstevel@tonic-gate 		total += size;
39367c478bd9Sstevel@tonic-gate 	}
39377c478bd9Sstevel@tonic-gate 
39387c478bd9Sstevel@tonic-gate 	request_size = REP_PROTOCOL_TRANSACTION_COMMIT_SIZE(total);
39397c478bd9Sstevel@tonic-gate 	request = alloca(request_size);
39407c478bd9Sstevel@tonic-gate 	(void) memset(request, '\0', request_size);
39417c478bd9Sstevel@tonic-gate 	request->rpr_request = REP_PROTOCOL_PROPERTYGRP_TX_COMMIT;
39427c478bd9Sstevel@tonic-gate 	request->rpr_entityid = tran->tran_pg.rd_d.rd_entity;
39437c478bd9Sstevel@tonic-gate 	request->rpr_size = request_size;
39447c478bd9Sstevel@tonic-gate 	cmd = (uintptr_t)request->rpr_cmd;
39457c478bd9Sstevel@tonic-gate 
39467c478bd9Sstevel@tonic-gate 	datael_finish_reset(&tran->tran_pg.rd_d);
39477c478bd9Sstevel@tonic-gate 
39487c478bd9Sstevel@tonic-gate 	new_total = 0;
39497c478bd9Sstevel@tonic-gate 	for (cur = uu_list_first(tran->tran_props); cur != NULL;
39507c478bd9Sstevel@tonic-gate 	    cur = uu_list_next(tran->tran_props, cur)) {
39517c478bd9Sstevel@tonic-gate 		size = commit_process(cur, (void *)cmd);
39527c478bd9Sstevel@tonic-gate 		if (size == BAD_SIZE) {
39537c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&h->rh_lock);
39547c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INTERNAL));
39557c478bd9Sstevel@tonic-gate 		}
39567c478bd9Sstevel@tonic-gate 		cmd += size;
39577c478bd9Sstevel@tonic-gate 		new_total += size;
39587c478bd9Sstevel@tonic-gate 	}
39597c478bd9Sstevel@tonic-gate 	assert(new_total == total);
39607c478bd9Sstevel@tonic-gate 
39617c478bd9Sstevel@tonic-gate 	r = make_door_call(h, request, request_size,
39627c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate 	if (r < 0) {
39657c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
39667c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
39677c478bd9Sstevel@tonic-gate 	}
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS &&
39707c478bd9Sstevel@tonic-gate 	    response.rpr_response != REP_PROTOCOL_FAIL_NOT_LATEST) {
39717c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
39727c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
39737c478bd9Sstevel@tonic-gate 	}
39747c478bd9Sstevel@tonic-gate 
39757c478bd9Sstevel@tonic-gate 	tran->tran_state = TRAN_STATE_COMMITTED;
39767c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
39777c478bd9Sstevel@tonic-gate 	return (response.rpr_response == REP_PROTOCOL_SUCCESS);
39787c478bd9Sstevel@tonic-gate }
39797c478bd9Sstevel@tonic-gate 
39807c478bd9Sstevel@tonic-gate static void
39817c478bd9Sstevel@tonic-gate transaction_reset(scf_transaction_t *tran)
39827c478bd9Sstevel@tonic-gate {
39837c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&tran->tran_pg.rd_d.rd_handle->rh_lock));
39847c478bd9Sstevel@tonic-gate 
39857c478bd9Sstevel@tonic-gate 	tran->tran_state = TRAN_STATE_NEW;
39867c478bd9Sstevel@tonic-gate 	datael_reset_locked(&tran->tran_pg.rd_d);
39877c478bd9Sstevel@tonic-gate }
39887c478bd9Sstevel@tonic-gate 
39897c478bd9Sstevel@tonic-gate static void
39907c478bd9Sstevel@tonic-gate scf_transaction_reset_impl(scf_transaction_t *tran, int and_destroy,
39917c478bd9Sstevel@tonic-gate     int and_reset_value)
39927c478bd9Sstevel@tonic-gate {
39937c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *cur;
39947c478bd9Sstevel@tonic-gate 	void *cookie;
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&tran->tran_pg.rd_d.rd_handle->rh_lock);
39977c478bd9Sstevel@tonic-gate 	cookie = NULL;
39987c478bd9Sstevel@tonic-gate 	while ((cur = uu_list_teardown(tran->tran_props, &cookie)) != NULL) {
39997c478bd9Sstevel@tonic-gate 		cur->entry_tx = NULL;
40007c478bd9Sstevel@tonic-gate 
40017c478bd9Sstevel@tonic-gate 		assert(cur->entry_state == ENTRY_STATE_IN_TX_ACTION);
40027c478bd9Sstevel@tonic-gate 		cur->entry_state = ENTRY_STATE_INVALID;
40037c478bd9Sstevel@tonic-gate 
40047c478bd9Sstevel@tonic-gate 		entry_invalidate(cur, and_destroy, and_reset_value);
40057c478bd9Sstevel@tonic-gate 		if (and_destroy)
40067c478bd9Sstevel@tonic-gate 			entry_destroy_locked(cur);
40077c478bd9Sstevel@tonic-gate 	}
40087c478bd9Sstevel@tonic-gate 	transaction_reset(tran);
40097c478bd9Sstevel@tonic-gate 	handle_unrefed(tran->tran_pg.rd_d.rd_handle);
40107c478bd9Sstevel@tonic-gate }
40117c478bd9Sstevel@tonic-gate 
40127c478bd9Sstevel@tonic-gate void
40137c478bd9Sstevel@tonic-gate scf_transaction_reset(scf_transaction_t *tran)
40147c478bd9Sstevel@tonic-gate {
40157c478bd9Sstevel@tonic-gate 	scf_transaction_reset_impl(tran, 0, 0);
40167c478bd9Sstevel@tonic-gate }
40177c478bd9Sstevel@tonic-gate 
40187c478bd9Sstevel@tonic-gate void
40197c478bd9Sstevel@tonic-gate scf_transaction_reset_all(scf_transaction_t *tran)
40207c478bd9Sstevel@tonic-gate {
40217c478bd9Sstevel@tonic-gate 	scf_transaction_reset_impl(tran, 0, 1);
40227c478bd9Sstevel@tonic-gate }
40237c478bd9Sstevel@tonic-gate 
40247c478bd9Sstevel@tonic-gate void
40257c478bd9Sstevel@tonic-gate scf_transaction_destroy(scf_transaction_t *val)
40267c478bd9Sstevel@tonic-gate {
40277c478bd9Sstevel@tonic-gate 	if (val == NULL)
40287c478bd9Sstevel@tonic-gate 		return;
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate 	scf_transaction_reset(val);
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate 	datael_destroy(&val->tran_pg.rd_d);
40337c478bd9Sstevel@tonic-gate 
40347c478bd9Sstevel@tonic-gate 	uu_list_destroy(val->tran_props);
40357c478bd9Sstevel@tonic-gate 	uu_free(val);
40367c478bd9Sstevel@tonic-gate }
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate void
40397c478bd9Sstevel@tonic-gate scf_transaction_destroy_children(scf_transaction_t *tran)
40407c478bd9Sstevel@tonic-gate {
40417c478bd9Sstevel@tonic-gate 	scf_transaction_reset_impl(tran, 1, 0);
40427c478bd9Sstevel@tonic-gate }
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate scf_transaction_entry_t *
40457c478bd9Sstevel@tonic-gate scf_entry_create(scf_handle_t *h)
40467c478bd9Sstevel@tonic-gate {
40477c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *ret;
40487c478bd9Sstevel@tonic-gate 
40497c478bd9Sstevel@tonic-gate 	if (h == NULL) {
40507c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
40517c478bd9Sstevel@tonic-gate 		return (NULL);
40527c478bd9Sstevel@tonic-gate 	}
40537c478bd9Sstevel@tonic-gate 
40547c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (scf_transaction_entry_t));
40557c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
40567c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
40577c478bd9Sstevel@tonic-gate 		return (NULL);
40587c478bd9Sstevel@tonic-gate 	}
40597c478bd9Sstevel@tonic-gate 	ret->entry_action = REP_PROTOCOL_TX_ENTRY_INVALID;
40607c478bd9Sstevel@tonic-gate 	ret->entry_handle = h;
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
40637c478bd9Sstevel@tonic-gate 	if (h->rh_flags & HANDLE_DEAD) {
40647c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
40657c478bd9Sstevel@tonic-gate 		uu_free(ret);
40667c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_HANDLE_DESTROYED);
40677c478bd9Sstevel@tonic-gate 		return (NULL);
40687c478bd9Sstevel@tonic-gate 	}
40697c478bd9Sstevel@tonic-gate 	h->rh_entries++;
40707c478bd9Sstevel@tonic-gate 	h->rh_extrefs++;
40717c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate 	uu_list_node_init(ret, &ret->entry_link, tran_entry_pool);
40747c478bd9Sstevel@tonic-gate 
40757c478bd9Sstevel@tonic-gate 	return (ret);
40767c478bd9Sstevel@tonic-gate }
40777c478bd9Sstevel@tonic-gate 
40787c478bd9Sstevel@tonic-gate scf_handle_t *
40797c478bd9Sstevel@tonic-gate scf_entry_handle(const scf_transaction_entry_t *val)
40807c478bd9Sstevel@tonic-gate {
40817c478bd9Sstevel@tonic-gate 	return (handle_get(val->entry_handle));
40827c478bd9Sstevel@tonic-gate }
40837c478bd9Sstevel@tonic-gate 
40847c478bd9Sstevel@tonic-gate void
40857c478bd9Sstevel@tonic-gate scf_entry_reset(scf_transaction_entry_t *entry)
40867c478bd9Sstevel@tonic-gate {
40877c478bd9Sstevel@tonic-gate 	scf_handle_t *h = entry->entry_handle;
40887c478bd9Sstevel@tonic-gate 
40897c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
40907c478bd9Sstevel@tonic-gate 	entry_invalidate(entry, 0, 0);
40917c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
40927c478bd9Sstevel@tonic-gate }
40937c478bd9Sstevel@tonic-gate 
40947c478bd9Sstevel@tonic-gate void
40957c478bd9Sstevel@tonic-gate scf_entry_destroy_children(scf_transaction_entry_t *entry)
40967c478bd9Sstevel@tonic-gate {
40977c478bd9Sstevel@tonic-gate 	scf_handle_t *h = entry->entry_handle;
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
41007c478bd9Sstevel@tonic-gate 	entry_invalidate(entry, 1, 0);
41017c478bd9Sstevel@tonic-gate 	handle_unrefed(h);			/* drops h->rh_lock */
41027c478bd9Sstevel@tonic-gate }
41037c478bd9Sstevel@tonic-gate 
41047c478bd9Sstevel@tonic-gate void
41057c478bd9Sstevel@tonic-gate scf_entry_destroy(scf_transaction_entry_t *entry)
41067c478bd9Sstevel@tonic-gate {
41077c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
41087c478bd9Sstevel@tonic-gate 
41097c478bd9Sstevel@tonic-gate 	if (entry == NULL)
41107c478bd9Sstevel@tonic-gate 		return;
41117c478bd9Sstevel@tonic-gate 
41127c478bd9Sstevel@tonic-gate 	h = entry->entry_handle;
41137c478bd9Sstevel@tonic-gate 
41147c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
41157c478bd9Sstevel@tonic-gate 	entry_destroy_locked(entry);
41167c478bd9Sstevel@tonic-gate 	handle_unrefed(h);			/* drops h->rh_lock */
41177c478bd9Sstevel@tonic-gate }
41187c478bd9Sstevel@tonic-gate 
41197c478bd9Sstevel@tonic-gate /*
41207c478bd9Sstevel@tonic-gate  * Fails with
41217c478bd9Sstevel@tonic-gate  *   _HANDLE_MISMATCH
41227c478bd9Sstevel@tonic-gate  *   _NOT_SET - has not been added to a transaction
41237c478bd9Sstevel@tonic-gate  *   _INTERNAL - entry is corrupt
41247c478bd9Sstevel@tonic-gate  *   _INVALID_ARGUMENT - entry's transaction is not started or corrupt
41257c478bd9Sstevel@tonic-gate  *			 entry is set to delete a property
41267c478bd9Sstevel@tonic-gate  *			 v is reset or corrupt
41277c478bd9Sstevel@tonic-gate  *   _TYPE_MISMATCH - entry & v's types aren't compatible
41287c478bd9Sstevel@tonic-gate  *   _IN_USE - v has been added to another entry
41297c478bd9Sstevel@tonic-gate  */
41307c478bd9Sstevel@tonic-gate int
41317c478bd9Sstevel@tonic-gate scf_entry_add_value(scf_transaction_entry_t *entry, scf_value_t *v)
41327c478bd9Sstevel@tonic-gate {
41337c478bd9Sstevel@tonic-gate 	scf_handle_t *h = entry->entry_handle;
41347c478bd9Sstevel@tonic-gate 
41357c478bd9Sstevel@tonic-gate 	if (h != v->value_handle)
41367c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
41377c478bd9Sstevel@tonic-gate 
41387c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
41397c478bd9Sstevel@tonic-gate 
41407c478bd9Sstevel@tonic-gate 	if (entry->entry_state == ENTRY_STATE_INVALID) {
41417c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
41427c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
41437c478bd9Sstevel@tonic-gate 	}
4144f2fa366aSjeanm 
4145f2fa366aSjeanm 	if (entry->entry_state != ENTRY_STATE_IN_TX_ACTION) {
4146f2fa366aSjeanm 		(void) pthread_mutex_unlock(&h->rh_lock);
4147f2fa366aSjeanm 		return (scf_set_error(SCF_ERROR_INTERNAL));
4148f2fa366aSjeanm 	}
41497c478bd9Sstevel@tonic-gate 
41507c478bd9Sstevel@tonic-gate 	if (entry->entry_tx->tran_state != TRAN_STATE_SETUP) {
41517c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
41527c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
41537c478bd9Sstevel@tonic-gate 	}
41547c478bd9Sstevel@tonic-gate 
41557c478bd9Sstevel@tonic-gate 	if (entry->entry_action == REP_PROTOCOL_TX_ENTRY_DELETE) {
41567c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
41577c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
41587c478bd9Sstevel@tonic-gate 	}
41597c478bd9Sstevel@tonic-gate 
41607c478bd9Sstevel@tonic-gate 	if (v->value_type == REP_PROTOCOL_TYPE_INVALID) {
41617c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
41627c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
41637c478bd9Sstevel@tonic-gate 	}
41647c478bd9Sstevel@tonic-gate 
41657c478bd9Sstevel@tonic-gate 	if (!scf_is_compatible_type(entry->entry_type, v->value_type)) {
41667c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
41677c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_TYPE_MISMATCH));
41687c478bd9Sstevel@tonic-gate 	}
41697c478bd9Sstevel@tonic-gate 
41707c478bd9Sstevel@tonic-gate 	if (v->value_tx != NULL) {
41717c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
41727c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_IN_USE));
41737c478bd9Sstevel@tonic-gate 	}
41747c478bd9Sstevel@tonic-gate 
41757c478bd9Sstevel@tonic-gate 	v->value_tx = entry;
41767c478bd9Sstevel@tonic-gate 	v->value_next = entry->entry_head;
41777c478bd9Sstevel@tonic-gate 	entry->entry_head = v;
41787c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
41797c478bd9Sstevel@tonic-gate 
41807c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
41817c478bd9Sstevel@tonic-gate }
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate /*
41847c478bd9Sstevel@tonic-gate  * value functions
41857c478bd9Sstevel@tonic-gate  */
41867c478bd9Sstevel@tonic-gate scf_value_t *
41877c478bd9Sstevel@tonic-gate scf_value_create(scf_handle_t *h)
41887c478bd9Sstevel@tonic-gate {
41897c478bd9Sstevel@tonic-gate 	scf_value_t *ret;
41907c478bd9Sstevel@tonic-gate 
41917c478bd9Sstevel@tonic-gate 	if (h == NULL) {
41927c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
41937c478bd9Sstevel@tonic-gate 		return (NULL);
41947c478bd9Sstevel@tonic-gate 	}
41957c478bd9Sstevel@tonic-gate 
41967c478bd9Sstevel@tonic-gate 	ret = uu_zalloc(sizeof (*ret));
41977c478bd9Sstevel@tonic-gate 	if (ret != NULL) {
41987c478bd9Sstevel@tonic-gate 		ret->value_type = REP_PROTOCOL_TYPE_INVALID;
41997c478bd9Sstevel@tonic-gate 		ret->value_handle = h;
42007c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&h->rh_lock);
42017c478bd9Sstevel@tonic-gate 		if (h->rh_flags & HANDLE_DEAD) {
42027c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&h->rh_lock);
42037c478bd9Sstevel@tonic-gate 			uu_free(ret);
42047c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
42057c478bd9Sstevel@tonic-gate 			return (NULL);
42067c478bd9Sstevel@tonic-gate 		}
42077c478bd9Sstevel@tonic-gate 		h->rh_values++;
42087c478bd9Sstevel@tonic-gate 		h->rh_extrefs++;
42097c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
42107c478bd9Sstevel@tonic-gate 	} else {
42117c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
42127c478bd9Sstevel@tonic-gate 	}
42137c478bd9Sstevel@tonic-gate 
42147c478bd9Sstevel@tonic-gate 	return (ret);
42157c478bd9Sstevel@tonic-gate }
42167c478bd9Sstevel@tonic-gate 
42177c478bd9Sstevel@tonic-gate static void
42187c478bd9Sstevel@tonic-gate scf_value_reset_locked(scf_value_t *val, int and_destroy)
42197c478bd9Sstevel@tonic-gate {
42207c478bd9Sstevel@tonic-gate 	scf_value_t **curp;
42217c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *te;
42227c478bd9Sstevel@tonic-gate 
42237c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
42247c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&h->rh_lock));
42257c478bd9Sstevel@tonic-gate 	if (val->value_tx != NULL) {
42267c478bd9Sstevel@tonic-gate 		te = val->value_tx;
42277c478bd9Sstevel@tonic-gate 		te->entry_tx->tran_invalid = 1;
42287c478bd9Sstevel@tonic-gate 
42297c478bd9Sstevel@tonic-gate 		val->value_tx = NULL;
42307c478bd9Sstevel@tonic-gate 
42317c478bd9Sstevel@tonic-gate 		for (curp = &te->entry_head; *curp != NULL;
42327c478bd9Sstevel@tonic-gate 		    curp = &(*curp)->value_next) {
42337c478bd9Sstevel@tonic-gate 			if (*curp == val) {
42347c478bd9Sstevel@tonic-gate 				*curp = val->value_next;
42357c478bd9Sstevel@tonic-gate 				curp = NULL;
42367c478bd9Sstevel@tonic-gate 				break;
42377c478bd9Sstevel@tonic-gate 			}
42387c478bd9Sstevel@tonic-gate 		}
42397c478bd9Sstevel@tonic-gate 		assert(curp == NULL);
42407c478bd9Sstevel@tonic-gate 	}
42417c478bd9Sstevel@tonic-gate 	val->value_type = REP_PROTOCOL_TYPE_INVALID;
42427c478bd9Sstevel@tonic-gate 
42437c478bd9Sstevel@tonic-gate 	if (and_destroy) {
42447c478bd9Sstevel@tonic-gate 		val->value_handle = NULL;
42457c478bd9Sstevel@tonic-gate 		assert(h->rh_values > 0);
42467c478bd9Sstevel@tonic-gate 		--h->rh_values;
42477c478bd9Sstevel@tonic-gate 		--h->rh_extrefs;
42487c478bd9Sstevel@tonic-gate 		uu_free(val);
42497c478bd9Sstevel@tonic-gate 	}
42507c478bd9Sstevel@tonic-gate }
42517c478bd9Sstevel@tonic-gate 
42527c478bd9Sstevel@tonic-gate void
42537c478bd9Sstevel@tonic-gate scf_value_reset(scf_value_t *val)
42547c478bd9Sstevel@tonic-gate {
42557c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
42567c478bd9Sstevel@tonic-gate 
42577c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
42587c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(val, 0);
42597c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
42607c478bd9Sstevel@tonic-gate }
42617c478bd9Sstevel@tonic-gate 
42627c478bd9Sstevel@tonic-gate scf_handle_t *
42637c478bd9Sstevel@tonic-gate scf_value_handle(const scf_value_t *val)
42647c478bd9Sstevel@tonic-gate {
42657c478bd9Sstevel@tonic-gate 	return (handle_get(val->value_handle));
42667c478bd9Sstevel@tonic-gate }
42677c478bd9Sstevel@tonic-gate 
42687c478bd9Sstevel@tonic-gate void
42697c478bd9Sstevel@tonic-gate scf_value_destroy(scf_value_t *val)
42707c478bd9Sstevel@tonic-gate {
42717c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 	if (val == NULL)
42747c478bd9Sstevel@tonic-gate 		return;
42757c478bd9Sstevel@tonic-gate 
42767c478bd9Sstevel@tonic-gate 	h = val->value_handle;
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
42797c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(val, 1);
42807c478bd9Sstevel@tonic-gate 	handle_unrefed(h);			/* drops h->rh_lock */
42817c478bd9Sstevel@tonic-gate }
42827c478bd9Sstevel@tonic-gate 
42837c478bd9Sstevel@tonic-gate scf_type_t
42847c478bd9Sstevel@tonic-gate scf_value_base_type(const scf_value_t *val)
42857c478bd9Sstevel@tonic-gate {
42867c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t t, cur;
42877c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
42887c478bd9Sstevel@tonic-gate 
42897c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
42907c478bd9Sstevel@tonic-gate 	t = val->value_type;
42917c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
42927c478bd9Sstevel@tonic-gate 
42937c478bd9Sstevel@tonic-gate 	for (;;) {
42947c478bd9Sstevel@tonic-gate 		cur = scf_proto_underlying_type(t);
42957c478bd9Sstevel@tonic-gate 		if (cur == t)
42967c478bd9Sstevel@tonic-gate 			break;
42977c478bd9Sstevel@tonic-gate 		t = cur;
42987c478bd9Sstevel@tonic-gate 	}
42997c478bd9Sstevel@tonic-gate 
43007c478bd9Sstevel@tonic-gate 	return (scf_protocol_type_to_type(t));
43017c478bd9Sstevel@tonic-gate }
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate scf_type_t
43047c478bd9Sstevel@tonic-gate scf_value_type(const scf_value_t *val)
43057c478bd9Sstevel@tonic-gate {
43067c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t t;
43077c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
43087c478bd9Sstevel@tonic-gate 
43097c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
43107c478bd9Sstevel@tonic-gate 	t = val->value_type;
43117c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
43127c478bd9Sstevel@tonic-gate 
43137c478bd9Sstevel@tonic-gate 	return (scf_protocol_type_to_type(t));
43147c478bd9Sstevel@tonic-gate }
43157c478bd9Sstevel@tonic-gate 
43167c478bd9Sstevel@tonic-gate int
43177c478bd9Sstevel@tonic-gate scf_value_is_type(const scf_value_t *val, scf_type_t base_arg)
43187c478bd9Sstevel@tonic-gate {
43197c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t t;
43207c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t base = scf_type_to_protocol_type(base_arg);
43217c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
43227c478bd9Sstevel@tonic-gate 
43237c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
43247c478bd9Sstevel@tonic-gate 	t = val->value_type;
43257c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
43267c478bd9Sstevel@tonic-gate 
43277c478bd9Sstevel@tonic-gate 	if (t == REP_PROTOCOL_TYPE_INVALID)
43287c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
43297c478bd9Sstevel@tonic-gate 	if (base == REP_PROTOCOL_TYPE_INVALID)
43307c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
43317c478bd9Sstevel@tonic-gate 	if (!scf_is_compatible_type(base, t))
43327c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_TYPE_MISMATCH));
43337c478bd9Sstevel@tonic-gate 
43347c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
43357c478bd9Sstevel@tonic-gate }
43367c478bd9Sstevel@tonic-gate 
43377c478bd9Sstevel@tonic-gate /*
43387c478bd9Sstevel@tonic-gate  * Fails with
43397c478bd9Sstevel@tonic-gate  *   _NOT_SET - val is reset
43407c478bd9Sstevel@tonic-gate  *   _TYPE_MISMATCH - val's type is not compatible with t
43417c478bd9Sstevel@tonic-gate  */
43427c478bd9Sstevel@tonic-gate static int
43437c478bd9Sstevel@tonic-gate scf_value_check_type(const scf_value_t *val, rep_protocol_value_type_t t)
43447c478bd9Sstevel@tonic-gate {
43457c478bd9Sstevel@tonic-gate 	if (val->value_type == REP_PROTOCOL_TYPE_INVALID) {
43467c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
43477c478bd9Sstevel@tonic-gate 		return (0);
43487c478bd9Sstevel@tonic-gate 	}
43497c478bd9Sstevel@tonic-gate 	if (!scf_is_compatible_type(t, val->value_type)) {
43507c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
43517c478bd9Sstevel@tonic-gate 		return (0);
43527c478bd9Sstevel@tonic-gate 	}
43537c478bd9Sstevel@tonic-gate 	return (1);
43547c478bd9Sstevel@tonic-gate }
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate /*
43577c478bd9Sstevel@tonic-gate  * Fails with
43587c478bd9Sstevel@tonic-gate  *   _NOT_SET - val is reset
43597c478bd9Sstevel@tonic-gate  *   _TYPE_MISMATCH - val is not _TYPE_BOOLEAN
43607c478bd9Sstevel@tonic-gate  */
43617c478bd9Sstevel@tonic-gate int
43627c478bd9Sstevel@tonic-gate scf_value_get_boolean(const scf_value_t *val, uint8_t *out)
43637c478bd9Sstevel@tonic-gate {
43647c478bd9Sstevel@tonic-gate 	char c;
43657c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
43667c478bd9Sstevel@tonic-gate 	uint8_t o;
43677c478bd9Sstevel@tonic-gate 
43687c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
43697c478bd9Sstevel@tonic-gate 	if (!scf_value_check_type(val, REP_PROTOCOL_TYPE_BOOLEAN)) {
43707c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
43717c478bd9Sstevel@tonic-gate 		return (-1);
43727c478bd9Sstevel@tonic-gate 	}
43737c478bd9Sstevel@tonic-gate 
43747c478bd9Sstevel@tonic-gate 	c = val->value_value[0];
43757c478bd9Sstevel@tonic-gate 	assert((c == '0' || c == '1') && val->value_value[1] == 0);
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate 	o = (c != '0');
43787c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
43797c478bd9Sstevel@tonic-gate 	if (out != NULL)
43807c478bd9Sstevel@tonic-gate 		*out = o;
43817c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
43827c478bd9Sstevel@tonic-gate }
43837c478bd9Sstevel@tonic-gate 
43847c478bd9Sstevel@tonic-gate int
43857c478bd9Sstevel@tonic-gate scf_value_get_count(const scf_value_t *val, uint64_t *out)
43867c478bd9Sstevel@tonic-gate {
43877c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
43887c478bd9Sstevel@tonic-gate 	uint64_t o;
43897c478bd9Sstevel@tonic-gate 
43907c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
43917c478bd9Sstevel@tonic-gate 	if (!scf_value_check_type(val, REP_PROTOCOL_TYPE_COUNT)) {
43927c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
43937c478bd9Sstevel@tonic-gate 		return (-1);
43947c478bd9Sstevel@tonic-gate 	}
43957c478bd9Sstevel@tonic-gate 
43967c478bd9Sstevel@tonic-gate 	o = strtoull(val->value_value, NULL, 10);
43977c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
43987c478bd9Sstevel@tonic-gate 	if (out != NULL)
43997c478bd9Sstevel@tonic-gate 		*out = o;
44007c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
44017c478bd9Sstevel@tonic-gate }
44027c478bd9Sstevel@tonic-gate 
44037c478bd9Sstevel@tonic-gate int
44047c478bd9Sstevel@tonic-gate scf_value_get_integer(const scf_value_t *val, int64_t *out)
44057c478bd9Sstevel@tonic-gate {
44067c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
44077c478bd9Sstevel@tonic-gate 	int64_t o;
44087c478bd9Sstevel@tonic-gate 
44097c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
44107c478bd9Sstevel@tonic-gate 	if (!scf_value_check_type(val, REP_PROTOCOL_TYPE_INTEGER)) {
44117c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
44127c478bd9Sstevel@tonic-gate 		return (-1);
44137c478bd9Sstevel@tonic-gate 	}
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate 	o = strtoll(val->value_value, NULL, 10);
44167c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
44177c478bd9Sstevel@tonic-gate 	if (out != NULL)
44187c478bd9Sstevel@tonic-gate 		*out = o;
44197c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
44207c478bd9Sstevel@tonic-gate }
44217c478bd9Sstevel@tonic-gate 
44227c478bd9Sstevel@tonic-gate int
44237c478bd9Sstevel@tonic-gate scf_value_get_time(const scf_value_t *val, int64_t *sec_out, int32_t *nsec_out)
44247c478bd9Sstevel@tonic-gate {
44257c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
44267c478bd9Sstevel@tonic-gate 	char *p;
44277c478bd9Sstevel@tonic-gate 	int64_t os;
44287c478bd9Sstevel@tonic-gate 	int32_t ons;
44297c478bd9Sstevel@tonic-gate 
44307c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
44317c478bd9Sstevel@tonic-gate 	if (!scf_value_check_type(val, REP_PROTOCOL_TYPE_TIME)) {
44327c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
44337c478bd9Sstevel@tonic-gate 		return (-1);
44347c478bd9Sstevel@tonic-gate 	}
44357c478bd9Sstevel@tonic-gate 
44367c478bd9Sstevel@tonic-gate 	os = strtoll(val->value_value, &p, 10);
44377c478bd9Sstevel@tonic-gate 	if (*p == '.')
44387c478bd9Sstevel@tonic-gate 		ons = strtoul(p + 1, NULL, 10);
44397c478bd9Sstevel@tonic-gate 	else
44407c478bd9Sstevel@tonic-gate 		ons = 0;
44417c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
44427c478bd9Sstevel@tonic-gate 	if (sec_out != NULL)
44437c478bd9Sstevel@tonic-gate 		*sec_out = os;
44447c478bd9Sstevel@tonic-gate 	if (nsec_out != NULL)
44457c478bd9Sstevel@tonic-gate 		*nsec_out = ons;
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
44487c478bd9Sstevel@tonic-gate }
44497c478bd9Sstevel@tonic-gate 
44507c478bd9Sstevel@tonic-gate /*
44517c478bd9Sstevel@tonic-gate  * Fails with
44527c478bd9Sstevel@tonic-gate  *   _NOT_SET - val is reset
44537c478bd9Sstevel@tonic-gate  *   _TYPE_MISMATCH - val's type is not compatible with _TYPE_STRING.
44547c478bd9Sstevel@tonic-gate  */
44557c478bd9Sstevel@tonic-gate ssize_t
44567c478bd9Sstevel@tonic-gate scf_value_get_astring(const scf_value_t *val, char *out, size_t len)
44577c478bd9Sstevel@tonic-gate {
44587c478bd9Sstevel@tonic-gate 	ssize_t ret;
44597c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
44607c478bd9Sstevel@tonic-gate 
44617c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
44627c478bd9Sstevel@tonic-gate 	if (!scf_value_check_type(val, REP_PROTOCOL_TYPE_STRING)) {
44637c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
44647c478bd9Sstevel@tonic-gate 		return ((ssize_t)-1);
44657c478bd9Sstevel@tonic-gate 	}
44667c478bd9Sstevel@tonic-gate 	ret = (ssize_t)strlcpy(out, val->value_value, len);
44677c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
44687c478bd9Sstevel@tonic-gate 	return (ret);
44697c478bd9Sstevel@tonic-gate }
44707c478bd9Sstevel@tonic-gate 
44717c478bd9Sstevel@tonic-gate ssize_t
44727c478bd9Sstevel@tonic-gate scf_value_get_ustring(const scf_value_t *val, char *out, size_t len)
44737c478bd9Sstevel@tonic-gate {
44747c478bd9Sstevel@tonic-gate 	ssize_t ret;
44757c478bd9Sstevel@tonic-gate 	scf_handle_t *h = val->value_handle;
44767c478bd9Sstevel@tonic-gate 
44777c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
44787c478bd9Sstevel@tonic-gate 	if (!scf_value_check_type(val, REP_PROTOCOL_SUBTYPE_USTRING)) {
44797c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
44807c478bd9Sstevel@tonic-gate 		return ((ssize_t)-1);
44817c478bd9Sstevel@tonic-gate 	}
44827c478bd9Sstevel@tonic-gate 	ret = (ssize_t)strlcpy(out, val->value_value, len);
44837c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
44847c478bd9Sstevel@tonic-gate 	return (ret);
44857c478bd9Sstevel@tonic-gate }
44867c478bd9Sstevel@tonic-gate 
44877c478bd9Sstevel@tonic-gate ssize_t
44887c478bd9Sstevel@tonic-gate scf_value_get_opaque(const scf_value_t *v, void *out, size_t len)
44897c478bd9Sstevel@tonic-gate {
44907c478bd9Sstevel@tonic-gate 	ssize_t ret;
44917c478bd9Sstevel@tonic-gate 	scf_handle_t *h = v->value_handle;
44927c478bd9Sstevel@tonic-gate 
44937c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
44947c478bd9Sstevel@tonic-gate 	if (!scf_value_check_type(v, REP_PROTOCOL_TYPE_OPAQUE)) {
44957c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
44967c478bd9Sstevel@tonic-gate 		return ((ssize_t)-1);
44977c478bd9Sstevel@tonic-gate 	}
44987c478bd9Sstevel@tonic-gate 	if (len > v->value_size)
44997c478bd9Sstevel@tonic-gate 		len = v->value_size;
45007c478bd9Sstevel@tonic-gate 	ret = len;
45017c478bd9Sstevel@tonic-gate 
45027c478bd9Sstevel@tonic-gate 	(void) memcpy(out, v->value_value, len);
45037c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
45047c478bd9Sstevel@tonic-gate 	return (ret);
45057c478bd9Sstevel@tonic-gate }
45067c478bd9Sstevel@tonic-gate 
45077c478bd9Sstevel@tonic-gate void
45087c478bd9Sstevel@tonic-gate scf_value_set_boolean(scf_value_t *v, uint8_t new)
45097c478bd9Sstevel@tonic-gate {
45107c478bd9Sstevel@tonic-gate 	scf_handle_t *h = v->value_handle;
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
45137c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(v, 0);
45147c478bd9Sstevel@tonic-gate 	v->value_type = REP_PROTOCOL_TYPE_BOOLEAN;
45157c478bd9Sstevel@tonic-gate 	(void) sprintf(v->value_value, "%d", (new != 0));
45167c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
45177c478bd9Sstevel@tonic-gate }
45187c478bd9Sstevel@tonic-gate 
45197c478bd9Sstevel@tonic-gate void
45207c478bd9Sstevel@tonic-gate scf_value_set_count(scf_value_t *v, uint64_t new)
45217c478bd9Sstevel@tonic-gate {
45227c478bd9Sstevel@tonic-gate 	scf_handle_t *h = v->value_handle;
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
45257c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(v, 0);
45267c478bd9Sstevel@tonic-gate 	v->value_type = REP_PROTOCOL_TYPE_COUNT;
45277c478bd9Sstevel@tonic-gate 	(void) sprintf(v->value_value, "%llu", (unsigned long long)new);
45287c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
45297c478bd9Sstevel@tonic-gate }
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate void
45327c478bd9Sstevel@tonic-gate scf_value_set_integer(scf_value_t *v, int64_t new)
45337c478bd9Sstevel@tonic-gate {
45347c478bd9Sstevel@tonic-gate 	scf_handle_t *h = v->value_handle;
45357c478bd9Sstevel@tonic-gate 
45367c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
45377c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(v, 0);
45387c478bd9Sstevel@tonic-gate 	v->value_type = REP_PROTOCOL_TYPE_INTEGER;
45397c478bd9Sstevel@tonic-gate 	(void) sprintf(v->value_value, "%lld", (long long)new);
45407c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
45417c478bd9Sstevel@tonic-gate }
45427c478bd9Sstevel@tonic-gate 
45437c478bd9Sstevel@tonic-gate int
45447c478bd9Sstevel@tonic-gate scf_value_set_time(scf_value_t *v, int64_t new_sec, int32_t new_nsec)
45457c478bd9Sstevel@tonic-gate {
45467c478bd9Sstevel@tonic-gate 	scf_handle_t *h = v->value_handle;
45477c478bd9Sstevel@tonic-gate 
45487c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
45497c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(v, 0);
45507c478bd9Sstevel@tonic-gate 	if (new_nsec < 0 || new_nsec >= NANOSEC) {
45517c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
45527c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
45537c478bd9Sstevel@tonic-gate 	}
45547c478bd9Sstevel@tonic-gate 	v->value_type = REP_PROTOCOL_TYPE_TIME;
45557c478bd9Sstevel@tonic-gate 	if (new_nsec == 0)
45567c478bd9Sstevel@tonic-gate 		(void) sprintf(v->value_value, "%lld", (long long)new_sec);
45577c478bd9Sstevel@tonic-gate 	else
45587c478bd9Sstevel@tonic-gate 		(void) sprintf(v->value_value, "%lld.%09u", (long long)new_sec,
45597c478bd9Sstevel@tonic-gate 		    (unsigned)new_nsec);
45607c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
45617c478bd9Sstevel@tonic-gate 	return (0);
45627c478bd9Sstevel@tonic-gate }
45637c478bd9Sstevel@tonic-gate 
45647c478bd9Sstevel@tonic-gate int
45657c478bd9Sstevel@tonic-gate scf_value_set_astring(scf_value_t *v, const char *new)
45667c478bd9Sstevel@tonic-gate {
45677c478bd9Sstevel@tonic-gate 	scf_handle_t *h = v->value_handle;
45687c478bd9Sstevel@tonic-gate 
45697c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
45707c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(v, 0);
45717c478bd9Sstevel@tonic-gate 	if (!scf_validate_encoded_value(REP_PROTOCOL_TYPE_STRING, new)) {
45727c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
45737c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
45747c478bd9Sstevel@tonic-gate 	}
45757c478bd9Sstevel@tonic-gate 	if (strlcpy(v->value_value, new, sizeof (v->value_value)) >=
45767c478bd9Sstevel@tonic-gate 	    sizeof (v->value_value)) {
45777c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
45787c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
45797c478bd9Sstevel@tonic-gate 	}
45807c478bd9Sstevel@tonic-gate 	v->value_type = REP_PROTOCOL_TYPE_STRING;
45817c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
45827c478bd9Sstevel@tonic-gate 	return (0);
45837c478bd9Sstevel@tonic-gate }
45847c478bd9Sstevel@tonic-gate 
45857c478bd9Sstevel@tonic-gate int
45867c478bd9Sstevel@tonic-gate scf_value_set_ustring(scf_value_t *v, const char *new)
45877c478bd9Sstevel@tonic-gate {
45887c478bd9Sstevel@tonic-gate 	scf_handle_t *h = v->value_handle;
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
45917c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(v, 0);
45927c478bd9Sstevel@tonic-gate 	if (!scf_validate_encoded_value(REP_PROTOCOL_SUBTYPE_USTRING, new)) {
45937c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
45947c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
45957c478bd9Sstevel@tonic-gate 	}
45967c478bd9Sstevel@tonic-gate 	if (strlcpy(v->value_value, new, sizeof (v->value_value)) >=
45977c478bd9Sstevel@tonic-gate 	    sizeof (v->value_value)) {
45987c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
45997c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
46007c478bd9Sstevel@tonic-gate 	}
46017c478bd9Sstevel@tonic-gate 	v->value_type = REP_PROTOCOL_SUBTYPE_USTRING;
46027c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
46037c478bd9Sstevel@tonic-gate 	return (0);
46047c478bd9Sstevel@tonic-gate }
46057c478bd9Sstevel@tonic-gate 
46067c478bd9Sstevel@tonic-gate int
46077c478bd9Sstevel@tonic-gate scf_value_set_opaque(scf_value_t *v, const void *new, size_t len)
46087c478bd9Sstevel@tonic-gate {
46097c478bd9Sstevel@tonic-gate 	scf_handle_t *h = v->value_handle;
46107c478bd9Sstevel@tonic-gate 
46117c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
46127c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(v, 0);
46137c478bd9Sstevel@tonic-gate 	if (len > sizeof (v->value_value)) {
46147c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
46157c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
46167c478bd9Sstevel@tonic-gate 	}
46177c478bd9Sstevel@tonic-gate 	(void) memcpy(v->value_value, new, len);
46187c478bd9Sstevel@tonic-gate 	v->value_size = len;
46197c478bd9Sstevel@tonic-gate 	v->value_type = REP_PROTOCOL_TYPE_OPAQUE;
46207c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
46217c478bd9Sstevel@tonic-gate 	return (0);
46227c478bd9Sstevel@tonic-gate }
46237c478bd9Sstevel@tonic-gate 
46247c478bd9Sstevel@tonic-gate /*
46257c478bd9Sstevel@tonic-gate  * Fails with
46267c478bd9Sstevel@tonic-gate  *   _NOT_SET - v_arg is reset
46277c478bd9Sstevel@tonic-gate  *   _INTERNAL - v_arg is corrupt
46287c478bd9Sstevel@tonic-gate  *
46297c478bd9Sstevel@tonic-gate  * If t is not _TYPE_INVALID, fails with
46307c478bd9Sstevel@tonic-gate  *   _TYPE_MISMATCH - v_arg's type is not compatible with t
46317c478bd9Sstevel@tonic-gate  */
46327c478bd9Sstevel@tonic-gate static ssize_t
46337c478bd9Sstevel@tonic-gate scf_value_get_as_string_common(const scf_value_t *v_arg,
46347c478bd9Sstevel@tonic-gate     rep_protocol_value_type_t t, char *buf, size_t bufsz)
46357c478bd9Sstevel@tonic-gate {
46367c478bd9Sstevel@tonic-gate 	scf_handle_t *h = v_arg->value_handle;
46377c478bd9Sstevel@tonic-gate 	scf_value_t v_s;
46387c478bd9Sstevel@tonic-gate 	scf_value_t *v = &v_s;
46397c478bd9Sstevel@tonic-gate 	ssize_t r;
46407c478bd9Sstevel@tonic-gate 	uint8_t b;
46417c478bd9Sstevel@tonic-gate 
46427c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
46437c478bd9Sstevel@tonic-gate 	if (t != REP_PROTOCOL_TYPE_INVALID && !scf_value_check_type(v_arg, t)) {
46447c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
46457c478bd9Sstevel@tonic-gate 		return (-1);
46467c478bd9Sstevel@tonic-gate 	}
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate 	v_s = *v_arg;			/* copy locally so we can unlock */
46497c478bd9Sstevel@tonic-gate 	h->rh_values++;			/* keep the handle from going away */
46507c478bd9Sstevel@tonic-gate 	h->rh_extrefs++;
46517c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
46527c478bd9Sstevel@tonic-gate 
46537c478bd9Sstevel@tonic-gate 
46547c478bd9Sstevel@tonic-gate 	switch (REP_PROTOCOL_BASE_TYPE(v->value_type)) {
46557c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TYPE_BOOLEAN:
46567c478bd9Sstevel@tonic-gate 		r = scf_value_get_boolean(v, &b);
46577c478bd9Sstevel@tonic-gate 		assert(r == SCF_SUCCESS);
46587c478bd9Sstevel@tonic-gate 
46597c478bd9Sstevel@tonic-gate 		r = strlcpy(buf, b ? "true" : "false", bufsz);
46607c478bd9Sstevel@tonic-gate 		break;
46617c478bd9Sstevel@tonic-gate 
46627c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TYPE_COUNT:
46637c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TYPE_INTEGER:
46647c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TYPE_TIME:
46657c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TYPE_STRING:
46667c478bd9Sstevel@tonic-gate 		r = strlcpy(buf, v->value_value, bufsz);
46677c478bd9Sstevel@tonic-gate 		break;
46687c478bd9Sstevel@tonic-gate 
46697c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TYPE_OPAQUE:
46707c478bd9Sstevel@tonic-gate 		/*
46717c478bd9Sstevel@tonic-gate 		 * Note that we only write out full hex bytes -- if they're
46727c478bd9Sstevel@tonic-gate 		 * short, and bufsz is even, we'll only fill (bufsz - 2) bytes
46737c478bd9Sstevel@tonic-gate 		 * with data.
46747c478bd9Sstevel@tonic-gate 		 */
46757c478bd9Sstevel@tonic-gate 		if (bufsz > 0)
46767c478bd9Sstevel@tonic-gate 			(void) scf_opaque_encode(buf, v->value_value,
46777c478bd9Sstevel@tonic-gate 			    MIN(v->value_size, (bufsz - 1)/2));
46787c478bd9Sstevel@tonic-gate 		r = (v->value_size * 2);
46797c478bd9Sstevel@tonic-gate 		break;
46807c478bd9Sstevel@tonic-gate 
46817c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TYPE_INVALID:
46827c478bd9Sstevel@tonic-gate 		r = scf_set_error(SCF_ERROR_NOT_SET);
46837c478bd9Sstevel@tonic-gate 		break;
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate 	default:
46867c478bd9Sstevel@tonic-gate 		r = (scf_set_error(SCF_ERROR_INTERNAL));
46877c478bd9Sstevel@tonic-gate 		break;
46887c478bd9Sstevel@tonic-gate 	}
46897c478bd9Sstevel@tonic-gate 
46907c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
46917c478bd9Sstevel@tonic-gate 	h->rh_values--;
46927c478bd9Sstevel@tonic-gate 	h->rh_extrefs--;
46937c478bd9Sstevel@tonic-gate 	handle_unrefed(h);
46947c478bd9Sstevel@tonic-gate 
46957c478bd9Sstevel@tonic-gate 	return (r);
46967c478bd9Sstevel@tonic-gate }
46977c478bd9Sstevel@tonic-gate 
46987c478bd9Sstevel@tonic-gate ssize_t
46997c478bd9Sstevel@tonic-gate scf_value_get_as_string(const scf_value_t *v, char *buf, size_t bufsz)
47007c478bd9Sstevel@tonic-gate {
47017c478bd9Sstevel@tonic-gate 	return (scf_value_get_as_string_common(v, REP_PROTOCOL_TYPE_INVALID,
47027c478bd9Sstevel@tonic-gate 	    buf, bufsz));
47037c478bd9Sstevel@tonic-gate }
47047c478bd9Sstevel@tonic-gate 
47057c478bd9Sstevel@tonic-gate ssize_t
47067c478bd9Sstevel@tonic-gate scf_value_get_as_string_typed(const scf_value_t *v, scf_type_t type,
47077c478bd9Sstevel@tonic-gate     char *buf, size_t bufsz)
47087c478bd9Sstevel@tonic-gate {
47097c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t ty = scf_type_to_protocol_type(type);
47107c478bd9Sstevel@tonic-gate 	if (ty == REP_PROTOCOL_TYPE_INVALID)
47117c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
47127c478bd9Sstevel@tonic-gate 
47137c478bd9Sstevel@tonic-gate 	return (scf_value_get_as_string_common(v, ty, buf, bufsz));
47147c478bd9Sstevel@tonic-gate }
47157c478bd9Sstevel@tonic-gate 
47167c478bd9Sstevel@tonic-gate int
47177c478bd9Sstevel@tonic-gate scf_value_set_from_string(scf_value_t *v, scf_type_t type, const char *str)
47187c478bd9Sstevel@tonic-gate {
47197c478bd9Sstevel@tonic-gate 	scf_handle_t *h = v->value_handle;
47207c478bd9Sstevel@tonic-gate 	rep_protocol_value_type_t ty;
47217c478bd9Sstevel@tonic-gate 
47227c478bd9Sstevel@tonic-gate 	switch (type) {
47237c478bd9Sstevel@tonic-gate 	case SCF_TYPE_BOOLEAN: {
47247c478bd9Sstevel@tonic-gate 		uint8_t b;
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate 		if (strcmp(str, "true") == 0 || strcmp(str, "t") == 0 ||
47277c478bd9Sstevel@tonic-gate 		    strcmp(str, "1") == 0)
47287c478bd9Sstevel@tonic-gate 			b = 1;
47297c478bd9Sstevel@tonic-gate 		else if (strcmp(str, "false") == 0 ||
47307c478bd9Sstevel@tonic-gate 		    strcmp(str, "f") == 0 || strcmp(str, "0") == 0)
47317c478bd9Sstevel@tonic-gate 			b = 0;
47327c478bd9Sstevel@tonic-gate 		else {
47337c478bd9Sstevel@tonic-gate 			goto bad;
47347c478bd9Sstevel@tonic-gate 		}
47357c478bd9Sstevel@tonic-gate 
47367c478bd9Sstevel@tonic-gate 		scf_value_set_boolean(v, b);
47377c478bd9Sstevel@tonic-gate 		return (0);
47387c478bd9Sstevel@tonic-gate 	}
47397c478bd9Sstevel@tonic-gate 
47407c478bd9Sstevel@tonic-gate 	case SCF_TYPE_COUNT: {
47417c478bd9Sstevel@tonic-gate 		uint64_t c;
47427c478bd9Sstevel@tonic-gate 		char *endp;
47437c478bd9Sstevel@tonic-gate 
47447c478bd9Sstevel@tonic-gate 		errno = 0;
4745f2fa366aSjeanm 		c = strtoull(str, &endp, 0);
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate 		if (errno != 0 || endp == str || *endp != '\0')
47487c478bd9Sstevel@tonic-gate 			goto bad;
47497c478bd9Sstevel@tonic-gate 
47507c478bd9Sstevel@tonic-gate 		scf_value_set_count(v, c);
47517c478bd9Sstevel@tonic-gate 		return (0);
47527c478bd9Sstevel@tonic-gate 	}
47537c478bd9Sstevel@tonic-gate 
47547c478bd9Sstevel@tonic-gate 	case SCF_TYPE_INTEGER: {
47557c478bd9Sstevel@tonic-gate 		int64_t i;
47567c478bd9Sstevel@tonic-gate 		char *endp;
47577c478bd9Sstevel@tonic-gate 
47587c478bd9Sstevel@tonic-gate 		errno = 0;
4759f2fa366aSjeanm 		i = strtoll(str, &endp, 0);
47607c478bd9Sstevel@tonic-gate 
47617c478bd9Sstevel@tonic-gate 		if (errno != 0 || endp == str || *endp != '\0')
47627c478bd9Sstevel@tonic-gate 			goto bad;
47637c478bd9Sstevel@tonic-gate 
47647c478bd9Sstevel@tonic-gate 		scf_value_set_integer(v, i);
47657c478bd9Sstevel@tonic-gate 		return (0);
47667c478bd9Sstevel@tonic-gate 	}
47677c478bd9Sstevel@tonic-gate 
47687c478bd9Sstevel@tonic-gate 	case SCF_TYPE_TIME: {
47697c478bd9Sstevel@tonic-gate 		int64_t s;
47707c478bd9Sstevel@tonic-gate 		uint32_t ns = 0;
47717c478bd9Sstevel@tonic-gate 		char *endp, *ns_str;
47727c478bd9Sstevel@tonic-gate 		size_t len;
47737c478bd9Sstevel@tonic-gate 
47747c478bd9Sstevel@tonic-gate 		errno = 0;
47757c478bd9Sstevel@tonic-gate 		s = strtoll(str, &endp, 10);
47767c478bd9Sstevel@tonic-gate 		if (errno != 0 || endp == str ||
47777c478bd9Sstevel@tonic-gate 		    (*endp != '\0' && *endp != '.'))
47787c478bd9Sstevel@tonic-gate 			goto bad;
47797c478bd9Sstevel@tonic-gate 
47807c478bd9Sstevel@tonic-gate 		if (*endp == '.') {
47817c478bd9Sstevel@tonic-gate 			ns_str = endp + 1;
47827c478bd9Sstevel@tonic-gate 			len = strlen(ns_str);
47837c478bd9Sstevel@tonic-gate 			if (len == 0 || len > 9)
47847c478bd9Sstevel@tonic-gate 				goto bad;
47857c478bd9Sstevel@tonic-gate 
47867c478bd9Sstevel@tonic-gate 			ns = strtoul(ns_str, &endp, 10);
47877c478bd9Sstevel@tonic-gate 			if (errno != 0 || endp == ns_str || *endp != '\0')
47887c478bd9Sstevel@tonic-gate 				goto bad;
47897c478bd9Sstevel@tonic-gate 
47907c478bd9Sstevel@tonic-gate 			while (len++ < 9)
47917c478bd9Sstevel@tonic-gate 				ns *= 10;
47927c478bd9Sstevel@tonic-gate 			assert(ns < NANOSEC);
47937c478bd9Sstevel@tonic-gate 		}
47947c478bd9Sstevel@tonic-gate 
47957c478bd9Sstevel@tonic-gate 		return (scf_value_set_time(v, s, ns));
47967c478bd9Sstevel@tonic-gate 	}
47977c478bd9Sstevel@tonic-gate 
47987c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
47997c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
48007c478bd9Sstevel@tonic-gate 	case SCF_TYPE_OPAQUE:
48017c478bd9Sstevel@tonic-gate 	case SCF_TYPE_URI:
48027c478bd9Sstevel@tonic-gate 	case SCF_TYPE_FMRI:
48037c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
48047c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
48057c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
48067c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
48077c478bd9Sstevel@tonic-gate 		ty = scf_type_to_protocol_type(type);
48087c478bd9Sstevel@tonic-gate 
48097c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&h->rh_lock);
48107c478bd9Sstevel@tonic-gate 		scf_value_reset_locked(v, 0);
48117c478bd9Sstevel@tonic-gate 		if (type == SCF_TYPE_OPAQUE) {
48127c478bd9Sstevel@tonic-gate 			v->value_size = scf_opaque_decode(v->value_value,
48137c478bd9Sstevel@tonic-gate 			    str, sizeof (v->value_value));
48147c478bd9Sstevel@tonic-gate 			if (!scf_validate_encoded_value(ty, str)) {
48157c478bd9Sstevel@tonic-gate 				(void) pthread_mutex_lock(&h->rh_lock);
48167c478bd9Sstevel@tonic-gate 				goto bad;
48177c478bd9Sstevel@tonic-gate 			}
48187c478bd9Sstevel@tonic-gate 		} else {
48197c478bd9Sstevel@tonic-gate 			(void) strlcpy(v->value_value, str,
48207c478bd9Sstevel@tonic-gate 			    sizeof (v->value_value));
48217c478bd9Sstevel@tonic-gate 			if (!scf_validate_encoded_value(ty, v->value_value)) {
48227c478bd9Sstevel@tonic-gate 				(void) pthread_mutex_lock(&h->rh_lock);
48237c478bd9Sstevel@tonic-gate 				goto bad;
48247c478bd9Sstevel@tonic-gate 			}
48257c478bd9Sstevel@tonic-gate 		}
48267c478bd9Sstevel@tonic-gate 		v->value_type = ty;
48277c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
48287c478bd9Sstevel@tonic-gate 		return (SCF_SUCCESS);
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_TYPE_INVALID:
48317c478bd9Sstevel@tonic-gate 	default:
48327c478bd9Sstevel@tonic-gate 		scf_value_reset(v);
48337c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_TYPE_MISMATCH));
48347c478bd9Sstevel@tonic-gate 	}
48357c478bd9Sstevel@tonic-gate bad:
48367c478bd9Sstevel@tonic-gate 	scf_value_reset(v);
48377c478bd9Sstevel@tonic-gate 	return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
48387c478bd9Sstevel@tonic-gate }
48397c478bd9Sstevel@tonic-gate 
48407c478bd9Sstevel@tonic-gate int
48417c478bd9Sstevel@tonic-gate scf_iter_property_values(scf_iter_t *iter, const scf_property_t *prop)
48427c478bd9Sstevel@tonic-gate {
48437c478bd9Sstevel@tonic-gate 	return (datael_setup_iter(iter, &prop->rd_d,
48447c478bd9Sstevel@tonic-gate 	    REP_PROTOCOL_ENTITY_VALUE, 0));
48457c478bd9Sstevel@tonic-gate }
48467c478bd9Sstevel@tonic-gate 
48477c478bd9Sstevel@tonic-gate int
48487c478bd9Sstevel@tonic-gate scf_iter_next_value(scf_iter_t *iter, scf_value_t *v)
48497c478bd9Sstevel@tonic-gate {
48507c478bd9Sstevel@tonic-gate 	scf_handle_t *h = iter->iter_handle;
48517c478bd9Sstevel@tonic-gate 
48527c478bd9Sstevel@tonic-gate 	struct rep_protocol_iter_read_value request;
48537c478bd9Sstevel@tonic-gate 	struct rep_protocol_value_response response;
48547c478bd9Sstevel@tonic-gate 
48557c478bd9Sstevel@tonic-gate 	int r;
48567c478bd9Sstevel@tonic-gate 
48577c478bd9Sstevel@tonic-gate 	if (h != v->value_handle)
48587c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
48617c478bd9Sstevel@tonic-gate 
48627c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(v, 0);
48637c478bd9Sstevel@tonic-gate 
48647c478bd9Sstevel@tonic-gate 	if (iter->iter_type == REP_PROTOCOL_ENTITY_NONE) {
48657c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
48667c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
48677c478bd9Sstevel@tonic-gate 	}
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate 	if (iter->iter_type != REP_PROTOCOL_ENTITY_VALUE) {
48707c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
48717c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
48727c478bd9Sstevel@tonic-gate 	}
48737c478bd9Sstevel@tonic-gate 
48747c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ITER_READ_VALUE;
48757c478bd9Sstevel@tonic-gate 	request.rpr_iterid = iter->iter_id;
48767c478bd9Sstevel@tonic-gate 	request.rpr_sequence = iter->iter_sequence;
48777c478bd9Sstevel@tonic-gate 
48787c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
48797c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
48807c478bd9Sstevel@tonic-gate 
48817c478bd9Sstevel@tonic-gate 	if (r < 0) {
48827c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
48837c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
48847c478bd9Sstevel@tonic-gate 	}
48857c478bd9Sstevel@tonic-gate 
48867c478bd9Sstevel@tonic-gate 	if (response.rpr_response == REP_PROTOCOL_DONE) {
48877c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
48887c478bd9Sstevel@tonic-gate 		return (0);
48897c478bd9Sstevel@tonic-gate 	}
48907c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS) {
48917c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
48927c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
48937c478bd9Sstevel@tonic-gate 	}
48947c478bd9Sstevel@tonic-gate 	iter->iter_sequence++;
48957c478bd9Sstevel@tonic-gate 
48967c478bd9Sstevel@tonic-gate 	v->value_type = response.rpr_type;
48977c478bd9Sstevel@tonic-gate 
48987c478bd9Sstevel@tonic-gate 	assert(scf_validate_encoded_value(response.rpr_type,
48997c478bd9Sstevel@tonic-gate 	    response.rpr_value));
49007c478bd9Sstevel@tonic-gate 
49017c478bd9Sstevel@tonic-gate 	if (v->value_type != REP_PROTOCOL_TYPE_OPAQUE) {
49027c478bd9Sstevel@tonic-gate 		(void) strlcpy(v->value_value, response.rpr_value,
49037c478bd9Sstevel@tonic-gate 		    sizeof (v->value_value));
49047c478bd9Sstevel@tonic-gate 	} else {
49057c478bd9Sstevel@tonic-gate 		v->value_size = scf_opaque_decode(v->value_value,
49067c478bd9Sstevel@tonic-gate 		    response.rpr_value, sizeof (v->value_value));
49077c478bd9Sstevel@tonic-gate 	}
49087c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
49097c478bd9Sstevel@tonic-gate 
49107c478bd9Sstevel@tonic-gate 	return (1);
49117c478bd9Sstevel@tonic-gate }
49127c478bd9Sstevel@tonic-gate 
49137c478bd9Sstevel@tonic-gate int
49147c478bd9Sstevel@tonic-gate scf_property_get_value(const scf_property_t *prop, scf_value_t *v)
49157c478bd9Sstevel@tonic-gate {
49167c478bd9Sstevel@tonic-gate 	scf_handle_t *h = prop->rd_d.rd_handle;
49177c478bd9Sstevel@tonic-gate 	struct rep_protocol_property_request request;
49187c478bd9Sstevel@tonic-gate 	struct rep_protocol_value_response response;
49197c478bd9Sstevel@tonic-gate 	int r;
49207c478bd9Sstevel@tonic-gate 
49217c478bd9Sstevel@tonic-gate 	if (h != v->value_handle)
49227c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
49237c478bd9Sstevel@tonic-gate 
49247c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
49257c478bd9Sstevel@tonic-gate 
49267c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_PROPERTY_GET_VALUE;
49277c478bd9Sstevel@tonic-gate 	request.rpr_entityid = prop->rd_d.rd_entity;
49287c478bd9Sstevel@tonic-gate 
49297c478bd9Sstevel@tonic-gate 	scf_value_reset_locked(v, 0);
49307c478bd9Sstevel@tonic-gate 	datael_finish_reset(&prop->rd_d);
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
49337c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
49347c478bd9Sstevel@tonic-gate 
49357c478bd9Sstevel@tonic-gate 	if (r < 0) {
49367c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
49377c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
49387c478bd9Sstevel@tonic-gate 	}
49397c478bd9Sstevel@tonic-gate 
49407c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS &&
49417c478bd9Sstevel@tonic-gate 	    response.rpr_response != REP_PROTOCOL_FAIL_TRUNCATED) {
49427c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&h->rh_lock);
49437c478bd9Sstevel@tonic-gate 		assert(response.rpr_response !=
49447c478bd9Sstevel@tonic-gate 		    REP_PROTOCOL_FAIL_TYPE_MISMATCH);
49457c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
49467c478bd9Sstevel@tonic-gate 	}
49477c478bd9Sstevel@tonic-gate 
49487c478bd9Sstevel@tonic-gate 	v->value_type = response.rpr_type;
49497c478bd9Sstevel@tonic-gate 	if (v->value_type != REP_PROTOCOL_TYPE_OPAQUE) {
49507c478bd9Sstevel@tonic-gate 		(void) strlcpy(v->value_value, response.rpr_value,
49517c478bd9Sstevel@tonic-gate 		    sizeof (v->value_value));
49527c478bd9Sstevel@tonic-gate 	} else {
49537c478bd9Sstevel@tonic-gate 		v->value_size = scf_opaque_decode(v->value_value,
49547c478bd9Sstevel@tonic-gate 		    response.rpr_value, sizeof (v->value_value));
49557c478bd9Sstevel@tonic-gate 	}
49567c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
49577c478bd9Sstevel@tonic-gate 	return ((response.rpr_response == REP_PROTOCOL_SUCCESS)?
49587c478bd9Sstevel@tonic-gate 	    SCF_SUCCESS : scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
49597c478bd9Sstevel@tonic-gate }
49607c478bd9Sstevel@tonic-gate 
49617c478bd9Sstevel@tonic-gate int
49627c478bd9Sstevel@tonic-gate scf_pg_get_parent_service(const scf_propertygroup_t *pg, scf_service_t *svc)
49637c478bd9Sstevel@tonic-gate {
49647c478bd9Sstevel@tonic-gate 	return (datael_get_parent(&pg->rd_d, &svc->rd_d));
49657c478bd9Sstevel@tonic-gate }
49667c478bd9Sstevel@tonic-gate 
49677c478bd9Sstevel@tonic-gate int
49687c478bd9Sstevel@tonic-gate scf_pg_get_parent_instance(const scf_propertygroup_t *pg, scf_instance_t *inst)
49697c478bd9Sstevel@tonic-gate {
49707c478bd9Sstevel@tonic-gate 	return (datael_get_parent(&pg->rd_d, &inst->rd_d));
49717c478bd9Sstevel@tonic-gate }
49727c478bd9Sstevel@tonic-gate 
49737c478bd9Sstevel@tonic-gate int
49747c478bd9Sstevel@tonic-gate scf_pg_get_parent_snaplevel(const scf_propertygroup_t *pg,
49757c478bd9Sstevel@tonic-gate     scf_snaplevel_t *level)
49767c478bd9Sstevel@tonic-gate {
49777c478bd9Sstevel@tonic-gate 	return (datael_get_parent(&pg->rd_d, &level->rd_d));
49787c478bd9Sstevel@tonic-gate }
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate int
49817c478bd9Sstevel@tonic-gate scf_service_get_parent(const scf_service_t *svc, scf_scope_t *s)
49827c478bd9Sstevel@tonic-gate {
49837c478bd9Sstevel@tonic-gate 	return (datael_get_parent(&svc->rd_d, &s->rd_d));
49847c478bd9Sstevel@tonic-gate }
49857c478bd9Sstevel@tonic-gate 
49867c478bd9Sstevel@tonic-gate int
49877c478bd9Sstevel@tonic-gate scf_instance_get_parent(const scf_instance_t *inst, scf_service_t *svc)
49887c478bd9Sstevel@tonic-gate {
49897c478bd9Sstevel@tonic-gate 	return (datael_get_parent(&inst->rd_d, &svc->rd_d));
49907c478bd9Sstevel@tonic-gate }
49917c478bd9Sstevel@tonic-gate 
49927c478bd9Sstevel@tonic-gate int
49937c478bd9Sstevel@tonic-gate scf_snapshot_get_parent(const scf_snapshot_t *inst, scf_instance_t *svc)
49947c478bd9Sstevel@tonic-gate {
49957c478bd9Sstevel@tonic-gate 	return (datael_get_parent(&inst->rd_d, &svc->rd_d));
49967c478bd9Sstevel@tonic-gate }
49977c478bd9Sstevel@tonic-gate 
49987c478bd9Sstevel@tonic-gate int
49997c478bd9Sstevel@tonic-gate scf_snaplevel_get_parent(const scf_snaplevel_t *inst, scf_snapshot_t *svc)
50007c478bd9Sstevel@tonic-gate {
50017c478bd9Sstevel@tonic-gate 	return (datael_get_parent(&inst->rd_d, &svc->rd_d));
50027c478bd9Sstevel@tonic-gate }
50037c478bd9Sstevel@tonic-gate 
50047c478bd9Sstevel@tonic-gate /*
50057c478bd9Sstevel@tonic-gate  * FMRI functions
50067c478bd9Sstevel@tonic-gate  *
50077c478bd9Sstevel@tonic-gate  * Note: In the scf_parse_svc_fmri(), scf_parse_file_fmri() and
50087c478bd9Sstevel@tonic-gate  * scf_parse_fmri(), fmri isn't const because that would require
50097c478bd9Sstevel@tonic-gate  * allocating memory. Also, note that scope, at least, is not necessarily
50107c478bd9Sstevel@tonic-gate  * in the passed in fmri.
50117c478bd9Sstevel@tonic-gate  */
50127c478bd9Sstevel@tonic-gate 
50137c478bd9Sstevel@tonic-gate int
50147c478bd9Sstevel@tonic-gate scf_parse_svc_fmri(char *fmri, const char **scope, const char **service,
50157c478bd9Sstevel@tonic-gate     const char **instance, const char **propertygroup, const char **property)
50167c478bd9Sstevel@tonic-gate {
50177c478bd9Sstevel@tonic-gate 	char *s, *e, *te, *tpg;
50187c478bd9Sstevel@tonic-gate 	char *my_s = NULL, *my_i = NULL, *my_pg = NULL, *my_p = NULL;
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 	if (scope != NULL)
50217c478bd9Sstevel@tonic-gate 		*scope = NULL;
50227c478bd9Sstevel@tonic-gate 	if (service != NULL)
50237c478bd9Sstevel@tonic-gate 		*service = NULL;
50247c478bd9Sstevel@tonic-gate 	if (instance != NULL)
50257c478bd9Sstevel@tonic-gate 		*instance = NULL;
50267c478bd9Sstevel@tonic-gate 	if (propertygroup != NULL)
50277c478bd9Sstevel@tonic-gate 		*propertygroup = NULL;
50287c478bd9Sstevel@tonic-gate 	if (property != NULL)
50297c478bd9Sstevel@tonic-gate 		*property = NULL;
50307c478bd9Sstevel@tonic-gate 
50317c478bd9Sstevel@tonic-gate 	s = fmri;
50327c478bd9Sstevel@tonic-gate 	e = strchr(s, '\0');
50337c478bd9Sstevel@tonic-gate 
50347c478bd9Sstevel@tonic-gate 	if (strncmp(s, SCF_FMRI_SVC_PREFIX,
50357c478bd9Sstevel@tonic-gate 	    sizeof (SCF_FMRI_SVC_PREFIX) - 1) == 0)
50367c478bd9Sstevel@tonic-gate 		s += sizeof (SCF_FMRI_SVC_PREFIX) - 1;
50377c478bd9Sstevel@tonic-gate 
50387c478bd9Sstevel@tonic-gate 	if (strncmp(s, SCF_FMRI_SCOPE_PREFIX,
50397c478bd9Sstevel@tonic-gate 	    sizeof (SCF_FMRI_SCOPE_PREFIX) - 1) == 0) {
50407c478bd9Sstevel@tonic-gate 		char *my_scope;
50417c478bd9Sstevel@tonic-gate 
50427c478bd9Sstevel@tonic-gate 		s += sizeof (SCF_FMRI_SCOPE_PREFIX) - 1;
50437c478bd9Sstevel@tonic-gate 		te = strstr(s, SCF_FMRI_SERVICE_PREFIX);
50447c478bd9Sstevel@tonic-gate 		if (te == NULL)
50457c478bd9Sstevel@tonic-gate 			te = e;
50467c478bd9Sstevel@tonic-gate 
50477c478bd9Sstevel@tonic-gate 		*te = 0;
50487c478bd9Sstevel@tonic-gate 		my_scope = s;
50497c478bd9Sstevel@tonic-gate 
50507c478bd9Sstevel@tonic-gate 		s = te;
50517c478bd9Sstevel@tonic-gate 		if (s < e)
50527c478bd9Sstevel@tonic-gate 			s += sizeof (SCF_FMRI_SERVICE_PREFIX) - 1;
50537c478bd9Sstevel@tonic-gate 
50547c478bd9Sstevel@tonic-gate 		/* If the scope ends with the suffix, remove it. */
50557c478bd9Sstevel@tonic-gate 		te = strstr(my_scope, SCF_FMRI_SCOPE_SUFFIX);
50567c478bd9Sstevel@tonic-gate 		if (te != NULL && te[sizeof (SCF_FMRI_SCOPE_SUFFIX) - 1] == 0)
50577c478bd9Sstevel@tonic-gate 			*te = 0;
50587c478bd9Sstevel@tonic-gate 
50597c478bd9Sstevel@tonic-gate 		/* Validate the scope. */
50607c478bd9Sstevel@tonic-gate 		if (my_scope[0] == '\0')
50617c478bd9Sstevel@tonic-gate 			my_scope = SCF_FMRI_LOCAL_SCOPE;
50627c478bd9Sstevel@tonic-gate 		else if (uu_check_name(my_scope, 0) == -1) {
50637c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
50647c478bd9Sstevel@tonic-gate 		}
50657c478bd9Sstevel@tonic-gate 
50667c478bd9Sstevel@tonic-gate 		if (scope != NULL)
50677c478bd9Sstevel@tonic-gate 			*scope = my_scope;
50687c478bd9Sstevel@tonic-gate 	} else {
50697c478bd9Sstevel@tonic-gate 		if (scope != NULL)
50707c478bd9Sstevel@tonic-gate 			*scope = SCF_FMRI_LOCAL_SCOPE;
50717c478bd9Sstevel@tonic-gate 	}
50727c478bd9Sstevel@tonic-gate 
50737c478bd9Sstevel@tonic-gate 	if (s[0] != 0) {
50747c478bd9Sstevel@tonic-gate 		if (strncmp(s, SCF_FMRI_SERVICE_PREFIX,
50757c478bd9Sstevel@tonic-gate 		    sizeof (SCF_FMRI_SERVICE_PREFIX) - 1) == 0)
50767c478bd9Sstevel@tonic-gate 			s += sizeof (SCF_FMRI_SERVICE_PREFIX) - 1;
50777c478bd9Sstevel@tonic-gate 
50787c478bd9Sstevel@tonic-gate 		/*
50797c478bd9Sstevel@tonic-gate 		 * Can't validate service here because it might not be null
50807c478bd9Sstevel@tonic-gate 		 * terminated.
50817c478bd9Sstevel@tonic-gate 		 */
50827c478bd9Sstevel@tonic-gate 		my_s = s;
50837c478bd9Sstevel@tonic-gate 	}
50847c478bd9Sstevel@tonic-gate 
50857c478bd9Sstevel@tonic-gate 	tpg = strstr(s, SCF_FMRI_PROPERTYGRP_PREFIX);
50867c478bd9Sstevel@tonic-gate 	te = strstr(s, SCF_FMRI_INSTANCE_PREFIX);
50877c478bd9Sstevel@tonic-gate 	if (te != NULL && (tpg == NULL || te < tpg)) {
50887c478bd9Sstevel@tonic-gate 		*te = 0;
50897c478bd9Sstevel@tonic-gate 		te += sizeof (SCF_FMRI_INSTANCE_PREFIX) - 1;
50907c478bd9Sstevel@tonic-gate 
50917c478bd9Sstevel@tonic-gate 		/* Can't validate instance here either. */
50927c478bd9Sstevel@tonic-gate 		my_i = s = te;
50937c478bd9Sstevel@tonic-gate 
50947c478bd9Sstevel@tonic-gate 		te = strstr(s, SCF_FMRI_PROPERTYGRP_PREFIX);
50957c478bd9Sstevel@tonic-gate 	} else {
50967c478bd9Sstevel@tonic-gate 		te = tpg;
50977c478bd9Sstevel@tonic-gate 	}
50987c478bd9Sstevel@tonic-gate 
50997c478bd9Sstevel@tonic-gate 	if (te != NULL) {
51007c478bd9Sstevel@tonic-gate 		*te = 0;
51017c478bd9Sstevel@tonic-gate 		te += sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1;
51027c478bd9Sstevel@tonic-gate 
51037c478bd9Sstevel@tonic-gate 		my_pg = s = te;
51047c478bd9Sstevel@tonic-gate 		te = strstr(s, SCF_FMRI_PROPERTY_PREFIX);
51057c478bd9Sstevel@tonic-gate 		if (te != NULL) {
51067c478bd9Sstevel@tonic-gate 			*te = 0;
51077c478bd9Sstevel@tonic-gate 			te += sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1;
51087c478bd9Sstevel@tonic-gate 
51097c478bd9Sstevel@tonic-gate 			my_p = te;
51107c478bd9Sstevel@tonic-gate 			s = te;
51117c478bd9Sstevel@tonic-gate 		}
51127c478bd9Sstevel@tonic-gate 	}
51137c478bd9Sstevel@tonic-gate 
51147c478bd9Sstevel@tonic-gate 	if (my_s != NULL) {
51157c478bd9Sstevel@tonic-gate 		if (uu_check_name(my_s, UU_NAME_DOMAIN | UU_NAME_PATH) == -1)
51167c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
51177c478bd9Sstevel@tonic-gate 
51187c478bd9Sstevel@tonic-gate 		if (service != NULL)
51197c478bd9Sstevel@tonic-gate 			*service = my_s;
51207c478bd9Sstevel@tonic-gate 	}
51217c478bd9Sstevel@tonic-gate 
51227c478bd9Sstevel@tonic-gate 	if (my_i != NULL) {
51237c478bd9Sstevel@tonic-gate 		if (uu_check_name(my_i, UU_NAME_DOMAIN) == -1)
51247c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
51257c478bd9Sstevel@tonic-gate 
51267c478bd9Sstevel@tonic-gate 		if (instance != NULL)
51277c478bd9Sstevel@tonic-gate 			*instance = my_i;
51287c478bd9Sstevel@tonic-gate 	}
51297c478bd9Sstevel@tonic-gate 
51307c478bd9Sstevel@tonic-gate 	if (my_pg != NULL) {
51317c478bd9Sstevel@tonic-gate 		if (uu_check_name(my_pg, UU_NAME_DOMAIN) == -1)
51327c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
51337c478bd9Sstevel@tonic-gate 
51347c478bd9Sstevel@tonic-gate 		if (propertygroup != NULL)
51357c478bd9Sstevel@tonic-gate 			*propertygroup = my_pg;
51367c478bd9Sstevel@tonic-gate 	}
51377c478bd9Sstevel@tonic-gate 
51387c478bd9Sstevel@tonic-gate 	if (my_p != NULL) {
51397c478bd9Sstevel@tonic-gate 		if (uu_check_name(my_p, UU_NAME_DOMAIN) == -1)
51407c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
51417c478bd9Sstevel@tonic-gate 
51427c478bd9Sstevel@tonic-gate 		if (property != NULL)
51437c478bd9Sstevel@tonic-gate 			*property = my_p;
51447c478bd9Sstevel@tonic-gate 	}
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate 	return (0);
51477c478bd9Sstevel@tonic-gate }
51487c478bd9Sstevel@tonic-gate 
51497c478bd9Sstevel@tonic-gate int
51507c478bd9Sstevel@tonic-gate scf_parse_file_fmri(char *fmri, const char **scope, const char **path)
51517c478bd9Sstevel@tonic-gate {
51527c478bd9Sstevel@tonic-gate 	char *s, *e, *te;
51537c478bd9Sstevel@tonic-gate 
51547c478bd9Sstevel@tonic-gate 	if (scope != NULL)
51557c478bd9Sstevel@tonic-gate 		*scope = NULL;
51567c478bd9Sstevel@tonic-gate 
51577c478bd9Sstevel@tonic-gate 	s = fmri;
51587c478bd9Sstevel@tonic-gate 	e = strchr(s, '\0');
51597c478bd9Sstevel@tonic-gate 
51607c478bd9Sstevel@tonic-gate 	if (strncmp(s, SCF_FMRI_FILE_PREFIX,
51617c478bd9Sstevel@tonic-gate 	    sizeof (SCF_FMRI_FILE_PREFIX) - 1) == 0)
51627c478bd9Sstevel@tonic-gate 		s += sizeof (SCF_FMRI_FILE_PREFIX) - 1;
51637c478bd9Sstevel@tonic-gate 
51647c478bd9Sstevel@tonic-gate 	if (strncmp(s, SCF_FMRI_SCOPE_PREFIX,
51657c478bd9Sstevel@tonic-gate 	    sizeof (SCF_FMRI_SCOPE_PREFIX) - 1) == 0) {
51667c478bd9Sstevel@tonic-gate 		char *my_scope;
51677c478bd9Sstevel@tonic-gate 
51687c478bd9Sstevel@tonic-gate 		s += sizeof (SCF_FMRI_SCOPE_PREFIX) - 1;
51697c478bd9Sstevel@tonic-gate 		te = strstr(s, SCF_FMRI_SERVICE_PREFIX);
51707c478bd9Sstevel@tonic-gate 		if (te == NULL)
51717c478bd9Sstevel@tonic-gate 			te = e;
51727c478bd9Sstevel@tonic-gate 
51737c478bd9Sstevel@tonic-gate 		*te = 0;
51747c478bd9Sstevel@tonic-gate 		my_scope = s;
51757c478bd9Sstevel@tonic-gate 
51767c478bd9Sstevel@tonic-gate 		s = te;
51777c478bd9Sstevel@tonic-gate 
51787c478bd9Sstevel@tonic-gate 		/* Validate the scope. */
51797c478bd9Sstevel@tonic-gate 		if (my_scope[0] != '\0' &&
51807c478bd9Sstevel@tonic-gate 		    strcmp(my_scope, SCF_FMRI_LOCAL_SCOPE) != 0) {
51817c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
51827c478bd9Sstevel@tonic-gate 		}
51837c478bd9Sstevel@tonic-gate 
51847c478bd9Sstevel@tonic-gate 		if (scope != NULL)
51857c478bd9Sstevel@tonic-gate 			*scope = my_scope;
51867c478bd9Sstevel@tonic-gate 	} else {
51877c478bd9Sstevel@tonic-gate 		/*
51887c478bd9Sstevel@tonic-gate 		 * FMRI paths must be absolute
51897c478bd9Sstevel@tonic-gate 		 */
51907c478bd9Sstevel@tonic-gate 		if (s[0] != '/')
51917c478bd9Sstevel@tonic-gate 			return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
51927c478bd9Sstevel@tonic-gate 	}
51937c478bd9Sstevel@tonic-gate 
51947c478bd9Sstevel@tonic-gate 	s += sizeof (SCF_FMRI_SERVICE_PREFIX) - 1;
51957c478bd9Sstevel@tonic-gate 
51967c478bd9Sstevel@tonic-gate 	if (s >= e)
51977c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate 	/*
52007c478bd9Sstevel@tonic-gate 	 * If the user requests it, return the full path of the file.
52017c478bd9Sstevel@tonic-gate 	 */
52027c478bd9Sstevel@tonic-gate 	if (path != NULL) {
52037c478bd9Sstevel@tonic-gate 		assert(s > fmri);
52047c478bd9Sstevel@tonic-gate 		s[-1] = '/';
52057c478bd9Sstevel@tonic-gate 		*path = s - 1;
52067c478bd9Sstevel@tonic-gate 	}
52077c478bd9Sstevel@tonic-gate 
52087c478bd9Sstevel@tonic-gate 	return (0);
52097c478bd9Sstevel@tonic-gate }
52107c478bd9Sstevel@tonic-gate 
52117c478bd9Sstevel@tonic-gate int
52127c478bd9Sstevel@tonic-gate scf_parse_fmri(char *fmri, int *type, const char **scope, const char **service,
52137c478bd9Sstevel@tonic-gate     const char **instance, const char **propertygroup, const char **property)
52147c478bd9Sstevel@tonic-gate {
52157c478bd9Sstevel@tonic-gate 	if (strncmp(fmri, SCF_FMRI_SVC_PREFIX,
52167c478bd9Sstevel@tonic-gate 	    sizeof (SCF_FMRI_SVC_PREFIX) - 1) == 0) {
52177c478bd9Sstevel@tonic-gate 		if (type)
52187c478bd9Sstevel@tonic-gate 			*type = SCF_FMRI_TYPE_SVC;
52197c478bd9Sstevel@tonic-gate 		return (scf_parse_svc_fmri(fmri, scope, service, instance,
522076cf44abSjeanm 		    propertygroup, property));
52217c478bd9Sstevel@tonic-gate 	} else if (strncmp(fmri, SCF_FMRI_FILE_PREFIX,
52227c478bd9Sstevel@tonic-gate 	    sizeof (SCF_FMRI_FILE_PREFIX) - 1) == 0) {
52237c478bd9Sstevel@tonic-gate 		if (type)
52247c478bd9Sstevel@tonic-gate 			*type = SCF_FMRI_TYPE_FILE;
52257c478bd9Sstevel@tonic-gate 		return (scf_parse_file_fmri(fmri, scope, NULL));
52267c478bd9Sstevel@tonic-gate 	} else {
52277c478bd9Sstevel@tonic-gate 		/*
52287c478bd9Sstevel@tonic-gate 		 * Parse as a svc if the fmri type is not explicitly
52297c478bd9Sstevel@tonic-gate 		 * specified.
52307c478bd9Sstevel@tonic-gate 		 */
52317c478bd9Sstevel@tonic-gate 		if (type)
52327c478bd9Sstevel@tonic-gate 			*type = SCF_FMRI_TYPE_SVC;
52337c478bd9Sstevel@tonic-gate 		return (scf_parse_svc_fmri(fmri, scope, service, instance,
52347c478bd9Sstevel@tonic-gate 		    propertygroup, property));
52357c478bd9Sstevel@tonic-gate 	}
52367c478bd9Sstevel@tonic-gate }
52377c478bd9Sstevel@tonic-gate 
52387c478bd9Sstevel@tonic-gate /*
52397c478bd9Sstevel@tonic-gate  * Fails with _INVALID_ARGUMENT.  fmri and buf may be equal.
52407c478bd9Sstevel@tonic-gate  */
52417c478bd9Sstevel@tonic-gate ssize_t
52427c478bd9Sstevel@tonic-gate scf_canonify_fmri(const char *fmri, char *buf, size_t bufsz)
52437c478bd9Sstevel@tonic-gate {
52447c478bd9Sstevel@tonic-gate 	const char *scope, *service, *instance, *pg, *property;
52457c478bd9Sstevel@tonic-gate 	char local[6 * REP_PROTOCOL_NAME_LEN];
52467c478bd9Sstevel@tonic-gate 	int r;
52477c478bd9Sstevel@tonic-gate 	size_t len;
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate 	if (strlcpy(local, fmri, sizeof (local)) >= sizeof (local)) {
52507c478bd9Sstevel@tonic-gate 		/* Should this be CONSTRAINT_VIOLATED? */
52517c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
52527c478bd9Sstevel@tonic-gate 		return (-1);
52537c478bd9Sstevel@tonic-gate 	}
52547c478bd9Sstevel@tonic-gate 
52557c478bd9Sstevel@tonic-gate 
52567c478bd9Sstevel@tonic-gate 	r = scf_parse_svc_fmri(local, &scope, &service, &instance, &pg,
52577c478bd9Sstevel@tonic-gate 	    &property);
52587c478bd9Sstevel@tonic-gate 	if (r != 0)
52597c478bd9Sstevel@tonic-gate 		return (-1);
52607c478bd9Sstevel@tonic-gate 
52617c478bd9Sstevel@tonic-gate 	len = strlcpy(buf, "svc:/", bufsz);
52627c478bd9Sstevel@tonic-gate 
52637c478bd9Sstevel@tonic-gate 	if (scope != NULL && strcmp(scope, SCF_SCOPE_LOCAL) != 0) {
52647c478bd9Sstevel@tonic-gate 		len += strlcat(buf, "/", bufsz);
52657c478bd9Sstevel@tonic-gate 		len += strlcat(buf, scope, bufsz);
52667c478bd9Sstevel@tonic-gate 	}
52677c478bd9Sstevel@tonic-gate 
52687c478bd9Sstevel@tonic-gate 	if (service)
52697c478bd9Sstevel@tonic-gate 		len += strlcat(buf, service, bufsz);
52707c478bd9Sstevel@tonic-gate 
52717c478bd9Sstevel@tonic-gate 	if (instance) {
52727c478bd9Sstevel@tonic-gate 		len += strlcat(buf, ":", bufsz);
52737c478bd9Sstevel@tonic-gate 		len += strlcat(buf, instance, bufsz);
52747c478bd9Sstevel@tonic-gate 	}
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate 	if (pg) {
52777c478bd9Sstevel@tonic-gate 		len += strlcat(buf, "/:properties/", bufsz);
52787c478bd9Sstevel@tonic-gate 		len += strlcat(buf, pg, bufsz);
52797c478bd9Sstevel@tonic-gate 	}
52807c478bd9Sstevel@tonic-gate 
52817c478bd9Sstevel@tonic-gate 	if (property) {
52827c478bd9Sstevel@tonic-gate 		len += strlcat(buf, "/", bufsz);
52837c478bd9Sstevel@tonic-gate 		len += strlcat(buf, property, bufsz);
52847c478bd9Sstevel@tonic-gate 	}
52857c478bd9Sstevel@tonic-gate 
52867c478bd9Sstevel@tonic-gate 	return (len);
52877c478bd9Sstevel@tonic-gate }
52887c478bd9Sstevel@tonic-gate 
528976cf44abSjeanm /*
529076cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _INVALID_ARGUMENT, _CONSTRAINT_VIOLATED,
529176cf44abSjeanm  * _NOT_FOUND, _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL, _NOT_SET, _DELETED,
529276cf44abSjeanm  * _NO_RESOURCES, _BACKEND_ACCESS.
529376cf44abSjeanm  */
52947c478bd9Sstevel@tonic-gate int
52957c478bd9Sstevel@tonic-gate scf_handle_decode_fmri(scf_handle_t *h, const char *fmri, scf_scope_t *sc,
52967c478bd9Sstevel@tonic-gate     scf_service_t *svc, scf_instance_t *inst, scf_propertygroup_t *pg,
52977c478bd9Sstevel@tonic-gate     scf_property_t *prop, int flags)
52987c478bd9Sstevel@tonic-gate {
52997c478bd9Sstevel@tonic-gate 	const char *scope, *service, *instance, *propertygroup, *property;
53007c478bd9Sstevel@tonic-gate 	int last;
53017c478bd9Sstevel@tonic-gate 	char local[6 * REP_PROTOCOL_NAME_LEN];
53027c478bd9Sstevel@tonic-gate 	int ret;
53037c478bd9Sstevel@tonic-gate 	const uint32_t holds = RH_HOLD_SCOPE | RH_HOLD_SERVICE |
53047c478bd9Sstevel@tonic-gate 	    RH_HOLD_INSTANCE | RH_HOLD_PG | RH_HOLD_PROPERTY;
53057c478bd9Sstevel@tonic-gate 
53067c478bd9Sstevel@tonic-gate 	/*
53077c478bd9Sstevel@tonic-gate 	 * verify that all handles match
53087c478bd9Sstevel@tonic-gate 	 */
53097c478bd9Sstevel@tonic-gate 	if ((sc != NULL && h != sc->rd_d.rd_handle) ||
53107c478bd9Sstevel@tonic-gate 	    (svc != NULL && h != svc->rd_d.rd_handle) ||
53117c478bd9Sstevel@tonic-gate 	    (inst != NULL && h != inst->rd_d.rd_handle) ||
53127c478bd9Sstevel@tonic-gate 	    (pg != NULL && h != pg->rd_d.rd_handle) ||
53137c478bd9Sstevel@tonic-gate 	    (prop != NULL && h != prop->rd_d.rd_handle))
53147c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
53157c478bd9Sstevel@tonic-gate 
53167c478bd9Sstevel@tonic-gate 	if (strlcpy(local, fmri, sizeof (local)) >= sizeof (local)) {
53177c478bd9Sstevel@tonic-gate 		ret = scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
53187c478bd9Sstevel@tonic-gate 		goto reset_args;
53197c478bd9Sstevel@tonic-gate 	}
53207c478bd9Sstevel@tonic-gate 
53217c478bd9Sstevel@tonic-gate 	/*
53227c478bd9Sstevel@tonic-gate 	 * We can simply return from an error in parsing, because
53237c478bd9Sstevel@tonic-gate 	 * scf_parse_fmri sets the error code correctly.
53247c478bd9Sstevel@tonic-gate 	 */
53257c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(local, &scope, &service, &instance,
53267c478bd9Sstevel@tonic-gate 	    &propertygroup, &property) == -1) {
53277c478bd9Sstevel@tonic-gate 		ret = -1;
53287c478bd9Sstevel@tonic-gate 		goto reset_args;
53297c478bd9Sstevel@tonic-gate 	}
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 	/*
53327c478bd9Sstevel@tonic-gate 	 * the FMRI looks valid at this point -- do constraint checks.
53337c478bd9Sstevel@tonic-gate 	 */
53347c478bd9Sstevel@tonic-gate 
53357c478bd9Sstevel@tonic-gate 	if (instance != NULL && (flags & SCF_DECODE_FMRI_REQUIRE_NO_INSTANCE)) {
53367c478bd9Sstevel@tonic-gate 		ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
53377c478bd9Sstevel@tonic-gate 		goto reset_args;
53387c478bd9Sstevel@tonic-gate 	}
53397c478bd9Sstevel@tonic-gate 	if (instance == NULL && (flags & SCF_DECODE_FMRI_REQUIRE_INSTANCE)) {
53407c478bd9Sstevel@tonic-gate 		ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
53417c478bd9Sstevel@tonic-gate 		goto reset_args;
53427c478bd9Sstevel@tonic-gate 	}
53437c478bd9Sstevel@tonic-gate 
53447c478bd9Sstevel@tonic-gate 	if (prop != NULL)
53457c478bd9Sstevel@tonic-gate 		last = REP_PROTOCOL_ENTITY_PROPERTY;
53467c478bd9Sstevel@tonic-gate 	else if (pg != NULL)
53477c478bd9Sstevel@tonic-gate 		last = REP_PROTOCOL_ENTITY_PROPERTYGRP;
53487c478bd9Sstevel@tonic-gate 	else if (inst != NULL)
53497c478bd9Sstevel@tonic-gate 		last = REP_PROTOCOL_ENTITY_INSTANCE;
53507c478bd9Sstevel@tonic-gate 	else if (svc != NULL)
53517c478bd9Sstevel@tonic-gate 		last = REP_PROTOCOL_ENTITY_SERVICE;
53527c478bd9Sstevel@tonic-gate 	else if (sc != NULL)
53537c478bd9Sstevel@tonic-gate 		last = REP_PROTOCOL_ENTITY_SCOPE;
53547c478bd9Sstevel@tonic-gate 	else
53557c478bd9Sstevel@tonic-gate 		last = REP_PROTOCOL_ENTITY_NONE;
53567c478bd9Sstevel@tonic-gate 
53577c478bd9Sstevel@tonic-gate 	if (flags & SCF_DECODE_FMRI_EXACT) {
53587c478bd9Sstevel@tonic-gate 		int last_fmri;
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate 		if (property != NULL)
53617c478bd9Sstevel@tonic-gate 			last_fmri = REP_PROTOCOL_ENTITY_PROPERTY;
53627c478bd9Sstevel@tonic-gate 		else if (propertygroup != NULL)
53637c478bd9Sstevel@tonic-gate 			last_fmri = REP_PROTOCOL_ENTITY_PROPERTYGRP;
53647c478bd9Sstevel@tonic-gate 		else if (instance != NULL)
53657c478bd9Sstevel@tonic-gate 			last_fmri = REP_PROTOCOL_ENTITY_INSTANCE;
53667c478bd9Sstevel@tonic-gate 		else if (service != NULL)
53677c478bd9Sstevel@tonic-gate 			last_fmri = REP_PROTOCOL_ENTITY_SERVICE;
53687c478bd9Sstevel@tonic-gate 		else if (scope != NULL)
53697c478bd9Sstevel@tonic-gate 			last_fmri = REP_PROTOCOL_ENTITY_SCOPE;
53707c478bd9Sstevel@tonic-gate 		else
53717c478bd9Sstevel@tonic-gate 			last_fmri = REP_PROTOCOL_ENTITY_NONE;
53727c478bd9Sstevel@tonic-gate 
53737c478bd9Sstevel@tonic-gate 		if (last != last_fmri) {
53747c478bd9Sstevel@tonic-gate 			ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
53757c478bd9Sstevel@tonic-gate 			goto reset_args;
53767c478bd9Sstevel@tonic-gate 		}
53777c478bd9Sstevel@tonic-gate 	}
53787c478bd9Sstevel@tonic-gate 
53797c478bd9Sstevel@tonic-gate 	if ((flags & SCF_DECODE_FMRI_TRUNCATE) &&
53807c478bd9Sstevel@tonic-gate 	    last == REP_PROTOCOL_ENTITY_NONE) {
53817c478bd9Sstevel@tonic-gate 		ret = 0;				/* nothing to do */
53827c478bd9Sstevel@tonic-gate 		goto reset_args;
53837c478bd9Sstevel@tonic-gate 	}
53847c478bd9Sstevel@tonic-gate 
53857c478bd9Sstevel@tonic-gate 	if (!(flags & SCF_DECODE_FMRI_TRUNCATE))
53867c478bd9Sstevel@tonic-gate 		last = REP_PROTOCOL_ENTITY_NONE;	/* never stop */
53877c478bd9Sstevel@tonic-gate 
53887c478bd9Sstevel@tonic-gate 	/*
53897c478bd9Sstevel@tonic-gate 	 * passed the constraint checks -- try to grab the thing itself.
53907c478bd9Sstevel@tonic-gate 	 */
53917c478bd9Sstevel@tonic-gate 
53927c478bd9Sstevel@tonic-gate 	handle_hold_subhandles(h, holds);
53937c478bd9Sstevel@tonic-gate 	if (sc == NULL)
53947c478bd9Sstevel@tonic-gate 		sc = h->rh_scope;
53957c478bd9Sstevel@tonic-gate 	else
53967c478bd9Sstevel@tonic-gate 		datael_reset(&sc->rd_d);
53977c478bd9Sstevel@tonic-gate 
53987c478bd9Sstevel@tonic-gate 	if (svc == NULL)
53997c478bd9Sstevel@tonic-gate 		svc = h->rh_service;
54007c478bd9Sstevel@tonic-gate 	else
54017c478bd9Sstevel@tonic-gate 		datael_reset(&svc->rd_d);
54027c478bd9Sstevel@tonic-gate 
54037c478bd9Sstevel@tonic-gate 	if (inst == NULL)
54047c478bd9Sstevel@tonic-gate 		inst = h->rh_instance;
54057c478bd9Sstevel@tonic-gate 	else
54067c478bd9Sstevel@tonic-gate 		datael_reset(&inst->rd_d);
54077c478bd9Sstevel@tonic-gate 
54087c478bd9Sstevel@tonic-gate 	if (pg == NULL)
54097c478bd9Sstevel@tonic-gate 		pg = h->rh_pg;
54107c478bd9Sstevel@tonic-gate 	else
54117c478bd9Sstevel@tonic-gate 		datael_reset(&pg->rd_d);
54127c478bd9Sstevel@tonic-gate 
54137c478bd9Sstevel@tonic-gate 	if (prop == NULL)
54147c478bd9Sstevel@tonic-gate 		prop = h->rh_property;
54157c478bd9Sstevel@tonic-gate 	else
54167c478bd9Sstevel@tonic-gate 		datael_reset(&prop->rd_d);
54177c478bd9Sstevel@tonic-gate 
54187c478bd9Sstevel@tonic-gate 	/*
54197c478bd9Sstevel@tonic-gate 	 * We only support local scopes, but we check *after* getting
54207c478bd9Sstevel@tonic-gate 	 * the local scope, so that any repository-related errors take
54217c478bd9Sstevel@tonic-gate 	 * precedence.
54227c478bd9Sstevel@tonic-gate 	 */
54237c478bd9Sstevel@tonic-gate 	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, sc) == -1) {
54247c478bd9Sstevel@tonic-gate 		handle_rele_subhandles(h, holds);
54257c478bd9Sstevel@tonic-gate 		ret = -1;
54267c478bd9Sstevel@tonic-gate 		goto reset_args;
54277c478bd9Sstevel@tonic-gate 	}
54287c478bd9Sstevel@tonic-gate 
54297c478bd9Sstevel@tonic-gate 	if (scope != NULL && strcmp(scope, SCF_FMRI_LOCAL_SCOPE) != 0) {
54307c478bd9Sstevel@tonic-gate 		handle_rele_subhandles(h, holds);
54317c478bd9Sstevel@tonic-gate 		ret = scf_set_error(SCF_ERROR_NOT_FOUND);
54327c478bd9Sstevel@tonic-gate 		goto reset_args;
54337c478bd9Sstevel@tonic-gate 	}
54347c478bd9Sstevel@tonic-gate 
54357c478bd9Sstevel@tonic-gate 
54367c478bd9Sstevel@tonic-gate 	if (service == NULL || last == REP_PROTOCOL_ENTITY_SCOPE) {
54377c478bd9Sstevel@tonic-gate 		handle_rele_subhandles(h, holds);
54387c478bd9Sstevel@tonic-gate 		return (0);
54397c478bd9Sstevel@tonic-gate 	}
54407c478bd9Sstevel@tonic-gate 
54417c478bd9Sstevel@tonic-gate 	if (scf_scope_get_service(sc, service, svc) == -1) {
54427c478bd9Sstevel@tonic-gate 		handle_rele_subhandles(h, holds);
54437c478bd9Sstevel@tonic-gate 		ret = -1;
54447c478bd9Sstevel@tonic-gate 		assert(scf_error() != SCF_ERROR_NOT_SET);
54457c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_DELETED)
54467c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NOT_FOUND);
54477c478bd9Sstevel@tonic-gate 		goto reset_args;
54487c478bd9Sstevel@tonic-gate 	}
54497c478bd9Sstevel@tonic-gate 
54507c478bd9Sstevel@tonic-gate 	if (last == REP_PROTOCOL_ENTITY_SERVICE) {
54517c478bd9Sstevel@tonic-gate 		handle_rele_subhandles(h, holds);
54527c478bd9Sstevel@tonic-gate 		return (0);
54537c478bd9Sstevel@tonic-gate 	}
54547c478bd9Sstevel@tonic-gate 
54557c478bd9Sstevel@tonic-gate 	if (instance == NULL) {
54567c478bd9Sstevel@tonic-gate 		if (propertygroup == NULL ||
54577c478bd9Sstevel@tonic-gate 		    last == REP_PROTOCOL_ENTITY_INSTANCE) {
54587c478bd9Sstevel@tonic-gate 			handle_rele_subhandles(h, holds);
54597c478bd9Sstevel@tonic-gate 			return (0);
54607c478bd9Sstevel@tonic-gate 		}
54617c478bd9Sstevel@tonic-gate 
54627c478bd9Sstevel@tonic-gate 		if (scf_service_get_pg(svc, propertygroup, pg) == -1) {
54637c478bd9Sstevel@tonic-gate 			handle_rele_subhandles(h, holds);
54647c478bd9Sstevel@tonic-gate 			ret = -1;
54657c478bd9Sstevel@tonic-gate 			assert(scf_error() != SCF_ERROR_NOT_SET);
54667c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_DELETED)
54677c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
54687c478bd9Sstevel@tonic-gate 			goto reset_args;
54697c478bd9Sstevel@tonic-gate 		}
54707c478bd9Sstevel@tonic-gate 	} else {
54717c478bd9Sstevel@tonic-gate 		if (scf_service_get_instance(svc, instance, inst) == -1) {
54727c478bd9Sstevel@tonic-gate 			handle_rele_subhandles(h, holds);
54737c478bd9Sstevel@tonic-gate 			ret = -1;
54747c478bd9Sstevel@tonic-gate 			assert(scf_error() != SCF_ERROR_NOT_SET);
54757c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_DELETED)
54767c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
54777c478bd9Sstevel@tonic-gate 			goto reset_args;
54787c478bd9Sstevel@tonic-gate 		}
54797c478bd9Sstevel@tonic-gate 
54807c478bd9Sstevel@tonic-gate 		if (propertygroup == NULL ||
54817c478bd9Sstevel@tonic-gate 		    last == REP_PROTOCOL_ENTITY_INSTANCE) {
54827c478bd9Sstevel@tonic-gate 			handle_rele_subhandles(h, holds);
54837c478bd9Sstevel@tonic-gate 			return (0);
54847c478bd9Sstevel@tonic-gate 		}
54857c478bd9Sstevel@tonic-gate 
54867c478bd9Sstevel@tonic-gate 		if (scf_instance_get_pg(inst, propertygroup, pg) == -1) {
54877c478bd9Sstevel@tonic-gate 			handle_rele_subhandles(h, holds);
54887c478bd9Sstevel@tonic-gate 			ret = -1;
54897c478bd9Sstevel@tonic-gate 			assert(scf_error() != SCF_ERROR_NOT_SET);
54907c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_DELETED)
54917c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
54927c478bd9Sstevel@tonic-gate 			goto reset_args;
54937c478bd9Sstevel@tonic-gate 		}
54947c478bd9Sstevel@tonic-gate 	}
54957c478bd9Sstevel@tonic-gate 
54967c478bd9Sstevel@tonic-gate 	if (property == NULL || last == REP_PROTOCOL_ENTITY_PROPERTYGRP) {
54977c478bd9Sstevel@tonic-gate 		handle_rele_subhandles(h, holds);
54987c478bd9Sstevel@tonic-gate 		return (0);
54997c478bd9Sstevel@tonic-gate 	}
55007c478bd9Sstevel@tonic-gate 
55017c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, property, prop) == -1) {
55027c478bd9Sstevel@tonic-gate 		handle_rele_subhandles(h, holds);
55037c478bd9Sstevel@tonic-gate 		ret = -1;
55047c478bd9Sstevel@tonic-gate 		assert(scf_error() != SCF_ERROR_NOT_SET);
55057c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_DELETED)
55067c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NOT_FOUND);
55077c478bd9Sstevel@tonic-gate 		goto reset_args;
55087c478bd9Sstevel@tonic-gate 	}
55097c478bd9Sstevel@tonic-gate 
55107c478bd9Sstevel@tonic-gate 	handle_rele_subhandles(h, holds);
55117c478bd9Sstevel@tonic-gate 	return (0);
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate reset_args:
55147c478bd9Sstevel@tonic-gate 	if (sc != NULL)
55157c478bd9Sstevel@tonic-gate 		datael_reset(&sc->rd_d);
55167c478bd9Sstevel@tonic-gate 	if (svc != NULL)
55177c478bd9Sstevel@tonic-gate 		datael_reset(&svc->rd_d);
55187c478bd9Sstevel@tonic-gate 	if (inst != NULL)
55197c478bd9Sstevel@tonic-gate 		datael_reset(&inst->rd_d);
55207c478bd9Sstevel@tonic-gate 	if (pg != NULL)
55217c478bd9Sstevel@tonic-gate 		datael_reset(&pg->rd_d);
55227c478bd9Sstevel@tonic-gate 	if (prop != NULL)
55237c478bd9Sstevel@tonic-gate 		datael_reset(&prop->rd_d);
55247c478bd9Sstevel@tonic-gate 
55257c478bd9Sstevel@tonic-gate 	return (ret);
55267c478bd9Sstevel@tonic-gate }
55277c478bd9Sstevel@tonic-gate 
55287c478bd9Sstevel@tonic-gate /*
55297c478bd9Sstevel@tonic-gate  * Fails with _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response too
55307c478bd9Sstevel@tonic-gate  * big, bad entity id, request not applicable to entity, name too long for
55317c478bd9Sstevel@tonic-gate  * buffer), _NOT_SET, or _DELETED.
55327c478bd9Sstevel@tonic-gate  */
55337c478bd9Sstevel@tonic-gate ssize_t
55347c478bd9Sstevel@tonic-gate scf_scope_to_fmri(const scf_scope_t *scope, char *out, size_t sz)
55357c478bd9Sstevel@tonic-gate {
55367c478bd9Sstevel@tonic-gate 	ssize_t r, len;
55377c478bd9Sstevel@tonic-gate 
55387c478bd9Sstevel@tonic-gate 	char tmp[REP_PROTOCOL_NAME_LEN];
55397c478bd9Sstevel@tonic-gate 
55407c478bd9Sstevel@tonic-gate 	r = scf_scope_get_name(scope, tmp, sizeof (tmp));
55417c478bd9Sstevel@tonic-gate 
55427c478bd9Sstevel@tonic-gate 	if (r <= 0)
55437c478bd9Sstevel@tonic-gate 		return (r);
55447c478bd9Sstevel@tonic-gate 
55457c478bd9Sstevel@tonic-gate 	len = strlcpy(out, SCF_FMRI_SVC_PREFIX, sz);
55467c478bd9Sstevel@tonic-gate 	if (strcmp(tmp, SCF_FMRI_LOCAL_SCOPE) != 0) {
55477c478bd9Sstevel@tonic-gate 		if (len >= sz)
55487c478bd9Sstevel@tonic-gate 			return (len + r + sizeof (SCF_FMRI_SCOPE_SUFFIX) - 1);
55497c478bd9Sstevel@tonic-gate 
55507c478bd9Sstevel@tonic-gate 		len = strlcat(out, tmp, sz);
55517c478bd9Sstevel@tonic-gate 		if (len >= sz)
55527c478bd9Sstevel@tonic-gate 			return (len + sizeof (SCF_FMRI_SCOPE_SUFFIX) - 1);
55537c478bd9Sstevel@tonic-gate 		len = strlcat(out,
55547c478bd9Sstevel@tonic-gate 		    SCF_FMRI_SCOPE_SUFFIX SCF_FMRI_SERVICE_PREFIX, sz);
55557c478bd9Sstevel@tonic-gate 	}
55567c478bd9Sstevel@tonic-gate 
55577c478bd9Sstevel@tonic-gate 	return (len);
55587c478bd9Sstevel@tonic-gate }
55597c478bd9Sstevel@tonic-gate 
55607c478bd9Sstevel@tonic-gate /*
55617c478bd9Sstevel@tonic-gate  * Fails with _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL (server response too
55627c478bd9Sstevel@tonic-gate  * big, bad element id, bad ids, bad types, scope has no parent, request not
55637c478bd9Sstevel@tonic-gate  * applicable to entity, name too long), _NOT_SET, _DELETED,
55647c478bd9Sstevel@tonic-gate  */
55657c478bd9Sstevel@tonic-gate ssize_t
55667c478bd9Sstevel@tonic-gate scf_service_to_fmri(const scf_service_t *svc, char *out, size_t sz)
55677c478bd9Sstevel@tonic-gate {
55687c478bd9Sstevel@tonic-gate 	scf_handle_t *h = svc->rd_d.rd_handle;
55697c478bd9Sstevel@tonic-gate 	scf_scope_t *scope = HANDLE_HOLD_SCOPE(h);
55707c478bd9Sstevel@tonic-gate 	ssize_t r, len;
55717c478bd9Sstevel@tonic-gate 
55727c478bd9Sstevel@tonic-gate 	char tmp[REP_PROTOCOL_NAME_LEN];
55737c478bd9Sstevel@tonic-gate 
55747c478bd9Sstevel@tonic-gate 	r = datael_get_parent(&svc->rd_d, &scope->rd_d);
55757c478bd9Sstevel@tonic-gate 	if (r != SCF_SUCCESS) {
55767c478bd9Sstevel@tonic-gate 		HANDLE_RELE_SCOPE(h);
55777c478bd9Sstevel@tonic-gate 
55787c478bd9Sstevel@tonic-gate 		assert(scf_error() != SCF_ERROR_HANDLE_MISMATCH);
55797c478bd9Sstevel@tonic-gate 		return (-1);
55807c478bd9Sstevel@tonic-gate 	}
55817c478bd9Sstevel@tonic-gate 	if (out != NULL && sz > 0)
55827c478bd9Sstevel@tonic-gate 		len = scf_scope_to_fmri(scope, out, sz);
55837c478bd9Sstevel@tonic-gate 	else
55847c478bd9Sstevel@tonic-gate 		len = scf_scope_to_fmri(scope, tmp, 2);
55857c478bd9Sstevel@tonic-gate 
55867c478bd9Sstevel@tonic-gate 	HANDLE_RELE_SCOPE(h);
55877c478bd9Sstevel@tonic-gate 
55887c478bd9Sstevel@tonic-gate 	if (len < 0)
55897c478bd9Sstevel@tonic-gate 		return (-1);
55907c478bd9Sstevel@tonic-gate 
55917c478bd9Sstevel@tonic-gate 	if (out == NULL || len >= sz)
55927c478bd9Sstevel@tonic-gate 		len += sizeof (SCF_FMRI_SERVICE_PREFIX) - 1;
55937c478bd9Sstevel@tonic-gate 	else
55947c478bd9Sstevel@tonic-gate 		len = strlcat(out, SCF_FMRI_SERVICE_PREFIX, sz);
55957c478bd9Sstevel@tonic-gate 
55967c478bd9Sstevel@tonic-gate 	r = scf_service_get_name(svc, tmp, sizeof (tmp));
55977c478bd9Sstevel@tonic-gate 	if (r < 0)
55987c478bd9Sstevel@tonic-gate 		return (r);
55997c478bd9Sstevel@tonic-gate 
56007c478bd9Sstevel@tonic-gate 	if (out == NULL || len >= sz)
56017c478bd9Sstevel@tonic-gate 		len += r;
56027c478bd9Sstevel@tonic-gate 	else
56037c478bd9Sstevel@tonic-gate 		len = strlcat(out, tmp, sz);
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate 	return (len);
56067c478bd9Sstevel@tonic-gate }
56077c478bd9Sstevel@tonic-gate 
56087c478bd9Sstevel@tonic-gate ssize_t
56097c478bd9Sstevel@tonic-gate scf_instance_to_fmri(const scf_instance_t *inst, char *out, size_t sz)
56107c478bd9Sstevel@tonic-gate {
56117c478bd9Sstevel@tonic-gate 	scf_handle_t *h = inst->rd_d.rd_handle;
56127c478bd9Sstevel@tonic-gate 	scf_service_t *svc = HANDLE_HOLD_SERVICE(h);
56137c478bd9Sstevel@tonic-gate 	ssize_t r, len;
56147c478bd9Sstevel@tonic-gate 
56157c478bd9Sstevel@tonic-gate 	char tmp[REP_PROTOCOL_NAME_LEN];
56167c478bd9Sstevel@tonic-gate 
56177c478bd9Sstevel@tonic-gate 	r = datael_get_parent(&inst->rd_d, &svc->rd_d);
56187c478bd9Sstevel@tonic-gate 	if (r != SCF_SUCCESS) {
56197c478bd9Sstevel@tonic-gate 		HANDLE_RELE_SERVICE(h);
56207c478bd9Sstevel@tonic-gate 		return (-1);
56217c478bd9Sstevel@tonic-gate 	}
56227c478bd9Sstevel@tonic-gate 
56237c478bd9Sstevel@tonic-gate 	len = scf_service_to_fmri(svc, out, sz);
56247c478bd9Sstevel@tonic-gate 
56257c478bd9Sstevel@tonic-gate 	HANDLE_RELE_SERVICE(h);
56267c478bd9Sstevel@tonic-gate 
56277c478bd9Sstevel@tonic-gate 	if (len < 0)
56287c478bd9Sstevel@tonic-gate 		return (len);
56297c478bd9Sstevel@tonic-gate 
56307c478bd9Sstevel@tonic-gate 	if (len >= sz)
56317c478bd9Sstevel@tonic-gate 		len += sizeof (SCF_FMRI_INSTANCE_PREFIX) - 1;
56327c478bd9Sstevel@tonic-gate 	else
56337c478bd9Sstevel@tonic-gate 		len = strlcat(out, SCF_FMRI_INSTANCE_PREFIX, sz);
56347c478bd9Sstevel@tonic-gate 
56357c478bd9Sstevel@tonic-gate 	r = scf_instance_get_name(inst, tmp, sizeof (tmp));
56367c478bd9Sstevel@tonic-gate 	if (r < 0)
56377c478bd9Sstevel@tonic-gate 		return (r);
56387c478bd9Sstevel@tonic-gate 
56397c478bd9Sstevel@tonic-gate 	if (len >= sz)
56407c478bd9Sstevel@tonic-gate 		len += r;
56417c478bd9Sstevel@tonic-gate 	else
56427c478bd9Sstevel@tonic-gate 		len = strlcat(out, tmp, sz);
56437c478bd9Sstevel@tonic-gate 
56447c478bd9Sstevel@tonic-gate 	return (len);
56457c478bd9Sstevel@tonic-gate }
56467c478bd9Sstevel@tonic-gate 
56477c478bd9Sstevel@tonic-gate ssize_t
56487c478bd9Sstevel@tonic-gate scf_pg_to_fmri(const scf_propertygroup_t *pg, char *out, size_t sz)
56497c478bd9Sstevel@tonic-gate {
56507c478bd9Sstevel@tonic-gate 	scf_handle_t *h = pg->rd_d.rd_handle;
56517c478bd9Sstevel@tonic-gate 
56527c478bd9Sstevel@tonic-gate 	struct rep_protocol_entity_parent_type request;
56537c478bd9Sstevel@tonic-gate 	struct rep_protocol_integer_response response;
56547c478bd9Sstevel@tonic-gate 
56557c478bd9Sstevel@tonic-gate 	char tmp[REP_PROTOCOL_NAME_LEN];
56567c478bd9Sstevel@tonic-gate 	ssize_t len, r;
56577c478bd9Sstevel@tonic-gate 
56587c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
56597c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_ENTITY_PARENT_TYPE;
56607c478bd9Sstevel@tonic-gate 	request.rpr_entityid = pg->rd_d.rd_entity;
56617c478bd9Sstevel@tonic-gate 
56627c478bd9Sstevel@tonic-gate 	datael_finish_reset(&pg->rd_d);
56637c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
56647c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
56657c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
56667c478bd9Sstevel@tonic-gate 
56677c478bd9Sstevel@tonic-gate 	if (r < 0)
56687c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
56697c478bd9Sstevel@tonic-gate 
56707c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS ||
56717c478bd9Sstevel@tonic-gate 	    r < sizeof (response)) {
56727c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
56737c478bd9Sstevel@tonic-gate 	}
56747c478bd9Sstevel@tonic-gate 
56757c478bd9Sstevel@tonic-gate 	switch (response.rpr_value) {
56767c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_ENTITY_SERVICE: {
56777c478bd9Sstevel@tonic-gate 		scf_service_t *svc;
56787c478bd9Sstevel@tonic-gate 
56797c478bd9Sstevel@tonic-gate 		svc = HANDLE_HOLD_SERVICE(h);
56807c478bd9Sstevel@tonic-gate 
56817c478bd9Sstevel@tonic-gate 		r = datael_get_parent(&pg->rd_d, &svc->rd_d);
56827c478bd9Sstevel@tonic-gate 
56837c478bd9Sstevel@tonic-gate 		if (r == SCF_SUCCESS)
56847c478bd9Sstevel@tonic-gate 			len = scf_service_to_fmri(svc, out, sz);
56857c478bd9Sstevel@tonic-gate 
56867c478bd9Sstevel@tonic-gate 		HANDLE_RELE_SERVICE(h);
56877c478bd9Sstevel@tonic-gate 		break;
56887c478bd9Sstevel@tonic-gate 	}
56897c478bd9Sstevel@tonic-gate 
56907c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_ENTITY_INSTANCE: {
56917c478bd9Sstevel@tonic-gate 		scf_instance_t *inst;
56927c478bd9Sstevel@tonic-gate 
56937c478bd9Sstevel@tonic-gate 		inst = HANDLE_HOLD_INSTANCE(h);
56947c478bd9Sstevel@tonic-gate 
56957c478bd9Sstevel@tonic-gate 		r = datael_get_parent(&pg->rd_d, &inst->rd_d);
56967c478bd9Sstevel@tonic-gate 
56977c478bd9Sstevel@tonic-gate 		if (r == SCF_SUCCESS)
56987c478bd9Sstevel@tonic-gate 			len = scf_instance_to_fmri(inst, out, sz);
56997c478bd9Sstevel@tonic-gate 
57007c478bd9Sstevel@tonic-gate 		HANDLE_RELE_INSTANCE(h);
57017c478bd9Sstevel@tonic-gate 		break;
57027c478bd9Sstevel@tonic-gate 	}
57037c478bd9Sstevel@tonic-gate 
57047c478bd9Sstevel@tonic-gate 	case REP_PROTOCOL_ENTITY_SNAPLEVEL: {
57057c478bd9Sstevel@tonic-gate 		scf_instance_t *inst = HANDLE_HOLD_INSTANCE(h);
57067c478bd9Sstevel@tonic-gate 		scf_snapshot_t *snap = HANDLE_HOLD_SNAPSHOT(h);
57077c478bd9Sstevel@tonic-gate 		scf_snaplevel_t *level = HANDLE_HOLD_SNAPLVL(h);
57087c478bd9Sstevel@tonic-gate 
57097c478bd9Sstevel@tonic-gate 		r = datael_get_parent(&pg->rd_d, &level->rd_d);
57107c478bd9Sstevel@tonic-gate 
57117c478bd9Sstevel@tonic-gate 		if (r == SCF_SUCCESS)
57127c478bd9Sstevel@tonic-gate 			r = datael_get_parent(&level->rd_d, &snap->rd_d);
57137c478bd9Sstevel@tonic-gate 
57147c478bd9Sstevel@tonic-gate 		if (r == SCF_SUCCESS)
57157c478bd9Sstevel@tonic-gate 			r = datael_get_parent(&snap->rd_d, &inst->rd_d);
57167c478bd9Sstevel@tonic-gate 
57177c478bd9Sstevel@tonic-gate 		if (r == SCF_SUCCESS)
57187c478bd9Sstevel@tonic-gate 			len = scf_instance_to_fmri(inst, out, sz);
57197c478bd9Sstevel@tonic-gate 
57207c478bd9Sstevel@tonic-gate 		HANDLE_RELE_INSTANCE(h);
57217c478bd9Sstevel@tonic-gate 		HANDLE_RELE_SNAPSHOT(h);
57227c478bd9Sstevel@tonic-gate 		HANDLE_RELE_SNAPLVL(h);
57237c478bd9Sstevel@tonic-gate 		break;
57247c478bd9Sstevel@tonic-gate 	}
57257c478bd9Sstevel@tonic-gate 
57267c478bd9Sstevel@tonic-gate 	default:
57277c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INTERNAL));
57287c478bd9Sstevel@tonic-gate 	}
57297c478bd9Sstevel@tonic-gate 
57307c478bd9Sstevel@tonic-gate 	if (r != SCF_SUCCESS)
57317c478bd9Sstevel@tonic-gate 		return (r);
57327c478bd9Sstevel@tonic-gate 
57337c478bd9Sstevel@tonic-gate 	if (len >= sz)
57347c478bd9Sstevel@tonic-gate 		len += sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1;
57357c478bd9Sstevel@tonic-gate 	else
57367c478bd9Sstevel@tonic-gate 		len = strlcat(out, SCF_FMRI_PROPERTYGRP_PREFIX, sz);
57377c478bd9Sstevel@tonic-gate 
57387c478bd9Sstevel@tonic-gate 	r = scf_pg_get_name(pg, tmp, sizeof (tmp));
57397c478bd9Sstevel@tonic-gate 
57407c478bd9Sstevel@tonic-gate 	if (r < 0)
57417c478bd9Sstevel@tonic-gate 		return (r);
57427c478bd9Sstevel@tonic-gate 
57437c478bd9Sstevel@tonic-gate 	if (len >= sz)
57447c478bd9Sstevel@tonic-gate 		len += r;
57457c478bd9Sstevel@tonic-gate 	else
57467c478bd9Sstevel@tonic-gate 		len = strlcat(out, tmp, sz);
57477c478bd9Sstevel@tonic-gate 
57487c478bd9Sstevel@tonic-gate 	return (len);
57497c478bd9Sstevel@tonic-gate }
57507c478bd9Sstevel@tonic-gate 
57517c478bd9Sstevel@tonic-gate ssize_t
57527c478bd9Sstevel@tonic-gate scf_property_to_fmri(const scf_property_t *prop, char *out, size_t sz)
57537c478bd9Sstevel@tonic-gate {
57547c478bd9Sstevel@tonic-gate 	scf_handle_t *h = prop->rd_d.rd_handle;
57557c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = HANDLE_HOLD_PG(h);
57567c478bd9Sstevel@tonic-gate 
57577c478bd9Sstevel@tonic-gate 	char tmp[REP_PROTOCOL_NAME_LEN];
57587c478bd9Sstevel@tonic-gate 	ssize_t len;
57597c478bd9Sstevel@tonic-gate 	int r;
57607c478bd9Sstevel@tonic-gate 
57617c478bd9Sstevel@tonic-gate 	r = datael_get_parent(&prop->rd_d, &pg->rd_d);
57627c478bd9Sstevel@tonic-gate 	if (r != SCF_SUCCESS) {
57637c478bd9Sstevel@tonic-gate 		HANDLE_RELE_PG(h);
57647c478bd9Sstevel@tonic-gate 		return (-1);
57657c478bd9Sstevel@tonic-gate 	}
57667c478bd9Sstevel@tonic-gate 
57677c478bd9Sstevel@tonic-gate 	len = scf_pg_to_fmri(pg, out, sz);
57687c478bd9Sstevel@tonic-gate 
57697c478bd9Sstevel@tonic-gate 	HANDLE_RELE_PG(h);
57707c478bd9Sstevel@tonic-gate 
57717c478bd9Sstevel@tonic-gate 	if (len >= sz)
57727c478bd9Sstevel@tonic-gate 		len += sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1;
57737c478bd9Sstevel@tonic-gate 	else
57747c478bd9Sstevel@tonic-gate 		len = strlcat(out, SCF_FMRI_PROPERTY_PREFIX, sz);
57757c478bd9Sstevel@tonic-gate 
57767c478bd9Sstevel@tonic-gate 	r = scf_property_get_name(prop, tmp, sizeof (tmp));
57777c478bd9Sstevel@tonic-gate 
57787c478bd9Sstevel@tonic-gate 	if (r < 0)
57797c478bd9Sstevel@tonic-gate 		return (r);
57807c478bd9Sstevel@tonic-gate 
57817c478bd9Sstevel@tonic-gate 	if (len >= sz)
57827c478bd9Sstevel@tonic-gate 		len += r;
57837c478bd9Sstevel@tonic-gate 	else
57847c478bd9Sstevel@tonic-gate 		len = strlcat(out, tmp, sz);
57857c478bd9Sstevel@tonic-gate 
57867c478bd9Sstevel@tonic-gate 	return (len);
57877c478bd9Sstevel@tonic-gate }
57887c478bd9Sstevel@tonic-gate 
578976cf44abSjeanm /*
579076cf44abSjeanm  * Fails with _HANDLE_MISMATCH, _NOT_BOUND, _CONNECTION_BROKEN, _INTERNAL
579176cf44abSjeanm  * (server response too big, bad entity id, request not applicable to entity,
579276cf44abSjeanm  * name too long for buffer, bad element id, iter already exists, element
579376cf44abSjeanm  * cannot have children of type, type is invalid, iter was reset, sequence
579476cf44abSjeanm  * was bad, iter walks values, iter does not walk type entities),
579576cf44abSjeanm  * _NOT_SET, _DELETED, or _CONSTRAINT_VIOLATED,
579676cf44abSjeanm  * _NOT_FOUND (scope has no parent),  _INVALID_ARGUMENT, _NO_RESOURCES,
579776cf44abSjeanm  * _BACKEND_ACCESS.
579876cf44abSjeanm  */
57997c478bd9Sstevel@tonic-gate int
58007c478bd9Sstevel@tonic-gate scf_pg_get_underlying_pg(const scf_propertygroup_t *pg,
58017c478bd9Sstevel@tonic-gate     scf_propertygroup_t *out)
58027c478bd9Sstevel@tonic-gate {
58037c478bd9Sstevel@tonic-gate 	scf_handle_t *h = pg->rd_d.rd_handle;
58047c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
58057c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
58067c478bd9Sstevel@tonic-gate 
58077c478bd9Sstevel@tonic-gate 	char me[REP_PROTOCOL_NAME_LEN];
58087c478bd9Sstevel@tonic-gate 	int r;
58097c478bd9Sstevel@tonic-gate 
58107c478bd9Sstevel@tonic-gate 	if (h != out->rd_d.rd_handle)
58117c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_HANDLE_MISMATCH));
58127c478bd9Sstevel@tonic-gate 
58137c478bd9Sstevel@tonic-gate 	r = scf_pg_get_name(pg, me, sizeof (me));
58147c478bd9Sstevel@tonic-gate 
58157c478bd9Sstevel@tonic-gate 	if (r < 0)
58167c478bd9Sstevel@tonic-gate 		return (r);
58177c478bd9Sstevel@tonic-gate 
58187c478bd9Sstevel@tonic-gate 	svc = HANDLE_HOLD_SERVICE(h);
58197c478bd9Sstevel@tonic-gate 	inst = HANDLE_HOLD_INSTANCE(h);
58207c478bd9Sstevel@tonic-gate 
58217c478bd9Sstevel@tonic-gate 	r = datael_get_parent(&pg->rd_d, &inst->rd_d);
58227c478bd9Sstevel@tonic-gate 
58237c478bd9Sstevel@tonic-gate 	if (r == SCF_SUCCESS) {
58247c478bd9Sstevel@tonic-gate 		r = datael_get_parent(&inst->rd_d, &svc->rd_d);
58257c478bd9Sstevel@tonic-gate 		if (r != SCF_SUCCESS) {
58267c478bd9Sstevel@tonic-gate 			goto out;
58277c478bd9Sstevel@tonic-gate 		}
58287c478bd9Sstevel@tonic-gate 		r = scf_service_get_pg(svc, me, out);
58297c478bd9Sstevel@tonic-gate 	} else {
58307c478bd9Sstevel@tonic-gate 		r = scf_set_error(SCF_ERROR_NOT_FOUND);
58317c478bd9Sstevel@tonic-gate 	}
58327c478bd9Sstevel@tonic-gate 
58337c478bd9Sstevel@tonic-gate out:
58347c478bd9Sstevel@tonic-gate 	HANDLE_RELE_SERVICE(h);
58357c478bd9Sstevel@tonic-gate 	HANDLE_RELE_INSTANCE(h);
58367c478bd9Sstevel@tonic-gate 	return (r);
58377c478bd9Sstevel@tonic-gate }
58387c478bd9Sstevel@tonic-gate 
58397c478bd9Sstevel@tonic-gate #define	LEGACY_SCHEME	"lrc:"
58407c478bd9Sstevel@tonic-gate #define	LEGACY_UNKNOWN	"unknown"
58417c478bd9Sstevel@tonic-gate 
58427c478bd9Sstevel@tonic-gate /*
58437c478bd9Sstevel@tonic-gate  * Implementation of scf_walk_fmri()
58447c478bd9Sstevel@tonic-gate  *
58457c478bd9Sstevel@tonic-gate  * This is a little tricky due to the many-to-many relationship between patterns
58467c478bd9Sstevel@tonic-gate  * and matches.  We need to be able to satisfy the following requirements:
58477c478bd9Sstevel@tonic-gate  *
58487c478bd9Sstevel@tonic-gate  * 	1) Detect patterns which match more than one FMRI, and be able to
58497c478bd9Sstevel@tonic-gate  *         report which FMRIs have been matched.
58507c478bd9Sstevel@tonic-gate  * 	2) Detect patterns which have not matched any FMRIs
58517c478bd9Sstevel@tonic-gate  * 	3) Visit each matching FMRI exactly once across all patterns
58527c478bd9Sstevel@tonic-gate  * 	4) Ignore FMRIs which have only been matched due to multiply-matching
58537c478bd9Sstevel@tonic-gate  *         patterns.
58547c478bd9Sstevel@tonic-gate  *
58557c478bd9Sstevel@tonic-gate  * We maintain an array of scf_pattern_t structures, one for each argument, and
58567c478bd9Sstevel@tonic-gate  * maintain a linked list of scf_match_t structures for each one.  We first
58577c478bd9Sstevel@tonic-gate  * qualify each pattern's type:
58587c478bd9Sstevel@tonic-gate  *
58597c478bd9Sstevel@tonic-gate  *	PATTERN_INVALID		The argument is invalid (too long).
58607c478bd9Sstevel@tonic-gate  *
58617c478bd9Sstevel@tonic-gate  *	PATTERN_EXACT		The pattern is a complete FMRI.  The list of
58627c478bd9Sstevel@tonic-gate  *				matches contains only a single entry.
58637c478bd9Sstevel@tonic-gate  *
58647c478bd9Sstevel@tonic-gate  * 	PATTERN_GLOB		The pattern will be matched against all
58657c478bd9Sstevel@tonic-gate  * 				FMRIs via fnmatch() in the second phase.
58667c478bd9Sstevel@tonic-gate  * 				Matches will be added to the pattern's list
58677c478bd9Sstevel@tonic-gate  * 				as they are found.
58687c478bd9Sstevel@tonic-gate  *
58697c478bd9Sstevel@tonic-gate  * 	PATTERN_PARTIAL		Everything else.  We will assume that this is
58707c478bd9Sstevel@tonic-gate  * 				an abbreviated FMRI, and match according to
58717c478bd9Sstevel@tonic-gate  * 				our abbreviated FMRI rules.  Matches will be
58727c478bd9Sstevel@tonic-gate  * 				added to the pattern's list as they are found.
58737c478bd9Sstevel@tonic-gate  *
58747c478bd9Sstevel@tonic-gate  * The first pass searches for arguments that are complete FMRIs.  These are
58757c478bd9Sstevel@tonic-gate  * classified as EXACT patterns and do not necessitate searching the entire
58767c478bd9Sstevel@tonic-gate  * tree.
58777c478bd9Sstevel@tonic-gate  *
58787c478bd9Sstevel@tonic-gate  * Once this is done, if we have any GLOB or PARTIAL patterns (or if no
58797c478bd9Sstevel@tonic-gate  * arguments were given), we iterate over all services and instances in the
58807c478bd9Sstevel@tonic-gate  * repository, looking for matches.
58817c478bd9Sstevel@tonic-gate  *
58827c478bd9Sstevel@tonic-gate  * When a match is found, we add the match to the pattern's list.  We also enter
58837c478bd9Sstevel@tonic-gate  * the match into a hash table, resulting in something like this:
58847c478bd9Sstevel@tonic-gate  *
58857c478bd9Sstevel@tonic-gate  *       scf_pattern_t       scf_match_t
58867c478bd9Sstevel@tonic-gate  *     +---------------+      +-------+     +-------+
58877c478bd9Sstevel@tonic-gate  *     | pattern 'foo' |----->| match |---->| match |
58887c478bd9Sstevel@tonic-gate  *     +---------------+      +-------+     +-------+
58897c478bd9Sstevel@tonic-gate  *                                |             |
58907c478bd9Sstevel@tonic-gate  *           scf_match_key_t      |             |
58917c478bd9Sstevel@tonic-gate  *           +--------------+     |             |
58927c478bd9Sstevel@tonic-gate  *           | FMRI bar/foo |<----+             |
58937c478bd9Sstevel@tonic-gate  *           +--------------+                   |
58947c478bd9Sstevel@tonic-gate  *           | FMRI baz/foo |<------------------+
58957c478bd9Sstevel@tonic-gate  *           +--------------+
58967c478bd9Sstevel@tonic-gate  *
58977c478bd9Sstevel@tonic-gate  * Once we have all of this set up, we do one pass to report patterns matching
58987c478bd9Sstevel@tonic-gate  * multiple FMRIs (if SCF_WALK_MULTIPLE is not set) and patterns for which no
58997c478bd9Sstevel@tonic-gate  * match was found.
59007c478bd9Sstevel@tonic-gate  *
59017c478bd9Sstevel@tonic-gate  * Finally, we walk through all valid patterns, and for each match, if we
59027c478bd9Sstevel@tonic-gate  * haven't already seen the match (as recorded in the hash table), then we
59037c478bd9Sstevel@tonic-gate  * execute the callback.
59047c478bd9Sstevel@tonic-gate  */
59057c478bd9Sstevel@tonic-gate 
59067c478bd9Sstevel@tonic-gate struct scf_matchkey;
59077c478bd9Sstevel@tonic-gate struct scf_match;
59087c478bd9Sstevel@tonic-gate 
59097c478bd9Sstevel@tonic-gate /*
59107c478bd9Sstevel@tonic-gate  * scf_matchkey_t
59117c478bd9Sstevel@tonic-gate  */
59127c478bd9Sstevel@tonic-gate typedef struct scf_matchkey {
59137c478bd9Sstevel@tonic-gate 	char			*sk_fmri;	/* Matching FMRI */
59147c478bd9Sstevel@tonic-gate 	char			*sk_legacy;	/* Legacy name */
59157c478bd9Sstevel@tonic-gate 	int			sk_seen;	/* If we've been seen */
59167c478bd9Sstevel@tonic-gate 	struct scf_matchkey	*sk_next;	/* Next in hash chain */
59177c478bd9Sstevel@tonic-gate } scf_matchkey_t;
59187c478bd9Sstevel@tonic-gate 
59197c478bd9Sstevel@tonic-gate /*
59207c478bd9Sstevel@tonic-gate  * scf_match_t
59217c478bd9Sstevel@tonic-gate  */
59227c478bd9Sstevel@tonic-gate typedef struct scf_match {
59237c478bd9Sstevel@tonic-gate 	scf_matchkey_t		*sm_key;
59247c478bd9Sstevel@tonic-gate 	struct scf_match	*sm_next;
59257c478bd9Sstevel@tonic-gate } scf_match_t;
59267c478bd9Sstevel@tonic-gate 
59277c478bd9Sstevel@tonic-gate #define	WALK_HTABLE_SIZE	123
59287c478bd9Sstevel@tonic-gate 
59297c478bd9Sstevel@tonic-gate /*
59307c478bd9Sstevel@tonic-gate  * scf_get_key()
59317c478bd9Sstevel@tonic-gate  *
59327c478bd9Sstevel@tonic-gate  * Given an FMRI and a hash table, returns the scf_matchkey_t corresponding to
59337c478bd9Sstevel@tonic-gate  * this FMRI.  If the FMRI does not exist, it is added to the hash table.  If a
59347c478bd9Sstevel@tonic-gate  * new entry cannot be allocated due to lack of memory, NULL is returned.
59357c478bd9Sstevel@tonic-gate  */
59367c478bd9Sstevel@tonic-gate static scf_matchkey_t *
59377c478bd9Sstevel@tonic-gate scf_get_key(scf_matchkey_t **htable, const char *fmri, const char *legacy)
59387c478bd9Sstevel@tonic-gate {
59397c478bd9Sstevel@tonic-gate 	uint_t h = 0, g;
59407c478bd9Sstevel@tonic-gate 	const char *p, *k;
59417c478bd9Sstevel@tonic-gate 	scf_matchkey_t *key;
59427c478bd9Sstevel@tonic-gate 
59437c478bd9Sstevel@tonic-gate 	k = strstr(fmri, ":/");
59447c478bd9Sstevel@tonic-gate 	assert(k != NULL);
59457c478bd9Sstevel@tonic-gate 	k += 2;
59467c478bd9Sstevel@tonic-gate 
59477c478bd9Sstevel@tonic-gate 	/*
59487c478bd9Sstevel@tonic-gate 	 * Generic hash function from uts/common/os/modhash.c.
59497c478bd9Sstevel@tonic-gate 	 */
59507c478bd9Sstevel@tonic-gate 	for (p = k; *p != '\0'; ++p) {
59517c478bd9Sstevel@tonic-gate 		h = (h << 4) + *p;
59527c478bd9Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
59537c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
59547c478bd9Sstevel@tonic-gate 			h ^= g;
59557c478bd9Sstevel@tonic-gate 		}
59567c478bd9Sstevel@tonic-gate 	}
59577c478bd9Sstevel@tonic-gate 
59587c478bd9Sstevel@tonic-gate 	h %= WALK_HTABLE_SIZE;
59597c478bd9Sstevel@tonic-gate 
59607c478bd9Sstevel@tonic-gate 	/*
59617c478bd9Sstevel@tonic-gate 	 * Search for an existing key
59627c478bd9Sstevel@tonic-gate 	 */
59637c478bd9Sstevel@tonic-gate 	for (key = htable[h]; key != NULL; key = key->sk_next) {
59647c478bd9Sstevel@tonic-gate 		if (strcmp(key->sk_fmri, fmri) == 0)
59657c478bd9Sstevel@tonic-gate 			return (key);
59667c478bd9Sstevel@tonic-gate 	}
59677c478bd9Sstevel@tonic-gate 
59687c478bd9Sstevel@tonic-gate 	if ((key = calloc(sizeof (scf_matchkey_t), 1)) == NULL)
59697c478bd9Sstevel@tonic-gate 		return (NULL);
59707c478bd9Sstevel@tonic-gate 
59717c478bd9Sstevel@tonic-gate 	/*
59727c478bd9Sstevel@tonic-gate 	 * Add new key to hash table.
59737c478bd9Sstevel@tonic-gate 	 */
59747c478bd9Sstevel@tonic-gate 	if ((key->sk_fmri = strdup(fmri)) == NULL) {
59757c478bd9Sstevel@tonic-gate 		free(key);
59767c478bd9Sstevel@tonic-gate 		return (NULL);
59777c478bd9Sstevel@tonic-gate 	}
59787c478bd9Sstevel@tonic-gate 
59797c478bd9Sstevel@tonic-gate 	if (legacy == NULL) {
59807c478bd9Sstevel@tonic-gate 		key->sk_legacy = NULL;
59817c478bd9Sstevel@tonic-gate 	} else if ((key->sk_legacy = strdup(legacy)) == NULL) {
59827c478bd9Sstevel@tonic-gate 		free(key->sk_fmri);
59837c478bd9Sstevel@tonic-gate 		free(key);
59847c478bd9Sstevel@tonic-gate 		return (NULL);
59857c478bd9Sstevel@tonic-gate 	}
59867c478bd9Sstevel@tonic-gate 
59877c478bd9Sstevel@tonic-gate 	key->sk_next = htable[h];
59887c478bd9Sstevel@tonic-gate 	htable[h] = key;
59897c478bd9Sstevel@tonic-gate 
59907c478bd9Sstevel@tonic-gate 	return (key);
59917c478bd9Sstevel@tonic-gate }
59927c478bd9Sstevel@tonic-gate 
59937c478bd9Sstevel@tonic-gate /*
59947c478bd9Sstevel@tonic-gate  * Given an FMRI, insert it into the pattern's list appropriately.
59957c478bd9Sstevel@tonic-gate  * svc_explicit indicates whether matching services should take
59967c478bd9Sstevel@tonic-gate  * precedence over matching instances.
59977c478bd9Sstevel@tonic-gate  */
59987c478bd9Sstevel@tonic-gate static scf_error_t
59997c478bd9Sstevel@tonic-gate scf_add_match(scf_matchkey_t **htable, const char *fmri, const char *legacy,
60007c478bd9Sstevel@tonic-gate     scf_pattern_t *pattern, int svc_explicit)
60017c478bd9Sstevel@tonic-gate {
60027c478bd9Sstevel@tonic-gate 	scf_match_t *match;
60037c478bd9Sstevel@tonic-gate 
60047c478bd9Sstevel@tonic-gate 	/*
60057c478bd9Sstevel@tonic-gate 	 * If svc_explicit is set, enforce the constaint that matching
60067c478bd9Sstevel@tonic-gate 	 * instances take precedence over matching services. Otherwise,
60077c478bd9Sstevel@tonic-gate 	 * matching services take precedence over matching instances.
60087c478bd9Sstevel@tonic-gate 	 */
60097c478bd9Sstevel@tonic-gate 	if (svc_explicit) {
60107c478bd9Sstevel@tonic-gate 		scf_match_t *next, *prev;
60117c478bd9Sstevel@tonic-gate 		/*
60127c478bd9Sstevel@tonic-gate 		 * If we match an instance, check to see if we must remove
60137c478bd9Sstevel@tonic-gate 		 * any matching services (for SCF_WALK_EXPLICIT).
60147c478bd9Sstevel@tonic-gate 		 */
60157c478bd9Sstevel@tonic-gate 		for (prev = match = pattern->sp_matches; match != NULL;
60167c478bd9Sstevel@tonic-gate 		    match = next) {
60177c478bd9Sstevel@tonic-gate 			size_t len = strlen(match->sm_key->sk_fmri);
60187c478bd9Sstevel@tonic-gate 			next = match->sm_next;
60197c478bd9Sstevel@tonic-gate 			if (strncmp(match->sm_key->sk_fmri, fmri, len) == 0 &&
60207c478bd9Sstevel@tonic-gate 			    fmri[len] == ':') {
60217c478bd9Sstevel@tonic-gate 				if (prev == match)
60227c478bd9Sstevel@tonic-gate 					pattern->sp_matches = match->sm_next;
60237c478bd9Sstevel@tonic-gate 				else
60247c478bd9Sstevel@tonic-gate 					prev->sm_next = match->sm_next;
60257c478bd9Sstevel@tonic-gate 				pattern->sp_matchcount--;
60267c478bd9Sstevel@tonic-gate 				free(match);
60277c478bd9Sstevel@tonic-gate 			} else
60287c478bd9Sstevel@tonic-gate 				prev = match;
60297c478bd9Sstevel@tonic-gate 		}
60307c478bd9Sstevel@tonic-gate 	} else {
60317c478bd9Sstevel@tonic-gate 		/*
60327c478bd9Sstevel@tonic-gate 		 * If we've matched a service don't add any instances (for
60337c478bd9Sstevel@tonic-gate 		 * SCF_WALK_SERVICE).
60347c478bd9Sstevel@tonic-gate 		 */
60357c478bd9Sstevel@tonic-gate 		for (match = pattern->sp_matches; match != NULL;
60367c478bd9Sstevel@tonic-gate 		    match = match->sm_next) {
60377c478bd9Sstevel@tonic-gate 			size_t len = strlen(match->sm_key->sk_fmri);
60387c478bd9Sstevel@tonic-gate 			if (strncmp(match->sm_key->sk_fmri, fmri, len) == 0 &&
60397c478bd9Sstevel@tonic-gate 			    fmri[len] == ':')
60407c478bd9Sstevel@tonic-gate 				return (0);
60417c478bd9Sstevel@tonic-gate 		}
60427c478bd9Sstevel@tonic-gate 	}
60437c478bd9Sstevel@tonic-gate 
60447c478bd9Sstevel@tonic-gate 	if ((match = malloc(sizeof (scf_match_t))) == NULL)
60457c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_NO_MEMORY);
60467c478bd9Sstevel@tonic-gate 
60477c478bd9Sstevel@tonic-gate 	if ((match->sm_key = scf_get_key(htable, fmri, legacy)) == NULL) {
60487c478bd9Sstevel@tonic-gate 		free(match);
60497c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_NO_MEMORY);
60507c478bd9Sstevel@tonic-gate 	}
60517c478bd9Sstevel@tonic-gate 
60527c478bd9Sstevel@tonic-gate 	match->sm_next = pattern->sp_matches;
60537c478bd9Sstevel@tonic-gate 	pattern->sp_matches = match;
60547c478bd9Sstevel@tonic-gate 	pattern->sp_matchcount++;
60557c478bd9Sstevel@tonic-gate 
60567c478bd9Sstevel@tonic-gate 	return (0);
60577c478bd9Sstevel@tonic-gate }
60587c478bd9Sstevel@tonic-gate 
60597c478bd9Sstevel@tonic-gate /*
60607c478bd9Sstevel@tonic-gate  * Returns 1 if the fmri matches the given pattern, 0 otherwise.
60617c478bd9Sstevel@tonic-gate  */
6062103b2b15Sgww int
60637c478bd9Sstevel@tonic-gate scf_cmp_pattern(char *fmri, scf_pattern_t *pattern)
60647c478bd9Sstevel@tonic-gate {
60657c478bd9Sstevel@tonic-gate 	char *tmp;
60667c478bd9Sstevel@tonic-gate 
60677c478bd9Sstevel@tonic-gate 	if (pattern->sp_type == PATTERN_GLOB) {
60687c478bd9Sstevel@tonic-gate 		if (fnmatch(pattern->sp_arg, fmri, 0) == 0)
60697c478bd9Sstevel@tonic-gate 			return (1);
60707c478bd9Sstevel@tonic-gate 	} else if (pattern->sp_type == PATTERN_PARTIAL &&
60717c478bd9Sstevel@tonic-gate 	    (tmp = strstr(fmri, pattern->sp_arg)) != NULL) {
60727c478bd9Sstevel@tonic-gate 		/*
60737c478bd9Sstevel@tonic-gate 		 * We only allow partial matches anchored on the end of
60747c478bd9Sstevel@tonic-gate 		 * a service or instance, and beginning on an element
60757c478bd9Sstevel@tonic-gate 		 * boundary.
60767c478bd9Sstevel@tonic-gate 		 */
60777c478bd9Sstevel@tonic-gate 		if (tmp != fmri && tmp[-1] != '/' && tmp[-1] != ':' &&
60787c478bd9Sstevel@tonic-gate 		    tmp[0] != ':')
60797c478bd9Sstevel@tonic-gate 			return (0);
60807c478bd9Sstevel@tonic-gate 		tmp += strlen(pattern->sp_arg);
60817c478bd9Sstevel@tonic-gate 		if (tmp != fmri + strlen(fmri) && tmp[0] != ':' &&
60827c478bd9Sstevel@tonic-gate 		    tmp[-1] != ':')
60837c478bd9Sstevel@tonic-gate 			return (0);
60847c478bd9Sstevel@tonic-gate 
60857c478bd9Sstevel@tonic-gate 		/*
60867c478bd9Sstevel@tonic-gate 		 * If the user has supplied a short pattern that matches
60877c478bd9Sstevel@tonic-gate 		 * 'svc:/' or 'lrc:/', ignore it.
60887c478bd9Sstevel@tonic-gate 		 */
60897c478bd9Sstevel@tonic-gate 		if (tmp <= fmri + 4)
60907c478bd9Sstevel@tonic-gate 			return (0);
60917c478bd9Sstevel@tonic-gate 
60927c478bd9Sstevel@tonic-gate 		return (1);
60937c478bd9Sstevel@tonic-gate 	}
60947c478bd9Sstevel@tonic-gate 
60957c478bd9Sstevel@tonic-gate 	return (0);
60967c478bd9Sstevel@tonic-gate }
60977c478bd9Sstevel@tonic-gate 
60987c478bd9Sstevel@tonic-gate /*
60997c478bd9Sstevel@tonic-gate  * Attempts to match the given FMRI against a set of patterns, keeping track of
61007c478bd9Sstevel@tonic-gate  * the results.
61017c478bd9Sstevel@tonic-gate  */
61027c478bd9Sstevel@tonic-gate static scf_error_t
61037c478bd9Sstevel@tonic-gate scf_pattern_match(scf_matchkey_t **htable, char *fmri, const char *legacy,
61047c478bd9Sstevel@tonic-gate     int npattern, scf_pattern_t *pattern, int svc_explicit)
61057c478bd9Sstevel@tonic-gate {
61067c478bd9Sstevel@tonic-gate 	int i;
61077c478bd9Sstevel@tonic-gate 	int ret = 0;
61087c478bd9Sstevel@tonic-gate 
61097c478bd9Sstevel@tonic-gate 	for (i = 0; i < npattern; i++) {
61107c478bd9Sstevel@tonic-gate 		if (scf_cmp_pattern(fmri, &pattern[i]) &&
61117c478bd9Sstevel@tonic-gate 		    (ret = scf_add_match(htable, fmri,
61127c478bd9Sstevel@tonic-gate 		    legacy, &pattern[i], svc_explicit)) != 0)
61137c478bd9Sstevel@tonic-gate 			return (ret);
61147c478bd9Sstevel@tonic-gate 	}
61157c478bd9Sstevel@tonic-gate 
61167c478bd9Sstevel@tonic-gate 	return (0);
61177c478bd9Sstevel@tonic-gate }
61187c478bd9Sstevel@tonic-gate 
611976cf44abSjeanm /*
612076cf44abSjeanm  * Fails with _INVALID_ARGUMENT, _HANDLE_DESTROYED, _INTERNAL (bad server
612176cf44abSjeanm  * response or id in use), _NO_MEMORY, _HANDLE_MISMATCH, _CONSTRAINT_VIOLATED,
612276cf44abSjeanm  * _NOT_FOUND, _NOT_BOUND, _CONNECTION_BROKEN, _NOT_SET, _DELETED,
612376cf44abSjeanm  * _NO_RESOURCES, _BACKEND_ACCESS, _TYPE_MISMATCH.
612476cf44abSjeanm  */
61257c478bd9Sstevel@tonic-gate scf_error_t
61267c478bd9Sstevel@tonic-gate scf_walk_fmri(scf_handle_t *h, int argc, char **argv, int flags,
61277c478bd9Sstevel@tonic-gate     scf_walk_callback callback, void *data, int *err,
61287c478bd9Sstevel@tonic-gate     void (*errfunc)(const char *, ...))
61297c478bd9Sstevel@tonic-gate {
61307c478bd9Sstevel@tonic-gate 	scf_pattern_t *pattern = NULL;
61317c478bd9Sstevel@tonic-gate 	int i;
61327c478bd9Sstevel@tonic-gate 	char *fmri = NULL;
61337c478bd9Sstevel@tonic-gate 	ssize_t max_fmri_length;
61347c478bd9Sstevel@tonic-gate 	scf_service_t *svc = NULL;
61357c478bd9Sstevel@tonic-gate 	scf_instance_t *inst = NULL;
61367c478bd9Sstevel@tonic-gate 	scf_iter_t *iter = NULL, *sciter = NULL, *siter = NULL;
61377c478bd9Sstevel@tonic-gate 	scf_scope_t *scope = NULL;
61387c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
61397c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
61407c478bd9Sstevel@tonic-gate 	scf_value_t *value = NULL;
61417c478bd9Sstevel@tonic-gate 	int ret = 0;
61427c478bd9Sstevel@tonic-gate 	scf_matchkey_t **htable = NULL;
61437c478bd9Sstevel@tonic-gate 	int pattern_search = 0;
61447c478bd9Sstevel@tonic-gate 	ssize_t max_name_length;
61457c478bd9Sstevel@tonic-gate 	char *pgname = NULL;
61467c478bd9Sstevel@tonic-gate 	scf_walkinfo_t info;
61477c478bd9Sstevel@tonic-gate 
61487c478bd9Sstevel@tonic-gate #ifndef NDEBUG
61497c478bd9Sstevel@tonic-gate 	if (flags & SCF_WALK_EXPLICIT)
61507c478bd9Sstevel@tonic-gate 		assert(flags & SCF_WALK_SERVICE);
61517c478bd9Sstevel@tonic-gate 	if (flags & SCF_WALK_NOINSTANCE)
61527c478bd9Sstevel@tonic-gate 		assert(flags & SCF_WALK_SERVICE);
61537c478bd9Sstevel@tonic-gate 	if (flags & SCF_WALK_PROPERTY)
61547c478bd9Sstevel@tonic-gate 		assert(!(flags & SCF_WALK_LEGACY));
61557c478bd9Sstevel@tonic-gate #endif
61567c478bd9Sstevel@tonic-gate 
61577c478bd9Sstevel@tonic-gate 	/*
61587c478bd9Sstevel@tonic-gate 	 * Setup initial variables
61597c478bd9Sstevel@tonic-gate 	 */
61607c478bd9Sstevel@tonic-gate 	if ((max_fmri_length = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH)) == -1 ||
61617c478bd9Sstevel@tonic-gate 	    (max_name_length = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) == -1)
61627c478bd9Sstevel@tonic-gate 		return (SCF_ERROR_INTERNAL);
61637c478bd9Sstevel@tonic-gate 
61647c478bd9Sstevel@tonic-gate 	if ((fmri = malloc(max_fmri_length + 1)) == NULL ||
61657c478bd9Sstevel@tonic-gate 	    (pgname = malloc(max_name_length + 1)) == NULL) {
61667c478bd9Sstevel@tonic-gate 		ret = SCF_ERROR_NO_MEMORY;
61677c478bd9Sstevel@tonic-gate 		goto error;
61687c478bd9Sstevel@tonic-gate 	}
61697c478bd9Sstevel@tonic-gate 
61707c478bd9Sstevel@tonic-gate 	if (argc == 0) {
61717c478bd9Sstevel@tonic-gate 		pattern = NULL;
61727c478bd9Sstevel@tonic-gate 	} else if ((pattern = calloc(argc, sizeof (scf_pattern_t)))
61737c478bd9Sstevel@tonic-gate 	    == NULL) {
61747c478bd9Sstevel@tonic-gate 		ret = SCF_ERROR_NO_MEMORY;
61757c478bd9Sstevel@tonic-gate 		goto error;
61767c478bd9Sstevel@tonic-gate 	}
61777c478bd9Sstevel@tonic-gate 
61787c478bd9Sstevel@tonic-gate 	if ((htable = calloc(WALK_HTABLE_SIZE, sizeof (void *))) == NULL) {
61797c478bd9Sstevel@tonic-gate 		ret = SCF_ERROR_NO_MEMORY;
61807c478bd9Sstevel@tonic-gate 		goto error;
61817c478bd9Sstevel@tonic-gate 	}
61827c478bd9Sstevel@tonic-gate 
61837c478bd9Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL ||
61847c478bd9Sstevel@tonic-gate 	    (svc = scf_service_create(h)) == NULL ||
61857c478bd9Sstevel@tonic-gate 	    (iter = scf_iter_create(h)) == NULL ||
61867c478bd9Sstevel@tonic-gate 	    (sciter = scf_iter_create(h)) == NULL ||
61877c478bd9Sstevel@tonic-gate 	    (siter = scf_iter_create(h)) == NULL ||
61887c478bd9Sstevel@tonic-gate 	    (scope = scf_scope_create(h)) == NULL ||
61897c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
61907c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
61917c478bd9Sstevel@tonic-gate 	    (value = scf_value_create(h)) == NULL) {
61927c478bd9Sstevel@tonic-gate 		ret = scf_error();
61937c478bd9Sstevel@tonic-gate 		goto error;
61947c478bd9Sstevel@tonic-gate 	}
61957c478bd9Sstevel@tonic-gate 
61967c478bd9Sstevel@tonic-gate 	/*
61977c478bd9Sstevel@tonic-gate 	 * For each fmri given, we first check to see if it's a full service,
61987c478bd9Sstevel@tonic-gate 	 * instance, property group, or property FMRI.  This avoids having to do
61997c478bd9Sstevel@tonic-gate 	 * the (rather expensive) walk of all instances.  Any element which does
62007c478bd9Sstevel@tonic-gate 	 * not match a full fmri is identified as a globbed pattern or a partial
62017c478bd9Sstevel@tonic-gate 	 * fmri and stored in a private array when walking instances.
62027c478bd9Sstevel@tonic-gate 	 */
62037c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
62047c478bd9Sstevel@tonic-gate 		const char *scope_name, *svc_name, *inst_name, *pg_name;
62057c478bd9Sstevel@tonic-gate 		const char *prop_name;
62067c478bd9Sstevel@tonic-gate 
62077c478bd9Sstevel@tonic-gate 		if (strlen(argv[i]) > max_fmri_length) {
62087c478bd9Sstevel@tonic-gate 			errfunc(scf_get_msg(SCF_MSG_ARGTOOLONG), argv[i]);
62097c478bd9Sstevel@tonic-gate 			if (err != NULL)
62107c478bd9Sstevel@tonic-gate 				*err = UU_EXIT_FATAL;
62117c478bd9Sstevel@tonic-gate 			continue;
62127c478bd9Sstevel@tonic-gate 		}
62137c478bd9Sstevel@tonic-gate 
62147c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri, argv[i]);
62157c478bd9Sstevel@tonic-gate 		if (scf_parse_svc_fmri(fmri, &scope_name, &svc_name, &inst_name,
62167c478bd9Sstevel@tonic-gate 		    &pg_name, &prop_name) != SCF_SUCCESS)
62177c478bd9Sstevel@tonic-gate 			goto badfmri;
62187c478bd9Sstevel@tonic-gate 
62197c478bd9Sstevel@tonic-gate 		/*
62207c478bd9Sstevel@tonic-gate 		 * If the user has specified SCF_WALK_PROPERTY, allow property
62217c478bd9Sstevel@tonic-gate 		 * groups and properties.
62227c478bd9Sstevel@tonic-gate 		 */
62237c478bd9Sstevel@tonic-gate 		if (pg_name != NULL || prop_name != NULL) {
62247c478bd9Sstevel@tonic-gate 			if (!(flags & SCF_WALK_PROPERTY))
62257c478bd9Sstevel@tonic-gate 				goto badfmri;
62267c478bd9Sstevel@tonic-gate 
62277c478bd9Sstevel@tonic-gate 			if (scf_handle_decode_fmri(h, argv[i], NULL, NULL,
62287c478bd9Sstevel@tonic-gate 			    NULL, pg, prop, 0) != 0)
62297c478bd9Sstevel@tonic-gate 				goto badfmri;
62307c478bd9Sstevel@tonic-gate 
62317c478bd9Sstevel@tonic-gate 			if (scf_pg_get_name(pg, NULL, 0) < 0 &&
62327c478bd9Sstevel@tonic-gate 			    scf_property_get_name(prop, NULL, 0) < 0)
62337c478bd9Sstevel@tonic-gate 				goto badfmri;
62347c478bd9Sstevel@tonic-gate 
62357c478bd9Sstevel@tonic-gate 			if (scf_canonify_fmri(argv[i], fmri, max_fmri_length)
62367c478bd9Sstevel@tonic-gate 			    <= 0) {
62377c478bd9Sstevel@tonic-gate 				/*
62387c478bd9Sstevel@tonic-gate 				 * scf_parse_fmri() should have caught this.
62397c478bd9Sstevel@tonic-gate 				 */
62407c478bd9Sstevel@tonic-gate 				abort();
62417c478bd9Sstevel@tonic-gate 			}
62427c478bd9Sstevel@tonic-gate 
62437c478bd9Sstevel@tonic-gate 			if ((ret = scf_add_match(htable, fmri, NULL,
62447c478bd9Sstevel@tonic-gate 			    &pattern[i], flags & SCF_WALK_EXPLICIT)) != 0)
62457c478bd9Sstevel@tonic-gate 				goto error;
62467c478bd9Sstevel@tonic-gate 
62477c478bd9Sstevel@tonic-gate 			if ((pattern[i].sp_arg = strdup(argv[i])) == NULL) {
62487c478bd9Sstevel@tonic-gate 				ret = SCF_ERROR_NO_MEMORY;
62497c478bd9Sstevel@tonic-gate 				goto error;
62507c478bd9Sstevel@tonic-gate 			}
62517c478bd9Sstevel@tonic-gate 			pattern[i].sp_type = PATTERN_EXACT;
62527c478bd9Sstevel@tonic-gate 		}
62537c478bd9Sstevel@tonic-gate 
62547c478bd9Sstevel@tonic-gate 		/*
62557c478bd9Sstevel@tonic-gate 		 * We need at least a service name
62567c478bd9Sstevel@tonic-gate 		 */
62577c478bd9Sstevel@tonic-gate 		if (scope_name == NULL || svc_name == NULL)
62587c478bd9Sstevel@tonic-gate 			goto badfmri;
62597c478bd9Sstevel@tonic-gate 
62607c478bd9Sstevel@tonic-gate 		/*
62617c478bd9Sstevel@tonic-gate 		 * If we have a fully qualified instance, add it to our list of
62627c478bd9Sstevel@tonic-gate 		 * fmris to watch.
62637c478bd9Sstevel@tonic-gate 		 */
62647c478bd9Sstevel@tonic-gate 		if (inst_name != NULL) {
62657c478bd9Sstevel@tonic-gate 			if (flags & SCF_WALK_NOINSTANCE)
62667c478bd9Sstevel@tonic-gate 				goto badfmri;
62677c478bd9Sstevel@tonic-gate 
62687c478bd9Sstevel@tonic-gate 			if (scf_handle_decode_fmri(h, argv[i], NULL, NULL,
62697c478bd9Sstevel@tonic-gate 			    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0)
62707c478bd9Sstevel@tonic-gate 				goto badfmri;
62717c478bd9Sstevel@tonic-gate 
62727c478bd9Sstevel@tonic-gate 			if (scf_canonify_fmri(argv[i], fmri, max_fmri_length)
62737c478bd9Sstevel@tonic-gate 			    <= 0)
62747c478bd9Sstevel@tonic-gate 				goto badfmri;
62757c478bd9Sstevel@tonic-gate 
62767c478bd9Sstevel@tonic-gate 			if ((ret = scf_add_match(htable, fmri, NULL,
62777c478bd9Sstevel@tonic-gate 			    &pattern[i], flags & SCF_WALK_EXPLICIT)) != 0)
62787c478bd9Sstevel@tonic-gate 				goto error;
62797c478bd9Sstevel@tonic-gate 
62807c478bd9Sstevel@tonic-gate 			if ((pattern[i].sp_arg = strdup(argv[i])) == NULL) {
62817c478bd9Sstevel@tonic-gate 				ret = SCF_ERROR_NO_MEMORY;
62827c478bd9Sstevel@tonic-gate 				goto error;
62837c478bd9Sstevel@tonic-gate 			}
62847c478bd9Sstevel@tonic-gate 			pattern[i].sp_type = PATTERN_EXACT;
62857c478bd9Sstevel@tonic-gate 
62867c478bd9Sstevel@tonic-gate 			continue;
62877c478bd9Sstevel@tonic-gate 		}
62887c478bd9Sstevel@tonic-gate 
62897c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, argv[i], NULL, svc,
62907c478bd9Sstevel@tonic-gate 		    NULL, NULL, NULL, SCF_DECODE_FMRI_EXACT) !=
62917c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS)
62927c478bd9Sstevel@tonic-gate 			goto badfmri;
62937c478bd9Sstevel@tonic-gate 
62947c478bd9Sstevel@tonic-gate 		/*
62957c478bd9Sstevel@tonic-gate 		 * If the user allows for bare services, then simply
62967c478bd9Sstevel@tonic-gate 		 * pass this service on.
62977c478bd9Sstevel@tonic-gate 		 */
62987c478bd9Sstevel@tonic-gate 		if (flags & SCF_WALK_SERVICE) {
62997c478bd9Sstevel@tonic-gate 			if (scf_service_to_fmri(svc, fmri,
63007c478bd9Sstevel@tonic-gate 			    max_fmri_length + 1) <= 0) {
63017c478bd9Sstevel@tonic-gate 				ret = scf_error();
63027c478bd9Sstevel@tonic-gate 				goto error;
63037c478bd9Sstevel@tonic-gate 			}
63047c478bd9Sstevel@tonic-gate 
63057c478bd9Sstevel@tonic-gate 			if ((ret = scf_add_match(htable, fmri, NULL,
63067c478bd9Sstevel@tonic-gate 			    &pattern[i], flags & SCF_WALK_EXPLICIT)) != 0)
63077c478bd9Sstevel@tonic-gate 				goto error;
63087c478bd9Sstevel@tonic-gate 
63097c478bd9Sstevel@tonic-gate 			if ((pattern[i].sp_arg = strdup(argv[i]))
63107c478bd9Sstevel@tonic-gate 			    == NULL) {
63117c478bd9Sstevel@tonic-gate 				ret = SCF_ERROR_NO_MEMORY;
63127c478bd9Sstevel@tonic-gate 				goto error;
63137c478bd9Sstevel@tonic-gate 			}
63147c478bd9Sstevel@tonic-gate 			pattern[i].sp_type = PATTERN_EXACT;
63157c478bd9Sstevel@tonic-gate 			continue;
63167c478bd9Sstevel@tonic-gate 		}
63177c478bd9Sstevel@tonic-gate 
63187c478bd9Sstevel@tonic-gate 		if (flags & SCF_WALK_NOINSTANCE)
63197c478bd9Sstevel@tonic-gate 			goto badfmri;
63207c478bd9Sstevel@tonic-gate 
63217c478bd9Sstevel@tonic-gate 		/*
63227c478bd9Sstevel@tonic-gate 		 * Otherwise, iterate over all instances in the service.
63237c478bd9Sstevel@tonic-gate 		 */
63247c478bd9Sstevel@tonic-gate 		if (scf_iter_service_instances(iter, svc) !=
63257c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS) {
63267c478bd9Sstevel@tonic-gate 			ret = scf_error();
63277c478bd9Sstevel@tonic-gate 			goto error;
63287c478bd9Sstevel@tonic-gate 		}
63297c478bd9Sstevel@tonic-gate 
63307c478bd9Sstevel@tonic-gate 		for (;;) {
63317c478bd9Sstevel@tonic-gate 			ret = scf_iter_next_instance(iter, inst);
63327c478bd9Sstevel@tonic-gate 			if (ret == 0)
63337c478bd9Sstevel@tonic-gate 				break;
63347c478bd9Sstevel@tonic-gate 			if (ret != 1) {
63357c478bd9Sstevel@tonic-gate 				ret = scf_error();
63367c478bd9Sstevel@tonic-gate 				goto error;
63377c478bd9Sstevel@tonic-gate 			}
63387c478bd9Sstevel@tonic-gate 
63397c478bd9Sstevel@tonic-gate 			if (scf_instance_to_fmri(inst, fmri,
63407c478bd9Sstevel@tonic-gate 			    max_fmri_length + 1) == -1)
63417c478bd9Sstevel@tonic-gate 				goto badfmri;
63427c478bd9Sstevel@tonic-gate 
63437c478bd9Sstevel@tonic-gate 			if ((ret = scf_add_match(htable, fmri, NULL,
63447c478bd9Sstevel@tonic-gate 			    &pattern[i], flags & SCF_WALK_EXPLICIT)) != 0)
63457c478bd9Sstevel@tonic-gate 				goto error;
63467c478bd9Sstevel@tonic-gate 		}
63477c478bd9Sstevel@tonic-gate 
63487c478bd9Sstevel@tonic-gate 		if ((pattern[i].sp_arg = strdup(argv[i])) == NULL) {
63497c478bd9Sstevel@tonic-gate 			ret = SCF_ERROR_NO_MEMORY;
63507c478bd9Sstevel@tonic-gate 			goto error;
63517c478bd9Sstevel@tonic-gate 		}
63527c478bd9Sstevel@tonic-gate 		pattern[i].sp_type = PATTERN_EXACT;
63537c478bd9Sstevel@tonic-gate 
63547c478bd9Sstevel@tonic-gate 		continue;
63557c478bd9Sstevel@tonic-gate 
63567c478bd9Sstevel@tonic-gate badfmri:
63577c478bd9Sstevel@tonic-gate 
63587c478bd9Sstevel@tonic-gate 		/*
63597c478bd9Sstevel@tonic-gate 		 * If we got here because of a fatal error, bail out
63607c478bd9Sstevel@tonic-gate 		 * immediately.
63617c478bd9Sstevel@tonic-gate 		 */
63627c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) {
63637c478bd9Sstevel@tonic-gate 			ret = scf_error();
63647c478bd9Sstevel@tonic-gate 			goto error;
63657c478bd9Sstevel@tonic-gate 		}
63667c478bd9Sstevel@tonic-gate 
63677c478bd9Sstevel@tonic-gate 		/*
63687c478bd9Sstevel@tonic-gate 		 * At this point we failed to interpret the argument as a
63697c478bd9Sstevel@tonic-gate 		 * complete fmri, so mark it as a partial or globbed FMRI for
63707c478bd9Sstevel@tonic-gate 		 * later processing.
63717c478bd9Sstevel@tonic-gate 		 */
63727c478bd9Sstevel@tonic-gate 		if (strpbrk(argv[i], "*?[") != NULL) {
63737c478bd9Sstevel@tonic-gate 			/*
63747c478bd9Sstevel@tonic-gate 			 * Prepend svc:/ to patterns which don't begin with * or
63757c478bd9Sstevel@tonic-gate 			 * svc: or lrc:.
63767c478bd9Sstevel@tonic-gate 			 */
63777c478bd9Sstevel@tonic-gate 			pattern[i].sp_type = PATTERN_GLOB;
63787c478bd9Sstevel@tonic-gate 			if (argv[i][0] == '*' ||
63797c478bd9Sstevel@tonic-gate 			    (strlen(argv[i]) >= 4 && argv[i][3] == ':'))
63807c478bd9Sstevel@tonic-gate 				pattern[i].sp_arg = strdup(argv[i]);
63817c478bd9Sstevel@tonic-gate 			else {
63827c478bd9Sstevel@tonic-gate 				pattern[i].sp_arg = malloc(strlen(argv[i]) + 6);
63837c478bd9Sstevel@tonic-gate 				if (pattern[i].sp_arg != NULL)
63847c478bd9Sstevel@tonic-gate 					(void) snprintf(pattern[i].sp_arg,
63857c478bd9Sstevel@tonic-gate 					    strlen(argv[i]) + 6, "svc:/%s",
63867c478bd9Sstevel@tonic-gate 					    argv[i]);
63877c478bd9Sstevel@tonic-gate 			}
63887c478bd9Sstevel@tonic-gate 		} else {
63897c478bd9Sstevel@tonic-gate 			pattern[i].sp_type = PATTERN_PARTIAL;
63907c478bd9Sstevel@tonic-gate 			pattern[i].sp_arg = strdup(argv[i]);
63917c478bd9Sstevel@tonic-gate 		}
63927c478bd9Sstevel@tonic-gate 		pattern_search = 1;
63937c478bd9Sstevel@tonic-gate 		if (pattern[i].sp_arg == NULL) {
63947c478bd9Sstevel@tonic-gate 			ret = SCF_ERROR_NO_MEMORY;
63957c478bd9Sstevel@tonic-gate 			goto error;
63967c478bd9Sstevel@tonic-gate 		}
63977c478bd9Sstevel@tonic-gate 	}
63987c478bd9Sstevel@tonic-gate 
63997c478bd9Sstevel@tonic-gate 	if (pattern_search || argc == 0) {
64007c478bd9Sstevel@tonic-gate 		/*
64017c478bd9Sstevel@tonic-gate 		 * We have a set of patterns to search for.  Iterate over all
64027c478bd9Sstevel@tonic-gate 		 * instances and legacy services searching for matches.
64037c478bd9Sstevel@tonic-gate 		 */
64047c478bd9Sstevel@tonic-gate 		if (scf_handle_get_local_scope(h, scope) != 0) {
64057c478bd9Sstevel@tonic-gate 			ret = scf_error();
64067c478bd9Sstevel@tonic-gate 			goto error;
64077c478bd9Sstevel@tonic-gate 		}
64087c478bd9Sstevel@tonic-gate 
64097c478bd9Sstevel@tonic-gate 		if (scf_iter_scope_services(sciter, scope) != 0) {
64107c478bd9Sstevel@tonic-gate 			ret = scf_error();
64117c478bd9Sstevel@tonic-gate 			goto error;
64127c478bd9Sstevel@tonic-gate 		}
64137c478bd9Sstevel@tonic-gate 
64147c478bd9Sstevel@tonic-gate 		for (;;) {
64157c478bd9Sstevel@tonic-gate 			ret = scf_iter_next_service(sciter, svc);
64167c478bd9Sstevel@tonic-gate 			if (ret == 0)
64177c478bd9Sstevel@tonic-gate 				break;
64187c478bd9Sstevel@tonic-gate 			if (ret != 1) {
64197c478bd9Sstevel@tonic-gate 				ret = scf_error();
64207c478bd9Sstevel@tonic-gate 				goto error;
64217c478bd9Sstevel@tonic-gate 			}
64227c478bd9Sstevel@tonic-gate 
64237c478bd9Sstevel@tonic-gate 			if (flags & SCF_WALK_SERVICE) {
64247c478bd9Sstevel@tonic-gate 				/*
64257c478bd9Sstevel@tonic-gate 				 * If the user is requesting bare services, try
64267c478bd9Sstevel@tonic-gate 				 * to match the service first.
64277c478bd9Sstevel@tonic-gate 				 */
64287c478bd9Sstevel@tonic-gate 				if (scf_service_to_fmri(svc, fmri,
64297c478bd9Sstevel@tonic-gate 				    max_fmri_length + 1) < 0) {
64307c478bd9Sstevel@tonic-gate 					ret = scf_error();
64317c478bd9Sstevel@tonic-gate 					goto error;
64327c478bd9Sstevel@tonic-gate 				}
64337c478bd9Sstevel@tonic-gate 
64347c478bd9Sstevel@tonic-gate 				if (argc == 0) {
64357c478bd9Sstevel@tonic-gate 					info.fmri = fmri;
64367c478bd9Sstevel@tonic-gate 					info.scope = scope;
64377c478bd9Sstevel@tonic-gate 					info.svc = svc;
64387c478bd9Sstevel@tonic-gate 					info.inst = NULL;
64397c478bd9Sstevel@tonic-gate 					info.pg = NULL;
64407c478bd9Sstevel@tonic-gate 					info.prop = NULL;
64417c478bd9Sstevel@tonic-gate 					if ((ret = callback(data, &info)) != 0)
64427c478bd9Sstevel@tonic-gate 						goto error;
64437c478bd9Sstevel@tonic-gate 					continue;
64447c478bd9Sstevel@tonic-gate 				} else if ((ret = scf_pattern_match(htable,
64457c478bd9Sstevel@tonic-gate 				    fmri, NULL, argc, pattern,
64467c478bd9Sstevel@tonic-gate 				    flags & SCF_WALK_EXPLICIT)) != 0) {
64477c478bd9Sstevel@tonic-gate 					goto error;
64487c478bd9Sstevel@tonic-gate 				}
64497c478bd9Sstevel@tonic-gate 			}
64507c478bd9Sstevel@tonic-gate 
64517c478bd9Sstevel@tonic-gate 			if (flags & SCF_WALK_NOINSTANCE)
64527c478bd9Sstevel@tonic-gate 				continue;
64537c478bd9Sstevel@tonic-gate 
64547c478bd9Sstevel@tonic-gate 			/*
64557c478bd9Sstevel@tonic-gate 			 * Iterate over all instances in the service.
64567c478bd9Sstevel@tonic-gate 			 */
64577c478bd9Sstevel@tonic-gate 			if (scf_iter_service_instances(siter, svc) != 0) {
64587c478bd9Sstevel@tonic-gate 				if (scf_error() != SCF_ERROR_DELETED) {
64597c478bd9Sstevel@tonic-gate 					ret = scf_error();
64607c478bd9Sstevel@tonic-gate 					goto error;
64617c478bd9Sstevel@tonic-gate 				}
64627c478bd9Sstevel@tonic-gate 				continue;
64637c478bd9Sstevel@tonic-gate 			}
64647c478bd9Sstevel@tonic-gate 
64657c478bd9Sstevel@tonic-gate 			for (;;) {
64667c478bd9Sstevel@tonic-gate 				ret = scf_iter_next_instance(siter, inst);
64677c478bd9Sstevel@tonic-gate 				if (ret == 0)
64687c478bd9Sstevel@tonic-gate 					break;
64697c478bd9Sstevel@tonic-gate 				if (ret != 1) {
64707c478bd9Sstevel@tonic-gate 					if (scf_error() != SCF_ERROR_DELETED) {
64717c478bd9Sstevel@tonic-gate 						ret = scf_error();
64727c478bd9Sstevel@tonic-gate 						goto error;
64737c478bd9Sstevel@tonic-gate 					}
64747c478bd9Sstevel@tonic-gate 					break;
64757c478bd9Sstevel@tonic-gate 				}
64767c478bd9Sstevel@tonic-gate 
64777c478bd9Sstevel@tonic-gate 				if (scf_instance_to_fmri(inst, fmri,
64787c478bd9Sstevel@tonic-gate 				    max_fmri_length + 1) < 0) {
64797c478bd9Sstevel@tonic-gate 					ret = scf_error();
64807c478bd9Sstevel@tonic-gate 					goto error;
64817c478bd9Sstevel@tonic-gate 				}
64827c478bd9Sstevel@tonic-gate 
64837c478bd9Sstevel@tonic-gate 				/*
64847c478bd9Sstevel@tonic-gate 				 * Without arguments, execute the callback
64857c478bd9Sstevel@tonic-gate 				 * immediately.
64867c478bd9Sstevel@tonic-gate 				 */
64877c478bd9Sstevel@tonic-gate 				if (argc == 0) {
64887c478bd9Sstevel@tonic-gate 					info.fmri = fmri;
64897c478bd9Sstevel@tonic-gate 					info.scope = scope;
64907c478bd9Sstevel@tonic-gate 					info.svc = svc;
64917c478bd9Sstevel@tonic-gate 					info.inst = inst;
64927c478bd9Sstevel@tonic-gate 					info.pg = NULL;
64937c478bd9Sstevel@tonic-gate 					info.prop = NULL;
64947c478bd9Sstevel@tonic-gate 					if ((ret = callback(data, &info)) != 0)
64957c478bd9Sstevel@tonic-gate 						goto error;
64967c478bd9Sstevel@tonic-gate 				} else if ((ret = scf_pattern_match(htable,
64977c478bd9Sstevel@tonic-gate 				    fmri, NULL, argc, pattern,
64987c478bd9Sstevel@tonic-gate 				    flags & SCF_WALK_EXPLICIT)) != 0) {
64997c478bd9Sstevel@tonic-gate 					goto error;
65007c478bd9Sstevel@tonic-gate 				}
65017c478bd9Sstevel@tonic-gate 			}
65027c478bd9Sstevel@tonic-gate 		}
65037c478bd9Sstevel@tonic-gate 
65047c478bd9Sstevel@tonic-gate 		/*
65057c478bd9Sstevel@tonic-gate 		 * Search legacy services
65067c478bd9Sstevel@tonic-gate 		 */
65077c478bd9Sstevel@tonic-gate 		if ((flags & SCF_WALK_LEGACY)) {
65087c478bd9Sstevel@tonic-gate 			if (scf_scope_get_service(scope, SCF_LEGACY_SERVICE,
65097c478bd9Sstevel@tonic-gate 			    svc) != 0) {
65107c478bd9Sstevel@tonic-gate 				if (scf_error() != SCF_ERROR_NOT_FOUND) {
65117c478bd9Sstevel@tonic-gate 					ret = scf_error();
65127c478bd9Sstevel@tonic-gate 					goto error;
65137c478bd9Sstevel@tonic-gate 				}
65147c478bd9Sstevel@tonic-gate 
65157c478bd9Sstevel@tonic-gate 				goto nolegacy;
65167c478bd9Sstevel@tonic-gate 			}
65177c478bd9Sstevel@tonic-gate 
65187c478bd9Sstevel@tonic-gate 			if (scf_iter_service_pgs_typed(iter, svc,
65197c478bd9Sstevel@tonic-gate 			    SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) {
65207c478bd9Sstevel@tonic-gate 				ret = scf_error();
65217c478bd9Sstevel@tonic-gate 				goto error;
65227c478bd9Sstevel@tonic-gate 			}
65237c478bd9Sstevel@tonic-gate 
65247c478bd9Sstevel@tonic-gate 			(void) strcpy(fmri, LEGACY_SCHEME);
65257c478bd9Sstevel@tonic-gate 
65267c478bd9Sstevel@tonic-gate 			for (;;) {
65277c478bd9Sstevel@tonic-gate 				ret = scf_iter_next_pg(iter, pg);
65287c478bd9Sstevel@tonic-gate 				if (ret == -1) {
65297c478bd9Sstevel@tonic-gate 					ret = scf_error();
65307c478bd9Sstevel@tonic-gate 					goto error;
65317c478bd9Sstevel@tonic-gate 				}
65327c478bd9Sstevel@tonic-gate 				if (ret == 0)
65337c478bd9Sstevel@tonic-gate 					break;
65347c478bd9Sstevel@tonic-gate 
65357c478bd9Sstevel@tonic-gate 				if (scf_pg_get_property(pg,
65367c478bd9Sstevel@tonic-gate 				    SCF_LEGACY_PROPERTY_NAME, prop) == -1) {
65377c478bd9Sstevel@tonic-gate 					ret = scf_error();
65387c478bd9Sstevel@tonic-gate 					if (ret == SCF_ERROR_DELETED ||
65397c478bd9Sstevel@tonic-gate 					    ret == SCF_ERROR_NOT_FOUND) {
65407c478bd9Sstevel@tonic-gate 						ret = 0;
65417c478bd9Sstevel@tonic-gate 						continue;
65427c478bd9Sstevel@tonic-gate 					}
65437c478bd9Sstevel@tonic-gate 					goto error;
65447c478bd9Sstevel@tonic-gate 				}
65457c478bd9Sstevel@tonic-gate 
65467c478bd9Sstevel@tonic-gate 				if (scf_property_is_type(prop, SCF_TYPE_ASTRING)
65477c478bd9Sstevel@tonic-gate 				    != SCF_SUCCESS) {
65487c478bd9Sstevel@tonic-gate 					if (scf_error() == SCF_ERROR_DELETED)
65497c478bd9Sstevel@tonic-gate 						continue;
65507c478bd9Sstevel@tonic-gate 					ret = scf_error();
65517c478bd9Sstevel@tonic-gate 					goto error;
65527c478bd9Sstevel@tonic-gate 				}
65537c478bd9Sstevel@tonic-gate 
65547c478bd9Sstevel@tonic-gate 				if (scf_property_get_value(prop, value) !=
65557c478bd9Sstevel@tonic-gate 				    SCF_SUCCESS)
65567c478bd9Sstevel@tonic-gate 					continue;
65577c478bd9Sstevel@tonic-gate 
65587c478bd9Sstevel@tonic-gate 				if (scf_value_get_astring(value,
65597c478bd9Sstevel@tonic-gate 				    fmri + sizeof (LEGACY_SCHEME) - 1,
65607c478bd9Sstevel@tonic-gate 				    max_fmri_length + 2 -
65617c478bd9Sstevel@tonic-gate 				    sizeof (LEGACY_SCHEME)) <= 0)
65627c478bd9Sstevel@tonic-gate 					continue;
65637c478bd9Sstevel@tonic-gate 
65647c478bd9Sstevel@tonic-gate 				if (scf_pg_get_name(pg, pgname,
65657c478bd9Sstevel@tonic-gate 				    max_name_length + 1) <= 0) {
65667c478bd9Sstevel@tonic-gate 					if (scf_error() == SCF_ERROR_DELETED)
65677c478bd9Sstevel@tonic-gate 						continue;
65687c478bd9Sstevel@tonic-gate 					ret = scf_error();
65697c478bd9Sstevel@tonic-gate 					goto error;
65707c478bd9Sstevel@tonic-gate 				}
65717c478bd9Sstevel@tonic-gate 
65727c478bd9Sstevel@tonic-gate 				if (argc == 0) {
65737c478bd9Sstevel@tonic-gate 					info.fmri = fmri;
65747c478bd9Sstevel@tonic-gate 					info.scope = scope;
65757c478bd9Sstevel@tonic-gate 					info.svc = NULL;
65767c478bd9Sstevel@tonic-gate 					info.inst = NULL;
65777c478bd9Sstevel@tonic-gate 					info.pg = pg;
65787c478bd9Sstevel@tonic-gate 					info.prop = NULL;
65797c478bd9Sstevel@tonic-gate 					if ((ret = callback(data, &info)) != 0)
65807c478bd9Sstevel@tonic-gate 						goto error;
65817c478bd9Sstevel@tonic-gate 				} else if ((ret = scf_pattern_match(htable,
65827c478bd9Sstevel@tonic-gate 				    fmri, pgname, argc, pattern,
65837c478bd9Sstevel@tonic-gate 				    flags & SCF_WALK_EXPLICIT)) != 0)
65847c478bd9Sstevel@tonic-gate 					goto error;
65857c478bd9Sstevel@tonic-gate 			}
65867c478bd9Sstevel@tonic-gate 
65877c478bd9Sstevel@tonic-gate 		}
65887c478bd9Sstevel@tonic-gate 	}
65897c478bd9Sstevel@tonic-gate nolegacy:
65907c478bd9Sstevel@tonic-gate 	ret = 0;
65917c478bd9Sstevel@tonic-gate 
65927c478bd9Sstevel@tonic-gate 	if (argc == 0)
65937c478bd9Sstevel@tonic-gate 		goto error;
65947c478bd9Sstevel@tonic-gate 
65957c478bd9Sstevel@tonic-gate 	/*
65967c478bd9Sstevel@tonic-gate 	 * Check all patterns, and see if we have that any that didn't match
65977c478bd9Sstevel@tonic-gate 	 * or any that matched multiple instances.  For svcprop, add up the
65987c478bd9Sstevel@tonic-gate 	 * total number of matching keys.
65997c478bd9Sstevel@tonic-gate 	 */
66007c478bd9Sstevel@tonic-gate 	info.count = 0;
66017c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
66027c478bd9Sstevel@tonic-gate 		scf_match_t *match;
66037c478bd9Sstevel@tonic-gate 
66047c478bd9Sstevel@tonic-gate 		if (pattern[i].sp_type == PATTERN_INVALID)
66057c478bd9Sstevel@tonic-gate 			continue;
66067c478bd9Sstevel@tonic-gate 		if (pattern[i].sp_matchcount == 0) {
66077c478bd9Sstevel@tonic-gate 			scf_msg_t msgid;
66087c478bd9Sstevel@tonic-gate 			/*
66097c478bd9Sstevel@tonic-gate 			 * Provide a useful error message based on the argument
66107c478bd9Sstevel@tonic-gate 			 * and the type of entity requested.
66117c478bd9Sstevel@tonic-gate 			 */
66127c478bd9Sstevel@tonic-gate 			if (!(flags & SCF_WALK_LEGACY) &&
66137c478bd9Sstevel@tonic-gate 			    strncmp(pattern[i].sp_arg, "lrc:/", 5) == 0)
66147c478bd9Sstevel@tonic-gate 				msgid = SCF_MSG_PATTERN_LEGACY;
66157c478bd9Sstevel@tonic-gate 			else if (flags & SCF_WALK_PROPERTY)
66167c478bd9Sstevel@tonic-gate 				msgid = SCF_MSG_PATTERN_NOENTITY;
66177c478bd9Sstevel@tonic-gate 			else if (flags & SCF_WALK_NOINSTANCE)
66187c478bd9Sstevel@tonic-gate 				msgid = SCF_MSG_PATTERN_NOSERVICE;
66197c478bd9Sstevel@tonic-gate 			else if (flags & SCF_WALK_SERVICE)
66207c478bd9Sstevel@tonic-gate 				msgid = SCF_MSG_PATTERN_NOINSTSVC;
66217c478bd9Sstevel@tonic-gate 			else
66227c478bd9Sstevel@tonic-gate 				msgid = SCF_MSG_PATTERN_NOINSTANCE;
66237c478bd9Sstevel@tonic-gate 
66247c478bd9Sstevel@tonic-gate 			errfunc(scf_get_msg(msgid), pattern[i].sp_arg);
66257c478bd9Sstevel@tonic-gate 			if (err)
66267c478bd9Sstevel@tonic-gate 				*err = UU_EXIT_FATAL;
66277c478bd9Sstevel@tonic-gate 		} else if (!(flags & SCF_WALK_MULTIPLE) &&
66287c478bd9Sstevel@tonic-gate 		    pattern[i].sp_matchcount > 1) {
66297c478bd9Sstevel@tonic-gate 			size_t len, off;
66307c478bd9Sstevel@tonic-gate 			char *msg;
66317c478bd9Sstevel@tonic-gate 
66327c478bd9Sstevel@tonic-gate 			/*
66337c478bd9Sstevel@tonic-gate 			 * Construct a message with all possible FMRIs before
66347c478bd9Sstevel@tonic-gate 			 * passing off to error handling function.
66357c478bd9Sstevel@tonic-gate 			 *
66367c478bd9Sstevel@tonic-gate 			 * Note that strlen(scf_get_msg(...)) includes the
66377c478bd9Sstevel@tonic-gate 			 * length of '%s', which accounts for the terminating
66387c478bd9Sstevel@tonic-gate 			 * null byte.
66397c478bd9Sstevel@tonic-gate 			 */
66407c478bd9Sstevel@tonic-gate 			len = strlen(scf_get_msg(SCF_MSG_PATTERN_MULTIMATCH)) +
66417c478bd9Sstevel@tonic-gate 			    strlen(pattern[i].sp_arg);
66427c478bd9Sstevel@tonic-gate 			for (match = pattern[i].sp_matches; match != NULL;
66437c478bd9Sstevel@tonic-gate 			    match = match->sm_next) {
66447c478bd9Sstevel@tonic-gate 				len += strlen(match->sm_key->sk_fmri) + 2;
66457c478bd9Sstevel@tonic-gate 			}
66467c478bd9Sstevel@tonic-gate 			if ((msg = malloc(len)) == NULL) {
66477c478bd9Sstevel@tonic-gate 				ret = SCF_ERROR_NO_MEMORY;
66487c478bd9Sstevel@tonic-gate 				goto error;
66497c478bd9Sstevel@tonic-gate 			}
66507c478bd9Sstevel@tonic-gate 
66517c478bd9Sstevel@tonic-gate 			/* LINTED - format argument */
66527c478bd9Sstevel@tonic-gate 			(void) snprintf(msg, len,
66537c478bd9Sstevel@tonic-gate 			    scf_get_msg(SCF_MSG_PATTERN_MULTIMATCH),
66547c478bd9Sstevel@tonic-gate 			    pattern[i].sp_arg);
66557c478bd9Sstevel@tonic-gate 			off = strlen(msg);
66567c478bd9Sstevel@tonic-gate 			for (match = pattern[i].sp_matches; match != NULL;
66577c478bd9Sstevel@tonic-gate 			    match = match->sm_next) {
66587c478bd9Sstevel@tonic-gate 				off += snprintf(msg + off, len - off, "\t%s\n",
66597c478bd9Sstevel@tonic-gate 				    match->sm_key->sk_fmri);
66607c478bd9Sstevel@tonic-gate 			}
66617c478bd9Sstevel@tonic-gate 
66627c478bd9Sstevel@tonic-gate 			errfunc(msg);
66637c478bd9Sstevel@tonic-gate 			if (err != NULL)
66647c478bd9Sstevel@tonic-gate 				*err = UU_EXIT_FATAL;
66657c478bd9Sstevel@tonic-gate 
66667c478bd9Sstevel@tonic-gate 			free(msg);
66677c478bd9Sstevel@tonic-gate 		} else {
66687c478bd9Sstevel@tonic-gate 			for (match = pattern[i].sp_matches; match != NULL;
66697c478bd9Sstevel@tonic-gate 			    match = match->sm_next) {
66707c478bd9Sstevel@tonic-gate 				if (!match->sm_key->sk_seen)
66717c478bd9Sstevel@tonic-gate 					info.count++;
66727c478bd9Sstevel@tonic-gate 				match->sm_key->sk_seen = 1;
66737c478bd9Sstevel@tonic-gate 			}
66747c478bd9Sstevel@tonic-gate 		}
66757c478bd9Sstevel@tonic-gate 	}
66767c478bd9Sstevel@tonic-gate 
66777c478bd9Sstevel@tonic-gate 	/*
66787c478bd9Sstevel@tonic-gate 	 * Clear 'sk_seen' for all keys.
66797c478bd9Sstevel@tonic-gate 	 */
66807c478bd9Sstevel@tonic-gate 	for (i = 0; i < WALK_HTABLE_SIZE; i++) {
66817c478bd9Sstevel@tonic-gate 		scf_matchkey_t *key;
66827c478bd9Sstevel@tonic-gate 		for (key = htable[i]; key != NULL; key = key->sk_next)
66837c478bd9Sstevel@tonic-gate 			key->sk_seen = 0;
66847c478bd9Sstevel@tonic-gate 	}
66857c478bd9Sstevel@tonic-gate 
66867c478bd9Sstevel@tonic-gate 	/*
66877c478bd9Sstevel@tonic-gate 	 * Iterate over all the FMRIs in our hash table and execute the
66887c478bd9Sstevel@tonic-gate 	 * callback.
66897c478bd9Sstevel@tonic-gate 	 */
66907c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++) {
66917c478bd9Sstevel@tonic-gate 		scf_match_t *match;
66927c478bd9Sstevel@tonic-gate 		scf_matchkey_t *key;
66937c478bd9Sstevel@tonic-gate 
66947c478bd9Sstevel@tonic-gate 		/*
66957c478bd9Sstevel@tonic-gate 		 * Ignore patterns which didn't match anything or matched too
66967c478bd9Sstevel@tonic-gate 		 * many FMRIs.
66977c478bd9Sstevel@tonic-gate 		 */
66987c478bd9Sstevel@tonic-gate 		if (pattern[i].sp_matchcount == 0 ||
66997c478bd9Sstevel@tonic-gate 		    (!(flags & SCF_WALK_MULTIPLE) &&
67007c478bd9Sstevel@tonic-gate 		    pattern[i].sp_matchcount > 1))
67017c478bd9Sstevel@tonic-gate 			continue;
67027c478bd9Sstevel@tonic-gate 
67037c478bd9Sstevel@tonic-gate 		for (match = pattern[i].sp_matches; match != NULL;
67047c478bd9Sstevel@tonic-gate 		    match = match->sm_next) {
67057c478bd9Sstevel@tonic-gate 
67067c478bd9Sstevel@tonic-gate 			key = match->sm_key;
67077c478bd9Sstevel@tonic-gate 			if (key->sk_seen)
67087c478bd9Sstevel@tonic-gate 				continue;
67097c478bd9Sstevel@tonic-gate 
67107c478bd9Sstevel@tonic-gate 			key->sk_seen = 1;
67117c478bd9Sstevel@tonic-gate 
67127c478bd9Sstevel@tonic-gate 			if (key->sk_legacy != NULL) {
67137c478bd9Sstevel@tonic-gate 				if (scf_scope_get_service(scope,
67147c478bd9Sstevel@tonic-gate 				    "smf/legacy_run", svc) != 0) {
67157c478bd9Sstevel@tonic-gate 					ret = scf_error();
67167c478bd9Sstevel@tonic-gate 					goto error;
67177c478bd9Sstevel@tonic-gate 				}
67187c478bd9Sstevel@tonic-gate 
67197c478bd9Sstevel@tonic-gate 				if (scf_service_get_pg(svc, key->sk_legacy,
67207c478bd9Sstevel@tonic-gate 				    pg) != 0)
67217c478bd9Sstevel@tonic-gate 					continue;
67227c478bd9Sstevel@tonic-gate 
67237c478bd9Sstevel@tonic-gate 				info.fmri = key->sk_fmri;
67247c478bd9Sstevel@tonic-gate 				info.scope = scope;
67257c478bd9Sstevel@tonic-gate 				info.svc = NULL;
67267c478bd9Sstevel@tonic-gate 				info.inst = NULL;
67277c478bd9Sstevel@tonic-gate 				info.pg = pg;
67287c478bd9Sstevel@tonic-gate 				info.prop = NULL;
67297c478bd9Sstevel@tonic-gate 				if ((ret = callback(data, &info)) != 0)
67307c478bd9Sstevel@tonic-gate 					goto error;
67317c478bd9Sstevel@tonic-gate 			} else {
67327c478bd9Sstevel@tonic-gate 				if (scf_handle_decode_fmri(h, key->sk_fmri,
67337c478bd9Sstevel@tonic-gate 				    scope, svc, inst, pg, prop, 0) !=
67347c478bd9Sstevel@tonic-gate 				    SCF_SUCCESS)
67357c478bd9Sstevel@tonic-gate 					continue;
67367c478bd9Sstevel@tonic-gate 
67377c478bd9Sstevel@tonic-gate 				info.fmri = key->sk_fmri;
67387c478bd9Sstevel@tonic-gate 				info.scope = scope;
67397c478bd9Sstevel@tonic-gate 				info.svc = svc;
67407c478bd9Sstevel@tonic-gate 				if (scf_instance_get_name(inst, NULL, 0) < 0) {
67417c478bd9Sstevel@tonic-gate 					if (scf_error() ==
67427c478bd9Sstevel@tonic-gate 					    SCF_ERROR_CONNECTION_BROKEN) {
67437c478bd9Sstevel@tonic-gate 						ret = scf_error();
67447c478bd9Sstevel@tonic-gate 						goto error;
67457c478bd9Sstevel@tonic-gate 					}
67467c478bd9Sstevel@tonic-gate 					info.inst = NULL;
67477c478bd9Sstevel@tonic-gate 				} else {
67487c478bd9Sstevel@tonic-gate 					info.inst = inst;
67497c478bd9Sstevel@tonic-gate 				}
67507c478bd9Sstevel@tonic-gate 				if (scf_pg_get_name(pg, NULL, 0) < 0) {
67517c478bd9Sstevel@tonic-gate 					if (scf_error() ==
67527c478bd9Sstevel@tonic-gate 					    SCF_ERROR_CONNECTION_BROKEN) {
67537c478bd9Sstevel@tonic-gate 						ret = scf_error();
67547c478bd9Sstevel@tonic-gate 						goto error;
67557c478bd9Sstevel@tonic-gate 					}
67567c478bd9Sstevel@tonic-gate 					info.pg = NULL;
67577c478bd9Sstevel@tonic-gate 				} else {
67587c478bd9Sstevel@tonic-gate 					info.pg = pg;
67597c478bd9Sstevel@tonic-gate 				}
67607c478bd9Sstevel@tonic-gate 				if (scf_property_get_name(prop, NULL, 0) < 0) {
67617c478bd9Sstevel@tonic-gate 					if (scf_error() ==
67627c478bd9Sstevel@tonic-gate 					    SCF_ERROR_CONNECTION_BROKEN) {
67637c478bd9Sstevel@tonic-gate 						ret = scf_error();
67647c478bd9Sstevel@tonic-gate 						goto error;
67657c478bd9Sstevel@tonic-gate 					}
67667c478bd9Sstevel@tonic-gate 					info.prop = NULL;
67677c478bd9Sstevel@tonic-gate 				} else {
67687c478bd9Sstevel@tonic-gate 					info.prop = prop;
67697c478bd9Sstevel@tonic-gate 				}
67707c478bd9Sstevel@tonic-gate 
67717c478bd9Sstevel@tonic-gate 				if ((ret = callback(data, &info)) != 0)
67727c478bd9Sstevel@tonic-gate 					goto error;
67737c478bd9Sstevel@tonic-gate 			}
67747c478bd9Sstevel@tonic-gate 		}
67757c478bd9Sstevel@tonic-gate 	}
67767c478bd9Sstevel@tonic-gate 
67777c478bd9Sstevel@tonic-gate error:
67787c478bd9Sstevel@tonic-gate 	if (htable) {
67797c478bd9Sstevel@tonic-gate 		scf_matchkey_t *key, *next;
67807c478bd9Sstevel@tonic-gate 
67817c478bd9Sstevel@tonic-gate 		for (i = 0; i < WALK_HTABLE_SIZE; i++) {
67827c478bd9Sstevel@tonic-gate 
67837c478bd9Sstevel@tonic-gate 			for (key = htable[i]; key != NULL;
67847c478bd9Sstevel@tonic-gate 			    key = next) {
67857c478bd9Sstevel@tonic-gate 
67867c478bd9Sstevel@tonic-gate 				next = key->sk_next;
67877c478bd9Sstevel@tonic-gate 
67887c478bd9Sstevel@tonic-gate 				if (key->sk_fmri != NULL)
67897c478bd9Sstevel@tonic-gate 					free(key->sk_fmri);
67907c478bd9Sstevel@tonic-gate 				if (key->sk_legacy != NULL)
67917c478bd9Sstevel@tonic-gate 					free(key->sk_legacy);
67927c478bd9Sstevel@tonic-gate 				free(key);
67937c478bd9Sstevel@tonic-gate 			}
67947c478bd9Sstevel@tonic-gate 		}
67957c478bd9Sstevel@tonic-gate 		free(htable);
67967c478bd9Sstevel@tonic-gate 	}
67977c478bd9Sstevel@tonic-gate 	if (pattern != NULL) {
67987c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
67997c478bd9Sstevel@tonic-gate 			scf_match_t *match, *next;
68007c478bd9Sstevel@tonic-gate 
68017c478bd9Sstevel@tonic-gate 			if (pattern[i].sp_arg != NULL)
68027c478bd9Sstevel@tonic-gate 				free(pattern[i].sp_arg);
68037c478bd9Sstevel@tonic-gate 
68047c478bd9Sstevel@tonic-gate 			for (match = pattern[i].sp_matches; match != NULL;
68057c478bd9Sstevel@tonic-gate 			    match = next) {
68067c478bd9Sstevel@tonic-gate 
68077c478bd9Sstevel@tonic-gate 				next = match->sm_next;
68087c478bd9Sstevel@tonic-gate 
68097c478bd9Sstevel@tonic-gate 				free(match);
68107c478bd9Sstevel@tonic-gate 			}
68117c478bd9Sstevel@tonic-gate 		}
68127c478bd9Sstevel@tonic-gate 		free(pattern);
68137c478bd9Sstevel@tonic-gate 	}
68147c478bd9Sstevel@tonic-gate 
68157c478bd9Sstevel@tonic-gate 	free(fmri);
68167c478bd9Sstevel@tonic-gate 	free(pgname);
68177c478bd9Sstevel@tonic-gate 
68187c478bd9Sstevel@tonic-gate 	scf_value_destroy(value);
68197c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
68207c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
68217c478bd9Sstevel@tonic-gate 	scf_scope_destroy(scope);
68227c478bd9Sstevel@tonic-gate 	scf_iter_destroy(siter);
68237c478bd9Sstevel@tonic-gate 	scf_iter_destroy(sciter);
68247c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
68257c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
68267c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
68277c478bd9Sstevel@tonic-gate 
68287c478bd9Sstevel@tonic-gate 	return (ret);
68297c478bd9Sstevel@tonic-gate }
68307c478bd9Sstevel@tonic-gate 
68317c478bd9Sstevel@tonic-gate /*
68327c478bd9Sstevel@tonic-gate  * _scf_request_backup:  a simple wrapper routine
68337c478bd9Sstevel@tonic-gate  */
68347c478bd9Sstevel@tonic-gate int
68357c478bd9Sstevel@tonic-gate _scf_request_backup(scf_handle_t *h, const char *name)
68367c478bd9Sstevel@tonic-gate {
68377c478bd9Sstevel@tonic-gate 	struct rep_protocol_backup_request request;
68387c478bd9Sstevel@tonic-gate 	struct rep_protocol_response response;
68397c478bd9Sstevel@tonic-gate 
68407c478bd9Sstevel@tonic-gate 	int r;
68417c478bd9Sstevel@tonic-gate 
68427c478bd9Sstevel@tonic-gate 	if (strlcpy(request.rpr_name, name, sizeof (request.rpr_name)) >=
68437c478bd9Sstevel@tonic-gate 	    sizeof (request.rpr_name))
68447c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
68457c478bd9Sstevel@tonic-gate 
68467c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&h->rh_lock);
68477c478bd9Sstevel@tonic-gate 	request.rpr_request = REP_PROTOCOL_BACKUP;
68488918dff3Sjwadams 	request.rpr_changeid = handle_next_changeid(h);
68497c478bd9Sstevel@tonic-gate 
68507c478bd9Sstevel@tonic-gate 	r = make_door_call(h, &request, sizeof (request),
68517c478bd9Sstevel@tonic-gate 	    &response, sizeof (response));
68527c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&h->rh_lock);
68537c478bd9Sstevel@tonic-gate 
68547c478bd9Sstevel@tonic-gate 	if (r < 0) {
68557c478bd9Sstevel@tonic-gate 		DOOR_ERRORS_BLOCK(r);
68567c478bd9Sstevel@tonic-gate 	}
68577c478bd9Sstevel@tonic-gate 
68587c478bd9Sstevel@tonic-gate 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
68597c478bd9Sstevel@tonic-gate 		return (scf_set_error(proto_error(response.rpr_response)));
68607c478bd9Sstevel@tonic-gate 	return (SCF_SUCCESS);
68617c478bd9Sstevel@tonic-gate }
68623eae19d9Swesolows 
68633eae19d9Swesolows int
68643eae19d9Swesolows _scf_pg_is_read_protected(const scf_propertygroup_t *pg, boolean_t *out)
68653eae19d9Swesolows {
68663eae19d9Swesolows 	char buf[REP_PROTOCOL_NAME_LEN];
68673eae19d9Swesolows 	ssize_t res;
68683eae19d9Swesolows 
68693eae19d9Swesolows 	res = datael_get_name(&pg->rd_d, buf, sizeof (buf),
68703eae19d9Swesolows 	    RP_ENTITY_NAME_PGREADPROT);
68713eae19d9Swesolows 
68723eae19d9Swesolows 	if (res == -1)
68733eae19d9Swesolows 		return (-1);
68743eae19d9Swesolows 
68753eae19d9Swesolows 	if (uu_strtouint(buf, out, sizeof (*out), 0, 0, 1) == -1)
68763eae19d9Swesolows 		return (scf_set_error(SCF_ERROR_INTERNAL));
68773eae19d9Swesolows 	return (SCF_SUCCESS);
68783eae19d9Swesolows }
68795b7f77adStw 
68805b7f77adStw /*
68815b7f77adStw  * _scf_set_annotation: a wrapper to set the annotation fields for SMF
68825b7f77adStw  * security auditing.
68835b7f77adStw  *
68845b7f77adStw  * Fails with following in scf_error_key thread specific data:
68855b7f77adStw  *	_INVALID_ARGUMENT - operation or file too large
68865b7f77adStw  *	_NOT_BOUND
68875b7f77adStw  *	_CONNECTION_BROKEN
68885b7f77adStw  *	_INTERNAL
68895b7f77adStw  *	_NO_RESOURCES
68905b7f77adStw  */
68915b7f77adStw int
68925b7f77adStw _scf_set_annotation(scf_handle_t *h, const char *operation, const char *file)
68935b7f77adStw {
68945b7f77adStw 	struct rep_protocol_annotation request;
68955b7f77adStw 	struct rep_protocol_response response;
68965b7f77adStw 	size_t copied;
68975b7f77adStw 	int r;
68985b7f77adStw 
68995b7f77adStw 	request.rpr_request = REP_PROTOCOL_SET_AUDIT_ANNOTATION;
69005b7f77adStw 	copied = strlcpy(request.rpr_operation,
69015b7f77adStw 	    (operation == NULL) ? "" : operation,
69025b7f77adStw 	    sizeof (request.rpr_operation));
69035b7f77adStw 	if (copied >= sizeof (request.rpr_operation))
69045b7f77adStw 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
69055b7f77adStw 
69065b7f77adStw 	copied = strlcpy(request.rpr_file,
69075b7f77adStw 	    (file == NULL) ? "" : file,
69085b7f77adStw 	    sizeof (request.rpr_file));
69095b7f77adStw 	if (copied >= sizeof (request.rpr_operation))
69105b7f77adStw 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
69115b7f77adStw 
69125b7f77adStw 	(void) pthread_mutex_lock(&h->rh_lock);
69135b7f77adStw 	r = make_door_call(h, &request, sizeof (request),
69145b7f77adStw 	    &response, sizeof (response));
69155b7f77adStw 	(void) pthread_mutex_unlock(&h->rh_lock);
69165b7f77adStw 
69175b7f77adStw 	if (r < 0) {
69185b7f77adStw 		DOOR_ERRORS_BLOCK(r);
69195b7f77adStw 	}
69205b7f77adStw 
69215b7f77adStw 	if (response.rpr_response != REP_PROTOCOL_SUCCESS)
69225b7f77adStw 		return (scf_set_error(proto_error(response.rpr_response)));
69235b7f77adStw 	return (0);
69245b7f77adStw }
6925