xref: /titanic_50/usr/src/cmd/svc/startd/libscf.c (revision 70cbfe41f2338b77c15f79c6625eca6e70c412f3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55b6c5443Srm88369  * Common Development and Distribution License (the "License").
65b6c5443Srm88369  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
213eae19d9Swesolows 
227c478bd9Sstevel@tonic-gate /*
235b7f77adStw21770  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
297c478bd9Sstevel@tonic-gate #include <assert.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <libscf.h>
327c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
337c478bd9Sstevel@tonic-gate #include <poll.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "startd.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	SMF_SNAPSHOT_RUNNING	"running"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate char *
437c478bd9Sstevel@tonic-gate inst_fmri_to_svc_fmri(const char *fmri)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	char *buf, *sfmri;
467c478bd9Sstevel@tonic-gate 	const char *scope, *svc;
477c478bd9Sstevel@tonic-gate 	int r;
487c478bd9Sstevel@tonic-gate 	boolean_t local;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
517c478bd9Sstevel@tonic-gate 	sfmri = startd_alloc(max_scf_fmri_size);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, fmri);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	r = scf_parse_svc_fmri(buf, &scope, &svc, NULL, NULL, NULL);
567c478bd9Sstevel@tonic-gate 	assert(r == 0);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	local = strcmp(scope, SCF_SCOPE_LOCAL) == 0;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	(void) snprintf(sfmri, max_scf_fmri_size, "svc:%s%s/%s",
617c478bd9Sstevel@tonic-gate 	    local ? "" : "//", local ? "" : scope, svc);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	return (sfmri);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * Wrapper for the scf_*_create() functions.  On SCF_ERROR_NO_MEMORY and
707c478bd9Sstevel@tonic-gate  * SCF_ERROR_NO_RESOURCES, retries or dies.  So this can only fail with
717c478bd9Sstevel@tonic-gate  * SCF_ERROR_INVALID_ARGUMENT, if h is NULL.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate void *
747c478bd9Sstevel@tonic-gate libscf_object_create(void *f(scf_handle_t *), scf_handle_t *h)
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	void *o;
777c478bd9Sstevel@tonic-gate 	uint_t try, msecs;
787c478bd9Sstevel@tonic-gate 	scf_error_t err;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	o = f(h);
817c478bd9Sstevel@tonic-gate 	if (o != NULL)
827c478bd9Sstevel@tonic-gate 		return (o);
837c478bd9Sstevel@tonic-gate 	err = scf_error();
847c478bd9Sstevel@tonic-gate 	if (err != SCF_ERROR_NO_MEMORY && err != SCF_ERROR_NO_RESOURCES)
857c478bd9Sstevel@tonic-gate 		return (NULL);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	for (try = 0; try < ALLOC_RETRY; ++try) {
907c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
917c478bd9Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
927c478bd9Sstevel@tonic-gate 		o = f(h);
937c478bd9Sstevel@tonic-gate 		if (o != NULL)
947c478bd9Sstevel@tonic-gate 			return (o);
957c478bd9Sstevel@tonic-gate 		err = scf_error();
967c478bd9Sstevel@tonic-gate 		if (err != SCF_ERROR_NO_MEMORY && err != SCF_ERROR_NO_RESOURCES)
977c478bd9Sstevel@tonic-gate 			return (NULL);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
1017c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate scf_snapshot_t *
1057c478bd9Sstevel@tonic-gate libscf_get_running_snapshot(scf_instance_t *inst)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
1087c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
1117c478bd9Sstevel@tonic-gate 	if (h == NULL)
1127c478bd9Sstevel@tonic-gate 		return (NULL);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
1157c478bd9Sstevel@tonic-gate 	if (snap == NULL)
1167c478bd9Sstevel@tonic-gate 		return (NULL);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, SMF_SNAPSHOT_RUNNING, snap) == 0)
1197c478bd9Sstevel@tonic-gate 		return (snap);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
1227c478bd9Sstevel@tonic-gate 	return (NULL);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * Make sure a service has a "running" snapshot.  If it doesn't, make one from
1277c478bd9Sstevel@tonic-gate  * the editing configuration.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate scf_snapshot_t *
1307c478bd9Sstevel@tonic-gate libscf_get_or_make_running_snapshot(scf_instance_t *inst, const char *fmri,
1317c478bd9Sstevel@tonic-gate     boolean_t retake)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
1347c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
1397c478bd9Sstevel@tonic-gate 	if (snap == NULL)
1407c478bd9Sstevel@tonic-gate 		goto err;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, SMF_SNAPSHOT_RUNNING, snap) == 0)
1437c478bd9Sstevel@tonic-gate 		return (snap);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	switch (scf_error()) {
1467c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
1477c478bd9Sstevel@tonic-gate 		break;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
1507c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
1517c478bd9Sstevel@tonic-gate 		return (NULL);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	default:
1547c478bd9Sstevel@tonic-gate err:
1557c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE,
1567c478bd9Sstevel@tonic-gate 		    "Could not check for running snapshot of %s (%s).\n", fmri,
1577c478bd9Sstevel@tonic-gate 		    scf_strerror(scf_error()));
1587c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
1597c478bd9Sstevel@tonic-gate 		return (NULL);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (_scf_snapshot_take_new(inst, SMF_SNAPSHOT_RUNNING, snap) == 0) {
1637c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Took running snapshot for %s.\n",
1647c478bd9Sstevel@tonic-gate 		    fmri);
1657c478bd9Sstevel@tonic-gate 	} else {
1667c478bd9Sstevel@tonic-gate 		if (retake && scf_error() == SCF_ERROR_BACKEND_READONLY)
1677c478bd9Sstevel@tonic-gate 			restarter_mark_pending_snapshot(fmri,
1687c478bd9Sstevel@tonic-gate 			    RINST_RETAKE_RUNNING);
1697c478bd9Sstevel@tonic-gate 		else
1707c478bd9Sstevel@tonic-gate 			log_error(LOG_DEBUG,
1717c478bd9Sstevel@tonic-gate 			    "Could not create running snapshot for %s "
1727c478bd9Sstevel@tonic-gate 			    "(%s).\n", fmri, scf_strerror(scf_error()));
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
1757c478bd9Sstevel@tonic-gate 		snap = NULL;
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	return (snap);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * When a service comes up, point the "start" snapshot at the "running"
1837c478bd9Sstevel@tonic-gate  * snapshot.  Returns 0 on success, ENOTSUP if fmri designates something other
1847c478bd9Sstevel@tonic-gate  * than an instance, ECONNABORTED, ENOENT if the instance does not exist, or
1857c478bd9Sstevel@tonic-gate  * EACCES.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate int
1887c478bd9Sstevel@tonic-gate libscf_snapshots_poststart(scf_handle_t *h, const char *fmri, boolean_t retake)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	scf_instance_t *inst = NULL;
1917c478bd9Sstevel@tonic-gate 	scf_snapshot_t *running, *start = NULL;
1927c478bd9Sstevel@tonic-gate 	int ret = 0, r;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	r = libscf_fmri_get_instance(h, fmri, &inst);
1957c478bd9Sstevel@tonic-gate 	switch (r) {
1967c478bd9Sstevel@tonic-gate 	case 0:
1977c478bd9Sstevel@tonic-gate 		break;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	case ENOTSUP:
2007c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
2017c478bd9Sstevel@tonic-gate 	case ENOENT:
2027c478bd9Sstevel@tonic-gate 		return (r);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	case EINVAL:
2057c478bd9Sstevel@tonic-gate 	default:
2067c478bd9Sstevel@tonic-gate 		assert(0);
2077c478bd9Sstevel@tonic-gate 		abort();
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	start = safe_scf_snapshot_create(h);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate again:
2137c478bd9Sstevel@tonic-gate 	running = libscf_get_or_make_running_snapshot(inst, fmri, retake);
2147c478bd9Sstevel@tonic-gate 	if (running == NULL) {
2157c478bd9Sstevel@tonic-gate 		ret = 0;
2167c478bd9Sstevel@tonic-gate 		goto out;
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate lookup:
2207c478bd9Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, "start", start) != 0) {
2217c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
2227c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
2237c478bd9Sstevel@tonic-gate 		default:
2247c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
2257c478bd9Sstevel@tonic-gate 			goto out;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
2287c478bd9Sstevel@tonic-gate 			if (_scf_snapshot_take_new(inst, "start", start) != 0) {
2297c478bd9Sstevel@tonic-gate 				switch (scf_error()) {
2307c478bd9Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
2317c478bd9Sstevel@tonic-gate 				default:
2327c478bd9Sstevel@tonic-gate 					ret = ECONNABORTED;
2337c478bd9Sstevel@tonic-gate 					goto out;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
2367c478bd9Sstevel@tonic-gate 					ret = ENOENT;
2377c478bd9Sstevel@tonic-gate 					goto out;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 				case SCF_ERROR_EXISTS:
2407c478bd9Sstevel@tonic-gate 					goto lookup;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NO_RESOURCES:
2437c478bd9Sstevel@tonic-gate 					uu_die("Repository server out of "
2447c478bd9Sstevel@tonic-gate 					    "resources.\n");
2457c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 				case SCF_ERROR_BACKEND_READONLY:
2487c478bd9Sstevel@tonic-gate 					goto readonly;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 				case SCF_ERROR_PERMISSION_DENIED:
2517c478bd9Sstevel@tonic-gate 					uu_die("Insufficient privileges.\n");
2527c478bd9Sstevel@tonic-gate 					/* NOTREACHED */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 				case SCF_ERROR_BACKEND_ACCESS:
2557c478bd9Sstevel@tonic-gate 					ret = EACCES;
2567c478bd9Sstevel@tonic-gate 					goto out;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
2595b7f77adStw21770 				case SCF_ERROR_INTERNAL:
2607c478bd9Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
2617c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
2627c478bd9Sstevel@tonic-gate 					bad_error("_scf_snapshot_take_new",
2637c478bd9Sstevel@tonic-gate 					    scf_error());
2647c478bd9Sstevel@tonic-gate 				}
2657c478bd9Sstevel@tonic-gate 			}
2667c478bd9Sstevel@tonic-gate 			break;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
2697c478bd9Sstevel@tonic-gate 			ret = ENOENT;
2707c478bd9Sstevel@tonic-gate 			goto out;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
2737c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
2747c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
2757c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_snapshot", scf_error());
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (_scf_snapshot_attach(running, start) == 0) {
2807c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Updated \"start\" snapshot for %s.\n",
2817c478bd9Sstevel@tonic-gate 		    fmri);
2827c478bd9Sstevel@tonic-gate 	} else {
2837c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
2847c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
2857c478bd9Sstevel@tonic-gate 		default:
2867c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
2877c478bd9Sstevel@tonic-gate 			goto out;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
2907c478bd9Sstevel@tonic-gate 			scf_snapshot_destroy(running);
2917c478bd9Sstevel@tonic-gate 			goto again;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NO_RESOURCES:
2947c478bd9Sstevel@tonic-gate 			uu_die("Repository server out of resources.\n");
2957c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		case SCF_ERROR_PERMISSION_DENIED:
2987c478bd9Sstevel@tonic-gate 			uu_die("Insufficient privileges.\n");
2997c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 		case SCF_ERROR_BACKEND_ACCESS:
3027c478bd9Sstevel@tonic-gate 			ret = EACCES;
3037c478bd9Sstevel@tonic-gate 			goto out;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		case SCF_ERROR_BACKEND_READONLY:
3067c478bd9Sstevel@tonic-gate readonly:
3077c478bd9Sstevel@tonic-gate 			if (retake)
3087c478bd9Sstevel@tonic-gate 				restarter_mark_pending_snapshot(fmri,
3097c478bd9Sstevel@tonic-gate 				    RINST_RETAKE_START);
3107c478bd9Sstevel@tonic-gate 			break;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
3137c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
3147c478bd9Sstevel@tonic-gate 			bad_error("_scf_snapshot_attach", scf_error());
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate out:
3197c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(start);
3207c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(running);
3217c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	return (ret);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * Before a refresh, update the "running" snapshot from the editing
3287c478bd9Sstevel@tonic-gate  * configuration.
3297c478bd9Sstevel@tonic-gate  *
3307c478bd9Sstevel@tonic-gate  * Returns 0 on success and -1 on failure.
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate int
3337c478bd9Sstevel@tonic-gate libscf_snapshots_refresh(scf_instance_t *inst, const char *fmri)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
3367c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
3377c478bd9Sstevel@tonic-gate 	boolean_t err = 1;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
3407c478bd9Sstevel@tonic-gate 	if (h == NULL)
3417c478bd9Sstevel@tonic-gate 		goto out;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
3447c478bd9Sstevel@tonic-gate 	if (snap == NULL)
3457c478bd9Sstevel@tonic-gate 		goto out;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, SMF_SNAPSHOT_RUNNING, snap) == 0) {
3487c478bd9Sstevel@tonic-gate 		if (_scf_snapshot_take_attach(inst, snap) == 0)
3497c478bd9Sstevel@tonic-gate 			err = 0;
3507c478bd9Sstevel@tonic-gate 	} else {
3517c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
3527c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
3537c478bd9Sstevel@tonic-gate 			err = 0;
3547c478bd9Sstevel@tonic-gate 			goto out;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
3577c478bd9Sstevel@tonic-gate 			break;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
3607c478bd9Sstevel@tonic-gate 			assert(0);
3617c478bd9Sstevel@tonic-gate 			abort();
3627c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		default:
3657c478bd9Sstevel@tonic-gate 			goto out;
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		log_error(LOG_DEBUG,
3697c478bd9Sstevel@tonic-gate 		    "Service %s has no %s snapshot; creating one.\n", fmri,
3707c478bd9Sstevel@tonic-gate 		    SMF_SNAPSHOT_RUNNING);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		if (_scf_snapshot_take_new(inst, SMF_SNAPSHOT_RUNNING,
3737c478bd9Sstevel@tonic-gate 		    snap) == 0)
3747c478bd9Sstevel@tonic-gate 			err = 0;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate out:
3787c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if (!err)
3817c478bd9Sstevel@tonic-gate 		return (0);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	log_error(LOG_WARNING,
3847c478bd9Sstevel@tonic-gate 	    "Could not update \"running\" snapshot for refresh of %s.\n", fmri);
3857c478bd9Sstevel@tonic-gate 	return (-1);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * int libscf_read_single_astring()
3907c478bd9Sstevel@tonic-gate  *   Reads a single astring value of the requested property into the
3917c478bd9Sstevel@tonic-gate  *   pre-allocated buffer (conventionally of size max_scf_value_size).
3927c478bd9Sstevel@tonic-gate  *   Multiple values constitute an error.
3937c478bd9Sstevel@tonic-gate  *
3947c478bd9Sstevel@tonic-gate  * Returns 0 on success or LIBSCF_PROPERTY_ABSENT or LIBSCF_PROPERTY_ERROR.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate static int
3977c478bd9Sstevel@tonic-gate libscf_read_single_astring(scf_handle_t *h, scf_property_t *prop, char **ret)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	scf_value_t *val = safe_scf_value_create(h);
4007c478bd9Sstevel@tonic-gate 	int r = 0;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) == -1) {
4037c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
4047c478bd9Sstevel@tonic-gate 			r = LIBSCF_PROPERTY_ABSENT;
4057c478bd9Sstevel@tonic-gate 		else
4067c478bd9Sstevel@tonic-gate 			r = LIBSCF_PROPERTY_ERROR;
4077c478bd9Sstevel@tonic-gate 		goto read_single_astring_fail;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (scf_value_get_astring(val, *ret, max_scf_value_size) <= 0) {
4117c478bd9Sstevel@tonic-gate 		r = LIBSCF_PROPERTY_ERROR;
4127c478bd9Sstevel@tonic-gate 		goto read_single_astring_fail;
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate read_single_astring_fail:
4167c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
4177c478bd9Sstevel@tonic-gate 	return (r);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate static int
4217c478bd9Sstevel@tonic-gate libscf_read_state(const scf_propertygroup_t *pg, const char *prop_name,
4227c478bd9Sstevel@tonic-gate     restarter_instance_state_t *state)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
4257c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
4267c478bd9Sstevel@tonic-gate 	char *char_state = startd_alloc(max_scf_value_size);
4277c478bd9Sstevel@tonic-gate 	int ret = 0;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
4307c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, prop_name, prop) == -1) {
4337c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
4347c478bd9Sstevel@tonic-gate 			ret = LIBSCF_PROPERTY_ABSENT;
4357c478bd9Sstevel@tonic-gate 		else
4367c478bd9Sstevel@tonic-gate 			ret = LIBSCF_PROPERTY_ERROR;
4377c478bd9Sstevel@tonic-gate 	} else {
4387c478bd9Sstevel@tonic-gate 		ret = libscf_read_single_astring(h, prop, &char_state);
4397c478bd9Sstevel@tonic-gate 		if (ret != 0) {
4407c478bd9Sstevel@tonic-gate 			if (ret != LIBSCF_PROPERTY_ABSENT)
4417c478bd9Sstevel@tonic-gate 				ret = LIBSCF_PROPERTY_ERROR;
4427c478bd9Sstevel@tonic-gate 		} else {
4437c478bd9Sstevel@tonic-gate 			*state = restarter_string_to_state(char_state);
4447c478bd9Sstevel@tonic-gate 			ret = 0;
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	startd_free(char_state, max_scf_value_size);
4497c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
4507c478bd9Sstevel@tonic-gate 	return (ret);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate  * int libscf_read_states(const scf_propertygroup_t *,
4557c478bd9Sstevel@tonic-gate  *   restarter_instance_state_t *, restarter_instance_state_t *)
4567c478bd9Sstevel@tonic-gate  *
4577c478bd9Sstevel@tonic-gate  *   Set the current state and next_state values for the given service instance.
4587c478bd9Sstevel@tonic-gate  *   Returns 0 on success, or a libscf error code on failure.
4597c478bd9Sstevel@tonic-gate  */
4607c478bd9Sstevel@tonic-gate int
4617c478bd9Sstevel@tonic-gate libscf_read_states(const scf_propertygroup_t *pg,
4627c478bd9Sstevel@tonic-gate     restarter_instance_state_t *state, restarter_instance_state_t *next_state)
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	int state_ret, next_state_ret, ret;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	state_ret = libscf_read_state(pg, SCF_PROPERTY_STATE, state);
4677c478bd9Sstevel@tonic-gate 	next_state_ret = libscf_read_state(pg, SCF_PROPERTY_NEXT_STATE,
4687c478bd9Sstevel@tonic-gate 	    next_state);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if (state_ret == LIBSCF_PROPERTY_ERROR ||
4717c478bd9Sstevel@tonic-gate 	    next_state_ret == LIBSCF_PROPERTY_ERROR) {
4727c478bd9Sstevel@tonic-gate 		ret = LIBSCF_PROPERTY_ERROR;
4737c478bd9Sstevel@tonic-gate 	} else if (state_ret == 0 && next_state_ret == 0) {
4747c478bd9Sstevel@tonic-gate 		ret = 0;
4757c478bd9Sstevel@tonic-gate 	} else if (state_ret == LIBSCF_PROPERTY_ABSENT &&
4767c478bd9Sstevel@tonic-gate 	    next_state_ret == LIBSCF_PROPERTY_ABSENT) {
4777c478bd9Sstevel@tonic-gate 		*state = RESTARTER_STATE_UNINIT;
4787c478bd9Sstevel@tonic-gate 		*next_state = RESTARTER_STATE_NONE;
4797c478bd9Sstevel@tonic-gate 		ret = 0;
4807c478bd9Sstevel@tonic-gate 	} else if (state_ret == LIBSCF_PROPERTY_ABSENT ||
4817c478bd9Sstevel@tonic-gate 	    next_state_ret == LIBSCF_PROPERTY_ABSENT) {
4827c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
4837c478bd9Sstevel@tonic-gate 		    "Only one repository state exists, setting "
4847c478bd9Sstevel@tonic-gate 		    "restarter states to MAINTENANCE and NONE\n");
4857c478bd9Sstevel@tonic-gate 		*state = RESTARTER_STATE_MAINT;
4867c478bd9Sstevel@tonic-gate 		*next_state = RESTARTER_STATE_NONE;
4877c478bd9Sstevel@tonic-gate 		ret = 0;
4887c478bd9Sstevel@tonic-gate 	} else {
4897c478bd9Sstevel@tonic-gate 		ret = LIBSCF_PROPERTY_ERROR;
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate read_states_out:
4937c478bd9Sstevel@tonic-gate 	return (ret);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * depgroup_empty()
4987c478bd9Sstevel@tonic-gate  *
4997c478bd9Sstevel@tonic-gate  * Returns 0 if not empty.
5007c478bd9Sstevel@tonic-gate  * Returns 1 if empty.
5017c478bd9Sstevel@tonic-gate  * Returns -1 on error (check scf_error()).
5027c478bd9Sstevel@tonic-gate  */
5037c478bd9Sstevel@tonic-gate int
5047c478bd9Sstevel@tonic-gate depgroup_empty(scf_handle_t *h, scf_propertygroup_t *pg)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	int empty = 1;
5077c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
5087c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
5097c478bd9Sstevel@tonic-gate 	int ret;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
5127c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
5137c478bd9Sstevel@tonic-gate 
5145b6c5443Srm88369 	if (scf_iter_pg_properties(iter, pg) != SCF_SUCCESS) {
5155b6c5443Srm88369 		scf_property_destroy(prop);
5165b6c5443Srm88369 		scf_iter_destroy(iter);
5177c478bd9Sstevel@tonic-gate 		return (-1);
5185b6c5443Srm88369 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	ret = scf_iter_next_property(iter, prop);
5215b6c5443Srm88369 	if (ret < 0) {
5225b6c5443Srm88369 		scf_property_destroy(prop);
5235b6c5443Srm88369 		scf_iter_destroy(iter);
5247c478bd9Sstevel@tonic-gate 		return (-1);
5255b6c5443Srm88369 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (ret == 1)
5287c478bd9Sstevel@tonic-gate 		empty = 0;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
5317c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	return (empty);
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate gv_type_t
5377c478bd9Sstevel@tonic-gate depgroup_read_scheme(scf_handle_t *h, scf_propertygroup_t *pg)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
5407c478bd9Sstevel@tonic-gate 	char *scheme = startd_alloc(max_scf_value_size);
5417c478bd9Sstevel@tonic-gate 	gv_type_t ret;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_TYPE, prop) == -1 ||
5467c478bd9Sstevel@tonic-gate 	    libscf_read_single_astring(h, prop, &scheme) != 0) {
5477c478bd9Sstevel@tonic-gate 		scf_property_destroy(prop);
5487c478bd9Sstevel@tonic-gate 		startd_free(scheme, max_scf_value_size);
5497c478bd9Sstevel@tonic-gate 		return (GVT_UNSUPPORTED);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	if (strcmp(scheme, "service") == 0)
5537c478bd9Sstevel@tonic-gate 		ret = GVT_INST;
5547c478bd9Sstevel@tonic-gate 	else if (strcmp(scheme, "path") == 0)
5557c478bd9Sstevel@tonic-gate 		ret = GVT_FILE;
5567c478bd9Sstevel@tonic-gate 	else
5577c478bd9Sstevel@tonic-gate 		ret = GVT_UNSUPPORTED;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	startd_free(scheme, max_scf_value_size);
5607c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
5617c478bd9Sstevel@tonic-gate 	return (ret);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate depgroup_type_t
5657c478bd9Sstevel@tonic-gate depgroup_read_grouping(scf_handle_t *h, scf_propertygroup_t *pg)
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate 	char *grouping = startd_alloc(max_scf_value_size);
5687c478bd9Sstevel@tonic-gate 	depgroup_type_t ret;
5697c478bd9Sstevel@tonic-gate 	scf_property_t *prop = safe_scf_property_create(h);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_GROUPING, prop) == -1 ||
5727c478bd9Sstevel@tonic-gate 	    libscf_read_single_astring(h, prop, &grouping) != 0) {
5737c478bd9Sstevel@tonic-gate 		scf_property_destroy(prop);
5747c478bd9Sstevel@tonic-gate 		startd_free(grouping, max_scf_value_size);
5757c478bd9Sstevel@tonic-gate 		return (DEPGRP_UNSUPPORTED);
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (strcmp(grouping, SCF_DEP_REQUIRE_ANY) == 0)
5797c478bd9Sstevel@tonic-gate 		ret = DEPGRP_REQUIRE_ANY;
5807c478bd9Sstevel@tonic-gate 	else if (strcmp(grouping, SCF_DEP_REQUIRE_ALL) == 0)
5817c478bd9Sstevel@tonic-gate 		ret = DEPGRP_REQUIRE_ALL;
5827c478bd9Sstevel@tonic-gate 	else if (strcmp(grouping, SCF_DEP_OPTIONAL_ALL) == 0)
5837c478bd9Sstevel@tonic-gate 		ret = DEPGRP_OPTIONAL_ALL;
5847c478bd9Sstevel@tonic-gate 	else if (strcmp(grouping, SCF_DEP_EXCLUDE_ALL) == 0)
5857c478bd9Sstevel@tonic-gate 		ret = DEPGRP_EXCLUDE_ALL;
5867c478bd9Sstevel@tonic-gate 	else {
5877c478bd9Sstevel@tonic-gate 		ret = DEPGRP_UNSUPPORTED;
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	startd_free(grouping, max_scf_value_size);
5907c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
5917c478bd9Sstevel@tonic-gate 	return (ret);
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate restarter_error_t
5957c478bd9Sstevel@tonic-gate depgroup_read_restart(scf_handle_t *h, scf_propertygroup_t *pg)
5967c478bd9Sstevel@tonic-gate {
5977c478bd9Sstevel@tonic-gate 	scf_property_t *prop = safe_scf_property_create(h);
5987c478bd9Sstevel@tonic-gate 	char *restart_on = startd_alloc(max_scf_value_size);
5997c478bd9Sstevel@tonic-gate 	restarter_error_t ret;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_RESTART_ON, prop) == -1 ||
6027c478bd9Sstevel@tonic-gate 	    libscf_read_single_astring(h, prop, &restart_on) != 0) {
6037c478bd9Sstevel@tonic-gate 		startd_free(restart_on, max_scf_value_size);
6047c478bd9Sstevel@tonic-gate 		scf_property_destroy(prop);
6057c478bd9Sstevel@tonic-gate 		return (RERR_UNSUPPORTED);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if (strcmp(restart_on, SCF_DEP_RESET_ON_ERROR) == 0)
6097c478bd9Sstevel@tonic-gate 		ret = RERR_FAULT;
6107c478bd9Sstevel@tonic-gate 	else if (strcmp(restart_on, SCF_DEP_RESET_ON_RESTART) == 0)
6117c478bd9Sstevel@tonic-gate 		ret = RERR_RESTART;
6127c478bd9Sstevel@tonic-gate 	else if (strcmp(restart_on, SCF_DEP_RESET_ON_REFRESH) == 0)
6137c478bd9Sstevel@tonic-gate 		ret = RERR_REFRESH;
6147c478bd9Sstevel@tonic-gate 	else if (strcmp(restart_on, SCF_DEP_RESET_ON_NONE) == 0)
6157c478bd9Sstevel@tonic-gate 		ret = RERR_NONE;
6167c478bd9Sstevel@tonic-gate 	else
6177c478bd9Sstevel@tonic-gate 		ret = RERR_UNSUPPORTED;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	startd_free(restart_on, max_scf_value_size);
6207c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
6217c478bd9Sstevel@tonic-gate 	return (ret);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * int get_boolean()
6267c478bd9Sstevel@tonic-gate  *   Fetches the value of a boolean property of the given property group.
6277c478bd9Sstevel@tonic-gate  *   Returns
6287c478bd9Sstevel@tonic-gate  *     0 - success
6297c478bd9Sstevel@tonic-gate  *     ECONNABORTED - repository connection broken
6307c478bd9Sstevel@tonic-gate  *     ECANCELED - pg was deleted
6317c478bd9Sstevel@tonic-gate  *     ENOENT - the property doesn't exist or has no values
6327c478bd9Sstevel@tonic-gate  *     EINVAL - the property has the wrong type
6337c478bd9Sstevel@tonic-gate  *		the property is not single-valued
6343eae19d9Swesolows  *     EACCES - the current user does not have permission to read the value
6357c478bd9Sstevel@tonic-gate  */
6367c478bd9Sstevel@tonic-gate static int
6377c478bd9Sstevel@tonic-gate get_boolean(scf_propertygroup_t *pg, const char *propname, uint8_t *valuep)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
6407c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
6417c478bd9Sstevel@tonic-gate 	scf_value_t *val;
6427c478bd9Sstevel@tonic-gate 	int ret = 0, r;
6437c478bd9Sstevel@tonic-gate 	scf_type_t type;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
6467c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
6477c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, propname, prop) != 0) {
6507c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
6517c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
6527c478bd9Sstevel@tonic-gate 		default:
6537c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
6547c478bd9Sstevel@tonic-gate 			goto out;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
6577c478bd9Sstevel@tonic-gate 			ret = ECANCELED;
6587c478bd9Sstevel@tonic-gate 			goto out;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
6617c478bd9Sstevel@tonic-gate 			ret = ENOENT;
6627c478bd9Sstevel@tonic-gate 			goto out;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
6657c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
6667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
6677c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (scf_property_type(prop, &type) != 0) {
6727c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
6737c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
6747c478bd9Sstevel@tonic-gate 		default:
6757c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
6767c478bd9Sstevel@tonic-gate 			goto out;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
6797c478bd9Sstevel@tonic-gate 			ret = ENOENT;
6807c478bd9Sstevel@tonic-gate 			goto out;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
6837c478bd9Sstevel@tonic-gate 			bad_error("scf_property_type", scf_error());
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if (type != SCF_TYPE_BOOLEAN) {
6887c478bd9Sstevel@tonic-gate 		ret = EINVAL;
6897c478bd9Sstevel@tonic-gate 		goto out;
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != 0) {
6937c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
6947c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
6957c478bd9Sstevel@tonic-gate 		default:
6967c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
6977c478bd9Sstevel@tonic-gate 			goto out;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7007c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7017c478bd9Sstevel@tonic-gate 			ret = ENOENT;
7027c478bd9Sstevel@tonic-gate 			goto out;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
7057c478bd9Sstevel@tonic-gate 			ret = EINVAL;
7067c478bd9Sstevel@tonic-gate 			goto out;
7077c478bd9Sstevel@tonic-gate 
7083eae19d9Swesolows 		case SCF_ERROR_PERMISSION_DENIED:
7093eae19d9Swesolows 			ret = EACCES;
7103eae19d9Swesolows 			goto out;
7113eae19d9Swesolows 
7127c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7137c478bd9Sstevel@tonic-gate 			bad_error("scf_property_get_value", scf_error());
7147c478bd9Sstevel@tonic-gate 		}
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	r = scf_value_get_boolean(val, valuep);
7187c478bd9Sstevel@tonic-gate 	assert(r == 0);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate out:
7217c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
7227c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
7237c478bd9Sstevel@tonic-gate 	return (ret);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate  * int get_count()
7287c478bd9Sstevel@tonic-gate  *   Fetches the value of a count property of the given property group.
7297c478bd9Sstevel@tonic-gate  *   Returns
7307c478bd9Sstevel@tonic-gate  *     0 - success
7317c478bd9Sstevel@tonic-gate  *     ECONNABORTED - repository connection broken
7327c478bd9Sstevel@tonic-gate  *                    unknown libscf error
7337c478bd9Sstevel@tonic-gate  *     ECANCELED - pg was deleted
7347c478bd9Sstevel@tonic-gate  *     ENOENT - the property doesn't exist or has no values
7357c478bd9Sstevel@tonic-gate  *     EINVAL - the property has the wrong type
7367c478bd9Sstevel@tonic-gate  *              the property is not single-valued
7373eae19d9Swesolows  *     EACCES - the current user does not have permission to read the value
7387c478bd9Sstevel@tonic-gate  */
7397c478bd9Sstevel@tonic-gate static int
7407c478bd9Sstevel@tonic-gate get_count(scf_propertygroup_t *pg, const char *propname, uint64_t *valuep)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
7437c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
7447c478bd9Sstevel@tonic-gate 	scf_value_t *val;
7457c478bd9Sstevel@tonic-gate 	int ret = 0, r;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
7487c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
7497c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, propname, prop) != 0) {
7527c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
7537c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7547c478bd9Sstevel@tonic-gate 		default:
7557c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
7567c478bd9Sstevel@tonic-gate 			goto out;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7597c478bd9Sstevel@tonic-gate 			ret = ECANCELED;
7607c478bd9Sstevel@tonic-gate 			goto out;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7637c478bd9Sstevel@tonic-gate 			ret = ENOENT;
7647c478bd9Sstevel@tonic-gate 			goto out;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7677c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7687c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7697c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
7707c478bd9Sstevel@tonic-gate 		}
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (scf_property_is_type(prop, SCF_TYPE_COUNT) != 0) {
7747c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
7757c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7767c478bd9Sstevel@tonic-gate 		default:
7777c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
7787c478bd9Sstevel@tonic-gate 			goto out;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 		case SCF_ERROR_TYPE_MISMATCH:
7817c478bd9Sstevel@tonic-gate 			ret = EINVAL;
7827c478bd9Sstevel@tonic-gate 			goto out;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7857c478bd9Sstevel@tonic-gate 			ret = ECANCELED;
7867c478bd9Sstevel@tonic-gate 			goto out;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7897c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
7907c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7917c478bd9Sstevel@tonic-gate 			bad_error("scf_property_is_type", scf_error());
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != 0) {
7967c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
7977c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7987c478bd9Sstevel@tonic-gate 		default:
7997c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
8007c478bd9Sstevel@tonic-gate 			goto out;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
8037c478bd9Sstevel@tonic-gate 			ret = ECANCELED;
8047c478bd9Sstevel@tonic-gate 			goto out;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
8077c478bd9Sstevel@tonic-gate 			ret = ENOENT;
8087c478bd9Sstevel@tonic-gate 			goto out;
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
8117c478bd9Sstevel@tonic-gate 			ret = EINVAL;
8127c478bd9Sstevel@tonic-gate 			goto out;
8137c478bd9Sstevel@tonic-gate 
8143eae19d9Swesolows 		case SCF_ERROR_PERMISSION_DENIED:
8153eae19d9Swesolows 			ret = EACCES;
8163eae19d9Swesolows 			goto out;
8173eae19d9Swesolows 
8187c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
8197c478bd9Sstevel@tonic-gate 			bad_error("scf_property_get_value", scf_error());
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	r = scf_value_get_count(val, valuep);
8247c478bd9Sstevel@tonic-gate 	assert(r == 0);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate out:
8277c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
8287c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
8297c478bd9Sstevel@tonic-gate 	return (ret);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate static void
8347c478bd9Sstevel@tonic-gate get_restarter(scf_handle_t *h, scf_propertygroup_t *pg, char **restarter)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	scf_property_t *prop = safe_scf_property_create(h);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_RESTARTER, prop) == -1 ||
8397c478bd9Sstevel@tonic-gate 	    libscf_read_single_astring(h, prop, restarter) != 0)
8407c478bd9Sstevel@tonic-gate 		*restarter[0] = '\0';
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate  * int libscf_instance_get_fmri(scf_instance_t *, char **)
8477c478bd9Sstevel@tonic-gate  *   Give a valid SCF instance, return its FMRI.  Returns 0 on success,
8487c478bd9Sstevel@tonic-gate  *   ECONNABORTED, or ECANCELED if inst is deleted.
8497c478bd9Sstevel@tonic-gate  */
8507c478bd9Sstevel@tonic-gate int
8517c478bd9Sstevel@tonic-gate libscf_instance_get_fmri(scf_instance_t *inst, char **retp)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	char *inst_fmri = startd_alloc(max_scf_fmri_size);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	inst_fmri[0] = 0;
8567c478bd9Sstevel@tonic-gate 	if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) <= 0) {
8577c478bd9Sstevel@tonic-gate 		startd_free(inst_fmri, max_scf_fmri_size);
8587c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
8597c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
8607c478bd9Sstevel@tonic-gate 		default:
8617c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
8647c478bd9Sstevel@tonic-gate 			return (ECANCELED);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
8677c478bd9Sstevel@tonic-gate 			assert(0);
8687c478bd9Sstevel@tonic-gate 			abort();
8697c478bd9Sstevel@tonic-gate 		}
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	*retp = inst_fmri;
8737c478bd9Sstevel@tonic-gate 	return (0);
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate  * int libscf_fmri_get_instance(scf_handle_t *, const char *,
8787c478bd9Sstevel@tonic-gate  *	scf_instance_t **)
8797c478bd9Sstevel@tonic-gate  *   Given a valid SCF handle and an FMRI, return the SCF instance that matches
8807c478bd9Sstevel@tonic-gate  *   exactly.  The instance must be released using scf_instance_destroy().
8817c478bd9Sstevel@tonic-gate  *   Returns 0 on success, EINVAL if the FMRI is invalid, ENOTSUP if the FMRI
8827c478bd9Sstevel@tonic-gate  *   is valid but designates something other than an instance, ECONNABORTED if
8837c478bd9Sstevel@tonic-gate  *   the repository connection is broken, or ENOENT if the instance does not
8847c478bd9Sstevel@tonic-gate  *   exist.
8857c478bd9Sstevel@tonic-gate  */
8867c478bd9Sstevel@tonic-gate int
8877c478bd9Sstevel@tonic-gate libscf_fmri_get_instance(scf_handle_t *h, const char *fmri,
8887c478bd9Sstevel@tonic-gate     scf_instance_t **instp)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
8917c478bd9Sstevel@tonic-gate 	int r;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	r = libscf_lookup_instance(fmri, inst);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	if (r == 0)
8987c478bd9Sstevel@tonic-gate 		*instp = inst;
8997c478bd9Sstevel@tonic-gate 	else
9007c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	return (r);
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate int
9067c478bd9Sstevel@tonic-gate libscf_lookup_instance(const char *fmri, scf_instance_t *inst)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(scf_instance_handle(inst), fmri, NULL, NULL,
9097c478bd9Sstevel@tonic-gate 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS) {
9107c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
9117c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
9127c478bd9Sstevel@tonic-gate 			return (EINVAL);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
9157c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
9187c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
9217c478bd9Sstevel@tonic-gate 			return (ENOENT);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
9247c478bd9Sstevel@tonic-gate 		default:
9257c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	return (0);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate /*
933*70cbfe41SPhilippe Jung  * int libscf_get_deathrow()
934*70cbfe41SPhilippe Jung  * Read deathrow for inst. Returns 0, ECONNABORTED if the connection to the
935*70cbfe41SPhilippe Jung  * repository is broken, ECANCELED if inst is deleted, or ENOENT if inst
936*70cbfe41SPhilippe Jung  * has no deathrow property group.
937*70cbfe41SPhilippe Jung  *
938*70cbfe41SPhilippe Jung  * If deathrow/deathrow was missing or invalid, *deathrow will be -1 and a
939*70cbfe41SPhilippe Jung  * debug message is logged.
940*70cbfe41SPhilippe Jung  */
941*70cbfe41SPhilippe Jung int
942*70cbfe41SPhilippe Jung libscf_get_deathrow(scf_handle_t *h, scf_instance_t *inst, int *deathrow)
943*70cbfe41SPhilippe Jung {
944*70cbfe41SPhilippe Jung 	scf_propertygroup_t *pg;
945*70cbfe41SPhilippe Jung 	int r;
946*70cbfe41SPhilippe Jung 	uint8_t deathrow_8;
947*70cbfe41SPhilippe Jung 
948*70cbfe41SPhilippe Jung 	pg = safe_scf_pg_create(h);
949*70cbfe41SPhilippe Jung 
950*70cbfe41SPhilippe Jung 	if (scf_instance_get_pg_composed(inst, NULL, SCF_PG_DEATHROW, pg) !=
951*70cbfe41SPhilippe Jung 	    0) {
952*70cbfe41SPhilippe Jung 		switch (scf_error()) {
953*70cbfe41SPhilippe Jung 		case SCF_ERROR_CONNECTION_BROKEN:
954*70cbfe41SPhilippe Jung 		default:
955*70cbfe41SPhilippe Jung 			scf_pg_destroy(pg);
956*70cbfe41SPhilippe Jung 			return (ECONNABORTED);
957*70cbfe41SPhilippe Jung 
958*70cbfe41SPhilippe Jung 		case SCF_ERROR_DELETED:
959*70cbfe41SPhilippe Jung 			scf_pg_destroy(pg);
960*70cbfe41SPhilippe Jung 			return (ECANCELED);
961*70cbfe41SPhilippe Jung 
962*70cbfe41SPhilippe Jung 		case SCF_ERROR_NOT_FOUND:
963*70cbfe41SPhilippe Jung 			*deathrow = -1;
964*70cbfe41SPhilippe Jung 			break;
965*70cbfe41SPhilippe Jung 
966*70cbfe41SPhilippe Jung 		case SCF_ERROR_HANDLE_MISMATCH:
967*70cbfe41SPhilippe Jung 		case SCF_ERROR_INVALID_ARGUMENT:
968*70cbfe41SPhilippe Jung 		case SCF_ERROR_NOT_SET:
969*70cbfe41SPhilippe Jung 			bad_error("libscf_get_deathrow", scf_error());
970*70cbfe41SPhilippe Jung 		}
971*70cbfe41SPhilippe Jung 	} else {
972*70cbfe41SPhilippe Jung 		switch (r = get_boolean(pg,
973*70cbfe41SPhilippe Jung 		    SCF_PROPERTY_DEATHROW, &deathrow_8)) {
974*70cbfe41SPhilippe Jung 		case 0:
975*70cbfe41SPhilippe Jung 			*deathrow = deathrow_8;
976*70cbfe41SPhilippe Jung 			break;
977*70cbfe41SPhilippe Jung 
978*70cbfe41SPhilippe Jung 		case ECONNABORTED:
979*70cbfe41SPhilippe Jung 		case ECANCELED:
980*70cbfe41SPhilippe Jung 			scf_pg_destroy(pg);
981*70cbfe41SPhilippe Jung 			return (r);
982*70cbfe41SPhilippe Jung 
983*70cbfe41SPhilippe Jung 		case ENOENT:
984*70cbfe41SPhilippe Jung 		case EINVAL:
985*70cbfe41SPhilippe Jung 			*deathrow = -1;
986*70cbfe41SPhilippe Jung 			break;
987*70cbfe41SPhilippe Jung 
988*70cbfe41SPhilippe Jung 		default:
989*70cbfe41SPhilippe Jung 			bad_error("get_boolean", r);
990*70cbfe41SPhilippe Jung 		}
991*70cbfe41SPhilippe Jung 	}
992*70cbfe41SPhilippe Jung 
993*70cbfe41SPhilippe Jung 	scf_pg_destroy(pg);
994*70cbfe41SPhilippe Jung 
995*70cbfe41SPhilippe Jung 	return (0);
996*70cbfe41SPhilippe Jung }
997*70cbfe41SPhilippe Jung 
998*70cbfe41SPhilippe Jung /*
9997c478bd9Sstevel@tonic-gate  * void libscf_get_basic_instance_data()
10007c478bd9Sstevel@tonic-gate  *   Read enabled, enabled_ovr, and restarter_fmri (into an allocated
10017c478bd9Sstevel@tonic-gate  *   buffer) for inst.  Returns 0, ECONNABORTED if the connection to the
10027c478bd9Sstevel@tonic-gate  *   repository is broken, ECANCELED if inst is deleted, or ENOENT if inst
10037c478bd9Sstevel@tonic-gate  *   has no general property group.
10047c478bd9Sstevel@tonic-gate  *
10057c478bd9Sstevel@tonic-gate  *   On success, restarter_fmri may be NULL.  If general/enabled was missing
10067c478bd9Sstevel@tonic-gate  *   or invalid, *enabledp will be -1 and a debug message is logged.
10077c478bd9Sstevel@tonic-gate  */
10087c478bd9Sstevel@tonic-gate int
10097c478bd9Sstevel@tonic-gate libscf_get_basic_instance_data(scf_handle_t *h, scf_instance_t *inst,
10107c478bd9Sstevel@tonic-gate     const char *fmri, int *enabledp, int *enabled_ovrp, char **restarter_fmri)
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
10137c478bd9Sstevel@tonic-gate 	int r;
10147c478bd9Sstevel@tonic-gate 	uint8_t enabled_8;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	if (enabled_ovrp == NULL)
10197c478bd9Sstevel@tonic-gate 		goto enabled;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, NULL, SCF_PG_GENERAL_OVR, pg) !=
10227c478bd9Sstevel@tonic-gate 	    0) {
10237c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
10247c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
10257c478bd9Sstevel@tonic-gate 		default:
10267c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
10277c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
10307c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
10317c478bd9Sstevel@tonic-gate 			return (ECANCELED);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
10347c478bd9Sstevel@tonic-gate 			*enabled_ovrp = -1;
10357c478bd9Sstevel@tonic-gate 			break;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
10387c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
10397c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
10407c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg_composed", scf_error());
10417c478bd9Sstevel@tonic-gate 		}
10427c478bd9Sstevel@tonic-gate 	} else {
10437c478bd9Sstevel@tonic-gate 		switch (r = get_boolean(pg, SCF_PROPERTY_ENABLED, &enabled_8)) {
10447c478bd9Sstevel@tonic-gate 		case 0:
10457c478bd9Sstevel@tonic-gate 			*enabled_ovrp = enabled_8;
10467c478bd9Sstevel@tonic-gate 			break;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
10497c478bd9Sstevel@tonic-gate 		case ECANCELED:
10507c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
10517c478bd9Sstevel@tonic-gate 			return (r);
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		case ENOENT:
10547c478bd9Sstevel@tonic-gate 		case EINVAL:
10557c478bd9Sstevel@tonic-gate 			*enabled_ovrp = -1;
10567c478bd9Sstevel@tonic-gate 			break;
10577c478bd9Sstevel@tonic-gate 
10583eae19d9Swesolows 		case EACCES:
10597c478bd9Sstevel@tonic-gate 		default:
10607c478bd9Sstevel@tonic-gate 			bad_error("get_boolean", r);
10617c478bd9Sstevel@tonic-gate 		}
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate enabled:
10657c478bd9Sstevel@tonic-gate 	/*
10667c478bd9Sstevel@tonic-gate 	 * Since general/restarter can be at the service level, we must do
10677c478bd9Sstevel@tonic-gate 	 * a composed lookup.  These properties are immediate, though, so we
10687c478bd9Sstevel@tonic-gate 	 * must use the "editing" snapshot.  Technically enabled shouldn't be
10697c478bd9Sstevel@tonic-gate 	 * at the service level, but looking it up composed, too, doesn't
10707c478bd9Sstevel@tonic-gate 	 * hurt.
10717c478bd9Sstevel@tonic-gate 	 */
10727c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, NULL, SCF_PG_GENERAL, pg) != 0) {
10737c478bd9Sstevel@tonic-gate 		scf_pg_destroy(pg);
10747c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
10757c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
10767c478bd9Sstevel@tonic-gate 		default:
10777c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
10807c478bd9Sstevel@tonic-gate 			return (ECANCELED);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
10837c478bd9Sstevel@tonic-gate 			return (ENOENT);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
10867c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg_composed", scf_error());
10877c478bd9Sstevel@tonic-gate 		}
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	switch (r = get_boolean(pg, SCF_PROPERTY_ENABLED, &enabled_8)) {
10917c478bd9Sstevel@tonic-gate 	case 0:
10927c478bd9Sstevel@tonic-gate 		*enabledp = enabled_8;
10937c478bd9Sstevel@tonic-gate 		break;
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
10967c478bd9Sstevel@tonic-gate 	case ECANCELED:
10977c478bd9Sstevel@tonic-gate 		scf_pg_destroy(pg);
10987c478bd9Sstevel@tonic-gate 		return (r);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	case ENOENT:
11017c478bd9Sstevel@tonic-gate 		/*
11027c478bd9Sstevel@tonic-gate 		 * DEBUG because this happens when svccfg import creates
11037c478bd9Sstevel@tonic-gate 		 * a temporary service.
11047c478bd9Sstevel@tonic-gate 		 */
11057c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
11067c478bd9Sstevel@tonic-gate 		    "general/enabled property of %s is missing.\n", fmri);
11077c478bd9Sstevel@tonic-gate 		*enabledp = -1;
11087c478bd9Sstevel@tonic-gate 		break;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	case EINVAL:
11117c478bd9Sstevel@tonic-gate 		log_framework(LOG_ERR,
11127c478bd9Sstevel@tonic-gate 		    "general/enabled property of %s is invalid.\n", fmri);
11137c478bd9Sstevel@tonic-gate 		*enabledp = -1;
11147c478bd9Sstevel@tonic-gate 		break;
11157c478bd9Sstevel@tonic-gate 
11163eae19d9Swesolows 	case EACCES:
11177c478bd9Sstevel@tonic-gate 	default:
11187c478bd9Sstevel@tonic-gate 		bad_error("get_boolean", r);
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	if (restarter_fmri != NULL)
11227c478bd9Sstevel@tonic-gate 		get_restarter(h, pg, restarter_fmri);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	return (0);
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate  * Sets pg to the name property group of s_inst.  If it doesn't exist, it is
11327c478bd9Sstevel@tonic-gate  * added.
11337c478bd9Sstevel@tonic-gate  *
11347c478bd9Sstevel@tonic-gate  * Fails with
11357c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository disconnection or unknown libscf error
11367c478bd9Sstevel@tonic-gate  *   ECANCELED - inst is deleted
11377c478bd9Sstevel@tonic-gate  *   EPERM - permission is denied
11387c478bd9Sstevel@tonic-gate  *   EACCES - backend denied access
11397c478bd9Sstevel@tonic-gate  *   EROFS - backend readonly
11407c478bd9Sstevel@tonic-gate  */
11417c478bd9Sstevel@tonic-gate int
11427c478bd9Sstevel@tonic-gate libscf_inst_get_or_add_pg(scf_instance_t *inst, const char *name,
11437c478bd9Sstevel@tonic-gate     const char *type, uint32_t flags, scf_propertygroup_t *pg)
11447c478bd9Sstevel@tonic-gate {
11457c478bd9Sstevel@tonic-gate 	uint32_t f;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate again:
11487c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, name, pg) == 0) {
11497c478bd9Sstevel@tonic-gate 		if (scf_pg_get_flags(pg, &f) != 0) {
11507c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
11517c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
11527c478bd9Sstevel@tonic-gate 			default:
11537c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
11567c478bd9Sstevel@tonic-gate 				goto add;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
11597c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_flags", scf_error());
11607c478bd9Sstevel@tonic-gate 			}
11617c478bd9Sstevel@tonic-gate 		}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 		if (f == flags)
11647c478bd9Sstevel@tonic-gate 			return (0);
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		if (scf_pg_delete(pg) != 0) {
11677c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
11687c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
11697c478bd9Sstevel@tonic-gate 			default:
11707c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
11737c478bd9Sstevel@tonic-gate 				break;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
11767c478bd9Sstevel@tonic-gate 				return (EPERM);
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
11797c478bd9Sstevel@tonic-gate 				return (EACCES);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
11827c478bd9Sstevel@tonic-gate 				return (EROFS);
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
11857c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_delete", scf_error());
11867c478bd9Sstevel@tonic-gate 			}
11877c478bd9Sstevel@tonic-gate 		}
11887c478bd9Sstevel@tonic-gate 	} else {
11897c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
11907c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
11917c478bd9Sstevel@tonic-gate 		default:
11927c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
11957c478bd9Sstevel@tonic-gate 			return (ECANCELED);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
11987c478bd9Sstevel@tonic-gate 			break;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
12017c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
12027c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
12037c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
12047c478bd9Sstevel@tonic-gate 		}
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate add:
12087c478bd9Sstevel@tonic-gate 	if (scf_instance_add_pg(inst, name, type, flags, pg) == 0)
12097c478bd9Sstevel@tonic-gate 		return (0);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	switch (scf_error()) {
12127c478bd9Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
12137c478bd9Sstevel@tonic-gate 	default:
12147c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
12177c478bd9Sstevel@tonic-gate 		return (ECANCELED);
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	case SCF_ERROR_EXISTS:
12207c478bd9Sstevel@tonic-gate 		goto again;
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	case SCF_ERROR_PERMISSION_DENIED:
12237c478bd9Sstevel@tonic-gate 		return (EPERM);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_ACCESS:
12267c478bd9Sstevel@tonic-gate 		return (EACCES);
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_READONLY:
12297c478bd9Sstevel@tonic-gate 		return (EROFS);
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	case SCF_ERROR_HANDLE_MISMATCH:
12327c478bd9Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
12337c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_SET:
12347c478bd9Sstevel@tonic-gate 		bad_error("scf_instance_add_pg", scf_error());
12357c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate /*
12407c478bd9Sstevel@tonic-gate  * Returns
12417c478bd9Sstevel@tonic-gate  *   0 - success
12427c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
12437c478bd9Sstevel@tonic-gate  *		  - unknown libscf error
12447c478bd9Sstevel@tonic-gate  *   ECANCELED
12457c478bd9Sstevel@tonic-gate  */
12467c478bd9Sstevel@tonic-gate static scf_error_t
12477c478bd9Sstevel@tonic-gate transaction_add_set(scf_transaction_t *tx, scf_transaction_entry_t *ent,
12487c478bd9Sstevel@tonic-gate     const char *pname, scf_type_t ty)
12497c478bd9Sstevel@tonic-gate {
12507c478bd9Sstevel@tonic-gate 	for (;;) {
12517c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, ent, pname,
12527c478bd9Sstevel@tonic-gate 		    ty) == 0)
12537c478bd9Sstevel@tonic-gate 			return (0);
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
12567c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
12577c478bd9Sstevel@tonic-gate 		default:
12587c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
12617c478bd9Sstevel@tonic-gate 			return (ECANCELED);
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
12647c478bd9Sstevel@tonic-gate 			break;
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
12677c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
12687c478bd9Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
12697c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
12707c478bd9Sstevel@tonic-gate 			bad_error("scf_transaction_property_change_type",
12717c478bd9Sstevel@tonic-gate 			    scf_error());
12727c478bd9Sstevel@tonic-gate 		}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, ent, pname, ty) == 0)
12757c478bd9Sstevel@tonic-gate 			return (0);
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
12787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
12797c478bd9Sstevel@tonic-gate 		default:
12807c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
12837c478bd9Sstevel@tonic-gate 			return (ECANCELED);
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
12867c478bd9Sstevel@tonic-gate 			break;
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
12897c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
12907c478bd9Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
12917c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
12927c478bd9Sstevel@tonic-gate 			bad_error("scf_transaction_property_new", scf_error());
12937c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
12947c478bd9Sstevel@tonic-gate 		}
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate }
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate /*
12997c478bd9Sstevel@tonic-gate  * Returns
13007c478bd9Sstevel@tonic-gate  *   0 - success
13017c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
13027c478bd9Sstevel@tonic-gate  *		  - unknown libscf error
13037c478bd9Sstevel@tonic-gate  *   ECANCELED - pg was deleted
13047c478bd9Sstevel@tonic-gate  *   EPERM
13057c478bd9Sstevel@tonic-gate  *   EACCES
13067c478bd9Sstevel@tonic-gate  *   EROFS
13077c478bd9Sstevel@tonic-gate  */
13087c478bd9Sstevel@tonic-gate static int
13097c478bd9Sstevel@tonic-gate pg_set_prop_value(scf_propertygroup_t *pg, const char *pname, scf_value_t *v)
13107c478bd9Sstevel@tonic-gate {
13117c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
13127c478bd9Sstevel@tonic-gate 	scf_transaction_t *tx;
13137c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *e;
13147c478bd9Sstevel@tonic-gate 	scf_type_t ty;
13157c478bd9Sstevel@tonic-gate 	scf_error_t scfe;
13167c478bd9Sstevel@tonic-gate 	int ret, r;
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
13197c478bd9Sstevel@tonic-gate 	tx = safe_scf_transaction_create(h);
13207c478bd9Sstevel@tonic-gate 	e = safe_scf_entry_create(h);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	ty = scf_value_type(v);
13237c478bd9Sstevel@tonic-gate 	assert(ty != SCF_TYPE_INVALID);
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	for (;;) {
13267c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != 0) {
13277c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
13287c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
13297c478bd9Sstevel@tonic-gate 			default:
13307c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
13317c478bd9Sstevel@tonic-gate 				goto out;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
13347c478bd9Sstevel@tonic-gate 				ret = ECANCELED;
13357c478bd9Sstevel@tonic-gate 				goto out;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
13387c478bd9Sstevel@tonic-gate 				ret = EPERM;
13397c478bd9Sstevel@tonic-gate 				goto out;
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
13427c478bd9Sstevel@tonic-gate 				ret = EACCES;
13437c478bd9Sstevel@tonic-gate 				goto out;
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
13467c478bd9Sstevel@tonic-gate 				ret = EROFS;
13477c478bd9Sstevel@tonic-gate 				goto out;
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
13507c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_start", ret);
13517c478bd9Sstevel@tonic-gate 			}
13527c478bd9Sstevel@tonic-gate 		}
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 		ret = transaction_add_set(tx, e, pname, ty);
13557c478bd9Sstevel@tonic-gate 		switch (ret) {
13567c478bd9Sstevel@tonic-gate 		case 0:
13577c478bd9Sstevel@tonic-gate 			break;
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
13607c478bd9Sstevel@tonic-gate 		case ECANCELED:
13617c478bd9Sstevel@tonic-gate 			goto out;
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 		default:
13647c478bd9Sstevel@tonic-gate 			bad_error("transaction_add_set", ret);
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 		r = scf_entry_add_value(e, v);
13687c478bd9Sstevel@tonic-gate 		assert(r == 0);
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 		r = scf_transaction_commit(tx);
13717c478bd9Sstevel@tonic-gate 		if (r == 1)
13727c478bd9Sstevel@tonic-gate 			break;
13737c478bd9Sstevel@tonic-gate 		if (r != 0) {
13747c478bd9Sstevel@tonic-gate 			scfe = scf_error();
13757c478bd9Sstevel@tonic-gate 			scf_transaction_reset(tx);
13767c478bd9Sstevel@tonic-gate 			switch (scfe) {
13777c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
13787c478bd9Sstevel@tonic-gate 			default:
13797c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
13807c478bd9Sstevel@tonic-gate 				goto out;
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
13837c478bd9Sstevel@tonic-gate 				ret = ECANCELED;
13847c478bd9Sstevel@tonic-gate 				goto out;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
13877c478bd9Sstevel@tonic-gate 				ret = EPERM;
13887c478bd9Sstevel@tonic-gate 				goto out;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
13917c478bd9Sstevel@tonic-gate 				ret = EACCES;
13927c478bd9Sstevel@tonic-gate 				goto out;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
13957c478bd9Sstevel@tonic-gate 				ret = EROFS;
13967c478bd9Sstevel@tonic-gate 				goto out;
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
13997c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_commit", scfe);
14007c478bd9Sstevel@tonic-gate 			}
14017c478bd9Sstevel@tonic-gate 		}
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
14067c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
14077c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
14087c478bd9Sstevel@tonic-gate 			default:
14097c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
14107c478bd9Sstevel@tonic-gate 				goto out;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
14137c478bd9Sstevel@tonic-gate 				ret = ECANCELED;
14147c478bd9Sstevel@tonic-gate 				goto out;
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
14177c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
14187c478bd9Sstevel@tonic-gate 			}
14197c478bd9Sstevel@tonic-gate 		}
14207c478bd9Sstevel@tonic-gate 	}
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	ret = 0;
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate out:
14257c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
14267c478bd9Sstevel@tonic-gate 	scf_entry_destroy(e);
14277c478bd9Sstevel@tonic-gate 	return (ret);
14287c478bd9Sstevel@tonic-gate }
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate /*
14317c478bd9Sstevel@tonic-gate  * Returns
14327c478bd9Sstevel@tonic-gate  *   0 - success
14337c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
14347c478bd9Sstevel@tonic-gate  *		  - unknown libscf error
14357c478bd9Sstevel@tonic-gate  *   ECANCELED - inst was deleted
14367c478bd9Sstevel@tonic-gate  *   EPERM
14377c478bd9Sstevel@tonic-gate  *   EACCES
14387c478bd9Sstevel@tonic-gate  *   EROFS
14397c478bd9Sstevel@tonic-gate  */
14407c478bd9Sstevel@tonic-gate int
14417c478bd9Sstevel@tonic-gate libscf_inst_set_boolean_prop(scf_instance_t *inst, const char *pgname,
14427c478bd9Sstevel@tonic-gate     const char *pgtype, uint32_t pgflags, const char *pname, int val)
14437c478bd9Sstevel@tonic-gate {
14447c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
14457c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
14467c478bd9Sstevel@tonic-gate 	scf_value_t *v;
14477c478bd9Sstevel@tonic-gate 	int ret = 0;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
14507c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
14517c478bd9Sstevel@tonic-gate 	v = safe_scf_value_create(h);
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	ret = libscf_inst_get_or_add_pg(inst, pgname, pgtype, pgflags, pg);
14547c478bd9Sstevel@tonic-gate 	switch (ret) {
14557c478bd9Sstevel@tonic-gate 	case 0:
14567c478bd9Sstevel@tonic-gate 		break;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
14597c478bd9Sstevel@tonic-gate 	case ECANCELED:
14607c478bd9Sstevel@tonic-gate 	case EPERM:
14617c478bd9Sstevel@tonic-gate 	case EACCES:
14627c478bd9Sstevel@tonic-gate 	case EROFS:
14637c478bd9Sstevel@tonic-gate 		goto out;
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	default:
14667c478bd9Sstevel@tonic-gate 		bad_error("libscf_inst_get_or_add_pg", ret);
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	scf_value_set_boolean(v, val);
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	ret = pg_set_prop_value(pg, pname, v);
14727c478bd9Sstevel@tonic-gate 	switch (ret) {
14737c478bd9Sstevel@tonic-gate 	case 0:
14747c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
14757c478bd9Sstevel@tonic-gate 	case ECANCELED:
14767c478bd9Sstevel@tonic-gate 	case EPERM:
14777c478bd9Sstevel@tonic-gate 	case EACCES:
14787c478bd9Sstevel@tonic-gate 	case EROFS:
14797c478bd9Sstevel@tonic-gate 		break;
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	default:
14827c478bd9Sstevel@tonic-gate 		bad_error("pg_set_prop_value", ret);
14837c478bd9Sstevel@tonic-gate 	}
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate out:
14867c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
14877c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
14887c478bd9Sstevel@tonic-gate 	return (ret);
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate /*
14927c478bd9Sstevel@tonic-gate  * Returns
14937c478bd9Sstevel@tonic-gate  *   0 - success
14947c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
14957c478bd9Sstevel@tonic-gate  *		  - unknown libscf error
14967c478bd9Sstevel@tonic-gate  *   ECANCELED - inst was deleted
14977c478bd9Sstevel@tonic-gate  *   EPERM
14987c478bd9Sstevel@tonic-gate  *   EACCES
14997c478bd9Sstevel@tonic-gate  *   EROFS
15007c478bd9Sstevel@tonic-gate  */
15017c478bd9Sstevel@tonic-gate int
15027c478bd9Sstevel@tonic-gate libscf_inst_set_count_prop(scf_instance_t *inst, const char *pgname,
15037c478bd9Sstevel@tonic-gate     const char *pgtype, uint32_t pgflags, const char *pname, uint64_t count)
15047c478bd9Sstevel@tonic-gate {
15057c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
15067c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
15077c478bd9Sstevel@tonic-gate 	scf_value_t *v;
15087c478bd9Sstevel@tonic-gate 	int ret = 0;
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
15117c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
15127c478bd9Sstevel@tonic-gate 	v = safe_scf_value_create(h);
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	ret = libscf_inst_get_or_add_pg(inst, pgname, pgtype, pgflags, pg);
15157c478bd9Sstevel@tonic-gate 	switch (ret) {
15167c478bd9Sstevel@tonic-gate 	case 0:
15177c478bd9Sstevel@tonic-gate 		break;
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
15207c478bd9Sstevel@tonic-gate 	case ECANCELED:
15217c478bd9Sstevel@tonic-gate 	case EPERM:
15227c478bd9Sstevel@tonic-gate 	case EACCES:
15237c478bd9Sstevel@tonic-gate 	case EROFS:
15247c478bd9Sstevel@tonic-gate 		goto out;
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	default:
15277c478bd9Sstevel@tonic-gate 		bad_error("libscf_inst_get_or_add_pg", ret);
15287c478bd9Sstevel@tonic-gate 	}
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	scf_value_set_count(v, count);
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	ret = pg_set_prop_value(pg, pname, v);
15337c478bd9Sstevel@tonic-gate 	switch (ret) {
15347c478bd9Sstevel@tonic-gate 	case 0:
15357c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
15367c478bd9Sstevel@tonic-gate 	case ECANCELED:
15377c478bd9Sstevel@tonic-gate 	case EPERM:
15387c478bd9Sstevel@tonic-gate 	case EACCES:
15397c478bd9Sstevel@tonic-gate 	case EROFS:
15407c478bd9Sstevel@tonic-gate 		break;
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	default:
15437c478bd9Sstevel@tonic-gate 		bad_error("pg_set_prop_value", ret);
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate out:
15477c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
15487c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
15497c478bd9Sstevel@tonic-gate 	return (ret);
15507c478bd9Sstevel@tonic-gate }
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate /*
15537c478bd9Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED if the repository connection is broken,
15547c478bd9Sstevel@tonic-gate  * ECANCELED if inst is deleted, EROFS if the backend is readonly, or EPERM if
15557c478bd9Sstevel@tonic-gate  * permission was denied.
15567c478bd9Sstevel@tonic-gate  */
15577c478bd9Sstevel@tonic-gate int
15587c478bd9Sstevel@tonic-gate libscf_set_enable_ovr(scf_instance_t *inst, int enable)
15597c478bd9Sstevel@tonic-gate {
15607c478bd9Sstevel@tonic-gate 	return (libscf_inst_set_boolean_prop(inst, SCF_PG_GENERAL_OVR,
15617c478bd9Sstevel@tonic-gate 	    SCF_PG_GENERAL_OVR_TYPE, SCF_PG_GENERAL_OVR_FLAGS,
15627c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_ENABLED, enable));
15637c478bd9Sstevel@tonic-gate }
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate /*
1566*70cbfe41SPhilippe Jung  * Returns 0 on success, ECONNABORTED if the repository connection is broken,
1567*70cbfe41SPhilippe Jung  * ECANCELED if inst is deleted, EROFS if the backend is readonly, or EPERM if
1568*70cbfe41SPhilippe Jung  * permission was denied.
1569*70cbfe41SPhilippe Jung  */
1570*70cbfe41SPhilippe Jung int
1571*70cbfe41SPhilippe Jung libscf_set_deathrow(scf_instance_t *inst, int deathrow)
1572*70cbfe41SPhilippe Jung {
1573*70cbfe41SPhilippe Jung 	return (libscf_inst_set_boolean_prop(inst, SCF_PG_DEATHROW,
1574*70cbfe41SPhilippe Jung 	    SCF_PG_DEATHROW_TYPE, SCF_PG_DEATHROW_FLAGS,
1575*70cbfe41SPhilippe Jung 	    SCF_PROPERTY_DEATHROW, deathrow));
1576*70cbfe41SPhilippe Jung }
1577*70cbfe41SPhilippe Jung 
1578*70cbfe41SPhilippe Jung /*
15797c478bd9Sstevel@tonic-gate  * Returns
15807c478bd9Sstevel@tonic-gate  *   0 - success
15817c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
15827c478bd9Sstevel@tonic-gate  *   ECANCELED - inst was deleted
15837c478bd9Sstevel@tonic-gate  *   EPERM
15847c478bd9Sstevel@tonic-gate  *   EACCES
15857c478bd9Sstevel@tonic-gate  *   EROFS
15867c478bd9Sstevel@tonic-gate  */
15877c478bd9Sstevel@tonic-gate int
15887c478bd9Sstevel@tonic-gate libscf_inst_delete_prop(scf_instance_t *inst, const char *pgname,
15897c478bd9Sstevel@tonic-gate     const char *pname)
15907c478bd9Sstevel@tonic-gate {
15917c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
15927c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
15937c478bd9Sstevel@tonic-gate 	scf_transaction_t *tx;
15947c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *e;
15957c478bd9Sstevel@tonic-gate 	scf_error_t serr;
15967c478bd9Sstevel@tonic-gate 	int ret = 0, r;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
15997c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, pg) != 0) {
16027c478bd9Sstevel@tonic-gate 		scf_pg_destroy(pg);
16037c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
16047c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
16057c478bd9Sstevel@tonic-gate 		default:
16067c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
16097c478bd9Sstevel@tonic-gate 			return (ECANCELED);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
16127c478bd9Sstevel@tonic-gate 			return (0);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
16157c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
16167c478bd9Sstevel@tonic-gate 		}
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	tx = safe_scf_transaction_create(h);
16207c478bd9Sstevel@tonic-gate 	e = safe_scf_entry_create(h);
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	for (;;) {
16237c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != 0) {
16247c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
16257c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
16267c478bd9Sstevel@tonic-gate 			default:
16277c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
16287c478bd9Sstevel@tonic-gate 				goto out;
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
16317c478bd9Sstevel@tonic-gate 				ret = 0;
16327c478bd9Sstevel@tonic-gate 				goto out;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
16357c478bd9Sstevel@tonic-gate 				ret = EPERM;
16367c478bd9Sstevel@tonic-gate 				goto out;
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
16397c478bd9Sstevel@tonic-gate 				ret = EACCES;
16407c478bd9Sstevel@tonic-gate 				goto out;
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
16437c478bd9Sstevel@tonic-gate 				ret = EROFS;
16447c478bd9Sstevel@tonic-gate 				goto out;
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
16477c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
16487c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
16497c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_start", scf_error());
16507c478bd9Sstevel@tonic-gate 			}
16517c478bd9Sstevel@tonic-gate 		}
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_delete(tx, e, pname) != 0) {
16547c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
16557c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
16567c478bd9Sstevel@tonic-gate 			default:
16577c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
16587c478bd9Sstevel@tonic-gate 				goto out;
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
16617c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
16627c478bd9Sstevel@tonic-gate 				ret = 0;
16637c478bd9Sstevel@tonic-gate 				goto out;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
16667c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
16677c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
16687c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
16697c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_property_delete",
16707c478bd9Sstevel@tonic-gate 				    scf_error());
16717c478bd9Sstevel@tonic-gate 			}
16727c478bd9Sstevel@tonic-gate 		}
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 		r = scf_transaction_commit(tx);
16757c478bd9Sstevel@tonic-gate 		if (r == 1)
16767c478bd9Sstevel@tonic-gate 			break;
16777c478bd9Sstevel@tonic-gate 		if (r != 0) {
16787c478bd9Sstevel@tonic-gate 			serr = scf_error();
16797c478bd9Sstevel@tonic-gate 			scf_transaction_reset(tx);
16807c478bd9Sstevel@tonic-gate 			switch (serr) {
16817c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
16827c478bd9Sstevel@tonic-gate 			default:
16837c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
16847c478bd9Sstevel@tonic-gate 				goto out;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
16877c478bd9Sstevel@tonic-gate 				ret = 0;
16887c478bd9Sstevel@tonic-gate 				goto out;
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
16917c478bd9Sstevel@tonic-gate 				ret = EPERM;
16927c478bd9Sstevel@tonic-gate 				goto out;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
16957c478bd9Sstevel@tonic-gate 				ret = EACCES;
16967c478bd9Sstevel@tonic-gate 				goto out;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
16997c478bd9Sstevel@tonic-gate 				ret = EROFS;
17007c478bd9Sstevel@tonic-gate 				goto out;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
17037c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
17047c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
17057c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_commit", serr);
17067c478bd9Sstevel@tonic-gate 			}
17077c478bd9Sstevel@tonic-gate 		}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
17127c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
17137c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
17147c478bd9Sstevel@tonic-gate 			default:
17157c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
17167c478bd9Sstevel@tonic-gate 				goto out;
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
17197c478bd9Sstevel@tonic-gate 				ret = 0;
17207c478bd9Sstevel@tonic-gate 				goto out;
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
17237c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
17247c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
17257c478bd9Sstevel@tonic-gate 			}
17267c478bd9Sstevel@tonic-gate 		}
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate out:
17307c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
17317c478bd9Sstevel@tonic-gate 	(void) scf_entry_destroy(e);
17327c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
17337c478bd9Sstevel@tonic-gate 	return (ret);
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate /*
17377c478bd9Sstevel@tonic-gate  * Returns 0, ECONNABORTED, ECANCELED, or EPERM.
17387c478bd9Sstevel@tonic-gate  */
17397c478bd9Sstevel@tonic-gate int
17407c478bd9Sstevel@tonic-gate libscf_delete_enable_ovr(scf_instance_t *inst)
17417c478bd9Sstevel@tonic-gate {
17427c478bd9Sstevel@tonic-gate 	return (libscf_inst_delete_prop(inst, SCF_PG_GENERAL_OVR,
17437c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_ENABLED));
17447c478bd9Sstevel@tonic-gate }
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate /*
17477c478bd9Sstevel@tonic-gate  * Fails with
17487c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
17497c478bd9Sstevel@tonic-gate  *   ECANCELED - pg was deleted
17507c478bd9Sstevel@tonic-gate  *   ENOENT - pg has no milestone property
17517c478bd9Sstevel@tonic-gate  *   EINVAL - the milestone property is misconfigured
17527c478bd9Sstevel@tonic-gate  */
17537c478bd9Sstevel@tonic-gate static int
17547c478bd9Sstevel@tonic-gate pg_get_milestone(scf_propertygroup_t *pg, scf_property_t *prop,
17557c478bd9Sstevel@tonic-gate     scf_value_t *val, char *buf, size_t buf_sz)
17567c478bd9Sstevel@tonic-gate {
17577c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_MILESTONE, prop) != 0) {
17587c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
17597c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
17607c478bd9Sstevel@tonic-gate 		default:
17617c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
17647c478bd9Sstevel@tonic-gate 			return (ECANCELED);
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
17677c478bd9Sstevel@tonic-gate 			return (ENOENT);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
17707c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
17717c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
17727c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
17737c478bd9Sstevel@tonic-gate 		}
17747c478bd9Sstevel@tonic-gate 	}
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != 0) {
17777c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
17787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
17797c478bd9Sstevel@tonic-gate 		default:
17807c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
17837c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
17847c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
17857c478bd9Sstevel@tonic-gate 			return (EINVAL);
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
17883eae19d9Swesolows 		case SCF_ERROR_PERMISSION_DENIED:
17897c478bd9Sstevel@tonic-gate 			bad_error("scf_property_get_value", scf_error());
17907c478bd9Sstevel@tonic-gate 		}
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	if (scf_value_get_astring(val, buf, buf_sz) < 0) {
17947c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
17957c478bd9Sstevel@tonic-gate 		case SCF_ERROR_TYPE_MISMATCH:
17967c478bd9Sstevel@tonic-gate 			return (EINVAL);
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
17997c478bd9Sstevel@tonic-gate 		default:
18007c478bd9Sstevel@tonic-gate 			bad_error("scf_value_get_astring", scf_error());
18017c478bd9Sstevel@tonic-gate 		}
18027c478bd9Sstevel@tonic-gate 	}
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	return (0);
18057c478bd9Sstevel@tonic-gate }
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate /*
18087c478bd9Sstevel@tonic-gate  * Fails with
18097c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
18107c478bd9Sstevel@tonic-gate  *   ECANCELED - inst was deleted
18117c478bd9Sstevel@tonic-gate  *   ENOENT - inst has no milestone property
18127c478bd9Sstevel@tonic-gate  *   EINVAL - the milestone property is misconfigured
18137c478bd9Sstevel@tonic-gate  */
18147c478bd9Sstevel@tonic-gate int
18157c478bd9Sstevel@tonic-gate libscf_get_milestone(scf_instance_t *inst, scf_property_t *prop,
18167c478bd9Sstevel@tonic-gate     scf_value_t *val, char *buf, size_t buf_sz)
18177c478bd9Sstevel@tonic-gate {
18187c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
18197c478bd9Sstevel@tonic-gate 	int r;
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(scf_instance_handle(inst));
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_OPTIONS_OVR, pg) == 0) {
18247c478bd9Sstevel@tonic-gate 		switch (r = pg_get_milestone(pg, prop, val, buf, buf_sz)) {
18257c478bd9Sstevel@tonic-gate 		case 0:
18267c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
18277c478bd9Sstevel@tonic-gate 		case EINVAL:
18287c478bd9Sstevel@tonic-gate 			goto out;
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 		case ECANCELED:
18317c478bd9Sstevel@tonic-gate 		case ENOENT:
18327c478bd9Sstevel@tonic-gate 			break;
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 		default:
18357c478bd9Sstevel@tonic-gate 			bad_error("pg_get_milestone", r);
18367c478bd9Sstevel@tonic-gate 		}
18377c478bd9Sstevel@tonic-gate 	} else {
18387c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
18397c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
18407c478bd9Sstevel@tonic-gate 		default:
18417c478bd9Sstevel@tonic-gate 			r = ECONNABORTED;
18427c478bd9Sstevel@tonic-gate 			goto out;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
18457c478bd9Sstevel@tonic-gate 			r = ECANCELED;
18467c478bd9Sstevel@tonic-gate 			goto out;
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
18497c478bd9Sstevel@tonic-gate 			break;
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
18527c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
18537c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
18547c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
18557c478bd9Sstevel@tonic-gate 		}
18567c478bd9Sstevel@tonic-gate 	}
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_OPTIONS, pg) == 0) {
18597c478bd9Sstevel@tonic-gate 		r = pg_get_milestone(pg, prop, val, buf, buf_sz);
18607c478bd9Sstevel@tonic-gate 	} else {
18617c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
18627c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
18637c478bd9Sstevel@tonic-gate 		default:
18647c478bd9Sstevel@tonic-gate 			r = ECONNABORTED;
18657c478bd9Sstevel@tonic-gate 			goto out;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
18687c478bd9Sstevel@tonic-gate 			r = ECANCELED;
18697c478bd9Sstevel@tonic-gate 			goto out;
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
18727c478bd9Sstevel@tonic-gate 			r = ENOENT;
18737c478bd9Sstevel@tonic-gate 			break;
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
18767c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
18777c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
18787c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
18797c478bd9Sstevel@tonic-gate 		}
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate out:
18837c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	return (r);
18867c478bd9Sstevel@tonic-gate }
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate /*
18897c478bd9Sstevel@tonic-gate  * Get the runlevel character from the runlevel property of the given property
18907c478bd9Sstevel@tonic-gate  * group.  Fails with
18917c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
18927c478bd9Sstevel@tonic-gate  *   ECANCELED - prop's property group was deleted
18937c478bd9Sstevel@tonic-gate  *   ENOENT - the property has no values
18947c478bd9Sstevel@tonic-gate  *   EINVAL - the property has more than one value
18957c478bd9Sstevel@tonic-gate  *	      the property is of the wrong type
18967c478bd9Sstevel@tonic-gate  *	      the property value is malformed
18977c478bd9Sstevel@tonic-gate  */
18987c478bd9Sstevel@tonic-gate int
18997c478bd9Sstevel@tonic-gate libscf_extract_runlevel(scf_property_t *prop, char *rlp)
19007c478bd9Sstevel@tonic-gate {
19017c478bd9Sstevel@tonic-gate 	scf_value_t *val;
19027c478bd9Sstevel@tonic-gate 	char buf[2];
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(scf_property_handle(prop));
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != 0) {
19077c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
19087c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
19097c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
19127c478bd9Sstevel@tonic-gate 			return (ENOENT);
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
19157c478bd9Sstevel@tonic-gate 			return (ECANCELED);
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
19187c478bd9Sstevel@tonic-gate 			return (EINVAL);
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
19217c478bd9Sstevel@tonic-gate 			return (ENOENT);
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
19247c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
19253eae19d9Swesolows 		case SCF_ERROR_PERMISSION_DENIED:
19267c478bd9Sstevel@tonic-gate 		default:
19277c478bd9Sstevel@tonic-gate 			bad_error("scf_property_get_value", scf_error());
19287c478bd9Sstevel@tonic-gate 		}
19297c478bd9Sstevel@tonic-gate 	}
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	if (scf_value_get_astring(val, buf, sizeof (buf)) < 0) {
19327c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
19337c478bd9Sstevel@tonic-gate 			bad_error("scf_value_get_astring", scf_error());
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 		return (EINVAL);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	if (buf[0] == '\0' || buf[1] != '\0')
19397c478bd9Sstevel@tonic-gate 		return (EINVAL);
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	*rlp = buf[0];
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	return (0);
19447c478bd9Sstevel@tonic-gate }
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate /*
19477c478bd9Sstevel@tonic-gate  * Delete the "runlevel" property from the given property group.  Also set the
19487c478bd9Sstevel@tonic-gate  * "milestone" property to the given string.  Fails with ECONNABORTED,
19497c478bd9Sstevel@tonic-gate  * ECANCELED, EPERM, EACCES, or EROFS.
19507c478bd9Sstevel@tonic-gate  */
19517c478bd9Sstevel@tonic-gate int
19527c478bd9Sstevel@tonic-gate libscf_clear_runlevel(scf_propertygroup_t *pg, const char *milestone)
19537c478bd9Sstevel@tonic-gate {
19547c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
19557c478bd9Sstevel@tonic-gate 	scf_transaction_t *tx;
19567c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *e_rl, *e_ms;
19577c478bd9Sstevel@tonic-gate 	scf_value_t *val;
19587c478bd9Sstevel@tonic-gate 	scf_error_t serr;
19597c478bd9Sstevel@tonic-gate 	boolean_t isempty = B_TRUE;
19607c478bd9Sstevel@tonic-gate 	int ret = 0, r;
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
19637c478bd9Sstevel@tonic-gate 	tx = safe_scf_transaction_create(h);
19647c478bd9Sstevel@tonic-gate 	e_rl = safe_scf_entry_create(h);
19657c478bd9Sstevel@tonic-gate 	e_ms = safe_scf_entry_create(h);
19667c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	if (milestone) {
19697c478bd9Sstevel@tonic-gate 		r = scf_value_set_astring(val, milestone);
19707c478bd9Sstevel@tonic-gate 		assert(r == 0);
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	for (;;) {
19747c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != 0) {
19757c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
19767c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
19777c478bd9Sstevel@tonic-gate 			default:
19787c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
19797c478bd9Sstevel@tonic-gate 				goto out;
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
19827c478bd9Sstevel@tonic-gate 				ret = ECANCELED;
19837c478bd9Sstevel@tonic-gate 				goto out;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
19867c478bd9Sstevel@tonic-gate 				ret = EPERM;
19877c478bd9Sstevel@tonic-gate 				goto out;
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
19907c478bd9Sstevel@tonic-gate 				ret = EACCES;
19917c478bd9Sstevel@tonic-gate 				goto out;
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
19947c478bd9Sstevel@tonic-gate 				ret = EROFS;
19957c478bd9Sstevel@tonic-gate 				goto out;
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
19987c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_start", scf_error());
19997c478bd9Sstevel@tonic-gate 			}
20007c478bd9Sstevel@tonic-gate 		}
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_delete(tx, e_rl,
20037c478bd9Sstevel@tonic-gate 		    "runlevel") == 0) {
20047c478bd9Sstevel@tonic-gate 			isempty = B_FALSE;
20057c478bd9Sstevel@tonic-gate 		} else {
20067c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
20077c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
20087c478bd9Sstevel@tonic-gate 			default:
20097c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
20107c478bd9Sstevel@tonic-gate 				goto out;
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
20137c478bd9Sstevel@tonic-gate 				ret = ECANCELED;
20147c478bd9Sstevel@tonic-gate 				goto out;
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
20177c478bd9Sstevel@tonic-gate 				break;
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
20207c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
20217c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
20227c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_property_delete",
20237c478bd9Sstevel@tonic-gate 				    scf_error());
20247c478bd9Sstevel@tonic-gate 			}
20257c478bd9Sstevel@tonic-gate 		}
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 		if (milestone) {
20287c478bd9Sstevel@tonic-gate 			ret = transaction_add_set(tx, e_ms,
20297c478bd9Sstevel@tonic-gate 			    SCF_PROPERTY_MILESTONE, SCF_TYPE_ASTRING);
20307c478bd9Sstevel@tonic-gate 			switch (ret) {
20317c478bd9Sstevel@tonic-gate 			case 0:
20327c478bd9Sstevel@tonic-gate 				break;
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
20357c478bd9Sstevel@tonic-gate 			case ECANCELED:
20367c478bd9Sstevel@tonic-gate 				goto out;
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 			default:
20397c478bd9Sstevel@tonic-gate 				bad_error("transaction_add_set", ret);
20407c478bd9Sstevel@tonic-gate 			}
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 			isempty = B_FALSE;
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 			r = scf_entry_add_value(e_ms, val);
20457c478bd9Sstevel@tonic-gate 			assert(r == 0);
20467c478bd9Sstevel@tonic-gate 		}
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 		if (isempty)
20497c478bd9Sstevel@tonic-gate 			goto out;
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 		r = scf_transaction_commit(tx);
20527c478bd9Sstevel@tonic-gate 		if (r == 1)
20537c478bd9Sstevel@tonic-gate 			break;
20547c478bd9Sstevel@tonic-gate 		if (r != 0) {
20557c478bd9Sstevel@tonic-gate 			serr = scf_error();
20567c478bd9Sstevel@tonic-gate 			scf_transaction_reset(tx);
20577c478bd9Sstevel@tonic-gate 			switch (serr) {
20587c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
20597c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
20607c478bd9Sstevel@tonic-gate 				goto out;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
20637c478bd9Sstevel@tonic-gate 				ret = EPERM;
20647c478bd9Sstevel@tonic-gate 				goto out;
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
20677c478bd9Sstevel@tonic-gate 				ret = EACCES;
20687c478bd9Sstevel@tonic-gate 				goto out;
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
20717c478bd9Sstevel@tonic-gate 				ret = EROFS;
20727c478bd9Sstevel@tonic-gate 				goto out;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 			default:
20757c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_commit", serr);
20767c478bd9Sstevel@tonic-gate 			}
20777c478bd9Sstevel@tonic-gate 		}
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
20827c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
20837c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
20847c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
20857c478bd9Sstevel@tonic-gate 				goto out;
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
20887c478bd9Sstevel@tonic-gate 				ret = ECANCELED;
20897c478bd9Sstevel@tonic-gate 				goto out;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 			default:
20927c478bd9Sstevel@tonic-gate 				assert(0);
20937c478bd9Sstevel@tonic-gate 				abort();
20947c478bd9Sstevel@tonic-gate 			}
20957c478bd9Sstevel@tonic-gate 		}
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate out:
20997c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
21007c478bd9Sstevel@tonic-gate 	scf_entry_destroy(e_rl);
21017c478bd9Sstevel@tonic-gate 	scf_entry_destroy(e_ms);
21027c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
21037c478bd9Sstevel@tonic-gate 	return (ret);
21047c478bd9Sstevel@tonic-gate }
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate /*
21077c478bd9Sstevel@tonic-gate  * int libscf_get_template_values(scf_instance_t *, scf_snapshot_t *,
21087c478bd9Sstevel@tonic-gate  *	char **)
21097c478bd9Sstevel@tonic-gate  *
21107c478bd9Sstevel@tonic-gate  *   Return template values for inst in *common_name suitable for use in
21117c478bd9Sstevel@tonic-gate  *   restarter_inst_t->ri_common_name.  Called by restarter_insert_inst().
21127c478bd9Sstevel@tonic-gate  *
21137c478bd9Sstevel@tonic-gate  *   Returns 0 on success, ECANCELED if the instance is deleted, ECHILD if
21147c478bd9Sstevel@tonic-gate  *   a value fetch failed for a property, ENOENT if the instance has no
21157c478bd9Sstevel@tonic-gate  *   tm_common_name property group or the property group is deleted, and
21167c478bd9Sstevel@tonic-gate  *   ECONNABORTED if the repository connection is broken.
21177c478bd9Sstevel@tonic-gate  */
21187c478bd9Sstevel@tonic-gate int
21197c478bd9Sstevel@tonic-gate libscf_get_template_values(scf_instance_t *inst, scf_snapshot_t *snap,
21207c478bd9Sstevel@tonic-gate     char **common_name, char **c_common_name)
21217c478bd9Sstevel@tonic-gate {
21227c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
21237c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
21247c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
21257c478bd9Sstevel@tonic-gate 	int ret = 0, r;
21267c478bd9Sstevel@tonic-gate 	char *cname = startd_alloc(max_scf_value_size);
21277c478bd9Sstevel@tonic-gate 	char *c_cname = startd_alloc(max_scf_value_size);
212892175b8eSrm88369 	int common_name_initialized = B_FALSE;
212992175b8eSrm88369 	int c_common_name_initialized = B_FALSE;
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
21327c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
21337c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	/*
21367c478bd9Sstevel@tonic-gate 	 * The tm_common_name property group, as with all template property
21377c478bd9Sstevel@tonic-gate 	 * groups, is optional.
21387c478bd9Sstevel@tonic-gate 	 */
21397c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, SCF_PG_TM_COMMON_NAME, pg)
21407c478bd9Sstevel@tonic-gate 	    == -1) {
21417c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
21427c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
21437c478bd9Sstevel@tonic-gate 			ret = ECANCELED;
21447c478bd9Sstevel@tonic-gate 			goto template_values_out;
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
21477c478bd9Sstevel@tonic-gate 			goto template_values_out;
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
21507c478bd9Sstevel@tonic-gate 		default:
21517c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
21527c478bd9Sstevel@tonic-gate 			goto template_values_out;
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
21557c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
21567c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
21577c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg_composed", scf_error());
21587c478bd9Sstevel@tonic-gate 		}
21597c478bd9Sstevel@tonic-gate 	}
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	/*
21627c478bd9Sstevel@tonic-gate 	 * The name we wish uses the current locale name as the property name.
21637c478bd9Sstevel@tonic-gate 	 */
21647c478bd9Sstevel@tonic-gate 	if (st->st_locale != NULL) {
21657c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, st->st_locale, prop) == -1) {
21667c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
21677c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
21687c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
21697c478bd9Sstevel@tonic-gate 				break;
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
21727c478bd9Sstevel@tonic-gate 			default:
21737c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
21747c478bd9Sstevel@tonic-gate 				goto template_values_out;
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
21777c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
21787c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
21797c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
21807c478bd9Sstevel@tonic-gate 			}
21817c478bd9Sstevel@tonic-gate 		} else {
21827c478bd9Sstevel@tonic-gate 			if ((r = libscf_read_single_astring(h, prop, &cname)) !=
21837c478bd9Sstevel@tonic-gate 			    0) {
21847c478bd9Sstevel@tonic-gate 				if (r != LIBSCF_PROPERTY_ABSENT)
21857c478bd9Sstevel@tonic-gate 					ret = ECHILD;
21867c478bd9Sstevel@tonic-gate 				goto template_values_out;
21877c478bd9Sstevel@tonic-gate 			}
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 			*common_name = cname;
219092175b8eSrm88369 			common_name_initialized = B_TRUE;
21917c478bd9Sstevel@tonic-gate 		}
21927c478bd9Sstevel@tonic-gate 	}
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	/*
21957c478bd9Sstevel@tonic-gate 	 * Also pull out the C locale name, as a fallback for the case where
21967c478bd9Sstevel@tonic-gate 	 * service offers no localized name.
21977c478bd9Sstevel@tonic-gate 	 */
21987c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, "C", prop) == -1) {
21997c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
22007c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
22017c478bd9Sstevel@tonic-gate 			ret = ENOENT;
22027c478bd9Sstevel@tonic-gate 			goto template_values_out;
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
22057c478bd9Sstevel@tonic-gate 			break;
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
22087c478bd9Sstevel@tonic-gate 		default:
22097c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
22107c478bd9Sstevel@tonic-gate 			goto template_values_out;
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
22137c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
22147c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
22157c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
22167c478bd9Sstevel@tonic-gate 		}
22177c478bd9Sstevel@tonic-gate 	} else {
22187c478bd9Sstevel@tonic-gate 		if ((r = libscf_read_single_astring(h, prop, &c_cname)) != 0) {
22197c478bd9Sstevel@tonic-gate 			if (r != LIBSCF_PROPERTY_ABSENT)
22207c478bd9Sstevel@tonic-gate 				ret = ECHILD;
22217c478bd9Sstevel@tonic-gate 			goto template_values_out;
22227c478bd9Sstevel@tonic-gate 		}
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 		*c_common_name = c_cname;
222592175b8eSrm88369 		c_common_name_initialized = B_TRUE;
22267c478bd9Sstevel@tonic-gate 	}
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate template_values_out:
223092175b8eSrm88369 	if (common_name_initialized == B_FALSE)
223192175b8eSrm88369 		startd_free(cname, max_scf_value_size);
223292175b8eSrm88369 	if (c_common_name_initialized == B_FALSE)
223392175b8eSrm88369 		startd_free(c_cname, max_scf_value_size);
22347c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
22357c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 	return (ret);
22387c478bd9Sstevel@tonic-gate }
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate /*
22417c478bd9Sstevel@tonic-gate  * int libscf_get_startd_properties(scf_handle_t *, scf_instance_t *,
22427c478bd9Sstevel@tonic-gate  *	scf_snapshot_t *, uint_t *, char **)
22437c478bd9Sstevel@tonic-gate  *
22447c478bd9Sstevel@tonic-gate  *   Return startd settings for inst in *flags suitable for use in
22457c478bd9Sstevel@tonic-gate  *   restarter_inst_t->ri_flags.  Called by restarter_insert_inst().
22467c478bd9Sstevel@tonic-gate  *
22477c478bd9Sstevel@tonic-gate  *   Returns 0 on success, ECANCELED if the instance is deleted, ECHILD if
22487c478bd9Sstevel@tonic-gate  *   a value fetch failed for a property, ENOENT if the instance has no
22497c478bd9Sstevel@tonic-gate  *   general property group or the property group is deleted, and
22507c478bd9Sstevel@tonic-gate  *   ECONNABORTED if the repository connection is broken.
22517c478bd9Sstevel@tonic-gate  */
22527c478bd9Sstevel@tonic-gate int
22537c478bd9Sstevel@tonic-gate libscf_get_startd_properties(scf_instance_t *inst,
22547c478bd9Sstevel@tonic-gate     scf_snapshot_t *snap, uint_t *flags, char **prefixp)
22557c478bd9Sstevel@tonic-gate {
22567c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
22577c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
22587c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
22597c478bd9Sstevel@tonic-gate 	int style = RINST_CONTRACT;
22607c478bd9Sstevel@tonic-gate 	char *style_str = startd_alloc(max_scf_value_size);
22617c478bd9Sstevel@tonic-gate 	int ret = 0, r;
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
22647c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
22657c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	/*
22687c478bd9Sstevel@tonic-gate 	 * The startd property group is optional.
22697c478bd9Sstevel@tonic-gate 	 */
22707c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, SCF_PG_STARTD, pg) == -1) {
22717c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
22727c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
22737c478bd9Sstevel@tonic-gate 			ret = ECANCELED;
22747c478bd9Sstevel@tonic-gate 			goto instance_flags_out;
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
22777c478bd9Sstevel@tonic-gate 			ret = ENOENT;
22787c478bd9Sstevel@tonic-gate 			goto instance_flags_out;
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
22817c478bd9Sstevel@tonic-gate 		default:
22827c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
22837c478bd9Sstevel@tonic-gate 			goto instance_flags_out;
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
22867c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
22877c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
22887c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg_composed", scf_error());
22897c478bd9Sstevel@tonic-gate 		}
22907c478bd9Sstevel@tonic-gate 	}
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	/*
22937c478bd9Sstevel@tonic-gate 	 * 1.  Duration property.
22947c478bd9Sstevel@tonic-gate 	 */
22957c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_DURATION, prop) == -1) {
22967c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
22977c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
22987c478bd9Sstevel@tonic-gate 			ret = ENOENT;
22997c478bd9Sstevel@tonic-gate 			goto instance_flags_out;
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
23027c478bd9Sstevel@tonic-gate 			break;
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
23057c478bd9Sstevel@tonic-gate 		default:
23067c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
23077c478bd9Sstevel@tonic-gate 			goto instance_flags_out;
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
23107c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
23117c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
23127c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
23137c478bd9Sstevel@tonic-gate 		}
23147c478bd9Sstevel@tonic-gate 	} else {
23157c478bd9Sstevel@tonic-gate 		errno = 0;
23167c478bd9Sstevel@tonic-gate 		if ((r = libscf_read_single_astring(h, prop, &style_str))
23177c478bd9Sstevel@tonic-gate 		    != 0) {
23187c478bd9Sstevel@tonic-gate 			if (r != LIBSCF_PROPERTY_ABSENT)
23197c478bd9Sstevel@tonic-gate 				ret = ECHILD;
23207c478bd9Sstevel@tonic-gate 			goto instance_flags_out;
23217c478bd9Sstevel@tonic-gate 		}
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 		if (strcmp(style_str, "child") == 0)
23247c478bd9Sstevel@tonic-gate 			style = RINST_WAIT;
23257c478bd9Sstevel@tonic-gate 		else if (strcmp(style_str, "transient") == 0)
23267c478bd9Sstevel@tonic-gate 			style = RINST_TRANSIENT;
23277c478bd9Sstevel@tonic-gate 	}
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 	/*
23307c478bd9Sstevel@tonic-gate 	 * 2.  utmpx prefix property.
23317c478bd9Sstevel@tonic-gate 	 */
23327c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_UTMPX_PREFIX, prop) == 0) {
23337c478bd9Sstevel@tonic-gate 		errno = 0;
23347c478bd9Sstevel@tonic-gate 		if ((r = libscf_read_single_astring(h, prop, prefixp)) != 0) {
23357c478bd9Sstevel@tonic-gate 			if (r != LIBSCF_PROPERTY_ABSENT)
23367c478bd9Sstevel@tonic-gate 				ret = ECHILD;
23377c478bd9Sstevel@tonic-gate 			goto instance_flags_out;
23387c478bd9Sstevel@tonic-gate 		}
23397c478bd9Sstevel@tonic-gate 	} else {
23407c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
23417c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
23427c478bd9Sstevel@tonic-gate 			ret = ENOENT;
23437c478bd9Sstevel@tonic-gate 			goto instance_flags_out;
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
23467c478bd9Sstevel@tonic-gate 			goto instance_flags_out;
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
23497c478bd9Sstevel@tonic-gate 		default:
23507c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
23517c478bd9Sstevel@tonic-gate 			goto instance_flags_out;
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
23547c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
23557c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
23567c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_property", scf_error());
23577c478bd9Sstevel@tonic-gate 		}
23587c478bd9Sstevel@tonic-gate 	}
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate instance_flags_out:
23617c478bd9Sstevel@tonic-gate 	startd_free(style_str, max_scf_value_size);
23627c478bd9Sstevel@tonic-gate 	*flags = (*flags & ~RINST_STYLE_MASK) | style;
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
23657c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	return (ret);
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate /*
23717c478bd9Sstevel@tonic-gate  * int libscf_read_method_ids(scf_handle_t *, scf_instance_t *, ctid_t *,
23727c478bd9Sstevel@tonic-gate  *   ctid_t *, pid_t *)
23737c478bd9Sstevel@tonic-gate  *
23747c478bd9Sstevel@tonic-gate  *  Sets given id_t variables to primary and transient contract IDs and start
23757c478bd9Sstevel@tonic-gate  *  PID.  Returns 0, ECONNABORTED, and ECANCELED.
23767c478bd9Sstevel@tonic-gate  */
23777c478bd9Sstevel@tonic-gate int
23787c478bd9Sstevel@tonic-gate libscf_read_method_ids(scf_handle_t *h, scf_instance_t *inst, const char *fmri,
23797c478bd9Sstevel@tonic-gate     ctid_t *primary, ctid_t *transient, pid_t *start_pid)
23807c478bd9Sstevel@tonic-gate {
23817c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
23827c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
23837c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
23847c478bd9Sstevel@tonic-gate 	uint64_t p, t;
23857c478bd9Sstevel@tonic-gate 	int ret = 0;
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	*primary = 0;
23887c478bd9Sstevel@tonic-gate 	*transient = 0;
23897c478bd9Sstevel@tonic-gate 	*start_pid = -1;
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
23927c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
23937c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) == -1) {
23967c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
23977c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
23987c478bd9Sstevel@tonic-gate 		default:
23997c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
24007c478bd9Sstevel@tonic-gate 			goto read_id_err;
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
24037c478bd9Sstevel@tonic-gate 			ret = ECANCELED;
24047c478bd9Sstevel@tonic-gate 			goto read_id_err;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
24077c478bd9Sstevel@tonic-gate 			goto read_id_err;
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
24107c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
24117c478bd9Sstevel@tonic-gate 		}
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	ret = get_count(pg, SCF_PROPERTY_CONTRACT, &p);
24157c478bd9Sstevel@tonic-gate 	switch (ret) {
24167c478bd9Sstevel@tonic-gate 	case 0:
24177c478bd9Sstevel@tonic-gate 		break;
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	case EINVAL:
24207c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE,
24217c478bd9Sstevel@tonic-gate 		    "%s: Ignoring %s/%s: multivalued or not of type count\n",
24227c478bd9Sstevel@tonic-gate 		    fmri, SCF_PG_RESTARTER, SCF_PROPERTY_CONTRACT);
24237c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
24247c478bd9Sstevel@tonic-gate 	case ENOENT:
24257c478bd9Sstevel@tonic-gate 		ret = 0;
24267c478bd9Sstevel@tonic-gate 		goto read_trans;
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
24297c478bd9Sstevel@tonic-gate 	case ECANCELED:
24307c478bd9Sstevel@tonic-gate 		goto read_id_err;
24317c478bd9Sstevel@tonic-gate 
24323eae19d9Swesolows 	case EACCES:
24337c478bd9Sstevel@tonic-gate 	default:
24347c478bd9Sstevel@tonic-gate 		bad_error("get_count", ret);
24357c478bd9Sstevel@tonic-gate 	}
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	*primary = p;
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate read_trans:
24407c478bd9Sstevel@tonic-gate 	ret = get_count(pg, SCF_PROPERTY_TRANSIENT_CONTRACT, &t);
24417c478bd9Sstevel@tonic-gate 	switch (ret) {
24427c478bd9Sstevel@tonic-gate 	case 0:
24437c478bd9Sstevel@tonic-gate 		break;
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 	case EINVAL:
24467c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE,
24477c478bd9Sstevel@tonic-gate 		    "%s: Ignoring %s/%s: multivalued or not of type count\n",
24487c478bd9Sstevel@tonic-gate 		    fmri, SCF_PG_RESTARTER, SCF_PROPERTY_TRANSIENT_CONTRACT);
24497c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 	case ENOENT:
24527c478bd9Sstevel@tonic-gate 		ret = 0;
24537c478bd9Sstevel@tonic-gate 		goto read_pid_only;
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
24567c478bd9Sstevel@tonic-gate 	case ECANCELED:
24577c478bd9Sstevel@tonic-gate 		goto read_id_err;
24587c478bd9Sstevel@tonic-gate 
24593eae19d9Swesolows 	case EACCES:
24607c478bd9Sstevel@tonic-gate 	default:
24617c478bd9Sstevel@tonic-gate 		bad_error("get_count", ret);
24627c478bd9Sstevel@tonic-gate 	}
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	*transient = t;
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate read_pid_only:
24677c478bd9Sstevel@tonic-gate 	ret = get_count(pg, SCF_PROPERTY_START_PID, &p);
24687c478bd9Sstevel@tonic-gate 	switch (ret) {
24697c478bd9Sstevel@tonic-gate 	case 0:
24707c478bd9Sstevel@tonic-gate 		break;
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 	case EINVAL:
24737c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE,
24747c478bd9Sstevel@tonic-gate 		    "%s: Ignoring %s/%s: multivalued or not of type count\n",
24757c478bd9Sstevel@tonic-gate 		    fmri, SCF_PG_RESTARTER, SCF_PROPERTY_START_PID);
24767c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
24777c478bd9Sstevel@tonic-gate 	case ENOENT:
24787c478bd9Sstevel@tonic-gate 		ret = 0;
24797c478bd9Sstevel@tonic-gate 		goto read_id_err;
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
24827c478bd9Sstevel@tonic-gate 	case ECANCELED:
24837c478bd9Sstevel@tonic-gate 		goto read_id_err;
24847c478bd9Sstevel@tonic-gate 
24853eae19d9Swesolows 	case EACCES:
24867c478bd9Sstevel@tonic-gate 	default:
24877c478bd9Sstevel@tonic-gate 		bad_error("get_count", ret);
24887c478bd9Sstevel@tonic-gate 	}
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	*start_pid = p;
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate read_id_err:
24937c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
24947c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
24957c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
24967c478bd9Sstevel@tonic-gate 	return (ret);
24977c478bd9Sstevel@tonic-gate }
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate /*
25007c478bd9Sstevel@tonic-gate  * Returns with
25017c478bd9Sstevel@tonic-gate  *   0 - success
25027c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
25037c478bd9Sstevel@tonic-gate  *		  - unknown libscf error
25047c478bd9Sstevel@tonic-gate  *   ECANCELED - s_inst was deleted
25057c478bd9Sstevel@tonic-gate  *   EPERM
25067c478bd9Sstevel@tonic-gate  *   EACCES
25077c478bd9Sstevel@tonic-gate  *   EROFS
25087c478bd9Sstevel@tonic-gate  */
25097c478bd9Sstevel@tonic-gate int
25107c478bd9Sstevel@tonic-gate libscf_write_start_pid(scf_instance_t *s_inst, pid_t pid)
25117c478bd9Sstevel@tonic-gate {
25127c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
25137c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *t_pid;
25147c478bd9Sstevel@tonic-gate 	scf_value_t *v_pid;
25157c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
25167c478bd9Sstevel@tonic-gate 	int ret = 0;
25177c478bd9Sstevel@tonic-gate 
25187c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(s_inst);
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
25217c478bd9Sstevel@tonic-gate 	t_pid = safe_scf_entry_create(h);
25227c478bd9Sstevel@tonic-gate 	v_pid = safe_scf_value_create(h);
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate get_pg:
25257c478bd9Sstevel@tonic-gate 	ret = libscf_inst_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
25267c478bd9Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
25277c478bd9Sstevel@tonic-gate 	switch (ret) {
25287c478bd9Sstevel@tonic-gate 	case 0:
25297c478bd9Sstevel@tonic-gate 		break;
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
25327c478bd9Sstevel@tonic-gate 	case ECANCELED:
25337c478bd9Sstevel@tonic-gate 	case EPERM:
25347c478bd9Sstevel@tonic-gate 	case EACCES:
25357c478bd9Sstevel@tonic-gate 	case EROFS:
25367c478bd9Sstevel@tonic-gate 		goto write_start_err;
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 	default:
25397c478bd9Sstevel@tonic-gate 		bad_error("libscf_inst_get_or_add_pg", ret);
25407c478bd9Sstevel@tonic-gate 	}
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	scf_value_set_count(v_pid, pid);
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	ret = pg_set_prop_value(pg, SCF_PROPERTY_START_PID, v_pid);
25457c478bd9Sstevel@tonic-gate 	switch (ret) {
25467c478bd9Sstevel@tonic-gate 	case 0:
25477c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
25487c478bd9Sstevel@tonic-gate 	case EPERM:
25497c478bd9Sstevel@tonic-gate 	case EACCES:
25507c478bd9Sstevel@tonic-gate 	case EROFS:
25517c478bd9Sstevel@tonic-gate 		break;
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	case ECANCELED:
25547c478bd9Sstevel@tonic-gate 		goto get_pg;
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 	default:
25577c478bd9Sstevel@tonic-gate 		bad_error("pg_set_prop_value", ret);
25587c478bd9Sstevel@tonic-gate 	}
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate write_start_err:
25617c478bd9Sstevel@tonic-gate 	scf_entry_destroy(t_pid);
25627c478bd9Sstevel@tonic-gate 	scf_value_destroy(v_pid);
25637c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	return (ret);
25667c478bd9Sstevel@tonic-gate }
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate /*
25697c478bd9Sstevel@tonic-gate  * Add a property indicating the instance log file.  If the dir is
25707c478bd9Sstevel@tonic-gate  * equal to LOG_PREFIX_EARLY, then the property restarter/alt_logfile
25717c478bd9Sstevel@tonic-gate  * of the instance is used; otherwise, restarter/logfile is used.
25727c478bd9Sstevel@tonic-gate  *
25737c478bd9Sstevel@tonic-gate  * Returns
25747c478bd9Sstevel@tonic-gate  *   0 - success
25757c478bd9Sstevel@tonic-gate  *   ECONNABORTED
25767c478bd9Sstevel@tonic-gate  *   ECANCELED
25777c478bd9Sstevel@tonic-gate  *   EPERM
25787c478bd9Sstevel@tonic-gate  *   EACCES
25797c478bd9Sstevel@tonic-gate  *   EROFS
25807c478bd9Sstevel@tonic-gate  *   EAGAIN
25817c478bd9Sstevel@tonic-gate  */
25827c478bd9Sstevel@tonic-gate int
25837c478bd9Sstevel@tonic-gate libscf_note_method_log(scf_instance_t *inst, const char *dir, const char *file)
25847c478bd9Sstevel@tonic-gate {
25857c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
25867c478bd9Sstevel@tonic-gate 	scf_value_t *v;
25877c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
25887c478bd9Sstevel@tonic-gate 	int ret = 0;
25897c478bd9Sstevel@tonic-gate 	char *logname;
25907c478bd9Sstevel@tonic-gate 	const char *propname;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
25937c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
25947c478bd9Sstevel@tonic-gate 	v = safe_scf_value_create(h);
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 	logname = uu_msprintf("%s%s", dir, file);
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	if (logname == NULL) {
25997c478bd9Sstevel@tonic-gate 		ret = errno;
26007c478bd9Sstevel@tonic-gate 		goto out;
26017c478bd9Sstevel@tonic-gate 	}
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	ret = libscf_inst_get_or_add_pg(inst, SCF_PG_RESTARTER,
26047c478bd9Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
26057c478bd9Sstevel@tonic-gate 	switch (ret) {
26067c478bd9Sstevel@tonic-gate 	case 0:
26077c478bd9Sstevel@tonic-gate 		break;
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
26107c478bd9Sstevel@tonic-gate 	case ECANCELED:
26117c478bd9Sstevel@tonic-gate 	case EPERM:
26127c478bd9Sstevel@tonic-gate 	case EACCES:
26137c478bd9Sstevel@tonic-gate 	case EROFS:
26147c478bd9Sstevel@tonic-gate 		goto out;
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 	default:
26177c478bd9Sstevel@tonic-gate 		bad_error("libscf_inst_get_or_add_pg", ret);
26187c478bd9Sstevel@tonic-gate 	}
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	(void) scf_value_set_astring(v, logname);
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	if (strcmp(LOG_PREFIX_EARLY, dir) == 0)
26237c478bd9Sstevel@tonic-gate 		propname = SCF_PROPERTY_ALT_LOGFILE;
26247c478bd9Sstevel@tonic-gate 	else
26257c478bd9Sstevel@tonic-gate 		propname = SCF_PROPERTY_LOGFILE;
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	ret = pg_set_prop_value(pg, propname, v);
26287c478bd9Sstevel@tonic-gate 	switch (ret) {
26297c478bd9Sstevel@tonic-gate 	case 0:
26307c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
26317c478bd9Sstevel@tonic-gate 	case ECANCELED:
26327c478bd9Sstevel@tonic-gate 	case EPERM:
26337c478bd9Sstevel@tonic-gate 	case EACCES:
26347c478bd9Sstevel@tonic-gate 	case EROFS:
26357c478bd9Sstevel@tonic-gate 		break;
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate 	default:
26387c478bd9Sstevel@tonic-gate 		bad_error("pg_set_prop_value", ret);
26397c478bd9Sstevel@tonic-gate 	}
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate out:
26427c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
26437c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
26447c478bd9Sstevel@tonic-gate 	uu_free(logname);
26457c478bd9Sstevel@tonic-gate 	return (ret);
26467c478bd9Sstevel@tonic-gate }
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate /*
26497c478bd9Sstevel@tonic-gate  * Returns
26507c478bd9Sstevel@tonic-gate  *   0 - success
26517c478bd9Sstevel@tonic-gate  *   ENAMETOOLONG - name is too long
26527c478bd9Sstevel@tonic-gate  *   ECONNABORTED
26537c478bd9Sstevel@tonic-gate  *   ECANCELED
26547c478bd9Sstevel@tonic-gate  *   EPERM
26557c478bd9Sstevel@tonic-gate  *   EACCES
26567c478bd9Sstevel@tonic-gate  *   EROFS
26577c478bd9Sstevel@tonic-gate  */
26587c478bd9Sstevel@tonic-gate int
26597c478bd9Sstevel@tonic-gate libscf_write_method_status(scf_instance_t *s_inst, const char *name,
26607c478bd9Sstevel@tonic-gate     int status)
26617c478bd9Sstevel@tonic-gate {
26627c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
26637c478bd9Sstevel@tonic-gate 	scf_transaction_t *tx;
26647c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *e_time, *e_stat;
26657c478bd9Sstevel@tonic-gate 	scf_value_t *v_time, *v_stat;
26667c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
26677c478bd9Sstevel@tonic-gate 	int ret = 0, r;
26687c478bd9Sstevel@tonic-gate 	char pname[30];
26697c478bd9Sstevel@tonic-gate 	struct timeval tv;
26707c478bd9Sstevel@tonic-gate 	scf_error_t scfe;
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	if (strlen(name) + sizeof ("_method_waitstatus") > sizeof (pname))
26737c478bd9Sstevel@tonic-gate 		return (ENAMETOOLONG);
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(s_inst);
26767c478bd9Sstevel@tonic-gate 
26777c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
26787c478bd9Sstevel@tonic-gate 	tx = safe_scf_transaction_create(h);
26797c478bd9Sstevel@tonic-gate 	e_time = safe_scf_entry_create(h);
26807c478bd9Sstevel@tonic-gate 	v_time = safe_scf_value_create(h);
26817c478bd9Sstevel@tonic-gate 	e_stat = safe_scf_entry_create(h);
26827c478bd9Sstevel@tonic-gate 	v_stat = safe_scf_value_create(h);
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate get_pg:
26857c478bd9Sstevel@tonic-gate 	ret = libscf_inst_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
26867c478bd9Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
26877c478bd9Sstevel@tonic-gate 	switch (ret) {
26887c478bd9Sstevel@tonic-gate 	case 0:
26897c478bd9Sstevel@tonic-gate 		break;
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
26927c478bd9Sstevel@tonic-gate 	case ECANCELED:
26937c478bd9Sstevel@tonic-gate 	case EPERM:
26947c478bd9Sstevel@tonic-gate 	case EACCES:
26957c478bd9Sstevel@tonic-gate 	case EROFS:
26967c478bd9Sstevel@tonic-gate 		goto out;
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	default:
26997c478bd9Sstevel@tonic-gate 		bad_error("libscf_inst_get_or_add_pg", ret);
27007c478bd9Sstevel@tonic-gate 	}
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&tv, NULL);
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	r = scf_value_set_time(v_time, tv.tv_sec, tv.tv_usec * 1000);
27057c478bd9Sstevel@tonic-gate 	assert(r == 0);
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 	scf_value_set_integer(v_stat, status);
27087c478bd9Sstevel@tonic-gate 
27097c478bd9Sstevel@tonic-gate 	for (;;) {
27107c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != 0) {
27117c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
27127c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
27137c478bd9Sstevel@tonic-gate 			default:
27147c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
27157c478bd9Sstevel@tonic-gate 				goto out;
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
27187c478bd9Sstevel@tonic-gate 				ret = ECANCELED;
27197c478bd9Sstevel@tonic-gate 				goto out;
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
27227c478bd9Sstevel@tonic-gate 				ret = EPERM;
27237c478bd9Sstevel@tonic-gate 				goto out;
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
27267c478bd9Sstevel@tonic-gate 				ret = EACCES;
27277c478bd9Sstevel@tonic-gate 				goto out;
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
27307c478bd9Sstevel@tonic-gate 				ret = EROFS;
27317c478bd9Sstevel@tonic-gate 				goto out;
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
27347c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_start", ret);
27357c478bd9Sstevel@tonic-gate 			}
27367c478bd9Sstevel@tonic-gate 		}
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "%s_method_timestamp",
27397c478bd9Sstevel@tonic-gate 		    name);
27407c478bd9Sstevel@tonic-gate 		ret = transaction_add_set(tx, e_time, pname, SCF_TYPE_TIME);
27417c478bd9Sstevel@tonic-gate 		switch (ret) {
27427c478bd9Sstevel@tonic-gate 		case 0:
27437c478bd9Sstevel@tonic-gate 			break;
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
27467c478bd9Sstevel@tonic-gate 		case ECANCELED:
27477c478bd9Sstevel@tonic-gate 			goto out;
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 		default:
27507c478bd9Sstevel@tonic-gate 			bad_error("transaction_add_set", ret);
27517c478bd9Sstevel@tonic-gate 		}
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 		r = scf_entry_add_value(e_time, v_time);
27547c478bd9Sstevel@tonic-gate 		assert(r == 0);
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, sizeof (pname), "%s_method_waitstatus",
27577c478bd9Sstevel@tonic-gate 		    name);
27587c478bd9Sstevel@tonic-gate 		ret = transaction_add_set(tx, e_stat, pname, SCF_TYPE_INTEGER);
27597c478bd9Sstevel@tonic-gate 		switch (ret) {
27607c478bd9Sstevel@tonic-gate 		case 0:
27617c478bd9Sstevel@tonic-gate 			break;
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
27647c478bd9Sstevel@tonic-gate 		case ECANCELED:
27657c478bd9Sstevel@tonic-gate 			goto out;
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate 		default:
27687c478bd9Sstevel@tonic-gate 			bad_error("transaction_add_set", ret);
27697c478bd9Sstevel@tonic-gate 		}
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 		r = scf_entry_add_value(e_stat, v_stat);
27727c478bd9Sstevel@tonic-gate 		if (r != 0)
27737c478bd9Sstevel@tonic-gate 			bad_error("scf_entry_add_value", scf_error());
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 		r = scf_transaction_commit(tx);
27767c478bd9Sstevel@tonic-gate 		if (r == 1)
27777c478bd9Sstevel@tonic-gate 			break;
27787c478bd9Sstevel@tonic-gate 		if (r != 0) {
27797c478bd9Sstevel@tonic-gate 			scfe = scf_error();
27807c478bd9Sstevel@tonic-gate 			scf_transaction_reset_all(tx);
27817c478bd9Sstevel@tonic-gate 			switch (scfe) {
27827c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
27837c478bd9Sstevel@tonic-gate 			default:
27847c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
27857c478bd9Sstevel@tonic-gate 				goto out;
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
27887c478bd9Sstevel@tonic-gate 				ret = ECANCELED;
27897c478bd9Sstevel@tonic-gate 				goto out;
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
27927c478bd9Sstevel@tonic-gate 				ret = EPERM;
27937c478bd9Sstevel@tonic-gate 				goto out;
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
27967c478bd9Sstevel@tonic-gate 				ret = EACCES;
27977c478bd9Sstevel@tonic-gate 				goto out;
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
28007c478bd9Sstevel@tonic-gate 				ret = EROFS;
28017c478bd9Sstevel@tonic-gate 				goto out;
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
28047c478bd9Sstevel@tonic-gate 				bad_error("scf_transaction_commit", scfe);
28057c478bd9Sstevel@tonic-gate 			}
28067c478bd9Sstevel@tonic-gate 		}
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 		scf_transaction_reset_all(tx);
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
28117c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
28127c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
28137c478bd9Sstevel@tonic-gate 			default:
28147c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
28157c478bd9Sstevel@tonic-gate 				goto out;
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
28187c478bd9Sstevel@tonic-gate 				ret = ECANCELED;
28197c478bd9Sstevel@tonic-gate 				goto out;
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
28227c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_update", scf_error());
28237c478bd9Sstevel@tonic-gate 			}
28247c478bd9Sstevel@tonic-gate 		}
28257c478bd9Sstevel@tonic-gate 	}
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate out:
28287c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
28297c478bd9Sstevel@tonic-gate 	scf_entry_destroy(e_time);
28307c478bd9Sstevel@tonic-gate 	scf_value_destroy(v_time);
28317c478bd9Sstevel@tonic-gate 	scf_entry_destroy(e_stat);
28327c478bd9Sstevel@tonic-gate 	scf_value_destroy(v_stat);
28337c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	return (ret);
28367c478bd9Sstevel@tonic-gate }
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate /*
28397c478bd9Sstevel@tonic-gate  * Call dgraph_add_instance() for each instance in the repository.
28407c478bd9Sstevel@tonic-gate  */
28417c478bd9Sstevel@tonic-gate void
28427c478bd9Sstevel@tonic-gate libscf_populate_graph(scf_handle_t *h)
28437c478bd9Sstevel@tonic-gate {
28447c478bd9Sstevel@tonic-gate 	scf_scope_t *scope;
28457c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
28467c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
28477c478bd9Sstevel@tonic-gate 	scf_iter_t *svc_iter;
28487c478bd9Sstevel@tonic-gate 	scf_iter_t *inst_iter;
28497c478bd9Sstevel@tonic-gate 	int ret;
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	scope = safe_scf_scope_create(h);
28527c478bd9Sstevel@tonic-gate 	svc = safe_scf_service_create(h);
28537c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
28547c478bd9Sstevel@tonic-gate 	svc_iter = safe_scf_iter_create(h);
28557c478bd9Sstevel@tonic-gate 	inst_iter = safe_scf_iter_create(h);
28567c478bd9Sstevel@tonic-gate 
2857*70cbfe41SPhilippe Jung 	deathrow_init();
2858*70cbfe41SPhilippe Jung 
28597c478bd9Sstevel@tonic-gate 	if ((ret = scf_handle_get_local_scope(h, scope)) !=
28607c478bd9Sstevel@tonic-gate 	    SCF_SUCCESS)
28617c478bd9Sstevel@tonic-gate 		uu_die("retrieving local scope failed: %d\n", ret);
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 	if (scf_iter_scope_services(svc_iter, scope) == -1)
28647c478bd9Sstevel@tonic-gate 		uu_die("walking local scope's services failed\n");
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	while (scf_iter_next_service(svc_iter, svc) > 0) {
28677c478bd9Sstevel@tonic-gate 		if (scf_iter_service_instances(inst_iter, svc) == -1)
28687c478bd9Sstevel@tonic-gate 			uu_die("unable to walk service's instances");
28697c478bd9Sstevel@tonic-gate 
28707c478bd9Sstevel@tonic-gate 		while (scf_iter_next_instance(inst_iter, inst) > 0) {
28717c478bd9Sstevel@tonic-gate 			char *fmri;
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 			if (libscf_instance_get_fmri(inst, &fmri) == 0) {
28747c478bd9Sstevel@tonic-gate 				int err;
28757c478bd9Sstevel@tonic-gate 
28767c478bd9Sstevel@tonic-gate 				err = dgraph_add_instance(fmri, inst, B_TRUE);
28777c478bd9Sstevel@tonic-gate 				if (err != 0 && err != EEXIST)
28787c478bd9Sstevel@tonic-gate 					log_error(LOG_WARNING,
28797c478bd9Sstevel@tonic-gate 					    "Failed to add %s (%s).\n", fmri,
28807c478bd9Sstevel@tonic-gate 					    strerror(err));
28817c478bd9Sstevel@tonic-gate 				startd_free(fmri, max_scf_fmri_size);
28827c478bd9Sstevel@tonic-gate 			}
28837c478bd9Sstevel@tonic-gate 		}
28847c478bd9Sstevel@tonic-gate 	}
28857c478bd9Sstevel@tonic-gate 
2886*70cbfe41SPhilippe Jung 	deathrow_fini();
2887*70cbfe41SPhilippe Jung 
28887c478bd9Sstevel@tonic-gate 	scf_iter_destroy(inst_iter);
28897c478bd9Sstevel@tonic-gate 	scf_iter_destroy(svc_iter);
28907c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
28917c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
28927c478bd9Sstevel@tonic-gate 	scf_scope_destroy(scope);
28937c478bd9Sstevel@tonic-gate }
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate /*
28967c478bd9Sstevel@tonic-gate  * Monitors get handled differently since there can be multiple of them.
28977c478bd9Sstevel@tonic-gate  *
28987c478bd9Sstevel@tonic-gate  * Returns exec string on success.  If method not defined, returns
28997c478bd9Sstevel@tonic-gate  * LIBSCF_PGROUP_ABSENT; if exec property missing, returns
29007c478bd9Sstevel@tonic-gate  * LIBSCF_PROPERTY_ABSENT.  Returns LIBSCF_PROPERTY_ERROR on other failures.
29017c478bd9Sstevel@tonic-gate  */
29027c478bd9Sstevel@tonic-gate char *
29037c478bd9Sstevel@tonic-gate libscf_get_method(scf_handle_t *h, int type, restarter_inst_t *inst,
29047c478bd9Sstevel@tonic-gate     scf_snapshot_t *snap, method_restart_t *restart_on, uint_t *cte_mask,
29057c478bd9Sstevel@tonic-gate     uint8_t *need_sessionp, uint64_t *timeout, uint8_t *timeout_retry)
29067c478bd9Sstevel@tonic-gate {
29077c478bd9Sstevel@tonic-gate 	scf_instance_t *scf_inst = NULL;
29087c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL, *pg_startd = NULL;
29097c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
29107c478bd9Sstevel@tonic-gate 	const char *name;
29117c478bd9Sstevel@tonic-gate 	char *method = startd_alloc(max_scf_value_size);
29127c478bd9Sstevel@tonic-gate 	char *ig = startd_alloc(max_scf_value_size);
29137c478bd9Sstevel@tonic-gate 	char *restart = startd_alloc(max_scf_value_size);
29147c478bd9Sstevel@tonic-gate 	char *ret;
29157c478bd9Sstevel@tonic-gate 	int error = 0, r;
29167c478bd9Sstevel@tonic-gate 
29177c478bd9Sstevel@tonic-gate 	scf_inst = safe_scf_instance_create(h);
29187c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
29197c478bd9Sstevel@tonic-gate 	pg_startd = safe_scf_pg_create(h);
29207c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	ret = NULL;
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 	*restart_on = METHOD_RESTART_UNKNOWN;
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate 	switch (type) {
29277c478bd9Sstevel@tonic-gate 	case METHOD_START:
29287c478bd9Sstevel@tonic-gate 		name = "start";
29297c478bd9Sstevel@tonic-gate 		break;
29307c478bd9Sstevel@tonic-gate 	case METHOD_STOP:
29317c478bd9Sstevel@tonic-gate 		name = "stop";
29327c478bd9Sstevel@tonic-gate 		break;
29337c478bd9Sstevel@tonic-gate 	case METHOD_REFRESH:
29347c478bd9Sstevel@tonic-gate 		name = "refresh";
29357c478bd9Sstevel@tonic-gate 		break;
29367c478bd9Sstevel@tonic-gate 	default:
29377c478bd9Sstevel@tonic-gate 		error = LIBSCF_PROPERTY_ERROR;
29387c478bd9Sstevel@tonic-gate 		goto get_method_cleanup;
29397c478bd9Sstevel@tonic-gate 	}
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, inst->ri_i.i_fmri, NULL, NULL, scf_inst,
29427c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1) {
29437c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
29447c478bd9Sstevel@tonic-gate 		    "%s: get_method decode instance FMRI failed: %s\n",
29457c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri, scf_strerror(scf_error()));
29467c478bd9Sstevel@tonic-gate 		error = LIBSCF_PROPERTY_ERROR;
29477c478bd9Sstevel@tonic-gate 		goto get_method_cleanup;
29487c478bd9Sstevel@tonic-gate 	}
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(scf_inst, snap, name, pg) == -1) {
29517c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
29527c478bd9Sstevel@tonic-gate 			error = LIBSCF_PGROUP_ABSENT;
29537c478bd9Sstevel@tonic-gate 		else
29547c478bd9Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
29557c478bd9Sstevel@tonic-gate 		goto get_method_cleanup;
29567c478bd9Sstevel@tonic-gate 	}
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_EXEC, prop) == -1) {
29597c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
29607c478bd9Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ABSENT;
29617c478bd9Sstevel@tonic-gate 		else
29627c478bd9Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
29637c478bd9Sstevel@tonic-gate 		goto get_method_cleanup;
29647c478bd9Sstevel@tonic-gate 	}
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 	error = libscf_read_single_astring(h, prop, &method);
29677c478bd9Sstevel@tonic-gate 	if (error != 0) {
29687c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
29697c478bd9Sstevel@tonic-gate 		    "%s: get_method failed: can't get a single astring "
29707c478bd9Sstevel@tonic-gate 		    "from %s/%s\n", inst->ri_i.i_fmri, name, SCF_PROPERTY_EXEC);
29717c478bd9Sstevel@tonic-gate 		goto get_method_cleanup;
29727c478bd9Sstevel@tonic-gate 	}
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 	error = expand_method_tokens(method, scf_inst, snap, type, &ret);
29757c478bd9Sstevel@tonic-gate 	if (error != 0) {
29767c478bd9Sstevel@tonic-gate 		log_instance(inst, B_TRUE, "Could not expand method tokens "
29775ca87c7fSlianep 		    "in \"%s\": %s.", method, ret);
29787c478bd9Sstevel@tonic-gate 		error = LIBSCF_PROPERTY_ERROR;
29797c478bd9Sstevel@tonic-gate 		goto get_method_cleanup;
29807c478bd9Sstevel@tonic-gate 	}
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate 	r = get_count(pg, SCF_PROPERTY_TIMEOUT, timeout);
29837c478bd9Sstevel@tonic-gate 	switch (r) {
29847c478bd9Sstevel@tonic-gate 	case 0:
29857c478bd9Sstevel@tonic-gate 		break;
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
29887c478bd9Sstevel@tonic-gate 		error = LIBSCF_PROPERTY_ERROR;
29897c478bd9Sstevel@tonic-gate 		goto get_method_cleanup;
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 	case EINVAL:
29927c478bd9Sstevel@tonic-gate 		log_instance(inst, B_TRUE, "%s/%s is multi-valued or not of "
29937c478bd9Sstevel@tonic-gate 		    "type count.  Using infinite timeout.", name,
29947c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_TIMEOUT);
29957c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
29967c478bd9Sstevel@tonic-gate 	case ECANCELED:
29977c478bd9Sstevel@tonic-gate 	case ENOENT:
29987c478bd9Sstevel@tonic-gate 		*timeout = METHOD_TIMEOUT_INFINITE;
29997c478bd9Sstevel@tonic-gate 		break;
30007c478bd9Sstevel@tonic-gate 
30013eae19d9Swesolows 	case EACCES:
30027c478bd9Sstevel@tonic-gate 	default:
30037c478bd9Sstevel@tonic-gate 		bad_error("get_count", r);
30047c478bd9Sstevel@tonic-gate 	}
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 	/* Both 0 and -1 (ugh) are considered infinite timeouts. */
30077c478bd9Sstevel@tonic-gate 	if (*timeout == -1 || *timeout == 0)
30087c478bd9Sstevel@tonic-gate 		*timeout = METHOD_TIMEOUT_INFINITE;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(scf_inst, snap, SCF_PG_STARTD,
30117c478bd9Sstevel@tonic-gate 	    pg_startd) == -1) {
30127c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
30137c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
30147c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
30157c478bd9Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
30167c478bd9Sstevel@tonic-gate 			goto get_method_cleanup;
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
30197c478bd9Sstevel@tonic-gate 			*cte_mask = 0;
30207c478bd9Sstevel@tonic-gate 			break;
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
30237c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
30247c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
30257c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
30267c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg_composed", scf_error());
30277c478bd9Sstevel@tonic-gate 		}
30287c478bd9Sstevel@tonic-gate 	} else {
30297c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg_startd, SCF_PROPERTY_IGNORE,
30307c478bd9Sstevel@tonic-gate 		    prop) == -1) {
30317c478bd9Sstevel@tonic-gate 			if (scf_error() == SCF_ERROR_NOT_FOUND)
30327c478bd9Sstevel@tonic-gate 				*cte_mask = 0;
30337c478bd9Sstevel@tonic-gate 			else {
30347c478bd9Sstevel@tonic-gate 				error = LIBSCF_PROPERTY_ERROR;
30357c478bd9Sstevel@tonic-gate 				goto get_method_cleanup;
30367c478bd9Sstevel@tonic-gate 			}
30377c478bd9Sstevel@tonic-gate 		} else {
30387c478bd9Sstevel@tonic-gate 			error = libscf_read_single_astring(h, prop, &ig);
30397c478bd9Sstevel@tonic-gate 			if (error != 0) {
30407c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
30417c478bd9Sstevel@tonic-gate 				    "%s: get_method failed: can't get a single "
30427c478bd9Sstevel@tonic-gate 				    "astring from %s/%s\n", inst->ri_i.i_fmri,
30437c478bd9Sstevel@tonic-gate 				    name, SCF_PROPERTY_IGNORE);
30447c478bd9Sstevel@tonic-gate 				goto get_method_cleanup;
30457c478bd9Sstevel@tonic-gate 			}
30467c478bd9Sstevel@tonic-gate 
30477c478bd9Sstevel@tonic-gate 			if (strcmp(ig, "core") == 0)
30487c478bd9Sstevel@tonic-gate 				*cte_mask = CT_PR_EV_CORE;
30497c478bd9Sstevel@tonic-gate 			else if (strcmp(ig, "signal") == 0)
30507c478bd9Sstevel@tonic-gate 				*cte_mask = CT_PR_EV_SIGNAL;
30517c478bd9Sstevel@tonic-gate 			else if (strcmp(ig, "core,signal") == 0 ||
30527c478bd9Sstevel@tonic-gate 			    strcmp(ig, "signal,core") == 0)
30537c478bd9Sstevel@tonic-gate 				*cte_mask = CT_PR_EV_CORE | CT_PR_EV_SIGNAL;
30547c478bd9Sstevel@tonic-gate 			else
30557c478bd9Sstevel@tonic-gate 				*cte_mask = 0;
30567c478bd9Sstevel@tonic-gate 		}
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 		r = get_boolean(pg_startd, SCF_PROPERTY_NEED_SESSION,
30597c478bd9Sstevel@tonic-gate 		    need_sessionp);
30607c478bd9Sstevel@tonic-gate 		switch (r) {
30617c478bd9Sstevel@tonic-gate 		case 0:
30627c478bd9Sstevel@tonic-gate 			break;
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
30657c478bd9Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
30667c478bd9Sstevel@tonic-gate 			goto get_method_cleanup;
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 		case ECANCELED:
30697c478bd9Sstevel@tonic-gate 		case ENOENT:
30707c478bd9Sstevel@tonic-gate 		case EINVAL:
30717c478bd9Sstevel@tonic-gate 			*need_sessionp = 0;
30727c478bd9Sstevel@tonic-gate 			break;
30737c478bd9Sstevel@tonic-gate 
30743eae19d9Swesolows 		case EACCES:
30757c478bd9Sstevel@tonic-gate 		default:
30767c478bd9Sstevel@tonic-gate 			bad_error("get_boolean", r);
30777c478bd9Sstevel@tonic-gate 		}
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 		/*
30807c478bd9Sstevel@tonic-gate 		 * Determine whether service has overriden retry after
30817c478bd9Sstevel@tonic-gate 		 * method timeout.  Default to retry if no value is
30827c478bd9Sstevel@tonic-gate 		 * specified.
30837c478bd9Sstevel@tonic-gate 		 */
30847c478bd9Sstevel@tonic-gate 		r = get_boolean(pg_startd, SCF_PROPERTY_TIMEOUT_RETRY,
30857c478bd9Sstevel@tonic-gate 		    timeout_retry);
30867c478bd9Sstevel@tonic-gate 		switch (r) {
30877c478bd9Sstevel@tonic-gate 		case 0:
30887c478bd9Sstevel@tonic-gate 			break;
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
30917c478bd9Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
30927c478bd9Sstevel@tonic-gate 			goto get_method_cleanup;
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 		case ECANCELED:
30957c478bd9Sstevel@tonic-gate 		case ENOENT:
30967c478bd9Sstevel@tonic-gate 		case EINVAL:
30977c478bd9Sstevel@tonic-gate 			*timeout_retry = 1;
30987c478bd9Sstevel@tonic-gate 			break;
30997c478bd9Sstevel@tonic-gate 
31003eae19d9Swesolows 		case EACCES:
31017c478bd9Sstevel@tonic-gate 		default:
31027c478bd9Sstevel@tonic-gate 			bad_error("get_boolean", r);
31037c478bd9Sstevel@tonic-gate 		}
31047c478bd9Sstevel@tonic-gate 	}
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate 	if (type != METHOD_START)
31077c478bd9Sstevel@tonic-gate 		goto get_method_cleanup;
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate 	/* Only start methods need to honor the restart_on property. */
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_RESTART_ON, prop) == -1) {
31127c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
31137c478bd9Sstevel@tonic-gate 			*restart_on = METHOD_RESTART_ALL;
31147c478bd9Sstevel@tonic-gate 		else
31157c478bd9Sstevel@tonic-gate 			error = LIBSCF_PROPERTY_ERROR;
31167c478bd9Sstevel@tonic-gate 		goto get_method_cleanup;
31177c478bd9Sstevel@tonic-gate 	}
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 	error = libscf_read_single_astring(h, prop, &restart);
31207c478bd9Sstevel@tonic-gate 	if (error != 0) {
31217c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
31227c478bd9Sstevel@tonic-gate 		    "%s: get_method failed: can't get a single astring "
31237c478bd9Sstevel@tonic-gate 		    "from %s/%s\n", inst->ri_i.i_fmri, name,
31247c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_RESTART_ON);
31257c478bd9Sstevel@tonic-gate 		goto get_method_cleanup;
31267c478bd9Sstevel@tonic-gate 	}
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate 	if (strcmp(restart, "all") == 0)
31297c478bd9Sstevel@tonic-gate 		*restart_on = METHOD_RESTART_ALL;
31307c478bd9Sstevel@tonic-gate 	else if (strcmp(restart, "external_fault") == 0)
31317c478bd9Sstevel@tonic-gate 		*restart_on = METHOD_RESTART_EXTERNAL_FAULT;
31327c478bd9Sstevel@tonic-gate 	else if (strcmp(restart, "any_fault") == 0)
31337c478bd9Sstevel@tonic-gate 		*restart_on = METHOD_RESTART_ANY_FAULT;
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate get_method_cleanup:
31367c478bd9Sstevel@tonic-gate 	startd_free(ig, max_scf_value_size);
31377c478bd9Sstevel@tonic-gate 	startd_free(method, max_scf_value_size);
31387c478bd9Sstevel@tonic-gate 	startd_free(restart, max_scf_value_size);
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	scf_instance_destroy(scf_inst);
31417c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
31427c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg_startd);
31437c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 	if (error != 0 && ret != NULL) {
31467c478bd9Sstevel@tonic-gate 		free(ret);
31477c478bd9Sstevel@tonic-gate 		ret = NULL;
31487c478bd9Sstevel@tonic-gate 	}
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 	errno = error;
31517c478bd9Sstevel@tonic-gate 	return (ret);
31527c478bd9Sstevel@tonic-gate }
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate /*
31557c478bd9Sstevel@tonic-gate  * Returns 1 if we've reached the fault threshold
31567c478bd9Sstevel@tonic-gate  */
31577c478bd9Sstevel@tonic-gate int
31587c478bd9Sstevel@tonic-gate update_fault_count(restarter_inst_t *inst, int type)
31597c478bd9Sstevel@tonic-gate {
31607c478bd9Sstevel@tonic-gate 	assert(type == FAULT_COUNT_INCR || type == FAULT_COUNT_RESET);
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	if (type == FAULT_COUNT_INCR) {
31637c478bd9Sstevel@tonic-gate 		inst->ri_i.i_fault_count++;
31647c478bd9Sstevel@tonic-gate 		log_framework(LOG_INFO, "%s: Increasing fault count to %d\n",
31657c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri, inst->ri_i.i_fault_count);
31667c478bd9Sstevel@tonic-gate 	}
31677c478bd9Sstevel@tonic-gate 	if (type == FAULT_COUNT_RESET)
31687c478bd9Sstevel@tonic-gate 		inst->ri_i.i_fault_count = 0;
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 	if (inst->ri_i.i_fault_count >= FAULT_THRESHOLD)
31717c478bd9Sstevel@tonic-gate 		return (1);
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 	return (0);
31747c478bd9Sstevel@tonic-gate }
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate /*
31777c478bd9Sstevel@tonic-gate  * int libscf_unset_action()
31787c478bd9Sstevel@tonic-gate  *   Delete any pending timestamps for the specified action which is
31797c478bd9Sstevel@tonic-gate  *   older than the supplied ts.
31807c478bd9Sstevel@tonic-gate  *
31817c478bd9Sstevel@tonic-gate  *   Returns 0 on success, ECONNABORTED, EACCES, or EPERM on failure.
31827c478bd9Sstevel@tonic-gate  */
31837c478bd9Sstevel@tonic-gate int
31847c478bd9Sstevel@tonic-gate libscf_unset_action(scf_handle_t *h, scf_propertygroup_t *pg,
31857c478bd9Sstevel@tonic-gate     admin_action_t a, hrtime_t ts)
31867c478bd9Sstevel@tonic-gate {
31877c478bd9Sstevel@tonic-gate 	scf_transaction_t *t;
31887c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *e;
31897c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
31907c478bd9Sstevel@tonic-gate 	scf_value_t *val;
31917c478bd9Sstevel@tonic-gate 	hrtime_t rep_ts;
31927c478bd9Sstevel@tonic-gate 	int ret = 0, r;
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 	t = safe_scf_transaction_create(h);
31957c478bd9Sstevel@tonic-gate 	e = safe_scf_entry_create(h);
31967c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
31977c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 	for (;;) {
32007c478bd9Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
32017c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
32027c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
32037c478bd9Sstevel@tonic-gate 			default:
32047c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
32057c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
32087c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
32117c478bd9Sstevel@tonic-gate 				assert(0);
32127c478bd9Sstevel@tonic-gate 				abort();
32137c478bd9Sstevel@tonic-gate 			}
32147c478bd9Sstevel@tonic-gate 		}
32157c478bd9Sstevel@tonic-gate 
32167c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) == -1) {
32177c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
32187c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
32197c478bd9Sstevel@tonic-gate 			default:
32207c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
32217c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
32247c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
32277c478bd9Sstevel@tonic-gate 				ret = EPERM;
32287c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
32317c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
32327c478bd9Sstevel@tonic-gate 				ret = EACCES;
32337c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
32367c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
32377c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
32387c478bd9Sstevel@tonic-gate 				assert(0);
32397c478bd9Sstevel@tonic-gate 				abort();
32407c478bd9Sstevel@tonic-gate 			}
32417c478bd9Sstevel@tonic-gate 		}
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 		/* Return failure only if the property hasn't been deleted. */
32447c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, admin_actions[a], prop) == -1) {
32457c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
32467c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
32477c478bd9Sstevel@tonic-gate 			default:
32487c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
32497c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
32527c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
32537c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
32567c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
32577c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
32587c478bd9Sstevel@tonic-gate 				assert(0);
32597c478bd9Sstevel@tonic-gate 				abort();
32607c478bd9Sstevel@tonic-gate 			}
32617c478bd9Sstevel@tonic-gate 		}
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) == -1) {
32647c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
32657c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
32667c478bd9Sstevel@tonic-gate 			default:
32677c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
32687c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
32717c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
32727c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
32757c478bd9Sstevel@tonic-gate 				/*
32767c478bd9Sstevel@tonic-gate 				 * More than one value was associated with
32777c478bd9Sstevel@tonic-gate 				 * this property -- this is incorrect. Take
32787c478bd9Sstevel@tonic-gate 				 * the opportunity to clean up and clear the
32797c478bd9Sstevel@tonic-gate 				 * entire property.
32807c478bd9Sstevel@tonic-gate 				 */
32817c478bd9Sstevel@tonic-gate 				rep_ts = ts;
32827c478bd9Sstevel@tonic-gate 				break;
32837c478bd9Sstevel@tonic-gate 
32843eae19d9Swesolows 			case SCF_ERROR_PERMISSION_DENIED:
32857c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
32867c478bd9Sstevel@tonic-gate 				assert(0);
32877c478bd9Sstevel@tonic-gate 				abort();
32887c478bd9Sstevel@tonic-gate 			}
32897c478bd9Sstevel@tonic-gate 		} else if (scf_value_get_integer(val, &rep_ts) == -1) {
32907c478bd9Sstevel@tonic-gate 			assert(scf_error() == SCF_ERROR_TYPE_MISMATCH);
32917c478bd9Sstevel@tonic-gate 			rep_ts = 0;
32927c478bd9Sstevel@tonic-gate 		}
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 		/* Repository ts is more current. Don't clear the action. */
32957c478bd9Sstevel@tonic-gate 		if (rep_ts > ts)
32967c478bd9Sstevel@tonic-gate 			goto unset_action_cleanup;
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate 		r = scf_transaction_property_change_type(t, e,
32997c478bd9Sstevel@tonic-gate 		    admin_actions[a], SCF_TYPE_INTEGER);
33007c478bd9Sstevel@tonic-gate 		assert(r == 0);
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 		r = scf_transaction_commit(t);
33037c478bd9Sstevel@tonic-gate 		if (r == 1)
33047c478bd9Sstevel@tonic-gate 			break;
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 		if (r != 0) {
33077c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
33087c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
33097c478bd9Sstevel@tonic-gate 			default:
33107c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
33117c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
33147c478bd9Sstevel@tonic-gate 				break;
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
33177c478bd9Sstevel@tonic-gate 				ret = EPERM;
33187c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
33217c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
33227c478bd9Sstevel@tonic-gate 				ret = EACCES;
33237c478bd9Sstevel@tonic-gate 				goto unset_action_cleanup;
33247c478bd9Sstevel@tonic-gate 
33257c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
33267c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
33277c478bd9Sstevel@tonic-gate 				assert(0);
33287c478bd9Sstevel@tonic-gate 				abort();
33297c478bd9Sstevel@tonic-gate 			}
33307c478bd9Sstevel@tonic-gate 		}
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 		scf_transaction_reset(t);
33337c478bd9Sstevel@tonic-gate 	}
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate unset_action_cleanup:
33367c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(t);
33377c478bd9Sstevel@tonic-gate 	scf_entry_destroy(e);
33387c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
33397c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
33407c478bd9Sstevel@tonic-gate 
33417c478bd9Sstevel@tonic-gate 	return (ret);
33427c478bd9Sstevel@tonic-gate }
33437c478bd9Sstevel@tonic-gate 
33447c478bd9Sstevel@tonic-gate /*
33457c478bd9Sstevel@tonic-gate  * Decorates & binds hndl.  hndl must be unbound.  Returns
33467c478bd9Sstevel@tonic-gate  *   0 - success
33477c478bd9Sstevel@tonic-gate  *   -1 - repository server is not running
33487c478bd9Sstevel@tonic-gate  *   -1 - repository server is out of resources
33497c478bd9Sstevel@tonic-gate  */
33507c478bd9Sstevel@tonic-gate static int
33517c478bd9Sstevel@tonic-gate handle_decorate_and_bind(scf_handle_t *hndl)
33527c478bd9Sstevel@tonic-gate {
33537c478bd9Sstevel@tonic-gate 	scf_value_t *door_dec_value;
33547c478bd9Sstevel@tonic-gate 
33557c478bd9Sstevel@tonic-gate 	door_dec_value = safe_scf_value_create(hndl);
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate 	/*
33587c478bd9Sstevel@tonic-gate 	 * Decorate if alternate door path set.
33597c478bd9Sstevel@tonic-gate 	 */
33607c478bd9Sstevel@tonic-gate 	if (st->st_door_path) {
33617c478bd9Sstevel@tonic-gate 		if (scf_value_set_astring(door_dec_value, st->st_door_path) !=
33627c478bd9Sstevel@tonic-gate 		    0)
33637c478bd9Sstevel@tonic-gate 			uu_die("$STARTD_ALT_DOOR is too long.\n");
33647c478bd9Sstevel@tonic-gate 
33657c478bd9Sstevel@tonic-gate 		if (scf_handle_decorate(hndl, "door_path", door_dec_value) != 0)
33667c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decorate", scf_error());
33677c478bd9Sstevel@tonic-gate 	}
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate 	scf_value_destroy(door_dec_value);
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate 	if (scf_handle_bind(hndl) == 0)
33727c478bd9Sstevel@tonic-gate 		return (0);
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 	switch (scf_error()) {
33757c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NO_SERVER:
33767c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NO_RESOURCES:
33777c478bd9Sstevel@tonic-gate 		return (-1);
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
33807c478bd9Sstevel@tonic-gate 	case SCF_ERROR_IN_USE:
33817c478bd9Sstevel@tonic-gate 	default:
33827c478bd9Sstevel@tonic-gate 		bad_error("scf_handle_bind", scf_error());
33837c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
33847c478bd9Sstevel@tonic-gate 	}
33857c478bd9Sstevel@tonic-gate }
33867c478bd9Sstevel@tonic-gate 
33877c478bd9Sstevel@tonic-gate scf_handle_t *
33887c478bd9Sstevel@tonic-gate libscf_handle_create_bound(scf_version_t v)
33897c478bd9Sstevel@tonic-gate {
33907c478bd9Sstevel@tonic-gate 	scf_handle_t *hndl = scf_handle_create(v);
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 	if (hndl == NULL)
33937c478bd9Sstevel@tonic-gate 		return (hndl);
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	if (handle_decorate_and_bind(hndl) == 0)
33967c478bd9Sstevel@tonic-gate 		return (hndl);
33977c478bd9Sstevel@tonic-gate 
33987c478bd9Sstevel@tonic-gate 	scf_handle_destroy(hndl);
33997c478bd9Sstevel@tonic-gate 	return (NULL);
34007c478bd9Sstevel@tonic-gate }
34017c478bd9Sstevel@tonic-gate 
34027c478bd9Sstevel@tonic-gate void
34037c478bd9Sstevel@tonic-gate libscf_handle_rebind(scf_handle_t *h)
34047c478bd9Sstevel@tonic-gate {
34057c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(h);
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_configd_live_lock);
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 	/*
34107c478bd9Sstevel@tonic-gate 	 * Try to rebind the handle before sleeping in case the server isn't
34117c478bd9Sstevel@tonic-gate 	 * really dead.
34127c478bd9Sstevel@tonic-gate 	 */
34137c478bd9Sstevel@tonic-gate 	while (handle_decorate_and_bind(h) != 0)
34147c478bd9Sstevel@tonic-gate 		(void) pthread_cond_wait(&st->st_configd_live_cv,
34157c478bd9Sstevel@tonic-gate 		    &st->st_configd_live_lock);
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_configd_live_lock);
34187c478bd9Sstevel@tonic-gate }
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate /*
34217c478bd9Sstevel@tonic-gate  * Create a handle and try to bind it until it succeeds.  Always returns
34227c478bd9Sstevel@tonic-gate  * a bound handle.
34237c478bd9Sstevel@tonic-gate  */
34247c478bd9Sstevel@tonic-gate scf_handle_t *
34257c478bd9Sstevel@tonic-gate libscf_handle_create_bound_loop()
34267c478bd9Sstevel@tonic-gate {
34277c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 	while ((h = scf_handle_create(SCF_VERSION)) == NULL) {
34307c478bd9Sstevel@tonic-gate 		/* This should have been caught earlier. */
34317c478bd9Sstevel@tonic-gate 		assert(scf_error() != SCF_ERROR_VERSION_MISMATCH);
34327c478bd9Sstevel@tonic-gate 		(void) sleep(2);
34337c478bd9Sstevel@tonic-gate 	}
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 	if (handle_decorate_and_bind(h) != 0)
34367c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 	return (h);
34397c478bd9Sstevel@tonic-gate }
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate /*
34427c478bd9Sstevel@tonic-gate  * Call cb for each dependency property group of inst.  cb is invoked with
34437c478bd9Sstevel@tonic-gate  * a pointer to the scf_propertygroup_t and arg.  If the repository connection
34447c478bd9Sstevel@tonic-gate  * is broken, returns ECONNABORTED.  If inst is deleted, returns ECANCELED.
34457c478bd9Sstevel@tonic-gate  * If cb returns non-zero, the walk is stopped and EINTR is returned.
34467c478bd9Sstevel@tonic-gate  * Otherwise returns 0.
34477c478bd9Sstevel@tonic-gate  */
34487c478bd9Sstevel@tonic-gate int
34497c478bd9Sstevel@tonic-gate walk_dependency_pgs(scf_instance_t *inst, callback_t cb, void *arg)
34507c478bd9Sstevel@tonic-gate {
34517c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
34527c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
34537c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
34547c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
34557c478bd9Sstevel@tonic-gate 	int r;
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
34587c478bd9Sstevel@tonic-gate 
34597c478bd9Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
34607c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
34617c478bd9Sstevel@tonic-gate 
34627c478bd9Sstevel@tonic-gate 	snap = libscf_get_running_snapshot(inst);
34637c478bd9Sstevel@tonic-gate 
34647c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed_composed(iter, inst, snap,
34657c478bd9Sstevel@tonic-gate 	    SCF_GROUP_DEPENDENCY) != 0) {
34667c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
34677c478bd9Sstevel@tonic-gate 		scf_pg_destroy(pg);
34687c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
34697c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
34707c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
34717c478bd9Sstevel@tonic-gate 		default:
34727c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
34757c478bd9Sstevel@tonic-gate 			return (ECANCELED);
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
34787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
34797c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
34807c478bd9Sstevel@tonic-gate 			assert(0);
34817c478bd9Sstevel@tonic-gate 			abort();
34827c478bd9Sstevel@tonic-gate 		}
34837c478bd9Sstevel@tonic-gate 	}
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate 	for (;;) {
34867c478bd9Sstevel@tonic-gate 		r = scf_iter_next_pg(iter, pg);
34877c478bd9Sstevel@tonic-gate 		if (r == 0)
34887c478bd9Sstevel@tonic-gate 			break;
34897c478bd9Sstevel@tonic-gate 		if (r == -1) {
34907c478bd9Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
34917c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
34927c478bd9Sstevel@tonic-gate 			scf_iter_destroy(iter);
349358276701Shg115875 
349458276701Shg115875 			switch (scf_error()) {
349558276701Shg115875 			case SCF_ERROR_CONNECTION_BROKEN:
34967c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
349758276701Shg115875 
349858276701Shg115875 			case SCF_ERROR_DELETED:
349958276701Shg115875 				return (ECANCELED);
350058276701Shg115875 
350158276701Shg115875 			case SCF_ERROR_NOT_SET:
350258276701Shg115875 			case SCF_ERROR_INVALID_ARGUMENT:
350358276701Shg115875 			case SCF_ERROR_NOT_BOUND:
350458276701Shg115875 			case SCF_ERROR_HANDLE_MISMATCH:
350558276701Shg115875 			default:
350658276701Shg115875 				bad_error("scf_iter_next_pg", scf_error());
350758276701Shg115875 			}
35087c478bd9Sstevel@tonic-gate 		}
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 		r = cb(pg, arg);
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 		if (r != 0)
35137c478bd9Sstevel@tonic-gate 			break;
35147c478bd9Sstevel@tonic-gate 	}
35157c478bd9Sstevel@tonic-gate 
35167c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
35177c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
35187c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate 	return (r == 0 ? 0 : EINTR);
35217c478bd9Sstevel@tonic-gate }
35227c478bd9Sstevel@tonic-gate 
35237c478bd9Sstevel@tonic-gate /*
35247c478bd9Sstevel@tonic-gate  * Call cb for each of the string values of prop.  cb is invoked with
35257c478bd9Sstevel@tonic-gate  * a pointer to the string and arg.  If the connection to the repository is
35267c478bd9Sstevel@tonic-gate  * broken, ECONNABORTED is returned.  If the property is deleted, ECANCELED is
35277c478bd9Sstevel@tonic-gate  * returned.  If the property does not have astring type, EINVAL is returned.
35287c478bd9Sstevel@tonic-gate  * If cb returns non-zero, the walk is stopped and EINTR is returned.
35297c478bd9Sstevel@tonic-gate  * Otherwise 0 is returned.
35307c478bd9Sstevel@tonic-gate  */
35317c478bd9Sstevel@tonic-gate int
35327c478bd9Sstevel@tonic-gate walk_property_astrings(scf_property_t *prop, callback_t cb, void *arg)
35337c478bd9Sstevel@tonic-gate {
35347c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
35357c478bd9Sstevel@tonic-gate 	scf_value_t *val;
35367c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
35377c478bd9Sstevel@tonic-gate 	char *buf;
35387c478bd9Sstevel@tonic-gate 	int r;
35397c478bd9Sstevel@tonic-gate 	ssize_t sz;
35407c478bd9Sstevel@tonic-gate 
35417c478bd9Sstevel@tonic-gate 	if (scf_property_is_type(prop, SCF_TYPE_ASTRING) != 0) {
35427c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
35437c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
35447c478bd9Sstevel@tonic-gate 		default:
35457c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
35487c478bd9Sstevel@tonic-gate 			return (ECANCELED);
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 		case SCF_ERROR_TYPE_MISMATCH:
35517c478bd9Sstevel@tonic-gate 			return (EINVAL);
35527c478bd9Sstevel@tonic-gate 
35537c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
35547c478bd9Sstevel@tonic-gate 			assert(0);
35557c478bd9Sstevel@tonic-gate 			abort();
35567c478bd9Sstevel@tonic-gate 		}
35577c478bd9Sstevel@tonic-gate 	}
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate 	h = scf_property_handle(prop);
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
35627c478bd9Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) != 0) {
35657c478bd9Sstevel@tonic-gate 		scf_iter_destroy(iter);
35667c478bd9Sstevel@tonic-gate 		scf_value_destroy(val);
35677c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
35687c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
35697c478bd9Sstevel@tonic-gate 		default:
35707c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
35737c478bd9Sstevel@tonic-gate 			return (ECANCELED);
35747c478bd9Sstevel@tonic-gate 
35757c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
35767c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
35777c478bd9Sstevel@tonic-gate 			assert(0);
35787c478bd9Sstevel@tonic-gate 			abort();
35797c478bd9Sstevel@tonic-gate 		}
35807c478bd9Sstevel@tonic-gate 	}
35817c478bd9Sstevel@tonic-gate 
35827c478bd9Sstevel@tonic-gate 	buf = startd_alloc(max_scf_value_size);
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 	for (;;) {
35857c478bd9Sstevel@tonic-gate 		r = scf_iter_next_value(iter, val);
35867c478bd9Sstevel@tonic-gate 		if (r < 0) {
35877c478bd9Sstevel@tonic-gate 			startd_free(buf, max_scf_value_size);
35887c478bd9Sstevel@tonic-gate 			scf_iter_destroy(iter);
35897c478bd9Sstevel@tonic-gate 			scf_value_destroy(val);
359058276701Shg115875 
359158276701Shg115875 			switch (scf_error()) {
359258276701Shg115875 			case SCF_ERROR_CONNECTION_BROKEN:
35937c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
359458276701Shg115875 
359558276701Shg115875 			case SCF_ERROR_DELETED:
359658276701Shg115875 				return (ECANCELED);
359758276701Shg115875 
359858276701Shg115875 			case SCF_ERROR_NOT_SET:
359958276701Shg115875 			case SCF_ERROR_INVALID_ARGUMENT:
360058276701Shg115875 			case SCF_ERROR_NOT_BOUND:
360158276701Shg115875 			case SCF_ERROR_HANDLE_MISMATCH:
36023eae19d9Swesolows 			case SCF_ERROR_PERMISSION_DENIED:
360358276701Shg115875 			default:
360458276701Shg115875 				bad_error("scf_iter_next_value", scf_error());
360558276701Shg115875 			}
36067c478bd9Sstevel@tonic-gate 		}
36077c478bd9Sstevel@tonic-gate 		if (r == 0)
36087c478bd9Sstevel@tonic-gate 			break;
36097c478bd9Sstevel@tonic-gate 
36107c478bd9Sstevel@tonic-gate 		sz = scf_value_get_astring(val, buf, max_scf_value_size);
36117c478bd9Sstevel@tonic-gate 		assert(sz >= 0);
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate 		r = cb(buf, arg);
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate 		if (r != 0)
36167c478bd9Sstevel@tonic-gate 			break;
36177c478bd9Sstevel@tonic-gate 	}
36187c478bd9Sstevel@tonic-gate 
36197c478bd9Sstevel@tonic-gate 	startd_free(buf, max_scf_value_size);
36207c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
36217c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate 	return (r == 0 ? 0 : EINTR);
36247c478bd9Sstevel@tonic-gate }
36257c478bd9Sstevel@tonic-gate 
36267c478bd9Sstevel@tonic-gate /*
36277c478bd9Sstevel@tonic-gate  * Returns 0 or ECONNABORTED.
36287c478bd9Sstevel@tonic-gate  */
36297c478bd9Sstevel@tonic-gate int
36307c478bd9Sstevel@tonic-gate libscf_create_self(scf_handle_t *h)
36317c478bd9Sstevel@tonic-gate {
36327c478bd9Sstevel@tonic-gate 	scf_scope_t *scope;
36337c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
36347c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
36357c478bd9Sstevel@tonic-gate 	instance_data_t idata;
36367c478bd9Sstevel@tonic-gate 	int ret = 0, r;
36377c478bd9Sstevel@tonic-gate 	ctid_t ctid;
36387c478bd9Sstevel@tonic-gate 	uint64_t uint64;
36397c478bd9Sstevel@tonic-gate 	uint_t count = 0, msecs = ALLOC_DELAY;
36407c478bd9Sstevel@tonic-gate 
36417c478bd9Sstevel@tonic-gate 	const char * const startd_svc = "system/svc/restarter";
36427c478bd9Sstevel@tonic-gate 	const char * const startd_inst = "default";
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate 	/* If SCF_SERVICE_STARTD changes, our strings must change, too. */
36457c478bd9Sstevel@tonic-gate 	assert(strcmp(SCF_SERVICE_STARTD,
36467c478bd9Sstevel@tonic-gate 	    "svc:/system/svc/restarter:default") == 0);
36477c478bd9Sstevel@tonic-gate 
36487c478bd9Sstevel@tonic-gate 	scope = safe_scf_scope_create(h);
36497c478bd9Sstevel@tonic-gate 	svc = safe_scf_service_create(h);
36507c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate 	if (scf_handle_get_scope(h, SCF_SCOPE_LOCAL, scope) != 0) {
36537c478bd9Sstevel@tonic-gate 		assert(scf_error() == SCF_ERROR_CONNECTION_BROKEN);
36547c478bd9Sstevel@tonic-gate 		ret = ECONNABORTED;
36557c478bd9Sstevel@tonic-gate 		goto out;
36567c478bd9Sstevel@tonic-gate 	}
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate get_svc:
36597c478bd9Sstevel@tonic-gate 	if (scf_scope_get_service(scope, startd_svc, svc) != 0) {
36607c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
36617c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
36627c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
36637c478bd9Sstevel@tonic-gate 		default:
36647c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
36657c478bd9Sstevel@tonic-gate 			goto out;
36667c478bd9Sstevel@tonic-gate 
36677c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
36687c478bd9Sstevel@tonic-gate 			break;
36697c478bd9Sstevel@tonic-gate 
36707c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
36717c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
36727c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
36737c478bd9Sstevel@tonic-gate 			bad_error("scf_scope_get_service", scf_error());
36747c478bd9Sstevel@tonic-gate 		}
36757c478bd9Sstevel@tonic-gate 
36767c478bd9Sstevel@tonic-gate add_svc:
36777c478bd9Sstevel@tonic-gate 		if (scf_scope_add_service(scope, startd_svc, svc) != 0) {
36787c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
36797c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
36807c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
36817c478bd9Sstevel@tonic-gate 			default:
36827c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
36837c478bd9Sstevel@tonic-gate 				goto out;
36847c478bd9Sstevel@tonic-gate 
36857c478bd9Sstevel@tonic-gate 			case SCF_ERROR_EXISTS:
36867c478bd9Sstevel@tonic-gate 				goto get_svc;
36877c478bd9Sstevel@tonic-gate 
36887c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
36897c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
36907c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
36917c478bd9Sstevel@tonic-gate 				uu_warn("Could not create %s: %s\n",
36927c478bd9Sstevel@tonic-gate 				    SCF_SERVICE_STARTD,
36937c478bd9Sstevel@tonic-gate 				    scf_strerror(scf_error()));
36947c478bd9Sstevel@tonic-gate 				goto out;
36957c478bd9Sstevel@tonic-gate 
36967c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
36977c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
36987c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
36997c478bd9Sstevel@tonic-gate 				bad_error("scf_scope_add_service", scf_error());
37007c478bd9Sstevel@tonic-gate 			}
37017c478bd9Sstevel@tonic-gate 		}
37027c478bd9Sstevel@tonic-gate 	}
37037c478bd9Sstevel@tonic-gate 
37047c478bd9Sstevel@tonic-gate 	if (scf_service_get_instance(svc, startd_inst, NULL) == 0)
37057c478bd9Sstevel@tonic-gate 		goto out;
37067c478bd9Sstevel@tonic-gate 
37077c478bd9Sstevel@tonic-gate 	switch (scf_error()) {
37087c478bd9Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
37097c478bd9Sstevel@tonic-gate 	default:
37107c478bd9Sstevel@tonic-gate 		ret = ECONNABORTED;
37117c478bd9Sstevel@tonic-gate 		goto out;
37127c478bd9Sstevel@tonic-gate 
37137c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
37147c478bd9Sstevel@tonic-gate 		break;
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
37177c478bd9Sstevel@tonic-gate 		goto add_svc;
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	case SCF_ERROR_HANDLE_MISMATCH:
37207c478bd9Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
37217c478bd9Sstevel@tonic-gate 	case SCF_ERROR_NOT_SET:
37227c478bd9Sstevel@tonic-gate 		bad_error("scf_service_get_instance", scf_error());
37237c478bd9Sstevel@tonic-gate 	}
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate add_inst:
37267c478bd9Sstevel@tonic-gate 	if (scf_service_add_instance(svc, startd_inst, inst) != 0) {
37277c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
37287c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
37297c478bd9Sstevel@tonic-gate 		default:
37307c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
37317c478bd9Sstevel@tonic-gate 			goto out;
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
37347c478bd9Sstevel@tonic-gate 			break;
37357c478bd9Sstevel@tonic-gate 
37367c478bd9Sstevel@tonic-gate 		case SCF_ERROR_PERMISSION_DENIED:
37377c478bd9Sstevel@tonic-gate 		case SCF_ERROR_BACKEND_ACCESS:
37387c478bd9Sstevel@tonic-gate 			uu_die("Could not create %s: %s\n", SCF_SERVICE_STARTD,
37397c478bd9Sstevel@tonic-gate 			    scf_strerror(scf_error()));
37407c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 		case SCF_ERROR_BACKEND_READONLY:
37437c478bd9Sstevel@tonic-gate 			log_error(LOG_NOTICE,
37447c478bd9Sstevel@tonic-gate 			    "Could not create %s: backend readonly.\n",
37457c478bd9Sstevel@tonic-gate 			    SCF_SERVICE_STARTD);
37467c478bd9Sstevel@tonic-gate 			goto out;
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
37497c478bd9Sstevel@tonic-gate 			goto add_svc;
37507c478bd9Sstevel@tonic-gate 
37517c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
37527c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
37537c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
37547c478bd9Sstevel@tonic-gate 			bad_error("scf_service_add_instance", scf_error());
37557c478bd9Sstevel@tonic-gate 		}
37567c478bd9Sstevel@tonic-gate 	}
37577c478bd9Sstevel@tonic-gate 
37587c478bd9Sstevel@tonic-gate 	/* Set start time. */
37597c478bd9Sstevel@tonic-gate 	idata.i_fmri = SCF_SERVICE_STARTD;
37607c478bd9Sstevel@tonic-gate 	idata.i_state = RESTARTER_STATE_NONE;
37617c478bd9Sstevel@tonic-gate 	idata.i_next_state = RESTARTER_STATE_NONE;
37627c478bd9Sstevel@tonic-gate set_state:
37637c478bd9Sstevel@tonic-gate 	switch (r = _restarter_commit_states(h, &idata, RESTARTER_STATE_ONLINE,
37647c478bd9Sstevel@tonic-gate 	    RESTARTER_STATE_NONE, NULL)) {
37657c478bd9Sstevel@tonic-gate 	case 0:
37667c478bd9Sstevel@tonic-gate 		break;
37677c478bd9Sstevel@tonic-gate 
37687c478bd9Sstevel@tonic-gate 	case ENOMEM:
37697c478bd9Sstevel@tonic-gate 		++count;
37707c478bd9Sstevel@tonic-gate 		if (count < ALLOC_RETRY) {
37717c478bd9Sstevel@tonic-gate 			(void) poll(NULL, 0, msecs);
37727c478bd9Sstevel@tonic-gate 			msecs *= ALLOC_DELAY_MULT;
37737c478bd9Sstevel@tonic-gate 			goto set_state;
37747c478bd9Sstevel@tonic-gate 		}
37757c478bd9Sstevel@tonic-gate 
37767c478bd9Sstevel@tonic-gate 		uu_die("Insufficient memory.\n");
37777c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
37787c478bd9Sstevel@tonic-gate 
37797c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
37807c478bd9Sstevel@tonic-gate 		ret = ECONNABORTED;
37817c478bd9Sstevel@tonic-gate 		goto out;
37827c478bd9Sstevel@tonic-gate 
37837c478bd9Sstevel@tonic-gate 	case ENOENT:
37847c478bd9Sstevel@tonic-gate 		goto add_inst;
37857c478bd9Sstevel@tonic-gate 
37867c478bd9Sstevel@tonic-gate 	case EPERM:
37877c478bd9Sstevel@tonic-gate 	case EACCES:
37887c478bd9Sstevel@tonic-gate 	case EROFS:
37897c478bd9Sstevel@tonic-gate 		uu_warn("Could not timestamp %s: %s\n", idata.i_fmri,
37907c478bd9Sstevel@tonic-gate 		    strerror(r));
37917c478bd9Sstevel@tonic-gate 		break;
37927c478bd9Sstevel@tonic-gate 
37937c478bd9Sstevel@tonic-gate 	case EINVAL:
37947c478bd9Sstevel@tonic-gate 	default:
37957c478bd9Sstevel@tonic-gate 		bad_error("_restarter_commit_states", r);
37967c478bd9Sstevel@tonic-gate 	}
37977c478bd9Sstevel@tonic-gate 
37987c478bd9Sstevel@tonic-gate 	/* Set general/enabled. */
37997c478bd9Sstevel@tonic-gate 	ret = libscf_inst_set_boolean_prop(inst, SCF_PG_GENERAL,
38007c478bd9Sstevel@tonic-gate 	    SCF_PG_GENERAL_TYPE, SCF_PG_GENERAL_FLAGS, SCF_PROPERTY_ENABLED, 1);
38017c478bd9Sstevel@tonic-gate 	switch (ret) {
38027c478bd9Sstevel@tonic-gate 	case 0:
38037c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
38047c478bd9Sstevel@tonic-gate 	case EPERM:
38057c478bd9Sstevel@tonic-gate 	case EACCES:
38067c478bd9Sstevel@tonic-gate 	case EROFS:
38077c478bd9Sstevel@tonic-gate 		break;
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 	case ECANCELED:
38107c478bd9Sstevel@tonic-gate 		goto add_inst;
38117c478bd9Sstevel@tonic-gate 
38127c478bd9Sstevel@tonic-gate 	default:
38137c478bd9Sstevel@tonic-gate 		bad_error("libscf_inst_set_boolean_prop", ret);
38147c478bd9Sstevel@tonic-gate 	}
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate 	ret = libscf_write_start_pid(inst, getpid());
38177c478bd9Sstevel@tonic-gate 	switch (ret) {
38187c478bd9Sstevel@tonic-gate 	case 0:
38197c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
38207c478bd9Sstevel@tonic-gate 	case EPERM:
38217c478bd9Sstevel@tonic-gate 	case EACCES:
38227c478bd9Sstevel@tonic-gate 	case EROFS:
38237c478bd9Sstevel@tonic-gate 		break;
38247c478bd9Sstevel@tonic-gate 
38257c478bd9Sstevel@tonic-gate 	case ECANCELED:
38267c478bd9Sstevel@tonic-gate 		goto add_inst;
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate 	default:
38297c478bd9Sstevel@tonic-gate 		bad_error("libscf_write_start_pid", ret);
38307c478bd9Sstevel@tonic-gate 	}
38317c478bd9Sstevel@tonic-gate 
38327c478bd9Sstevel@tonic-gate 	ctid = proc_get_ctid();
38337c478bd9Sstevel@tonic-gate 	if (ctid > 0) {
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 		uint64 = (uint64_t)ctid;
38367c478bd9Sstevel@tonic-gate 		ret = libscf_inst_set_count_prop(inst,
38377c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER, SCF_PG_RESTARTER_TYPE,
38387c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_FLAGS, SCF_PROPERTY_CONTRACT, uint64);
38397c478bd9Sstevel@tonic-gate 
38407c478bd9Sstevel@tonic-gate 		switch (ret) {
38417c478bd9Sstevel@tonic-gate 		case 0:
38427c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
38437c478bd9Sstevel@tonic-gate 		case EPERM:
38447c478bd9Sstevel@tonic-gate 		case EACCES:
38457c478bd9Sstevel@tonic-gate 		case EROFS:
38467c478bd9Sstevel@tonic-gate 			break;
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate 		case ECANCELED:
38497c478bd9Sstevel@tonic-gate 			goto add_inst;
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate 		default:
38527c478bd9Sstevel@tonic-gate 			bad_error("libscf_inst_set_count_prop", ret);
38537c478bd9Sstevel@tonic-gate 		}
38547c478bd9Sstevel@tonic-gate 	}
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate 	ret = libscf_note_method_log(inst, LOG_PREFIX_EARLY,
38577c478bd9Sstevel@tonic-gate 	    STARTD_DEFAULT_LOG);
38587c478bd9Sstevel@tonic-gate 	if (ret == 0) {
38597c478bd9Sstevel@tonic-gate 		ret = libscf_note_method_log(inst, LOG_PREFIX_NORMAL,
38607c478bd9Sstevel@tonic-gate 		    STARTD_DEFAULT_LOG);
38617c478bd9Sstevel@tonic-gate 	}
38627c478bd9Sstevel@tonic-gate 
38637c478bd9Sstevel@tonic-gate 	switch (ret) {
38647c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
38657c478bd9Sstevel@tonic-gate 		case EPERM:
38667c478bd9Sstevel@tonic-gate 		case EACCES:
38677c478bd9Sstevel@tonic-gate 		case EROFS:
38687c478bd9Sstevel@tonic-gate 		case EAGAIN:
38697c478bd9Sstevel@tonic-gate 			break;
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate 		case ECANCELED:
38727c478bd9Sstevel@tonic-gate 			goto add_inst;
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate 		default:
38757c478bd9Sstevel@tonic-gate 			bad_error("libscf_note_method_log", ret);
38767c478bd9Sstevel@tonic-gate 	}
38777c478bd9Sstevel@tonic-gate 
38787c478bd9Sstevel@tonic-gate out:
38797c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
38807c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
38817c478bd9Sstevel@tonic-gate 	scf_scope_destroy(scope);
38827c478bd9Sstevel@tonic-gate 	return (ret);
38837c478bd9Sstevel@tonic-gate }
38847c478bd9Sstevel@tonic-gate 
38857c478bd9Sstevel@tonic-gate /*
38867c478bd9Sstevel@tonic-gate  * Returns
38877c478bd9Sstevel@tonic-gate  *   0 - success
38887c478bd9Sstevel@tonic-gate  *   ENOENT - SCF_SERVICE_STARTD does not exist in repository
38897c478bd9Sstevel@tonic-gate  *   EPERM
38907c478bd9Sstevel@tonic-gate  *   EACCES
38917c478bd9Sstevel@tonic-gate  *   EROFS
38927c478bd9Sstevel@tonic-gate  */
38937c478bd9Sstevel@tonic-gate int
38947c478bd9Sstevel@tonic-gate libscf_set_reconfig(int set)
38957c478bd9Sstevel@tonic-gate {
38967c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
38977c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
38987c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
38997c478bd9Sstevel@tonic-gate 	int ret = 0;
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
39027c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
39037c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
39047c478bd9Sstevel@tonic-gate 
39057c478bd9Sstevel@tonic-gate again:
39067c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL,
39077c478bd9Sstevel@tonic-gate 	    inst, NULL, NULL,  SCF_DECODE_FMRI_EXACT) == -1) {
39087c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
39097c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
39107c478bd9Sstevel@tonic-gate 		default:
39117c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
39127c478bd9Sstevel@tonic-gate 			goto again;
39137c478bd9Sstevel@tonic-gate 
39147c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
39157c478bd9Sstevel@tonic-gate 			ret = ENOENT;
39167c478bd9Sstevel@tonic-gate 			goto reconfig_out;
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
39197c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
39207c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
39217c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
39227c478bd9Sstevel@tonic-gate 		}
39237c478bd9Sstevel@tonic-gate 	}
39247c478bd9Sstevel@tonic-gate 
39257c478bd9Sstevel@tonic-gate 	ret = libscf_inst_set_boolean_prop(inst, "system", SCF_GROUP_FRAMEWORK,
39267c478bd9Sstevel@tonic-gate 	    SCF_PG_FLAG_NONPERSISTENT, "reconfigure", set);
39277c478bd9Sstevel@tonic-gate 	switch (ret) {
39287c478bd9Sstevel@tonic-gate 	case 0:
39297c478bd9Sstevel@tonic-gate 	case EPERM:
39307c478bd9Sstevel@tonic-gate 	case EACCES:
39317c478bd9Sstevel@tonic-gate 	case EROFS:
39327c478bd9Sstevel@tonic-gate 		break;
39337c478bd9Sstevel@tonic-gate 
39347c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
39357c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
39367c478bd9Sstevel@tonic-gate 		goto again;
39377c478bd9Sstevel@tonic-gate 
39387c478bd9Sstevel@tonic-gate 	case ECANCELED:
39397c478bd9Sstevel@tonic-gate 		ret = ENOENT;
39407c478bd9Sstevel@tonic-gate 		break;
39417c478bd9Sstevel@tonic-gate 
39427c478bd9Sstevel@tonic-gate 	default:
39437c478bd9Sstevel@tonic-gate 		bad_error("libscf_inst_set_boolean_prop", ret);
39447c478bd9Sstevel@tonic-gate 	}
39457c478bd9Sstevel@tonic-gate 
39467c478bd9Sstevel@tonic-gate reconfig_out:
39477c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
39487c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
39497c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
39507c478bd9Sstevel@tonic-gate 	return (ret);
39517c478bd9Sstevel@tonic-gate }
39527c478bd9Sstevel@tonic-gate 
39537c478bd9Sstevel@tonic-gate /*
39547c478bd9Sstevel@tonic-gate  * Set inst->ri_m_inst to the scf instance for inst.  If it has been deleted,
39557c478bd9Sstevel@tonic-gate  * set inst->ri_mi_deleted to true.  If the repository connection is broken, it
39567c478bd9Sstevel@tonic-gate  * is rebound with libscf_handle_rebound().
39577c478bd9Sstevel@tonic-gate  */
39587c478bd9Sstevel@tonic-gate void
39597c478bd9Sstevel@tonic-gate libscf_reget_instance(restarter_inst_t *inst)
39607c478bd9Sstevel@tonic-gate {
39617c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
39627c478bd9Sstevel@tonic-gate 	int r;
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst->ri_m_inst);
39657c478bd9Sstevel@tonic-gate 
39667c478bd9Sstevel@tonic-gate again:
39677c478bd9Sstevel@tonic-gate 	r = libscf_lookup_instance(inst->ri_i.i_fmri, inst->ri_m_inst);
39687c478bd9Sstevel@tonic-gate 	switch (r) {
39697c478bd9Sstevel@tonic-gate 	case 0:
39707c478bd9Sstevel@tonic-gate 	case ENOENT:
39717c478bd9Sstevel@tonic-gate 		inst->ri_mi_deleted = (r == ENOENT);
39727c478bd9Sstevel@tonic-gate 		return;
39737c478bd9Sstevel@tonic-gate 
39747c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
39757c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
39767c478bd9Sstevel@tonic-gate 		goto again;
39777c478bd9Sstevel@tonic-gate 
39787c478bd9Sstevel@tonic-gate 	case EINVAL:
39797c478bd9Sstevel@tonic-gate 	case ENOTSUP:
39807c478bd9Sstevel@tonic-gate 	default:
39817c478bd9Sstevel@tonic-gate 		bad_error("libscf_lookup_instance", r);
39827c478bd9Sstevel@tonic-gate 	}
39837c478bd9Sstevel@tonic-gate }
3984