17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
594501b61Sskamm  * Common Development and Distribution License (the "License").
694501b61Sskamm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
213eae19d9Swesolows 
227c478bd9Sstevel@tonic-gate /*
23c42520ebSJan Friedel  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24*afffa6e9SMarcel Telka  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include "libscf_impl.h"
287c478bd9Sstevel@tonic-gate 
291f6eb021SLiane Praza #include <assert.h>
307c478bd9Sstevel@tonic-gate #include <libuutil.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
3494501b61Sskamm #include <sys/param.h>
3594501b61Sskamm #include <errno.h>
3694501b61Sskamm #include <libgen.h>
37dfac3eb2SDavid Powell #include <assert.h>
387c478bd9Sstevel@tonic-gate #include "midlevel_impl.h"
39d75e6a5dStn #include "lowlevel_impl.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifndef NDEBUG
427c478bd9Sstevel@tonic-gate #define	bad_error(func, err)	{					\
437c478bd9Sstevel@tonic-gate 	uu_warn("%s:%d: %s failed with unexpected error %d.  Aborting.\n", \
447c478bd9Sstevel@tonic-gate 	    __FILE__, __LINE__, func, err);				\
457c478bd9Sstevel@tonic-gate 	abort();							\
467c478bd9Sstevel@tonic-gate }
477c478bd9Sstevel@tonic-gate #else
487c478bd9Sstevel@tonic-gate #define	bad_error(func, err)	abort()
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
5194501b61Sskamm /* Path to speedy files area must end with a slash */
5294501b61Sskamm #define	SMF_SPEEDY_FILES_PATH		"/etc/svc/volatile/"
5394501b61Sskamm 
54d75e6a5dStn void
55d75e6a5dStn scf_simple_handle_destroy(scf_simple_handle_t *simple_h)
56d75e6a5dStn {
57d75e6a5dStn 	if (simple_h == NULL)
58d75e6a5dStn 		return;
59d75e6a5dStn 
60d75e6a5dStn 	scf_pg_destroy(simple_h->running_pg);
61d75e6a5dStn 	scf_pg_destroy(simple_h->editing_pg);
62d75e6a5dStn 	scf_snapshot_destroy(simple_h->snap);
63d75e6a5dStn 	scf_instance_destroy(simple_h->inst);
64d75e6a5dStn 	scf_handle_destroy(simple_h->h);
65d75e6a5dStn 	uu_free(simple_h);
66d75e6a5dStn }
67d75e6a5dStn 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Given a base service FMRI and the names of a property group and property,
707c478bd9Sstevel@tonic-gate  * assemble_fmri() merges them into a property FMRI.  Note that if the base
717c478bd9Sstevel@tonic-gate  * FMRI is NULL, assemble_fmri() gets the base FMRI from scf_myname().
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static char *
757c478bd9Sstevel@tonic-gate assemble_fmri(scf_handle_t *h, const char *base, const char *pg,
767c478bd9Sstevel@tonic-gate     const char *prop)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	size_t	fmri_sz, pglen;
797c478bd9Sstevel@tonic-gate 	ssize_t baselen;
807c478bd9Sstevel@tonic-gate 	char	*fmri_buf;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
837c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
847c478bd9Sstevel@tonic-gate 		return (NULL);
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (pg == NULL)
887c478bd9Sstevel@tonic-gate 		pglen = strlen(SCF_PG_APP_DEFAULT);
897c478bd9Sstevel@tonic-gate 	else
907c478bd9Sstevel@tonic-gate 		pglen = strlen(pg);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (base == NULL) {
937c478bd9Sstevel@tonic-gate 		if ((baselen = scf_myname(h, NULL, 0)) == -1)
947c478bd9Sstevel@tonic-gate 			return (NULL);
957c478bd9Sstevel@tonic-gate 	} else {
967c478bd9Sstevel@tonic-gate 		baselen = strlen(base);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	fmri_sz = baselen + sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
1007c478bd9Sstevel@tonic-gate 	    pglen + sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
1017c478bd9Sstevel@tonic-gate 	    strlen(prop) + 1;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if ((fmri_buf = malloc(fmri_sz)) == NULL) {
1047c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1057c478bd9Sstevel@tonic-gate 		return (NULL);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (base == NULL) {
1097c478bd9Sstevel@tonic-gate 		if (scf_myname(h, fmri_buf, fmri_sz) == -1) {
1107c478bd9Sstevel@tonic-gate 			free(fmri_buf);
1117c478bd9Sstevel@tonic-gate 			return (NULL);
1127c478bd9Sstevel@tonic-gate 		}
1137c478bd9Sstevel@tonic-gate 	} else {
1147c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri_buf, base);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTYGRP_PREFIX);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (pg == NULL)
1207c478bd9Sstevel@tonic-gate 		(void) strcat(fmri_buf, SCF_PG_APP_DEFAULT);
1217c478bd9Sstevel@tonic-gate 	else
1227c478bd9Sstevel@tonic-gate 		(void) strcat(fmri_buf, pg);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTY_PREFIX);
1257c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, prop);
1267c478bd9Sstevel@tonic-gate 	return (fmri_buf);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Given a property, this function allocates and fills an scf_simple_prop_t
1317c478bd9Sstevel@tonic-gate  * with the data it contains.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static scf_simple_prop_t *
1357c478bd9Sstevel@tonic-gate fill_prop(scf_property_t *prop, const char *pgname, const char *propname,
1367c478bd9Sstevel@tonic-gate     scf_handle_t *h)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 		*ret;
1397c478bd9Sstevel@tonic-gate 	scf_iter_t 			*iter;
1407c478bd9Sstevel@tonic-gate 	scf_value_t 			*val;
1417c478bd9Sstevel@tonic-gate 	int 				iterret, i;
1427c478bd9Sstevel@tonic-gate 	ssize_t 			valsize, numvals;
1437c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val 	*vallist = NULL, *vallist_backup = NULL;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if ((ret = malloc(sizeof (*ret))) == NULL) {
1467c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1477c478bd9Sstevel@tonic-gate 		return (NULL);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	ret->pr_next = NULL;
1517c478bd9Sstevel@tonic-gate 	ret->pr_pg = NULL;
1527c478bd9Sstevel@tonic-gate 	ret->pr_iter = 0;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (pgname == NULL)
1557c478bd9Sstevel@tonic-gate 		ret->pr_pgname = strdup(SCF_PG_APP_DEFAULT);
1567c478bd9Sstevel@tonic-gate 	else
1577c478bd9Sstevel@tonic-gate 		ret->pr_pgname = strdup(pgname);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (ret->pr_pgname == NULL) {
1607c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1617c478bd9Sstevel@tonic-gate 		free(ret);
1627c478bd9Sstevel@tonic-gate 		return (NULL);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if ((ret->pr_propname = strdup(propname)) == NULL) {
1667c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1677c478bd9Sstevel@tonic-gate 		free(ret->pr_pgname);
1687c478bd9Sstevel@tonic-gate 		free(ret);
1697c478bd9Sstevel@tonic-gate 		return (NULL);
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	if (scf_property_type(prop, &ret->pr_type) == -1)
1737c478bd9Sstevel@tonic-gate 		goto error3;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
1767c478bd9Sstevel@tonic-gate 		goto error3;
1777c478bd9Sstevel@tonic-gate 	if ((val = scf_value_create(h)) == NULL) {
1787c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
1797c478bd9Sstevel@tonic-gate 		goto error3;
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) == -1)
1837c478bd9Sstevel@tonic-gate 		goto error1;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	for (numvals = 0; (iterret = scf_iter_next_value(iter, val)) == 1;
1867c478bd9Sstevel@tonic-gate 	    numvals++) {
1877c478bd9Sstevel@tonic-gate 		vallist_backup = vallist;
1887c478bd9Sstevel@tonic-gate 		if ((vallist = realloc(vallist, (numvals + 1) *
1897c478bd9Sstevel@tonic-gate 		    sizeof (*vallist))) == NULL) {
1907c478bd9Sstevel@tonic-gate 			vallist = vallist_backup;
1917c478bd9Sstevel@tonic-gate 			goto error1;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		switch (ret->pr_type) {
1957c478bd9Sstevel@tonic-gate 		case SCF_TYPE_BOOLEAN:
1967c478bd9Sstevel@tonic-gate 			if (scf_value_get_boolean(val,
1977c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_bool) == -1)
1987c478bd9Sstevel@tonic-gate 				goto error1;
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		case SCF_TYPE_COUNT:
2027c478bd9Sstevel@tonic-gate 			if (scf_value_get_count(val,
2037c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_uint) == -1)
2047c478bd9Sstevel@tonic-gate 				goto error1;
2057c478bd9Sstevel@tonic-gate 			break;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		case SCF_TYPE_INTEGER:
2087c478bd9Sstevel@tonic-gate 			if (scf_value_get_integer(val,
2097c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_int) == -1)
2107c478bd9Sstevel@tonic-gate 				goto error1;
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		case SCF_TYPE_TIME:
2147c478bd9Sstevel@tonic-gate 			if (scf_value_get_time(val,
2157c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_time.t_sec,
2167c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_time.t_nsec) == -1)
2177c478bd9Sstevel@tonic-gate 				goto error1;
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		case SCF_TYPE_ASTRING:
2217c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_str = NULL;
2227c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_astring(val, NULL, 0)) ==
2237c478bd9Sstevel@tonic-gate 			    -1)
2247c478bd9Sstevel@tonic-gate 				goto error1;
2257c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2267c478bd9Sstevel@tonic-gate 			    NULL) {
2277c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2287c478bd9Sstevel@tonic-gate 				goto error1;
2297c478bd9Sstevel@tonic-gate 			}
2307c478bd9Sstevel@tonic-gate 			if (scf_value_get_astring(val,
2317c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_str, valsize+1) == -1) {
2327c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_str);
2337c478bd9Sstevel@tonic-gate 				goto error1;
2347c478bd9Sstevel@tonic-gate 			}
2357c478bd9Sstevel@tonic-gate 			break;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		case SCF_TYPE_USTRING:
2387c478bd9Sstevel@tonic-gate 		case SCF_TYPE_HOST:
2397c478bd9Sstevel@tonic-gate 		case SCF_TYPE_HOSTNAME:
240b56bf881SAntonello Cruz 		case SCF_TYPE_NET_ADDR:
2417c478bd9Sstevel@tonic-gate 		case SCF_TYPE_NET_ADDR_V4:
2427c478bd9Sstevel@tonic-gate 		case SCF_TYPE_NET_ADDR_V6:
2437c478bd9Sstevel@tonic-gate 		case SCF_TYPE_URI:
2447c478bd9Sstevel@tonic-gate 		case SCF_TYPE_FMRI:
2457c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_str = NULL;
2467c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_ustring(val, NULL, 0)) ==
2477c478bd9Sstevel@tonic-gate 			    -1)
2487c478bd9Sstevel@tonic-gate 				goto error1;
2497c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2507c478bd9Sstevel@tonic-gate 			    NULL) {
2517c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2527c478bd9Sstevel@tonic-gate 				goto error1;
2537c478bd9Sstevel@tonic-gate 			}
2547c478bd9Sstevel@tonic-gate 			if (scf_value_get_ustring(val,
2557c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_str, valsize+1) == -1) {
2567c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_str);
2577c478bd9Sstevel@tonic-gate 				goto error1;
2587c478bd9Sstevel@tonic-gate 			}
2597c478bd9Sstevel@tonic-gate 			break;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		case SCF_TYPE_OPAQUE:
2627c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_opaque.o_value = NULL;
2637c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_opaque(val, NULL, 0)) ==
2647c478bd9Sstevel@tonic-gate 			    -1)
2657c478bd9Sstevel@tonic-gate 				goto error1;
2667c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_opaque.o_value =
2677c478bd9Sstevel@tonic-gate 			    malloc(valsize)) == NULL) {
2687c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2697c478bd9Sstevel@tonic-gate 				goto error1;
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_opaque.o_size = valsize;
2727c478bd9Sstevel@tonic-gate 			if (scf_value_get_opaque(val,
2737c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_opaque.o_value,
2747c478bd9Sstevel@tonic-gate 			    valsize) == -1) {
2757c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_opaque.o_value);
2767c478bd9Sstevel@tonic-gate 				goto error1;
2777c478bd9Sstevel@tonic-gate 			}
2787c478bd9Sstevel@tonic-gate 			break;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		default:
2817c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2827c478bd9Sstevel@tonic-gate 			goto error1;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	if (iterret == -1) {
2883eae19d9Swesolows 		int err = scf_error();
2893eae19d9Swesolows 		if (err != SCF_ERROR_CONNECTION_BROKEN &&
2903eae19d9Swesolows 		    err != SCF_ERROR_PERMISSION_DENIED)
2917c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2927c478bd9Sstevel@tonic-gate 		goto error1;
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	ret->pr_vallist = vallist;
2967c478bd9Sstevel@tonic-gate 	ret->pr_numvalues = numvals;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
2997c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	return (ret);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
3057c478bd9Sstevel@tonic-gate 	 * for failures at various stages during the function.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate error1:
3097c478bd9Sstevel@tonic-gate 	if (vallist == NULL)
3107c478bd9Sstevel@tonic-gate 		goto error2;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	switch (ret->pr_type) {
3137c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
3147c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
3157c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
3167c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
317b56bf881SAntonello Cruz 	case SCF_TYPE_NET_ADDR:
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 
539fee38f6fStn general_pg_get:
540fee38f6fStn 	if (scf_instance_get_pg(inst, SCF_PG_GENERAL, gpg) == -1) {
5417c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5427c478bd9Sstevel@tonic-gate 			goto out;
5437c478bd9Sstevel@tonic-gate 
544fee38f6fStn 		if (scf_instance_add_pg(inst, SCF_PG_GENERAL,
545fee38f6fStn 		    SCF_GROUP_FRAMEWORK, SCF_PG_GENERAL_FLAGS, gpg) == -1) {
5467c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_EXISTS)
5477c478bd9Sstevel@tonic-gate 				goto out;
548fee38f6fStn 			goto general_pg_get;
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 	}
551fee38f6fStn 
552fee38f6fStn 	if (strcmp(pgname, SCF_PG_GENERAL) != 0) {
553fee38f6fStn get:
554fee38f6fStn 		if (scf_instance_get_pg(inst, pgname, gpg) == -1) {
555fee38f6fStn 			if (scf_error() != SCF_ERROR_NOT_FOUND)
556fee38f6fStn 				goto out;
557fee38f6fStn 
558fee38f6fStn 			if (scf_instance_add_pg(inst, pgname,
559fee38f6fStn 			    SCF_GROUP_FRAMEWORK, pgflags, gpg) == -1) {
560fee38f6fStn 				if (scf_error() != SCF_ERROR_EXISTS)
561fee38f6fStn 					goto out;
562fee38f6fStn 				goto get;
563fee38f6fStn 			}
564fee38f6fStn 		}
565fee38f6fStn 	}
566fee38f6fStn 
5677c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) == -1) {
5687c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5697c478bd9Sstevel@tonic-gate 			goto out;
5707c478bd9Sstevel@tonic-gate 		else
5717c478bd9Sstevel@tonic-gate 			goto set;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	/*
5757c478bd9Sstevel@tonic-gate 	 * If it's already set the way we want, forgo the transaction.
5767c478bd9Sstevel@tonic-gate 	 */
5777c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(eprop, v) == -1) {
5787c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
5797c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
5807c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
5817c478bd9Sstevel@tonic-gate 			/* Misconfigured, so set anyway. */
5827c478bd9Sstevel@tonic-gate 			goto set;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		default:
5857c478bd9Sstevel@tonic-gate 			goto out;
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	if (scf_value_get_boolean(v, &b) == -1) {
5897c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
5907c478bd9Sstevel@tonic-gate 			goto out;
5917c478bd9Sstevel@tonic-gate 		goto set;
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 	if (b == desired) {
5947c478bd9Sstevel@tonic-gate 		ret = 0;
5957c478bd9Sstevel@tonic-gate 		goto out;
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate set:
5997c478bd9Sstevel@tonic-gate 	do {
6007c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) == -1)
6017c478bd9Sstevel@tonic-gate 			goto out;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		if (transaction_property_set(tx, ent, SCF_PROPERTY_ENABLED,
6047c478bd9Sstevel@tonic-gate 		    SCF_TYPE_BOOLEAN) != 0) {
6057c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
6067c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
6077c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
6087c478bd9Sstevel@tonic-gate 			default:
6097c478bd9Sstevel@tonic-gate 				goto out;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
6127c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
6137c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
6147c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
6157c478bd9Sstevel@tonic-gate 				bad_error("transaction_property_set",
6167c478bd9Sstevel@tonic-gate 				    scf_error());
6177c478bd9Sstevel@tonic-gate 			}
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		scf_value_set_boolean(v, desired);
6217c478bd9Sstevel@tonic-gate 		if (scf_entry_add_value(ent, v) == -1)
6227c478bd9Sstevel@tonic-gate 			goto out;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		committed = scf_transaction_commit(tx);
6257c478bd9Sstevel@tonic-gate 		if (committed == -1)
6267c478bd9Sstevel@tonic-gate 			goto out;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		if (committed == 0) { /* out-of-sync */
6317c478bd9Sstevel@tonic-gate 			if (scf_pg_update(gpg) == -1)
6327c478bd9Sstevel@tonic-gate 				goto out;
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 	} while (committed == 0);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	ret = 0;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate out:
6397c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
6407c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
6417c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
6427c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
6437c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	return (ret);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate static int
6497c478bd9Sstevel@tonic-gate delete_inst_enabled(const scf_instance_t *inst, const char *pgname)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	scf_transaction_t 	*tx = NULL;
6527c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *ent = NULL;
6537c478bd9Sstevel@tonic-gate 	scf_propertygroup_t 	*gpg = NULL;
6547c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
6557c478bd9Sstevel@tonic-gate 	int			ret = -1;
6567c478bd9Sstevel@tonic-gate 	int			committed;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
6597c478bd9Sstevel@tonic-gate 		return (-1);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
6627c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
6637c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
6647c478bd9Sstevel@tonic-gate 		goto out;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) != 0)
6677c478bd9Sstevel@tonic-gate 		goto error;
6687c478bd9Sstevel@tonic-gate 	do {
6697c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) == -1 ||
6707c478bd9Sstevel@tonic-gate 		    scf_transaction_property_delete(tx, ent,
6717c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_ENABLED) == -1 ||
6727c478bd9Sstevel@tonic-gate 		    (committed = scf_transaction_commit(tx)) == -1)
6737c478bd9Sstevel@tonic-gate 			goto error;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		if (committed == 0 && scf_pg_update(gpg) == -1)
6787c478bd9Sstevel@tonic-gate 			goto error;
6797c478bd9Sstevel@tonic-gate 	} while (committed == 0);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	ret = 0;
6827c478bd9Sstevel@tonic-gate 	goto out;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate error:
6857c478bd9Sstevel@tonic-gate 	switch (scf_error()) {
6867c478bd9Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
6877c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
6887c478bd9Sstevel@tonic-gate 		/* success */
6897c478bd9Sstevel@tonic-gate 		ret = 0;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate out:
6937c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
6947c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
6957c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	return (ret);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate /*
7017c478bd9Sstevel@tonic-gate  * Returns 0 on success or -1 on failure.  On failure leaves scf_error() set to
7027c478bd9Sstevel@tonic-gate  *   SCF_ERROR_HANDLE_DESTROYED - inst's handle has been destroyed
7037c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_BOUND - inst's handle is not bound
7047c478bd9Sstevel@tonic-gate  *   SCF_ERROR_CONNECTION_BROKEN - the repository connection was broken
7057c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_SET - inst is not set
7067c478bd9Sstevel@tonic-gate  *   SCF_ERROR_DELETED - inst was deleted
7077c478bd9Sstevel@tonic-gate  *   SCF_ERROR_PERMISSION_DENIED
7087c478bd9Sstevel@tonic-gate  *   SCF_ERROR_BACKEND_ACCESS
7097c478bd9Sstevel@tonic-gate  *   SCF_ERROR_BACKEND_READONLY
7107c478bd9Sstevel@tonic-gate  */
7117c478bd9Sstevel@tonic-gate static int
7127c478bd9Sstevel@tonic-gate set_inst_action_inst(scf_instance_t *inst, const char *action)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	scf_handle_t			*h;
7157c478bd9Sstevel@tonic-gate 	scf_transaction_t		*tx = NULL;
7167c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t		*ent = NULL;
7177c478bd9Sstevel@tonic-gate 	scf_propertygroup_t		*pg = NULL;
7187c478bd9Sstevel@tonic-gate 	scf_property_t			*prop = NULL;
7197c478bd9Sstevel@tonic-gate 	scf_value_t			*v = NULL;
7207c478bd9Sstevel@tonic-gate 	int				trans, ret = -1;
7217c478bd9Sstevel@tonic-gate 	int64_t				t;
7227c478bd9Sstevel@tonic-gate 	hrtime_t			timestamp;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL ||
7257c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
7267c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
7277c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL ||
7287c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
7297c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
7307c478bd9Sstevel@tonic-gate 		goto out;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate get:
7337c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER_ACTIONS, pg) == -1) {
7347c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
7357c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
7367c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7377c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7387c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7397c478bd9Sstevel@tonic-gate 		default:
7407c478bd9Sstevel@tonic-gate 			goto out;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7437c478bd9Sstevel@tonic-gate 			break;
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7467c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7477c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		/* Try creating the restarter_actions property group. */
7517c478bd9Sstevel@tonic-gate add:
7527c478bd9Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
7537c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_TYPE,
7547c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) {
7557c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
7567c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
7577c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7587c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
7597c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
7607c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
7617c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
7627c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
7637c478bd9Sstevel@tonic-gate 			default:
7647c478bd9Sstevel@tonic-gate 				goto out;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 			case SCF_ERROR_EXISTS:
7677c478bd9Sstevel@tonic-gate 				goto get;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
7707c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
7717c478bd9Sstevel@tonic-gate 				bad_error("scf_instance_add_pg", scf_error());
7727c478bd9Sstevel@tonic-gate 			}
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	for (;;) {
7777c478bd9Sstevel@tonic-gate 		timestamp = gethrtime();
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, action, prop) != 0) {
7807c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
7817c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7827c478bd9Sstevel@tonic-gate 			default:
7837c478bd9Sstevel@tonic-gate 				goto out;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
7867c478bd9Sstevel@tonic-gate 				goto add;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
7897c478bd9Sstevel@tonic-gate 				break;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
7927c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
7937c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
7947c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
7957c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
7967c478bd9Sstevel@tonic-gate 			}
7977c478bd9Sstevel@tonic-gate 		} else if (scf_property_get_value(prop, v) != 0) {
7987c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
7997c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8007c478bd9Sstevel@tonic-gate 			default:
8017c478bd9Sstevel@tonic-gate 				goto out;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8047c478bd9Sstevel@tonic-gate 				goto add;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
8077c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
8087c478bd9Sstevel@tonic-gate 				break;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8117c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8127c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8137c478bd9Sstevel@tonic-gate 				bad_error("scf_property_get_value",
8147c478bd9Sstevel@tonic-gate 				    scf_error());
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate 		} else if (scf_value_get_integer(v, &t) != 0) {
8177c478bd9Sstevel@tonic-gate 			bad_error("scf_value_get_integer", scf_error());
8187c478bd9Sstevel@tonic-gate 		} else if (t > timestamp) {
8197c478bd9Sstevel@tonic-gate 			break;
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) == -1) {
8237c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8247c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8257c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8267c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8277c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8287c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8297c478bd9Sstevel@tonic-gate 			default:
8307c478bd9Sstevel@tonic-gate 				goto out;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8337c478bd9Sstevel@tonic-gate 				goto add;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8367c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8377c478bd9Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
8387c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_start", scf_error());
8397c478bd9Sstevel@tonic-gate 			}
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		if (transaction_property_set(tx, ent, action,
8437c478bd9Sstevel@tonic-gate 		    SCF_TYPE_INTEGER) != 0) {
8447c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8457c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8467c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8477c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8487c478bd9Sstevel@tonic-gate 			default:
8497c478bd9Sstevel@tonic-gate 				goto out;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8527c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
8537c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8547c478bd9Sstevel@tonic-gate 				bad_error("transaction_property_set",
8557c478bd9Sstevel@tonic-gate 				    scf_error());
8567c478bd9Sstevel@tonic-gate 			}
8577c478bd9Sstevel@tonic-gate 		}
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 		scf_value_set_integer(v, timestamp);
8607c478bd9Sstevel@tonic-gate 		if (scf_entry_add_value(ent, v) == -1)
8617c478bd9Sstevel@tonic-gate 			bad_error("scf_entry_add_value", scf_error());
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		trans = scf_transaction_commit(tx);
8647c478bd9Sstevel@tonic-gate 		if (trans == 1)
8657c478bd9Sstevel@tonic-gate 			break;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 		if (trans != 0) {
8687c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8697c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8707c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8717c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8727c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8737c478bd9Sstevel@tonic-gate 			default:
8747c478bd9Sstevel@tonic-gate 				goto out;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8777c478bd9Sstevel@tonic-gate 				scf_transaction_reset(tx);
8787c478bd9Sstevel@tonic-gate 				goto add;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
8817c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8827c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8837c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_commit",
8847c478bd9Sstevel@tonic-gate 				    scf_error());
8857c478bd9Sstevel@tonic-gate 			}
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
889bc9767f9Stn 		if (scf_pg_update(pg) == -1) {
8907c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8917c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8927c478bd9Sstevel@tonic-gate 			default:
8937c478bd9Sstevel@tonic-gate 				goto out;
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8967c478bd9Sstevel@tonic-gate 				goto add;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8997c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
9007c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
9017c478bd9Sstevel@tonic-gate 			}
9027c478bd9Sstevel@tonic-gate 		}
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	ret = 0;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate out:
9087c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
9097c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
9107c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
9117c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
9127c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
9137c478bd9Sstevel@tonic-gate 	return (ret);
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate static int
9177c478bd9Sstevel@tonic-gate set_inst_action(const char *fmri, const char *action)
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
9207c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
9217c478bd9Sstevel@tonic-gate 	int ret = -1;
9227c478bd9Sstevel@tonic-gate 
923f6e214c7SGavin Maltby 	h = _scf_handle_create_and_bind(SCF_VERSION);
9247c478bd9Sstevel@tonic-gate 	if (h == NULL)
9257c478bd9Sstevel@tonic-gate 		return (-1);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	if (inst != NULL) {
9307c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
931bf6e99ccSGary Mills 		    NULL, SCF_DECODE_FMRI_EXACT) == 0) {
9327c478bd9Sstevel@tonic-gate 			ret = set_inst_action_inst(inst, action);
933bf6e99ccSGary Mills 			if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
934bf6e99ccSGary Mills 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
935bf6e99ccSGary Mills 		} else {
936bf6e99ccSGary Mills 			switch (scf_error()) {
937bf6e99ccSGary Mills 			case SCF_ERROR_CONSTRAINT_VIOLATED:
938bf6e99ccSGary Mills 				(void) scf_set_error(
939bf6e99ccSGary Mills 				    SCF_ERROR_INVALID_ARGUMENT);
940bf6e99ccSGary Mills 				break;
941bf6e99ccSGary Mills 			case SCF_ERROR_DELETED:
942bf6e99ccSGary Mills 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
943bf6e99ccSGary Mills 				break;
944bf6e99ccSGary Mills 			}
945bf6e99ccSGary Mills 		}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	return (ret);
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate /*
9577c478bd9Sstevel@tonic-gate  * get_inst_state() gets the state string from an instance, and returns
9587c478bd9Sstevel@tonic-gate  * the SCF_STATE_* constant that coincides with the instance's current state.
9597c478bd9Sstevel@tonic-gate  */
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate static int
9627c478bd9Sstevel@tonic-gate get_inst_state(scf_instance_t *inst, scf_handle_t *h)
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate 	scf_propertygroup_t	*pg = NULL;
9657c478bd9Sstevel@tonic-gate 	scf_property_t		*prop = NULL;
9667c478bd9Sstevel@tonic-gate 	scf_value_t		*val = NULL;
9677c478bd9Sstevel@tonic-gate 	char			state[MAX_SCF_STATE_STRING_SZ];
9687c478bd9Sstevel@tonic-gate 	int			ret = -1;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	if (((pg = scf_pg_create(h)) == NULL) ||
9717c478bd9Sstevel@tonic-gate 	    ((prop = scf_property_create(h)) == NULL) ||
9727c478bd9Sstevel@tonic-gate 	    ((val = scf_value_create(h)) == NULL))
9737c478bd9Sstevel@tonic-gate 		goto out;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	/* Pull the state property from the instance */
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1 ||
9787c478bd9Sstevel@tonic-gate 	    scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
9797c478bd9Sstevel@tonic-gate 	    scf_property_get_value(prop, val) == -1) {
9807c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
9817c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
9827c478bd9Sstevel@tonic-gate 		goto out;
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	if (scf_value_get_astring(val, state, sizeof (state)) <= 0) {
9867c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INTERNAL);
9877c478bd9Sstevel@tonic-gate 		goto out;
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
9917c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_UNINIT;
9927c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
9937c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_MAINT;
9947c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0) {
9957c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_OFFLINE;
9967c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) {
9977c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_DISABLED;
9987c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
9997c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_ONLINE;
10007c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
10017c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_DEGRADED;
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate out:
10057c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
10067c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
10077c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	return (ret);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate /*
10137c478bd9Sstevel@tonic-gate  * Sets an instance to be enabled or disabled after reboot, using the
10147c478bd9Sstevel@tonic-gate  * temporary (overriding) general_ovr property group to reflect the
10157c478bd9Sstevel@tonic-gate  * present state, if it is different.
10167c478bd9Sstevel@tonic-gate  */
10177c478bd9Sstevel@tonic-gate static int
10187c478bd9Sstevel@tonic-gate set_inst_enabled_atboot(scf_instance_t *inst, uint8_t desired)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	int enabled;
10217c478bd9Sstevel@tonic-gate 	int persistent;
10227c478bd9Sstevel@tonic-gate 	int ret = -1;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	if ((persistent = get_inst_enabled(inst, SCF_PG_GENERAL)) < 0) {
10257c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
10267c478bd9Sstevel@tonic-gate 			goto out;
10277c478bd9Sstevel@tonic-gate 		persistent = B_FALSE;
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 	if ((enabled = get_inst_enabled(inst, SCF_PG_GENERAL_OVR)) < 0) {
10307c478bd9Sstevel@tonic-gate 		enabled = persistent;
10317c478bd9Sstevel@tonic-gate 		if (persistent != desired) {
10327c478bd9Sstevel@tonic-gate 			/*
10337c478bd9Sstevel@tonic-gate 			 * Temporarily store the present enabled state.
10347c478bd9Sstevel@tonic-gate 			 */
10357c478bd9Sstevel@tonic-gate 			if (set_inst_enabled(inst, persistent,
10367c478bd9Sstevel@tonic-gate 			    SCF_PG_GENERAL_OVR, SCF_PG_GENERAL_OVR_FLAGS))
10377c478bd9Sstevel@tonic-gate 				goto out;
10387c478bd9Sstevel@tonic-gate 		}
10397c478bd9Sstevel@tonic-gate 	}
10407c478bd9Sstevel@tonic-gate 	if (persistent != desired)
10417c478bd9Sstevel@tonic-gate 		if (set_inst_enabled(inst, desired, SCF_PG_GENERAL,
10427c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_FLAGS))
10437c478bd9Sstevel@tonic-gate 			goto out;
10447c478bd9Sstevel@tonic-gate 	if (enabled == desired)
10457c478bd9Sstevel@tonic-gate 		ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
10467c478bd9Sstevel@tonic-gate 	else
10477c478bd9Sstevel@tonic-gate 		ret = 0;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate out:
10507c478bd9Sstevel@tonic-gate 	return (ret);
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate static int
10547c478bd9Sstevel@tonic-gate set_inst_enabled_flags(const char *fmri, int flags, uint8_t desired)
10557c478bd9Sstevel@tonic-gate {
10567c478bd9Sstevel@tonic-gate 	int ret = -1;
10577c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
10587c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	if (flags & ~(SMF_TEMPORARY | SMF_AT_NEXT_BOOT) ||
10617c478bd9Sstevel@tonic-gate 	    flags & SMF_TEMPORARY && flags & SMF_AT_NEXT_BOOT) {
10627c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
10637c478bd9Sstevel@tonic-gate 		return (ret);
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 
1066f6e214c7SGavin Maltby 	if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
10677c478bd9Sstevel@tonic-gate 		return (ret);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL) {
10707c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
10717c478bd9Sstevel@tonic-gate 		return (ret);
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
10757c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) == -1) {
10767c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
10777c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
10787c478bd9Sstevel@tonic-gate 		goto out;
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	if (flags & SMF_AT_NEXT_BOOT) {
10827c478bd9Sstevel@tonic-gate 		ret = set_inst_enabled_atboot(inst, desired);
10837c478bd9Sstevel@tonic-gate 	} else {
10847c478bd9Sstevel@tonic-gate 		if (set_inst_enabled(inst, desired, flags & SMF_TEMPORARY ?
10857c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, flags & SMF_TEMPORARY ?
10867c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_OVR_FLAGS : SCF_PG_GENERAL_FLAGS))
10877c478bd9Sstevel@tonic-gate 			goto out;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 		/*
10907c478bd9Sstevel@tonic-gate 		 * Make the persistent value effective by deleting the
10917c478bd9Sstevel@tonic-gate 		 * temporary one.
10927c478bd9Sstevel@tonic-gate 		 */
10937c478bd9Sstevel@tonic-gate 		if (flags & SMF_TEMPORARY)
10947c478bd9Sstevel@tonic-gate 			ret = 0;
10957c478bd9Sstevel@tonic-gate 		else
10967c478bd9Sstevel@tonic-gate 			ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate out:
11007c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
11017c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
1102bf6e99ccSGary Mills 	if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
1103bf6e99ccSGary Mills 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
11047c478bd9Sstevel@tonic-gate 	return (ret);
11057c478bd9Sstevel@tonic-gate }
11067c478bd9Sstevel@tonic-gate 
1107d75e6a5dStn /*
1108d75e6a5dStn  * Create and return a pg from the instance associated with the given handle.
1109d75e6a5dStn  * This function is only called in scf_transaction_setup and
1110d75e6a5dStn  * scf_transaction_restart where the h->rh_instance pointer is properly filled
1111d75e6a5dStn  * in by scf_general_setup_pg().
1112d75e6a5dStn  */
1113d75e6a5dStn static scf_propertygroup_t *
1114d75e6a5dStn get_instance_pg(scf_simple_handle_t *simple_h)
1115d75e6a5dStn {
1116d75e6a5dStn 	scf_propertygroup_t	*ret_pg = scf_pg_create(simple_h->h);
1117d75e6a5dStn 	char			*pg_name;
1118d75e6a5dStn 	ssize_t			namelen;
1119d75e6a5dStn 
1120d75e6a5dStn 	if (ret_pg == NULL) {
1121d75e6a5dStn 		return (NULL);
1122d75e6a5dStn 	}
1123d75e6a5dStn 
11241f6eb021SLiane Praza 	namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
11251f6eb021SLiane Praza 	assert(namelen > 0);
1126d75e6a5dStn 
1127d75e6a5dStn 	if ((pg_name = malloc(namelen)) == NULL) {
1128d75e6a5dStn 		if (scf_error() == SCF_ERROR_NOT_SET) {
1129d75e6a5dStn 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1130d75e6a5dStn 		}
1131d75e6a5dStn 		return (NULL);
1132d75e6a5dStn 	}
1133d75e6a5dStn 
1134d75e6a5dStn 	if (scf_pg_get_name(simple_h->running_pg, pg_name, namelen) < 0) {
1135d75e6a5dStn 		if (scf_error() == SCF_ERROR_NOT_SET) {
1136d75e6a5dStn 			(void) scf_set_error(SCF_ERROR_INTERNAL);
1137d75e6a5dStn 		}
1138d75e6a5dStn 		return (NULL);
1139d75e6a5dStn 	}
1140d75e6a5dStn 
1141d75e6a5dStn 	/* Get pg from instance */
1142d75e6a5dStn 	if (scf_instance_get_pg(simple_h->inst, pg_name, ret_pg) == -1) {
1143d75e6a5dStn 		return (NULL);
1144d75e6a5dStn 	}
1145d75e6a5dStn 
1146d75e6a5dStn 	return (ret_pg);
1147d75e6a5dStn }
1148d75e6a5dStn 
11497c478bd9Sstevel@tonic-gate int
11507c478bd9Sstevel@tonic-gate smf_enable_instance(const char *fmri, int flags)
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate 	return (set_inst_enabled_flags(fmri, flags, B_TRUE));
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate int
11567c478bd9Sstevel@tonic-gate smf_disable_instance(const char *fmri, int flags)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate 	return (set_inst_enabled_flags(fmri, flags, B_FALSE));
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate int
11627c478bd9Sstevel@tonic-gate _smf_refresh_instance_i(scf_instance_t *inst)
11637c478bd9Sstevel@tonic-gate {
11647c478bd9Sstevel@tonic-gate 	return (set_inst_action_inst(inst, SCF_PROPERTY_REFRESH));
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate 
1167f6e214c7SGavin Maltby int
1168f6e214c7SGavin Maltby _smf_refresh_all_instances(scf_service_t *s)
1169f6e214c7SGavin Maltby {
1170f6e214c7SGavin Maltby 	scf_handle_t	*h = scf_service_handle(s);
1171f6e214c7SGavin Maltby 	scf_instance_t	*i = scf_instance_create(h);
1172f6e214c7SGavin Maltby 	scf_iter_t	*it = scf_iter_create(h);
1173f6e214c7SGavin Maltby 	int err, r = -1;
1174f6e214c7SGavin Maltby 
1175f6e214c7SGavin Maltby 	if (h == NULL || i == NULL || it == NULL)
1176f6e214c7SGavin Maltby 		goto error;
1177f6e214c7SGavin Maltby 
1178f6e214c7SGavin Maltby 	if (scf_iter_service_instances(it, s) != 0)
1179f6e214c7SGavin Maltby 		goto error;
1180f6e214c7SGavin Maltby 
1181f6e214c7SGavin Maltby 	while ((err = scf_iter_next_instance(it, i)) == 1)
1182f6e214c7SGavin Maltby 		if (_smf_refresh_instance_i(i) != 0)
1183f6e214c7SGavin Maltby 			goto error;
1184f6e214c7SGavin Maltby 
1185f6e214c7SGavin Maltby 	if (err == -1)
1186f6e214c7SGavin Maltby 		goto error;
1187f6e214c7SGavin Maltby 
1188f6e214c7SGavin Maltby 	r = 0;
1189f6e214c7SGavin Maltby error:
1190f6e214c7SGavin Maltby 	scf_instance_destroy(i);
1191f6e214c7SGavin Maltby 	scf_iter_destroy(it);
1192f6e214c7SGavin Maltby 
1193f6e214c7SGavin Maltby 	return (r);
1194f6e214c7SGavin Maltby }
1195f6e214c7SGavin Maltby 
11967c478bd9Sstevel@tonic-gate int
11977c478bd9Sstevel@tonic-gate smf_refresh_instance(const char *instance)
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	return (set_inst_action(instance, SCF_PROPERTY_REFRESH));
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate int
12037c478bd9Sstevel@tonic-gate smf_restart_instance(const char *instance)
12047c478bd9Sstevel@tonic-gate {
12057c478bd9Sstevel@tonic-gate 	return (set_inst_action(instance, SCF_PROPERTY_RESTART));
12067c478bd9Sstevel@tonic-gate }
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate int
12097c478bd9Sstevel@tonic-gate smf_maintain_instance(const char *instance, int flags)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate 	if (flags & SMF_TEMPORARY)
12127c478bd9Sstevel@tonic-gate 		return (set_inst_action(instance,
12137c478bd9Sstevel@tonic-gate 		    (flags & SMF_IMMEDIATE) ?
12147c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_IMMTEMP :
12157c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_TEMPORARY));
12167c478bd9Sstevel@tonic-gate 	else
12177c478bd9Sstevel@tonic-gate 		return (set_inst_action(instance,
12187c478bd9Sstevel@tonic-gate 		    (flags & SMF_IMMEDIATE) ?
12197c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_IMMEDIATE :
12207c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON));
12217c478bd9Sstevel@tonic-gate }
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate int
12247c478bd9Sstevel@tonic-gate smf_degrade_instance(const char *instance, int flags)
12257c478bd9Sstevel@tonic-gate {
12267c478bd9Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
12277c478bd9Sstevel@tonic-gate 	const char			*state_str;
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	if (flags & SMF_TEMPORARY)
12307c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
12337c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
12347c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
12377c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12387c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
12397c478bd9Sstevel@tonic-gate 	}
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	if (strcmp(state_str, SCF_STATE_STRING_ONLINE) != 0) {
12427c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12437c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
12447c478bd9Sstevel@tonic-gate 	}
12457c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	return (set_inst_action(instance, (flags & SMF_IMMEDIATE) ?
12487c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_DEGRADE_IMMEDIATE : SCF_PROPERTY_DEGRADED));
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate int
12527c478bd9Sstevel@tonic-gate smf_restore_instance(const char *instance)
12537c478bd9Sstevel@tonic-gate {
12547c478bd9Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
12557c478bd9Sstevel@tonic-gate 	const char			*state_str;
12567c478bd9Sstevel@tonic-gate 	int				ret;
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
12597c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
12607c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
12637c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12647c478bd9Sstevel@tonic-gate 		return (SCF_FAILED);
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	if (strcmp(state_str, SCF_STATE_STRING_MAINT) == 0) {
12687c478bd9Sstevel@tonic-gate 		ret = set_inst_action(instance, SCF_PROPERTY_MAINT_OFF);
12697c478bd9Sstevel@tonic-gate 	} else if (strcmp(state_str, SCF_STATE_STRING_DEGRADED) == 0) {
12707c478bd9Sstevel@tonic-gate 		ret = set_inst_action(instance, SCF_PROPERTY_RESTORE);
12717c478bd9Sstevel@tonic-gate 	} else {
12727c478bd9Sstevel@tonic-gate 		ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
12767c478bd9Sstevel@tonic-gate 	return (ret);
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate char *
12807c478bd9Sstevel@tonic-gate smf_get_state(const char *instance)
12817c478bd9Sstevel@tonic-gate {
12827c478bd9Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
12837c478bd9Sstevel@tonic-gate 	const char			*state_str;
12847c478bd9Sstevel@tonic-gate 	char				*ret;
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
12877c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
12887c478bd9Sstevel@tonic-gate 		return (NULL);
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
12917c478bd9Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12927c478bd9Sstevel@tonic-gate 		return (NULL);
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	if ((ret = strdup(state_str)) == NULL)
12967c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	scf_simple_prop_free(prop);
12997c478bd9Sstevel@tonic-gate 	return (ret);
13007c478bd9Sstevel@tonic-gate }
13017c478bd9Sstevel@tonic-gate 
1302d75e6a5dStn /*
1303d75e6a5dStn  * scf_general_pg_setup(fmri, pg_name)
1304d75e6a5dStn  * Create a scf_simple_handle_t and fill in the instance, snapshot, and
1305d75e6a5dStn  * property group fields associated with the given fmri and property group
1306d75e6a5dStn  * name.
1307d75e6a5dStn  * Returns:
1308d75e6a5dStn  *      Handle  on success
1309d75e6a5dStn  *      Null  on error with scf_error set to:
1310d75e6a5dStn  *              SCF_ERROR_HANDLE_MISMATCH,
1311d75e6a5dStn  *              SCF_ERROR_INVALID_ARGUMENT,
1312d75e6a5dStn  *              SCF_ERROR_CONSTRAINT_VIOLATED,
1313d75e6a5dStn  *              SCF_ERROR_NOT_FOUND,
1314d75e6a5dStn  *              SCF_ERROR_NOT_SET,
1315d75e6a5dStn  *              SCF_ERROR_DELETED,
1316d75e6a5dStn  *              SCF_ERROR_NOT_BOUND,
1317d75e6a5dStn  *              SCF_ERROR_CONNECTION_BROKEN,
1318d75e6a5dStn  *              SCF_ERROR_INTERNAL,
1319d75e6a5dStn  *              SCF_ERROR_NO_RESOURCES,
1320d75e6a5dStn  *              SCF_ERROR_BACKEND_ACCESS
1321d75e6a5dStn  */
1322d75e6a5dStn scf_simple_handle_t *
1323d75e6a5dStn scf_general_pg_setup(const char *fmri, const char *pg_name)
1324d75e6a5dStn {
1325d75e6a5dStn 	scf_simple_handle_t	*ret;
1326d75e6a5dStn 
1327d75e6a5dStn 	ret = uu_zalloc(sizeof (*ret));
1328d75e6a5dStn 	if (ret == NULL) {
1329d75e6a5dStn 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1330d75e6a5dStn 		return (NULL);
1331d75e6a5dStn 	} else {
1332d75e6a5dStn 
1333f6e214c7SGavin Maltby 		ret->h = _scf_handle_create_and_bind(SCF_VERSION);
1334d75e6a5dStn 		ret->inst = scf_instance_create(ret->h);
1335d75e6a5dStn 		ret->snap = scf_snapshot_create(ret->h);
1336d75e6a5dStn 		ret->running_pg = scf_pg_create(ret->h);
1337d75e6a5dStn 	}
1338d75e6a5dStn 
1339d75e6a5dStn 	if ((ret->h == NULL) || (ret->inst == NULL) ||
1340d75e6a5dStn 	    (ret->snap == NULL) || (ret->running_pg == NULL)) {
1341d75e6a5dStn 		goto out;
1342d75e6a5dStn 	}
1343d75e6a5dStn 
1344d75e6a5dStn 	if (scf_handle_decode_fmri(ret->h, fmri, NULL, NULL, ret->inst,
1345d75e6a5dStn 	    NULL, NULL, NULL) == -1) {
1346d75e6a5dStn 		goto out;
1347d75e6a5dStn 	}
1348d75e6a5dStn 
1349d75e6a5dStn 	if ((scf_instance_get_snapshot(ret->inst, "running", ret->snap))
1350d75e6a5dStn 	    != 0) {
1351d75e6a5dStn 		goto out;
1352d75e6a5dStn 	}
1353d75e6a5dStn 
1354d75e6a5dStn 	if (scf_instance_get_pg_composed(ret->inst, ret->snap, pg_name,
1355d75e6a5dStn 	    ret->running_pg) != 0) {
1356d75e6a5dStn 		goto out;
1357d75e6a5dStn 	}
1358d75e6a5dStn 
1359d75e6a5dStn 	return (ret);
1360d75e6a5dStn 
1361d75e6a5dStn out:
1362d75e6a5dStn 	scf_simple_handle_destroy(ret);
1363d75e6a5dStn 	return (NULL);
1364d75e6a5dStn }
1365d75e6a5dStn 
1366d75e6a5dStn /*
1367d75e6a5dStn  * scf_transaction_setup(h)
1368d75e6a5dStn  * creates and starts the transaction
1369d75e6a5dStn  * Returns:
1370d75e6a5dStn  *      transaction  on success
1371d75e6a5dStn  *      NULL on failure with scf_error set to:
1372d75e6a5dStn  *      SCF_ERROR_NO_MEMORY,
1373d75e6a5dStn  *	SCF_ERROR_INVALID_ARGUMENT,
1374d75e6a5dStn  *      SCF_ERROR_HANDLE_DESTROYED,
1375d75e6a5dStn  *	SCF_ERROR_INTERNAL,
1376d75e6a5dStn  *	SCF_ERROR_NO_RESOURCES,
1377d75e6a5dStn  *      SCF_ERROR_NOT_BOUND,
1378d75e6a5dStn  *	SCF_ERROR_CONNECTION_BROKEN,
1379d75e6a5dStn  *      SCF_ERROR_NOT_SET,
1380d75e6a5dStn  *	SCF_ERROR_DELETED,
1381d75e6a5dStn  *	SCF_ERROR_CONSTRAINT_VIOLATED,
1382d75e6a5dStn  *      SCF_ERROR_HANDLE_MISMATCH,
1383d75e6a5dStn  *	SCF_ERROR_BACKEND_ACCESS,
1384d75e6a5dStn  *	SCF_ERROR_IN_USE
1385d75e6a5dStn  */
1386d75e6a5dStn scf_transaction_t *
1387d75e6a5dStn scf_transaction_setup(scf_simple_handle_t *simple_h)
1388d75e6a5dStn {
1389d75e6a5dStn 	scf_transaction_t	*tx = NULL;
1390d75e6a5dStn 
1391d75e6a5dStn 	if ((tx = scf_transaction_create(simple_h->h)) == NULL) {
1392d75e6a5dStn 		return (NULL);
1393d75e6a5dStn 	}
1394d75e6a5dStn 
1395d75e6a5dStn 	if ((simple_h->editing_pg = get_instance_pg(simple_h)) == NULL) {
1396d75e6a5dStn 		return (NULL);
1397d75e6a5dStn 	}
1398d75e6a5dStn 
1399d75e6a5dStn 	if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
1400d75e6a5dStn 		scf_pg_destroy(simple_h->editing_pg);
1401d75e6a5dStn 		simple_h->editing_pg = NULL;
1402d75e6a5dStn 		return (NULL);
1403d75e6a5dStn 	}
1404d75e6a5dStn 
1405d75e6a5dStn 	return (tx);
1406d75e6a5dStn }
1407d75e6a5dStn 
1408d75e6a5dStn int
1409d75e6a5dStn scf_transaction_restart(scf_simple_handle_t *simple_h, scf_transaction_t *tx)
1410d75e6a5dStn {
1411d75e6a5dStn 	scf_transaction_reset(tx);
1412d75e6a5dStn 
1413d75e6a5dStn 	if (scf_pg_update(simple_h->editing_pg) == -1) {
1414d75e6a5dStn 		return (SCF_FAILED);
1415d75e6a5dStn 	}
1416d75e6a5dStn 
1417d75e6a5dStn 	if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
1418d75e6a5dStn 		return (SCF_FAILED);
1419d75e6a5dStn 	}
1420d75e6a5dStn 
1421d75e6a5dStn 	return (SCF_SUCCESS);
1422d75e6a5dStn }
1423d75e6a5dStn 
1424d75e6a5dStn /*
1425d75e6a5dStn  * scf_read_count_property(scf_simple_handle_t *simple_h, char *prop_name,
1426d75e6a5dStn  * uint64_t *ret_count)
1427d75e6a5dStn  *
1428d75e6a5dStn  * For the given property name, return the count value.
1429d75e6a5dStn  * RETURNS:
1430d75e6a5dStn  *	SCF_SUCCESS
1431d75e6a5dStn  *	SCF_FAILED on failure with scf_error() set to:
1432d75e6a5dStn  *		SCF_ERROR_HANDLE_DESTROYED
1433d75e6a5dStn  *		SCF_ERROR_INTERNAL
1434d75e6a5dStn  *		SCF_ERROR_NO_RESOURCES
1435d75e6a5dStn  *		SCF_ERROR_NO_MEMORY
1436d75e6a5dStn  *		SCF_ERROR_HANDLE_MISMATCH
1437d75e6a5dStn  *		SCF_ERROR_INVALID_ARGUMENT
1438d75e6a5dStn  *		SCF_ERROR_NOT_BOUND
1439d75e6a5dStn  *		SCF_ERROR_CONNECTION_BROKEN
1440d75e6a5dStn  *		SCF_ERROR_NOT_SET
1441d75e6a5dStn  *		SCF_ERROR_DELETED
1442d75e6a5dStn  *		SCF_ERROR_BACKEND_ACCESS
1443d75e6a5dStn  *		SCF_ERROR_CONSTRAINT_VIOLATED
1444d75e6a5dStn  *		SCF_ERROR_TYPE_MISMATCH
1445d75e6a5dStn  */
1446d75e6a5dStn int
1447d75e6a5dStn scf_read_count_property(
1448d75e6a5dStn 	scf_simple_handle_t	*simple_h,
1449d75e6a5dStn 	char			*prop_name,
1450d75e6a5dStn 	uint64_t		*ret_count)
1451d75e6a5dStn {
1452d75e6a5dStn 	scf_property_t		*prop = scf_property_create(simple_h->h);
1453d75e6a5dStn 	scf_value_t		*val = scf_value_create(simple_h->h);
1454d75e6a5dStn 	int			ret = SCF_FAILED;
1455d75e6a5dStn 
1456d75e6a5dStn 	if ((val == NULL) || (prop == NULL)) {
1457d75e6a5dStn 		goto out;
1458d75e6a5dStn 	}
1459d75e6a5dStn 
1460d75e6a5dStn 	/*
1461d75e6a5dStn 	 * Get the property struct that goes with this property group and
1462d75e6a5dStn 	 * property name.
1463d75e6a5dStn 	 */
1464d75e6a5dStn 	if (scf_pg_get_property(simple_h->running_pg, prop_name, prop) != 0) {
1465d75e6a5dStn 		goto out;
1466d75e6a5dStn 	}
1467d75e6a5dStn 
1468d75e6a5dStn 	/* Get the value structure */
1469d75e6a5dStn 	if (scf_property_get_value(prop, val) == -1) {
1470d75e6a5dStn 		goto out;
1471d75e6a5dStn 	}
1472d75e6a5dStn 
1473d75e6a5dStn 	/*
1474d75e6a5dStn 	 * Now get the count value.
1475d75e6a5dStn 	 */
1476d75e6a5dStn 	if (scf_value_get_count(val, ret_count) == -1) {
1477d75e6a5dStn 		goto out;
1478d75e6a5dStn 	}
1479d75e6a5dStn 
1480d75e6a5dStn 	ret = SCF_SUCCESS;
1481d75e6a5dStn 
1482d75e6a5dStn out:
1483d75e6a5dStn 	scf_property_destroy(prop);
1484d75e6a5dStn 	scf_value_destroy(val);
1485d75e6a5dStn 	return (ret);
1486d75e6a5dStn }
1487d75e6a5dStn 
1488d75e6a5dStn /*
1489d75e6a5dStn  * scf_trans_add_count_property(trans, propname, count, create_flag)
1490d75e6a5dStn  *
1491d75e6a5dStn  * Set a count property transaction entry into the pending SMF transaction.
1492d75e6a5dStn  * The transaction is created and committed outside of this function.
1493d75e6a5dStn  * Returns:
1494d75e6a5dStn  *	SCF_SUCCESS
1495d75e6a5dStn  *	SCF_FAILED on failure with scf_error() set to:
1496d75e6a5dStn  *			SCF_ERROR_HANDLE_DESTROYED,
1497d75e6a5dStn  *			SCF_ERROR_INVALID_ARGUMENT,
1498d75e6a5dStn  *			SCF_ERROR_NO_MEMORY,
1499d75e6a5dStn  *			SCF_ERROR_HANDLE_MISMATCH,
1500d75e6a5dStn  *			SCF_ERROR_NOT_SET,
1501d75e6a5dStn  *			SCF_ERROR_IN_USE,
1502d75e6a5dStn  *			SCF_ERROR_NOT_FOUND,
1503d75e6a5dStn  *			SCF_ERROR_EXISTS,
1504d75e6a5dStn  *			SCF_ERROR_TYPE_MISMATCH,
1505d75e6a5dStn  *			SCF_ERROR_NOT_BOUND,
1506d75e6a5dStn  *			SCF_ERROR_CONNECTION_BROKEN,
1507d75e6a5dStn  *			SCF_ERROR_INTERNAL,
1508d75e6a5dStn  *			SCF_ERROR_DELETED,
1509d75e6a5dStn  *			SCF_ERROR_NO_RESOURCES,
1510d75e6a5dStn  *			SCF_ERROR_BACKEND_ACCESS
1511d75e6a5dStn  */
1512d75e6a5dStn int
1513d75e6a5dStn scf_set_count_property(
1514d75e6a5dStn 	scf_transaction_t	*trans,
1515d75e6a5dStn 	char			*propname,
1516d75e6a5dStn 	uint64_t		count,
1517d75e6a5dStn 	boolean_t		create_flag)
1518d75e6a5dStn {
1519d75e6a5dStn 	scf_handle_t		*handle = scf_transaction_handle(trans);
1520d75e6a5dStn 	scf_value_t		*value = scf_value_create(handle);
1521d75e6a5dStn 	scf_transaction_entry_t	*entry = scf_entry_create(handle);
1522d75e6a5dStn 
1523d75e6a5dStn 	if ((value == NULL) || (entry == NULL)) {
1524d75e6a5dStn 		return (SCF_FAILED);
1525d75e6a5dStn 	}
1526d75e6a5dStn 
1527d75e6a5dStn 	/*
1528d75e6a5dStn 	 * Property must be set in transaction and won't take
1529d75e6a5dStn 	 * effect until the transaction is committed.
1530d75e6a5dStn 	 *
1531d75e6a5dStn 	 * Attempt to change the current value. However, create new property
1532d75e6a5dStn 	 * if it doesn't exist and the create flag is set.
1533d75e6a5dStn 	 */
1534d75e6a5dStn 	if (scf_transaction_property_change(trans, entry, propname,
1535d75e6a5dStn 	    SCF_TYPE_COUNT) == 0) {
1536d75e6a5dStn 		scf_value_set_count(value, count);
1537d75e6a5dStn 		if (scf_entry_add_value(entry, value) == 0) {
1538d75e6a5dStn 			return (SCF_SUCCESS);
1539d75e6a5dStn 		}
1540d75e6a5dStn 	} else {
1541d75e6a5dStn 		if ((create_flag == B_TRUE) &&
1542d75e6a5dStn 		    (scf_error() == SCF_ERROR_NOT_FOUND)) {
1543d75e6a5dStn 			if (scf_transaction_property_new(trans, entry, propname,
1544d75e6a5dStn 			    SCF_TYPE_COUNT) == 0) {
1545d75e6a5dStn 				scf_value_set_count(value, count);
1546d75e6a5dStn 				if (scf_entry_add_value(entry, value) == 0) {
1547d75e6a5dStn 					return (SCF_SUCCESS);
1548d75e6a5dStn 				}
1549d75e6a5dStn 			}
1550d75e6a5dStn 		}
1551d75e6a5dStn 	}
1552d75e6a5dStn 
1553d75e6a5dStn 	/*
1554d75e6a5dStn 	 * cleanup if there were any errors that didn't leave these
1555d75e6a5dStn 	 * values where they would be cleaned up later.
1556d75e6a5dStn 	 */
1557d75e6a5dStn 	if (value != NULL)
1558d75e6a5dStn 		scf_value_destroy(value);
1559d75e6a5dStn 	if (entry != NULL)
1560d75e6a5dStn 		scf_entry_destroy(entry);
1561d75e6a5dStn 	return (SCF_FAILED);
1562d75e6a5dStn }
1563d75e6a5dStn 
15647c478bd9Sstevel@tonic-gate int
15657c478bd9Sstevel@tonic-gate scf_simple_walk_instances(uint_t state_flags, void *private,
15667c478bd9Sstevel@tonic-gate     int (*inst_callback)(scf_handle_t *, scf_instance_t *, void *))
15677c478bd9Sstevel@tonic-gate {
15687c478bd9Sstevel@tonic-gate 	scf_scope_t 		*scope = NULL;
15697c478bd9Sstevel@tonic-gate 	scf_service_t		*svc = NULL;
15707c478bd9Sstevel@tonic-gate 	scf_instance_t		*inst = NULL;
15717c478bd9Sstevel@tonic-gate 	scf_iter_t		*svc_iter = NULL, *inst_iter = NULL;
15727c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
15737c478bd9Sstevel@tonic-gate 	int			ret = SCF_FAILED;
15747c478bd9Sstevel@tonic-gate 	int			svc_iter_ret, inst_iter_ret;
15757c478bd9Sstevel@tonic-gate 	int			inst_state;
15767c478bd9Sstevel@tonic-gate 
1577f6e214c7SGavin Maltby 	if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
15787c478bd9Sstevel@tonic-gate 		return (ret);
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	if (((scope = scf_scope_create(h)) == NULL) ||
15817c478bd9Sstevel@tonic-gate 	    ((svc = scf_service_create(h)) == NULL) ||
15827c478bd9Sstevel@tonic-gate 	    ((inst = scf_instance_create(h)) == NULL) ||
15837c478bd9Sstevel@tonic-gate 	    ((svc_iter = scf_iter_create(h)) == NULL) ||
15847c478bd9Sstevel@tonic-gate 	    ((inst_iter = scf_iter_create(h)) == NULL))
15857c478bd9Sstevel@tonic-gate 		goto out;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	/*
15887c478bd9Sstevel@tonic-gate 	 * Get the local scope, and set up nested iteration through every
15897c478bd9Sstevel@tonic-gate 	 * local service, and every instance of every service.
15907c478bd9Sstevel@tonic-gate 	 */
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	if ((scf_handle_get_local_scope(h, scope) != SCF_SUCCESS) ||
15937c478bd9Sstevel@tonic-gate 	    (scf_iter_scope_services(svc_iter, scope) != SCF_SUCCESS))
15947c478bd9Sstevel@tonic-gate 		goto out;
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	while ((svc_iter_ret = scf_iter_next_service(svc_iter, svc)) > 0) {
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 		if ((scf_iter_service_instances(inst_iter, svc)) !=
15997c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS)
16007c478bd9Sstevel@tonic-gate 			goto out;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 		while ((inst_iter_ret =
16037c478bd9Sstevel@tonic-gate 		    scf_iter_next_instance(inst_iter, inst)) > 0) {
16047c478bd9Sstevel@tonic-gate 			/*
16057c478bd9Sstevel@tonic-gate 			 * If get_inst_state fails from an internal error,
16067c478bd9Sstevel@tonic-gate 			 * IE, being unable to get the property group or
16077c478bd9Sstevel@tonic-gate 			 * property containing the state of the instance,
16087c478bd9Sstevel@tonic-gate 			 * we continue instead of failing, as this might just
16097c478bd9Sstevel@tonic-gate 			 * be an improperly configured instance.
16107c478bd9Sstevel@tonic-gate 			 */
16117c478bd9Sstevel@tonic-gate 			if ((inst_state = get_inst_state(inst, h)) == -1) {
16127c478bd9Sstevel@tonic-gate 				if (scf_error() == SCF_ERROR_INTERNAL) {
16137c478bd9Sstevel@tonic-gate 					continue;
16147c478bd9Sstevel@tonic-gate 				} else {
16157c478bd9Sstevel@tonic-gate 					goto out;
16167c478bd9Sstevel@tonic-gate 				}
16177c478bd9Sstevel@tonic-gate 			}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 			if ((uint_t)inst_state & state_flags) {
16207c478bd9Sstevel@tonic-gate 				if (inst_callback(h, inst, private) !=
16217c478bd9Sstevel@tonic-gate 				    SCF_SUCCESS) {
16227c478bd9Sstevel@tonic-gate 					(void) scf_set_error(
16237c478bd9Sstevel@tonic-gate 					    SCF_ERROR_CALLBACK_FAILED);
16247c478bd9Sstevel@tonic-gate 					goto out;
16257c478bd9Sstevel@tonic-gate 				}
16267c478bd9Sstevel@tonic-gate 			}
16277c478bd9Sstevel@tonic-gate 		}
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 		if (inst_iter_ret == -1)
16307c478bd9Sstevel@tonic-gate 			goto out;
16317c478bd9Sstevel@tonic-gate 		scf_iter_reset(inst_iter);
16327c478bd9Sstevel@tonic-gate 	}
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	if (svc_iter_ret != -1)
16357c478bd9Sstevel@tonic-gate 		ret = SCF_SUCCESS;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate out:
16387c478bd9Sstevel@tonic-gate 	scf_scope_destroy(scope);
16397c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
16407c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
16417c478bd9Sstevel@tonic-gate 	scf_iter_destroy(svc_iter);
16427c478bd9Sstevel@tonic-gate 	scf_iter_destroy(inst_iter);
16437c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	return (ret);
16467c478bd9Sstevel@tonic-gate }
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate scf_simple_prop_t *
16507c478bd9Sstevel@tonic-gate scf_simple_prop_get(scf_handle_t *hin, const char *instance, const char *pgname,
16517c478bd9Sstevel@tonic-gate 			const char *propname)
16527c478bd9Sstevel@tonic-gate {
16537c478bd9Sstevel@tonic-gate 	char 			*fmri_buf, *svcfmri = NULL;
16547c478bd9Sstevel@tonic-gate 	ssize_t 		fmri_sz;
16557c478bd9Sstevel@tonic-gate 	scf_property_t 		*prop = NULL;
16567c478bd9Sstevel@tonic-gate 	scf_service_t 		*svc = NULL;
16577c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 	*ret;
16587c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
16597c478bd9Sstevel@tonic-gate 	boolean_t		local_h = B_TRUE;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	/* If the user passed in a handle, use it. */
16627c478bd9Sstevel@tonic-gate 	if (hin != NULL) {
16637c478bd9Sstevel@tonic-gate 		h = hin;
16647c478bd9Sstevel@tonic-gate 		local_h = B_FALSE;
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 
1667f6e214c7SGavin Maltby 	if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
16687c478bd9Sstevel@tonic-gate 		return (NULL);
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	if ((fmri_buf = assemble_fmri(h, instance, pgname, propname)) == NULL) {
16717c478bd9Sstevel@tonic-gate 		if (local_h)
16727c478bd9Sstevel@tonic-gate 			scf_handle_destroy(h);
16737c478bd9Sstevel@tonic-gate 		return (NULL);
16747c478bd9Sstevel@tonic-gate 	}
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	if ((svc = scf_service_create(h)) == NULL ||
16777c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL)
16787c478bd9Sstevel@tonic-gate 		goto error1;
16797c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri_buf, NULL, NULL, NULL, NULL, prop,
16807c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
16817c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
16827c478bd9Sstevel@tonic-gate 		/*
16837c478bd9Sstevel@tonic-gate 		 * If the property isn't found in the instance, we grab the
16847c478bd9Sstevel@tonic-gate 		 * underlying service, create an FMRI out of it, and then
16857c478bd9Sstevel@tonic-gate 		 * query the datastore again at the service level for the
16867c478bd9Sstevel@tonic-gate 		 * property.
16877c478bd9Sstevel@tonic-gate 		 */
16887c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
16897c478bd9Sstevel@tonic-gate 			if (scf_handle_decode_fmri(h, fmri_buf, NULL, svc,
16907c478bd9Sstevel@tonic-gate 			    NULL, NULL, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1)
16917c478bd9Sstevel@tonic-gate 				goto error1;
16921f6eb021SLiane Praza 
16931f6eb021SLiane Praza 			fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1;
16941f6eb021SLiane Praza 			assert(fmri_sz > 0);
16951f6eb021SLiane Praza 
16967c478bd9Sstevel@tonic-gate 			if (scf_service_to_fmri(svc, fmri_buf, fmri_sz) == -1)
16977c478bd9Sstevel@tonic-gate 				goto error1;
16987c478bd9Sstevel@tonic-gate 			if ((svcfmri = assemble_fmri(h, fmri_buf, pgname,
16997c478bd9Sstevel@tonic-gate 			    propname)) == NULL)
17007c478bd9Sstevel@tonic-gate 				goto error1;
17017c478bd9Sstevel@tonic-gate 			if (scf_handle_decode_fmri(h, svcfmri, NULL, NULL,
17027c478bd9Sstevel@tonic-gate 			    NULL, NULL, prop, 0) == -1) {
17037c478bd9Sstevel@tonic-gate 				free(svcfmri);
17047c478bd9Sstevel@tonic-gate 				goto error1;
17057c478bd9Sstevel@tonic-gate 			}
17067c478bd9Sstevel@tonic-gate 			free(svcfmri);
17077c478bd9Sstevel@tonic-gate 			break;
17087c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
17097c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
17107c478bd9Sstevel@tonic-gate 		default:
17117c478bd9Sstevel@tonic-gate 			goto error1;
17127c478bd9Sstevel@tonic-gate 		}
17137c478bd9Sstevel@tonic-gate 	}
17147c478bd9Sstevel@tonic-gate 	/*
17157c478bd9Sstevel@tonic-gate 	 * At this point, we've successfully pulled the property from the
17167c478bd9Sstevel@tonic-gate 	 * datastore, and simply need to copy its innards into an
17177c478bd9Sstevel@tonic-gate 	 * scf_simple_prop_t.
17187c478bd9Sstevel@tonic-gate 	 */
17197c478bd9Sstevel@tonic-gate 	if ((ret = fill_prop(prop, pgname, propname, h)) == NULL)
17207c478bd9Sstevel@tonic-gate 		goto error1;
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
17237c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
17247c478bd9Sstevel@tonic-gate 	free(fmri_buf);
17257c478bd9Sstevel@tonic-gate 	if (local_h)
17267c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
17277c478bd9Sstevel@tonic-gate 	return (ret);
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	/*
17307c478bd9Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
17317c478bd9Sstevel@tonic-gate 	 * for failures at various stages during the function.
17327c478bd9Sstevel@tonic-gate 	 */
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate error1:
17357c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
17367c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
17377c478bd9Sstevel@tonic-gate error2:
17387c478bd9Sstevel@tonic-gate 	free(fmri_buf);
17397c478bd9Sstevel@tonic-gate 	if (local_h)
17407c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
17417c478bd9Sstevel@tonic-gate 	return (NULL);
17427c478bd9Sstevel@tonic-gate }
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate void
17467c478bd9Sstevel@tonic-gate scf_simple_prop_free(scf_simple_prop_t *prop)
17477c478bd9Sstevel@tonic-gate {
17487c478bd9Sstevel@tonic-gate 	int i;
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	if (prop == NULL)
17517c478bd9Sstevel@tonic-gate 		return;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	free(prop->pr_propname);
17547c478bd9Sstevel@tonic-gate 	free(prop->pr_pgname);
17557c478bd9Sstevel@tonic-gate 	switch (prop->pr_type) {
17567c478bd9Sstevel@tonic-gate 	case SCF_TYPE_OPAQUE: {
17577c478bd9Sstevel@tonic-gate 		for (i = 0; i < prop->pr_numvalues; i++) {
17587c478bd9Sstevel@tonic-gate 			free(prop->pr_vallist[i].pv_opaque.o_value);
17597c478bd9Sstevel@tonic-gate 		}
17607c478bd9Sstevel@tonic-gate 		break;
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
17637c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
17647c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
17657c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
1766b56bf881SAntonello Cruz 	case SCF_TYPE_NET_ADDR:
17677c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
17687c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
17697c478bd9Sstevel@tonic-gate 	case SCF_TYPE_URI:
17707c478bd9Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
17717c478bd9Sstevel@tonic-gate 		for (i = 0; i < prop->pr_numvalues; i++) {
17727c478bd9Sstevel@tonic-gate 			free(prop->pr_vallist[i].pv_str);
17737c478bd9Sstevel@tonic-gate 		}
17747c478bd9Sstevel@tonic-gate 		break;
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 	default:
17777c478bd9Sstevel@tonic-gate 		break;
17787c478bd9Sstevel@tonic-gate 	}
17797c478bd9Sstevel@tonic-gate 	free(prop->pr_vallist);
17807c478bd9Sstevel@tonic-gate 	free(prop);
17817c478bd9Sstevel@tonic-gate }
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate scf_simple_app_props_t *
17857c478bd9Sstevel@tonic-gate scf_simple_app_props_get(scf_handle_t *hin, const char *inst_fmri)
17867c478bd9Sstevel@tonic-gate {
17877c478bd9Sstevel@tonic-gate 	scf_instance_t 		*inst = NULL;
17887c478bd9Sstevel@tonic-gate 	scf_service_t 		*svc = NULL;
17897c478bd9Sstevel@tonic-gate 	scf_propertygroup_t 	*pg = NULL;
17907c478bd9Sstevel@tonic-gate 	scf_property_t 		*prop = NULL;
17917c478bd9Sstevel@tonic-gate 	scf_simple_app_props_t	*ret = NULL;
17927c478bd9Sstevel@tonic-gate 	scf_iter_t		*pgiter = NULL, *propiter = NULL;
17937c478bd9Sstevel@tonic-gate 	struct scf_simple_pg	*thispg = NULL, *nextpg;
17947c478bd9Sstevel@tonic-gate 	scf_simple_prop_t	*thisprop, *nextprop;
17957c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
17967c478bd9Sstevel@tonic-gate 	int			pgiter_ret, propiter_ret;
17977c478bd9Sstevel@tonic-gate 	ssize_t			namelen;
17987c478bd9Sstevel@tonic-gate 	char 			*propname = NULL, *pgname = NULL, *sys_fmri;
17997c478bd9Sstevel@tonic-gate 	uint8_t			found;
18007c478bd9Sstevel@tonic-gate 	boolean_t		local_h = B_TRUE;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	/* If the user passed in a handle, use it. */
18037c478bd9Sstevel@tonic-gate 	if (hin != NULL) {
18047c478bd9Sstevel@tonic-gate 		h = hin;
18057c478bd9Sstevel@tonic-gate 		local_h = B_FALSE;
18067c478bd9Sstevel@tonic-gate 	}
18077c478bd9Sstevel@tonic-gate 
1808f6e214c7SGavin Maltby 	if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
18097c478bd9Sstevel@tonic-gate 		return (NULL);
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	if (inst_fmri == NULL) {
18127c478bd9Sstevel@tonic-gate 		if ((namelen = scf_myname(h, NULL, 0)) == -1) {
18137c478bd9Sstevel@tonic-gate 			if (local_h)
18147c478bd9Sstevel@tonic-gate 				scf_handle_destroy(h);
18157c478bd9Sstevel@tonic-gate 			return (NULL);
18167c478bd9Sstevel@tonic-gate 		}
18177c478bd9Sstevel@tonic-gate 		if ((sys_fmri = malloc(namelen + 1)) == NULL) {
18187c478bd9Sstevel@tonic-gate 			if (local_h)
18197c478bd9Sstevel@tonic-gate 				scf_handle_destroy(h);
18207c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18217c478bd9Sstevel@tonic-gate 			return (NULL);
18227c478bd9Sstevel@tonic-gate 		}
18237c478bd9Sstevel@tonic-gate 		if (scf_myname(h, sys_fmri, namelen + 1) == -1) {
18247c478bd9Sstevel@tonic-gate 			if (local_h)
18257c478bd9Sstevel@tonic-gate 				scf_handle_destroy(h);
18267c478bd9Sstevel@tonic-gate 			free(sys_fmri);
18277c478bd9Sstevel@tonic-gate 			return (NULL);
18287c478bd9Sstevel@tonic-gate 		}
18297c478bd9Sstevel@tonic-gate 	} else {
18303eae19d9Swesolows 		if ((sys_fmri = strdup(inst_fmri)) == NULL) {
18313eae19d9Swesolows 			if (local_h)
18323eae19d9Swesolows 				scf_handle_destroy(h);
18333eae19d9Swesolows 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18343eae19d9Swesolows 			return (NULL);
18353eae19d9Swesolows 		}
18367c478bd9Sstevel@tonic-gate 	}
18377c478bd9Sstevel@tonic-gate 
18381f6eb021SLiane Praza 	namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
18391f6eb021SLiane Praza 	assert(namelen > 0);
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL ||
18427c478bd9Sstevel@tonic-gate 	    (svc = scf_service_create(h)) == NULL ||
18437c478bd9Sstevel@tonic-gate 	    (pgiter = scf_iter_create(h)) == NULL ||
18447c478bd9Sstevel@tonic-gate 	    (propiter = scf_iter_create(h)) == NULL ||
18457c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
1846*afffa6e9SMarcel Telka 	    (prop = scf_property_create(h)) == NULL) {
1847*afffa6e9SMarcel Telka 		free(sys_fmri);
18487c478bd9Sstevel@tonic-gate 		goto error2;
1849*afffa6e9SMarcel Telka 	}
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, sys_fmri, NULL, svc, inst, NULL, NULL,
18527c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
1853*afffa6e9SMarcel Telka 		free(sys_fmri);
18547c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
18557c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
18567c478bd9Sstevel@tonic-gate 		goto error2;
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	if ((ret = malloc(sizeof (*ret))) == NULL ||
18607c478bd9Sstevel@tonic-gate 	    (thispg = malloc(sizeof (*thispg))) == NULL ||
18617c478bd9Sstevel@tonic-gate 	    (propname = malloc(namelen)) == NULL ||
18627c478bd9Sstevel@tonic-gate 	    (pgname = malloc(namelen)) == NULL) {
18637c478bd9Sstevel@tonic-gate 		free(thispg);
18647c478bd9Sstevel@tonic-gate 		free(ret);
1865*afffa6e9SMarcel Telka 		free(sys_fmri);
18667c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18677c478bd9Sstevel@tonic-gate 		goto error2;
18687c478bd9Sstevel@tonic-gate 	}
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	ret->ap_fmri = sys_fmri;
18717c478bd9Sstevel@tonic-gate 	thispg->pg_name = NULL;
18727c478bd9Sstevel@tonic-gate 	thispg->pg_proplist = NULL;
18737c478bd9Sstevel@tonic-gate 	thispg->pg_next = NULL;
18747c478bd9Sstevel@tonic-gate 	ret->ap_pglist = thispg;
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	if (scf_iter_service_pgs_typed(pgiter, svc, SCF_GROUP_APPLICATION) !=
18777c478bd9Sstevel@tonic-gate 	    0) {
18787c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
18797c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
18807c478bd9Sstevel@tonic-gate 		goto error1;
18817c478bd9Sstevel@tonic-gate 	}
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
18847c478bd9Sstevel@tonic-gate 		if (thispg->pg_name != NULL) {
18857c478bd9Sstevel@tonic-gate 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
18867c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18877c478bd9Sstevel@tonic-gate 				goto error1;
18887c478bd9Sstevel@tonic-gate 			}
18893eae19d9Swesolows 			nextpg->pg_name = NULL;
18903eae19d9Swesolows 			nextpg->pg_next = NULL;
18913eae19d9Swesolows 			nextpg->pg_proplist = NULL;
18927c478bd9Sstevel@tonic-gate 			thispg->pg_next = nextpg;
18937c478bd9Sstevel@tonic-gate 			thispg = nextpg;
18947c478bd9Sstevel@tonic-gate 		} else {
18957c478bd9Sstevel@tonic-gate 			/* This is the first iteration */
18967c478bd9Sstevel@tonic-gate 			nextpg = thispg;
18977c478bd9Sstevel@tonic-gate 		}
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 		if ((nextpg->pg_name = malloc(namelen)) == NULL) {
19007c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
19017c478bd9Sstevel@tonic-gate 			goto error1;
19027c478bd9Sstevel@tonic-gate 		}
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 		if (scf_pg_get_name(pg, nextpg->pg_name, namelen) < 0) {
19057c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
19067c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19077c478bd9Sstevel@tonic-gate 			goto error1;
19087c478bd9Sstevel@tonic-gate 		}
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 		thisprop = NULL;
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 		scf_iter_reset(propiter);
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 		if (scf_iter_pg_properties(propiter, pg) != 0) {
19157c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19167c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19177c478bd9Sstevel@tonic-gate 			goto error1;
19187c478bd9Sstevel@tonic-gate 		}
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 		while ((propiter_ret = scf_iter_next_property(propiter, prop))
19217c478bd9Sstevel@tonic-gate 		    == 1) {
19227c478bd9Sstevel@tonic-gate 			if (scf_property_get_name(prop, propname, namelen) <
19237c478bd9Sstevel@tonic-gate 			    0) {
19247c478bd9Sstevel@tonic-gate 				if (scf_error() == SCF_ERROR_NOT_SET)
19257c478bd9Sstevel@tonic-gate 					(void) scf_set_error(
19267c478bd9Sstevel@tonic-gate 					    SCF_ERROR_INTERNAL);
19277c478bd9Sstevel@tonic-gate 				goto error1;
19287c478bd9Sstevel@tonic-gate 			}
19297c478bd9Sstevel@tonic-gate 			if (thisprop != NULL) {
19307c478bd9Sstevel@tonic-gate 				if ((nextprop = fill_prop(prop,
19317c478bd9Sstevel@tonic-gate 				    nextpg->pg_name, propname, h)) == NULL)
19327c478bd9Sstevel@tonic-gate 					goto error1;
19337c478bd9Sstevel@tonic-gate 				thisprop->pr_next = nextprop;
19347c478bd9Sstevel@tonic-gate 				thisprop = nextprop;
19357c478bd9Sstevel@tonic-gate 			} else {
19367c478bd9Sstevel@tonic-gate 				/* This is the first iteration */
19377c478bd9Sstevel@tonic-gate 				if ((thisprop = fill_prop(prop,
19387c478bd9Sstevel@tonic-gate 				    nextpg->pg_name, propname, h)) == NULL)
19397c478bd9Sstevel@tonic-gate 					goto error1;
19407c478bd9Sstevel@tonic-gate 				nextpg->pg_proplist = thisprop;
19417c478bd9Sstevel@tonic-gate 				nextprop = thisprop;
19427c478bd9Sstevel@tonic-gate 			}
19437c478bd9Sstevel@tonic-gate 			nextprop->pr_pg = nextpg;
19447c478bd9Sstevel@tonic-gate 			nextprop->pr_next = NULL;
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 		if (propiter_ret == -1) {
19487c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19497c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19507c478bd9Sstevel@tonic-gate 			goto error1;
19517c478bd9Sstevel@tonic-gate 		}
19527c478bd9Sstevel@tonic-gate 	}
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if (pgiter_ret == -1) {
19557c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19567c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
19577c478bd9Sstevel@tonic-gate 		goto error1;
19587c478bd9Sstevel@tonic-gate 	}
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	/*
19617c478bd9Sstevel@tonic-gate 	 * At this point, we've filled the scf_simple_app_props_t with all the
19627c478bd9Sstevel@tonic-gate 	 * properties at the service level.  Now we iterate over all the
19637c478bd9Sstevel@tonic-gate 	 * properties at the instance level, overwriting any duplicate
19647c478bd9Sstevel@tonic-gate 	 * properties, in order to provide service/instance composition.
19657c478bd9Sstevel@tonic-gate 	 */
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	scf_iter_reset(pgiter);
19687c478bd9Sstevel@tonic-gate 	scf_iter_reset(propiter);
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed(pgiter, inst, SCF_GROUP_APPLICATION)
19717c478bd9Sstevel@tonic-gate 	    != 0) {
19727c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19737c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
19747c478bd9Sstevel@tonic-gate 		goto error1;
19757c478bd9Sstevel@tonic-gate 	}
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 		thispg = ret->ap_pglist;
19807c478bd9Sstevel@tonic-gate 		found = 0;
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		/*
19837c478bd9Sstevel@tonic-gate 		 * Find either the end of the list, so we can append the
19847c478bd9Sstevel@tonic-gate 		 * property group, or an existing property group that matches
19857c478bd9Sstevel@tonic-gate 		 * it, so we can insert/overwrite its properties.
19867c478bd9Sstevel@tonic-gate 		 */
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 		if (scf_pg_get_name(pg, pgname, namelen) < 0) {
19897c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
19907c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19917c478bd9Sstevel@tonic-gate 			goto error1;
19927c478bd9Sstevel@tonic-gate 		}
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 		while ((thispg != NULL) && (thispg->pg_name != NULL)) {
19957c478bd9Sstevel@tonic-gate 			if (strcmp(thispg->pg_name, pgname) == 0) {
19967c478bd9Sstevel@tonic-gate 				found = 1;
19977c478bd9Sstevel@tonic-gate 				break;
19987c478bd9Sstevel@tonic-gate 			}
19997c478bd9Sstevel@tonic-gate 			if (thispg->pg_next == NULL)
20007c478bd9Sstevel@tonic-gate 				break;
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 			thispg = thispg->pg_next;
20037c478bd9Sstevel@tonic-gate 		}
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 		scf_iter_reset(propiter);
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 		if (scf_iter_pg_properties(propiter, pg) != 0) {
20087c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20097c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
20107c478bd9Sstevel@tonic-gate 			goto error1;
20117c478bd9Sstevel@tonic-gate 		}
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 		if (found) {
20147c478bd9Sstevel@tonic-gate 			/*
20157c478bd9Sstevel@tonic-gate 			 * insert_app_props inserts or overwrites the
20167c478bd9Sstevel@tonic-gate 			 * properties in thispg.
20177c478bd9Sstevel@tonic-gate 			 */
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 			if (insert_app_props(propiter, pgname, propname,
20207c478bd9Sstevel@tonic-gate 			    thispg, prop, namelen, h) == -1)
20217c478bd9Sstevel@tonic-gate 				goto error1;
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 		} else {
20247c478bd9Sstevel@tonic-gate 			/*
20257c478bd9Sstevel@tonic-gate 			 * If the property group wasn't found, we're adding
20267c478bd9Sstevel@tonic-gate 			 * a newly allocated property group to the end of the
20277c478bd9Sstevel@tonic-gate 			 * list.
20287c478bd9Sstevel@tonic-gate 			 */
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
20317c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
20327c478bd9Sstevel@tonic-gate 				goto error1;
20337c478bd9Sstevel@tonic-gate 			}
20347c478bd9Sstevel@tonic-gate 			nextpg->pg_next = NULL;
20357c478bd9Sstevel@tonic-gate 			nextpg->pg_proplist = NULL;
20367c478bd9Sstevel@tonic-gate 			thisprop = NULL;
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 			if ((nextpg->pg_name = strdup(pgname)) == NULL) {
20393eae19d9Swesolows 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
20407c478bd9Sstevel@tonic-gate 				free(nextpg);
20417c478bd9Sstevel@tonic-gate 				goto error1;
20427c478bd9Sstevel@tonic-gate 			}
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 			if (thispg->pg_name == NULL) {
20457c478bd9Sstevel@tonic-gate 				free(thispg);
20467c478bd9Sstevel@tonic-gate 				ret->ap_pglist = nextpg;
20477c478bd9Sstevel@tonic-gate 			} else {
20487c478bd9Sstevel@tonic-gate 				thispg->pg_next = nextpg;
20497c478bd9Sstevel@tonic-gate 			}
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 			while ((propiter_ret =
20527c478bd9Sstevel@tonic-gate 			    scf_iter_next_property(propiter, prop)) == 1) {
20537c478bd9Sstevel@tonic-gate 				if (scf_property_get_name(prop, propname,
20547c478bd9Sstevel@tonic-gate 				    namelen) < 0) {
20557c478bd9Sstevel@tonic-gate 					if (scf_error() == SCF_ERROR_NOT_SET)
20567c478bd9Sstevel@tonic-gate 						(void) scf_set_error(
20577c478bd9Sstevel@tonic-gate 						    SCF_ERROR_INTERNAL);
20587c478bd9Sstevel@tonic-gate 					goto error1;
20597c478bd9Sstevel@tonic-gate 				}
20607c478bd9Sstevel@tonic-gate 				if (thisprop != NULL) {
20617c478bd9Sstevel@tonic-gate 					if ((nextprop = fill_prop(prop,
20627c478bd9Sstevel@tonic-gate 					    pgname, propname, h)) ==
20637c478bd9Sstevel@tonic-gate 					    NULL)
20647c478bd9Sstevel@tonic-gate 						goto error1;
20657c478bd9Sstevel@tonic-gate 					thisprop->pr_next = nextprop;
20667c478bd9Sstevel@tonic-gate 					thisprop = nextprop;
20677c478bd9Sstevel@tonic-gate 				} else {
20687c478bd9Sstevel@tonic-gate 					/* This is the first iteration */
20697c478bd9Sstevel@tonic-gate 					if ((thisprop = fill_prop(prop,
20707c478bd9Sstevel@tonic-gate 					    pgname, propname, h)) ==
20717c478bd9Sstevel@tonic-gate 					    NULL)
20727c478bd9Sstevel@tonic-gate 						goto error1;
20737c478bd9Sstevel@tonic-gate 					nextpg->pg_proplist = thisprop;
20747c478bd9Sstevel@tonic-gate 					nextprop = thisprop;
20757c478bd9Sstevel@tonic-gate 				}
20767c478bd9Sstevel@tonic-gate 				nextprop->pr_pg = nextpg;
20777c478bd9Sstevel@tonic-gate 				nextprop->pr_next = NULL;
20787c478bd9Sstevel@tonic-gate 			}
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 			if (propiter_ret == -1) {
20817c478bd9Sstevel@tonic-gate 				if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20827c478bd9Sstevel@tonic-gate 					(void) scf_set_error(
20837c478bd9Sstevel@tonic-gate 					    SCF_ERROR_INTERNAL);
20847c478bd9Sstevel@tonic-gate 				goto error1;
20857c478bd9Sstevel@tonic-gate 			}
20867c478bd9Sstevel@tonic-gate 		}
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	}
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	if (pgiter_ret == -1) {
20917c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20927c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
20937c478bd9Sstevel@tonic-gate 		goto error1;
20947c478bd9Sstevel@tonic-gate 	}
20957c478bd9Sstevel@tonic-gate 
2096*afffa6e9SMarcel Telka 	if (ret->ap_pglist->pg_name == NULL)
2097*afffa6e9SMarcel Telka 		goto error1;
2098*afffa6e9SMarcel Telka 
20997c478bd9Sstevel@tonic-gate 	scf_iter_destroy(pgiter);
21007c478bd9Sstevel@tonic-gate 	scf_iter_destroy(propiter);
21017c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
21027c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
21037c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
21047c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
21057c478bd9Sstevel@tonic-gate 	free(propname);
21067c478bd9Sstevel@tonic-gate 	free(pgname);
21077c478bd9Sstevel@tonic-gate 	if (local_h)
21087c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	return (ret);
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	/*
21137c478bd9Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
21147c478bd9Sstevel@tonic-gate 	 * for failures at various stages during the function.
21157c478bd9Sstevel@tonic-gate 	 */
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate error1:
21187c478bd9Sstevel@tonic-gate 	scf_simple_app_props_free(ret);
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate error2:
21217c478bd9Sstevel@tonic-gate 	scf_iter_destroy(pgiter);
21227c478bd9Sstevel@tonic-gate 	scf_iter_destroy(propiter);
21237c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
21247c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
21257c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
21267c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
21277c478bd9Sstevel@tonic-gate 	free(propname);
21287c478bd9Sstevel@tonic-gate 	free(pgname);
21297c478bd9Sstevel@tonic-gate 	if (local_h)
21307c478bd9Sstevel@tonic-gate 		scf_handle_destroy(h);
21317c478bd9Sstevel@tonic-gate 	return (NULL);
21327c478bd9Sstevel@tonic-gate }
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate void
21367c478bd9Sstevel@tonic-gate scf_simple_app_props_free(scf_simple_app_props_t *propblock)
21377c478bd9Sstevel@tonic-gate {
21387c478bd9Sstevel@tonic-gate 	struct scf_simple_pg 	*pgthis, *pgnext;
21397c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 	*propthis, *propnext;
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	if ((propblock == NULL) || (propblock->ap_pglist == NULL))
21427c478bd9Sstevel@tonic-gate 		return;
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 	for (pgthis = propblock->ap_pglist; pgthis != NULL; pgthis = pgnext) {
21457c478bd9Sstevel@tonic-gate 		pgnext = pgthis->pg_next;
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 		propthis = pgthis->pg_proplist;
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 		while (propthis != NULL) {
21507c478bd9Sstevel@tonic-gate 			propnext = propthis->pr_next;
21517c478bd9Sstevel@tonic-gate 			scf_simple_prop_free(propthis);
21527c478bd9Sstevel@tonic-gate 			propthis = propnext;
21537c478bd9Sstevel@tonic-gate 		}
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 		free(pgthis->pg_name);
21567c478bd9Sstevel@tonic-gate 		free(pgthis);
21577c478bd9Sstevel@tonic-gate 	}
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	free(propblock->ap_fmri);
21607c478bd9Sstevel@tonic-gate 	free(propblock);
21617c478bd9Sstevel@tonic-gate }
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate const scf_simple_prop_t *
21647c478bd9Sstevel@tonic-gate scf_simple_app_props_next(const scf_simple_app_props_t *propblock,
21657c478bd9Sstevel@tonic-gate     scf_simple_prop_t *last)
21667c478bd9Sstevel@tonic-gate {
21677c478bd9Sstevel@tonic-gate 	struct scf_simple_pg 	*this;
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	if (propblock == NULL) {
21707c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
21717c478bd9Sstevel@tonic-gate 		return (NULL);
21727c478bd9Sstevel@tonic-gate 	}
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	this = propblock->ap_pglist;
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	/*
21777c478bd9Sstevel@tonic-gate 	 * We're looking for the first property in this block if last is
21787c478bd9Sstevel@tonic-gate 	 * NULL
21797c478bd9Sstevel@tonic-gate 	 */
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	if (last == NULL) {
21827c478bd9Sstevel@tonic-gate 		/* An empty pglist is legal, it just means no properties */
21837c478bd9Sstevel@tonic-gate 		if (this == NULL) {
21847c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NONE);
21857c478bd9Sstevel@tonic-gate 			return (NULL);
21867c478bd9Sstevel@tonic-gate 		}
21877c478bd9Sstevel@tonic-gate 		/*
21887c478bd9Sstevel@tonic-gate 		 * Walk until we find a pg with a property in it, or we run
21897c478bd9Sstevel@tonic-gate 		 * out of property groups.
21907c478bd9Sstevel@tonic-gate 		 */
21917c478bd9Sstevel@tonic-gate 		while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
21927c478bd9Sstevel@tonic-gate 			this = this->pg_next;
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 		if (this->pg_proplist == NULL) {
21957c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NONE);
21967c478bd9Sstevel@tonic-gate 			return (NULL);
21977c478bd9Sstevel@tonic-gate 		}
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 		return (this->pg_proplist);
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	}
22027c478bd9Sstevel@tonic-gate 	/*
22037c478bd9Sstevel@tonic-gate 	 * If last isn't NULL, then return the next prop in the property group,
22047c478bd9Sstevel@tonic-gate 	 * or walk the property groups until we find another property, or
22057c478bd9Sstevel@tonic-gate 	 * run out of property groups.
22067c478bd9Sstevel@tonic-gate 	 */
22077c478bd9Sstevel@tonic-gate 	if (last->pr_next != NULL)
22087c478bd9Sstevel@tonic-gate 		return (last->pr_next);
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	if (last->pr_pg->pg_next == NULL) {
22117c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
22127c478bd9Sstevel@tonic-gate 		return (NULL);
22137c478bd9Sstevel@tonic-gate 	}
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	this = last->pr_pg->pg_next;
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
22187c478bd9Sstevel@tonic-gate 		this = this->pg_next;
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	if (this->pg_proplist == NULL) {
22217c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
22227c478bd9Sstevel@tonic-gate 		return (NULL);
22237c478bd9Sstevel@tonic-gate 	}
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	return (this->pg_proplist);
22267c478bd9Sstevel@tonic-gate }
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate const scf_simple_prop_t *
22297c478bd9Sstevel@tonic-gate scf_simple_app_props_search(const scf_simple_app_props_t *propblock,
22307c478bd9Sstevel@tonic-gate     const char *pgname, const char *propname)
22317c478bd9Sstevel@tonic-gate {
22327c478bd9Sstevel@tonic-gate 	struct scf_simple_pg 	*pg;
22337c478bd9Sstevel@tonic-gate 	scf_simple_prop_t 	*prop;
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	if ((propblock == NULL) || (propname == NULL)) {
22367c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
22377c478bd9Sstevel@tonic-gate 		return (NULL);
22387c478bd9Sstevel@tonic-gate 	}
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 	pg = propblock->ap_pglist;
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	/*
22437c478bd9Sstevel@tonic-gate 	 * If pgname is NULL, we're searching the default application
22447c478bd9Sstevel@tonic-gate 	 * property group, otherwise we look for the specified group.
22457c478bd9Sstevel@tonic-gate 	 */
22467c478bd9Sstevel@tonic-gate 	if (pgname == NULL) {
22477c478bd9Sstevel@tonic-gate 		while ((pg != NULL) &&
22487c478bd9Sstevel@tonic-gate 		    (strcmp(SCF_PG_APP_DEFAULT, pg->pg_name) != 0))
22497c478bd9Sstevel@tonic-gate 			pg = pg->pg_next;
22507c478bd9Sstevel@tonic-gate 	} else {
22517c478bd9Sstevel@tonic-gate 		while ((pg != NULL) && (strcmp(pgname, pg->pg_name) != 0))
22527c478bd9Sstevel@tonic-gate 			pg = pg->pg_next;
22537c478bd9Sstevel@tonic-gate 	}
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	if (pg == NULL) {
22567c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
22577c478bd9Sstevel@tonic-gate 		return (NULL);
22587c478bd9Sstevel@tonic-gate 	}
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	prop = pg->pg_proplist;
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	while ((prop != NULL) && (strcmp(propname, prop->pr_propname) != 0))
22637c478bd9Sstevel@tonic-gate 		prop = prop->pr_next;
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
22667c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
22677c478bd9Sstevel@tonic-gate 		return (NULL);
22687c478bd9Sstevel@tonic-gate 	}
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	return (prop);
22717c478bd9Sstevel@tonic-gate }
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate void
22747c478bd9Sstevel@tonic-gate scf_simple_prop_next_reset(scf_simple_prop_t *prop)
22757c478bd9Sstevel@tonic-gate {
22767c478bd9Sstevel@tonic-gate 	if (prop == NULL)
22777c478bd9Sstevel@tonic-gate 		return;
22787c478bd9Sstevel@tonic-gate 	prop->pr_iter = 0;
22797c478bd9Sstevel@tonic-gate }
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate ssize_t
22827c478bd9Sstevel@tonic-gate scf_simple_prop_numvalues(const scf_simple_prop_t *prop)
22837c478bd9Sstevel@tonic-gate {
22847c478bd9Sstevel@tonic-gate 	if (prop == NULL)
22857c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	return (prop->pr_numvalues);
22887c478bd9Sstevel@tonic-gate }
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate scf_type_t
22927c478bd9Sstevel@tonic-gate scf_simple_prop_type(const scf_simple_prop_t *prop)
22937c478bd9Sstevel@tonic-gate {
22947c478bd9Sstevel@tonic-gate 	if (prop == NULL)
22957c478bd9Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	return (prop->pr_type);
22987c478bd9Sstevel@tonic-gate }
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate char *
23027c478bd9Sstevel@tonic-gate scf_simple_prop_name(const scf_simple_prop_t *prop)
23037c478bd9Sstevel@tonic-gate {
23047c478bd9Sstevel@tonic-gate 	if ((prop == NULL) || (prop->pr_propname == NULL)) {
23057c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
23067c478bd9Sstevel@tonic-gate 		return (NULL);
23077c478bd9Sstevel@tonic-gate 	}
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	return (prop->pr_propname);
23107c478bd9Sstevel@tonic-gate }
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate char *
23147c478bd9Sstevel@tonic-gate scf_simple_prop_pgname(const scf_simple_prop_t *prop)
23157c478bd9Sstevel@tonic-gate {
23167c478bd9Sstevel@tonic-gate 	if ((prop == NULL) || (prop->pr_pgname == NULL)) {
23177c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
23187c478bd9Sstevel@tonic-gate 		return (NULL);
23197c478bd9Sstevel@tonic-gate 	}
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	return (prop->pr_pgname);
23227c478bd9Sstevel@tonic-gate }
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate static union scf_simple_prop_val *
23267c478bd9Sstevel@tonic-gate scf_next_val(scf_simple_prop_t *prop, scf_type_t type)
23277c478bd9Sstevel@tonic-gate {
23287c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
23297c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
23307c478bd9Sstevel@tonic-gate 		return (NULL);
23317c478bd9Sstevel@tonic-gate 	}
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	switch (prop->pr_type) {
23347c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
23357c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
23367c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
2337b56bf881SAntonello Cruz 	case SCF_TYPE_NET_ADDR:
23387c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
23397c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
23407c478bd9Sstevel@tonic-gate 	case SCF_TYPE_URI:
23417c478bd9Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
23427c478bd9Sstevel@tonic-gate 		if (type != SCF_TYPE_USTRING) {
23437c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
23447c478bd9Sstevel@tonic-gate 			return (NULL);
23457c478bd9Sstevel@tonic-gate 		}
23467c478bd9Sstevel@tonic-gate 		break;
23477c478bd9Sstevel@tonic-gate 		}
23487c478bd9Sstevel@tonic-gate 	default: {
23497c478bd9Sstevel@tonic-gate 		if (type != prop->pr_type) {
23507c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
23517c478bd9Sstevel@tonic-gate 			return (NULL);
23527c478bd9Sstevel@tonic-gate 		}
23537c478bd9Sstevel@tonic-gate 		break;
23547c478bd9Sstevel@tonic-gate 		}
23557c478bd9Sstevel@tonic-gate 	}
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	if (prop->pr_iter >= prop->pr_numvalues) {
23587c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
23597c478bd9Sstevel@tonic-gate 		return (NULL);
23607c478bd9Sstevel@tonic-gate 	}
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	return (&prop->pr_vallist[prop->pr_iter++]);
23637c478bd9Sstevel@tonic-gate }
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate uint8_t *
23677c478bd9Sstevel@tonic-gate scf_simple_prop_next_boolean(scf_simple_prop_t *prop)
23687c478bd9Sstevel@tonic-gate {
23697c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_BOOLEAN);
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 	if (ret == NULL)
23747c478bd9Sstevel@tonic-gate 		return (NULL);
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 	return (&ret->pv_bool);
23777c478bd9Sstevel@tonic-gate }
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate uint64_t *
23817c478bd9Sstevel@tonic-gate scf_simple_prop_next_count(scf_simple_prop_t *prop)
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_COUNT);
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	if (ret == NULL)
23887c478bd9Sstevel@tonic-gate 		return (NULL);
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	return (&ret->pv_uint);
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate int64_t *
23957c478bd9Sstevel@tonic-gate scf_simple_prop_next_integer(scf_simple_prop_t *prop)
23967c478bd9Sstevel@tonic-gate {
23977c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_INTEGER);
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	if (ret == NULL)
24027c478bd9Sstevel@tonic-gate 		return (NULL);
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	return (&ret->pv_int);
24057c478bd9Sstevel@tonic-gate }
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate int64_t *
24087c478bd9Sstevel@tonic-gate scf_simple_prop_next_time(scf_simple_prop_t *prop, int32_t *nsec)
24097c478bd9Sstevel@tonic-gate {
24107c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_TIME);
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	if (ret == NULL)
24157c478bd9Sstevel@tonic-gate 		return (NULL);
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	if (nsec != NULL)
24187c478bd9Sstevel@tonic-gate 		*nsec = ret->pv_time.t_nsec;
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 	return (&ret->pv_time.t_sec);
24217c478bd9Sstevel@tonic-gate }
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate char *
24247c478bd9Sstevel@tonic-gate scf_simple_prop_next_astring(scf_simple_prop_t *prop)
24257c478bd9Sstevel@tonic-gate {
24267c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_ASTRING);
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate 	if (ret == NULL)
24317c478bd9Sstevel@tonic-gate 		return (NULL);
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 	return (ret->pv_str);
24347c478bd9Sstevel@tonic-gate }
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate char *
24377c478bd9Sstevel@tonic-gate scf_simple_prop_next_ustring(scf_simple_prop_t *prop)
24387c478bd9Sstevel@tonic-gate {
24397c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_USTRING);
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 	if (ret == NULL)
24447c478bd9Sstevel@tonic-gate 		return (NULL);
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	return (ret->pv_str);
24477c478bd9Sstevel@tonic-gate }
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate void *
24507c478bd9Sstevel@tonic-gate scf_simple_prop_next_opaque(scf_simple_prop_t *prop, size_t *length)
24517c478bd9Sstevel@tonic-gate {
24527c478bd9Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_OPAQUE);
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	if (ret == NULL) {
24577c478bd9Sstevel@tonic-gate 		*length = 0;
24587c478bd9Sstevel@tonic-gate 		return (NULL);
24597c478bd9Sstevel@tonic-gate 	}
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate 	*length = ret->pv_opaque.o_size;
24627c478bd9Sstevel@tonic-gate 	return (ret->pv_opaque.o_value);
24637c478bd9Sstevel@tonic-gate }
246494501b61Sskamm 
246594501b61Sskamm /*
246694501b61Sskamm  * Generate a filename based on the fmri and the given name and return
246794501b61Sskamm  * it in the buffer of MAXPATHLEN provided by the caller.
246894501b61Sskamm  * If temp_filename is non-zero, also generate a temporary, unique filename
246994501b61Sskamm  * and return it in the temp buffer of MAXPATHLEN provided by the caller.
247094501b61Sskamm  * The path to the generated pathname is also created.
247194501b61Sskamm  * Given fmri should begin with a scheme such as "svc:".
247294501b61Sskamm  * Returns
247394501b61Sskamm  *      0 on success
247494501b61Sskamm  *      -1 if filename would exceed MAXPATHLEN or
247594501b61Sskamm  *	-2 if unable to create directory to filename path
247694501b61Sskamm  */
247794501b61Sskamm int
247894501b61Sskamm gen_filenms_from_fmri(const char *fmri, const char *name, char *filename,
247994501b61Sskamm     char *temp_filename)
248094501b61Sskamm {
248194501b61Sskamm 	int		len;
248294501b61Sskamm 
248394501b61Sskamm 	len = strlen(SMF_SPEEDY_FILES_PATH);
248494501b61Sskamm 	len += strlen(fmri);
248594501b61Sskamm 	len += 2;			/* for slash and null */
248694501b61Sskamm 	len += strlen(name);
248794501b61Sskamm 	len += 6;			/* For X's needed for mkstemp */
248894501b61Sskamm 
248994501b61Sskamm 	if (len > MAXPATHLEN)
249094501b61Sskamm 		return (-1);
249194501b61Sskamm 
249294501b61Sskamm 	/* Construct directory name first - speedy path ends in slash */
249394501b61Sskamm 	(void) strcpy(filename, SMF_SPEEDY_FILES_PATH);
249494501b61Sskamm 	(void) strcat(filename, fmri);
249594501b61Sskamm 	if (mkdirp(filename, 0755) == -1) {
249694501b61Sskamm 		/* errno is set */
249794501b61Sskamm 		if (errno != EEXIST)
249894501b61Sskamm 			return (-2);
249994501b61Sskamm 	}
250094501b61Sskamm 
250194501b61Sskamm 	(void) strcat(filename, "/");
250294501b61Sskamm 	(void) strcat(filename, name);
250394501b61Sskamm 
250494501b61Sskamm 	if (temp_filename) {
250594501b61Sskamm 		(void) strcpy(temp_filename, filename);
250694501b61Sskamm 		(void) strcat(temp_filename, "XXXXXX");
250794501b61Sskamm 	}
250894501b61Sskamm 
250994501b61Sskamm 	return (0);
251094501b61Sskamm }
2511dfac3eb2SDavid Powell 
2512f6e214c7SGavin Maltby scf_type_t
2513dfac3eb2SDavid Powell scf_true_base_type(scf_type_t type)
2514dfac3eb2SDavid Powell {
2515dfac3eb2SDavid Powell 	scf_type_t base = type;
2516dfac3eb2SDavid Powell 
2517dfac3eb2SDavid Powell 	do {
2518dfac3eb2SDavid Powell 		type = base;
2519dfac3eb2SDavid Powell 		(void) scf_type_base_type(type, &base);
2520dfac3eb2SDavid Powell 	} while (base != type);
2521dfac3eb2SDavid Powell 
2522dfac3eb2SDavid Powell 	return (base);
2523dfac3eb2SDavid Powell }
2524dfac3eb2SDavid Powell 
2525dfac3eb2SDavid Powell /*
2526dfac3eb2SDavid Powell  * Convenience routine which frees all strings and opaque data
2527dfac3eb2SDavid Powell  * allocated by scf_read_propvec.
2528dfac3eb2SDavid Powell  *
2529dfac3eb2SDavid Powell  * Like free(3C), this function preserves the value of errno.
2530dfac3eb2SDavid Powell  */
2531dfac3eb2SDavid Powell void
2532dfac3eb2SDavid Powell scf_clean_propvec(scf_propvec_t *propvec)
2533dfac3eb2SDavid Powell {
2534dfac3eb2SDavid Powell 	int saved_errno = errno;
2535dfac3eb2SDavid Powell 	scf_propvec_t *prop;
2536dfac3eb2SDavid Powell 
2537dfac3eb2SDavid Powell 	for (prop = propvec; prop->pv_prop != NULL; prop++) {
2538dfac3eb2SDavid Powell 		assert(prop->pv_type != SCF_TYPE_INVALID);
2539dfac3eb2SDavid Powell 		if (prop->pv_type == SCF_TYPE_OPAQUE) {
2540dfac3eb2SDavid Powell 			scf_opaque_t *o = prop->pv_ptr;
2541dfac3eb2SDavid Powell 
2542dfac3eb2SDavid Powell 			if (o->so_addr != NULL)
2543dfac3eb2SDavid Powell 				free(o->so_addr);
2544dfac3eb2SDavid Powell 		} else if (scf_true_base_type(prop->pv_type) ==
2545dfac3eb2SDavid Powell 		    SCF_TYPE_ASTRING) {
2546dfac3eb2SDavid Powell 			if (*(char **)prop->pv_ptr != NULL)
2547dfac3eb2SDavid Powell 				free(*(char **)prop->pv_ptr);
2548dfac3eb2SDavid Powell 		}
2549dfac3eb2SDavid Powell 	}
2550dfac3eb2SDavid Powell 
2551dfac3eb2SDavid Powell 	errno = saved_errno;
2552dfac3eb2SDavid Powell }
2553dfac3eb2SDavid Powell 
2554dfac3eb2SDavid Powell static int
2555dfac3eb2SDavid Powell count_props(scf_propvec_t *props)
2556dfac3eb2SDavid Powell {
2557dfac3eb2SDavid Powell 	int count = 0;
2558dfac3eb2SDavid Powell 
2559dfac3eb2SDavid Powell 	for (; props->pv_prop != NULL; props++)
2560dfac3eb2SDavid Powell 		count++;
2561dfac3eb2SDavid Powell 	return (count);
2562dfac3eb2SDavid Powell }
2563dfac3eb2SDavid Powell 
2564dfac3eb2SDavid Powell /*
2565dfac3eb2SDavid Powell  * Reads a vector of properties from the specified fmri/property group.
2566dfac3eb2SDavid Powell  * If 'running' is true, reads from the running snapshot instead of the
2567dfac3eb2SDavid Powell  * editing snapshot.
2568dfac3eb2SDavid Powell  *
2569dfac3eb2SDavid Powell  * For string types, a buffer is allocated using malloc(3C) to hold the
2570dfac3eb2SDavid Powell  * zero-terminated string, a pointer to which is stored in the
2571dfac3eb2SDavid Powell  * caller-provided char **.  It is the caller's responsbility to free
2572dfac3eb2SDavid Powell  * this string.  To simplify error handling, unread strings are
2573dfac3eb2SDavid Powell  * initialized to NULL.
2574dfac3eb2SDavid Powell  *
2575dfac3eb2SDavid Powell  * For opaque types, a buffer is allocated using malloc(3C) to hold the
2576dfac3eb2SDavid Powell  * opaque data.  A pointer to this buffer and its size are stored in
2577dfac3eb2SDavid Powell  * the caller-provided scf_opaque_t.  It is the caller's responsibility
2578dfac3eb2SDavid Powell  * to free this buffer.  To simplify error handling, the address fields
2579dfac3eb2SDavid Powell  * for unread opaque data are initialized to NULL.
2580dfac3eb2SDavid Powell  *
2581dfac3eb2SDavid Powell  * All other data is stored directly in caller-provided variables or
2582dfac3eb2SDavid Powell  * structures.
2583dfac3eb2SDavid Powell  *
2584dfac3eb2SDavid Powell  * If this function fails to read a specific property, *badprop is set
2585dfac3eb2SDavid Powell  * to point at that property's entry in the properties array.
2586dfac3eb2SDavid Powell  *
2587dfac3eb2SDavid Powell  * On all failures, all memory allocated by this function is freed.
2588dfac3eb2SDavid Powell  */
2589dfac3eb2SDavid Powell int
2590dfac3eb2SDavid Powell scf_read_propvec(const char *fmri, const char *pgname, boolean_t running,
2591dfac3eb2SDavid Powell     scf_propvec_t *properties, scf_propvec_t **badprop)
2592dfac3eb2SDavid Powell {
2593f6e214c7SGavin Maltby 	scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2594dfac3eb2SDavid Powell 	scf_service_t *s = scf_service_create(h);
2595dfac3eb2SDavid Powell 	scf_instance_t *i = scf_instance_create(h);
2596dfac3eb2SDavid Powell 	scf_snapshot_t *snap = running ? scf_snapshot_create(h) : NULL;
2597dfac3eb2SDavid Powell 	scf_propertygroup_t *pg = scf_pg_create(h);
2598dfac3eb2SDavid Powell 	scf_property_t *p = scf_property_create(h);
2599dfac3eb2SDavid Powell 	scf_value_t *v = scf_value_create(h);
2600dfac3eb2SDavid Powell 	boolean_t instance = B_TRUE;
2601dfac3eb2SDavid Powell 	scf_propvec_t *prop;
2602dfac3eb2SDavid Powell 	int error = 0;
2603dfac3eb2SDavid Powell 
2604dfac3eb2SDavid Powell 	if (h == NULL || s == NULL || i == NULL || (running && snap == NULL) ||
2605dfac3eb2SDavid Powell 	    pg == NULL || p == NULL || v == NULL)
2606dfac3eb2SDavid Powell 		goto scferror;
2607dfac3eb2SDavid Powell 
2608dfac3eb2SDavid Powell 	if (scf_handle_decode_fmri(h, fmri, NULL, s, i, NULL, NULL, 0) == -1)
2609dfac3eb2SDavid Powell 		goto scferror;
2610dfac3eb2SDavid Powell 
2611dfac3eb2SDavid Powell 	if (scf_instance_to_fmri(i, NULL, 0) == -1) {
2612dfac3eb2SDavid Powell 		if (scf_error() != SCF_ERROR_NOT_SET)
2613dfac3eb2SDavid Powell 			goto scferror;
2614dfac3eb2SDavid Powell 		instance = B_FALSE;
2615dfac3eb2SDavid Powell 	}
2616dfac3eb2SDavid Powell 
2617dfac3eb2SDavid Powell 	if (running) {
2618dfac3eb2SDavid Powell 		if (!instance) {
2619dfac3eb2SDavid Powell 			error = SCF_ERROR_TYPE_MISMATCH;
2620dfac3eb2SDavid Powell 			goto out;
2621dfac3eb2SDavid Powell 		}
2622dfac3eb2SDavid Powell 
2623dfac3eb2SDavid Powell 		if (scf_instance_get_snapshot(i, "running", snap) !=
2624dfac3eb2SDavid Powell 		    SCF_SUCCESS)
2625dfac3eb2SDavid Powell 			goto scferror;
2626dfac3eb2SDavid Powell 	}
2627dfac3eb2SDavid Powell 
2628dfac3eb2SDavid Powell 	if ((instance ? scf_instance_get_pg_composed(i, snap, pgname, pg) :
2629dfac3eb2SDavid Powell 	    scf_service_get_pg(s, pgname, pg)) == -1)
2630dfac3eb2SDavid Powell 		goto scferror;
2631dfac3eb2SDavid Powell 
2632dfac3eb2SDavid Powell 	for (prop = properties; prop->pv_prop != NULL; prop++) {
2633dfac3eb2SDavid Powell 		if (prop->pv_type == SCF_TYPE_OPAQUE)
2634dfac3eb2SDavid Powell 			((scf_opaque_t *)prop->pv_ptr)->so_addr = NULL;
2635dfac3eb2SDavid Powell 		else if (scf_true_base_type(prop->pv_type) == SCF_TYPE_ASTRING)
2636dfac3eb2SDavid Powell 			*((char **)prop->pv_ptr) = NULL;
2637dfac3eb2SDavid Powell 	}
2638dfac3eb2SDavid Powell 
2639dfac3eb2SDavid Powell 	for (prop = properties; prop->pv_prop != NULL; prop++) {
2640dfac3eb2SDavid Powell 		int ret = 0;
2641dfac3eb2SDavid Powell 
2642dfac3eb2SDavid Powell 		if (scf_pg_get_property(pg, prop->pv_prop, p) == -1 ||
2643dfac3eb2SDavid Powell 		    scf_property_get_value(p, v) == -1) {
2644dfac3eb2SDavid Powell 			*badprop = prop;
2645dfac3eb2SDavid Powell 			goto scferror;
2646dfac3eb2SDavid Powell 		}
2647dfac3eb2SDavid Powell 		switch (prop->pv_type) {
2648dfac3eb2SDavid Powell 		case SCF_TYPE_BOOLEAN: {
2649dfac3eb2SDavid Powell 			uint8_t b;
2650dfac3eb2SDavid Powell 
2651dfac3eb2SDavid Powell 			ret = scf_value_get_boolean(v, &b);
2652dfac3eb2SDavid Powell 			if (ret == -1)
2653dfac3eb2SDavid Powell 				break;
2654dfac3eb2SDavid Powell 			if (prop->pv_aux != 0) {
2655dfac3eb2SDavid Powell 				uint64_t *bits = prop->pv_ptr;
2656dfac3eb2SDavid Powell 				*bits = b ? (*bits | prop->pv_aux) :
2657dfac3eb2SDavid Powell 				    (*bits & ~prop->pv_aux);
2658dfac3eb2SDavid Powell 			} else {
2659dfac3eb2SDavid Powell 				boolean_t *bool = prop->pv_ptr;
2660dfac3eb2SDavid Powell 				*bool = b ? B_TRUE : B_FALSE;
2661dfac3eb2SDavid Powell 			}
2662dfac3eb2SDavid Powell 			break;
2663dfac3eb2SDavid Powell 		}
2664dfac3eb2SDavid Powell 		case SCF_TYPE_COUNT:
2665dfac3eb2SDavid Powell 			ret = scf_value_get_count(v, prop->pv_ptr);
2666dfac3eb2SDavid Powell 			break;
2667dfac3eb2SDavid Powell 		case SCF_TYPE_INTEGER:
2668dfac3eb2SDavid Powell 			ret = scf_value_get_integer(v, prop->pv_ptr);
2669dfac3eb2SDavid Powell 			break;
2670dfac3eb2SDavid Powell 		case SCF_TYPE_TIME: {
2671dfac3eb2SDavid Powell 			scf_time_t *time = prop->pv_ptr;
2672dfac3eb2SDavid Powell 
26731f6eb021SLiane Praza 			ret = scf_value_get_time(v, &time->t_seconds,
26741f6eb021SLiane Praza 			    &time->t_ns);
2675dfac3eb2SDavid Powell 			break;
2676dfac3eb2SDavid Powell 		}
2677dfac3eb2SDavid Powell 		case SCF_TYPE_OPAQUE: {
2678dfac3eb2SDavid Powell 			scf_opaque_t *opaque = prop->pv_ptr;
2679dfac3eb2SDavid Powell 			ssize_t size = scf_value_get_opaque(v, NULL, 0);
2680dfac3eb2SDavid Powell 
2681dfac3eb2SDavid Powell 			if (size == -1) {
2682dfac3eb2SDavid Powell 				*badprop = prop;
2683dfac3eb2SDavid Powell 				goto scferror;
2684dfac3eb2SDavid Powell 			}
2685dfac3eb2SDavid Powell 			if ((opaque->so_addr = malloc(size)) == NULL) {
2686dfac3eb2SDavid Powell 				error = SCF_ERROR_NO_MEMORY;
2687dfac3eb2SDavid Powell 				goto out;
2688dfac3eb2SDavid Powell 			}
2689dfac3eb2SDavid Powell 			opaque->so_size = size;
2690dfac3eb2SDavid Powell 			ret = scf_value_get_opaque(v, opaque->so_addr, size);
2691dfac3eb2SDavid Powell 			break;
2692dfac3eb2SDavid Powell 		}
2693dfac3eb2SDavid Powell 		default: {
2694dfac3eb2SDavid Powell 			char *s;
2695dfac3eb2SDavid Powell 			ssize_t size;
2696dfac3eb2SDavid Powell 
2697dfac3eb2SDavid Powell 			assert(scf_true_base_type(prop->pv_type) ==
2698dfac3eb2SDavid Powell 			    SCF_TYPE_ASTRING);
2699dfac3eb2SDavid Powell 
2700dfac3eb2SDavid Powell 			size = scf_value_get_astring(v, NULL, 0);
2701dfac3eb2SDavid Powell 			if (size == -1) {
2702dfac3eb2SDavid Powell 				*badprop = prop;
2703dfac3eb2SDavid Powell 				goto scferror;
2704dfac3eb2SDavid Powell 			}
2705dfac3eb2SDavid Powell 			if ((s = malloc(++size)) == NULL) {
2706dfac3eb2SDavid Powell 				error = SCF_ERROR_NO_MEMORY;
2707dfac3eb2SDavid Powell 				goto out;
2708dfac3eb2SDavid Powell 			}
2709dfac3eb2SDavid Powell 			ret = scf_value_get_astring(v, s, size);
2710dfac3eb2SDavid Powell 			*(char **)prop->pv_ptr = s;
2711dfac3eb2SDavid Powell 		}
2712dfac3eb2SDavid Powell 
2713dfac3eb2SDavid Powell 		if (ret == -1) {
2714dfac3eb2SDavid Powell 			*badprop = prop;
2715dfac3eb2SDavid Powell 			goto scferror;
2716dfac3eb2SDavid Powell 		}
2717dfac3eb2SDavid Powell 
2718dfac3eb2SDavid Powell 		}
2719dfac3eb2SDavid Powell 	}
2720dfac3eb2SDavid Powell 
2721dfac3eb2SDavid Powell 	goto out;
2722dfac3eb2SDavid Powell 
2723dfac3eb2SDavid Powell scferror:
2724dfac3eb2SDavid Powell 	error = scf_error();
2725dfac3eb2SDavid Powell 	scf_clean_propvec(properties);
2726dfac3eb2SDavid Powell 
2727dfac3eb2SDavid Powell out:
2728c42520ebSJan Friedel 	scf_value_destroy(v);
2729c42520ebSJan Friedel 	scf_property_destroy(p);
2730dfac3eb2SDavid Powell 	scf_pg_destroy(pg);
2731dfac3eb2SDavid Powell 	scf_snapshot_destroy(snap);
2732dfac3eb2SDavid Powell 	scf_instance_destroy(i);
2733dfac3eb2SDavid Powell 	scf_service_destroy(s);
2734dfac3eb2SDavid Powell 	scf_handle_destroy(h);
2735dfac3eb2SDavid Powell 
2736dfac3eb2SDavid Powell 	if (error != 0) {
2737dfac3eb2SDavid Powell 		(void) scf_set_error(error);
2738dfac3eb2SDavid Powell 		return (SCF_FAILED);
2739dfac3eb2SDavid Powell 	}
2740dfac3eb2SDavid Powell 
2741dfac3eb2SDavid Powell 	return (SCF_SUCCESS);
2742dfac3eb2SDavid Powell }
2743dfac3eb2SDavid Powell 
2744dfac3eb2SDavid Powell /*
2745dfac3eb2SDavid Powell  * Writes a vector of properties to the specified fmri/property group.
2746dfac3eb2SDavid Powell  *
2747dfac3eb2SDavid Powell  * If this function fails to write a specific property, *badprop is set
2748dfac3eb2SDavid Powell  * to point at that property's entry in the properties array.
2749dfac3eb2SDavid Powell  *
2750dfac3eb2SDavid Powell  * One significant difference between this function and the
2751dfac3eb2SDavid Powell  * scf_read_propvec function is that for string types, pv_ptr is a
2752dfac3eb2SDavid Powell  * char *, not a char **.  This means that you can't write a propvec
2753dfac3eb2SDavid Powell  * you just read, but makes other uses (hopefully the majority) simpler.
2754dfac3eb2SDavid Powell  */
2755dfac3eb2SDavid Powell int
2756dfac3eb2SDavid Powell scf_write_propvec(const char *fmri, const char *pgname,
2757dfac3eb2SDavid Powell     scf_propvec_t *properties, scf_propvec_t **badprop)
2758dfac3eb2SDavid Powell {
2759f6e214c7SGavin Maltby 	scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
2760dfac3eb2SDavid Powell 	scf_service_t *s = scf_service_create(h);
2761dfac3eb2SDavid Powell 	scf_instance_t *inst = scf_instance_create(h);
2762dfac3eb2SDavid Powell 	scf_snapshot_t *snap = scf_snapshot_create(h);
2763dfac3eb2SDavid Powell 	scf_propertygroup_t *pg = scf_pg_create(h);
2764dfac3eb2SDavid Powell 	scf_property_t *p = scf_property_create(h);
2765dfac3eb2SDavid Powell 	scf_transaction_t *tx = scf_transaction_create(h);
2766dfac3eb2SDavid Powell 	scf_value_t **v = NULL;
2767dfac3eb2SDavid Powell 	scf_transaction_entry_t **e = NULL;
2768dfac3eb2SDavid Powell 	boolean_t instance = B_TRUE;
2769dfac3eb2SDavid Powell 	int i, n;
2770dfac3eb2SDavid Powell 	scf_propvec_t *prop;
2771dfac3eb2SDavid Powell 	int error = 0, ret;
2772dfac3eb2SDavid Powell 
2773dfac3eb2SDavid Powell 	n = count_props(properties);
2774dfac3eb2SDavid Powell 	v = calloc(n, sizeof (scf_value_t *));
2775dfac3eb2SDavid Powell 	e = calloc(n, sizeof (scf_transaction_entry_t *));
2776dfac3eb2SDavid Powell 
2777dfac3eb2SDavid Powell 	if (v == NULL || e == NULL) {
2778dfac3eb2SDavid Powell 		error = SCF_ERROR_NO_MEMORY;
2779dfac3eb2SDavid Powell 		goto out;
2780dfac3eb2SDavid Powell 	}
2781dfac3eb2SDavid Powell 
2782dfac3eb2SDavid Powell 	if (h == NULL || s == NULL || inst == NULL || pg == NULL || p == NULL ||
2783dfac3eb2SDavid Powell 	    tx == NULL)
2784dfac3eb2SDavid Powell 		goto scferror;
2785dfac3eb2SDavid Powell 
2786dfac3eb2SDavid Powell 	for (i = 0; i < n; i++) {
2787dfac3eb2SDavid Powell 		v[i] = scf_value_create(h);
2788dfac3eb2SDavid Powell 		e[i] = scf_entry_create(h);
2789dfac3eb2SDavid Powell 		if (v[i] == NULL || e[i] == NULL)
2790dfac3eb2SDavid Powell 			goto scferror;
2791dfac3eb2SDavid Powell 	}
2792dfac3eb2SDavid Powell 
2793dfac3eb2SDavid Powell 	if (scf_handle_decode_fmri(h, fmri, NULL, s, inst, NULL, NULL, 0)
2794dfac3eb2SDavid Powell 	    != SCF_SUCCESS)
2795dfac3eb2SDavid Powell 		goto scferror;
2796dfac3eb2SDavid Powell 
2797dfac3eb2SDavid Powell 	if (scf_instance_to_fmri(inst, NULL, 0) == -1) {
2798dfac3eb2SDavid Powell 		if (scf_error() != SCF_ERROR_NOT_SET)
2799dfac3eb2SDavid Powell 			goto scferror;
2800dfac3eb2SDavid Powell 		instance = B_FALSE;
2801dfac3eb2SDavid Powell 	}
2802dfac3eb2SDavid Powell 
2803dfac3eb2SDavid Powell 	if ((instance ? scf_instance_get_pg(inst, pgname, pg) :
2804dfac3eb2SDavid Powell 	    scf_service_get_pg(s, pgname, pg)) == -1)
2805dfac3eb2SDavid Powell 		goto scferror;
2806dfac3eb2SDavid Powell 
2807dfac3eb2SDavid Powell top:
2808dfac3eb2SDavid Powell 	if (scf_transaction_start(tx, pg) == -1)
2809dfac3eb2SDavid Powell 		goto scferror;
2810dfac3eb2SDavid Powell 
2811dfac3eb2SDavid Powell 	for (prop = properties, i = 0; prop->pv_prop != NULL; prop++, i++) {
2812dfac3eb2SDavid Powell 		ret = scf_transaction_property_change(tx, e[i], prop->pv_prop,
2813dfac3eb2SDavid Powell 		    prop->pv_type);
2814dfac3eb2SDavid Powell 		if (ret == -1 && scf_error() == SCF_ERROR_NOT_FOUND)
2815dfac3eb2SDavid Powell 			ret = scf_transaction_property_new(tx, e[i],
2816dfac3eb2SDavid Powell 			    prop->pv_prop, prop->pv_type);
2817dfac3eb2SDavid Powell 
2818dfac3eb2SDavid Powell 		if (ret == -1) {
2819dfac3eb2SDavid Powell 			*badprop = prop;
2820dfac3eb2SDavid Powell 			goto scferror;
2821dfac3eb2SDavid Powell 		}
2822dfac3eb2SDavid Powell 
2823dfac3eb2SDavid Powell 		switch (prop->pv_type) {
2824dfac3eb2SDavid Powell 		case SCF_TYPE_BOOLEAN: {
2825dfac3eb2SDavid Powell 			boolean_t b = (prop->pv_aux != 0) ?
2826dfac3eb2SDavid Powell 			    (*(uint64_t *)prop->pv_ptr & prop->pv_aux) != 0 :
2827dfac3eb2SDavid Powell 			    *(boolean_t *)prop->pv_ptr;
2828dfac3eb2SDavid Powell 
2829dfac3eb2SDavid Powell 			scf_value_set_boolean(v[i], b ? 1 : 0);
2830dfac3eb2SDavid Powell 			break;
2831dfac3eb2SDavid Powell 		}
2832dfac3eb2SDavid Powell 		case SCF_TYPE_COUNT:
2833dfac3eb2SDavid Powell 			scf_value_set_count(v[i], *(uint64_t *)prop->pv_ptr);
2834dfac3eb2SDavid Powell 			break;
2835dfac3eb2SDavid Powell 		case SCF_TYPE_INTEGER:
2836dfac3eb2SDavid Powell 			scf_value_set_integer(v[i], *(int64_t *)prop->pv_ptr);
2837dfac3eb2SDavid Powell 			break;
2838dfac3eb2SDavid Powell 		case SCF_TYPE_TIME: {
2839dfac3eb2SDavid Powell 			scf_time_t *time = prop->pv_ptr;
2840dfac3eb2SDavid Powell 
28411f6eb021SLiane Praza 			ret = scf_value_set_time(v[i], time->t_seconds,
28421f6eb021SLiane Praza 			    time->t_ns);
2843dfac3eb2SDavid Powell 			break;
2844dfac3eb2SDavid Powell 		}
2845dfac3eb2SDavid Powell 		case SCF_TYPE_OPAQUE: {
2846dfac3eb2SDavid Powell 			scf_opaque_t *opaque = prop->pv_ptr;
2847dfac3eb2SDavid Powell 
2848dfac3eb2SDavid Powell 			ret = scf_value_set_opaque(v[i], opaque->so_addr,
2849dfac3eb2SDavid Powell 			    opaque->so_size);
2850dfac3eb2SDavid Powell 			break;
2851dfac3eb2SDavid Powell 		}
2852dfac3eb2SDavid Powell 		case SCF_TYPE_ASTRING:
2853dfac3eb2SDavid Powell 			ret = scf_value_set_astring(v[i],
2854dfac3eb2SDavid Powell 			    (const char *)prop->pv_ptr);
2855dfac3eb2SDavid Powell 			break;
2856dfac3eb2SDavid Powell 		default:
2857dfac3eb2SDavid Powell 			ret = scf_value_set_from_string(v[i], prop->pv_type,
2858dfac3eb2SDavid Powell 			    (const char *)prop->pv_ptr);
2859dfac3eb2SDavid Powell 		}
2860dfac3eb2SDavid Powell 
2861dfac3eb2SDavid Powell 		if (ret == -1 || scf_entry_add_value(e[i], v[i]) == -1) {
2862dfac3eb2SDavid Powell 			*badprop = prop;
2863dfac3eb2SDavid Powell 			goto scferror;
2864dfac3eb2SDavid Powell 		}
2865dfac3eb2SDavid Powell 	}
2866dfac3eb2SDavid Powell 
2867dfac3eb2SDavid Powell 	ret = scf_transaction_commit(tx);
2868dfac3eb2SDavid Powell 	if (ret == 1)
2869dfac3eb2SDavid Powell 		goto out;
2870dfac3eb2SDavid Powell 
2871dfac3eb2SDavid Powell 	if (ret == 0 && scf_pg_update(pg) != -1) {
2872dfac3eb2SDavid Powell 		scf_transaction_reset(tx);
2873dfac3eb2SDavid Powell 		goto top;
2874dfac3eb2SDavid Powell 	}
2875dfac3eb2SDavid Powell 
2876dfac3eb2SDavid Powell scferror:
2877dfac3eb2SDavid Powell 	error = scf_error();
2878dfac3eb2SDavid Powell 
2879dfac3eb2SDavid Powell out:
2880dfac3eb2SDavid Powell 	if (v != NULL) {
2881dfac3eb2SDavid Powell 		for (i = 0; i < n; i++)
2882dfac3eb2SDavid Powell 			scf_value_destroy(v[i]);
2883dfac3eb2SDavid Powell 		free(v);
2884dfac3eb2SDavid Powell 	}
2885dfac3eb2SDavid Powell 
2886dfac3eb2SDavid Powell 	if (e != NULL) {
2887dfac3eb2SDavid Powell 		for (i = 0; i < n; i++)
2888dfac3eb2SDavid Powell 			scf_entry_destroy(e[i]);
2889dfac3eb2SDavid Powell 		free(e);
2890dfac3eb2SDavid Powell 	}
2891dfac3eb2SDavid Powell 
2892dfac3eb2SDavid Powell 	scf_transaction_destroy(tx);
2893dfac3eb2SDavid Powell 	scf_property_destroy(p);
2894dfac3eb2SDavid Powell 	scf_pg_destroy(pg);
2895dfac3eb2SDavid Powell 	scf_snapshot_destroy(snap);
2896dfac3eb2SDavid Powell 	scf_instance_destroy(inst);
2897dfac3eb2SDavid Powell 	scf_service_destroy(s);
2898dfac3eb2SDavid Powell 	scf_handle_destroy(h);
2899dfac3eb2SDavid Powell 
2900dfac3eb2SDavid Powell 	if (error != 0) {
2901dfac3eb2SDavid Powell 		(void) scf_set_error(error);
2902dfac3eb2SDavid Powell 		return (SCF_FAILED);
2903dfac3eb2SDavid Powell 	}
2904dfac3eb2SDavid Powell 
2905dfac3eb2SDavid Powell 	return (SCF_SUCCESS);
2906dfac3eb2SDavid Powell }
2907eb1a3463STruong Nguyen 
2908eb1a3463STruong Nguyen /*
2909eb1a3463STruong Nguyen  * Returns
2910eb1a3463STruong Nguyen  *   0 - success
2911eb1a3463STruong Nguyen  *   ECONNABORTED - repository connection broken
2912eb1a3463STruong Nguyen  *   ECANCELED - inst was deleted
2913eb1a3463STruong Nguyen  *   EPERM
2914eb1a3463STruong Nguyen  *   EACCES
2915eb1a3463STruong Nguyen  *   EROFS
2916eb1a3463STruong Nguyen  *   ENOMEM
2917eb1a3463STruong Nguyen  */
2918eb1a3463STruong Nguyen int
2919eb1a3463STruong Nguyen scf_instance_delete_prop(scf_instance_t *inst, const char *pgname,
2920eb1a3463STruong Nguyen     const char *pname)
2921eb1a3463STruong Nguyen {
2922eb1a3463STruong Nguyen 	scf_handle_t *h;
2923eb1a3463STruong Nguyen 	scf_propertygroup_t *pg;
2924eb1a3463STruong Nguyen 	scf_transaction_t *tx;
2925eb1a3463STruong Nguyen 	scf_transaction_entry_t *e;
2926eb1a3463STruong Nguyen 	int error = 0, ret = 1, r;
2927eb1a3463STruong Nguyen 
2928eb1a3463STruong Nguyen 	h = scf_instance_handle(inst);
2929eb1a3463STruong Nguyen 
2930eb1a3463STruong Nguyen 	if ((pg = scf_pg_create(h)) == NULL) {
2931eb1a3463STruong Nguyen 		return (ENOMEM);
2932eb1a3463STruong Nguyen 	}
2933eb1a3463STruong Nguyen 
2934eb1a3463STruong Nguyen 	if (scf_instance_get_pg(inst, pgname, pg) != 0) {
2935eb1a3463STruong Nguyen 		error = scf_error();
2936eb1a3463STruong Nguyen 		scf_pg_destroy(pg);
2937eb1a3463STruong Nguyen 		switch (error) {
2938eb1a3463STruong Nguyen 		case SCF_ERROR_NOT_FOUND:
2939eb1a3463STruong Nguyen 			return (SCF_SUCCESS);
2940eb1a3463STruong Nguyen 
2941eb1a3463STruong Nguyen 		case SCF_ERROR_DELETED:
2942eb1a3463STruong Nguyen 			return (ECANCELED);
2943eb1a3463STruong Nguyen 
2944eb1a3463STruong Nguyen 		case SCF_ERROR_CONNECTION_BROKEN:
2945eb1a3463STruong Nguyen 		default:
2946eb1a3463STruong Nguyen 			return (ECONNABORTED);
2947eb1a3463STruong Nguyen 
2948eb1a3463STruong Nguyen 		case SCF_ERROR_NOT_SET:
2949eb1a3463STruong Nguyen 			bad_error("scf_instance_get_pg", scf_error());
2950eb1a3463STruong Nguyen 		}
2951eb1a3463STruong Nguyen 	}
2952eb1a3463STruong Nguyen 
2953eb1a3463STruong Nguyen 	tx = scf_transaction_create(h);
2954eb1a3463STruong Nguyen 	e = scf_entry_create(h);
2955eb1a3463STruong Nguyen 	if (tx == NULL || e == NULL) {
2956eb1a3463STruong Nguyen 		ret = ENOMEM;
2957eb1a3463STruong Nguyen 		goto out;
2958eb1a3463STruong Nguyen 	}
2959eb1a3463STruong Nguyen 
2960eb1a3463STruong Nguyen 	for (;;) {
2961eb1a3463STruong Nguyen 		if (scf_transaction_start(tx, pg) != 0) {
2962eb1a3463STruong Nguyen 			goto scferror;
2963eb1a3463STruong Nguyen 		}
2964eb1a3463STruong Nguyen 
2965eb1a3463STruong Nguyen 		if (scf_transaction_property_delete(tx, e, pname) != 0) {
2966eb1a3463STruong Nguyen 			goto scferror;
2967eb1a3463STruong Nguyen 		}
2968eb1a3463STruong Nguyen 
2969eb1a3463STruong Nguyen 		if ((r = scf_transaction_commit(tx)) == 1) {
2970eb1a3463STruong Nguyen 			ret = 0;
2971eb1a3463STruong Nguyen 			goto out;
2972eb1a3463STruong Nguyen 		}
2973eb1a3463STruong Nguyen 
2974eb1a3463STruong Nguyen 		if (r == -1) {
2975eb1a3463STruong Nguyen 			goto scferror;
2976eb1a3463STruong Nguyen 		}
2977eb1a3463STruong Nguyen 
2978eb1a3463STruong Nguyen 		scf_transaction_reset(tx);
2979eb1a3463STruong Nguyen 		if (scf_pg_update(pg) == -1) {
2980eb1a3463STruong Nguyen 			goto scferror;
2981eb1a3463STruong Nguyen 		}
2982eb1a3463STruong Nguyen 	}
2983eb1a3463STruong Nguyen 
2984eb1a3463STruong Nguyen scferror:
2985eb1a3463STruong Nguyen 	switch (scf_error()) {
2986eb1a3463STruong Nguyen 	case SCF_ERROR_DELETED:
2987eb1a3463STruong Nguyen 	case SCF_ERROR_NOT_FOUND:
2988eb1a3463STruong Nguyen 		ret = 0;
2989eb1a3463STruong Nguyen 		break;
2990eb1a3463STruong Nguyen 
2991eb1a3463STruong Nguyen 	case SCF_ERROR_PERMISSION_DENIED:
2992eb1a3463STruong Nguyen 		ret = EPERM;
2993eb1a3463STruong Nguyen 		break;
2994eb1a3463STruong Nguyen 
2995eb1a3463STruong Nguyen 	case SCF_ERROR_BACKEND_ACCESS:
2996eb1a3463STruong Nguyen 		ret = EACCES;
2997eb1a3463STruong Nguyen 		break;
2998eb1a3463STruong Nguyen 
2999eb1a3463STruong Nguyen 	case SCF_ERROR_BACKEND_READONLY:
3000eb1a3463STruong Nguyen 		ret = EROFS;
3001eb1a3463STruong Nguyen 		break;
3002eb1a3463STruong Nguyen 
3003eb1a3463STruong Nguyen 	case SCF_ERROR_CONNECTION_BROKEN:
3004eb1a3463STruong Nguyen 	default:
3005eb1a3463STruong Nguyen 		ret = ECONNABORTED;
3006eb1a3463STruong Nguyen 		break;
3007eb1a3463STruong Nguyen 
3008eb1a3463STruong Nguyen 	case SCF_ERROR_HANDLE_MISMATCH:
3009eb1a3463STruong Nguyen 	case SCF_ERROR_INVALID_ARGUMENT:
3010eb1a3463STruong Nguyen 	case SCF_ERROR_NOT_BOUND:
3011eb1a3463STruong Nguyen 	case SCF_ERROR_NOT_SET:
3012eb1a3463STruong Nguyen 		bad_error("scf_instance_delete_prop", scf_error());
3013eb1a3463STruong Nguyen 	}
3014eb1a3463STruong Nguyen 
3015eb1a3463STruong Nguyen out:
3016eb1a3463STruong Nguyen 	scf_transaction_destroy(tx);
3017eb1a3463STruong Nguyen 	scf_entry_destroy(e);
3018eb1a3463STruong Nguyen 	scf_pg_destroy(pg);
3019eb1a3463STruong Nguyen 
3020eb1a3463STruong Nguyen 	return (ret);
3021eb1a3463STruong Nguyen }
3022b56bf881SAntonello Cruz 
3023b56bf881SAntonello Cruz /*
3024b56bf881SAntonello Cruz  * Check the "application/auto_enable" property for the passed FMRI.
3025b56bf881SAntonello Cruz  * scf_simple_prop_get() should find the property on an instance
3026b56bf881SAntonello Cruz  * or on the service FMRI.  The routine returns:
3027b56bf881SAntonello Cruz  * -1: inconclusive (likely no such property or FMRI)
3028b56bf881SAntonello Cruz  *  0: auto_enable is false
3029b56bf881SAntonello Cruz  *  1: auto_enable is true
3030b56bf881SAntonello Cruz  */
3031b56bf881SAntonello Cruz static int
3032b56bf881SAntonello Cruz is_auto_enabled(char *fmri)
3033b56bf881SAntonello Cruz {
3034b56bf881SAntonello Cruz 	scf_simple_prop_t *prop;
3035b56bf881SAntonello Cruz 	int retval = -1;
3036b56bf881SAntonello Cruz 	uint8_t *ret;
3037b56bf881SAntonello Cruz 
3038b56bf881SAntonello Cruz 	prop = scf_simple_prop_get(NULL, fmri, SCF_GROUP_APPLICATION,
3039b56bf881SAntonello Cruz 	    "auto_enable");
3040b56bf881SAntonello Cruz 	if (!prop)
3041b56bf881SAntonello Cruz 		return (retval);
3042b56bf881SAntonello Cruz 	ret = scf_simple_prop_next_boolean(prop);
3043b56bf881SAntonello Cruz 	retval = (*ret != 0);
3044b56bf881SAntonello Cruz 	scf_simple_prop_free(prop);
3045b56bf881SAntonello Cruz 	return (retval);
3046b56bf881SAntonello Cruz }
3047b56bf881SAntonello Cruz 
3048b56bf881SAntonello Cruz /*
3049b56bf881SAntonello Cruz  * Check an array of services and enable any that don't have the
3050b56bf881SAntonello Cruz  * "application/auto_enable" property set to "false", which is
3051b56bf881SAntonello Cruz  * the interface to turn off this behaviour (see PSARC 2004/739).
3052b56bf881SAntonello Cruz  */
3053b56bf881SAntonello Cruz void
3054b56bf881SAntonello Cruz _check_services(char **svcs)
3055b56bf881SAntonello Cruz {
3056b56bf881SAntonello Cruz 	char *s;
3057b56bf881SAntonello Cruz 
3058b56bf881SAntonello Cruz 	for (; *svcs; svcs++) {
3059b56bf881SAntonello Cruz 		if (is_auto_enabled(*svcs) == 0)
3060b56bf881SAntonello Cruz 			continue;
3061b56bf881SAntonello Cruz 		if ((s = smf_get_state(*svcs)) != NULL) {
3062b56bf881SAntonello Cruz 			if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0)
3063b56bf881SAntonello Cruz 				(void) smf_enable_instance(*svcs,
3064b56bf881SAntonello Cruz 				    SMF_TEMPORARY);
3065b56bf881SAntonello Cruz 			free(s);
3066b56bf881SAntonello Cruz 		}
3067b56bf881SAntonello Cruz 	}
3068b56bf881SAntonello Cruz }
3069f6e214c7SGavin Maltby 
3070f6e214c7SGavin Maltby /*ARGSUSED*/
3071f6e214c7SGavin Maltby static int
3072f6e214c7SGavin Maltby str_compare(const char *s1, const char *s2, size_t n)
3073f6e214c7SGavin Maltby {
3074f6e214c7SGavin Maltby 	return (strcmp(s1, s2));
3075f6e214c7SGavin Maltby }
3076f6e214c7SGavin Maltby 
3077f6e214c7SGavin Maltby static int
3078f6e214c7SGavin Maltby str_n_compare(const char *s1, const char *s2, size_t n)
3079f6e214c7SGavin Maltby {
3080f6e214c7SGavin Maltby 	return (strncmp(s1, s2, n));
3081f6e214c7SGavin Maltby }
3082f6e214c7SGavin Maltby 
3083f6e214c7SGavin Maltby int32_t
3084f6e214c7SGavin Maltby state_from_string(const char *state, size_t l)
3085f6e214c7SGavin Maltby {
3086f6e214c7SGavin Maltby 	int (*str_cmp)(const char *, const char *, size_t);
3087f6e214c7SGavin Maltby 
3088f6e214c7SGavin Maltby 	if (l == 0)
3089f6e214c7SGavin Maltby 		str_cmp = str_compare;
3090f6e214c7SGavin Maltby 	else
3091f6e214c7SGavin Maltby 		str_cmp = str_n_compare;
3092f6e214c7SGavin Maltby 
3093f6e214c7SGavin Maltby 	if (str_cmp(SCF_STATE_STRING_UNINIT, state, l) == 0)
3094f6e214c7SGavin Maltby 		return (SCF_STATE_UNINIT);
3095f6e214c7SGavin Maltby 	else if (str_cmp(SCF_STATE_STRING_MAINT, state, l) == 0)
3096f6e214c7SGavin Maltby 		return (SCF_STATE_MAINT);
3097f6e214c7SGavin Maltby 	else if (str_cmp(SCF_STATE_STRING_OFFLINE, state, l) == 0)
3098f6e214c7SGavin Maltby 		return (SCF_STATE_OFFLINE);
3099f6e214c7SGavin Maltby 	else if (str_cmp(SCF_STATE_STRING_DISABLED, state, l) == 0)
3100f6e214c7SGavin Maltby 		return (SCF_STATE_DISABLED);
3101f6e214c7SGavin Maltby 	else if (str_cmp(SCF_STATE_STRING_ONLINE, state, l) == 0)
3102f6e214c7SGavin Maltby 		return (SCF_STATE_ONLINE);
3103f6e214c7SGavin Maltby 	else if (str_cmp(SCF_STATE_STRING_DEGRADED, state, l) == 0)
3104f6e214c7SGavin Maltby 		return (SCF_STATE_DEGRADED);
3105f6e214c7SGavin Maltby 	else if (str_cmp("all", state, l) == 0)
3106f6e214c7SGavin Maltby 		return (SCF_STATE_ALL);
3107f6e214c7SGavin Maltby 	else
3108f6e214c7SGavin Maltby 		return (-1);
3109f6e214c7SGavin Maltby }
3110f6e214c7SGavin Maltby 
3111f6e214c7SGavin Maltby /*
3112f6e214c7SGavin Maltby  * int32_t smf_state_from_string()
3113f6e214c7SGavin Maltby  * return the value of the macro SCF_STATE_* for the corresponding state
3114f6e214c7SGavin Maltby  * it returns SCF_STATE_ALL if "all" is passed. -1 if the string passed doesn't
3115f6e214c7SGavin Maltby  * correspond to any valid state.
3116f6e214c7SGavin Maltby  */
3117f6e214c7SGavin Maltby int32_t
3118f6e214c7SGavin Maltby smf_state_from_string(const char *state)
3119f6e214c7SGavin Maltby {
3120f6e214c7SGavin Maltby 	return (state_from_string(state, 0));
3121f6e214c7SGavin Maltby }
3122f6e214c7SGavin Maltby 
3123f6e214c7SGavin Maltby /*
3124f6e214c7SGavin Maltby  * smf_state_to_string()
3125f6e214c7SGavin Maltby  * Takes an int32_t representing an SMF state and returns
3126f6e214c7SGavin Maltby  * the corresponding string. The string is read only and need not to be
3127f6e214c7SGavin Maltby  * freed.
3128f6e214c7SGavin Maltby  * returns NULL on invalid input.
3129f6e214c7SGavin Maltby  */
3130f6e214c7SGavin Maltby const char *
3131f6e214c7SGavin Maltby smf_state_to_string(int32_t s)
3132f6e214c7SGavin Maltby {
3133f6e214c7SGavin Maltby 	switch (s) {
3134f6e214c7SGavin Maltby 	case SCF_STATE_UNINIT:
3135f6e214c7SGavin Maltby 		return (SCF_STATE_STRING_UNINIT);
3136f6e214c7SGavin Maltby 	case SCF_STATE_MAINT:
3137f6e214c7SGavin Maltby 		return (SCF_STATE_STRING_MAINT);
3138f6e214c7SGavin Maltby 	case SCF_STATE_OFFLINE:
3139f6e214c7SGavin Maltby 		return (SCF_STATE_STRING_OFFLINE);
3140f6e214c7SGavin Maltby 	case SCF_STATE_DISABLED:
3141f6e214c7SGavin Maltby 		return (SCF_STATE_STRING_DISABLED);
3142f6e214c7SGavin Maltby 	case SCF_STATE_ONLINE:
3143f6e214c7SGavin Maltby 		return (SCF_STATE_STRING_ONLINE);
3144f6e214c7SGavin Maltby 	case SCF_STATE_DEGRADED:
3145f6e214c7SGavin Maltby 		return (SCF_STATE_STRING_DEGRADED);
3146f6e214c7SGavin Maltby 	case SCF_STATE_ALL:
3147f6e214c7SGavin Maltby 		return ("all");
3148f6e214c7SGavin Maltby 	default:
3149f6e214c7SGavin Maltby 		return (NULL);
3150f6e214c7SGavin Maltby 	}
3151f6e214c7SGavin Maltby }
3152