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
594501b61Sskamm  * Common Development and Distribution License (the "License").
694501b61Sskamm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d75e6a5dStn  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include "libscf_impl.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <libuutil.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
3594501b61Sskamm #include <sys/param.h>
3694501b61Sskamm #include <errno.h>
3794501b61Sskamm #include <libgen.h>
387c478bd9Sstevel@tonic-gate #include "midlevel_impl.h"
39*d75e6a5dStn #include "lowlevel_impl.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifndef NDEBUG
427c478bd9Sstevel@tonic-gate #define	bad_error(func, err)	{					\
437c478bd9Sstevel@tonic-gate 	uu_warn("%s:%d: %s failed with unexpected error %d.  Aborting.\n", \
447c478bd9Sstevel@tonic-gate 	    __FILE__, __LINE__, func, err);				\
457c478bd9Sstevel@tonic-gate 	abort();							\
467c478bd9Sstevel@tonic-gate }
477c478bd9Sstevel@tonic-gate #else
487c478bd9Sstevel@tonic-gate #define	bad_error(func, err)	abort()
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
5194501b61Sskamm /* Path to speedy files area must end with a slash */
5294501b61Sskamm #define	SMF_SPEEDY_FILES_PATH		"/etc/svc/volatile/"
5394501b61Sskamm 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * Internal private function that creates and binds a handle.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate static scf_handle_t *
587c478bd9Sstevel@tonic-gate handle_create(void)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	h = scf_handle_create(SCF_VERSION);
637c478bd9Sstevel@tonic-gate 	if (h == NULL)
647c478bd9Sstevel@tonic-gate 		return (NULL);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if (scf_handle_bind(h) == -1) {
677c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
687c478bd9Sstevel@tonic-gate 		return (NULL);
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 	return (h);
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
73*d75e6a5dStn void
74*d75e6a5dStn scf_simple_handle_destroy(scf_simple_handle_t *simple_h)
75*d75e6a5dStn {
76*d75e6a5dStn 	if (simple_h == NULL)
77*d75e6a5dStn 		return;
78*d75e6a5dStn 
79*d75e6a5dStn 	scf_pg_destroy(simple_h->running_pg);
80*d75e6a5dStn 	scf_pg_destroy(simple_h->editing_pg);
81*d75e6a5dStn 	scf_snapshot_destroy(simple_h->snap);
82*d75e6a5dStn 	scf_instance_destroy(simple_h->inst);
83*d75e6a5dStn 	scf_handle_destroy(simple_h->h);
84*d75e6a5dStn 	uu_free(simple_h);
85*d75e6a5dStn }
86*d75e6a5dStn 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Given a base service FMRI and the names of a property group and property,
897c478bd9Sstevel@tonic-gate  * assemble_fmri() merges them into a property FMRI.  Note that if the base
907c478bd9Sstevel@tonic-gate  * FMRI is NULL, assemble_fmri() gets the base FMRI from scf_myname().
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static char *
947c478bd9Sstevel@tonic-gate assemble_fmri(scf_handle_t *h, const char *base, const char *pg,
957c478bd9Sstevel@tonic-gate     const char *prop)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	size_t	fmri_sz, pglen;
987c478bd9Sstevel@tonic-gate 	ssize_t baselen;
997c478bd9Sstevel@tonic-gate 	char	*fmri_buf;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
1027c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
1037c478bd9Sstevel@tonic-gate 		return (NULL);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (pg == NULL)
1077c478bd9Sstevel@tonic-gate 		pglen = strlen(SCF_PG_APP_DEFAULT);
1087c478bd9Sstevel@tonic-gate 	else
1097c478bd9Sstevel@tonic-gate 		pglen = strlen(pg);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	if (base == NULL) {
1127c478bd9Sstevel@tonic-gate 		if ((baselen = scf_myname(h, NULL, 0)) == -1)
1137c478bd9Sstevel@tonic-gate 			return (NULL);
1147c478bd9Sstevel@tonic-gate 	} else {
1157c478bd9Sstevel@tonic-gate 		baselen = strlen(base);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	fmri_sz = baselen + sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
1197c478bd9Sstevel@tonic-gate 	    pglen + sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
1207c478bd9Sstevel@tonic-gate 	    strlen(prop) + 1;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if ((fmri_buf = malloc(fmri_sz)) == NULL) {
1237c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1247c478bd9Sstevel@tonic-gate 		return (NULL);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (base == NULL) {
1287c478bd9Sstevel@tonic-gate 		if (scf_myname(h, fmri_buf, fmri_sz) == -1) {
1297c478bd9Sstevel@tonic-gate 			free(fmri_buf);
1307c478bd9Sstevel@tonic-gate 			return (NULL);
1317c478bd9Sstevel@tonic-gate 		}
1327c478bd9Sstevel@tonic-gate 	} else {
1337c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri_buf, base);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTYGRP_PREFIX);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (pg == NULL)
1397c478bd9Sstevel@tonic-gate 		(void) strcat(fmri_buf, SCF_PG_APP_DEFAULT);
1407c478bd9Sstevel@tonic-gate 	else
1417c478bd9Sstevel@tonic-gate 		(void) strcat(fmri_buf, pg);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTY_PREFIX);
1447c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, prop);
1457c478bd9Sstevel@tonic-gate 	return (fmri_buf);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate  * Given a property, this function allocates and fills an scf_simple_prop_t
1507c478bd9Sstevel@tonic-gate  * with the data it contains.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static scf_simple_prop_t *
1547c478bd9Sstevel@tonic-gate fill_prop(scf_property_t *prop, const char *pgname, const char *propname,
1557c478bd9Sstevel@tonic-gate     scf_handle_t *h)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 		*ret;
1587c478bd9Sstevel@tonic-gate 	scf_iter_t 			*iter;
1597c478bd9Sstevel@tonic-gate 	scf_value_t 			*val;
1607c478bd9Sstevel@tonic-gate 	int 				iterret, i;
1617c478bd9Sstevel@tonic-gate 	ssize_t 			valsize, numvals;
1627c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val 	*vallist = NULL, *vallist_backup = NULL;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if ((ret = malloc(sizeof (*ret))) == NULL) {
1657c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1667c478bd9Sstevel@tonic-gate 		return (NULL);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	ret->pr_next = NULL;
1707c478bd9Sstevel@tonic-gate 	ret->pr_pg = NULL;
1717c478bd9Sstevel@tonic-gate 	ret->pr_iter = 0;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (pgname == NULL)
1747c478bd9Sstevel@tonic-gate 		ret->pr_pgname = strdup(SCF_PG_APP_DEFAULT);
1757c478bd9Sstevel@tonic-gate 	else
1767c478bd9Sstevel@tonic-gate 		ret->pr_pgname = strdup(pgname);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	if (ret->pr_pgname == NULL) {
1797c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1807c478bd9Sstevel@tonic-gate 		free(ret);
1817c478bd9Sstevel@tonic-gate 		return (NULL);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if ((ret->pr_propname = strdup(propname)) == NULL) {
1857c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1867c478bd9Sstevel@tonic-gate 		free(ret->pr_pgname);
1877c478bd9Sstevel@tonic-gate 		free(ret);
1887c478bd9Sstevel@tonic-gate 		return (NULL);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (scf_property_type(prop, &ret->pr_type) == -1)
1927c478bd9Sstevel@tonic-gate 		goto error3;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
1957c478bd9Sstevel@tonic-gate 		goto error3;
1967c478bd9Sstevel@tonic-gate 	if ((val = scf_value_create(h)) == NULL) {
1977c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
1987c478bd9Sstevel@tonic-gate 		goto error3;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) == -1)
2027c478bd9Sstevel@tonic-gate 		goto error1;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	for (numvals = 0; (iterret = scf_iter_next_value(iter, val)) == 1;
2057c478bd9Sstevel@tonic-gate 	    numvals++) {
2067c478bd9Sstevel@tonic-gate 		vallist_backup = vallist;
2077c478bd9Sstevel@tonic-gate 		if ((vallist = realloc(vallist, (numvals + 1) *
2087c478bd9Sstevel@tonic-gate 		    sizeof (*vallist))) == NULL) {
2097c478bd9Sstevel@tonic-gate 			vallist = vallist_backup;
2107c478bd9Sstevel@tonic-gate 			goto error1;
2117c478bd9Sstevel@tonic-gate 		}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		switch (ret->pr_type) {
2147c478bd9Sstevel@tonic-gate 		case SCF_TYPE_BOOLEAN:
2157c478bd9Sstevel@tonic-gate 			if (scf_value_get_boolean(val,
2167c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_bool) == -1)
2177c478bd9Sstevel@tonic-gate 				goto error1;
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		case SCF_TYPE_COUNT:
2217c478bd9Sstevel@tonic-gate 			if (scf_value_get_count(val,
2227c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_uint) == -1)
2237c478bd9Sstevel@tonic-gate 				goto error1;
2247c478bd9Sstevel@tonic-gate 			break;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		case SCF_TYPE_INTEGER:
2277c478bd9Sstevel@tonic-gate 			if (scf_value_get_integer(val,
2287c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_int) == -1)
2297c478bd9Sstevel@tonic-gate 				goto error1;
2307c478bd9Sstevel@tonic-gate 			break;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		case SCF_TYPE_TIME:
2337c478bd9Sstevel@tonic-gate 			if (scf_value_get_time(val,
2347c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_time.t_sec,
2357c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_time.t_nsec) == -1)
2367c478bd9Sstevel@tonic-gate 				goto error1;
2377c478bd9Sstevel@tonic-gate 			break;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		case SCF_TYPE_ASTRING:
2407c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_str = NULL;
2417c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_astring(val, NULL, 0)) ==
2427c478bd9Sstevel@tonic-gate 			    -1)
2437c478bd9Sstevel@tonic-gate 				goto error1;
2447c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2457c478bd9Sstevel@tonic-gate 			    NULL) {
2467c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2477c478bd9Sstevel@tonic-gate 				goto error1;
2487c478bd9Sstevel@tonic-gate 			}
2497c478bd9Sstevel@tonic-gate 			if (scf_value_get_astring(val,
2507c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_str, valsize+1) == -1) {
2517c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_str);
2527c478bd9Sstevel@tonic-gate 				goto error1;
2537c478bd9Sstevel@tonic-gate 			}
2547c478bd9Sstevel@tonic-gate 			break;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		case SCF_TYPE_USTRING:
2577c478bd9Sstevel@tonic-gate 		case SCF_TYPE_HOST:
2587c478bd9Sstevel@tonic-gate 		case SCF_TYPE_HOSTNAME:
2597c478bd9Sstevel@tonic-gate 		case SCF_TYPE_NET_ADDR_V4:
2607c478bd9Sstevel@tonic-gate 		case SCF_TYPE_NET_ADDR_V6:
2617c478bd9Sstevel@tonic-gate 		case SCF_TYPE_URI:
2627c478bd9Sstevel@tonic-gate 		case SCF_TYPE_FMRI:
2637c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_str = NULL;
2647c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_ustring(val, NULL, 0)) ==
2657c478bd9Sstevel@tonic-gate 			    -1)
2667c478bd9Sstevel@tonic-gate 				goto error1;
2677c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2687c478bd9Sstevel@tonic-gate 			    NULL) {
2697c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2707c478bd9Sstevel@tonic-gate 				goto error1;
2717c478bd9Sstevel@tonic-gate 			}
2727c478bd9Sstevel@tonic-gate 			if (scf_value_get_ustring(val,
2737c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_str, valsize+1) == -1) {
2747c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_str);
2757c478bd9Sstevel@tonic-gate 				goto error1;
2767c478bd9Sstevel@tonic-gate 			}
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		case SCF_TYPE_OPAQUE:
2807c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_opaque.o_value = NULL;
2817c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_opaque(val, NULL, 0)) ==
2827c478bd9Sstevel@tonic-gate 			    -1)
2837c478bd9Sstevel@tonic-gate 				goto error1;
2847c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_opaque.o_value =
2857c478bd9Sstevel@tonic-gate 			    malloc(valsize)) == NULL) {
2867c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2877c478bd9Sstevel@tonic-gate 				goto error1;
2887c478bd9Sstevel@tonic-gate 			}
2897c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_opaque.o_size = valsize;
2907c478bd9Sstevel@tonic-gate 			if (scf_value_get_opaque(val,
2917c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_opaque.o_value,
2927c478bd9Sstevel@tonic-gate 			    valsize) == -1) {
2937c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_opaque.o_value);
2947c478bd9Sstevel@tonic-gate 				goto error1;
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 			break;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		default:
2997c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
3007c478bd9Sstevel@tonic-gate 			goto error1;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	if (iterret == -1) {
3067c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
3077c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
3087c478bd9Sstevel@tonic-gate 		goto error1;
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	ret->pr_vallist = vallist;
3127c478bd9Sstevel@tonic-gate 	ret->pr_numvalues = numvals;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
3157c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	return (ret);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
3217c478bd9Sstevel@tonic-gate 	 * for failures at various stages during the function.
3227c478bd9Sstevel@tonic-gate 	 */
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate error1:
3257c478bd9Sstevel@tonic-gate 	if (vallist == NULL)
3267c478bd9Sstevel@tonic-gate 		goto error2;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	switch (ret->pr_type) {
3297c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
3307c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
3317c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
3327c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
3337c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
3347c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
3357c478bd9Sstevel@tonic-gate 	case SCF_TYPE_URI:
3367c478bd9Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
3377c478bd9Sstevel@tonic-gate 		for (i = 0; i < numvals; i++) {
3387c478bd9Sstevel@tonic-gate 			free(vallist[i].pv_str);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 		break;
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 	case SCF_TYPE_OPAQUE: {
3437c478bd9Sstevel@tonic-gate 		for (i = 0; i < numvals; i++) {
3447c478bd9Sstevel@tonic-gate 			free(vallist[i].pv_opaque.o_value);
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate 		break;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	default:
3497c478bd9Sstevel@tonic-gate 		break;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	free(vallist);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate error2:
3557c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
3567c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate error3:
3597c478bd9Sstevel@tonic-gate 	free(ret->pr_pgname);
3607c478bd9Sstevel@tonic-gate 	free(ret->pr_propname);
3617c478bd9Sstevel@tonic-gate 	free(ret);
3627c478bd9Sstevel@tonic-gate 	return (NULL);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * insert_app_props iterates over a property iterator, getting all the
3677c478bd9Sstevel@tonic-gate  * properties from a property group, and adding or overwriting them into
3687c478bd9Sstevel@tonic-gate  * a simple_app_props_t.  This is used by scf_simple_app_props_get to provide
3697c478bd9Sstevel@tonic-gate  * service/instance composition while filling the app_props_t.
3707c478bd9Sstevel@tonic-gate  * insert_app_props iterates over a single property group.
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate static int
3747c478bd9Sstevel@tonic-gate insert_app_props(scf_iter_t *propiter, char *pgname, char *propname, struct
3757c478bd9Sstevel@tonic-gate     scf_simple_pg *thispg, scf_property_t *prop, size_t namelen,
3767c478bd9Sstevel@tonic-gate     scf_handle_t *h)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	scf_simple_prop_t	*thisprop, *prevprop, *newprop;
3797c478bd9Sstevel@tonic-gate 	uint8_t			found;
3807c478bd9Sstevel@tonic-gate 	int			propiter_ret;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	while ((propiter_ret = scf_iter_next_property(propiter, prop)) == 1) {
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		if (scf_property_get_name(prop, propname, namelen) < 0) {
3857c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
3867c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
3877c478bd9Sstevel@tonic-gate 			return (-1);
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		thisprop = thispg->pg_proplist;
3917c478bd9Sstevel@tonic-gate 		prevprop = thispg->pg_proplist;
3927c478bd9Sstevel@tonic-gate 		found = 0;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		while ((thisprop != NULL) && (!found)) {
3957c478bd9Sstevel@tonic-gate 			if (strcmp(thisprop->pr_propname, propname) == 0) {
3967c478bd9Sstevel@tonic-gate 				found = 1;
3977c478bd9Sstevel@tonic-gate 				if ((newprop = fill_prop(prop, pgname,
3987c478bd9Sstevel@tonic-gate 				    propname, h)) == NULL)
3997c478bd9Sstevel@tonic-gate 					return (-1);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 				if (thisprop == thispg->pg_proplist)
4027c478bd9Sstevel@tonic-gate 					thispg->pg_proplist = newprop;
4037c478bd9Sstevel@tonic-gate 				else
4047c478bd9Sstevel@tonic-gate 					prevprop->pr_next = newprop;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 				newprop->pr_pg = thispg;
4077c478bd9Sstevel@tonic-gate 				newprop->pr_next = thisprop->pr_next;
4087c478bd9Sstevel@tonic-gate 				scf_simple_prop_free(thisprop);
4097c478bd9Sstevel@tonic-gate 				thisprop = NULL;
4107c478bd9Sstevel@tonic-gate 			} else {
4117c478bd9Sstevel@tonic-gate 				if (thisprop != thispg->pg_proplist)
4127c478bd9Sstevel@tonic-gate 					prevprop = prevprop->pr_next;
4137c478bd9Sstevel@tonic-gate 				thisprop = thisprop->pr_next;
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		if (!found) {
4187c478bd9Sstevel@tonic-gate 			if ((newprop = fill_prop(prop, pgname, propname, h)) ==
4197c478bd9Sstevel@tonic-gate 			    NULL)
4207c478bd9Sstevel@tonic-gate 				return (-1);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 			if (thispg->pg_proplist == NULL)
4237c478bd9Sstevel@tonic-gate 				thispg->pg_proplist = newprop;
4247c478bd9Sstevel@tonic-gate 			else
4257c478bd9Sstevel@tonic-gate 				prevprop->pr_next = newprop;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 			newprop->pr_pg = thispg;
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if (propiter_ret == -1) {
4327c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
4337c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
4347c478bd9Sstevel@tonic-gate 		return (-1);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	return (0);
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * Sets up e in tx to set pname's values.  Returns 0 on success or -1 on
4437c478bd9Sstevel@tonic-gate  * failure, with scf_error() set to
4447c478bd9Sstevel@tonic-gate  *   SCF_ERROR_HANDLE_MISMATCH - tx & e are derived from different handles
4457c478bd9Sstevel@tonic-gate  *   SCF_ERROR_INVALID_ARGUMENT - pname or ty are invalid
4467c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_BOUND - handle is not bound
4477c478bd9Sstevel@tonic-gate  *   SCF_ERROR_CONNECTION_BROKEN - connection was broken
4487c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_SET - tx has not been started
4497c478bd9Sstevel@tonic-gate  *   SCF_ERROR_DELETED - the pg tx was started on was deleted
4507c478bd9Sstevel@tonic-gate  */
4517c478bd9Sstevel@tonic-gate static int
4527c478bd9Sstevel@tonic-gate transaction_property_set(scf_transaction_t *tx, scf_transaction_entry_t *e,
4537c478bd9Sstevel@tonic-gate     const char *pname, scf_type_t ty)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 	for (;;) {
4567c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, e, pname, ty) == 0)
4577c478bd9Sstevel@tonic-gate 			return (0);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
4607c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
4617c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4627c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
4637c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4647c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
4657c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
4667c478bd9Sstevel@tonic-gate 		default:
4677c478bd9Sstevel@tonic-gate 			return (-1);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
4707c478bd9Sstevel@tonic-gate 			break;
4717c478bd9Sstevel@tonic-gate 		}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, e, pname, ty) == 0)
4747c478bd9Sstevel@tonic-gate 			return (0);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
4777c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
4787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4797c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
4807c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4817c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
4827c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
4837c478bd9Sstevel@tonic-gate 		default:
4847c478bd9Sstevel@tonic-gate 			return (-1);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
4877c478bd9Sstevel@tonic-gate 			break;
4887c478bd9Sstevel@tonic-gate 		}
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate static int
4937c478bd9Sstevel@tonic-gate get_inst_enabled(const scf_instance_t *inst, const char *pgname)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	scf_propertygroup_t 	*gpg = NULL;
4967c478bd9Sstevel@tonic-gate 	scf_property_t 		*eprop = NULL;
4977c478bd9Sstevel@tonic-gate 	scf_value_t 		*v = NULL;
4987c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
4997c478bd9Sstevel@tonic-gate 	uint8_t			enabled;
5007c478bd9Sstevel@tonic-gate 	int			ret = -1;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
5037c478bd9Sstevel@tonic-gate 		return (-1);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
5067c478bd9Sstevel@tonic-gate 	    (eprop = scf_property_create(h)) == NULL ||
5077c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL)
5087c478bd9Sstevel@tonic-gate 		goto out;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) ||
5117c478bd9Sstevel@tonic-gate 	    scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) ||
5127c478bd9Sstevel@tonic-gate 	    scf_property_get_value(eprop, v) ||
5137c478bd9Sstevel@tonic-gate 	    scf_value_get_boolean(v, &enabled))
5147c478bd9Sstevel@tonic-gate 		goto out;
5157c478bd9Sstevel@tonic-gate 	ret = enabled;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate out:
5187c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
5197c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
5207c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
5217c478bd9Sstevel@tonic-gate 	return (ret);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate  * set_inst_enabled() is a "master" enable/disable call that takes the
5267c478bd9Sstevel@tonic-gate  * instance and the desired state for the enabled bit in the instance's
5277c478bd9Sstevel@tonic-gate  * named property group.  If the group doesn't exist, it's created with the
5287c478bd9Sstevel@tonic-gate  * given flags.  Called by smf_{dis,en}able_instance().
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate static int
5317c478bd9Sstevel@tonic-gate set_inst_enabled(const scf_instance_t *inst, uint8_t desired,
5327c478bd9Sstevel@tonic-gate     const char *pgname, uint32_t pgflags)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	scf_transaction_t 	*tx = NULL;
5357c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *ent = NULL;
5367c478bd9Sstevel@tonic-gate 	scf_propertygroup_t 	*gpg = NULL;
5377c478bd9Sstevel@tonic-gate 	scf_property_t 		*eprop = NULL;
5387c478bd9Sstevel@tonic-gate 	scf_value_t 		*v = NULL;
5397c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
5407c478bd9Sstevel@tonic-gate 	int 			ret = -1;
5417c478bd9Sstevel@tonic-gate 	int			committed;
5427c478bd9Sstevel@tonic-gate 	uint8_t			b;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
5457c478bd9Sstevel@tonic-gate 		return (-1);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
5487c478bd9Sstevel@tonic-gate 	    (eprop = scf_property_create(h)) == NULL ||
5497c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL ||
5507c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
5517c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
5527c478bd9Sstevel@tonic-gate 		goto out;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate get:
5557c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) == -1) {
5567c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5577c478bd9Sstevel@tonic-gate 			goto out;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, pgname, SCF_GROUP_FRAMEWORK,
5607c478bd9Sstevel@tonic-gate 		    pgflags, gpg) == -1) {
5617c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_EXISTS)
5627c478bd9Sstevel@tonic-gate 				goto out;
5637c478bd9Sstevel@tonic-gate 			goto get;
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) == -1) {
5677c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5687c478bd9Sstevel@tonic-gate 			goto out;
5697c478bd9Sstevel@tonic-gate 		else
5707c478bd9Sstevel@tonic-gate 			goto set;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	/*
5747c478bd9Sstevel@tonic-gate 	 * If it's already set the way we want, forgo the transaction.
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(eprop, v) == -1) {
5777c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
5787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
5797c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
5807c478bd9Sstevel@tonic-gate 			/* Misconfigured, so set anyway. */
5817c478bd9Sstevel@tonic-gate 			goto set;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		default:
5847c478bd9Sstevel@tonic-gate 			goto out;
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 	if (scf_value_get_boolean(v, &b) == -1) {
5887c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
5897c478bd9Sstevel@tonic-gate 			goto out;
5907c478bd9Sstevel@tonic-gate 		goto set;
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	if (b == desired) {
5937c478bd9Sstevel@tonic-gate 		ret = 0;
5947c478bd9Sstevel@tonic-gate 		goto out;
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate set:
5987c478bd9Sstevel@tonic-gate 	do {
5997c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) == -1)
6007c478bd9Sstevel@tonic-gate 			goto out;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		if (transaction_property_set(tx, ent, SCF_PROPERTY_ENABLED,
6037c478bd9Sstevel@tonic-gate 		    SCF_TYPE_BOOLEAN) != 0) {
6047c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
6057c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
6067c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
6077c478bd9Sstevel@tonic-gate 			default:
6087c478bd9Sstevel@tonic-gate 				goto out;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
6117c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
6127c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
6137c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
6147c478bd9Sstevel@tonic-gate 				bad_error("transaction_property_set",
6157c478bd9Sstevel@tonic-gate 				    scf_error());
6167c478bd9Sstevel@tonic-gate 			}
6177c478bd9Sstevel@tonic-gate 		}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		scf_value_set_boolean(v, desired);
6207c478bd9Sstevel@tonic-gate 		if (scf_entry_add_value(ent, v) == -1)
6217c478bd9Sstevel@tonic-gate 			goto out;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 		committed = scf_transaction_commit(tx);
6247c478bd9Sstevel@tonic-gate 		if (committed == -1)
6257c478bd9Sstevel@tonic-gate 			goto out;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 		if (committed == 0) { /* out-of-sync */
6307c478bd9Sstevel@tonic-gate 			if (scf_pg_update(gpg) == -1)
6317c478bd9Sstevel@tonic-gate 				goto out;
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 	} while (committed == 0);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	ret = 0;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate out:
6387c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
6397c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
6407c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
6417c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
6427c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	return (ret);
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate static int
6487c478bd9Sstevel@tonic-gate delete_inst_enabled(const scf_instance_t *inst, const char *pgname)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	scf_transaction_t 	*tx = NULL;
6517c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *ent = NULL;
6527c478bd9Sstevel@tonic-gate 	scf_propertygroup_t 	*gpg = NULL;
6537c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
6547c478bd9Sstevel@tonic-gate 	int			ret = -1;
6557c478bd9Sstevel@tonic-gate 	int			committed;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
6587c478bd9Sstevel@tonic-gate 		return (-1);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
6617c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
6627c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
6637c478bd9Sstevel@tonic-gate 		goto out;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) != 0)
6667c478bd9Sstevel@tonic-gate 		goto error;
6677c478bd9Sstevel@tonic-gate 	do {
6687c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) == -1 ||
6697c478bd9Sstevel@tonic-gate 		    scf_transaction_property_delete(tx, ent,
6707c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_ENABLED) == -1 ||
6717c478bd9Sstevel@tonic-gate 		    (committed = scf_transaction_commit(tx)) == -1)
6727c478bd9Sstevel@tonic-gate 			goto error;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		if (committed == 0 && scf_pg_update(gpg) == -1)
6777c478bd9Sstevel@tonic-gate 			goto error;
6787c478bd9Sstevel@tonic-gate 	} while (committed == 0);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	ret = 0;
6817c478bd9Sstevel@tonic-gate 	goto out;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate error:
6847c478bd9Sstevel@tonic-gate 	switch (scf_error()) {
6857c478bd9Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
6867c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
6877c478bd9Sstevel@tonic-gate 		/* success */
6887c478bd9Sstevel@tonic-gate 		ret = 0;
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate out:
6927c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
6937c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
6947c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	return (ret);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate /*
7007c478bd9Sstevel@tonic-gate  * Returns 0 on success or -1 on failure.  On failure leaves scf_error() set to
7017c478bd9Sstevel@tonic-gate  *   SCF_ERROR_HANDLE_DESTROYED - inst's handle has been destroyed
7027c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_BOUND - inst's handle is not bound
7037c478bd9Sstevel@tonic-gate  *   SCF_ERROR_CONNECTION_BROKEN - the repository connection was broken
7047c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_SET - inst is not set
7057c478bd9Sstevel@tonic-gate  *   SCF_ERROR_DELETED - inst was deleted
7067c478bd9Sstevel@tonic-gate  *   SCF_ERROR_PERMISSION_DENIED
7077c478bd9Sstevel@tonic-gate  *   SCF_ERROR_BACKEND_ACCESS
7087c478bd9Sstevel@tonic-gate  *   SCF_ERROR_BACKEND_READONLY
7097c478bd9Sstevel@tonic-gate  */
7107c478bd9Sstevel@tonic-gate static int
7117c478bd9Sstevel@tonic-gate set_inst_action_inst(scf_instance_t *inst, const char *action)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	scf_handle_t			*h;
7147c478bd9Sstevel@tonic-gate 	scf_transaction_t		*tx = NULL;
7157c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t		*ent = NULL;
7167c478bd9Sstevel@tonic-gate 	scf_propertygroup_t		*pg = NULL;
7177c478bd9Sstevel@tonic-gate 	scf_property_t			*prop = NULL;
7187c478bd9Sstevel@tonic-gate 	scf_value_t			*v = NULL;
7197c478bd9Sstevel@tonic-gate 	int				trans, ret = -1;
7207c478bd9Sstevel@tonic-gate 	int64_t				t;
7217c478bd9Sstevel@tonic-gate 	hrtime_t			timestamp;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL ||
7247c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
7257c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
7267c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL ||
7277c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
7287c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
7297c478bd9Sstevel@tonic-gate 		goto out;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate get:
7327c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER_ACTIONS, pg) == -1) {
7337c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
7347c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
7357c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7367c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7377c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7387c478bd9Sstevel@tonic-gate 		default:
7397c478bd9Sstevel@tonic-gate 			goto out;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7427c478bd9Sstevel@tonic-gate 			break;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7457c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7467c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		/* Try creating the restarter_actions property group. */
7507c478bd9Sstevel@tonic-gate add:
7517c478bd9Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
7527c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_TYPE,
7537c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) {
7547c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
7557c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
7567c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7577c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
7587c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
7597c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
7607c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
7617c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
7627c478bd9Sstevel@tonic-gate 			default:
7637c478bd9Sstevel@tonic-gate 				goto out;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 			case SCF_ERROR_EXISTS:
7667c478bd9Sstevel@tonic-gate 				goto get;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
7697c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
7707c478bd9Sstevel@tonic-gate 				bad_error("scf_instance_add_pg", scf_error());
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	for (;;) {
7767c478bd9Sstevel@tonic-gate 		timestamp = gethrtime();
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, action, prop) != 0) {
7797c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
7807c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7817c478bd9Sstevel@tonic-gate 			default:
7827c478bd9Sstevel@tonic-gate 				goto out;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
7857c478bd9Sstevel@tonic-gate 				goto add;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
7887c478bd9Sstevel@tonic-gate 				break;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
7917c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
7927c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
7937c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
7947c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
7957c478bd9Sstevel@tonic-gate 			}
7967c478bd9Sstevel@tonic-gate 		} else if (scf_property_get_value(prop, v) != 0) {
7977c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
7987c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7997c478bd9Sstevel@tonic-gate 			default:
8007c478bd9Sstevel@tonic-gate 				goto out;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8037c478bd9Sstevel@tonic-gate 				goto add;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
8067c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
8077c478bd9Sstevel@tonic-gate 				break;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8107c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8117c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8127c478bd9Sstevel@tonic-gate 				bad_error("scf_property_get_value",
8137c478bd9Sstevel@tonic-gate 				    scf_error());
8147c478bd9Sstevel@tonic-gate 			}
8157c478bd9Sstevel@tonic-gate 		} else if (scf_value_get_integer(v, &t) != 0) {
8167c478bd9Sstevel@tonic-gate 			bad_error("scf_value_get_integer", scf_error());
8177c478bd9Sstevel@tonic-gate 		} else if (t > timestamp) {
8187c478bd9Sstevel@tonic-gate 			break;
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) == -1) {
8227c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8237c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8247c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8257c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8267c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8277c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8287c478bd9Sstevel@tonic-gate 			default:
8297c478bd9Sstevel@tonic-gate 				goto out;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8327c478bd9Sstevel@tonic-gate 				goto add;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8357c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8367c478bd9Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
8377c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_start", scf_error());
8387c478bd9Sstevel@tonic-gate 			}
8397c478bd9Sstevel@tonic-gate 		}
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		if (transaction_property_set(tx, ent, action,
8427c478bd9Sstevel@tonic-gate 		    SCF_TYPE_INTEGER) != 0) {
8437c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8447c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8457c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8467c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8477c478bd9Sstevel@tonic-gate 			default:
8487c478bd9Sstevel@tonic-gate 				goto out;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8517c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
8527c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8537c478bd9Sstevel@tonic-gate 				bad_error("transaction_property_set",
8547c478bd9Sstevel@tonic-gate 				    scf_error());
8557c478bd9Sstevel@tonic-gate 			}
8567c478bd9Sstevel@tonic-gate 		}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		scf_value_set_integer(v, timestamp);
8597c478bd9Sstevel@tonic-gate 		if (scf_entry_add_value(ent, v) == -1)
8607c478bd9Sstevel@tonic-gate 			bad_error("scf_entry_add_value", scf_error());
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 		trans = scf_transaction_commit(tx);
8637c478bd9Sstevel@tonic-gate 		if (trans == 1)
8647c478bd9Sstevel@tonic-gate 			break;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 		if (trans != 0) {
8677c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8687c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8697c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8707c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8717c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8727c478bd9Sstevel@tonic-gate 			default:
8737c478bd9Sstevel@tonic-gate 				goto out;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8767c478bd9Sstevel@tonic-gate 				scf_transaction_reset(tx);
8777c478bd9Sstevel@tonic-gate 				goto add;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
8807c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8817c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8827c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_commit",
8837c478bd9Sstevel@tonic-gate 				    scf_error());
8847c478bd9Sstevel@tonic-gate 			}
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
8887c478bd9Sstevel@tonic-gate 		if (scf_pg_update(pg) != 0) {
8897c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8907c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8917c478bd9Sstevel@tonic-gate 			default:
8927c478bd9Sstevel@tonic-gate 				goto out;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8957c478bd9Sstevel@tonic-gate 				goto add;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8987c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8997c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
9007c478bd9Sstevel@tonic-gate 			}
9017c478bd9Sstevel@tonic-gate 		}
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	ret = 0;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate out:
9077c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
9087c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
9097c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
9107c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
9117c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
9127c478bd9Sstevel@tonic-gate 	return (ret);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate static int
9167c478bd9Sstevel@tonic-gate set_inst_action(const char *fmri, const char *action)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
9197c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
9207c478bd9Sstevel@tonic-gate 	int ret = -1;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	h = handle_create();
9237c478bd9Sstevel@tonic-gate 	if (h == NULL)
9247c478bd9Sstevel@tonic-gate 		return (-1);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (inst != NULL) {
9297c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
9307c478bd9Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) == 0)
9317c478bd9Sstevel@tonic-gate 			ret = set_inst_action_inst(inst, action);
9327c478bd9Sstevel@tonic-gate 		else if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
9337c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
9367c478bd9Sstevel@tonic-gate 	}
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	return (ret);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate /*
9457c478bd9Sstevel@tonic-gate  * get_inst_state() gets the state string from an instance, and returns
9467c478bd9Sstevel@tonic-gate  * the SCF_STATE_* constant that coincides with the instance's current state.
9477c478bd9Sstevel@tonic-gate  */
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate static int
9507c478bd9Sstevel@tonic-gate get_inst_state(scf_instance_t *inst, scf_handle_t *h)
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	scf_propertygroup_t	*pg = NULL;
9537c478bd9Sstevel@tonic-gate 	scf_property_t		*prop = NULL;
9547c478bd9Sstevel@tonic-gate 	scf_value_t		*val = NULL;
9557c478bd9Sstevel@tonic-gate 	char			state[MAX_SCF_STATE_STRING_SZ];
9567c478bd9Sstevel@tonic-gate 	int			ret = -1;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if (((pg = scf_pg_create(h)) == NULL) ||
9597c478bd9Sstevel@tonic-gate 	    ((prop = scf_property_create(h)) == NULL) ||
9607c478bd9Sstevel@tonic-gate 	    ((val = scf_value_create(h)) == NULL))
9617c478bd9Sstevel@tonic-gate 		goto out;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	/* Pull the state property from the instance */
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1 ||
9667c478bd9Sstevel@tonic-gate 	    scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
9677c478bd9Sstevel@tonic-gate 	    scf_property_get_value(prop, val) == -1) {
9687c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
9697c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
9707c478bd9Sstevel@tonic-gate 		goto out;
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	if (scf_value_get_astring(val, state, sizeof (state)) <= 0) {
9747c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INTERNAL);
9757c478bd9Sstevel@tonic-gate 		goto out;
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
9797c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_UNINIT;
9807c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
9817c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_MAINT;
9827c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0) {
9837c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_OFFLINE;
9847c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) {
9857c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_DISABLED;
9867c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
9877c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_ONLINE;
9887c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
9897c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_DEGRADED;
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate out:
9937c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
9947c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
9957c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	return (ret);
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate /*
10017c478bd9Sstevel@tonic-gate  * Sets an instance to be enabled or disabled after reboot, using the
10027c478bd9Sstevel@tonic-gate  * temporary (overriding) general_ovr property group to reflect the
10037c478bd9Sstevel@tonic-gate  * present state, if it is different.
10047c478bd9Sstevel@tonic-gate  */
10057c478bd9Sstevel@tonic-gate static int
10067c478bd9Sstevel@tonic-gate set_inst_enabled_atboot(scf_instance_t *inst, uint8_t desired)
10077c478bd9Sstevel@tonic-gate {
10087c478bd9Sstevel@tonic-gate 	int enabled;
10097c478bd9Sstevel@tonic-gate 	int persistent;
10107c478bd9Sstevel@tonic-gate 	int ret = -1;
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	if ((persistent = get_inst_enabled(inst, SCF_PG_GENERAL)) < 0) {
10137c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
10147c478bd9Sstevel@tonic-gate 			goto out;
10157c478bd9Sstevel@tonic-gate 		persistent = B_FALSE;
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 	if ((enabled = get_inst_enabled(inst, SCF_PG_GENERAL_OVR)) < 0) {
10187c478bd9Sstevel@tonic-gate 		enabled = persistent;
10197c478bd9Sstevel@tonic-gate 		if (persistent != desired) {
10207c478bd9Sstevel@tonic-gate 			/*
10217c478bd9Sstevel@tonic-gate 			 * Temporarily store the present enabled state.
10227c478bd9Sstevel@tonic-gate 			 */
10237c478bd9Sstevel@tonic-gate 			if (set_inst_enabled(inst, persistent,
10247c478bd9Sstevel@tonic-gate 			    SCF_PG_GENERAL_OVR, SCF_PG_GENERAL_OVR_FLAGS))
10257c478bd9Sstevel@tonic-gate 				goto out;
10267c478bd9Sstevel@tonic-gate 		}
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 	if (persistent != desired)
10297c478bd9Sstevel@tonic-gate 		if (set_inst_enabled(inst, desired, SCF_PG_GENERAL,
10307c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_FLAGS))
10317c478bd9Sstevel@tonic-gate 			goto out;
10327c478bd9Sstevel@tonic-gate 	if (enabled == desired)
10337c478bd9Sstevel@tonic-gate 		ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
10347c478bd9Sstevel@tonic-gate 	else
10357c478bd9Sstevel@tonic-gate 		ret = 0;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate out:
10387c478bd9Sstevel@tonic-gate 	return (ret);
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate static int
10427c478bd9Sstevel@tonic-gate set_inst_enabled_flags(const char *fmri, int flags, uint8_t desired)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	int ret = -1;
10457c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
10467c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	if (flags & ~(SMF_TEMPORARY | SMF_AT_NEXT_BOOT) ||
10497c478bd9Sstevel@tonic-gate 	    flags & SMF_TEMPORARY && flags & SMF_AT_NEXT_BOOT) {
10507c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
10517c478bd9Sstevel@tonic-gate 		return (ret);
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if ((h = handle_create()) == NULL)
10557c478bd9Sstevel@tonic-gate 		return (ret);
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL) {
10587c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
10597c478bd9Sstevel@tonic-gate 		return (ret);
10607c478bd9Sstevel@tonic-gate 	}
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
10637c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) == -1) {
10647c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
10657c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
10667c478bd9Sstevel@tonic-gate 		goto out;
10677c478bd9Sstevel@tonic-gate 	}
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	if (flags & SMF_AT_NEXT_BOOT) {
10707c478bd9Sstevel@tonic-gate 		ret = set_inst_enabled_atboot(inst, desired);
10717c478bd9Sstevel@tonic-gate 	} else {
10727c478bd9Sstevel@tonic-gate 		if (set_inst_enabled(inst, desired, flags & SMF_TEMPORARY ?
10737c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, flags & SMF_TEMPORARY ?
10747c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_OVR_FLAGS : SCF_PG_GENERAL_FLAGS))
10757c478bd9Sstevel@tonic-gate 			goto out;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 		/*
10787c478bd9Sstevel@tonic-gate 		 * Make the persistent value effective by deleting the
10797c478bd9Sstevel@tonic-gate 		 * temporary one.
10807c478bd9Sstevel@tonic-gate 		 */
10817c478bd9Sstevel@tonic-gate 		if (flags & SMF_TEMPORARY)
10827c478bd9Sstevel@tonic-gate 			ret = 0;
10837c478bd9Sstevel@tonic-gate 		else
10847c478bd9Sstevel@tonic-gate 			ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate out:
10887c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
10897c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
10907c478bd9Sstevel@tonic-gate 	return (ret);
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate 
1093*d75e6a5dStn /*
1094*d75e6a5dStn  * Create and return a pg from the instance associated with the given handle.
1095*d75e6a5dStn  * This function is only called in scf_transaction_setup and
1096*d75e6a5dStn  * scf_transaction_restart where the h->rh_instance pointer is properly filled
1097*d75e6a5dStn  * in by scf_general_setup_pg().
1098*d75e6a5dStn  */
1099*d75e6a5dStn static scf_propertygroup_t *
1100*d75e6a5dStn get_instance_pg(scf_simple_handle_t *simple_h)
1101*d75e6a5dStn {
1102*d75e6a5dStn 	scf_propertygroup_t	*ret_pg = scf_pg_create(simple_h->h);
1103*d75e6a5dStn 	char			*pg_name;
1104*d75e6a5dStn 	ssize_t			namelen;
1105*d75e6a5dStn 
1106*d75e6a5dStn 	if (ret_pg == NULL) {
1107*d75e6a5dStn 		return (NULL);
1108*d75e6a5dStn 	}
1109*d75e6a5dStn 
1110*d75e6a5dStn 	if ((namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) == -1) {
1111*d75e6a5dStn 		if (scf_error() == SCF_ERROR_NOT_SET) {
1112*d75e6a5dStn 			(void) scf_set_error(SCF_ERROR_INTERNAL);
1113*d75e6a5dStn 		}
1114*d75e6a5dStn 		return (NULL);
1115*d75e6a5dStn 	}
1116*d75e6a5dStn 
1117*d75e6a5dStn 	if ((pg_name = malloc(namelen)) == NULL) {
1118*d75e6a5dStn 		if (scf_error() == SCF_ERROR_NOT_SET) {
1119*d75e6a5dStn 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1120*d75e6a5dStn 		}
1121*d75e6a5dStn 		return (NULL);
1122*d75e6a5dStn 	}
1123*d75e6a5dStn 
1124*d75e6a5dStn 	if (scf_pg_get_name(simple_h->running_pg, pg_name, namelen) < 0) {
1125*d75e6a5dStn 		if (scf_error() == SCF_ERROR_NOT_SET) {
1126*d75e6a5dStn 			(void) scf_set_error(SCF_ERROR_INTERNAL);
1127*d75e6a5dStn 		}
1128*d75e6a5dStn 		return (NULL);
1129*d75e6a5dStn 	}
1130*d75e6a5dStn 
1131*d75e6a5dStn 	/* Get pg from instance */
1132*d75e6a5dStn 	if (scf_instance_get_pg(simple_h->inst, pg_name, ret_pg) == -1) {
1133*d75e6a5dStn 		return (NULL);
1134*d75e6a5dStn 	}
1135*d75e6a5dStn 
1136*d75e6a5dStn 	return (ret_pg);
1137*d75e6a5dStn }
1138*d75e6a5dStn 
11397c478bd9Sstevel@tonic-gate int
11407c478bd9Sstevel@tonic-gate smf_enable_instance(const char *fmri, int flags)
11417c478bd9Sstevel@tonic-gate {
11427c478bd9Sstevel@tonic-gate 	return (set_inst_enabled_flags(fmri, flags, B_TRUE));
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate int
11467c478bd9Sstevel@tonic-gate smf_disable_instance(const char *fmri, int flags)
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate 	return (set_inst_enabled_flags(fmri, flags, B_FALSE));
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate int
11527c478bd9Sstevel@tonic-gate _smf_refresh_instance_i(scf_instance_t *inst)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	return (set_inst_action_inst(inst, SCF_PROPERTY_REFRESH));
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate int
11587c478bd9Sstevel@tonic-gate smf_refresh_instance(const char *instance)
11597c478bd9Sstevel@tonic-gate {
11607c478bd9Sstevel@tonic-gate 	return (set_inst_action(instance, SCF_PROPERTY_REFRESH));
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate int
11647c478bd9Sstevel@tonic-gate smf_restart_instance(const char *instance)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate 	return (set_inst_action(instance, SCF_PROPERTY_RESTART));
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate int
11707c478bd9Sstevel@tonic-gate smf_maintain_instance(const char *instance, int flags)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	if (flags & SMF_TEMPORARY)
11737c478bd9Sstevel@tonic-gate 		return (set_inst_action(instance,
11747c478bd9Sstevel@tonic-gate 		    (flags & SMF_IMMEDIATE) ?
11757c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_IMMTEMP :
11767c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_TEMPORARY));
11777c478bd9Sstevel@tonic-gate 	else
11787c478bd9Sstevel@tonic-gate 		return (set_inst_action(instance,
11797c478bd9Sstevel@tonic-gate 		    (flags & SMF_IMMEDIATE) ?
11807c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_IMMEDIATE :
11817c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON));
11827c478bd9Sstevel@tonic-gate }
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate int
11857c478bd9Sstevel@tonic-gate smf_degrade_instance(const char *instance, int flags)
11867c478bd9Sstevel@tonic-gate {
11877c478bd9Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
11887c478bd9Sstevel@tonic-gate 	const char			*state_str;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	if (flags & SMF_TEMPORARY)
11917c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
11947c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
11957c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
11987c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
11997c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	if (strcmp(state_str, SCF_STATE_STRING_ONLINE) != 0) {
12037c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12047c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	return (set_inst_action(instance, (flags & SMF_IMMEDIATE) ?
12097c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_DEGRADE_IMMEDIATE : SCF_PROPERTY_DEGRADED));
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate int
12137c478bd9Sstevel@tonic-gate smf_restore_instance(const char *instance)
12147c478bd9Sstevel@tonic-gate {
12157c478bd9Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
12167c478bd9Sstevel@tonic-gate 	const char			*state_str;
12177c478bd9Sstevel@tonic-gate 	int				ret;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
12207c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
12217c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
12247c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12257c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	if (strcmp(state_str, SCF_STATE_STRING_MAINT) == 0) {
12297c478bd9Sstevel@tonic-gate 		ret = set_inst_action(instance, SCF_PROPERTY_MAINT_OFF);
12307c478bd9Sstevel@tonic-gate 	} else if (strcmp(state_str, SCF_STATE_STRING_DEGRADED) == 0) {
12317c478bd9Sstevel@tonic-gate 		ret = set_inst_action(instance, SCF_PROPERTY_RESTORE);
12327c478bd9Sstevel@tonic-gate 	} else {
12337c478bd9Sstevel@tonic-gate 		ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
12377c478bd9Sstevel@tonic-gate 	return (ret);
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate char *
12417c478bd9Sstevel@tonic-gate smf_get_state(const char *instance)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
12447c478bd9Sstevel@tonic-gate 	const char			*state_str;
12457c478bd9Sstevel@tonic-gate 	char				*ret;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
12487c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
12497c478bd9Sstevel@tonic-gate 		return (NULL);
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
12527c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12537c478bd9Sstevel@tonic-gate 		return (NULL);
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	if ((ret = strdup(state_str)) == NULL)
12577c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
12607c478bd9Sstevel@tonic-gate 	return (ret);
12617c478bd9Sstevel@tonic-gate }
12627c478bd9Sstevel@tonic-gate 
1263*d75e6a5dStn /*
1264*d75e6a5dStn  * scf_general_pg_setup(fmri, pg_name)
1265*d75e6a5dStn  * Create a scf_simple_handle_t and fill in the instance, snapshot, and
1266*d75e6a5dStn  * property group fields associated with the given fmri and property group
1267*d75e6a5dStn  * name.
1268*d75e6a5dStn  * Returns:
1269*d75e6a5dStn  *      Handle  on success
1270*d75e6a5dStn  *      Null  on error with scf_error set to:
1271*d75e6a5dStn  *              SCF_ERROR_HANDLE_MISMATCH,
1272*d75e6a5dStn  *              SCF_ERROR_INVALID_ARGUMENT,
1273*d75e6a5dStn  *              SCF_ERROR_CONSTRAINT_VIOLATED,
1274*d75e6a5dStn  *              SCF_ERROR_NOT_FOUND,
1275*d75e6a5dStn  *              SCF_ERROR_NOT_SET,
1276*d75e6a5dStn  *              SCF_ERROR_DELETED,
1277*d75e6a5dStn  *              SCF_ERROR_NOT_BOUND,
1278*d75e6a5dStn  *              SCF_ERROR_CONNECTION_BROKEN,
1279*d75e6a5dStn  *              SCF_ERROR_INTERNAL,
1280*d75e6a5dStn  *              SCF_ERROR_NO_RESOURCES,
1281*d75e6a5dStn  *              SCF_ERROR_BACKEND_ACCESS
1282*d75e6a5dStn  */
1283*d75e6a5dStn scf_simple_handle_t *
1284*d75e6a5dStn scf_general_pg_setup(const char *fmri, const char *pg_name)
1285*d75e6a5dStn {
1286*d75e6a5dStn 	scf_simple_handle_t	*ret;
1287*d75e6a5dStn 
1288*d75e6a5dStn 	ret = uu_zalloc(sizeof (*ret));
1289*d75e6a5dStn 	if (ret == NULL) {
1290*d75e6a5dStn 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1291*d75e6a5dStn 		return (NULL);
1292*d75e6a5dStn 	} else {
1293*d75e6a5dStn 
1294*d75e6a5dStn 		ret->h = handle_create();
1295*d75e6a5dStn 		ret->inst = scf_instance_create(ret->h);
1296*d75e6a5dStn 		ret->snap = scf_snapshot_create(ret->h);
1297*d75e6a5dStn 		ret->running_pg = scf_pg_create(ret->h);
1298*d75e6a5dStn 	}
1299*d75e6a5dStn 
1300*d75e6a5dStn 	if ((ret->h == NULL) || (ret->inst == NULL) ||
1301*d75e6a5dStn 	    (ret->snap == NULL) || (ret->running_pg == NULL)) {
1302*d75e6a5dStn 		goto out;
1303*d75e6a5dStn 	}
1304*d75e6a5dStn 
1305*d75e6a5dStn 	if (scf_handle_decode_fmri(ret->h, fmri, NULL, NULL, ret->inst,
1306*d75e6a5dStn 	    NULL, NULL, NULL) == -1) {
1307*d75e6a5dStn 		goto out;
1308*d75e6a5dStn 	}
1309*d75e6a5dStn 
1310*d75e6a5dStn 	if ((scf_instance_get_snapshot(ret->inst, "running", ret->snap))
1311*d75e6a5dStn 	    != 0) {
1312*d75e6a5dStn 		goto out;
1313*d75e6a5dStn 	}
1314*d75e6a5dStn 
1315*d75e6a5dStn 	if (scf_instance_get_pg_composed(ret->inst, ret->snap, pg_name,
1316*d75e6a5dStn 	    ret->running_pg) != 0) {
1317*d75e6a5dStn 		goto out;
1318*d75e6a5dStn 	}
1319*d75e6a5dStn 
1320*d75e6a5dStn 	return (ret);
1321*d75e6a5dStn 
1322*d75e6a5dStn out:
1323*d75e6a5dStn 	scf_simple_handle_destroy(ret);
1324*d75e6a5dStn 	return (NULL);
1325*d75e6a5dStn }
1326*d75e6a5dStn 
1327*d75e6a5dStn /*
1328*d75e6a5dStn  * scf_transaction_setup(h)
1329*d75e6a5dStn  * creates and starts the transaction
1330*d75e6a5dStn  * Returns:
1331*d75e6a5dStn  *      transaction  on success
1332*d75e6a5dStn  *      NULL on failure with scf_error set to:
1333*d75e6a5dStn  *      SCF_ERROR_NO_MEMORY,
1334*d75e6a5dStn  *	SCF_ERROR_INVALID_ARGUMENT,
1335*d75e6a5dStn  *      SCF_ERROR_HANDLE_DESTROYED,
1336*d75e6a5dStn  *	SCF_ERROR_INTERNAL,
1337*d75e6a5dStn  *	SCF_ERROR_NO_RESOURCES,
1338*d75e6a5dStn  *      SCF_ERROR_NOT_BOUND,
1339*d75e6a5dStn  *	SCF_ERROR_CONNECTION_BROKEN,
1340*d75e6a5dStn  *      SCF_ERROR_NOT_SET,
1341*d75e6a5dStn  *	SCF_ERROR_DELETED,
1342*d75e6a5dStn  *	SCF_ERROR_CONSTRAINT_VIOLATED,
1343*d75e6a5dStn  *      SCF_ERROR_HANDLE_MISMATCH,
1344*d75e6a5dStn  *	SCF_ERROR_BACKEND_ACCESS,
1345*d75e6a5dStn  *	SCF_ERROR_IN_USE
1346*d75e6a5dStn  */
1347*d75e6a5dStn scf_transaction_t *
1348*d75e6a5dStn scf_transaction_setup(scf_simple_handle_t *simple_h)
1349*d75e6a5dStn {
1350*d75e6a5dStn 	scf_transaction_t	*tx = NULL;
1351*d75e6a5dStn 
1352*d75e6a5dStn 	if ((tx = scf_transaction_create(simple_h->h)) == NULL) {
1353*d75e6a5dStn 		return (NULL);
1354*d75e6a5dStn 	}
1355*d75e6a5dStn 
1356*d75e6a5dStn 	if ((simple_h->editing_pg = get_instance_pg(simple_h)) == NULL) {
1357*d75e6a5dStn 		return (NULL);
1358*d75e6a5dStn 	}
1359*d75e6a5dStn 
1360*d75e6a5dStn 	if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
1361*d75e6a5dStn 		scf_pg_destroy(simple_h->editing_pg);
1362*d75e6a5dStn 		simple_h->editing_pg = NULL;
1363*d75e6a5dStn 		return (NULL);
1364*d75e6a5dStn 	}
1365*d75e6a5dStn 
1366*d75e6a5dStn 	return (tx);
1367*d75e6a5dStn }
1368*d75e6a5dStn 
1369*d75e6a5dStn int
1370*d75e6a5dStn scf_transaction_restart(scf_simple_handle_t *simple_h, scf_transaction_t *tx)
1371*d75e6a5dStn {
1372*d75e6a5dStn 	scf_transaction_reset(tx);
1373*d75e6a5dStn 
1374*d75e6a5dStn 	if (scf_pg_update(simple_h->editing_pg) == -1) {
1375*d75e6a5dStn 		return (SCF_FAILED);
1376*d75e6a5dStn 	}
1377*d75e6a5dStn 
1378*d75e6a5dStn 	if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
1379*d75e6a5dStn 		return (SCF_FAILED);
1380*d75e6a5dStn 	}
1381*d75e6a5dStn 
1382*d75e6a5dStn 	return (SCF_SUCCESS);
1383*d75e6a5dStn }
1384*d75e6a5dStn 
1385*d75e6a5dStn /*
1386*d75e6a5dStn  * scf_read_count_property(scf_simple_handle_t *simple_h, char *prop_name,
1387*d75e6a5dStn  * uint64_t *ret_count)
1388*d75e6a5dStn  *
1389*d75e6a5dStn  * For the given property name, return the count value.
1390*d75e6a5dStn  * RETURNS:
1391*d75e6a5dStn  *	SCF_SUCCESS
1392*d75e6a5dStn  *	SCF_FAILED on failure with scf_error() set to:
1393*d75e6a5dStn  *		SCF_ERROR_HANDLE_DESTROYED
1394*d75e6a5dStn  *		SCF_ERROR_INTERNAL
1395*d75e6a5dStn  *		SCF_ERROR_NO_RESOURCES
1396*d75e6a5dStn  *		SCF_ERROR_NO_MEMORY
1397*d75e6a5dStn  *		SCF_ERROR_HANDLE_MISMATCH
1398*d75e6a5dStn  *		SCF_ERROR_INVALID_ARGUMENT
1399*d75e6a5dStn  *		SCF_ERROR_NOT_BOUND
1400*d75e6a5dStn  *		SCF_ERROR_CONNECTION_BROKEN
1401*d75e6a5dStn  *		SCF_ERROR_NOT_SET
1402*d75e6a5dStn  *		SCF_ERROR_DELETED
1403*d75e6a5dStn  *		SCF_ERROR_BACKEND_ACCESS
1404*d75e6a5dStn  *		SCF_ERROR_CONSTRAINT_VIOLATED
1405*d75e6a5dStn  *		SCF_ERROR_TYPE_MISMATCH
1406*d75e6a5dStn  */
1407*d75e6a5dStn int
1408*d75e6a5dStn scf_read_count_property(
1409*d75e6a5dStn 	scf_simple_handle_t	*simple_h,
1410*d75e6a5dStn 	char			*prop_name,
1411*d75e6a5dStn 	uint64_t		*ret_count)
1412*d75e6a5dStn {
1413*d75e6a5dStn 	scf_property_t		*prop = scf_property_create(simple_h->h);
1414*d75e6a5dStn 	scf_value_t		*val = scf_value_create(simple_h->h);
1415*d75e6a5dStn 	int			ret = SCF_FAILED;
1416*d75e6a5dStn 
1417*d75e6a5dStn 	if ((val == NULL) || (prop == NULL)) {
1418*d75e6a5dStn 		goto out;
1419*d75e6a5dStn 	}
1420*d75e6a5dStn 
1421*d75e6a5dStn 	/*
1422*d75e6a5dStn 	 * Get the property struct that goes with this property group and
1423*d75e6a5dStn 	 * property name.
1424*d75e6a5dStn 	 */
1425*d75e6a5dStn 	if (scf_pg_get_property(simple_h->running_pg, prop_name, prop) != 0) {
1426*d75e6a5dStn 		goto out;
1427*d75e6a5dStn 	}
1428*d75e6a5dStn 
1429*d75e6a5dStn 	/* Get the value structure */
1430*d75e6a5dStn 	if (scf_property_get_value(prop, val) == -1) {
1431*d75e6a5dStn 		goto out;
1432*d75e6a5dStn 	}
1433*d75e6a5dStn 
1434*d75e6a5dStn 	/*
1435*d75e6a5dStn 	 * Now get the count value.
1436*d75e6a5dStn 	 */
1437*d75e6a5dStn 	if (scf_value_get_count(val, ret_count) == -1) {
1438*d75e6a5dStn 		goto out;
1439*d75e6a5dStn 	}
1440*d75e6a5dStn 
1441*d75e6a5dStn 	ret = SCF_SUCCESS;
1442*d75e6a5dStn 
1443*d75e6a5dStn out:
1444*d75e6a5dStn 	scf_property_destroy(prop);
1445*d75e6a5dStn 	scf_value_destroy(val);
1446*d75e6a5dStn 	return (ret);
1447*d75e6a5dStn }
1448*d75e6a5dStn 
1449*d75e6a5dStn /*
1450*d75e6a5dStn  * scf_trans_add_count_property(trans, propname, count, create_flag)
1451*d75e6a5dStn  *
1452*d75e6a5dStn  * Set a count property transaction entry into the pending SMF transaction.
1453*d75e6a5dStn  * The transaction is created and committed outside of this function.
1454*d75e6a5dStn  * Returns:
1455*d75e6a5dStn  *	SCF_SUCCESS
1456*d75e6a5dStn  *	SCF_FAILED on failure with scf_error() set to:
1457*d75e6a5dStn  *			SCF_ERROR_HANDLE_DESTROYED,
1458*d75e6a5dStn  *			SCF_ERROR_INVALID_ARGUMENT,
1459*d75e6a5dStn  *			SCF_ERROR_NO_MEMORY,
1460*d75e6a5dStn  *			SCF_ERROR_HANDLE_MISMATCH,
1461*d75e6a5dStn  *			SCF_ERROR_NOT_SET,
1462*d75e6a5dStn  *			SCF_ERROR_IN_USE,
1463*d75e6a5dStn  *			SCF_ERROR_NOT_FOUND,
1464*d75e6a5dStn  *			SCF_ERROR_EXISTS,
1465*d75e6a5dStn  *			SCF_ERROR_TYPE_MISMATCH,
1466*d75e6a5dStn  *			SCF_ERROR_NOT_BOUND,
1467*d75e6a5dStn  *			SCF_ERROR_CONNECTION_BROKEN,
1468*d75e6a5dStn  *			SCF_ERROR_INTERNAL,
1469*d75e6a5dStn  *			SCF_ERROR_DELETED,
1470*d75e6a5dStn  *			SCF_ERROR_NO_RESOURCES,
1471*d75e6a5dStn  *			SCF_ERROR_BACKEND_ACCESS
1472*d75e6a5dStn  */
1473*d75e6a5dStn int
1474*d75e6a5dStn scf_set_count_property(
1475*d75e6a5dStn 	scf_transaction_t	*trans,
1476*d75e6a5dStn 	char			*propname,
1477*d75e6a5dStn 	uint64_t		count,
1478*d75e6a5dStn 	boolean_t		create_flag)
1479*d75e6a5dStn {
1480*d75e6a5dStn 	scf_handle_t		*handle = scf_transaction_handle(trans);
1481*d75e6a5dStn 	scf_value_t		*value = scf_value_create(handle);
1482*d75e6a5dStn 	scf_transaction_entry_t	*entry = scf_entry_create(handle);
1483*d75e6a5dStn 
1484*d75e6a5dStn 	if ((value == NULL) || (entry == NULL)) {
1485*d75e6a5dStn 		return (SCF_FAILED);
1486*d75e6a5dStn 	}
1487*d75e6a5dStn 
1488*d75e6a5dStn 	/*
1489*d75e6a5dStn 	 * Property must be set in transaction and won't take
1490*d75e6a5dStn 	 * effect until the transaction is committed.
1491*d75e6a5dStn 	 *
1492*d75e6a5dStn 	 * Attempt to change the current value. However, create new property
1493*d75e6a5dStn 	 * if it doesn't exist and the create flag is set.
1494*d75e6a5dStn 	 */
1495*d75e6a5dStn 	if (scf_transaction_property_change(trans, entry, propname,
1496*d75e6a5dStn 	    SCF_TYPE_COUNT) == 0) {
1497*d75e6a5dStn 		scf_value_set_count(value, count);
1498*d75e6a5dStn 		if (scf_entry_add_value(entry, value) == 0) {
1499*d75e6a5dStn 			return (SCF_SUCCESS);
1500*d75e6a5dStn 		}
1501*d75e6a5dStn 	} else {
1502*d75e6a5dStn 		if ((create_flag == B_TRUE) &&
1503*d75e6a5dStn 		    (scf_error() == SCF_ERROR_NOT_FOUND)) {
1504*d75e6a5dStn 			if (scf_transaction_property_new(trans, entry, propname,
1505*d75e6a5dStn 			    SCF_TYPE_COUNT) == 0) {
1506*d75e6a5dStn 				scf_value_set_count(value, count);
1507*d75e6a5dStn 				if (scf_entry_add_value(entry, value) == 0) {
1508*d75e6a5dStn 					return (SCF_SUCCESS);
1509*d75e6a5dStn 				}
1510*d75e6a5dStn 			}
1511*d75e6a5dStn 		}
1512*d75e6a5dStn 	}
1513*d75e6a5dStn 
1514*d75e6a5dStn 	/*
1515*d75e6a5dStn 	 * cleanup if there were any errors that didn't leave these
1516*d75e6a5dStn 	 * values where they would be cleaned up later.
1517*d75e6a5dStn 	 */
1518*d75e6a5dStn 	if (value != NULL)
1519*d75e6a5dStn 		scf_value_destroy(value);
1520*d75e6a5dStn 	if (entry != NULL)
1521*d75e6a5dStn 		scf_entry_destroy(entry);
1522*d75e6a5dStn 	return (SCF_FAILED);
1523*d75e6a5dStn }
1524*d75e6a5dStn 
15257c478bd9Sstevel@tonic-gate int
15267c478bd9Sstevel@tonic-gate scf_simple_walk_instances(uint_t state_flags, void *private,
15277c478bd9Sstevel@tonic-gate     int (*inst_callback)(scf_handle_t *, scf_instance_t *, void *))
15287c478bd9Sstevel@tonic-gate {
15297c478bd9Sstevel@tonic-gate 	scf_scope_t 		*scope = NULL;
15307c478bd9Sstevel@tonic-gate 	scf_service_t		*svc = NULL;
15317c478bd9Sstevel@tonic-gate 	scf_instance_t		*inst = NULL;
15327c478bd9Sstevel@tonic-gate 	scf_iter_t		*svc_iter = NULL, *inst_iter = NULL;
15337c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
15347c478bd9Sstevel@tonic-gate 	int			ret = SCF_FAILED;
15357c478bd9Sstevel@tonic-gate 	int			svc_iter_ret, inst_iter_ret;
15367c478bd9Sstevel@tonic-gate 	int			inst_state;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	if ((h = handle_create()) == NULL)
15397c478bd9Sstevel@tonic-gate 		return (ret);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if (((scope = scf_scope_create(h)) == NULL) ||
15427c478bd9Sstevel@tonic-gate 	    ((svc = scf_service_create(h)) == NULL) ||
15437c478bd9Sstevel@tonic-gate 	    ((inst = scf_instance_create(h)) == NULL) ||
15447c478bd9Sstevel@tonic-gate 	    ((svc_iter = scf_iter_create(h)) == NULL) ||
15457c478bd9Sstevel@tonic-gate 	    ((inst_iter = scf_iter_create(h)) == NULL))
15467c478bd9Sstevel@tonic-gate 		goto out;
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	/*
15497c478bd9Sstevel@tonic-gate 	 * Get the local scope, and set up nested iteration through every
15507c478bd9Sstevel@tonic-gate 	 * local service, and every instance of every service.
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	if ((scf_handle_get_local_scope(h, scope) != SCF_SUCCESS) ||
15547c478bd9Sstevel@tonic-gate 	    (scf_iter_scope_services(svc_iter, scope) != SCF_SUCCESS))
15557c478bd9Sstevel@tonic-gate 		goto out;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	while ((svc_iter_ret = scf_iter_next_service(svc_iter, svc)) > 0) {
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 		if ((scf_iter_service_instances(inst_iter, svc)) !=
15607c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS)
15617c478bd9Sstevel@tonic-gate 			goto out;
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 		while ((inst_iter_ret =
15647c478bd9Sstevel@tonic-gate 		    scf_iter_next_instance(inst_iter, inst)) > 0) {
15657c478bd9Sstevel@tonic-gate 			/*
15667c478bd9Sstevel@tonic-gate 			 * If get_inst_state fails from an internal error,
15677c478bd9Sstevel@tonic-gate 			 * IE, being unable to get the property group or
15687c478bd9Sstevel@tonic-gate 			 * property containing the state of the instance,
15697c478bd9Sstevel@tonic-gate 			 * we continue instead of failing, as this might just
15707c478bd9Sstevel@tonic-gate 			 * be an improperly configured instance.
15717c478bd9Sstevel@tonic-gate 			 */
15727c478bd9Sstevel@tonic-gate 			if ((inst_state = get_inst_state(inst, h)) == -1) {
15737c478bd9Sstevel@tonic-gate 				if (scf_error() == SCF_ERROR_INTERNAL) {
15747c478bd9Sstevel@tonic-gate 					continue;
15757c478bd9Sstevel@tonic-gate 				} else {
15767c478bd9Sstevel@tonic-gate 					goto out;
15777c478bd9Sstevel@tonic-gate 				}
15787c478bd9Sstevel@tonic-gate 			}
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 			if ((uint_t)inst_state & state_flags) {
15817c478bd9Sstevel@tonic-gate 				if (inst_callback(h, inst, private) !=
15827c478bd9Sstevel@tonic-gate 				    SCF_SUCCESS) {
15837c478bd9Sstevel@tonic-gate 					(void) scf_set_error(
15847c478bd9Sstevel@tonic-gate 					    SCF_ERROR_CALLBACK_FAILED);
15857c478bd9Sstevel@tonic-gate 					goto out;
15867c478bd9Sstevel@tonic-gate 				}
15877c478bd9Sstevel@tonic-gate 			}
15887c478bd9Sstevel@tonic-gate 		}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 		if (inst_iter_ret == -1)
15917c478bd9Sstevel@tonic-gate 			goto out;
15927c478bd9Sstevel@tonic-gate 		scf_iter_reset(inst_iter);
15937c478bd9Sstevel@tonic-gate 	}
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	if (svc_iter_ret != -1)
15967c478bd9Sstevel@tonic-gate 		ret = SCF_SUCCESS;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate out:
15997c478bd9Sstevel@tonic-gate 	scf_scope_destroy(scope);
16007c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
16017c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
16027c478bd9Sstevel@tonic-gate 	scf_iter_destroy(svc_iter);
16037c478bd9Sstevel@tonic-gate 	scf_iter_destroy(inst_iter);
16047c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	return (ret);
16077c478bd9Sstevel@tonic-gate }
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate scf_simple_prop_t *
16117c478bd9Sstevel@tonic-gate scf_simple_prop_get(scf_handle_t *hin, const char *instance, const char *pgname,
16127c478bd9Sstevel@tonic-gate 			const char *propname)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate 	char 			*fmri_buf, *svcfmri = NULL;
16157c478bd9Sstevel@tonic-gate 	ssize_t 		fmri_sz;
16167c478bd9Sstevel@tonic-gate 	scf_property_t 		*prop = NULL;
16177c478bd9Sstevel@tonic-gate 	scf_service_t 		*svc = NULL;
16187c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 	*ret;
16197c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
16207c478bd9Sstevel@tonic-gate 	boolean_t		local_h = B_TRUE;
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	/* If the user passed in a handle, use it. */
16237c478bd9Sstevel@tonic-gate 	if (hin != NULL) {
16247c478bd9Sstevel@tonic-gate 		h = hin;
16257c478bd9Sstevel@tonic-gate 		local_h = B_FALSE;
16267c478bd9Sstevel@tonic-gate 	}
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	if (local_h && ((h = handle_create()) == NULL))
16297c478bd9Sstevel@tonic-gate 		return (NULL);
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	if ((fmri_buf = assemble_fmri(h, instance, pgname, propname)) == NULL) {
16327c478bd9Sstevel@tonic-gate 		if (local_h)
16337c478bd9Sstevel@tonic-gate 			scf_handle_destroy(h);
16347c478bd9Sstevel@tonic-gate 		return (NULL);
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	if ((svc = scf_service_create(h)) == NULL ||
16387c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL)
16397c478bd9Sstevel@tonic-gate 		goto error1;
16407c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri_buf, NULL, NULL, NULL, NULL, prop,
16417c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
16427c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
16437c478bd9Sstevel@tonic-gate 		/*
16447c478bd9Sstevel@tonic-gate 		 * If the property isn't found in the instance, we grab the
16457c478bd9Sstevel@tonic-gate 		 * underlying service, create an FMRI out of it, and then
16467c478bd9Sstevel@tonic-gate 		 * query the datastore again at the service level for the
16477c478bd9Sstevel@tonic-gate 		 * property.
16487c478bd9Sstevel@tonic-gate 		 */
16497c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
16507c478bd9Sstevel@tonic-gate 			if (scf_handle_decode_fmri(h, fmri_buf, NULL, svc,
16517c478bd9Sstevel@tonic-gate 			    NULL, NULL, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1)
16527c478bd9Sstevel@tonic-gate 				goto error1;
16537c478bd9Sstevel@tonic-gate 			if ((fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH)) ==
16547c478bd9Sstevel@tonic-gate 			    -1) {
16557c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
16567c478bd9Sstevel@tonic-gate 				goto error1;
16577c478bd9Sstevel@tonic-gate 			}
16587c478bd9Sstevel@tonic-gate 			if (scf_service_to_fmri(svc, fmri_buf, fmri_sz) == -1)
16597c478bd9Sstevel@tonic-gate 				goto error1;
16607c478bd9Sstevel@tonic-gate 			if ((svcfmri = assemble_fmri(h, fmri_buf, pgname,
16617c478bd9Sstevel@tonic-gate 			    propname)) == NULL)
16627c478bd9Sstevel@tonic-gate 				goto error1;
16637c478bd9Sstevel@tonic-gate 			if (scf_handle_decode_fmri(h, svcfmri, NULL, NULL,
16647c478bd9Sstevel@tonic-gate 			    NULL, NULL, prop, 0) == -1) {
16657c478bd9Sstevel@tonic-gate 				free(svcfmri);
16667c478bd9Sstevel@tonic-gate 				goto error1;
16677c478bd9Sstevel@tonic-gate 			}
16687c478bd9Sstevel@tonic-gate 			free(svcfmri);
16697c478bd9Sstevel@tonic-gate 			break;
16707c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
16717c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
16727c478bd9Sstevel@tonic-gate 		default:
16737c478bd9Sstevel@tonic-gate 			goto error1;
16747c478bd9Sstevel@tonic-gate 		}
16757c478bd9Sstevel@tonic-gate 	}
16767c478bd9Sstevel@tonic-gate 	/*
16777c478bd9Sstevel@tonic-gate 	 * At this point, we've successfully pulled the property from the
16787c478bd9Sstevel@tonic-gate 	 * datastore, and simply need to copy its innards into an
16797c478bd9Sstevel@tonic-gate 	 * scf_simple_prop_t.
16807c478bd9Sstevel@tonic-gate 	 */
16817c478bd9Sstevel@tonic-gate 	if ((ret = fill_prop(prop, pgname, propname, h)) == NULL)
16827c478bd9Sstevel@tonic-gate 		goto error1;
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
16857c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
16867c478bd9Sstevel@tonic-gate 	free(fmri_buf);
16877c478bd9Sstevel@tonic-gate 	if (local_h)
16887c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
16897c478bd9Sstevel@tonic-gate 	return (ret);
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	/*
16927c478bd9Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
16937c478bd9Sstevel@tonic-gate 	 * for failures at various stages during the function.
16947c478bd9Sstevel@tonic-gate 	 */
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate error1:
16977c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
16987c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
16997c478bd9Sstevel@tonic-gate error2:
17007c478bd9Sstevel@tonic-gate 	free(fmri_buf);
17017c478bd9Sstevel@tonic-gate 	if (local_h)
17027c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
17037c478bd9Sstevel@tonic-gate 	return (NULL);
17047c478bd9Sstevel@tonic-gate }
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate void
17087c478bd9Sstevel@tonic-gate scf_simple_prop_free(scf_simple_prop_t *prop)
17097c478bd9Sstevel@tonic-gate {
17107c478bd9Sstevel@tonic-gate 	int i;
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	if (prop == NULL)
17137c478bd9Sstevel@tonic-gate 		return;
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	free(prop->pr_propname);
17167c478bd9Sstevel@tonic-gate 	free(prop->pr_pgname);
17177c478bd9Sstevel@tonic-gate 	switch (prop->pr_type) {
17187c478bd9Sstevel@tonic-gate 	case SCF_TYPE_OPAQUE: {
17197c478bd9Sstevel@tonic-gate 		for (i = 0; i < prop->pr_numvalues; i++) {
17207c478bd9Sstevel@tonic-gate 			free(prop->pr_vallist[i].pv_opaque.o_value);
17217c478bd9Sstevel@tonic-gate 		}
17227c478bd9Sstevel@tonic-gate 		break;
17237c478bd9Sstevel@tonic-gate 	}
17247c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
17257c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
17267c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
17277c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
17287c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
17297c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
17307c478bd9Sstevel@tonic-gate 	case SCF_TYPE_URI:
17317c478bd9Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
17327c478bd9Sstevel@tonic-gate 		for (i = 0; i < prop->pr_numvalues; i++) {
17337c478bd9Sstevel@tonic-gate 			free(prop->pr_vallist[i].pv_str);
17347c478bd9Sstevel@tonic-gate 		}
17357c478bd9Sstevel@tonic-gate 		break;
17367c478bd9Sstevel@tonic-gate 	}
17377c478bd9Sstevel@tonic-gate 	default:
17387c478bd9Sstevel@tonic-gate 		break;
17397c478bd9Sstevel@tonic-gate 	}
17407c478bd9Sstevel@tonic-gate 	free(prop->pr_vallist);
17417c478bd9Sstevel@tonic-gate 	free(prop);
17427c478bd9Sstevel@tonic-gate }
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate scf_simple_app_props_t *
17467c478bd9Sstevel@tonic-gate scf_simple_app_props_get(scf_handle_t *hin, const char *inst_fmri)
17477c478bd9Sstevel@tonic-gate {
17487c478bd9Sstevel@tonic-gate 	scf_instance_t 		*inst = NULL;
17497c478bd9Sstevel@tonic-gate 	scf_service_t 		*svc = NULL;
17507c478bd9Sstevel@tonic-gate 	scf_propertygroup_t 	*pg = NULL;
17517c478bd9Sstevel@tonic-gate 	scf_property_t 		*prop = NULL;
17527c478bd9Sstevel@tonic-gate 	scf_simple_app_props_t	*ret = NULL;
17537c478bd9Sstevel@tonic-gate 	scf_iter_t		*pgiter = NULL, *propiter = NULL;
17547c478bd9Sstevel@tonic-gate 	struct scf_simple_pg	*thispg = NULL, *nextpg;
17557c478bd9Sstevel@tonic-gate 	scf_simple_prop_t	*thisprop, *nextprop;
17567c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
17577c478bd9Sstevel@tonic-gate 	int			pgiter_ret, propiter_ret;
17587c478bd9Sstevel@tonic-gate 	ssize_t			namelen;
17597c478bd9Sstevel@tonic-gate 	char 			*propname = NULL, *pgname = NULL, *sys_fmri;
17607c478bd9Sstevel@tonic-gate 	uint8_t			found;
17617c478bd9Sstevel@tonic-gate 	boolean_t		local_h = B_TRUE;
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	/* If the user passed in a handle, use it. */
17647c478bd9Sstevel@tonic-gate 	if (hin != NULL) {
17657c478bd9Sstevel@tonic-gate 		h = hin;
17667c478bd9Sstevel@tonic-gate 		local_h = B_FALSE;
17677c478bd9Sstevel@tonic-gate 	}
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	if (local_h && ((h = handle_create()) == NULL))
17707c478bd9Sstevel@tonic-gate 		return (NULL);
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	if (inst_fmri == NULL) {
17737c478bd9Sstevel@tonic-gate 		if ((namelen = scf_myname(h, NULL, 0)) == -1) {
17747c478bd9Sstevel@tonic-gate 			if (local_h)
17757c478bd9Sstevel@tonic-gate 				scf_handle_destroy(h);
17767c478bd9Sstevel@tonic-gate 			return (NULL);
17777c478bd9Sstevel@tonic-gate 		}
17787c478bd9Sstevel@tonic-gate 		if ((sys_fmri = malloc(namelen + 1)) == NULL) {
17797c478bd9Sstevel@tonic-gate 			if (local_h)
17807c478bd9Sstevel@tonic-gate 				scf_handle_destroy(h);
17817c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
17827c478bd9Sstevel@tonic-gate 			return (NULL);
17837c478bd9Sstevel@tonic-gate 		}
17847c478bd9Sstevel@tonic-gate 		if (scf_myname(h, sys_fmri, namelen + 1) == -1) {
17857c478bd9Sstevel@tonic-gate 			if (local_h)
17867c478bd9Sstevel@tonic-gate 				scf_handle_destroy(h);
17877c478bd9Sstevel@tonic-gate 			free(sys_fmri);
17887c478bd9Sstevel@tonic-gate 			return (NULL);
17897c478bd9Sstevel@tonic-gate 		}
17907c478bd9Sstevel@tonic-gate 	} else {
17917c478bd9Sstevel@tonic-gate 		sys_fmri = strdup(inst_fmri);
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	if ((namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) == -1) {
17957c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INTERNAL);
17967c478bd9Sstevel@tonic-gate 		return (NULL);
17977c478bd9Sstevel@tonic-gate 	}
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL ||
18007c478bd9Sstevel@tonic-gate 	    (svc = scf_service_create(h)) == NULL ||
18017c478bd9Sstevel@tonic-gate 	    (pgiter = scf_iter_create(h)) == NULL ||
18027c478bd9Sstevel@tonic-gate 	    (propiter = scf_iter_create(h)) == NULL ||
18037c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
18047c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL)
18057c478bd9Sstevel@tonic-gate 		goto error2;
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, sys_fmri, NULL, svc, inst, NULL, NULL,
18087c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
18097c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
18107c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
18117c478bd9Sstevel@tonic-gate 		goto error2;
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	if ((ret = malloc(sizeof (*ret))) == NULL ||
18157c478bd9Sstevel@tonic-gate 	    (thispg = malloc(sizeof (*thispg))) == NULL ||
18167c478bd9Sstevel@tonic-gate 	    (propname = malloc(namelen)) == NULL ||
18177c478bd9Sstevel@tonic-gate 	    (pgname = malloc(namelen)) == NULL) {
18187c478bd9Sstevel@tonic-gate 		free(thispg);
18197c478bd9Sstevel@tonic-gate 		free(ret);
18207c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18217c478bd9Sstevel@tonic-gate 		goto error2;
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	ret->ap_fmri = sys_fmri;
18257c478bd9Sstevel@tonic-gate 	thispg->pg_name = NULL;
18267c478bd9Sstevel@tonic-gate 	thispg->pg_proplist = NULL;
18277c478bd9Sstevel@tonic-gate 	thispg->pg_next = NULL;
18287c478bd9Sstevel@tonic-gate 	ret->ap_pglist = thispg;
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	if (scf_iter_service_pgs_typed(pgiter, svc, SCF_GROUP_APPLICATION) !=
18317c478bd9Sstevel@tonic-gate 	    0) {
18327c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
18337c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
18347c478bd9Sstevel@tonic-gate 		goto error1;
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
18387c478bd9Sstevel@tonic-gate 		if (thispg->pg_name != NULL) {
18397c478bd9Sstevel@tonic-gate 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
18407c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18417c478bd9Sstevel@tonic-gate 				goto error1;
18427c478bd9Sstevel@tonic-gate 			}
18437c478bd9Sstevel@tonic-gate 			thispg->pg_next = nextpg;
18447c478bd9Sstevel@tonic-gate 			thispg = nextpg;
18457c478bd9Sstevel@tonic-gate 		} else {
18467c478bd9Sstevel@tonic-gate 			/* This is the first iteration */
18477c478bd9Sstevel@tonic-gate 			nextpg = thispg;
18487c478bd9Sstevel@tonic-gate 		}
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 		if ((nextpg->pg_name = malloc(namelen)) == NULL) {
18517c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18527c478bd9Sstevel@tonic-gate 			goto error1;
18537c478bd9Sstevel@tonic-gate 		}
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 		if (scf_pg_get_name(pg, nextpg->pg_name, namelen) < 0) {
18567c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
18577c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
18587c478bd9Sstevel@tonic-gate 			goto error1;
18597c478bd9Sstevel@tonic-gate 		}
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 		nextpg->pg_next = NULL;
18627c478bd9Sstevel@tonic-gate 		nextpg->pg_proplist = NULL;
18637c478bd9Sstevel@tonic-gate 		thisprop = NULL;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 		scf_iter_reset(propiter);
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 		if (scf_iter_pg_properties(propiter, pg) != 0) {
18687c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
18697c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
18707c478bd9Sstevel@tonic-gate 			goto error1;
18717c478bd9Sstevel@tonic-gate 		}
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 		while ((propiter_ret = scf_iter_next_property(propiter, prop))
18747c478bd9Sstevel@tonic-gate 		    == 1) {
18757c478bd9Sstevel@tonic-gate 			if (scf_property_get_name(prop, propname, namelen) <
18767c478bd9Sstevel@tonic-gate 			    0) {
18777c478bd9Sstevel@tonic-gate 				if (scf_error() == SCF_ERROR_NOT_SET)
18787c478bd9Sstevel@tonic-gate 					(void) scf_set_error(
18797c478bd9Sstevel@tonic-gate 					    SCF_ERROR_INTERNAL);
18807c478bd9Sstevel@tonic-gate 				goto error1;
18817c478bd9Sstevel@tonic-gate 			}
18827c478bd9Sstevel@tonic-gate 			if (thisprop != NULL) {
18837c478bd9Sstevel@tonic-gate 				if ((nextprop = fill_prop(prop,
18847c478bd9Sstevel@tonic-gate 				    nextpg->pg_name, propname, h)) == NULL)
18857c478bd9Sstevel@tonic-gate 					goto error1;
18867c478bd9Sstevel@tonic-gate 				thisprop->pr_next = nextprop;
18877c478bd9Sstevel@tonic-gate 				thisprop = nextprop;
18887c478bd9Sstevel@tonic-gate 			} else {
18897c478bd9Sstevel@tonic-gate 				/* This is the first iteration */
18907c478bd9Sstevel@tonic-gate 				if ((thisprop = fill_prop(prop,
18917c478bd9Sstevel@tonic-gate 				    nextpg->pg_name, propname, h)) == NULL)
18927c478bd9Sstevel@tonic-gate 					goto error1;
18937c478bd9Sstevel@tonic-gate 				nextpg->pg_proplist = thisprop;
18947c478bd9Sstevel@tonic-gate 				nextprop = thisprop;
18957c478bd9Sstevel@tonic-gate 			}
18967c478bd9Sstevel@tonic-gate 			nextprop->pr_pg = nextpg;
18977c478bd9Sstevel@tonic-gate 			nextprop->pr_next = NULL;
18987c478bd9Sstevel@tonic-gate 		}
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 		if (propiter_ret == -1) {
19017c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19027c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19037c478bd9Sstevel@tonic-gate 			goto error1;
19047c478bd9Sstevel@tonic-gate 		}
19057c478bd9Sstevel@tonic-gate 	}
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	if (pgiter_ret == -1) {
19087c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19097c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
19107c478bd9Sstevel@tonic-gate 		goto error1;
19117c478bd9Sstevel@tonic-gate 	}
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	/*
19147c478bd9Sstevel@tonic-gate 	 * At this point, we've filled the scf_simple_app_props_t with all the
19157c478bd9Sstevel@tonic-gate 	 * properties at the service level.  Now we iterate over all the
19167c478bd9Sstevel@tonic-gate 	 * properties at the instance level, overwriting any duplicate
19177c478bd9Sstevel@tonic-gate 	 * properties, in order to provide service/instance composition.
19187c478bd9Sstevel@tonic-gate 	 */
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	scf_iter_reset(pgiter);
19217c478bd9Sstevel@tonic-gate 	scf_iter_reset(propiter);
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed(pgiter, inst, SCF_GROUP_APPLICATION)
19247c478bd9Sstevel@tonic-gate 	    != 0) {
19257c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19267c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
19277c478bd9Sstevel@tonic-gate 		goto error1;
19287c478bd9Sstevel@tonic-gate 	}
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 		thispg = ret->ap_pglist;
19337c478bd9Sstevel@tonic-gate 		found = 0;
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 		/*
19367c478bd9Sstevel@tonic-gate 		 * Find either the end of the list, so we can append the
19377c478bd9Sstevel@tonic-gate 		 * property group, or an existing property group that matches
19387c478bd9Sstevel@tonic-gate 		 * it, so we can insert/overwrite its properties.
19397c478bd9Sstevel@tonic-gate 		 */
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 		if (scf_pg_get_name(pg, pgname, namelen) < 0) {
19427c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
19437c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19447c478bd9Sstevel@tonic-gate 			goto error1;
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 		while ((thispg != NULL) && (thispg->pg_name != NULL)) {
19487c478bd9Sstevel@tonic-gate 			if (strcmp(thispg->pg_name, pgname) == 0) {
19497c478bd9Sstevel@tonic-gate 				found = 1;
19507c478bd9Sstevel@tonic-gate 				break;
19517c478bd9Sstevel@tonic-gate 			}
19527c478bd9Sstevel@tonic-gate 			if (thispg->pg_next == NULL)
19537c478bd9Sstevel@tonic-gate 				break;
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 			thispg = thispg->pg_next;
19567c478bd9Sstevel@tonic-gate 		}
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 		scf_iter_reset(propiter);
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 		if (scf_iter_pg_properties(propiter, pg) != 0) {
19617c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19627c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19637c478bd9Sstevel@tonic-gate 			goto error1;
19647c478bd9Sstevel@tonic-gate 		}
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 		if (found) {
19677c478bd9Sstevel@tonic-gate 			/*
19687c478bd9Sstevel@tonic-gate 			 * insert_app_props inserts or overwrites the
19697c478bd9Sstevel@tonic-gate 			 * properties in thispg.
19707c478bd9Sstevel@tonic-gate 			 */
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 			if (insert_app_props(propiter, pgname, propname,
19737c478bd9Sstevel@tonic-gate 			    thispg, prop, namelen, h) == -1)
19747c478bd9Sstevel@tonic-gate 				goto error1;
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 		} else {
19777c478bd9Sstevel@tonic-gate 			/*
19787c478bd9Sstevel@tonic-gate 			 * If the property group wasn't found, we're adding
19797c478bd9Sstevel@tonic-gate 			 * a newly allocated property group to the end of the
19807c478bd9Sstevel@tonic-gate 			 * list.
19817c478bd9Sstevel@tonic-gate 			 */
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
19847c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
19857c478bd9Sstevel@tonic-gate 				goto error1;
19867c478bd9Sstevel@tonic-gate 			}
19877c478bd9Sstevel@tonic-gate 			nextpg->pg_next = NULL;
19887c478bd9Sstevel@tonic-gate 			nextpg->pg_proplist = NULL;
19897c478bd9Sstevel@tonic-gate 			thisprop = NULL;
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 			if ((nextpg->pg_name = strdup(pgname)) == NULL) {
19927c478bd9Sstevel@tonic-gate 				free(nextpg);
19937c478bd9Sstevel@tonic-gate 				goto error1;
19947c478bd9Sstevel@tonic-gate 			}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 			if (thispg->pg_name == NULL) {
19977c478bd9Sstevel@tonic-gate 				free(thispg);
19987c478bd9Sstevel@tonic-gate 				ret->ap_pglist = nextpg;
19997c478bd9Sstevel@tonic-gate 			} else {
20007c478bd9Sstevel@tonic-gate 				thispg->pg_next = nextpg;
20017c478bd9Sstevel@tonic-gate 			}
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 			while ((propiter_ret =
20047c478bd9Sstevel@tonic-gate 			    scf_iter_next_property(propiter, prop)) == 1) {
20057c478bd9Sstevel@tonic-gate 				if (scf_property_get_name(prop, propname,
20067c478bd9Sstevel@tonic-gate 				    namelen) < 0) {
20077c478bd9Sstevel@tonic-gate 					if (scf_error() == SCF_ERROR_NOT_SET)
20087c478bd9Sstevel@tonic-gate 						(void) scf_set_error(
20097c478bd9Sstevel@tonic-gate 						    SCF_ERROR_INTERNAL);
20107c478bd9Sstevel@tonic-gate 					goto error1;
20117c478bd9Sstevel@tonic-gate 				}
20127c478bd9Sstevel@tonic-gate 				if (thisprop != NULL) {
20137c478bd9Sstevel@tonic-gate 					if ((nextprop = fill_prop(prop,
20147c478bd9Sstevel@tonic-gate 					    pgname, propname, h)) ==
20157c478bd9Sstevel@tonic-gate 					    NULL)
20167c478bd9Sstevel@tonic-gate 						goto error1;
20177c478bd9Sstevel@tonic-gate 					thisprop->pr_next = nextprop;
20187c478bd9Sstevel@tonic-gate 					thisprop = nextprop;
20197c478bd9Sstevel@tonic-gate 				} else {
20207c478bd9Sstevel@tonic-gate 					/* This is the first iteration */
20217c478bd9Sstevel@tonic-gate 					if ((thisprop = fill_prop(prop,
20227c478bd9Sstevel@tonic-gate 					    pgname, propname, h)) ==
20237c478bd9Sstevel@tonic-gate 					    NULL)
20247c478bd9Sstevel@tonic-gate 						goto error1;
20257c478bd9Sstevel@tonic-gate 					nextpg->pg_proplist = thisprop;
20267c478bd9Sstevel@tonic-gate 					nextprop = thisprop;
20277c478bd9Sstevel@tonic-gate 				}
20287c478bd9Sstevel@tonic-gate 				nextprop->pr_pg = nextpg;
20297c478bd9Sstevel@tonic-gate 				nextprop->pr_next = NULL;
20307c478bd9Sstevel@tonic-gate 			}
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 			if (propiter_ret == -1) {
20337c478bd9Sstevel@tonic-gate 				if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20347c478bd9Sstevel@tonic-gate 					(void) scf_set_error(
20357c478bd9Sstevel@tonic-gate 					    SCF_ERROR_INTERNAL);
20367c478bd9Sstevel@tonic-gate 				goto error1;
20377c478bd9Sstevel@tonic-gate 			}
20387c478bd9Sstevel@tonic-gate 		}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	}
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	if (pgiter_ret == -1) {
20437c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20447c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
20457c478bd9Sstevel@tonic-gate 		goto error1;
20467c478bd9Sstevel@tonic-gate 	}
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	scf_iter_destroy(pgiter);
20497c478bd9Sstevel@tonic-gate 	scf_iter_destroy(propiter);
20507c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
20517c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
20527c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
20537c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
20547c478bd9Sstevel@tonic-gate 	free(propname);
20557c478bd9Sstevel@tonic-gate 	free(pgname);
20567c478bd9Sstevel@tonic-gate 	if (local_h)
20577c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	if (ret->ap_pglist->pg_name == NULL)
20607c478bd9Sstevel@tonic-gate 		return (NULL);
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	return (ret);
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	/*
20657c478bd9Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
20667c478bd9Sstevel@tonic-gate 	 * for failures at various stages during the function.
20677c478bd9Sstevel@tonic-gate 	 */
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate error1:
20707c478bd9Sstevel@tonic-gate 	scf_simple_app_props_free(ret);
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate error2:
20737c478bd9Sstevel@tonic-gate 	scf_iter_destroy(pgiter);
20747c478bd9Sstevel@tonic-gate 	scf_iter_destroy(propiter);
20757c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
20767c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
20777c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
20787c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
20797c478bd9Sstevel@tonic-gate 	free(propname);
20807c478bd9Sstevel@tonic-gate 	free(pgname);
20817c478bd9Sstevel@tonic-gate 	if (local_h)
20827c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
20837c478bd9Sstevel@tonic-gate 	return (NULL);
20847c478bd9Sstevel@tonic-gate }
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate void
20887c478bd9Sstevel@tonic-gate scf_simple_app_props_free(scf_simple_app_props_t *propblock)
20897c478bd9Sstevel@tonic-gate {
20907c478bd9Sstevel@tonic-gate 	struct scf_simple_pg 	*pgthis, *pgnext;
20917c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 	*propthis, *propnext;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if ((propblock == NULL) || (propblock->ap_pglist == NULL))
20947c478bd9Sstevel@tonic-gate 		return;
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	for (pgthis = propblock->ap_pglist; pgthis != NULL; pgthis = pgnext) {
20977c478bd9Sstevel@tonic-gate 		pgnext = pgthis->pg_next;
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 		propthis = pgthis->pg_proplist;
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 		while (propthis != NULL) {
21027c478bd9Sstevel@tonic-gate 			propnext = propthis->pr_next;
21037c478bd9Sstevel@tonic-gate 			scf_simple_prop_free(propthis);
21047c478bd9Sstevel@tonic-gate 			propthis = propnext;
21057c478bd9Sstevel@tonic-gate 		}
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 		free(pgthis->pg_name);
21087c478bd9Sstevel@tonic-gate 		free(pgthis);
21097c478bd9Sstevel@tonic-gate 	}
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 	free(propblock->ap_fmri);
21127c478bd9Sstevel@tonic-gate 	free(propblock);
21137c478bd9Sstevel@tonic-gate }
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate const scf_simple_prop_t *
21167c478bd9Sstevel@tonic-gate scf_simple_app_props_next(const scf_simple_app_props_t *propblock,
21177c478bd9Sstevel@tonic-gate     scf_simple_prop_t *last)
21187c478bd9Sstevel@tonic-gate {
21197c478bd9Sstevel@tonic-gate 	struct scf_simple_pg 	*this;
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	if (propblock == NULL) {
21227c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
21237c478bd9Sstevel@tonic-gate 		return (NULL);
21247c478bd9Sstevel@tonic-gate 	}
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	this = propblock->ap_pglist;
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	/*
21297c478bd9Sstevel@tonic-gate 	 * We're looking for the first property in this block if last is
21307c478bd9Sstevel@tonic-gate 	 * NULL
21317c478bd9Sstevel@tonic-gate 	 */
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	if (last == NULL) {
21347c478bd9Sstevel@tonic-gate 		/* An empty pglist is legal, it just means no properties */
21357c478bd9Sstevel@tonic-gate 		if (this == NULL) {
21367c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NONE);
21377c478bd9Sstevel@tonic-gate 			return (NULL);
21387c478bd9Sstevel@tonic-gate 		}
21397c478bd9Sstevel@tonic-gate 		/*
21407c478bd9Sstevel@tonic-gate 		 * Walk until we find a pg with a property in it, or we run
21417c478bd9Sstevel@tonic-gate 		 * out of property groups.
21427c478bd9Sstevel@tonic-gate 		 */
21437c478bd9Sstevel@tonic-gate 		while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
21447c478bd9Sstevel@tonic-gate 			this = this->pg_next;
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 		if (this->pg_proplist == NULL) {
21477c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NONE);
21487c478bd9Sstevel@tonic-gate 			return (NULL);
21497c478bd9Sstevel@tonic-gate 		}
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 		return (this->pg_proplist);
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	}
21547c478bd9Sstevel@tonic-gate 	/*
21557c478bd9Sstevel@tonic-gate 	 * If last isn't NULL, then return the next prop in the property group,
21567c478bd9Sstevel@tonic-gate 	 * or walk the property groups until we find another property, or
21577c478bd9Sstevel@tonic-gate 	 * run out of property groups.
21587c478bd9Sstevel@tonic-gate 	 */
21597c478bd9Sstevel@tonic-gate 	if (last->pr_next != NULL)
21607c478bd9Sstevel@tonic-gate 		return (last->pr_next);
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	if (last->pr_pg->pg_next == NULL) {
21637c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
21647c478bd9Sstevel@tonic-gate 		return (NULL);
21657c478bd9Sstevel@tonic-gate 	}
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	this = last->pr_pg->pg_next;
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
21707c478bd9Sstevel@tonic-gate 		this = this->pg_next;
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 	if (this->pg_proplist == NULL) {
21737c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
21747c478bd9Sstevel@tonic-gate 		return (NULL);
21757c478bd9Sstevel@tonic-gate 	}
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	return (this->pg_proplist);
21787c478bd9Sstevel@tonic-gate }
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate const scf_simple_prop_t *
21817c478bd9Sstevel@tonic-gate scf_simple_app_props_search(const scf_simple_app_props_t *propblock,
21827c478bd9Sstevel@tonic-gate     const char *pgname, const char *propname)
21837c478bd9Sstevel@tonic-gate {
21847c478bd9Sstevel@tonic-gate 	struct scf_simple_pg 	*pg;
21857c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 	*prop;
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	if ((propblock == NULL) || (propname == NULL)) {
21887c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
21897c478bd9Sstevel@tonic-gate 		return (NULL);
21907c478bd9Sstevel@tonic-gate 	}
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	pg = propblock->ap_pglist;
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	/*
21957c478bd9Sstevel@tonic-gate 	 * If pgname is NULL, we're searching the default application
21967c478bd9Sstevel@tonic-gate 	 * property group, otherwise we look for the specified group.
21977c478bd9Sstevel@tonic-gate 	 */
21987c478bd9Sstevel@tonic-gate 	if (pgname == NULL) {
21997c478bd9Sstevel@tonic-gate 		while ((pg != NULL) &&
22007c478bd9Sstevel@tonic-gate 		    (strcmp(SCF_PG_APP_DEFAULT, pg->pg_name) != 0))
22017c478bd9Sstevel@tonic-gate 			pg = pg->pg_next;
22027c478bd9Sstevel@tonic-gate 	} else {
22037c478bd9Sstevel@tonic-gate 		while ((pg != NULL) && (strcmp(pgname, pg->pg_name) != 0))
22047c478bd9Sstevel@tonic-gate 			pg = pg->pg_next;
22057c478bd9Sstevel@tonic-gate 	}
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	if (pg == NULL) {
22087c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
22097c478bd9Sstevel@tonic-gate 		return (NULL);
22107c478bd9Sstevel@tonic-gate 	}
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	prop = pg->pg_proplist;
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	while ((prop != NULL) && (strcmp(propname, prop->pr_propname) != 0))
22157c478bd9Sstevel@tonic-gate 		prop = prop->pr_next;
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
22187c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
22197c478bd9Sstevel@tonic-gate 		return (NULL);
22207c478bd9Sstevel@tonic-gate 	}
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	return (prop);
22237c478bd9Sstevel@tonic-gate }
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate void
22267c478bd9Sstevel@tonic-gate scf_simple_prop_next_reset(scf_simple_prop_t *prop)
22277c478bd9Sstevel@tonic-gate {
22287c478bd9Sstevel@tonic-gate 	if (prop == NULL)
22297c478bd9Sstevel@tonic-gate 		return;
22307c478bd9Sstevel@tonic-gate 	prop->pr_iter = 0;
22317c478bd9Sstevel@tonic-gate }
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate ssize_t
22347c478bd9Sstevel@tonic-gate scf_simple_prop_numvalues(const scf_simple_prop_t *prop)
22357c478bd9Sstevel@tonic-gate {
22367c478bd9Sstevel@tonic-gate 	if (prop == NULL)
22377c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	return (prop->pr_numvalues);
22407c478bd9Sstevel@tonic-gate }
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate scf_type_t
22447c478bd9Sstevel@tonic-gate scf_simple_prop_type(const scf_simple_prop_t *prop)
22457c478bd9Sstevel@tonic-gate {
22467c478bd9Sstevel@tonic-gate 	if (prop == NULL)
22477c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 	return (prop->pr_type);
22507c478bd9Sstevel@tonic-gate }
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate char *
22547c478bd9Sstevel@tonic-gate scf_simple_prop_name(const scf_simple_prop_t *prop)
22557c478bd9Sstevel@tonic-gate {
22567c478bd9Sstevel@tonic-gate 	if ((prop == NULL) || (prop->pr_propname == NULL)) {
22577c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
22587c478bd9Sstevel@tonic-gate 		return (NULL);
22597c478bd9Sstevel@tonic-gate 	}
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 	return (prop->pr_propname);
22627c478bd9Sstevel@tonic-gate }
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate char *
22667c478bd9Sstevel@tonic-gate scf_simple_prop_pgname(const scf_simple_prop_t *prop)
22677c478bd9Sstevel@tonic-gate {
22687c478bd9Sstevel@tonic-gate 	if ((prop == NULL) || (prop->pr_pgname == NULL)) {
22697c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
22707c478bd9Sstevel@tonic-gate 		return (NULL);
22717c478bd9Sstevel@tonic-gate 	}
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 	return (prop->pr_pgname);
22747c478bd9Sstevel@tonic-gate }
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate static union scf_simple_prop_val *
22787c478bd9Sstevel@tonic-gate scf_next_val(scf_simple_prop_t *prop, scf_type_t type)
22797c478bd9Sstevel@tonic-gate {
22807c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
22817c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
22827c478bd9Sstevel@tonic-gate 		return (NULL);
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	switch (prop->pr_type) {
22867c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
22877c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
22887c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
22897c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
22907c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
22917c478bd9Sstevel@tonic-gate 	case SCF_TYPE_URI:
22927c478bd9Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
22937c478bd9Sstevel@tonic-gate 		if (type != SCF_TYPE_USTRING) {
22947c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
22957c478bd9Sstevel@tonic-gate 			return (NULL);
22967c478bd9Sstevel@tonic-gate 		}
22977c478bd9Sstevel@tonic-gate 		break;
22987c478bd9Sstevel@tonic-gate 		}
22997c478bd9Sstevel@tonic-gate 	default: {
23007c478bd9Sstevel@tonic-gate 		if (type != prop->pr_type) {
23017c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
23027c478bd9Sstevel@tonic-gate 			return (NULL);
23037c478bd9Sstevel@tonic-gate 		}
23047c478bd9Sstevel@tonic-gate 		break;
23057c478bd9Sstevel@tonic-gate 		}
23067c478bd9Sstevel@tonic-gate 	}
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 	if (prop->pr_iter >= prop->pr_numvalues) {
23097c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
23107c478bd9Sstevel@tonic-gate 		return (NULL);
23117c478bd9Sstevel@tonic-gate 	}
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	return (&prop->pr_vallist[prop->pr_iter++]);
23147c478bd9Sstevel@tonic-gate }
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate uint8_t *
23187c478bd9Sstevel@tonic-gate scf_simple_prop_next_boolean(scf_simple_prop_t *prop)
23197c478bd9Sstevel@tonic-gate {
23207c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_BOOLEAN);
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	if (ret == NULL)
23257c478bd9Sstevel@tonic-gate 		return (NULL);
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 	return (&ret->pv_bool);
23287c478bd9Sstevel@tonic-gate }
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate uint64_t *
23327c478bd9Sstevel@tonic-gate scf_simple_prop_next_count(scf_simple_prop_t *prop)
23337c478bd9Sstevel@tonic-gate {
23347c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_COUNT);
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 	if (ret == NULL)
23397c478bd9Sstevel@tonic-gate 		return (NULL);
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 	return (&ret->pv_uint);
23427c478bd9Sstevel@tonic-gate }
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate int64_t *
23467c478bd9Sstevel@tonic-gate scf_simple_prop_next_integer(scf_simple_prop_t *prop)
23477c478bd9Sstevel@tonic-gate {
23487c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_INTEGER);
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	if (ret == NULL)
23537c478bd9Sstevel@tonic-gate 		return (NULL);
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	return (&ret->pv_int);
23567c478bd9Sstevel@tonic-gate }
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate int64_t *
23597c478bd9Sstevel@tonic-gate scf_simple_prop_next_time(scf_simple_prop_t *prop, int32_t *nsec)
23607c478bd9Sstevel@tonic-gate {
23617c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_TIME);
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	if (ret == NULL)
23667c478bd9Sstevel@tonic-gate 		return (NULL);
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	if (nsec != NULL)
23697c478bd9Sstevel@tonic-gate 		*nsec = ret->pv_time.t_nsec;
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	return (&ret->pv_time.t_sec);
23727c478bd9Sstevel@tonic-gate }
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate char *
23757c478bd9Sstevel@tonic-gate scf_simple_prop_next_astring(scf_simple_prop_t *prop)
23767c478bd9Sstevel@tonic-gate {
23777c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_ASTRING);
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	if (ret == NULL)
23827c478bd9Sstevel@tonic-gate 		return (NULL);
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	return (ret->pv_str);
23857c478bd9Sstevel@tonic-gate }
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate char *
23887c478bd9Sstevel@tonic-gate scf_simple_prop_next_ustring(scf_simple_prop_t *prop)
23897c478bd9Sstevel@tonic-gate {
23907c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_USTRING);
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 	if (ret == NULL)
23957c478bd9Sstevel@tonic-gate 		return (NULL);
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate 	return (ret->pv_str);
23987c478bd9Sstevel@tonic-gate }
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate void *
24017c478bd9Sstevel@tonic-gate scf_simple_prop_next_opaque(scf_simple_prop_t *prop, size_t *length)
24027c478bd9Sstevel@tonic-gate {
24037c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_OPAQUE);
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
24087c478bd9Sstevel@tonic-gate 		*length = 0;
24097c478bd9Sstevel@tonic-gate 		return (NULL);
24107c478bd9Sstevel@tonic-gate 	}
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	*length = ret->pv_opaque.o_size;
24137c478bd9Sstevel@tonic-gate 	return (ret->pv_opaque.o_value);
24147c478bd9Sstevel@tonic-gate }
241594501b61Sskamm 
241694501b61Sskamm /*
241794501b61Sskamm  * Generate a filename based on the fmri and the given name and return
241894501b61Sskamm  * it in the buffer of MAXPATHLEN provided by the caller.
241994501b61Sskamm  * If temp_filename is non-zero, also generate a temporary, unique filename
242094501b61Sskamm  * and return it in the temp buffer of MAXPATHLEN provided by the caller.
242194501b61Sskamm  * The path to the generated pathname is also created.
242294501b61Sskamm  * Given fmri should begin with a scheme such as "svc:".
242394501b61Sskamm  * Returns
242494501b61Sskamm  *      0 on success
242594501b61Sskamm  *      -1 if filename would exceed MAXPATHLEN or
242694501b61Sskamm  *	-2 if unable to create directory to filename path
242794501b61Sskamm  */
242894501b61Sskamm int
242994501b61Sskamm gen_filenms_from_fmri(const char *fmri, const char *name, char *filename,
243094501b61Sskamm     char *temp_filename)
243194501b61Sskamm {
243294501b61Sskamm 	int		len;
243394501b61Sskamm 
243494501b61Sskamm 	len = strlen(SMF_SPEEDY_FILES_PATH);
243594501b61Sskamm 	len += strlen(fmri);
243694501b61Sskamm 	len += 2;			/* for slash and null */
243794501b61Sskamm 	len += strlen(name);
243894501b61Sskamm 	len += 6;			/* For X's needed for mkstemp */
243994501b61Sskamm 
244094501b61Sskamm 	if (len > MAXPATHLEN)
244194501b61Sskamm 		return (-1);
244294501b61Sskamm 
244394501b61Sskamm 	/* Construct directory name first - speedy path ends in slash */
244494501b61Sskamm 	(void) strcpy(filename, SMF_SPEEDY_FILES_PATH);
244594501b61Sskamm 	(void) strcat(filename, fmri);
244694501b61Sskamm 	if (mkdirp(filename, 0755) == -1) {
244794501b61Sskamm 		/* errno is set */
244894501b61Sskamm 		if (errno != EEXIST)
244994501b61Sskamm 			return (-2);
245094501b61Sskamm 	}
245194501b61Sskamm 
245294501b61Sskamm 	(void) strcat(filename, "/");
245394501b61Sskamm 	(void) strcat(filename, name);
245494501b61Sskamm 
245594501b61Sskamm 	if (temp_filename) {
245694501b61Sskamm 		(void) strcpy(temp_filename, filename);
245794501b61Sskamm 		(void) strcat(temp_filename, "XXXXXX");
245894501b61Sskamm 	}
245994501b61Sskamm 
246094501b61Sskamm 	return (0);
246194501b61Sskamm }
2462