xref: /illumos-gate/usr/src/cmd/svc/svcadm/svcadm.c (revision e8f5b3f5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57646ae23Slianep  * Common Development and Distribution License (the "License").
67646ae23Slianep  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22eb1a3463STruong Nguyen  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * svcadm - request adminstrative actions for service instances
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <locale.h>
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <libscf.h>
337c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
34eb1a3463STruong Nguyen #include <libcontract.h>
35eb1a3463STruong Nguyen #include <libcontract_priv.h>
36eb1a3463STruong Nguyen #include <sys/contract/process.h>
377c478bd9Sstevel@tonic-gate #include <libuutil.h>
387c478bd9Sstevel@tonic-gate #include <stddef.h>
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
43eb1a3463STruong Nguyen #include <fcntl.h>
44eb1a3463STruong Nguyen #include <procfs.h>
457c478bd9Sstevel@tonic-gate #include <assert.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
497c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SUNW_OST_OSCMD"
507c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* Must be a power of two */
537c478bd9Sstevel@tonic-gate #define	HT_BUCKETS	64
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Exit codes for enable and disable -s.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate #define	EXIT_SVC_FAILURE	3
597c478bd9Sstevel@tonic-gate #define	EXIT_DEP_FAILURE	4
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * How long we will wait (in seconds) for a service to change state
637c478bd9Sstevel@tonic-gate  * before re-checking its dependencies.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate #define	WAIT_INTERVAL		3
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #ifndef NDEBUG
687c478bd9Sstevel@tonic-gate #define	bad_error(func, err)	{					\
697c478bd9Sstevel@tonic-gate 	uu_warn("%s:%d: %s() failed with unexpected error %d.\n",	\
707c478bd9Sstevel@tonic-gate 	    __FILE__, __LINE__, (func), (err));				\
717c478bd9Sstevel@tonic-gate 	abort();							\
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate #else
747c478bd9Sstevel@tonic-gate #define	bad_error(func, err)	abort()
757c478bd9Sstevel@tonic-gate #endif
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate struct ht_elt {
797c478bd9Sstevel@tonic-gate 	struct ht_elt	*next;
807c478bd9Sstevel@tonic-gate 	boolean_t	active;
817c478bd9Sstevel@tonic-gate 	char		str[1];
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate scf_handle_t *h;
867c478bd9Sstevel@tonic-gate ssize_t max_scf_fmri_sz;
877c478bd9Sstevel@tonic-gate static const char *emsg_permission_denied;
887c478bd9Sstevel@tonic-gate static const char *emsg_nomem;
897c478bd9Sstevel@tonic-gate static const char *emsg_create_pg_perm_denied;
907c478bd9Sstevel@tonic-gate static const char *emsg_pg_perm_denied;
917c478bd9Sstevel@tonic-gate static const char *emsg_prop_perm_denied;
927c478bd9Sstevel@tonic-gate static const char *emsg_no_service;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static int exit_status = 0;
957c478bd9Sstevel@tonic-gate static int verbose = 0;
967c478bd9Sstevel@tonic-gate static char *scratch_fmri;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static struct ht_elt **visited;
997c478bd9Sstevel@tonic-gate 
1000b5c9250Shg void do_scfdie(int lineno) __NORETURN;
1010b5c9250Shg static void usage_milestone(void) __NORETURN;
102eb1a3463STruong Nguyen static void set_astring_prop(const char *, const char *, const char *,
103eb1a3463STruong Nguyen     uint32_t, const char *, const char *);
1040b5c9250Shg 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * Visitors from synch.c, needed for enable -s and disable -s.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate extern int is_enabled(scf_instance_t *);
1097c478bd9Sstevel@tonic-gate extern int has_potential(scf_instance_t *, int);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate void
1127c478bd9Sstevel@tonic-gate do_scfdie(int lineno)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	scf_error_t err;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	switch (err = scf_error()) {
1177c478bd9Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
1187c478bd9Sstevel@tonic-gate 		uu_die(gettext("Connection to repository server broken.  "
1197c478bd9Sstevel@tonic-gate 		    "Exiting.\n"));
1207c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_READONLY:
1237c478bd9Sstevel@tonic-gate 		uu_die(gettext("Repository is read-only.  Exiting.\n"));
1247c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	default:
1277c478bd9Sstevel@tonic-gate #ifdef NDEBUG
1287c478bd9Sstevel@tonic-gate 		uu_die(gettext("Unexpected libscf error: %s.  Exiting.\n"),
1297c478bd9Sstevel@tonic-gate 		    scf_strerror(err));
1307c478bd9Sstevel@tonic-gate #else
1317c478bd9Sstevel@tonic-gate 		uu_die("Unexpected libscf error on line %d: %s.\n", lineno,
1327c478bd9Sstevel@tonic-gate 		    scf_strerror(err));
1337c478bd9Sstevel@tonic-gate #endif
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #define	scfdie()	do_scfdie(__LINE__)
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static void
1407c478bd9Sstevel@tonic-gate usage()
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
1437c478bd9Sstevel@tonic-gate 	"Usage: %1$s [-v] [cmd [args ... ]]\n\n"
1447c478bd9Sstevel@tonic-gate 	"\t%1$s enable [-rst] <service> ...\t- enable and online service(s)\n"
1457c478bd9Sstevel@tonic-gate 	"\t%1$s disable [-st] <service> ...\t- disable and offline service(s)\n"
1467c478bd9Sstevel@tonic-gate 	"\t%1$s restart <service> ...\t\t- restart specified service(s)\n"
1477c478bd9Sstevel@tonic-gate 	"\t%1$s refresh <service> ...\t\t- re-read service configuration\n"
1487c478bd9Sstevel@tonic-gate 	"\t%1$s mark [-It] <state> <service> ...\t- set maintenance state\n"
1497c478bd9Sstevel@tonic-gate 	"\t%1$s clear <service> ...\t\t- clear maintenance state\n"
1507c478bd9Sstevel@tonic-gate 	"\t%1$s milestone [-d] <milestone>\t- advance to a service milestone\n"
1517c478bd9Sstevel@tonic-gate 	"\n\t"
1527c478bd9Sstevel@tonic-gate 	"Services can be specified using an FMRI, abbreviation, or fnmatch(5)\n"
1537c478bd9Sstevel@tonic-gate 	"\tpattern, as shown in these examples for svc:/network/smtp:sendmail\n"
1547c478bd9Sstevel@tonic-gate 	"\n"
1557c478bd9Sstevel@tonic-gate 	"\t%1$s <cmd> svc:/network/smtp:sendmail\n"
1567c478bd9Sstevel@tonic-gate 	"\t%1$s <cmd> network/smtp:sendmail\n"
1577c478bd9Sstevel@tonic-gate 	"\t%1$s <cmd> network/*mail\n"
1587c478bd9Sstevel@tonic-gate 	"\t%1$s <cmd> network/smtp\n"
1597c478bd9Sstevel@tonic-gate 	"\t%1$s <cmd> smtp:sendmail\n"
1607c478bd9Sstevel@tonic-gate 	"\t%1$s <cmd> smtp\n"
1617c478bd9Sstevel@tonic-gate 	"\t%1$s <cmd> sendmail\n"), uu_getpname());
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * FMRI hash table for recursive enable.
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static uint32_t
1727c478bd9Sstevel@tonic-gate hash_fmri(const char *str)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	uint32_t h = 0, g;
1757c478bd9Sstevel@tonic-gate 	const char *p;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* Generic hash function from uts/common/os/modhash.c . */
1787c478bd9Sstevel@tonic-gate 	for (p = str; *p != '\0'; ++p) {
1797c478bd9Sstevel@tonic-gate 		h = (h << 4) + *p;
1807c478bd9Sstevel@tonic-gate 		if ((g = (h & 0xf0000000)) != 0) {
1817c478bd9Sstevel@tonic-gate 			h ^= (g >> 24);
1827c478bd9Sstevel@tonic-gate 			h ^= g;
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	return (h);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * Return 1 if str has been visited, 0 if it has not, and -1 if memory could not
1917c478bd9Sstevel@tonic-gate  * be allocated.
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate static int
1947c478bd9Sstevel@tonic-gate visited_find_or_add(const char *str, struct ht_elt **hep)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	uint32_t h;
1977c478bd9Sstevel@tonic-gate 	uint_t i;
1987c478bd9Sstevel@tonic-gate 	struct ht_elt *he;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	h = hash_fmri(str);
2017c478bd9Sstevel@tonic-gate 	i = h & (HT_BUCKETS - 1);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	for (he = visited[i]; he != NULL; he = he->next) {
2047c478bd9Sstevel@tonic-gate 		if (strcmp(he->str, str) == 0) {
2057c478bd9Sstevel@tonic-gate 			if (hep)
2067c478bd9Sstevel@tonic-gate 				*hep = he;
2077c478bd9Sstevel@tonic-gate 			return (1);
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	he = malloc(offsetof(struct ht_elt, str) + strlen(str) + 1);
2127c478bd9Sstevel@tonic-gate 	if (he == NULL)
2137c478bd9Sstevel@tonic-gate 		return (-1);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	(void) strcpy(he->str, str);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	he->next = visited[i];
2187c478bd9Sstevel@tonic-gate 	visited[i] = he;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (hep)
2217c478bd9Sstevel@tonic-gate 		*hep = he;
2227c478bd9Sstevel@tonic-gate 	return (0);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * Returns 0, ECANCELED if pg is deleted, ENOENT if propname doesn't exist,
2287c478bd9Sstevel@tonic-gate  * EINVAL if the property is not of boolean type or has no values, and E2BIG
2297c478bd9Sstevel@tonic-gate  * if it has more than one value.  *bp is set if 0 or E2BIG is returned.
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate int
2327c478bd9Sstevel@tonic-gate get_bool_prop(scf_propertygroup_t *pg, const char *propname, uint8_t *bp)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
2357c478bd9Sstevel@tonic-gate 	scf_value_t *val;
2367c478bd9Sstevel@tonic-gate 	int ret;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if ((prop = scf_property_create(h)) == NULL ||
2397c478bd9Sstevel@tonic-gate 	    (val = scf_value_create(h)) == NULL)
2407c478bd9Sstevel@tonic-gate 		scfdie();
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, propname, prop) != 0) {
2437c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
2447c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
2457c478bd9Sstevel@tonic-gate 			ret = ECANCELED;
2467c478bd9Sstevel@tonic-gate 			goto out;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
2497c478bd9Sstevel@tonic-gate 			ret = ENOENT;
2507c478bd9Sstevel@tonic-gate 			goto out;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
2537c478bd9Sstevel@tonic-gate 			assert(0);
2547c478bd9Sstevel@tonic-gate 			abort();
2557c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		default:
2587c478bd9Sstevel@tonic-gate 			scfdie();
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) == 0) {
2637c478bd9Sstevel@tonic-gate 		ret = 0;
2647c478bd9Sstevel@tonic-gate 	} else {
2657c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
2667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
2677c478bd9Sstevel@tonic-gate 			ret = ENOENT;
2687c478bd9Sstevel@tonic-gate 			goto out;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
2717c478bd9Sstevel@tonic-gate 			ret = EINVAL;
2727c478bd9Sstevel@tonic-gate 			goto out;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
2757c478bd9Sstevel@tonic-gate 			ret = E2BIG;
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
2797c478bd9Sstevel@tonic-gate 			assert(0);
2807c478bd9Sstevel@tonic-gate 			abort();
2817c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		default:
2847c478bd9Sstevel@tonic-gate 			scfdie();
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if (scf_value_get_boolean(val, bp) != 0) {
2897c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
2907c478bd9Sstevel@tonic-gate 			scfdie();
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		ret = EINVAL;
2937c478bd9Sstevel@tonic-gate 		goto out;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate out:
2977c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
2987c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
2997c478bd9Sstevel@tonic-gate 	return (ret);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * Returns 0, EPERM, or EROFS.
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate static int
3067c478bd9Sstevel@tonic-gate set_bool_prop(scf_propertygroup_t *pg, const char *propname, boolean_t b)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	scf_value_t *v;
3097c478bd9Sstevel@tonic-gate 	scf_transaction_t *tx;
3107c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *ent;
3117c478bd9Sstevel@tonic-gate 	int ret = 0, r;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if ((tx = scf_transaction_create(h)) == NULL ||
3147c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL ||
3157c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL)
3167c478bd9Sstevel@tonic-gate 		scfdie();
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	scf_value_set_boolean(v, b);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	for (;;) {
3217c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) == -1) {
3227c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
3237c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
3247c478bd9Sstevel@tonic-gate 				ret = EPERM;
3257c478bd9Sstevel@tonic-gate 				goto out;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
3287c478bd9Sstevel@tonic-gate 				ret = EROFS;
3297c478bd9Sstevel@tonic-gate 				goto out;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 			default:
3327c478bd9Sstevel@tonic-gate 				scfdie();
3337c478bd9Sstevel@tonic-gate 			}
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, ent, propname,
3377c478bd9Sstevel@tonic-gate 		    SCF_TYPE_BOOLEAN) != 0) {
3387c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
3397c478bd9Sstevel@tonic-gate 				scfdie();
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 			if (scf_transaction_property_new(tx, ent, propname,
3427c478bd9Sstevel@tonic-gate 			    SCF_TYPE_BOOLEAN) != 0)
3437c478bd9Sstevel@tonic-gate 				scfdie();
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		r = scf_entry_add_value(ent, v);
3477c478bd9Sstevel@tonic-gate 		assert(r == 0);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		r = scf_transaction_commit(tx);
3507c478bd9Sstevel@tonic-gate 		if (r == 1)
3517c478bd9Sstevel@tonic-gate 			break;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		if (r != 0) {
3567c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
3577c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
3587c478bd9Sstevel@tonic-gate 				ret = EPERM;
3597c478bd9Sstevel@tonic-gate 				goto out;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
3627c478bd9Sstevel@tonic-gate 				ret = EROFS;
3637c478bd9Sstevel@tonic-gate 				goto out;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 			default:
3667c478bd9Sstevel@tonic-gate 				scfdie();
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1)
3717c478bd9Sstevel@tonic-gate 			scfdie();
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate out:
3757c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
3767c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
3777c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
3787c478bd9Sstevel@tonic-gate 	return (ret);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * Gets the single astring value of the propname property of pg.  prop & v are
3837c478bd9Sstevel@tonic-gate  * scratch space.  Returns the length of the string on success or
3847c478bd9Sstevel@tonic-gate  *   -ENOENT - pg has no property named propname
3857c478bd9Sstevel@tonic-gate  *   -E2BIG - property has no values or multiple values
3867c478bd9Sstevel@tonic-gate  *   -EINVAL - property type is not compatible with astring
3877c478bd9Sstevel@tonic-gate  */
3887c478bd9Sstevel@tonic-gate ssize_t
3897c478bd9Sstevel@tonic-gate get_astring_prop(const scf_propertygroup_t *pg, const char *propname,
3907c478bd9Sstevel@tonic-gate     scf_property_t *prop, scf_value_t *v, char *buf, size_t bufsz)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	ssize_t sz;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, propname, prop) != 0) {
3957c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
3967c478bd9Sstevel@tonic-gate 			scfdie();
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		return (-ENOENT);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	if (scf_property_get_value(prop, v) != 0) {
4027c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
4037c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
4047c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
4057c478bd9Sstevel@tonic-gate 			return (-E2BIG);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		default:
4087c478bd9Sstevel@tonic-gate 			scfdie();
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	sz = scf_value_get_astring(v, buf, bufsz);
4137c478bd9Sstevel@tonic-gate 	if (sz < 0) {
4147c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_TYPE_MISMATCH)
4157c478bd9Sstevel@tonic-gate 			scfdie();
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		return (-EINVAL);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	return (sz);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
424eb1a3463STruong Nguyen  * Returns 0 or EPERM.
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate static int
427eb1a3463STruong Nguyen pg_get_or_add(const scf_instance_t *inst, const char *pgname,
428eb1a3463STruong Nguyen     const char *pgtype, uint32_t pgflags, scf_propertygroup_t *pg)
4297c478bd9Sstevel@tonic-gate {
430eb1a3463STruong Nguyen again:
431eb1a3463STruong Nguyen 	if (scf_instance_get_pg(inst, pgname, pg) == 0)
432eb1a3463STruong Nguyen 		return (0);
4337c478bd9Sstevel@tonic-gate 
434eb1a3463STruong Nguyen 	if (scf_error() != SCF_ERROR_NOT_FOUND)
4357c478bd9Sstevel@tonic-gate 		scfdie();
4367c478bd9Sstevel@tonic-gate 
437eb1a3463STruong Nguyen 	if (scf_instance_add_pg(inst, pgname, pgtype, pgflags, pg) == 0)
438eb1a3463STruong Nguyen 		return (0);
4397c478bd9Sstevel@tonic-gate 
440eb1a3463STruong Nguyen 	switch (scf_error()) {
441eb1a3463STruong Nguyen 	case SCF_ERROR_EXISTS:
442eb1a3463STruong Nguyen 		goto again;
4437c478bd9Sstevel@tonic-gate 
444eb1a3463STruong Nguyen 	case SCF_ERROR_PERMISSION_DENIED:
445eb1a3463STruong Nguyen 		return (EPERM);
4467c478bd9Sstevel@tonic-gate 
447eb1a3463STruong Nguyen 	default:
448eb1a3463STruong Nguyen 		scfdie();
449eb1a3463STruong Nguyen 		/* NOTREACHED */
450eb1a3463STruong Nguyen 	}
451eb1a3463STruong Nguyen }
4527c478bd9Sstevel@tonic-gate 
453eb1a3463STruong Nguyen static int
454eb1a3463STruong Nguyen my_ct_name(char *out, size_t len)
455eb1a3463STruong Nguyen {
456eb1a3463STruong Nguyen 	ct_stathdl_t st;
457eb1a3463STruong Nguyen 	char *ct_fmri;
458eb1a3463STruong Nguyen 	ctid_t ct;
459eb1a3463STruong Nguyen 	int fd, errno, ret;
4607c478bd9Sstevel@tonic-gate 
461eb1a3463STruong Nguyen 	if ((ct = getctid()) == -1)
462eb1a3463STruong Nguyen 		uu_die(gettext("Could not get contract id for process"));
4637c478bd9Sstevel@tonic-gate 
464eb1a3463STruong Nguyen 	fd = contract_open(ct, "process", "status", O_RDONLY);
4657c478bd9Sstevel@tonic-gate 
466eb1a3463STruong Nguyen 	if ((errno = ct_status_read(fd, CTD_ALL, &st)) != 0)
467eb1a3463STruong Nguyen 		uu_warn(gettext("Could not read status of contract "
468eb1a3463STruong Nguyen 		    "%ld: %s.\n"), ct, strerror(errno));
4697c478bd9Sstevel@tonic-gate 
470eb1a3463STruong Nguyen 	if ((errno = ct_pr_status_get_svc_fmri(st, &ct_fmri)) != 0)
471eb1a3463STruong Nguyen 		uu_warn(gettext("Could not get svc_fmri for contract "
472eb1a3463STruong Nguyen 		    "%ld: %s.\n"), ct, strerror(errno));
4737c478bd9Sstevel@tonic-gate 
474eb1a3463STruong Nguyen 	ret = strlcpy(out, ct_fmri, len);
4757c478bd9Sstevel@tonic-gate 
476eb1a3463STruong Nguyen 	ct_status_free(st);
477eb1a3463STruong Nguyen 	(void) close(fd);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	return (ret);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /*
483eb1a3463STruong Nguyen  * Set auxiliary_tty and auxiliary_fmri properties in restarter_actions pg to
484eb1a3463STruong Nguyen  * communicate whether the action is requested from a tty and the fmri of the
485eb1a3463STruong Nguyen  * responsible process.
486*e8f5b3f5STruong Nguyen  *
487*e8f5b3f5STruong Nguyen  * Returns 0, EPERM, or EROFS
4887c478bd9Sstevel@tonic-gate  */
4897c478bd9Sstevel@tonic-gate static int
490eb1a3463STruong Nguyen restarter_setup(const char *fmri, const scf_instance_t *inst)
4917c478bd9Sstevel@tonic-gate {
492eb1a3463STruong Nguyen 	boolean_t b = B_FALSE;
493eb1a3463STruong Nguyen 	scf_propertygroup_t *pg = NULL;
494*e8f5b3f5STruong Nguyen 	int ret = 0;
4957c478bd9Sstevel@tonic-gate 
496eb1a3463STruong Nguyen 	if ((pg = scf_pg_create(h)) == NULL)
4977c478bd9Sstevel@tonic-gate 		scfdie();
4987c478bd9Sstevel@tonic-gate 
499eb1a3463STruong Nguyen 	if (pg_get_or_add(inst, SCF_PG_RESTARTER_ACTIONS,
500eb1a3463STruong Nguyen 	    SCF_PG_RESTARTER_ACTIONS_TYPE, SCF_PG_RESTARTER_ACTIONS_FLAGS,
501*e8f5b3f5STruong Nguyen 	    pg) == EPERM) {
502*e8f5b3f5STruong Nguyen 		if (!verbose)
503*e8f5b3f5STruong Nguyen 			uu_warn(emsg_permission_denied, fmri);
504*e8f5b3f5STruong Nguyen 		else
505*e8f5b3f5STruong Nguyen 			uu_warn(emsg_create_pg_perm_denied, fmri,
506*e8f5b3f5STruong Nguyen 			    SCF_PG_RESTARTER_ACTIONS);
507*e8f5b3f5STruong Nguyen 
508*e8f5b3f5STruong Nguyen 		ret = EPERM;
509*e8f5b3f5STruong Nguyen 		goto out;
510*e8f5b3f5STruong Nguyen 	}
5117c478bd9Sstevel@tonic-gate 
512eb1a3463STruong Nguyen 	/* Set auxiliary_tty property */
513eb1a3463STruong Nguyen 	if (isatty(STDIN_FILENO))
514eb1a3463STruong Nguyen 		b = B_TRUE;
5157c478bd9Sstevel@tonic-gate 
516eb1a3463STruong Nguyen 	/* Create and set state to disabled */
517eb1a3463STruong Nguyen 	switch (set_bool_prop(pg, SCF_PROPERTY_AUX_TTY, b) != 0) {
518eb1a3463STruong Nguyen 	case 0:
519eb1a3463STruong Nguyen 		break;
520eb1a3463STruong Nguyen 
521eb1a3463STruong Nguyen 	case EPERM:
522*e8f5b3f5STruong Nguyen 		if (!verbose)
523*e8f5b3f5STruong Nguyen 			uu_warn(emsg_permission_denied, fmri);
524*e8f5b3f5STruong Nguyen 		else
525*e8f5b3f5STruong Nguyen 			uu_warn(emsg_prop_perm_denied, fmri,
526*e8f5b3f5STruong Nguyen 			    SCF_PG_RESTARTER_ACTIONS, SCF_PROPERTY_AUX_TTY);
527*e8f5b3f5STruong Nguyen 
528*e8f5b3f5STruong Nguyen 		ret = EPERM;
529*e8f5b3f5STruong Nguyen 		goto out;
530*e8f5b3f5STruong Nguyen 		/* NOTREACHED */
531eb1a3463STruong Nguyen 
532eb1a3463STruong Nguyen 	case EROFS:
533*e8f5b3f5STruong Nguyen 		/* Shouldn't happen, but it can. */
534*e8f5b3f5STruong Nguyen 		if (!verbose)
535*e8f5b3f5STruong Nguyen 			uu_warn(gettext("%s: Repository read-only.\n"), fmri);
536*e8f5b3f5STruong Nguyen 		else
537*e8f5b3f5STruong Nguyen 			uu_warn(gettext("%s: Could not set %s/%s "
538*e8f5b3f5STruong Nguyen 			    "(repository read-only).\n"), fmri,
539*e8f5b3f5STruong Nguyen 			    SCF_PG_RESTARTER_ACTIONS, SCF_PROPERTY_AUX_TTY);
540*e8f5b3f5STruong Nguyen 
541*e8f5b3f5STruong Nguyen 		ret = EROFS;
542*e8f5b3f5STruong Nguyen 		goto out;
543*e8f5b3f5STruong Nguyen 		/* NOTREACHED */
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	default:
5467c478bd9Sstevel@tonic-gate 		scfdie();
5477c478bd9Sstevel@tonic-gate 	}
548eb1a3463STruong Nguyen 
549eb1a3463STruong Nguyen 	if (my_ct_name(scratch_fmri, max_scf_fmri_sz) > 0) {
550eb1a3463STruong Nguyen 		set_astring_prop(fmri, SCF_PG_RESTARTER_ACTIONS,
551eb1a3463STruong Nguyen 		    SCF_PG_RESTARTER_ACTIONS_TYPE,
552eb1a3463STruong Nguyen 		    SCF_PG_RESTARTER_ACTIONS_FLAGS,
553eb1a3463STruong Nguyen 		    SCF_PROPERTY_AUX_FMRI, scratch_fmri);
554eb1a3463STruong Nguyen 	} else {
555eb1a3463STruong Nguyen 		uu_warn(gettext("%s: Could not set %s/%s: "
556eb1a3463STruong Nguyen 		    "my_ct_name failed.\n"), fmri,
557eb1a3463STruong Nguyen 		    SCF_PG_RESTARTER_ACTIONS, SCF_PROPERTY_AUX_FMRI);
558eb1a3463STruong Nguyen 	}
559eb1a3463STruong Nguyen 
560*e8f5b3f5STruong Nguyen out:
561eb1a3463STruong Nguyen 	scf_pg_destroy(pg);
562*e8f5b3f5STruong Nguyen 	return (ret);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * Enable or disable inst, per enable.  If temp is true, set
5677c478bd9Sstevel@tonic-gate  * general_ovr/enabled.  Otherwise set general/enabled and delete
5687c478bd9Sstevel@tonic-gate  * general_ovr/enabled if it exists (order is important here: we don't want the
5697c478bd9Sstevel@tonic-gate  * enabled status to glitch).
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate static void
5727c478bd9Sstevel@tonic-gate set_inst_enabled(const char *fmri, scf_instance_t *inst, boolean_t temp,
5737c478bd9Sstevel@tonic-gate     boolean_t enable)
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
5767c478bd9Sstevel@tonic-gate 	uint8_t b;
5777c478bd9Sstevel@tonic-gate 	const char *pgname = NULL;	/* For emsg_pg_perm_denied */
5787c478bd9Sstevel@tonic-gate 	int r;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	pg = scf_pg_create(h);
5817c478bd9Sstevel@tonic-gate 	if (pg == NULL)
5827c478bd9Sstevel@tonic-gate 		scfdie();
5837c478bd9Sstevel@tonic-gate 
584eb1a3463STruong Nguyen 	if (restarter_setup(fmri, inst))
585*e8f5b3f5STruong Nguyen 		goto out;
586eb1a3463STruong Nguyen 
587fee38f6fStn 	/*
588fee38f6fStn 	 * An instance's configuration is incomplete if general/enabled
589fee38f6fStn 	 * doesn't exist. Create both the property group and property
590fee38f6fStn 	 * here if they don't exist.
591fee38f6fStn 	 */
592fee38f6fStn 	pgname = SCF_PG_GENERAL;
593fee38f6fStn 	if (pg_get_or_add(inst, pgname, SCF_PG_GENERAL_TYPE,
594fee38f6fStn 	    SCF_PG_GENERAL_FLAGS, pg) != 0)
595fee38f6fStn 		goto eperm;
596fee38f6fStn 
597fee38f6fStn 	if (get_bool_prop(pg, SCF_PROPERTY_ENABLED, &b) != 0) {
598fee38f6fStn 		/* Create and set state to disabled */
599fee38f6fStn 		switch (set_bool_prop(pg, SCF_PROPERTY_ENABLED, B_FALSE) != 0) {
600fee38f6fStn 		case 0:
601fee38f6fStn 			break;
602fee38f6fStn 
603fee38f6fStn 		case EPERM:
604fee38f6fStn 			goto eperm;
605fee38f6fStn 
606fee38f6fStn 		case EROFS:
607fee38f6fStn 			/* Shouldn't happen, but it can. */
608fee38f6fStn 			if (!verbose)
609fee38f6fStn 				uu_warn(gettext("%s: Repository read-only.\n"),
610fee38f6fStn 				    fmri);
611fee38f6fStn 			else
612fee38f6fStn 				uu_warn(gettext("%s: Could not set %s/%s "
613fee38f6fStn 				    "(repository read-only).\n"), fmri,
614fee38f6fStn 				    SCF_PG_GENERAL, SCF_PROPERTY_ENABLED);
615fee38f6fStn 			goto out;
616fee38f6fStn 
617fee38f6fStn 		default:
618fee38f6fStn 			assert(0);
619fee38f6fStn 			abort();
620fee38f6fStn 		}
621fee38f6fStn 	}
622fee38f6fStn 
6237c478bd9Sstevel@tonic-gate 	if (temp) {
6247c478bd9Sstevel@tonic-gate 		/* Set general_ovr/enabled */
6257c478bd9Sstevel@tonic-gate 		pgname = SCF_PG_GENERAL_OVR;
6267c478bd9Sstevel@tonic-gate 		if (pg_get_or_add(inst, pgname, SCF_PG_GENERAL_OVR_TYPE,
6277c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_OVR_FLAGS, pg) != 0)
6287c478bd9Sstevel@tonic-gate 			goto eperm;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		switch (set_bool_prop(pg, SCF_PROPERTY_ENABLED, enable) != 0) {
6317c478bd9Sstevel@tonic-gate 		case 0:
6327c478bd9Sstevel@tonic-gate 			break;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		case EPERM:
6357c478bd9Sstevel@tonic-gate 			goto eperm;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		case EROFS:
6387c478bd9Sstevel@tonic-gate 			/* Shouldn't happen, but it can. */
6397c478bd9Sstevel@tonic-gate 			if (!verbose)
6407c478bd9Sstevel@tonic-gate 				uu_warn(gettext("%s: Repository read-only.\n"),
6417c478bd9Sstevel@tonic-gate 				    fmri);
6427c478bd9Sstevel@tonic-gate 			else
6437c478bd9Sstevel@tonic-gate 				uu_warn(gettext("%s: Could not set %s/%s "
6447c478bd9Sstevel@tonic-gate 				    "(repository read-only).\n"), fmri,
6457c478bd9Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED);
6467c478bd9Sstevel@tonic-gate 			goto out;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		default:
6497c478bd9Sstevel@tonic-gate 			assert(0);
6507c478bd9Sstevel@tonic-gate 			abort();
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 		if (verbose)
6547c478bd9Sstevel@tonic-gate 			(void) printf(enable ?
6557c478bd9Sstevel@tonic-gate 			    gettext("%s temporarily enabled.\n") :
6567c478bd9Sstevel@tonic-gate 			    gettext("%s temporarily disabled.\n"), fmri);
6577c478bd9Sstevel@tonic-gate 	} else {
6587c478bd9Sstevel@tonic-gate again:
659fee38f6fStn 		/*
660fee38f6fStn 		 * Both pg and property should exist since we created
661fee38f6fStn 		 * them earlier. However, there's still a chance that
662fee38f6fStn 		 * someone may have deleted the property out from under
663fee38f6fStn 		 * us.
664fee38f6fStn 		 */
6657c478bd9Sstevel@tonic-gate 		if (pg_get_or_add(inst, pgname, SCF_PG_GENERAL_TYPE,
6667c478bd9Sstevel@tonic-gate 		    SCF_PG_GENERAL_FLAGS, pg) != 0)
6677c478bd9Sstevel@tonic-gate 			goto eperm;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		switch (set_bool_prop(pg, SCF_PROPERTY_ENABLED, enable)) {
6707c478bd9Sstevel@tonic-gate 		case 0:
6717c478bd9Sstevel@tonic-gate 			break;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		case EPERM:
6747c478bd9Sstevel@tonic-gate 			goto eperm;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		case EROFS:
6777c478bd9Sstevel@tonic-gate 			/*
6787c478bd9Sstevel@tonic-gate 			 * If general/enabled is already set the way we want,
6797c478bd9Sstevel@tonic-gate 			 * proceed.
6807c478bd9Sstevel@tonic-gate 			 */
6817c478bd9Sstevel@tonic-gate 			switch (get_bool_prop(pg, SCF_PROPERTY_ENABLED, &b)) {
6827c478bd9Sstevel@tonic-gate 			case 0:
6837c478bd9Sstevel@tonic-gate 				if ((b != 0) == (enable != B_FALSE))
6847c478bd9Sstevel@tonic-gate 					break;
6857c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 			case ENOENT:
6887c478bd9Sstevel@tonic-gate 			case EINVAL:
6897c478bd9Sstevel@tonic-gate 			case E2BIG:
6907c478bd9Sstevel@tonic-gate 				if (!verbose)
6917c478bd9Sstevel@tonic-gate 					uu_warn(gettext("%s: Repository "
6927c478bd9Sstevel@tonic-gate 					    "read-only.\n"), fmri);
6937c478bd9Sstevel@tonic-gate 				else
6947c478bd9Sstevel@tonic-gate 					uu_warn(gettext("%s: Could not set "
6957c478bd9Sstevel@tonic-gate 					    "%s/%s (repository read-only).\n"),
6967c478bd9Sstevel@tonic-gate 					    fmri, SCF_PG_GENERAL,
6977c478bd9Sstevel@tonic-gate 					    SCF_PROPERTY_ENABLED);
6987c478bd9Sstevel@tonic-gate 				goto out;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 			case ECANCELED:
7017c478bd9Sstevel@tonic-gate 				goto again;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 			default:
7047c478bd9Sstevel@tonic-gate 				assert(0);
7057c478bd9Sstevel@tonic-gate 				abort();
7067c478bd9Sstevel@tonic-gate 			}
7077c478bd9Sstevel@tonic-gate 			break;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		default:
7107c478bd9Sstevel@tonic-gate 			assert(0);
7117c478bd9Sstevel@tonic-gate 			abort();
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		pgname = SCF_PG_GENERAL_OVR;
715eb1a3463STruong Nguyen 		r = scf_instance_delete_prop(inst, pgname,
716eb1a3463STruong Nguyen 		    SCF_PROPERTY_ENABLED);
717eb1a3463STruong Nguyen 		switch (r) {
718eb1a3463STruong Nguyen 		case 0:
719eb1a3463STruong Nguyen 			break;
7207c478bd9Sstevel@tonic-gate 
721eb1a3463STruong Nguyen 		case ECANCELED:
722eb1a3463STruong Nguyen 			uu_warn(emsg_no_service, fmri);
723eb1a3463STruong Nguyen 			goto out;
7247c478bd9Sstevel@tonic-gate 
725eb1a3463STruong Nguyen 		case EPERM:
726eb1a3463STruong Nguyen 			goto eperm;
7277c478bd9Sstevel@tonic-gate 
728eb1a3463STruong Nguyen 		case EACCES:
729eb1a3463STruong Nguyen 			uu_warn(gettext("Could not delete %s/%s "
730eb1a3463STruong Nguyen 			    "property of %s: backend access denied.\n"),
731eb1a3463STruong Nguyen 			    pgname, SCF_PROPERTY_ENABLED, fmri);
732eb1a3463STruong Nguyen 			goto out;
7337c478bd9Sstevel@tonic-gate 
734eb1a3463STruong Nguyen 		case EROFS:
735eb1a3463STruong Nguyen 			uu_warn(gettext("Could not delete %s/%s "
736eb1a3463STruong Nguyen 			    "property of %s: backend is read-only.\n"),
737eb1a3463STruong Nguyen 			    pgname, SCF_PROPERTY_ENABLED, fmri);
738eb1a3463STruong Nguyen 			goto out;
7397c478bd9Sstevel@tonic-gate 
740eb1a3463STruong Nguyen 		default:
741eb1a3463STruong Nguyen 			bad_error("scf_instance_delete_prop", r);
7427c478bd9Sstevel@tonic-gate 		}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		if (verbose)
7457c478bd9Sstevel@tonic-gate 			(void) printf(enable ?  gettext("%s enabled.\n") :
7467c478bd9Sstevel@tonic-gate 			    gettext("%s disabled.\n"), fmri);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
7507c478bd9Sstevel@tonic-gate 	return;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate eperm:
7537c478bd9Sstevel@tonic-gate 	assert(pgname != NULL);
7547c478bd9Sstevel@tonic-gate 	if (!verbose)
7557c478bd9Sstevel@tonic-gate 		uu_warn(emsg_permission_denied, fmri);
7567c478bd9Sstevel@tonic-gate 	else
7577c478bd9Sstevel@tonic-gate 		uu_warn(emsg_pg_perm_denied, fmri, pgname);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate out:
7607c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
7617c478bd9Sstevel@tonic-gate 	exit_status = 1;
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate  * Set inst to the instance which corresponds to fmri.  If fmri identifies
7667c478bd9Sstevel@tonic-gate  * a service with a single instance, get that instance.
7677c478bd9Sstevel@tonic-gate  *
7687c478bd9Sstevel@tonic-gate  * Fails with
7697c478bd9Sstevel@tonic-gate  *   ENOTSUP - fmri has an unsupported scheme
7707c478bd9Sstevel@tonic-gate  *   EINVAL - fmri is invalid
7717c478bd9Sstevel@tonic-gate  *   ENOTDIR - fmri does not identify a service or instance
7727c478bd9Sstevel@tonic-gate  *   ENOENT - could not locate instance
7737c478bd9Sstevel@tonic-gate  *   E2BIG - fmri is a service with multiple instances (warning not printed)
7747c478bd9Sstevel@tonic-gate  */
7757c478bd9Sstevel@tonic-gate static int
7767c478bd9Sstevel@tonic-gate get_inst_mult(const char *fmri, scf_instance_t *inst)
7777c478bd9Sstevel@tonic-gate {
7787c478bd9Sstevel@tonic-gate 	char *cfmri;
7797c478bd9Sstevel@tonic-gate 	const char *svc_name, *inst_name, *pg_name;
7807c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
7817c478bd9Sstevel@tonic-gate 	scf_instance_t *inst2;
7827c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
7837c478bd9Sstevel@tonic-gate 	int ret;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	if (strncmp(fmri, "lrc:", sizeof ("lrc:") - 1) == 0) {
7867c478bd9Sstevel@tonic-gate 		uu_warn(gettext("FMRI \"%s\" is a legacy service.\n"), fmri);
7877c478bd9Sstevel@tonic-gate 		exit_status = 1;
7887c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	cfmri = strdup(fmri);
7927c478bd9Sstevel@tonic-gate 	if (cfmri == NULL)
7937c478bd9Sstevel@tonic-gate 		uu_die(emsg_nomem);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(cfmri, NULL, &svc_name, &inst_name, &pg_name,
7967c478bd9Sstevel@tonic-gate 	    NULL) != SCF_SUCCESS) {
7977c478bd9Sstevel@tonic-gate 		free(cfmri);
7987c478bd9Sstevel@tonic-gate 		uu_warn(gettext("FMRI \"%s\" is invalid.\n"), fmri);
7997c478bd9Sstevel@tonic-gate 		exit_status = 1;
8007c478bd9Sstevel@tonic-gate 		return (EINVAL);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	free(cfmri);
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if (svc_name == NULL || pg_name != NULL) {
8067c478bd9Sstevel@tonic-gate 		uu_warn(gettext(
8077c478bd9Sstevel@tonic-gate 		    "FMRI \"%s\" does not designate a service or instance.\n"),
8087c478bd9Sstevel@tonic-gate 		    fmri);
8097c478bd9Sstevel@tonic-gate 		exit_status = 1;
8107c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (inst_name != NULL) {
8147c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
8157c478bd9Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) == 0)
8167c478bd9Sstevel@tonic-gate 			return (0);
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
8197c478bd9Sstevel@tonic-gate 			scfdie();
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 		uu_warn(gettext("No such instance \"%s\".\n"), fmri);
8227c478bd9Sstevel@tonic-gate 		exit_status = 1;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 		return (ENOENT);
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if ((svc = scf_service_create(h)) == NULL ||
8287c478bd9Sstevel@tonic-gate 	    (inst2 = scf_instance_create(h)) == NULL ||
8297c478bd9Sstevel@tonic-gate 	    (iter = scf_iter_create(h)) == NULL)
8307c478bd9Sstevel@tonic-gate 		scfdie();
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
8337c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS) {
8347c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
8357c478bd9Sstevel@tonic-gate 			scfdie();
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		uu_warn(emsg_no_service, fmri);
8387c478bd9Sstevel@tonic-gate 		exit_status = 1;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 		ret = ENOENT;
8417c478bd9Sstevel@tonic-gate 		goto out;
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	/* If the service has only one child, use it. */
8457c478bd9Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != SCF_SUCCESS)
8467c478bd9Sstevel@tonic-gate 		scfdie();
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	ret = scf_iter_next_instance(iter, inst);
8497c478bd9Sstevel@tonic-gate 	if (ret < 0)
8507c478bd9Sstevel@tonic-gate 		scfdie();
8517c478bd9Sstevel@tonic-gate 	if (ret != 1) {
8527c478bd9Sstevel@tonic-gate 		uu_warn(gettext("Service \"%s\" has no instances.\n"),
8537c478bd9Sstevel@tonic-gate 		    fmri);
8547c478bd9Sstevel@tonic-gate 		exit_status = 1;
8557c478bd9Sstevel@tonic-gate 		ret = ENOENT;
8567c478bd9Sstevel@tonic-gate 		goto out;
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	ret = scf_iter_next_instance(iter, inst2);
8607c478bd9Sstevel@tonic-gate 	if (ret < 0)
8617c478bd9Sstevel@tonic-gate 		scfdie();
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	if (ret != 0) {
8647c478bd9Sstevel@tonic-gate 		ret = E2BIG;
8657c478bd9Sstevel@tonic-gate 		goto out;
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	ret = 0;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate out:
8717c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
8727c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst2);
8737c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
8747c478bd9Sstevel@tonic-gate 	return (ret);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate /*
8787c478bd9Sstevel@tonic-gate  * Same as get_inst_mult(), but on E2BIG prints a warning and returns ENOENT.
8797c478bd9Sstevel@tonic-gate  */
8807c478bd9Sstevel@tonic-gate static int
8817c478bd9Sstevel@tonic-gate get_inst(const char *fmri, scf_instance_t *inst)
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	int r;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	r = get_inst_mult(fmri, inst);
8867c478bd9Sstevel@tonic-gate 	if (r != E2BIG)
8877c478bd9Sstevel@tonic-gate 		return (r);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	uu_warn(gettext("operation on service %s is ambiguous; "
8907c478bd9Sstevel@tonic-gate 	    "instance specification needed.\n"), fmri);
8917c478bd9Sstevel@tonic-gate 	return (ENOENT);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate static char *
8957c478bd9Sstevel@tonic-gate inst_get_fmri(const scf_instance_t *inst)
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate 	ssize_t sz;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	sz = scf_instance_to_fmri(inst, scratch_fmri, max_scf_fmri_sz);
9007c478bd9Sstevel@tonic-gate 	if (sz < 0)
9017c478bd9Sstevel@tonic-gate 		scfdie();
9027c478bd9Sstevel@tonic-gate 	if (sz >= max_scf_fmri_sz)
9037c478bd9Sstevel@tonic-gate 		uu_die(gettext("scf_instance_to_fmri() returned unexpectedly "
9047c478bd9Sstevel@tonic-gate 		    "long value.\n"));
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	return (scratch_fmri);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate static ssize_t
9107c478bd9Sstevel@tonic-gate dep_get_astring(const char *fmri, const char *pgname,
9117c478bd9Sstevel@tonic-gate     const scf_propertygroup_t *pg, const char *propname, scf_property_t *prop,
9127c478bd9Sstevel@tonic-gate     scf_value_t *v, char *buf, size_t bufsz)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	ssize_t sz;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	sz = get_astring_prop(pg, propname, prop, v, buf, bufsz);
9177c478bd9Sstevel@tonic-gate 	if (sz >= 0)
9187c478bd9Sstevel@tonic-gate 		return (sz);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	switch (-sz) {
9217c478bd9Sstevel@tonic-gate 	case ENOENT:
9227c478bd9Sstevel@tonic-gate 		uu_warn(gettext("\"%s\" is misconfigured (\"%s\" dependency "
9237c478bd9Sstevel@tonic-gate 		    "lacks \"%s\" property.)\n"), fmri, pgname, propname);
9247c478bd9Sstevel@tonic-gate 		return (-1);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	case E2BIG:
9277c478bd9Sstevel@tonic-gate 		uu_warn(gettext("\"%s\" is misconfigured (\"%s/%s\" property "
9287c478bd9Sstevel@tonic-gate 		    "is not single-valued.)\n"), fmri, pgname, propname);
9297c478bd9Sstevel@tonic-gate 		return (-1);
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	case EINVAL:
9327c478bd9Sstevel@tonic-gate 		uu_warn(gettext("\"%s\" is misconfigured (\"%s/%s\" property "
9337c478bd9Sstevel@tonic-gate 		    "is not of astring type.)\n"), fmri, pgname, propname);
9347c478bd9Sstevel@tonic-gate 		return (-1);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	default:
9377c478bd9Sstevel@tonic-gate 		assert(0);
9387c478bd9Sstevel@tonic-gate 		abort();
9397c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate static boolean_t
9447c478bd9Sstevel@tonic-gate multiple_instances(scf_iter_t *iter, scf_value_t *v, char *buf)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	int count = 0, r;
9477c478bd9Sstevel@tonic-gate 	boolean_t ret;
9487c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
9517c478bd9Sstevel@tonic-gate 	if (inst == NULL)
9527c478bd9Sstevel@tonic-gate 		scfdie();
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	for (;;) {
9557c478bd9Sstevel@tonic-gate 		r = scf_iter_next_value(iter, v);
9567c478bd9Sstevel@tonic-gate 		if (r == 0) {
9577c478bd9Sstevel@tonic-gate 			ret = B_FALSE;
9587c478bd9Sstevel@tonic-gate 			goto out;
9597c478bd9Sstevel@tonic-gate 		}
9607c478bd9Sstevel@tonic-gate 		if (r != 1)
9617c478bd9Sstevel@tonic-gate 			scfdie();
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 		if (scf_value_get_astring(v, buf, max_scf_fmri_sz) < 0)
9647c478bd9Sstevel@tonic-gate 			scfdie();
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 		switch (get_inst_mult(buf, inst)) {
9677c478bd9Sstevel@tonic-gate 		case 0:
9687c478bd9Sstevel@tonic-gate 			++count;
9697c478bd9Sstevel@tonic-gate 			if (count > 1) {
9707c478bd9Sstevel@tonic-gate 				ret = B_TRUE;
9717c478bd9Sstevel@tonic-gate 				goto out;
9727c478bd9Sstevel@tonic-gate 			}
9737c478bd9Sstevel@tonic-gate 			break;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 		case ENOTSUP:
9767c478bd9Sstevel@tonic-gate 		case EINVAL:
9777c478bd9Sstevel@tonic-gate 		case ENOTDIR:
9787c478bd9Sstevel@tonic-gate 		case ENOENT:
9797c478bd9Sstevel@tonic-gate 			continue;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		case E2BIG:
9827c478bd9Sstevel@tonic-gate 			ret = B_TRUE;
9837c478bd9Sstevel@tonic-gate 			goto out;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		default:
9867c478bd9Sstevel@tonic-gate 			assert(0);
9877c478bd9Sstevel@tonic-gate 			abort();
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate out:
9927c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
9937c478bd9Sstevel@tonic-gate 	return (ret);
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate /*
9977c478bd9Sstevel@tonic-gate  * Enable the service or instance identified by fmri and its dependencies,
9987c478bd9Sstevel@tonic-gate  * recursively.  Specifically, call get_inst(fmri), enable the result, and
9997c478bd9Sstevel@tonic-gate  * recurse on its restarter and the dependencies.  To avoid duplication of
10007c478bd9Sstevel@tonic-gate  * effort or looping around a dependency cycle, each FMRI is entered into the
10017c478bd9Sstevel@tonic-gate  * "visited" hash table.  While recursing, the hash table entry is marked
10027c478bd9Sstevel@tonic-gate  * "active", so that if we come upon it again, we know we've hit a cycle.
10037c478bd9Sstevel@tonic-gate  * exclude_all and optional_all dependencies are ignored.  require_any
10047c478bd9Sstevel@tonic-gate  * dependencies are followed only if they comprise a single service; otherwise
10057c478bd9Sstevel@tonic-gate  * the user is warned.
10067c478bd9Sstevel@tonic-gate  *
10077c478bd9Sstevel@tonic-gate  * fmri must point to a writable max_scf_fmri_sz buffer.  Returns EINVAL if fmri
10087c478bd9Sstevel@tonic-gate  * is invalid, E2BIG if fmri identifies a service with multiple instances, ELOOP
10097c478bd9Sstevel@tonic-gate  * on cycle detection, or 0 on success.
10107c478bd9Sstevel@tonic-gate  */
10117c478bd9Sstevel@tonic-gate static int
10127c478bd9Sstevel@tonic-gate enable_fmri_rec(char *fmri, boolean_t temp)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
10157c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
10167c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
10177c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
10187c478bd9Sstevel@tonic-gate 	scf_value_t *v;
10197c478bd9Sstevel@tonic-gate 	scf_iter_t *pg_iter, *val_iter;
10207c478bd9Sstevel@tonic-gate 	scf_type_t ty;
10217c478bd9Sstevel@tonic-gate 	char *buf, *pgname;
10227c478bd9Sstevel@tonic-gate 	ssize_t name_sz, len, sz;
10237c478bd9Sstevel@tonic-gate 	int ret;
10247c478bd9Sstevel@tonic-gate 	struct ht_elt *he;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	len = scf_canonify_fmri(fmri, fmri, max_scf_fmri_sz);
10277c478bd9Sstevel@tonic-gate 	if (len < 0) {
10287c478bd9Sstevel@tonic-gate 		assert(scf_error() == SCF_ERROR_INVALID_ARGUMENT);
10297c478bd9Sstevel@tonic-gate 		return (EINVAL);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 	assert(len < max_scf_fmri_sz);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	switch (visited_find_or_add(fmri, &he)) {
10347c478bd9Sstevel@tonic-gate 	case 0:
10357c478bd9Sstevel@tonic-gate 		he->active = B_TRUE;
10367c478bd9Sstevel@tonic-gate 		break;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	case 1:
10397c478bd9Sstevel@tonic-gate 		return (he->active ? ELOOP : 0);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	case -1:
10427c478bd9Sstevel@tonic-gate 		uu_die(emsg_nomem);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	default:
10457c478bd9Sstevel@tonic-gate 		assert(0);
10467c478bd9Sstevel@tonic-gate 		abort();
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
10507c478bd9Sstevel@tonic-gate 	if (inst == NULL)
10517c478bd9Sstevel@tonic-gate 		scfdie();
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	switch (get_inst_mult(fmri, inst)) {
10547c478bd9Sstevel@tonic-gate 	case 0:
10557c478bd9Sstevel@tonic-gate 		break;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	case E2BIG:
10587c478bd9Sstevel@tonic-gate 		he->active = B_FALSE;
10597c478bd9Sstevel@tonic-gate 		return (E2BIG);
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	default:
10627c478bd9Sstevel@tonic-gate 		he->active = B_FALSE;
10637c478bd9Sstevel@tonic-gate 		return (0);
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	set_inst_enabled(fmri, inst, temp, B_TRUE);
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	if ((snap = scf_snapshot_create(h)) == NULL ||
10697c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
10707c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
10717c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL ||
10727c478bd9Sstevel@tonic-gate 	    (pg_iter = scf_iter_create(h)) == NULL ||
10737c478bd9Sstevel@tonic-gate 	    (val_iter = scf_iter_create(h)) == NULL)
10747c478bd9Sstevel@tonic-gate 		scfdie();
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	buf = malloc(max_scf_fmri_sz);
10777c478bd9Sstevel@tonic-gate 	if (buf == NULL)
10787c478bd9Sstevel@tonic-gate 		uu_die(emsg_nomem);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	name_sz = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
10817c478bd9Sstevel@tonic-gate 	if (name_sz < 0)
10827c478bd9Sstevel@tonic-gate 		scfdie();
10837c478bd9Sstevel@tonic-gate 	++name_sz;
10847c478bd9Sstevel@tonic-gate 	pgname = malloc(name_sz);
10857c478bd9Sstevel@tonic-gate 	if (pgname == NULL)
10867c478bd9Sstevel@tonic-gate 		uu_die(emsg_nomem);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	if (scf_instance_get_snapshot(inst, "running", snap) != 0) {
10897c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
10907c478bd9Sstevel@tonic-gate 			scfdie();
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
10937c478bd9Sstevel@tonic-gate 		snap = NULL;
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	/* Enable restarter */
10977c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, SCF_PG_GENERAL, pg) != 0) {
10987c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
10997c478bd9Sstevel@tonic-gate 			scfdie();
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 		uu_warn(gettext("\"%s\" is misconfigured (lacks \"%s\" "
11027c478bd9Sstevel@tonic-gate 		    "property group).\n"), fmri, SCF_PG_GENERAL);
11037c478bd9Sstevel@tonic-gate 		ret = 0;
11047c478bd9Sstevel@tonic-gate 		goto out;
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	sz = get_astring_prop(pg, SCF_PROPERTY_RESTARTER, prop, v, buf,
11087c478bd9Sstevel@tonic-gate 	    max_scf_fmri_sz);
11097c478bd9Sstevel@tonic-gate 	if (sz > max_scf_fmri_sz) {
11107c478bd9Sstevel@tonic-gate 		uu_warn(gettext("\"%s\" is misconfigured (the value of "
11117c478bd9Sstevel@tonic-gate 		    "\"%s/%s\" is too long).\n"), fmri, SCF_PG_GENERAL,
11127c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_RESTARTER);
11137c478bd9Sstevel@tonic-gate 		ret = 0;
11147c478bd9Sstevel@tonic-gate 		goto out;
11157c478bd9Sstevel@tonic-gate 	} else if (sz >= 0) {
11167c478bd9Sstevel@tonic-gate 		switch (enable_fmri_rec(buf, temp)) {
11177c478bd9Sstevel@tonic-gate 		case 0:
11187c478bd9Sstevel@tonic-gate 			break;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 		case EINVAL:
11217c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Restarter FMRI for \"%s\" is "
11227c478bd9Sstevel@tonic-gate 			    "invalid.\n"), fmri);
11237c478bd9Sstevel@tonic-gate 			break;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 		case E2BIG:
11267c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Restarter FMRI for \"%s\" identifies "
11277c478bd9Sstevel@tonic-gate 			    "a service with multiple instances.\n"), fmri);
11287c478bd9Sstevel@tonic-gate 			break;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 		case ELOOP:
11317c478bd9Sstevel@tonic-gate 			ret = ELOOP;
11327c478bd9Sstevel@tonic-gate 			goto out;
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 		default:
11357c478bd9Sstevel@tonic-gate 			assert(0);
11367c478bd9Sstevel@tonic-gate 			abort();
11377c478bd9Sstevel@tonic-gate 		}
11387c478bd9Sstevel@tonic-gate 	} else if (sz < 0) {
11397c478bd9Sstevel@tonic-gate 		switch (-sz) {
11407c478bd9Sstevel@tonic-gate 		case ENOENT:
11417c478bd9Sstevel@tonic-gate 			break;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 		case E2BIG:
11447c478bd9Sstevel@tonic-gate 			uu_warn(gettext("\"%s\" is misconfigured (\"%s/%s\" "
11457c478bd9Sstevel@tonic-gate 			    "property is not single-valued).\n"), fmri,
11467c478bd9Sstevel@tonic-gate 			    SCF_PG_GENERAL, SCF_PROPERTY_RESTARTER);
11477c478bd9Sstevel@tonic-gate 			ret = 0;
11487c478bd9Sstevel@tonic-gate 			goto out;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 		case EINVAL:
11517c478bd9Sstevel@tonic-gate 			uu_warn(gettext("\"%s\" is misconfigured (\"%s/%s\" "
11527c478bd9Sstevel@tonic-gate 			    "property is not of astring type).\n"), fmri,
11537c478bd9Sstevel@tonic-gate 			    SCF_PG_GENERAL, SCF_PROPERTY_RESTARTER);
11547c478bd9Sstevel@tonic-gate 			ret = 0;
11557c478bd9Sstevel@tonic-gate 			goto out;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 		default:
11587c478bd9Sstevel@tonic-gate 			assert(0);
11597c478bd9Sstevel@tonic-gate 			abort();
11607c478bd9Sstevel@tonic-gate 		}
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	if (scf_iter_instance_pgs_typed_composed(pg_iter, inst, snap,
11647c478bd9Sstevel@tonic-gate 	    SCF_GROUP_DEPENDENCY) == -1)
11657c478bd9Sstevel@tonic-gate 		scfdie();
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	while (scf_iter_next_pg(pg_iter, pg) > 0) {
11687c478bd9Sstevel@tonic-gate 		len = scf_pg_get_name(pg, pgname, name_sz);
11697c478bd9Sstevel@tonic-gate 		if (len < 0)
11707c478bd9Sstevel@tonic-gate 			scfdie();
11717c478bd9Sstevel@tonic-gate 		assert(len < name_sz);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		if (dep_get_astring(fmri, pgname, pg, SCF_PROPERTY_TYPE, prop,
11747c478bd9Sstevel@tonic-gate 		    v, buf, max_scf_fmri_sz) < 0)
11757c478bd9Sstevel@tonic-gate 			continue;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 		if (strcmp(buf, "service") != 0)
11787c478bd9Sstevel@tonic-gate 			continue;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 		if (dep_get_astring(fmri, pgname, pg, SCF_PROPERTY_GROUPING,
11817c478bd9Sstevel@tonic-gate 		    prop, v, buf, max_scf_fmri_sz) < 0)
11827c478bd9Sstevel@tonic-gate 			continue;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 		if (strcmp(buf, SCF_DEP_EXCLUDE_ALL) == 0 ||
11857c478bd9Sstevel@tonic-gate 		    strcmp(buf, SCF_DEP_OPTIONAL_ALL) == 0)
11867c478bd9Sstevel@tonic-gate 			continue;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 		if (strcmp(buf, SCF_DEP_REQUIRE_ALL) != 0 &&
11897c478bd9Sstevel@tonic-gate 		    strcmp(buf, SCF_DEP_REQUIRE_ANY) != 0) {
11907c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Dependency \"%s\" of \"%s\" has "
11917c478bd9Sstevel@tonic-gate 			    "unknown type \"%s\".\n"), pgname, fmri, buf);
11927c478bd9Sstevel@tonic-gate 			continue;
11937c478bd9Sstevel@tonic-gate 		}
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) ==
11967c478bd9Sstevel@tonic-gate 		    -1) {
11977c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
11987c478bd9Sstevel@tonic-gate 				scfdie();
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 			uu_warn(gettext("\"%s\" is misconfigured (\"%s\" "
12017c478bd9Sstevel@tonic-gate 			    "dependency lacks \"%s\" property.)\n"), fmri,
12027c478bd9Sstevel@tonic-gate 			    pgname, SCF_PROPERTY_ENTITIES);
12037c478bd9Sstevel@tonic-gate 			continue;
12047c478bd9Sstevel@tonic-gate 		}
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 		if (scf_property_type(prop, &ty) != SCF_SUCCESS)
12077c478bd9Sstevel@tonic-gate 			scfdie();
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 		if (ty != SCF_TYPE_FMRI) {
12107c478bd9Sstevel@tonic-gate 			uu_warn(gettext("\"%s\" is misconfigured (property "
12117c478bd9Sstevel@tonic-gate 			    "\"%s/%s\" is not of fmri type).\n"), fmri, pgname,
12127c478bd9Sstevel@tonic-gate 			    SCF_PROPERTY_ENTITIES);
12137c478bd9Sstevel@tonic-gate 			continue;
12147c478bd9Sstevel@tonic-gate 		}
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 		if (scf_iter_property_values(val_iter, prop) == -1)
12177c478bd9Sstevel@tonic-gate 			scfdie();
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 		if (strcmp(buf, SCF_DEP_REQUIRE_ANY) == 0) {
12207c478bd9Sstevel@tonic-gate 			if (multiple_instances(val_iter, v, buf)) {
12217c478bd9Sstevel@tonic-gate 				(void) printf(gettext("%s requires one of:\n"),
12227c478bd9Sstevel@tonic-gate 				    fmri);
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 				if (scf_iter_property_values(val_iter, prop) !=
12257c478bd9Sstevel@tonic-gate 				    0)
12267c478bd9Sstevel@tonic-gate 					scfdie();
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 				for (;;) {
12297c478bd9Sstevel@tonic-gate 					int r;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 					r = scf_iter_next_value(val_iter, v);
12327c478bd9Sstevel@tonic-gate 					if (r == 0)
12337c478bd9Sstevel@tonic-gate 						break;
12347c478bd9Sstevel@tonic-gate 					if (r != 1)
12357c478bd9Sstevel@tonic-gate 						scfdie();
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 					if (scf_value_get_astring(v, buf,
12387c478bd9Sstevel@tonic-gate 					    max_scf_fmri_sz) < 0)
12397c478bd9Sstevel@tonic-gate 						scfdie();
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 					(void) fputs("  ", stdout);
12427c478bd9Sstevel@tonic-gate 					(void) puts(buf);
12437c478bd9Sstevel@tonic-gate 				}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 				continue;
12467c478bd9Sstevel@tonic-gate 			}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 			/*
12497c478bd9Sstevel@tonic-gate 			 * Since there's only one instance, we can enable it.
12507c478bd9Sstevel@tonic-gate 			 * Reset val_iter and continue.
12517c478bd9Sstevel@tonic-gate 			 */
12527c478bd9Sstevel@tonic-gate 			if (scf_iter_property_values(val_iter, prop) != 0)
12537c478bd9Sstevel@tonic-gate 				scfdie();
12547c478bd9Sstevel@tonic-gate 		}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 		for (;;) {
12577c478bd9Sstevel@tonic-gate 			ret = scf_iter_next_value(val_iter, v);
12587c478bd9Sstevel@tonic-gate 			if (ret == 0)
12597c478bd9Sstevel@tonic-gate 				break;
12607c478bd9Sstevel@tonic-gate 			if (ret != 1)
12617c478bd9Sstevel@tonic-gate 				scfdie();
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 			if (scf_value_get_astring(v, buf, max_scf_fmri_sz) ==
12647c478bd9Sstevel@tonic-gate 			    -1)
12657c478bd9Sstevel@tonic-gate 				scfdie();
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 			switch (enable_fmri_rec(buf, temp)) {
12687c478bd9Sstevel@tonic-gate 			case 0:
12697c478bd9Sstevel@tonic-gate 				break;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 			case EINVAL:
12727c478bd9Sstevel@tonic-gate 				uu_warn(gettext("\"%s\" dependency of \"%s\" "
12737c478bd9Sstevel@tonic-gate 				    "has invalid FMRI \"%s\".\n"), pgname,
12747c478bd9Sstevel@tonic-gate 				    fmri, buf);
12757c478bd9Sstevel@tonic-gate 				break;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 			case E2BIG:
12787c478bd9Sstevel@tonic-gate 				uu_warn(gettext("%s depends on %s, which has "
12797c478bd9Sstevel@tonic-gate 				    "multiple instances.\n"), fmri, buf);
12807c478bd9Sstevel@tonic-gate 				break;
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 			case ELOOP:
12837c478bd9Sstevel@tonic-gate 				ret = ELOOP;
12847c478bd9Sstevel@tonic-gate 				goto out;
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 			default:
12877c478bd9Sstevel@tonic-gate 				assert(0);
12887c478bd9Sstevel@tonic-gate 				abort();
12897c478bd9Sstevel@tonic-gate 			}
12907c478bd9Sstevel@tonic-gate 		}
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	ret = 0;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate out:
12967c478bd9Sstevel@tonic-gate 	he->active = B_FALSE;
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	free(buf);
12997c478bd9Sstevel@tonic-gate 	free(pgname);
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(v);
13027c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
13037c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
13047c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
13057c478bd9Sstevel@tonic-gate 	scf_iter_destroy(pg_iter);
13067c478bd9Sstevel@tonic-gate 	scf_iter_destroy(val_iter);
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	return (ret);
13097c478bd9Sstevel@tonic-gate }
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate /*
13127c478bd9Sstevel@tonic-gate  * fmri here is only used for verbose messages.
13137c478bd9Sstevel@tonic-gate  */
13147c478bd9Sstevel@tonic-gate static void
13157c478bd9Sstevel@tonic-gate set_inst_action(const char *fmri, const scf_instance_t *inst,
13167c478bd9Sstevel@tonic-gate     const char *action)
13177c478bd9Sstevel@tonic-gate {
13187c478bd9Sstevel@tonic-gate 	scf_transaction_t *tx;
13197c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *ent;
13207c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
13217c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
13227c478bd9Sstevel@tonic-gate 	scf_value_t *v;
13237c478bd9Sstevel@tonic-gate 	int ret;
13247c478bd9Sstevel@tonic-gate 	int64_t t;
13257c478bd9Sstevel@tonic-gate 	hrtime_t timestamp;
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	const char * const scf_pg_restarter_actions = SCF_PG_RESTARTER_ACTIONS;
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	if ((pg = scf_pg_create(h)) == NULL ||
13307c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
13317c478bd9Sstevel@tonic-gate 	    (v = scf_value_create(h)) == NULL ||
13327c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
13337c478bd9Sstevel@tonic-gate 	    (ent = scf_entry_create(h)) == NULL)
13347c478bd9Sstevel@tonic-gate 		scfdie();
13357c478bd9Sstevel@tonic-gate 
1336eb1a3463STruong Nguyen 	if (restarter_setup(fmri, inst))
1337*e8f5b3f5STruong Nguyen 		goto out;
1338eb1a3463STruong Nguyen 
13397c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, scf_pg_restarter_actions, pg) == -1) {
13407c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
13417c478bd9Sstevel@tonic-gate 			scfdie();
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 		/* Try creating the restarter_actions property group. */
13447c478bd9Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, scf_pg_restarter_actions,
13457c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_TYPE,
13467c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER_ACTIONS_FLAGS, pg) == -1) {
13477c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
13487c478bd9Sstevel@tonic-gate 			case SCF_ERROR_EXISTS:
13497c478bd9Sstevel@tonic-gate 				/* Someone must have added it. */
13507c478bd9Sstevel@tonic-gate 				break;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
13537c478bd9Sstevel@tonic-gate 				if (!verbose)
13547c478bd9Sstevel@tonic-gate 					uu_warn(emsg_permission_denied, fmri);
13557c478bd9Sstevel@tonic-gate 				else
13567c478bd9Sstevel@tonic-gate 					uu_warn(emsg_create_pg_perm_denied,
13577c478bd9Sstevel@tonic-gate 					    fmri, scf_pg_restarter_actions);
13587c478bd9Sstevel@tonic-gate 				goto out;
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 			default:
13617c478bd9Sstevel@tonic-gate 				scfdie();
13627c478bd9Sstevel@tonic-gate 			}
13637c478bd9Sstevel@tonic-gate 		}
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	/*
13677c478bd9Sstevel@tonic-gate 	 * If we lose the transaction race and need to retry, there are 2
13687c478bd9Sstevel@tonic-gate 	 * potential other winners:
13697c478bd9Sstevel@tonic-gate 	 *	- another process setting actions
13707c478bd9Sstevel@tonic-gate 	 *	- the restarter marking the action complete
13717c478bd9Sstevel@tonic-gate 	 * Therefore, re-read the property every time through the loop before
13727c478bd9Sstevel@tonic-gate 	 * making any decisions based on their values.
13737c478bd9Sstevel@tonic-gate 	 */
13747c478bd9Sstevel@tonic-gate 	do {
13757c478bd9Sstevel@tonic-gate 		timestamp = gethrtime();
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) == -1) {
13787c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
13797c478bd9Sstevel@tonic-gate 				scfdie();
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 			if (!verbose)
13827c478bd9Sstevel@tonic-gate 				uu_warn(emsg_permission_denied, fmri);
13837c478bd9Sstevel@tonic-gate 			else
13847c478bd9Sstevel@tonic-gate 				uu_warn(emsg_pg_perm_denied, fmri,
13857c478bd9Sstevel@tonic-gate 				    scf_pg_restarter_actions);
13867c478bd9Sstevel@tonic-gate 			goto out;
13877c478bd9Sstevel@tonic-gate 		}
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, action, prop) == -1) {
13907c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
13917c478bd9Sstevel@tonic-gate 				scfdie();
13927c478bd9Sstevel@tonic-gate 			if (scf_transaction_property_new(tx, ent,
13937c478bd9Sstevel@tonic-gate 			    action, SCF_TYPE_INTEGER) == -1)
13947c478bd9Sstevel@tonic-gate 				scfdie();
13957c478bd9Sstevel@tonic-gate 			goto action_set;
13967c478bd9Sstevel@tonic-gate 		} else {
13977c478bd9Sstevel@tonic-gate 			if (scf_transaction_property_change_type(tx, ent,
13987c478bd9Sstevel@tonic-gate 			    action, SCF_TYPE_INTEGER) == -1)
13997c478bd9Sstevel@tonic-gate 				scfdie();
14007c478bd9Sstevel@tonic-gate 		}
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 		if (scf_property_get_value(prop, v) == -1) {
14037c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
14047c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
14057c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
14067c478bd9Sstevel@tonic-gate 				/* Misconfigured, so set anyway. */
14077c478bd9Sstevel@tonic-gate 				goto action_set;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 			default:
14107c478bd9Sstevel@tonic-gate 				scfdie();
14117c478bd9Sstevel@tonic-gate 			}
14127c478bd9Sstevel@tonic-gate 		} else {
14137c478bd9Sstevel@tonic-gate 			if (scf_value_get_integer(v, &t) == -1) {
14147c478bd9Sstevel@tonic-gate 				assert(scf_error() == SCF_ERROR_TYPE_MISMATCH);
14157c478bd9Sstevel@tonic-gate 				goto action_set;
14167c478bd9Sstevel@tonic-gate 			}
14177c478bd9Sstevel@tonic-gate 			if (t > timestamp)
14187c478bd9Sstevel@tonic-gate 				break;
14197c478bd9Sstevel@tonic-gate 		}
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate action_set:
14227c478bd9Sstevel@tonic-gate 		scf_value_set_integer(v, timestamp);
14237c478bd9Sstevel@tonic-gate 		if (scf_entry_add_value(ent, v) == -1)
14247c478bd9Sstevel@tonic-gate 			scfdie();
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 		ret = scf_transaction_commit(tx);
14277c478bd9Sstevel@tonic-gate 		if (ret == -1) {
14287c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
14297c478bd9Sstevel@tonic-gate 				scfdie();
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 			if (!verbose)
14327c478bd9Sstevel@tonic-gate 				uu_warn(emsg_permission_denied, fmri);
14337c478bd9Sstevel@tonic-gate 			else
14347c478bd9Sstevel@tonic-gate 				uu_warn(emsg_prop_perm_denied, fmri,
14357c478bd9Sstevel@tonic-gate 				    scf_pg_restarter_actions, action);
14367c478bd9Sstevel@tonic-gate 			scf_transaction_reset(tx);
14377c478bd9Sstevel@tonic-gate 			goto out;
14387c478bd9Sstevel@tonic-gate 		}
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 		scf_transaction_reset(tx);
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 		if (ret == 0) {
14437c478bd9Sstevel@tonic-gate 			if (scf_pg_update(pg) == -1)
14447c478bd9Sstevel@tonic-gate 				scfdie();
14457c478bd9Sstevel@tonic-gate 		}
14467c478bd9Sstevel@tonic-gate 	} while (ret == 0);
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if (verbose)
14497c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Action %s set for %s.\n"), action, fmri);
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate out:
14527c478bd9Sstevel@tonic-gate 	scf_value_destroy(v);
14537c478bd9Sstevel@tonic-gate 	scf_entry_destroy(ent);
14547c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
14557c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
14567c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate /*
14607c478bd9Sstevel@tonic-gate  * Get the state of inst.  state should point to a buffer of
14617c478bd9Sstevel@tonic-gate  * MAX_SCF_STATE_STRING_SZ bytes.  Returns 0 on success or -1 if
14627c478bd9Sstevel@tonic-gate  *   no restarter property group
14637c478bd9Sstevel@tonic-gate  *   no state property
14647c478bd9Sstevel@tonic-gate  *   state property is misconfigured (wrong type, not single-valued)
14657c478bd9Sstevel@tonic-gate  *   state value is too long
14667c478bd9Sstevel@tonic-gate  * In these cases, fmri is used to print a warning.
14677c478bd9Sstevel@tonic-gate  *
14687c478bd9Sstevel@tonic-gate  * If pgp is non-NULL, a successful call to inst_get_state will store
14697c478bd9Sstevel@tonic-gate  * the SCF_PG_RESTARTER property group in *pgp, and the caller will be
14707c478bd9Sstevel@tonic-gate  * responsible for calling scf_pg_destroy on the property group.
14717c478bd9Sstevel@tonic-gate  */
14727c478bd9Sstevel@tonic-gate int
14737c478bd9Sstevel@tonic-gate inst_get_state(scf_instance_t *inst, char *state, const char *fmri,
14747c478bd9Sstevel@tonic-gate     scf_propertygroup_t **pgp)
14757c478bd9Sstevel@tonic-gate {
14767c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
14777c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
14787c478bd9Sstevel@tonic-gate 	scf_value_t *val;
14797c478bd9Sstevel@tonic-gate 	int ret = -1;
14807c478bd9Sstevel@tonic-gate 	ssize_t szret;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	if ((pg = scf_pg_create(h)) == NULL ||
14837c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
14847c478bd9Sstevel@tonic-gate 	    (val = scf_value_create(h)) == NULL)
14857c478bd9Sstevel@tonic-gate 		scfdie();
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != SCF_SUCCESS) {
14887c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
14897c478bd9Sstevel@tonic-gate 			scfdie();
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 		uu_warn(gettext("%s is misconfigured (lacks \"%s\" property "
14927c478bd9Sstevel@tonic-gate 		    "group).\n"), fmri ? fmri : inst_get_fmri(inst),
14937c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER);
14947c478bd9Sstevel@tonic-gate 		goto out;
14957c478bd9Sstevel@tonic-gate 	}
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	szret = get_astring_prop(pg, SCF_PROPERTY_STATE, prop, val, state,
14987c478bd9Sstevel@tonic-gate 	    MAX_SCF_STATE_STRING_SZ);
14997c478bd9Sstevel@tonic-gate 	if (szret < 0) {
15007c478bd9Sstevel@tonic-gate 		switch (-szret) {
15017c478bd9Sstevel@tonic-gate 		case ENOENT:
15027c478bd9Sstevel@tonic-gate 			uu_warn(gettext("%s is misconfigured (\"%s\" property "
15037c478bd9Sstevel@tonic-gate 			    "group lacks \"%s\" property).\n"),
15047c478bd9Sstevel@tonic-gate 			    fmri ? fmri : inst_get_fmri(inst), SCF_PG_RESTARTER,
15057c478bd9Sstevel@tonic-gate 			    SCF_PROPERTY_STATE);
15067c478bd9Sstevel@tonic-gate 			goto out;
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 		case E2BIG:
15097c478bd9Sstevel@tonic-gate 			uu_warn(gettext("%s is misconfigured (\"%s/%s\" "
15107c478bd9Sstevel@tonic-gate 			    "property is not single-valued).\n"),
15117c478bd9Sstevel@tonic-gate 			    fmri ? fmri : inst_get_fmri(inst), SCF_PG_RESTARTER,
15127c478bd9Sstevel@tonic-gate 			    SCF_PROPERTY_STATE);
15137c478bd9Sstevel@tonic-gate 			goto out;
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 		case EINVAL:
15167c478bd9Sstevel@tonic-gate 			uu_warn(gettext("%s is misconfigured (\"%s/%s\" "
15177c478bd9Sstevel@tonic-gate 			    "property is not of type astring).\n"),
15187c478bd9Sstevel@tonic-gate 			    fmri ? fmri : inst_get_fmri(inst), SCF_PG_RESTARTER,
15197c478bd9Sstevel@tonic-gate 			    SCF_PROPERTY_STATE);
15207c478bd9Sstevel@tonic-gate 			goto out;
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 		default:
15237c478bd9Sstevel@tonic-gate 			assert(0);
15247c478bd9Sstevel@tonic-gate 			abort();
15257c478bd9Sstevel@tonic-gate 		}
15267c478bd9Sstevel@tonic-gate 	}
15277c478bd9Sstevel@tonic-gate 	if (szret >= MAX_SCF_STATE_STRING_SZ) {
15287c478bd9Sstevel@tonic-gate 		uu_warn(gettext("%s is misconfigured (\"%s/%s\" property value "
15297c478bd9Sstevel@tonic-gate 		    "is too long).\n"), fmri ? fmri : inst_get_fmri(inst),
15307c478bd9Sstevel@tonic-gate 		    SCF_PG_RESTARTER, SCF_PROPERTY_STATE);
15317c478bd9Sstevel@tonic-gate 		goto out;
15327c478bd9Sstevel@tonic-gate 	}
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	ret = 0;
15357c478bd9Sstevel@tonic-gate 	if (pgp)
15367c478bd9Sstevel@tonic-gate 		*pgp = pg;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate out:
15397c478bd9Sstevel@tonic-gate 	(void) scf_value_destroy(val);
15407c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
15417c478bd9Sstevel@tonic-gate 	if (ret || pgp == NULL)
15427c478bd9Sstevel@tonic-gate 		scf_pg_destroy(pg);
15437c478bd9Sstevel@tonic-gate 	return (ret);
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate static void
15477c478bd9Sstevel@tonic-gate set_astring_prop(const char *fmri, const char *pgname, const char *pgtype,
15487c478bd9Sstevel@tonic-gate     uint32_t pgflags, const char *propname, const char *str)
15497c478bd9Sstevel@tonic-gate {
15507c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
15517c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
15527c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
15537c478bd9Sstevel@tonic-gate 	scf_value_t *val;
15547c478bd9Sstevel@tonic-gate 	scf_transaction_t *tx;
15557c478bd9Sstevel@tonic-gate 	scf_transaction_entry_t *txent;
15567c478bd9Sstevel@tonic-gate 	int ret;
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
15597c478bd9Sstevel@tonic-gate 	if (inst == NULL)
15607c478bd9Sstevel@tonic-gate 		scfdie();
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	if (get_inst(fmri, inst) != 0)
15637c478bd9Sstevel@tonic-gate 		return;
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	if ((pg = scf_pg_create(h)) == NULL ||
15667c478bd9Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
15677c478bd9Sstevel@tonic-gate 	    (val = scf_value_create(h)) == NULL ||
15687c478bd9Sstevel@tonic-gate 	    (tx = scf_transaction_create(h)) == NULL ||
15697c478bd9Sstevel@tonic-gate 	    (txent = scf_entry_create(h)) == NULL)
15707c478bd9Sstevel@tonic-gate 		scfdie();
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, pgname, pg) != SCF_SUCCESS) {
15737c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
15747c478bd9Sstevel@tonic-gate 			scfdie();
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 		if (scf_instance_add_pg(inst, pgname, pgtype, pgflags, pg) !=
15777c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS) {
15787c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
15797c478bd9Sstevel@tonic-gate 			case SCF_ERROR_EXISTS:
15807c478bd9Sstevel@tonic-gate 				if (scf_instance_get_pg(inst, pgname, pg) !=
15817c478bd9Sstevel@tonic-gate 				    SCF_SUCCESS) {
15827c478bd9Sstevel@tonic-gate 					if (scf_error() != SCF_ERROR_NOT_FOUND)
15837c478bd9Sstevel@tonic-gate 						scfdie();
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 					uu_warn(gettext("Repository write "
15867c478bd9Sstevel@tonic-gate 					    "contention.\n"));
15877c478bd9Sstevel@tonic-gate 					goto out;
15887c478bd9Sstevel@tonic-gate 				}
15897c478bd9Sstevel@tonic-gate 				break;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
15927c478bd9Sstevel@tonic-gate 				if (!verbose)
15937c478bd9Sstevel@tonic-gate 					uu_warn(emsg_permission_denied, fmri);
15947c478bd9Sstevel@tonic-gate 				else
15957c478bd9Sstevel@tonic-gate 					uu_warn(emsg_create_pg_perm_denied,
15967c478bd9Sstevel@tonic-gate 					    fmri, pgname);
15977c478bd9Sstevel@tonic-gate 				goto out;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 			default:
16007c478bd9Sstevel@tonic-gate 				scfdie();
16017c478bd9Sstevel@tonic-gate 			}
16027c478bd9Sstevel@tonic-gate 		}
16037c478bd9Sstevel@tonic-gate 	}
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	do {
16067c478bd9Sstevel@tonic-gate 		if (scf_transaction_start(tx, pg) != SCF_SUCCESS) {
16077c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
16087c478bd9Sstevel@tonic-gate 				scfdie();
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 			if (!verbose)
16117c478bd9Sstevel@tonic-gate 				uu_warn(emsg_permission_denied, fmri);
16127c478bd9Sstevel@tonic-gate 			else
16137c478bd9Sstevel@tonic-gate 				uu_warn(emsg_pg_perm_denied, fmri, pgname);
16147c478bd9Sstevel@tonic-gate 			goto out;
16157c478bd9Sstevel@tonic-gate 		}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, txent, propname,
16187c478bd9Sstevel@tonic-gate 		    SCF_TYPE_ASTRING) != 0) {
16197c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_NOT_FOUND)
16207c478bd9Sstevel@tonic-gate 				scfdie();
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 			if (scf_transaction_property_new(tx, txent, propname,
16237c478bd9Sstevel@tonic-gate 			    SCF_TYPE_ASTRING) != 0)
16247c478bd9Sstevel@tonic-gate 				scfdie();
16257c478bd9Sstevel@tonic-gate 		}
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 		if (scf_value_set_astring(val, str) != SCF_SUCCESS)
16287c478bd9Sstevel@tonic-gate 			scfdie();
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 		if (scf_entry_add_value(txent, val) != SCF_SUCCESS)
16317c478bd9Sstevel@tonic-gate 			scfdie();
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 		ret = scf_transaction_commit(tx);
16347c478bd9Sstevel@tonic-gate 		if (ret == -1) {
16357c478bd9Sstevel@tonic-gate 			if (scf_error() != SCF_ERROR_PERMISSION_DENIED)
16367c478bd9Sstevel@tonic-gate 				scfdie();
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 			if (!verbose)
16397c478bd9Sstevel@tonic-gate 				uu_warn(emsg_permission_denied, fmri);
16407c478bd9Sstevel@tonic-gate 			else
16417c478bd9Sstevel@tonic-gate 				uu_warn(emsg_prop_perm_denied, fmri, pgname,
16427c478bd9Sstevel@tonic-gate 				    propname);
16437c478bd9Sstevel@tonic-gate 			goto out;
16447c478bd9Sstevel@tonic-gate 		}
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 		if (ret == 0) {
16477c478bd9Sstevel@tonic-gate 			scf_transaction_reset(tx);
16487c478bd9Sstevel@tonic-gate 
1649bc9767f9Stn 			if (scf_pg_update(pg) == -1)
16507c478bd9Sstevel@tonic-gate 				scfdie();
16517c478bd9Sstevel@tonic-gate 		}
16527c478bd9Sstevel@tonic-gate 	} while (ret == 0);
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate out:
16557c478bd9Sstevel@tonic-gate 	scf_transaction_destroy(tx);
16567c478bd9Sstevel@tonic-gate 	scf_entry_destroy(txent);
16577c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
16587c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
16597c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
16607c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
16617c478bd9Sstevel@tonic-gate }
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate /*
16657c478bd9Sstevel@tonic-gate  * Flags to control enable and disable actions.
16667c478bd9Sstevel@tonic-gate  */
16677c478bd9Sstevel@tonic-gate #define	SET_ENABLED	0x1
16687c478bd9Sstevel@tonic-gate #define	SET_TEMPORARY	0x2
16697c478bd9Sstevel@tonic-gate #define	SET_RECURSIVE	0x4
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate static int
16727c478bd9Sstevel@tonic-gate set_fmri_enabled(void *data, scf_walkinfo_t *wip)
16737c478bd9Sstevel@tonic-gate {
16747c478bd9Sstevel@tonic-gate 	int flags = (int)data;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	assert(wip->inst != NULL);
16777c478bd9Sstevel@tonic-gate 	assert(wip->pg == NULL);
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	if (flags & SET_RECURSIVE) {
16807c478bd9Sstevel@tonic-gate 		char *fmri_buf = malloc(max_scf_fmri_sz);
16817c478bd9Sstevel@tonic-gate 		if (fmri_buf == NULL)
16827c478bd9Sstevel@tonic-gate 			uu_die(emsg_nomem);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 		visited = calloc(HT_BUCKETS, sizeof (*visited));
16857c478bd9Sstevel@tonic-gate 		if (visited == NULL)
16867c478bd9Sstevel@tonic-gate 			uu_die(emsg_nomem);
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 		/* scf_walk_fmri() guarantees that fmri isn't too long */
16897c478bd9Sstevel@tonic-gate 		assert(strlen(wip->fmri) <= max_scf_fmri_sz);
16907c478bd9Sstevel@tonic-gate 		(void) strlcpy(fmri_buf, wip->fmri, max_scf_fmri_sz);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 		switch (enable_fmri_rec(fmri_buf, (flags & SET_TEMPORARY))) {
16937c478bd9Sstevel@tonic-gate 		case E2BIG:
16947c478bd9Sstevel@tonic-gate 			uu_warn(gettext("operation on service %s is ambiguous; "
16957c478bd9Sstevel@tonic-gate 			    "instance specification needed.\n"), fmri_buf);
16967c478bd9Sstevel@tonic-gate 			break;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 		case ELOOP:
16997c478bd9Sstevel@tonic-gate 			uu_warn(gettext("%s: Dependency cycle detected.\n"),
17007c478bd9Sstevel@tonic-gate 			    fmri_buf);
17017c478bd9Sstevel@tonic-gate 		}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 		free(visited);
17047c478bd9Sstevel@tonic-gate 		free(fmri_buf);
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	} else {
17077c478bd9Sstevel@tonic-gate 		set_inst_enabled(wip->fmri, wip->inst,
17087c478bd9Sstevel@tonic-gate 		    (flags & SET_TEMPORARY) != 0, (flags & SET_ENABLED) != 0);
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	return (0);
17127c478bd9Sstevel@tonic-gate }
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate /* ARGSUSED */
17157c478bd9Sstevel@tonic-gate static int
17167c478bd9Sstevel@tonic-gate wait_fmri_enabled(void *data, scf_walkinfo_t *wip)
17177c478bd9Sstevel@tonic-gate {
17187c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
17197c478bd9Sstevel@tonic-gate 	char state[MAX_SCF_STATE_STRING_SZ];
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	assert(wip->inst != NULL);
17227c478bd9Sstevel@tonic-gate 	assert(wip->pg == NULL);
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	do {
17257c478bd9Sstevel@tonic-gate 		if (pg)
17267c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
17277c478bd9Sstevel@tonic-gate 		if (inst_get_state(wip->inst, state, wip->fmri, &pg) != 0) {
17287c478bd9Sstevel@tonic-gate 			exit_status = EXIT_SVC_FAILURE;
17297c478bd9Sstevel@tonic-gate 			return (0);
17307c478bd9Sstevel@tonic-gate 		}
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 		if (strcmp(state, SCF_STATE_STRING_ONLINE) == 0 ||
17337c478bd9Sstevel@tonic-gate 		    strcmp(state, SCF_STATE_STRING_DEGRADED) == 0) {
17347c478bd9Sstevel@tonic-gate 			/*
17357c478bd9Sstevel@tonic-gate 			 * We're done.
17367c478bd9Sstevel@tonic-gate 			 */
17377c478bd9Sstevel@tonic-gate 			goto out;
17387c478bd9Sstevel@tonic-gate 		}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 		if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
17417c478bd9Sstevel@tonic-gate 			/*
17427c478bd9Sstevel@tonic-gate 			 * The service is ill.
17437c478bd9Sstevel@tonic-gate 			 */
17447c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Instance \"%s\" is in maintenance"
17457c478bd9Sstevel@tonic-gate 			    " state.\n"), wip->fmri);
17467c478bd9Sstevel@tonic-gate 			exit_status = EXIT_SVC_FAILURE;
17477c478bd9Sstevel@tonic-gate 			goto out;
17487c478bd9Sstevel@tonic-gate 		}
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 		if (!is_enabled(wip->inst)) {
17517c478bd9Sstevel@tonic-gate 			/*
17527c478bd9Sstevel@tonic-gate 			 * Someone stepped in and disabled the service.
17537c478bd9Sstevel@tonic-gate 			 */
17547c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Instance \"%s\" has been disabled"
17557c478bd9Sstevel@tonic-gate 			    " by another entity.\n"), wip->fmri);
17567c478bd9Sstevel@tonic-gate 			exit_status = EXIT_SVC_FAILURE;
17577c478bd9Sstevel@tonic-gate 			goto out;
17587c478bd9Sstevel@tonic-gate 		}
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 		if (!has_potential(wip->inst, B_FALSE)) {
17617c478bd9Sstevel@tonic-gate 			/*
17627c478bd9Sstevel@tonic-gate 			 * Our dependencies aren't met.  We'll never
17637c478bd9Sstevel@tonic-gate 			 * amount to anything.
17647c478bd9Sstevel@tonic-gate 			 */
17657c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Instance \"%s\" has unsatisfied"
17667c478bd9Sstevel@tonic-gate 			    " dependencies.\n"), wip->fmri);
17677c478bd9Sstevel@tonic-gate 			/*
17687c478bd9Sstevel@tonic-gate 			 * EXIT_SVC_FAILURE takes precedence over
17697c478bd9Sstevel@tonic-gate 			 * EXIT_DEP_FAILURE
17707c478bd9Sstevel@tonic-gate 			 */
17717c478bd9Sstevel@tonic-gate 			if (exit_status == 0)
17727c478bd9Sstevel@tonic-gate 				exit_status = EXIT_DEP_FAILURE;
17737c478bd9Sstevel@tonic-gate 			goto out;
17747c478bd9Sstevel@tonic-gate 		}
17757c478bd9Sstevel@tonic-gate 	} while (_scf_pg_wait(pg, WAIT_INTERVAL) >= 0);
17767c478bd9Sstevel@tonic-gate 	scfdie();
17777c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate out:
17807c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
17817c478bd9Sstevel@tonic-gate 	return (0);
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate /* ARGSUSED */
17857c478bd9Sstevel@tonic-gate static int
17867c478bd9Sstevel@tonic-gate wait_fmri_disabled(void *data, scf_walkinfo_t *wip)
17877c478bd9Sstevel@tonic-gate {
17887c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
17897c478bd9Sstevel@tonic-gate 	char state[MAX_SCF_STATE_STRING_SZ];
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	assert(wip->inst != NULL);
17927c478bd9Sstevel@tonic-gate 	assert(wip->pg == NULL);
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	do {
17957c478bd9Sstevel@tonic-gate 		if (pg)
17967c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
17977c478bd9Sstevel@tonic-gate 		if (inst_get_state(wip->inst, state, wip->fmri, &pg) != 0) {
17987c478bd9Sstevel@tonic-gate 			exit_status = EXIT_SVC_FAILURE;
17997c478bd9Sstevel@tonic-gate 			return (0);
18007c478bd9Sstevel@tonic-gate 		}
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 		if (strcmp(state, SCF_STATE_STRING_DISABLED) == 0) {
18037c478bd9Sstevel@tonic-gate 			/*
18047c478bd9Sstevel@tonic-gate 			 * We're done.
18057c478bd9Sstevel@tonic-gate 			 */
18067c478bd9Sstevel@tonic-gate 			goto out;
18077c478bd9Sstevel@tonic-gate 		}
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 		if (is_enabled(wip->inst)) {
18107c478bd9Sstevel@tonic-gate 			/*
18117c478bd9Sstevel@tonic-gate 			 * Someone stepped in and enabled the service.
18127c478bd9Sstevel@tonic-gate 			 */
18137c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Instance \"%s\" has been enabled"
18147c478bd9Sstevel@tonic-gate 			    " by another entity.\n"), wip->fmri);
18157c478bd9Sstevel@tonic-gate 			exit_status = EXIT_SVC_FAILURE;
18167c478bd9Sstevel@tonic-gate 			goto out;
18177c478bd9Sstevel@tonic-gate 		}
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 		if (!has_potential(wip->inst, B_TRUE)) {
18207c478bd9Sstevel@tonic-gate 			/*
18217c478bd9Sstevel@tonic-gate 			 * Our restarter is hopeless.
18227c478bd9Sstevel@tonic-gate 			 */
18237c478bd9Sstevel@tonic-gate 			uu_warn(gettext("Restarter for instance \"%s\" is"
18247c478bd9Sstevel@tonic-gate 			    " unavailable.\n"), wip->fmri);
18257c478bd9Sstevel@tonic-gate 			/*
18267c478bd9Sstevel@tonic-gate 			 * EXIT_SVC_FAILURE takes precedence over
18277c478bd9Sstevel@tonic-gate 			 * EXIT_DEP_FAILURE
18287c478bd9Sstevel@tonic-gate 			 */
18297c478bd9Sstevel@tonic-gate 			if (exit_status == 0)
18307c478bd9Sstevel@tonic-gate 				exit_status = EXIT_DEP_FAILURE;
18317c478bd9Sstevel@tonic-gate 			goto out;
18327c478bd9Sstevel@tonic-gate 		}
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	} while (_scf_pg_wait(pg, WAIT_INTERVAL) >= 0);
18357c478bd9Sstevel@tonic-gate 	scfdie();
18367c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate out:
18397c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
18407c478bd9Sstevel@tonic-gate 	return (0);
18417c478bd9Sstevel@tonic-gate }
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate /* ARGSUSED */
18447c478bd9Sstevel@tonic-gate static int
18457c478bd9Sstevel@tonic-gate clear_instance(void *data, scf_walkinfo_t *wip)
18467c478bd9Sstevel@tonic-gate {
18477c478bd9Sstevel@tonic-gate 	char state[MAX_SCF_STATE_STRING_SZ];
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	assert(wip->inst != NULL);
18507c478bd9Sstevel@tonic-gate 	assert(wip->pg == NULL);
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	if (inst_get_state(wip->inst, state, wip->fmri, NULL) != 0)
18537c478bd9Sstevel@tonic-gate 		return (0);
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_MAINT) == 0) {
18567c478bd9Sstevel@tonic-gate 		set_inst_action(wip->fmri, wip->inst, SCF_PROPERTY_MAINT_OFF);
18577c478bd9Sstevel@tonic-gate 	} else if (strcmp(state, SCF_STATE_STRING_DEGRADED) ==
18587c478bd9Sstevel@tonic-gate 	    0) {
18597c478bd9Sstevel@tonic-gate 		set_inst_action(wip->fmri, wip->inst, SCF_PROPERTY_RESTORE);
18607c478bd9Sstevel@tonic-gate 	} else {
18617c478bd9Sstevel@tonic-gate 		uu_warn(gettext("Instance \"%s\" is not in a "
18627c478bd9Sstevel@tonic-gate 		    "maintenance or degraded state.\n"), wip->fmri);
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 		exit_status = 1;
18657c478bd9Sstevel@tonic-gate 	}
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	return (0);
18687c478bd9Sstevel@tonic-gate }
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate static int
18717c478bd9Sstevel@tonic-gate set_fmri_action(void *action, scf_walkinfo_t *wip)
18727c478bd9Sstevel@tonic-gate {
18737c478bd9Sstevel@tonic-gate 	assert(wip->inst != NULL && wip->pg == NULL);
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	set_inst_action(wip->fmri, wip->inst, action);
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	return (0);
18787c478bd9Sstevel@tonic-gate }
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate /*
18817c478bd9Sstevel@tonic-gate  * Flags to control 'mark' action.
18827c478bd9Sstevel@tonic-gate  */
18837c478bd9Sstevel@tonic-gate #define	MARK_IMMEDIATE	0x1
18847c478bd9Sstevel@tonic-gate #define	MARK_TEMPORARY	0x2
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate static int
18877c478bd9Sstevel@tonic-gate force_degraded(void *data, scf_walkinfo_t *wip)
18887c478bd9Sstevel@tonic-gate {
18897c478bd9Sstevel@tonic-gate 	int flags = (int)data;
18907c478bd9Sstevel@tonic-gate 	char state[MAX_SCF_STATE_STRING_SZ];
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 	if (inst_get_state(wip->inst, state, wip->fmri, NULL) != 0) {
18937c478bd9Sstevel@tonic-gate 		exit_status = 1;
18947c478bd9Sstevel@tonic-gate 		return (0);
18957c478bd9Sstevel@tonic-gate 	}
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	if (strcmp(state, SCF_STATE_STRING_ONLINE) != 0) {
18987c478bd9Sstevel@tonic-gate 		uu_warn(gettext("Instance \"%s\" is not online.\n"), wip->fmri);
18997c478bd9Sstevel@tonic-gate 		exit_status = 1;
19007c478bd9Sstevel@tonic-gate 		return (0);
19017c478bd9Sstevel@tonic-gate 	}
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	set_inst_action(wip->fmri, wip->inst, (flags & MARK_IMMEDIATE) ?
19047c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_DEGRADE_IMMEDIATE : SCF_PROPERTY_DEGRADED);
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	return (0);
19077c478bd9Sstevel@tonic-gate }
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate static int
19107c478bd9Sstevel@tonic-gate force_maintenance(void *data, scf_walkinfo_t *wip)
19117c478bd9Sstevel@tonic-gate {
19127c478bd9Sstevel@tonic-gate 	int flags = (int)data;
19137c478bd9Sstevel@tonic-gate 	const char *prop;
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	if (flags & MARK_IMMEDIATE) {
19167c478bd9Sstevel@tonic-gate 		prop = (flags & MARK_TEMPORARY) ?
19177c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_IMMTEMP :
19187c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_IMMEDIATE;
19197c478bd9Sstevel@tonic-gate 	} else {
19207c478bd9Sstevel@tonic-gate 		prop = (flags & MARK_TEMPORARY) ?
19217c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON_TEMPORARY :
19227c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MAINT_ON;
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	set_inst_action(wip->fmri, wip->inst, prop);
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	return (0);
19287c478bd9Sstevel@tonic-gate }
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate static void
19317c478bd9Sstevel@tonic-gate set_milestone(const char *fmri, boolean_t temporary)
19327c478bd9Sstevel@tonic-gate {
19337c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
19347c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
19357c478bd9Sstevel@tonic-gate 	int r;
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	if (temporary) {
19387c478bd9Sstevel@tonic-gate 		set_astring_prop(SCF_SERVICE_STARTD, SCF_PG_OPTIONS_OVR,
19397c478bd9Sstevel@tonic-gate 		    SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS,
19407c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MILESTONE, fmri);
19417c478bd9Sstevel@tonic-gate 		return;
19427c478bd9Sstevel@tonic-gate 	}
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	if ((inst = scf_instance_create(h)) == NULL ||
19457c478bd9Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL)
19467c478bd9Sstevel@tonic-gate 		scfdie();
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	if (get_inst(SCF_SERVICE_STARTD, inst) != 0) {
19497c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
19507c478bd9Sstevel@tonic-gate 		return;
19517c478bd9Sstevel@tonic-gate 	}
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	/*
19547c478bd9Sstevel@tonic-gate 	 * Set the persistent milestone before deleting the override so we don't
19557c478bd9Sstevel@tonic-gate 	 * glitch.
19567c478bd9Sstevel@tonic-gate 	 */
19577c478bd9Sstevel@tonic-gate 	set_astring_prop(SCF_SERVICE_STARTD, SCF_PG_OPTIONS,
19587c478bd9Sstevel@tonic-gate 	    SCF_PG_OPTIONS_TYPE, SCF_PG_OPTIONS_FLAGS, SCF_PROPERTY_MILESTONE,
19597c478bd9Sstevel@tonic-gate 	    fmri);
19607c478bd9Sstevel@tonic-gate 
1961eb1a3463STruong Nguyen 	r = scf_instance_delete_prop(inst, SCF_PG_OPTIONS_OVR,
1962eb1a3463STruong Nguyen 	    SCF_PROPERTY_MILESTONE);
1963eb1a3463STruong Nguyen 	switch (r) {
1964eb1a3463STruong Nguyen 	case 0:
1965eb1a3463STruong Nguyen 		break;
19667c478bd9Sstevel@tonic-gate 
1967eb1a3463STruong Nguyen 	case ECANCELED:
1968eb1a3463STruong Nguyen 		uu_warn(emsg_no_service, fmri);
1969eb1a3463STruong Nguyen 		exit_status = 1;
1970eb1a3463STruong Nguyen 		goto out;
19717c478bd9Sstevel@tonic-gate 
1972eb1a3463STruong Nguyen 	case EPERM:
1973eb1a3463STruong Nguyen 		uu_warn(gettext("Could not delete %s/%s property of "
1974eb1a3463STruong Nguyen 		    "%s: permission denied.\n"), SCF_PG_OPTIONS_OVR,
1975eb1a3463STruong Nguyen 		    SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD);
1976eb1a3463STruong Nguyen 		exit_status = 1;
1977eb1a3463STruong Nguyen 		goto out;
19787c478bd9Sstevel@tonic-gate 
1979eb1a3463STruong Nguyen 	case EACCES:
1980eb1a3463STruong Nguyen 		uu_warn(gettext("Could not delete %s/%s property of "
1981eb1a3463STruong Nguyen 		    "%s: access denied.\n"), SCF_PG_OPTIONS_OVR,
1982eb1a3463STruong Nguyen 		    SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD);
1983eb1a3463STruong Nguyen 		exit_status = 1;
1984eb1a3463STruong Nguyen 		goto out;
19857c478bd9Sstevel@tonic-gate 
1986eb1a3463STruong Nguyen 	case EROFS:
1987eb1a3463STruong Nguyen 		uu_warn(gettext("Could not delete %s/%s property of "
1988eb1a3463STruong Nguyen 		    "%s: backend read-only.\n"), SCF_PG_OPTIONS_OVR,
1989eb1a3463STruong Nguyen 		    SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD);
1990eb1a3463STruong Nguyen 		exit_status = 1;
1991eb1a3463STruong Nguyen 		goto out;
19927c478bd9Sstevel@tonic-gate 
1993eb1a3463STruong Nguyen 	default:
1994eb1a3463STruong Nguyen 		bad_error("scf_instance_delete_prop", r);
19957c478bd9Sstevel@tonic-gate 	}
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate out:
19987c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
19997c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
20007c478bd9Sstevel@tonic-gate }
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate static char const *milestones[] = {
20037c478bd9Sstevel@tonic-gate 	SCF_MILESTONE_SINGLE_USER,
20047c478bd9Sstevel@tonic-gate 	SCF_MILESTONE_MULTI_USER,
20057c478bd9Sstevel@tonic-gate 	SCF_MILESTONE_MULTI_USER_SERVER,
20067c478bd9Sstevel@tonic-gate 	NULL
20077c478bd9Sstevel@tonic-gate };
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate static void
20100b5c9250Shg usage_milestone(void)
20117c478bd9Sstevel@tonic-gate {
20127c478bd9Sstevel@tonic-gate 	const char **ms;
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
20157c478bd9Sstevel@tonic-gate 	"Usage: svcadm milestone [-d] <milestone>\n\n"
20167c478bd9Sstevel@tonic-gate 	"\t-d\tmake the specified milestone the default for system boot\n\n"
20177c478bd9Sstevel@tonic-gate 	"\tMilestones can be specified using an FMRI or abbreviation.\n"
20187c478bd9Sstevel@tonic-gate 	"\tThe major milestones are as follows:\n\n"
20197c478bd9Sstevel@tonic-gate 	"\tall\n"
20207c478bd9Sstevel@tonic-gate 	"\tnone\n"));
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	for (ms = milestones; *ms != NULL; ms++)
20237c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\t%s\n", *ms);
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
20267c478bd9Sstevel@tonic-gate }
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate static const char *
20297c478bd9Sstevel@tonic-gate validate_milestone(const char *milestone)
20307c478bd9Sstevel@tonic-gate {
20317c478bd9Sstevel@tonic-gate 	const char **ms;
20327c478bd9Sstevel@tonic-gate 	const char *tmp;
20337c478bd9Sstevel@tonic-gate 	size_t len;
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 	if (strcmp(milestone, "all") == 0)
20367c478bd9Sstevel@tonic-gate 		return (milestone);
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	if (strcmp(milestone, "none") == 0)
20397c478bd9Sstevel@tonic-gate 		return (milestone);
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	/*
20427c478bd9Sstevel@tonic-gate 	 * Determine if this is a full or partial milestone
20437c478bd9Sstevel@tonic-gate 	 */
20447c478bd9Sstevel@tonic-gate 	for (ms = milestones; *ms != NULL; ms++) {
20457c478bd9Sstevel@tonic-gate 		if ((tmp = strstr(*ms, milestone)) != NULL) {
20467c478bd9Sstevel@tonic-gate 			len = strlen(milestone);
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 			/*
20497c478bd9Sstevel@tonic-gate 			 * The beginning of the string must align with the start
20507c478bd9Sstevel@tonic-gate 			 * of a milestone fmri, or on the boundary between
20517c478bd9Sstevel@tonic-gate 			 * elements.  The end of the string must align with the
20527c478bd9Sstevel@tonic-gate 			 * end of the milestone, or at the instance boundary.
20537c478bd9Sstevel@tonic-gate 			 */
20547c478bd9Sstevel@tonic-gate 			if ((tmp == *ms || tmp[-1] == '/') &&
20557c478bd9Sstevel@tonic-gate 			    (tmp[len] == '\0' || tmp[len] == ':'))
20567c478bd9Sstevel@tonic-gate 				return (*ms);
20577c478bd9Sstevel@tonic-gate 		}
20587c478bd9Sstevel@tonic-gate 	}
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
20617c478bd9Sstevel@tonic-gate 	    gettext("\"%s\" is not a valid major milestone.\n"), milestone);
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	usage_milestone();
20647c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
20657c478bd9Sstevel@tonic-gate }
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20687c478bd9Sstevel@tonic-gate static void
20697c478bd9Sstevel@tonic-gate quiet(const char *fmt, ...)
20707c478bd9Sstevel@tonic-gate {
20717c478bd9Sstevel@tonic-gate 	/* Do nothing */
20727c478bd9Sstevel@tonic-gate }
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate int
20757c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
20767c478bd9Sstevel@tonic-gate {
20777c478bd9Sstevel@tonic-gate 	int o;
20787c478bd9Sstevel@tonic-gate 	int err;
2079c0889d7aSstevep 	int sw_back;
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
20827c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	(void) uu_setpname(argv[0]);
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	if (argc < 2)
20877c478bd9Sstevel@tonic-gate 		usage();
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	max_scf_fmri_sz = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
20907c478bd9Sstevel@tonic-gate 	if (max_scf_fmri_sz < 0)
20917c478bd9Sstevel@tonic-gate 		scfdie();
20927c478bd9Sstevel@tonic-gate 	++max_scf_fmri_sz;
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	scratch_fmri = malloc(max_scf_fmri_sz);
20957c478bd9Sstevel@tonic-gate 	if (scratch_fmri == NULL)
20967c478bd9Sstevel@tonic-gate 		uu_die(emsg_nomem);
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	h = scf_handle_create(SCF_VERSION);
20997c478bd9Sstevel@tonic-gate 	if (h == NULL)
21007c478bd9Sstevel@tonic-gate 		scfdie();
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	if (scf_handle_bind(h) == -1)
21037c478bd9Sstevel@tonic-gate 		uu_die(gettext("Couldn't bind to svc.configd.\n"));
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	while ((o = getopt(argc, argv, "v")) != -1) {
21067c478bd9Sstevel@tonic-gate 		if (o == 'v')
21077c478bd9Sstevel@tonic-gate 			verbose = 1;
21087c478bd9Sstevel@tonic-gate 		else
21097c478bd9Sstevel@tonic-gate 			usage();
21107c478bd9Sstevel@tonic-gate 	}
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	if (optind >= argc)
21137c478bd9Sstevel@tonic-gate 		usage();
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	emsg_permission_denied = gettext("%s: Permission denied.\n");
21167c478bd9Sstevel@tonic-gate 	emsg_nomem = gettext("Out of memory.\n");
21177c478bd9Sstevel@tonic-gate 	emsg_create_pg_perm_denied = gettext("%s: Couldn't create \"%s\" "
21187c478bd9Sstevel@tonic-gate 	    "property group (permission denied).\n");
21197c478bd9Sstevel@tonic-gate 	emsg_pg_perm_denied = gettext("%s: Couldn't modify \"%s\" property "
21207c478bd9Sstevel@tonic-gate 	    "group (permission denied).\n");
21217c478bd9Sstevel@tonic-gate 	emsg_prop_perm_denied = gettext("%s: Couldn't modify \"%s/%s\" "
21227c478bd9Sstevel@tonic-gate 	    "property (permission denied).\n");
21237c478bd9Sstevel@tonic-gate 	emsg_no_service = gettext("No such service \"%s\".\n");
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 	if (strcmp(argv[optind], "enable") == 0) {
21267c478bd9Sstevel@tonic-gate 		int flags = SET_ENABLED;
21277c478bd9Sstevel@tonic-gate 		int wait = 0;
21287c478bd9Sstevel@tonic-gate 		int error = 0;
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 		++optind;
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 		while ((o = getopt(argc, argv, "rst")) != -1) {
21337c478bd9Sstevel@tonic-gate 			if (o == 'r')
21347c478bd9Sstevel@tonic-gate 				flags |= SET_RECURSIVE;
21357c478bd9Sstevel@tonic-gate 			else if (o == 't')
21367c478bd9Sstevel@tonic-gate 				flags |= SET_TEMPORARY;
21377c478bd9Sstevel@tonic-gate 			else if (o == 's')
21387c478bd9Sstevel@tonic-gate 				wait = 1;
21397c478bd9Sstevel@tonic-gate 			else if (o == '?')
21407c478bd9Sstevel@tonic-gate 				usage();
21417c478bd9Sstevel@tonic-gate 			else {
21427c478bd9Sstevel@tonic-gate 				assert(0);
21437c478bd9Sstevel@tonic-gate 				abort();
21447c478bd9Sstevel@tonic-gate 			}
21457c478bd9Sstevel@tonic-gate 		}
21467c478bd9Sstevel@tonic-gate 		argc -= optind;
21477c478bd9Sstevel@tonic-gate 		argv += optind;
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 		if (argc <= 0)
21507c478bd9Sstevel@tonic-gate 			usage();
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 		/*
21537c478bd9Sstevel@tonic-gate 		 * We want to continue with -s processing if we had
21547c478bd9Sstevel@tonic-gate 		 * invalid options, but not if an enable failed.  We
21557c478bd9Sstevel@tonic-gate 		 * squelch output the second time we walk fmris; we saw
21567c478bd9Sstevel@tonic-gate 		 * the errors the first time.
21577c478bd9Sstevel@tonic-gate 		 */
21587c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv, 0, set_fmri_enabled,
21597c478bd9Sstevel@tonic-gate 		    (void *)flags, &error, uu_warn)) != 0) {
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
21627c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
21637c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 		} else if (wait && exit_status == 0 &&
21667c478bd9Sstevel@tonic-gate 		    (err = scf_walk_fmri(h, argc, argv, 0, wait_fmri_enabled,
21677c478bd9Sstevel@tonic-gate 		    (void *)flags, &error, quiet)) != 0) {
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
21707c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
21717c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
21727c478bd9Sstevel@tonic-gate 		}
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 		if (error > 0)
21757c478bd9Sstevel@tonic-gate 			exit_status = error;
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[optind], "disable") == 0) {
21787c478bd9Sstevel@tonic-gate 		int flags = 0;
21797c478bd9Sstevel@tonic-gate 		int wait = 0;
21807c478bd9Sstevel@tonic-gate 		int error = 0;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 		++optind;
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 		while ((o = getopt(argc, argv, "st")) != -1) {
21857c478bd9Sstevel@tonic-gate 			if (o == 't')
21867c478bd9Sstevel@tonic-gate 				flags |= SET_TEMPORARY;
21877c478bd9Sstevel@tonic-gate 			else if (o == 's')
21887c478bd9Sstevel@tonic-gate 				wait = 1;
21897c478bd9Sstevel@tonic-gate 			else if (o == '?')
21907c478bd9Sstevel@tonic-gate 				usage();
21917c478bd9Sstevel@tonic-gate 			else {
21927c478bd9Sstevel@tonic-gate 				assert(0);
21937c478bd9Sstevel@tonic-gate 				abort();
21947c478bd9Sstevel@tonic-gate 			}
21957c478bd9Sstevel@tonic-gate 		}
21967c478bd9Sstevel@tonic-gate 		argc -= optind;
21977c478bd9Sstevel@tonic-gate 		argv += optind;
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 		if (argc <= 0)
22007c478bd9Sstevel@tonic-gate 			usage();
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 		/*
22037c478bd9Sstevel@tonic-gate 		 * We want to continue with -s processing if we had
22047c478bd9Sstevel@tonic-gate 		 * invalid options, but not if a disable failed.  We
22057c478bd9Sstevel@tonic-gate 		 * squelch output the second time we walk fmris; we saw
22067c478bd9Sstevel@tonic-gate 		 * the errors the first time.
22077c478bd9Sstevel@tonic-gate 		 */
22087c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc, argv, 0, set_fmri_enabled,
22097c478bd9Sstevel@tonic-gate 		    (void *)flags, &exit_status, uu_warn)) != 0) {
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
22127c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
22137c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 		} else if (wait && exit_status == 0 &&
22167c478bd9Sstevel@tonic-gate 		    (err = scf_walk_fmri(h, argc, argv, 0, wait_fmri_disabled,
22177c478bd9Sstevel@tonic-gate 		    (void *)flags, &error, quiet)) != 0) {
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
22207c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
22217c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
22227c478bd9Sstevel@tonic-gate 		}
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 		if (error > 0)
22257c478bd9Sstevel@tonic-gate 			exit_status = error;
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[optind], "restart") == 0) {
22287c478bd9Sstevel@tonic-gate 		++optind;
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 		if (optind >= argc)
22317c478bd9Sstevel@tonic-gate 			usage();
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc - optind, argv + optind, 0,
22347c478bd9Sstevel@tonic-gate 		    set_fmri_action, (void *)SCF_PROPERTY_RESTART,
22357c478bd9Sstevel@tonic-gate 		    &exit_status, uu_warn)) != 0) {
22367c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
22377c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
22387c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
22397c478bd9Sstevel@tonic-gate 		}
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[optind], "refresh") == 0) {
22427c478bd9Sstevel@tonic-gate 		++optind;
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 		if (optind >= argc)
22457c478bd9Sstevel@tonic-gate 			usage();
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc - optind, argv + optind, 0,
22487c478bd9Sstevel@tonic-gate 		    set_fmri_action, (void *)SCF_PROPERTY_REFRESH,
22497c478bd9Sstevel@tonic-gate 		    &exit_status, uu_warn)) != 0) {
22507c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
22517c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(scf_error()));
22527c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
22537c478bd9Sstevel@tonic-gate 		}
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[optind], "mark") == 0) {
22567c478bd9Sstevel@tonic-gate 		int flags = 0;
22577c478bd9Sstevel@tonic-gate 		scf_walk_callback callback;
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 		++optind;
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 		while ((o = getopt(argc, argv, "It")) != -1) {
22627c478bd9Sstevel@tonic-gate 			if (o == 'I')
22637c478bd9Sstevel@tonic-gate 				flags |= MARK_IMMEDIATE;
22647c478bd9Sstevel@tonic-gate 			else if (o == 't')
22657c478bd9Sstevel@tonic-gate 				flags |= MARK_TEMPORARY;
22667c478bd9Sstevel@tonic-gate 			else if (o == '?')
22677c478bd9Sstevel@tonic-gate 				usage();
22687c478bd9Sstevel@tonic-gate 			else {
22697c478bd9Sstevel@tonic-gate 				assert(0);
22707c478bd9Sstevel@tonic-gate 				abort();
22717c478bd9Sstevel@tonic-gate 			}
22727c478bd9Sstevel@tonic-gate 		}
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 		if (argc - optind < 2)
22757c478bd9Sstevel@tonic-gate 			usage();
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 		if (strcmp(argv[optind], "degraded") == 0) {
22787c478bd9Sstevel@tonic-gate 			if (flags & MARK_TEMPORARY)
22797c478bd9Sstevel@tonic-gate 				uu_xdie(UU_EXIT_USAGE, gettext("-t may not be "
22807c478bd9Sstevel@tonic-gate 				    "used with degraded.\n"));
22817c478bd9Sstevel@tonic-gate 			callback = force_degraded;
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 		} else if (strcmp(argv[optind], "maintenance") == 0) {
22847c478bd9Sstevel@tonic-gate 			callback = force_maintenance;
22857c478bd9Sstevel@tonic-gate 		} else {
22867c478bd9Sstevel@tonic-gate 			usage();
22877c478bd9Sstevel@tonic-gate 		}
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc - optind - 1,
22907c478bd9Sstevel@tonic-gate 		    argv + optind + 1, 0, callback, NULL, &exit_status,
22917c478bd9Sstevel@tonic-gate 		    uu_warn)) != 0) {
22927c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
22937c478bd9Sstevel@tonic-gate 			    "instances: %s\n"),
22947c478bd9Sstevel@tonic-gate 			    scf_strerror(err));
22957c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
22967c478bd9Sstevel@tonic-gate 		}
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[optind], "clear") == 0) {
22997c478bd9Sstevel@tonic-gate 		++optind;
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 		if (optind >= argc)
23027c478bd9Sstevel@tonic-gate 			usage();
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 		if ((err = scf_walk_fmri(h, argc - optind, argv + optind, 0,
23057c478bd9Sstevel@tonic-gate 		    clear_instance, NULL, &exit_status, uu_warn)) != 0) {
23067c478bd9Sstevel@tonic-gate 			uu_warn(gettext("failed to iterate over "
23077c478bd9Sstevel@tonic-gate 			    "instances: %s\n"), scf_strerror(err));
23087c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
23097c478bd9Sstevel@tonic-gate 		}
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[optind], "milestone") == 0) {
23127c478bd9Sstevel@tonic-gate 		boolean_t temporary = B_TRUE;
23137c478bd9Sstevel@tonic-gate 		const char *milestone;
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 		++optind;
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 		while ((o = getopt(argc, argv, "d")) != -1) {
23187c478bd9Sstevel@tonic-gate 			if (o == 'd')
23197c478bd9Sstevel@tonic-gate 				temporary = B_FALSE;
23207c478bd9Sstevel@tonic-gate 			else if (o == '?')
23217c478bd9Sstevel@tonic-gate 				usage_milestone();
23227c478bd9Sstevel@tonic-gate 			else {
23237c478bd9Sstevel@tonic-gate 				assert(0);
23247c478bd9Sstevel@tonic-gate 				abort();
23257c478bd9Sstevel@tonic-gate 			}
23267c478bd9Sstevel@tonic-gate 		}
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 		if (optind >= argc)
23297c478bd9Sstevel@tonic-gate 			usage_milestone();
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 		milestone = validate_milestone(argv[optind]);
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 		set_milestone(milestone, temporary);
23347c478bd9Sstevel@tonic-gate 	} else if (strcmp(argv[optind], "_smf_backup") == 0) {
23357c478bd9Sstevel@tonic-gate 		const char *reason = NULL;
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 		++optind;
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 		if (optind != argc - 1)
23407c478bd9Sstevel@tonic-gate 			usage();
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 		if ((err = _scf_request_backup(h, argv[optind])) !=
23437c478bd9Sstevel@tonic-gate 		    SCF_SUCCESS) {
23447c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
23457c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
23467c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
23477c478bd9Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
23487c478bd9Sstevel@tonic-gate 				scfdie();
23497c478bd9Sstevel@tonic-gate 				break;
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
23527c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
23537c478bd9Sstevel@tonic-gate 				reason = scf_strerror(scf_error());
23547c478bd9Sstevel@tonic-gate 				break;
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INTERNAL:
23577c478bd9Sstevel@tonic-gate 				reason =
23587c478bd9Sstevel@tonic-gate 				    "unknown error (see console for details)";
23597c478bd9Sstevel@tonic-gate 				break;
23607c478bd9Sstevel@tonic-gate 			}
2361c0889d7aSstevep 
23627c478bd9Sstevel@tonic-gate 			uu_warn("failed to backup repository: %s\n", reason);
23637c478bd9Sstevel@tonic-gate 			exit_status = UU_EXIT_FATAL;
23647c478bd9Sstevel@tonic-gate 		}
2365c0889d7aSstevep 	} else if (strcmp(argv[optind], "_smf_repository_switch") == 0) {
2366c0889d7aSstevep 		const char *reason = NULL;
2367c0889d7aSstevep 
2368c0889d7aSstevep 		++optind;
2369c0889d7aSstevep 
2370c0889d7aSstevep 		/*
2371c0889d7aSstevep 		 * Check argument and setup scf_switch structure
2372c0889d7aSstevep 		 */
2373c0889d7aSstevep 		if (optind != argc - 1)
2374c0889d7aSstevep 			exit(1);
2375c0889d7aSstevep 
2376c0889d7aSstevep 		if (strcmp(argv[optind], "fast") == 0) {
2377c0889d7aSstevep 			sw_back = 0;
2378c0889d7aSstevep 		} else if (strcmp(argv[optind], "perm") == 0) {
2379c0889d7aSstevep 			sw_back = 1;
2380c0889d7aSstevep 		} else {
2381c0889d7aSstevep 			exit(UU_EXIT_USAGE);
2382c0889d7aSstevep 		}
2383c0889d7aSstevep 
2384c0889d7aSstevep 		/*
2385c0889d7aSstevep 		 * Call into switch primitive
2386c0889d7aSstevep 		 */
2387c0889d7aSstevep 		if ((err = _scf_repository_switch(h, sw_back)) !=
2388c0889d7aSstevep 		    SCF_SUCCESS) {
2389c0889d7aSstevep 			/*
2390c0889d7aSstevep 			 * Retrieve per thread SCF error code
2391c0889d7aSstevep 			 */
2392c0889d7aSstevep 			switch (scf_error()) {
2393c0889d7aSstevep 			case SCF_ERROR_NOT_BOUND:
2394c0889d7aSstevep 				abort();
2395c0889d7aSstevep 				/* NOTREACHED */
2396c0889d7aSstevep 
2397c0889d7aSstevep 			case SCF_ERROR_CONNECTION_BROKEN:
2398c0889d7aSstevep 			case SCF_ERROR_BACKEND_READONLY:
2399c0889d7aSstevep 				scfdie();
2400c0889d7aSstevep 				/* NOTREACHED */
2401c0889d7aSstevep 
2402c0889d7aSstevep 			case SCF_ERROR_PERMISSION_DENIED:
2403c0889d7aSstevep 			case SCF_ERROR_INVALID_ARGUMENT:
2404c0889d7aSstevep 				reason = scf_strerror(scf_error());
2405c0889d7aSstevep 				break;
2406c0889d7aSstevep 
2407c0889d7aSstevep 			case SCF_ERROR_INTERNAL:
2408c0889d7aSstevep 				reason = "File operation error: (see console)";
2409c0889d7aSstevep 				break;
2410c0889d7aSstevep 
2411c0889d7aSstevep 			default:
2412c0889d7aSstevep 				abort();
2413c0889d7aSstevep 				/* NOTREACHED */
2414c0889d7aSstevep 			}
2415c0889d7aSstevep 
2416c0889d7aSstevep 			uu_warn("failed to switch repository: %s\n", reason);
2417c0889d7aSstevep 			exit_status = UU_EXIT_FATAL;
2418c0889d7aSstevep 		}
24197c478bd9Sstevel@tonic-gate 	} else {
24207c478bd9Sstevel@tonic-gate 		usage();
24217c478bd9Sstevel@tonic-gate 	}
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	if (scf_handle_unbind(h) == -1)
24247c478bd9Sstevel@tonic-gate 		scfdie();
24257c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	return (exit_status);
24287c478bd9Sstevel@tonic-gate }
2429