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
5*94501b61Sskamm  * Common Development and Distribution License (the "License").
6*94501b61Sskamm  * 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*94501b61Sskamm  * Copyright 2006 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>
35*94501b61Sskamm #include <sys/param.h>
36*94501b61Sskamm #include <errno.h>
37*94501b61Sskamm #include <libgen.h>
387c478bd9Sstevel@tonic-gate #include "midlevel_impl.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifndef NDEBUG
417c478bd9Sstevel@tonic-gate #define	bad_error(func, err)	{					\
427c478bd9Sstevel@tonic-gate 	uu_warn("%s:%d: %s failed with unexpected error %d.  Aborting.\n", \
437c478bd9Sstevel@tonic-gate 	    __FILE__, __LINE__, func, err);				\
447c478bd9Sstevel@tonic-gate 	abort();							\
457c478bd9Sstevel@tonic-gate }
467c478bd9Sstevel@tonic-gate #else
477c478bd9Sstevel@tonic-gate #define	bad_error(func, err)	abort()
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate 
50*94501b61Sskamm /* Path to speedy files area must end with a slash */
51*94501b61Sskamm #define	SMF_SPEEDY_FILES_PATH		"/etc/svc/volatile/"
52*94501b61Sskamm 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Internal private function that creates and binds a handle.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate static scf_handle_t *
577c478bd9Sstevel@tonic-gate handle_create(void)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	h = scf_handle_create(SCF_VERSION);
627c478bd9Sstevel@tonic-gate 	if (h == NULL)
637c478bd9Sstevel@tonic-gate 		return (NULL);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (scf_handle_bind(h) == -1) {
667c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
677c478bd9Sstevel@tonic-gate 		return (NULL);
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 	return (h);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Given a base service FMRI and the names of a property group and property,
747c478bd9Sstevel@tonic-gate  * assemble_fmri() merges them into a property FMRI.  Note that if the base
757c478bd9Sstevel@tonic-gate  * FMRI is NULL, assemble_fmri() gets the base FMRI from scf_myname().
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static char *
797c478bd9Sstevel@tonic-gate assemble_fmri(scf_handle_t *h, const char *base, const char *pg,
807c478bd9Sstevel@tonic-gate     const char *prop)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	size_t	fmri_sz, pglen;
837c478bd9Sstevel@tonic-gate 	ssize_t baselen;
847c478bd9Sstevel@tonic-gate 	char	*fmri_buf;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
877c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
887c478bd9Sstevel@tonic-gate 		return (NULL);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (pg == NULL)
927c478bd9Sstevel@tonic-gate 		pglen = strlen(SCF_PG_APP_DEFAULT);
937c478bd9Sstevel@tonic-gate 	else
947c478bd9Sstevel@tonic-gate 		pglen = strlen(pg);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (base == NULL) {
977c478bd9Sstevel@tonic-gate 		if ((baselen = scf_myname(h, NULL, 0)) == -1)
987c478bd9Sstevel@tonic-gate 			return (NULL);
997c478bd9Sstevel@tonic-gate 	} else {
1007c478bd9Sstevel@tonic-gate 		baselen = strlen(base);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	fmri_sz = baselen + sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
1047c478bd9Sstevel@tonic-gate 	    pglen + sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
1057c478bd9Sstevel@tonic-gate 	    strlen(prop) + 1;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if ((fmri_buf = malloc(fmri_sz)) == NULL) {
1087c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1097c478bd9Sstevel@tonic-gate 		return (NULL);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (base == NULL) {
1137c478bd9Sstevel@tonic-gate 		if (scf_myname(h, fmri_buf, fmri_sz) == -1) {
1147c478bd9Sstevel@tonic-gate 			free(fmri_buf);
1157c478bd9Sstevel@tonic-gate 			return (NULL);
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 	} else {
1187c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri_buf, base);
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTYGRP_PREFIX);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (pg == NULL)
1247c478bd9Sstevel@tonic-gate 		(void) strcat(fmri_buf, SCF_PG_APP_DEFAULT);
1257c478bd9Sstevel@tonic-gate 	else
1267c478bd9Sstevel@tonic-gate 		(void) strcat(fmri_buf, pg);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTY_PREFIX);
1297c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, prop);
1307c478bd9Sstevel@tonic-gate 	return (fmri_buf);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * Given a property, this function allocates and fills an scf_simple_prop_t
1357c478bd9Sstevel@tonic-gate  * with the data it contains.
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static scf_simple_prop_t *
1397c478bd9Sstevel@tonic-gate fill_prop(scf_property_t *prop, const char *pgname, const char *propname,
1407c478bd9Sstevel@tonic-gate     scf_handle_t *h)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 		*ret;
1437c478bd9Sstevel@tonic-gate 	scf_iter_t 			*iter;
1447c478bd9Sstevel@tonic-gate 	scf_value_t 			*val;
1457c478bd9Sstevel@tonic-gate 	int 				iterret, i;
1467c478bd9Sstevel@tonic-gate 	ssize_t 			valsize, numvals;
1477c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val 	*vallist = NULL, *vallist_backup = NULL;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if ((ret = malloc(sizeof (*ret))) == NULL) {
1507c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1517c478bd9Sstevel@tonic-gate 		return (NULL);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	ret->pr_next = NULL;
1557c478bd9Sstevel@tonic-gate 	ret->pr_pg = NULL;
1567c478bd9Sstevel@tonic-gate 	ret->pr_iter = 0;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	if (pgname == NULL)
1597c478bd9Sstevel@tonic-gate 		ret->pr_pgname = strdup(SCF_PG_APP_DEFAULT);
1607c478bd9Sstevel@tonic-gate 	else
1617c478bd9Sstevel@tonic-gate 		ret->pr_pgname = strdup(pgname);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (ret->pr_pgname == NULL) {
1647c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1657c478bd9Sstevel@tonic-gate 		free(ret);
1667c478bd9Sstevel@tonic-gate 		return (NULL);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if ((ret->pr_propname = strdup(propname)) == NULL) {
1707c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1717c478bd9Sstevel@tonic-gate 		free(ret->pr_pgname);
1727c478bd9Sstevel@tonic-gate 		free(ret);
1737c478bd9Sstevel@tonic-gate 		return (NULL);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (scf_property_type(prop, &ret->pr_type) == -1)
1777c478bd9Sstevel@tonic-gate 		goto error3;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
1807c478bd9Sstevel@tonic-gate 		goto error3;
1817c478bd9Sstevel@tonic-gate 	if ((val = scf_value_create(h)) == NULL) {
1827c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
1837c478bd9Sstevel@tonic-gate 		goto error3;
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) == -1)
1877c478bd9Sstevel@tonic-gate 		goto error1;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	for (numvals = 0; (iterret = scf_iter_next_value(iter, val)) == 1;
1907c478bd9Sstevel@tonic-gate 	    numvals++) {
1917c478bd9Sstevel@tonic-gate 		vallist_backup = vallist;
1927c478bd9Sstevel@tonic-gate 		if ((vallist = realloc(vallist, (numvals + 1) *
1937c478bd9Sstevel@tonic-gate 		    sizeof (*vallist))) == NULL) {
1947c478bd9Sstevel@tonic-gate 			vallist = vallist_backup;
1957c478bd9Sstevel@tonic-gate 			goto error1;
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 		switch (ret->pr_type) {
1997c478bd9Sstevel@tonic-gate 		case SCF_TYPE_BOOLEAN:
2007c478bd9Sstevel@tonic-gate 			if (scf_value_get_boolean(val,
2017c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_bool) == -1)
2027c478bd9Sstevel@tonic-gate 				goto error1;
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		case SCF_TYPE_COUNT:
2067c478bd9Sstevel@tonic-gate 			if (scf_value_get_count(val,
2077c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_uint) == -1)
2087c478bd9Sstevel@tonic-gate 				goto error1;
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		case SCF_TYPE_INTEGER:
2127c478bd9Sstevel@tonic-gate 			if (scf_value_get_integer(val,
2137c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_int) == -1)
2147c478bd9Sstevel@tonic-gate 				goto error1;
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		case SCF_TYPE_TIME:
2187c478bd9Sstevel@tonic-gate 			if (scf_value_get_time(val,
2197c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_time.t_sec,
2207c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_time.t_nsec) == -1)
2217c478bd9Sstevel@tonic-gate 				goto error1;
2227c478bd9Sstevel@tonic-gate 			break;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		case SCF_TYPE_ASTRING:
2257c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_str = NULL;
2267c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_astring(val, NULL, 0)) ==
2277c478bd9Sstevel@tonic-gate 			    -1)
2287c478bd9Sstevel@tonic-gate 				goto error1;
2297c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2307c478bd9Sstevel@tonic-gate 			    NULL) {
2317c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2327c478bd9Sstevel@tonic-gate 				goto error1;
2337c478bd9Sstevel@tonic-gate 			}
2347c478bd9Sstevel@tonic-gate 			if (scf_value_get_astring(val,
2357c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_str, valsize+1) == -1) {
2367c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_str);
2377c478bd9Sstevel@tonic-gate 				goto error1;
2387c478bd9Sstevel@tonic-gate 			}
2397c478bd9Sstevel@tonic-gate 			break;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		case SCF_TYPE_USTRING:
2427c478bd9Sstevel@tonic-gate 		case SCF_TYPE_HOST:
2437c478bd9Sstevel@tonic-gate 		case SCF_TYPE_HOSTNAME:
2447c478bd9Sstevel@tonic-gate 		case SCF_TYPE_NET_ADDR_V4:
2457c478bd9Sstevel@tonic-gate 		case SCF_TYPE_NET_ADDR_V6:
2467c478bd9Sstevel@tonic-gate 		case SCF_TYPE_URI:
2477c478bd9Sstevel@tonic-gate 		case SCF_TYPE_FMRI:
2487c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_str = NULL;
2497c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_ustring(val, NULL, 0)) ==
2507c478bd9Sstevel@tonic-gate 			    -1)
2517c478bd9Sstevel@tonic-gate 				goto error1;
2527c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2537c478bd9Sstevel@tonic-gate 			    NULL) {
2547c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2557c478bd9Sstevel@tonic-gate 				goto error1;
2567c478bd9Sstevel@tonic-gate 			}
2577c478bd9Sstevel@tonic-gate 			if (scf_value_get_ustring(val,
2587c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_str, valsize+1) == -1) {
2597c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_str);
2607c478bd9Sstevel@tonic-gate 				goto error1;
2617c478bd9Sstevel@tonic-gate 			}
2627c478bd9Sstevel@tonic-gate 			break;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		case SCF_TYPE_OPAQUE:
2657c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_opaque.o_value = NULL;
2667c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_opaque(val, NULL, 0)) ==
2677c478bd9Sstevel@tonic-gate 			    -1)
2687c478bd9Sstevel@tonic-gate 				goto error1;
2697c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_opaque.o_value =
2707c478bd9Sstevel@tonic-gate 			    malloc(valsize)) == NULL) {
2717c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2727c478bd9Sstevel@tonic-gate 				goto error1;
2737c478bd9Sstevel@tonic-gate 			}
2747c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_opaque.o_size = valsize;
2757c478bd9Sstevel@tonic-gate 			if (scf_value_get_opaque(val,
2767c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_opaque.o_value,
2777c478bd9Sstevel@tonic-gate 			    valsize) == -1) {
2787c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_opaque.o_value);
2797c478bd9Sstevel@tonic-gate 				goto error1;
2807c478bd9Sstevel@tonic-gate 			}
2817c478bd9Sstevel@tonic-gate 			break;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		default:
2847c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2857c478bd9Sstevel@tonic-gate 			goto error1;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (iterret == -1) {
2917c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
2927c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2937c478bd9Sstevel@tonic-gate 		goto error1;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	ret->pr_vallist = vallist;
2977c478bd9Sstevel@tonic-gate 	ret->pr_numvalues = numvals;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
3007c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	return (ret);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
3067c478bd9Sstevel@tonic-gate 	 * for failures at various stages during the function.
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate error1:
3107c478bd9Sstevel@tonic-gate 	if (vallist == NULL)
3117c478bd9Sstevel@tonic-gate 		goto error2;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	switch (ret->pr_type) {
3147c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
3157c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
3167c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
3177c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
3187c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
3197c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
3207c478bd9Sstevel@tonic-gate 	case SCF_TYPE_URI:
3217c478bd9Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
3227c478bd9Sstevel@tonic-gate 		for (i = 0; i < numvals; i++) {
3237c478bd9Sstevel@tonic-gate 			free(vallist[i].pv_str);
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 		break;
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 	case SCF_TYPE_OPAQUE: {
3287c478bd9Sstevel@tonic-gate 		for (i = 0; i < numvals; i++) {
3297c478bd9Sstevel@tonic-gate 			free(vallist[i].pv_opaque.o_value);
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 		break;
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 	default:
3347c478bd9Sstevel@tonic-gate 		break;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	free(vallist);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate error2:
3407c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
3417c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate error3:
3447c478bd9Sstevel@tonic-gate 	free(ret->pr_pgname);
3457c478bd9Sstevel@tonic-gate 	free(ret->pr_propname);
3467c478bd9Sstevel@tonic-gate 	free(ret);
3477c478bd9Sstevel@tonic-gate 	return (NULL);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * insert_app_props iterates over a property iterator, getting all the
3527c478bd9Sstevel@tonic-gate  * properties from a property group, and adding or overwriting them into
3537c478bd9Sstevel@tonic-gate  * a simple_app_props_t.  This is used by scf_simple_app_props_get to provide
3547c478bd9Sstevel@tonic-gate  * service/instance composition while filling the app_props_t.
3557c478bd9Sstevel@tonic-gate  * insert_app_props iterates over a single property group.
3567c478bd9Sstevel@tonic-gate  */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate static int
3597c478bd9Sstevel@tonic-gate insert_app_props(scf_iter_t *propiter, char *pgname, char *propname, struct
3607c478bd9Sstevel@tonic-gate     scf_simple_pg *thispg, scf_property_t *prop, size_t namelen,
3617c478bd9Sstevel@tonic-gate     scf_handle_t *h)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	scf_simple_prop_t	*thisprop, *prevprop, *newprop;
3647c478bd9Sstevel@tonic-gate 	uint8_t			found;
3657c478bd9Sstevel@tonic-gate 	int			propiter_ret;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	while ((propiter_ret = scf_iter_next_property(propiter, prop)) == 1) {
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 		if (scf_property_get_name(prop, propname, namelen) < 0) {
3707c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
3717c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
3727c478bd9Sstevel@tonic-gate 			return (-1);
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		thisprop = thispg->pg_proplist;
3767c478bd9Sstevel@tonic-gate 		prevprop = thispg->pg_proplist;
3777c478bd9Sstevel@tonic-gate 		found = 0;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 		while ((thisprop != NULL) && (!found)) {
3807c478bd9Sstevel@tonic-gate 			if (strcmp(thisprop->pr_propname, propname) == 0) {
3817c478bd9Sstevel@tonic-gate 				found = 1;
3827c478bd9Sstevel@tonic-gate 				if ((newprop = fill_prop(prop, pgname,
3837c478bd9Sstevel@tonic-gate 				    propname, h)) == NULL)
3847c478bd9Sstevel@tonic-gate 					return (-1);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 				if (thisprop == thispg->pg_proplist)
3877c478bd9Sstevel@tonic-gate 					thispg->pg_proplist = newprop;
3887c478bd9Sstevel@tonic-gate 				else
3897c478bd9Sstevel@tonic-gate 					prevprop->pr_next = newprop;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 				newprop->pr_pg = thispg;
3927c478bd9Sstevel@tonic-gate 				newprop->pr_next = thisprop->pr_next;
3937c478bd9Sstevel@tonic-gate 				scf_simple_prop_free(thisprop);
3947c478bd9Sstevel@tonic-gate 				thisprop = NULL;
3957c478bd9Sstevel@tonic-gate 			} else {
3967c478bd9Sstevel@tonic-gate 				if (thisprop != thispg->pg_proplist)
3977c478bd9Sstevel@tonic-gate 					prevprop = prevprop->pr_next;
3987c478bd9Sstevel@tonic-gate 				thisprop = thisprop->pr_next;
3997c478bd9Sstevel@tonic-gate 			}
4007c478bd9Sstevel@tonic-gate 		}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		if (!found) {
4037c478bd9Sstevel@tonic-gate 			if ((newprop = fill_prop(prop, pgname, propname, h)) ==
4047c478bd9Sstevel@tonic-gate 			    NULL)
4057c478bd9Sstevel@tonic-gate 				return (-1);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 			if (thispg->pg_proplist == NULL)
4087c478bd9Sstevel@tonic-gate 				thispg->pg_proplist = newprop;
4097c478bd9Sstevel@tonic-gate 			else
4107c478bd9Sstevel@tonic-gate 				prevprop->pr_next = newprop;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 			newprop->pr_pg = thispg;
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (propiter_ret == -1) {
4177c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
4187c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
4197c478bd9Sstevel@tonic-gate 		return (-1);
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	return (0);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * Sets up e in tx to set pname's values.  Returns 0 on success or -1 on
4287c478bd9Sstevel@tonic-gate  * failure, with scf_error() set to
4297c478bd9Sstevel@tonic-gate  *   SCF_ERROR_HANDLE_MISMATCH - tx & e are derived from different handles
4307c478bd9Sstevel@tonic-gate  *   SCF_ERROR_INVALID_ARGUMENT - pname or ty are invalid
4317c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_BOUND - handle is not bound
4327c478bd9Sstevel@tonic-gate  *   SCF_ERROR_CONNECTION_BROKEN - connection was broken
4337c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_SET - tx has not been started
4347c478bd9Sstevel@tonic-gate  *   SCF_ERROR_DELETED - the pg tx was started on was deleted
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate static int
4377c478bd9Sstevel@tonic-gate transaction_property_set(scf_transaction_t *tx, scf_transaction_entry_t *e,
4387c478bd9Sstevel@tonic-gate     const char *pname, scf_type_t ty)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	for (;;) {
4417c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, e, pname, ty) == 0)
4427c478bd9Sstevel@tonic-gate 			return (0);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
4457c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
4467c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4477c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
4487c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4497c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
4507c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
4517c478bd9Sstevel@tonic-gate 		default:
4527c478bd9Sstevel@tonic-gate 			return (-1);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
4557c478bd9Sstevel@tonic-gate 			break;
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, e, pname, ty) == 0)
4597c478bd9Sstevel@tonic-gate 			return (0);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
4627c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
4637c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4647c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
4657c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
4677c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
4687c478bd9Sstevel@tonic-gate 		default:
4697c478bd9Sstevel@tonic-gate 			return (-1);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
4727c478bd9Sstevel@tonic-gate 			break;
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate static int
4787c478bd9Sstevel@tonic-gate get_inst_enabled(const scf_instance_t *inst, const char *pgname)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	scf_propertygroup_t 	*gpg = NULL;
4817c478bd9Sstevel@tonic-gate 	scf_property_t 		*eprop = NULL;
4827c478bd9Sstevel@tonic-gate 	scf_value_t 		*v = NULL;
4837c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
4847c478bd9Sstevel@tonic-gate 	uint8_t			enabled;
4857c478bd9Sstevel@tonic-gate 	int			ret = -1;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
4887c478bd9Sstevel@tonic-gate 		return (-1);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
4917c478bd9Sstevel@tonic-gate 	    (eprop = scf_property_create(h)) == NULL ||
4927c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL)
4937c478bd9Sstevel@tonic-gate 		goto out;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) ||
4967c478bd9Sstevel@tonic-gate 	    scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) ||
4977c478bd9Sstevel@tonic-gate 	    scf_property_get_value(eprop, v) ||
4987c478bd9Sstevel@tonic-gate 	    scf_value_get_boolean(v, &enabled))
4997c478bd9Sstevel@tonic-gate 		goto out;
5007c478bd9Sstevel@tonic-gate 	ret = enabled;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate out:
5037c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
5047c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
5057c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
5067c478bd9Sstevel@tonic-gate 	return (ret);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate  * set_inst_enabled() is a "master" enable/disable call that takes the
5117c478bd9Sstevel@tonic-gate  * instance and the desired state for the enabled bit in the instance's
5127c478bd9Sstevel@tonic-gate  * named property group.  If the group doesn't exist, it's created with the
5137c478bd9Sstevel@tonic-gate  * given flags.  Called by smf_{dis,en}able_instance().
5147c478bd9Sstevel@tonic-gate  */
5157c478bd9Sstevel@tonic-gate static int
5167c478bd9Sstevel@tonic-gate set_inst_enabled(const scf_instance_t *inst, uint8_t desired,
5177c478bd9Sstevel@tonic-gate     const char *pgname, uint32_t pgflags)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	scf_transaction_t 	*tx = NULL;
5207c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *ent = NULL;
5217c478bd9Sstevel@tonic-gate 	scf_propertygroup_t 	*gpg = NULL;
5227c478bd9Sstevel@tonic-gate 	scf_property_t 		*eprop = NULL;
5237c478bd9Sstevel@tonic-gate 	scf_value_t 		*v = NULL;
5247c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
5257c478bd9Sstevel@tonic-gate 	int 			ret = -1;
5267c478bd9Sstevel@tonic-gate 	int			committed;
5277c478bd9Sstevel@tonic-gate 	uint8_t			b;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
5307c478bd9Sstevel@tonic-gate 		return (-1);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
5337c478bd9Sstevel@tonic-gate 	    (eprop = scf_property_create(h)) == NULL ||
5347c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL ||
5357c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
5367c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
5377c478bd9Sstevel@tonic-gate 		goto out;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate get:
5407c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) == -1) {
5417c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5427c478bd9Sstevel@tonic-gate 			goto out;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, pgname, SCF_GROUP_FRAMEWORK,
5457c478bd9Sstevel@tonic-gate 		    pgflags, gpg) == -1) {
5467c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_EXISTS)
5477c478bd9Sstevel@tonic-gate 				goto out;
5487c478bd9Sstevel@tonic-gate 			goto get;
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) == -1) {
5527c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5537c478bd9Sstevel@tonic-gate 			goto out;
5547c478bd9Sstevel@tonic-gate 		else
5557c478bd9Sstevel@tonic-gate 			goto set;
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	/*
5597c478bd9Sstevel@tonic-gate 	 * If it's already set the way we want, forgo the transaction.
5607c478bd9Sstevel@tonic-gate 	 */
5617c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(eprop, v) == -1) {
5627c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
5637c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
5647c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
5657c478bd9Sstevel@tonic-gate 			/* Misconfigured, so set anyway. */
5667c478bd9Sstevel@tonic-gate 			goto set;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		default:
5697c478bd9Sstevel@tonic-gate 			goto out;
5707c478bd9Sstevel@tonic-gate 		}
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 	if (scf_value_get_boolean(v, &b) == -1) {
5737c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
5747c478bd9Sstevel@tonic-gate 			goto out;
5757c478bd9Sstevel@tonic-gate 		goto set;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 	if (b == desired) {
5787c478bd9Sstevel@tonic-gate 		ret = 0;
5797c478bd9Sstevel@tonic-gate 		goto out;
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate set:
5837c478bd9Sstevel@tonic-gate 	do {
5847c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) == -1)
5857c478bd9Sstevel@tonic-gate 			goto out;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		if (transaction_property_set(tx, ent, SCF_PROPERTY_ENABLED,
5887c478bd9Sstevel@tonic-gate 		    SCF_TYPE_BOOLEAN) != 0) {
5897c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
5907c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
5917c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
5927c478bd9Sstevel@tonic-gate 			default:
5937c478bd9Sstevel@tonic-gate 				goto out;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
5967c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
5977c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
5987c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
5997c478bd9Sstevel@tonic-gate 				bad_error("transaction_property_set",
6007c478bd9Sstevel@tonic-gate 				    scf_error());
6017c478bd9Sstevel@tonic-gate 			}
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		scf_value_set_boolean(v, desired);
6057c478bd9Sstevel@tonic-gate 		if (scf_entry_add_value(ent, v) == -1)
6067c478bd9Sstevel@tonic-gate 			goto out;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		committed = scf_transaction_commit(tx);
6097c478bd9Sstevel@tonic-gate 		if (committed == -1)
6107c478bd9Sstevel@tonic-gate 			goto out;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		if (committed == 0) { /* out-of-sync */
6157c478bd9Sstevel@tonic-gate 			if (scf_pg_update(gpg) == -1)
6167c478bd9Sstevel@tonic-gate 				goto out;
6177c478bd9Sstevel@tonic-gate 		}
6187c478bd9Sstevel@tonic-gate 	} while (committed == 0);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	ret = 0;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate out:
6237c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
6247c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
6257c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
6267c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
6277c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	return (ret);
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate static int
6337c478bd9Sstevel@tonic-gate delete_inst_enabled(const scf_instance_t *inst, const char *pgname)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate 	scf_transaction_t 	*tx = NULL;
6367c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *ent = NULL;
6377c478bd9Sstevel@tonic-gate 	scf_propertygroup_t 	*gpg = NULL;
6387c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
6397c478bd9Sstevel@tonic-gate 	int			ret = -1;
6407c478bd9Sstevel@tonic-gate 	int			committed;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
6437c478bd9Sstevel@tonic-gate 		return (-1);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
6467c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
6477c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
6487c478bd9Sstevel@tonic-gate 		goto out;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) != 0)
6517c478bd9Sstevel@tonic-gate 		goto error;
6527c478bd9Sstevel@tonic-gate 	do {
6537c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) == -1 ||
6547c478bd9Sstevel@tonic-gate 		    scf_transaction_property_delete(tx, ent,
6557c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_ENABLED) == -1 ||
6567c478bd9Sstevel@tonic-gate 		    (committed = scf_transaction_commit(tx)) == -1)
6577c478bd9Sstevel@tonic-gate 			goto error;
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 		if (committed == 0 && scf_pg_update(gpg) == -1)
6627c478bd9Sstevel@tonic-gate 			goto error;
6637c478bd9Sstevel@tonic-gate 	} while (committed == 0);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	ret = 0;
6667c478bd9Sstevel@tonic-gate 	goto out;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate error:
6697c478bd9Sstevel@tonic-gate 	switch (scf_error()) {
6707c478bd9Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
6717c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
6727c478bd9Sstevel@tonic-gate 		/* success */
6737c478bd9Sstevel@tonic-gate 		ret = 0;
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate out:
6777c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
6787c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
6797c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	return (ret);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate /*
6857c478bd9Sstevel@tonic-gate  * Returns 0 on success or -1 on failure.  On failure leaves scf_error() set to
6867c478bd9Sstevel@tonic-gate  *   SCF_ERROR_HANDLE_DESTROYED - inst's handle has been destroyed
6877c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_BOUND - inst's handle is not bound
6887c478bd9Sstevel@tonic-gate  *   SCF_ERROR_CONNECTION_BROKEN - the repository connection was broken
6897c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_SET - inst is not set
6907c478bd9Sstevel@tonic-gate  *   SCF_ERROR_DELETED - inst was deleted
6917c478bd9Sstevel@tonic-gate  *   SCF_ERROR_PERMISSION_DENIED
6927c478bd9Sstevel@tonic-gate  *   SCF_ERROR_BACKEND_ACCESS
6937c478bd9Sstevel@tonic-gate  *   SCF_ERROR_BACKEND_READONLY
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate static int
6967c478bd9Sstevel@tonic-gate set_inst_action_inst(scf_instance_t *inst, const char *action)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	scf_handle_t			*h;
6997c478bd9Sstevel@tonic-gate 	scf_transaction_t		*tx = NULL;
7007c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t		*ent = NULL;
7017c478bd9Sstevel@tonic-gate 	scf_propertygroup_t		*pg = NULL;
7027c478bd9Sstevel@tonic-gate 	scf_property_t			*prop = NULL;
7037c478bd9Sstevel@tonic-gate 	scf_value_t			*v = NULL;
7047c478bd9Sstevel@tonic-gate 	int				trans, ret = -1;
7057c478bd9Sstevel@tonic-gate 	int64_t				t;
7067c478bd9Sstevel@tonic-gate 	hrtime_t			timestamp;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL ||
7097c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
7107c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
7117c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL ||
7127c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
7137c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
7147c478bd9Sstevel@tonic-gate 		goto out;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate get:
7177c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER_ACTIONS, pg) == -1) {
7187c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
7197c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
7207c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7217c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7227c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7237c478bd9Sstevel@tonic-gate 		default:
7247c478bd9Sstevel@tonic-gate 			goto out;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7277c478bd9Sstevel@tonic-gate 			break;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7307c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7317c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		/* Try creating the restarter_actions property group. */
7357c478bd9Sstevel@tonic-gate add:
7367c478bd9Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
7377c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_TYPE,
7387c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) {
7397c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
7407c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
7417c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7427c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
7437c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
7447c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
7457c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
7467c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
7477c478bd9Sstevel@tonic-gate 			default:
7487c478bd9Sstevel@tonic-gate 				goto out;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 			case SCF_ERROR_EXISTS:
7517c478bd9Sstevel@tonic-gate 				goto get;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
7547c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
7557c478bd9Sstevel@tonic-gate 				bad_error("scf_instance_add_pg", scf_error());
7567c478bd9Sstevel@tonic-gate 			}
7577c478bd9Sstevel@tonic-gate 		}
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	for (;;) {
7617c478bd9Sstevel@tonic-gate 		timestamp = gethrtime();
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, action, prop) != 0) {
7647c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
7657c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7667c478bd9Sstevel@tonic-gate 			default:
7677c478bd9Sstevel@tonic-gate 				goto out;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
7707c478bd9Sstevel@tonic-gate 				goto add;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
7737c478bd9Sstevel@tonic-gate 				break;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
7767c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
7777c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
7787c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
7797c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
7807c478bd9Sstevel@tonic-gate 			}
7817c478bd9Sstevel@tonic-gate 		} else if (scf_property_get_value(prop, v) != 0) {
7827c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
7837c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7847c478bd9Sstevel@tonic-gate 			default:
7857c478bd9Sstevel@tonic-gate 				goto out;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
7887c478bd9Sstevel@tonic-gate 				goto add;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
7917c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
7927c478bd9Sstevel@tonic-gate 				break;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
7957c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
7967c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
7977c478bd9Sstevel@tonic-gate 				bad_error("scf_property_get_value",
7987c478bd9Sstevel@tonic-gate 				    scf_error());
7997c478bd9Sstevel@tonic-gate 			}
8007c478bd9Sstevel@tonic-gate 		} else if (scf_value_get_integer(v, &t) != 0) {
8017c478bd9Sstevel@tonic-gate 			bad_error("scf_value_get_integer", scf_error());
8027c478bd9Sstevel@tonic-gate 		} else if (t > timestamp) {
8037c478bd9Sstevel@tonic-gate 			break;
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) == -1) {
8077c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8087c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8097c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8107c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8117c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8127c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8137c478bd9Sstevel@tonic-gate 			default:
8147c478bd9Sstevel@tonic-gate 				goto out;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8177c478bd9Sstevel@tonic-gate 				goto add;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8207c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8217c478bd9Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
8227c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_start", scf_error());
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 		if (transaction_property_set(tx, ent, action,
8277c478bd9Sstevel@tonic-gate 		    SCF_TYPE_INTEGER) != 0) {
8287c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8297c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8307c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8317c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8327c478bd9Sstevel@tonic-gate 			default:
8337c478bd9Sstevel@tonic-gate 				goto out;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8367c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
8377c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8387c478bd9Sstevel@tonic-gate 				bad_error("transaction_property_set",
8397c478bd9Sstevel@tonic-gate 				    scf_error());
8407c478bd9Sstevel@tonic-gate 			}
8417c478bd9Sstevel@tonic-gate 		}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 		scf_value_set_integer(v, timestamp);
8447c478bd9Sstevel@tonic-gate 		if (scf_entry_add_value(ent, v) == -1)
8457c478bd9Sstevel@tonic-gate 			bad_error("scf_entry_add_value", scf_error());
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 		trans = scf_transaction_commit(tx);
8487c478bd9Sstevel@tonic-gate 		if (trans == 1)
8497c478bd9Sstevel@tonic-gate 			break;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 		if (trans != 0) {
8527c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8537c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8547c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8557c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8567c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8577c478bd9Sstevel@tonic-gate 			default:
8587c478bd9Sstevel@tonic-gate 				goto out;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8617c478bd9Sstevel@tonic-gate 				scf_transaction_reset(tx);
8627c478bd9Sstevel@tonic-gate 				goto add;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
8657c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8667c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8677c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_commit",
8687c478bd9Sstevel@tonic-gate 				    scf_error());
8697c478bd9Sstevel@tonic-gate 			}
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
8737c478bd9Sstevel@tonic-gate 		if (scf_pg_update(pg) != 0) {
8747c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8757c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8767c478bd9Sstevel@tonic-gate 			default:
8777c478bd9Sstevel@tonic-gate 				goto out;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8807c478bd9Sstevel@tonic-gate 				goto add;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8837c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8847c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
8857c478bd9Sstevel@tonic-gate 			}
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	ret = 0;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate out:
8927c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
8937c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
8947c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
8957c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
8967c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
8977c478bd9Sstevel@tonic-gate 	return (ret);
8987c478bd9Sstevel@tonic-gate }
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate static int
9017c478bd9Sstevel@tonic-gate set_inst_action(const char *fmri, const char *action)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
9047c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
9057c478bd9Sstevel@tonic-gate 	int ret = -1;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	h = handle_create();
9087c478bd9Sstevel@tonic-gate 	if (h == NULL)
9097c478bd9Sstevel@tonic-gate 		return (-1);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	if (inst != NULL) {
9147c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
9157c478bd9Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) == 0)
9167c478bd9Sstevel@tonic-gate 			ret = set_inst_action_inst(inst, action);
9177c478bd9Sstevel@tonic-gate 		else if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
9187c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	return (ret);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate /*
9307c478bd9Sstevel@tonic-gate  * get_inst_state() gets the state string from an instance, and returns
9317c478bd9Sstevel@tonic-gate  * the SCF_STATE_* constant that coincides with the instance's current state.
9327c478bd9Sstevel@tonic-gate  */
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate static int
9357c478bd9Sstevel@tonic-gate get_inst_state(scf_instance_t *inst, scf_handle_t *h)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	scf_propertygroup_t	*pg = NULL;
9387c478bd9Sstevel@tonic-gate 	scf_property_t		*prop = NULL;
9397c478bd9Sstevel@tonic-gate 	scf_value_t		*val = NULL;
9407c478bd9Sstevel@tonic-gate 	char			state[MAX_SCF_STATE_STRING_SZ];
9417c478bd9Sstevel@tonic-gate 	int			ret = -1;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	if (((pg = scf_pg_create(h)) == NULL) ||
9447c478bd9Sstevel@tonic-gate 	    ((prop = scf_property_create(h)) == NULL) ||
9457c478bd9Sstevel@tonic-gate 	    ((val = scf_value_create(h)) == NULL))
9467c478bd9Sstevel@tonic-gate 		goto out;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	/* Pull the state property from the instance */
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1 ||
9517c478bd9Sstevel@tonic-gate 	    scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
9527c478bd9Sstevel@tonic-gate 	    scf_property_get_value(prop, val) == -1) {
9537c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
9547c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
9557c478bd9Sstevel@tonic-gate 		goto out;
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if (scf_value_get_astring(val, state, sizeof (state)) <= 0) {
9597c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INTERNAL);
9607c478bd9Sstevel@tonic-gate 		goto out;
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
9647c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_UNINIT;
9657c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
9667c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_MAINT;
9677c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0) {
9687c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_OFFLINE;
9697c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) {
9707c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_DISABLED;
9717c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
9727c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_ONLINE;
9737c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
9747c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_DEGRADED;
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate out:
9787c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
9797c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
9807c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	return (ret);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate /*
9867c478bd9Sstevel@tonic-gate  * Sets an instance to be enabled or disabled after reboot, using the
9877c478bd9Sstevel@tonic-gate  * temporary (overriding) general_ovr property group to reflect the
9887c478bd9Sstevel@tonic-gate  * present state, if it is different.
9897c478bd9Sstevel@tonic-gate  */
9907c478bd9Sstevel@tonic-gate static int
9917c478bd9Sstevel@tonic-gate set_inst_enabled_atboot(scf_instance_t *inst, uint8_t desired)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate 	int enabled;
9947c478bd9Sstevel@tonic-gate 	int persistent;
9957c478bd9Sstevel@tonic-gate 	int ret = -1;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	if ((persistent = get_inst_enabled(inst, SCF_PG_GENERAL)) < 0) {
9987c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
9997c478bd9Sstevel@tonic-gate 			goto out;
10007c478bd9Sstevel@tonic-gate 		persistent = B_FALSE;
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate 	if ((enabled = get_inst_enabled(inst, SCF_PG_GENERAL_OVR)) < 0) {
10037c478bd9Sstevel@tonic-gate 		enabled = persistent;
10047c478bd9Sstevel@tonic-gate 		if (persistent != desired) {
10057c478bd9Sstevel@tonic-gate 			/*
10067c478bd9Sstevel@tonic-gate 			 * Temporarily store the present enabled state.
10077c478bd9Sstevel@tonic-gate 			 */
10087c478bd9Sstevel@tonic-gate 			if (set_inst_enabled(inst, persistent,
10097c478bd9Sstevel@tonic-gate 			    SCF_PG_GENERAL_OVR, SCF_PG_GENERAL_OVR_FLAGS))
10107c478bd9Sstevel@tonic-gate 				goto out;
10117c478bd9Sstevel@tonic-gate 		}
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 	if (persistent != desired)
10147c478bd9Sstevel@tonic-gate 		if (set_inst_enabled(inst, desired, SCF_PG_GENERAL,
10157c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_FLAGS))
10167c478bd9Sstevel@tonic-gate 			goto out;
10177c478bd9Sstevel@tonic-gate 	if (enabled == desired)
10187c478bd9Sstevel@tonic-gate 		ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
10197c478bd9Sstevel@tonic-gate 	else
10207c478bd9Sstevel@tonic-gate 		ret = 0;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate out:
10237c478bd9Sstevel@tonic-gate 	return (ret);
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate static int
10277c478bd9Sstevel@tonic-gate set_inst_enabled_flags(const char *fmri, int flags, uint8_t desired)
10287c478bd9Sstevel@tonic-gate {
10297c478bd9Sstevel@tonic-gate 	int ret = -1;
10307c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
10317c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	if (flags & ~(SMF_TEMPORARY | SMF_AT_NEXT_BOOT) ||
10347c478bd9Sstevel@tonic-gate 	    flags & SMF_TEMPORARY && flags & SMF_AT_NEXT_BOOT) {
10357c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
10367c478bd9Sstevel@tonic-gate 		return (ret);
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	if ((h = handle_create()) == NULL)
10407c478bd9Sstevel@tonic-gate 		return (ret);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL) {
10437c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
10447c478bd9Sstevel@tonic-gate 		return (ret);
10457c478bd9Sstevel@tonic-gate 	}
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
10487c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) == -1) {
10497c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
10507c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
10517c478bd9Sstevel@tonic-gate 		goto out;
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if (flags & SMF_AT_NEXT_BOOT) {
10557c478bd9Sstevel@tonic-gate 		ret = set_inst_enabled_atboot(inst, desired);
10567c478bd9Sstevel@tonic-gate 	} else {
10577c478bd9Sstevel@tonic-gate 		if (set_inst_enabled(inst, desired, flags & SMF_TEMPORARY ?
10587c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, flags & SMF_TEMPORARY ?
10597c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_OVR_FLAGS : SCF_PG_GENERAL_FLAGS))
10607c478bd9Sstevel@tonic-gate 			goto out;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 		/*
10637c478bd9Sstevel@tonic-gate 		 * Make the persistent value effective by deleting the
10647c478bd9Sstevel@tonic-gate 		 * temporary one.
10657c478bd9Sstevel@tonic-gate 		 */
10667c478bd9Sstevel@tonic-gate 		if (flags & SMF_TEMPORARY)
10677c478bd9Sstevel@tonic-gate 			ret = 0;
10687c478bd9Sstevel@tonic-gate 		else
10697c478bd9Sstevel@tonic-gate 			ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate out:
10737c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
10747c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
10757c478bd9Sstevel@tonic-gate 	return (ret);
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate int
10797c478bd9Sstevel@tonic-gate smf_enable_instance(const char *fmri, int flags)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 	return (set_inst_enabled_flags(fmri, flags, B_TRUE));
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate int
10857c478bd9Sstevel@tonic-gate smf_disable_instance(const char *fmri, int flags)
10867c478bd9Sstevel@tonic-gate {
10877c478bd9Sstevel@tonic-gate 	return (set_inst_enabled_flags(fmri, flags, B_FALSE));
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate int
10917c478bd9Sstevel@tonic-gate _smf_refresh_instance_i(scf_instance_t *inst)
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	return (set_inst_action_inst(inst, SCF_PROPERTY_REFRESH));
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate int
10977c478bd9Sstevel@tonic-gate smf_refresh_instance(const char *instance)
10987c478bd9Sstevel@tonic-gate {
10997c478bd9Sstevel@tonic-gate 	return (set_inst_action(instance, SCF_PROPERTY_REFRESH));
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate int
11037c478bd9Sstevel@tonic-gate smf_restart_instance(const char *instance)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate 	return (set_inst_action(instance, SCF_PROPERTY_RESTART));
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate int
11097c478bd9Sstevel@tonic-gate smf_maintain_instance(const char *instance, int flags)
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate 	if (flags & SMF_TEMPORARY)
11127c478bd9Sstevel@tonic-gate 		return (set_inst_action(instance,
11137c478bd9Sstevel@tonic-gate 		    (flags & SMF_IMMEDIATE) ?
11147c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_IMMTEMP :
11157c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_TEMPORARY));
11167c478bd9Sstevel@tonic-gate 	else
11177c478bd9Sstevel@tonic-gate 		return (set_inst_action(instance,
11187c478bd9Sstevel@tonic-gate 		    (flags & SMF_IMMEDIATE) ?
11197c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_IMMEDIATE :
11207c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON));
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate int
11247c478bd9Sstevel@tonic-gate smf_degrade_instance(const char *instance, int flags)
11257c478bd9Sstevel@tonic-gate {
11267c478bd9Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
11277c478bd9Sstevel@tonic-gate 	const char			*state_str;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (flags & SMF_TEMPORARY)
11307c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
11337c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
11347c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
11377c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
11387c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	if (strcmp(state_str, SCF_STATE_STRING_ONLINE) != 0) {
11427c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
11437c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	return (set_inst_action(instance, (flags & SMF_IMMEDIATE) ?
11487c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_DEGRADE_IMMEDIATE : SCF_PROPERTY_DEGRADED));
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate int
11527c478bd9Sstevel@tonic-gate smf_restore_instance(const char *instance)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
11557c478bd9Sstevel@tonic-gate 	const char			*state_str;
11567c478bd9Sstevel@tonic-gate 	int				ret;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
11597c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
11607c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
11637c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
11647c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	if (strcmp(state_str, SCF_STATE_STRING_MAINT) == 0) {
11687c478bd9Sstevel@tonic-gate 		ret = set_inst_action(instance, SCF_PROPERTY_MAINT_OFF);
11697c478bd9Sstevel@tonic-gate 	} else if (strcmp(state_str, SCF_STATE_STRING_DEGRADED) == 0) {
11707c478bd9Sstevel@tonic-gate 		ret = set_inst_action(instance, SCF_PROPERTY_RESTORE);
11717c478bd9Sstevel@tonic-gate 	} else {
11727c478bd9Sstevel@tonic-gate 		ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
11767c478bd9Sstevel@tonic-gate 	return (ret);
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate char *
11807c478bd9Sstevel@tonic-gate smf_get_state(const char *instance)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
11837c478bd9Sstevel@tonic-gate 	const char			*state_str;
11847c478bd9Sstevel@tonic-gate 	char				*ret;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
11877c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
11887c478bd9Sstevel@tonic-gate 		return (NULL);
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
11917c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
11927c478bd9Sstevel@tonic-gate 		return (NULL);
11937c478bd9Sstevel@tonic-gate 	}
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	if ((ret = strdup(state_str)) == NULL)
11967c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
11997c478bd9Sstevel@tonic-gate 	return (ret);
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate int
12037c478bd9Sstevel@tonic-gate scf_simple_walk_instances(uint_t state_flags, void *private,
12047c478bd9Sstevel@tonic-gate     int (*inst_callback)(scf_handle_t *, scf_instance_t *, void *))
12057c478bd9Sstevel@tonic-gate {
12067c478bd9Sstevel@tonic-gate 	scf_scope_t 		*scope = NULL;
12077c478bd9Sstevel@tonic-gate 	scf_service_t		*svc = NULL;
12087c478bd9Sstevel@tonic-gate 	scf_instance_t		*inst = NULL;
12097c478bd9Sstevel@tonic-gate 	scf_iter_t		*svc_iter = NULL, *inst_iter = NULL;
12107c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
12117c478bd9Sstevel@tonic-gate 	int			ret = SCF_FAILED;
12127c478bd9Sstevel@tonic-gate 	int			svc_iter_ret, inst_iter_ret;
12137c478bd9Sstevel@tonic-gate 	int			inst_state;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	if ((h = handle_create()) == NULL)
12167c478bd9Sstevel@tonic-gate 		return (ret);
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	if (((scope = scf_scope_create(h)) == NULL) ||
12197c478bd9Sstevel@tonic-gate 	    ((svc = scf_service_create(h)) == NULL) ||
12207c478bd9Sstevel@tonic-gate 	    ((inst = scf_instance_create(h)) == NULL) ||
12217c478bd9Sstevel@tonic-gate 	    ((svc_iter = scf_iter_create(h)) == NULL) ||
12227c478bd9Sstevel@tonic-gate 	    ((inst_iter = scf_iter_create(h)) == NULL))
12237c478bd9Sstevel@tonic-gate 		goto out;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	/*
12267c478bd9Sstevel@tonic-gate 	 * Get the local scope, and set up nested iteration through every
12277c478bd9Sstevel@tonic-gate 	 * local service, and every instance of every service.
12287c478bd9Sstevel@tonic-gate 	 */
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	if ((scf_handle_get_local_scope(h, scope) != SCF_SUCCESS) ||
12317c478bd9Sstevel@tonic-gate 	    (scf_iter_scope_services(svc_iter, scope) != SCF_SUCCESS))
12327c478bd9Sstevel@tonic-gate 		goto out;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	while ((svc_iter_ret = scf_iter_next_service(svc_iter, svc)) > 0) {
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 		if ((scf_iter_service_instances(inst_iter, svc)) !=
12377c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS)
12387c478bd9Sstevel@tonic-gate 			goto out;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 		while ((inst_iter_ret =
12417c478bd9Sstevel@tonic-gate 		    scf_iter_next_instance(inst_iter, inst)) > 0) {
12427c478bd9Sstevel@tonic-gate 			/*
12437c478bd9Sstevel@tonic-gate 			 * If get_inst_state fails from an internal error,
12447c478bd9Sstevel@tonic-gate 			 * IE, being unable to get the property group or
12457c478bd9Sstevel@tonic-gate 			 * property containing the state of the instance,
12467c478bd9Sstevel@tonic-gate 			 * we continue instead of failing, as this might just
12477c478bd9Sstevel@tonic-gate 			 * be an improperly configured instance.
12487c478bd9Sstevel@tonic-gate 			 */
12497c478bd9Sstevel@tonic-gate 			if ((inst_state = get_inst_state(inst, h)) == -1) {
12507c478bd9Sstevel@tonic-gate 				if (scf_error() == SCF_ERROR_INTERNAL) {
12517c478bd9Sstevel@tonic-gate 					continue;
12527c478bd9Sstevel@tonic-gate 				} else {
12537c478bd9Sstevel@tonic-gate 					goto out;
12547c478bd9Sstevel@tonic-gate 				}
12557c478bd9Sstevel@tonic-gate 			}
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 			if ((uint_t)inst_state & state_flags) {
12587c478bd9Sstevel@tonic-gate 				if (inst_callback(h, inst, private) !=
12597c478bd9Sstevel@tonic-gate 				    SCF_SUCCESS) {
12607c478bd9Sstevel@tonic-gate 					(void) scf_set_error(
12617c478bd9Sstevel@tonic-gate 					    SCF_ERROR_CALLBACK_FAILED);
12627c478bd9Sstevel@tonic-gate 					goto out;
12637c478bd9Sstevel@tonic-gate 				}
12647c478bd9Sstevel@tonic-gate 			}
12657c478bd9Sstevel@tonic-gate 		}
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 		if (inst_iter_ret == -1)
12687c478bd9Sstevel@tonic-gate 			goto out;
12697c478bd9Sstevel@tonic-gate 		scf_iter_reset(inst_iter);
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	if (svc_iter_ret != -1)
12737c478bd9Sstevel@tonic-gate 		ret = SCF_SUCCESS;
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate out:
12767c478bd9Sstevel@tonic-gate 	scf_scope_destroy(scope);
12777c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
12787c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
12797c478bd9Sstevel@tonic-gate 	scf_iter_destroy(svc_iter);
12807c478bd9Sstevel@tonic-gate 	scf_iter_destroy(inst_iter);
12817c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	return (ret);
12847c478bd9Sstevel@tonic-gate }
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate scf_simple_prop_t *
12887c478bd9Sstevel@tonic-gate scf_simple_prop_get(scf_handle_t *hin, const char *instance, const char *pgname,
12897c478bd9Sstevel@tonic-gate 			const char *propname)
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate 	char 			*fmri_buf, *svcfmri = NULL;
12927c478bd9Sstevel@tonic-gate 	ssize_t 		fmri_sz;
12937c478bd9Sstevel@tonic-gate 	scf_property_t 		*prop = NULL;
12947c478bd9Sstevel@tonic-gate 	scf_service_t 		*svc = NULL;
12957c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 	*ret;
12967c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
12977c478bd9Sstevel@tonic-gate 	boolean_t		local_h = B_TRUE;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	/* If the user passed in a handle, use it. */
13007c478bd9Sstevel@tonic-gate 	if (hin != NULL) {
13017c478bd9Sstevel@tonic-gate 		h = hin;
13027c478bd9Sstevel@tonic-gate 		local_h = B_FALSE;
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	if (local_h && ((h = handle_create()) == NULL))
13067c478bd9Sstevel@tonic-gate 		return (NULL);
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	if ((fmri_buf = assemble_fmri(h, instance, pgname, propname)) == NULL) {
13097c478bd9Sstevel@tonic-gate 		if (local_h)
13107c478bd9Sstevel@tonic-gate 			scf_handle_destroy(h);
13117c478bd9Sstevel@tonic-gate 		return (NULL);
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	if ((svc = scf_service_create(h)) == NULL ||
13157c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL)
13167c478bd9Sstevel@tonic-gate 		goto error1;
13177c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri_buf, NULL, NULL, NULL, NULL, prop,
13187c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
13197c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
13207c478bd9Sstevel@tonic-gate 		/*
13217c478bd9Sstevel@tonic-gate 		 * If the property isn't found in the instance, we grab the
13227c478bd9Sstevel@tonic-gate 		 * underlying service, create an FMRI out of it, and then
13237c478bd9Sstevel@tonic-gate 		 * query the datastore again at the service level for the
13247c478bd9Sstevel@tonic-gate 		 * property.
13257c478bd9Sstevel@tonic-gate 		 */
13267c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
13277c478bd9Sstevel@tonic-gate 			if (scf_handle_decode_fmri(h, fmri_buf, NULL, svc,
13287c478bd9Sstevel@tonic-gate 			    NULL, NULL, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1)
13297c478bd9Sstevel@tonic-gate 				goto error1;
13307c478bd9Sstevel@tonic-gate 			if ((fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH)) ==
13317c478bd9Sstevel@tonic-gate 			    -1) {
13327c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
13337c478bd9Sstevel@tonic-gate 				goto error1;
13347c478bd9Sstevel@tonic-gate 			}
13357c478bd9Sstevel@tonic-gate 			if (scf_service_to_fmri(svc, fmri_buf, fmri_sz) == -1)
13367c478bd9Sstevel@tonic-gate 				goto error1;
13377c478bd9Sstevel@tonic-gate 			if ((svcfmri = assemble_fmri(h, fmri_buf, pgname,
13387c478bd9Sstevel@tonic-gate 			    propname)) == NULL)
13397c478bd9Sstevel@tonic-gate 				goto error1;
13407c478bd9Sstevel@tonic-gate 			if (scf_handle_decode_fmri(h, svcfmri, NULL, NULL,
13417c478bd9Sstevel@tonic-gate 			    NULL, NULL, prop, 0) == -1) {
13427c478bd9Sstevel@tonic-gate 				free(svcfmri);
13437c478bd9Sstevel@tonic-gate 				goto error1;
13447c478bd9Sstevel@tonic-gate 			}
13457c478bd9Sstevel@tonic-gate 			free(svcfmri);
13467c478bd9Sstevel@tonic-gate 			break;
13477c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
13487c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
13497c478bd9Sstevel@tonic-gate 		default:
13507c478bd9Sstevel@tonic-gate 			goto error1;
13517c478bd9Sstevel@tonic-gate 		}
13527c478bd9Sstevel@tonic-gate 	}
13537c478bd9Sstevel@tonic-gate 	/*
13547c478bd9Sstevel@tonic-gate 	 * At this point, we've successfully pulled the property from the
13557c478bd9Sstevel@tonic-gate 	 * datastore, and simply need to copy its innards into an
13567c478bd9Sstevel@tonic-gate 	 * scf_simple_prop_t.
13577c478bd9Sstevel@tonic-gate 	 */
13587c478bd9Sstevel@tonic-gate 	if ((ret = fill_prop(prop, pgname, propname, h)) == NULL)
13597c478bd9Sstevel@tonic-gate 		goto error1;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
13627c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
13637c478bd9Sstevel@tonic-gate 	free(fmri_buf);
13647c478bd9Sstevel@tonic-gate 	if (local_h)
13657c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
13667c478bd9Sstevel@tonic-gate 	return (ret);
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	/*
13697c478bd9Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
13707c478bd9Sstevel@tonic-gate 	 * for failures at various stages during the function.
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate error1:
13747c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
13757c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
13767c478bd9Sstevel@tonic-gate error2:
13777c478bd9Sstevel@tonic-gate 	free(fmri_buf);
13787c478bd9Sstevel@tonic-gate 	if (local_h)
13797c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
13807c478bd9Sstevel@tonic-gate 	return (NULL);
13817c478bd9Sstevel@tonic-gate }
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate void
13857c478bd9Sstevel@tonic-gate scf_simple_prop_free(scf_simple_prop_t *prop)
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate 	int i;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	if (prop == NULL)
13907c478bd9Sstevel@tonic-gate 		return;
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	free(prop->pr_propname);
13937c478bd9Sstevel@tonic-gate 	free(prop->pr_pgname);
13947c478bd9Sstevel@tonic-gate 	switch (prop->pr_type) {
13957c478bd9Sstevel@tonic-gate 	case SCF_TYPE_OPAQUE: {
13967c478bd9Sstevel@tonic-gate 		for (i = 0; i < prop->pr_numvalues; i++) {
13977c478bd9Sstevel@tonic-gate 			free(prop->pr_vallist[i].pv_opaque.o_value);
13987c478bd9Sstevel@tonic-gate 		}
13997c478bd9Sstevel@tonic-gate 		break;
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
14027c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
14037c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
14047c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
14057c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
14067c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
14077c478bd9Sstevel@tonic-gate 	case SCF_TYPE_URI:
14087c478bd9Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
14097c478bd9Sstevel@tonic-gate 		for (i = 0; i < prop->pr_numvalues; i++) {
14107c478bd9Sstevel@tonic-gate 			free(prop->pr_vallist[i].pv_str);
14117c478bd9Sstevel@tonic-gate 		}
14127c478bd9Sstevel@tonic-gate 		break;
14137c478bd9Sstevel@tonic-gate 	}
14147c478bd9Sstevel@tonic-gate 	default:
14157c478bd9Sstevel@tonic-gate 		break;
14167c478bd9Sstevel@tonic-gate 	}
14177c478bd9Sstevel@tonic-gate 	free(prop->pr_vallist);
14187c478bd9Sstevel@tonic-gate 	free(prop);
14197c478bd9Sstevel@tonic-gate }
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate scf_simple_app_props_t *
14237c478bd9Sstevel@tonic-gate scf_simple_app_props_get(scf_handle_t *hin, const char *inst_fmri)
14247c478bd9Sstevel@tonic-gate {
14257c478bd9Sstevel@tonic-gate 	scf_instance_t 		*inst = NULL;
14267c478bd9Sstevel@tonic-gate 	scf_service_t 		*svc = NULL;
14277c478bd9Sstevel@tonic-gate 	scf_propertygroup_t 	*pg = NULL;
14287c478bd9Sstevel@tonic-gate 	scf_property_t 		*prop = NULL;
14297c478bd9Sstevel@tonic-gate 	scf_simple_app_props_t	*ret = NULL;
14307c478bd9Sstevel@tonic-gate 	scf_iter_t		*pgiter = NULL, *propiter = NULL;
14317c478bd9Sstevel@tonic-gate 	struct scf_simple_pg	*thispg = NULL, *nextpg;
14327c478bd9Sstevel@tonic-gate 	scf_simple_prop_t	*thisprop, *nextprop;
14337c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
14347c478bd9Sstevel@tonic-gate 	int			pgiter_ret, propiter_ret;
14357c478bd9Sstevel@tonic-gate 	ssize_t			namelen;
14367c478bd9Sstevel@tonic-gate 	char 			*propname = NULL, *pgname = NULL, *sys_fmri;
14377c478bd9Sstevel@tonic-gate 	uint8_t			found;
14387c478bd9Sstevel@tonic-gate 	boolean_t		local_h = B_TRUE;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	/* If the user passed in a handle, use it. */
14417c478bd9Sstevel@tonic-gate 	if (hin != NULL) {
14427c478bd9Sstevel@tonic-gate 		h = hin;
14437c478bd9Sstevel@tonic-gate 		local_h = B_FALSE;
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	if (local_h && ((h = handle_create()) == NULL))
14477c478bd9Sstevel@tonic-gate 		return (NULL);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	if (inst_fmri == NULL) {
14507c478bd9Sstevel@tonic-gate 		if ((namelen = scf_myname(h, NULL, 0)) == -1) {
14517c478bd9Sstevel@tonic-gate 			if (local_h)
14527c478bd9Sstevel@tonic-gate 				scf_handle_destroy(h);
14537c478bd9Sstevel@tonic-gate 			return (NULL);
14547c478bd9Sstevel@tonic-gate 		}
14557c478bd9Sstevel@tonic-gate 		if ((sys_fmri = malloc(namelen + 1)) == NULL) {
14567c478bd9Sstevel@tonic-gate 			if (local_h)
14577c478bd9Sstevel@tonic-gate 				scf_handle_destroy(h);
14587c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
14597c478bd9Sstevel@tonic-gate 			return (NULL);
14607c478bd9Sstevel@tonic-gate 		}
14617c478bd9Sstevel@tonic-gate 		if (scf_myname(h, sys_fmri, namelen + 1) == -1) {
14627c478bd9Sstevel@tonic-gate 			if (local_h)
14637c478bd9Sstevel@tonic-gate 				scf_handle_destroy(h);
14647c478bd9Sstevel@tonic-gate 			free(sys_fmri);
14657c478bd9Sstevel@tonic-gate 			return (NULL);
14667c478bd9Sstevel@tonic-gate 		}
14677c478bd9Sstevel@tonic-gate 	} else {
14687c478bd9Sstevel@tonic-gate 		sys_fmri = strdup(inst_fmri);
14697c478bd9Sstevel@tonic-gate 	}
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	if ((namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH)) == -1) {
14727c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INTERNAL);
14737c478bd9Sstevel@tonic-gate 		return (NULL);
14747c478bd9Sstevel@tonic-gate 	}
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL ||
14777c478bd9Sstevel@tonic-gate 	    (svc = scf_service_create(h)) == NULL ||
14787c478bd9Sstevel@tonic-gate 	    (pgiter = scf_iter_create(h)) == NULL ||
14797c478bd9Sstevel@tonic-gate 	    (propiter = scf_iter_create(h)) == NULL ||
14807c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
14817c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL)
14827c478bd9Sstevel@tonic-gate 		goto error2;
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, sys_fmri, NULL, svc, inst, NULL, NULL,
14857c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
14867c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
14877c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
14887c478bd9Sstevel@tonic-gate 		goto error2;
14897c478bd9Sstevel@tonic-gate 	}
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	if ((ret = malloc(sizeof (*ret))) == NULL ||
14927c478bd9Sstevel@tonic-gate 	    (thispg = malloc(sizeof (*thispg))) == NULL ||
14937c478bd9Sstevel@tonic-gate 	    (propname = malloc(namelen)) == NULL ||
14947c478bd9Sstevel@tonic-gate 	    (pgname = malloc(namelen)) == NULL) {
14957c478bd9Sstevel@tonic-gate 		free(thispg);
14967c478bd9Sstevel@tonic-gate 		free(ret);
14977c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
14987c478bd9Sstevel@tonic-gate 		goto error2;
14997c478bd9Sstevel@tonic-gate 	}
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	ret->ap_fmri = sys_fmri;
15027c478bd9Sstevel@tonic-gate 	thispg->pg_name = NULL;
15037c478bd9Sstevel@tonic-gate 	thispg->pg_proplist = NULL;
15047c478bd9Sstevel@tonic-gate 	thispg->pg_next = NULL;
15057c478bd9Sstevel@tonic-gate 	ret->ap_pglist = thispg;
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	if (scf_iter_service_pgs_typed(pgiter, svc, SCF_GROUP_APPLICATION) !=
15087c478bd9Sstevel@tonic-gate 	    0) {
15097c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
15107c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
15117c478bd9Sstevel@tonic-gate 		goto error1;
15127c478bd9Sstevel@tonic-gate 	}
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
15157c478bd9Sstevel@tonic-gate 		if (thispg->pg_name != NULL) {
15167c478bd9Sstevel@tonic-gate 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
15177c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
15187c478bd9Sstevel@tonic-gate 				goto error1;
15197c478bd9Sstevel@tonic-gate 			}
15207c478bd9Sstevel@tonic-gate 			thispg->pg_next = nextpg;
15217c478bd9Sstevel@tonic-gate 			thispg = nextpg;
15227c478bd9Sstevel@tonic-gate 		} else {
15237c478bd9Sstevel@tonic-gate 			/* This is the first iteration */
15247c478bd9Sstevel@tonic-gate 			nextpg = thispg;
15257c478bd9Sstevel@tonic-gate 		}
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 		if ((nextpg->pg_name = malloc(namelen)) == NULL) {
15287c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
15297c478bd9Sstevel@tonic-gate 			goto error1;
15307c478bd9Sstevel@tonic-gate 		}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 		if (scf_pg_get_name(pg, nextpg->pg_name, namelen) < 0) {
15337c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
15347c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
15357c478bd9Sstevel@tonic-gate 			goto error1;
15367c478bd9Sstevel@tonic-gate 		}
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 		nextpg->pg_next = NULL;
15397c478bd9Sstevel@tonic-gate 		nextpg->pg_proplist = NULL;
15407c478bd9Sstevel@tonic-gate 		thisprop = NULL;
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 		scf_iter_reset(propiter);
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 		if (scf_iter_pg_properties(propiter, pg) != 0) {
15457c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
15467c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
15477c478bd9Sstevel@tonic-gate 			goto error1;
15487c478bd9Sstevel@tonic-gate 		}
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 		while ((propiter_ret = scf_iter_next_property(propiter, prop))
15517c478bd9Sstevel@tonic-gate 		    == 1) {
15527c478bd9Sstevel@tonic-gate 			if (scf_property_get_name(prop, propname, namelen) <
15537c478bd9Sstevel@tonic-gate 			    0) {
15547c478bd9Sstevel@tonic-gate 				if (scf_error() == SCF_ERROR_NOT_SET)
15557c478bd9Sstevel@tonic-gate 					(void) scf_set_error(
15567c478bd9Sstevel@tonic-gate 					    SCF_ERROR_INTERNAL);
15577c478bd9Sstevel@tonic-gate 				goto error1;
15587c478bd9Sstevel@tonic-gate 			}
15597c478bd9Sstevel@tonic-gate 			if (thisprop != NULL) {
15607c478bd9Sstevel@tonic-gate 				if ((nextprop = fill_prop(prop,
15617c478bd9Sstevel@tonic-gate 				    nextpg->pg_name, propname, h)) == NULL)
15627c478bd9Sstevel@tonic-gate 					goto error1;
15637c478bd9Sstevel@tonic-gate 				thisprop->pr_next = nextprop;
15647c478bd9Sstevel@tonic-gate 				thisprop = nextprop;
15657c478bd9Sstevel@tonic-gate 			} else {
15667c478bd9Sstevel@tonic-gate 				/* This is the first iteration */
15677c478bd9Sstevel@tonic-gate 				if ((thisprop = fill_prop(prop,
15687c478bd9Sstevel@tonic-gate 				    nextpg->pg_name, propname, h)) == NULL)
15697c478bd9Sstevel@tonic-gate 					goto error1;
15707c478bd9Sstevel@tonic-gate 				nextpg->pg_proplist = thisprop;
15717c478bd9Sstevel@tonic-gate 				nextprop = thisprop;
15727c478bd9Sstevel@tonic-gate 			}
15737c478bd9Sstevel@tonic-gate 			nextprop->pr_pg = nextpg;
15747c478bd9Sstevel@tonic-gate 			nextprop->pr_next = NULL;
15757c478bd9Sstevel@tonic-gate 		}
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 		if (propiter_ret == -1) {
15787c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
15797c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
15807c478bd9Sstevel@tonic-gate 			goto error1;
15817c478bd9Sstevel@tonic-gate 		}
15827c478bd9Sstevel@tonic-gate 	}
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	if (pgiter_ret == -1) {
15857c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
15867c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
15877c478bd9Sstevel@tonic-gate 		goto error1;
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	/*
15917c478bd9Sstevel@tonic-gate 	 * At this point, we've filled the scf_simple_app_props_t with all the
15927c478bd9Sstevel@tonic-gate 	 * properties at the service level.  Now we iterate over all the
15937c478bd9Sstevel@tonic-gate 	 * properties at the instance level, overwriting any duplicate
15947c478bd9Sstevel@tonic-gate 	 * properties, in order to provide service/instance composition.
15957c478bd9Sstevel@tonic-gate 	 */
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	scf_iter_reset(pgiter);
15987c478bd9Sstevel@tonic-gate 	scf_iter_reset(propiter);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed(pgiter, inst, SCF_GROUP_APPLICATION)
16017c478bd9Sstevel@tonic-gate 	    != 0) {
16027c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
16037c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
16047c478bd9Sstevel@tonic-gate 		goto error1;
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 		thispg = ret->ap_pglist;
16107c478bd9Sstevel@tonic-gate 		found = 0;
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 		/*
16137c478bd9Sstevel@tonic-gate 		 * Find either the end of the list, so we can append the
16147c478bd9Sstevel@tonic-gate 		 * property group, or an existing property group that matches
16157c478bd9Sstevel@tonic-gate 		 * it, so we can insert/overwrite its properties.
16167c478bd9Sstevel@tonic-gate 		 */
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 		if (scf_pg_get_name(pg, pgname, namelen) < 0) {
16197c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
16207c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
16217c478bd9Sstevel@tonic-gate 			goto error1;
16227c478bd9Sstevel@tonic-gate 		}
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 		while ((thispg != NULL) && (thispg->pg_name != NULL)) {
16257c478bd9Sstevel@tonic-gate 			if (strcmp(thispg->pg_name, pgname) == 0) {
16267c478bd9Sstevel@tonic-gate 				found = 1;
16277c478bd9Sstevel@tonic-gate 				break;
16287c478bd9Sstevel@tonic-gate 			}
16297c478bd9Sstevel@tonic-gate 			if (thispg->pg_next == NULL)
16307c478bd9Sstevel@tonic-gate 				break;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 			thispg = thispg->pg_next;
16337c478bd9Sstevel@tonic-gate 		}
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 		scf_iter_reset(propiter);
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 		if (scf_iter_pg_properties(propiter, pg) != 0) {
16387c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
16397c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
16407c478bd9Sstevel@tonic-gate 			goto error1;
16417c478bd9Sstevel@tonic-gate 		}
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 		if (found) {
16447c478bd9Sstevel@tonic-gate 			/*
16457c478bd9Sstevel@tonic-gate 			 * insert_app_props inserts or overwrites the
16467c478bd9Sstevel@tonic-gate 			 * properties in thispg.
16477c478bd9Sstevel@tonic-gate 			 */
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 			if (insert_app_props(propiter, pgname, propname,
16507c478bd9Sstevel@tonic-gate 			    thispg, prop, namelen, h) == -1)
16517c478bd9Sstevel@tonic-gate 				goto error1;
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 		} else {
16547c478bd9Sstevel@tonic-gate 			/*
16557c478bd9Sstevel@tonic-gate 			 * If the property group wasn't found, we're adding
16567c478bd9Sstevel@tonic-gate 			 * a newly allocated property group to the end of the
16577c478bd9Sstevel@tonic-gate 			 * list.
16587c478bd9Sstevel@tonic-gate 			 */
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
16617c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
16627c478bd9Sstevel@tonic-gate 				goto error1;
16637c478bd9Sstevel@tonic-gate 			}
16647c478bd9Sstevel@tonic-gate 			nextpg->pg_next = NULL;
16657c478bd9Sstevel@tonic-gate 			nextpg->pg_proplist = NULL;
16667c478bd9Sstevel@tonic-gate 			thisprop = NULL;
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 			if ((nextpg->pg_name = strdup(pgname)) == NULL) {
16697c478bd9Sstevel@tonic-gate 				free(nextpg);
16707c478bd9Sstevel@tonic-gate 				goto error1;
16717c478bd9Sstevel@tonic-gate 			}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 			if (thispg->pg_name == NULL) {
16747c478bd9Sstevel@tonic-gate 				free(thispg);
16757c478bd9Sstevel@tonic-gate 				ret->ap_pglist = nextpg;
16767c478bd9Sstevel@tonic-gate 			} else {
16777c478bd9Sstevel@tonic-gate 				thispg->pg_next = nextpg;
16787c478bd9Sstevel@tonic-gate 			}
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 			while ((propiter_ret =
16817c478bd9Sstevel@tonic-gate 			    scf_iter_next_property(propiter, prop)) == 1) {
16827c478bd9Sstevel@tonic-gate 				if (scf_property_get_name(prop, propname,
16837c478bd9Sstevel@tonic-gate 				    namelen) < 0) {
16847c478bd9Sstevel@tonic-gate 					if (scf_error() == SCF_ERROR_NOT_SET)
16857c478bd9Sstevel@tonic-gate 						(void) scf_set_error(
16867c478bd9Sstevel@tonic-gate 						    SCF_ERROR_INTERNAL);
16877c478bd9Sstevel@tonic-gate 					goto error1;
16887c478bd9Sstevel@tonic-gate 				}
16897c478bd9Sstevel@tonic-gate 				if (thisprop != NULL) {
16907c478bd9Sstevel@tonic-gate 					if ((nextprop = fill_prop(prop,
16917c478bd9Sstevel@tonic-gate 					    pgname, propname, h)) ==
16927c478bd9Sstevel@tonic-gate 					    NULL)
16937c478bd9Sstevel@tonic-gate 						goto error1;
16947c478bd9Sstevel@tonic-gate 					thisprop->pr_next = nextprop;
16957c478bd9Sstevel@tonic-gate 					thisprop = nextprop;
16967c478bd9Sstevel@tonic-gate 				} else {
16977c478bd9Sstevel@tonic-gate 					/* This is the first iteration */
16987c478bd9Sstevel@tonic-gate 					if ((thisprop = fill_prop(prop,
16997c478bd9Sstevel@tonic-gate 					    pgname, propname, h)) ==
17007c478bd9Sstevel@tonic-gate 					    NULL)
17017c478bd9Sstevel@tonic-gate 						goto error1;
17027c478bd9Sstevel@tonic-gate 					nextpg->pg_proplist = thisprop;
17037c478bd9Sstevel@tonic-gate 					nextprop = thisprop;
17047c478bd9Sstevel@tonic-gate 				}
17057c478bd9Sstevel@tonic-gate 				nextprop->pr_pg = nextpg;
17067c478bd9Sstevel@tonic-gate 				nextprop->pr_next = NULL;
17077c478bd9Sstevel@tonic-gate 			}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 			if (propiter_ret == -1) {
17107c478bd9Sstevel@tonic-gate 				if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
17117c478bd9Sstevel@tonic-gate 					(void) scf_set_error(
17127c478bd9Sstevel@tonic-gate 					    SCF_ERROR_INTERNAL);
17137c478bd9Sstevel@tonic-gate 				goto error1;
17147c478bd9Sstevel@tonic-gate 			}
17157c478bd9Sstevel@tonic-gate 		}
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	if (pgiter_ret == -1) {
17207c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
17217c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
17227c478bd9Sstevel@tonic-gate 		goto error1;
17237c478bd9Sstevel@tonic-gate 	}
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	scf_iter_destroy(pgiter);
17267c478bd9Sstevel@tonic-gate 	scf_iter_destroy(propiter);
17277c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
17287c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
17297c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
17307c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
17317c478bd9Sstevel@tonic-gate 	free(propname);
17327c478bd9Sstevel@tonic-gate 	free(pgname);
17337c478bd9Sstevel@tonic-gate 	if (local_h)
17347c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	if (ret->ap_pglist->pg_name == NULL)
17377c478bd9Sstevel@tonic-gate 		return (NULL);
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	return (ret);
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	/*
17427c478bd9Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
17437c478bd9Sstevel@tonic-gate 	 * for failures at various stages during the function.
17447c478bd9Sstevel@tonic-gate 	 */
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate error1:
17477c478bd9Sstevel@tonic-gate 	scf_simple_app_props_free(ret);
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate error2:
17507c478bd9Sstevel@tonic-gate 	scf_iter_destroy(pgiter);
17517c478bd9Sstevel@tonic-gate 	scf_iter_destroy(propiter);
17527c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
17537c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
17547c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
17557c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
17567c478bd9Sstevel@tonic-gate 	free(propname);
17577c478bd9Sstevel@tonic-gate 	free(pgname);
17587c478bd9Sstevel@tonic-gate 	if (local_h)
17597c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
17607c478bd9Sstevel@tonic-gate 	return (NULL);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate void
17657c478bd9Sstevel@tonic-gate scf_simple_app_props_free(scf_simple_app_props_t *propblock)
17667c478bd9Sstevel@tonic-gate {
17677c478bd9Sstevel@tonic-gate 	struct scf_simple_pg 	*pgthis, *pgnext;
17687c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 	*propthis, *propnext;
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	if ((propblock == NULL) || (propblock->ap_pglist == NULL))
17717c478bd9Sstevel@tonic-gate 		return;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	for (pgthis = propblock->ap_pglist; pgthis != NULL; pgthis = pgnext) {
17747c478bd9Sstevel@tonic-gate 		pgnext = pgthis->pg_next;
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 		propthis = pgthis->pg_proplist;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 		while (propthis != NULL) {
17797c478bd9Sstevel@tonic-gate 			propnext = propthis->pr_next;
17807c478bd9Sstevel@tonic-gate 			scf_simple_prop_free(propthis);
17817c478bd9Sstevel@tonic-gate 			propthis = propnext;
17827c478bd9Sstevel@tonic-gate 		}
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 		free(pgthis->pg_name);
17857c478bd9Sstevel@tonic-gate 		free(pgthis);
17867c478bd9Sstevel@tonic-gate 	}
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	free(propblock->ap_fmri);
17897c478bd9Sstevel@tonic-gate 	free(propblock);
17907c478bd9Sstevel@tonic-gate }
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate const scf_simple_prop_t *
17937c478bd9Sstevel@tonic-gate scf_simple_app_props_next(const scf_simple_app_props_t *propblock,
17947c478bd9Sstevel@tonic-gate     scf_simple_prop_t *last)
17957c478bd9Sstevel@tonic-gate {
17967c478bd9Sstevel@tonic-gate 	struct scf_simple_pg 	*this;
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	if (propblock == NULL) {
17997c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
18007c478bd9Sstevel@tonic-gate 		return (NULL);
18017c478bd9Sstevel@tonic-gate 	}
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	this = propblock->ap_pglist;
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	/*
18067c478bd9Sstevel@tonic-gate 	 * We're looking for the first property in this block if last is
18077c478bd9Sstevel@tonic-gate 	 * NULL
18087c478bd9Sstevel@tonic-gate 	 */
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	if (last == NULL) {
18117c478bd9Sstevel@tonic-gate 		/* An empty pglist is legal, it just means no properties */
18127c478bd9Sstevel@tonic-gate 		if (this == NULL) {
18137c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NONE);
18147c478bd9Sstevel@tonic-gate 			return (NULL);
18157c478bd9Sstevel@tonic-gate 		}
18167c478bd9Sstevel@tonic-gate 		/*
18177c478bd9Sstevel@tonic-gate 		 * Walk until we find a pg with a property in it, or we run
18187c478bd9Sstevel@tonic-gate 		 * out of property groups.
18197c478bd9Sstevel@tonic-gate 		 */
18207c478bd9Sstevel@tonic-gate 		while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
18217c478bd9Sstevel@tonic-gate 			this = this->pg_next;
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 		if (this->pg_proplist == NULL) {
18247c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NONE);
18257c478bd9Sstevel@tonic-gate 			return (NULL);
18267c478bd9Sstevel@tonic-gate 		}
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 		return (this->pg_proplist);
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate 	/*
18327c478bd9Sstevel@tonic-gate 	 * If last isn't NULL, then return the next prop in the property group,
18337c478bd9Sstevel@tonic-gate 	 * or walk the property groups until we find another property, or
18347c478bd9Sstevel@tonic-gate 	 * run out of property groups.
18357c478bd9Sstevel@tonic-gate 	 */
18367c478bd9Sstevel@tonic-gate 	if (last->pr_next != NULL)
18377c478bd9Sstevel@tonic-gate 		return (last->pr_next);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	if (last->pr_pg->pg_next == NULL) {
18407c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
18417c478bd9Sstevel@tonic-gate 		return (NULL);
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	this = last->pr_pg->pg_next;
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
18477c478bd9Sstevel@tonic-gate 		this = this->pg_next;
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	if (this->pg_proplist == NULL) {
18507c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
18517c478bd9Sstevel@tonic-gate 		return (NULL);
18527c478bd9Sstevel@tonic-gate 	}
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	return (this->pg_proplist);
18557c478bd9Sstevel@tonic-gate }
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate const scf_simple_prop_t *
18587c478bd9Sstevel@tonic-gate scf_simple_app_props_search(const scf_simple_app_props_t *propblock,
18597c478bd9Sstevel@tonic-gate     const char *pgname, const char *propname)
18607c478bd9Sstevel@tonic-gate {
18617c478bd9Sstevel@tonic-gate 	struct scf_simple_pg 	*pg;
18627c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 	*prop;
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	if ((propblock == NULL) || (propname == NULL)) {
18657c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
18667c478bd9Sstevel@tonic-gate 		return (NULL);
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	pg = propblock->ap_pglist;
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	/*
18727c478bd9Sstevel@tonic-gate 	 * If pgname is NULL, we're searching the default application
18737c478bd9Sstevel@tonic-gate 	 * property group, otherwise we look for the specified group.
18747c478bd9Sstevel@tonic-gate 	 */
18757c478bd9Sstevel@tonic-gate 	if (pgname == NULL) {
18767c478bd9Sstevel@tonic-gate 		while ((pg != NULL) &&
18777c478bd9Sstevel@tonic-gate 		    (strcmp(SCF_PG_APP_DEFAULT, pg->pg_name) != 0))
18787c478bd9Sstevel@tonic-gate 			pg = pg->pg_next;
18797c478bd9Sstevel@tonic-gate 	} else {
18807c478bd9Sstevel@tonic-gate 		while ((pg != NULL) && (strcmp(pgname, pg->pg_name) != 0))
18817c478bd9Sstevel@tonic-gate 			pg = pg->pg_next;
18827c478bd9Sstevel@tonic-gate 	}
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	if (pg == NULL) {
18857c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
18867c478bd9Sstevel@tonic-gate 		return (NULL);
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	prop = pg->pg_proplist;
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	while ((prop != NULL) && (strcmp(propname, prop->pr_propname) != 0))
18927c478bd9Sstevel@tonic-gate 		prop = prop->pr_next;
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
18957c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
18967c478bd9Sstevel@tonic-gate 		return (NULL);
18977c478bd9Sstevel@tonic-gate 	}
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	return (prop);
19007c478bd9Sstevel@tonic-gate }
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate void
19037c478bd9Sstevel@tonic-gate scf_simple_prop_next_reset(scf_simple_prop_t *prop)
19047c478bd9Sstevel@tonic-gate {
19057c478bd9Sstevel@tonic-gate 	if (prop == NULL)
19067c478bd9Sstevel@tonic-gate 		return;
19077c478bd9Sstevel@tonic-gate 	prop->pr_iter = 0;
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate ssize_t
19117c478bd9Sstevel@tonic-gate scf_simple_prop_numvalues(const scf_simple_prop_t *prop)
19127c478bd9Sstevel@tonic-gate {
19137c478bd9Sstevel@tonic-gate 	if (prop == NULL)
19147c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 	return (prop->pr_numvalues);
19177c478bd9Sstevel@tonic-gate }
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate scf_type_t
19217c478bd9Sstevel@tonic-gate scf_simple_prop_type(const scf_simple_prop_t *prop)
19227c478bd9Sstevel@tonic-gate {
19237c478bd9Sstevel@tonic-gate 	if (prop == NULL)
19247c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	return (prop->pr_type);
19277c478bd9Sstevel@tonic-gate }
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate char *
19317c478bd9Sstevel@tonic-gate scf_simple_prop_name(const scf_simple_prop_t *prop)
19327c478bd9Sstevel@tonic-gate {
19337c478bd9Sstevel@tonic-gate 	if ((prop == NULL) || (prop->pr_propname == NULL)) {
19347c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
19357c478bd9Sstevel@tonic-gate 		return (NULL);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	return (prop->pr_propname);
19397c478bd9Sstevel@tonic-gate }
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate char *
19437c478bd9Sstevel@tonic-gate scf_simple_prop_pgname(const scf_simple_prop_t *prop)
19447c478bd9Sstevel@tonic-gate {
19457c478bd9Sstevel@tonic-gate 	if ((prop == NULL) || (prop->pr_pgname == NULL)) {
19467c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
19477c478bd9Sstevel@tonic-gate 		return (NULL);
19487c478bd9Sstevel@tonic-gate 	}
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	return (prop->pr_pgname);
19517c478bd9Sstevel@tonic-gate }
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate static union scf_simple_prop_val *
19557c478bd9Sstevel@tonic-gate scf_next_val(scf_simple_prop_t *prop, scf_type_t type)
19567c478bd9Sstevel@tonic-gate {
19577c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
19587c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
19597c478bd9Sstevel@tonic-gate 		return (NULL);
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	switch (prop->pr_type) {
19637c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
19647c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
19657c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
19667c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
19677c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
19687c478bd9Sstevel@tonic-gate 	case SCF_TYPE_URI:
19697c478bd9Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
19707c478bd9Sstevel@tonic-gate 		if (type != SCF_TYPE_USTRING) {
19717c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
19727c478bd9Sstevel@tonic-gate 			return (NULL);
19737c478bd9Sstevel@tonic-gate 		}
19747c478bd9Sstevel@tonic-gate 		break;
19757c478bd9Sstevel@tonic-gate 		}
19767c478bd9Sstevel@tonic-gate 	default: {
19777c478bd9Sstevel@tonic-gate 		if (type != prop->pr_type) {
19787c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
19797c478bd9Sstevel@tonic-gate 			return (NULL);
19807c478bd9Sstevel@tonic-gate 		}
19817c478bd9Sstevel@tonic-gate 		break;
19827c478bd9Sstevel@tonic-gate 		}
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	if (prop->pr_iter >= prop->pr_numvalues) {
19867c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
19877c478bd9Sstevel@tonic-gate 		return (NULL);
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	return (&prop->pr_vallist[prop->pr_iter++]);
19917c478bd9Sstevel@tonic-gate }
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate uint8_t *
19957c478bd9Sstevel@tonic-gate scf_simple_prop_next_boolean(scf_simple_prop_t *prop)
19967c478bd9Sstevel@tonic-gate {
19977c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_BOOLEAN);
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	if (ret == NULL)
20027c478bd9Sstevel@tonic-gate 		return (NULL);
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	return (&ret->pv_bool);
20057c478bd9Sstevel@tonic-gate }
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate uint64_t *
20097c478bd9Sstevel@tonic-gate scf_simple_prop_next_count(scf_simple_prop_t *prop)
20107c478bd9Sstevel@tonic-gate {
20117c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_COUNT);
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 	if (ret == NULL)
20167c478bd9Sstevel@tonic-gate 		return (NULL);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	return (&ret->pv_uint);
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate int64_t *
20237c478bd9Sstevel@tonic-gate scf_simple_prop_next_integer(scf_simple_prop_t *prop)
20247c478bd9Sstevel@tonic-gate {
20257c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_INTEGER);
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	if (ret == NULL)
20307c478bd9Sstevel@tonic-gate 		return (NULL);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	return (&ret->pv_int);
20337c478bd9Sstevel@tonic-gate }
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate int64_t *
20367c478bd9Sstevel@tonic-gate scf_simple_prop_next_time(scf_simple_prop_t *prop, int32_t *nsec)
20377c478bd9Sstevel@tonic-gate {
20387c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_TIME);
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	if (ret == NULL)
20437c478bd9Sstevel@tonic-gate 		return (NULL);
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	if (nsec != NULL)
20467c478bd9Sstevel@tonic-gate 		*nsec = ret->pv_time.t_nsec;
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 	return (&ret->pv_time.t_sec);
20497c478bd9Sstevel@tonic-gate }
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate char *
20527c478bd9Sstevel@tonic-gate scf_simple_prop_next_astring(scf_simple_prop_t *prop)
20537c478bd9Sstevel@tonic-gate {
20547c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_ASTRING);
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	if (ret == NULL)
20597c478bd9Sstevel@tonic-gate 		return (NULL);
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	return (ret->pv_str);
20627c478bd9Sstevel@tonic-gate }
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate char *
20657c478bd9Sstevel@tonic-gate scf_simple_prop_next_ustring(scf_simple_prop_t *prop)
20667c478bd9Sstevel@tonic-gate {
20677c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_USTRING);
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	if (ret == NULL)
20727c478bd9Sstevel@tonic-gate 		return (NULL);
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	return (ret->pv_str);
20757c478bd9Sstevel@tonic-gate }
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate void *
20787c478bd9Sstevel@tonic-gate scf_simple_prop_next_opaque(scf_simple_prop_t *prop, size_t *length)
20797c478bd9Sstevel@tonic-gate {
20807c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_OPAQUE);
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
20857c478bd9Sstevel@tonic-gate 		*length = 0;
20867c478bd9Sstevel@tonic-gate 		return (NULL);
20877c478bd9Sstevel@tonic-gate 	}
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	*length = ret->pv_opaque.o_size;
20907c478bd9Sstevel@tonic-gate 	return (ret->pv_opaque.o_value);
20917c478bd9Sstevel@tonic-gate }
2092*94501b61Sskamm 
2093*94501b61Sskamm /*
2094*94501b61Sskamm  * Generate a filename based on the fmri and the given name and return
2095*94501b61Sskamm  * it in the buffer of MAXPATHLEN provided by the caller.
2096*94501b61Sskamm  * If temp_filename is non-zero, also generate a temporary, unique filename
2097*94501b61Sskamm  * and return it in the temp buffer of MAXPATHLEN provided by the caller.
2098*94501b61Sskamm  * The path to the generated pathname is also created.
2099*94501b61Sskamm  * Given fmri should begin with a scheme such as "svc:".
2100*94501b61Sskamm  * Returns
2101*94501b61Sskamm  *      0 on success
2102*94501b61Sskamm  *      -1 if filename would exceed MAXPATHLEN or
2103*94501b61Sskamm  *	-2 if unable to create directory to filename path
2104*94501b61Sskamm  */
2105*94501b61Sskamm int
2106*94501b61Sskamm gen_filenms_from_fmri(const char *fmri, const char *name, char *filename,
2107*94501b61Sskamm     char *temp_filename)
2108*94501b61Sskamm {
2109*94501b61Sskamm 	int		len;
2110*94501b61Sskamm 
2111*94501b61Sskamm 	len = strlen(SMF_SPEEDY_FILES_PATH);
2112*94501b61Sskamm 	len += strlen(fmri);
2113*94501b61Sskamm 	len += 2;			/* for slash and null */
2114*94501b61Sskamm 	len += strlen(name);
2115*94501b61Sskamm 	len += 6;			/* For X's needed for mkstemp */
2116*94501b61Sskamm 
2117*94501b61Sskamm 	if (len > MAXPATHLEN)
2118*94501b61Sskamm 		return (-1);
2119*94501b61Sskamm 
2120*94501b61Sskamm 	/* Construct directory name first - speedy path ends in slash */
2121*94501b61Sskamm 	(void) strcpy(filename, SMF_SPEEDY_FILES_PATH);
2122*94501b61Sskamm 	(void) strcat(filename, fmri);
2123*94501b61Sskamm 	if (mkdirp(filename, 0755) == -1) {
2124*94501b61Sskamm 		/* errno is set */
2125*94501b61Sskamm 		if (errno != EEXIST)
2126*94501b61Sskamm 			return (-2);
2127*94501b61Sskamm 	}
2128*94501b61Sskamm 
2129*94501b61Sskamm 	(void) strcat(filename, "/");
2130*94501b61Sskamm 	(void) strcat(filename, name);
2131*94501b61Sskamm 
2132*94501b61Sskamm 	if (temp_filename) {
2133*94501b61Sskamm 		(void) strcpy(temp_filename, filename);
2134*94501b61Sskamm 		(void) strcat(temp_filename, "XXXXXX");
2135*94501b61Sskamm 	}
2136*94501b61Sskamm 
2137*94501b61Sskamm 	return (0);
2138*94501b61Sskamm }
2139