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