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.
24afffa6e9SMarcel Telka  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
25e6ccb06dSAndrew Stormont  * Copyright 2018 RackTop Systems.
26*8fff7887SJohn Levon  * Copyright 2020 Joyent, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "libscf_impl.h"
307c478bd9Sstevel@tonic-gate 
311f6eb021SLiane Praza #include <assert.h>
327c478bd9Sstevel@tonic-gate #include <libuutil.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
3694501b61Sskamm #include <sys/param.h>
3794501b61Sskamm #include <errno.h>
3894501b61Sskamm #include <libgen.h>
39dfac3eb2SDavid Powell #include <assert.h>
407c478bd9Sstevel@tonic-gate #include "midlevel_impl.h"
41d75e6a5dStn #include "lowlevel_impl.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #ifndef NDEBUG
447c478bd9Sstevel@tonic-gate #define	bad_error(func, err)	{					\
457c478bd9Sstevel@tonic-gate 	uu_warn("%s:%d: %s failed with unexpected error %d.  Aborting.\n", \
467c478bd9Sstevel@tonic-gate 	    __FILE__, __LINE__, func, err);				\
477c478bd9Sstevel@tonic-gate 	abort();							\
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate #else
507c478bd9Sstevel@tonic-gate #define	bad_error(func, err)	abort()
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
5394501b61Sskamm /* Path to speedy files area must end with a slash */
5494501b61Sskamm #define	SMF_SPEEDY_FILES_PATH		"/etc/svc/volatile/"
5594501b61Sskamm 
56d75e6a5dStn void
scf_simple_handle_destroy(scf_simple_handle_t * simple_h)57d75e6a5dStn scf_simple_handle_destroy(scf_simple_handle_t *simple_h)
58d75e6a5dStn {
59d75e6a5dStn 	if (simple_h == NULL)
60d75e6a5dStn 		return;
61d75e6a5dStn 
62d75e6a5dStn 	scf_pg_destroy(simple_h->running_pg);
63d75e6a5dStn 	scf_pg_destroy(simple_h->editing_pg);
64d75e6a5dStn 	scf_snapshot_destroy(simple_h->snap);
65d75e6a5dStn 	scf_instance_destroy(simple_h->inst);
66d75e6a5dStn 	scf_handle_destroy(simple_h->h);
67d75e6a5dStn 	uu_free(simple_h);
68d75e6a5dStn }
69d75e6a5dStn 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * Given a base service FMRI and the names of a property group and property,
727c478bd9Sstevel@tonic-gate  * assemble_fmri() merges them into a property FMRI.  Note that if the base
737c478bd9Sstevel@tonic-gate  * FMRI is NULL, assemble_fmri() gets the base FMRI from scf_myname().
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate static char *
assemble_fmri(scf_handle_t * h,const char * base,const char * pg,const char * prop)777c478bd9Sstevel@tonic-gate assemble_fmri(scf_handle_t *h, const char *base, const char *pg,
787c478bd9Sstevel@tonic-gate     const char *prop)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	size_t	fmri_sz, pglen;
817c478bd9Sstevel@tonic-gate 	ssize_t baselen;
827c478bd9Sstevel@tonic-gate 	char	*fmri_buf;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
857c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
867c478bd9Sstevel@tonic-gate 		return (NULL);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (pg == NULL)
907c478bd9Sstevel@tonic-gate 		pglen = strlen(SCF_PG_APP_DEFAULT);
917c478bd9Sstevel@tonic-gate 	else
927c478bd9Sstevel@tonic-gate 		pglen = strlen(pg);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	if (base == NULL) {
957c478bd9Sstevel@tonic-gate 		if ((baselen = scf_myname(h, NULL, 0)) == -1)
967c478bd9Sstevel@tonic-gate 			return (NULL);
977c478bd9Sstevel@tonic-gate 	} else {
987c478bd9Sstevel@tonic-gate 		baselen = strlen(base);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	fmri_sz = baselen + sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
1027c478bd9Sstevel@tonic-gate 	    pglen + sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
1037c478bd9Sstevel@tonic-gate 	    strlen(prop) + 1;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if ((fmri_buf = malloc(fmri_sz)) == NULL) {
1067c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1077c478bd9Sstevel@tonic-gate 		return (NULL);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (base == NULL) {
1117c478bd9Sstevel@tonic-gate 		if (scf_myname(h, fmri_buf, fmri_sz) == -1) {
1127c478bd9Sstevel@tonic-gate 			free(fmri_buf);
1137c478bd9Sstevel@tonic-gate 			return (NULL);
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 	} else {
1167c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri_buf, base);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTYGRP_PREFIX);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (pg == NULL)
1227c478bd9Sstevel@tonic-gate 		(void) strcat(fmri_buf, SCF_PG_APP_DEFAULT);
1237c478bd9Sstevel@tonic-gate 	else
1247c478bd9Sstevel@tonic-gate 		(void) strcat(fmri_buf, pg);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTY_PREFIX);
1277c478bd9Sstevel@tonic-gate 	(void) strcat(fmri_buf, prop);
1287c478bd9Sstevel@tonic-gate 	return (fmri_buf);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * Given a property, this function allocates and fills an scf_simple_prop_t
1337c478bd9Sstevel@tonic-gate  * with the data it contains.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static scf_simple_prop_t *
fill_prop(scf_property_t * prop,const char * pgname,const char * propname,scf_handle_t * h)1377c478bd9Sstevel@tonic-gate fill_prop(scf_property_t *prop, const char *pgname, const char *propname,
1387c478bd9Sstevel@tonic-gate     scf_handle_t *h)
1397c478bd9Sstevel@tonic-gate {
1404a014300SToomas Soome 	scf_simple_prop_t		*ret;
1414a014300SToomas Soome 	scf_iter_t			*iter;
1424a014300SToomas Soome 	scf_value_t			*val;
1434a014300SToomas Soome 	int				iterret, i;
1444a014300SToomas Soome 	ssize_t				valsize, numvals;
1454a014300SToomas Soome 	union scf_simple_prop_val	*vallist = NULL, *vallist_backup = NULL;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if ((ret = malloc(sizeof (*ret))) == NULL) {
1487c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1497c478bd9Sstevel@tonic-gate 		return (NULL);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	ret->pr_next = NULL;
1537c478bd9Sstevel@tonic-gate 	ret->pr_pg = NULL;
1547c478bd9Sstevel@tonic-gate 	ret->pr_iter = 0;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (pgname == NULL)
1577c478bd9Sstevel@tonic-gate 		ret->pr_pgname = strdup(SCF_PG_APP_DEFAULT);
1587c478bd9Sstevel@tonic-gate 	else
1597c478bd9Sstevel@tonic-gate 		ret->pr_pgname = strdup(pgname);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (ret->pr_pgname == NULL) {
1627c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1637c478bd9Sstevel@tonic-gate 		free(ret);
1647c478bd9Sstevel@tonic-gate 		return (NULL);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if ((ret->pr_propname = strdup(propname)) == NULL) {
1687c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1697c478bd9Sstevel@tonic-gate 		free(ret->pr_pgname);
1707c478bd9Sstevel@tonic-gate 		free(ret);
1717c478bd9Sstevel@tonic-gate 		return (NULL);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (scf_property_type(prop, &ret->pr_type) == -1)
1757c478bd9Sstevel@tonic-gate 		goto error3;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
1787c478bd9Sstevel@tonic-gate 		goto error3;
1797c478bd9Sstevel@tonic-gate 	if ((val = scf_value_create(h)) == NULL) {
1807c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
1817c478bd9Sstevel@tonic-gate 		goto error3;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) == -1)
1857c478bd9Sstevel@tonic-gate 		goto error1;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	for (numvals = 0; (iterret = scf_iter_next_value(iter, val)) == 1;
1887c478bd9Sstevel@tonic-gate 	    numvals++) {
1897c478bd9Sstevel@tonic-gate 		vallist_backup = vallist;
1907c478bd9Sstevel@tonic-gate 		if ((vallist = realloc(vallist, (numvals + 1) *
1917c478bd9Sstevel@tonic-gate 		    sizeof (*vallist))) == NULL) {
1927c478bd9Sstevel@tonic-gate 			vallist = vallist_backup;
1937c478bd9Sstevel@tonic-gate 			goto error1;
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		switch (ret->pr_type) {
1977c478bd9Sstevel@tonic-gate 		case SCF_TYPE_BOOLEAN:
1987c478bd9Sstevel@tonic-gate 			if (scf_value_get_boolean(val,
1997c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_bool) == -1)
2007c478bd9Sstevel@tonic-gate 				goto error1;
2017c478bd9Sstevel@tonic-gate 			break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		case SCF_TYPE_COUNT:
2047c478bd9Sstevel@tonic-gate 			if (scf_value_get_count(val,
2057c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_uint) == -1)
2067c478bd9Sstevel@tonic-gate 				goto error1;
2077c478bd9Sstevel@tonic-gate 			break;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		case SCF_TYPE_INTEGER:
2107c478bd9Sstevel@tonic-gate 			if (scf_value_get_integer(val,
2117c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_int) == -1)
2127c478bd9Sstevel@tonic-gate 				goto error1;
2137c478bd9Sstevel@tonic-gate 			break;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		case SCF_TYPE_TIME:
2167c478bd9Sstevel@tonic-gate 			if (scf_value_get_time(val,
2177c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_time.t_sec,
2187c478bd9Sstevel@tonic-gate 			    &vallist[numvals].pv_time.t_nsec) == -1)
2197c478bd9Sstevel@tonic-gate 				goto error1;
2207c478bd9Sstevel@tonic-gate 			break;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		case SCF_TYPE_ASTRING:
2237c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_str = NULL;
2247c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_astring(val, NULL, 0)) ==
2257c478bd9Sstevel@tonic-gate 			    -1)
2267c478bd9Sstevel@tonic-gate 				goto error1;
2277c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2287c478bd9Sstevel@tonic-gate 			    NULL) {
2297c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2307c478bd9Sstevel@tonic-gate 				goto error1;
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 			if (scf_value_get_astring(val,
2337c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_str, valsize+1) == -1) {
2347c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_str);
2357c478bd9Sstevel@tonic-gate 				goto error1;
2367c478bd9Sstevel@tonic-gate 			}
2377c478bd9Sstevel@tonic-gate 			break;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		case SCF_TYPE_USTRING:
2407c478bd9Sstevel@tonic-gate 		case SCF_TYPE_HOST:
2417c478bd9Sstevel@tonic-gate 		case SCF_TYPE_HOSTNAME:
242b56bf881SAntonello Cruz 		case SCF_TYPE_NET_ADDR:
2437c478bd9Sstevel@tonic-gate 		case SCF_TYPE_NET_ADDR_V4:
2447c478bd9Sstevel@tonic-gate 		case SCF_TYPE_NET_ADDR_V6:
2457c478bd9Sstevel@tonic-gate 		case SCF_TYPE_URI:
2467c478bd9Sstevel@tonic-gate 		case SCF_TYPE_FMRI:
2477c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_str = NULL;
2487c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_ustring(val, NULL, 0)) ==
2497c478bd9Sstevel@tonic-gate 			    -1)
2507c478bd9Sstevel@tonic-gate 				goto error1;
2517c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2527c478bd9Sstevel@tonic-gate 			    NULL) {
2537c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2547c478bd9Sstevel@tonic-gate 				goto error1;
2557c478bd9Sstevel@tonic-gate 			}
2567c478bd9Sstevel@tonic-gate 			if (scf_value_get_ustring(val,
2577c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_str, valsize+1) == -1) {
2587c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_str);
2597c478bd9Sstevel@tonic-gate 				goto error1;
2607c478bd9Sstevel@tonic-gate 			}
2617c478bd9Sstevel@tonic-gate 			break;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		case SCF_TYPE_OPAQUE:
2647c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_opaque.o_value = NULL;
2657c478bd9Sstevel@tonic-gate 			if ((valsize = scf_value_get_opaque(val, NULL, 0)) ==
2667c478bd9Sstevel@tonic-gate 			    -1)
2677c478bd9Sstevel@tonic-gate 				goto error1;
2687c478bd9Sstevel@tonic-gate 			if ((vallist[numvals].pv_opaque.o_value =
2697c478bd9Sstevel@tonic-gate 			    malloc(valsize)) == NULL) {
2707c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2717c478bd9Sstevel@tonic-gate 				goto error1;
2727c478bd9Sstevel@tonic-gate 			}
2737c478bd9Sstevel@tonic-gate 			vallist[numvals].pv_opaque.o_size = valsize;
2747c478bd9Sstevel@tonic-gate 			if (scf_value_get_opaque(val,
2757c478bd9Sstevel@tonic-gate 			    vallist[numvals].pv_opaque.o_value,
2767c478bd9Sstevel@tonic-gate 			    valsize) == -1) {
2777c478bd9Sstevel@tonic-gate 				free(vallist[numvals].pv_opaque.o_value);
2787c478bd9Sstevel@tonic-gate 				goto error1;
2797c478bd9Sstevel@tonic-gate 			}
2807c478bd9Sstevel@tonic-gate 			break;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		default:
2837c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2847c478bd9Sstevel@tonic-gate 			goto error1;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (iterret == -1) {
2903eae19d9Swesolows 		int err = scf_error();
2913eae19d9Swesolows 		if (err != SCF_ERROR_CONNECTION_BROKEN &&
2923eae19d9Swesolows 		    err != SCF_ERROR_PERMISSION_DENIED)
2937c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2947c478bd9Sstevel@tonic-gate 		goto error1;
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	ret->pr_vallist = vallist;
2987c478bd9Sstevel@tonic-gate 	ret->pr_numvalues = numvals;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
3017c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	return (ret);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
3077c478bd9Sstevel@tonic-gate 	 * for failures at various stages during the function.
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate error1:
3117c478bd9Sstevel@tonic-gate 	if (vallist == NULL)
3127c478bd9Sstevel@tonic-gate 		goto error2;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	switch (ret->pr_type) {
3157c478bd9Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
3167c478bd9Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
3177c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOST:
3187c478bd9Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
319b56bf881SAntonello Cruz 	case SCF_TYPE_NET_ADDR:
3207c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
3217c478bd9Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
3227c478bd9Sstevel@tonic-gate 	case SCF_TYPE_URI:
3237c478bd9Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
3247c478bd9Sstevel@tonic-gate 		for (i = 0; i < numvals; i++) {
3257c478bd9Sstevel@tonic-gate 			free(vallist[i].pv_str);
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 		break;
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 	case SCF_TYPE_OPAQUE: {
3307c478bd9Sstevel@tonic-gate 		for (i = 0; i < numvals; i++) {
3317c478bd9Sstevel@tonic-gate 			free(vallist[i].pv_opaque.o_value);
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		break;
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	default:
3367c478bd9Sstevel@tonic-gate 		break;
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	free(vallist);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate error2:
3427c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
3437c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate error3:
3467c478bd9Sstevel@tonic-gate 	free(ret->pr_pgname);
3477c478bd9Sstevel@tonic-gate 	free(ret->pr_propname);
3487c478bd9Sstevel@tonic-gate 	free(ret);
3497c478bd9Sstevel@tonic-gate 	return (NULL);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * insert_app_props iterates over a property iterator, getting all the
3547c478bd9Sstevel@tonic-gate  * properties from a property group, and adding or overwriting them into
3557c478bd9Sstevel@tonic-gate  * a simple_app_props_t.  This is used by scf_simple_app_props_get to provide
3567c478bd9Sstevel@tonic-gate  * service/instance composition while filling the app_props_t.
3577c478bd9Sstevel@tonic-gate  * insert_app_props iterates over a single property group.
3587c478bd9Sstevel@tonic-gate  */
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate static int
insert_app_props(scf_iter_t * propiter,char * pgname,char * propname,struct scf_simple_pg * thispg,scf_property_t * prop,size_t namelen,scf_handle_t * h)3617c478bd9Sstevel@tonic-gate insert_app_props(scf_iter_t *propiter, char *pgname, char *propname, struct
3627c478bd9Sstevel@tonic-gate     scf_simple_pg *thispg, scf_property_t *prop, size_t namelen,
3637c478bd9Sstevel@tonic-gate     scf_handle_t *h)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	scf_simple_prop_t	*thisprop, *prevprop, *newprop;
3667c478bd9Sstevel@tonic-gate 	uint8_t			found;
3677c478bd9Sstevel@tonic-gate 	int			propiter_ret;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	while ((propiter_ret = scf_iter_next_property(propiter, prop)) == 1) {
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		if (scf_property_get_name(prop, propname, namelen) < 0) {
3727c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
3737c478bd9Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
3747c478bd9Sstevel@tonic-gate 			return (-1);
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		thisprop = thispg->pg_proplist;
3787c478bd9Sstevel@tonic-gate 		prevprop = thispg->pg_proplist;
3797c478bd9Sstevel@tonic-gate 		found = 0;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		while ((thisprop != NULL) && (!found)) {
3827c478bd9Sstevel@tonic-gate 			if (strcmp(thisprop->pr_propname, propname) == 0) {
3837c478bd9Sstevel@tonic-gate 				found = 1;
3847c478bd9Sstevel@tonic-gate 				if ((newprop = fill_prop(prop, pgname,
3857c478bd9Sstevel@tonic-gate 				    propname, h)) == NULL)
3867c478bd9Sstevel@tonic-gate 					return (-1);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 				if (thisprop == thispg->pg_proplist)
3897c478bd9Sstevel@tonic-gate 					thispg->pg_proplist = newprop;
3907c478bd9Sstevel@tonic-gate 				else
3917c478bd9Sstevel@tonic-gate 					prevprop->pr_next = newprop;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 				newprop->pr_pg = thispg;
3947c478bd9Sstevel@tonic-gate 				newprop->pr_next = thisprop->pr_next;
3957c478bd9Sstevel@tonic-gate 				scf_simple_prop_free(thisprop);
3967c478bd9Sstevel@tonic-gate 				thisprop = NULL;
3977c478bd9Sstevel@tonic-gate 			} else {
3987c478bd9Sstevel@tonic-gate 				if (thisprop != thispg->pg_proplist)
3997c478bd9Sstevel@tonic-gate 					prevprop = prevprop->pr_next;
4007c478bd9Sstevel@tonic-gate 				thisprop = thisprop->pr_next;
4017c478bd9Sstevel@tonic-gate 			}
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		if (!found) {
4057c478bd9Sstevel@tonic-gate 			if ((newprop = fill_prop(prop, pgname, propname, h)) ==
4067c478bd9Sstevel@tonic-gate 			    NULL)
4077c478bd9Sstevel@tonic-gate 				return (-1);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 			if (thispg->pg_proplist == NULL)
4107c478bd9Sstevel@tonic-gate 				thispg->pg_proplist = newprop;
4117c478bd9Sstevel@tonic-gate 			else
4127c478bd9Sstevel@tonic-gate 				prevprop->pr_next = newprop;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 			newprop->pr_pg = thispg;
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (propiter_ret == -1) {
4197c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
4207c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
4217c478bd9Sstevel@tonic-gate 		return (-1);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	return (0);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate /*
4297c478bd9Sstevel@tonic-gate  * Sets up e in tx to set pname's values.  Returns 0 on success or -1 on
4307c478bd9Sstevel@tonic-gate  * failure, with scf_error() set to
4317c478bd9Sstevel@tonic-gate  *   SCF_ERROR_HANDLE_MISMATCH - tx & e are derived from different handles
4327c478bd9Sstevel@tonic-gate  *   SCF_ERROR_INVALID_ARGUMENT - pname or ty are invalid
4337c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_BOUND - handle is not bound
4347c478bd9Sstevel@tonic-gate  *   SCF_ERROR_CONNECTION_BROKEN - connection was broken
4357c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_SET - tx has not been started
4367c478bd9Sstevel@tonic-gate  *   SCF_ERROR_DELETED - the pg tx was started on was deleted
4377c478bd9Sstevel@tonic-gate  */
4387c478bd9Sstevel@tonic-gate static int
transaction_property_set(scf_transaction_t * tx,scf_transaction_entry_t * e,const char * pname,scf_type_t ty)4397c478bd9Sstevel@tonic-gate transaction_property_set(scf_transaction_t *tx, scf_transaction_entry_t *e,
4407c478bd9Sstevel@tonic-gate     const char *pname, scf_type_t ty)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	for (;;) {
4437c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, e, pname, ty) == 0)
4447c478bd9Sstevel@tonic-gate 			return (0);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
4477c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
4487c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4497c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
4507c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4517c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
4527c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
4537c478bd9Sstevel@tonic-gate 		default:
4547c478bd9Sstevel@tonic-gate 			return (-1);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
4577c478bd9Sstevel@tonic-gate 			break;
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, e, pname, ty) == 0)
4617c478bd9Sstevel@tonic-gate 			return (0);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
4647c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
4657c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
4677c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4687c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
4697c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
4707c478bd9Sstevel@tonic-gate 		default:
4717c478bd9Sstevel@tonic-gate 			return (-1);
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
4747c478bd9Sstevel@tonic-gate 			break;
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate static int
get_inst_enabled(const scf_instance_t * inst,const char * pgname)4807c478bd9Sstevel@tonic-gate get_inst_enabled(const scf_instance_t *inst, const char *pgname)
4817c478bd9Sstevel@tonic-gate {
4824a014300SToomas Soome 	scf_propertygroup_t	*gpg = NULL;
4834a014300SToomas Soome 	scf_property_t		*eprop = NULL;
4844a014300SToomas Soome 	scf_value_t		*v = NULL;
4857c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
4867c478bd9Sstevel@tonic-gate 	uint8_t			enabled;
4877c478bd9Sstevel@tonic-gate 	int			ret = -1;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
4907c478bd9Sstevel@tonic-gate 		return (-1);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
4937c478bd9Sstevel@tonic-gate 	    (eprop = scf_property_create(h)) == NULL ||
4947c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL)
4957c478bd9Sstevel@tonic-gate 		goto out;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) ||
4987c478bd9Sstevel@tonic-gate 	    scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) ||
4997c478bd9Sstevel@tonic-gate 	    scf_property_get_value(eprop, v) ||
5007c478bd9Sstevel@tonic-gate 	    scf_value_get_boolean(v, &enabled))
5017c478bd9Sstevel@tonic-gate 		goto out;
5027c478bd9Sstevel@tonic-gate 	ret = enabled;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate out:
5057c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
5067c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
5077c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
5087c478bd9Sstevel@tonic-gate 	return (ret);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate  * set_inst_enabled() is a "master" enable/disable call that takes the
5137c478bd9Sstevel@tonic-gate  * instance and the desired state for the enabled bit in the instance's
5147c478bd9Sstevel@tonic-gate  * named property group.  If the group doesn't exist, it's created with the
5157c478bd9Sstevel@tonic-gate  * given flags.  Called by smf_{dis,en}able_instance().
516*8fff7887SJohn Levon  *
517*8fff7887SJohn Levon  * Note that if we're enabling, comment will be "", and we use that to clear out
518*8fff7887SJohn Levon  * any old disabled comment.
5197c478bd9Sstevel@tonic-gate  */
5207c478bd9Sstevel@tonic-gate static int
set_inst_enabled(const scf_instance_t * inst,uint8_t desired,const char * pgname,uint32_t pgflags,const char * comment)5217c478bd9Sstevel@tonic-gate set_inst_enabled(const scf_instance_t *inst, uint8_t desired,
522*8fff7887SJohn Levon     const char *pgname, uint32_t pgflags, const char *comment)
5237c478bd9Sstevel@tonic-gate {
5244a014300SToomas Soome 	scf_transaction_t	*tx = NULL;
525*8fff7887SJohn Levon 	scf_transaction_entry_t *ent1 = NULL;
526*8fff7887SJohn Levon 	scf_transaction_entry_t *ent2 = NULL;
5274a014300SToomas Soome 	scf_propertygroup_t	*gpg = NULL;
5284a014300SToomas Soome 	scf_property_t		*eprop = NULL;
529*8fff7887SJohn Levon 	scf_value_t		*v1 = NULL;
530*8fff7887SJohn Levon 	scf_value_t		*v2 = NULL;
5317c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
5324a014300SToomas Soome 	int			ret = -1;
5337c478bd9Sstevel@tonic-gate 	int			committed;
5347c478bd9Sstevel@tonic-gate 	uint8_t			b;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
5377c478bd9Sstevel@tonic-gate 		return (-1);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
5407c478bd9Sstevel@tonic-gate 	    (eprop = scf_property_create(h)) == NULL ||
541*8fff7887SJohn Levon 	    (v1 = scf_value_create(h)) == NULL ||
542*8fff7887SJohn Levon 	    (v2 = scf_value_create(h)) == NULL ||
5437c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
544*8fff7887SJohn Levon 	    (ent1 = scf_entry_create(h)) == NULL ||
545*8fff7887SJohn Levon 	    (ent2 = scf_entry_create(h)) == NULL)
5467c478bd9Sstevel@tonic-gate 		goto out;
5477c478bd9Sstevel@tonic-gate 
548fee38f6fStn general_pg_get:
549fee38f6fStn 	if (scf_instance_get_pg(inst, SCF_PG_GENERAL, gpg) == -1) {
5507c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5517c478bd9Sstevel@tonic-gate 			goto out;
5527c478bd9Sstevel@tonic-gate 
553fee38f6fStn 		if (scf_instance_add_pg(inst, SCF_PG_GENERAL,
554fee38f6fStn 		    SCF_GROUP_FRAMEWORK, SCF_PG_GENERAL_FLAGS, gpg) == -1) {
5557c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_EXISTS)
5567c478bd9Sstevel@tonic-gate 				goto out;
557fee38f6fStn 			goto general_pg_get;
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 	}
560fee38f6fStn 
561fee38f6fStn 	if (strcmp(pgname, SCF_PG_GENERAL) != 0) {
562fee38f6fStn get:
563fee38f6fStn 		if (scf_instance_get_pg(inst, pgname, gpg) == -1) {
564fee38f6fStn 			if (scf_error() != SCF_ERROR_NOT_FOUND)
565fee38f6fStn 				goto out;
566fee38f6fStn 
567fee38f6fStn 			if (scf_instance_add_pg(inst, pgname,
568fee38f6fStn 			    SCF_GROUP_FRAMEWORK, pgflags, gpg) == -1) {
569fee38f6fStn 				if (scf_error() != SCF_ERROR_EXISTS)
570fee38f6fStn 					goto out;
571fee38f6fStn 				goto get;
572fee38f6fStn 			}
573fee38f6fStn 		}
574fee38f6fStn 	}
575fee38f6fStn 
5767c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) == -1) {
5777c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5787c478bd9Sstevel@tonic-gate 			goto out;
5797c478bd9Sstevel@tonic-gate 		else
5807c478bd9Sstevel@tonic-gate 			goto set;
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/*
5847c478bd9Sstevel@tonic-gate 	 * If it's already set the way we want, forgo the transaction.
5857c478bd9Sstevel@tonic-gate 	 */
586*8fff7887SJohn Levon 	if (scf_property_get_value(eprop, v1) == -1) {
5877c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
5887c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
5897c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
5907c478bd9Sstevel@tonic-gate 			/* Misconfigured, so set anyway. */
5917c478bd9Sstevel@tonic-gate 			goto set;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		default:
5947c478bd9Sstevel@tonic-gate 			goto out;
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 	}
597*8fff7887SJohn Levon 	if (scf_value_get_boolean(v1, &b) == -1) {
5987c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
5997c478bd9Sstevel@tonic-gate 			goto out;
6007c478bd9Sstevel@tonic-gate 		goto set;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 	if (b == desired) {
6037c478bd9Sstevel@tonic-gate 		ret = 0;
6047c478bd9Sstevel@tonic-gate 		goto out;
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate set:
6087c478bd9Sstevel@tonic-gate 	do {
6097c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) == -1)
6107c478bd9Sstevel@tonic-gate 			goto out;
6117c478bd9Sstevel@tonic-gate 
612*8fff7887SJohn Levon 		if (transaction_property_set(tx, ent1, SCF_PROPERTY_ENABLED,
6137c478bd9Sstevel@tonic-gate 		    SCF_TYPE_BOOLEAN) != 0) {
6147c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
6157c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
6167c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
6177c478bd9Sstevel@tonic-gate 			default:
6187c478bd9Sstevel@tonic-gate 				goto out;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
6217c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
6227c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
6237c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
6247c478bd9Sstevel@tonic-gate 				bad_error("transaction_property_set",
6257c478bd9Sstevel@tonic-gate 				    scf_error());
6267c478bd9Sstevel@tonic-gate 			}
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 
629*8fff7887SJohn Levon 		scf_value_set_boolean(v1, desired);
630*8fff7887SJohn Levon 		if (scf_entry_add_value(ent1, v1) == -1)
631*8fff7887SJohn Levon 			goto out;
632*8fff7887SJohn Levon 
633*8fff7887SJohn Levon 		if (transaction_property_set(tx, ent2,
634*8fff7887SJohn Levon 		    SCF_PROPERTY_COMMENT, SCF_TYPE_ASTRING) != 0) {
635*8fff7887SJohn Levon 			switch (scf_error()) {
636*8fff7887SJohn Levon 			case SCF_ERROR_CONNECTION_BROKEN:
637*8fff7887SJohn Levon 			case SCF_ERROR_DELETED:
638*8fff7887SJohn Levon 			default:
639*8fff7887SJohn Levon 				goto out;
640*8fff7887SJohn Levon 
641*8fff7887SJohn Levon 			case SCF_ERROR_HANDLE_MISMATCH:
642*8fff7887SJohn Levon 			case SCF_ERROR_INVALID_ARGUMENT:
643*8fff7887SJohn Levon 			case SCF_ERROR_NOT_BOUND:
644*8fff7887SJohn Levon 			case SCF_ERROR_NOT_SET:
645*8fff7887SJohn Levon 				bad_error("transaction_property_set",
646*8fff7887SJohn Levon 				    scf_error());
647*8fff7887SJohn Levon 			}
648*8fff7887SJohn Levon 		}
649*8fff7887SJohn Levon 
650*8fff7887SJohn Levon 		if (scf_value_set_astring(v2, comment) == -1)
651*8fff7887SJohn Levon 			goto out;
652*8fff7887SJohn Levon 
653*8fff7887SJohn Levon 		if (scf_entry_add_value(ent2, v2) == -1)
6547c478bd9Sstevel@tonic-gate 			goto out;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		committed = scf_transaction_commit(tx);
6577c478bd9Sstevel@tonic-gate 		if (committed == -1)
6587c478bd9Sstevel@tonic-gate 			goto out;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		if (committed == 0) { /* out-of-sync */
6637c478bd9Sstevel@tonic-gate 			if (scf_pg_update(gpg) == -1)
6647c478bd9Sstevel@tonic-gate 				goto out;
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 	} while (committed == 0);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	ret = 0;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate out:
671*8fff7887SJohn Levon 	scf_value_destroy(v1);
672*8fff7887SJohn Levon 	scf_value_destroy(v2);
673*8fff7887SJohn Levon 	scf_entry_destroy(ent1);
674*8fff7887SJohn Levon 	scf_entry_destroy(ent2);
6757c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
6767c478bd9Sstevel@tonic-gate 	scf_property_destroy(eprop);
6777c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	return (ret);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate static int
delete_inst_enabled(const scf_instance_t * inst,const char * pgname)6837c478bd9Sstevel@tonic-gate delete_inst_enabled(const scf_instance_t *inst, const char *pgname)
6847c478bd9Sstevel@tonic-gate {
6854a014300SToomas Soome 	scf_transaction_t	*tx = NULL;
686*8fff7887SJohn Levon 	scf_transaction_entry_t *ent1 = NULL;
687*8fff7887SJohn Levon 	scf_transaction_entry_t *ent2 = NULL;
6884a014300SToomas Soome 	scf_propertygroup_t	*gpg = NULL;
6897c478bd9Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
6907c478bd9Sstevel@tonic-gate 	int			ret = -1;
6917c478bd9Sstevel@tonic-gate 	int			committed;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
6947c478bd9Sstevel@tonic-gate 		return (-1);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
6977c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
698*8fff7887SJohn Levon 	    (ent1 = scf_entry_create(h)) == NULL ||
699*8fff7887SJohn Levon 	    (ent2 = scf_entry_create(h)) == NULL)
7007c478bd9Sstevel@tonic-gate 		goto out;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) != 0)
7037c478bd9Sstevel@tonic-gate 		goto error;
7047c478bd9Sstevel@tonic-gate 	do {
705*8fff7887SJohn Levon 		if (scf_transaction_start(tx, gpg) == -1)
706*8fff7887SJohn Levon 			goto error;
707*8fff7887SJohn Levon 
708*8fff7887SJohn Levon 		ret = scf_transaction_property_delete(tx, ent1,
709*8fff7887SJohn Levon 		    SCF_PROPERTY_ENABLED);
710*8fff7887SJohn Levon 
711*8fff7887SJohn Levon 		if (ret == -1 && scf_error() != SCF_ERROR_DELETED &&
712*8fff7887SJohn Levon 		    scf_error() != SCF_ERROR_NOT_FOUND)
713*8fff7887SJohn Levon 			goto error;
714*8fff7887SJohn Levon 
715*8fff7887SJohn Levon 		ret = scf_transaction_property_delete(tx, ent2,
716*8fff7887SJohn Levon 		    SCF_PROPERTY_COMMENT);
717*8fff7887SJohn Levon 
718*8fff7887SJohn Levon 		if (ret == -1 && scf_error() != SCF_ERROR_DELETED &&
719*8fff7887SJohn Levon 		    scf_error() != SCF_ERROR_NOT_FOUND)
720*8fff7887SJohn Levon 			goto error;
721*8fff7887SJohn Levon 
722*8fff7887SJohn Levon 		if ((committed = scf_transaction_commit(tx)) == -1)
7237c478bd9Sstevel@tonic-gate 			goto error;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 		if (committed == 0 && scf_pg_update(gpg) == -1)
7287c478bd9Sstevel@tonic-gate 			goto error;
7297c478bd9Sstevel@tonic-gate 	} while (committed == 0);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	ret = 0;
7327c478bd9Sstevel@tonic-gate 	goto out;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate error:
7357c478bd9Sstevel@tonic-gate 	switch (scf_error()) {
7367c478bd9Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
7377c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
7387c478bd9Sstevel@tonic-gate 		/* success */
7397c478bd9Sstevel@tonic-gate 		ret = 0;
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate out:
743*8fff7887SJohn Levon 	scf_entry_destroy(ent1);
744*8fff7887SJohn Levon 	scf_entry_destroy(ent2);
7457c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
7467c478bd9Sstevel@tonic-gate 	scf_pg_destroy(gpg);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	return (ret);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate  * Returns 0 on success or -1 on failure.  On failure leaves scf_error() set to
7537c478bd9Sstevel@tonic-gate  *   SCF_ERROR_HANDLE_DESTROYED - inst's handle has been destroyed
7547c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_BOUND - inst's handle is not bound
7557c478bd9Sstevel@tonic-gate  *   SCF_ERROR_CONNECTION_BROKEN - the repository connection was broken
7567c478bd9Sstevel@tonic-gate  *   SCF_ERROR_NOT_SET - inst is not set
7577c478bd9Sstevel@tonic-gate  *   SCF_ERROR_DELETED - inst was deleted
7587c478bd9Sstevel@tonic-gate  *   SCF_ERROR_PERMISSION_DENIED
7597c478bd9Sstevel@tonic-gate  *   SCF_ERROR_BACKEND_ACCESS
7607c478bd9Sstevel@tonic-gate  *   SCF_ERROR_BACKEND_READONLY
7617c478bd9Sstevel@tonic-gate  */
7627c478bd9Sstevel@tonic-gate static int
set_inst_action_inst(scf_instance_t * inst,const char * action)7637c478bd9Sstevel@tonic-gate set_inst_action_inst(scf_instance_t *inst, const char *action)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate 	scf_handle_t			*h;
7667c478bd9Sstevel@tonic-gate 	scf_transaction_t		*tx = NULL;
7677c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t		*ent = NULL;
7687c478bd9Sstevel@tonic-gate 	scf_propertygroup_t		*pg = NULL;
7697c478bd9Sstevel@tonic-gate 	scf_property_t			*prop = NULL;
7707c478bd9Sstevel@tonic-gate 	scf_value_t			*v = NULL;
7717c478bd9Sstevel@tonic-gate 	int				trans, ret = -1;
7727c478bd9Sstevel@tonic-gate 	int64_t				t;
7737c478bd9Sstevel@tonic-gate 	hrtime_t			timestamp;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL ||
7767c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
7777c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
7787c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL ||
7797c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
7807c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
7817c478bd9Sstevel@tonic-gate 		goto out;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate get:
7847c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER_ACTIONS, pg) == -1) {
7857c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
7867c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
7877c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7887c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7897c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7907c478bd9Sstevel@tonic-gate 		default:
7917c478bd9Sstevel@tonic-gate 			goto out;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7947c478bd9Sstevel@tonic-gate 			break;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7977c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7987c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
7997c478bd9Sstevel@tonic-gate 		}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		/* Try creating the restarter_actions property group. */
8027c478bd9Sstevel@tonic-gate add:
8037c478bd9Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
8047c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_TYPE,
8057c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) {
8067c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8077c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8087c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8097c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8107c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8117c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8127c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8137c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8147c478bd9Sstevel@tonic-gate 			default:
8157c478bd9Sstevel@tonic-gate 				goto out;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 			case SCF_ERROR_EXISTS:
8187c478bd9Sstevel@tonic-gate 				goto get;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8217c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
8227c478bd9Sstevel@tonic-gate 				bad_error("scf_instance_add_pg", scf_error());
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	for (;;) {
8287c478bd9Sstevel@tonic-gate 		timestamp = gethrtime();
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, action, prop) != 0) {
8317c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8327c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8337c478bd9Sstevel@tonic-gate 			default:
8347c478bd9Sstevel@tonic-gate 				goto out;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8377c478bd9Sstevel@tonic-gate 				goto add;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
8407c478bd9Sstevel@tonic-gate 				break;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8437c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
8447c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8457c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8467c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 		} else if (scf_property_get_value(prop, v) != 0) {
8497c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8507c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8517c478bd9Sstevel@tonic-gate 			default:
8527c478bd9Sstevel@tonic-gate 				goto out;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8557c478bd9Sstevel@tonic-gate 				goto add;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
8587c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
8597c478bd9Sstevel@tonic-gate 				break;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8627c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8637c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8647c478bd9Sstevel@tonic-gate 				bad_error("scf_property_get_value",
8657c478bd9Sstevel@tonic-gate 				    scf_error());
8667c478bd9Sstevel@tonic-gate 			}
8677c478bd9Sstevel@tonic-gate 		} else if (scf_value_get_integer(v, &t) != 0) {
8687c478bd9Sstevel@tonic-gate 			bad_error("scf_value_get_integer", scf_error());
8697c478bd9Sstevel@tonic-gate 		} else if (t > timestamp) {
8707c478bd9Sstevel@tonic-gate 			break;
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) == -1) {
8747c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8757c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8767c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8777c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8787c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8797c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8807c478bd9Sstevel@tonic-gate 			default:
8817c478bd9Sstevel@tonic-gate 				goto out;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8847c478bd9Sstevel@tonic-gate 				goto add;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8877c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8887c478bd9Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
8897c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_start", scf_error());
8907c478bd9Sstevel@tonic-gate 			}
8917c478bd9Sstevel@tonic-gate 		}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		if (transaction_property_set(tx, ent, action,
8947c478bd9Sstevel@tonic-gate 		    SCF_TYPE_INTEGER) != 0) {
8957c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
8967c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8977c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8987c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8997c478bd9Sstevel@tonic-gate 			default:
9007c478bd9Sstevel@tonic-gate 				goto out;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
9037c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
9047c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
9057c478bd9Sstevel@tonic-gate 				bad_error("transaction_property_set",
9067c478bd9Sstevel@tonic-gate 				    scf_error());
9077c478bd9Sstevel@tonic-gate 			}
9087c478bd9Sstevel@tonic-gate 		}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 		scf_value_set_integer(v, timestamp);
9117c478bd9Sstevel@tonic-gate 		if (scf_entry_add_value(ent, v) == -1)
9127c478bd9Sstevel@tonic-gate 			bad_error("scf_entry_add_value", scf_error());
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 		trans = scf_transaction_commit(tx);
9157c478bd9Sstevel@tonic-gate 		if (trans == 1)
9167c478bd9Sstevel@tonic-gate 			break;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 		if (trans != 0) {
9197c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
9207c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
9217c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
9227c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
9237c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
9247c478bd9Sstevel@tonic-gate 			default:
9257c478bd9Sstevel@tonic-gate 				goto out;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
9287c478bd9Sstevel@tonic-gate 				scf_transaction_reset(tx);
9297c478bd9Sstevel@tonic-gate 				goto add;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
9327c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
9337c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
9347c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_commit",
9357c478bd9Sstevel@tonic-gate 				    scf_error());
9367c478bd9Sstevel@tonic-gate 			}
9377c478bd9Sstevel@tonic-gate 		}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
940bc9767f9Stn 		if (scf_pg_update(pg) == -1) {
9417c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
9427c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
9437c478bd9Sstevel@tonic-gate 			default:
9447c478bd9Sstevel@tonic-gate 				goto out;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
9477c478bd9Sstevel@tonic-gate 				goto add;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
9507c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
9517c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
9527c478bd9Sstevel@tonic-gate 			}
9537c478bd9Sstevel@tonic-gate 		}
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	ret = 0;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate out:
9597c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
9607c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
9617c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
9627c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
9637c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
9647c478bd9Sstevel@tonic-gate 	return (ret);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate static int
set_inst_action(const char * fmri,const char * action)9687c478bd9Sstevel@tonic-gate set_inst_action(const char *fmri, const char *action)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
9717c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
9727c478bd9Sstevel@tonic-gate 	int ret = -1;
9737c478bd9Sstevel@tonic-gate 
974f6e214c7SGavin Maltby 	h = _scf_handle_create_and_bind(SCF_VERSION);
9757c478bd9Sstevel@tonic-gate 	if (h == NULL)
9767c478bd9Sstevel@tonic-gate 		return (-1);
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	if (inst != NULL) {
9817c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
982bf6e99ccSGary Mills 		    NULL, SCF_DECODE_FMRI_EXACT) == 0) {
9837c478bd9Sstevel@tonic-gate 			ret = set_inst_action_inst(inst, action);
984bf6e99ccSGary Mills 			if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
985bf6e99ccSGary Mills 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
986bf6e99ccSGary Mills 		} else {
987bf6e99ccSGary Mills 			switch (scf_error()) {
988bf6e99ccSGary Mills 			case SCF_ERROR_CONSTRAINT_VIOLATED:
989bf6e99ccSGary Mills 				(void) scf_set_error(
990bf6e99ccSGary Mills 				    SCF_ERROR_INVALID_ARGUMENT);
991bf6e99ccSGary Mills 				break;
992bf6e99ccSGary Mills 			case SCF_ERROR_DELETED:
993bf6e99ccSGary Mills 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
994bf6e99ccSGary Mills 				break;
995bf6e99ccSGary Mills 			}
996bf6e99ccSGary Mills 		}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	return (ret);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate  * get_inst_state() gets the state string from an instance, and returns
10097c478bd9Sstevel@tonic-gate  * the SCF_STATE_* constant that coincides with the instance's current state.
10107c478bd9Sstevel@tonic-gate  */
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate static int
get_inst_state(scf_instance_t * inst,scf_handle_t * h)10137c478bd9Sstevel@tonic-gate get_inst_state(scf_instance_t *inst, scf_handle_t *h)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate 	scf_propertygroup_t	*pg = NULL;
10167c478bd9Sstevel@tonic-gate 	scf_property_t		*prop = NULL;
10177c478bd9Sstevel@tonic-gate 	scf_value_t		*val = NULL;
10187c478bd9Sstevel@tonic-gate 	char			state[MAX_SCF_STATE_STRING_SZ];
10197c478bd9Sstevel@tonic-gate 	int			ret = -1;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	if (((pg = scf_pg_create(h)) == NULL) ||
10227c478bd9Sstevel@tonic-gate 	    ((prop = scf_property_create(h)) == NULL) ||
10237c478bd9Sstevel@tonic-gate 	    ((val = scf_value_create(h)) == NULL))
10247c478bd9Sstevel@tonic-gate 		goto out;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	/* Pull the state property from the instance */
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1 ||
10297c478bd9Sstevel@tonic-gate 	    scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
10307c478bd9Sstevel@tonic-gate 	    scf_property_get_value(prop, val) == -1) {
10317c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
10327c478bd9Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
10337c478bd9Sstevel@tonic-gate 		goto out;
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	if (scf_value_get_astring(val, state, sizeof (state)) <= 0) {
10377c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INTERNAL);
10387c478bd9Sstevel@tonic-gate 		goto out;
10397c478bd9Sstevel@tonic-gate 	}
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
10427c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_UNINIT;
10437c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
10447c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_MAINT;
10457c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0) {
10467c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_OFFLINE;
10477c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) {
10487c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_DISABLED;
10497c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
10507c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_ONLINE;
10517c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
10527c478bd9Sstevel@tonic-gate 		ret = SCF_STATE_DEGRADED;
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate out:
10567c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
10577c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
10587c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	return (ret);
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate /*
10647c478bd9Sstevel@tonic-gate  * Sets an instance to be enabled or disabled after reboot, using the
10657c478bd9Sstevel@tonic-gate  * temporary (overriding) general_ovr property group to reflect the
10667c478bd9Sstevel@tonic-gate  * present state, if it is different.
10677c478bd9Sstevel@tonic-gate  */
10687c478bd9Sstevel@tonic-gate static int
set_inst_enabled_atboot(scf_instance_t * inst,uint8_t desired,const char * comment)1069*8fff7887SJohn Levon set_inst_enabled_atboot(scf_instance_t *inst, uint8_t desired,
1070*8fff7887SJohn Levon     const char *comment)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	int enabled;
10737c478bd9Sstevel@tonic-gate 	int persistent;
10747c478bd9Sstevel@tonic-gate 	int ret = -1;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	if ((persistent = get_inst_enabled(inst, SCF_PG_GENERAL)) < 0) {
10777c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
10787c478bd9Sstevel@tonic-gate 			goto out;
10797c478bd9Sstevel@tonic-gate 		persistent = B_FALSE;
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 	if ((enabled = get_inst_enabled(inst, SCF_PG_GENERAL_OVR)) < 0) {
10827c478bd9Sstevel@tonic-gate 		enabled = persistent;
10837c478bd9Sstevel@tonic-gate 		if (persistent != desired) {
10847c478bd9Sstevel@tonic-gate 			/*
10857c478bd9Sstevel@tonic-gate 			 * Temporarily store the present enabled state.
10867c478bd9Sstevel@tonic-gate 			 */
10877c478bd9Sstevel@tonic-gate 			if (set_inst_enabled(inst, persistent,
1088*8fff7887SJohn Levon 			    SCF_PG_GENERAL_OVR, SCF_PG_GENERAL_OVR_FLAGS,
1089*8fff7887SJohn Levon 			    comment))
10907c478bd9Sstevel@tonic-gate 				goto out;
10917c478bd9Sstevel@tonic-gate 		}
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 	if (persistent != desired)
10947c478bd9Sstevel@tonic-gate 		if (set_inst_enabled(inst, desired, SCF_PG_GENERAL,
1095*8fff7887SJohn Levon 		    SCF_PG_GENERAL_FLAGS, comment))
10967c478bd9Sstevel@tonic-gate 			goto out;
10977c478bd9Sstevel@tonic-gate 	if (enabled == desired)
10987c478bd9Sstevel@tonic-gate 		ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
10997c478bd9Sstevel@tonic-gate 	else
11007c478bd9Sstevel@tonic-gate 		ret = 0;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate out:
11037c478bd9Sstevel@tonic-gate 	return (ret);
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate static int
set_inst_enabled_flags(const char * fmri,int flags,uint8_t desired,const char * comment)1107*8fff7887SJohn Levon set_inst_enabled_flags(const char *fmri, int flags, uint8_t desired,
1108*8fff7887SJohn Levon     const char *comment)
11097c478bd9Sstevel@tonic-gate {
11107c478bd9Sstevel@tonic-gate 	int ret = -1;
11117c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
11127c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	if (flags & ~(SMF_TEMPORARY | SMF_AT_NEXT_BOOT) ||
11157c478bd9Sstevel@tonic-gate 	    flags & SMF_TEMPORARY && flags & SMF_AT_NEXT_BOOT) {
11167c478bd9Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
11177c478bd9Sstevel@tonic-gate 		return (ret);
11187c478bd9Sstevel@tonic-gate 	}
11197c478bd9Sstevel@tonic-gate 
1120