xref: /onnv-gate/usr/src/lib/libscf/common/midlevel.c (revision 12967:ab9ae749152f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53175Sskamm  * Common Development and Distribution License (the "License").
63175Sskamm  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215040Swesolows 
220Sstevel@tonic-gate /*
2312466SJan.Friedel@Sun.COM  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include "libscf_impl.h"
270Sstevel@tonic-gate 
287887SLiane.Praza@Sun.COM #include <assert.h>
290Sstevel@tonic-gate #include <libuutil.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <stdlib.h>
333175Sskamm #include <sys/param.h>
343175Sskamm #include <errno.h>
353175Sskamm #include <libgen.h>
367734SDavid.Powell@sun.com #include <assert.h>
370Sstevel@tonic-gate #include "midlevel_impl.h"
384119Stn143363 #include "lowlevel_impl.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #ifndef NDEBUG
410Sstevel@tonic-gate #define	bad_error(func, err)	{					\
420Sstevel@tonic-gate 	uu_warn("%s:%d: %s failed with unexpected error %d.  Aborting.\n", \
430Sstevel@tonic-gate 	    __FILE__, __LINE__, func, err);				\
440Sstevel@tonic-gate 	abort();							\
450Sstevel@tonic-gate }
460Sstevel@tonic-gate #else
470Sstevel@tonic-gate #define	bad_error(func, err)	abort()
480Sstevel@tonic-gate #endif
490Sstevel@tonic-gate 
503175Sskamm /* Path to speedy files area must end with a slash */
513175Sskamm #define	SMF_SPEEDY_FILES_PATH		"/etc/svc/volatile/"
523175Sskamm 
534119Stn143363 void
scf_simple_handle_destroy(scf_simple_handle_t * simple_h)544119Stn143363 scf_simple_handle_destroy(scf_simple_handle_t *simple_h)
554119Stn143363 {
564119Stn143363 	if (simple_h == NULL)
574119Stn143363 		return;
584119Stn143363 
594119Stn143363 	scf_pg_destroy(simple_h->running_pg);
604119Stn143363 	scf_pg_destroy(simple_h->editing_pg);
614119Stn143363 	scf_snapshot_destroy(simple_h->snap);
624119Stn143363 	scf_instance_destroy(simple_h->inst);
634119Stn143363 	scf_handle_destroy(simple_h->h);
644119Stn143363 	uu_free(simple_h);
654119Stn143363 }
664119Stn143363 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * Given a base service FMRI and the names of a property group and property,
690Sstevel@tonic-gate  * assemble_fmri() merges them into a property FMRI.  Note that if the base
700Sstevel@tonic-gate  * FMRI is NULL, assemble_fmri() gets the base FMRI from scf_myname().
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static char *
assemble_fmri(scf_handle_t * h,const char * base,const char * pg,const char * prop)740Sstevel@tonic-gate assemble_fmri(scf_handle_t *h, const char *base, const char *pg,
750Sstevel@tonic-gate     const char *prop)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	size_t	fmri_sz, pglen;
780Sstevel@tonic-gate 	ssize_t baselen;
790Sstevel@tonic-gate 	char	*fmri_buf;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (prop == NULL) {
820Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
830Sstevel@tonic-gate 		return (NULL);
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (pg == NULL)
870Sstevel@tonic-gate 		pglen = strlen(SCF_PG_APP_DEFAULT);
880Sstevel@tonic-gate 	else
890Sstevel@tonic-gate 		pglen = strlen(pg);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (base == NULL) {
920Sstevel@tonic-gate 		if ((baselen = scf_myname(h, NULL, 0)) == -1)
930Sstevel@tonic-gate 			return (NULL);
940Sstevel@tonic-gate 	} else {
950Sstevel@tonic-gate 		baselen = strlen(base);
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	fmri_sz = baselen + sizeof (SCF_FMRI_PROPERTYGRP_PREFIX) - 1 +
990Sstevel@tonic-gate 	    pglen + sizeof (SCF_FMRI_PROPERTY_PREFIX) - 1 +
1000Sstevel@tonic-gate 	    strlen(prop) + 1;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	if ((fmri_buf = malloc(fmri_sz)) == NULL) {
1030Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1040Sstevel@tonic-gate 		return (NULL);
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (base == NULL) {
1080Sstevel@tonic-gate 		if (scf_myname(h, fmri_buf, fmri_sz) == -1) {
1090Sstevel@tonic-gate 			free(fmri_buf);
1100Sstevel@tonic-gate 			return (NULL);
1110Sstevel@tonic-gate 		}
1120Sstevel@tonic-gate 	} else {
1130Sstevel@tonic-gate 		(void) strcpy(fmri_buf, base);
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTYGRP_PREFIX);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (pg == NULL)
1190Sstevel@tonic-gate 		(void) strcat(fmri_buf, SCF_PG_APP_DEFAULT);
1200Sstevel@tonic-gate 	else
1210Sstevel@tonic-gate 		(void) strcat(fmri_buf, pg);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	(void) strcat(fmri_buf, SCF_FMRI_PROPERTY_PREFIX);
1240Sstevel@tonic-gate 	(void) strcat(fmri_buf, prop);
1250Sstevel@tonic-gate 	return (fmri_buf);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * Given a property, this function allocates and fills an scf_simple_prop_t
1300Sstevel@tonic-gate  * with the data it contains.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static scf_simple_prop_t *
fill_prop(scf_property_t * prop,const char * pgname,const char * propname,scf_handle_t * h)1340Sstevel@tonic-gate fill_prop(scf_property_t *prop, const char *pgname, const char *propname,
1350Sstevel@tonic-gate     scf_handle_t *h)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	scf_simple_prop_t 		*ret;
1380Sstevel@tonic-gate 	scf_iter_t 			*iter;
1390Sstevel@tonic-gate 	scf_value_t 			*val;
1400Sstevel@tonic-gate 	int 				iterret, i;
1410Sstevel@tonic-gate 	ssize_t 			valsize, numvals;
1420Sstevel@tonic-gate 	union scf_simple_prop_val 	*vallist = NULL, *vallist_backup = NULL;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if ((ret = malloc(sizeof (*ret))) == NULL) {
1450Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1460Sstevel@tonic-gate 		return (NULL);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	ret->pr_next = NULL;
1500Sstevel@tonic-gate 	ret->pr_pg = NULL;
1510Sstevel@tonic-gate 	ret->pr_iter = 0;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if (pgname == NULL)
1540Sstevel@tonic-gate 		ret->pr_pgname = strdup(SCF_PG_APP_DEFAULT);
1550Sstevel@tonic-gate 	else
1560Sstevel@tonic-gate 		ret->pr_pgname = strdup(pgname);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	if (ret->pr_pgname == NULL) {
1590Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1600Sstevel@tonic-gate 		free(ret);
1610Sstevel@tonic-gate 		return (NULL);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if ((ret->pr_propname = strdup(propname)) == NULL) {
1650Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
1660Sstevel@tonic-gate 		free(ret->pr_pgname);
1670Sstevel@tonic-gate 		free(ret);
1680Sstevel@tonic-gate 		return (NULL);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	if (scf_property_type(prop, &ret->pr_type) == -1)
1720Sstevel@tonic-gate 		goto error3;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
1750Sstevel@tonic-gate 		goto error3;
1760Sstevel@tonic-gate 	if ((val = scf_value_create(h)) == NULL) {
1770Sstevel@tonic-gate 		scf_iter_destroy(iter);
1780Sstevel@tonic-gate 		goto error3;
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) == -1)
1820Sstevel@tonic-gate 		goto error1;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	for (numvals = 0; (iterret = scf_iter_next_value(iter, val)) == 1;
1850Sstevel@tonic-gate 	    numvals++) {
1860Sstevel@tonic-gate 		vallist_backup = vallist;
1870Sstevel@tonic-gate 		if ((vallist = realloc(vallist, (numvals + 1) *
1880Sstevel@tonic-gate 		    sizeof (*vallist))) == NULL) {
1890Sstevel@tonic-gate 			vallist = vallist_backup;
1900Sstevel@tonic-gate 			goto error1;
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		switch (ret->pr_type) {
1940Sstevel@tonic-gate 		case SCF_TYPE_BOOLEAN:
1950Sstevel@tonic-gate 			if (scf_value_get_boolean(val,
1960Sstevel@tonic-gate 			    &vallist[numvals].pv_bool) == -1)
1970Sstevel@tonic-gate 				goto error1;
1980Sstevel@tonic-gate 			break;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		case SCF_TYPE_COUNT:
2010Sstevel@tonic-gate 			if (scf_value_get_count(val,
2020Sstevel@tonic-gate 			    &vallist[numvals].pv_uint) == -1)
2030Sstevel@tonic-gate 				goto error1;
2040Sstevel@tonic-gate 			break;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		case SCF_TYPE_INTEGER:
2070Sstevel@tonic-gate 			if (scf_value_get_integer(val,
2080Sstevel@tonic-gate 			    &vallist[numvals].pv_int) == -1)
2090Sstevel@tonic-gate 				goto error1;
2100Sstevel@tonic-gate 			break;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		case SCF_TYPE_TIME:
2130Sstevel@tonic-gate 			if (scf_value_get_time(val,
2140Sstevel@tonic-gate 			    &vallist[numvals].pv_time.t_sec,
2150Sstevel@tonic-gate 			    &vallist[numvals].pv_time.t_nsec) == -1)
2160Sstevel@tonic-gate 				goto error1;
2170Sstevel@tonic-gate 			break;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		case SCF_TYPE_ASTRING:
2200Sstevel@tonic-gate 			vallist[numvals].pv_str = NULL;
2210Sstevel@tonic-gate 			if ((valsize = scf_value_get_astring(val, NULL, 0)) ==
2220Sstevel@tonic-gate 			    -1)
2230Sstevel@tonic-gate 				goto error1;
2240Sstevel@tonic-gate 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2250Sstevel@tonic-gate 			    NULL) {
2260Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2270Sstevel@tonic-gate 				goto error1;
2280Sstevel@tonic-gate 			}
2290Sstevel@tonic-gate 			if (scf_value_get_astring(val,
2300Sstevel@tonic-gate 			    vallist[numvals].pv_str, valsize+1) == -1) {
2310Sstevel@tonic-gate 				free(vallist[numvals].pv_str);
2320Sstevel@tonic-gate 				goto error1;
2330Sstevel@tonic-gate 			}
2340Sstevel@tonic-gate 			break;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		case SCF_TYPE_USTRING:
2370Sstevel@tonic-gate 		case SCF_TYPE_HOST:
2380Sstevel@tonic-gate 		case SCF_TYPE_HOSTNAME:
23912483SAntonello.Cruz@Sun.COM 		case SCF_TYPE_NET_ADDR:
2400Sstevel@tonic-gate 		case SCF_TYPE_NET_ADDR_V4:
2410Sstevel@tonic-gate 		case SCF_TYPE_NET_ADDR_V6:
2420Sstevel@tonic-gate 		case SCF_TYPE_URI:
2430Sstevel@tonic-gate 		case SCF_TYPE_FMRI:
2440Sstevel@tonic-gate 			vallist[numvals].pv_str = NULL;
2450Sstevel@tonic-gate 			if ((valsize = scf_value_get_ustring(val, NULL, 0)) ==
2460Sstevel@tonic-gate 			    -1)
2470Sstevel@tonic-gate 				goto error1;
2480Sstevel@tonic-gate 			if ((vallist[numvals].pv_str = malloc(valsize+1)) ==
2490Sstevel@tonic-gate 			    NULL) {
2500Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2510Sstevel@tonic-gate 				goto error1;
2520Sstevel@tonic-gate 			}
2530Sstevel@tonic-gate 			if (scf_value_get_ustring(val,
2540Sstevel@tonic-gate 			    vallist[numvals].pv_str, valsize+1) == -1) {
2550Sstevel@tonic-gate 				free(vallist[numvals].pv_str);
2560Sstevel@tonic-gate 				goto error1;
2570Sstevel@tonic-gate 			}
2580Sstevel@tonic-gate 			break;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 		case SCF_TYPE_OPAQUE:
2610Sstevel@tonic-gate 			vallist[numvals].pv_opaque.o_value = NULL;
2620Sstevel@tonic-gate 			if ((valsize = scf_value_get_opaque(val, NULL, 0)) ==
2630Sstevel@tonic-gate 			    -1)
2640Sstevel@tonic-gate 				goto error1;
2650Sstevel@tonic-gate 			if ((vallist[numvals].pv_opaque.o_value =
2660Sstevel@tonic-gate 			    malloc(valsize)) == NULL) {
2670Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
2680Sstevel@tonic-gate 				goto error1;
2690Sstevel@tonic-gate 			}
2700Sstevel@tonic-gate 			vallist[numvals].pv_opaque.o_size = valsize;
2710Sstevel@tonic-gate 			if (scf_value_get_opaque(val,
2720Sstevel@tonic-gate 			    vallist[numvals].pv_opaque.o_value,
2730Sstevel@tonic-gate 			    valsize) == -1) {
2740Sstevel@tonic-gate 				free(vallist[numvals].pv_opaque.o_value);
2750Sstevel@tonic-gate 				goto error1;
2760Sstevel@tonic-gate 			}
2770Sstevel@tonic-gate 			break;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		default:
2800Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2810Sstevel@tonic-gate 			goto error1;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (iterret == -1) {
2875040Swesolows 		int err = scf_error();
2885040Swesolows 		if (err != SCF_ERROR_CONNECTION_BROKEN &&
2895040Swesolows 		    err != SCF_ERROR_PERMISSION_DENIED)
2900Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
2910Sstevel@tonic-gate 		goto error1;
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	ret->pr_vallist = vallist;
2950Sstevel@tonic-gate 	ret->pr_numvalues = numvals;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	scf_iter_destroy(iter);
2980Sstevel@tonic-gate 	(void) scf_value_destroy(val);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	return (ret);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	/*
3030Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
3040Sstevel@tonic-gate 	 * for failures at various stages during the function.
3050Sstevel@tonic-gate 	 */
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate error1:
3080Sstevel@tonic-gate 	if (vallist == NULL)
3090Sstevel@tonic-gate 		goto error2;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	switch (ret->pr_type) {
3120Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
3130Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
3140Sstevel@tonic-gate 	case SCF_TYPE_HOST:
3150Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
31612483SAntonello.Cruz@Sun.COM 	case SCF_TYPE_NET_ADDR:
3170Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
3180Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
3190Sstevel@tonic-gate 	case SCF_TYPE_URI:
3200Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
3210Sstevel@tonic-gate 		for (i = 0; i < numvals; i++) {
3220Sstevel@tonic-gate 			free(vallist[i].pv_str);
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 		break;
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 	case SCF_TYPE_OPAQUE: {
3270Sstevel@tonic-gate 		for (i = 0; i < numvals; i++) {
3280Sstevel@tonic-gate 			free(vallist[i].pv_opaque.o_value);
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 		break;
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 	default:
3330Sstevel@tonic-gate 		break;
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	free(vallist);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate error2:
3390Sstevel@tonic-gate 	scf_iter_destroy(iter);
3400Sstevel@tonic-gate 	(void) scf_value_destroy(val);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate error3:
3430Sstevel@tonic-gate 	free(ret->pr_pgname);
3440Sstevel@tonic-gate 	free(ret->pr_propname);
3450Sstevel@tonic-gate 	free(ret);
3460Sstevel@tonic-gate 	return (NULL);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
3500Sstevel@tonic-gate  * insert_app_props iterates over a property iterator, getting all the
3510Sstevel@tonic-gate  * properties from a property group, and adding or overwriting them into
3520Sstevel@tonic-gate  * a simple_app_props_t.  This is used by scf_simple_app_props_get to provide
3530Sstevel@tonic-gate  * service/instance composition while filling the app_props_t.
3540Sstevel@tonic-gate  * insert_app_props iterates over a single property group.
3550Sstevel@tonic-gate  */
3560Sstevel@tonic-gate 
3570Sstevel@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)3580Sstevel@tonic-gate insert_app_props(scf_iter_t *propiter, char *pgname, char *propname, struct
3590Sstevel@tonic-gate     scf_simple_pg *thispg, scf_property_t *prop, size_t namelen,
3600Sstevel@tonic-gate     scf_handle_t *h)
3610Sstevel@tonic-gate {
3620Sstevel@tonic-gate 	scf_simple_prop_t	*thisprop, *prevprop, *newprop;
3630Sstevel@tonic-gate 	uint8_t			found;
3640Sstevel@tonic-gate 	int			propiter_ret;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	while ((propiter_ret = scf_iter_next_property(propiter, prop)) == 1) {
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		if (scf_property_get_name(prop, propname, namelen) < 0) {
3690Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
3700Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
3710Sstevel@tonic-gate 			return (-1);
3720Sstevel@tonic-gate 		}
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 		thisprop = thispg->pg_proplist;
3750Sstevel@tonic-gate 		prevprop = thispg->pg_proplist;
3760Sstevel@tonic-gate 		found = 0;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		while ((thisprop != NULL) && (!found)) {
3790Sstevel@tonic-gate 			if (strcmp(thisprop->pr_propname, propname) == 0) {
3800Sstevel@tonic-gate 				found = 1;
3810Sstevel@tonic-gate 				if ((newprop = fill_prop(prop, pgname,
3820Sstevel@tonic-gate 				    propname, h)) == NULL)
3830Sstevel@tonic-gate 					return (-1);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 				if (thisprop == thispg->pg_proplist)
3860Sstevel@tonic-gate 					thispg->pg_proplist = newprop;
3870Sstevel@tonic-gate 				else
3880Sstevel@tonic-gate 					prevprop->pr_next = newprop;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 				newprop->pr_pg = thispg;
3910Sstevel@tonic-gate 				newprop->pr_next = thisprop->pr_next;
3920Sstevel@tonic-gate 				scf_simple_prop_free(thisprop);
3930Sstevel@tonic-gate 				thisprop = NULL;
3940Sstevel@tonic-gate 			} else {
3950Sstevel@tonic-gate 				if (thisprop != thispg->pg_proplist)
3960Sstevel@tonic-gate 					prevprop = prevprop->pr_next;
3970Sstevel@tonic-gate 				thisprop = thisprop->pr_next;
3980Sstevel@tonic-gate 			}
3990Sstevel@tonic-gate 		}
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		if (!found) {
4020Sstevel@tonic-gate 			if ((newprop = fill_prop(prop, pgname, propname, h)) ==
4030Sstevel@tonic-gate 			    NULL)
4040Sstevel@tonic-gate 				return (-1);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 			if (thispg->pg_proplist == NULL)
4070Sstevel@tonic-gate 				thispg->pg_proplist = newprop;
4080Sstevel@tonic-gate 			else
4090Sstevel@tonic-gate 				prevprop->pr_next = newprop;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 			newprop->pr_pg = thispg;
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if (propiter_ret == -1) {
4160Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
4170Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
4180Sstevel@tonic-gate 		return (-1);
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	return (0);
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate /*
4260Sstevel@tonic-gate  * Sets up e in tx to set pname's values.  Returns 0 on success or -1 on
4270Sstevel@tonic-gate  * failure, with scf_error() set to
4280Sstevel@tonic-gate  *   SCF_ERROR_HANDLE_MISMATCH - tx & e are derived from different handles
4290Sstevel@tonic-gate  *   SCF_ERROR_INVALID_ARGUMENT - pname or ty are invalid
4300Sstevel@tonic-gate  *   SCF_ERROR_NOT_BOUND - handle is not bound
4310Sstevel@tonic-gate  *   SCF_ERROR_CONNECTION_BROKEN - connection was broken
4320Sstevel@tonic-gate  *   SCF_ERROR_NOT_SET - tx has not been started
4330Sstevel@tonic-gate  *   SCF_ERROR_DELETED - the pg tx was started on was deleted
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate static int
transaction_property_set(scf_transaction_t * tx,scf_transaction_entry_t * e,const char * pname,scf_type_t ty)4360Sstevel@tonic-gate transaction_property_set(scf_transaction_t *tx, scf_transaction_entry_t *e,
4370Sstevel@tonic-gate     const char *pname, scf_type_t ty)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	for (;;) {
4400Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, e, pname, ty) == 0)
4410Sstevel@tonic-gate 			return (0);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 		switch (scf_error()) {
4440Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
4450Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4460Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
4470Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4480Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
4490Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
4500Sstevel@tonic-gate 		default:
4510Sstevel@tonic-gate 			return (-1);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
4540Sstevel@tonic-gate 			break;
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, e, pname, ty) == 0)
4580Sstevel@tonic-gate 			return (0);
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 		switch (scf_error()) {
4610Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
4620Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4630Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
4640Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4650Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
4660Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
4670Sstevel@tonic-gate 		default:
4680Sstevel@tonic-gate 			return (-1);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
4710Sstevel@tonic-gate 			break;
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate static int
get_inst_enabled(const scf_instance_t * inst,const char * pgname)4770Sstevel@tonic-gate get_inst_enabled(const scf_instance_t *inst, const char *pgname)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate 	scf_propertygroup_t 	*gpg = NULL;
4800Sstevel@tonic-gate 	scf_property_t 		*eprop = NULL;
4810Sstevel@tonic-gate 	scf_value_t 		*v = NULL;
4820Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
4830Sstevel@tonic-gate 	uint8_t			enabled;
4840Sstevel@tonic-gate 	int			ret = -1;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
4870Sstevel@tonic-gate 		return (-1);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
4900Sstevel@tonic-gate 	    (eprop = scf_property_create(h)) == NULL ||
4910Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL)
4920Sstevel@tonic-gate 		goto out;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) ||
4950Sstevel@tonic-gate 	    scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) ||
4960Sstevel@tonic-gate 	    scf_property_get_value(eprop, v) ||
4970Sstevel@tonic-gate 	    scf_value_get_boolean(v, &enabled))
4980Sstevel@tonic-gate 		goto out;
4990Sstevel@tonic-gate 	ret = enabled;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate out:
5020Sstevel@tonic-gate 	scf_pg_destroy(gpg);
5030Sstevel@tonic-gate 	scf_property_destroy(eprop);
5040Sstevel@tonic-gate 	scf_value_destroy(v);
5050Sstevel@tonic-gate 	return (ret);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate /*
5090Sstevel@tonic-gate  * set_inst_enabled() is a "master" enable/disable call that takes the
5100Sstevel@tonic-gate  * instance and the desired state for the enabled bit in the instance's
5110Sstevel@tonic-gate  * named property group.  If the group doesn't exist, it's created with the
5120Sstevel@tonic-gate  * given flags.  Called by smf_{dis,en}able_instance().
5130Sstevel@tonic-gate  */
5140Sstevel@tonic-gate static int
set_inst_enabled(const scf_instance_t * inst,uint8_t desired,const char * pgname,uint32_t pgflags)5150Sstevel@tonic-gate set_inst_enabled(const scf_instance_t *inst, uint8_t desired,
5160Sstevel@tonic-gate     const char *pgname, uint32_t pgflags)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	scf_transaction_t 	*tx = NULL;
5190Sstevel@tonic-gate 	scf_transaction_entry_t *ent = NULL;
5200Sstevel@tonic-gate 	scf_propertygroup_t 	*gpg = NULL;
5210Sstevel@tonic-gate 	scf_property_t 		*eprop = NULL;
5220Sstevel@tonic-gate 	scf_value_t 		*v = NULL;
5230Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
5240Sstevel@tonic-gate 	int 			ret = -1;
5250Sstevel@tonic-gate 	int			committed;
5260Sstevel@tonic-gate 	uint8_t			b;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
5290Sstevel@tonic-gate 		return (-1);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
5320Sstevel@tonic-gate 	    (eprop = scf_property_create(h)) == NULL ||
5330Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL ||
5340Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
5350Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
5360Sstevel@tonic-gate 		goto out;
5370Sstevel@tonic-gate 
5384171Stn143363 general_pg_get:
5394171Stn143363 	if (scf_instance_get_pg(inst, SCF_PG_GENERAL, gpg) == -1) {
5400Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5410Sstevel@tonic-gate 			goto out;
5420Sstevel@tonic-gate 
5434171Stn143363 		if (scf_instance_add_pg(inst, SCF_PG_GENERAL,
5444171Stn143363 		    SCF_GROUP_FRAMEWORK, SCF_PG_GENERAL_FLAGS, gpg) == -1) {
5450Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_EXISTS)
5460Sstevel@tonic-gate 				goto out;
5474171Stn143363 			goto general_pg_get;
5480Sstevel@tonic-gate 		}
5490Sstevel@tonic-gate 	}
5504171Stn143363 
5514171Stn143363 	if (strcmp(pgname, SCF_PG_GENERAL) != 0) {
5524171Stn143363 get:
5534171Stn143363 		if (scf_instance_get_pg(inst, pgname, gpg) == -1) {
5544171Stn143363 			if (scf_error() != SCF_ERROR_NOT_FOUND)
5554171Stn143363 				goto out;
5564171Stn143363 
5574171Stn143363 			if (scf_instance_add_pg(inst, pgname,
5584171Stn143363 			    SCF_GROUP_FRAMEWORK, pgflags, gpg) == -1) {
5594171Stn143363 				if (scf_error() != SCF_ERROR_EXISTS)
5604171Stn143363 					goto out;
5614171Stn143363 				goto get;
5624171Stn143363 			}
5634171Stn143363 		}
5644171Stn143363 	}
5654171Stn143363 
5660Sstevel@tonic-gate 	if (scf_pg_get_property(gpg, SCF_PROPERTY_ENABLED, eprop) == -1) {
5670Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
5680Sstevel@tonic-gate 			goto out;
5690Sstevel@tonic-gate 		else
5700Sstevel@tonic-gate 			goto set;
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	/*
5740Sstevel@tonic-gate 	 * If it's already set the way we want, forgo the transaction.
5750Sstevel@tonic-gate 	 */
5760Sstevel@tonic-gate 	if (scf_property_get_value(eprop, v) == -1) {
5770Sstevel@tonic-gate 		switch (scf_error()) {
5780Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
5790Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
5800Sstevel@tonic-gate 			/* Misconfigured, so set anyway. */
5810Sstevel@tonic-gate 			goto set;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 		default:
5840Sstevel@tonic-gate 			goto out;
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 	if (scf_value_get_boolean(v, &b) == -1) {
5880Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
5890Sstevel@tonic-gate 			goto out;
5900Sstevel@tonic-gate 		goto set;
5910Sstevel@tonic-gate 	}
5920Sstevel@tonic-gate 	if (b == desired) {
5930Sstevel@tonic-gate 		ret = 0;
5940Sstevel@tonic-gate 		goto out;
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate set:
5980Sstevel@tonic-gate 	do {
5990Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) == -1)
6000Sstevel@tonic-gate 			goto out;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 		if (transaction_property_set(tx, ent, SCF_PROPERTY_ENABLED,
6030Sstevel@tonic-gate 		    SCF_TYPE_BOOLEAN) != 0) {
6040Sstevel@tonic-gate 			switch (scf_error()) {
6050Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
6060Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
6070Sstevel@tonic-gate 			default:
6080Sstevel@tonic-gate 				goto out;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
6110Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
6120Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
6130Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
6140Sstevel@tonic-gate 				bad_error("transaction_property_set",
6150Sstevel@tonic-gate 				    scf_error());
6160Sstevel@tonic-gate 			}
6170Sstevel@tonic-gate 		}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		scf_value_set_boolean(v, desired);
6200Sstevel@tonic-gate 		if (scf_entry_add_value(ent, v) == -1)
6210Sstevel@tonic-gate 			goto out;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		committed = scf_transaction_commit(tx);
6240Sstevel@tonic-gate 		if (committed == -1)
6250Sstevel@tonic-gate 			goto out;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 		scf_transaction_reset(tx);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 		if (committed == 0) { /* out-of-sync */
6300Sstevel@tonic-gate 			if (scf_pg_update(gpg) == -1)
6310Sstevel@tonic-gate 				goto out;
6320Sstevel@tonic-gate 		}
6330Sstevel@tonic-gate 	} while (committed == 0);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	ret = 0;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate out:
6380Sstevel@tonic-gate 	scf_value_destroy(v);
6390Sstevel@tonic-gate 	scf_entry_destroy(ent);
6400Sstevel@tonic-gate 	scf_transaction_destroy(tx);
6410Sstevel@tonic-gate 	scf_property_destroy(eprop);
6420Sstevel@tonic-gate 	scf_pg_destroy(gpg);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	return (ret);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate static int
delete_inst_enabled(const scf_instance_t * inst,const char * pgname)6480Sstevel@tonic-gate delete_inst_enabled(const scf_instance_t *inst, const char *pgname)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate 	scf_transaction_t 	*tx = NULL;
6510Sstevel@tonic-gate 	scf_transaction_entry_t *ent = NULL;
6520Sstevel@tonic-gate 	scf_propertygroup_t 	*gpg = NULL;
6530Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
6540Sstevel@tonic-gate 	int			ret = -1;
6550Sstevel@tonic-gate 	int			committed;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL)
6580Sstevel@tonic-gate 		return (-1);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	if ((gpg = scf_pg_create(h)) == NULL ||
6610Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
6620Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
6630Sstevel@tonic-gate 		goto out;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, gpg) != 0)
6660Sstevel@tonic-gate 		goto error;
6670Sstevel@tonic-gate 	do {
6680Sstevel@tonic-gate 		if (scf_transaction_start(tx, gpg) == -1 ||
6690Sstevel@tonic-gate 		    scf_transaction_property_delete(tx, ent,
6700Sstevel@tonic-gate 		    SCF_PROPERTY_ENABLED) == -1 ||
6710Sstevel@tonic-gate 		    (committed = scf_transaction_commit(tx)) == -1)
6720Sstevel@tonic-gate 			goto error;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 		scf_transaction_reset(tx);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 		if (committed == 0 && scf_pg_update(gpg) == -1)
6770Sstevel@tonic-gate 			goto error;
6780Sstevel@tonic-gate 	} while (committed == 0);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	ret = 0;
6810Sstevel@tonic-gate 	goto out;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate error:
6840Sstevel@tonic-gate 	switch (scf_error()) {
6850Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
6860Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
6870Sstevel@tonic-gate 		/* success */
6880Sstevel@tonic-gate 		ret = 0;
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate out:
6920Sstevel@tonic-gate 	scf_entry_destroy(ent);
6930Sstevel@tonic-gate 	scf_transaction_destroy(tx);
6940Sstevel@tonic-gate 	scf_pg_destroy(gpg);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	return (ret);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate /*
7000Sstevel@tonic-gate  * Returns 0 on success or -1 on failure.  On failure leaves scf_error() set to
7010Sstevel@tonic-gate  *   SCF_ERROR_HANDLE_DESTROYED - inst's handle has been destroyed
7020Sstevel@tonic-gate  *   SCF_ERROR_NOT_BOUND - inst's handle is not bound
7030Sstevel@tonic-gate  *   SCF_ERROR_CONNECTION_BROKEN - the repository connection was broken
7040Sstevel@tonic-gate  *   SCF_ERROR_NOT_SET - inst is not set
7050Sstevel@tonic-gate  *   SCF_ERROR_DELETED - inst was deleted
7060Sstevel@tonic-gate  *   SCF_ERROR_PERMISSION_DENIED
7070Sstevel@tonic-gate  *   SCF_ERROR_BACKEND_ACCESS
7080Sstevel@tonic-gate  *   SCF_ERROR_BACKEND_READONLY
7090Sstevel@tonic-gate  */
7100Sstevel@tonic-gate static int
set_inst_action_inst(scf_instance_t * inst,const char * action)7110Sstevel@tonic-gate set_inst_action_inst(scf_instance_t *inst, const char *action)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate 	scf_handle_t			*h;
7140Sstevel@tonic-gate 	scf_transaction_t		*tx = NULL;
7150Sstevel@tonic-gate 	scf_transaction_entry_t		*ent = NULL;
7160Sstevel@tonic-gate 	scf_propertygroup_t		*pg = NULL;
7170Sstevel@tonic-gate 	scf_property_t			*prop = NULL;
7180Sstevel@tonic-gate 	scf_value_t			*v = NULL;
7190Sstevel@tonic-gate 	int				trans, ret = -1;
7200Sstevel@tonic-gate 	int64_t				t;
7210Sstevel@tonic-gate 	hrtime_t			timestamp;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	if ((h = scf_instance_handle(inst)) == NULL ||
7240Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
7250Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
7260Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL ||
7270Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
7280Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
7290Sstevel@tonic-gate 		goto out;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate get:
7320Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER_ACTIONS, pg) == -1) {
7330Sstevel@tonic-gate 		switch (scf_error()) {
7340Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
7350Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7360Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7370Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7380Sstevel@tonic-gate 		default:
7390Sstevel@tonic-gate 			goto out;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7420Sstevel@tonic-gate 			break;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7450Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7460Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
7470Sstevel@tonic-gate 		}
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 		/* Try creating the restarter_actions property group. */
7500Sstevel@tonic-gate add:
7510Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
7520Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_TYPE,
7530Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) {
7540Sstevel@tonic-gate 			switch (scf_error()) {
7550Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
7560Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7570Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
7580Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
7590Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
7600Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
7610Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
7620Sstevel@tonic-gate 			default:
7630Sstevel@tonic-gate 				goto out;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 			case SCF_ERROR_EXISTS:
7660Sstevel@tonic-gate 				goto get;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
7690Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
7700Sstevel@tonic-gate 				bad_error("scf_instance_add_pg", scf_error());
7710Sstevel@tonic-gate 			}
7720Sstevel@tonic-gate 		}
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	for (;;) {
7760Sstevel@tonic-gate 		timestamp = gethrtime();
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 		if (scf_pg_get_property(pg, action, prop) != 0) {
7790Sstevel@tonic-gate 			switch (scf_error()) {
7800Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7810Sstevel@tonic-gate 			default:
7820Sstevel@tonic-gate 				goto out;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
7850Sstevel@tonic-gate 				goto add;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
7880Sstevel@tonic-gate 				break;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
7910Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
7920Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
7930Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
7940Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
7950Sstevel@tonic-gate 			}
7960Sstevel@tonic-gate 		} else if (scf_property_get_value(prop, v) != 0) {
7970Sstevel@tonic-gate 			switch (scf_error()) {
7980Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
7990Sstevel@tonic-gate 			default:
8000Sstevel@tonic-gate 				goto out;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8030Sstevel@tonic-gate 				goto add;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
8060Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
8070Sstevel@tonic-gate 				break;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8100Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8110Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8120Sstevel@tonic-gate 				bad_error("scf_property_get_value",
8130Sstevel@tonic-gate 				    scf_error());
8140Sstevel@tonic-gate 			}
8150Sstevel@tonic-gate 		} else if (scf_value_get_integer(v, &t) != 0) {
8160Sstevel@tonic-gate 			bad_error("scf_value_get_integer", scf_error());
8170Sstevel@tonic-gate 		} else if (t > timestamp) {
8180Sstevel@tonic-gate 			break;
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) == -1) {
8220Sstevel@tonic-gate 			switch (scf_error()) {
8230Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8240Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8250Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8260Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8270Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8280Sstevel@tonic-gate 			default:
8290Sstevel@tonic-gate 				goto out;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8320Sstevel@tonic-gate 				goto add;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8350Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8360Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
8370Sstevel@tonic-gate 				bad_error("scf_transaction_start", scf_error());
8380Sstevel@tonic-gate 			}
8390Sstevel@tonic-gate 		}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 		if (transaction_property_set(tx, ent, action,
8420Sstevel@tonic-gate 		    SCF_TYPE_INTEGER) != 0) {
8430Sstevel@tonic-gate 			switch (scf_error()) {
8440Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8450Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8460Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8470Sstevel@tonic-gate 			default:
8480Sstevel@tonic-gate 				goto out;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8510Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
8520Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8530Sstevel@tonic-gate 				bad_error("transaction_property_set",
8540Sstevel@tonic-gate 				    scf_error());
8550Sstevel@tonic-gate 			}
8560Sstevel@tonic-gate 		}
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		scf_value_set_integer(v, timestamp);
8590Sstevel@tonic-gate 		if (scf_entry_add_value(ent, v) == -1)
8600Sstevel@tonic-gate 			bad_error("scf_entry_add_value", scf_error());
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		trans = scf_transaction_commit(tx);
8630Sstevel@tonic-gate 		if (trans == 1)
8640Sstevel@tonic-gate 			break;
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		if (trans != 0) {
8670Sstevel@tonic-gate 			switch (scf_error()) {
8680Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8690Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8700Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8710Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8720Sstevel@tonic-gate 			default:
8730Sstevel@tonic-gate 				goto out;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8760Sstevel@tonic-gate 				scf_transaction_reset(tx);
8770Sstevel@tonic-gate 				goto add;
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
8800Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8810Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8820Sstevel@tonic-gate 				bad_error("scf_transaction_commit",
8830Sstevel@tonic-gate 				    scf_error());
8840Sstevel@tonic-gate 			}
8850Sstevel@tonic-gate 		}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 		scf_transaction_reset(tx);
8886575Stn143363 		if (scf_pg_update(pg) == -1) {
8890Sstevel@tonic-gate 			switch (scf_error()) {
8900Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8910Sstevel@tonic-gate 			default:
8920Sstevel@tonic-gate 				goto out;
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
8950Sstevel@tonic-gate 				goto add;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8980Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
8990Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
9000Sstevel@tonic-gate 			}
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	ret = 0;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate out:
9070Sstevel@tonic-gate 	scf_value_destroy(v);
9080Sstevel@tonic-gate 	scf_entry_destroy(ent);
9090Sstevel@tonic-gate 	scf_transaction_destroy(tx);
9100Sstevel@tonic-gate 	scf_property_destroy(prop);
9110Sstevel@tonic-gate 	scf_pg_destroy(pg);
9120Sstevel@tonic-gate 	return (ret);
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate static int
set_inst_action(const char * fmri,const char * action)9160Sstevel@tonic-gate set_inst_action(const char *fmri, const char *action)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate 	scf_handle_t *h;
9190Sstevel@tonic-gate 	scf_instance_t *inst;
9200Sstevel@tonic-gate 	int ret = -1;
9210Sstevel@tonic-gate 
922*12967Sgavin.maltby@oracle.com 	h = _scf_handle_create_and_bind(SCF_VERSION);
9230Sstevel@tonic-gate 	if (h == NULL)
9240Sstevel@tonic-gate 		return (-1);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	inst = scf_instance_create(h);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	if (inst != NULL) {
9290Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
93010057Smills@cc.umanitoba.ca 		    NULL, SCF_DECODE_FMRI_EXACT) == 0) {
9310Sstevel@tonic-gate 			ret = set_inst_action_inst(inst, action);
93210057Smills@cc.umanitoba.ca 			if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
93310057Smills@cc.umanitoba.ca 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
93410057Smills@cc.umanitoba.ca 		} else {
93510057Smills@cc.umanitoba.ca 			switch (scf_error()) {
93610057Smills@cc.umanitoba.ca 			case SCF_ERROR_CONSTRAINT_VIOLATED:
93710057Smills@cc.umanitoba.ca 				(void) scf_set_error(
93810057Smills@cc.umanitoba.ca 				    SCF_ERROR_INVALID_ARGUMENT);
93910057Smills@cc.umanitoba.ca 				break;
94010057Smills@cc.umanitoba.ca 			case SCF_ERROR_DELETED:
94110057Smills@cc.umanitoba.ca 				(void) scf_set_error(SCF_ERROR_NOT_FOUND);
94210057Smills@cc.umanitoba.ca 				break;
94310057Smills@cc.umanitoba.ca 			}
94410057Smills@cc.umanitoba.ca 		}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		scf_instance_destroy(inst);
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	scf_handle_destroy(h);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	return (ret);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate /*
9560Sstevel@tonic-gate  * get_inst_state() gets the state string from an instance, and returns
9570Sstevel@tonic-gate  * the SCF_STATE_* constant that coincides with the instance's current state.
9580Sstevel@tonic-gate  */
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate static int
get_inst_state(scf_instance_t * inst,scf_handle_t * h)9610Sstevel@tonic-gate get_inst_state(scf_instance_t *inst, scf_handle_t *h)
9620Sstevel@tonic-gate {
9630Sstevel@tonic-gate 	scf_propertygroup_t	*pg = NULL;
9640Sstevel@tonic-gate 	scf_property_t		*prop = NULL;
9650Sstevel@tonic-gate 	scf_value_t		*val = NULL;
9660Sstevel@tonic-gate 	char			state[MAX_SCF_STATE_STRING_SZ];
9670Sstevel@tonic-gate 	int			ret = -1;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	if (((pg = scf_pg_create(h)) == NULL) ||
9700Sstevel@tonic-gate 	    ((prop = scf_property_create(h)) == NULL) ||
9710Sstevel@tonic-gate 	    ((val = scf_value_create(h)) == NULL))
9720Sstevel@tonic-gate 		goto out;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	/* Pull the state property from the instance */
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1 ||
9770Sstevel@tonic-gate 	    scf_pg_get_property(pg, SCF_PROPERTY_STATE, prop) == -1 ||
9780Sstevel@tonic-gate 	    scf_property_get_value(prop, val) == -1) {
9790Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
9800Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
9810Sstevel@tonic-gate 		goto out;
9820Sstevel@tonic-gate 	}
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	if (scf_value_get_astring(val, state, sizeof (state)) <= 0) {
9850Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INTERNAL);
9860Sstevel@tonic-gate 		goto out;
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_UNINIT) == 0) {
9900Sstevel@tonic-gate 		ret = SCF_STATE_UNINIT;
9910Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
9920Sstevel@tonic-gate 		ret = SCF_STATE_MAINT;
9930Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_OFFLINE) == 0) {
9940Sstevel@tonic-gate 		ret = SCF_STATE_OFFLINE;
9950Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) {
9960Sstevel@tonic-gate 		ret = SCF_STATE_DISABLED;
9970Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0) {
9980Sstevel@tonic-gate 		ret = SCF_STATE_ONLINE;
9990Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
10000Sstevel@tonic-gate 		ret = SCF_STATE_DEGRADED;
10010Sstevel@tonic-gate 	}
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate out:
10040Sstevel@tonic-gate 	scf_pg_destroy(pg);
10050Sstevel@tonic-gate 	scf_property_destroy(prop);
10060Sstevel@tonic-gate 	(void) scf_value_destroy(val);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	return (ret);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate /*
10120Sstevel@tonic-gate  * Sets an instance to be enabled or disabled after reboot, using the
10130Sstevel@tonic-gate  * temporary (overriding) general_ovr property group to reflect the
10140Sstevel@tonic-gate  * present state, if it is different.
10150Sstevel@tonic-gate  */
10160Sstevel@tonic-gate static int
set_inst_enabled_atboot(scf_instance_t * inst,uint8_t desired)10170Sstevel@tonic-gate set_inst_enabled_atboot(scf_instance_t *inst, uint8_t desired)
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	int enabled;
10200Sstevel@tonic-gate 	int persistent;
10210Sstevel@tonic-gate 	int ret = -1;
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	if ((persistent = get_inst_enabled(inst, SCF_PG_GENERAL)) < 0) {
10240Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
10250Sstevel@tonic-gate 			goto out;
10260Sstevel@tonic-gate 		persistent = B_FALSE;
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate 	if ((enabled = get_inst_enabled(inst, SCF_PG_GENERAL_OVR)) < 0) {
10290Sstevel@tonic-gate 		enabled = persistent;
10300Sstevel@tonic-gate 		if (persistent != desired) {
10310Sstevel@tonic-gate 			/*
10320Sstevel@tonic-gate 			 * Temporarily store the present enabled state.
10330Sstevel@tonic-gate 			 */
10340Sstevel@tonic-gate 			if (set_inst_enabled(inst, persistent,
10350Sstevel@tonic-gate 			    SCF_PG_GENERAL_OVR, SCF_PG_GENERAL_OVR_FLAGS))
10360Sstevel@tonic-gate 				goto out;
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 	if (persistent != desired)
10400Sstevel@tonic-gate 		if (set_inst_enabled(inst, desired, SCF_PG_GENERAL,
10410Sstevel@tonic-gate 		    SCF_PG_GENERAL_FLAGS))
10420Sstevel@tonic-gate 			goto out;
10430Sstevel@tonic-gate 	if (enabled == desired)
10440Sstevel@tonic-gate 		ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
10450Sstevel@tonic-gate 	else
10460Sstevel@tonic-gate 		ret = 0;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate out:
10490Sstevel@tonic-gate 	return (ret);
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate static int
set_inst_enabled_flags(const char * fmri,int flags,uint8_t desired)10530Sstevel@tonic-gate set_inst_enabled_flags(const char *fmri, int flags, uint8_t desired)
10540Sstevel@tonic-gate {
10550Sstevel@tonic-gate 	int ret = -1;
10560Sstevel@tonic-gate 	scf_handle_t *h;
10570Sstevel@tonic-gate 	scf_instance_t *inst;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	if (flags & ~(SMF_TEMPORARY | SMF_AT_NEXT_BOOT) ||
10600Sstevel@tonic-gate 	    flags & SMF_TEMPORARY && flags & SMF_AT_NEXT_BOOT) {
10610Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
10620Sstevel@tonic-gate 		return (ret);
10630Sstevel@tonic-gate 	}
10640Sstevel@tonic-gate 
1065*12967Sgavin.maltby@oracle.com 	if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
10660Sstevel@tonic-gate 		return (ret);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL) {
10690Sstevel@tonic-gate 		scf_handle_destroy(h);
10700Sstevel@tonic-gate 		return (ret);
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, NULL,
10740Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) == -1) {
10750Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
10760Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
10770Sstevel@tonic-gate 		goto out;
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	if (flags & SMF_AT_NEXT_BOOT) {
10810Sstevel@tonic-gate 		ret = set_inst_enabled_atboot(inst, desired);
10820Sstevel@tonic-gate 	} else {
10830Sstevel@tonic-gate 		if (set_inst_enabled(inst, desired, flags & SMF_TEMPORARY ?
10840Sstevel@tonic-gate 		    SCF_PG_GENERAL_OVR : SCF_PG_GENERAL, flags & SMF_TEMPORARY ?
10850Sstevel@tonic-gate 		    SCF_PG_GENERAL_OVR_FLAGS : SCF_PG_GENERAL_FLAGS))
10860Sstevel@tonic-gate 			goto out;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 		/*
10890Sstevel@tonic-gate 		 * Make the persistent value effective by deleting the
10900Sstevel@tonic-gate 		 * temporary one.
10910Sstevel@tonic-gate 		 */
10920Sstevel@tonic-gate 		if (flags & SMF_TEMPORARY)
10930Sstevel@tonic-gate 			ret = 0;
10940Sstevel@tonic-gate 		else
10950Sstevel@tonic-gate 			ret = delete_inst_enabled(inst, SCF_PG_GENERAL_OVR);
10960Sstevel@tonic-gate 	}
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate out:
10990Sstevel@tonic-gate 	scf_instance_destroy(inst);
11000Sstevel@tonic-gate 	scf_handle_destroy(h);
110110057Smills@cc.umanitoba.ca 	if (ret == -1 && scf_error() == SCF_ERROR_DELETED)
110210057Smills@cc.umanitoba.ca 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
11030Sstevel@tonic-gate 	return (ret);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate 
11064119Stn143363 /*
11074119Stn143363  * Create and return a pg from the instance associated with the given handle.
11084119Stn143363  * This function is only called in scf_transaction_setup and
11094119Stn143363  * scf_transaction_restart where the h->rh_instance pointer is properly filled
11104119Stn143363  * in by scf_general_setup_pg().
11114119Stn143363  */
11124119Stn143363 static scf_propertygroup_t *
get_instance_pg(scf_simple_handle_t * simple_h)11134119Stn143363 get_instance_pg(scf_simple_handle_t *simple_h)
11144119Stn143363 {
11154119Stn143363 	scf_propertygroup_t	*ret_pg = scf_pg_create(simple_h->h);
11164119Stn143363 	char			*pg_name;
11174119Stn143363 	ssize_t			namelen;
11184119Stn143363 
11194119Stn143363 	if (ret_pg == NULL) {
11204119Stn143363 		return (NULL);
11214119Stn143363 	}
11224119Stn143363 
11237887SLiane.Praza@Sun.COM 	namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
11247887SLiane.Praza@Sun.COM 	assert(namelen > 0);
11254119Stn143363 
11264119Stn143363 	if ((pg_name = malloc(namelen)) == NULL) {
11274119Stn143363 		if (scf_error() == SCF_ERROR_NOT_SET) {
11284119Stn143363 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
11294119Stn143363 		}
11304119Stn143363 		return (NULL);
11314119Stn143363 	}
11324119Stn143363 
11334119Stn143363 	if (scf_pg_get_name(simple_h->running_pg, pg_name, namelen) < 0) {
11344119Stn143363 		if (scf_error() == SCF_ERROR_NOT_SET) {
11354119Stn143363 			(void) scf_set_error(SCF_ERROR_INTERNAL);
11364119Stn143363 		}
11374119Stn143363 		return (NULL);
11384119Stn143363 	}
11394119Stn143363 
11404119Stn143363 	/* Get pg from instance */
11414119Stn143363 	if (scf_instance_get_pg(simple_h->inst, pg_name, ret_pg) == -1) {
11424119Stn143363 		return (NULL);
11434119Stn143363 	}
11444119Stn143363 
11454119Stn143363 	return (ret_pg);
11464119Stn143363 }
11474119Stn143363 
11480Sstevel@tonic-gate int
smf_enable_instance(const char * fmri,int flags)11490Sstevel@tonic-gate smf_enable_instance(const char *fmri, int flags)
11500Sstevel@tonic-gate {
11510Sstevel@tonic-gate 	return (set_inst_enabled_flags(fmri, flags, B_TRUE));
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate int
smf_disable_instance(const char * fmri,int flags)11550Sstevel@tonic-gate smf_disable_instance(const char *fmri, int flags)
11560Sstevel@tonic-gate {
11570Sstevel@tonic-gate 	return (set_inst_enabled_flags(fmri, flags, B_FALSE));
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate int
_smf_refresh_instance_i(scf_instance_t * inst)11610Sstevel@tonic-gate _smf_refresh_instance_i(scf_instance_t *inst)
11620Sstevel@tonic-gate {
11630Sstevel@tonic-gate 	return (set_inst_action_inst(inst, SCF_PROPERTY_REFRESH));
11640Sstevel@tonic-gate }
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate int
_smf_refresh_all_instances(scf_service_t * s)1167*12967Sgavin.maltby@oracle.com _smf_refresh_all_instances(scf_service_t *s)
1168*12967Sgavin.maltby@oracle.com {
1169*12967Sgavin.maltby@oracle.com 	scf_handle_t	*h = scf_service_handle(s);
1170*12967Sgavin.maltby@oracle.com 	scf_instance_t	*i = scf_instance_create(h);
1171*12967Sgavin.maltby@oracle.com 	scf_iter_t	*it = scf_iter_create(h);
1172*12967Sgavin.maltby@oracle.com 	int err, r = -1;
1173*12967Sgavin.maltby@oracle.com 
1174*12967Sgavin.maltby@oracle.com 	if (h == NULL || i == NULL || it == NULL)
1175*12967Sgavin.maltby@oracle.com 		goto error;
1176*12967Sgavin.maltby@oracle.com 
1177*12967Sgavin.maltby@oracle.com 	if (scf_iter_service_instances(it, s) != 0)
1178*12967Sgavin.maltby@oracle.com 		goto error;
1179*12967Sgavin.maltby@oracle.com 
1180*12967Sgavin.maltby@oracle.com 	while ((err = scf_iter_next_instance(it, i)) == 1)
1181*12967Sgavin.maltby@oracle.com 		if (_smf_refresh_instance_i(i) != 0)
1182*12967Sgavin.maltby@oracle.com 			goto error;
1183*12967Sgavin.maltby@oracle.com 
1184*12967Sgavin.maltby@oracle.com 	if (err == -1)
1185*12967Sgavin.maltby@oracle.com 		goto error;
1186*12967Sgavin.maltby@oracle.com 
1187*12967Sgavin.maltby@oracle.com 	r = 0;
1188*12967Sgavin.maltby@oracle.com error:
1189*12967Sgavin.maltby@oracle.com 	scf_instance_destroy(i);
1190*12967Sgavin.maltby@oracle.com 	scf_iter_destroy(it);
1191*12967Sgavin.maltby@oracle.com 
1192*12967Sgavin.maltby@oracle.com 	return (r);
1193*12967Sgavin.maltby@oracle.com }
1194*12967Sgavin.maltby@oracle.com 
1195*12967Sgavin.maltby@oracle.com int
smf_refresh_instance(const char * instance)11960Sstevel@tonic-gate smf_refresh_instance(const char *instance)
11970Sstevel@tonic-gate {
11980Sstevel@tonic-gate 	return (set_inst_action(instance, SCF_PROPERTY_REFRESH));
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate int
smf_restart_instance(const char * instance)12020Sstevel@tonic-gate smf_restart_instance(const char *instance)
12030Sstevel@tonic-gate {
12040Sstevel@tonic-gate 	return (set_inst_action(instance, SCF_PROPERTY_RESTART));
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate int
smf_maintain_instance(const char * instance,int flags)12080Sstevel@tonic-gate smf_maintain_instance(const char *instance, int flags)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	if (flags & SMF_TEMPORARY)
12110Sstevel@tonic-gate 		return (set_inst_action(instance,
12120Sstevel@tonic-gate 		    (flags & SMF_IMMEDIATE) ?
12130Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_IMMTEMP :
12140Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_TEMPORARY));
12150Sstevel@tonic-gate 	else
12160Sstevel@tonic-gate 		return (set_inst_action(instance,
12170Sstevel@tonic-gate 		    (flags & SMF_IMMEDIATE) ?
12180Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_IMMEDIATE :
12190Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON));
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate int
smf_degrade_instance(const char * instance,int flags)12230Sstevel@tonic-gate smf_degrade_instance(const char *instance, int flags)
12240Sstevel@tonic-gate {
12250Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
12260Sstevel@tonic-gate 	const char			*state_str;
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	if (flags & SMF_TEMPORARY)
12290Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_INVALID_ARGUMENT));
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
12320Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
12330Sstevel@tonic-gate 		return (SCF_FAILED);
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
12360Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12370Sstevel@tonic-gate 		return (SCF_FAILED);
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	if (strcmp(state_str, SCF_STATE_STRING_ONLINE) != 0) {
12410Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12420Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED));
12430Sstevel@tonic-gate 	}
12440Sstevel@tonic-gate 	scf_simple_prop_free(prop);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	return (set_inst_action(instance, (flags & SMF_IMMEDIATE) ?
12470Sstevel@tonic-gate 	    SCF_PROPERTY_DEGRADE_IMMEDIATE : SCF_PROPERTY_DEGRADED));
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate int
smf_restore_instance(const char * instance)12510Sstevel@tonic-gate smf_restore_instance(const char *instance)
12520Sstevel@tonic-gate {
12530Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
12540Sstevel@tonic-gate 	const char			*state_str;
12550Sstevel@tonic-gate 	int				ret;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
12580Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
12590Sstevel@tonic-gate 		return (SCF_FAILED);
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
12620Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12630Sstevel@tonic-gate 		return (SCF_FAILED);
12640Sstevel@tonic-gate 	}
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	if (strcmp(state_str, SCF_STATE_STRING_MAINT) == 0) {
12670Sstevel@tonic-gate 		ret = set_inst_action(instance, SCF_PROPERTY_MAINT_OFF);
12680Sstevel@tonic-gate 	} else if (strcmp(state_str, SCF_STATE_STRING_DEGRADED) == 0) {
12690Sstevel@tonic-gate 		ret = set_inst_action(instance, SCF_PROPERTY_RESTORE);
12700Sstevel@tonic-gate 	} else {
12710Sstevel@tonic-gate 		ret = scf_set_error(SCF_ERROR_CONSTRAINT_VIOLATED);
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	scf_simple_prop_free(prop);
12750Sstevel@tonic-gate 	return (ret);
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate char *
smf_get_state(const char * instance)12790Sstevel@tonic-gate smf_get_state(const char *instance)
12800Sstevel@tonic-gate {
12810Sstevel@tonic-gate 	scf_simple_prop_t		*prop;
12820Sstevel@tonic-gate 	const char			*state_str;
12830Sstevel@tonic-gate 	char				*ret;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	if ((prop = scf_simple_prop_get(NULL, instance, SCF_PG_RESTARTER,
12860Sstevel@tonic-gate 	    SCF_PROPERTY_STATE)) == NULL)
12870Sstevel@tonic-gate 		return (NULL);
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	if ((state_str = scf_simple_prop_next_astring(prop)) == NULL) {
12900Sstevel@tonic-gate 		scf_simple_prop_free(prop);
12910Sstevel@tonic-gate 		return (NULL);
12920Sstevel@tonic-gate 	}
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	if ((ret = strdup(state_str)) == NULL)
12950Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	scf_simple_prop_free(prop);
12980Sstevel@tonic-gate 	return (ret);
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate 
13014119Stn143363 /*
13024119Stn143363  * scf_general_pg_setup(fmri, pg_name)
13034119Stn143363  * Create a scf_simple_handle_t and fill in the instance, snapshot, and
13044119Stn143363  * property group fields associated with the given fmri and property group
13054119Stn143363  * name.
13064119Stn143363  * Returns:
13074119Stn143363  *      Handle  on success
13084119Stn143363  *      Null  on error with scf_error set to:
13094119Stn143363  *              SCF_ERROR_HANDLE_MISMATCH,
13104119Stn143363  *              SCF_ERROR_INVALID_ARGUMENT,
13114119Stn143363  *              SCF_ERROR_CONSTRAINT_VIOLATED,
13124119Stn143363  *              SCF_ERROR_NOT_FOUND,
13134119Stn143363  *              SCF_ERROR_NOT_SET,
13144119Stn143363  *              SCF_ERROR_DELETED,
13154119Stn143363  *              SCF_ERROR_NOT_BOUND,
13164119Stn143363  *              SCF_ERROR_CONNECTION_BROKEN,
13174119Stn143363  *              SCF_ERROR_INTERNAL,
13184119Stn143363  *              SCF_ERROR_NO_RESOURCES,
13194119Stn143363  *              SCF_ERROR_BACKEND_ACCESS
13204119Stn143363  */
13214119Stn143363 scf_simple_handle_t *
scf_general_pg_setup(const char * fmri,const char * pg_name)13224119Stn143363 scf_general_pg_setup(const char *fmri, const char *pg_name)
13234119Stn143363 {
13244119Stn143363 	scf_simple_handle_t	*ret;
13254119Stn143363 
13264119Stn143363 	ret = uu_zalloc(sizeof (*ret));
13274119Stn143363 	if (ret == NULL) {
13284119Stn143363 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
13294119Stn143363 		return (NULL);
13304119Stn143363 	} else {
13314119Stn143363 
1332*12967Sgavin.maltby@oracle.com 		ret->h = _scf_handle_create_and_bind(SCF_VERSION);
13334119Stn143363 		ret->inst = scf_instance_create(ret->h);
13344119Stn143363 		ret->snap = scf_snapshot_create(ret->h);
13354119Stn143363 		ret->running_pg = scf_pg_create(ret->h);
13364119Stn143363 	}
13374119Stn143363 
13384119Stn143363 	if ((ret->h == NULL) || (ret->inst == NULL) ||
13394119Stn143363 	    (ret->snap == NULL) || (ret->running_pg == NULL)) {
13404119Stn143363 		goto out;
13414119Stn143363 	}
13424119Stn143363 
13434119Stn143363 	if (scf_handle_decode_fmri(ret->h, fmri, NULL, NULL, ret->inst,
13444119Stn143363 	    NULL, NULL, NULL) == -1) {
13454119Stn143363 		goto out;
13464119Stn143363 	}
13474119Stn143363 
13484119Stn143363 	if ((scf_instance_get_snapshot(ret->inst, "running", ret->snap))
13494119Stn143363 	    != 0) {
13504119Stn143363 		goto out;
13514119Stn143363 	}
13524119Stn143363 
13534119Stn143363 	if (scf_instance_get_pg_composed(ret->inst, ret->snap, pg_name,
13544119Stn143363 	    ret->running_pg) != 0) {
13554119Stn143363 		goto out;
13564119Stn143363 	}
13574119Stn143363 
13584119Stn143363 	return (ret);
13594119Stn143363 
13604119Stn143363 out:
13614119Stn143363 	scf_simple_handle_destroy(ret);
13624119Stn143363 	return (NULL);
13634119Stn143363 }
13644119Stn143363 
13654119Stn143363 /*
13664119Stn143363  * scf_transaction_setup(h)
13674119Stn143363  * creates and starts the transaction
13684119Stn143363  * Returns:
13694119Stn143363  *      transaction  on success
13704119Stn143363  *      NULL on failure with scf_error set to:
13714119Stn143363  *      SCF_ERROR_NO_MEMORY,
13724119Stn143363  *	SCF_ERROR_INVALID_ARGUMENT,
13734119Stn143363  *      SCF_ERROR_HANDLE_DESTROYED,
13744119Stn143363  *	SCF_ERROR_INTERNAL,
13754119Stn143363  *	SCF_ERROR_NO_RESOURCES,
13764119Stn143363  *      SCF_ERROR_NOT_BOUND,
13774119Stn143363  *	SCF_ERROR_CONNECTION_BROKEN,
13784119Stn143363  *      SCF_ERROR_NOT_SET,
13794119Stn143363  *	SCF_ERROR_DELETED,
13804119Stn143363  *	SCF_ERROR_CONSTRAINT_VIOLATED,
13814119Stn143363  *      SCF_ERROR_HANDLE_MISMATCH,
13824119Stn143363  *	SCF_ERROR_BACKEND_ACCESS,
13834119Stn143363  *	SCF_ERROR_IN_USE
13844119Stn143363  */
13854119Stn143363 scf_transaction_t *
scf_transaction_setup(scf_simple_handle_t * simple_h)13864119Stn143363 scf_transaction_setup(scf_simple_handle_t *simple_h)
13874119Stn143363 {
13884119Stn143363 	scf_transaction_t	*tx = NULL;
13894119Stn143363 
13904119Stn143363 	if ((tx = scf_transaction_create(simple_h->h)) == NULL) {
13914119Stn143363 		return (NULL);
13924119Stn143363 	}
13934119Stn143363 
13944119Stn143363 	if ((simple_h->editing_pg = get_instance_pg(simple_h)) == NULL) {
13954119Stn143363 		return (NULL);
13964119Stn143363 	}
13974119Stn143363 
13984119Stn143363 	if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
13994119Stn143363 		scf_pg_destroy(simple_h->editing_pg);
14004119Stn143363 		simple_h->editing_pg = NULL;
14014119Stn143363 		return (NULL);
14024119Stn143363 	}
14034119Stn143363 
14044119Stn143363 	return (tx);
14054119Stn143363 }
14064119Stn143363 
14074119Stn143363 int
scf_transaction_restart(scf_simple_handle_t * simple_h,scf_transaction_t * tx)14084119Stn143363 scf_transaction_restart(scf_simple_handle_t *simple_h, scf_transaction_t *tx)
14094119Stn143363 {
14104119Stn143363 	scf_transaction_reset(tx);
14114119Stn143363 
14124119Stn143363 	if (scf_pg_update(simple_h->editing_pg) == -1) {
14134119Stn143363 		return (SCF_FAILED);
14144119Stn143363 	}
14154119Stn143363 
14164119Stn143363 	if (scf_transaction_start(tx, simple_h->editing_pg) == -1) {
14174119Stn143363 		return (SCF_FAILED);
14184119Stn143363 	}
14194119Stn143363 
14204119Stn143363 	return (SCF_SUCCESS);
14214119Stn143363 }
14224119Stn143363 
14234119Stn143363 /*
14244119Stn143363  * scf_read_count_property(scf_simple_handle_t *simple_h, char *prop_name,
14254119Stn143363  * uint64_t *ret_count)
14264119Stn143363  *
14274119Stn143363  * For the given property name, return the count value.
14284119Stn143363  * RETURNS:
14294119Stn143363  *	SCF_SUCCESS
14304119Stn143363  *	SCF_FAILED on failure with scf_error() set to:
14314119Stn143363  *		SCF_ERROR_HANDLE_DESTROYED
14324119Stn143363  *		SCF_ERROR_INTERNAL
14334119Stn143363  *		SCF_ERROR_NO_RESOURCES
14344119Stn143363  *		SCF_ERROR_NO_MEMORY
14354119Stn143363  *		SCF_ERROR_HANDLE_MISMATCH
14364119Stn143363  *		SCF_ERROR_INVALID_ARGUMENT
14374119Stn143363  *		SCF_ERROR_NOT_BOUND
14384119Stn143363  *		SCF_ERROR_CONNECTION_BROKEN
14394119Stn143363  *		SCF_ERROR_NOT_SET
14404119Stn143363  *		SCF_ERROR_DELETED
14414119Stn143363  *		SCF_ERROR_BACKEND_ACCESS
14424119Stn143363  *		SCF_ERROR_CONSTRAINT_VIOLATED
14434119Stn143363  *		SCF_ERROR_TYPE_MISMATCH
14444119Stn143363  */
14454119Stn143363 int
scf_read_count_property(scf_simple_handle_t * simple_h,char * prop_name,uint64_t * ret_count)14464119Stn143363 scf_read_count_property(
14474119Stn143363 	scf_simple_handle_t	*simple_h,
14484119Stn143363 	char			*prop_name,
14494119Stn143363 	uint64_t		*ret_count)
14504119Stn143363 {
14514119Stn143363 	scf_property_t		*prop = scf_property_create(simple_h->h);
14524119Stn143363 	scf_value_t		*val = scf_value_create(simple_h->h);
14534119Stn143363 	int			ret = SCF_FAILED;
14544119Stn143363 
14554119Stn143363 	if ((val == NULL) || (prop == NULL)) {
14564119Stn143363 		goto out;
14574119Stn143363 	}
14584119Stn143363 
14594119Stn143363 	/*
14604119Stn143363 	 * Get the property struct that goes with this property group and
14614119Stn143363 	 * property name.
14624119Stn143363 	 */
14634119Stn143363 	if (scf_pg_get_property(simple_h->running_pg, prop_name, prop) != 0) {
14644119Stn143363 		goto out;
14654119Stn143363 	}
14664119Stn143363 
14674119Stn143363 	/* Get the value structure */
14684119Stn143363 	if (scf_property_get_value(prop, val) == -1) {
14694119Stn143363 		goto out;
14704119Stn143363 	}
14714119Stn143363 
14724119Stn143363 	/*
14734119Stn143363 	 * Now get the count value.
14744119Stn143363 	 */
14754119Stn143363 	if (scf_value_get_count(val, ret_count) == -1) {
14764119Stn143363 		goto out;
14774119Stn143363 	}
14784119Stn143363 
14794119Stn143363 	ret = SCF_SUCCESS;
14804119Stn143363 
14814119Stn143363 out:
14824119Stn143363 	scf_property_destroy(prop);
14834119Stn143363 	scf_value_destroy(val);
14844119Stn143363 	return (ret);
14854119Stn143363 }
14864119Stn143363 
14874119Stn143363 /*
14884119Stn143363  * scf_trans_add_count_property(trans, propname, count, create_flag)
14894119Stn143363  *
14904119Stn143363  * Set a count property transaction entry into the pending SMF transaction.
14914119Stn143363  * The transaction is created and committed outside of this function.
14924119Stn143363  * Returns:
14934119Stn143363  *	SCF_SUCCESS
14944119Stn143363  *	SCF_FAILED on failure with scf_error() set to:
14954119Stn143363  *			SCF_ERROR_HANDLE_DESTROYED,
14964119Stn143363  *			SCF_ERROR_INVALID_ARGUMENT,
14974119Stn143363  *			SCF_ERROR_NO_MEMORY,
14984119Stn143363  *			SCF_ERROR_HANDLE_MISMATCH,
14994119Stn143363  *			SCF_ERROR_NOT_SET,
15004119Stn143363  *			SCF_ERROR_IN_USE,
15014119Stn143363  *			SCF_ERROR_NOT_FOUND,
15024119Stn143363  *			SCF_ERROR_EXISTS,
15034119Stn143363  *			SCF_ERROR_TYPE_MISMATCH,
15044119Stn143363  *			SCF_ERROR_NOT_BOUND,
15054119Stn143363  *			SCF_ERROR_CONNECTION_BROKEN,
15064119Stn143363  *			SCF_ERROR_INTERNAL,
15074119Stn143363  *			SCF_ERROR_DELETED,
15084119Stn143363  *			SCF_ERROR_NO_RESOURCES,
15094119Stn143363  *			SCF_ERROR_BACKEND_ACCESS
15104119Stn143363  */
15114119Stn143363 int
scf_set_count_property(scf_transaction_t * trans,char * propname,uint64_t count,boolean_t create_flag)15124119Stn143363 scf_set_count_property(
15134119Stn143363 	scf_transaction_t	*trans,
15144119Stn143363 	char			*propname,
15154119Stn143363 	uint64_t		count,
15164119Stn143363 	boolean_t		create_flag)
15174119Stn143363 {
15184119Stn143363 	scf_handle_t		*handle = scf_transaction_handle(trans);
15194119Stn143363 	scf_value_t		*value = scf_value_create(handle);
15204119Stn143363 	scf_transaction_entry_t	*entry = scf_entry_create(handle);
15214119Stn143363 
15224119Stn143363 	if ((value == NULL) || (entry == NULL)) {
15234119Stn143363 		return (SCF_FAILED);
15244119Stn143363 	}
15254119Stn143363 
15264119Stn143363 	/*
15274119Stn143363 	 * Property must be set in transaction and won't take
15284119Stn143363 	 * effect until the transaction is committed.
15294119Stn143363 	 *
15304119Stn143363 	 * Attempt to change the current value. However, create new property
15314119Stn143363 	 * if it doesn't exist and the create flag is set.
15324119Stn143363 	 */
15334119Stn143363 	if (scf_transaction_property_change(trans, entry, propname,
15344119Stn143363 	    SCF_TYPE_COUNT) == 0) {
15354119Stn143363 		scf_value_set_count(value, count);
15364119Stn143363 		if (scf_entry_add_value(entry, value) == 0) {
15374119Stn143363 			return (SCF_SUCCESS);
15384119Stn143363 		}
15394119Stn143363 	} else {
15404119Stn143363 		if ((create_flag == B_TRUE) &&
15414119Stn143363 		    (scf_error() == SCF_ERROR_NOT_FOUND)) {
15424119Stn143363 			if (scf_transaction_property_new(trans, entry, propname,
15434119Stn143363 			    SCF_TYPE_COUNT) == 0) {
15444119Stn143363 				scf_value_set_count(value, count);
15454119Stn143363 				if (scf_entry_add_value(entry, value) == 0) {
15464119Stn143363 					return (SCF_SUCCESS);
15474119Stn143363 				}
15484119Stn143363 			}
15494119Stn143363 		}
15504119Stn143363 	}
15514119Stn143363 
15524119Stn143363 	/*
15534119Stn143363 	 * cleanup if there were any errors that didn't leave these
15544119Stn143363 	 * values where they would be cleaned up later.
15554119Stn143363 	 */
15564119Stn143363 	if (value != NULL)
15574119Stn143363 		scf_value_destroy(value);
15584119Stn143363 	if (entry != NULL)
15594119Stn143363 		scf_entry_destroy(entry);
15604119Stn143363 	return (SCF_FAILED);
15614119Stn143363 }
15624119Stn143363 
15630Sstevel@tonic-gate int
scf_simple_walk_instances(uint_t state_flags,void * private,int (* inst_callback)(scf_handle_t *,scf_instance_t *,void *))15640Sstevel@tonic-gate scf_simple_walk_instances(uint_t state_flags, void *private,
15650Sstevel@tonic-gate     int (*inst_callback)(scf_handle_t *, scf_instance_t *, void *))
15660Sstevel@tonic-gate {
15670Sstevel@tonic-gate 	scf_scope_t 		*scope = NULL;
15680Sstevel@tonic-gate 	scf_service_t		*svc = NULL;
15690Sstevel@tonic-gate 	scf_instance_t		*inst = NULL;
15700Sstevel@tonic-gate 	scf_iter_t		*svc_iter = NULL, *inst_iter = NULL;
15710Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
15720Sstevel@tonic-gate 	int			ret = SCF_FAILED;
15730Sstevel@tonic-gate 	int			svc_iter_ret, inst_iter_ret;
15740Sstevel@tonic-gate 	int			inst_state;
15750Sstevel@tonic-gate 
1576*12967Sgavin.maltby@oracle.com 	if ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL)
15770Sstevel@tonic-gate 		return (ret);
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	if (((scope = scf_scope_create(h)) == NULL) ||
15800Sstevel@tonic-gate 	    ((svc = scf_service_create(h)) == NULL) ||
15810Sstevel@tonic-gate 	    ((inst = scf_instance_create(h)) == NULL) ||
15820Sstevel@tonic-gate 	    ((svc_iter = scf_iter_create(h)) == NULL) ||
15830Sstevel@tonic-gate 	    ((inst_iter = scf_iter_create(h)) == NULL))
15840Sstevel@tonic-gate 		goto out;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	/*
15870Sstevel@tonic-gate 	 * Get the local scope, and set up nested iteration through every
15880Sstevel@tonic-gate 	 * local service, and every instance of every service.
15890Sstevel@tonic-gate 	 */
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	if ((scf_handle_get_local_scope(h, scope) != SCF_SUCCESS) ||
15920Sstevel@tonic-gate 	    (scf_iter_scope_services(svc_iter, scope) != SCF_SUCCESS))
15930Sstevel@tonic-gate 		goto out;
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	while ((svc_iter_ret = scf_iter_next_service(svc_iter, svc)) > 0) {
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 		if ((scf_iter_service_instances(inst_iter, svc)) !=
15980Sstevel@tonic-gate 		    SCF_SUCCESS)
15990Sstevel@tonic-gate 			goto out;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 		while ((inst_iter_ret =
16020Sstevel@tonic-gate 		    scf_iter_next_instance(inst_iter, inst)) > 0) {
16030Sstevel@tonic-gate 			/*
16040Sstevel@tonic-gate 			 * If get_inst_state fails from an internal error,
16050Sstevel@tonic-gate 			 * IE, being unable to get the property group or
16060Sstevel@tonic-gate 			 * property containing the state of the instance,
16070Sstevel@tonic-gate 			 * we continue instead of failing, as this might just
16080Sstevel@tonic-gate 			 * be an improperly configured instance.
16090Sstevel@tonic-gate 			 */
16100Sstevel@tonic-gate 			if ((inst_state = get_inst_state(inst, h)) == -1) {
16110Sstevel@tonic-gate 				if (scf_error() == SCF_ERROR_INTERNAL) {
16120Sstevel@tonic-gate 					continue;
16130Sstevel@tonic-gate 				} else {
16140Sstevel@tonic-gate 					goto out;
16150Sstevel@tonic-gate 				}
16160Sstevel@tonic-gate 			}
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 			if ((uint_t)inst_state & state_flags) {
16190Sstevel@tonic-gate 				if (inst_callback(h, inst, private) !=
16200Sstevel@tonic-gate 				    SCF_SUCCESS) {
16210Sstevel@tonic-gate 					(void) scf_set_error(
16220Sstevel@tonic-gate 					    SCF_ERROR_CALLBACK_FAILED);
16230Sstevel@tonic-gate 					goto out;
16240Sstevel@tonic-gate 				}
16250Sstevel@tonic-gate 			}
16260Sstevel@tonic-gate 		}
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 		if (inst_iter_ret == -1)
16290Sstevel@tonic-gate 			goto out;
16300Sstevel@tonic-gate 		scf_iter_reset(inst_iter);
16310Sstevel@tonic-gate 	}
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	if (svc_iter_ret != -1)
16340Sstevel@tonic-gate 		ret = SCF_SUCCESS;
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate out:
16370Sstevel@tonic-gate 	scf_scope_destroy(scope);
16380Sstevel@tonic-gate 	scf_service_destroy(svc);
16390Sstevel@tonic-gate 	scf_instance_destroy(inst);
16400Sstevel@tonic-gate 	scf_iter_destroy(svc_iter);
16410Sstevel@tonic-gate 	scf_iter_destroy(inst_iter);
16420Sstevel@tonic-gate 	scf_handle_destroy(h);
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	return (ret);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate scf_simple_prop_t *
scf_simple_prop_get(scf_handle_t * hin,const char * instance,const char * pgname,const char * propname)16490Sstevel@tonic-gate scf_simple_prop_get(scf_handle_t *hin, const char *instance, const char *pgname,
16500Sstevel@tonic-gate 			const char *propname)
16510Sstevel@tonic-gate {
16520Sstevel@tonic-gate 	char 			*fmri_buf, *svcfmri = NULL;
16530Sstevel@tonic-gate 	ssize_t 		fmri_sz;
16540Sstevel@tonic-gate 	scf_property_t 		*prop = NULL;
16550Sstevel@tonic-gate 	scf_service_t 		*svc = NULL;
16560Sstevel@tonic-gate 	scf_simple_prop_t 	*ret;
16570Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
16580Sstevel@tonic-gate 	boolean_t		local_h = B_TRUE;
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	/* If the user passed in a handle, use it. */
16610Sstevel@tonic-gate 	if (hin != NULL) {
16620Sstevel@tonic-gate 		h = hin;
16630Sstevel@tonic-gate 		local_h = B_FALSE;
16640Sstevel@tonic-gate 	}
16650Sstevel@tonic-gate 
1666*12967Sgavin.maltby@oracle.com 	if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
16670Sstevel@tonic-gate 		return (NULL);
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	if ((fmri_buf = assemble_fmri(h, instance, pgname, propname)) == NULL) {
16700Sstevel@tonic-gate 		if (local_h)
16710Sstevel@tonic-gate 			scf_handle_destroy(h);
16720Sstevel@tonic-gate 		return (NULL);
16730Sstevel@tonic-gate 	}
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	if ((svc = scf_service_create(h)) == NULL ||
16760Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL)
16770Sstevel@tonic-gate 		goto error1;
16780Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri_buf, NULL, NULL, NULL, NULL, prop,
16790Sstevel@tonic-gate 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
16800Sstevel@tonic-gate 		switch (scf_error()) {
16810Sstevel@tonic-gate 		/*
16820Sstevel@tonic-gate 		 * If the property isn't found in the instance, we grab the
16830Sstevel@tonic-gate 		 * underlying service, create an FMRI out of it, and then
16840Sstevel@tonic-gate 		 * query the datastore again at the service level for the
16850Sstevel@tonic-gate 		 * property.
16860Sstevel@tonic-gate 		 */
16870Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
16880Sstevel@tonic-gate 			if (scf_handle_decode_fmri(h, fmri_buf, NULL, svc,
16890Sstevel@tonic-gate 			    NULL, NULL, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1)
16900Sstevel@tonic-gate 				goto error1;
16917887SLiane.Praza@Sun.COM 
16927887SLiane.Praza@Sun.COM 			fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH) + 1;
16937887SLiane.Praza@Sun.COM 			assert(fmri_sz > 0);
16947887SLiane.Praza@Sun.COM 
16950Sstevel@tonic-gate 			if (scf_service_to_fmri(svc, fmri_buf, fmri_sz) == -1)
16960Sstevel@tonic-gate 				goto error1;
16970Sstevel@tonic-gate 			if ((svcfmri = assemble_fmri(h, fmri_buf, pgname,
16980Sstevel@tonic-gate 			    propname)) == NULL)
16990Sstevel@tonic-gate 				goto error1;
17000Sstevel@tonic-gate 			if (scf_handle_decode_fmri(h, svcfmri, NULL, NULL,
17010Sstevel@tonic-gate 			    NULL, NULL, prop, 0) == -1) {
17020Sstevel@tonic-gate 				free(svcfmri);
17030Sstevel@tonic-gate 				goto error1;
17040Sstevel@tonic-gate 			}
17050Sstevel@tonic-gate 			free(svcfmri);
17060Sstevel@tonic-gate 			break;
17070Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
17080Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
17090Sstevel@tonic-gate 		default:
17100Sstevel@tonic-gate 			goto error1;
17110Sstevel@tonic-gate 		}
17120Sstevel@tonic-gate 	}
17130Sstevel@tonic-gate 	/*
17140Sstevel@tonic-gate 	 * At this point, we've successfully pulled the property from the
17150Sstevel@tonic-gate 	 * datastore, and simply need to copy its innards into an
17160Sstevel@tonic-gate 	 * scf_simple_prop_t.
17170Sstevel@tonic-gate 	 */
17180Sstevel@tonic-gate 	if ((ret = fill_prop(prop, pgname, propname, h)) == NULL)
17190Sstevel@tonic-gate 		goto error1;
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	scf_service_destroy(svc);
17220Sstevel@tonic-gate 	scf_property_destroy(prop);
17230Sstevel@tonic-gate 	free(fmri_buf);
17240Sstevel@tonic-gate 	if (local_h)
17250Sstevel@tonic-gate 		scf_handle_destroy(h);
17260Sstevel@tonic-gate 	return (ret);
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	/*
17290Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
17300Sstevel@tonic-gate 	 * for failures at various stages during the function.
17310Sstevel@tonic-gate 	 */
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate error1:
17340Sstevel@tonic-gate 	scf_service_destroy(svc);
17350Sstevel@tonic-gate 	scf_property_destroy(prop);
17360Sstevel@tonic-gate error2:
17370Sstevel@tonic-gate 	free(fmri_buf);
17380Sstevel@tonic-gate 	if (local_h)
17390Sstevel@tonic-gate 		scf_handle_destroy(h);
17400Sstevel@tonic-gate 	return (NULL);
17410Sstevel@tonic-gate }
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate void
scf_simple_prop_free(scf_simple_prop_t * prop)17450Sstevel@tonic-gate scf_simple_prop_free(scf_simple_prop_t *prop)
17460Sstevel@tonic-gate {
17470Sstevel@tonic-gate 	int i;
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	if (prop == NULL)
17500Sstevel@tonic-gate 		return;
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	free(prop->pr_propname);
17530Sstevel@tonic-gate 	free(prop->pr_pgname);
17540Sstevel@tonic-gate 	switch (prop->pr_type) {
17550Sstevel@tonic-gate 	case SCF_TYPE_OPAQUE: {
17560Sstevel@tonic-gate 		for (i = 0; i < prop->pr_numvalues; i++) {
17570Sstevel@tonic-gate 			free(prop->pr_vallist[i].pv_opaque.o_value);
17580Sstevel@tonic-gate 		}
17590Sstevel@tonic-gate 		break;
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 	case SCF_TYPE_ASTRING:
17620Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
17630Sstevel@tonic-gate 	case SCF_TYPE_HOST:
17640Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
176512483SAntonello.Cruz@Sun.COM 	case SCF_TYPE_NET_ADDR:
17660Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
17670Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
17680Sstevel@tonic-gate 	case SCF_TYPE_URI:
17690Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
17700Sstevel@tonic-gate 		for (i = 0; i < prop->pr_numvalues; i++) {
17710Sstevel@tonic-gate 			free(prop->pr_vallist[i].pv_str);
17720Sstevel@tonic-gate 		}
17730Sstevel@tonic-gate 		break;
17740Sstevel@tonic-gate 	}
17750Sstevel@tonic-gate 	default:
17760Sstevel@tonic-gate 		break;
17770Sstevel@tonic-gate 	}
17780Sstevel@tonic-gate 	free(prop->pr_vallist);
17790Sstevel@tonic-gate 	free(prop);
17800Sstevel@tonic-gate }
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate scf_simple_app_props_t *
scf_simple_app_props_get(scf_handle_t * hin,const char * inst_fmri)17840Sstevel@tonic-gate scf_simple_app_props_get(scf_handle_t *hin, const char *inst_fmri)
17850Sstevel@tonic-gate {
17860Sstevel@tonic-gate 	scf_instance_t 		*inst = NULL;
17870Sstevel@tonic-gate 	scf_service_t 		*svc = NULL;
17880Sstevel@tonic-gate 	scf_propertygroup_t 	*pg = NULL;
17890Sstevel@tonic-gate 	scf_property_t 		*prop = NULL;
17900Sstevel@tonic-gate 	scf_simple_app_props_t	*ret = NULL;
17910Sstevel@tonic-gate 	scf_iter_t		*pgiter = NULL, *propiter = NULL;
17920Sstevel@tonic-gate 	struct scf_simple_pg	*thispg = NULL, *nextpg;
17930Sstevel@tonic-gate 	scf_simple_prop_t	*thisprop, *nextprop;
17940Sstevel@tonic-gate 	scf_handle_t		*h = NULL;
17950Sstevel@tonic-gate 	int			pgiter_ret, propiter_ret;
17960Sstevel@tonic-gate 	ssize_t			namelen;
17970Sstevel@tonic-gate 	char 			*propname = NULL, *pgname = NULL, *sys_fmri;
17980Sstevel@tonic-gate 	uint8_t			found;
17990Sstevel@tonic-gate 	boolean_t		local_h = B_TRUE;
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 	/* If the user passed in a handle, use it. */
18020Sstevel@tonic-gate 	if (hin != NULL) {
18030Sstevel@tonic-gate 		h = hin;
18040Sstevel@tonic-gate 		local_h = B_FALSE;
18050Sstevel@tonic-gate 	}
18060Sstevel@tonic-gate 
1807*12967Sgavin.maltby@oracle.com 	if (local_h && ((h = _scf_handle_create_and_bind(SCF_VERSION)) == NULL))
18080Sstevel@tonic-gate 		return (NULL);
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 	if (inst_fmri == NULL) {
18110Sstevel@tonic-gate 		if ((namelen = scf_myname(h, NULL, 0)) == -1) {
18120Sstevel@tonic-gate 			if (local_h)
18130Sstevel@tonic-gate 				scf_handle_destroy(h);
18140Sstevel@tonic-gate 			return (NULL);
18150Sstevel@tonic-gate 		}
18160Sstevel@tonic-gate 		if ((sys_fmri = malloc(namelen + 1)) == NULL) {
18170Sstevel@tonic-gate 			if (local_h)
18180Sstevel@tonic-gate 				scf_handle_destroy(h);
18190Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18200Sstevel@tonic-gate 			return (NULL);
18210Sstevel@tonic-gate 		}
18220Sstevel@tonic-gate 		if (scf_myname(h, sys_fmri, namelen + 1) == -1) {
18230Sstevel@tonic-gate 			if (local_h)
18240Sstevel@tonic-gate 				scf_handle_destroy(h);
18250Sstevel@tonic-gate 			free(sys_fmri);
18260Sstevel@tonic-gate 			return (NULL);
18270Sstevel@tonic-gate 		}
18280Sstevel@tonic-gate 	} else {
18295040Swesolows 		if ((sys_fmri = strdup(inst_fmri)) == NULL) {
18305040Swesolows 			if (local_h)
18315040Swesolows 				scf_handle_destroy(h);
18325040Swesolows 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18335040Swesolows 			return (NULL);
18345040Swesolows 		}
18350Sstevel@tonic-gate 	}
18360Sstevel@tonic-gate 
18377887SLiane.Praza@Sun.COM 	namelen = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH) + 1;
18387887SLiane.Praza@Sun.COM 	assert(namelen > 0);
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL ||
18410Sstevel@tonic-gate 	    (svc = scf_service_create(h)) == NULL ||
18420Sstevel@tonic-gate 	    (pgiter = scf_iter_create(h)) == NULL ||
18430Sstevel@tonic-gate 	    (propiter = scf_iter_create(h)) == NULL ||
18440Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
18450Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL)
18460Sstevel@tonic-gate 		goto error2;
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, sys_fmri, NULL, svc, inst, NULL, NULL,
18490Sstevel@tonic-gate 	    SCF_DECODE_FMRI_REQUIRE_INSTANCE) == -1) {
18500Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONSTRAINT_VIOLATED)
18510Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INVALID_ARGUMENT);
18520Sstevel@tonic-gate 		goto error2;
18530Sstevel@tonic-gate 	}
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 	if ((ret = malloc(sizeof (*ret))) == NULL ||
18560Sstevel@tonic-gate 	    (thispg = malloc(sizeof (*thispg))) == NULL ||
18570Sstevel@tonic-gate 	    (propname = malloc(namelen)) == NULL ||
18580Sstevel@tonic-gate 	    (pgname = malloc(namelen)) == NULL) {
18590Sstevel@tonic-gate 		free(thispg);
18600Sstevel@tonic-gate 		free(ret);
18610Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18620Sstevel@tonic-gate 		goto error2;
18630Sstevel@tonic-gate 	}
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	ret->ap_fmri = sys_fmri;
18660Sstevel@tonic-gate 	thispg->pg_name = NULL;
18670Sstevel@tonic-gate 	thispg->pg_proplist = NULL;
18680Sstevel@tonic-gate 	thispg->pg_next = NULL;
18690Sstevel@tonic-gate 	ret->ap_pglist = thispg;
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	if (scf_iter_service_pgs_typed(pgiter, svc, SCF_GROUP_APPLICATION) !=
18720Sstevel@tonic-gate 	    0) {
18730Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
18740Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
18750Sstevel@tonic-gate 		goto error1;
18760Sstevel@tonic-gate 	}
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
18790Sstevel@tonic-gate 		if (thispg->pg_name != NULL) {
18800Sstevel@tonic-gate 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
18810Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18820Sstevel@tonic-gate 				goto error1;
18830Sstevel@tonic-gate 			}
18845040Swesolows 			nextpg->pg_name = NULL;
18855040Swesolows 			nextpg->pg_next = NULL;
18865040Swesolows 			nextpg->pg_proplist = NULL;
18870Sstevel@tonic-gate 			thispg->pg_next = nextpg;
18880Sstevel@tonic-gate 			thispg = nextpg;
18890Sstevel@tonic-gate 		} else {
18900Sstevel@tonic-gate 			/* This is the first iteration */
18910Sstevel@tonic-gate 			nextpg = thispg;
18920Sstevel@tonic-gate 		}
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 		if ((nextpg->pg_name = malloc(namelen)) == NULL) {
18950Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NO_MEMORY);
18960Sstevel@tonic-gate 			goto error1;
18970Sstevel@tonic-gate 		}
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 		if (scf_pg_get_name(pg, nextpg->pg_name, namelen) < 0) {
19000Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
19010Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19020Sstevel@tonic-gate 			goto error1;
19030Sstevel@tonic-gate 		}
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 		thisprop = NULL;
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 		scf_iter_reset(propiter);
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 		if (scf_iter_pg_properties(propiter, pg) != 0) {
19100Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19110Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19120Sstevel@tonic-gate 			goto error1;
19130Sstevel@tonic-gate 		}
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 		while ((propiter_ret = scf_iter_next_property(propiter, prop))
19160Sstevel@tonic-gate 		    == 1) {
19170Sstevel@tonic-gate 			if (scf_property_get_name(prop, propname, namelen) <
19180Sstevel@tonic-gate 			    0) {
19190Sstevel@tonic-gate 				if (scf_error() == SCF_ERROR_NOT_SET)
19200Sstevel@tonic-gate 					(void) scf_set_error(
19210Sstevel@tonic-gate 					    SCF_ERROR_INTERNAL);
19220Sstevel@tonic-gate 				goto error1;
19230Sstevel@tonic-gate 			}
19240Sstevel@tonic-gate 			if (thisprop != NULL) {
19250Sstevel@tonic-gate 				if ((nextprop = fill_prop(prop,
19260Sstevel@tonic-gate 				    nextpg->pg_name, propname, h)) == NULL)
19270Sstevel@tonic-gate 					goto error1;
19280Sstevel@tonic-gate 				thisprop->pr_next = nextprop;
19290Sstevel@tonic-gate 				thisprop = nextprop;
19300Sstevel@tonic-gate 			} else {
19310Sstevel@tonic-gate 				/* This is the first iteration */
19320Sstevel@tonic-gate 				if ((thisprop = fill_prop(prop,
19330Sstevel@tonic-gate 				    nextpg->pg_name, propname, h)) == NULL)
19340Sstevel@tonic-gate 					goto error1;
19350Sstevel@tonic-gate 				nextpg->pg_proplist = thisprop;
19360Sstevel@tonic-gate 				nextprop = thisprop;
19370Sstevel@tonic-gate 			}
19380Sstevel@tonic-gate 			nextprop->pr_pg = nextpg;
19390Sstevel@tonic-gate 			nextprop->pr_next = NULL;
19400Sstevel@tonic-gate 		}
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 		if (propiter_ret == -1) {
19430Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19440Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19450Sstevel@tonic-gate 			goto error1;
19460Sstevel@tonic-gate 		}
19470Sstevel@tonic-gate 	}
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	if (pgiter_ret == -1) {
19500Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19510Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
19520Sstevel@tonic-gate 		goto error1;
19530Sstevel@tonic-gate 	}
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 	/*
19560Sstevel@tonic-gate 	 * At this point, we've filled the scf_simple_app_props_t with all the
19570Sstevel@tonic-gate 	 * properties at the service level.  Now we iterate over all the
19580Sstevel@tonic-gate 	 * properties at the instance level, overwriting any duplicate
19590Sstevel@tonic-gate 	 * properties, in order to provide service/instance composition.
19600Sstevel@tonic-gate 	 */
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 	scf_iter_reset(pgiter);
19630Sstevel@tonic-gate 	scf_iter_reset(propiter);
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed(pgiter, inst, SCF_GROUP_APPLICATION)
19660Sstevel@tonic-gate 	    != 0) {
19670Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
19680Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
19690Sstevel@tonic-gate 		goto error1;
19700Sstevel@tonic-gate 	}
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 	while ((pgiter_ret = scf_iter_next_pg(pgiter, pg)) == 1) {
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 		thispg = ret->ap_pglist;
19750Sstevel@tonic-gate 		found = 0;
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 		/*
19780Sstevel@tonic-gate 		 * Find either the end of the list, so we can append the
19790Sstevel@tonic-gate 		 * property group, or an existing property group that matches
19800Sstevel@tonic-gate 		 * it, so we can insert/overwrite its properties.
19810Sstevel@tonic-gate 		 */
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 		if (scf_pg_get_name(pg, pgname, namelen) < 0) {
19840Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_SET)
19850Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
19860Sstevel@tonic-gate 			goto error1;
19870Sstevel@tonic-gate 		}
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 		while ((thispg != NULL) && (thispg->pg_name != NULL)) {
19900Sstevel@tonic-gate 			if (strcmp(thispg->pg_name, pgname) == 0) {
19910Sstevel@tonic-gate 				found = 1;
19920Sstevel@tonic-gate 				break;
19930Sstevel@tonic-gate 			}
19940Sstevel@tonic-gate 			if (thispg->pg_next == NULL)
19950Sstevel@tonic-gate 				break;
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 			thispg = thispg->pg_next;
19980Sstevel@tonic-gate 		}
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 		scf_iter_reset(propiter);
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 		if (scf_iter_pg_properties(propiter, pg) != 0) {
20030Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20040Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_INTERNAL);
20050Sstevel@tonic-gate 			goto error1;
20060Sstevel@tonic-gate 		}
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 		if (found) {
20090Sstevel@tonic-gate 			/*
20100Sstevel@tonic-gate 			 * insert_app_props inserts or overwrites the
20110Sstevel@tonic-gate 			 * properties in thispg.
20120Sstevel@tonic-gate 			 */
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 			if (insert_app_props(propiter, pgname, propname,
20150Sstevel@tonic-gate 			    thispg, prop, namelen, h) == -1)
20160Sstevel@tonic-gate 				goto error1;
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 		} else {
20190Sstevel@tonic-gate 			/*
20200Sstevel@tonic-gate 			 * If the property group wasn't found, we're adding
20210Sstevel@tonic-gate 			 * a newly allocated property group to the end of the
20220Sstevel@tonic-gate 			 * list.
20230Sstevel@tonic-gate 			 */
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 			if ((nextpg = malloc(sizeof (*nextpg))) == NULL) {
20260Sstevel@tonic-gate 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
20270Sstevel@tonic-gate 				goto error1;
20280Sstevel@tonic-gate 			}
20290Sstevel@tonic-gate 			nextpg->pg_next = NULL;
20300Sstevel@tonic-gate 			nextpg->pg_proplist = NULL;
20310Sstevel@tonic-gate 			thisprop = NULL;
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 			if ((nextpg->pg_name = strdup(pgname)) == NULL) {
20345040Swesolows 				(void) scf_set_error(SCF_ERROR_NO_MEMORY);
20350Sstevel@tonic-gate 				free(nextpg);
20360Sstevel@tonic-gate 				goto error1;
20370Sstevel@tonic-gate 			}
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 			if (thispg->pg_name == NULL) {
20400Sstevel@tonic-gate 				free(thispg);
20410Sstevel@tonic-gate 				ret->ap_pglist = nextpg;
20420Sstevel@tonic-gate 			} else {
20430Sstevel@tonic-gate 				thispg->pg_next = nextpg;
20440Sstevel@tonic-gate 			}
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 			while ((propiter_ret =
20470Sstevel@tonic-gate 			    scf_iter_next_property(propiter, prop)) == 1) {
20480Sstevel@tonic-gate 				if (scf_property_get_name(prop, propname,
20490Sstevel@tonic-gate 				    namelen) < 0) {
20500Sstevel@tonic-gate 					if (scf_error() == SCF_ERROR_NOT_SET)
20510Sstevel@tonic-gate 						(void) scf_set_error(
20520Sstevel@tonic-gate 						    SCF_ERROR_INTERNAL);
20530Sstevel@tonic-gate 					goto error1;
20540Sstevel@tonic-gate 				}
20550Sstevel@tonic-gate 				if (thisprop != NULL) {
20560Sstevel@tonic-gate 					if ((nextprop = fill_prop(prop,
20570Sstevel@tonic-gate 					    pgname, propname, h)) ==
20580Sstevel@tonic-gate 					    NULL)
20590Sstevel@tonic-gate 						goto error1;
20600Sstevel@tonic-gate 					thisprop->pr_next = nextprop;
20610Sstevel@tonic-gate 					thisprop = nextprop;
20620Sstevel@tonic-gate 				} else {
20630Sstevel@tonic-gate 					/* This is the first iteration */
20640Sstevel@tonic-gate 					if ((thisprop = fill_prop(prop,
20650Sstevel@tonic-gate 					    pgname, propname, h)) ==
20660Sstevel@tonic-gate 					    NULL)
20670Sstevel@tonic-gate 						goto error1;
20680Sstevel@tonic-gate 					nextpg->pg_proplist = thisprop;
20690Sstevel@tonic-gate 					nextprop = thisprop;
20700Sstevel@tonic-gate 				}
20710Sstevel@tonic-gate 				nextprop->pr_pg = nextpg;
20720Sstevel@tonic-gate 				nextprop->pr_next = NULL;
20730Sstevel@tonic-gate 			}
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 			if (propiter_ret == -1) {
20760Sstevel@tonic-gate 				if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20770Sstevel@tonic-gate 					(void) scf_set_error(
20780Sstevel@tonic-gate 					    SCF_ERROR_INTERNAL);
20790Sstevel@tonic-gate 				goto error1;
20800Sstevel@tonic-gate 			}
20810Sstevel@tonic-gate 		}
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	}
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 	if (pgiter_ret == -1) {
20860Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_CONNECTION_BROKEN)
20870Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_INTERNAL);
20880Sstevel@tonic-gate 		goto error1;
20890Sstevel@tonic-gate 	}
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 	scf_iter_destroy(pgiter);
20920Sstevel@tonic-gate 	scf_iter_destroy(propiter);
20930Sstevel@tonic-gate 	scf_pg_destroy(pg);
20940Sstevel@tonic-gate 	scf_property_destroy(prop);
20950Sstevel@tonic-gate 	scf_instance_destroy(inst);
20960Sstevel@tonic-gate 	scf_service_destroy(svc);
20970Sstevel@tonic-gate 	free(propname);
20980Sstevel@tonic-gate 	free(pgname);
20990Sstevel@tonic-gate 	if (local_h)
21000Sstevel@tonic-gate 		scf_handle_destroy(h);
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	if (ret->ap_pglist->pg_name == NULL)
21030Sstevel@tonic-gate 		return (NULL);
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	return (ret);
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 	/*
21080Sstevel@tonic-gate 	 * Exit point for a successful call.  Below this line are exit points
21090Sstevel@tonic-gate 	 * for failures at various stages during the function.
21100Sstevel@tonic-gate 	 */
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate error1:
21130Sstevel@tonic-gate 	scf_simple_app_props_free(ret);
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate error2:
21160Sstevel@tonic-gate 	scf_iter_destroy(pgiter);
21170Sstevel@tonic-gate 	scf_iter_destroy(propiter);
21180Sstevel@tonic-gate 	scf_pg_destroy(pg);
21190Sstevel@tonic-gate 	scf_property_destroy(prop);
21200Sstevel@tonic-gate 	scf_instance_destroy(inst);
21210Sstevel@tonic-gate 	scf_service_destroy(svc);
21220Sstevel@tonic-gate 	free(propname);
21230Sstevel@tonic-gate 	free(pgname);
21240Sstevel@tonic-gate 	if (local_h)
21250Sstevel@tonic-gate 		scf_handle_destroy(h);
21260Sstevel@tonic-gate 	return (NULL);
21270Sstevel@tonic-gate }
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate void
scf_simple_app_props_free(scf_simple_app_props_t * propblock)21310Sstevel@tonic-gate scf_simple_app_props_free(scf_simple_app_props_t *propblock)
21320Sstevel@tonic-gate {
21330Sstevel@tonic-gate 	struct scf_simple_pg 	*pgthis, *pgnext;
21340Sstevel@tonic-gate 	scf_simple_prop_t 	*propthis, *propnext;
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	if ((propblock == NULL) || (propblock->ap_pglist == NULL))
21370Sstevel@tonic-gate 		return;
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 	for (pgthis = propblock->ap_pglist; pgthis != NULL; pgthis = pgnext) {
21400Sstevel@tonic-gate 		pgnext = pgthis->pg_next;
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 		propthis = pgthis->pg_proplist;
21430Sstevel@tonic-gate 
21440Sstevel@tonic-gate 		while (propthis != NULL) {
21450Sstevel@tonic-gate 			propnext = propthis->pr_next;
21460Sstevel@tonic-gate 			scf_simple_prop_free(propthis);
21470Sstevel@tonic-gate 			propthis = propnext;
21480Sstevel@tonic-gate 		}
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 		free(pgthis->pg_name);
21510Sstevel@tonic-gate 		free(pgthis);
21520Sstevel@tonic-gate 	}
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 	free(propblock->ap_fmri);
21550Sstevel@tonic-gate 	free(propblock);
21560Sstevel@tonic-gate }
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate const scf_simple_prop_t *
scf_simple_app_props_next(const scf_simple_app_props_t * propblock,scf_simple_prop_t * last)21590Sstevel@tonic-gate scf_simple_app_props_next(const scf_simple_app_props_t *propblock,
21600Sstevel@tonic-gate     scf_simple_prop_t *last)
21610Sstevel@tonic-gate {
21620Sstevel@tonic-gate 	struct scf_simple_pg 	*this;
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 	if (propblock == NULL) {
21650Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
21660Sstevel@tonic-gate 		return (NULL);
21670Sstevel@tonic-gate 	}
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate 	this = propblock->ap_pglist;
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 	/*
21720Sstevel@tonic-gate 	 * We're looking for the first property in this block if last is
21730Sstevel@tonic-gate 	 * NULL
21740Sstevel@tonic-gate 	 */
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 	if (last == NULL) {
21770Sstevel@tonic-gate 		/* An empty pglist is legal, it just means no properties */
21780Sstevel@tonic-gate 		if (this == NULL) {
21790Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NONE);
21800Sstevel@tonic-gate 			return (NULL);
21810Sstevel@tonic-gate 		}
21820Sstevel@tonic-gate 		/*
21830Sstevel@tonic-gate 		 * Walk until we find a pg with a property in it, or we run
21840Sstevel@tonic-gate 		 * out of property groups.
21850Sstevel@tonic-gate 		 */
21860Sstevel@tonic-gate 		while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
21870Sstevel@tonic-gate 			this = this->pg_next;
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 		if (this->pg_proplist == NULL) {
21900Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_NONE);
21910Sstevel@tonic-gate 			return (NULL);
21920Sstevel@tonic-gate 		}
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 		return (this->pg_proplist);
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	}
21970Sstevel@tonic-gate 	/*
21980Sstevel@tonic-gate 	 * If last isn't NULL, then return the next prop in the property group,
21990Sstevel@tonic-gate 	 * or walk the property groups until we find another property, or
22000Sstevel@tonic-gate 	 * run out of property groups.
22010Sstevel@tonic-gate 	 */
22020Sstevel@tonic-gate 	if (last->pr_next != NULL)
22030Sstevel@tonic-gate 		return (last->pr_next);
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	if (last->pr_pg->pg_next == NULL) {
22060Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
22070Sstevel@tonic-gate 		return (NULL);
22080Sstevel@tonic-gate 	}
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 	this = last->pr_pg->pg_next;
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 	while ((this->pg_proplist == NULL) && (this->pg_next != NULL))
22130Sstevel@tonic-gate 		this = this->pg_next;
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	if (this->pg_proplist == NULL) {
22160Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
22170Sstevel@tonic-gate 		return (NULL);
22180Sstevel@tonic-gate 	}
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	return (this->pg_proplist);
22210Sstevel@tonic-gate }
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate const scf_simple_prop_t *
scf_simple_app_props_search(const scf_simple_app_props_t * propblock,const char * pgname,const char * propname)22240Sstevel@tonic-gate scf_simple_app_props_search(const scf_simple_app_props_t *propblock,
22250Sstevel@tonic-gate     const char *pgname, const char *propname)
22260Sstevel@tonic-gate {
22270Sstevel@tonic-gate 	struct scf_simple_pg 	*pg;
22280Sstevel@tonic-gate 	scf_simple_prop_t 	*prop;
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 	if ((propblock == NULL) || (propname == NULL)) {
22310Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
22320Sstevel@tonic-gate 		return (NULL);
22330Sstevel@tonic-gate 	}
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 	pg = propblock->ap_pglist;
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	/*
22380Sstevel@tonic-gate 	 * If pgname is NULL, we're searching the default application
22390Sstevel@tonic-gate 	 * property group, otherwise we look for the specified group.
22400Sstevel@tonic-gate 	 */
22410Sstevel@tonic-gate 	if (pgname == NULL) {
22420Sstevel@tonic-gate 		while ((pg != NULL) &&
22430Sstevel@tonic-gate 		    (strcmp(SCF_PG_APP_DEFAULT, pg->pg_name) != 0))
22440Sstevel@tonic-gate 			pg = pg->pg_next;
22450Sstevel@tonic-gate 	} else {
22460Sstevel@tonic-gate 		while ((pg != NULL) && (strcmp(pgname, pg->pg_name) != 0))
22470Sstevel@tonic-gate 			pg = pg->pg_next;
22480Sstevel@tonic-gate 	}
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 	if (pg == NULL) {
22510Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
22520Sstevel@tonic-gate 		return (NULL);
22530Sstevel@tonic-gate 	}
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	prop = pg->pg_proplist;
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	while ((prop != NULL) && (strcmp(propname, prop->pr_propname) != 0))
22580Sstevel@tonic-gate 		prop = prop->pr_next;
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	if (prop == NULL) {
22610Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_FOUND);
22620Sstevel@tonic-gate 		return (NULL);
22630Sstevel@tonic-gate 	}
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	return (prop);
22660Sstevel@tonic-gate }
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate void
scf_simple_prop_next_reset(scf_simple_prop_t * prop)22690Sstevel@tonic-gate scf_simple_prop_next_reset(scf_simple_prop_t *prop)
22700Sstevel@tonic-gate {
22710Sstevel@tonic-gate 	if (prop == NULL)
22720Sstevel@tonic-gate 		return;
22730Sstevel@tonic-gate 	prop->pr_iter = 0;
22740Sstevel@tonic-gate }
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate ssize_t
scf_simple_prop_numvalues(const scf_simple_prop_t * prop)22770Sstevel@tonic-gate scf_simple_prop_numvalues(const scf_simple_prop_t *prop)
22780Sstevel@tonic-gate {
22790Sstevel@tonic-gate 	if (prop == NULL)
22800Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
22810Sstevel@tonic-gate 
22820Sstevel@tonic-gate 	return (prop->pr_numvalues);
22830Sstevel@tonic-gate }
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate scf_type_t
scf_simple_prop_type(const scf_simple_prop_t * prop)22870Sstevel@tonic-gate scf_simple_prop_type(const scf_simple_prop_t *prop)
22880Sstevel@tonic-gate {
22890Sstevel@tonic-gate 	if (prop == NULL)
22900Sstevel@tonic-gate 		return (scf_set_error(SCF_ERROR_NOT_SET));
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 	return (prop->pr_type);
22930Sstevel@tonic-gate }
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate char *
scf_simple_prop_name(const scf_simple_prop_t * prop)22970Sstevel@tonic-gate scf_simple_prop_name(const scf_simple_prop_t *prop)
22980Sstevel@tonic-gate {
22990Sstevel@tonic-gate 	if ((prop == NULL) || (prop->pr_propname == NULL)) {
23000Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
23010Sstevel@tonic-gate 		return (NULL);
23020Sstevel@tonic-gate 	}
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 	return (prop->pr_propname);
23050Sstevel@tonic-gate }
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate char *
scf_simple_prop_pgname(const scf_simple_prop_t * prop)23090Sstevel@tonic-gate scf_simple_prop_pgname(const scf_simple_prop_t *prop)
23100Sstevel@tonic-gate {
23110Sstevel@tonic-gate 	if ((prop == NULL) || (prop->pr_pgname == NULL)) {
23120Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
23130Sstevel@tonic-gate 		return (NULL);
23140Sstevel@tonic-gate 	}
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	return (prop->pr_pgname);
23170Sstevel@tonic-gate }
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate static union scf_simple_prop_val *
scf_next_val(scf_simple_prop_t * prop,scf_type_t type)23210Sstevel@tonic-gate scf_next_val(scf_simple_prop_t *prop, scf_type_t type)
23220Sstevel@tonic-gate {
23230Sstevel@tonic-gate 	if (prop == NULL) {
23240Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NOT_SET);
23250Sstevel@tonic-gate 		return (NULL);
23260Sstevel@tonic-gate 	}
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 	switch (prop->pr_type) {
23290Sstevel@tonic-gate 	case SCF_TYPE_USTRING:
23300Sstevel@tonic-gate 	case SCF_TYPE_HOST:
23310Sstevel@tonic-gate 	case SCF_TYPE_HOSTNAME:
233212483SAntonello.Cruz@Sun.COM 	case SCF_TYPE_NET_ADDR:
23330Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V4:
23340Sstevel@tonic-gate 	case SCF_TYPE_NET_ADDR_V6:
23350Sstevel@tonic-gate 	case SCF_TYPE_URI:
23360Sstevel@tonic-gate 	case SCF_TYPE_FMRI: {
23370Sstevel@tonic-gate 		if (type != SCF_TYPE_USTRING) {
23380Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
23390Sstevel@tonic-gate 			return (NULL);
23400Sstevel@tonic-gate 		}
23410Sstevel@tonic-gate 		break;
23420Sstevel@tonic-gate 		}
23430Sstevel@tonic-gate 	default: {
23440Sstevel@tonic-gate 		if (type != prop->pr_type) {
23450Sstevel@tonic-gate 			(void) scf_set_error(SCF_ERROR_TYPE_MISMATCH);
23460Sstevel@tonic-gate 			return (NULL);
23470Sstevel@tonic-gate 		}
23480Sstevel@tonic-gate 		break;
23490Sstevel@tonic-gate 		}
23500Sstevel@tonic-gate 	}
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 	if (prop->pr_iter >= prop->pr_numvalues) {
23530Sstevel@tonic-gate 		(void) scf_set_error(SCF_ERROR_NONE);
23540Sstevel@tonic-gate 		return (NULL);
23550Sstevel@tonic-gate 	}
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 	return (&prop->pr_vallist[prop->pr_iter++]);
23580Sstevel@tonic-gate }
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate uint8_t *
scf_simple_prop_next_boolean(scf_simple_prop_t * prop)23620Sstevel@tonic-gate scf_simple_prop_next_boolean(scf_simple_prop_t *prop)
23630Sstevel@tonic-gate {
23640Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_BOOLEAN);
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 	if (ret == NULL)
23690Sstevel@tonic-gate 		return (NULL);
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	return (&ret->pv_bool);
23720Sstevel@tonic-gate }
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate uint64_t *
scf_simple_prop_next_count(scf_simple_prop_t * prop)23760Sstevel@tonic-gate scf_simple_prop_next_count(scf_simple_prop_t *prop)
23770Sstevel@tonic-gate {
23780Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_COUNT);
23810Sstevel@tonic-gate 
23820Sstevel@tonic-gate 	if (ret == NULL)
23830Sstevel@tonic-gate 		return (NULL);
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 	return (&ret->pv_uint);
23860Sstevel@tonic-gate }
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate int64_t *
scf_simple_prop_next_integer(scf_simple_prop_t * prop)23900Sstevel@tonic-gate scf_simple_prop_next_integer(scf_simple_prop_t *prop)
23910Sstevel@tonic-gate {
23920Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_INTEGER);
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate 	if (ret == NULL)
23970Sstevel@tonic-gate 		return (NULL);
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	return (&ret->pv_int);
24000Sstevel@tonic-gate }
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate int64_t *
scf_simple_prop_next_time(scf_simple_prop_t * prop,int32_t * nsec)24030Sstevel@tonic-gate scf_simple_prop_next_time(scf_simple_prop_t *prop, int32_t *nsec)
24040Sstevel@tonic-gate {
24050Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_TIME);
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	if (ret == NULL)
24100Sstevel@tonic-gate 		return (NULL);
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 	if (nsec != NULL)
24130Sstevel@tonic-gate 		*nsec = ret->pv_time.t_nsec;
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 	return (&ret->pv_time.t_sec);
24160Sstevel@tonic-gate }
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate char *
scf_simple_prop_next_astring(scf_simple_prop_t * prop)24190Sstevel@tonic-gate scf_simple_prop_next_astring(scf_simple_prop_t *prop)
24200Sstevel@tonic-gate {
24210Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_ASTRING);
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	if (ret == NULL)
24260Sstevel@tonic-gate 		return (NULL);
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	return (ret->pv_str);
24290Sstevel@tonic-gate }
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate char *
scf_simple_prop_next_ustring(scf_simple_prop_t * prop)24320Sstevel@tonic-gate scf_simple_prop_next_ustring(scf_simple_prop_t *prop)
24330Sstevel@tonic-gate {
24340Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_USTRING);
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 	if (ret == NULL)
24390Sstevel@tonic-gate 		return (NULL);
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 	return (ret->pv_str);
24420Sstevel@tonic-gate }
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate void *
scf_simple_prop_next_opaque(scf_simple_prop_t * prop,size_t * length)24450Sstevel@tonic-gate scf_simple_prop_next_opaque(scf_simple_prop_t *prop, size_t *length)
24460Sstevel@tonic-gate {
24470Sstevel@tonic-gate 	union scf_simple_prop_val *ret;
24480Sstevel@tonic-gate 
24490Sstevel@tonic-gate 	ret = scf_next_val(prop, SCF_TYPE_OPAQUE);
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	if (ret == NULL) {
24520Sstevel@tonic-gate 		*length = 0;
24530Sstevel@tonic-gate 		return (NULL);
24540Sstevel@tonic-gate 	}
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 	*length = ret->pv_opaque.o_size;
24570Sstevel@tonic-gate 	return (ret->pv_opaque.o_value);
24580Sstevel@tonic-gate }
24593175Sskamm 
24603175Sskamm /*
24613175Sskamm  * Generate a filename based on the fmri and the given name and return
24623175Sskamm  * it in the buffer of MAXPATHLEN provided by the caller.
24633175Sskamm  * If temp_filename is non-zero, also generate a temporary, unique filename
24643175Sskamm  * and return it in the temp buffer of MAXPATHLEN provided by the caller.
24653175Sskamm  * The path to the generated pathname is also created.
24663175Sskamm  * Given fmri should begin with a scheme such as "svc:".
24673175Sskamm  * Returns
24683175Sskamm  *      0 on success
24693175Sskamm  *      -1 if filename would exceed MAXPATHLEN or
24703175Sskamm  *	-2 if unable to create directory to filename path
24713175Sskamm  */
24723175Sskamm int
gen_filenms_from_fmri(const char * fmri,const char * name,char * filename,char * temp_filename)24733175Sskamm gen_filenms_from_fmri(const char *fmri, const char *name, char *filename,
24743175Sskamm     char *temp_filename)
24753175Sskamm {
24763175Sskamm 	int		len;
24773175Sskamm 
24783175Sskamm 	len = strlen(SMF_SPEEDY_FILES_PATH);
24793175Sskamm 	len += strlen(fmri);
24803175Sskamm 	len += 2;			/* for slash and null */
24813175Sskamm 	len += strlen(name);
24823175Sskamm 	len += 6;			/* For X's needed for mkstemp */
24833175Sskamm 
24843175Sskamm 	if (len > MAXPATHLEN)
24853175Sskamm 		return (-1);
24863175Sskamm 
24873175Sskamm 	/* Construct directory name first - speedy path ends in slash */
24883175Sskamm 	(void) strcpy(filename, SMF_SPEEDY_FILES_PATH);
24893175Sskamm 	(void) strcat(filename, fmri);
24903175Sskamm 	if (mkdirp(filename, 0755) == -1) {
24913175Sskamm 		/* errno is set */
24923175Sskamm 		if (errno != EEXIST)
24933175Sskamm 			return (-2);
24943175Sskamm 	}
24953175Sskamm 
24963175Sskamm 	(void) strcat(filename, "/");
24973175Sskamm 	(void) strcat(filename, name);
24983175Sskamm 
24993175Sskamm 	if (temp_filename) {
25003175Sskamm 		(void) strcpy(temp_filename, filename);
25013175Sskamm 		(void) strcat(temp_filename, "XXXXXX");
25023175Sskamm 	}
25033175Sskamm 
25043175Sskamm 	return (0);
25053175Sskamm }
25067734SDavid.Powell@sun.com 
2507*12967Sgavin.maltby@oracle.com scf_type_t
scf_true_base_type(scf_type_t type)25087734SDavid.Powell@sun.com scf_true_base_type(scf_type_t type)
25097734SDavid.Powell@sun.com {
25107734SDavid.Powell@sun.com 	scf_type_t base = type;
25117734SDavid.Powell@sun.com 
25127734SDavid.Powell@sun.com 	do {
25137734SDavid.Powell@sun.com 		type = base;
25147734SDavid.Powell@sun.com 		(void) scf_type_base_type(type, &base);
25157734SDavid.Powell@sun.com 	} while (base != type);
25167734SDavid.Powell@sun.com 
25177734SDavid.Powell@sun.com 	return (base);
25187734SDavid.Powell@sun.com }
25197734SDavid.Powell@sun.com 
25207734SDavid.Powell@sun.com /*
25217734SDavid.Powell@sun.com  * Convenience routine which frees all strings and opaque data
25227734SDavid.Powell@sun.com  * allocated by scf_read_propvec.
25237734SDavid.Powell@sun.com  *
25247734SDavid.Powell@sun.com  * Like free(3C), this function preserves the value of errno.
25257734SDavid.Powell@sun.com  */
25267734SDavid.Powell@sun.com void
scf_clean_propvec(scf_propvec_t * propvec)25277734SDavid.Powell@sun.com scf_clean_propvec(scf_propvec_t *propvec)
25287734SDavid.Powell@sun.com {
25297734SDavid.Powell@sun.com 	int saved_errno = errno;
25307734SDavid.Powell@sun.com 	scf_propvec_t *prop;
25317734SDavid.Powell@sun.com 
25327734SDavid.Powell@sun.com 	for (prop = propvec; prop->pv_prop != NULL; prop++) {
25337734SDavid.Powell@sun.com 		assert(prop->pv_type != SCF_TYPE_INVALID);
25347734SDavid.Powell@sun.com 		if (prop->pv_type == SCF_TYPE_OPAQUE) {
25357734SDavid.Powell@sun.com 			scf_opaque_t *o = prop->pv_ptr;
25367734SDavid.Powell@sun.com 
25377734SDavid.Powell@sun.com 			if (o->so_addr != NULL)
25387734SDavid.Powell@sun.com 				free(o->so_addr);
25397734SDavid.Powell@sun.com 		} else if (scf_true_base_type(prop->pv_type) ==
25407734SDavid.Powell@sun.com 		    SCF_TYPE_ASTRING) {
25417734SDavid.Powell@sun.com 			if (*(char **)prop->pv_ptr != NULL)
25427734SDavid.Powell@sun.com 				free(*(char **)prop->pv_ptr);
25437734SDavid.Powell@sun.com 		}
25447734SDavid.Powell@sun.com 	}
25457734SDavid.Powell@sun.com 
25467734SDavid.Powell@sun.com 	errno = saved_errno;
25477734SDavid.Powell@sun.com }
25487734SDavid.Powell@sun.com 
25497734SDavid.Powell@sun.com static int
count_props(scf_propvec_t * props)25507734SDavid.Powell@sun.com count_props(scf_propvec_t *props)
25517734SDavid.Powell@sun.com {
25527734SDavid.Powell@sun.com 	int count = 0;
25537734SDavid.Powell@sun.com 
25547734SDavid.Powell@sun.com 	for (; props->pv_prop != NULL; props++)
25557734SDavid.Powell@sun.com 		count++;
25567734SDavid.Powell@sun.com 	return (count);
25577734SDavid.Powell@sun.com }
25587734SDavid.Powell@sun.com 
25597734SDavid.Powell@sun.com /*
25607734SDavid.Powell@sun.com  * Reads a vector of properties from the specified fmri/property group.
25617734SDavid.Powell@sun.com  * If 'running' is true, reads from the running snapshot instead of the
25627734SDavid.Powell@sun.com  * editing snapshot.
25637734SDavid.Powell@sun.com  *
25647734SDavid.Powell@sun.com  * For string types, a buffer is allocated using malloc(3C) to hold the
25657734SDavid.Powell@sun.com  * zero-terminated string, a pointer to which is stored in the
25667734SDavid.Powell@sun.com  * caller-provided char **.  It is the caller's responsbility to free
25677734SDavid.Powell@sun.com  * this string.  To simplify error handling, unread strings are
25687734SDavid.Powell@sun.com  * initialized to NULL.
25697734SDavid.Powell@sun.com  *
25707734SDavid.Powell@sun.com  * For opaque types, a buffer is allocated using malloc(3C) to hold the
25717734SDavid.Powell@sun.com  * opaque data.  A pointer to this buffer and its size are stored in
25727734SDavid.Powell@sun.com  * the caller-provided scf_opaque_t.  It is the caller's responsibility
25737734SDavid.Powell@sun.com  * to free this buffer.  To simplify error handling, the address fields
25747734SDavid.Powell@sun.com  * for unread opaque data are initialized to NULL.
25757734SDavid.Powell@sun.com  *
25767734SDavid.Powell@sun.com  * All other data is stored directly in caller-provided variables or
25777734SDavid.Powell@sun.com  * structures.
25787734SDavid.Powell@sun.com  *
25797734SDavid.Powell@sun.com  * If this function fails to read a specific property, *badprop is set
25807734SDavid.Powell@sun.com  * to point at that property's entry in the properties array.
25817734SDavid.Powell@sun.com  *
25827734SDavid.Powell@sun.com  * On all failures, all memory allocated by this function is freed.
25837734SDavid.Powell@sun.com  */
25847734SDavid.Powell@sun.com int
scf_read_propvec(const char * fmri,const char * pgname,boolean_t running,scf_propvec_t * properties,scf_propvec_t ** badprop)25857734SDavid.Powell@sun.com scf_read_propvec(const char *fmri, const char *pgname, boolean_t running,
25867734SDavid.Powell@sun.com     scf_propvec_t *properties, scf_propvec_t **badprop)
25877734SDavid.Powell@sun.com {
2588*12967Sgavin.maltby@oracle.com 	scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
25897734SDavid.Powell@sun.com 	scf_service_t *s = scf_service_create(h);
25907734SDavid.Powell@sun.com 	scf_instance_t *i = scf_instance_create(h);
25917734SDavid.Powell@sun.com 	scf_snapshot_t *snap = running ? scf_snapshot_create(h) : NULL;
25927734SDavid.Powell@sun.com 	scf_propertygroup_t *pg = scf_pg_create(h);
25937734SDavid.Powell@sun.com 	scf_property_t *p = scf_property_create(h);
25947734SDavid.Powell@sun.com 	scf_value_t *v = scf_value_create(h);
25957734SDavid.Powell@sun.com 	boolean_t instance = B_TRUE;
25967734SDavid.Powell@sun.com 	scf_propvec_t *prop;
25977734SDavid.Powell@sun.com 	int error = 0;
25987734SDavid.Powell@sun.com 
25997734SDavid.Powell@sun.com 	if (h == NULL || s == NULL || i == NULL || (running && snap == NULL) ||
26007734SDavid.Powell@sun.com 	    pg == NULL || p == NULL || v == NULL)
26017734SDavid.Powell@sun.com 		goto scferror;
26027734SDavid.Powell@sun.com 
26037734SDavid.Powell@sun.com 	if (scf_handle_decode_fmri(h, fmri, NULL, s, i, NULL, NULL, 0) == -1)
26047734SDavid.Powell@sun.com 		goto scferror;
26057734SDavid.Powell@sun.com 
26067734SDavid.Powell@sun.com 	if (scf_instance_to_fmri(i, NULL, 0) == -1) {
26077734SDavid.Powell@sun.com 		if (scf_error() != SCF_ERROR_NOT_SET)
26087734SDavid.Powell@sun.com 			goto scferror;
26097734SDavid.Powell@sun.com 		instance = B_FALSE;
26107734SDavid.Powell@sun.com 	}
26117734SDavid.Powell@sun.com 
26127734SDavid.Powell@sun.com 	if (running) {
26137734SDavid.Powell@sun.com 		if (!instance) {
26147734SDavid.Powell@sun.com 			error = SCF_ERROR_TYPE_MISMATCH;
26157734SDavid.Powell@sun.com 			goto out;
26167734SDavid.Powell@sun.com 		}
26177734SDavid.Powell@sun.com 
26187734SDavid.Powell@sun.com 		if (scf_instance_get_snapshot(i, "running", snap) !=
26197734SDavid.Powell@sun.com 		    SCF_SUCCESS)
26207734SDavid.Powell@sun.com 			goto scferror;
26217734SDavid.Powell@sun.com 	}
26227734SDavid.Powell@sun.com 
26237734SDavid.Powell@sun.com 	if ((instance ? scf_instance_get_pg_composed(i, snap, pgname, pg) :
26247734SDavid.Powell@sun.com 	    scf_service_get_pg(s, pgname, pg)) == -1)
26257734SDavid.Powell@sun.com 		goto scferror;
26267734SDavid.Powell@sun.com 
26277734SDavid.Powell@sun.com 	for (prop = properties; prop->pv_prop != NULL; prop++) {
26287734SDavid.Powell@sun.com 		if (prop->pv_type == SCF_TYPE_OPAQUE)
26297734SDavid.Powell@sun.com 			((scf_opaque_t *)prop->pv_ptr)->so_addr = NULL;
26307734SDavid.Powell@sun.com 		else if (scf_true_base_type(prop->pv_type) == SCF_TYPE_ASTRING)
26317734SDavid.Powell@sun.com 			*((char **)prop->pv_ptr) = NULL;
26327734SDavid.Powell@sun.com 	}
26337734SDavid.Powell@sun.com 
26347734SDavid.Powell@sun.com 	for (prop = properties; prop->pv_prop != NULL; prop++) {
26357734SDavid.Powell@sun.com 		int ret = 0;
26367734SDavid.Powell@sun.com 
26377734SDavid.Powell@sun.com 		if (scf_pg_get_property(pg, prop->pv_prop, p) == -1 ||
26387734SDavid.Powell@sun.com 		    scf_property_get_value(p, v) == -1) {
26397734SDavid.Powell@sun.com 			*badprop = prop;
26407734SDavid.Powell@sun.com 			goto scferror;
26417734SDavid.Powell@sun.com 		}
26427734SDavid.Powell@sun.com 		switch (prop->pv_type) {
26437734SDavid.Powell@sun.com 		case SCF_TYPE_BOOLEAN: {
26447734SDavid.Powell@sun.com 			uint8_t b;
26457734SDavid.Powell@sun.com 
26467734SDavid.Powell@sun.com 			ret = scf_value_get_boolean(v, &b);
26477734SDavid.Powell@sun.com 			if (ret == -1)
26487734SDavid.Powell@sun.com 				break;
26497734SDavid.Powell@sun.com 			if (prop->pv_aux != 0) {
26507734SDavid.Powell@sun.com 				uint64_t *bits = prop->pv_ptr;
26517734SDavid.Powell@sun.com 				*bits = b ? (*bits | prop->pv_aux) :
26527734SDavid.Powell@sun.com 				    (*bits & ~prop->pv_aux);
26537734SDavid.Powell@sun.com 			} else {
26547734SDavid.Powell@sun.com 				boolean_t *bool = prop->pv_ptr;
26557734SDavid.Powell@sun.com 				*bool = b ? B_TRUE : B_FALSE;
26567734SDavid.Powell@sun.com 			}
26577734SDavid.Powell@sun.com 			break;
26587734SDavid.Powell@sun.com 		}
26597734SDavid.Powell@sun.com 		case SCF_TYPE_COUNT:
26607734SDavid.Powell@sun.com 			ret = scf_value_get_count(v, prop->pv_ptr);
26617734SDavid.Powell@sun.com 			break;
26627734SDavid.Powell@sun.com 		case SCF_TYPE_INTEGER:
26637734SDavid.Powell@sun.com 			ret = scf_value_get_integer(v, prop->pv_ptr);
26647734SDavid.Powell@sun.com 			break;
26657734SDavid.Powell@sun.com 		case SCF_TYPE_TIME: {
26667734SDavid.Powell@sun.com 			scf_time_t *time = prop->pv_ptr;
26677734SDavid.Powell@sun.com 
26687887SLiane.Praza@Sun.COM 			ret = scf_value_get_time(v, &time->t_seconds,
26697887SLiane.Praza@Sun.COM 			    &time->t_ns);
26707734SDavid.Powell@sun.com 			break;
26717734SDavid.Powell@sun.com 		}
26727734SDavid.Powell@sun.com 		case SCF_TYPE_OPAQUE: {
26737734SDavid.Powell@sun.com 			scf_opaque_t *opaque = prop->pv_ptr;
26747734SDavid.Powell@sun.com 			ssize_t size = scf_value_get_opaque(v, NULL, 0);
26757734SDavid.Powell@sun.com 
26767734SDavid.Powell@sun.com 			if (size == -1) {
26777734SDavid.Powell@sun.com 				*badprop = prop;
26787734SDavid.Powell@sun.com 				goto scferror;
26797734SDavid.Powell@sun.com 			}
26807734SDavid.Powell@sun.com 			if ((opaque->so_addr = malloc(size)) == NULL) {
26817734SDavid.Powell@sun.com 				error = SCF_ERROR_NO_MEMORY;
26827734SDavid.Powell@sun.com 				goto out;
26837734SDavid.Powell@sun.com 			}
26847734SDavid.Powell@sun.com 			opaque->so_size = size;
26857734SDavid.Powell@sun.com 			ret = scf_value_get_opaque(v, opaque->so_addr, size);
26867734SDavid.Powell@sun.com 			break;
26877734SDavid.Powell@sun.com 		}
26887734SDavid.Powell@sun.com 		default: {
26897734SDavid.Powell@sun.com 			char *s;
26907734SDavid.Powell@sun.com 			ssize_t size;
26917734SDavid.Powell@sun.com 
26927734SDavid.Powell@sun.com 			assert(scf_true_base_type(prop->pv_type) ==
26937734SDavid.Powell@sun.com 			    SCF_TYPE_ASTRING);
26947734SDavid.Powell@sun.com 
26957734SDavid.Powell@sun.com 			size = scf_value_get_astring(v, NULL, 0);
26967734SDavid.Powell@sun.com 			if (size == -1) {
26977734SDavid.Powell@sun.com 				*badprop = prop;
26987734SDavid.Powell@sun.com 				goto scferror;
26997734SDavid.Powell@sun.com 			}
27007734SDavid.Powell@sun.com 			if ((s = malloc(++size)) == NULL) {
27017734SDavid.Powell@sun.com 				error = SCF_ERROR_NO_MEMORY;
27027734SDavid.Powell@sun.com 				goto out;
27037734SDavid.Powell@sun.com 			}
27047734SDavid.Powell@sun.com 			ret = scf_value_get_astring(v, s, size);
27057734SDavid.Powell@sun.com 			*(char **)prop->pv_ptr = s;
27067734SDavid.Powell@sun.com 		}
27077734SDavid.Powell@sun.com 
27087734SDavid.Powell@sun.com 		if (ret == -1) {
27097734SDavid.Powell@sun.com 			*badprop = prop;
27107734SDavid.Powell@sun.com 			goto scferror;
27117734SDavid.Powell@sun.com 		}
27127734SDavid.Powell@sun.com 
27137734SDavid.Powell@sun.com 		}
27147734SDavid.Powell@sun.com 	}
27157734SDavid.Powell@sun.com 
27167734SDavid.Powell@sun.com 	goto out;
27177734SDavid.Powell@sun.com 
27187734SDavid.Powell@sun.com scferror:
27197734SDavid.Powell@sun.com 	error = scf_error();
27207734SDavid.Powell@sun.com 	scf_clean_propvec(properties);
27217734SDavid.Powell@sun.com 
27227734SDavid.Powell@sun.com out:
272312466SJan.Friedel@Sun.COM 	scf_value_destroy(v);
272412466SJan.Friedel@Sun.COM 	scf_property_destroy(p);
27257734SDavid.Powell@sun.com 	scf_pg_destroy(pg);
27267734SDavid.Powell@sun.com 	scf_snapshot_destroy(snap);
27277734SDavid.Powell@sun.com 	scf_instance_destroy(i);
27287734SDavid.Powell@sun.com 	scf_service_destroy(s);
27297734SDavid.Powell@sun.com 	scf_handle_destroy(h);
27307734SDavid.Powell@sun.com 
27317734SDavid.Powell@sun.com 	if (error != 0) {
27327734SDavid.Powell@sun.com 		(void) scf_set_error(error);
27337734SDavid.Powell@sun.com 		return (SCF_FAILED);
27347734SDavid.Powell@sun.com 	}
27357734SDavid.Powell@sun.com 
27367734SDavid.Powell@sun.com 	return (SCF_SUCCESS);
27377734SDavid.Powell@sun.com }
27387734SDavid.Powell@sun.com 
27397734SDavid.Powell@sun.com /*
27407734SDavid.Powell@sun.com  * Writes a vector of properties to the specified fmri/property group.
27417734SDavid.Powell@sun.com  *
27427734SDavid.Powell@sun.com  * If this function fails to write a specific property, *badprop is set
27437734SDavid.Powell@sun.com  * to point at that property's entry in the properties array.
27447734SDavid.Powell@sun.com  *
27457734SDavid.Powell@sun.com  * One significant difference between this function and the
27467734SDavid.Powell@sun.com  * scf_read_propvec function is that for string types, pv_ptr is a
27477734SDavid.Powell@sun.com  * char *, not a char **.  This means that you can't write a propvec
27487734SDavid.Powell@sun.com  * you just read, but makes other uses (hopefully the majority) simpler.
27497734SDavid.Powell@sun.com  */
27507734SDavid.Powell@sun.com int
scf_write_propvec(const char * fmri,const char * pgname,scf_propvec_t * properties,scf_propvec_t ** badprop)27517734SDavid.Powell@sun.com scf_write_propvec(const char *fmri, const char *pgname,
27527734SDavid.Powell@sun.com     scf_propvec_t *properties, scf_propvec_t **badprop)
27537734SDavid.Powell@sun.com {
2754*12967Sgavin.maltby@oracle.com 	scf_handle_t *h = _scf_handle_create_and_bind(SCF_VERSION);
27557734SDavid.Powell@sun.com 	scf_service_t *s = scf_service_create(h);
27567734SDavid.Powell@sun.com 	scf_instance_t *inst = scf_instance_create(h);
27577734SDavid.Powell@sun.com 	scf_snapshot_t *snap = scf_snapshot_create(h);
27587734SDavid.Powell@sun.com 	scf_propertygroup_t *pg = scf_pg_create(h);
27597734SDavid.Powell@sun.com 	scf_property_t *p = scf_property_create(h);
27607734SDavid.Powell@sun.com 	scf_transaction_t *tx = scf_transaction_create(h);
27617734SDavid.Powell@sun.com 	scf_value_t **v = NULL;
27627734SDavid.Powell@sun.com 	scf_transaction_entry_t **e = NULL;
27637734SDavid.Powell@sun.com 	boolean_t instance = B_TRUE;
27647734SDavid.Powell@sun.com 	int i, n;
27657734SDavid.Powell@sun.com 	scf_propvec_t *prop;
27667734SDavid.Powell@sun.com 	int error = 0, ret;
27677734SDavid.Powell@sun.com 
27687734SDavid.Powell@sun.com 	n = count_props(properties);
27697734SDavid.Powell@sun.com 	v = calloc(n, sizeof (scf_value_t *));
27707734SDavid.Powell@sun.com 	e = calloc(n, sizeof (scf_transaction_entry_t *));
27717734SDavid.Powell@sun.com 
27727734SDavid.Powell@sun.com 	if (v == NULL || e == NULL) {
27737734SDavid.Powell@sun.com 		error = SCF_ERROR_NO_MEMORY;
27747734SDavid.Powell@sun.com 		goto out;
27757734SDavid.Powell@sun.com 	}
27767734SDavid.Powell@sun.com 
27777734SDavid.Powell@sun.com 	if (h == NULL || s == NULL || inst == NULL || pg == NULL || p == NULL ||
27787734SDavid.Powell@sun.com 	    tx == NULL)
27797734SDavid.Powell@sun.com 		goto scferror;
27807734SDavid.Powell@sun.com 
27817734SDavid.Powell@sun.com 	for (i = 0; i < n; i++) {
27827734SDavid.Powell@sun.com 		v[i] = scf_value_create(h);
27837734SDavid.Powell@sun.com 		e[i] = scf_entry_create(h);
27847734SDavid.Powell@sun.com 		if (v[i] == NULL || e[i] == NULL)
27857734SDavid.Powell@sun.com 			goto scferror;
27867734SDavid.Powell@sun.com 	}
27877734SDavid.Powell@sun.com 
27887734SDavid.Powell@sun.com 	if (scf_handle_decode_fmri(h, fmri, NULL, s, inst, NULL, NULL, 0)
27897734SDavid.Powell@sun.com 	    != SCF_SUCCESS)
27907734SDavid.Powell@sun.com 		goto scferror;
27917734SDavid.Powell@sun.com 
27927734SDavid.Powell@sun.com 	if (scf_instance_to_fmri(inst, NULL, 0) == -1) {
27937734SDavid.Powell@sun.com 		if (scf_error() != SCF_ERROR_NOT_SET)
27947734SDavid.Powell@sun.com 			goto scferror;
27957734SDavid.Powell@sun.com 		instance = B_FALSE;
27967734SDavid.Powell@sun.com 	}
27977734SDavid.Powell@sun.com 
27987734SDavid.Powell@sun.com 	if ((instance ? scf_instance_get_pg(inst, pgname, pg) :
27997734SDavid.Powell@sun.com 	    scf_service_get_pg(s, pgname, pg)) == -1)
28007734SDavid.Powell@sun.com 		goto scferror;
28017734SDavid.Powell@sun.com 
28027734SDavid.Powell@sun.com top:
28037734SDavid.Powell@sun.com 	if (scf_transaction_start(tx, pg) == -1)
28047734SDavid.Powell@sun.com 		goto scferror;
28057734SDavid.Powell@sun.com 
28067734SDavid.Powell@sun.com 	for (prop = properties, i = 0; prop->pv_prop != NULL; prop++, i++) {
28077734SDavid.Powell@sun.com 		ret = scf_transaction_property_change(tx, e[i], prop->pv_prop,
28087734SDavid.Powell@sun.com 		    prop->pv_type);
28097734SDavid.Powell@sun.com 		if (ret == -1 && scf_error() == SCF_ERROR_NOT_FOUND)
28107734SDavid.Powell@sun.com 			ret = scf_transaction_property_new(tx, e[i],
28117734SDavid.Powell@sun.com 			    prop->pv_prop, prop->pv_type);
28127734SDavid.Powell@sun.com 
28137734SDavid.Powell@sun.com 		if (ret == -1) {
28147734SDavid.Powell@sun.com 			*badprop = prop;
28157734SDavid.Powell@sun.com 			goto scferror;
28167734SDavid.Powell@sun.com 		}
28177734SDavid.Powell@sun.com 
28187734SDavid.Powell@sun.com 		switch (prop->pv_type) {
28197734SDavid.Powell@sun.com 		case SCF_TYPE_BOOLEAN: {
28207734SDavid.Powell@sun.com 			boolean_t b = (prop->pv_aux != 0) ?
28217734SDavid.Powell@sun.com 			    (*(uint64_t *)prop->pv_ptr & prop->pv_aux) != 0 :
28227734SDavid.Powell@sun.com 			    *(boolean_t *)prop->pv_ptr;
28237734SDavid.Powell@sun.com 
28247734SDavid.Powell@sun.com 			scf_value_set_boolean(v[i], b ? 1 : 0);
28257734SDavid.Powell@sun.com 			break;
28267734SDavid.Powell@sun.com 		}
28277734SDavid.Powell@sun.com 		case SCF_TYPE_COUNT:
28287734SDavid.Powell@sun.com 			scf_value_set_count(v[i], *(uint64_t *)prop->pv_ptr);
28297734SDavid.Powell@sun.com 			break;
28307734SDavid.Powell@sun.com 		case SCF_TYPE_INTEGER:
28317734SDavid.Powell@sun.com 			scf_value_set_integer(v[i], *(int64_t *)prop->pv_ptr);
28327734SDavid.Powell@sun.com 			break;
28337734SDavid.Powell@sun.com 		case SCF_TYPE_TIME: {
28347734SDavid.Powell@sun.com 			scf_time_t *time = prop->pv_ptr;
28357734SDavid.Powell@sun.com 
28367887SLiane.Praza@Sun.COM 			ret = scf_value_set_time(v[i], time->t_seconds,
28377887SLiane.Praza@Sun.COM 			    time->t_ns);
28387734SDavid.Powell@sun.com 			break;
28397734SDavid.Powell@sun.com 		}
28407734SDavid.Powell@sun.com 		case SCF_TYPE_OPAQUE: {
28417734SDavid.Powell@sun.com 			scf_opaque_t *opaque = prop->pv_ptr;
28427734SDavid.Powell@sun.com 
28437734SDavid.Powell@sun.com 			ret = scf_value_set_opaque(v[i], opaque->so_addr,
28447734SDavid.Powell@sun.com 			    opaque->so_size);
28457734SDavid.Powell@sun.com 			break;
28467734SDavid.Powell@sun.com 		}
28477734SDavid.Powell@sun.com 		case SCF_TYPE_ASTRING:
28487734SDavid.Powell@sun.com 			ret = scf_value_set_astring(v[i],
28497734SDavid.Powell@sun.com 			    (const char *)prop->pv_ptr);
28507734SDavid.Powell@sun.com 			break;
28517734SDavid.Powell@sun.com 		default:
28527734SDavid.Powell@sun.com 			ret = scf_value_set_from_string(v[i], prop->pv_type,
28537734SDavid.Powell@sun.com 			    (const char *)prop->pv_ptr);
28547734SDavid.Powell@sun.com 		}
28557734SDavid.Powell@sun.com 
28567734SDavid.Powell@sun.com 		if (ret == -1 || scf_entry_add_value(e[i], v[i]) == -1) {
28577734SDavid.Powell@sun.com 			*badprop = prop;
28587734SDavid.Powell@sun.com 			goto scferror;
28597734SDavid.Powell@sun.com 		}
28607734SDavid.Powell@sun.com 	}
28617734SDavid.Powell@sun.com 
28627734SDavid.Powell@sun.com 	ret = scf_transaction_commit(tx);
28637734SDavid.Powell@sun.com 	if (ret == 1)
28647734SDavid.Powell@sun.com 		goto out;
28657734SDavid.Powell@sun.com 
28667734SDavid.Powell@sun.com 	if (ret == 0 && scf_pg_update(pg) != -1) {
28677734SDavid.Powell@sun.com 		scf_transaction_reset(tx);
28687734SDavid.Powell@sun.com 		goto top;
28697734SDavid.Powell@sun.com 	}
28707734SDavid.Powell@sun.com 
28717734SDavid.Powell@sun.com scferror:
28727734SDavid.Powell@sun.com 	error = scf_error();
28737734SDavid.Powell@sun.com 
28747734SDavid.Powell@sun.com out:
28757734SDavid.Powell@sun.com 	if (v != NULL) {
28767734SDavid.Powell@sun.com 		for (i = 0; i < n; i++)
28777734SDavid.Powell@sun.com 			scf_value_destroy(v[i]);
28787734SDavid.Powell@sun.com 		free(v);
28797734SDavid.Powell@sun.com 	}
28807734SDavid.Powell@sun.com 
28817734SDavid.Powell@sun.com 	if (e != NULL) {
28827734SDavid.Powell@sun.com 		for (i = 0; i < n; i++)
28837734SDavid.Powell@sun.com 			scf_entry_destroy(e[i]);
28847734SDavid.Powell@sun.com 		free(e);
28857734SDavid.Powell@sun.com 	}
28867734SDavid.Powell@sun.com 
28877734SDavid.Powell@sun.com 	scf_transaction_destroy(tx);
28887734SDavid.Powell@sun.com 	scf_property_destroy(p);
28897734SDavid.Powell@sun.com 	scf_pg_destroy(pg);
28907734SDavid.Powell@sun.com 	scf_snapshot_destroy(snap);
28917734SDavid.Powell@sun.com 	scf_instance_destroy(inst);
28927734SDavid.Powell@sun.com 	scf_service_destroy(s);
28937734SDavid.Powell@sun.com 	scf_handle_destroy(h);
28947734SDavid.Powell@sun.com 
28957734SDavid.Powell@sun.com 	if (error != 0) {
28967734SDavid.Powell@sun.com 		(void) scf_set_error(error);
28977734SDavid.Powell@sun.com 		return (SCF_FAILED);
28987734SDavid.Powell@sun.com 	}
28997734SDavid.Powell@sun.com 
29007734SDavid.Powell@sun.com 	return (SCF_SUCCESS);
29017734SDavid.Powell@sun.com }
29028823STruong.Q.Nguyen@Sun.COM 
29038823STruong.Q.Nguyen@Sun.COM /*
29048823STruong.Q.Nguyen@Sun.COM  * Returns
29058823STruong.Q.Nguyen@Sun.COM  *   0 - success
29068823STruong.Q.Nguyen@Sun.COM  *   ECONNABORTED - repository connection broken
29078823STruong.Q.Nguyen@Sun.COM  *   ECANCELED - inst was deleted
29088823STruong.Q.Nguyen@Sun.COM  *   EPERM
29098823STruong.Q.Nguyen@Sun.COM  *   EACCES
29108823STruong.Q.Nguyen@Sun.COM  *   EROFS
29118823STruong.Q.Nguyen@Sun.COM  *   ENOMEM
29128823STruong.Q.Nguyen@Sun.COM  */
29138823STruong.Q.Nguyen@Sun.COM int
scf_instance_delete_prop(scf_instance_t * inst,const char * pgname,const char * pname)29148823STruong.Q.Nguyen@Sun.COM scf_instance_delete_prop(scf_instance_t *inst, const char *pgname,
29158823STruong.Q.Nguyen@Sun.COM     const char *pname)
29168823STruong.Q.Nguyen@Sun.COM {
29178823STruong.Q.Nguyen@Sun.COM 	scf_handle_t *h;
29188823STruong.Q.Nguyen@Sun.COM 	scf_propertygroup_t *pg;
29198823STruong.Q.Nguyen@Sun.COM 	scf_transaction_t *tx;
29208823STruong.Q.Nguyen@Sun.COM 	scf_transaction_entry_t *e;
29218823STruong.Q.Nguyen@Sun.COM 	int error = 0, ret = 1, r;
29228823STruong.Q.Nguyen@Sun.COM 
29238823STruong.Q.Nguyen@Sun.COM 	h = scf_instance_handle(inst);
29248823STruong.Q.Nguyen@Sun.COM 
29258823STruong.Q.Nguyen@Sun.COM 	if ((pg = scf_pg_create(h)) == NULL) {
29268823STruong.Q.Nguyen@Sun.COM 		return (ENOMEM);
29278823STruong.Q.Nguyen@Sun.COM 	}
29288823STruong.Q.Nguyen@Sun.COM 
29298823STruong.Q.Nguyen@Sun.COM 	if (scf_instance_get_pg(inst, pgname, pg) != 0) {
29308823STruong.Q.Nguyen@Sun.COM 		error = scf_error();
29318823STruong.Q.Nguyen@Sun.COM 		scf_pg_destroy(pg);
29328823STruong.Q.Nguyen@Sun.COM 		switch (error) {
29338823STruong.Q.Nguyen@Sun.COM 		case SCF_ERROR_NOT_FOUND:
29348823STruong.Q.Nguyen@Sun.COM 			return (SCF_SUCCESS);
29358823STruong.Q.Nguyen@Sun.COM 
29368823STruong.Q.Nguyen@Sun.COM 		case SCF_ERROR_DELETED:
29378823STruong.Q.Nguyen@Sun.COM 			return (ECANCELED);
29388823STruong.Q.Nguyen@Sun.COM 
29398823STruong.Q.Nguyen@Sun.COM 		case SCF_ERROR_CONNECTION_BROKEN:
29408823STruong.Q.Nguyen@Sun.COM 		default:
29418823STruong.Q.Nguyen@Sun.COM 			return (ECONNABORTED);
29428823STruong.Q.Nguyen@Sun.COM 
29438823STruong.Q.Nguyen@Sun.COM 		case SCF_ERROR_NOT_SET:
29448823STruong.Q.Nguyen@Sun.COM 			bad_error("scf_instance_get_pg", scf_error());
29458823STruong.Q.Nguyen@Sun.COM 		}
29468823STruong.Q.Nguyen@Sun.COM 	}
29478823STruong.Q.Nguyen@Sun.COM 
29488823STruong.Q.Nguyen@Sun.COM 	tx = scf_transaction_create(h);
29498823STruong.Q.Nguyen@Sun.COM 	e = scf_entry_create(h);
29508823STruong.Q.Nguyen@Sun.COM 	if (tx == NULL || e == NULL) {
29518823STruong.Q.Nguyen@Sun.COM 		ret = ENOMEM;
29528823STruong.Q.Nguyen@Sun.COM 		goto out;
29538823STruong.Q.Nguyen@Sun.COM 	}
29548823STruong.Q.Nguyen@Sun.COM 
29558823STruong.Q.Nguyen@Sun.COM 	for (;;) {
29568823STruong.Q.Nguyen@Sun.COM 		if (scf_transaction_start(tx, pg) != 0) {
29578823STruong.Q.Nguyen@Sun.COM 			goto scferror;
29588823STruong.Q.Nguyen@Sun.COM 		}
29598823STruong.Q.Nguyen@Sun.COM 
29608823STruong.Q.Nguyen@Sun.COM 		if (scf_transaction_property_delete(tx, e, pname) != 0) {
29618823STruong.Q.Nguyen@Sun.COM 			goto scferror;
29628823STruong.Q.Nguyen@Sun.COM 		}
29638823STruong.Q.Nguyen@Sun.COM 
29648823STruong.Q.Nguyen@Sun.COM 		if ((r = scf_transaction_commit(tx)) == 1) {
29658823STruong.Q.Nguyen@Sun.COM 			ret = 0;
29668823STruong.Q.Nguyen@Sun.COM 			goto out;
29678823STruong.Q.Nguyen@Sun.COM 		}
29688823STruong.Q.Nguyen@Sun.COM 
29698823STruong.Q.Nguyen@Sun.COM 		if (r == -1) {
29708823STruong.Q.Nguyen@Sun.COM 			goto scferror;
29718823STruong.Q.Nguyen@Sun.COM 		}
29728823STruong.Q.Nguyen@Sun.COM 
29738823STruong.Q.Nguyen@Sun.COM 		scf_transaction_reset(tx);
29748823STruong.Q.Nguyen@Sun.COM 		if (scf_pg_update(pg) == -1) {
29758823STruong.Q.Nguyen@Sun.COM 			goto scferror;
29768823STruong.Q.Nguyen@Sun.COM 		}
29778823STruong.Q.Nguyen@Sun.COM 	}
29788823STruong.Q.Nguyen@Sun.COM 
29798823STruong.Q.Nguyen@Sun.COM scferror:
29808823STruong.Q.Nguyen@Sun.COM 	switch (scf_error()) {
29818823STruong.Q.Nguyen@Sun.COM 	case SCF_ERROR_DELETED:
29828823STruong.Q.Nguyen@Sun.COM 	case SCF_ERROR_NOT_FOUND:
29838823STruong.Q.Nguyen@Sun.COM 		ret = 0;
29848823STruong.Q.Nguyen@Sun.COM 		break;
29858823STruong.Q.Nguyen@Sun.COM 
29868823STruong.Q.Nguyen@Sun.COM 	case SCF_ERROR_PERMISSION_DENIED:
29878823STruong.Q.Nguyen@Sun.COM 		ret = EPERM;
29888823STruong.Q.Nguyen@Sun.COM 		break;
29898823STruong.Q.Nguyen@Sun.COM 
29908823STruong.Q.Nguyen@Sun.COM 	case SCF_ERROR_BACKEND_ACCESS:
29918823STruong.Q.Nguyen@Sun.COM 		ret = EACCES;
29928823STruong.Q.Nguyen@Sun.COM 		break;
29938823STruong.Q.Nguyen@Sun.COM 
29948823STruong.Q.Nguyen@Sun.COM 	case SCF_ERROR_BACKEND_READONLY:
29958823STruong.Q.Nguyen@Sun.COM 		ret = EROFS;
29968823STruong.Q.Nguyen@Sun.COM 		break;
29978823STruong.Q.Nguyen@Sun.COM 
29988823STruong.Q.Nguyen@Sun.COM 	case SCF_ERROR_CONNECTION_BROKEN:
29998823STruong.Q.Nguyen@Sun.COM 	default:
30008823STruong.Q.Nguyen@Sun.COM 		ret = ECONNABORTED;
30018823STruong.Q.Nguyen@Sun.COM 		break;
30028823STruong.Q.Nguyen@Sun.COM 
30038823STruong.Q.Nguyen@Sun.COM 	case SCF_ERROR_HANDLE_MISMATCH:
30048823STruong.Q.Nguyen@Sun.COM 	case SCF_ERROR_INVALID_ARGUMENT:
30058823STruong.Q.Nguyen@Sun.COM 	case SCF_ERROR_NOT_BOUND:
30068823STruong.Q.Nguyen@Sun.COM 	case SCF_ERROR_NOT_SET:
30078823STruong.Q.Nguyen@Sun.COM 		bad_error("scf_instance_delete_prop", scf_error());
30088823STruong.Q.Nguyen@Sun.COM 	}
30098823STruong.Q.Nguyen@Sun.COM 
30108823STruong.Q.Nguyen@Sun.COM out:
30118823STruong.Q.Nguyen@Sun.COM 	scf_transaction_destroy(tx);
30128823STruong.Q.Nguyen@Sun.COM 	scf_entry_destroy(e);
30138823STruong.Q.Nguyen@Sun.COM 	scf_pg_destroy(pg);
30148823STruong.Q.Nguyen@Sun.COM 
30158823STruong.Q.Nguyen@Sun.COM 	return (ret);
30168823STruong.Q.Nguyen@Sun.COM }
301712483SAntonello.Cruz@Sun.COM 
301812483SAntonello.Cruz@Sun.COM /*
301912483SAntonello.Cruz@Sun.COM  * Check the "application/auto_enable" property for the passed FMRI.
302012483SAntonello.Cruz@Sun.COM  * scf_simple_prop_get() should find the property on an instance
302112483SAntonello.Cruz@Sun.COM  * or on the service FMRI.  The routine returns:
302212483SAntonello.Cruz@Sun.COM  * -1: inconclusive (likely no such property or FMRI)
302312483SAntonello.Cruz@Sun.COM  *  0: auto_enable is false
302412483SAntonello.Cruz@Sun.COM  *  1: auto_enable is true
302512483SAntonello.Cruz@Sun.COM  */
302612483SAntonello.Cruz@Sun.COM static int
is_auto_enabled(char * fmri)302712483SAntonello.Cruz@Sun.COM is_auto_enabled(char *fmri)
302812483SAntonello.Cruz@Sun.COM {
302912483SAntonello.Cruz@Sun.COM 	scf_simple_prop_t *prop;
303012483SAntonello.Cruz@Sun.COM 	int retval = -1;
303112483SAntonello.Cruz@Sun.COM 	uint8_t *ret;
303212483SAntonello.Cruz@Sun.COM 
303312483SAntonello.Cruz@Sun.COM 	prop = scf_simple_prop_get(NULL, fmri, SCF_GROUP_APPLICATION,
303412483SAntonello.Cruz@Sun.COM 	    "auto_enable");
303512483SAntonello.Cruz@Sun.COM 	if (!prop)
303612483SAntonello.Cruz@Sun.COM 		return (retval);
303712483SAntonello.Cruz@Sun.COM 	ret = scf_simple_prop_next_boolean(prop);
303812483SAntonello.Cruz@Sun.COM 	retval = (*ret != 0);
303912483SAntonello.Cruz@Sun.COM 	scf_simple_prop_free(prop);
304012483SAntonello.Cruz@Sun.COM 	return (retval);
304112483SAntonello.Cruz@Sun.COM }
304212483SAntonello.Cruz@Sun.COM 
304312483SAntonello.Cruz@Sun.COM /*
304412483SAntonello.Cruz@Sun.COM  * Check an array of services and enable any that don't have the
304512483SAntonello.Cruz@Sun.COM  * "application/auto_enable" property set to "false", which is
304612483SAntonello.Cruz@Sun.COM  * the interface to turn off this behaviour (see PSARC 2004/739).
304712483SAntonello.Cruz@Sun.COM  */
304812483SAntonello.Cruz@Sun.COM void
_check_services(char ** svcs)304912483SAntonello.Cruz@Sun.COM _check_services(char **svcs)
305012483SAntonello.Cruz@Sun.COM {
305112483SAntonello.Cruz@Sun.COM 	char *s;
305212483SAntonello.Cruz@Sun.COM 
305312483SAntonello.Cruz@Sun.COM 	for (; *svcs; svcs++) {
305412483SAntonello.Cruz@Sun.COM 		if (is_auto_enabled(*svcs) == 0)
305512483SAntonello.Cruz@Sun.COM 			continue;
305612483SAntonello.Cruz@Sun.COM 		if ((s = smf_get_state(*svcs)) != NULL) {
305712483SAntonello.Cruz@Sun.COM 			if (strcmp(SCF_STATE_STRING_DISABLED, s) == 0)
305812483SAntonello.Cruz@Sun.COM 				(void) smf_enable_instance(*svcs,
305912483SAntonello.Cruz@Sun.COM 				    SMF_TEMPORARY);
306012483SAntonello.Cruz@Sun.COM 			free(s);
306112483SAntonello.Cruz@Sun.COM 		}
306212483SAntonello.Cruz@Sun.COM 	}
306312483SAntonello.Cruz@Sun.COM }
3064*12967Sgavin.maltby@oracle.com 
3065*12967Sgavin.maltby@oracle.com /*ARGSUSED*/
3066*12967Sgavin.maltby@oracle.com static int
str_compare(const char * s1,const char * s2,size_t n)3067*12967Sgavin.maltby@oracle.com str_compare(const char *s1, const char *s2, size_t n)
3068*12967Sgavin.maltby@oracle.com {
3069*12967Sgavin.maltby@oracle.com 	return (strcmp(s1, s2));
3070*12967Sgavin.maltby@oracle.com }
3071*12967Sgavin.maltby@oracle.com 
3072*12967Sgavin.maltby@oracle.com static int
str_n_compare(const char * s1,const char * s2,size_t n)3073*12967Sgavin.maltby@oracle.com str_n_compare(const char *s1, const char *s2, size_t n)
3074*12967Sgavin.maltby@oracle.com {
3075*12967Sgavin.maltby@oracle.com 	return (strncmp(s1, s2, n));
3076*12967Sgavin.maltby@oracle.com }
3077*12967Sgavin.maltby@oracle.com 
3078*12967Sgavin.maltby@oracle.com int32_t
state_from_string(const char * state,size_t l)3079*12967Sgavin.maltby@oracle.com state_from_string(const char *state, size_t l)
3080*12967Sgavin.maltby@oracle.com {
3081*12967Sgavin.maltby@oracle.com 	int (*str_cmp)(const char *, const char *, size_t);
3082*12967Sgavin.maltby@oracle.com 
3083*12967Sgavin.maltby@oracle.com 	if (l == 0)
3084*12967Sgavin.maltby@oracle.com 		str_cmp = str_compare;
3085*12967Sgavin.maltby@oracle.com 	else
3086*12967Sgavin.maltby@oracle.com 		str_cmp = str_n_compare;
3087*12967Sgavin.maltby@oracle.com 
3088*12967Sgavin.maltby@oracle.com 	if (str_cmp(SCF_STATE_STRING_UNINIT, state, l) == 0)
3089*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_UNINIT);
3090*12967Sgavin.maltby@oracle.com 	else if (str_cmp(SCF_STATE_STRING_MAINT, state, l) == 0)
3091*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_MAINT);
3092*12967Sgavin.maltby@oracle.com 	else if (str_cmp(SCF_STATE_STRING_OFFLINE, state, l) == 0)
3093*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_OFFLINE);
3094*12967Sgavin.maltby@oracle.com 	else if (str_cmp(SCF_STATE_STRING_DISABLED, state, l) == 0)
3095*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_DISABLED);
3096*12967Sgavin.maltby@oracle.com 	else if (str_cmp(SCF_STATE_STRING_ONLINE, state, l) == 0)
3097*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_ONLINE);
3098*12967Sgavin.maltby@oracle.com 	else if (str_cmp(SCF_STATE_STRING_DEGRADED, state, l) == 0)
3099*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_DEGRADED);
3100*12967Sgavin.maltby@oracle.com 	else if (str_cmp("all", state, l) == 0)
3101*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_ALL);
3102*12967Sgavin.maltby@oracle.com 	else
3103*12967Sgavin.maltby@oracle.com 		return (-1);
3104*12967Sgavin.maltby@oracle.com }
3105*12967Sgavin.maltby@oracle.com 
3106*12967Sgavin.maltby@oracle.com /*
3107*12967Sgavin.maltby@oracle.com  * int32_t smf_state_from_string()
3108*12967Sgavin.maltby@oracle.com  * return the value of the macro SCF_STATE_* for the corresponding state
3109*12967Sgavin.maltby@oracle.com  * it returns SCF_STATE_ALL if "all" is passed. -1 if the string passed doesn't
3110*12967Sgavin.maltby@oracle.com  * correspond to any valid state.
3111*12967Sgavin.maltby@oracle.com  */
3112*12967Sgavin.maltby@oracle.com int32_t
smf_state_from_string(const char * state)3113*12967Sgavin.maltby@oracle.com smf_state_from_string(const char *state)
3114*12967Sgavin.maltby@oracle.com {
3115*12967Sgavin.maltby@oracle.com 	return (state_from_string(state, 0));
3116*12967Sgavin.maltby@oracle.com }
3117*12967Sgavin.maltby@oracle.com 
3118*12967Sgavin.maltby@oracle.com /*
3119*12967Sgavin.maltby@oracle.com  * smf_state_to_string()
3120*12967Sgavin.maltby@oracle.com  * Takes an int32_t representing an SMF state and returns
3121*12967Sgavin.maltby@oracle.com  * the corresponding string. The string is read only and need not to be
3122*12967Sgavin.maltby@oracle.com  * freed.
3123*12967Sgavin.maltby@oracle.com  * returns NULL on invalid input.
3124*12967Sgavin.maltby@oracle.com  */
3125*12967Sgavin.maltby@oracle.com const char *
smf_state_to_string(int32_t s)3126*12967Sgavin.maltby@oracle.com smf_state_to_string(int32_t s)
3127*12967Sgavin.maltby@oracle.com {
3128*12967Sgavin.maltby@oracle.com 	switch (s) {
3129*12967Sgavin.maltby@oracle.com 	case SCF_STATE_UNINIT:
3130*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_STRING_UNINIT);
3131*12967Sgavin.maltby@oracle.com 	case SCF_STATE_MAINT:
3132*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_STRING_MAINT);
3133*12967Sgavin.maltby@oracle.com 	case SCF_STATE_OFFLINE:
3134*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_STRING_OFFLINE);
3135*12967Sgavin.maltby@oracle.com 	case SCF_STATE_DISABLED:
3136*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_STRING_DISABLED);
3137*12967Sgavin.maltby@oracle.com 	case SCF_STATE_ONLINE:
3138*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_STRING_ONLINE);
3139*12967Sgavin.maltby@oracle.com 	case SCF_STATE_DEGRADED:
3140*12967Sgavin.maltby@oracle.com 		return (SCF_STATE_STRING_DEGRADED);
3141*12967Sgavin.maltby@oracle.com 	case SCF_STATE_ALL:
3142*12967Sgavin.maltby@oracle.com 		return ("all");
3143*12967Sgavin.maltby@oracle.com 	default:
3144*12967Sgavin.maltby@oracle.com 		return (NULL);
3145*12967Sgavin.maltby@oracle.com 	}
3146*12967Sgavin.maltby@oracle.com }
3147