xref: /onnv-gate/usr/src/cmd/svc/startd/libscf.c (revision 8554:b4fdbe67b54c)
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
52682Srm88369  * Common Development and Distribution License (the "License").
62682Srm88369  * 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 /*
23*8554SSean.Wilcox@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/contract/process.h>
290Sstevel@tonic-gate #include <assert.h>
300Sstevel@tonic-gate #include <errno.h>
310Sstevel@tonic-gate #include <libscf.h>
320Sstevel@tonic-gate #include <libscf_priv.h>
330Sstevel@tonic-gate #include <poll.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <unistd.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "startd.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #define	SMF_SNAPSHOT_RUNNING	"running"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate char *
430Sstevel@tonic-gate inst_fmri_to_svc_fmri(const char *fmri)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	char *buf, *sfmri;
460Sstevel@tonic-gate 	const char *scope, *svc;
470Sstevel@tonic-gate 	int r;
480Sstevel@tonic-gate 	boolean_t local;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
510Sstevel@tonic-gate 	sfmri = startd_alloc(max_scf_fmri_size);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	(void) strcpy(buf, fmri);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	r = scf_parse_svc_fmri(buf, &scope, &svc, NULL, NULL, NULL);
560Sstevel@tonic-gate 	assert(r == 0);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	local = strcmp(scope, SCF_SCOPE_LOCAL) == 0;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	(void) snprintf(sfmri, max_scf_fmri_size, "svc:%s%s/%s",
610Sstevel@tonic-gate 	    local ? "" : "//", local ? "" : scope, svc);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	return (sfmri);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * Wrapper for the scf_*_create() functions.  On SCF_ERROR_NO_MEMORY and
700Sstevel@tonic-gate  * SCF_ERROR_NO_RESOURCES, retries or dies.  So this can only fail with
710Sstevel@tonic-gate  * SCF_ERROR_INVALID_ARGUMENT, if h is NULL.
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate void *
740Sstevel@tonic-gate libscf_object_create(void *f(scf_handle_t *), scf_handle_t *h)
750Sstevel@tonic-gate {
760Sstevel@tonic-gate 	void *o;
770Sstevel@tonic-gate 	uint_t try, msecs;
780Sstevel@tonic-gate 	scf_error_t err;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	o = f(h);
810Sstevel@tonic-gate 	if (o != NULL)
820Sstevel@tonic-gate 		return (o);
830Sstevel@tonic-gate 	err = scf_error();
840Sstevel@tonic-gate 	if (err != SCF_ERROR_NO_MEMORY && err != SCF_ERROR_NO_RESOURCES)
850Sstevel@tonic-gate 		return (NULL);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	for (try = 0; try < ALLOC_RETRY; ++try) {
900Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
910Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
920Sstevel@tonic-gate 		o = f(h);
930Sstevel@tonic-gate 		if (o != NULL)
940Sstevel@tonic-gate 			return (o);
950Sstevel@tonic-gate 		err = scf_error();
960Sstevel@tonic-gate 		if (err != SCF_ERROR_NO_MEMORY && err != SCF_ERROR_NO_RESOURCES)
970Sstevel@tonic-gate 			return (NULL);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
1010Sstevel@tonic-gate 	/* NOTREACHED */
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate scf_snapshot_t *
1050Sstevel@tonic-gate libscf_get_running_snapshot(scf_instance_t *inst)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	scf_handle_t *h;
1080Sstevel@tonic-gate 	scf_snapshot_t *snap;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	h = scf_instance_handle(inst);
1110Sstevel@tonic-gate 	if (h == NULL)
1120Sstevel@tonic-gate 		return (NULL);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
1150Sstevel@tonic-gate 	if (snap == NULL)
1160Sstevel@tonic-gate 		return (NULL);
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, SMF_SNAPSHOT_RUNNING, snap) == 0)
1190Sstevel@tonic-gate 		return (snap);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
1220Sstevel@tonic-gate 	return (NULL);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * Make sure a service has a "running" snapshot.  If it doesn't, make one from
1270Sstevel@tonic-gate  * the editing configuration.
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate scf_snapshot_t *
1300Sstevel@tonic-gate libscf_get_or_make_running_snapshot(scf_instance_t *inst, const char *fmri,
1310Sstevel@tonic-gate     boolean_t retake)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	scf_handle_t *h;
1340Sstevel@tonic-gate 	scf_snapshot_t *snap;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	h = scf_instance_handle(inst);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
1390Sstevel@tonic-gate 	if (snap == NULL)
1400Sstevel@tonic-gate 		goto err;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, SMF_SNAPSHOT_RUNNING, snap) == 0)
1430Sstevel@tonic-gate 		return (snap);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	switch (scf_error()) {
1460Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
1470Sstevel@tonic-gate 		break;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
1500Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
1510Sstevel@tonic-gate 		return (NULL);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	default:
1540Sstevel@tonic-gate err:
1550Sstevel@tonic-gate 		log_error(LOG_NOTICE,
1560Sstevel@tonic-gate 		    "Could not check for running snapshot of %s (%s).\n", fmri,
1570Sstevel@tonic-gate 		    scf_strerror(scf_error()));
1580Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
1590Sstevel@tonic-gate 		return (NULL);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if (_scf_snapshot_take_new(inst, SMF_SNAPSHOT_RUNNING, snap) == 0) {
1630Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Took running snapshot for %s.\n",
1640Sstevel@tonic-gate 		    fmri);
1650Sstevel@tonic-gate 	} else {
1660Sstevel@tonic-gate 		if (retake && scf_error() == SCF_ERROR_BACKEND_READONLY)
1670Sstevel@tonic-gate 			restarter_mark_pending_snapshot(fmri,
1680Sstevel@tonic-gate 			    RINST_RETAKE_RUNNING);
1690Sstevel@tonic-gate 		else
1700Sstevel@tonic-gate 			log_error(LOG_DEBUG,
1710Sstevel@tonic-gate 			    "Could not create running snapshot for %s "
1720Sstevel@tonic-gate 			    "(%s).\n", fmri, scf_strerror(scf_error()));
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
1750Sstevel@tonic-gate 		snap = NULL;
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	return (snap);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate  * When a service comes up, point the "start" snapshot at the "running"
1830Sstevel@tonic-gate  * snapshot.  Returns 0 on success, ENOTSUP if fmri designates something other
1840Sstevel@tonic-gate  * than an instance, ECONNABORTED, ENOENT if the instance does not exist, or
1850Sstevel@tonic-gate  * EACCES.
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate int
1880Sstevel@tonic-gate libscf_snapshots_poststart(scf_handle_t *h, const char *fmri, boolean_t retake)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	scf_instance_t *inst = NULL;
1910Sstevel@tonic-gate 	scf_snapshot_t *running, *start = NULL;
1920Sstevel@tonic-gate 	int ret = 0, r;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	r = libscf_fmri_get_instance(h, fmri, &inst);
1950Sstevel@tonic-gate 	switch (r) {
1960Sstevel@tonic-gate 	case 0:
1970Sstevel@tonic-gate 		break;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	case ENOTSUP:
2000Sstevel@tonic-gate 	case ECONNABORTED:
2010Sstevel@tonic-gate 	case ENOENT:
2020Sstevel@tonic-gate 		return (r);
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	case EINVAL:
2050Sstevel@tonic-gate 	default:
2060Sstevel@tonic-gate 		assert(0);
2070Sstevel@tonic-gate 		abort();
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	start = safe_scf_snapshot_create(h);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate again:
2130Sstevel@tonic-gate 	running = libscf_get_or_make_running_snapshot(inst, fmri, retake);
2140Sstevel@tonic-gate 	if (running == NULL) {
2150Sstevel@tonic-gate 		ret = 0;
2160Sstevel@tonic-gate 		goto out;
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate lookup:
2200Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, "start", start) != 0) {
2210Sstevel@tonic-gate 		switch (scf_error()) {
2220Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
2230Sstevel@tonic-gate 		default:
2240Sstevel@tonic-gate 			ret = ECONNABORTED;
2250Sstevel@tonic-gate 			goto out;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
2280Sstevel@tonic-gate 			if (_scf_snapshot_take_new(inst, "start", start) != 0) {
2290Sstevel@tonic-gate 				switch (scf_error()) {
2300Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
2310Sstevel@tonic-gate 				default:
2320Sstevel@tonic-gate 					ret = ECONNABORTED;
2330Sstevel@tonic-gate 					goto out;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
2360Sstevel@tonic-gate 					ret = ENOENT;
2370Sstevel@tonic-gate 					goto out;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 				case SCF_ERROR_EXISTS:
2400Sstevel@tonic-gate 					goto lookup;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 				case SCF_ERROR_NO_RESOURCES:
2430Sstevel@tonic-gate 					uu_die("Repository server out of "
2440Sstevel@tonic-gate 					    "resources.\n");
2450Sstevel@tonic-gate 					/* NOTREACHED */
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 				case SCF_ERROR_BACKEND_READONLY:
2480Sstevel@tonic-gate 					goto readonly;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 				case SCF_ERROR_PERMISSION_DENIED:
2510Sstevel@tonic-gate 					uu_die("Insufficient privileges.\n");
2520Sstevel@tonic-gate 					/* NOTREACHED */
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 				case SCF_ERROR_BACKEND_ACCESS:
2550Sstevel@tonic-gate 					ret = EACCES;
2560Sstevel@tonic-gate 					goto out;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
2595777Stw21770 				case SCF_ERROR_INTERNAL:
2600Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
2610Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
2620Sstevel@tonic-gate 					bad_error("_scf_snapshot_take_new",
2630Sstevel@tonic-gate 					    scf_error());
2640Sstevel@tonic-gate 				}
2650Sstevel@tonic-gate 			}
2660Sstevel@tonic-gate 			break;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
2690Sstevel@tonic-gate 			ret = ENOENT;
2700Sstevel@tonic-gate 			goto out;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
2730Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
2740Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
2750Sstevel@tonic-gate 			bad_error("scf_instance_get_snapshot", scf_error());
2760Sstevel@tonic-gate 		}
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (_scf_snapshot_attach(running, start) == 0) {
2800Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Updated \"start\" snapshot for %s.\n",
2810Sstevel@tonic-gate 		    fmri);
2820Sstevel@tonic-gate 	} else {
2830Sstevel@tonic-gate 		switch (scf_error()) {
2840Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
2850Sstevel@tonic-gate 		default:
2860Sstevel@tonic-gate 			ret = ECONNABORTED;
2870Sstevel@tonic-gate 			goto out;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
2900Sstevel@tonic-gate 			scf_snapshot_destroy(running);
2910Sstevel@tonic-gate 			goto again;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		case SCF_ERROR_NO_RESOURCES:
2940Sstevel@tonic-gate 			uu_die("Repository server out of resources.\n");
2950Sstevel@tonic-gate 			/* NOTREACHED */
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 		case SCF_ERROR_PERMISSION_DENIED:
2980Sstevel@tonic-gate 			uu_die("Insufficient privileges.\n");
2990Sstevel@tonic-gate 			/* NOTREACHED */
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		case SCF_ERROR_BACKEND_ACCESS:
3020Sstevel@tonic-gate 			ret = EACCES;
3030Sstevel@tonic-gate 			goto out;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		case SCF_ERROR_BACKEND_READONLY:
3060Sstevel@tonic-gate readonly:
3070Sstevel@tonic-gate 			if (retake)
3080Sstevel@tonic-gate 				restarter_mark_pending_snapshot(fmri,
3090Sstevel@tonic-gate 				    RINST_RETAKE_START);
3100Sstevel@tonic-gate 			break;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
3130Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
3140Sstevel@tonic-gate 			bad_error("_scf_snapshot_attach", scf_error());
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate out:
3190Sstevel@tonic-gate 	scf_snapshot_destroy(start);
3200Sstevel@tonic-gate 	scf_snapshot_destroy(running);
3210Sstevel@tonic-gate 	scf_instance_destroy(inst);
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	return (ret);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * Before a refresh, update the "running" snapshot from the editing
3280Sstevel@tonic-gate  * configuration.
3290Sstevel@tonic-gate  *
3300Sstevel@tonic-gate  * Returns 0 on success and -1 on failure.
3310Sstevel@tonic-gate  */
3320Sstevel@tonic-gate int
3330Sstevel@tonic-gate libscf_snapshots_refresh(scf_instance_t *inst, const char *fmri)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	scf_handle_t *h;
3360Sstevel@tonic-gate 	scf_snapshot_t *snap;
3370Sstevel@tonic-gate 	boolean_t err = 1;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	h = scf_instance_handle(inst);
3400Sstevel@tonic-gate 	if (h == NULL)
3410Sstevel@tonic-gate 		goto out;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
3440Sstevel@tonic-gate 	if (snap == NULL)
3450Sstevel@tonic-gate 		goto out;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, SMF_SNAPSHOT_RUNNING, snap) == 0) {
3480Sstevel@tonic-gate 		if (_scf_snapshot_take_attach(inst, snap) == 0)
3490Sstevel@tonic-gate 			err = 0;
3500Sstevel@tonic-gate 	} else {
3510Sstevel@tonic-gate 		switch (scf_error()) {
3520Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
3530Sstevel@tonic-gate 			err = 0;
3540Sstevel@tonic-gate 			goto out;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
3570Sstevel@tonic-gate 			break;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
3600Sstevel@tonic-gate 			assert(0);
3610Sstevel@tonic-gate 			abort();
3620Sstevel@tonic-gate 			/* NOTREACHED */
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		default:
3650Sstevel@tonic-gate 			goto out;
3660Sstevel@tonic-gate 		}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		log_error(LOG_DEBUG,
3690Sstevel@tonic-gate 		    "Service %s has no %s snapshot; creating one.\n", fmri,
3700Sstevel@tonic-gate 		    SMF_SNAPSHOT_RUNNING);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		if (_scf_snapshot_take_new(inst, SMF_SNAPSHOT_RUNNING,
3730Sstevel@tonic-gate 		    snap) == 0)
3740Sstevel@tonic-gate 			err = 0;
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate out:
3780Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	if (!err)
3810Sstevel@tonic-gate 		return (0);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	log_error(LOG_WARNING,
3840Sstevel@tonic-gate 	    "Could not update \"running\" snapshot for refresh of %s.\n", fmri);
3850Sstevel@tonic-gate 	return (-1);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate  * int libscf_read_single_astring()
3900Sstevel@tonic-gate  *   Reads a single astring value of the requested property into the
3910Sstevel@tonic-gate  *   pre-allocated buffer (conventionally of size max_scf_value_size).
3920Sstevel@tonic-gate  *   Multiple values constitute an error.
3930Sstevel@tonic-gate  *
3940Sstevel@tonic-gate  * Returns 0 on success or LIBSCF_PROPERTY_ABSENT or LIBSCF_PROPERTY_ERROR.
3950Sstevel@tonic-gate  */
3960Sstevel@tonic-gate static int
3970Sstevel@tonic-gate libscf_read_single_astring(scf_handle_t *h, scf_property_t *prop, char **ret)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	scf_value_t *val = safe_scf_value_create(h);
4000Sstevel@tonic-gate 	int r = 0;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) == -1) {
4030Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
4040Sstevel@tonic-gate 			r = LIBSCF_PROPERTY_ABSENT;
4050Sstevel@tonic-gate 		else
4060Sstevel@tonic-gate 			r = LIBSCF_PROPERTY_ERROR;
4070Sstevel@tonic-gate 		goto read_single_astring_fail;
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if (scf_value_get_astring(val, *ret, max_scf_value_size) <= 0) {
4110Sstevel@tonic-gate 		r = LIBSCF_PROPERTY_ERROR;
4120Sstevel@tonic-gate 		goto read_single_astring_fail;
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate read_single_astring_fail:
4160Sstevel@tonic-gate 	scf_value_destroy(val);
4170Sstevel@tonic-gate 	return (r);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate static int
4210Sstevel@tonic-gate libscf_read_state(const scf_propertygroup_t *pg, const char *prop_name,
4220Sstevel@tonic-gate     restarter_instance_state_t *state)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	scf_handle_t *h;
4250Sstevel@tonic-gate 	scf_property_t *prop;
4260Sstevel@tonic-gate 	char *char_state = startd_alloc(max_scf_value_size);
4270Sstevel@tonic-gate 	int ret = 0;
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	h = scf_pg_handle(pg);
4300Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if (scf_pg_get_property(pg, prop_name, prop) == -1) {
4330Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
4340Sstevel@tonic-gate 			ret = LIBSCF_PROPERTY_ABSENT;
4350Sstevel@tonic-gate 		else
4360Sstevel@tonic-gate 			ret = LIBSCF_PROPERTY_ERROR;
4370Sstevel@tonic-gate 	} else {
4380Sstevel@tonic-gate 		ret = libscf_read_single_astring(h, prop, &char_state);
4390Sstevel@tonic-gate 		if (ret != 0) {
4400Sstevel@tonic-gate 			if (ret != LIBSCF_PROPERTY_ABSENT)
4410Sstevel@tonic-gate 				ret = LIBSCF_PROPERTY_ERROR;
4420Sstevel@tonic-gate 		} else {
4430Sstevel@tonic-gate 			*state = restarter_string_to_state(char_state);
4440Sstevel@tonic-gate 			ret = 0;
4450Sstevel@tonic-gate 		}
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	startd_free(char_state, max_scf_value_size);
4490Sstevel@tonic-gate 	scf_property_destroy(prop);
4500Sstevel@tonic-gate 	return (ret);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * int libscf_read_states(const scf_propertygroup_t *,
4550Sstevel@tonic-gate  *   restarter_instance_state_t *, restarter_instance_state_t *)
4560Sstevel@tonic-gate  *
4570Sstevel@tonic-gate  *   Set the current state and next_state values for the given service instance.
4580Sstevel@tonic-gate  *   Returns 0 on success, or a libscf error code on failure.
4590Sstevel@tonic-gate  */
4600Sstevel@tonic-gate int
4610Sstevel@tonic-gate libscf_read_states(const scf_propertygroup_t *pg,
4620Sstevel@tonic-gate     restarter_instance_state_t *state, restarter_instance_state_t *next_state)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	int state_ret, next_state_ret, ret;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	state_ret = libscf_read_state(pg, SCF_PROPERTY_STATE, state);
4670Sstevel@tonic-gate 	next_state_ret = libscf_read_state(pg, SCF_PROPERTY_NEXT_STATE,
4680Sstevel@tonic-gate 	    next_state);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (state_ret == LIBSCF_PROPERTY_ERROR ||
4710Sstevel@tonic-gate 	    next_state_ret == LIBSCF_PROPERTY_ERROR) {
4720Sstevel@tonic-gate 		ret = LIBSCF_PROPERTY_ERROR;
4730Sstevel@tonic-gate 	} else if (state_ret == 0 && next_state_ret == 0) {
4740Sstevel@tonic-gate 		ret = 0;
4750Sstevel@tonic-gate 	} else if (state_ret == LIBSCF_PROPERTY_ABSENT &&
4760Sstevel@tonic-gate 	    next_state_ret == LIBSCF_PROPERTY_ABSENT) {
4770Sstevel@tonic-gate 		*state = RESTARTER_STATE_UNINIT;
4780Sstevel@tonic-gate 		*next_state = RESTARTER_STATE_NONE;
4790Sstevel@tonic-gate 		ret = 0;
4800Sstevel@tonic-gate 	} else if (state_ret == LIBSCF_PROPERTY_ABSENT ||
4810Sstevel@tonic-gate 	    next_state_ret == LIBSCF_PROPERTY_ABSENT) {
4820Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
4830Sstevel@tonic-gate 		    "Only one repository state exists, setting "
4840Sstevel@tonic-gate 		    "restarter states to MAINTENANCE and NONE\n");
4850Sstevel@tonic-gate 		*state = RESTARTER_STATE_MAINT;
4860Sstevel@tonic-gate 		*next_state = RESTARTER_STATE_NONE;
4870Sstevel@tonic-gate 		ret = 0;
4880Sstevel@tonic-gate 	} else {
4890Sstevel@tonic-gate 		ret = LIBSCF_PROPERTY_ERROR;
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate read_states_out:
4930Sstevel@tonic-gate 	return (ret);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate /*
4970Sstevel@tonic-gate  * depgroup_empty()
4980Sstevel@tonic-gate  *
4990Sstevel@tonic-gate  * Returns 0 if not empty.
5000Sstevel@tonic-gate  * Returns 1 if empty.
5010Sstevel@tonic-gate  * Returns -1 on error (check scf_error()).
5020Sstevel@tonic-gate  */
5030Sstevel@tonic-gate int
5040Sstevel@tonic-gate depgroup_empty(scf_handle_t *h, scf_propertygroup_t *pg)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate 	int empty = 1;
5070Sstevel@tonic-gate 	scf_iter_t *iter;
5080Sstevel@tonic-gate 	scf_property_t *prop;
5090Sstevel@tonic-gate 	int ret;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
5120Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
5130Sstevel@tonic-gate 
5142682Srm88369 	if (scf_iter_pg_properties(iter, pg) != SCF_SUCCESS) {
5152682Srm88369 		scf_property_destroy(prop);
5162682Srm88369 		scf_iter_destroy(iter);
5170Sstevel@tonic-gate 		return (-1);
5182682Srm88369 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	ret = scf_iter_next_property(iter, prop);
5212682Srm88369 	if (ret < 0) {
5222682Srm88369 		scf_property_destroy(prop);
5232682Srm88369 		scf_iter_destroy(iter);
5240Sstevel@tonic-gate 		return (-1);
5252682Srm88369 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if (ret == 1)
5280Sstevel@tonic-gate 		empty = 0;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	scf_property_destroy(prop);
5310Sstevel@tonic-gate 	scf_iter_destroy(iter);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	return (empty);
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate gv_type_t
5370Sstevel@tonic-gate depgroup_read_scheme(scf_handle_t *h, scf_propertygroup_t *pg)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate 	scf_property_t *prop;
5400Sstevel@tonic-gate 	char *scheme = startd_alloc(max_scf_value_size);
5410Sstevel@tonic-gate 	gv_type_t ret;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_TYPE, prop) == -1 ||
5460Sstevel@tonic-gate 	    libscf_read_single_astring(h, prop, &scheme) != 0) {
5470Sstevel@tonic-gate 		scf_property_destroy(prop);
5480Sstevel@tonic-gate 		startd_free(scheme, max_scf_value_size);
5490Sstevel@tonic-gate 		return (GVT_UNSUPPORTED);
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	if (strcmp(scheme, "service") == 0)
5530Sstevel@tonic-gate 		ret = GVT_INST;
5540Sstevel@tonic-gate 	else if (strcmp(scheme, "path") == 0)
5550Sstevel@tonic-gate 		ret = GVT_FILE;
5560Sstevel@tonic-gate 	else
5570Sstevel@tonic-gate 		ret = GVT_UNSUPPORTED;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	startd_free(scheme, max_scf_value_size);
5600Sstevel@tonic-gate 	scf_property_destroy(prop);
5610Sstevel@tonic-gate 	return (ret);
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate depgroup_type_t
5650Sstevel@tonic-gate depgroup_read_grouping(scf_handle_t *h, scf_propertygroup_t *pg)
5660Sstevel@tonic-gate {
5670Sstevel@tonic-gate 	char *grouping = startd_alloc(max_scf_value_size);
5680Sstevel@tonic-gate 	depgroup_type_t ret;
5690Sstevel@tonic-gate 	scf_property_t *prop = safe_scf_property_create(h);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_GROUPING, prop) == -1 ||
5720Sstevel@tonic-gate 	    libscf_read_single_astring(h, prop, &grouping) != 0) {
5730Sstevel@tonic-gate 		scf_property_destroy(prop);
5740Sstevel@tonic-gate 		startd_free(grouping, max_scf_value_size);
5750Sstevel@tonic-gate 		return (DEPGRP_UNSUPPORTED);
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	if (strcmp(grouping, SCF_DEP_REQUIRE_ANY) == 0)
5790Sstevel@tonic-gate 		ret = DEPGRP_REQUIRE_ANY;
5800Sstevel@tonic-gate 	else if (strcmp(grouping, SCF_DEP_REQUIRE_ALL) == 0)
5810Sstevel@tonic-gate 		ret = DEPGRP_REQUIRE_ALL;
5820Sstevel@tonic-gate 	else if (strcmp(grouping, SCF_DEP_OPTIONAL_ALL) == 0)
5830Sstevel@tonic-gate 		ret = DEPGRP_OPTIONAL_ALL;
5840Sstevel@tonic-gate 	else if (strcmp(grouping, SCF_DEP_EXCLUDE_ALL) == 0)
5850Sstevel@tonic-gate 		ret = DEPGRP_EXCLUDE_ALL;
5860Sstevel@tonic-gate 	else {
5870Sstevel@tonic-gate 		ret = DEPGRP_UNSUPPORTED;
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 	startd_free(grouping, max_scf_value_size);
5900Sstevel@tonic-gate 	scf_property_destroy(prop);
5910Sstevel@tonic-gate 	return (ret);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate restarter_error_t
5950Sstevel@tonic-gate depgroup_read_restart(scf_handle_t *h, scf_propertygroup_t *pg)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 	scf_property_t *prop = safe_scf_property_create(h);
5980Sstevel@tonic-gate 	char *restart_on = startd_alloc(max_scf_value_size);
5990Sstevel@tonic-gate 	restarter_error_t ret;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_RESTART_ON, prop) == -1 ||
6020Sstevel@tonic-gate 	    libscf_read_single_astring(h, prop, &restart_on) != 0) {
6030Sstevel@tonic-gate 		startd_free(restart_on, max_scf_value_size);
6040Sstevel@tonic-gate 		scf_property_destroy(prop);
6050Sstevel@tonic-gate 		return (RERR_UNSUPPORTED);
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (strcmp(restart_on, SCF_DEP_RESET_ON_ERROR) == 0)
6090Sstevel@tonic-gate 		ret = RERR_FAULT;
6100Sstevel@tonic-gate 	else if (strcmp(restart_on, SCF_DEP_RESET_ON_RESTART) == 0)
6110Sstevel@tonic-gate 		ret = RERR_RESTART;
6120Sstevel@tonic-gate 	else if (strcmp(restart_on, SCF_DEP_RESET_ON_REFRESH) == 0)
6130Sstevel@tonic-gate 		ret = RERR_REFRESH;
6140Sstevel@tonic-gate 	else if (strcmp(restart_on, SCF_DEP_RESET_ON_NONE) == 0)
6150Sstevel@tonic-gate 		ret = RERR_NONE;
6160Sstevel@tonic-gate 	else
6170Sstevel@tonic-gate 		ret = RERR_UNSUPPORTED;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	startd_free(restart_on, max_scf_value_size);
6200Sstevel@tonic-gate 	scf_property_destroy(prop);
6210Sstevel@tonic-gate 	return (ret);
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate /*
6250Sstevel@tonic-gate  * int get_boolean()
6260Sstevel@tonic-gate  *   Fetches the value of a boolean property of the given property group.
6270Sstevel@tonic-gate  *   Returns
6280Sstevel@tonic-gate  *     0 - success
6290Sstevel@tonic-gate  *     ECONNABORTED - repository connection broken
6300Sstevel@tonic-gate  *     ECANCELED - pg was deleted
6310Sstevel@tonic-gate  *     ENOENT - the property doesn't exist or has no values
6320Sstevel@tonic-gate  *     EINVAL - the property has the wrong type
6330Sstevel@tonic-gate  *		the property is not single-valued
6345040Swesolows  *     EACCES - the current user does not have permission to read the value
6350Sstevel@tonic-gate  */
6360Sstevel@tonic-gate static int
6370Sstevel@tonic-gate get_boolean(scf_propertygroup_t *pg, const char *propname, uint8_t *valuep)
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate 	scf_handle_t *h;
6400Sstevel@tonic-gate 	scf_property_t *prop;
6410Sstevel@tonic-gate 	scf_value_t *val;
6420Sstevel@tonic-gate 	int ret = 0, r;
6430Sstevel@tonic-gate 	scf_type_t type;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	h = scf_pg_handle(pg);
6460Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
6470Sstevel@tonic-gate 	val = safe_scf_value_create(h);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	if (scf_pg_get_property(pg, propname, prop) != 0) {
6500Sstevel@tonic-gate 		switch (scf_error()) {
6510Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
6520Sstevel@tonic-gate 		default:
6530Sstevel@tonic-gate 			ret = ECONNABORTED;
6540Sstevel@tonic-gate 			goto out;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
6570Sstevel@tonic-gate 			ret = ECANCELED;
6580Sstevel@tonic-gate 			goto out;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
6610Sstevel@tonic-gate 			ret = ENOENT;
6620Sstevel@tonic-gate 			goto out;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
6650Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
6660Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
6670Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if (scf_property_type(prop, &type) != 0) {
6720Sstevel@tonic-gate 		switch (scf_error()) {
6730Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
6740Sstevel@tonic-gate 		default:
6750Sstevel@tonic-gate 			ret = ECONNABORTED;
6760Sstevel@tonic-gate 			goto out;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
6790Sstevel@tonic-gate 			ret = ENOENT;
6800Sstevel@tonic-gate 			goto out;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
6830Sstevel@tonic-gate 			bad_error("scf_property_type", scf_error());
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	if (type != SCF_TYPE_BOOLEAN) {
6880Sstevel@tonic-gate 		ret = EINVAL;
6890Sstevel@tonic-gate 		goto out;
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != 0) {
6930Sstevel@tonic-gate 		switch (scf_error()) {
6940Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
6950Sstevel@tonic-gate 		default:
6960Sstevel@tonic-gate 			ret = ECONNABORTED;
6970Sstevel@tonic-gate 			goto out;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7000Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7010Sstevel@tonic-gate 			ret = ENOENT;
7020Sstevel@tonic-gate 			goto out;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
7050Sstevel@tonic-gate 			ret = EINVAL;
7060Sstevel@tonic-gate 			goto out;
7070Sstevel@tonic-gate 
7085040Swesolows 		case SCF_ERROR_PERMISSION_DENIED:
7095040Swesolows 			ret = EACCES;
7105040Swesolows 			goto out;
7115040Swesolows 
7120Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7130Sstevel@tonic-gate 			bad_error("scf_property_get_value", scf_error());
7140Sstevel@tonic-gate 		}
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	r = scf_value_get_boolean(val, valuep);
7180Sstevel@tonic-gate 	assert(r == 0);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate out:
7210Sstevel@tonic-gate 	scf_value_destroy(val);
7220Sstevel@tonic-gate 	scf_property_destroy(prop);
7230Sstevel@tonic-gate 	return (ret);
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate /*
7270Sstevel@tonic-gate  * int get_count()
7280Sstevel@tonic-gate  *   Fetches the value of a count property of the given property group.
7290Sstevel@tonic-gate  *   Returns
7300Sstevel@tonic-gate  *     0 - success
7310Sstevel@tonic-gate  *     ECONNABORTED - repository connection broken
7320Sstevel@tonic-gate  *                    unknown libscf error
7330Sstevel@tonic-gate  *     ECANCELED - pg was deleted
7340Sstevel@tonic-gate  *     ENOENT - the property doesn't exist or has no values
7350Sstevel@tonic-gate  *     EINVAL - the property has the wrong type
7360Sstevel@tonic-gate  *              the property is not single-valued
7375040Swesolows  *     EACCES - the current user does not have permission to read the value
7380Sstevel@tonic-gate  */
7390Sstevel@tonic-gate static int
7400Sstevel@tonic-gate get_count(scf_propertygroup_t *pg, const char *propname, uint64_t *valuep)
7410Sstevel@tonic-gate {
7420Sstevel@tonic-gate 	scf_handle_t *h;
7430Sstevel@tonic-gate 	scf_property_t *prop;
7440Sstevel@tonic-gate 	scf_value_t *val;
7450Sstevel@tonic-gate 	int ret = 0, r;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	h = scf_pg_handle(pg);
7480Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
7490Sstevel@tonic-gate 	val = safe_scf_value_create(h);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	if (scf_pg_get_property(pg, propname, prop) != 0) {
7520Sstevel@tonic-gate 		switch (scf_error()) {
7530Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7540Sstevel@tonic-gate 		default:
7550Sstevel@tonic-gate 			ret = ECONNABORTED;
7560Sstevel@tonic-gate 			goto out;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7590Sstevel@tonic-gate 			ret = ECANCELED;
7600Sstevel@tonic-gate 			goto out;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7630Sstevel@tonic-gate 			ret = ENOENT;
7640Sstevel@tonic-gate 			goto out;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7670Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7680Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7690Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0) {
7740Sstevel@tonic-gate 		switch (scf_error()) {
7750Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7760Sstevel@tonic-gate 		default:
7770Sstevel@tonic-gate 			ret = ECONNABORTED;
7780Sstevel@tonic-gate 			goto out;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 		case SCF_ERROR_TYPE_MISMATCH:
7810Sstevel@tonic-gate 			ret = EINVAL;
7820Sstevel@tonic-gate 			goto out;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7850Sstevel@tonic-gate 			ret = ECANCELED;
7860Sstevel@tonic-gate 			goto out;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7890Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
7900Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7910Sstevel@tonic-gate 			bad_error("scf_property_is_type", scf_error());
7920Sstevel@tonic-gate 		}
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != 0) {
7960Sstevel@tonic-gate 		switch (scf_error()) {
7970Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7980Sstevel@tonic-gate 		default:
7990Sstevel@tonic-gate 			ret = ECONNABORTED;
8000Sstevel@tonic-gate 			goto out;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
8030Sstevel@tonic-gate 			ret = ECANCELED;
8040Sstevel@tonic-gate 			goto out;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
8070Sstevel@tonic-gate 			ret = ENOENT;
8080Sstevel@tonic-gate 			goto out;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
8110Sstevel@tonic-gate 			ret = EINVAL;
8120Sstevel@tonic-gate 			goto out;
8130Sstevel@tonic-gate 
8145040Swesolows 		case SCF_ERROR_PERMISSION_DENIED:
8155040Swesolows 			ret = EACCES;
8165040Swesolows 			goto out;
8175040Swesolows 
8180Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
8190Sstevel@tonic-gate 			bad_error("scf_property_get_value", scf_error());
8200Sstevel@tonic-gate 		}
8210Sstevel@tonic-gate 	}
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	r = scf_value_get_count(val, valuep);
8240Sstevel@tonic-gate 	assert(r == 0);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate out:
8270Sstevel@tonic-gate 	scf_value_destroy(val);
8280Sstevel@tonic-gate 	scf_property_destroy(prop);
8290Sstevel@tonic-gate 	return (ret);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate static void
8340Sstevel@tonic-gate get_restarter(scf_handle_t *h, scf_propertygroup_t *pg, char **restarter)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	scf_property_t *prop = safe_scf_property_create(h);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER, prop) == -1 ||
8390Sstevel@tonic-gate 	    libscf_read_single_astring(h, prop, restarter) != 0)
8400Sstevel@tonic-gate 		*restarter[0] = '\0';
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	scf_property_destroy(prop);
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate /*
8460Sstevel@tonic-gate  * int libscf_instance_get_fmri(scf_instance_t *, char **)
8470Sstevel@tonic-gate  *   Give a valid SCF instance, return its FMRI.  Returns 0 on success,
8480Sstevel@tonic-gate  *   ECONNABORTED, or ECANCELED if inst is deleted.
8490Sstevel@tonic-gate  */
8500Sstevel@tonic-gate int
8510Sstevel@tonic-gate libscf_instance_get_fmri(scf_instance_t *inst, char **retp)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	char *inst_fmri = startd_alloc(max_scf_fmri_size);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	inst_fmri[0] = 0;
8560Sstevel@tonic-gate 	if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) <= 0) {
8570Sstevel@tonic-gate 		startd_free(inst_fmri, max_scf_fmri_size);
8580Sstevel@tonic-gate 		switch (scf_error()) {
8590Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
8600Sstevel@tonic-gate 		default:
8610Sstevel@tonic-gate 			return (ECONNABORTED);
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
8640Sstevel@tonic-gate 			return (ECANCELED);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
8670Sstevel@tonic-gate 			assert(0);
8680Sstevel@tonic-gate 			abort();
8690Sstevel@tonic-gate 		}
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	*retp = inst_fmri;
8730Sstevel@tonic-gate 	return (0);
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate /*
8770Sstevel@tonic-gate  * int libscf_fmri_get_instance(scf_handle_t *, const char *,
8780Sstevel@tonic-gate  *	scf_instance_t **)
8790Sstevel@tonic-gate  *   Given a valid SCF handle and an FMRI, return the SCF instance that matches
8800Sstevel@tonic-gate  *   exactly.  The instance must be released using scf_instance_destroy().
8810Sstevel@tonic-gate  *   Returns 0 on success, EINVAL if the FMRI is invalid, ENOTSUP if the FMRI
8820Sstevel@tonic-gate  *   is valid but designates something other than an instance, ECONNABORTED if
8830Sstevel@tonic-gate  *   the repository connection is broken, or ENOENT if the instance does not
8840Sstevel@tonic-gate  *   exist.
8850Sstevel@tonic-gate  */
8860Sstevel@tonic-gate int
8870Sstevel@tonic-gate libscf_fmri_get_instance(scf_handle_t *h, const char *fmri,
8880Sstevel@tonic-gate     scf_instance_t **instp)
8890Sstevel@tonic-gate {
8900Sstevel@tonic-gate 	scf_instance_t *inst;
8910Sstevel@tonic-gate 	int r;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	r = libscf_lookup_instance(fmri, inst);
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	if (r == 0)
8980Sstevel@tonic-gate 		*instp = inst;
8990Sstevel@tonic-gate 	else
9000Sstevel@tonic-gate 		scf_instance_destroy(inst);
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	return (r);
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate int
9060Sstevel@tonic-gate libscf_lookup_instance(const char *fmri, scf_instance_t *inst)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate 	if (scf_handle_decode_fmri(scf_instance_handle(inst), fmri, NULL, NULL,
9090Sstevel@tonic-gate 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS) {
9100Sstevel@tonic-gate 		switch (scf_error()) {
9110Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
9120Sstevel@tonic-gate 			return (EINVAL);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
9150Sstevel@tonic-gate 			return (ENOTSUP);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
9180Sstevel@tonic-gate 			return (ECONNABORTED);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
9210Sstevel@tonic-gate 			return (ENOENT);
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
9240Sstevel@tonic-gate 		default:
9250Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	return (0);
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate /*
9337475SPhilippe.Jung@Sun.COM  * int libscf_get_deathrow()
9347475SPhilippe.Jung@Sun.COM  * Read deathrow for inst. Returns 0, ECONNABORTED if the connection to the
9357475SPhilippe.Jung@Sun.COM  * repository is broken, ECANCELED if inst is deleted, or ENOENT if inst
9367475SPhilippe.Jung@Sun.COM  * has no deathrow property group.
9377475SPhilippe.Jung@Sun.COM  *
9387475SPhilippe.Jung@Sun.COM  * If deathrow/deathrow was missing or invalid, *deathrow will be -1 and a
9397475SPhilippe.Jung@Sun.COM  * debug message is logged.
9407475SPhilippe.Jung@Sun.COM  */
9417475SPhilippe.Jung@Sun.COM int
9427475SPhilippe.Jung@Sun.COM libscf_get_deathrow(scf_handle_t *h, scf_instance_t *inst, int *deathrow)
9437475SPhilippe.Jung@Sun.COM {
9447475SPhilippe.Jung@Sun.COM 	scf_propertygroup_t *pg;
9457475SPhilippe.Jung@Sun.COM 	int r;
9467475SPhilippe.Jung@Sun.COM 	uint8_t deathrow_8;
9477475SPhilippe.Jung@Sun.COM 
9487475SPhilippe.Jung@Sun.COM 	pg = safe_scf_pg_create(h);
9497475SPhilippe.Jung@Sun.COM 
9507475SPhilippe.Jung@Sun.COM 	if (scf_instance_get_pg_composed(inst, NULL, SCF_PG_DEATHROW, pg) !=
9517475SPhilippe.Jung@Sun.COM 	    0) {
9527475SPhilippe.Jung@Sun.COM 		switch (scf_error()) {
9537475SPhilippe.Jung@Sun.COM 		case SCF_ERROR_CONNECTION_BROKEN:
9547475SPhilippe.Jung@Sun.COM 		default:
9557475SPhilippe.Jung@Sun.COM 			scf_pg_destroy(pg);
9567475SPhilippe.Jung@Sun.COM 			return (ECONNABORTED);
9577475SPhilippe.Jung@Sun.COM 
9587475SPhilippe.Jung@Sun.COM 		case SCF_ERROR_DELETED:
9597475SPhilippe.Jung@Sun.COM 			scf_pg_destroy(pg);
9607475SPhilippe.Jung@Sun.COM 			return (ECANCELED);
9617475SPhilippe.Jung@Sun.COM 
9627475SPhilippe.Jung@Sun.COM 		case SCF_ERROR_NOT_FOUND:
9637475SPhilippe.Jung@Sun.COM 			*deathrow = -1;
9647475SPhilippe.Jung@Sun.COM 			break;
9657475SPhilippe.Jung@Sun.COM 
9667475SPhilippe.Jung@Sun.COM 		case SCF_ERROR_HANDLE_MISMATCH:
9677475SPhilippe.Jung@Sun.COM 		case SCF_ERROR_INVALID_ARGUMENT:
9687475SPhilippe.Jung@Sun.COM 		case SCF_ERROR_NOT_SET:
9697475SPhilippe.Jung@Sun.COM 			bad_error("libscf_get_deathrow", scf_error());
9707475SPhilippe.Jung@Sun.COM 		}
9717475SPhilippe.Jung@Sun.COM 	} else {
9727475SPhilippe.Jung@Sun.COM 		switch (r = get_boolean(pg,
9737475SPhilippe.Jung@Sun.COM 		    SCF_PROPERTY_DEATHROW, &deathrow_8)) {
9747475SPhilippe.Jung@Sun.COM 		case 0:
9757475SPhilippe.Jung@Sun.COM 			*deathrow = deathrow_8;
9767475SPhilippe.Jung@Sun.COM 			break;
9777475SPhilippe.Jung@Sun.COM 
9787475SPhilippe.Jung@Sun.COM 		case ECONNABORTED:
9797475SPhilippe.Jung@Sun.COM 		case ECANCELED:
9807475SPhilippe.Jung@Sun.COM 			scf_pg_destroy(pg);
9817475SPhilippe.Jung@Sun.COM 			return (r);
9827475SPhilippe.Jung@Sun.COM 
9837475SPhilippe.Jung@Sun.COM 		case ENOENT:
9847475SPhilippe.Jung@Sun.COM 		case EINVAL:
9857475SPhilippe.Jung@Sun.COM 			*deathrow = -1;
9867475SPhilippe.Jung@Sun.COM 			break;
9877475SPhilippe.Jung@Sun.COM 
9887475SPhilippe.Jung@Sun.COM 		default:
9897475SPhilippe.Jung@Sun.COM 			bad_error("get_boolean", r);
9907475SPhilippe.Jung@Sun.COM 		}
9917475SPhilippe.Jung@Sun.COM 	}
9927475SPhilippe.Jung@Sun.COM 
9937475SPhilippe.Jung@Sun.COM 	scf_pg_destroy(pg);
9947475SPhilippe.Jung@Sun.COM 
9957475SPhilippe.Jung@Sun.COM 	return (0);
9967475SPhilippe.Jung@Sun.COM }
9977475SPhilippe.Jung@Sun.COM 
9987475SPhilippe.Jung@Sun.COM /*
9990Sstevel@tonic-gate  * void libscf_get_basic_instance_data()
10000Sstevel@tonic-gate  *   Read enabled, enabled_ovr, and restarter_fmri (into an allocated
10010Sstevel@tonic-gate  *   buffer) for inst.  Returns 0, ECONNABORTED if the connection to the
10020Sstevel@tonic-gate  *   repository is broken, ECANCELED if inst is deleted, or ENOENT if inst
10030Sstevel@tonic-gate  *   has no general property group.
10040Sstevel@tonic-gate  *
10050Sstevel@tonic-gate  *   On success, restarter_fmri may be NULL.  If general/enabled was missing
10060Sstevel@tonic-gate  *   or invalid, *enabledp will be -1 and a debug message is logged.
10070Sstevel@tonic-gate  */
10080Sstevel@tonic-gate int
10090Sstevel@tonic-gate libscf_get_basic_instance_data(scf_handle_t *h, scf_instance_t *inst,
10100Sstevel@tonic-gate     const char *fmri, int *enabledp, int *enabled_ovrp, char **restarter_fmri)
10110Sstevel@tonic-gate {
10120Sstevel@tonic-gate 	scf_propertygroup_t *pg;
10130Sstevel@tonic-gate 	int r;
10140Sstevel@tonic-gate 	uint8_t enabled_8;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	if (enabled_ovrp == NULL)
10190Sstevel@tonic-gate 		goto enabled;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, NULL, SCF_PG_GENERAL_OVR, pg) !=
10220Sstevel@tonic-gate 	    0) {
10230Sstevel@tonic-gate 		switch (scf_error()) {
10240Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
10250Sstevel@tonic-gate 		default:
10260Sstevel@tonic-gate 			scf_pg_destroy(pg);
10270Sstevel@tonic-gate 			return (ECONNABORTED);
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
10300Sstevel@tonic-gate 			scf_pg_destroy(pg);
10310Sstevel@tonic-gate 			return (ECANCELED);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
10340Sstevel@tonic-gate 			*enabled_ovrp = -1;
10350Sstevel@tonic-gate 			break;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
10380Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
10390Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
10400Sstevel@tonic-gate 			bad_error("scf_instance_get_pg_composed", scf_error());
10410Sstevel@tonic-gate 		}
10420Sstevel@tonic-gate 	} else {
10430Sstevel@tonic-gate 		switch (r = get_boolean(pg, SCF_PROPERTY_ENABLED, &enabled_8)) {
10440Sstevel@tonic-gate 		case 0:
10450Sstevel@tonic-gate 			*enabled_ovrp = enabled_8;
10460Sstevel@tonic-gate 			break;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 		case ECONNABORTED:
10490Sstevel@tonic-gate 		case ECANCELED:
10500Sstevel@tonic-gate 			scf_pg_destroy(pg);
10510Sstevel@tonic-gate 			return (r);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 		case ENOENT:
10540Sstevel@tonic-gate 		case EINVAL:
10550Sstevel@tonic-gate 			*enabled_ovrp = -1;
10560Sstevel@tonic-gate 			break;
10570Sstevel@tonic-gate 
10585040Swesolows 		case EACCES:
10590Sstevel@tonic-gate 		default:
10600Sstevel@tonic-gate 			bad_error("get_boolean", r);
10610Sstevel@tonic-gate 		}
10620Sstevel@tonic-gate 	}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate enabled:
10650Sstevel@tonic-gate 	/*
10660Sstevel@tonic-gate 	 * Since general/restarter can be at the service level, we must do
10670Sstevel@tonic-gate 	 * a composed lookup.  These properties are immediate, though, so we
10680Sstevel@tonic-gate 	 * must use the "editing" snapshot.  Technically enabled shouldn't be
10690Sstevel@tonic-gate 	 * at the service level, but looking it up composed, too, doesn't
10700Sstevel@tonic-gate 	 * hurt.
10710Sstevel@tonic-gate 	 */
10720Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, NULL, SCF_PG_GENERAL, pg) != 0) {
10730Sstevel@tonic-gate 		scf_pg_destroy(pg);
10740Sstevel@tonic-gate 		switch (scf_error()) {
10750Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
10760Sstevel@tonic-gate 		default:
10770Sstevel@tonic-gate 			return (ECONNABORTED);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
10800Sstevel@tonic-gate 			return (ECANCELED);
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
10830Sstevel@tonic-gate 			return (ENOENT);
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
10860Sstevel@tonic-gate 			bad_error("scf_instance_get_pg_composed", scf_error());
10870Sstevel@tonic-gate 		}
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 	switch (r = get_boolean(pg, SCF_PROPERTY_ENABLED, &enabled_8)) {
10910Sstevel@tonic-gate 	case 0:
10920Sstevel@tonic-gate 		*enabledp = enabled_8;
10930Sstevel@tonic-gate 		break;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	case ECONNABORTED:
10960Sstevel@tonic-gate 	case ECANCELED:
10970Sstevel@tonic-gate 		scf_pg_destroy(pg);
10980Sstevel@tonic-gate 		return (r);
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	case ENOENT:
11010Sstevel@tonic-gate 		/*
11020Sstevel@tonic-gate 		 * DEBUG because this happens when svccfg import creates
11030Sstevel@tonic-gate 		 * a temporary service.
11040Sstevel@tonic-gate 		 */
11050Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
11060Sstevel@tonic-gate 		    "general/enabled property of %s is missing.\n", fmri);
11070Sstevel@tonic-gate 		*enabledp = -1;
11080Sstevel@tonic-gate 		break;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	case EINVAL:
11110Sstevel@tonic-gate 		log_framework(LOG_ERR,
11120Sstevel@tonic-gate 		    "general/enabled property of %s is invalid.\n", fmri);
11130Sstevel@tonic-gate 		*enabledp = -1;
11140Sstevel@tonic-gate 		break;
11150Sstevel@tonic-gate 
11165040Swesolows 	case EACCES:
11170Sstevel@tonic-gate 	default:
11180Sstevel@tonic-gate 		bad_error("get_boolean", r);
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	if (restarter_fmri != NULL)
11220Sstevel@tonic-gate 		get_restarter(h, pg, restarter_fmri);
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	scf_pg_destroy(pg);
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	return (0);
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate /*
11310Sstevel@tonic-gate  * Sets pg to the name property group of s_inst.  If it doesn't exist, it is
11320Sstevel@tonic-gate  * added.
11330Sstevel@tonic-gate  *
11340Sstevel@tonic-gate  * Fails with
11350Sstevel@tonic-gate  *   ECONNABORTED - repository disconnection or unknown libscf error
11360Sstevel@tonic-gate  *   ECANCELED - inst is deleted
11370Sstevel@tonic-gate  *   EPERM - permission is denied
11380Sstevel@tonic-gate  *   EACCES - backend denied access
11390Sstevel@tonic-gate  *   EROFS - backend readonly
11400Sstevel@tonic-gate  */
11410Sstevel@tonic-gate int
11420Sstevel@tonic-gate libscf_inst_get_or_add_pg(scf_instance_t *inst, const char *name,
11430Sstevel@tonic-gate     const char *type, uint32_t flags, scf_propertygroup_t *pg)
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate 	uint32_t f;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate again:
11480Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, name, pg) == 0) {
11490Sstevel@tonic-gate 		if (scf_pg_get_flags(pg, &f) != 0) {
11500Sstevel@tonic-gate 			switch (scf_error()) {
11510Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
11520Sstevel@tonic-gate 			default:
11530Sstevel@tonic-gate 				return (ECONNABORTED);
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
11560Sstevel@tonic-gate 				goto add;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
11590Sstevel@tonic-gate 				bad_error("scf_pg_get_flags", scf_error());
11600Sstevel@tonic-gate 			}
11610Sstevel@tonic-gate 		}
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 		if (f == flags)
11640Sstevel@tonic-gate 			return (0);
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 		if (scf_pg_delete(pg) != 0) {
11670Sstevel@tonic-gate 			switch (scf_error()) {
11680Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
11690Sstevel@tonic-gate 			default:
11700Sstevel@tonic-gate 				return (ECONNABORTED);
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
11730Sstevel@tonic-gate 				break;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
11760Sstevel@tonic-gate 				return (EPERM);
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
11790Sstevel@tonic-gate 				return (EACCES);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
11820Sstevel@tonic-gate 				return (EROFS);
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
11850Sstevel@tonic-gate 				bad_error("scf_pg_delete", scf_error());
11860Sstevel@tonic-gate 			}
11870Sstevel@tonic-gate 		}
11880Sstevel@tonic-gate 	} else {
11890Sstevel@tonic-gate 		switch (scf_error()) {
11900Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
11910Sstevel@tonic-gate 		default:
11920Sstevel@tonic-gate 			return (ECONNABORTED);
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
11950Sstevel@tonic-gate 			return (ECANCELED);
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
11980Sstevel@tonic-gate 			break;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
12010Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
12020Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
12030Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
12040Sstevel@tonic-gate 		}
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate add:
12080Sstevel@tonic-gate 	if (scf_instance_add_pg(inst, name, type, flags, pg) == 0)
12090Sstevel@tonic-gate 		return (0);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	switch (scf_error()) {
12120Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
12130Sstevel@tonic-gate 	default:
12140Sstevel@tonic-gate 		return (ECONNABORTED);
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
12170Sstevel@tonic-gate 		return (ECANCELED);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	case SCF_ERROR_EXISTS:
12200Sstevel@tonic-gate 		goto again;
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	case SCF_ERROR_PERMISSION_DENIED:
12230Sstevel@tonic-gate 		return (EPERM);
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_ACCESS:
12260Sstevel@tonic-gate 		return (EACCES);
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_READONLY:
12290Sstevel@tonic-gate 		return (EROFS);
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	case SCF_ERROR_HANDLE_MISMATCH:
12320Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
12330Sstevel@tonic-gate 	case SCF_ERROR_NOT_SET:
12340Sstevel@tonic-gate 		bad_error("scf_instance_add_pg", scf_error());
12350Sstevel@tonic-gate 		/* NOTREACHED */
12360Sstevel@tonic-gate 	}
12370Sstevel@tonic-gate }
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate /*
12400Sstevel@tonic-gate  * Returns
12410Sstevel@tonic-gate  *   0 - success
12420Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
12430Sstevel@tonic-gate  *		  - unknown libscf error
12440Sstevel@tonic-gate  *   ECANCELED
12450Sstevel@tonic-gate  */
12460Sstevel@tonic-gate static scf_error_t
12470Sstevel@tonic-gate transaction_add_set(scf_transaction_t *tx, scf_transaction_entry_t *ent,
12480Sstevel@tonic-gate     const char *pname, scf_type_t ty)
12490Sstevel@tonic-gate {
12500Sstevel@tonic-gate 	for (;;) {
12510Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, ent, pname,
12520Sstevel@tonic-gate 		    ty) == 0)
12530Sstevel@tonic-gate 			return (0);
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 		switch (scf_error()) {
12560Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
12570Sstevel@tonic-gate 		default:
12580Sstevel@tonic-gate 			return (ECONNABORTED);
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
12610Sstevel@tonic-gate 			return (ECANCELED);
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
12640Sstevel@tonic-gate 			break;
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
12670Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
12680Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
12690Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
12700Sstevel@tonic-gate 			bad_error("scf_transaction_property_change_type",
12710Sstevel@tonic-gate 			    scf_error());
12720Sstevel@tonic-gate 		}
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, ent, pname, ty) == 0)
12750Sstevel@tonic-gate 			return (0);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 		switch (scf_error()) {
12780Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
12790Sstevel@tonic-gate 		default:
12800Sstevel@tonic-gate 			return (ECONNABORTED);
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
12830Sstevel@tonic-gate 			return (ECANCELED);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
12860Sstevel@tonic-gate 			break;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
12890Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
12900Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
12910Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
12920Sstevel@tonic-gate 			bad_error("scf_transaction_property_new", scf_error());
12930Sstevel@tonic-gate 			/* NOTREACHED */
12940Sstevel@tonic-gate 		}
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate /*
12990Sstevel@tonic-gate  * Returns
13000Sstevel@tonic-gate  *   0 - success
13010Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
13020Sstevel@tonic-gate  *		  - unknown libscf error
13030Sstevel@tonic-gate  *   ECANCELED - pg was deleted
13040Sstevel@tonic-gate  *   EPERM
13050Sstevel@tonic-gate  *   EACCES
13060Sstevel@tonic-gate  *   EROFS
13070Sstevel@tonic-gate  */
13080Sstevel@tonic-gate static int
13090Sstevel@tonic-gate pg_set_prop_value(scf_propertygroup_t *pg, const char *pname, scf_value_t *v)
13100Sstevel@tonic-gate {
13110Sstevel@tonic-gate 	scf_handle_t *h;
13120Sstevel@tonic-gate 	scf_transaction_t *tx;
13130Sstevel@tonic-gate 	scf_transaction_entry_t *e;
13140Sstevel@tonic-gate 	scf_type_t ty;
13150Sstevel@tonic-gate 	scf_error_t scfe;
13160Sstevel@tonic-gate 	int ret, r;
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	h = scf_pg_handle(pg);
13190Sstevel@tonic-gate 	tx = safe_scf_transaction_create(h);
13200Sstevel@tonic-gate 	e = safe_scf_entry_create(h);
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	ty = scf_value_type(v);
13230Sstevel@tonic-gate 	assert(ty != SCF_TYPE_INVALID);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	for (;;) {
13260Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != 0) {
13270Sstevel@tonic-gate 			switch (scf_error()) {
13280Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
13290Sstevel@tonic-gate 			default:
13300Sstevel@tonic-gate 				ret = ECONNABORTED;
13310Sstevel@tonic-gate 				goto out;
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
13340Sstevel@tonic-gate 				ret = ECANCELED;
13350Sstevel@tonic-gate 				goto out;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
13380Sstevel@tonic-gate 				ret = EPERM;
13390Sstevel@tonic-gate 				goto out;
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
13420Sstevel@tonic-gate 				ret = EACCES;
13430Sstevel@tonic-gate 				goto out;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
13460Sstevel@tonic-gate 				ret = EROFS;
13470Sstevel@tonic-gate 				goto out;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
13500Sstevel@tonic-gate 				bad_error("scf_transaction_start", ret);
13510Sstevel@tonic-gate 			}
13520Sstevel@tonic-gate 		}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 		ret = transaction_add_set(tx, e, pname, ty);
13550Sstevel@tonic-gate 		switch (ret) {
13560Sstevel@tonic-gate 		case 0:
13570Sstevel@tonic-gate 			break;
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 		case ECONNABORTED:
13600Sstevel@tonic-gate 		case ECANCELED:
13610Sstevel@tonic-gate 			goto out;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 		default:
13640Sstevel@tonic-gate 			bad_error("transaction_add_set", ret);
13650Sstevel@tonic-gate 		}
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 		r = scf_entry_add_value(e, v);
13680Sstevel@tonic-gate 		assert(r == 0);
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 		r = scf_transaction_commit(tx);
13710Sstevel@tonic-gate 		if (r == 1)
13720Sstevel@tonic-gate 			break;
13730Sstevel@tonic-gate 		if (r != 0) {
13740Sstevel@tonic-gate 			scfe = scf_error();
13750Sstevel@tonic-gate 			scf_transaction_reset(tx);
13760Sstevel@tonic-gate 			switch (scfe) {
13770Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
13780Sstevel@tonic-gate 			default:
13790Sstevel@tonic-gate 				ret = ECONNABORTED;
13800Sstevel@tonic-gate 				goto out;
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
13830Sstevel@tonic-gate 				ret = ECANCELED;
13840Sstevel@tonic-gate 				goto out;
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
13870Sstevel@tonic-gate 				ret = EPERM;
13880Sstevel@tonic-gate 				goto out;
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
13910Sstevel@tonic-gate 				ret = EACCES;
13920Sstevel@tonic-gate 				goto out;
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
13950Sstevel@tonic-gate 				ret = EROFS;
13960Sstevel@tonic-gate 				goto out;
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
13990Sstevel@tonic-gate 				bad_error("scf_transaction_commit", scfe);
14000Sstevel@tonic-gate 			}
14010Sstevel@tonic-gate 		}
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 		scf_transaction_reset(tx);
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
14060Sstevel@tonic-gate 			switch (scf_error()) {
14070Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
14080Sstevel@tonic-gate 			default:
14090Sstevel@tonic-gate 				ret = ECONNABORTED;
14100Sstevel@tonic-gate 				goto out;
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
14130Sstevel@tonic-gate 				ret = ECANCELED;
14140Sstevel@tonic-gate 				goto out;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
14170Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
14180Sstevel@tonic-gate 			}
14190Sstevel@tonic-gate 		}
14200Sstevel@tonic-gate 	}
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	ret = 0;
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate out:
14250Sstevel@tonic-gate 	scf_transaction_destroy(tx);
14260Sstevel@tonic-gate 	scf_entry_destroy(e);
14270Sstevel@tonic-gate 	return (ret);
14280Sstevel@tonic-gate }
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate /*
14310Sstevel@tonic-gate  * Returns
14320Sstevel@tonic-gate  *   0 - success
14330Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
14340Sstevel@tonic-gate  *		  - unknown libscf error
14350Sstevel@tonic-gate  *   ECANCELED - inst was deleted
14360Sstevel@tonic-gate  *   EPERM
14370Sstevel@tonic-gate  *   EACCES
14380Sstevel@tonic-gate  *   EROFS
14390Sstevel@tonic-gate  */
14400Sstevel@tonic-gate int
14410Sstevel@tonic-gate libscf_inst_set_boolean_prop(scf_instance_t *inst, const char *pgname,
14420Sstevel@tonic-gate     const char *pgtype, uint32_t pgflags, const char *pname, int val)
14430Sstevel@tonic-gate {
14440Sstevel@tonic-gate 	scf_handle_t *h;
14450Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
14460Sstevel@tonic-gate 	scf_value_t *v;
14470Sstevel@tonic-gate 	int ret = 0;
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	h = scf_instance_handle(inst);
14500Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
14510Sstevel@tonic-gate 	v = safe_scf_value_create(h);
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	ret = libscf_inst_get_or_add_pg(inst, pgname, pgtype, pgflags, pg);
14540Sstevel@tonic-gate 	switch (ret) {
14550Sstevel@tonic-gate 	case 0:
14560Sstevel@tonic-gate 		break;
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 	case ECONNABORTED:
14590Sstevel@tonic-gate 	case ECANCELED:
14600Sstevel@tonic-gate 	case EPERM:
14610Sstevel@tonic-gate 	case EACCES:
14620Sstevel@tonic-gate 	case EROFS:
14630Sstevel@tonic-gate 		goto out;
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	default:
14660Sstevel@tonic-gate 		bad_error("libscf_inst_get_or_add_pg", ret);
14670Sstevel@tonic-gate 	}
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	scf_value_set_boolean(v, val);
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	ret = pg_set_prop_value(pg, pname, v);
14720Sstevel@tonic-gate 	switch (ret) {
14730Sstevel@tonic-gate 	case 0:
14740Sstevel@tonic-gate 	case ECONNABORTED:
14750Sstevel@tonic-gate 	case ECANCELED:
14760Sstevel@tonic-gate 	case EPERM:
14770Sstevel@tonic-gate 	case EACCES:
14780Sstevel@tonic-gate 	case EROFS:
14790Sstevel@tonic-gate 		break;
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	default:
14820Sstevel@tonic-gate 		bad_error("pg_set_prop_value", ret);
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate out:
14860Sstevel@tonic-gate 	scf_pg_destroy(pg);
14870Sstevel@tonic-gate 	scf_value_destroy(v);
14880Sstevel@tonic-gate 	return (ret);
14890Sstevel@tonic-gate }
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate /*
14920Sstevel@tonic-gate  * Returns
14930Sstevel@tonic-gate  *   0 - success
14940Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
14950Sstevel@tonic-gate  *		  - unknown libscf error
14960Sstevel@tonic-gate  *   ECANCELED - inst was deleted
14970Sstevel@tonic-gate  *   EPERM
14980Sstevel@tonic-gate  *   EACCES
14990Sstevel@tonic-gate  *   EROFS
15000Sstevel@tonic-gate  */
15010Sstevel@tonic-gate int
15020Sstevel@tonic-gate libscf_inst_set_count_prop(scf_instance_t *inst, const char *pgname,
15030Sstevel@tonic-gate     const char *pgtype, uint32_t pgflags, const char *pname, uint64_t count)
15040Sstevel@tonic-gate {
15050Sstevel@tonic-gate 	scf_handle_t *h;
15060Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
15070Sstevel@tonic-gate 	scf_value_t *v;
15080Sstevel@tonic-gate 	int ret = 0;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	h = scf_instance_handle(inst);
15110Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
15120Sstevel@tonic-gate 	v = safe_scf_value_create(h);
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	ret = libscf_inst_get_or_add_pg(inst, pgname, pgtype, pgflags, pg);
15150Sstevel@tonic-gate 	switch (ret) {
15160Sstevel@tonic-gate 	case 0:
15170Sstevel@tonic-gate 		break;
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	case ECONNABORTED:
15200Sstevel@tonic-gate 	case ECANCELED:
15210Sstevel@tonic-gate 	case EPERM:
15220Sstevel@tonic-gate 	case EACCES:
15230Sstevel@tonic-gate 	case EROFS:
15240Sstevel@tonic-gate 		goto out;
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	default:
15270Sstevel@tonic-gate 		bad_error("libscf_inst_get_or_add_pg", ret);
15280Sstevel@tonic-gate 	}
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 	scf_value_set_count(v, count);
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	ret = pg_set_prop_value(pg, pname, v);
15330Sstevel@tonic-gate 	switch (ret) {
15340Sstevel@tonic-gate 	case 0:
15350Sstevel@tonic-gate 	case ECONNABORTED:
15360Sstevel@tonic-gate 	case ECANCELED:
15370Sstevel@tonic-gate 	case EPERM:
15380Sstevel@tonic-gate 	case EACCES:
15390Sstevel@tonic-gate 	case EROFS:
15400Sstevel@tonic-gate 		break;
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	default:
15430Sstevel@tonic-gate 		bad_error("pg_set_prop_value", ret);
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate out:
15470Sstevel@tonic-gate 	scf_pg_destroy(pg);
15480Sstevel@tonic-gate 	scf_value_destroy(v);
15490Sstevel@tonic-gate 	return (ret);
15500Sstevel@tonic-gate }
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate /*
15530Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED if the repository connection is broken,
15540Sstevel@tonic-gate  * ECANCELED if inst is deleted, EROFS if the backend is readonly, or EPERM if
15550Sstevel@tonic-gate  * permission was denied.
15560Sstevel@tonic-gate  */
15570Sstevel@tonic-gate int
15580Sstevel@tonic-gate libscf_set_enable_ovr(scf_instance_t *inst, int enable)
15590Sstevel@tonic-gate {
15600Sstevel@tonic-gate 	return (libscf_inst_set_boolean_prop(inst, SCF_PG_GENERAL_OVR,
15610Sstevel@tonic-gate 	    SCF_PG_GENERAL_OVR_TYPE, SCF_PG_GENERAL_OVR_FLAGS,
15620Sstevel@tonic-gate 	    SCF_PROPERTY_ENABLED, enable));
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate /*
15667475SPhilippe.Jung@Sun.COM  * Returns 0 on success, ECONNABORTED if the repository connection is broken,
15677475SPhilippe.Jung@Sun.COM  * ECANCELED if inst is deleted, EROFS if the backend is readonly, or EPERM if
15687475SPhilippe.Jung@Sun.COM  * permission was denied.
15697475SPhilippe.Jung@Sun.COM  */
15707475SPhilippe.Jung@Sun.COM int
15717475SPhilippe.Jung@Sun.COM libscf_set_deathrow(scf_instance_t *inst, int deathrow)
15727475SPhilippe.Jung@Sun.COM {
15737475SPhilippe.Jung@Sun.COM 	return (libscf_inst_set_boolean_prop(inst, SCF_PG_DEATHROW,
15747475SPhilippe.Jung@Sun.COM 	    SCF_PG_DEATHROW_TYPE, SCF_PG_DEATHROW_FLAGS,
15757475SPhilippe.Jung@Sun.COM 	    SCF_PROPERTY_DEATHROW, deathrow));
15767475SPhilippe.Jung@Sun.COM }
15777475SPhilippe.Jung@Sun.COM 
15787475SPhilippe.Jung@Sun.COM /*
15790Sstevel@tonic-gate  * Returns
15800Sstevel@tonic-gate  *   0 - success
15810Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
15820Sstevel@tonic-gate  *   ECANCELED - inst was deleted
15830Sstevel@tonic-gate  *   EPERM
15840Sstevel@tonic-gate  *   EACCES
15850Sstevel@tonic-gate  *   EROFS
15860Sstevel@tonic-gate  */
15870Sstevel@tonic-gate int
15880Sstevel@tonic-gate libscf_inst_delete_prop(scf_instance_t *inst, const char *pgname,
15890Sstevel@tonic-gate     const char *pname)
15900Sstevel@tonic-gate {
15910Sstevel@tonic-gate 	scf_handle_t *h;
15920Sstevel@tonic-gate 	scf_propertygroup_t *pg;
15930Sstevel@tonic-gate 	scf_transaction_t *tx;
15940Sstevel@tonic-gate 	scf_transaction_entry_t *e;
15950Sstevel@tonic-gate 	scf_error_t serr;
15960Sstevel@tonic-gate 	int ret = 0, r;
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	h = scf_instance_handle(inst);
15990Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, pg) != 0) {
16020Sstevel@tonic-gate 		scf_pg_destroy(pg);
16030Sstevel@tonic-gate 		switch (scf_error()) {
16040Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
16050Sstevel@tonic-gate 		default:
16060Sstevel@tonic-gate 			return (ECONNABORTED);
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
16090Sstevel@tonic-gate 			return (ECANCELED);
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
16120Sstevel@tonic-gate 			return (0);
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
16150Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
16160Sstevel@tonic-gate 		}
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	tx = safe_scf_transaction_create(h);
16200Sstevel@tonic-gate 	e = safe_scf_entry_create(h);
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	for (;;) {
16230Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != 0) {
16240Sstevel@tonic-gate 			switch (scf_error()) {
16250Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
16260Sstevel@tonic-gate 			default:
16270Sstevel@tonic-gate 				ret = ECONNABORTED;
16280Sstevel@tonic-gate 				goto out;
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
16310Sstevel@tonic-gate 				ret = 0;
16320Sstevel@tonic-gate 				goto out;
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
16350Sstevel@tonic-gate 				ret = EPERM;
16360Sstevel@tonic-gate 				goto out;
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
16390Sstevel@tonic-gate 				ret = EACCES;
16400Sstevel@tonic-gate 				goto out;
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
16430Sstevel@tonic-gate 				ret = EROFS;
16440Sstevel@tonic-gate 				goto out;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
16470Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
16480Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
16490Sstevel@tonic-gate 				bad_error("scf_transaction_start", scf_error());
16500Sstevel@tonic-gate 			}
16510Sstevel@tonic-gate 		}
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 		if (scf_transaction_property_delete(tx, e, pname) != 0) {
16540Sstevel@tonic-gate 			switch (scf_error()) {
16550Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
16560Sstevel@tonic-gate 			default:
16570Sstevel@tonic-gate 				ret = ECONNABORTED;
16580Sstevel@tonic-gate 				goto out;
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
16610Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
16620Sstevel@tonic-gate 				ret = 0;
16630Sstevel@tonic-gate 				goto out;
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
16660Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
16670Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
16680Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
16690Sstevel@tonic-gate 				bad_error("scf_transaction_property_delete",
16700Sstevel@tonic-gate 				    scf_error());
16710Sstevel@tonic-gate 			}
16720Sstevel@tonic-gate 		}
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 		r = scf_transaction_commit(tx);
16750Sstevel@tonic-gate 		if (r == 1)
16760Sstevel@tonic-gate 			break;
16770Sstevel@tonic-gate 		if (r != 0) {
16780Sstevel@tonic-gate 			serr = scf_error();
16790Sstevel@tonic-gate 			scf_transaction_reset(tx);
16800Sstevel@tonic-gate 			switch (serr) {
16810Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
16820Sstevel@tonic-gate 			default:
16830Sstevel@tonic-gate 				ret = ECONNABORTED;
16840Sstevel@tonic-gate 				goto out;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
16870Sstevel@tonic-gate 				ret = 0;
16880Sstevel@tonic-gate 				goto out;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
16910Sstevel@tonic-gate 				ret = EPERM;
16920Sstevel@tonic-gate 				goto out;
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
16950Sstevel@tonic-gate 				ret = EACCES;
16960Sstevel@tonic-gate 				goto out;
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
16990Sstevel@tonic-gate 				ret = EROFS;
17000Sstevel@tonic-gate 				goto out;
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
17030Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
17040Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
17050Sstevel@tonic-gate 				bad_error("scf_transaction_commit", serr);
17060Sstevel@tonic-gate 			}
17070Sstevel@tonic-gate 		}
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate 		scf_transaction_reset(tx);
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
17120Sstevel@tonic-gate 			switch (scf_error()) {
17130Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
17140Sstevel@tonic-gate 			default:
17150Sstevel@tonic-gate 				ret = ECONNABORTED;
17160Sstevel@tonic-gate 				goto out;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
17190Sstevel@tonic-gate 				ret = 0;
17200Sstevel@tonic-gate 				goto out;
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
17230Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
17240Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
17250Sstevel@tonic-gate 			}
17260Sstevel@tonic-gate 		}
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate out:
17300Sstevel@tonic-gate 	scf_transaction_destroy(tx);
17310Sstevel@tonic-gate 	(void) scf_entry_destroy(e);
17320Sstevel@tonic-gate 	scf_pg_destroy(pg);
17330Sstevel@tonic-gate 	return (ret);
17340Sstevel@tonic-gate }
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate /*
17370Sstevel@tonic-gate  * Returns 0, ECONNABORTED, ECANCELED, or EPERM.
17380Sstevel@tonic-gate  */
17390Sstevel@tonic-gate int
17400Sstevel@tonic-gate libscf_delete_enable_ovr(scf_instance_t *inst)
17410Sstevel@tonic-gate {
17420Sstevel@tonic-gate 	return (libscf_inst_delete_prop(inst, SCF_PG_GENERAL_OVR,
17430Sstevel@tonic-gate 	    SCF_PROPERTY_ENABLED));
17440Sstevel@tonic-gate }
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate /*
17470Sstevel@tonic-gate  * Fails with
17480Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
17490Sstevel@tonic-gate  *   ECANCELED - pg was deleted
17500Sstevel@tonic-gate  *   ENOENT - pg has no milestone property
17510Sstevel@tonic-gate  *   EINVAL - the milestone property is misconfigured
17520Sstevel@tonic-gate  */
17530Sstevel@tonic-gate static int
17540Sstevel@tonic-gate pg_get_milestone(scf_propertygroup_t *pg, scf_property_t *prop,
17550Sstevel@tonic-gate     scf_value_t *val, char *buf, size_t buf_sz)
17560Sstevel@tonic-gate {
17570Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_MILESTONE, prop) != 0) {
17580Sstevel@tonic-gate 		switch (scf_error()) {
17590Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
17600Sstevel@tonic-gate 		default:
17610Sstevel@tonic-gate 			return (ECONNABORTED);
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
17640Sstevel@tonic-gate 			return (ECANCELED);
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
17670Sstevel@tonic-gate 			return (ENOENT);
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
17700Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
17710Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
17720Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
17730Sstevel@tonic-gate 		}
17740Sstevel@tonic-gate 	}
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != 0) {
17770Sstevel@tonic-gate 		switch (scf_error()) {
17780Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
17790Sstevel@tonic-gate 		default:
17800Sstevel@tonic-gate 			return (ECONNABORTED);
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
17830Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
17840Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
17850Sstevel@tonic-gate 			return (EINVAL);
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
17885040Swesolows 		case SCF_ERROR_PERMISSION_DENIED:
17890Sstevel@tonic-gate 			bad_error("scf_property_get_value", scf_error());
17900Sstevel@tonic-gate 		}
17910Sstevel@tonic-gate 	}
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	if (scf_value_get_astring(val, buf, buf_sz) < 0) {
17940Sstevel@tonic-gate 		switch (scf_error()) {
17950Sstevel@tonic-gate 		case SCF_ERROR_TYPE_MISMATCH:
17960Sstevel@tonic-gate 			return (EINVAL);
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
17990Sstevel@tonic-gate 		default:
18000Sstevel@tonic-gate 			bad_error("scf_value_get_astring", scf_error());
18010Sstevel@tonic-gate 		}
18020Sstevel@tonic-gate 	}
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 	return (0);
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate /*
18080Sstevel@tonic-gate  * Fails with
18090Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
18100Sstevel@tonic-gate  *   ECANCELED - inst was deleted
18110Sstevel@tonic-gate  *   ENOENT - inst has no milestone property
18120Sstevel@tonic-gate  *   EINVAL - the milestone property is misconfigured
18130Sstevel@tonic-gate  */
18140Sstevel@tonic-gate int
18150Sstevel@tonic-gate libscf_get_milestone(scf_instance_t *inst, scf_property_t *prop,
18160Sstevel@tonic-gate     scf_value_t *val, char *buf, size_t buf_sz)
18170Sstevel@tonic-gate {
18180Sstevel@tonic-gate 	scf_propertygroup_t *pg;
18190Sstevel@tonic-gate 	int r;
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 	pg = safe_scf_pg_create(scf_instance_handle(inst));
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_OPTIONS_OVR, pg) == 0) {
18240Sstevel@tonic-gate 		switch (r = pg_get_milestone(pg, prop, val, buf, buf_sz)) {
18250Sstevel@tonic-gate 		case 0:
18260Sstevel@tonic-gate 		case ECONNABORTED:
18270Sstevel@tonic-gate 		case EINVAL:
18280Sstevel@tonic-gate 			goto out;
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 		case ECANCELED:
18310Sstevel@tonic-gate 		case ENOENT:
18320Sstevel@tonic-gate 			break;
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 		default:
18350Sstevel@tonic-gate 			bad_error("pg_get_milestone", r);
18360Sstevel@tonic-gate 		}
18370Sstevel@tonic-gate 	} else {
18380Sstevel@tonic-gate 		switch (scf_error()) {
18390Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
18400Sstevel@tonic-gate 		default:
18410Sstevel@tonic-gate 			r = ECONNABORTED;
18420Sstevel@tonic-gate 			goto out;
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
18450Sstevel@tonic-gate 			r = ECANCELED;
18460Sstevel@tonic-gate 			goto out;
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
18490Sstevel@tonic-gate 			break;
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
18520Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
18530Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
18540Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
18550Sstevel@tonic-gate 		}
18560Sstevel@tonic-gate 	}
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_OPTIONS, pg) == 0) {
18590Sstevel@tonic-gate 		r = pg_get_milestone(pg, prop, val, buf, buf_sz);
18600Sstevel@tonic-gate 	} else {
18610Sstevel@tonic-gate 		switch (scf_error()) {
18620Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
18630Sstevel@tonic-gate 		default:
18640Sstevel@tonic-gate 			r = ECONNABORTED;
18650Sstevel@tonic-gate 			goto out;
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
18680Sstevel@tonic-gate 			r = ECANCELED;
18690Sstevel@tonic-gate 			goto out;
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
18720Sstevel@tonic-gate 			r = ENOENT;
18730Sstevel@tonic-gate 			break;
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
18760Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
18770Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
18780Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
18790Sstevel@tonic-gate 		}
18800Sstevel@tonic-gate 	}
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate out:
18830Sstevel@tonic-gate 	scf_pg_destroy(pg);
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	return (r);
18860Sstevel@tonic-gate }
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate /*
18890Sstevel@tonic-gate  * Get the runlevel character from the runlevel property of the given property
18900Sstevel@tonic-gate  * group.  Fails with
18910Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
18920Sstevel@tonic-gate  *   ECANCELED - prop's property group was deleted
18930Sstevel@tonic-gate  *   ENOENT - the property has no values
18940Sstevel@tonic-gate  *   EINVAL - the property has more than one value
18950Sstevel@tonic-gate  *	      the property is of the wrong type
18960Sstevel@tonic-gate  *	      the property value is malformed
18970Sstevel@tonic-gate  */
18980Sstevel@tonic-gate int
18990Sstevel@tonic-gate libscf_extract_runlevel(scf_property_t *prop, char *rlp)
19000Sstevel@tonic-gate {
19010Sstevel@tonic-gate 	scf_value_t *val;
19020Sstevel@tonic-gate 	char buf[2];
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	val = safe_scf_value_create(scf_property_handle(prop));
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != 0) {
19070Sstevel@tonic-gate 		switch (scf_error()) {
19080Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
19090Sstevel@tonic-gate 			return (ECONNABORTED);
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
19120Sstevel@tonic-gate 			return (ENOENT);
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
19150Sstevel@tonic-gate 			return (ECANCELED);
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
19180Sstevel@tonic-gate 			return (EINVAL);
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
19210Sstevel@tonic-gate 			return (ENOENT);
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
19240Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
19255040Swesolows 		case SCF_ERROR_PERMISSION_DENIED:
19260Sstevel@tonic-gate 		default:
19270Sstevel@tonic-gate 			bad_error("scf_property_get_value", scf_error());
19280Sstevel@tonic-gate 		}
19290Sstevel@tonic-gate 	}
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 	if (scf_value_get_astring(val, buf, sizeof (buf)) < 0) {
19320Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
19330Sstevel@tonic-gate 			bad_error("scf_value_get_astring", scf_error());
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 		return (EINVAL);
19360Sstevel@tonic-gate 	}
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 	if (buf[0] == '\0' || buf[1] != '\0')
19390Sstevel@tonic-gate 		return (EINVAL);
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	*rlp = buf[0];
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	return (0);
19440Sstevel@tonic-gate }
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate /*
19470Sstevel@tonic-gate  * Delete the "runlevel" property from the given property group.  Also set the
19480Sstevel@tonic-gate  * "milestone" property to the given string.  Fails with ECONNABORTED,
19490Sstevel@tonic-gate  * ECANCELED, EPERM, EACCES, or EROFS.
19500Sstevel@tonic-gate  */
19510Sstevel@tonic-gate int
19520Sstevel@tonic-gate libscf_clear_runlevel(scf_propertygroup_t *pg, const char *milestone)
19530Sstevel@tonic-gate {
19540Sstevel@tonic-gate 	scf_handle_t *h;
19550Sstevel@tonic-gate 	scf_transaction_t *tx;
19560Sstevel@tonic-gate 	scf_transaction_entry_t *e_rl, *e_ms;
19570Sstevel@tonic-gate 	scf_value_t *val;
19580Sstevel@tonic-gate 	scf_error_t serr;
19590Sstevel@tonic-gate 	boolean_t isempty = B_TRUE;
19600Sstevel@tonic-gate 	int ret = 0, r;
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 	h = scf_pg_handle(pg);
19630Sstevel@tonic-gate 	tx = safe_scf_transaction_create(h);
19640Sstevel@tonic-gate 	e_rl = safe_scf_entry_create(h);
19650Sstevel@tonic-gate 	e_ms = safe_scf_entry_create(h);
19660Sstevel@tonic-gate 	val = safe_scf_value_create(h);
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	if (milestone) {
19690Sstevel@tonic-gate 		r = scf_value_set_astring(val, milestone);
19700Sstevel@tonic-gate 		assert(r == 0);
19710Sstevel@tonic-gate 	}
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	for (;;) {
19740Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != 0) {
19750Sstevel@tonic-gate 			switch (scf_error()) {
19760Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
19770Sstevel@tonic-gate 			default:
19780Sstevel@tonic-gate 				ret = ECONNABORTED;
19790Sstevel@tonic-gate 				goto out;
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
19820Sstevel@tonic-gate 				ret = ECANCELED;
19830Sstevel@tonic-gate 				goto out;
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
19860Sstevel@tonic-gate 				ret = EPERM;
19870Sstevel@tonic-gate 				goto out;
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
19900Sstevel@tonic-gate 				ret = EACCES;
19910Sstevel@tonic-gate 				goto out;
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
19940Sstevel@tonic-gate 				ret = EROFS;
19950Sstevel@tonic-gate 				goto out;
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
19980Sstevel@tonic-gate 				bad_error("scf_transaction_start", scf_error());
19990Sstevel@tonic-gate 			}
20000Sstevel@tonic-gate 		}
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 		if (scf_transaction_property_delete(tx, e_rl,
20030Sstevel@tonic-gate 		    "runlevel") == 0) {
20040Sstevel@tonic-gate 			isempty = B_FALSE;
20050Sstevel@tonic-gate 		} else {
20060Sstevel@tonic-gate 			switch (scf_error()) {
20070Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
20080Sstevel@tonic-gate 			default:
20090Sstevel@tonic-gate 				ret = ECONNABORTED;
20100Sstevel@tonic-gate 				goto out;
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
20130Sstevel@tonic-gate 				ret = ECANCELED;
20140Sstevel@tonic-gate 				goto out;
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
20170Sstevel@tonic-gate 				break;
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
20200Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
20210Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
20220Sstevel@tonic-gate 				bad_error("scf_transaction_property_delete",
20230Sstevel@tonic-gate 				    scf_error());
20240Sstevel@tonic-gate 			}
20250Sstevel@tonic-gate 		}
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 		if (milestone) {
20280Sstevel@tonic-gate 			ret = transaction_add_set(tx, e_ms,
20290Sstevel@tonic-gate 			    SCF_PROPERTY_MILESTONE, SCF_TYPE_ASTRING);
20300Sstevel@tonic-gate 			switch (ret) {
20310Sstevel@tonic-gate 			case 0:
20320Sstevel@tonic-gate 				break;
20330Sstevel@tonic-gate 
20340Sstevel@tonic-gate 			case ECONNABORTED:
20350Sstevel@tonic-gate 			case ECANCELED:
20360Sstevel@tonic-gate 				goto out;
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 			default:
20390Sstevel@tonic-gate 				bad_error("transaction_add_set", ret);
20400Sstevel@tonic-gate 			}
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 			isempty = B_FALSE;
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 			r = scf_entry_add_value(e_ms, val);
20450Sstevel@tonic-gate 			assert(r == 0);
20460Sstevel@tonic-gate 		}
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 		if (isempty)
20490Sstevel@tonic-gate 			goto out;
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 		r = scf_transaction_commit(tx);
20520Sstevel@tonic-gate 		if (r == 1)
20530Sstevel@tonic-gate 			break;
20540Sstevel@tonic-gate 		if (r != 0) {
20550Sstevel@tonic-gate 			serr = scf_error();
20560Sstevel@tonic-gate 			scf_transaction_reset(tx);
20570Sstevel@tonic-gate 			switch (serr) {
20580Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
20590Sstevel@tonic-gate 				ret = ECONNABORTED;
20600Sstevel@tonic-gate 				goto out;
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
20630Sstevel@tonic-gate 				ret = EPERM;
20640Sstevel@tonic-gate 				goto out;
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
20670Sstevel@tonic-gate 				ret = EACCES;
20680Sstevel@tonic-gate 				goto out;
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
20710Sstevel@tonic-gate 				ret = EROFS;
20720Sstevel@tonic-gate 				goto out;
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 			default:
20750Sstevel@tonic-gate 				bad_error("scf_transaction_commit", serr);
20760Sstevel@tonic-gate 			}
20770Sstevel@tonic-gate 		}
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 		scf_transaction_reset(tx);
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
20820Sstevel@tonic-gate 			switch (scf_error()) {
20830Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
20840Sstevel@tonic-gate 				ret = ECONNABORTED;
20850Sstevel@tonic-gate 				goto out;
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
20880Sstevel@tonic-gate 				ret = ECANCELED;
20890Sstevel@tonic-gate 				goto out;
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 			default:
20920Sstevel@tonic-gate 				assert(0);
20930Sstevel@tonic-gate 				abort();
20940Sstevel@tonic-gate 			}
20950Sstevel@tonic-gate 		}
20960Sstevel@tonic-gate 	}
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate out:
20990Sstevel@tonic-gate 	scf_transaction_destroy(tx);
21000Sstevel@tonic-gate 	scf_entry_destroy(e_rl);
21010Sstevel@tonic-gate 	scf_entry_destroy(e_ms);
21020Sstevel@tonic-gate 	scf_value_destroy(val);
21030Sstevel@tonic-gate 	return (ret);
21040Sstevel@tonic-gate }
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate /*
21070Sstevel@tonic-gate  * int libscf_get_template_values(scf_instance_t *, scf_snapshot_t *,
21080Sstevel@tonic-gate  *	char **)
21090Sstevel@tonic-gate  *
21100Sstevel@tonic-gate  *   Return template values for inst in *common_name suitable for use in
21110Sstevel@tonic-gate  *   restarter_inst_t->ri_common_name.  Called by restarter_insert_inst().
21120Sstevel@tonic-gate  *
21130Sstevel@tonic-gate  *   Returns 0 on success, ECANCELED if the instance is deleted, ECHILD if
21140Sstevel@tonic-gate  *   a value fetch failed for a property, ENOENT if the instance has no
21150Sstevel@tonic-gate  *   tm_common_name property group or the property group is deleted, and
21160Sstevel@tonic-gate  *   ECONNABORTED if the repository connection is broken.
21170Sstevel@tonic-gate  */
21180Sstevel@tonic-gate int
21190Sstevel@tonic-gate libscf_get_template_values(scf_instance_t *inst, scf_snapshot_t *snap,
21200Sstevel@tonic-gate     char **common_name, char **c_common_name)
21210Sstevel@tonic-gate {
21220Sstevel@tonic-gate 	scf_handle_t *h;
21230Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
21240Sstevel@tonic-gate 	scf_property_t *prop = NULL;
21250Sstevel@tonic-gate 	int ret = 0, r;
21260Sstevel@tonic-gate 	char *cname = startd_alloc(max_scf_value_size);
21270Sstevel@tonic-gate 	char *c_cname = startd_alloc(max_scf_value_size);
2128837Srm88369 	int common_name_initialized = B_FALSE;
2129837Srm88369 	int c_common_name_initialized = B_FALSE;
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 	h = scf_instance_handle(inst);
21320Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
21330Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 	/*
21360Sstevel@tonic-gate 	 * The tm_common_name property group, as with all template property
21370Sstevel@tonic-gate 	 * groups, is optional.
21380Sstevel@tonic-gate 	 */
21390Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, SCF_PG_TM_COMMON_NAME, pg)
21400Sstevel@tonic-gate 	    == -1) {
21410Sstevel@tonic-gate 		switch (scf_error()) {
21420Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
21430Sstevel@tonic-gate 			ret = ECANCELED;
21440Sstevel@tonic-gate 			goto template_values_out;
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
21470Sstevel@tonic-gate 			goto template_values_out;
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
21500Sstevel@tonic-gate 		default:
21510Sstevel@tonic-gate 			ret = ECONNABORTED;
21520Sstevel@tonic-gate 			goto template_values_out;
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
21550Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
21560Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
21570Sstevel@tonic-gate 			bad_error("scf_instance_get_pg_composed", scf_error());
21580Sstevel@tonic-gate 		}
21590Sstevel@tonic-gate 	}
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	/*
21620Sstevel@tonic-gate 	 * The name we wish uses the current locale name as the property name.
21630Sstevel@tonic-gate 	 */
21640Sstevel@tonic-gate 	if (st->st_locale != NULL) {
21650Sstevel@tonic-gate 		if (scf_pg_get_property(pg, st->st_locale, prop) == -1) {
21660Sstevel@tonic-gate 			switch (scf_error()) {
21670Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
21680Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
21690Sstevel@tonic-gate 				break;
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
21720Sstevel@tonic-gate 			default:
21730Sstevel@tonic-gate 				ret = ECONNABORTED;
21740Sstevel@tonic-gate 				goto template_values_out;
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
21770Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
21780Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
21790Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
21800Sstevel@tonic-gate 			}
21810Sstevel@tonic-gate 		} else {
21820Sstevel@tonic-gate 			if ((r = libscf_read_single_astring(h, prop, &cname)) !=
21830Sstevel@tonic-gate 			    0) {
21840Sstevel@tonic-gate 				if (r != LIBSCF_PROPERTY_ABSENT)
21850Sstevel@tonic-gate 					ret = ECHILD;
21860Sstevel@tonic-gate 				goto template_values_out;
21870Sstevel@tonic-gate 			}
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 			*common_name = cname;
2190837Srm88369 			common_name_initialized = B_TRUE;
21910Sstevel@tonic-gate 		}
21920Sstevel@tonic-gate 	}
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 	/*
21950Sstevel@tonic-gate 	 * Also pull out the C locale name, as a fallback for the case where
21960Sstevel@tonic-gate 	 * service offers no localized name.
21970Sstevel@tonic-gate 	 */
21980Sstevel@tonic-gate 	if (scf_pg_get_property(pg, "C", prop) == -1) {
21990Sstevel@tonic-gate 		switch (scf_error()) {
22000Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
22010Sstevel@tonic-gate 			ret = ENOENT;
22020Sstevel@tonic-gate 			goto template_values_out;
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
22050Sstevel@tonic-gate 			break;
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
22080Sstevel@tonic-gate 		default:
22090Sstevel@tonic-gate 			ret = ECONNABORTED;
22100Sstevel@tonic-gate 			goto template_values_out;
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
22130Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
22140Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
22150Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
22160Sstevel@tonic-gate 		}
22170Sstevel@tonic-gate 	} else {
22180Sstevel@tonic-gate 		if ((r = libscf_read_single_astring(h, prop, &c_cname)) != 0) {
22190Sstevel@tonic-gate 			if (r != LIBSCF_PROPERTY_ABSENT)
22200Sstevel@tonic-gate 				ret = ECHILD;
22210Sstevel@tonic-gate 			goto template_values_out;
22220Sstevel@tonic-gate 		}
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 		*c_common_name = c_cname;
2225837Srm88369 		c_common_name_initialized = B_TRUE;
22260Sstevel@tonic-gate 	}
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate template_values_out:
2230837Srm88369 	if (common_name_initialized == B_FALSE)
2231837Srm88369 		startd_free(cname, max_scf_value_size);
2232837Srm88369 	if (c_common_name_initialized == B_FALSE)
2233837Srm88369 		startd_free(c_cname, max_scf_value_size);
22340Sstevel@tonic-gate 	scf_property_destroy(prop);
22350Sstevel@tonic-gate 	scf_pg_destroy(pg);
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	return (ret);
22380Sstevel@tonic-gate }
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate /*
22410Sstevel@tonic-gate  * int libscf_get_startd_properties(scf_handle_t *, scf_instance_t *,
22420Sstevel@tonic-gate  *	scf_snapshot_t *, uint_t *, char **)
22430Sstevel@tonic-gate  *
22440Sstevel@tonic-gate  *   Return startd settings for inst in *flags suitable for use in
22450Sstevel@tonic-gate  *   restarter_inst_t->ri_flags.  Called by restarter_insert_inst().
22460Sstevel@tonic-gate  *
22470Sstevel@tonic-gate  *   Returns 0 on success, ECANCELED if the instance is deleted, ECHILD if
22480Sstevel@tonic-gate  *   a value fetch failed for a property, ENOENT if the instance has no
22490Sstevel@tonic-gate  *   general property group or the property group is deleted, and
22500Sstevel@tonic-gate  *   ECONNABORTED if the repository connection is broken.
22510Sstevel@tonic-gate  */
22520Sstevel@tonic-gate int
22530Sstevel@tonic-gate libscf_get_startd_properties(scf_instance_t *inst,
22540Sstevel@tonic-gate     scf_snapshot_t *snap, uint_t *flags, char **prefixp)
22550Sstevel@tonic-gate {
22560Sstevel@tonic-gate 	scf_handle_t *h;
22570Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
22580Sstevel@tonic-gate 	scf_property_t *prop = NULL;
22590Sstevel@tonic-gate 	int style = RINST_CONTRACT;
22600Sstevel@tonic-gate 	char *style_str = startd_alloc(max_scf_value_size);
22610Sstevel@tonic-gate 	int ret = 0, r;
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	h = scf_instance_handle(inst);
22640Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
22650Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	/*
22680Sstevel@tonic-gate 	 * The startd property group is optional.
22690Sstevel@tonic-gate 	 */
22700Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, SCF_PG_STARTD, pg) == -1) {
22710Sstevel@tonic-gate 		switch (scf_error()) {
22720Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
22730Sstevel@tonic-gate 			ret = ECANCELED;
22740Sstevel@tonic-gate 			goto instance_flags_out;
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
22770Sstevel@tonic-gate 			ret = ENOENT;
22780Sstevel@tonic-gate 			goto instance_flags_out;
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
22810Sstevel@tonic-gate 		default:
22820Sstevel@tonic-gate 			ret = ECONNABORTED;
22830Sstevel@tonic-gate 			goto instance_flags_out;
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
22860Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
22870Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
22880Sstevel@tonic-gate 			bad_error("scf_instance_get_pg_composed", scf_error());
22890Sstevel@tonic-gate 		}
22900Sstevel@tonic-gate 	}
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 	/*
22930Sstevel@tonic-gate 	 * 1.  Duration property.
22940Sstevel@tonic-gate 	 */
22950Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_DURATION, prop) == -1) {
22960Sstevel@tonic-gate 		switch (scf_error()) {
22970Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
22980Sstevel@tonic-gate 			ret = ENOENT;
22990Sstevel@tonic-gate 			goto instance_flags_out;
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
23020Sstevel@tonic-gate 			break;
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
23050Sstevel@tonic-gate 		default:
23060Sstevel@tonic-gate 			ret = ECONNABORTED;
23070Sstevel@tonic-gate 			goto instance_flags_out;
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
23100Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
23110Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
23120Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
23130Sstevel@tonic-gate 		}
23140Sstevel@tonic-gate 	} else {
23150Sstevel@tonic-gate 		errno = 0;
23160Sstevel@tonic-gate 		if ((r = libscf_read_single_astring(h, prop, &style_str))
23170Sstevel@tonic-gate 		    != 0) {
23180Sstevel@tonic-gate 			if (r != LIBSCF_PROPERTY_ABSENT)
23190Sstevel@tonic-gate 				ret = ECHILD;
23200Sstevel@tonic-gate 			goto instance_flags_out;
23210Sstevel@tonic-gate 		}
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate 		if (strcmp(style_str, "child") == 0)
23240Sstevel@tonic-gate 			style = RINST_WAIT;
23250Sstevel@tonic-gate 		else if (strcmp(style_str, "transient") == 0)
23260Sstevel@tonic-gate 			style = RINST_TRANSIENT;
23270Sstevel@tonic-gate 	}
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 	/*
23300Sstevel@tonic-gate 	 * 2.  utmpx prefix property.
23310Sstevel@tonic-gate 	 */
23320Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_UTMPX_PREFIX, prop) == 0) {
23330Sstevel@tonic-gate 		errno = 0;
23340Sstevel@tonic-gate 		if ((r = libscf_read_single_astring(h, prop, prefixp)) != 0) {
23350Sstevel@tonic-gate 			if (r != LIBSCF_PROPERTY_ABSENT)
23360Sstevel@tonic-gate 				ret = ECHILD;
23370Sstevel@tonic-gate 			goto instance_flags_out;
23380Sstevel@tonic-gate 		}
23390Sstevel@tonic-gate 	} else {
23400Sstevel@tonic-gate 		switch (scf_error()) {
23410Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
23420Sstevel@tonic-gate 			ret = ENOENT;
23430Sstevel@tonic-gate 			goto instance_flags_out;
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
23460Sstevel@tonic-gate 			goto instance_flags_out;
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
23490Sstevel@tonic-gate 		default:
23500Sstevel@tonic-gate 			ret = ECONNABORTED;
23510Sstevel@tonic-gate 			goto instance_flags_out;
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
23540Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
23550Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
23560Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
23570Sstevel@tonic-gate 		}
23580Sstevel@tonic-gate 	}
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate instance_flags_out:
23610Sstevel@tonic-gate 	startd_free(style_str, max_scf_value_size);
23620Sstevel@tonic-gate 	*flags = (*flags & ~RINST_STYLE_MASK) | style;
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 	scf_property_destroy(prop);
23650Sstevel@tonic-gate 	scf_pg_destroy(pg);
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	return (ret);
23680Sstevel@tonic-gate }
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate /*
23710Sstevel@tonic-gate  * int libscf_read_method_ids(scf_handle_t *, scf_instance_t *, ctid_t *,
23720Sstevel@tonic-gate  *   ctid_t *, pid_t *)
23730Sstevel@tonic-gate  *
23740Sstevel@tonic-gate  *  Sets given id_t variables to primary and transient contract IDs and start
23750Sstevel@tonic-gate  *  PID.  Returns 0, ECONNABORTED, and ECANCELED.
23760Sstevel@tonic-gate  */
23770Sstevel@tonic-gate int
23780Sstevel@tonic-gate libscf_read_method_ids(scf_handle_t *h, scf_instance_t *inst, const char *fmri,
23790Sstevel@tonic-gate     ctid_t *primary, ctid_t *transient, pid_t *start_pid)
23800Sstevel@tonic-gate {
23810Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
23820Sstevel@tonic-gate 	scf_property_t *prop = NULL;
23830Sstevel@tonic-gate 	scf_value_t *val = NULL;
23840Sstevel@tonic-gate 	uint64_t p, t;
23850Sstevel@tonic-gate 	int ret = 0;
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 	*primary = 0;
23880Sstevel@tonic-gate 	*transient = 0;
23890Sstevel@tonic-gate 	*start_pid = -1;
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
23920Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
23930Sstevel@tonic-gate 	val = safe_scf_value_create(h);
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1) {
23960Sstevel@tonic-gate 		switch (scf_error()) {
23970Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
23980Sstevel@tonic-gate 		default:
23990Sstevel@tonic-gate 			ret = ECONNABORTED;
24000Sstevel@tonic-gate 			goto read_id_err;
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
24030Sstevel@tonic-gate 			ret = ECANCELED;
24040Sstevel@tonic-gate 			goto read_id_err;
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
24070Sstevel@tonic-gate 			goto read_id_err;
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
24100Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
24110Sstevel@tonic-gate 		}
24120Sstevel@tonic-gate 	}
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 	ret = get_count(pg, SCF_PROPERTY_CONTRACT, &p);
24150Sstevel@tonic-gate 	switch (ret) {
24160Sstevel@tonic-gate 	case 0:
24170Sstevel@tonic-gate 		break;
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 	case EINVAL:
24200Sstevel@tonic-gate 		log_error(LOG_NOTICE,
24210Sstevel@tonic-gate 		    "%s: Ignoring %s/%s: multivalued or not of type count\n",
24220Sstevel@tonic-gate 		    fmri, SCF_PG_RESTARTER, SCF_PROPERTY_CONTRACT);
24230Sstevel@tonic-gate 		/* FALLTHROUGH */
24240Sstevel@tonic-gate 	case ENOENT:
24250Sstevel@tonic-gate 		ret = 0;
24260Sstevel@tonic-gate 		goto read_trans;
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	case ECONNABORTED:
24290Sstevel@tonic-gate 	case ECANCELED:
24300Sstevel@tonic-gate 		goto read_id_err;
24310Sstevel@tonic-gate 
24325040Swesolows 	case EACCES:
24330Sstevel@tonic-gate 	default:
24340Sstevel@tonic-gate 		bad_error("get_count", ret);
24350Sstevel@tonic-gate 	}
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 	*primary = p;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate read_trans:
24400Sstevel@tonic-gate 	ret = get_count(pg, SCF_PROPERTY_TRANSIENT_CONTRACT, &t);
24410Sstevel@tonic-gate 	switch (ret) {
24420Sstevel@tonic-gate 	case 0:
24430Sstevel@tonic-gate 		break;
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	case EINVAL:
24460Sstevel@tonic-gate 		log_error(LOG_NOTICE,
24470Sstevel@tonic-gate 		    "%s: Ignoring %s/%s: multivalued or not of type count\n",
24480Sstevel@tonic-gate 		    fmri, SCF_PG_RESTARTER, SCF_PROPERTY_TRANSIENT_CONTRACT);
24490Sstevel@tonic-gate 		/* FALLTHROUGH */
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	case ENOENT:
24520Sstevel@tonic-gate 		ret = 0;
24530Sstevel@tonic-gate 		goto read_pid_only;
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 	case ECONNABORTED:
24560Sstevel@tonic-gate 	case ECANCELED:
24570Sstevel@tonic-gate 		goto read_id_err;
24580Sstevel@tonic-gate 
24595040Swesolows 	case EACCES:
24600Sstevel@tonic-gate 	default:
24610Sstevel@tonic-gate 		bad_error("get_count", ret);
24620Sstevel@tonic-gate 	}
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 	*transient = t;
24650Sstevel@tonic-gate 
24660Sstevel@tonic-gate read_pid_only:
24670Sstevel@tonic-gate 	ret = get_count(pg, SCF_PROPERTY_START_PID, &p);
24680Sstevel@tonic-gate 	switch (ret) {
24690Sstevel@tonic-gate 	case 0:
24700Sstevel@tonic-gate 		break;
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	case EINVAL:
24730Sstevel@tonic-gate 		log_error(LOG_NOTICE,
24740Sstevel@tonic-gate 		    "%s: Ignoring %s/%s: multivalued or not of type count\n",
24750Sstevel@tonic-gate 		    fmri, SCF_PG_RESTARTER, SCF_PROPERTY_START_PID);
24760Sstevel@tonic-gate 		/* FALLTHROUGH */
24770Sstevel@tonic-gate 	case ENOENT:
24780Sstevel@tonic-gate 		ret = 0;
24790Sstevel@tonic-gate 		goto read_id_err;
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	case ECONNABORTED:
24820Sstevel@tonic-gate 	case ECANCELED:
24830Sstevel@tonic-gate 		goto read_id_err;
24840Sstevel@tonic-gate 
24855040Swesolows 	case EACCES:
24860Sstevel@tonic-gate 	default:
24870Sstevel@tonic-gate 		bad_error("get_count", ret);
24880Sstevel@tonic-gate 	}
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	*start_pid = p;
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate read_id_err:
24930Sstevel@tonic-gate 	scf_value_destroy(val);
24940Sstevel@tonic-gate 	scf_property_destroy(prop);
24950Sstevel@tonic-gate 	scf_pg_destroy(pg);
24960Sstevel@tonic-gate 	return (ret);
24970Sstevel@tonic-gate }
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate /*
25000Sstevel@tonic-gate  * Returns with
25010Sstevel@tonic-gate  *   0 - success
25020Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
25030Sstevel@tonic-gate  *		  - unknown libscf error
25040Sstevel@tonic-gate  *   ECANCELED - s_inst was deleted
25050Sstevel@tonic-gate  *   EPERM
25060Sstevel@tonic-gate  *   EACCES
25070Sstevel@tonic-gate  *   EROFS
25080Sstevel@tonic-gate  */
25090Sstevel@tonic-gate int
25100Sstevel@tonic-gate libscf_write_start_pid(scf_instance_t *s_inst, pid_t pid)
25110Sstevel@tonic-gate {
25120Sstevel@tonic-gate 	scf_handle_t *h;
25130Sstevel@tonic-gate 	scf_transaction_entry_t *t_pid;
25140Sstevel@tonic-gate 	scf_value_t *v_pid;
25150Sstevel@tonic-gate 	scf_propertygroup_t *pg;
25160Sstevel@tonic-gate 	int ret = 0;
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate 	h = scf_instance_handle(s_inst);
25190Sstevel@tonic-gate 
25200Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
25210Sstevel@tonic-gate 	t_pid = safe_scf_entry_create(h);
25220Sstevel@tonic-gate 	v_pid = safe_scf_value_create(h);
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate get_pg:
25250Sstevel@tonic-gate 	ret = libscf_inst_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
25260Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
25270Sstevel@tonic-gate 	switch (ret) {
25280Sstevel@tonic-gate 	case 0:
25290Sstevel@tonic-gate 		break;
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	case ECONNABORTED:
25320Sstevel@tonic-gate 	case ECANCELED:
25330Sstevel@tonic-gate 	case EPERM:
25340Sstevel@tonic-gate 	case EACCES:
25350Sstevel@tonic-gate 	case EROFS:
25360Sstevel@tonic-gate 		goto write_start_err;
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	default:
25390Sstevel@tonic-gate 		bad_error("libscf_inst_get_or_add_pg", ret);
25400Sstevel@tonic-gate 	}
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	scf_value_set_count(v_pid, pid);
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 	ret = pg_set_prop_value(pg, SCF_PROPERTY_START_PID, v_pid);
25450Sstevel@tonic-gate 	switch (ret) {
25460Sstevel@tonic-gate 	case 0:
25470Sstevel@tonic-gate 	case ECONNABORTED:
25480Sstevel@tonic-gate 	case EPERM:
25490Sstevel@tonic-gate 	case EACCES:
25500Sstevel@tonic-gate 	case EROFS:
25510Sstevel@tonic-gate 		break;
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	case ECANCELED:
25540Sstevel@tonic-gate 		goto get_pg;
25550Sstevel@tonic-gate 
25560Sstevel@tonic-gate 	default:
25570Sstevel@tonic-gate 		bad_error("pg_set_prop_value", ret);
25580Sstevel@tonic-gate 	}
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate write_start_err:
25610Sstevel@tonic-gate 	scf_entry_destroy(t_pid);
25620Sstevel@tonic-gate 	scf_value_destroy(v_pid);
25630Sstevel@tonic-gate 	scf_pg_destroy(pg);
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 	return (ret);
25660Sstevel@tonic-gate }
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate /*
25690Sstevel@tonic-gate  * Add a property indicating the instance log file.  If the dir is
25700Sstevel@tonic-gate  * equal to LOG_PREFIX_EARLY, then the property restarter/alt_logfile
25710Sstevel@tonic-gate  * of the instance is used; otherwise, restarter/logfile is used.
25720Sstevel@tonic-gate  *
25730Sstevel@tonic-gate  * Returns
25740Sstevel@tonic-gate  *   0 - success
25750Sstevel@tonic-gate  *   ECONNABORTED
25760Sstevel@tonic-gate  *   ECANCELED
25770Sstevel@tonic-gate  *   EPERM
25780Sstevel@tonic-gate  *   EACCES
25790Sstevel@tonic-gate  *   EROFS
25800Sstevel@tonic-gate  *   EAGAIN
25810Sstevel@tonic-gate  */
25820Sstevel@tonic-gate int
25830Sstevel@tonic-gate libscf_note_method_log(scf_instance_t *inst, const char *dir, const char *file)
25840Sstevel@tonic-gate {
25850Sstevel@tonic-gate 	scf_handle_t *h;
25860Sstevel@tonic-gate 	scf_value_t *v;
25870Sstevel@tonic-gate 	scf_propertygroup_t *pg;
25880Sstevel@tonic-gate 	int ret = 0;
25890Sstevel@tonic-gate 	char *logname;
25900Sstevel@tonic-gate 	const char *propname;
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 	h = scf_instance_handle(inst);
25930Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
25940Sstevel@tonic-gate 	v = safe_scf_value_create(h);
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 	logname = uu_msprintf("%s%s", dir, file);
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 	if (logname == NULL) {
25990Sstevel@tonic-gate 		ret = errno;
26000Sstevel@tonic-gate 		goto out;
26010Sstevel@tonic-gate 	}
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 	ret = libscf_inst_get_or_add_pg(inst, SCF_PG_RESTARTER,
26040Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
26050Sstevel@tonic-gate 	switch (ret) {
26060Sstevel@tonic-gate 	case 0:
26070Sstevel@tonic-gate 		break;
26080Sstevel@tonic-gate 
26090Sstevel@tonic-gate 	case ECONNABORTED:
26100Sstevel@tonic-gate 	case ECANCELED:
26110Sstevel@tonic-gate 	case EPERM:
26120Sstevel@tonic-gate 	case EACCES:
26130Sstevel@tonic-gate 	case EROFS:
26140Sstevel@tonic-gate 		goto out;
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 	default:
26170Sstevel@tonic-gate 		bad_error("libscf_inst_get_or_add_pg", ret);
26180Sstevel@tonic-gate 	}
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate 	(void) scf_value_set_astring(v, logname);
26210Sstevel@tonic-gate 
26220Sstevel@tonic-gate 	if (strcmp(LOG_PREFIX_EARLY, dir) == 0)
26230Sstevel@tonic-gate 		propname = SCF_PROPERTY_ALT_LOGFILE;
26240Sstevel@tonic-gate 	else
26250Sstevel@tonic-gate 		propname = SCF_PROPERTY_LOGFILE;
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	ret = pg_set_prop_value(pg, propname, v);
26280Sstevel@tonic-gate 	switch (ret) {
26290Sstevel@tonic-gate 	case 0:
26300Sstevel@tonic-gate 	case ECONNABORTED:
26310Sstevel@tonic-gate 	case ECANCELED:
26320Sstevel@tonic-gate 	case EPERM:
26330Sstevel@tonic-gate 	case EACCES:
26340Sstevel@tonic-gate 	case EROFS:
26350Sstevel@tonic-gate 		break;
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	default:
26380Sstevel@tonic-gate 		bad_error("pg_set_prop_value", ret);
26390Sstevel@tonic-gate 	}
26400Sstevel@tonic-gate 
26410Sstevel@tonic-gate out:
26420Sstevel@tonic-gate 	scf_pg_destroy(pg);
26430Sstevel@tonic-gate 	scf_value_destroy(v);
26440Sstevel@tonic-gate 	uu_free(logname);
26450Sstevel@tonic-gate 	return (ret);
26460Sstevel@tonic-gate }
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate /*
26490Sstevel@tonic-gate  * Returns
26500Sstevel@tonic-gate  *   0 - success
26510Sstevel@tonic-gate  *   ENAMETOOLONG - name is too long
26520Sstevel@tonic-gate  *   ECONNABORTED
26530Sstevel@tonic-gate  *   ECANCELED
26540Sstevel@tonic-gate  *   EPERM
26550Sstevel@tonic-gate  *   EACCES
26560Sstevel@tonic-gate  *   EROFS
26570Sstevel@tonic-gate  */
26580Sstevel@tonic-gate int
26590Sstevel@tonic-gate libscf_write_method_status(scf_instance_t *s_inst, const char *name,
26600Sstevel@tonic-gate     int status)
26610Sstevel@tonic-gate {
26620Sstevel@tonic-gate 	scf_handle_t *h;
26630Sstevel@tonic-gate 	scf_transaction_t *tx;
26640Sstevel@tonic-gate 	scf_transaction_entry_t *e_time, *e_stat;
26650Sstevel@tonic-gate 	scf_value_t *v_time, *v_stat;
26660Sstevel@tonic-gate 	scf_propertygroup_t *pg;
26670Sstevel@tonic-gate 	int ret = 0, r;
26680Sstevel@tonic-gate 	char pname[30];
26690Sstevel@tonic-gate 	struct timeval tv;
26700Sstevel@tonic-gate 	scf_error_t scfe;
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate 	if (strlen(name) + sizeof ("_method_waitstatus") > sizeof (pname))
26730Sstevel@tonic-gate 		return (ENAMETOOLONG);
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	h = scf_instance_handle(s_inst);
26760Sstevel@tonic-gate 
26770Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
26780Sstevel@tonic-gate 	tx = safe_scf_transaction_create(h);
26790Sstevel@tonic-gate 	e_time = safe_scf_entry_create(h);
26800Sstevel@tonic-gate 	v_time = safe_scf_value_create(h);
26810Sstevel@tonic-gate 	e_stat = safe_scf_entry_create(h);
26820Sstevel@tonic-gate 	v_stat = safe_scf_value_create(h);
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate get_pg:
26850Sstevel@tonic-gate 	ret = libscf_inst_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
26860Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
26870Sstevel@tonic-gate 	switch (ret) {
26880Sstevel@tonic-gate 	case 0:
26890Sstevel@tonic-gate 		break;
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	case ECONNABORTED:
26920Sstevel@tonic-gate 	case ECANCELED:
26930Sstevel@tonic-gate 	case EPERM:
26940Sstevel@tonic-gate 	case EACCES:
26950Sstevel@tonic-gate 	case EROFS:
26960Sstevel@tonic-gate 		goto out;
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate 	default:
26990Sstevel@tonic-gate 		bad_error("libscf_inst_get_or_add_pg", ret);
27000Sstevel@tonic-gate 	}
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	(void) gettimeofday(&tv, NULL);
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate 	r = scf_value_set_time(v_time, tv.tv_sec, tv.tv_usec * 1000);
27050Sstevel@tonic-gate 	assert(r == 0);
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	scf_value_set_integer(v_stat, status);
27080Sstevel@tonic-gate 
27090Sstevel@tonic-gate 	for (;;) {
27100Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != 0) {
27110Sstevel@tonic-gate 			switch (scf_error()) {
27120Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
27130Sstevel@tonic-gate 			default:
27140Sstevel@tonic-gate 				ret = ECONNABORTED;
27150Sstevel@tonic-gate 				goto out;
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
27180Sstevel@tonic-gate 				ret = ECANCELED;
27190Sstevel@tonic-gate 				goto out;
27200Sstevel@tonic-gate 
27210Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
27220Sstevel@tonic-gate 				ret = EPERM;
27230Sstevel@tonic-gate 				goto out;
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
27260Sstevel@tonic-gate 				ret = EACCES;
27270Sstevel@tonic-gate 				goto out;
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
27300Sstevel@tonic-gate 				ret = EROFS;
27310Sstevel@tonic-gate 				goto out;
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
27340Sstevel@tonic-gate 				bad_error("scf_transaction_start", ret);
27350Sstevel@tonic-gate 			}
27360Sstevel@tonic-gate 		}
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "%s_method_timestamp",
27390Sstevel@tonic-gate 		    name);
27400Sstevel@tonic-gate 		ret = transaction_add_set(tx, e_time, pname, SCF_TYPE_TIME);
27410Sstevel@tonic-gate 		switch (ret) {
27420Sstevel@tonic-gate 		case 0:
27430Sstevel@tonic-gate 			break;
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 		case ECONNABORTED:
27460Sstevel@tonic-gate 		case ECANCELED:
27470Sstevel@tonic-gate 			goto out;
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 		default:
27500Sstevel@tonic-gate 			bad_error("transaction_add_set", ret);
27510Sstevel@tonic-gate 		}
27520Sstevel@tonic-gate 
27530Sstevel@tonic-gate 		r = scf_entry_add_value(e_time, v_time);
27540Sstevel@tonic-gate 		assert(r == 0);
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "%s_method_waitstatus",
27570Sstevel@tonic-gate 		    name);
27580Sstevel@tonic-gate 		ret = transaction_add_set(tx, e_stat, pname, SCF_TYPE_INTEGER);
27590Sstevel@tonic-gate 		switch (ret) {
27600Sstevel@tonic-gate 		case 0:
27610Sstevel@tonic-gate 			break;
27620Sstevel@tonic-gate 
27630Sstevel@tonic-gate 		case ECONNABORTED:
27640Sstevel@tonic-gate 		case ECANCELED:
27650Sstevel@tonic-gate 			goto out;
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 		default:
27680Sstevel@tonic-gate 			bad_error("transaction_add_set", ret);
27690Sstevel@tonic-gate 		}
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 		r = scf_entry_add_value(e_stat, v_stat);
27720Sstevel@tonic-gate 		if (r != 0)
27730Sstevel@tonic-gate 			bad_error("scf_entry_add_value", scf_error());
27740Sstevel@tonic-gate 
27750Sstevel@tonic-gate 		r = scf_transaction_commit(tx);
27760Sstevel@tonic-gate 		if (r == 1)
27770Sstevel@tonic-gate 			break;
27780Sstevel@tonic-gate 		if (r != 0) {
27790Sstevel@tonic-gate 			scfe = scf_error();
27800Sstevel@tonic-gate 			scf_transaction_reset_all(tx);
27810Sstevel@tonic-gate 			switch (scfe) {
27820Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
27830Sstevel@tonic-gate 			default:
27840Sstevel@tonic-gate 				ret = ECONNABORTED;
27850Sstevel@tonic-gate 				goto out;
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
27880Sstevel@tonic-gate 				ret = ECANCELED;
27890Sstevel@tonic-gate 				goto out;
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
27920Sstevel@tonic-gate 				ret = EPERM;
27930Sstevel@tonic-gate 				goto out;
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
27960Sstevel@tonic-gate 				ret = EACCES;
27970Sstevel@tonic-gate 				goto out;
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
28000Sstevel@tonic-gate 				ret = EROFS;
28010Sstevel@tonic-gate 				goto out;
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
28040Sstevel@tonic-gate 				bad_error("scf_transaction_commit", scfe);
28050Sstevel@tonic-gate 			}
28060Sstevel@tonic-gate 		}
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate 		scf_transaction_reset_all(tx);
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
28110Sstevel@tonic-gate 			switch (scf_error()) {
28120Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
28130Sstevel@tonic-gate 			default:
28140Sstevel@tonic-gate 				ret = ECONNABORTED;
28150Sstevel@tonic-gate 				goto out;
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
28180Sstevel@tonic-gate 				ret = ECANCELED;
28190Sstevel@tonic-gate 				goto out;
28200Sstevel@tonic-gate 
28210Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
28220Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
28230Sstevel@tonic-gate 			}
28240Sstevel@tonic-gate 		}
28250Sstevel@tonic-gate 	}
28260Sstevel@tonic-gate 
28270Sstevel@tonic-gate out:
28280Sstevel@tonic-gate 	scf_transaction_destroy(tx);
28290Sstevel@tonic-gate 	scf_entry_destroy(e_time);
28300Sstevel@tonic-gate 	scf_value_destroy(v_time);
28310Sstevel@tonic-gate 	scf_entry_destroy(e_stat);
28320Sstevel@tonic-gate 	scf_value_destroy(v_stat);
28330Sstevel@tonic-gate 	scf_pg_destroy(pg);
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate 	return (ret);
28360Sstevel@tonic-gate }
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate /*
28390Sstevel@tonic-gate  * Call dgraph_add_instance() for each instance in the repository.
28400Sstevel@tonic-gate  */
28410Sstevel@tonic-gate void
28420Sstevel@tonic-gate libscf_populate_graph(scf_handle_t *h)
28430Sstevel@tonic-gate {
28440Sstevel@tonic-gate 	scf_scope_t *scope;
28450Sstevel@tonic-gate 	scf_service_t *svc;
28460Sstevel@tonic-gate 	scf_instance_t *inst;
28470Sstevel@tonic-gate 	scf_iter_t *svc_iter;
28480Sstevel@tonic-gate 	scf_iter_t *inst_iter;
28490Sstevel@tonic-gate 	int ret;
28500Sstevel@tonic-gate 
28510Sstevel@tonic-gate 	scope = safe_scf_scope_create(h);
28520Sstevel@tonic-gate 	svc = safe_scf_service_create(h);
28530Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
28540Sstevel@tonic-gate 	svc_iter = safe_scf_iter_create(h);
28550Sstevel@tonic-gate 	inst_iter = safe_scf_iter_create(h);
28560Sstevel@tonic-gate 
28577475SPhilippe.Jung@Sun.COM 	deathrow_init();
28587475SPhilippe.Jung@Sun.COM 
28590Sstevel@tonic-gate 	if ((ret = scf_handle_get_local_scope(h, scope)) !=
28600Sstevel@tonic-gate 	    SCF_SUCCESS)
28610Sstevel@tonic-gate 		uu_die("retrieving local scope failed: %d\n", ret);
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	if (scf_iter_scope_services(svc_iter, scope) == -1)
28640Sstevel@tonic-gate 		uu_die("walking local scope's services failed\n");
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	while (scf_iter_next_service(svc_iter, svc) > 0) {
28670Sstevel@tonic-gate 		if (scf_iter_service_instances(inst_iter, svc) == -1)
28680Sstevel@tonic-gate 			uu_die("unable to walk service's instances");
28690Sstevel@tonic-gate 
28700Sstevel@tonic-gate 		while (scf_iter_next_instance(inst_iter, inst) > 0) {
28710Sstevel@tonic-gate 			char *fmri;
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 			if (libscf_instance_get_fmri(inst, &fmri) == 0) {
28740Sstevel@tonic-gate 				int err;
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 				err = dgraph_add_instance(fmri, inst, B_TRUE);
28770Sstevel@tonic-gate 				if (err != 0 && err != EEXIST)
28780Sstevel@tonic-gate 					log_error(LOG_WARNING,
28790Sstevel@tonic-gate 					    "Failed to add %s (%s).\n", fmri,
28800Sstevel@tonic-gate 					    strerror(err));
28810Sstevel@tonic-gate 				startd_free(fmri, max_scf_fmri_size);
28820Sstevel@tonic-gate 			}
28830Sstevel@tonic-gate 		}
28840Sstevel@tonic-gate 	}
28850Sstevel@tonic-gate 
28867475SPhilippe.Jung@Sun.COM 	deathrow_fini();
28877475SPhilippe.Jung@Sun.COM 
28880Sstevel@tonic-gate 	scf_iter_destroy(inst_iter);
28890Sstevel@tonic-gate 	scf_iter_destroy(svc_iter);
28900Sstevel@tonic-gate 	scf_instance_destroy(inst);
28910Sstevel@tonic-gate 	scf_service_destroy(svc);
28920Sstevel@tonic-gate 	scf_scope_destroy(scope);
28930Sstevel@tonic-gate }
28940Sstevel@tonic-gate 
28950Sstevel@tonic-gate /*
28960Sstevel@tonic-gate  * Monitors get handled differently since there can be multiple of them.
28970Sstevel@tonic-gate  *
28980Sstevel@tonic-gate  * Returns exec string on success.  If method not defined, returns
28990Sstevel@tonic-gate  * LIBSCF_PGROUP_ABSENT; if exec property missing, returns
29000Sstevel@tonic-gate  * LIBSCF_PROPERTY_ABSENT.  Returns LIBSCF_PROPERTY_ERROR on other failures.
29010Sstevel@tonic-gate  */
29020Sstevel@tonic-gate char *
29030Sstevel@tonic-gate libscf_get_method(scf_handle_t *h, int type, restarter_inst_t *inst,
29040Sstevel@tonic-gate     scf_snapshot_t *snap, method_restart_t *restart_on, uint_t *cte_mask,
29050Sstevel@tonic-gate     uint8_t *need_sessionp, uint64_t *timeout, uint8_t *timeout_retry)
29060Sstevel@tonic-gate {
29070Sstevel@tonic-gate 	scf_instance_t *scf_inst = NULL;
29080Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL, *pg_startd = NULL;
29090Sstevel@tonic-gate 	scf_property_t *prop = NULL;
29100Sstevel@tonic-gate 	const char *name;
29110Sstevel@tonic-gate 	char *method = startd_alloc(max_scf_value_size);
29120Sstevel@tonic-gate 	char *ig = startd_alloc(max_scf_value_size);
29130Sstevel@tonic-gate 	char *restart = startd_alloc(max_scf_value_size);
29140Sstevel@tonic-gate 	char *ret;
29150Sstevel@tonic-gate 	int error = 0, r;
29160Sstevel@tonic-gate 
29170Sstevel@tonic-gate 	scf_inst = safe_scf_instance_create(h);
29180Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
29190Sstevel@tonic-gate 	pg_startd = safe_scf_pg_create(h);
29200Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
29210Sstevel@tonic-gate 
29220Sstevel@tonic-gate 	ret = NULL;
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate 	*restart_on = METHOD_RESTART_UNKNOWN;
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate 	switch (type) {
29270Sstevel@tonic-gate 	case METHOD_START:
29280Sstevel@tonic-gate 		name = "start";
29290Sstevel@tonic-gate 		break;
29300Sstevel@tonic-gate 	case METHOD_STOP:
29310Sstevel@tonic-gate 		name = "stop";
29320Sstevel@tonic-gate 		break;
29330Sstevel@tonic-gate 	case METHOD_REFRESH:
29340Sstevel@tonic-gate 		name = "refresh";
29350Sstevel@tonic-gate 		break;
29360Sstevel@tonic-gate 	default:
29370Sstevel@tonic-gate 		error = LIBSCF_PROPERTY_ERROR;
29380Sstevel@tonic-gate 		goto get_method_cleanup;
29390Sstevel@tonic-gate 	}
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, inst->ri_i.i_fmri, NULL, NULL, scf_inst,
29420Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1) {
29430Sstevel@tonic-gate 		log_error(LOG_WARNING,
29440Sstevel@tonic-gate 		    "%s: get_method decode instance FMRI failed: %s\n",
29450Sstevel@tonic-gate 		    inst->ri_i.i_fmri, scf_strerror(scf_error()));
29460Sstevel@tonic-gate 		error = LIBSCF_PROPERTY_ERROR;
29470Sstevel@tonic-gate 		goto get_method_cleanup;
29480Sstevel@tonic-gate 	}
29490Sstevel@tonic-gate 
29500Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(scf_inst, snap, name, pg) == -1) {
29510Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
29520Sstevel@tonic-gate 			error = LIBSCF_PGROUP_ABSENT;
29530Sstevel@tonic-gate 		else
29540Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
29550Sstevel@tonic-gate 		goto get_method_cleanup;
29560Sstevel@tonic-gate 	}
29570Sstevel@tonic-gate 
29580Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_EXEC, prop) == -1) {
29590Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
29600Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ABSENT;
29610Sstevel@tonic-gate 		else
29620Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
29630Sstevel@tonic-gate 		goto get_method_cleanup;
29640Sstevel@tonic-gate 	}
29650Sstevel@tonic-gate 
29660Sstevel@tonic-gate 	error = libscf_read_single_astring(h, prop, &method);
29670Sstevel@tonic-gate 	if (error != 0) {
29680Sstevel@tonic-gate 		log_error(LOG_WARNING,
29690Sstevel@tonic-gate 		    "%s: get_method failed: can't get a single astring "
29700Sstevel@tonic-gate 		    "from %s/%s\n", inst->ri_i.i_fmri, name, SCF_PROPERTY_EXEC);
29710Sstevel@tonic-gate 		goto get_method_cleanup;
29720Sstevel@tonic-gate 	}
29730Sstevel@tonic-gate 
29740Sstevel@tonic-gate 	error = expand_method_tokens(method, scf_inst, snap, type, &ret);
29750Sstevel@tonic-gate 	if (error != 0) {
29760Sstevel@tonic-gate 		log_instance(inst, B_TRUE, "Could not expand method tokens "
29775238Slianep 		    "in \"%s\": %s.", method, ret);
29780Sstevel@tonic-gate 		error = LIBSCF_PROPERTY_ERROR;
29790Sstevel@tonic-gate 		goto get_method_cleanup;
29800Sstevel@tonic-gate 	}
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	r = get_count(pg, SCF_PROPERTY_TIMEOUT, timeout);
29830Sstevel@tonic-gate 	switch (r) {
29840Sstevel@tonic-gate 	case 0:
29850Sstevel@tonic-gate 		break;
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate 	case ECONNABORTED:
29880Sstevel@tonic-gate 		error = LIBSCF_PROPERTY_ERROR;
29890Sstevel@tonic-gate 		goto get_method_cleanup;
29900Sstevel@tonic-gate 
29910Sstevel@tonic-gate 	case EINVAL:
29920Sstevel@tonic-gate 		log_instance(inst, B_TRUE, "%s/%s is multi-valued or not of "
29930Sstevel@tonic-gate 		    "type count.  Using infinite timeout.", name,
29940Sstevel@tonic-gate 		    SCF_PROPERTY_TIMEOUT);
29950Sstevel@tonic-gate 		/* FALLTHROUGH */
29960Sstevel@tonic-gate 	case ECANCELED:
29970Sstevel@tonic-gate 	case ENOENT:
29980Sstevel@tonic-gate 		*timeout = METHOD_TIMEOUT_INFINITE;
29990Sstevel@tonic-gate 		break;
30000Sstevel@tonic-gate 
30015040Swesolows 	case EACCES:
30020Sstevel@tonic-gate 	default:
30030Sstevel@tonic-gate 		bad_error("get_count", r);
30040Sstevel@tonic-gate 	}
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	/* Both 0 and -1 (ugh) are considered infinite timeouts. */
30070Sstevel@tonic-gate 	if (*timeout == -1 || *timeout == 0)
30080Sstevel@tonic-gate 		*timeout = METHOD_TIMEOUT_INFINITE;
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(scf_inst, snap, SCF_PG_STARTD,
30110Sstevel@tonic-gate 	    pg_startd) == -1) {
30120Sstevel@tonic-gate 		switch (scf_error()) {
30130Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
30140Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
30150Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
30160Sstevel@tonic-gate 			goto get_method_cleanup;
30170Sstevel@tonic-gate 
30180Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
30190Sstevel@tonic-gate 			*cte_mask = 0;
30200Sstevel@tonic-gate 			break;
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
30230Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
30240Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
30250Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
30260Sstevel@tonic-gate 			bad_error("scf_instance_get_pg_composed", scf_error());
30270Sstevel@tonic-gate 		}
30280Sstevel@tonic-gate 	} else {
30290Sstevel@tonic-gate 		if (scf_pg_get_property(pg_startd, SCF_PROPERTY_IGNORE,
30300Sstevel@tonic-gate 		    prop) == -1) {
30310Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_FOUND)
30320Sstevel@tonic-gate 				*cte_mask = 0;
30330Sstevel@tonic-gate 			else {
30340Sstevel@tonic-gate 				error = LIBSCF_PROPERTY_ERROR;
30350Sstevel@tonic-gate 				goto get_method_cleanup;
30360Sstevel@tonic-gate 			}
30370Sstevel@tonic-gate 		} else {
30380Sstevel@tonic-gate 			error = libscf_read_single_astring(h, prop, &ig);
30390Sstevel@tonic-gate 			if (error != 0) {
30400Sstevel@tonic-gate 				log_error(LOG_WARNING,
30410Sstevel@tonic-gate 				    "%s: get_method failed: can't get a single "
30420Sstevel@tonic-gate 				    "astring from %s/%s\n", inst->ri_i.i_fmri,
30430Sstevel@tonic-gate 				    name, SCF_PROPERTY_IGNORE);
30440Sstevel@tonic-gate 				goto get_method_cleanup;
30450Sstevel@tonic-gate 			}
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 			if (strcmp(ig, "core") == 0)
30480Sstevel@tonic-gate 				*cte_mask = CT_PR_EV_CORE;
30490Sstevel@tonic-gate 			else if (strcmp(ig, "signal") == 0)
30500Sstevel@tonic-gate 				*cte_mask = CT_PR_EV_SIGNAL;
30510Sstevel@tonic-gate 			else if (strcmp(ig, "core,signal") == 0 ||
30520Sstevel@tonic-gate 			    strcmp(ig, "signal,core") == 0)
30530Sstevel@tonic-gate 				*cte_mask = CT_PR_EV_CORE | CT_PR_EV_SIGNAL;
30540Sstevel@tonic-gate 			else
30550Sstevel@tonic-gate 				*cte_mask = 0;
30560Sstevel@tonic-gate 		}
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate 		r = get_boolean(pg_startd, SCF_PROPERTY_NEED_SESSION,
30590Sstevel@tonic-gate 		    need_sessionp);
30600Sstevel@tonic-gate 		switch (r) {
30610Sstevel@tonic-gate 		case 0:
30620Sstevel@tonic-gate 			break;
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate 		case ECONNABORTED:
30650Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
30660Sstevel@tonic-gate 			goto get_method_cleanup;
30670Sstevel@tonic-gate 
30680Sstevel@tonic-gate 		case ECANCELED:
30690Sstevel@tonic-gate 		case ENOENT:
30700Sstevel@tonic-gate 		case EINVAL:
30710Sstevel@tonic-gate 			*need_sessionp = 0;
30720Sstevel@tonic-gate 			break;
30730Sstevel@tonic-gate 
30745040Swesolows 		case EACCES:
30750Sstevel@tonic-gate 		default:
30760Sstevel@tonic-gate 			bad_error("get_boolean", r);
30770Sstevel@tonic-gate 		}
30780Sstevel@tonic-gate 
30790Sstevel@tonic-gate 		/*
30800Sstevel@tonic-gate 		 * Determine whether service has overriden retry after
30810Sstevel@tonic-gate 		 * method timeout.  Default to retry if no value is
30820Sstevel@tonic-gate 		 * specified.
30830Sstevel@tonic-gate 		 */
30840Sstevel@tonic-gate 		r = get_boolean(pg_startd, SCF_PROPERTY_TIMEOUT_RETRY,
30850Sstevel@tonic-gate 		    timeout_retry);
30860Sstevel@tonic-gate 		switch (r) {
30870Sstevel@tonic-gate 		case 0:
30880Sstevel@tonic-gate 			break;
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate 		case ECONNABORTED:
30910Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
30920Sstevel@tonic-gate 			goto get_method_cleanup;
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 		case ECANCELED:
30950Sstevel@tonic-gate 		case ENOENT:
30960Sstevel@tonic-gate 		case EINVAL:
30970Sstevel@tonic-gate 			*timeout_retry = 1;
30980Sstevel@tonic-gate 			break;
30990Sstevel@tonic-gate 
31005040Swesolows 		case EACCES:
31010Sstevel@tonic-gate 		default:
31020Sstevel@tonic-gate 			bad_error("get_boolean", r);
31030Sstevel@tonic-gate 		}
31040Sstevel@tonic-gate 	}
31050Sstevel@tonic-gate 
31060Sstevel@tonic-gate 	if (type != METHOD_START)
31070Sstevel@tonic-gate 		goto get_method_cleanup;
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate 	/* Only start methods need to honor the restart_on property. */
31100Sstevel@tonic-gate 
31110Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_RESTART_ON, prop) == -1) {
31120Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
31130Sstevel@tonic-gate 			*restart_on = METHOD_RESTART_ALL;
31140Sstevel@tonic-gate 		else
31150Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
31160Sstevel@tonic-gate 		goto get_method_cleanup;
31170Sstevel@tonic-gate 	}
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate 	error = libscf_read_single_astring(h, prop, &restart);
31200Sstevel@tonic-gate 	if (error != 0) {
31210Sstevel@tonic-gate 		log_error(LOG_WARNING,
31220Sstevel@tonic-gate 		    "%s: get_method failed: can't get a single astring "
31230Sstevel@tonic-gate 		    "from %s/%s\n", inst->ri_i.i_fmri, name,
31240Sstevel@tonic-gate 		    SCF_PROPERTY_RESTART_ON);
31250Sstevel@tonic-gate 		goto get_method_cleanup;
31260Sstevel@tonic-gate 	}
31270Sstevel@tonic-gate 
31280Sstevel@tonic-gate 	if (strcmp(restart, "all") == 0)
31290Sstevel@tonic-gate 		*restart_on = METHOD_RESTART_ALL;
31300Sstevel@tonic-gate 	else if (strcmp(restart, "external_fault") == 0)
31310Sstevel@tonic-gate 		*restart_on = METHOD_RESTART_EXTERNAL_FAULT;
31320Sstevel@tonic-gate 	else if (strcmp(restart, "any_fault") == 0)
31330Sstevel@tonic-gate 		*restart_on = METHOD_RESTART_ANY_FAULT;
31340Sstevel@tonic-gate 
31350Sstevel@tonic-gate get_method_cleanup:
31360Sstevel@tonic-gate 	startd_free(ig, max_scf_value_size);
31370Sstevel@tonic-gate 	startd_free(method, max_scf_value_size);
31380Sstevel@tonic-gate 	startd_free(restart, max_scf_value_size);
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate 	scf_instance_destroy(scf_inst);
31410Sstevel@tonic-gate 	scf_pg_destroy(pg);
31420Sstevel@tonic-gate 	scf_pg_destroy(pg_startd);
31430Sstevel@tonic-gate 	scf_property_destroy(prop);
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate 	if (error != 0 && ret != NULL) {
31460Sstevel@tonic-gate 		free(ret);
31470Sstevel@tonic-gate 		ret = NULL;
31480Sstevel@tonic-gate 	}
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate 	errno = error;
31510Sstevel@tonic-gate 	return (ret);
31520Sstevel@tonic-gate }
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate /*
31550Sstevel@tonic-gate  * Returns 1 if we've reached the fault threshold
31560Sstevel@tonic-gate  */
31570Sstevel@tonic-gate int
31580Sstevel@tonic-gate update_fault_count(restarter_inst_t *inst, int type)
31590Sstevel@tonic-gate {
31600Sstevel@tonic-gate 	assert(type == FAULT_COUNT_INCR || type == FAULT_COUNT_RESET);
31610Sstevel@tonic-gate 
31620Sstevel@tonic-gate 	if (type == FAULT_COUNT_INCR) {
31630Sstevel@tonic-gate 		inst->ri_i.i_fault_count++;
31640Sstevel@tonic-gate 		log_framework(LOG_INFO, "%s: Increasing fault count to %d\n",
31650Sstevel@tonic-gate 		    inst->ri_i.i_fmri, inst->ri_i.i_fault_count);
31660Sstevel@tonic-gate 	}
31670Sstevel@tonic-gate 	if (type == FAULT_COUNT_RESET)
31680Sstevel@tonic-gate 		inst->ri_i.i_fault_count = 0;
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 	if (inst->ri_i.i_fault_count >= FAULT_THRESHOLD)
31710Sstevel@tonic-gate 		return (1);
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 	return (0);
31740Sstevel@tonic-gate }
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate /*
31770Sstevel@tonic-gate  * int libscf_unset_action()
31780Sstevel@tonic-gate  *   Delete any pending timestamps for the specified action which is
31790Sstevel@tonic-gate  *   older than the supplied ts.
31800Sstevel@tonic-gate  *
31810Sstevel@tonic-gate  *   Returns 0 on success, ECONNABORTED, EACCES, or EPERM on failure.
31820Sstevel@tonic-gate  */
31830Sstevel@tonic-gate int
31840Sstevel@tonic-gate libscf_unset_action(scf_handle_t *h, scf_propertygroup_t *pg,
31850Sstevel@tonic-gate     admin_action_t a, hrtime_t ts)
31860Sstevel@tonic-gate {
31870Sstevel@tonic-gate 	scf_transaction_t *t;
31880Sstevel@tonic-gate 	scf_transaction_entry_t *e;
31890Sstevel@tonic-gate 	scf_property_t *prop;
31900Sstevel@tonic-gate 	scf_value_t *val;
31910Sstevel@tonic-gate 	hrtime_t rep_ts;
31920Sstevel@tonic-gate 	int ret = 0, r;
31930Sstevel@tonic-gate 
31940Sstevel@tonic-gate 	t = safe_scf_transaction_create(h);
31950Sstevel@tonic-gate 	e = safe_scf_entry_create(h);
31960Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
31970Sstevel@tonic-gate 	val = safe_scf_value_create(h);
31980Sstevel@tonic-gate 
31990Sstevel@tonic-gate 	for (;;) {
32000Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
32010Sstevel@tonic-gate 			switch (scf_error()) {
32020Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
32030Sstevel@tonic-gate 			default:
32040Sstevel@tonic-gate 				ret = ECONNABORTED;
32050Sstevel@tonic-gate 				goto unset_action_cleanup;
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
32080Sstevel@tonic-gate 				goto unset_action_cleanup;
32090Sstevel@tonic-gate 
32100Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
32110Sstevel@tonic-gate 				assert(0);
32120Sstevel@tonic-gate 				abort();
32130Sstevel@tonic-gate 			}
32140Sstevel@tonic-gate 		}
32150Sstevel@tonic-gate 
32160Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) == -1) {
32170Sstevel@tonic-gate 			switch (scf_error()) {
32180Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
32190Sstevel@tonic-gate 			default:
32200Sstevel@tonic-gate 				ret = ECONNABORTED;
32210Sstevel@tonic-gate 				goto unset_action_cleanup;
32220Sstevel@tonic-gate 
32230Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
32240Sstevel@tonic-gate 				goto unset_action_cleanup;
32250Sstevel@tonic-gate 
32260Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
32270Sstevel@tonic-gate 				ret = EPERM;
32280Sstevel@tonic-gate 				goto unset_action_cleanup;
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
32310Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
32320Sstevel@tonic-gate 				ret = EACCES;
32330Sstevel@tonic-gate 				goto unset_action_cleanup;
32340Sstevel@tonic-gate 
32350Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
32360Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
32370Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
32380Sstevel@tonic-gate 				assert(0);
32390Sstevel@tonic-gate 				abort();
32400Sstevel@tonic-gate 			}
32410Sstevel@tonic-gate 		}
32420Sstevel@tonic-gate 
32430Sstevel@tonic-gate 		/* Return failure only if the property hasn't been deleted. */
32440Sstevel@tonic-gate 		if (scf_pg_get_property(pg, admin_actions[a], prop) == -1) {
32450Sstevel@tonic-gate 			switch (scf_error()) {
32460Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
32470Sstevel@tonic-gate 			default:
32480Sstevel@tonic-gate 				ret = ECONNABORTED;
32490Sstevel@tonic-gate 				goto unset_action_cleanup;
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
32520Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
32530Sstevel@tonic-gate 				goto unset_action_cleanup;
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
32560Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
32570Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
32580Sstevel@tonic-gate 				assert(0);
32590Sstevel@tonic-gate 				abort();
32600Sstevel@tonic-gate 			}
32610Sstevel@tonic-gate 		}
32620Sstevel@tonic-gate 
32630Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) == -1) {
32640Sstevel@tonic-gate 			switch (scf_error()) {
32650Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
32660Sstevel@tonic-gate 			default:
32670Sstevel@tonic-gate 				ret = ECONNABORTED;
32680Sstevel@tonic-gate 				goto unset_action_cleanup;
32690Sstevel@tonic-gate 
32700Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
32710Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
32720Sstevel@tonic-gate 				goto unset_action_cleanup;
32730Sstevel@tonic-gate 
32740Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
32750Sstevel@tonic-gate 				/*
32760Sstevel@tonic-gate 				 * More than one value was associated with
32770Sstevel@tonic-gate 				 * this property -- this is incorrect. Take
32780Sstevel@tonic-gate 				 * the opportunity to clean up and clear the
32790Sstevel@tonic-gate 				 * entire property.
32800Sstevel@tonic-gate 				 */
32810Sstevel@tonic-gate 				rep_ts = ts;
32820Sstevel@tonic-gate 				break;
32830Sstevel@tonic-gate 
32845040Swesolows 			case SCF_ERROR_PERMISSION_DENIED:
32850Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
32860Sstevel@tonic-gate 				assert(0);
32870Sstevel@tonic-gate 				abort();
32880Sstevel@tonic-gate 			}
32890Sstevel@tonic-gate 		} else if (scf_value_get_integer(val, &rep_ts) == -1) {
32900Sstevel@tonic-gate 			assert(scf_error() == SCF_ERROR_TYPE_MISMATCH);
32910Sstevel@tonic-gate 			rep_ts = 0;
32920Sstevel@tonic-gate 		}
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 		/* Repository ts is more current. Don't clear the action. */
32950Sstevel@tonic-gate 		if (rep_ts > ts)
32960Sstevel@tonic-gate 			goto unset_action_cleanup;
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate 		r = scf_transaction_property_change_type(t, e,
32990Sstevel@tonic-gate 		    admin_actions[a], SCF_TYPE_INTEGER);
33000Sstevel@tonic-gate 		assert(r == 0);
33010Sstevel@tonic-gate 
33020Sstevel@tonic-gate 		r = scf_transaction_commit(t);
33030Sstevel@tonic-gate 		if (r == 1)
33040Sstevel@tonic-gate 			break;
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 		if (r != 0) {
33070Sstevel@tonic-gate 			switch (scf_error()) {
33080Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
33090Sstevel@tonic-gate 			default:
33100Sstevel@tonic-gate 				ret = ECONNABORTED;
33110Sstevel@tonic-gate 				goto unset_action_cleanup;
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
33140Sstevel@tonic-gate 				break;
33150Sstevel@tonic-gate 
33160Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
33170Sstevel@tonic-gate 				ret = EPERM;
33180Sstevel@tonic-gate 				goto unset_action_cleanup;
33190Sstevel@tonic-gate 
33200Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
33210Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
33220Sstevel@tonic-gate 				ret = EACCES;
33230Sstevel@tonic-gate 				goto unset_action_cleanup;
33240Sstevel@tonic-gate 
33250Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
33260Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
33270Sstevel@tonic-gate 				assert(0);
33280Sstevel@tonic-gate 				abort();
33290Sstevel@tonic-gate 			}
33300Sstevel@tonic-gate 		}
33310Sstevel@tonic-gate 
33320Sstevel@tonic-gate 		scf_transaction_reset(t);
33330Sstevel@tonic-gate 	}
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate unset_action_cleanup:
33360Sstevel@tonic-gate 	scf_transaction_destroy(t);
33370Sstevel@tonic-gate 	scf_entry_destroy(e);
33380Sstevel@tonic-gate 	scf_property_destroy(prop);
33390Sstevel@tonic-gate 	scf_value_destroy(val);
33400Sstevel@tonic-gate 
33410Sstevel@tonic-gate 	return (ret);
33420Sstevel@tonic-gate }
33430Sstevel@tonic-gate 
33440Sstevel@tonic-gate /*
33450Sstevel@tonic-gate  * Decorates & binds hndl.  hndl must be unbound.  Returns
33460Sstevel@tonic-gate  *   0 - success
33470Sstevel@tonic-gate  *   -1 - repository server is not running
33480Sstevel@tonic-gate  *   -1 - repository server is out of resources
33490Sstevel@tonic-gate  */
33500Sstevel@tonic-gate static int
33510Sstevel@tonic-gate handle_decorate_and_bind(scf_handle_t *hndl)
33520Sstevel@tonic-gate {
33530Sstevel@tonic-gate 	scf_value_t *door_dec_value;
33540Sstevel@tonic-gate 
33550Sstevel@tonic-gate 	door_dec_value = safe_scf_value_create(hndl);
33560Sstevel@tonic-gate 
33570Sstevel@tonic-gate 	/*
33580Sstevel@tonic-gate 	 * Decorate if alternate door path set.
33590Sstevel@tonic-gate 	 */
33600Sstevel@tonic-gate 	if (st->st_door_path) {
33610Sstevel@tonic-gate 		if (scf_value_set_astring(door_dec_value, st->st_door_path) !=
33620Sstevel@tonic-gate 		    0)
33630Sstevel@tonic-gate 			uu_die("$STARTD_ALT_DOOR is too long.\n");
33640Sstevel@tonic-gate 
33650Sstevel@tonic-gate 		if (scf_handle_decorate(hndl, "door_path", door_dec_value) != 0)
33660Sstevel@tonic-gate 			bad_error("scf_handle_decorate", scf_error());
33670Sstevel@tonic-gate 	}
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 	scf_value_destroy(door_dec_value);
33700Sstevel@tonic-gate 
33710Sstevel@tonic-gate 	if (scf_handle_bind(hndl) == 0)
33720Sstevel@tonic-gate 		return (0);
33730Sstevel@tonic-gate 
33740Sstevel@tonic-gate 	switch (scf_error()) {
33750Sstevel@tonic-gate 	case SCF_ERROR_NO_SERVER:
33760Sstevel@tonic-gate 	case SCF_ERROR_NO_RESOURCES:
33770Sstevel@tonic-gate 		return (-1);
33780Sstevel@tonic-gate 
33790Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
33800Sstevel@tonic-gate 	case SCF_ERROR_IN_USE:
33810Sstevel@tonic-gate 	default:
33820Sstevel@tonic-gate 		bad_error("scf_handle_bind", scf_error());
33830Sstevel@tonic-gate 		/* NOTREACHED */
33840Sstevel@tonic-gate 	}
33850Sstevel@tonic-gate }
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate scf_handle_t *
33880Sstevel@tonic-gate libscf_handle_create_bound(scf_version_t v)
33890Sstevel@tonic-gate {
33900Sstevel@tonic-gate 	scf_handle_t *hndl = scf_handle_create(v);
33910Sstevel@tonic-gate 
33920Sstevel@tonic-gate 	if (hndl == NULL)
33930Sstevel@tonic-gate 		return (hndl);
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 	if (handle_decorate_and_bind(hndl) == 0)
33960Sstevel@tonic-gate 		return (hndl);
33970Sstevel@tonic-gate 
33980Sstevel@tonic-gate 	scf_handle_destroy(hndl);
33990Sstevel@tonic-gate 	return (NULL);
34000Sstevel@tonic-gate }
34010Sstevel@tonic-gate 
34020Sstevel@tonic-gate void
34030Sstevel@tonic-gate libscf_handle_rebind(scf_handle_t *h)
34040Sstevel@tonic-gate {
34050Sstevel@tonic-gate 	(void) scf_handle_unbind(h);
34060Sstevel@tonic-gate 
34070Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_configd_live_lock);
34080Sstevel@tonic-gate 
34090Sstevel@tonic-gate 	/*
34100Sstevel@tonic-gate 	 * Try to rebind the handle before sleeping in case the server isn't
34110Sstevel@tonic-gate 	 * really dead.
34120Sstevel@tonic-gate 	 */
34130Sstevel@tonic-gate 	while (handle_decorate_and_bind(h) != 0)
34140Sstevel@tonic-gate 		(void) pthread_cond_wait(&st->st_configd_live_cv,
34150Sstevel@tonic-gate 		    &st->st_configd_live_lock);
34160Sstevel@tonic-gate 
34170Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_configd_live_lock);
34180Sstevel@tonic-gate }
34190Sstevel@tonic-gate 
34200Sstevel@tonic-gate /*
34210Sstevel@tonic-gate  * Create a handle and try to bind it until it succeeds.  Always returns
34220Sstevel@tonic-gate  * a bound handle.
34230Sstevel@tonic-gate  */
34240Sstevel@tonic-gate scf_handle_t *
34250Sstevel@tonic-gate libscf_handle_create_bound_loop()
34260Sstevel@tonic-gate {
34270Sstevel@tonic-gate 	scf_handle_t *h;
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate 	while ((h = scf_handle_create(SCF_VERSION)) == NULL) {
34300Sstevel@tonic-gate 		/* This should have been caught earlier. */
34310Sstevel@tonic-gate 		assert(scf_error() != SCF_ERROR_VERSION_MISMATCH);
34320Sstevel@tonic-gate 		(void) sleep(2);
34330Sstevel@tonic-gate 	}
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 	if (handle_decorate_and_bind(h) != 0)
34360Sstevel@tonic-gate 		libscf_handle_rebind(h);
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 	return (h);
34390Sstevel@tonic-gate }
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate /*
34420Sstevel@tonic-gate  * Call cb for each dependency property group of inst.  cb is invoked with
34430Sstevel@tonic-gate  * a pointer to the scf_propertygroup_t and arg.  If the repository connection
34440Sstevel@tonic-gate  * is broken, returns ECONNABORTED.  If inst is deleted, returns ECANCELED.
34450Sstevel@tonic-gate  * If cb returns non-zero, the walk is stopped and EINTR is returned.
34460Sstevel@tonic-gate  * Otherwise returns 0.
34470Sstevel@tonic-gate  */
34480Sstevel@tonic-gate int
34490Sstevel@tonic-gate walk_dependency_pgs(scf_instance_t *inst, callback_t cb, void *arg)
34500Sstevel@tonic-gate {
34510Sstevel@tonic-gate 	scf_handle_t *h;
34520Sstevel@tonic-gate 	scf_snapshot_t *snap;
34530Sstevel@tonic-gate 	scf_iter_t *iter;
34540Sstevel@tonic-gate 	scf_propertygroup_t *pg;
34550Sstevel@tonic-gate 	int r;
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 	h = scf_instance_handle(inst);
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
34600Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 	snap = libscf_get_running_snapshot(inst);
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed_composed(iter, inst, snap,
34650Sstevel@tonic-gate 	    SCF_GROUP_DEPENDENCY) != 0) {
34660Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
34670Sstevel@tonic-gate 		scf_pg_destroy(pg);
34680Sstevel@tonic-gate 		scf_iter_destroy(iter);
34690Sstevel@tonic-gate 		switch (scf_error()) {
34700Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
34710Sstevel@tonic-gate 		default:
34720Sstevel@tonic-gate 			return (ECONNABORTED);
34730Sstevel@tonic-gate 
34740Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
34750Sstevel@tonic-gate 			return (ECANCELED);
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
34780Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
34790Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
34800Sstevel@tonic-gate 			assert(0);
34810Sstevel@tonic-gate 			abort();
34820Sstevel@tonic-gate 		}
34830Sstevel@tonic-gate 	}
34840Sstevel@tonic-gate 
34850Sstevel@tonic-gate 	for (;;) {
34860Sstevel@tonic-gate 		r = scf_iter_next_pg(iter, pg);
34870Sstevel@tonic-gate 		if (r == 0)
34880Sstevel@tonic-gate 			break;
34890Sstevel@tonic-gate 		if (r == -1) {
34900Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
34910Sstevel@tonic-gate 			scf_pg_destroy(pg);
34920Sstevel@tonic-gate 			scf_iter_destroy(iter);
34932787Shg115875 
34942787Shg115875 			switch (scf_error()) {
34952787Shg115875 			case SCF_ERROR_CONNECTION_BROKEN:
34962787Shg115875 				return (ECONNABORTED);
34972787Shg115875 
34982787Shg115875 			case SCF_ERROR_DELETED:
34992787Shg115875 				return (ECANCELED);
35002787Shg115875 
35012787Shg115875 			case SCF_ERROR_NOT_SET:
35022787Shg115875 			case SCF_ERROR_INVALID_ARGUMENT:
35032787Shg115875 			case SCF_ERROR_NOT_BOUND:
35042787Shg115875 			case SCF_ERROR_HANDLE_MISMATCH:
35052787Shg115875 			default:
35062787Shg115875 				bad_error("scf_iter_next_pg", scf_error());
35072787Shg115875 			}
35080Sstevel@tonic-gate 		}
35090Sstevel@tonic-gate 
35100Sstevel@tonic-gate 		r = cb(pg, arg);
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate 		if (r != 0)
35130Sstevel@tonic-gate 			break;
35140Sstevel@tonic-gate 	}
35150Sstevel@tonic-gate 
35160Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
35170Sstevel@tonic-gate 	scf_pg_destroy(pg);
35180Sstevel@tonic-gate 	scf_iter_destroy(iter);
35190Sstevel@tonic-gate 
35200Sstevel@tonic-gate 	return (r == 0 ? 0 : EINTR);
35210Sstevel@tonic-gate }
35220Sstevel@tonic-gate 
35230Sstevel@tonic-gate /*
35240Sstevel@tonic-gate  * Call cb for each of the string values of prop.  cb is invoked with
35250Sstevel@tonic-gate  * a pointer to the string and arg.  If the connection to the repository is
35260Sstevel@tonic-gate  * broken, ECONNABORTED is returned.  If the property is deleted, ECANCELED is
35270Sstevel@tonic-gate  * returned.  If the property does not have astring type, EINVAL is returned.
35280Sstevel@tonic-gate  * If cb returns non-zero, the walk is stopped and EINTR is returned.
35290Sstevel@tonic-gate  * Otherwise 0 is returned.
35300Sstevel@tonic-gate  */
35310Sstevel@tonic-gate int
35320Sstevel@tonic-gate walk_property_astrings(scf_property_t *prop, callback_t cb, void *arg)
35330Sstevel@tonic-gate {
35340Sstevel@tonic-gate 	scf_handle_t *h;
35350Sstevel@tonic-gate 	scf_value_t *val;
35360Sstevel@tonic-gate 	scf_iter_t *iter;
35370Sstevel@tonic-gate 	char *buf;
35380Sstevel@tonic-gate 	int r;
35390Sstevel@tonic-gate 	ssize_t sz;
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate 	if (scf_property_is_type(prop, SCF_TYPE_ASTRING) != 0) {
35420Sstevel@tonic-gate 		switch (scf_error()) {
35430Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
35440Sstevel@tonic-gate 		default:
35450Sstevel@tonic-gate 			return (ECONNABORTED);
35460Sstevel@tonic-gate 
35470Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
35480Sstevel@tonic-gate 			return (ECANCELED);
35490Sstevel@tonic-gate 
35500Sstevel@tonic-gate 		case SCF_ERROR_TYPE_MISMATCH:
35510Sstevel@tonic-gate 			return (EINVAL);
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
35540Sstevel@tonic-gate 			assert(0);
35550Sstevel@tonic-gate 			abort();
35560Sstevel@tonic-gate 		}
35570Sstevel@tonic-gate 	}
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 	h = scf_property_handle(prop);
35600Sstevel@tonic-gate 
35610Sstevel@tonic-gate 	val = safe_scf_value_create(h);
35620Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
35630Sstevel@tonic-gate 
35640Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) != 0) {
35650Sstevel@tonic-gate 		scf_iter_destroy(iter);
35660Sstevel@tonic-gate 		scf_value_destroy(val);
35670Sstevel@tonic-gate 		switch (scf_error()) {
35680Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
35690Sstevel@tonic-gate 		default:
35700Sstevel@tonic-gate 			return (ECONNABORTED);
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
35730Sstevel@tonic-gate 			return (ECANCELED);
35740Sstevel@tonic-gate 
35750Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
35760Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
35770Sstevel@tonic-gate 			assert(0);
35780Sstevel@tonic-gate 			abort();
35790Sstevel@tonic-gate 		}
35800Sstevel@tonic-gate 	}
35810Sstevel@tonic-gate 
35820Sstevel@tonic-gate 	buf = startd_alloc(max_scf_value_size);
35830Sstevel@tonic-gate 
35840Sstevel@tonic-gate 	for (;;) {
35850Sstevel@tonic-gate 		r = scf_iter_next_value(iter, val);
35860Sstevel@tonic-gate 		if (r < 0) {
35870Sstevel@tonic-gate 			startd_free(buf, max_scf_value_size);
35880Sstevel@tonic-gate 			scf_iter_destroy(iter);
35890Sstevel@tonic-gate 			scf_value_destroy(val);
35902787Shg115875 
35912787Shg115875 			switch (scf_error()) {
35922787Shg115875 			case SCF_ERROR_CONNECTION_BROKEN:
35932787Shg115875 				return (ECONNABORTED);
35942787Shg115875 
35952787Shg115875 			case SCF_ERROR_DELETED:
35962787Shg115875 				return (ECANCELED);
35972787Shg115875 
35982787Shg115875 			case SCF_ERROR_NOT_SET:
35992787Shg115875 			case SCF_ERROR_INVALID_ARGUMENT:
36002787Shg115875 			case SCF_ERROR_NOT_BOUND:
36012787Shg115875 			case SCF_ERROR_HANDLE_MISMATCH:
36025040Swesolows 			case SCF_ERROR_PERMISSION_DENIED:
36032787Shg115875 			default:
36042787Shg115875 				bad_error("scf_iter_next_value", scf_error());
36052787Shg115875 			}
36060Sstevel@tonic-gate 		}
36070Sstevel@tonic-gate 		if (r == 0)
36080Sstevel@tonic-gate 			break;
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 		sz = scf_value_get_astring(val, buf, max_scf_value_size);
36110Sstevel@tonic-gate 		assert(sz >= 0);
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 		r = cb(buf, arg);
36140Sstevel@tonic-gate 
36150Sstevel@tonic-gate 		if (r != 0)
36160Sstevel@tonic-gate 			break;
36170Sstevel@tonic-gate 	}
36180Sstevel@tonic-gate 
36190Sstevel@tonic-gate 	startd_free(buf, max_scf_value_size);
36200Sstevel@tonic-gate 	scf_value_destroy(val);
36210Sstevel@tonic-gate 	scf_iter_destroy(iter);
36220Sstevel@tonic-gate 
36230Sstevel@tonic-gate 	return (r == 0 ? 0 : EINTR);
36240Sstevel@tonic-gate }
36250Sstevel@tonic-gate 
36260Sstevel@tonic-gate /*
36270Sstevel@tonic-gate  * Returns 0 or ECONNABORTED.
36280Sstevel@tonic-gate  */
36290Sstevel@tonic-gate int
36300Sstevel@tonic-gate libscf_create_self(scf_handle_t *h)
36310Sstevel@tonic-gate {
36320Sstevel@tonic-gate 	scf_scope_t *scope;
36330Sstevel@tonic-gate 	scf_service_t *svc;
36340Sstevel@tonic-gate 	scf_instance_t *inst;
36350Sstevel@tonic-gate 	instance_data_t idata;
36360Sstevel@tonic-gate 	int ret = 0, r;
36370Sstevel@tonic-gate 	ctid_t ctid;
36380Sstevel@tonic-gate 	uint64_t uint64;
36390Sstevel@tonic-gate 	uint_t count = 0, msecs = ALLOC_DELAY;
36400Sstevel@tonic-gate 
36410Sstevel@tonic-gate 	const char * const startd_svc = "system/svc/restarter";
36420Sstevel@tonic-gate 	const char * const startd_inst = "default";
36430Sstevel@tonic-gate 
36440Sstevel@tonic-gate 	/* If SCF_SERVICE_STARTD changes, our strings must change, too. */
36450Sstevel@tonic-gate 	assert(strcmp(SCF_SERVICE_STARTD,
36460Sstevel@tonic-gate 	    "svc:/system/svc/restarter:default") == 0);
36470Sstevel@tonic-gate 
36480Sstevel@tonic-gate 	scope = safe_scf_scope_create(h);
36490Sstevel@tonic-gate 	svc = safe_scf_service_create(h);
36500Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, scope) != 0) {
36530Sstevel@tonic-gate 		assert(scf_error() == SCF_ERROR_CONNECTION_BROKEN);
36540Sstevel@tonic-gate 		ret = ECONNABORTED;
36550Sstevel@tonic-gate 		goto out;
36560Sstevel@tonic-gate 	}
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate get_svc:
36590Sstevel@tonic-gate 	if (scf_scope_get_service(scope, startd_svc, svc) != 0) {
36600Sstevel@tonic-gate 		switch (scf_error()) {
36610Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
36620Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
36630Sstevel@tonic-gate 		default:
36640Sstevel@tonic-gate 			ret = ECONNABORTED;
36650Sstevel@tonic-gate 			goto out;
36660Sstevel@tonic-gate 
36670Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
36680Sstevel@tonic-gate 			break;
36690Sstevel@tonic-gate 
36700Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
36710Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
36720Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
36730Sstevel@tonic-gate 			bad_error("scf_scope_get_service", scf_error());
36740Sstevel@tonic-gate 		}
36750Sstevel@tonic-gate 
36760Sstevel@tonic-gate add_svc:
36770Sstevel@tonic-gate 		if (scf_scope_add_service(scope, startd_svc, svc) != 0) {
36780Sstevel@tonic-gate 			switch (scf_error()) {
36790Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
36800Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
36810Sstevel@tonic-gate 			default:
36820Sstevel@tonic-gate 				ret = ECONNABORTED;
36830Sstevel@tonic-gate 				goto out;
36840Sstevel@tonic-gate 
36850Sstevel@tonic-gate 			case SCF_ERROR_EXISTS:
36860Sstevel@tonic-gate 				goto get_svc;
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
36890Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
36900Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
36910Sstevel@tonic-gate 				uu_warn("Could not create %s: %s\n",
36920Sstevel@tonic-gate 				    SCF_SERVICE_STARTD,
36930Sstevel@tonic-gate 				    scf_strerror(scf_error()));
36940Sstevel@tonic-gate 				goto out;
36950Sstevel@tonic-gate 
36960Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
36970Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
36980Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
36990Sstevel@tonic-gate 				bad_error("scf_scope_add_service", scf_error());
37000Sstevel@tonic-gate 			}
37010Sstevel@tonic-gate 		}
37020Sstevel@tonic-gate 	}
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate 	if (scf_service_get_instance(svc, startd_inst, NULL) == 0)
37050Sstevel@tonic-gate 		goto out;
37060Sstevel@tonic-gate 
37070Sstevel@tonic-gate 	switch (scf_error()) {
37080Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
37090Sstevel@tonic-gate 	default:
37100Sstevel@tonic-gate 		ret = ECONNABORTED;
37110Sstevel@tonic-gate 		goto out;
37120Sstevel@tonic-gate 
37130Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
37140Sstevel@tonic-gate 		break;
37150Sstevel@tonic-gate 
37160Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
37170Sstevel@tonic-gate 		goto add_svc;
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate 	case SCF_ERROR_HANDLE_MISMATCH:
37200Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
37210Sstevel@tonic-gate 	case SCF_ERROR_NOT_SET:
37220Sstevel@tonic-gate 		bad_error("scf_service_get_instance", scf_error());
37230Sstevel@tonic-gate 	}
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate add_inst:
37260Sstevel@tonic-gate 	if (scf_service_add_instance(svc, startd_inst, inst) != 0) {
37270Sstevel@tonic-gate 		switch (scf_error()) {
37280Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
37290Sstevel@tonic-gate 		default:
37300Sstevel@tonic-gate 			ret = ECONNABORTED;
37310Sstevel@tonic-gate 			goto out;
37320Sstevel@tonic-gate 
37330Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
37340Sstevel@tonic-gate 			break;
37350Sstevel@tonic-gate 
37360Sstevel@tonic-gate 		case SCF_ERROR_PERMISSION_DENIED:
37370Sstevel@tonic-gate 		case SCF_ERROR_BACKEND_ACCESS:
37380Sstevel@tonic-gate 			uu_die("Could not create %s: %s\n", SCF_SERVICE_STARTD,
37390Sstevel@tonic-gate 			    scf_strerror(scf_error()));
37400Sstevel@tonic-gate 			/* NOTREACHED */
37410Sstevel@tonic-gate 
37420Sstevel@tonic-gate 		case SCF_ERROR_BACKEND_READONLY:
37430Sstevel@tonic-gate 			log_error(LOG_NOTICE,
37440Sstevel@tonic-gate 			    "Could not create %s: backend readonly.\n",
37450Sstevel@tonic-gate 			    SCF_SERVICE_STARTD);
37460Sstevel@tonic-gate 			goto out;
37470Sstevel@tonic-gate 
37480Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
37490Sstevel@tonic-gate 			goto add_svc;
37500Sstevel@tonic-gate 
37510Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
37520Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
37530Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
37540Sstevel@tonic-gate 			bad_error("scf_service_add_instance", scf_error());
37550Sstevel@tonic-gate 		}
37560Sstevel@tonic-gate 	}
37570Sstevel@tonic-gate 
37580Sstevel@tonic-gate 	/* Set start time. */
37590Sstevel@tonic-gate 	idata.i_fmri = SCF_SERVICE_STARTD;
37600Sstevel@tonic-gate 	idata.i_state = RESTARTER_STATE_NONE;
37610Sstevel@tonic-gate 	idata.i_next_state = RESTARTER_STATE_NONE;
37620Sstevel@tonic-gate set_state:
37630Sstevel@tonic-gate 	switch (r = _restarter_commit_states(h, &idata, RESTARTER_STATE_ONLINE,
37640Sstevel@tonic-gate 	    RESTARTER_STATE_NONE, NULL)) {
37650Sstevel@tonic-gate 	case 0:
37660Sstevel@tonic-gate 		break;
37670Sstevel@tonic-gate 
37680Sstevel@tonic-gate 	case ENOMEM:
37690Sstevel@tonic-gate 		++count;
37700Sstevel@tonic-gate 		if (count < ALLOC_RETRY) {
37710Sstevel@tonic-gate 			(void) poll(NULL, 0, msecs);
37720Sstevel@tonic-gate 			msecs *= ALLOC_DELAY_MULT;
37730Sstevel@tonic-gate 			goto set_state;
37740Sstevel@tonic-gate 		}
37750Sstevel@tonic-gate 
37760Sstevel@tonic-gate 		uu_die("Insufficient memory.\n");
37770Sstevel@tonic-gate 		/* NOTREACHED */
37780Sstevel@tonic-gate 
37790Sstevel@tonic-gate 	case ECONNABORTED:
37800Sstevel@tonic-gate 		ret = ECONNABORTED;
37810Sstevel@tonic-gate 		goto out;
37820Sstevel@tonic-gate 
37830Sstevel@tonic-gate 	case ENOENT:
37840Sstevel@tonic-gate 		goto add_inst;
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 	case EPERM:
37870Sstevel@tonic-gate 	case EACCES:
37880Sstevel@tonic-gate 	case EROFS:
37890Sstevel@tonic-gate 		uu_warn("Could not timestamp %s: %s\n", idata.i_fmri,
37900Sstevel@tonic-gate 		    strerror(r));
37910Sstevel@tonic-gate 		break;
37920Sstevel@tonic-gate 
37930Sstevel@tonic-gate 	case EINVAL:
37940Sstevel@tonic-gate 	default:
37950Sstevel@tonic-gate 		bad_error("_restarter_commit_states", r);
37960Sstevel@tonic-gate 	}
37970Sstevel@tonic-gate 
37980Sstevel@tonic-gate 	/* Set general/enabled. */
37990Sstevel@tonic-gate 	ret = libscf_inst_set_boolean_prop(inst, SCF_PG_GENERAL,
38000Sstevel@tonic-gate 	    SCF_PG_GENERAL_TYPE, SCF_PG_GENERAL_FLAGS, SCF_PROPERTY_ENABLED, 1);
38010Sstevel@tonic-gate 	switch (ret) {
38020Sstevel@tonic-gate 	case 0:
38030Sstevel@tonic-gate 	case ECONNABORTED:
38040Sstevel@tonic-gate 	case EPERM:
38050Sstevel@tonic-gate 	case EACCES:
38060Sstevel@tonic-gate 	case EROFS:
38070Sstevel@tonic-gate 		break;
38080Sstevel@tonic-gate 
38090Sstevel@tonic-gate 	case ECANCELED:
38100Sstevel@tonic-gate 		goto add_inst;
38110Sstevel@tonic-gate 
38120Sstevel@tonic-gate 	default:
38130Sstevel@tonic-gate 		bad_error("libscf_inst_set_boolean_prop", ret);
38140Sstevel@tonic-gate 	}
38150Sstevel@tonic-gate 
38160Sstevel@tonic-gate 	ret = libscf_write_start_pid(inst, getpid());
38170Sstevel@tonic-gate 	switch (ret) {
38180Sstevel@tonic-gate 	case 0:
38190Sstevel@tonic-gate 	case ECONNABORTED:
38200Sstevel@tonic-gate 	case EPERM:
38210Sstevel@tonic-gate 	case EACCES:
38220Sstevel@tonic-gate 	case EROFS:
38230Sstevel@tonic-gate 		break;
38240Sstevel@tonic-gate 
38250Sstevel@tonic-gate 	case ECANCELED:
38260Sstevel@tonic-gate 		goto add_inst;
38270Sstevel@tonic-gate 
38280Sstevel@tonic-gate 	default:
38290Sstevel@tonic-gate 		bad_error("libscf_write_start_pid", ret);
38300Sstevel@tonic-gate 	}
38310Sstevel@tonic-gate 
38320Sstevel@tonic-gate 	ctid = proc_get_ctid();
38330Sstevel@tonic-gate 	if (ctid > 0) {
38340Sstevel@tonic-gate 
38350Sstevel@tonic-gate 		uint64 = (uint64_t)ctid;
38360Sstevel@tonic-gate 		ret = libscf_inst_set_count_prop(inst,
38370Sstevel@tonic-gate 		    SCF_PG_RESTARTER, SCF_PG_RESTARTER_TYPE,
38380Sstevel@tonic-gate 		    SCF_PG_RESTARTER_FLAGS, SCF_PROPERTY_CONTRACT, uint64);
38390Sstevel@tonic-gate 
38400Sstevel@tonic-gate 		switch (ret) {
38410Sstevel@tonic-gate 		case 0:
38420Sstevel@tonic-gate 		case ECONNABORTED:
38430Sstevel@tonic-gate 		case EPERM:
38440Sstevel@tonic-gate 		case EACCES:
38450Sstevel@tonic-gate 		case EROFS:
38460Sstevel@tonic-gate 			break;
38470Sstevel@tonic-gate 
38480Sstevel@tonic-gate 		case ECANCELED:
38490Sstevel@tonic-gate 			goto add_inst;
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 		default:
38520Sstevel@tonic-gate 			bad_error("libscf_inst_set_count_prop", ret);
38530Sstevel@tonic-gate 		}
38540Sstevel@tonic-gate 	}
38550Sstevel@tonic-gate 
38560Sstevel@tonic-gate 	ret = libscf_note_method_log(inst, LOG_PREFIX_EARLY,
38570Sstevel@tonic-gate 	    STARTD_DEFAULT_LOG);
38580Sstevel@tonic-gate 	if (ret == 0) {
38590Sstevel@tonic-gate 		ret = libscf_note_method_log(inst, LOG_PREFIX_NORMAL,
38600Sstevel@tonic-gate 		    STARTD_DEFAULT_LOG);
38610Sstevel@tonic-gate 	}
38620Sstevel@tonic-gate 
38630Sstevel@tonic-gate 	switch (ret) {
3864*8554SSean.Wilcox@Sun.COM 		case 0:
38650Sstevel@tonic-gate 		case ECONNABORTED:
38660Sstevel@tonic-gate 		case EPERM:
38670Sstevel@tonic-gate 		case EACCES:
38680Sstevel@tonic-gate 		case EROFS:
38690Sstevel@tonic-gate 		case EAGAIN:
38700Sstevel@tonic-gate 			break;
38710Sstevel@tonic-gate 
38720Sstevel@tonic-gate 		case ECANCELED:
38730Sstevel@tonic-gate 			goto add_inst;
38740Sstevel@tonic-gate 
38750Sstevel@tonic-gate 		default:
38760Sstevel@tonic-gate 			bad_error("libscf_note_method_log", ret);
38770Sstevel@tonic-gate 	}
38780Sstevel@tonic-gate 
38790Sstevel@tonic-gate out:
38800Sstevel@tonic-gate 	scf_instance_destroy(inst);
38810Sstevel@tonic-gate 	scf_service_destroy(svc);
38820Sstevel@tonic-gate 	scf_scope_destroy(scope);
38830Sstevel@tonic-gate 	return (ret);
38840Sstevel@tonic-gate }
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate /*
38870Sstevel@tonic-gate  * Returns
38880Sstevel@tonic-gate  *   0 - success
38890Sstevel@tonic-gate  *   ENOENT - SCF_SERVICE_STARTD does not exist in repository
38900Sstevel@tonic-gate  *   EPERM
38910Sstevel@tonic-gate  *   EACCES
38920Sstevel@tonic-gate  *   EROFS
38930Sstevel@tonic-gate  */
38940Sstevel@tonic-gate int
38950Sstevel@tonic-gate libscf_set_reconfig(int set)
38960Sstevel@tonic-gate {
38970Sstevel@tonic-gate 	scf_handle_t *h;
38980Sstevel@tonic-gate 	scf_instance_t *inst;
38990Sstevel@tonic-gate 	scf_propertygroup_t *pg;
39000Sstevel@tonic-gate 	int ret = 0;
39010Sstevel@tonic-gate 
39020Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
39030Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
39040Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
39050Sstevel@tonic-gate 
39060Sstevel@tonic-gate again:
39070Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL,
39080Sstevel@tonic-gate 	    inst, NULL, NULL,  SCF_DECODE_FMRI_EXACT) == -1) {
39090Sstevel@tonic-gate 		switch (scf_error()) {
39100Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
39110Sstevel@tonic-gate 		default:
39120Sstevel@tonic-gate 			libscf_handle_rebind(h);
39130Sstevel@tonic-gate 			goto again;
39140Sstevel@tonic-gate 
39150Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
39160Sstevel@tonic-gate 			ret = ENOENT;
39170Sstevel@tonic-gate 			goto reconfig_out;
39180Sstevel@tonic-gate 
39190Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
39200Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
39210Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
39220Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
39230Sstevel@tonic-gate 		}
39240Sstevel@tonic-gate 	}
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 	ret = libscf_inst_set_boolean_prop(inst, "system", SCF_GROUP_FRAMEWORK,
39270Sstevel@tonic-gate 	    SCF_PG_FLAG_NONPERSISTENT, "reconfigure", set);
39280Sstevel@tonic-gate 	switch (ret) {
39290Sstevel@tonic-gate 	case 0:
39300Sstevel@tonic-gate 	case EPERM:
39310Sstevel@tonic-gate 	case EACCES:
39320Sstevel@tonic-gate 	case EROFS:
39330Sstevel@tonic-gate 		break;
39340Sstevel@tonic-gate 
39350Sstevel@tonic-gate 	case ECONNABORTED:
39360Sstevel@tonic-gate 		libscf_handle_rebind(h);
39370Sstevel@tonic-gate 		goto again;
39380Sstevel@tonic-gate 
39390Sstevel@tonic-gate 	case ECANCELED:
39400Sstevel@tonic-gate 		ret = ENOENT;
39410Sstevel@tonic-gate 		break;
39420Sstevel@tonic-gate 
39430Sstevel@tonic-gate 	default:
39440Sstevel@tonic-gate 		bad_error("libscf_inst_set_boolean_prop", ret);
39450Sstevel@tonic-gate 	}
39460Sstevel@tonic-gate 
39470Sstevel@tonic-gate reconfig_out:
39480Sstevel@tonic-gate 	scf_pg_destroy(pg);
39490Sstevel@tonic-gate 	scf_instance_destroy(inst);
39500Sstevel@tonic-gate 	scf_handle_destroy(h);
39510Sstevel@tonic-gate 	return (ret);
39520Sstevel@tonic-gate }
39530Sstevel@tonic-gate 
39540Sstevel@tonic-gate /*
39550Sstevel@tonic-gate  * Set inst->ri_m_inst to the scf instance for inst.  If it has been deleted,
39560Sstevel@tonic-gate  * set inst->ri_mi_deleted to true.  If the repository connection is broken, it
39570Sstevel@tonic-gate  * is rebound with libscf_handle_rebound().
39580Sstevel@tonic-gate  */
39590Sstevel@tonic-gate void
39600Sstevel@tonic-gate libscf_reget_instance(restarter_inst_t *inst)
39610Sstevel@tonic-gate {
39620Sstevel@tonic-gate 	scf_handle_t *h;
39630Sstevel@tonic-gate 	int r;
39640Sstevel@tonic-gate 
39650Sstevel@tonic-gate 	h = scf_instance_handle(inst->ri_m_inst);
39660Sstevel@tonic-gate 
39670Sstevel@tonic-gate again:
39680Sstevel@tonic-gate 	r = libscf_lookup_instance(inst->ri_i.i_fmri, inst->ri_m_inst);
39690Sstevel@tonic-gate 	switch (r) {
39700Sstevel@tonic-gate 	case 0:
39710Sstevel@tonic-gate 	case ENOENT:
39720Sstevel@tonic-gate 		inst->ri_mi_deleted = (r == ENOENT);
39730Sstevel@tonic-gate 		return;
39740Sstevel@tonic-gate 
39750Sstevel@tonic-gate 	case ECONNABORTED:
39760Sstevel@tonic-gate 		libscf_handle_rebind(h);
39770Sstevel@tonic-gate 		goto again;
39780Sstevel@tonic-gate 
39790Sstevel@tonic-gate 	case EINVAL:
39800Sstevel@tonic-gate 	case ENOTSUP:
39810Sstevel@tonic-gate 	default:
39820Sstevel@tonic-gate 		bad_error("libscf_lookup_instance", r);
39830Sstevel@tonic-gate 	}
39840Sstevel@tonic-gate }
3985