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