xref: /onnv-gate/usr/src/lib/librestart/common/librestart.c (revision 9263:48d14e1f550f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51712Srm88369  * Common Development and Distribution License (the "License").
61712Srm88369  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216812Sraf 
220Sstevel@tonic-gate /*
238823STruong.Q.Nguyen@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <librestart.h>
280Sstevel@tonic-gate #include <librestart_priv.h>
290Sstevel@tonic-gate #include <libscf.h>
300Sstevel@tonic-gate #include <libscf_priv.h>
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <assert.h>
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <dlfcn.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <exec_attr.h>
370Sstevel@tonic-gate #include <grp.h>
380Sstevel@tonic-gate #include <libsysevent.h>
390Sstevel@tonic-gate #include <libuutil.h>
400Sstevel@tonic-gate #include <limits.h>
410Sstevel@tonic-gate #include <link.h>
420Sstevel@tonic-gate #include <malloc.h>
430Sstevel@tonic-gate #include <pool.h>
440Sstevel@tonic-gate #include <priv.h>
450Sstevel@tonic-gate #include <project.h>
460Sstevel@tonic-gate #include <pthread.h>
470Sstevel@tonic-gate #include <pwd.h>
480Sstevel@tonic-gate #include <secdb.h>
490Sstevel@tonic-gate #include <signal.h>
500Sstevel@tonic-gate #include <stdlib.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <syslog.h>
530Sstevel@tonic-gate #include <sys/corectl.h>
540Sstevel@tonic-gate #include <sys/machelf.h>
550Sstevel@tonic-gate #include <sys/task.h>
560Sstevel@tonic-gate #include <sys/types.h>
570Sstevel@tonic-gate #include <time.h>
580Sstevel@tonic-gate #include <unistd.h>
590Sstevel@tonic-gate #include <ucontext.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	min(a, b)		((a) > (b) ? (b) : (a))
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	MKW_TRUE	":true"
640Sstevel@tonic-gate #define	MKW_KILL	":kill"
650Sstevel@tonic-gate #define	MKW_KILL_PROC	":kill_process"
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	ALLOCFAIL	((char *)"Allocation failure.")
680Sstevel@tonic-gate #define	RCBROKEN	((char *)"Repository connection broken.")
690Sstevel@tonic-gate 
706045Srm88369 #define	MAX_COMMIT_RETRIES		10
710Sstevel@tonic-gate #define	MAX_COMMIT_RETRY_INT		(5 * 1000000)	/* 5 seconds */
720Sstevel@tonic-gate #define	INITIAL_COMMIT_RETRY_INT	(10000)		/* 1/100th second */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * bad_fail() catches bugs in this and lower layers by reporting supposedly
760Sstevel@tonic-gate  * impossible function failures.  The NDEBUG case keeps the strings out of the
770Sstevel@tonic-gate  * library but still calls abort() so we can root-cause from the coredump.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate #ifndef NDEBUG
800Sstevel@tonic-gate #define	bad_fail(func, err)	{					\
810Sstevel@tonic-gate 	(void) fprintf(stderr,						\
820Sstevel@tonic-gate 	    "At %s:%d, %s() failed with unexpected error %d.  Aborting.\n", \
830Sstevel@tonic-gate 	    __FILE__, __LINE__, (func), (err));				\
840Sstevel@tonic-gate 	abort();							\
850Sstevel@tonic-gate }
860Sstevel@tonic-gate #else
870Sstevel@tonic-gate #define	bad_fail(func, err)	abort()
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate 
900Sstevel@tonic-gate struct restarter_event_handle {
910Sstevel@tonic-gate 	char				*reh_restarter_name;
920Sstevel@tonic-gate 	char				*reh_delegate_channel_name;
930Sstevel@tonic-gate 	evchan_t			*reh_delegate_channel;
940Sstevel@tonic-gate 	char				*reh_delegate_subscriber_id;
950Sstevel@tonic-gate 	char				*reh_master_channel_name;
960Sstevel@tonic-gate 	evchan_t			*reh_master_channel;
970Sstevel@tonic-gate 	char				*reh_master_subscriber_id;
980Sstevel@tonic-gate 	int				(*reh_handler)(restarter_event_t *);
990Sstevel@tonic-gate };
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate struct restarter_event {
1020Sstevel@tonic-gate 	sysevent_t			*re_sysevent;
1030Sstevel@tonic-gate 	restarter_event_type_t		re_type;
1040Sstevel@tonic-gate 	char				*re_instance_name;
1050Sstevel@tonic-gate 	restarter_event_handle_t	*re_event_handle;
1060Sstevel@tonic-gate 	restarter_instance_state_t	re_state;
1070Sstevel@tonic-gate 	restarter_instance_state_t	re_next_state;
1080Sstevel@tonic-gate };
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static const char * const allocfail = "Allocation failure.\n";
1110Sstevel@tonic-gate static const char * const rcbroken = "Repository connection broken.\n";
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static int method_context_safety = 0;	/* Can safely call pools/projects. */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate int ndebug = 1;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate static void
1180Sstevel@tonic-gate free_restarter_event_handle(struct restarter_event_handle *h)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	if (h == NULL)
1210Sstevel@tonic-gate 		return;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	/*
1240Sstevel@tonic-gate 	 * Just free the memory -- don't unbind the sysevent handle,
1250Sstevel@tonic-gate 	 * as otherwise events may be lost if this is just a restarter
1260Sstevel@tonic-gate 	 * restart.
1270Sstevel@tonic-gate 	 */
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if (h->reh_restarter_name != NULL)
1300Sstevel@tonic-gate 		free(h->reh_restarter_name);
1310Sstevel@tonic-gate 	if (h->reh_delegate_channel_name != NULL)
1320Sstevel@tonic-gate 		free(h->reh_delegate_channel_name);
1330Sstevel@tonic-gate 	if (h->reh_delegate_subscriber_id != NULL)
1340Sstevel@tonic-gate 		free(h->reh_delegate_subscriber_id);
1350Sstevel@tonic-gate 	if (h->reh_master_channel_name != NULL)
1360Sstevel@tonic-gate 		free(h->reh_master_channel_name);
1370Sstevel@tonic-gate 	if (h->reh_master_subscriber_id != NULL)
1380Sstevel@tonic-gate 		free(h->reh_master_subscriber_id);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	free(h);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate char *
1440Sstevel@tonic-gate _restarter_get_channel_name(const char *fmri, int type)
1450Sstevel@tonic-gate {
146*9263SSean.Wilcox@Sun.COM 	char *name;
1470Sstevel@tonic-gate 	char *chan_name = malloc(MAX_CHNAME_LEN);
1480Sstevel@tonic-gate 	char prefix_name[3];
149*9263SSean.Wilcox@Sun.COM 	int i;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	if (chan_name == NULL)
1520Sstevel@tonic-gate 		return (NULL);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	if (type == RESTARTER_CHANNEL_DELEGATE)
1550Sstevel@tonic-gate 		(void) strcpy(prefix_name, "d_");
1560Sstevel@tonic-gate 	else if (type == RESTARTER_CHANNEL_MASTER)
1570Sstevel@tonic-gate 		(void) strcpy(prefix_name, "m_");
1580Sstevel@tonic-gate 	else {
1590Sstevel@tonic-gate 		free(chan_name);
1600Sstevel@tonic-gate 		return (NULL);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
163*9263SSean.Wilcox@Sun.COM 	/*
164*9263SSean.Wilcox@Sun.COM 	 * Create a unique name
165*9263SSean.Wilcox@Sun.COM 	 *
166*9263SSean.Wilcox@Sun.COM 	 * Use the entire name, using a replacement of the /
167*9263SSean.Wilcox@Sun.COM 	 * characters to get a better name.
168*9263SSean.Wilcox@Sun.COM 	 *
169*9263SSean.Wilcox@Sun.COM 	 * Remove the svc:/ from the beginning as this really
170*9263SSean.Wilcox@Sun.COM 	 * isn't going to provide any uniqueness...
171*9263SSean.Wilcox@Sun.COM 	 *
172*9263SSean.Wilcox@Sun.COM 	 * An fmri name greater than MAX_CHNAME_LEN is going
173*9263SSean.Wilcox@Sun.COM 	 * to be rejected as too long for the chan_name below
174*9263SSean.Wilcox@Sun.COM 	 * in the snprintf call.
175*9263SSean.Wilcox@Sun.COM 	 */
176*9263SSean.Wilcox@Sun.COM 	if ((name = strdup(strchr(fmri, '/') + 1)) == NULL) {
177*9263SSean.Wilcox@Sun.COM 		free(chan_name);
178*9263SSean.Wilcox@Sun.COM 		return (NULL);
179*9263SSean.Wilcox@Sun.COM 	}
180*9263SSean.Wilcox@Sun.COM 	i = 0;
181*9263SSean.Wilcox@Sun.COM 	while (name[i]) {
182*9263SSean.Wilcox@Sun.COM 		if (name[i] == '/') {
183*9263SSean.Wilcox@Sun.COM 			name[i] = '_';
184*9263SSean.Wilcox@Sun.COM 		}
185*9263SSean.Wilcox@Sun.COM 
186*9263SSean.Wilcox@Sun.COM 		i++;
187*9263SSean.Wilcox@Sun.COM 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * Should check for [a-z],[A-Z],[0-9],.,_,-,:
1910Sstevel@tonic-gate 	 */
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	if (snprintf(chan_name, MAX_CHNAME_LEN, "com.sun:scf:%s%s",
1940Sstevel@tonic-gate 	    prefix_name, name) > MAX_CHNAME_LEN) {
1950Sstevel@tonic-gate 		free(chan_name);
196*9263SSean.Wilcox@Sun.COM 		chan_name = NULL;
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
199*9263SSean.Wilcox@Sun.COM 	free(name);
2000Sstevel@tonic-gate 	return (chan_name);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate int
2040Sstevel@tonic-gate cb(sysevent_t *syse, void *cookie)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate 	restarter_event_handle_t *h = (restarter_event_handle_t *)cookie;
2070Sstevel@tonic-gate 	restarter_event_t *e;
2080Sstevel@tonic-gate 	nvlist_t *attr_list = NULL;
2090Sstevel@tonic-gate 	int ret = 0;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	e = uu_zalloc(sizeof (restarter_event_t));
2120Sstevel@tonic-gate 	if (e == NULL)
2130Sstevel@tonic-gate 		uu_die(allocfail);
2140Sstevel@tonic-gate 	e->re_event_handle = h;
2150Sstevel@tonic-gate 	e->re_sysevent = syse;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if (sysevent_get_attr_list(syse, &attr_list) != 0)
2180Sstevel@tonic-gate 		uu_die(allocfail);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if ((nvlist_lookup_uint32(attr_list, RESTARTER_NAME_TYPE,
2210Sstevel@tonic-gate 	    &(e->re_type)) != 0) ||
2220Sstevel@tonic-gate 	    (nvlist_lookup_string(attr_list,
2230Sstevel@tonic-gate 	    RESTARTER_NAME_INSTANCE, &(e->re_instance_name)) != 0)) {
2240Sstevel@tonic-gate 		uu_warn("%s: Can't decode nvlist for event %p\n",
2250Sstevel@tonic-gate 		    h->reh_restarter_name, (void *)syse);
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		ret = 0;
2280Sstevel@tonic-gate 	} else {
2290Sstevel@tonic-gate 		ret = h->reh_handler(e);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	uu_free(e);
2330Sstevel@tonic-gate 	nvlist_free(attr_list);
2340Sstevel@tonic-gate 	return (ret);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate  * restarter_bind_handle(uint32_t, char *, int (*)(restarter_event_t *), int,
2390Sstevel@tonic-gate  *     restarter_event_handle_t **)
2400Sstevel@tonic-gate  *
2410Sstevel@tonic-gate  * Bind to a delegated restarter event channel.
2420Sstevel@tonic-gate  * Each delegated restarter gets its own channel for resource management.
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * Returns 0 on success or
2450Sstevel@tonic-gate  *   ENOTSUP	version mismatch
2460Sstevel@tonic-gate  *   EINVAL	restarter_name or event_handle is NULL
2470Sstevel@tonic-gate  *   ENOMEM	out of memory, too many channels, or too many subscriptions
2480Sstevel@tonic-gate  *   EBUSY	sysevent_evc_bind() could not establish binding
2490Sstevel@tonic-gate  *   EFAULT	internal sysevent_evc_bind()/sysevent_evc_subscribe() error
2500Sstevel@tonic-gate  *   EMFILE	out of file descriptors
2510Sstevel@tonic-gate  *   EPERM	insufficient privilege for sysevent_evc_bind()
2520Sstevel@tonic-gate  *   EEXIST	already subscribed
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate int
2550Sstevel@tonic-gate restarter_bind_handle(uint32_t version, const char *restarter_name,
2560Sstevel@tonic-gate     int (*event_handler)(restarter_event_t *), int flags,
2570Sstevel@tonic-gate     restarter_event_handle_t **rehp)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	restarter_event_handle_t *h;
2600Sstevel@tonic-gate 	size_t sz;
2610Sstevel@tonic-gate 	int err;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	if (version != RESTARTER_EVENT_VERSION)
2640Sstevel@tonic-gate 		return (ENOTSUP);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (restarter_name == NULL || event_handler == NULL)
2670Sstevel@tonic-gate 		return (EINVAL);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (flags & RESTARTER_FLAG_DEBUG)
2700Sstevel@tonic-gate 		ndebug++;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if ((h = uu_zalloc(sizeof (restarter_event_handle_t))) == NULL)
2730Sstevel@tonic-gate 		return (ENOMEM);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	h->reh_delegate_subscriber_id = malloc(MAX_SUBID_LEN);
2760Sstevel@tonic-gate 	h->reh_master_subscriber_id = malloc(MAX_SUBID_LEN);
2770Sstevel@tonic-gate 	h->reh_restarter_name = strdup(restarter_name);
2780Sstevel@tonic-gate 	if (h->reh_delegate_subscriber_id == NULL ||
2790Sstevel@tonic-gate 	    h->reh_master_subscriber_id == NULL ||
2800Sstevel@tonic-gate 	    h->reh_restarter_name == NULL) {
2810Sstevel@tonic-gate 		free_restarter_event_handle(h);
2820Sstevel@tonic-gate 		return (ENOMEM);
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	sz = strlcpy(h->reh_delegate_subscriber_id, "del", MAX_SUBID_LEN);
2860Sstevel@tonic-gate 	assert(sz < MAX_SUBID_LEN);
2870Sstevel@tonic-gate 	sz = strlcpy(h->reh_master_subscriber_id, "master", MAX_SUBID_LEN);
2880Sstevel@tonic-gate 	assert(sz < MAX_SUBID_LEN);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	h->reh_delegate_channel_name =
2910Sstevel@tonic-gate 	    _restarter_get_channel_name(restarter_name,
2920Sstevel@tonic-gate 	    RESTARTER_CHANNEL_DELEGATE);
2930Sstevel@tonic-gate 	h->reh_master_channel_name =
2940Sstevel@tonic-gate 	    _restarter_get_channel_name(restarter_name,
2950Sstevel@tonic-gate 	    RESTARTER_CHANNEL_MASTER);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (h->reh_delegate_channel_name == NULL ||
2980Sstevel@tonic-gate 	    h->reh_master_channel_name == NULL) {
2990Sstevel@tonic-gate 		free_restarter_event_handle(h);
3000Sstevel@tonic-gate 		return (ENOMEM);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (sysevent_evc_bind(h->reh_delegate_channel_name,
3040Sstevel@tonic-gate 	    &h->reh_delegate_channel, EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
3050Sstevel@tonic-gate 		err = errno;
3060Sstevel@tonic-gate 		assert(err != EINVAL);
3070Sstevel@tonic-gate 		assert(err != ENOENT);
3080Sstevel@tonic-gate 		free_restarter_event_handle(h);
3090Sstevel@tonic-gate 		return (err);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if (sysevent_evc_bind(h->reh_master_channel_name,
3130Sstevel@tonic-gate 	    &h->reh_master_channel, EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
3140Sstevel@tonic-gate 		err = errno;
3150Sstevel@tonic-gate 		assert(err != EINVAL);
3160Sstevel@tonic-gate 		assert(err != ENOENT);
3170Sstevel@tonic-gate 		free_restarter_event_handle(h);
3180Sstevel@tonic-gate 		return (err);
3190Sstevel@tonic-gate 	}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	h->reh_handler = event_handler;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	assert(strlen(restarter_name) <= MAX_CLASS_LEN - 1);
3240Sstevel@tonic-gate 	assert(strlen(h->reh_delegate_subscriber_id) <= MAX_SUBID_LEN - 1);
3250Sstevel@tonic-gate 	assert(strlen(h->reh_master_subscriber_id) <= MAX_SUBID_LEN - 1);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (sysevent_evc_subscribe(h->reh_delegate_channel,
3280Sstevel@tonic-gate 	    h->reh_delegate_subscriber_id, EC_ALL, cb, h, EVCH_SUB_KEEP) != 0) {
3290Sstevel@tonic-gate 		err = errno;
3300Sstevel@tonic-gate 		assert(err != EINVAL);
3310Sstevel@tonic-gate 		free_restarter_event_handle(h);
3320Sstevel@tonic-gate 		return (err);
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	*rehp = h;
3360Sstevel@tonic-gate 	return (0);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate restarter_event_handle_t *
3400Sstevel@tonic-gate restarter_event_get_handle(restarter_event_t *e)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate 	assert(e != NULL && e->re_event_handle != NULL);
3430Sstevel@tonic-gate 	return (e->re_event_handle);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate restarter_event_type_t
3470Sstevel@tonic-gate restarter_event_get_type(restarter_event_t *e)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	assert(e != NULL);
3500Sstevel@tonic-gate 	return (e->re_type);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate ssize_t
3540Sstevel@tonic-gate restarter_event_get_instance(restarter_event_t *e, char *inst, size_t sz)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate 	assert(e != NULL && inst != NULL);
3570Sstevel@tonic-gate 	return ((ssize_t)strlcpy(inst, e->re_instance_name, sz));
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate int
3610Sstevel@tonic-gate restarter_event_get_current_states(restarter_event_t *e,
3620Sstevel@tonic-gate     restarter_instance_state_t *state, restarter_instance_state_t *next_state)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	if (e == NULL)
3650Sstevel@tonic-gate 		return (-1);
3660Sstevel@tonic-gate 	*state = e->re_state;
3670Sstevel@tonic-gate 	*next_state = e->re_next_state;
3680Sstevel@tonic-gate 	return (0);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate /*
3726045Srm88369  * restarter_event_publish_retry() is a wrapper around sysevent_evc_publish().
3736045Srm88369  * In case, the event cannot be sent at the first attempt (sysevent_evc_publish
3746045Srm88369  * returned EAGAIN - sysevent queue full), this function retries a few time
3756045Srm88369  * and return ENOSPC if it reaches the retry limit.
3766045Srm88369  *
3776045Srm88369  * The arguments to this function map the arguments of sysevent_evc_publish().
3786045Srm88369  *
3796045Srm88369  * On success, return 0. On error, return
3806045Srm88369  *
3816045Srm88369  *   EFAULT - internal sysevent_evc_publish() error
3826045Srm88369  *   ENOMEM - internal sysevent_evc_publish() error
3836045Srm88369  *   EBADF - scp is invalid (sysevent_evc_publish() returned EINVAL)
3846045Srm88369  *   ENOSPC - sysevent queue full (sysevent_evc_publish() returned EAGAIN)
3856045Srm88369  */
3866045Srm88369 int
3876045Srm88369 restarter_event_publish_retry(evchan_t *scp, const char *class,
3886045Srm88369     const char *subclass, const char *vendor, const char *pub_name,
3896045Srm88369     nvlist_t *attr_list, uint32_t flags)
3906045Srm88369 {
3916045Srm88369 	int retries, ret;
3926045Srm88369 	useconds_t retry_int = INITIAL_COMMIT_RETRY_INT;
3936045Srm88369 
3946045Srm88369 	for (retries = 0; retries < MAX_COMMIT_RETRIES; retries++) {
3956045Srm88369 		ret = sysevent_evc_publish(scp, class, subclass, vendor,
3966045Srm88369 		    pub_name, attr_list, flags);
3976045Srm88369 		if (ret == 0)
3986045Srm88369 			break;
3996045Srm88369 
4006045Srm88369 		switch (ret) {
4016045Srm88369 		case EAGAIN:
4026045Srm88369 			/* Queue is full */
4036045Srm88369 			(void) usleep(retry_int);
4046045Srm88369 
4056045Srm88369 			retry_int = min(retry_int * 2, MAX_COMMIT_RETRY_INT);
4066045Srm88369 			break;
4076045Srm88369 
4086045Srm88369 		case EINVAL:
4096045Srm88369 			ret = EBADF;
4106045Srm88369 			/* FALLTHROUGH */
4116045Srm88369 
4126045Srm88369 		case EFAULT:
4136045Srm88369 		case ENOMEM:
4146045Srm88369 			return (ret);
4156045Srm88369 
4166045Srm88369 		case EOVERFLOW:
4176045Srm88369 		default:
4186045Srm88369 			/* internal error - abort */
4196045Srm88369 			bad_fail("sysevent_evc_publish", ret);
4206045Srm88369 		}
4216045Srm88369 	}
4226045Srm88369 
4236045Srm88369 	if (retries == MAX_COMMIT_RETRIES)
4246045Srm88369 		ret = ENOSPC;
4256045Srm88369 
4266045Srm88369 	return (ret);
4276045Srm88369 }
4286045Srm88369 
4296045Srm88369 /*
4300Sstevel@tonic-gate  * Commit the state, next state, and auxiliary state into the repository.
4310Sstevel@tonic-gate  * Let the graph engine know about the state change and error.  On success,
4320Sstevel@tonic-gate  * return 0. On error, return
4330Sstevel@tonic-gate  *   EINVAL - aux has spaces
4340Sstevel@tonic-gate  *	    - inst is invalid or not an instance FMRI
4350Sstevel@tonic-gate  *   EPROTO - librestart compiled against different libscf
4360Sstevel@tonic-gate  *   ENOMEM - out of memory
4370Sstevel@tonic-gate  *	    - repository server out of resources
4380Sstevel@tonic-gate  *   ENOTACTIVE - repository server not running
4390Sstevel@tonic-gate  *   ECONNABORTED - repository connection established, but then broken
4400Sstevel@tonic-gate  *		  - unknown libscf error
4410Sstevel@tonic-gate  *   ENOENT - inst does not exist in the repository
4420Sstevel@tonic-gate  *   EPERM - insufficient permissions
4430Sstevel@tonic-gate  *   EACCESS - backend access denied
4440Sstevel@tonic-gate  *   EROFS - backend is readonly
4450Sstevel@tonic-gate  *   EFAULT - internal sysevent_evc_publish() error
4460Sstevel@tonic-gate  *   EBADF - h is invalid (sysevent_evc_publish() returned EINVAL)
4476045Srm88369  *   ENOSPC - sysevent queue full (sysevent_evc_publish() returned EAGAIN)
4480Sstevel@tonic-gate  */
4490Sstevel@tonic-gate int
4500Sstevel@tonic-gate restarter_set_states(restarter_event_handle_t *h, const char *inst,
4510Sstevel@tonic-gate     restarter_instance_state_t cur_state,
4520Sstevel@tonic-gate     restarter_instance_state_t new_cur_state,
4530Sstevel@tonic-gate     restarter_instance_state_t next_state,
4540Sstevel@tonic-gate     restarter_instance_state_t new_next_state, restarter_error_t e,
4550Sstevel@tonic-gate     const char *aux)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	nvlist_t *attr;
4580Sstevel@tonic-gate 	scf_handle_t *scf_h;
4590Sstevel@tonic-gate 	instance_data_t id;
4600Sstevel@tonic-gate 	int ret = 0;
4610Sstevel@tonic-gate 	char *p = (char *)aux;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	assert(h->reh_master_channel != NULL);
4640Sstevel@tonic-gate 	assert(h->reh_master_channel_name != NULL);
4650Sstevel@tonic-gate 	assert(h->reh_master_subscriber_id != NULL);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	/* Validate format of auxiliary state: no spaces allowed */
468863Slianep 	if (p != NULL) {
469863Slianep 		while (*p != '\0') {
470863Slianep 			if (isspace(*p))
471863Slianep 				return (EINVAL);
472863Slianep 			p++;
473863Slianep 		}
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	if ((scf_h = scf_handle_create(SCF_VERSION)) == NULL) {
4770Sstevel@tonic-gate 		switch (scf_error()) {
4780Sstevel@tonic-gate 		case SCF_ERROR_VERSION_MISMATCH:
4790Sstevel@tonic-gate 			return (EPROTO);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 		case SCF_ERROR_NO_MEMORY:
4820Sstevel@tonic-gate 			return (ENOMEM);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		default:
4850Sstevel@tonic-gate 			bad_fail("scf_handle_create", scf_error());
4860Sstevel@tonic-gate 		}
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if (scf_handle_bind(scf_h) == -1) {
4900Sstevel@tonic-gate 		scf_handle_destroy(scf_h);
4910Sstevel@tonic-gate 		switch (scf_error()) {
4920Sstevel@tonic-gate 		case SCF_ERROR_NO_SERVER:
4930Sstevel@tonic-gate 			return (ENOTACTIVE);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		case SCF_ERROR_NO_RESOURCES:
4960Sstevel@tonic-gate 			return (ENOMEM);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4990Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
5000Sstevel@tonic-gate 		default:
5010Sstevel@tonic-gate 			bad_fail("scf_handle_bind", scf_error());
5020Sstevel@tonic-gate 		}
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 ||
5060Sstevel@tonic-gate 	    nvlist_add_int32(attr, RESTARTER_NAME_STATE, new_cur_state) != 0 ||
5070Sstevel@tonic-gate 	    nvlist_add_int32(attr, RESTARTER_NAME_NEXT_STATE, new_next_state)
5080Sstevel@tonic-gate 	    != 0 ||
5090Sstevel@tonic-gate 	    nvlist_add_int32(attr, RESTARTER_NAME_ERROR, e) != 0 ||
5100Sstevel@tonic-gate 	    nvlist_add_string(attr, RESTARTER_NAME_INSTANCE, inst) != 0) {
5110Sstevel@tonic-gate 		ret = ENOMEM;
5126045Srm88369 	} else {
5136045Srm88369 		id.i_fmri = inst;
5146045Srm88369 		id.i_state = cur_state;
5156045Srm88369 		id.i_next_state = next_state;
5166045Srm88369 
5176045Srm88369 		ret = _restarter_commit_states(scf_h, &id, new_cur_state,
5186045Srm88369 		    new_next_state, aux);
5196045Srm88369 
5206045Srm88369 		if (ret == 0) {
5216045Srm88369 			ret = restarter_event_publish_retry(
5226045Srm88369 			    h->reh_master_channel, "master", "state_change",
5236045Srm88369 			    "com.sun", "librestart", attr, EVCH_NOSLEEP);
5240Sstevel@tonic-gate 		}
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	nvlist_free(attr);
5280Sstevel@tonic-gate 	(void) scf_handle_unbind(scf_h);
5290Sstevel@tonic-gate 	scf_handle_destroy(scf_h);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	return (ret);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate restarter_instance_state_t
5350Sstevel@tonic-gate restarter_string_to_state(char *string)
5360Sstevel@tonic-gate {
5370Sstevel@tonic-gate 	assert(string != NULL);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	if (strcmp(string, SCF_STATE_STRING_NONE) == 0)
5400Sstevel@tonic-gate 		return (RESTARTER_STATE_NONE);
5410Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_UNINIT) == 0)
5420Sstevel@tonic-gate 		return (RESTARTER_STATE_UNINIT);
5430Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_MAINT) == 0)
5440Sstevel@tonic-gate 		return (RESTARTER_STATE_MAINT);
5450Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_OFFLINE) == 0)
5460Sstevel@tonic-gate 		return (RESTARTER_STATE_OFFLINE);
5470Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_DISABLED) == 0)
5480Sstevel@tonic-gate 		return (RESTARTER_STATE_DISABLED);
5490Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_ONLINE) == 0)
5500Sstevel@tonic-gate 		return (RESTARTER_STATE_ONLINE);
5510Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_DEGRADED) == 0)
5520Sstevel@tonic-gate 		return (RESTARTER_STATE_DEGRADED);
5530Sstevel@tonic-gate 	else {
5540Sstevel@tonic-gate 		return (RESTARTER_STATE_NONE);
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate ssize_t
5590Sstevel@tonic-gate restarter_state_to_string(restarter_instance_state_t state, char *string,
5600Sstevel@tonic-gate     size_t len)
5610Sstevel@tonic-gate {
5620Sstevel@tonic-gate 	assert(string != NULL);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	if (state == RESTARTER_STATE_NONE)
5650Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_NONE, len));
5660Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_UNINIT)
5670Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_UNINIT, len));
5680Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_MAINT)
5690Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_MAINT, len));
5700Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_OFFLINE)
5710Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_OFFLINE,
5720Sstevel@tonic-gate 		    len));
5730Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_DISABLED)
5740Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_DISABLED,
5750Sstevel@tonic-gate 		    len));
5760Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_ONLINE)
5770Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_ONLINE, len));
5780Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_DEGRADED)
5790Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_DEGRADED,
5800Sstevel@tonic-gate 		    len));
5810Sstevel@tonic-gate 	else
5820Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, "unknown", len));
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate  * Sets pg to the name property group of s_inst.  If it doesn't exist, it is
5870Sstevel@tonic-gate  * added.
5880Sstevel@tonic-gate  *
5890Sstevel@tonic-gate  * Fails with
5900Sstevel@tonic-gate  *   ECONNABORTED - repository disconnection or unknown libscf error
5910Sstevel@tonic-gate  *   EBADF - inst is not set
5920Sstevel@tonic-gate  *   ECANCELED - inst is deleted
5930Sstevel@tonic-gate  *   EPERM - permission is denied
5940Sstevel@tonic-gate  *   EACCES - backend denied access
5950Sstevel@tonic-gate  *   EROFS - backend readonly
5960Sstevel@tonic-gate  */
5970Sstevel@tonic-gate static int
5980Sstevel@tonic-gate instance_get_or_add_pg(scf_instance_t *inst, const char *name,
5990Sstevel@tonic-gate     const char *type, uint32_t flags, scf_propertygroup_t *pg)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate again:
6020Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, name, pg) == 0)
6030Sstevel@tonic-gate 		return (0);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	switch (scf_error()) {
6060Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
6070Sstevel@tonic-gate 	default:
6080Sstevel@tonic-gate 		return (ECONNABORTED);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	case SCF_ERROR_NOT_SET:
6110Sstevel@tonic-gate 		return (EBADF);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
6140Sstevel@tonic-gate 		return (ECANCELED);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
6170Sstevel@tonic-gate 		break;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	case SCF_ERROR_HANDLE_MISMATCH:
6200Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
6210Sstevel@tonic-gate 		bad_fail("scf_instance_get_pg", scf_error());
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if (scf_instance_add_pg(inst, name, type, flags, pg) == 0)
6250Sstevel@tonic-gate 		return (0);
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	switch (scf_error()) {
6280Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
6290Sstevel@tonic-gate 	default:
6300Sstevel@tonic-gate 		return (ECONNABORTED);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
6330Sstevel@tonic-gate 		return (ECANCELED);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	case SCF_ERROR_EXISTS:
6360Sstevel@tonic-gate 		goto again;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	case SCF_ERROR_PERMISSION_DENIED:
6390Sstevel@tonic-gate 		return (EPERM);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_ACCESS:
6420Sstevel@tonic-gate 		return (EACCES);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_READONLY:
6450Sstevel@tonic-gate 		return (EROFS);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	case SCF_ERROR_HANDLE_MISMATCH:
6480Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
6490Sstevel@tonic-gate 	case SCF_ERROR_NOT_SET:			/* should be caught above */
6500Sstevel@tonic-gate 		bad_fail("scf_instance_add_pg", scf_error());
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	return (0);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate /*
6570Sstevel@tonic-gate  * Fails with
6580Sstevel@tonic-gate  *   ECONNABORTED
6590Sstevel@tonic-gate  *   ECANCELED - pg was deleted
6600Sstevel@tonic-gate  */
6610Sstevel@tonic-gate static int
6620Sstevel@tonic-gate tx_set_value(scf_transaction_t *tx, scf_transaction_entry_t *ent,
6630Sstevel@tonic-gate     const char *pname, scf_type_t ty, scf_value_t *val)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate 	int r;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	for (;;) {
6680Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, ent, pname,
6690Sstevel@tonic-gate 		    ty) == 0)
6700Sstevel@tonic-gate 			break;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 		switch (scf_error()) {
6730Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
6740Sstevel@tonic-gate 		default:
6750Sstevel@tonic-gate 			return (ECONNABORTED);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
6780Sstevel@tonic-gate 			return (ECANCELED);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
6840Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
6850Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
6860Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
6870Sstevel@tonic-gate 			bad_fail("scf_transaction_property_change_type",
6880Sstevel@tonic-gate 			    scf_error());
6890Sstevel@tonic-gate 		}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, ent, pname, ty) == 0)
6920Sstevel@tonic-gate 			break;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		switch (scf_error()) {
6950Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
6960Sstevel@tonic-gate 		default:
6970Sstevel@tonic-gate 			return (ECONNABORTED);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7000Sstevel@tonic-gate 			return (ECANCELED);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
7030Sstevel@tonic-gate 			break;
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7060Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7070Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
7080Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7090Sstevel@tonic-gate 			bad_fail("scf_transaction_property_new", scf_error());
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	r = scf_entry_add_value(ent, val);
7140Sstevel@tonic-gate 	assert(r == 0);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	return (0);
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate /*
7200Sstevel@tonic-gate  * Commit new_state, new_next_state, and aux to the repository for id.  If
7210Sstevel@tonic-gate  * successful, also set id's state and next-state as given, and return 0.
7220Sstevel@tonic-gate  * Fails with
7230Sstevel@tonic-gate  *   ENOMEM - out of memory
7240Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
7250Sstevel@tonic-gate  *		  - unknown libscf error
7260Sstevel@tonic-gate  *   EINVAL - id->i_fmri is invalid or not an instance FMRI
7270Sstevel@tonic-gate  *   ENOENT - id->i_fmri does not exist
7280Sstevel@tonic-gate  *   EPERM - insufficient permissions
7290Sstevel@tonic-gate  *   EACCES - backend access denied
7300Sstevel@tonic-gate  *   EROFS - backend is readonly
7310Sstevel@tonic-gate  */
7320Sstevel@tonic-gate int
7330Sstevel@tonic-gate _restarter_commit_states(scf_handle_t *h, instance_data_t *id,
7340Sstevel@tonic-gate     restarter_instance_state_t new_state,
7350Sstevel@tonic-gate     restarter_instance_state_t new_state_next, const char *aux)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	char str_state[MAX_SCF_STATE_STRING_SZ];
7380Sstevel@tonic-gate 	char str_new_state[MAX_SCF_STATE_STRING_SZ];
7390Sstevel@tonic-gate 	char str_state_next[MAX_SCF_STATE_STRING_SZ];
7400Sstevel@tonic-gate 	char str_new_state_next[MAX_SCF_STATE_STRING_SZ];
7410Sstevel@tonic-gate 	int ret = 0, r;
7420Sstevel@tonic-gate 	struct timeval now;
7430Sstevel@tonic-gate 	ssize_t sz;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	scf_transaction_t *t = NULL;
7460Sstevel@tonic-gate 	scf_transaction_entry_t *t_state = NULL, *t_state_next = NULL;
7470Sstevel@tonic-gate 	scf_transaction_entry_t *t_stime = NULL, *t_aux = NULL;
7480Sstevel@tonic-gate 	scf_value_t *v_state = NULL, *v_state_next = NULL, *v_stime = NULL;
7490Sstevel@tonic-gate 	scf_value_t *v_aux = NULL;
7500Sstevel@tonic-gate 	scf_instance_t *s_inst = NULL;
7510Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	assert(new_state != RESTARTER_STATE_NONE);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	if ((s_inst = scf_instance_create(h)) == NULL ||
7560Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
7570Sstevel@tonic-gate 	    (t = scf_transaction_create(h)) == NULL ||
7580Sstevel@tonic-gate 	    (t_state = scf_entry_create(h)) == NULL ||
7590Sstevel@tonic-gate 	    (t_state_next = scf_entry_create(h)) == NULL ||
7600Sstevel@tonic-gate 	    (t_stime = scf_entry_create(h)) == NULL ||
7610Sstevel@tonic-gate 	    (t_aux = scf_entry_create(h)) == NULL ||
7620Sstevel@tonic-gate 	    (v_state = scf_value_create(h)) == NULL ||
7630Sstevel@tonic-gate 	    (v_state_next = scf_value_create(h)) == NULL ||
7640Sstevel@tonic-gate 	    (v_stime = scf_value_create(h)) == NULL ||
7650Sstevel@tonic-gate 	    (v_aux = scf_value_create(h)) == NULL) {
7660Sstevel@tonic-gate 		ret = ENOMEM;
7670Sstevel@tonic-gate 		goto out;
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	sz = restarter_state_to_string(new_state, str_new_state,
7710Sstevel@tonic-gate 	    sizeof (str_new_state));
7720Sstevel@tonic-gate 	assert(sz < sizeof (str_new_state));
7730Sstevel@tonic-gate 	sz = restarter_state_to_string(new_state_next, str_new_state_next,
7740Sstevel@tonic-gate 	    sizeof (str_new_state_next));
7750Sstevel@tonic-gate 	assert(sz < sizeof (str_new_state_next));
7760Sstevel@tonic-gate 	sz = restarter_state_to_string(id->i_state, str_state,
7770Sstevel@tonic-gate 	    sizeof (str_state));
7780Sstevel@tonic-gate 	assert(sz < sizeof (str_state));
7790Sstevel@tonic-gate 	sz = restarter_state_to_string(id->i_next_state, str_state_next,
7800Sstevel@tonic-gate 	    sizeof (str_state_next));
7810Sstevel@tonic-gate 	assert(sz < sizeof (str_state_next));
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	ret = gettimeofday(&now, NULL);
7840Sstevel@tonic-gate 	assert(ret != -1);
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, id->i_fmri, NULL, NULL, s_inst,
7870Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1) {
7880Sstevel@tonic-gate 		switch (scf_error()) {
7890Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7900Sstevel@tonic-gate 		default:
7910Sstevel@tonic-gate 			ret = ECONNABORTED;
7920Sstevel@tonic-gate 			break;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7950Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
7960Sstevel@tonic-gate 			ret = EINVAL;
7970Sstevel@tonic-gate 			break;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
8000Sstevel@tonic-gate 			ret = ENOENT;
8010Sstevel@tonic-gate 			break;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
8040Sstevel@tonic-gate 			bad_fail("scf_handle_decode_fmri", scf_error());
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 		goto out;
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (scf_value_set_astring(v_state, str_new_state) != 0 ||
8118823STruong.Q.Nguyen@Sun.COM 	    scf_value_set_astring(v_state_next, str_new_state_next) != 0)
8120Sstevel@tonic-gate 		bad_fail("scf_value_set_astring", scf_error());
8130Sstevel@tonic-gate 
8148823STruong.Q.Nguyen@Sun.COM 	if (aux) {
8158823STruong.Q.Nguyen@Sun.COM 		if (scf_value_set_astring(v_aux, aux) != 0)
8168823STruong.Q.Nguyen@Sun.COM 			bad_fail("scf_value_set_astring", scf_error());
8178823STruong.Q.Nguyen@Sun.COM 	}
8188823STruong.Q.Nguyen@Sun.COM 
8190Sstevel@tonic-gate 	if (scf_value_set_time(v_stime, now.tv_sec, now.tv_usec * 1000) != 0)
8200Sstevel@tonic-gate 		bad_fail("scf_value_set_time", scf_error());
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate add_pg:
8230Sstevel@tonic-gate 	switch (r = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
8240Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg)) {
8250Sstevel@tonic-gate 	case 0:
8260Sstevel@tonic-gate 		break;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	case ECONNABORTED:
8290Sstevel@tonic-gate 	case EPERM:
8300Sstevel@tonic-gate 	case EACCES:
8310Sstevel@tonic-gate 	case EROFS:
8320Sstevel@tonic-gate 		ret = r;
8330Sstevel@tonic-gate 		goto out;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	case ECANCELED:
8360Sstevel@tonic-gate 		ret = ENOENT;
8370Sstevel@tonic-gate 		goto out;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	case EBADF:
8400Sstevel@tonic-gate 	default:
8410Sstevel@tonic-gate 		bad_fail("instance_get_or_add_pg", r);
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	for (;;) {
8450Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) != 0) {
8460Sstevel@tonic-gate 			switch (scf_error()) {
8470Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8480Sstevel@tonic-gate 			default:
8490Sstevel@tonic-gate 				ret = ECONNABORTED;
8500Sstevel@tonic-gate 				goto out;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8530Sstevel@tonic-gate 				goto add_pg;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8560Sstevel@tonic-gate 				ret = EPERM;
8570Sstevel@tonic-gate 				goto out;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8600Sstevel@tonic-gate 				ret = EACCES;
8610Sstevel@tonic-gate 				goto out;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8640Sstevel@tonic-gate 				ret = EROFS;
8650Sstevel@tonic-gate 				goto out;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8680Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
8690Sstevel@tonic-gate 				bad_fail("scf_transaction_start", scf_error());
8700Sstevel@tonic-gate 			}
8710Sstevel@tonic-gate 		}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 		if ((r = tx_set_value(t, t_state, SCF_PROPERTY_STATE,
8740Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, v_state)) != 0 ||
8750Sstevel@tonic-gate 		    (r = tx_set_value(t, t_state_next, SCF_PROPERTY_NEXT_STATE,
8760Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, v_state_next)) != 0 ||
8770Sstevel@tonic-gate 		    (r = tx_set_value(t, t_stime, SCF_PROPERTY_STATE_TIMESTAMP,
8780Sstevel@tonic-gate 		    SCF_TYPE_TIME, v_stime)) != 0) {
8790Sstevel@tonic-gate 			switch (r) {
8800Sstevel@tonic-gate 			case ECONNABORTED:
8810Sstevel@tonic-gate 				ret = ECONNABORTED;
8820Sstevel@tonic-gate 				goto out;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 			case ECANCELED:
8850Sstevel@tonic-gate 				scf_transaction_reset(t);
8860Sstevel@tonic-gate 				goto add_pg;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 			default:
8890Sstevel@tonic-gate 				bad_fail("tx_set_value", r);
8900Sstevel@tonic-gate 			}
8910Sstevel@tonic-gate 		}
8920Sstevel@tonic-gate 
8938823STruong.Q.Nguyen@Sun.COM 		if (aux) {
8948823STruong.Q.Nguyen@Sun.COM 			if ((r = tx_set_value(t, t_aux, SCF_PROPERTY_AUX_STATE,
8958823STruong.Q.Nguyen@Sun.COM 			    SCF_TYPE_ASTRING, v_aux)) != 0) {
8968823STruong.Q.Nguyen@Sun.COM 				switch (r) {
8978823STruong.Q.Nguyen@Sun.COM 				case ECONNABORTED:
8988823STruong.Q.Nguyen@Sun.COM 					ret = ECONNABORTED;
8998823STruong.Q.Nguyen@Sun.COM 					goto out;
9008823STruong.Q.Nguyen@Sun.COM 
9018823STruong.Q.Nguyen@Sun.COM 				case ECANCELED:
9028823STruong.Q.Nguyen@Sun.COM 					scf_transaction_reset(t);
9038823STruong.Q.Nguyen@Sun.COM 					goto add_pg;
9048823STruong.Q.Nguyen@Sun.COM 
9058823STruong.Q.Nguyen@Sun.COM 				default:
9068823STruong.Q.Nguyen@Sun.COM 					bad_fail("tx_set_value", r);
9078823STruong.Q.Nguyen@Sun.COM 				}
9088823STruong.Q.Nguyen@Sun.COM 			}
9098823STruong.Q.Nguyen@Sun.COM 		}
9108823STruong.Q.Nguyen@Sun.COM 
9110Sstevel@tonic-gate 		ret = scf_transaction_commit(t);
9120Sstevel@tonic-gate 		if (ret == 1)
9130Sstevel@tonic-gate 			break;
9140Sstevel@tonic-gate 		if (ret == -1) {
9150Sstevel@tonic-gate 			switch (scf_error()) {
9160Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
9170Sstevel@tonic-gate 			default:
9180Sstevel@tonic-gate 				ret = ECONNABORTED;
9190Sstevel@tonic-gate 				goto out;
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
9220Sstevel@tonic-gate 				ret = EPERM;
9230Sstevel@tonic-gate 				goto out;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
9260Sstevel@tonic-gate 				ret = EACCES;
9270Sstevel@tonic-gate 				goto out;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
9300Sstevel@tonic-gate 				ret = EROFS;
9310Sstevel@tonic-gate 				goto out;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
9340Sstevel@tonic-gate 				bad_fail("scf_transaction_commit", scf_error());
9350Sstevel@tonic-gate 			}
9360Sstevel@tonic-gate 		}
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 		scf_transaction_reset(t);
9390Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
9400Sstevel@tonic-gate 			switch (scf_error()) {
9410Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
9420Sstevel@tonic-gate 			default:
9430Sstevel@tonic-gate 				ret = ECONNABORTED;
9440Sstevel@tonic-gate 				goto out;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
9470Sstevel@tonic-gate 				goto add_pg;
9480Sstevel@tonic-gate 			}
9490Sstevel@tonic-gate 		}
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	id->i_state = new_state;
9530Sstevel@tonic-gate 	id->i_next_state = new_state_next;
9540Sstevel@tonic-gate 	ret = 0;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate out:
9570Sstevel@tonic-gate 	scf_transaction_destroy(t);
9580Sstevel@tonic-gate 	scf_entry_destroy(t_state);
9590Sstevel@tonic-gate 	scf_entry_destroy(t_state_next);
9600Sstevel@tonic-gate 	scf_entry_destroy(t_stime);
9610Sstevel@tonic-gate 	scf_entry_destroy(t_aux);
9620Sstevel@tonic-gate 	scf_value_destroy(v_state);
9630Sstevel@tonic-gate 	scf_value_destroy(v_state_next);
9640Sstevel@tonic-gate 	scf_value_destroy(v_stime);
9650Sstevel@tonic-gate 	scf_value_destroy(v_aux);
9660Sstevel@tonic-gate 	scf_pg_destroy(pg);
9670Sstevel@tonic-gate 	scf_instance_destroy(s_inst);
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	return (ret);
9700Sstevel@tonic-gate }
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate /*
9730Sstevel@tonic-gate  * Fails with
9740Sstevel@tonic-gate  *   EINVAL - type is invalid
9750Sstevel@tonic-gate  *   ENOMEM
9760Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
9770Sstevel@tonic-gate  *   EBADF - s_inst is not set
9780Sstevel@tonic-gate  *   ECANCELED - s_inst is deleted
9790Sstevel@tonic-gate  *   EPERM - permission denied
9800Sstevel@tonic-gate  *   EACCES - backend access denied
9810Sstevel@tonic-gate  *   EROFS - backend readonly
9820Sstevel@tonic-gate  */
9830Sstevel@tonic-gate int
9840Sstevel@tonic-gate restarter_remove_contract(scf_instance_t *s_inst, ctid_t contract_id,
9850Sstevel@tonic-gate     restarter_contract_type_t type)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 	scf_handle_t *h;
9880Sstevel@tonic-gate 	scf_transaction_t *t = NULL;
9890Sstevel@tonic-gate 	scf_transaction_entry_t *t_cid = NULL;
9900Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
9910Sstevel@tonic-gate 	scf_property_t *prop = NULL;
9920Sstevel@tonic-gate 	scf_value_t *val;
9930Sstevel@tonic-gate 	scf_iter_t *iter = NULL;
9940Sstevel@tonic-gate 	const char *pname;
9950Sstevel@tonic-gate 	int ret = 0, primary;
9960Sstevel@tonic-gate 	uint64_t c;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	switch (type) {
9990Sstevel@tonic-gate 	case RESTARTER_CONTRACT_PRIMARY:
10000Sstevel@tonic-gate 		primary = 1;
10010Sstevel@tonic-gate 		break;
10020Sstevel@tonic-gate 	case RESTARTER_CONTRACT_TRANSIENT:
10030Sstevel@tonic-gate 		primary = 0;
10040Sstevel@tonic-gate 		break;
10050Sstevel@tonic-gate 	default:
10060Sstevel@tonic-gate 		return (EINVAL);
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	h = scf_instance_handle(s_inst);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	pg = scf_pg_create(h);
10120Sstevel@tonic-gate 	prop = scf_property_create(h);
10130Sstevel@tonic-gate 	iter = scf_iter_create(h);
10140Sstevel@tonic-gate 	t = scf_transaction_create(h);
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (pg == NULL || prop == NULL || iter == NULL || t == NULL) {
10170Sstevel@tonic-gate 		ret = ENOMEM;
10180Sstevel@tonic-gate 		goto remove_contract_cleanup;
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate add:
10220Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
10230Sstevel@tonic-gate 	ret = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
10240Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
10250Sstevel@tonic-gate 	if (ret != 0)
10260Sstevel@tonic-gate 		goto remove_contract_cleanup;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	pname = primary? SCF_PROPERTY_CONTRACT :
10290Sstevel@tonic-gate 	    SCF_PROPERTY_TRANSIENT_CONTRACT;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	for (;;) {
10320Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) != 0) {
10330Sstevel@tonic-gate 			switch (scf_error()) {
10340Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
10350Sstevel@tonic-gate 			default:
10360Sstevel@tonic-gate 				ret = ECONNABORTED;
10370Sstevel@tonic-gate 				goto remove_contract_cleanup;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
10400Sstevel@tonic-gate 				goto add;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
10430Sstevel@tonic-gate 				ret = EPERM;
10440Sstevel@tonic-gate 				goto remove_contract_cleanup;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
10470Sstevel@tonic-gate 				ret = EACCES;
10480Sstevel@tonic-gate 				goto remove_contract_cleanup;
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
10510Sstevel@tonic-gate 				ret = EROFS;
10520Sstevel@tonic-gate 				goto remove_contract_cleanup;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
10550Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
10560Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
10570Sstevel@tonic-gate 				bad_fail("scf_transaction_start", scf_error());
10580Sstevel@tonic-gate 			}
10590Sstevel@tonic-gate 		}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 		t_cid = scf_entry_create(h);
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 		if (scf_pg_get_property(pg, pname, prop) == 0) {
10640Sstevel@tonic-gate replace:
10650Sstevel@tonic-gate 			if (scf_transaction_property_change_type(t, t_cid,
10660Sstevel@tonic-gate 			    pname, SCF_TYPE_COUNT) != 0) {
10670Sstevel@tonic-gate 				switch (scf_error()) {
10680Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
10690Sstevel@tonic-gate 				default:
10700Sstevel@tonic-gate 					ret = ECONNABORTED;
10710Sstevel@tonic-gate 					goto remove_contract_cleanup;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
10740Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
10750Sstevel@tonic-gate 					goto add;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
10780Sstevel@tonic-gate 					goto new;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
10810Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
10820Sstevel@tonic-gate 				case SCF_ERROR_IN_USE:
10830Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
10840Sstevel@tonic-gate 					bad_fail(
10850Sstevel@tonic-gate 					"scf_transaction_property_changetype",
10860Sstevel@tonic-gate 					    scf_error());
10870Sstevel@tonic-gate 				}
10880Sstevel@tonic-gate 			}
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) == 0) {
10910Sstevel@tonic-gate 				if (scf_iter_property_values(iter, prop) != 0) {
10920Sstevel@tonic-gate 					switch (scf_error()) {
10930Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
10940Sstevel@tonic-gate 					default:
10950Sstevel@tonic-gate 						ret = ECONNABORTED;
10960Sstevel@tonic-gate 						goto remove_contract_cleanup;
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 					case SCF_ERROR_NOT_SET:
10990Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
11000Sstevel@tonic-gate 						bad_fail(
11010Sstevel@tonic-gate 						    "scf_iter_property_values",
11020Sstevel@tonic-gate 						    scf_error());
11030Sstevel@tonic-gate 					}
11040Sstevel@tonic-gate 				}
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate next_val:
11070Sstevel@tonic-gate 				val = scf_value_create(h);
11080Sstevel@tonic-gate 				if (val == NULL) {
11090Sstevel@tonic-gate 					assert(scf_error() ==
11100Sstevel@tonic-gate 					    SCF_ERROR_NO_MEMORY);
11110Sstevel@tonic-gate 					ret = ENOMEM;
11120Sstevel@tonic-gate 					goto remove_contract_cleanup;
11130Sstevel@tonic-gate 				}
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 				ret = scf_iter_next_value(iter, val);
11160Sstevel@tonic-gate 				if (ret == -1) {
11170Sstevel@tonic-gate 					switch (scf_error()) {
11180Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
11190Sstevel@tonic-gate 						ret = ECONNABORTED;
11200Sstevel@tonic-gate 						goto remove_contract_cleanup;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 					case SCF_ERROR_DELETED:
11230Sstevel@tonic-gate 						scf_value_destroy(val);
11240Sstevel@tonic-gate 						goto add;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
11270Sstevel@tonic-gate 					case SCF_ERROR_INVALID_ARGUMENT:
11285040Swesolows 					case SCF_ERROR_PERMISSION_DENIED:
11290Sstevel@tonic-gate 					default:
11300Sstevel@tonic-gate 						bad_fail("scf_iter_next_value",
11310Sstevel@tonic-gate 						    scf_error());
11320Sstevel@tonic-gate 					}
11330Sstevel@tonic-gate 				}
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 				if (ret == 1) {
11360Sstevel@tonic-gate 					ret = scf_value_get_count(val, &c);
11370Sstevel@tonic-gate 					assert(ret == 0);
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 					if (c != contract_id) {
11400Sstevel@tonic-gate 						ret = scf_entry_add_value(t_cid,
11410Sstevel@tonic-gate 						    val);
11420Sstevel@tonic-gate 						assert(ret == 0);
11430Sstevel@tonic-gate 					} else {
11440Sstevel@tonic-gate 						scf_value_destroy(val);
11450Sstevel@tonic-gate 					}
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 					goto next_val;
11480Sstevel@tonic-gate 				}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 				scf_value_destroy(val);
11510Sstevel@tonic-gate 			} else {
11520Sstevel@tonic-gate 				switch (scf_error()) {
11530Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
11540Sstevel@tonic-gate 				default:
11550Sstevel@tonic-gate 					ret = ECONNABORTED;
11560Sstevel@tonic-gate 					goto remove_contract_cleanup;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 				case SCF_ERROR_TYPE_MISMATCH:
11590Sstevel@tonic-gate 					break;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
11620Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
11630Sstevel@tonic-gate 					bad_fail("scf_property_is_type",
11640Sstevel@tonic-gate 					    scf_error());
11650Sstevel@tonic-gate 				}
11660Sstevel@tonic-gate 			}
11670Sstevel@tonic-gate 		} else {
11680Sstevel@tonic-gate 			switch (scf_error()) {
11690Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
11700Sstevel@tonic-gate 			default:
11710Sstevel@tonic-gate 				ret = ECONNABORTED;
11720Sstevel@tonic-gate 				goto remove_contract_cleanup;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
11750Sstevel@tonic-gate 				scf_entry_destroy(t_cid);
11760Sstevel@tonic-gate 				goto add;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
11790Sstevel@tonic-gate 				break;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
11820Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
11830Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
11840Sstevel@tonic-gate 				bad_fail("scf_pg_get_property", scf_error());
11850Sstevel@tonic-gate 			}
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate new:
11880Sstevel@tonic-gate 			if (scf_transaction_property_new(t, t_cid, pname,
11890Sstevel@tonic-gate 			    SCF_TYPE_COUNT) != 0) {
11900Sstevel@tonic-gate 				switch (scf_error()) {
11910Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
11920Sstevel@tonic-gate 				default:
11930Sstevel@tonic-gate 					ret = ECONNABORTED;
11940Sstevel@tonic-gate 					goto remove_contract_cleanup;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
11970Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
11980Sstevel@tonic-gate 					goto add;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 				case SCF_ERROR_EXISTS:
12010Sstevel@tonic-gate 					goto replace;
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
12040Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
12050Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
12060Sstevel@tonic-gate 					bad_fail("scf_transaction_property_new",
12070Sstevel@tonic-gate 					    scf_error());
12080Sstevel@tonic-gate 				}
12090Sstevel@tonic-gate 			}
12100Sstevel@tonic-gate 		}
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 		ret = scf_transaction_commit(t);
12130Sstevel@tonic-gate 		if (ret == -1) {
12140Sstevel@tonic-gate 			switch (scf_error()) {
12150Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
12160Sstevel@tonic-gate 			default:
12170Sstevel@tonic-gate 				ret = ECONNABORTED;
12180Sstevel@tonic-gate 				goto remove_contract_cleanup;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
12210Sstevel@tonic-gate 				goto add;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
12240Sstevel@tonic-gate 				ret = EPERM;
12250Sstevel@tonic-gate 				goto remove_contract_cleanup;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
12280Sstevel@tonic-gate 				ret = EACCES;
12290Sstevel@tonic-gate 				goto remove_contract_cleanup;
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
12320Sstevel@tonic-gate 				ret = EROFS;
12330Sstevel@tonic-gate 				goto remove_contract_cleanup;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
12360Sstevel@tonic-gate 				bad_fail("scf_transaction_commit", scf_error());
12370Sstevel@tonic-gate 			}
12380Sstevel@tonic-gate 		}
12390Sstevel@tonic-gate 		if (ret == 1) {
12400Sstevel@tonic-gate 			ret = 0;
12410Sstevel@tonic-gate 			break;
12420Sstevel@tonic-gate 		}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 		scf_transaction_destroy_children(t);
12450Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
12460Sstevel@tonic-gate 			switch (scf_error()) {
12470Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
12480Sstevel@tonic-gate 			default:
12490Sstevel@tonic-gate 				ret = ECONNABORTED;
12500Sstevel@tonic-gate 				goto remove_contract_cleanup;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
12530Sstevel@tonic-gate 				goto add;
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
12560Sstevel@tonic-gate 				bad_fail("scf_pg_update", scf_error());
12570Sstevel@tonic-gate 			}
12580Sstevel@tonic-gate 		}
12590Sstevel@tonic-gate 	}
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate remove_contract_cleanup:
12620Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
12630Sstevel@tonic-gate 	scf_transaction_destroy(t);
12640Sstevel@tonic-gate 	scf_iter_destroy(iter);
12650Sstevel@tonic-gate 	scf_property_destroy(prop);
12660Sstevel@tonic-gate 	scf_pg_destroy(pg);
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	return (ret);
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate /*
12720Sstevel@tonic-gate  * Fails with
12730Sstevel@tonic-gate  *   EINVAL - type is invalid
12740Sstevel@tonic-gate  *   ENOMEM
12750Sstevel@tonic-gate  *   ECONNABORTED - repository disconnection
12760Sstevel@tonic-gate  *   EBADF - s_inst is not set
12770Sstevel@tonic-gate  *   ECANCELED - s_inst is deleted
12780Sstevel@tonic-gate  *   EPERM
12790Sstevel@tonic-gate  *   EACCES
12800Sstevel@tonic-gate  *   EROFS
12810Sstevel@tonic-gate  */
12820Sstevel@tonic-gate int
12830Sstevel@tonic-gate restarter_store_contract(scf_instance_t *s_inst, ctid_t contract_id,
12840Sstevel@tonic-gate     restarter_contract_type_t type)
12850Sstevel@tonic-gate {
12860Sstevel@tonic-gate 	scf_handle_t *h;
12870Sstevel@tonic-gate 	scf_transaction_t *t = NULL;
12880Sstevel@tonic-gate 	scf_transaction_entry_t *t_cid = NULL;
12890Sstevel@tonic-gate 	scf_value_t *val;
12900Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
12910Sstevel@tonic-gate 	scf_property_t *prop = NULL;
12920Sstevel@tonic-gate 	scf_iter_t *iter = NULL;
12930Sstevel@tonic-gate 	const char *pname;
12940Sstevel@tonic-gate 	int ret = 0, primary;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	if (type == RESTARTER_CONTRACT_PRIMARY)
12970Sstevel@tonic-gate 		primary = 1;
12980Sstevel@tonic-gate 	else if (type == RESTARTER_CONTRACT_TRANSIENT)
12990Sstevel@tonic-gate 		primary = 0;
13000Sstevel@tonic-gate 	else
13010Sstevel@tonic-gate 		return (EINVAL);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	h = scf_instance_handle(s_inst);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	pg = scf_pg_create(h);
13060Sstevel@tonic-gate 	prop = scf_property_create(h);
13070Sstevel@tonic-gate 	iter = scf_iter_create(h);
13080Sstevel@tonic-gate 	t = scf_transaction_create(h);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	if (pg == NULL || prop == NULL || iter == NULL || t == NULL) {
13110Sstevel@tonic-gate 		ret = ENOMEM;
13120Sstevel@tonic-gate 		goto out;
13130Sstevel@tonic-gate 	}
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate add:
13160Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
13170Sstevel@tonic-gate 	ret = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
13180Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
13190Sstevel@tonic-gate 	if (ret != 0)
13200Sstevel@tonic-gate 		goto out;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	pname = primary ? SCF_PROPERTY_CONTRACT :
13230Sstevel@tonic-gate 	    SCF_PROPERTY_TRANSIENT_CONTRACT;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	for (;;) {
13260Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) != 0) {
13270Sstevel@tonic-gate 			switch (scf_error()) {
13280Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
13290Sstevel@tonic-gate 			default:
13300Sstevel@tonic-gate 				ret = ECONNABORTED;
13310Sstevel@tonic-gate 				goto out;
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
13340Sstevel@tonic-gate 				goto add;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
13370Sstevel@tonic-gate 				ret = EPERM;
13380Sstevel@tonic-gate 				goto out;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
13410Sstevel@tonic-gate 				ret = EACCES;
13420Sstevel@tonic-gate 				goto out;
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
13450Sstevel@tonic-gate 				ret = EROFS;
13460Sstevel@tonic-gate 				goto out;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
13490Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
13500Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
13510Sstevel@tonic-gate 				bad_fail("scf_transaction_start", scf_error());
13520Sstevel@tonic-gate 			}
13530Sstevel@tonic-gate 		}
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 		t_cid = scf_entry_create(h);
13560Sstevel@tonic-gate 		if (t_cid == NULL) {
13570Sstevel@tonic-gate 			ret = ENOMEM;
13580Sstevel@tonic-gate 			goto out;
13590Sstevel@tonic-gate 		}
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 		if (scf_pg_get_property(pg, pname, prop) == 0) {
13620Sstevel@tonic-gate replace:
13630Sstevel@tonic-gate 			if (scf_transaction_property_change_type(t, t_cid,
13640Sstevel@tonic-gate 			    pname, SCF_TYPE_COUNT) != 0) {
13650Sstevel@tonic-gate 				switch (scf_error()) {
13660Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
13670Sstevel@tonic-gate 				default:
13680Sstevel@tonic-gate 					ret = ECONNABORTED;
13690Sstevel@tonic-gate 					goto out;
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
13720Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
13730Sstevel@tonic-gate 					goto add;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
13760Sstevel@tonic-gate 					goto new;
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
13790Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
13800Sstevel@tonic-gate 				case SCF_ERROR_IN_USE:
13810Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
13820Sstevel@tonic-gate 					bad_fail(
13830Sstevel@tonic-gate 					"scf_transaction_propert_change_type",
13840Sstevel@tonic-gate 					    scf_error());
13850Sstevel@tonic-gate 				}
13860Sstevel@tonic-gate 			}
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) == 0) {
13890Sstevel@tonic-gate 				if (scf_iter_property_values(iter, prop) != 0) {
13900Sstevel@tonic-gate 					switch (scf_error()) {
13910Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
13920Sstevel@tonic-gate 					default:
13930Sstevel@tonic-gate 						ret = ECONNABORTED;
13940Sstevel@tonic-gate 						goto out;
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 					case SCF_ERROR_NOT_SET:
13970Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
13980Sstevel@tonic-gate 						bad_fail(
13990Sstevel@tonic-gate 						    "scf_iter_property_values",
14000Sstevel@tonic-gate 						    scf_error());
14010Sstevel@tonic-gate 					}
14020Sstevel@tonic-gate 				}
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate next_val:
14050Sstevel@tonic-gate 				val = scf_value_create(h);
14060Sstevel@tonic-gate 				if (val == NULL) {
14070Sstevel@tonic-gate 					assert(scf_error() ==
14080Sstevel@tonic-gate 					    SCF_ERROR_NO_MEMORY);
14090Sstevel@tonic-gate 					ret = ENOMEM;
14100Sstevel@tonic-gate 					goto out;
14110Sstevel@tonic-gate 				}
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 				ret = scf_iter_next_value(iter, val);
14140Sstevel@tonic-gate 				if (ret == -1) {
14150Sstevel@tonic-gate 					switch (scf_error()) {
14160Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
14170Sstevel@tonic-gate 					default:
14180Sstevel@tonic-gate 						ret = ECONNABORTED;
14190Sstevel@tonic-gate 						goto out;
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 					case SCF_ERROR_DELETED:
14220Sstevel@tonic-gate 						scf_value_destroy(val);
14230Sstevel@tonic-gate 						goto add;
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
14260Sstevel@tonic-gate 					case SCF_ERROR_INVALID_ARGUMENT:
14275040Swesolows 					case SCF_ERROR_PERMISSION_DENIED:
14280Sstevel@tonic-gate 						bad_fail(
14290Sstevel@tonic-gate 						    "scf_iter_next_value",
14300Sstevel@tonic-gate 						    scf_error());
14310Sstevel@tonic-gate 					}
14320Sstevel@tonic-gate 				}
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 				if (ret == 1) {
14350Sstevel@tonic-gate 					ret = scf_entry_add_value(t_cid, val);
14360Sstevel@tonic-gate 					assert(ret == 0);
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 					goto next_val;
14390Sstevel@tonic-gate 				}
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 				scf_value_destroy(val);
14420Sstevel@tonic-gate 			} else {
14430Sstevel@tonic-gate 				switch (scf_error()) {
14440Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
14450Sstevel@tonic-gate 				default:
14460Sstevel@tonic-gate 					ret = ECONNABORTED;
14470Sstevel@tonic-gate 					goto out;
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 				case SCF_ERROR_TYPE_MISMATCH:
14500Sstevel@tonic-gate 					break;
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
14530Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
14540Sstevel@tonic-gate 					bad_fail("scf_property_is_type",
14550Sstevel@tonic-gate 					    scf_error());
14560Sstevel@tonic-gate 				}
14570Sstevel@tonic-gate 			}
14580Sstevel@tonic-gate 		} else {
14590Sstevel@tonic-gate 			switch (scf_error()) {
14600Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
14610Sstevel@tonic-gate 			default:
14620Sstevel@tonic-gate 				ret = ECONNABORTED;
14630Sstevel@tonic-gate 				goto out;
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
14660Sstevel@tonic-gate 				scf_entry_destroy(t_cid);
14670Sstevel@tonic-gate 				goto add;
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
14700Sstevel@tonic-gate 				break;
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
14730Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
14740Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
14750Sstevel@tonic-gate 				bad_fail("scf_pg_get_property", scf_error());
14760Sstevel@tonic-gate 			}
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate new:
14790Sstevel@tonic-gate 			if (scf_transaction_property_new(t, t_cid, pname,
14800Sstevel@tonic-gate 			    SCF_TYPE_COUNT) != 0) {
14810Sstevel@tonic-gate 				switch (scf_error()) {
14820Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
14830Sstevel@tonic-gate 				default:
14840Sstevel@tonic-gate 					ret = ECONNABORTED;
14850Sstevel@tonic-gate 					goto out;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
14880Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
14890Sstevel@tonic-gate 					goto add;
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 				case SCF_ERROR_EXISTS:
14920Sstevel@tonic-gate 					goto replace;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
14950Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
14960Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
14970Sstevel@tonic-gate 					bad_fail("scf_transaction_property_new",
14980Sstevel@tonic-gate 					    scf_error());
14990Sstevel@tonic-gate 				}
15000Sstevel@tonic-gate 			}
15010Sstevel@tonic-gate 		}
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 		val = scf_value_create(h);
15040Sstevel@tonic-gate 		if (val == NULL) {
15050Sstevel@tonic-gate 			assert(scf_error() == SCF_ERROR_NO_MEMORY);
15060Sstevel@tonic-gate 			ret = ENOMEM;
15070Sstevel@tonic-gate 			goto out;
15080Sstevel@tonic-gate 		}
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 		scf_value_set_count(val, contract_id);
15110Sstevel@tonic-gate 		ret = scf_entry_add_value(t_cid, val);
15120Sstevel@tonic-gate 		assert(ret == 0);
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 		ret = scf_transaction_commit(t);
15150Sstevel@tonic-gate 		if (ret == -1) {
15160Sstevel@tonic-gate 			switch (scf_error()) {
15170Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
15180Sstevel@tonic-gate 			default:
15190Sstevel@tonic-gate 				ret = ECONNABORTED;
15200Sstevel@tonic-gate 				goto out;
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
15230Sstevel@tonic-gate 				goto add;
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
15260Sstevel@tonic-gate 				ret = EPERM;
15270Sstevel@tonic-gate 				goto out;
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
15300Sstevel@tonic-gate 				ret = EACCES;
15310Sstevel@tonic-gate 				goto out;
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
15340Sstevel@tonic-gate 				ret = EROFS;
15350Sstevel@tonic-gate 				goto out;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
15380Sstevel@tonic-gate 				bad_fail("scf_transaction_commit", scf_error());
15390Sstevel@tonic-gate 			}
15400Sstevel@tonic-gate 		}
15410Sstevel@tonic-gate 		if (ret == 1) {
15420Sstevel@tonic-gate 			ret = 0;
15430Sstevel@tonic-gate 			break;
15440Sstevel@tonic-gate 		}
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 		scf_transaction_destroy_children(t);
15470Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
15480Sstevel@tonic-gate 			switch (scf_error()) {
15490Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
15500Sstevel@tonic-gate 			default:
15510Sstevel@tonic-gate 				ret = ECONNABORTED;
15520Sstevel@tonic-gate 				goto out;
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
15550Sstevel@tonic-gate 				goto add;
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
15580Sstevel@tonic-gate 				bad_fail("scf_pg_update", scf_error());
15590Sstevel@tonic-gate 			}
15600Sstevel@tonic-gate 		}
15610Sstevel@tonic-gate 	}
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate out:
15640Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
15650Sstevel@tonic-gate 	scf_transaction_destroy(t);
15660Sstevel@tonic-gate 	scf_iter_destroy(iter);
15670Sstevel@tonic-gate 	scf_property_destroy(prop);
15680Sstevel@tonic-gate 	scf_pg_destroy(pg);
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	return (ret);
15710Sstevel@tonic-gate }
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate int
15740Sstevel@tonic-gate restarter_rm_libs_loadable()
15750Sstevel@tonic-gate {
15760Sstevel@tonic-gate 	void *libhndl;
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	if (method_context_safety)
15790Sstevel@tonic-gate 		return (1);
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 	if ((libhndl = dlopen("libpool.so", RTLD_LAZY | RTLD_LOCAL)) == NULL)
15820Sstevel@tonic-gate 		return (0);
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	(void) dlclose(libhndl);
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	if ((libhndl = dlopen("libproject.so", RTLD_LAZY | RTLD_LOCAL)) == NULL)
15870Sstevel@tonic-gate 		return (0);
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	(void) dlclose(libhndl);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	method_context_safety = 1;
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	return (1);
15940Sstevel@tonic-gate }
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate static int
15970Sstevel@tonic-gate get_astring_val(scf_propertygroup_t *pg, const char *name, char *buf,
15980Sstevel@tonic-gate     size_t bufsz, scf_property_t *prop, scf_value_t *val)
15990Sstevel@tonic-gate {
16000Sstevel@tonic-gate 	ssize_t szret;
16010Sstevel@tonic-gate 
1602*9263SSean.Wilcox@Sun.COM 	if (pg == NULL)
1603*9263SSean.Wilcox@Sun.COM 		return (-1);
1604*9263SSean.Wilcox@Sun.COM 
16050Sstevel@tonic-gate 	if (scf_pg_get_property(pg, name, prop) != SCF_SUCCESS) {
16060Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
16070Sstevel@tonic-gate 			uu_die(rcbroken);
16080Sstevel@tonic-gate 		return (-1);
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
16120Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
16130Sstevel@tonic-gate 			uu_die(rcbroken);
16140Sstevel@tonic-gate 		return (-1);
16150Sstevel@tonic-gate 	}
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	szret = scf_value_get_astring(val, buf, bufsz);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	return (szret >= 0 ? 0 : -1);
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate 
16228823STruong.Q.Nguyen@Sun.COM static int
16238823STruong.Q.Nguyen@Sun.COM get_boolean_val(scf_propertygroup_t *pg, const char *name, uint8_t *b,
16248823STruong.Q.Nguyen@Sun.COM     scf_property_t *prop, scf_value_t *val)
16258823STruong.Q.Nguyen@Sun.COM {
16268823STruong.Q.Nguyen@Sun.COM 	if (scf_pg_get_property(pg, name, prop) != SCF_SUCCESS) {
16278823STruong.Q.Nguyen@Sun.COM 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
16288823STruong.Q.Nguyen@Sun.COM 			uu_die(rcbroken);
16298823STruong.Q.Nguyen@Sun.COM 		return (-1);
16308823STruong.Q.Nguyen@Sun.COM 	}
16318823STruong.Q.Nguyen@Sun.COM 
16328823STruong.Q.Nguyen@Sun.COM 	if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
16338823STruong.Q.Nguyen@Sun.COM 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
16348823STruong.Q.Nguyen@Sun.COM 			uu_die(rcbroken);
16358823STruong.Q.Nguyen@Sun.COM 		return (-1);
16368823STruong.Q.Nguyen@Sun.COM 	}
16378823STruong.Q.Nguyen@Sun.COM 
16388823STruong.Q.Nguyen@Sun.COM 	if (scf_value_get_boolean(val, b))
16398823STruong.Q.Nguyen@Sun.COM 		return (-1);
16408823STruong.Q.Nguyen@Sun.COM 
16418823STruong.Q.Nguyen@Sun.COM 	return (0);
16428823STruong.Q.Nguyen@Sun.COM }
16438823STruong.Q.Nguyen@Sun.COM 
16440Sstevel@tonic-gate /*
16450Sstevel@tonic-gate  * Try to load mcp->pwd, if it isn't already.
16460Sstevel@tonic-gate  * Fails with
16470Sstevel@tonic-gate  *   ENOMEM - malloc() failed
16480Sstevel@tonic-gate  *   ENOENT - no entry found
16490Sstevel@tonic-gate  *   EIO - I/O error
16500Sstevel@tonic-gate  *   EMFILE - process out of file descriptors
16510Sstevel@tonic-gate  *   ENFILE - system out of file handles
16520Sstevel@tonic-gate  */
16530Sstevel@tonic-gate static int
16540Sstevel@tonic-gate lookup_pwd(struct method_context *mcp)
16550Sstevel@tonic-gate {
16560Sstevel@tonic-gate 	struct passwd *pwdp;
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	if (mcp->pwbuf != NULL && mcp->pwd.pw_uid == mcp->uid)
16590Sstevel@tonic-gate 		return (0);
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	if (mcp->pwbuf == NULL) {
16620Sstevel@tonic-gate 		mcp->pwbufsz = sysconf(_SC_GETPW_R_SIZE_MAX);
16630Sstevel@tonic-gate 		assert(mcp->pwbufsz >= 0);
16640Sstevel@tonic-gate 		mcp->pwbuf = malloc(mcp->pwbufsz);
16650Sstevel@tonic-gate 		if (mcp->pwbuf == NULL)
16660Sstevel@tonic-gate 			return (ENOMEM);
16670Sstevel@tonic-gate 	}
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	do {
16700Sstevel@tonic-gate 		errno = 0;
16710Sstevel@tonic-gate 		pwdp = getpwuid_r(mcp->uid, &mcp->pwd, mcp->pwbuf,
16720Sstevel@tonic-gate 		    mcp->pwbufsz);
16730Sstevel@tonic-gate 	} while (pwdp == NULL && errno == EINTR);
16740Sstevel@tonic-gate 	if (pwdp != NULL)
16750Sstevel@tonic-gate 		return (0);
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	free(mcp->pwbuf);
16780Sstevel@tonic-gate 	mcp->pwbuf = NULL;
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	switch (errno) {
16810Sstevel@tonic-gate 	case 0:
16820Sstevel@tonic-gate 	default:
16830Sstevel@tonic-gate 		/*
16840Sstevel@tonic-gate 		 * Until bug 5065780 is fixed, getpwuid_r() can fail with
16850Sstevel@tonic-gate 		 * ENOENT, particularly on the miniroot.  Since the
16860Sstevel@tonic-gate 		 * documentation is inaccurate, we'll return ENOENT for unknown
16870Sstevel@tonic-gate 		 * errors.
16880Sstevel@tonic-gate 		 */
16890Sstevel@tonic-gate 		return (ENOENT);
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	case EIO:
16920Sstevel@tonic-gate 	case EMFILE:
16930Sstevel@tonic-gate 	case ENFILE:
16940Sstevel@tonic-gate 		return (errno);
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	case ERANGE:
16970Sstevel@tonic-gate 		bad_fail("getpwuid_r", errno);
16980Sstevel@tonic-gate 		/* NOTREACHED */
16990Sstevel@tonic-gate 	}
17000Sstevel@tonic-gate }
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate /*
17030Sstevel@tonic-gate  * Get the user id for str.  Returns 0 on success or
17040Sstevel@tonic-gate  *   ERANGE	the uid is too big
17050Sstevel@tonic-gate  *   EINVAL	the string starts with a digit, but is not a valid uid
17060Sstevel@tonic-gate  *   ENOMEM	out of memory
17070Sstevel@tonic-gate  *   ENOENT	no passwd entry for str
17080Sstevel@tonic-gate  *   EIO	an I/O error has occurred
17090Sstevel@tonic-gate  *   EMFILE/ENFILE  out of file descriptors
17100Sstevel@tonic-gate  */
17110Sstevel@tonic-gate int
17120Sstevel@tonic-gate get_uid(const char *str, struct method_context *ci, uid_t *uidp)
17130Sstevel@tonic-gate {
17140Sstevel@tonic-gate 	if (isdigit(str[0])) {
17150Sstevel@tonic-gate 		uid_t uid;
17160Sstevel@tonic-gate 		char *cp;
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 		errno = 0;
17190Sstevel@tonic-gate 		uid = strtol(str, &cp, 10);
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 		if (uid == 0 && errno != 0) {
17220Sstevel@tonic-gate 			assert(errno != EINVAL);
17230Sstevel@tonic-gate 			return (errno);
17240Sstevel@tonic-gate 		}
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 		for (; *cp != '\0'; ++cp)
17270Sstevel@tonic-gate 			if (*cp != ' ' || *cp != '\t')
17280Sstevel@tonic-gate 				return (EINVAL);
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 		if (uid > UID_MAX)
17310Sstevel@tonic-gate 			return (EINVAL);
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 		*uidp = uid;
17340Sstevel@tonic-gate 		return (0);
17350Sstevel@tonic-gate 	} else {
17360Sstevel@tonic-gate 		struct passwd *pwdp;
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 		if (ci->pwbuf == NULL) {
17390Sstevel@tonic-gate 			ci->pwbufsz = sysconf(_SC_GETPW_R_SIZE_MAX);
17400Sstevel@tonic-gate 			ci->pwbuf = malloc(ci->pwbufsz);
17410Sstevel@tonic-gate 			if (ci->pwbuf == NULL)
17420Sstevel@tonic-gate 				return (ENOMEM);
17430Sstevel@tonic-gate 		}
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 		do {
17460Sstevel@tonic-gate 			errno = 0;
17470Sstevel@tonic-gate 			pwdp =
17480Sstevel@tonic-gate 			    getpwnam_r(str, &ci->pwd, ci->pwbuf, ci->pwbufsz);
17490Sstevel@tonic-gate 		} while (pwdp == NULL && errno == EINTR);
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 		if (pwdp != NULL) {
17520Sstevel@tonic-gate 			*uidp = ci->pwd.pw_uid;
17530Sstevel@tonic-gate 			return (0);
17540Sstevel@tonic-gate 		} else {
17550Sstevel@tonic-gate 			free(ci->pwbuf);
17560Sstevel@tonic-gate 			ci->pwbuf = NULL;
17570Sstevel@tonic-gate 			switch (errno) {
17580Sstevel@tonic-gate 			case 0:
17590Sstevel@tonic-gate 				return (ENOENT);
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 			case ENOENT:
17620Sstevel@tonic-gate 			case EIO:
17630Sstevel@tonic-gate 			case EMFILE:
17640Sstevel@tonic-gate 			case ENFILE:
17650Sstevel@tonic-gate 				return (errno);
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 			case ERANGE:
17680Sstevel@tonic-gate 			default:
17690Sstevel@tonic-gate 				bad_fail("getpwnam_r", errno);
17700Sstevel@tonic-gate 				/* NOTREACHED */
17710Sstevel@tonic-gate 			}
17720Sstevel@tonic-gate 		}
17730Sstevel@tonic-gate 	}
17740Sstevel@tonic-gate }
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate gid_t
17770Sstevel@tonic-gate get_gid(const char *str)
17780Sstevel@tonic-gate {
17790Sstevel@tonic-gate 	if (isdigit(str[0])) {
17800Sstevel@tonic-gate 		gid_t gid;
17810Sstevel@tonic-gate 		char *cp;
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 		errno = 0;
17840Sstevel@tonic-gate 		gid = strtol(str, &cp, 10);
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 		if (gid == 0 && errno != 0)
17874321Scasper 			return ((gid_t)-1);
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 		for (; *cp != '\0'; ++cp)
17900Sstevel@tonic-gate 			if (*cp != ' ' || *cp != '\t')
17914321Scasper 				return ((gid_t)-1);
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 		return (gid);
17940Sstevel@tonic-gate 	} else {
17950Sstevel@tonic-gate 		struct group grp, *ret;
17960Sstevel@tonic-gate 		char *buffer;
17970Sstevel@tonic-gate 		size_t buflen;
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 		buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
18000Sstevel@tonic-gate 		buffer = malloc(buflen);
18010Sstevel@tonic-gate 		if (buffer == NULL)
18020Sstevel@tonic-gate 			uu_die(allocfail);
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 		errno = 0;
18050Sstevel@tonic-gate 		ret = getgrnam_r(str, &grp, buffer, buflen);
18060Sstevel@tonic-gate 		free(buffer);
18070Sstevel@tonic-gate 
18084321Scasper 		return (ret == NULL ? (gid_t)-1 : grp.gr_gid);
18090Sstevel@tonic-gate 	}
18100Sstevel@tonic-gate }
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate /*
18130Sstevel@tonic-gate  * Fails with
18140Sstevel@tonic-gate  *   ENOMEM - out of memory
18150Sstevel@tonic-gate  *   ENOENT - no passwd entry
18160Sstevel@tonic-gate  *	      no project entry
18170Sstevel@tonic-gate  *   EIO - an I/O error occurred
18180Sstevel@tonic-gate  *   EMFILE - the process is out of file descriptors
18190Sstevel@tonic-gate  *   ENFILE - the system is out of file handles
18200Sstevel@tonic-gate  *   ERANGE - the project id is out of range
18210Sstevel@tonic-gate  *   EINVAL - str is invalid
18220Sstevel@tonic-gate  *   E2BIG - the project entry was too big
18230Sstevel@tonic-gate  *   -1 - the name service switch is misconfigured
18240Sstevel@tonic-gate  */
18250Sstevel@tonic-gate int
18260Sstevel@tonic-gate get_projid(const char *str, struct method_context *cip)
18270Sstevel@tonic-gate {
18280Sstevel@tonic-gate 	int ret;
18290Sstevel@tonic-gate 	void *buf;
18300Sstevel@tonic-gate 	const size_t bufsz = PROJECT_BUFSZ;
18310Sstevel@tonic-gate 	struct project proj, *pp;
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 	if (strcmp(str, ":default") == 0) {
18340Sstevel@tonic-gate 		if (cip->uid == 0) {
18350Sstevel@tonic-gate 			/* Don't change project for root services */
18360Sstevel@tonic-gate 			cip->project = NULL;
18370Sstevel@tonic-gate 			return (0);
18380Sstevel@tonic-gate 		}
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 		switch (ret = lookup_pwd(cip)) {
18410Sstevel@tonic-gate 		case 0:
18420Sstevel@tonic-gate 			break;
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 		case ENOMEM:
18450Sstevel@tonic-gate 		case ENOENT:
18460Sstevel@tonic-gate 		case EIO:
18470Sstevel@tonic-gate 		case EMFILE:
18480Sstevel@tonic-gate 		case ENFILE:
18490Sstevel@tonic-gate 			return (ret);
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 		default:
18520Sstevel@tonic-gate 			bad_fail("lookup_pwd", ret);
18530Sstevel@tonic-gate 		}
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate 		buf = malloc(bufsz);
18560Sstevel@tonic-gate 		if (buf == NULL)
18570Sstevel@tonic-gate 			return (ENOMEM);
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 		do {
18600Sstevel@tonic-gate 			errno = 0;
18610Sstevel@tonic-gate 			pp = getdefaultproj(cip->pwd.pw_name, &proj, buf,
18620Sstevel@tonic-gate 			    bufsz);
18630Sstevel@tonic-gate 		} while (pp == NULL && errno == EINTR);
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 		/* to be continued ... */
18660Sstevel@tonic-gate 	} else {
18670Sstevel@tonic-gate 		projid_t projid;
18680Sstevel@tonic-gate 		char *cp;
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 		if (!isdigit(str[0])) {
18710Sstevel@tonic-gate 			cip->project = strdup(str);
18720Sstevel@tonic-gate 			return (cip->project != NULL ? 0 : ENOMEM);
18730Sstevel@tonic-gate 		}
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 		errno = 0;
18760Sstevel@tonic-gate 		projid = strtol(str, &cp, 10);
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 		if (projid == 0 && errno != 0) {
18790Sstevel@tonic-gate 			assert(errno == ERANGE);
18800Sstevel@tonic-gate 			return (errno);
18810Sstevel@tonic-gate 		}
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 		for (; *cp != '\0'; ++cp)
18840Sstevel@tonic-gate 			if (*cp != ' ' || *cp != '\t')
18850Sstevel@tonic-gate 				return (EINVAL);
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 		if (projid > MAXPROJID)
18880Sstevel@tonic-gate 			return (ERANGE);
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 		buf = malloc(bufsz);
18910Sstevel@tonic-gate 		if (buf == NULL)
18920Sstevel@tonic-gate 			return (ENOMEM);
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 		do {
18950Sstevel@tonic-gate 			errno = 0;
18960Sstevel@tonic-gate 			pp = getprojbyid(projid, &proj, buf, bufsz);
18970Sstevel@tonic-gate 		} while (pp == NULL && errno == EINTR);
18980Sstevel@tonic-gate 	}
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	if (pp) {
19010Sstevel@tonic-gate 		cip->project = strdup(pp->pj_name);
19020Sstevel@tonic-gate 		free(buf);
19030Sstevel@tonic-gate 		return (cip->project != NULL ? 0 : ENOMEM);
19040Sstevel@tonic-gate 	}
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	free(buf);
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	switch (errno) {
19090Sstevel@tonic-gate 	case 0:
19100Sstevel@tonic-gate 		return (ENOENT);
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	case EIO:
19130Sstevel@tonic-gate 	case EMFILE:
19140Sstevel@tonic-gate 	case ENFILE:
19150Sstevel@tonic-gate 		return (errno);
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	case ERANGE:
19180Sstevel@tonic-gate 		return (E2BIG);
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 	default:
19210Sstevel@tonic-gate 		return (-1);
19220Sstevel@tonic-gate 	}
19230Sstevel@tonic-gate }
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate /*
19260Sstevel@tonic-gate  * Parse the supp_groups property value and populate ci->groups.  Returns
19270Sstevel@tonic-gate  * EINVAL (get_gid() failed for one of the components), E2BIG (the property has
19280Sstevel@tonic-gate  * more than NGROUPS_MAX-1 groups), or 0 on success.
19290Sstevel@tonic-gate  */
19300Sstevel@tonic-gate int
19310Sstevel@tonic-gate get_groups(char *str, struct method_context *ci)
19320Sstevel@tonic-gate {
19330Sstevel@tonic-gate 	char *cp, *end, *next;
19340Sstevel@tonic-gate 	uint_t i;
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 	const char * const whitespace = " \t";
19370Sstevel@tonic-gate 	const char * const illegal = ", \t";
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	if (str[0] == '\0') {
19400Sstevel@tonic-gate 		ci->ngroups = 0;
19410Sstevel@tonic-gate 		return (0);
19420Sstevel@tonic-gate 	}
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 	for (cp = str, i = 0; *cp != '\0'; ) {
19450Sstevel@tonic-gate 		/* skip whitespace */
19460Sstevel@tonic-gate 		cp += strspn(cp, whitespace);
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 		/* find the end */
19490Sstevel@tonic-gate 		end = cp + strcspn(cp, illegal);
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 		/* skip whitespace after end */
19520Sstevel@tonic-gate 		next = end + strspn(end, whitespace);
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 		/* if there's a comma, it separates the fields */
19550Sstevel@tonic-gate 		if (*next == ',')
19560Sstevel@tonic-gate 			++next;
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 		*end = '\0';
19590Sstevel@tonic-gate 
19604321Scasper 		if ((ci->groups[i] = get_gid(cp)) == (gid_t)-1) {
19610Sstevel@tonic-gate 			ci->ngroups = 0;
19620Sstevel@tonic-gate 			return (EINVAL);
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 		++i;
19660Sstevel@tonic-gate 		if (i > NGROUPS_MAX - 1) {
19670Sstevel@tonic-gate 			ci->ngroups = 0;
19680Sstevel@tonic-gate 			return (E2BIG);
19690Sstevel@tonic-gate 		}
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 		cp = next;
19720Sstevel@tonic-gate 	}
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 	ci->ngroups = i;
19750Sstevel@tonic-gate 	return (0);
19760Sstevel@tonic-gate }
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate /*
19790Sstevel@tonic-gate  * Eventually, we will return a structured error in the case of
19800Sstevel@tonic-gate  * retryable or abortable failures such as memory allocation errors and
19810Sstevel@tonic-gate  * repository connection failures.  For now, these failures are just
19820Sstevel@tonic-gate  * encoded in the failure string.
19830Sstevel@tonic-gate  */
19840Sstevel@tonic-gate static const char *
1985*9263SSean.Wilcox@Sun.COM get_profile(scf_propertygroup_t *methpg, scf_propertygroup_t *instpg,
1986*9263SSean.Wilcox@Sun.COM     scf_property_t *prop, scf_value_t *val, const char *cmdline,
1987*9263SSean.Wilcox@Sun.COM     struct method_context *ci)
19880Sstevel@tonic-gate {
19890Sstevel@tonic-gate 	char *buf = ci->vbuf;
19900Sstevel@tonic-gate 	ssize_t buf_sz = ci->vbuf_sz;
19910Sstevel@tonic-gate 	char cmd[PATH_MAX];
19920Sstevel@tonic-gate 	char *cp, *value;
19930Sstevel@tonic-gate 	const char *cmdp;
19940Sstevel@tonic-gate 	execattr_t *eap;
1995*9263SSean.Wilcox@Sun.COM 	const char *errstr = NULL;
1996*9263SSean.Wilcox@Sun.COM 
1997*9263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_PROFILE, buf, buf_sz, prop,
1998*9263SSean.Wilcox@Sun.COM 	    val) == 0 || get_astring_val(instpg, SCF_PROPERTY_PROFILE, buf,
1999*9263SSean.Wilcox@Sun.COM 	    buf_sz, prop, val) == 0))
20000Sstevel@tonic-gate 		return ("Could not get profile property.");
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	/* Extract the command from the command line. */
20030Sstevel@tonic-gate 	cp = strpbrk(cmdline, " \t");
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	if (cp == NULL) {
20060Sstevel@tonic-gate 		cmdp = cmdline;
20070Sstevel@tonic-gate 	} else {
20080Sstevel@tonic-gate 		(void) strncpy(cmd, cmdline, cp - cmdline);
20090Sstevel@tonic-gate 		cmd[cp - cmdline] = '\0';
20100Sstevel@tonic-gate 		cmdp = cmd;
20110Sstevel@tonic-gate 	}
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 	/* Require that cmdp[0] == '/'? */
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	eap = getexecprof(buf, KV_COMMAND, cmdp, GET_ONE);
20160Sstevel@tonic-gate 	if (eap == NULL)
20170Sstevel@tonic-gate 		return ("Could not find profile.");
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	/* Based on pfexec.c */
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 	/* Get the euid first so we don't override ci->pwd for the uid. */
20220Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_EUID_KW)) != NULL) {
20230Sstevel@tonic-gate 		if (get_uid(value, ci, &ci->euid) != 0) {
20244321Scasper 			ci->euid = (uid_t)-1;
20250Sstevel@tonic-gate 			errstr = "Could not interpret profile euid.";
20260Sstevel@tonic-gate 			goto out;
20270Sstevel@tonic-gate 		}
20280Sstevel@tonic-gate 	}
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_UID_KW)) != NULL) {
20310Sstevel@tonic-gate 		if (get_uid(value, ci, &ci->uid) != 0) {
20324321Scasper 			ci->euid = ci->uid = (uid_t)-1;
20330Sstevel@tonic-gate 			errstr = "Could not interpret profile uid.";
20340Sstevel@tonic-gate 			goto out;
20350Sstevel@tonic-gate 		}
20360Sstevel@tonic-gate 		ci->euid = ci->uid;
20370Sstevel@tonic-gate 	}
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_GID_KW)) != NULL) {
20400Sstevel@tonic-gate 		ci->egid = ci->gid = get_gid(value);
20414321Scasper 		if (ci->gid == (gid_t)-1) {
20420Sstevel@tonic-gate 			errstr = "Could not interpret profile gid.";
20430Sstevel@tonic-gate 			goto out;
20440Sstevel@tonic-gate 		}
20450Sstevel@tonic-gate 	}
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_EGID_KW)) != NULL) {
20480Sstevel@tonic-gate 		ci->egid = get_gid(value);
20494321Scasper 		if (ci->egid == (gid_t)-1) {
20500Sstevel@tonic-gate 			errstr = "Could not interpret profile egid.";
20510Sstevel@tonic-gate 			goto out;
20520Sstevel@tonic-gate 		}
20530Sstevel@tonic-gate 	}
20540Sstevel@tonic-gate 
20550Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_LPRIV_KW)) != NULL) {
20560Sstevel@tonic-gate 		ci->lpriv_set = priv_str_to_set(value, ",", NULL);
20570Sstevel@tonic-gate 		if (ci->lpriv_set == NULL) {
20580Sstevel@tonic-gate 			if (errno != EINVAL)
20590Sstevel@tonic-gate 				errstr = ALLOCFAIL;
20600Sstevel@tonic-gate 			else
20610Sstevel@tonic-gate 				errstr = "Could not interpret profile "
20620Sstevel@tonic-gate 				    "limitprivs.";
20630Sstevel@tonic-gate 			goto out;
20640Sstevel@tonic-gate 		}
20650Sstevel@tonic-gate 	}
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_IPRIV_KW)) != NULL) {
20680Sstevel@tonic-gate 		ci->priv_set = priv_str_to_set(value, ",", NULL);
20690Sstevel@tonic-gate 		if (ci->priv_set == NULL) {
20700Sstevel@tonic-gate 			if (errno != EINVAL)
20710Sstevel@tonic-gate 				errstr = ALLOCFAIL;
20720Sstevel@tonic-gate 			else
20730Sstevel@tonic-gate 				errstr = "Could not interpret profile privs.";
20740Sstevel@tonic-gate 			goto out;
20750Sstevel@tonic-gate 		}
20760Sstevel@tonic-gate 	}
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate out:
20790Sstevel@tonic-gate 	free_execattr(eap);
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	return (errstr);
20820Sstevel@tonic-gate }
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate /*
20850Sstevel@tonic-gate  * Eventually, we will return a structured error in the case of
20860Sstevel@tonic-gate  * retryable or abortable failures such as memory allocation errors and
20870Sstevel@tonic-gate  * repository connection failures.  For now, these failures are just
20880Sstevel@tonic-gate  * encoded in the failure string.
20890Sstevel@tonic-gate  */
20900Sstevel@tonic-gate static const char *
2091*9263SSean.Wilcox@Sun.COM get_ids(scf_propertygroup_t *methpg, scf_propertygroup_t *instpg,
2092*9263SSean.Wilcox@Sun.COM     scf_property_t *prop, scf_value_t *val, struct method_context *ci)
20930Sstevel@tonic-gate {
2094*9263SSean.Wilcox@Sun.COM 	char *errstr = NULL;
20950Sstevel@tonic-gate 	char *vbuf = ci->vbuf;
20960Sstevel@tonic-gate 	ssize_t vbuf_sz = ci->vbuf_sz;
20970Sstevel@tonic-gate 	int r;
20980Sstevel@tonic-gate 
2099*9263SSean.Wilcox@Sun.COM 	/*
2100*9263SSean.Wilcox@Sun.COM 	 * This should never happen because the caller should fall through
2101*9263SSean.Wilcox@Sun.COM 	 * another path of just setting the ids to defaults, instead of
2102*9263SSean.Wilcox@Sun.COM 	 * attempting to get the ids here.
2103*9263SSean.Wilcox@Sun.COM 	 */
2104*9263SSean.Wilcox@Sun.COM 	if (methpg == NULL && instpg == NULL) {
2105*9263SSean.Wilcox@Sun.COM 		errstr = "No property groups to get ids from.";
2106*9263SSean.Wilcox@Sun.COM 		goto out;
2107*9263SSean.Wilcox@Sun.COM 	}
2108*9263SSean.Wilcox@Sun.COM 
2109*9263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_USER,
2110*9263SSean.Wilcox@Sun.COM 	    vbuf, vbuf_sz, prop, val) == 0 || get_astring_val(instpg,
2111*9263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_USER, vbuf, vbuf_sz, prop,
2112*9263SSean.Wilcox@Sun.COM 	    val) == 0)) {
21130Sstevel@tonic-gate 		errstr = "Could not get user property.";
21140Sstevel@tonic-gate 		goto out;
21150Sstevel@tonic-gate 	}
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	if (get_uid(vbuf, ci, &ci->uid) != 0) {
21184321Scasper 		ci->uid = (uid_t)-1;
21190Sstevel@tonic-gate 		errstr = "Could not interpret user property.";
21200Sstevel@tonic-gate 		goto out;
21210Sstevel@tonic-gate 	}
21220Sstevel@tonic-gate 
2123*9263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_GROUP, vbuf, vbuf_sz, prop,
2124*9263SSean.Wilcox@Sun.COM 	    val) == 0 || get_astring_val(instpg, SCF_PROPERTY_GROUP, vbuf,
2125*9263SSean.Wilcox@Sun.COM 	    vbuf_sz, prop, val) == 0)) {
2126*9263SSean.Wilcox@Sun.COM 		if (scf_error() == SCF_ERROR_NOT_FOUND) {
2127*9263SSean.Wilcox@Sun.COM 			(void) strcpy(vbuf, ":default");
2128*9263SSean.Wilcox@Sun.COM 		} else {
2129*9263SSean.Wilcox@Sun.COM 			errstr = "Could not get group property.";
2130*9263SSean.Wilcox@Sun.COM 			goto out;
2131*9263SSean.Wilcox@Sun.COM 		}
21320Sstevel@tonic-gate 	}
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") != 0) {
21350Sstevel@tonic-gate 		ci->gid = get_gid(vbuf);
21364321Scasper 		if (ci->gid == (gid_t)-1) {
21370Sstevel@tonic-gate 			errstr = "Could not interpret group property.";
21380Sstevel@tonic-gate 			goto out;
21390Sstevel@tonic-gate 		}
21400Sstevel@tonic-gate 	} else {
21410Sstevel@tonic-gate 		switch (r = lookup_pwd(ci)) {
21420Sstevel@tonic-gate 		case 0:
21430Sstevel@tonic-gate 			ci->gid = ci->pwd.pw_gid;
21440Sstevel@tonic-gate 			break;
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 		case ENOENT:
21474321Scasper 			ci->gid = (gid_t)-1;
21480Sstevel@tonic-gate 			errstr = "No passwd entry.";
21490Sstevel@tonic-gate 			goto out;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 		case ENOMEM:
21520Sstevel@tonic-gate 			errstr = "Out of memory.";
21530Sstevel@tonic-gate 			goto out;
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 		case EIO:
21560Sstevel@tonic-gate 		case EMFILE:
21570Sstevel@tonic-gate 		case ENFILE:
21580Sstevel@tonic-gate 			errstr = "getpwuid_r() failed.";
21590Sstevel@tonic-gate 			goto out;
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 		default:
21620Sstevel@tonic-gate 			bad_fail("lookup_pwd", r);
21630Sstevel@tonic-gate 		}
21640Sstevel@tonic-gate 	}
21650Sstevel@tonic-gate 
2166*9263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_SUPP_GROUPS, vbuf, vbuf_sz,
2167*9263SSean.Wilcox@Sun.COM 	    prop, val) == 0 || get_astring_val(instpg,
2168*9263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_SUPP_GROUPS, vbuf, vbuf_sz, prop, val) == 0)) {
2169*9263SSean.Wilcox@Sun.COM 		if (scf_error() == SCF_ERROR_NOT_FOUND) {
2170*9263SSean.Wilcox@Sun.COM 			(void) strcpy(vbuf, ":default");
2171*9263SSean.Wilcox@Sun.COM 		} else {
2172*9263SSean.Wilcox@Sun.COM 			errstr = "Could not get supplemental groups property.";
2173*9263SSean.Wilcox@Sun.COM 			goto out;
2174*9263SSean.Wilcox@Sun.COM 		}
21750Sstevel@tonic-gate 	}
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") != 0) {
21780Sstevel@tonic-gate 		switch (r = get_groups(vbuf, ci)) {
21790Sstevel@tonic-gate 		case 0:
21800Sstevel@tonic-gate 			break;
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 		case EINVAL:
21830Sstevel@tonic-gate 			errstr =
21840Sstevel@tonic-gate 			    "Could not interpret supplemental groups property.";
21850Sstevel@tonic-gate 			goto out;
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 		case E2BIG:
21880Sstevel@tonic-gate 			errstr = "Too many supplemental groups.";
21890Sstevel@tonic-gate 			goto out;
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 		default:
21920Sstevel@tonic-gate 			bad_fail("get_groups", r);
21930Sstevel@tonic-gate 		}
21940Sstevel@tonic-gate 	} else {
21950Sstevel@tonic-gate 		ci->ngroups = -1;
21960Sstevel@tonic-gate 	}
21970Sstevel@tonic-gate 
2198*9263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_PRIVILEGES, vbuf, vbuf_sz,
2199*9263SSean.Wilcox@Sun.COM 	    prop, val) == 0 || get_astring_val(instpg, SCF_PROPERTY_PRIVILEGES,
2200*9263SSean.Wilcox@Sun.COM 	    vbuf, vbuf_sz, prop, val) == 0)) {
2201*9263SSean.Wilcox@Sun.COM 		if (scf_error() == SCF_ERROR_NOT_FOUND) {
2202*9263SSean.Wilcox@Sun.COM 			(void) strcpy(vbuf, ":default");
2203*9263SSean.Wilcox@Sun.COM 		} else {
2204*9263SSean.Wilcox@Sun.COM 			errstr = "Could not get privileges property.";
2205*9263SSean.Wilcox@Sun.COM 			goto out;
2206*9263SSean.Wilcox@Sun.COM 		}
22070Sstevel@tonic-gate 	}
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 	/*
22100Sstevel@tonic-gate 	 * For default privs, we need to keep priv_set == NULL, as
22110Sstevel@tonic-gate 	 * we use this test elsewhere.
22120Sstevel@tonic-gate 	 */
22130Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") != 0) {
22140Sstevel@tonic-gate 		ci->priv_set = priv_str_to_set(vbuf, ",", NULL);
22150Sstevel@tonic-gate 		if (ci->priv_set == NULL) {
22160Sstevel@tonic-gate 			if (errno != EINVAL) {
22170Sstevel@tonic-gate 				errstr = ALLOCFAIL;
22180Sstevel@tonic-gate 			} else {
2219*9263SSean.Wilcox@Sun.COM 				errstr =
2220*9263SSean.Wilcox@Sun.COM 				    "Could not interpret privileges property.";
22210Sstevel@tonic-gate 			}
22220Sstevel@tonic-gate 			goto out;
22230Sstevel@tonic-gate 		}
22240Sstevel@tonic-gate 	}
22250Sstevel@tonic-gate 
2226*9263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_LIMIT_PRIVILEGES, vbuf,
2227*9263SSean.Wilcox@Sun.COM 	    vbuf_sz, prop, val) == 0 || get_astring_val(instpg,
2228*9263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_LIMIT_PRIVILEGES, vbuf, vbuf_sz, prop, val) == 0)) {
2229*9263SSean.Wilcox@Sun.COM 		if (scf_error() == SCF_ERROR_NOT_FOUND) {
2230*9263SSean.Wilcox@Sun.COM 			(void) strcpy(vbuf, ":default");
2231*9263SSean.Wilcox@Sun.COM 		} else {
2232*9263SSean.Wilcox@Sun.COM 			errstr = "Could not get limit_privileges property.";
2233*9263SSean.Wilcox@Sun.COM 			goto out;
2234*9263SSean.Wilcox@Sun.COM 		}
22350Sstevel@tonic-gate 	}
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") == 0)
22380Sstevel@tonic-gate 		/*
22390Sstevel@tonic-gate 		 * L must default to all privileges so root NPA services see
22400Sstevel@tonic-gate 		 * iE = all.  "zone" is all privileges available in the current
22410Sstevel@tonic-gate 		 * zone, equivalent to "all" in the global zone.
22420Sstevel@tonic-gate 		 */
22430Sstevel@tonic-gate 		(void) strcpy(vbuf, "zone");
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	ci->lpriv_set = priv_str_to_set(vbuf, ",", NULL);
22460Sstevel@tonic-gate 	if (ci->lpriv_set == NULL) {
22470Sstevel@tonic-gate 		if (errno != EINVAL)
22480Sstevel@tonic-gate 			errstr = ALLOCFAIL;
22490Sstevel@tonic-gate 		else {
2250*9263SSean.Wilcox@Sun.COM 			errstr =
2251*9263SSean.Wilcox@Sun.COM 			    "Could not interpret limit_privileges property.";
22520Sstevel@tonic-gate 		}
22530Sstevel@tonic-gate 		goto out;
22540Sstevel@tonic-gate 	}
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate out:
22570Sstevel@tonic-gate 	return (errstr);
22580Sstevel@tonic-gate }
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate static int
22610Sstevel@tonic-gate get_environment(scf_handle_t *h, scf_propertygroup_t *pg,
22620Sstevel@tonic-gate     struct method_context *mcp, scf_property_t *prop, scf_value_t *val)
22630Sstevel@tonic-gate {
22640Sstevel@tonic-gate 	scf_iter_t *iter;
22650Sstevel@tonic-gate 	scf_type_t type;
22660Sstevel@tonic-gate 	size_t i = 0;
22670Sstevel@tonic-gate 	int ret;
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENVIRONMENT, prop) != 0) {
22700Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
22710Sstevel@tonic-gate 			return (ENOENT);
22720Sstevel@tonic-gate 		return (scf_error());
22730Sstevel@tonic-gate 	}
22740Sstevel@tonic-gate 	if (scf_property_type(prop, &type) != 0)
22750Sstevel@tonic-gate 		return (scf_error());
22760Sstevel@tonic-gate 	if (type != SCF_TYPE_ASTRING)
22770Sstevel@tonic-gate 		return (EINVAL);
22780Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
22790Sstevel@tonic-gate 		return (scf_error());
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) != 0) {
22820Sstevel@tonic-gate 		ret = scf_error();
22830Sstevel@tonic-gate 		scf_iter_destroy(iter);
22840Sstevel@tonic-gate 		return (ret);
22850Sstevel@tonic-gate 	}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 	mcp->env_sz = 10;
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 	if ((mcp->env = uu_zalloc(sizeof (*mcp->env) * mcp->env_sz)) == NULL) {
22900Sstevel@tonic-gate 		ret = ENOMEM;
22910Sstevel@tonic-gate 		goto out;
22920Sstevel@tonic-gate 	}
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	while ((ret = scf_iter_next_value(iter, val)) == 1) {
22950Sstevel@tonic-gate 		ret = scf_value_get_as_string(val, mcp->vbuf, mcp->vbuf_sz);
22960Sstevel@tonic-gate 		if (ret == -1) {
22970Sstevel@tonic-gate 			ret = scf_error();
22980Sstevel@tonic-gate 			goto out;
22990Sstevel@tonic-gate 		}
23000Sstevel@tonic-gate 
23010Sstevel@tonic-gate 		if ((mcp->env[i] = strdup(mcp->vbuf)) == NULL) {
23020Sstevel@tonic-gate 			ret = ENOMEM;
23030Sstevel@tonic-gate 			goto out;
23040Sstevel@tonic-gate 		}
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate 		if (++i == mcp->env_sz) {
23070Sstevel@tonic-gate 			char **env;
23080Sstevel@tonic-gate 			mcp->env_sz *= 2;
23090Sstevel@tonic-gate 			env = uu_zalloc(sizeof (*mcp->env) * mcp->env_sz);
23100Sstevel@tonic-gate 			if (env == NULL) {
23110Sstevel@tonic-gate 				ret = ENOMEM;
23120Sstevel@tonic-gate 				goto out;
23130Sstevel@tonic-gate 			}
23140Sstevel@tonic-gate 			(void) memcpy(env, mcp->env,
23150Sstevel@tonic-gate 			    sizeof (*mcp->env) * (mcp->env_sz / 2));
23160Sstevel@tonic-gate 			free(mcp->env);
23170Sstevel@tonic-gate 			mcp->env = env;
23180Sstevel@tonic-gate 		}
23190Sstevel@tonic-gate 	}
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	if (ret == -1)
23220Sstevel@tonic-gate 		ret = scf_error();
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate out:
23250Sstevel@tonic-gate 	scf_iter_destroy(iter);
23260Sstevel@tonic-gate 	return (ret);
23270Sstevel@tonic-gate }
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate /*
23300Sstevel@tonic-gate  * Fetch method context information from the repository, allocate and fill
23310Sstevel@tonic-gate  * a method_context structure, return it in *mcpp, and return NULL.  On error,
23320Sstevel@tonic-gate  * return a human-readable string which indicates the error.
23330Sstevel@tonic-gate  *
2334*9263SSean.Wilcox@Sun.COM  * If no method_context is defined, original init context is provided, where
2335*9263SSean.Wilcox@Sun.COM  * the working directory is '/', and uid/gid are 0/0.  But if a method_context
2336*9263SSean.Wilcox@Sun.COM  * is defined at any level the smf_method(5) method_context defaults are used.
2337*9263SSean.Wilcox@Sun.COM  *
23380Sstevel@tonic-gate  * Eventually, we will return a structured error in the case of
23390Sstevel@tonic-gate  * retryable or abortable failures such as memory allocation errors and
23400Sstevel@tonic-gate  * repository connection failures.  For now, these failures are just
23410Sstevel@tonic-gate  * encoded in the failure string.
23420Sstevel@tonic-gate  */
23430Sstevel@tonic-gate const char *
23440Sstevel@tonic-gate restarter_get_method_context(uint_t version, scf_instance_t *inst,
23450Sstevel@tonic-gate     scf_snapshot_t *snap, const char *mname, const char *cmdline,
23460Sstevel@tonic-gate     struct method_context **mcpp)
23470Sstevel@tonic-gate {
23480Sstevel@tonic-gate 	scf_handle_t *h;
23490Sstevel@tonic-gate 	scf_propertygroup_t *methpg = NULL;
23500Sstevel@tonic-gate 	scf_propertygroup_t *instpg = NULL;
23510Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
23520Sstevel@tonic-gate 	scf_property_t *prop = NULL;
23530Sstevel@tonic-gate 	scf_value_t *val = NULL;
23540Sstevel@tonic-gate 	scf_type_t ty;
23550Sstevel@tonic-gate 	uint8_t use_profile;
23560Sstevel@tonic-gate 	int ret;
2357*9263SSean.Wilcox@Sun.COM 	int mc_used = 0;
23580Sstevel@tonic-gate 	const char *errstr = NULL;
23590Sstevel@tonic-gate 	struct method_context *cip;
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	if (version != RESTARTER_METHOD_CONTEXT_VERSION)
23620Sstevel@tonic-gate 		return ("Unknown method_context version.");
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 	/* Get the handle before we allocate anything. */
23650Sstevel@tonic-gate 	h = scf_instance_handle(inst);
23660Sstevel@tonic-gate 	if (h == NULL)
23670Sstevel@tonic-gate 		return (scf_strerror(scf_error()));
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	cip = malloc(sizeof (*cip));
23700Sstevel@tonic-gate 	if (cip == NULL)
23710Sstevel@tonic-gate 		return (ALLOCFAIL);
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 	(void) memset(cip, 0, sizeof (*cip));
23744321Scasper 	cip->uid = (uid_t)-1;
23754321Scasper 	cip->euid = (uid_t)-1;
23764321Scasper 	cip->gid = (gid_t)-1;
23774321Scasper 	cip->egid = (gid_t)-1;
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate 	cip->vbuf_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
23800Sstevel@tonic-gate 	assert(cip->vbuf_sz >= 0);
23810Sstevel@tonic-gate 	cip->vbuf = malloc(cip->vbuf_sz);
23820Sstevel@tonic-gate 	if (cip->vbuf == NULL) {
23830Sstevel@tonic-gate 		free(cip);
23840Sstevel@tonic-gate 		return (ALLOCFAIL);
23850Sstevel@tonic-gate 	}
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 	if ((instpg = scf_pg_create(h)) == NULL ||
23880Sstevel@tonic-gate 	    (methpg = scf_pg_create(h)) == NULL ||
23890Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
23900Sstevel@tonic-gate 	    (val = scf_value_create(h)) == NULL) {
23910Sstevel@tonic-gate 		errstr = ALLOCFAIL;
23920Sstevel@tonic-gate 		goto out;
23930Sstevel@tonic-gate 	}
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 	/*
23960Sstevel@tonic-gate 	 * The method environment, and the credentials/profile data,
23970Sstevel@tonic-gate 	 * may be found either in the pg for the method (methpg),
23980Sstevel@tonic-gate 	 * or in the instance/service SCF_PG_METHOD_CONTEXT pg (named
23990Sstevel@tonic-gate 	 * instpg below).
24000Sstevel@tonic-gate 	 */
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, mname, methpg) !=
24030Sstevel@tonic-gate 	    SCF_SUCCESS) {
24040Sstevel@tonic-gate 		errstr = scf_strerror(scf_error());
24050Sstevel@tonic-gate 		goto out;
24060Sstevel@tonic-gate 	}
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, SCF_PG_METHOD_CONTEXT,
24090Sstevel@tonic-gate 	    instpg) != SCF_SUCCESS) {
24100Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND) {
24110Sstevel@tonic-gate 			errstr = scf_strerror(scf_error());
24120Sstevel@tonic-gate 			goto out;
24130Sstevel@tonic-gate 		}
24140Sstevel@tonic-gate 		scf_pg_destroy(instpg);
24150Sstevel@tonic-gate 		instpg = NULL;
2416*9263SSean.Wilcox@Sun.COM 	} else {
2417*9263SSean.Wilcox@Sun.COM 		mc_used++;
24180Sstevel@tonic-gate 	}
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	ret = get_environment(h, methpg, cip, prop, val);
24210Sstevel@tonic-gate 	if (ret == ENOENT && instpg != NULL) {
24220Sstevel@tonic-gate 		ret = get_environment(h, instpg, cip, prop, val);
24230Sstevel@tonic-gate 	}
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	switch (ret) {
24260Sstevel@tonic-gate 	case 0:
2427*9263SSean.Wilcox@Sun.COM 		mc_used++;
2428*9263SSean.Wilcox@Sun.COM 		break;
24290Sstevel@tonic-gate 	case ENOENT:
24300Sstevel@tonic-gate 		break;
24310Sstevel@tonic-gate 	case ENOMEM:
24320Sstevel@tonic-gate 		errstr = "Out of memory.";
24330Sstevel@tonic-gate 		goto out;
24340Sstevel@tonic-gate 	case EINVAL:
24350Sstevel@tonic-gate 		errstr = "Invalid method environment.";
24360Sstevel@tonic-gate 		goto out;
24370Sstevel@tonic-gate 	default:
24380Sstevel@tonic-gate 		errstr = scf_strerror(ret);
24390Sstevel@tonic-gate 		goto out;
24400Sstevel@tonic-gate 	}
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate 	pg = methpg;
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate 	ret = scf_pg_get_property(pg, SCF_PROPERTY_USE_PROFILE, prop);
24450Sstevel@tonic-gate 	if (ret && scf_error() == SCF_ERROR_NOT_FOUND && instpg != NULL) {
2446*9263SSean.Wilcox@Sun.COM 		pg = NULL;
2447*9263SSean.Wilcox@Sun.COM 		ret = scf_pg_get_property(instpg, SCF_PROPERTY_USE_PROFILE,
2448*9263SSean.Wilcox@Sun.COM 		    prop);
24490Sstevel@tonic-gate 	}
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	if (ret) {
24520Sstevel@tonic-gate 		switch (scf_error()) {
24530Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
2454*9263SSean.Wilcox@Sun.COM 			/* No profile context: use default credentials */
24550Sstevel@tonic-gate 			cip->uid = 0;
24560Sstevel@tonic-gate 			cip->gid = 0;
2457*9263SSean.Wilcox@Sun.COM 			break;
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
24600Sstevel@tonic-gate 			errstr = RCBROKEN;
24610Sstevel@tonic-gate 			goto out;
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
24640Sstevel@tonic-gate 			errstr = "\"use_profile\" property deleted.";
24650Sstevel@tonic-gate 			goto out;
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
24680Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
24690Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
24700Sstevel@tonic-gate 		default:
24710Sstevel@tonic-gate 			bad_fail("scf_pg_get_property", scf_error());
24720Sstevel@tonic-gate 		}
2473*9263SSean.Wilcox@Sun.COM 	} else {
2474*9263SSean.Wilcox@Sun.COM 		if (scf_property_type(prop, &ty) != SCF_SUCCESS) {
2475*9263SSean.Wilcox@Sun.COM 			switch (scf_error()) {
2476*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONNECTION_BROKEN:
2477*9263SSean.Wilcox@Sun.COM 				errstr = RCBROKEN;
2478*9263SSean.Wilcox@Sun.COM 				break;
2479*9263SSean.Wilcox@Sun.COM 
2480*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_DELETED:
2481*9263SSean.Wilcox@Sun.COM 				errstr = "\"use profile\" property deleted.";
2482*9263SSean.Wilcox@Sun.COM 				break;
2483*9263SSean.Wilcox@Sun.COM 
2484*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_SET:
2485*9263SSean.Wilcox@Sun.COM 			default:
2486*9263SSean.Wilcox@Sun.COM 				bad_fail("scf_property_type", scf_error());
2487*9263SSean.Wilcox@Sun.COM 			}
2488*9263SSean.Wilcox@Sun.COM 
2489*9263SSean.Wilcox@Sun.COM 			goto out;
2490*9263SSean.Wilcox@Sun.COM 		}
2491*9263SSean.Wilcox@Sun.COM 
2492*9263SSean.Wilcox@Sun.COM 		if (ty != SCF_TYPE_BOOLEAN) {
2493*9263SSean.Wilcox@Sun.COM 			errstr = "\"use profile\" property is not boolean.";
2494*9263SSean.Wilcox@Sun.COM 			goto out;
2495*9263SSean.Wilcox@Sun.COM 		}
2496*9263SSean.Wilcox@Sun.COM 
2497*9263SSean.Wilcox@Sun.COM 		if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2498*9263SSean.Wilcox@Sun.COM 			switch (scf_error()) {
2499*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONNECTION_BROKEN:
2500*9263SSean.Wilcox@Sun.COM 				errstr = RCBROKEN;
2501*9263SSean.Wilcox@Sun.COM 				break;
2502*9263SSean.Wilcox@Sun.COM 
2503*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONSTRAINT_VIOLATED:
2504*9263SSean.Wilcox@Sun.COM 				errstr =
2505*9263SSean.Wilcox@Sun.COM 				    "\"use profile\" property has multiple "
2506*9263SSean.Wilcox@Sun.COM 				    "values.";
2507*9263SSean.Wilcox@Sun.COM 				break;
2508*9263SSean.Wilcox@Sun.COM 
2509*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_FOUND:
2510*9263SSean.Wilcox@Sun.COM 				errstr = "\"use profile\" property has no "
2511*9263SSean.Wilcox@Sun.COM 				    "values.";
2512*9263SSean.Wilcox@Sun.COM 				break;
2513*9263SSean.Wilcox@Sun.COM 
2514*9263SSean.Wilcox@Sun.COM 			default:
2515*9263SSean.Wilcox@Sun.COM 				bad_fail("scf_property_get_value", scf_error());
2516*9263SSean.Wilcox@Sun.COM 			}
2517*9263SSean.Wilcox@Sun.COM 
2518*9263SSean.Wilcox@Sun.COM 			goto out;
2519*9263SSean.Wilcox@Sun.COM 		}
2520*9263SSean.Wilcox@Sun.COM 
2521*9263SSean.Wilcox@Sun.COM 		mc_used++;
2522*9263SSean.Wilcox@Sun.COM 		ret = scf_value_get_boolean(val, &use_profile);
2523*9263SSean.Wilcox@Sun.COM 		assert(ret == SCF_SUCCESS);
2524*9263SSean.Wilcox@Sun.COM 
2525*9263SSean.Wilcox@Sun.COM 		/* get ids & privileges */
2526*9263SSean.Wilcox@Sun.COM 		if (use_profile)
2527*9263SSean.Wilcox@Sun.COM 			errstr = get_profile(pg, instpg, prop, val, cmdline,
2528*9263SSean.Wilcox@Sun.COM 			    cip);
2529*9263SSean.Wilcox@Sun.COM 		else
2530*9263SSean.Wilcox@Sun.COM 			errstr = get_ids(pg, instpg, prop, val, cip);
2531*9263SSean.Wilcox@Sun.COM 
2532*9263SSean.Wilcox@Sun.COM 		if (errstr != NULL)
2533*9263SSean.Wilcox@Sun.COM 			goto out;
25340Sstevel@tonic-gate 	}
25350Sstevel@tonic-gate 
2536*9263SSean.Wilcox@Sun.COM 	/* get working directory */
2537*9263SSean.Wilcox@Sun.COM 	if ((methpg != NULL && scf_pg_get_property(methpg,
2538*9263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_WORKING_DIRECTORY, prop) == SCF_SUCCESS) ||
2539*9263SSean.Wilcox@Sun.COM 	    (instpg != NULL && scf_pg_get_property(instpg,
2540*9263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_WORKING_DIRECTORY, prop) == SCF_SUCCESS)) {
2541*9263SSean.Wilcox@Sun.COM 		if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2542*9263SSean.Wilcox@Sun.COM 			switch (scf_error()) {
2543*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONNECTION_BROKEN:
2544*9263SSean.Wilcox@Sun.COM 				errstr = RCBROKEN;
2545*9263SSean.Wilcox@Sun.COM 				break;
2546*9263SSean.Wilcox@Sun.COM 
2547*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONSTRAINT_VIOLATED:
2548*9263SSean.Wilcox@Sun.COM 				errstr =
2549*9263SSean.Wilcox@Sun.COM 				    "\"working directory\" property has "
2550*9263SSean.Wilcox@Sun.COM 				    "multiple values.";
2551*9263SSean.Wilcox@Sun.COM 				break;
2552*9263SSean.Wilcox@Sun.COM 
2553*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_FOUND:
2554*9263SSean.Wilcox@Sun.COM 				errstr = "\"working directory\" property has "
2555*9263SSean.Wilcox@Sun.COM 				    "no values.";
2556*9263SSean.Wilcox@Sun.COM 				break;
2557*9263SSean.Wilcox@Sun.COM 
2558*9263SSean.Wilcox@Sun.COM 			default:
2559*9263SSean.Wilcox@Sun.COM 				bad_fail("scf_property_get_value", scf_error());
2560*9263SSean.Wilcox@Sun.COM 			}
2561*9263SSean.Wilcox@Sun.COM 
2562*9263SSean.Wilcox@Sun.COM 			goto out;
2563*9263SSean.Wilcox@Sun.COM 		}
2564*9263SSean.Wilcox@Sun.COM 
2565*9263SSean.Wilcox@Sun.COM 		mc_used++;
2566*9263SSean.Wilcox@Sun.COM 		ret = scf_value_get_astring(val, cip->vbuf, cip->vbuf_sz);
2567*9263SSean.Wilcox@Sun.COM 		assert(ret != -1);
2568*9263SSean.Wilcox@Sun.COM 	} else {
25690Sstevel@tonic-gate 		switch (scf_error()) {
2570*9263SSean.Wilcox@Sun.COM 		case SCF_ERROR_NOT_FOUND:
2571*9263SSean.Wilcox@Sun.COM 			/* okay if missing. */
2572*9263SSean.Wilcox@Sun.COM 			(void) strcpy(cip->vbuf, ":default");
25730Sstevel@tonic-gate 			break;
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
25760Sstevel@tonic-gate 			errstr = RCBROKEN;
2577*9263SSean.Wilcox@Sun.COM 			goto out;
2578*9263SSean.Wilcox@Sun.COM 
2579*9263SSean.Wilcox@Sun.COM 		case SCF_ERROR_DELETED:
2580*9263SSean.Wilcox@Sun.COM 			errstr = "\"working_directory\" property deleted.";
2581*9263SSean.Wilcox@Sun.COM 			goto out;
2582*9263SSean.Wilcox@Sun.COM 
2583*9263SSean.Wilcox@Sun.COM 		case SCF_ERROR_HANDLE_MISMATCH:
2584*9263SSean.Wilcox@Sun.COM 		case SCF_ERROR_INVALID_ARGUMENT:
2585*9263SSean.Wilcox@Sun.COM 		case SCF_ERROR_NOT_SET:
25860Sstevel@tonic-gate 		default:
2587*9263SSean.Wilcox@Sun.COM 			bad_fail("scf_pg_get_property", scf_error());
25880Sstevel@tonic-gate 		}
25890Sstevel@tonic-gate 	}
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate 	if (strcmp(cip->vbuf, ":default") == 0 ||
25920Sstevel@tonic-gate 	    strcmp(cip->vbuf, ":home") == 0) {
25930Sstevel@tonic-gate 		switch (ret = lookup_pwd(cip)) {
25940Sstevel@tonic-gate 		case 0:
25950Sstevel@tonic-gate 			break;
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 		case ENOMEM:
25980Sstevel@tonic-gate 			errstr = "Out of memory.";
25990Sstevel@tonic-gate 			goto out;
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 		case ENOENT:
26020Sstevel@tonic-gate 		case EIO:
26030Sstevel@tonic-gate 		case EMFILE:
26040Sstevel@tonic-gate 		case ENFILE:
26050Sstevel@tonic-gate 			errstr = "Could not get passwd entry.";
26060Sstevel@tonic-gate 			goto out;
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 		default:
26090Sstevel@tonic-gate 			bad_fail("lookup_pwd", ret);
26100Sstevel@tonic-gate 		}
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 		cip->working_dir = strdup(cip->pwd.pw_dir);
26130Sstevel@tonic-gate 		if (cip->working_dir == NULL) {
26140Sstevel@tonic-gate 			errstr = ALLOCFAIL;
26150Sstevel@tonic-gate 			goto out;
26160Sstevel@tonic-gate 		}
26170Sstevel@tonic-gate 	} else {
26180Sstevel@tonic-gate 		cip->working_dir = strdup(cip->vbuf);
26190Sstevel@tonic-gate 		if (cip->working_dir == NULL) {
26200Sstevel@tonic-gate 			errstr = ALLOCFAIL;
26210Sstevel@tonic-gate 			goto out;
26220Sstevel@tonic-gate 		}
26230Sstevel@tonic-gate 	}
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 	/* get (optional) corefile pattern */
2626*9263SSean.Wilcox@Sun.COM 	if ((methpg != NULL && scf_pg_get_property(methpg,
2627*9263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_COREFILE_PATTERN, prop) == SCF_SUCCESS) ||
2628*9263SSean.Wilcox@Sun.COM 	    (instpg != NULL && scf_pg_get_property(instpg,
2629*9263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_COREFILE_PATTERN, prop) == SCF_SUCCESS)) {
2630*9263SSean.Wilcox@Sun.COM 		if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2631*9263SSean.Wilcox@Sun.COM 			switch (scf_error()) {
2632*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONNECTION_BROKEN:
2633*9263SSean.Wilcox@Sun.COM 				errstr = RCBROKEN;
2634*9263SSean.Wilcox@Sun.COM 				break;
2635*9263SSean.Wilcox@Sun.COM 
2636*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONSTRAINT_VIOLATED:
2637*9263SSean.Wilcox@Sun.COM 				errstr =
2638*9263SSean.Wilcox@Sun.COM 				    "\"core_pattern\" property has multiple "
2639*9263SSean.Wilcox@Sun.COM 				    "values.";
2640*9263SSean.Wilcox@Sun.COM 				break;
2641*9263SSean.Wilcox@Sun.COM 
2642*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_FOUND:
2643*9263SSean.Wilcox@Sun.COM 				errstr = "\"core_pattern\" property has no "
2644*9263SSean.Wilcox@Sun.COM 				    "values.";
2645*9263SSean.Wilcox@Sun.COM 				break;
2646*9263SSean.Wilcox@Sun.COM 
2647*9263SSean.Wilcox@Sun.COM 			default:
2648*9263SSean.Wilcox@Sun.COM 				bad_fail("scf_property_get_value", scf_error());
2649*9263SSean.Wilcox@Sun.COM 			}
2650*9263SSean.Wilcox@Sun.COM 
2651*9263SSean.Wilcox@Sun.COM 		} else {
2652*9263SSean.Wilcox@Sun.COM 
2653*9263SSean.Wilcox@Sun.COM 			ret = scf_value_get_astring(val, cip->vbuf,
2654*9263SSean.Wilcox@Sun.COM 			    cip->vbuf_sz);
2655*9263SSean.Wilcox@Sun.COM 			assert(ret != -1);
2656*9263SSean.Wilcox@Sun.COM 
2657*9263SSean.Wilcox@Sun.COM 			cip->corefile_pattern = strdup(cip->vbuf);
2658*9263SSean.Wilcox@Sun.COM 			if (cip->corefile_pattern == NULL) {
2659*9263SSean.Wilcox@Sun.COM 				errstr = ALLOCFAIL;
2660*9263SSean.Wilcox@Sun.COM 				goto out;
2661*9263SSean.Wilcox@Sun.COM 			}
26620Sstevel@tonic-gate 		}
26630Sstevel@tonic-gate 
2664*9263SSean.Wilcox@Sun.COM 		mc_used++;
26650Sstevel@tonic-gate 	} else {
26660Sstevel@tonic-gate 		switch (scf_error()) {
26670Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
26680Sstevel@tonic-gate 			/* okay if missing. */
26690Sstevel@tonic-gate 			break;
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
26720Sstevel@tonic-gate 			errstr = RCBROKEN;
26730Sstevel@tonic-gate 			goto out;
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
26760Sstevel@tonic-gate 			errstr = "\"corefile_pattern\" property deleted.";
26770Sstevel@tonic-gate 			goto out;
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
26800Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
26810Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
26820Sstevel@tonic-gate 		default:
26830Sstevel@tonic-gate 			bad_fail("scf_pg_get_property", scf_error());
26840Sstevel@tonic-gate 		}
26850Sstevel@tonic-gate 	}
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	if (restarter_rm_libs_loadable()) {
26880Sstevel@tonic-gate 		/* get project */
2689*9263SSean.Wilcox@Sun.COM 		if ((methpg != NULL && scf_pg_get_property(methpg,
2690*9263SSean.Wilcox@Sun.COM 		    SCF_PROPERTY_PROJECT, prop) == SCF_SUCCESS) ||
2691*9263SSean.Wilcox@Sun.COM 		    (instpg != NULL && scf_pg_get_property(instpg,
2692*9263SSean.Wilcox@Sun.COM 		    SCF_PROPERTY_PROJECT, prop) == SCF_SUCCESS)) {
2693*9263SSean.Wilcox@Sun.COM 			if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2694*9263SSean.Wilcox@Sun.COM 				switch (scf_error()) {
2695*9263SSean.Wilcox@Sun.COM 				case SCF_ERROR_CONNECTION_BROKEN:
2696*9263SSean.Wilcox@Sun.COM 					errstr = RCBROKEN;
2697*9263SSean.Wilcox@Sun.COM 					break;
2698*9263SSean.Wilcox@Sun.COM 
2699*9263SSean.Wilcox@Sun.COM 				case SCF_ERROR_CONSTRAINT_VIOLATED:
2700*9263SSean.Wilcox@Sun.COM 					errstr =
2701*9263SSean.Wilcox@Sun.COM 					    "\"project\" property has "
2702*9263SSean.Wilcox@Sun.COM 					    "multiple values.";
2703*9263SSean.Wilcox@Sun.COM 					break;
2704*9263SSean.Wilcox@Sun.COM 
2705*9263SSean.Wilcox@Sun.COM 				case SCF_ERROR_NOT_FOUND:
2706*9263SSean.Wilcox@Sun.COM 					errstr = "\"project\" property has "
2707*9263SSean.Wilcox@Sun.COM 					    "no values.";
2708*9263SSean.Wilcox@Sun.COM 					break;
2709*9263SSean.Wilcox@Sun.COM 
2710*9263SSean.Wilcox@Sun.COM 				default:
2711*9263SSean.Wilcox@Sun.COM 					bad_fail("scf_property_get_value",
2712*9263SSean.Wilcox@Sun.COM 					    scf_error());
2713*9263SSean.Wilcox@Sun.COM 				}
2714*9263SSean.Wilcox@Sun.COM 
2715*9263SSean.Wilcox@Sun.COM 				(void) strcpy(cip->vbuf, ":default");
2716*9263SSean.Wilcox@Sun.COM 			} else {
2717*9263SSean.Wilcox@Sun.COM 				ret = scf_value_get_astring(val, cip->vbuf,
2718*9263SSean.Wilcox@Sun.COM 				    cip->vbuf_sz);
2719*9263SSean.Wilcox@Sun.COM 				assert(ret != -1);
2720*9263SSean.Wilcox@Sun.COM 			}
2721*9263SSean.Wilcox@Sun.COM 
2722*9263SSean.Wilcox@Sun.COM 			mc_used++;
2723*9263SSean.Wilcox@Sun.COM 		} else {
2724*9263SSean.Wilcox@Sun.COM 			(void) strcpy(cip->vbuf, ":default");
27250Sstevel@tonic-gate 		}
27260Sstevel@tonic-gate 
27270Sstevel@tonic-gate 		switch (ret = get_projid(cip->vbuf, cip)) {
27280Sstevel@tonic-gate 		case 0:
27290Sstevel@tonic-gate 			break;
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 		case ENOMEM:
27320Sstevel@tonic-gate 			errstr = "Out of memory.";
27330Sstevel@tonic-gate 			goto out;
27340Sstevel@tonic-gate 
27350Sstevel@tonic-gate 		case ENOENT:
27360Sstevel@tonic-gate 			errstr = "Missing passwd or project entry.";
27370Sstevel@tonic-gate 			goto out;
27380Sstevel@tonic-gate 
27390Sstevel@tonic-gate 		case EIO:
27400Sstevel@tonic-gate 			errstr = "I/O error.";
27410Sstevel@tonic-gate 			goto out;
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 		case EMFILE:
27440Sstevel@tonic-gate 		case ENFILE:
27450Sstevel@tonic-gate 			errstr = "Out of file descriptors.";
27460Sstevel@tonic-gate 			goto out;
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 		case -1:
27490Sstevel@tonic-gate 			errstr = "Name service switch is misconfigured.";
27500Sstevel@tonic-gate 			goto out;
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 		case ERANGE:
27530Sstevel@tonic-gate 			errstr = "Project ID too big.";
27540Sstevel@tonic-gate 			goto out;
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate 		case EINVAL:
27570Sstevel@tonic-gate 			errstr = "Project ID is invalid.";
27580Sstevel@tonic-gate 			goto out;
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 		case E2BIG:
27610Sstevel@tonic-gate 			errstr = "Project entry is too big.";
27620Sstevel@tonic-gate 			goto out;
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate 		default:
27650Sstevel@tonic-gate 			bad_fail("get_projid", ret);
27660Sstevel@tonic-gate 		}
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 		/* get resource pool */
2769*9263SSean.Wilcox@Sun.COM 		if ((methpg != NULL && scf_pg_get_property(methpg,
2770*9263SSean.Wilcox@Sun.COM 		    SCF_PROPERTY_RESOURCE_POOL, prop) == SCF_SUCCESS) ||
2771*9263SSean.Wilcox@Sun.COM 		    (instpg != NULL && scf_pg_get_property(instpg,
2772*9263SSean.Wilcox@Sun.COM 		    SCF_PROPERTY_RESOURCE_POOL, prop) == SCF_SUCCESS)) {
2773*9263SSean.Wilcox@Sun.COM 			if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2774*9263SSean.Wilcox@Sun.COM 				switch (scf_error()) {
2775*9263SSean.Wilcox@Sun.COM 				case SCF_ERROR_CONNECTION_BROKEN:
2776*9263SSean.Wilcox@Sun.COM 					errstr = RCBROKEN;
2777*9263SSean.Wilcox@Sun.COM 					break;
2778*9263SSean.Wilcox@Sun.COM 
2779*9263SSean.Wilcox@Sun.COM 				case SCF_ERROR_CONSTRAINT_VIOLATED:
2780*9263SSean.Wilcox@Sun.COM 					errstr =
2781*9263SSean.Wilcox@Sun.COM 					    "\"resource_pool\" property has "
2782*9263SSean.Wilcox@Sun.COM 					    "multiple values.";
2783*9263SSean.Wilcox@Sun.COM 					break;
2784*9263SSean.Wilcox@Sun.COM 
2785*9263SSean.Wilcox@Sun.COM 				case SCF_ERROR_NOT_FOUND:
2786*9263SSean.Wilcox@Sun.COM 					errstr = "\"resource_pool\" property "
2787*9263SSean.Wilcox@Sun.COM 					    "has no values.";
2788*9263SSean.Wilcox@Sun.COM 					break;
2789*9263SSean.Wilcox@Sun.COM 
2790*9263SSean.Wilcox@Sun.COM 				default:
2791*9263SSean.Wilcox@Sun.COM 					bad_fail("scf_property_get_value",
2792*9263SSean.Wilcox@Sun.COM 					    scf_error());
2793*9263SSean.Wilcox@Sun.COM 				}
2794*9263SSean.Wilcox@Sun.COM 
2795*9263SSean.Wilcox@Sun.COM 				(void) strcpy(cip->vbuf, ":default");
2796*9263SSean.Wilcox@Sun.COM 			} else {
2797*9263SSean.Wilcox@Sun.COM 				ret = scf_value_get_astring(val, cip->vbuf,
2798*9263SSean.Wilcox@Sun.COM 				    cip->vbuf_sz);
2799*9263SSean.Wilcox@Sun.COM 				assert(ret != -1);
2800*9263SSean.Wilcox@Sun.COM 			}
2801*9263SSean.Wilcox@Sun.COM 
2802*9263SSean.Wilcox@Sun.COM 			mc_used++;
2803*9263SSean.Wilcox@Sun.COM 		} else {
2804*9263SSean.Wilcox@Sun.COM 			switch (scf_error()) {
2805*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_FOUND:
2806*9263SSean.Wilcox@Sun.COM 				/* okay if missing. */
2807*9263SSean.Wilcox@Sun.COM 				(void) strcpy(cip->vbuf, ":default");
2808*9263SSean.Wilcox@Sun.COM 				break;
2809*9263SSean.Wilcox@Sun.COM 
2810*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONNECTION_BROKEN:
2811*9263SSean.Wilcox@Sun.COM 				errstr = RCBROKEN;
2812*9263SSean.Wilcox@Sun.COM 				goto out;
2813*9263SSean.Wilcox@Sun.COM 
2814*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_DELETED:
2815*9263SSean.Wilcox@Sun.COM 				errstr = "\"resource_pool\" property deleted.";
2816*9263SSean.Wilcox@Sun.COM 				goto out;
2817*9263SSean.Wilcox@Sun.COM 
2818*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_HANDLE_MISMATCH:
2819*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_INVALID_ARGUMENT:
2820*9263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_SET:
2821*9263SSean.Wilcox@Sun.COM 			default:
2822*9263SSean.Wilcox@Sun.COM 				bad_fail("scf_pg_get_property", scf_error());
2823*9263SSean.Wilcox@Sun.COM 			}
28240Sstevel@tonic-gate 		}
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 		if (strcmp(cip->vbuf, ":default") != 0) {
28270Sstevel@tonic-gate 			cip->resource_pool = strdup(cip->vbuf);
28280Sstevel@tonic-gate 			if (cip->resource_pool == NULL) {
28290Sstevel@tonic-gate 				errstr = ALLOCFAIL;
28300Sstevel@tonic-gate 				goto out;
28310Sstevel@tonic-gate 			}
28320Sstevel@tonic-gate 		}
28330Sstevel@tonic-gate 	}
28340Sstevel@tonic-gate 
2835*9263SSean.Wilcox@Sun.COM 	/*
2836*9263SSean.Wilcox@Sun.COM 	 * A method_context was not used for any configurable
2837*9263SSean.Wilcox@Sun.COM 	 * elements or attributes, so reset and use the simple
2838*9263SSean.Wilcox@Sun.COM 	 * defaults that provide historic init behavior.
2839*9263SSean.Wilcox@Sun.COM 	 */
2840*9263SSean.Wilcox@Sun.COM 	if (mc_used == 0) {
2841*9263SSean.Wilcox@Sun.COM 		(void) memset(cip, 0, sizeof (*cip));
2842*9263SSean.Wilcox@Sun.COM 		cip->uid = 0;
2843*9263SSean.Wilcox@Sun.COM 		cip->gid = 0;
2844*9263SSean.Wilcox@Sun.COM 		cip->euid = (uid_t)-1;
2845*9263SSean.Wilcox@Sun.COM 		cip->egid = (gid_t)-1;
2846*9263SSean.Wilcox@Sun.COM 	}
2847*9263SSean.Wilcox@Sun.COM 
28480Sstevel@tonic-gate 	*mcpp = cip;
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate out:
28510Sstevel@tonic-gate 	(void) scf_value_destroy(val);
28520Sstevel@tonic-gate 	scf_property_destroy(prop);
28530Sstevel@tonic-gate 	scf_pg_destroy(instpg);
28540Sstevel@tonic-gate 	scf_pg_destroy(methpg);
28550Sstevel@tonic-gate 
28560Sstevel@tonic-gate 	if (cip->pwbuf != NULL)
28570Sstevel@tonic-gate 		free(cip->pwbuf);
28580Sstevel@tonic-gate 	free(cip->vbuf);
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 	if (errstr != NULL)
28610Sstevel@tonic-gate 		restarter_free_method_context(cip);
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	return (errstr);
28640Sstevel@tonic-gate }
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate /*
28670Sstevel@tonic-gate  * Modify the current process per the given method_context.  On success, returns
28680Sstevel@tonic-gate  * 0.  Note that the environment is not modified by this function to include the
28690Sstevel@tonic-gate  * environment variables in cip->env.
28700Sstevel@tonic-gate  *
28710Sstevel@tonic-gate  * On failure, sets *fp to NULL or the name of the function which failed,
28720Sstevel@tonic-gate  * and returns one of the following error codes.  The words in parentheses are
28730Sstevel@tonic-gate  * the values to which *fp may be set for the error case.
28740Sstevel@tonic-gate  *   ENOMEM - malloc() failed
28750Sstevel@tonic-gate  *   EIO - an I/O error occurred (getpwuid_r, chdir)
28760Sstevel@tonic-gate  *   EMFILE - process is out of file descriptors (getpwuid_r)
28770Sstevel@tonic-gate  *   ENFILE - system is out of file handles (getpwuid_r)
28780Sstevel@tonic-gate  *   EINVAL - gid or egid is out of range (setregid)
28790Sstevel@tonic-gate  *	      ngroups is too big (setgroups)
28800Sstevel@tonic-gate  *	      project's project id is bad (setproject)
28810Sstevel@tonic-gate  *	      uid or euid is out of range (setreuid)
28821712Srm88369  *	      poolname is invalid (pool_set_binding)
28830Sstevel@tonic-gate  *   EPERM - insufficient privilege (setregid, initgroups, setgroups, setppriv,
28840Sstevel@tonic-gate  *	         setproject, setreuid, settaskid)
28850Sstevel@tonic-gate  *   ENOENT - uid has a passwd entry but no shadow entry
28860Sstevel@tonic-gate  *	      working_dir does not exist (chdir)
28870Sstevel@tonic-gate  *	      uid has no passwd entry
28880Sstevel@tonic-gate  *	      the pool could not be found (pool_set_binding)
28890Sstevel@tonic-gate  *   EFAULT - lpriv_set or priv_set has a bad address (setppriv)
28900Sstevel@tonic-gate  *	      working_dir has a bad address (chdir)
28910Sstevel@tonic-gate  *   EACCES - could not access working_dir (chdir)
28920Sstevel@tonic-gate  *	      in a TASK_FINAL task (setproject, settaskid)
28930Sstevel@tonic-gate  *	      no resource pool accepting default binding exists (setproject)
28940Sstevel@tonic-gate  *   ELOOP - too many symbolic links in working_dir (chdir)
28950Sstevel@tonic-gate  *   ENAMETOOLONG - working_dir is too long (chdir)
28960Sstevel@tonic-gate  *   ENOLINK - working_dir is on an inaccessible remote machine (chdir)
28970Sstevel@tonic-gate  *   ENOTDIR - working_dir is not a directory (chdir)
28980Sstevel@tonic-gate  *   ESRCH - uid is not a user of project (setproject)
28990Sstevel@tonic-gate  *	     project is invalid (setproject)
29000Sstevel@tonic-gate  *	     the resource pool specified for project is unknown (setproject)
29010Sstevel@tonic-gate  *   EBADF - the configuration for the pool is invalid (pool_set_binding)
29020Sstevel@tonic-gate  *   -1 - core_set_process_path() failed (core_set_process_path)
29030Sstevel@tonic-gate  *	  a resource control assignment failed (setproject)
29040Sstevel@tonic-gate  *	  a system error occurred during pool_set_binding (pool_set_binding)
29050Sstevel@tonic-gate  */
29060Sstevel@tonic-gate int
29070Sstevel@tonic-gate restarter_set_method_context(struct method_context *cip, const char **fp)
29080Sstevel@tonic-gate {
29090Sstevel@tonic-gate 	pid_t mypid = -1;
29100Sstevel@tonic-gate 	int r, ret;
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate 	cip->pwbuf = NULL;
29130Sstevel@tonic-gate 	*fp = NULL;
29140Sstevel@tonic-gate 
29154321Scasper 	if (cip->gid != (gid_t)-1) {
29160Sstevel@tonic-gate 		if (setregid(cip->gid,
29174321Scasper 		    cip->egid != (gid_t)-1 ? cip->egid : cip->gid) != 0) {
29180Sstevel@tonic-gate 			*fp = "setregid";
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 			ret = errno;
29210Sstevel@tonic-gate 			assert(ret == EINVAL || ret == EPERM);
29220Sstevel@tonic-gate 			goto out;
29230Sstevel@tonic-gate 		}
29240Sstevel@tonic-gate 	} else {
29250Sstevel@tonic-gate 		if (cip->pwbuf == NULL) {
29260Sstevel@tonic-gate 			switch (ret = lookup_pwd(cip)) {
29270Sstevel@tonic-gate 			case 0:
29280Sstevel@tonic-gate 				break;
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate 			case ENOMEM:
29310Sstevel@tonic-gate 			case ENOENT:
29320Sstevel@tonic-gate 				*fp = NULL;
29330Sstevel@tonic-gate 				goto out;
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate 			case EIO:
29360Sstevel@tonic-gate 			case EMFILE:
29370Sstevel@tonic-gate 			case ENFILE:
29380Sstevel@tonic-gate 				*fp = "getpwuid_r";
29390Sstevel@tonic-gate 				goto out;
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate 			default:
29420Sstevel@tonic-gate 				bad_fail("lookup_pwd", ret);
29430Sstevel@tonic-gate 			}
29440Sstevel@tonic-gate 		}
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 		if (setregid(cip->pwd.pw_gid,
29474321Scasper 		    cip->egid != (gid_t)-1 ?
29484321Scasper 		    cip->egid : cip->pwd.pw_gid) != 0) {
29490Sstevel@tonic-gate 			*fp = "setregid";
29500Sstevel@tonic-gate 
29510Sstevel@tonic-gate 			ret = errno;
29520Sstevel@tonic-gate 			assert(ret == EINVAL || ret == EPERM);
29530Sstevel@tonic-gate 			goto out;
29540Sstevel@tonic-gate 		}
29550Sstevel@tonic-gate 	}
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 	if (cip->ngroups == -1) {
29580Sstevel@tonic-gate 		if (cip->pwbuf == NULL) {
29590Sstevel@tonic-gate 			switch (ret = lookup_pwd(cip)) {
29600Sstevel@tonic-gate 			case 0:
29610Sstevel@tonic-gate 				break;
29620Sstevel@tonic-gate 
29630Sstevel@tonic-gate 			case ENOMEM:
29640Sstevel@tonic-gate 			case ENOENT:
29650Sstevel@tonic-gate 				*fp = NULL;
29660Sstevel@tonic-gate 				goto out;
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 			case EIO:
29690Sstevel@tonic-gate 			case EMFILE:
29700Sstevel@tonic-gate 			case ENFILE:
29710Sstevel@tonic-gate 				*fp = "getpwuid_r";
29720Sstevel@tonic-gate 				goto out;
29730Sstevel@tonic-gate 
29740Sstevel@tonic-gate 			default:
29750Sstevel@tonic-gate 				bad_fail("lookup_pwd", ret);
29760Sstevel@tonic-gate 			}
29770Sstevel@tonic-gate 		}
29780Sstevel@tonic-gate 
29790Sstevel@tonic-gate 		/* Ok if cip->gid == -1 */
29800Sstevel@tonic-gate 		if (initgroups(cip->pwd.pw_name, cip->gid) != 0) {
29810Sstevel@tonic-gate 			*fp = "initgroups";
29820Sstevel@tonic-gate 			ret = errno;
29830Sstevel@tonic-gate 			assert(ret == EPERM);
29840Sstevel@tonic-gate 			goto out;
29850Sstevel@tonic-gate 		}
29860Sstevel@tonic-gate 	} else if (cip->ngroups > 0 &&
29870Sstevel@tonic-gate 	    setgroups(cip->ngroups, cip->groups) != 0) {
29880Sstevel@tonic-gate 		*fp = "setgroups";
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 		ret = errno;
29910Sstevel@tonic-gate 		assert(ret == EINVAL || ret == EPERM);
29920Sstevel@tonic-gate 		goto out;
29930Sstevel@tonic-gate 	}
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate 	if (cip->corefile_pattern != NULL) {
29960Sstevel@tonic-gate 		mypid = getpid();
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate 		if (core_set_process_path(cip->corefile_pattern,
29990Sstevel@tonic-gate 		    strlen(cip->corefile_pattern) + 1, mypid) != 0) {
30000Sstevel@tonic-gate 			*fp = "core_set_process_path";
30010Sstevel@tonic-gate 			ret = -1;
30020Sstevel@tonic-gate 			goto out;
30030Sstevel@tonic-gate 		}
30040Sstevel@tonic-gate 	}
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	if (restarter_rm_libs_loadable()) {
30070Sstevel@tonic-gate 		if (cip->project == NULL) {
30080Sstevel@tonic-gate 			if (settaskid(getprojid(), TASK_NORMAL) == -1) {
30090Sstevel@tonic-gate 				switch (errno) {
30100Sstevel@tonic-gate 				case EACCES:
30110Sstevel@tonic-gate 				case EPERM:
30120Sstevel@tonic-gate 					*fp = "settaskid";
30130Sstevel@tonic-gate 					ret = errno;
30140Sstevel@tonic-gate 					goto out;
30150Sstevel@tonic-gate 
30160Sstevel@tonic-gate 				case EINVAL:
30170Sstevel@tonic-gate 				default:
30180Sstevel@tonic-gate 					bad_fail("settaskid", errno);
30190Sstevel@tonic-gate 				}
30200Sstevel@tonic-gate 			}
30210Sstevel@tonic-gate 		} else {
30220Sstevel@tonic-gate 			switch (ret = lookup_pwd(cip)) {
30230Sstevel@tonic-gate 			case 0:
30240Sstevel@tonic-gate 				break;
30250Sstevel@tonic-gate 
30260Sstevel@tonic-gate 			case ENOMEM:
30270Sstevel@tonic-gate 			case ENOENT:
30280Sstevel@tonic-gate 				*fp = NULL;
30290Sstevel@tonic-gate 				goto out;
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate 			case EIO:
30320Sstevel@tonic-gate 			case EMFILE:
30330Sstevel@tonic-gate 			case ENFILE:
30340Sstevel@tonic-gate 				*fp = "getpwuid_r";
30350Sstevel@tonic-gate 				goto out;
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate 			default:
30380Sstevel@tonic-gate 				bad_fail("lookup_pwd", ret);
30390Sstevel@tonic-gate 			}
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate 			*fp = "setproject";
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 			switch (setproject(cip->project, cip->pwd.pw_name,
30440Sstevel@tonic-gate 			    TASK_NORMAL)) {
30450Sstevel@tonic-gate 			case 0:
30460Sstevel@tonic-gate 				break;
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 			case SETPROJ_ERR_TASK:
30490Sstevel@tonic-gate 			case SETPROJ_ERR_POOL:
30500Sstevel@tonic-gate 				ret = errno;
30510Sstevel@tonic-gate 				goto out;
30520Sstevel@tonic-gate 
30530Sstevel@tonic-gate 			default:
30540Sstevel@tonic-gate 				ret = -1;
30550Sstevel@tonic-gate 				goto out;
30560Sstevel@tonic-gate 			}
30570Sstevel@tonic-gate 		}
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate 		if (cip->resource_pool != NULL) {
30600Sstevel@tonic-gate 			if (mypid == -1)
30610Sstevel@tonic-gate 				mypid = getpid();
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate 			*fp = "pool_set_binding";
30640Sstevel@tonic-gate 
30650Sstevel@tonic-gate 			if (pool_set_binding(cip->resource_pool, P_PID,
30660Sstevel@tonic-gate 			    mypid) != PO_SUCCESS) {
30670Sstevel@tonic-gate 				switch (pool_error()) {
30681712Srm88369 				case POE_INVALID_SEARCH:
30691712Srm88369 					ret = ENOENT;
30701712Srm88369 					break;
30711712Srm88369 
30720Sstevel@tonic-gate 				case POE_BADPARAM:
30731712Srm88369 					ret = EINVAL;
30740Sstevel@tonic-gate 					break;
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 				case POE_INVALID_CONF:
30770Sstevel@tonic-gate 					ret = EBADF;
30780Sstevel@tonic-gate 					break;
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate 				case POE_SYSTEM:
30810Sstevel@tonic-gate 					ret = -1;
30820Sstevel@tonic-gate 					break;
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 				default:
30850Sstevel@tonic-gate 					bad_fail("pool_set_binding",
30860Sstevel@tonic-gate 					    pool_error());
30870Sstevel@tonic-gate 				}
30880Sstevel@tonic-gate 
30890Sstevel@tonic-gate 				goto out;
30900Sstevel@tonic-gate 			}
30910Sstevel@tonic-gate 		}
30920Sstevel@tonic-gate 	}
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	/*
30953879Svp157776 	 * Now, we have to assume our ID. If the UID is 0, we want it to be
30963879Svp157776 	 * privilege-aware, otherwise the limit set gets used instead of E/P.
30970Sstevel@tonic-gate 	 * We can do this by setting P as well, which keeps
30980Sstevel@tonic-gate 	 * PA status (see priv_can_clear_PA()).
30990Sstevel@tonic-gate 	 */
31000Sstevel@tonic-gate 
3101*9263SSean.Wilcox@Sun.COM 	*fp = "setppriv";
3102*9263SSean.Wilcox@Sun.COM 
3103*9263SSean.Wilcox@Sun.COM 	if (cip->lpriv_set != NULL) {
3104*9263SSean.Wilcox@Sun.COM 		if (setppriv(PRIV_SET, PRIV_LIMIT, cip->lpriv_set) != 0) {
3105*9263SSean.Wilcox@Sun.COM 			ret = errno;
3106*9263SSean.Wilcox@Sun.COM 			assert(ret == EFAULT || ret == EPERM);
3107*9263SSean.Wilcox@Sun.COM 			goto out;
3108*9263SSean.Wilcox@Sun.COM 		}
3109*9263SSean.Wilcox@Sun.COM 	}
3110*9263SSean.Wilcox@Sun.COM 	if (cip->priv_set != NULL) {
3111*9263SSean.Wilcox@Sun.COM 		if (setppriv(PRIV_SET, PRIV_INHERITABLE, cip->priv_set) != 0) {
3112*9263SSean.Wilcox@Sun.COM 			ret = errno;
3113*9263SSean.Wilcox@Sun.COM 			assert(ret == EFAULT || ret == EPERM);
3114*9263SSean.Wilcox@Sun.COM 			goto out;
3115*9263SSean.Wilcox@Sun.COM 		}
3116*9263SSean.Wilcox@Sun.COM 	}
3117*9263SSean.Wilcox@Sun.COM 
3118*9263SSean.Wilcox@Sun.COM 	/*
3119*9263SSean.Wilcox@Sun.COM 	 * If the limit privset is already set, then must be privilege
3120*9263SSean.Wilcox@Sun.COM 	 * aware.  Otherwise, don't assume anything, and force privilege
3121*9263SSean.Wilcox@Sun.COM 	 * aware status.
3122*9263SSean.Wilcox@Sun.COM 	 */
3123*9263SSean.Wilcox@Sun.COM 
3124*9263SSean.Wilcox@Sun.COM 	if (cip->lpriv_set == NULL && cip->priv_set != NULL) {
3125*9263SSean.Wilcox@Sun.COM 		ret = setpflags(PRIV_AWARE, 1);
3126*9263SSean.Wilcox@Sun.COM 		assert(ret == 0);
3127*9263SSean.Wilcox@Sun.COM 	}
3128*9263SSean.Wilcox@Sun.COM 
31290Sstevel@tonic-gate 	*fp = "setreuid";
31304321Scasper 	if (setreuid(cip->uid,
31314321Scasper 	    cip->euid != (uid_t)-1 ? cip->euid : cip->uid) != 0) {
31320Sstevel@tonic-gate 		ret = errno;
31330Sstevel@tonic-gate 		assert(ret == EINVAL || ret == EPERM);
31340Sstevel@tonic-gate 		goto out;
31350Sstevel@tonic-gate 	}
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	*fp = "setppriv";
31380Sstevel@tonic-gate 	if (cip->priv_set != NULL) {
31390Sstevel@tonic-gate 		if (setppriv(PRIV_SET, PRIV_PERMITTED, cip->priv_set) != 0) {
31400Sstevel@tonic-gate 			ret = errno;
31410Sstevel@tonic-gate 			assert(ret == EFAULT || ret == EPERM);
31420Sstevel@tonic-gate 			goto out;
31430Sstevel@tonic-gate 		}
31440Sstevel@tonic-gate 	}
31450Sstevel@tonic-gate 
31463879Svp157776 	/*
31473879Svp157776 	 * The last thing to do is chdir to the specified working directory.
31483879Svp157776 	 * This should come after the uid switching as only the user might
31493879Svp157776 	 * have access to the specified directory.
31503879Svp157776 	 */
31513879Svp157776 	if (cip->working_dir != NULL) {
31525040Swesolows 		do {
31533879Svp157776 			r = chdir(cip->working_dir);
31545040Swesolows 		} while (r != 0 && errno == EINTR);
31553879Svp157776 		if (r != 0) {
31563879Svp157776 			*fp = "chdir";
31573879Svp157776 			ret = errno;
31583879Svp157776 			goto out;
31593879Svp157776 		}
31603879Svp157776 	}
31613879Svp157776 
31620Sstevel@tonic-gate 	ret = 0;
31630Sstevel@tonic-gate out:
31640Sstevel@tonic-gate 	free(cip->pwbuf);
31650Sstevel@tonic-gate 	cip->pwbuf = NULL;
31660Sstevel@tonic-gate 	return (ret);
31670Sstevel@tonic-gate }
31680Sstevel@tonic-gate 
31690Sstevel@tonic-gate void
31700Sstevel@tonic-gate restarter_free_method_context(struct method_context *mcp)
31710Sstevel@tonic-gate {
31720Sstevel@tonic-gate 	size_t i;
31730Sstevel@tonic-gate 
31740Sstevel@tonic-gate 	if (mcp->lpriv_set != NULL)
31750Sstevel@tonic-gate 		priv_freeset(mcp->lpriv_set);
31760Sstevel@tonic-gate 	if (mcp->priv_set != NULL)
31770Sstevel@tonic-gate 		priv_freeset(mcp->priv_set);
31780Sstevel@tonic-gate 
31790Sstevel@tonic-gate 	if (mcp->env != NULL) {
31800Sstevel@tonic-gate 		for (i = 0; i < mcp->env_sz; i++)
31810Sstevel@tonic-gate 			free(mcp->env[i]);
31820Sstevel@tonic-gate 		free(mcp->env);
31830Sstevel@tonic-gate 	}
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 	free(mcp->working_dir);
31860Sstevel@tonic-gate 	free(mcp->corefile_pattern);
31870Sstevel@tonic-gate 	free(mcp->project);
31880Sstevel@tonic-gate 	free(mcp->resource_pool);
31890Sstevel@tonic-gate 	free(mcp);
31900Sstevel@tonic-gate }
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate /*
31930Sstevel@tonic-gate  * Method keyword functions
31940Sstevel@tonic-gate  */
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate int
31970Sstevel@tonic-gate restarter_is_null_method(const char *meth)
31980Sstevel@tonic-gate {
31990Sstevel@tonic-gate 	return (strcmp(meth, MKW_TRUE) == 0);
32000Sstevel@tonic-gate }
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate static int
32030Sstevel@tonic-gate is_kill_method(const char *method, const char *kill_str,
32040Sstevel@tonic-gate     size_t kill_str_len)
32050Sstevel@tonic-gate {
32060Sstevel@tonic-gate 	const char *cp;
32070Sstevel@tonic-gate 	int sig;
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate 	if (strncmp(method, kill_str, kill_str_len) != 0 ||
32100Sstevel@tonic-gate 	    (method[kill_str_len] != '\0' &&
32110Sstevel@tonic-gate 	    !isspace(method[kill_str_len])))
32120Sstevel@tonic-gate 		return (-1);
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate 	cp = method + kill_str_len;
32150Sstevel@tonic-gate 	while (*cp != '\0' && isspace(*cp))
32160Sstevel@tonic-gate 		++cp;
32170Sstevel@tonic-gate 
32180Sstevel@tonic-gate 	if (*cp == '\0')
32190Sstevel@tonic-gate 		return (SIGTERM);
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate 	if (*cp != '-')
32220Sstevel@tonic-gate 		return (-1);
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate 	return (str2sig(cp + 1, &sig) == 0 ? sig : -1);
32250Sstevel@tonic-gate }
32260Sstevel@tonic-gate 
32270Sstevel@tonic-gate int
32280Sstevel@tonic-gate restarter_is_kill_proc_method(const char *method)
32290Sstevel@tonic-gate {
32300Sstevel@tonic-gate 	return (is_kill_method(method, MKW_KILL_PROC,
32310Sstevel@tonic-gate 	    sizeof (MKW_KILL_PROC) - 1));
32320Sstevel@tonic-gate }
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate int
32350Sstevel@tonic-gate restarter_is_kill_method(const char *method)
32360Sstevel@tonic-gate {
32370Sstevel@tonic-gate 	return (is_kill_method(method, MKW_KILL, sizeof (MKW_KILL) - 1));
32380Sstevel@tonic-gate }
32390Sstevel@tonic-gate 
32400Sstevel@tonic-gate /*
32410Sstevel@tonic-gate  * Stubs for now.
32420Sstevel@tonic-gate  */
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate /* ARGSUSED */
32450Sstevel@tonic-gate int
32460Sstevel@tonic-gate restarter_event_get_enabled(restarter_event_t *e)
32470Sstevel@tonic-gate {
32480Sstevel@tonic-gate 	return (-1);
32490Sstevel@tonic-gate }
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate /* ARGSUSED */
32520Sstevel@tonic-gate uint64_t
32530Sstevel@tonic-gate restarter_event_get_seq(restarter_event_t *e)
32540Sstevel@tonic-gate {
32550Sstevel@tonic-gate 	return (-1);
32560Sstevel@tonic-gate }
32570Sstevel@tonic-gate 
32580Sstevel@tonic-gate /* ARGSUSED */
32590Sstevel@tonic-gate void
32600Sstevel@tonic-gate restarter_event_get_time(restarter_event_t *e, hrtime_t *time)
32610Sstevel@tonic-gate {
32620Sstevel@tonic-gate }
32638823STruong.Q.Nguyen@Sun.COM 
32648823STruong.Q.Nguyen@Sun.COM /*
32658823STruong.Q.Nguyen@Sun.COM  * Check for and validate fmri specified in restarter_actions/auxiliary_fmri
32668823STruong.Q.Nguyen@Sun.COM  * 0 - Success
32678823STruong.Q.Nguyen@Sun.COM  * 1 - Failure
32688823STruong.Q.Nguyen@Sun.COM  */
32698823STruong.Q.Nguyen@Sun.COM int
32708823STruong.Q.Nguyen@Sun.COM restarter_inst_validate_ractions_aux_fmri(scf_instance_t *inst)
32718823STruong.Q.Nguyen@Sun.COM {
32728823STruong.Q.Nguyen@Sun.COM 	scf_handle_t *h;
32738823STruong.Q.Nguyen@Sun.COM 	scf_propertygroup_t *pg;
32748823STruong.Q.Nguyen@Sun.COM 	scf_property_t *prop;
32758823STruong.Q.Nguyen@Sun.COM 	scf_value_t *val;
32768823STruong.Q.Nguyen@Sun.COM 	char *aux_fmri;
32778823STruong.Q.Nguyen@Sun.COM 	size_t size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
32788823STruong.Q.Nguyen@Sun.COM 	int ret = 1;
32798823STruong.Q.Nguyen@Sun.COM 
32808823STruong.Q.Nguyen@Sun.COM 	if ((aux_fmri = malloc(size)) == NULL)
32818823STruong.Q.Nguyen@Sun.COM 		return (1);
32828823STruong.Q.Nguyen@Sun.COM 
32838823STruong.Q.Nguyen@Sun.COM 	h = scf_instance_handle(inst);
32848823STruong.Q.Nguyen@Sun.COM 
32858823STruong.Q.Nguyen@Sun.COM 	pg = scf_pg_create(h);
32868823STruong.Q.Nguyen@Sun.COM 	prop = scf_property_create(h);
32878823STruong.Q.Nguyen@Sun.COM 	val = scf_value_create(h);
32888823STruong.Q.Nguyen@Sun.COM 	if (pg == NULL || prop == NULL || val == NULL)
32898823STruong.Q.Nguyen@Sun.COM 		goto out;
32908823STruong.Q.Nguyen@Sun.COM 
32918823STruong.Q.Nguyen@Sun.COM 	if (instance_get_or_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
32928823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER_ACTIONS_TYPE, SCF_PG_RESTARTER_ACTIONS_FLAGS,
32938823STruong.Q.Nguyen@Sun.COM 	    pg) != SCF_SUCCESS)
32948823STruong.Q.Nguyen@Sun.COM 		goto out;
32958823STruong.Q.Nguyen@Sun.COM 
32968823STruong.Q.Nguyen@Sun.COM 	if (get_astring_val(pg, SCF_PROPERTY_AUX_FMRI, aux_fmri, size,
32978823STruong.Q.Nguyen@Sun.COM 	    prop, val) != SCF_SUCCESS)
32988823STruong.Q.Nguyen@Sun.COM 		goto out;
32998823STruong.Q.Nguyen@Sun.COM 
33008823STruong.Q.Nguyen@Sun.COM 	if (scf_parse_fmri(aux_fmri, NULL, NULL, NULL, NULL, NULL,
33018823STruong.Q.Nguyen@Sun.COM 	    NULL) != SCF_SUCCESS)
33028823STruong.Q.Nguyen@Sun.COM 		goto out;
33038823STruong.Q.Nguyen@Sun.COM 
33048823STruong.Q.Nguyen@Sun.COM 	ret = 0;
33058823STruong.Q.Nguyen@Sun.COM 
33068823STruong.Q.Nguyen@Sun.COM out:
33078823STruong.Q.Nguyen@Sun.COM 	free(aux_fmri);
33088823STruong.Q.Nguyen@Sun.COM 	scf_value_destroy(val);
33098823STruong.Q.Nguyen@Sun.COM 	scf_property_destroy(prop);
33108823STruong.Q.Nguyen@Sun.COM 	scf_pg_destroy(pg);
33118823STruong.Q.Nguyen@Sun.COM 	return (ret);
33128823STruong.Q.Nguyen@Sun.COM }
33138823STruong.Q.Nguyen@Sun.COM 
33148823STruong.Q.Nguyen@Sun.COM /*
33158823STruong.Q.Nguyen@Sun.COM  * Get instance's boolean value in restarter_actions/auxiliary_tty
33168823STruong.Q.Nguyen@Sun.COM  * Return -1 on failure
33178823STruong.Q.Nguyen@Sun.COM  */
33188823STruong.Q.Nguyen@Sun.COM int
33198823STruong.Q.Nguyen@Sun.COM restarter_inst_ractions_from_tty(scf_instance_t *inst)
33208823STruong.Q.Nguyen@Sun.COM {
33218823STruong.Q.Nguyen@Sun.COM 	scf_handle_t *h;
33228823STruong.Q.Nguyen@Sun.COM 	scf_propertygroup_t *pg;
33238823STruong.Q.Nguyen@Sun.COM 	scf_property_t *prop;
33248823STruong.Q.Nguyen@Sun.COM 	scf_value_t *val;
33258823STruong.Q.Nguyen@Sun.COM 	uint8_t	has_tty;
33268823STruong.Q.Nguyen@Sun.COM 	int ret = -1;
33278823STruong.Q.Nguyen@Sun.COM 
33288823STruong.Q.Nguyen@Sun.COM 	h = scf_instance_handle(inst);
33298823STruong.Q.Nguyen@Sun.COM 	pg = scf_pg_create(h);
33308823STruong.Q.Nguyen@Sun.COM 	prop = scf_property_create(h);
33318823STruong.Q.Nguyen@Sun.COM 	val = scf_value_create(h);
33328823STruong.Q.Nguyen@Sun.COM 	if (pg == NULL || prop == NULL || val == NULL)
33338823STruong.Q.Nguyen@Sun.COM 		goto out;
33348823STruong.Q.Nguyen@Sun.COM 
33358823STruong.Q.Nguyen@Sun.COM 	if (instance_get_or_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
33368823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER_ACTIONS_TYPE, SCF_PG_RESTARTER_ACTIONS_FLAGS,
33378823STruong.Q.Nguyen@Sun.COM 	    pg) != SCF_SUCCESS)
33388823STruong.Q.Nguyen@Sun.COM 		goto out;
33398823STruong.Q.Nguyen@Sun.COM 
33408823STruong.Q.Nguyen@Sun.COM 	if (get_boolean_val(pg, SCF_PROPERTY_AUX_TTY, &has_tty, prop,
33418823STruong.Q.Nguyen@Sun.COM 	    val) != SCF_SUCCESS)
33428823STruong.Q.Nguyen@Sun.COM 		goto out;
33438823STruong.Q.Nguyen@Sun.COM 
33448823STruong.Q.Nguyen@Sun.COM 	ret = has_tty;
33458823STruong.Q.Nguyen@Sun.COM 
33468823STruong.Q.Nguyen@Sun.COM out:
33478823STruong.Q.Nguyen@Sun.COM 	scf_value_destroy(val);
33488823STruong.Q.Nguyen@Sun.COM 	scf_property_destroy(prop);
33498823STruong.Q.Nguyen@Sun.COM 	scf_pg_destroy(pg);
33508823STruong.Q.Nguyen@Sun.COM 	return (ret);
33518823STruong.Q.Nguyen@Sun.COM }
33528823STruong.Q.Nguyen@Sun.COM 
33538823STruong.Q.Nguyen@Sun.COM static int
33548823STruong.Q.Nguyen@Sun.COM restarter_inst_set_astring_prop(scf_instance_t *inst, const char *pgname,
33558823STruong.Q.Nguyen@Sun.COM     const char *pgtype, uint32_t pgflags, const char *pname, const char *str)
33568823STruong.Q.Nguyen@Sun.COM {
33578823STruong.Q.Nguyen@Sun.COM 	scf_handle_t *h;
33588823STruong.Q.Nguyen@Sun.COM 	scf_propertygroup_t *pg;
33598823STruong.Q.Nguyen@Sun.COM 	scf_transaction_t *t;
33608823STruong.Q.Nguyen@Sun.COM 	scf_transaction_entry_t *e;
33618823STruong.Q.Nguyen@Sun.COM 	scf_value_t *v;
33628823STruong.Q.Nguyen@Sun.COM 	int ret = 1, r;
33638823STruong.Q.Nguyen@Sun.COM 
33648823STruong.Q.Nguyen@Sun.COM 	h = scf_instance_handle(inst);
33658823STruong.Q.Nguyen@Sun.COM 
33668823STruong.Q.Nguyen@Sun.COM 	pg = scf_pg_create(h);
33678823STruong.Q.Nguyen@Sun.COM 	t = scf_transaction_create(h);
33688823STruong.Q.Nguyen@Sun.COM 	e = scf_entry_create(h);
33698823STruong.Q.Nguyen@Sun.COM 	v = scf_value_create(h);
33708823STruong.Q.Nguyen@Sun.COM 	if (pg == NULL || t == NULL || e == NULL || v == NULL)
33718823STruong.Q.Nguyen@Sun.COM 		goto out;
33728823STruong.Q.Nguyen@Sun.COM 
33738823STruong.Q.Nguyen@Sun.COM 	if (instance_get_or_add_pg(inst, pgname, pgtype, pgflags, pg))
33748823STruong.Q.Nguyen@Sun.COM 		goto out;
33758823STruong.Q.Nguyen@Sun.COM 
33768823STruong.Q.Nguyen@Sun.COM 	if (scf_value_set_astring(v, str) != SCF_SUCCESS)
33778823STruong.Q.Nguyen@Sun.COM 		goto out;
33788823STruong.Q.Nguyen@Sun.COM 
33798823STruong.Q.Nguyen@Sun.COM 	for (;;) {
33808823STruong.Q.Nguyen@Sun.COM 		if (scf_transaction_start(t, pg) != 0)
33818823STruong.Q.Nguyen@Sun.COM 			goto out;
33828823STruong.Q.Nguyen@Sun.COM 
33838823STruong.Q.Nguyen@Sun.COM 		if (tx_set_value(t, e, pname, SCF_TYPE_ASTRING, v) != 0)
33848823STruong.Q.Nguyen@Sun.COM 			goto out;
33858823STruong.Q.Nguyen@Sun.COM 
33868823STruong.Q.Nguyen@Sun.COM 		if ((r = scf_transaction_commit(t)) == 1)
33878823STruong.Q.Nguyen@Sun.COM 			break;
33888823STruong.Q.Nguyen@Sun.COM 
33898823STruong.Q.Nguyen@Sun.COM 		if (r == -1)
33908823STruong.Q.Nguyen@Sun.COM 			goto out;
33918823STruong.Q.Nguyen@Sun.COM 
33928823STruong.Q.Nguyen@Sun.COM 		scf_transaction_reset(t);
33938823STruong.Q.Nguyen@Sun.COM 		if (scf_pg_update(pg) == -1)
33948823STruong.Q.Nguyen@Sun.COM 			goto out;
33958823STruong.Q.Nguyen@Sun.COM 	}
33968823STruong.Q.Nguyen@Sun.COM 	ret = 0;
33978823STruong.Q.Nguyen@Sun.COM 
33988823STruong.Q.Nguyen@Sun.COM out:
33998823STruong.Q.Nguyen@Sun.COM 	scf_transaction_destroy(t);
34008823STruong.Q.Nguyen@Sun.COM 	scf_entry_destroy(e);
34018823STruong.Q.Nguyen@Sun.COM 	scf_value_destroy(v);
34028823STruong.Q.Nguyen@Sun.COM 	scf_pg_destroy(pg);
34038823STruong.Q.Nguyen@Sun.COM 
34048823STruong.Q.Nguyen@Sun.COM 	return (ret);
34058823STruong.Q.Nguyen@Sun.COM }
34068823STruong.Q.Nguyen@Sun.COM 
34078823STruong.Q.Nguyen@Sun.COM int
34088823STruong.Q.Nguyen@Sun.COM restarter_inst_set_aux_fmri(scf_instance_t *inst)
34098823STruong.Q.Nguyen@Sun.COM {
34108823STruong.Q.Nguyen@Sun.COM 	scf_handle_t *h;
34118823STruong.Q.Nguyen@Sun.COM 	scf_propertygroup_t *pg;
34128823STruong.Q.Nguyen@Sun.COM 	scf_property_t *prop;
34138823STruong.Q.Nguyen@Sun.COM 	scf_value_t *val;
34148823STruong.Q.Nguyen@Sun.COM 	char *aux_fmri;
34158823STruong.Q.Nguyen@Sun.COM 	size_t size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
34168823STruong.Q.Nguyen@Sun.COM 	int ret = 1;
34178823STruong.Q.Nguyen@Sun.COM 
34188823STruong.Q.Nguyen@Sun.COM 	if ((aux_fmri = malloc(size)) == NULL)
34198823STruong.Q.Nguyen@Sun.COM 		return (1);
34208823STruong.Q.Nguyen@Sun.COM 
34218823STruong.Q.Nguyen@Sun.COM 	h = scf_instance_handle(inst);
34228823STruong.Q.Nguyen@Sun.COM 
34238823STruong.Q.Nguyen@Sun.COM 	pg = scf_pg_create(h);
34248823STruong.Q.Nguyen@Sun.COM 	prop = scf_property_create(h);
34258823STruong.Q.Nguyen@Sun.COM 	val = scf_value_create(h);
34268823STruong.Q.Nguyen@Sun.COM 	if (pg == NULL || prop == NULL || val == NULL)
34278823STruong.Q.Nguyen@Sun.COM 		goto out;
34288823STruong.Q.Nguyen@Sun.COM 
34298823STruong.Q.Nguyen@Sun.COM 	/*
34308823STruong.Q.Nguyen@Sun.COM 	 * Get auxiliary_fmri value from restarter_actions pg
34318823STruong.Q.Nguyen@Sun.COM 	 */
34328823STruong.Q.Nguyen@Sun.COM 	if (instance_get_or_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
34338823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER_ACTIONS_TYPE, SCF_PG_RESTARTER_ACTIONS_FLAGS,
34348823STruong.Q.Nguyen@Sun.COM 	    pg) != SCF_SUCCESS)
34358823STruong.Q.Nguyen@Sun.COM 		goto out;
34368823STruong.Q.Nguyen@Sun.COM 
34378823STruong.Q.Nguyen@Sun.COM 	if (get_astring_val(pg, SCF_PROPERTY_AUX_FMRI, aux_fmri, size,
34388823STruong.Q.Nguyen@Sun.COM 	    prop, val) != SCF_SUCCESS)
34398823STruong.Q.Nguyen@Sun.COM 		goto out;
34408823STruong.Q.Nguyen@Sun.COM 
34418823STruong.Q.Nguyen@Sun.COM 	/*
34428823STruong.Q.Nguyen@Sun.COM 	 * Populate restarter/auxiliary_fmri with the obtained fmri.
34438823STruong.Q.Nguyen@Sun.COM 	 */
34448823STruong.Q.Nguyen@Sun.COM 	ret = restarter_inst_set_astring_prop(inst, SCF_PG_RESTARTER,
34458823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS,
34468823STruong.Q.Nguyen@Sun.COM 	    SCF_PROPERTY_AUX_FMRI, aux_fmri);
34478823STruong.Q.Nguyen@Sun.COM 
34488823STruong.Q.Nguyen@Sun.COM out:
34498823STruong.Q.Nguyen@Sun.COM 	free(aux_fmri);
34508823STruong.Q.Nguyen@Sun.COM 	scf_value_destroy(val);
34518823STruong.Q.Nguyen@Sun.COM 	scf_property_destroy(prop);
34528823STruong.Q.Nguyen@Sun.COM 	scf_pg_destroy(pg);
34538823STruong.Q.Nguyen@Sun.COM 	return (ret);
34548823STruong.Q.Nguyen@Sun.COM }
34558823STruong.Q.Nguyen@Sun.COM 
34568823STruong.Q.Nguyen@Sun.COM int
34578823STruong.Q.Nguyen@Sun.COM restarter_inst_reset_aux_fmri(scf_instance_t *inst)
34588823STruong.Q.Nguyen@Sun.COM {
34598823STruong.Q.Nguyen@Sun.COM 	return (scf_instance_delete_prop(inst,
34608823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER, SCF_PROPERTY_AUX_FMRI));
34618823STruong.Q.Nguyen@Sun.COM }
34628823STruong.Q.Nguyen@Sun.COM 
34638823STruong.Q.Nguyen@Sun.COM int
34648823STruong.Q.Nguyen@Sun.COM restarter_inst_reset_ractions_aux_fmri(scf_instance_t *inst)
34658823STruong.Q.Nguyen@Sun.COM {
34668823STruong.Q.Nguyen@Sun.COM 	return (scf_instance_delete_prop(inst,
34678823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER_ACTIONS, SCF_PROPERTY_AUX_FMRI));
34688823STruong.Q.Nguyen@Sun.COM }
3469