xref: /onnv-gate/usr/src/lib/librestart/common/librestart.c (revision 9765:2522fef20c5f)
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 
110*9765SSean.Wilcox@Sun.COM /*
111*9765SSean.Wilcox@Sun.COM  * A static no memory error message mc_error_t structure
112*9765SSean.Wilcox@Sun.COM  * to be used in cases when memory errors are to be returned
113*9765SSean.Wilcox@Sun.COM  * This avoids the need to attempt to allocate memory for the
114*9765SSean.Wilcox@Sun.COM  * message, therefore getting into a cycle of no memory failures.
115*9765SSean.Wilcox@Sun.COM  */
116*9765SSean.Wilcox@Sun.COM mc_error_t mc_nomem_err = {
117*9765SSean.Wilcox@Sun.COM 	0, ENOMEM, sizeof ("Out of memory") - 1, "Out of memory"
118*9765SSean.Wilcox@Sun.COM };
119*9765SSean.Wilcox@Sun.COM 
1200Sstevel@tonic-gate static const char * const allocfail = "Allocation failure.\n";
1210Sstevel@tonic-gate static const char * const rcbroken = "Repository connection broken.\n";
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate static int method_context_safety = 0;	/* Can safely call pools/projects. */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int ndebug = 1;
1260Sstevel@tonic-gate 
127*9765SSean.Wilcox@Sun.COM /* PRINTFLIKE3 */
128*9765SSean.Wilcox@Sun.COM static mc_error_t *
129*9765SSean.Wilcox@Sun.COM mc_error_create(mc_error_t *e, int type, const char *format, ...)
130*9765SSean.Wilcox@Sun.COM {
131*9765SSean.Wilcox@Sun.COM 	mc_error_t	*le;
132*9765SSean.Wilcox@Sun.COM 	va_list		args;
133*9765SSean.Wilcox@Sun.COM 	int		size;
134*9765SSean.Wilcox@Sun.COM 
135*9765SSean.Wilcox@Sun.COM 	/*
136*9765SSean.Wilcox@Sun.COM 	 * If the type is ENOMEM and format is NULL, then
137*9765SSean.Wilcox@Sun.COM 	 * go ahead and return the default nomem error.
138*9765SSean.Wilcox@Sun.COM 	 * Otherwise, attempt to allocate the memory and if
139*9765SSean.Wilcox@Sun.COM 	 * that fails then there is no reason to continue.
140*9765SSean.Wilcox@Sun.COM 	 */
141*9765SSean.Wilcox@Sun.COM 	if (type == ENOMEM && format == NULL)
142*9765SSean.Wilcox@Sun.COM 		return (&mc_nomem_err);
143*9765SSean.Wilcox@Sun.COM 
144*9765SSean.Wilcox@Sun.COM 	if (e == NULL && (le = malloc(sizeof (mc_error_t))) == NULL)
145*9765SSean.Wilcox@Sun.COM 		return (&mc_nomem_err);
146*9765SSean.Wilcox@Sun.COM 	else
147*9765SSean.Wilcox@Sun.COM 		le = e;
148*9765SSean.Wilcox@Sun.COM 
149*9765SSean.Wilcox@Sun.COM 	le->type = type;
150*9765SSean.Wilcox@Sun.COM 	le->destroy = 1;
151*9765SSean.Wilcox@Sun.COM 	va_start(args, format);
152*9765SSean.Wilcox@Sun.COM 	size = vsnprintf(NULL, 0, format, args) + 1;
153*9765SSean.Wilcox@Sun.COM 	if (size >= RESTARTER_ERRMSGSZ) {
154*9765SSean.Wilcox@Sun.COM 		if ((le = realloc(e, sizeof (mc_error_t) +
155*9765SSean.Wilcox@Sun.COM 		    (size - RESTARTER_ERRMSGSZ))) == NULL) {
156*9765SSean.Wilcox@Sun.COM 			size = RESTARTER_ERRMSGSZ - 1;
157*9765SSean.Wilcox@Sun.COM 			le = e;
158*9765SSean.Wilcox@Sun.COM 		}
159*9765SSean.Wilcox@Sun.COM 	}
160*9765SSean.Wilcox@Sun.COM 
161*9765SSean.Wilcox@Sun.COM 	le->size = size;
162*9765SSean.Wilcox@Sun.COM 	(void) vsnprintf(le->msg, le->size, format, args);
163*9765SSean.Wilcox@Sun.COM 	va_end(args);
164*9765SSean.Wilcox@Sun.COM 
165*9765SSean.Wilcox@Sun.COM 	return (le);
166*9765SSean.Wilcox@Sun.COM }
167*9765SSean.Wilcox@Sun.COM 
168*9765SSean.Wilcox@Sun.COM void
169*9765SSean.Wilcox@Sun.COM restarter_mc_error_destroy(mc_error_t *mc_err)
170*9765SSean.Wilcox@Sun.COM {
171*9765SSean.Wilcox@Sun.COM 	if (mc_err == NULL)
172*9765SSean.Wilcox@Sun.COM 		return;
173*9765SSean.Wilcox@Sun.COM 
174*9765SSean.Wilcox@Sun.COM 	/*
175*9765SSean.Wilcox@Sun.COM 	 * If the error messages was allocated then free.
176*9765SSean.Wilcox@Sun.COM 	 */
177*9765SSean.Wilcox@Sun.COM 	if (mc_err->destroy) {
178*9765SSean.Wilcox@Sun.COM 		free(mc_err);
179*9765SSean.Wilcox@Sun.COM 	}
180*9765SSean.Wilcox@Sun.COM }
181*9765SSean.Wilcox@Sun.COM 
1820Sstevel@tonic-gate static void
1830Sstevel@tonic-gate free_restarter_event_handle(struct restarter_event_handle *h)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	if (h == NULL)
1860Sstevel@tonic-gate 		return;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/*
1890Sstevel@tonic-gate 	 * Just free the memory -- don't unbind the sysevent handle,
1900Sstevel@tonic-gate 	 * as otherwise events may be lost if this is just a restarter
1910Sstevel@tonic-gate 	 * restart.
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if (h->reh_restarter_name != NULL)
1950Sstevel@tonic-gate 		free(h->reh_restarter_name);
1960Sstevel@tonic-gate 	if (h->reh_delegate_channel_name != NULL)
1970Sstevel@tonic-gate 		free(h->reh_delegate_channel_name);
1980Sstevel@tonic-gate 	if (h->reh_delegate_subscriber_id != NULL)
1990Sstevel@tonic-gate 		free(h->reh_delegate_subscriber_id);
2000Sstevel@tonic-gate 	if (h->reh_master_channel_name != NULL)
2010Sstevel@tonic-gate 		free(h->reh_master_channel_name);
2020Sstevel@tonic-gate 	if (h->reh_master_subscriber_id != NULL)
2030Sstevel@tonic-gate 		free(h->reh_master_subscriber_id);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	free(h);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate char *
2090Sstevel@tonic-gate _restarter_get_channel_name(const char *fmri, int type)
2100Sstevel@tonic-gate {
2119263SSean.Wilcox@Sun.COM 	char *name;
2120Sstevel@tonic-gate 	char *chan_name = malloc(MAX_CHNAME_LEN);
2130Sstevel@tonic-gate 	char prefix_name[3];
2149263SSean.Wilcox@Sun.COM 	int i;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if (chan_name == NULL)
2170Sstevel@tonic-gate 		return (NULL);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (type == RESTARTER_CHANNEL_DELEGATE)
2200Sstevel@tonic-gate 		(void) strcpy(prefix_name, "d_");
2210Sstevel@tonic-gate 	else if (type == RESTARTER_CHANNEL_MASTER)
2220Sstevel@tonic-gate 		(void) strcpy(prefix_name, "m_");
2230Sstevel@tonic-gate 	else {
2240Sstevel@tonic-gate 		free(chan_name);
2250Sstevel@tonic-gate 		return (NULL);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2289263SSean.Wilcox@Sun.COM 	/*
2299263SSean.Wilcox@Sun.COM 	 * Create a unique name
2309263SSean.Wilcox@Sun.COM 	 *
2319263SSean.Wilcox@Sun.COM 	 * Use the entire name, using a replacement of the /
2329263SSean.Wilcox@Sun.COM 	 * characters to get a better name.
2339263SSean.Wilcox@Sun.COM 	 *
2349263SSean.Wilcox@Sun.COM 	 * Remove the svc:/ from the beginning as this really
2359263SSean.Wilcox@Sun.COM 	 * isn't going to provide any uniqueness...
2369263SSean.Wilcox@Sun.COM 	 *
2379263SSean.Wilcox@Sun.COM 	 * An fmri name greater than MAX_CHNAME_LEN is going
2389263SSean.Wilcox@Sun.COM 	 * to be rejected as too long for the chan_name below
2399263SSean.Wilcox@Sun.COM 	 * in the snprintf call.
2409263SSean.Wilcox@Sun.COM 	 */
2419263SSean.Wilcox@Sun.COM 	if ((name = strdup(strchr(fmri, '/') + 1)) == NULL) {
2429263SSean.Wilcox@Sun.COM 		free(chan_name);
2439263SSean.Wilcox@Sun.COM 		return (NULL);
2449263SSean.Wilcox@Sun.COM 	}
2459263SSean.Wilcox@Sun.COM 	i = 0;
2469263SSean.Wilcox@Sun.COM 	while (name[i]) {
2479263SSean.Wilcox@Sun.COM 		if (name[i] == '/') {
2489263SSean.Wilcox@Sun.COM 			name[i] = '_';
2499263SSean.Wilcox@Sun.COM 		}
2509263SSean.Wilcox@Sun.COM 
2519263SSean.Wilcox@Sun.COM 		i++;
2529263SSean.Wilcox@Sun.COM 	}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * Should check for [a-z],[A-Z],[0-9],.,_,-,:
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if (snprintf(chan_name, MAX_CHNAME_LEN, "com.sun:scf:%s%s",
2590Sstevel@tonic-gate 	    prefix_name, name) > MAX_CHNAME_LEN) {
2600Sstevel@tonic-gate 		free(chan_name);
2619263SSean.Wilcox@Sun.COM 		chan_name = NULL;
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2649263SSean.Wilcox@Sun.COM 	free(name);
2650Sstevel@tonic-gate 	return (chan_name);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate int
2690Sstevel@tonic-gate cb(sysevent_t *syse, void *cookie)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	restarter_event_handle_t *h = (restarter_event_handle_t *)cookie;
2720Sstevel@tonic-gate 	restarter_event_t *e;
2730Sstevel@tonic-gate 	nvlist_t *attr_list = NULL;
2740Sstevel@tonic-gate 	int ret = 0;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	e = uu_zalloc(sizeof (restarter_event_t));
2770Sstevel@tonic-gate 	if (e == NULL)
2780Sstevel@tonic-gate 		uu_die(allocfail);
2790Sstevel@tonic-gate 	e->re_event_handle = h;
2800Sstevel@tonic-gate 	e->re_sysevent = syse;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if (sysevent_get_attr_list(syse, &attr_list) != 0)
2830Sstevel@tonic-gate 		uu_die(allocfail);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	if ((nvlist_lookup_uint32(attr_list, RESTARTER_NAME_TYPE,
2860Sstevel@tonic-gate 	    &(e->re_type)) != 0) ||
2870Sstevel@tonic-gate 	    (nvlist_lookup_string(attr_list,
2880Sstevel@tonic-gate 	    RESTARTER_NAME_INSTANCE, &(e->re_instance_name)) != 0)) {
2890Sstevel@tonic-gate 		uu_warn("%s: Can't decode nvlist for event %p\n",
2900Sstevel@tonic-gate 		    h->reh_restarter_name, (void *)syse);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		ret = 0;
2930Sstevel@tonic-gate 	} else {
2940Sstevel@tonic-gate 		ret = h->reh_handler(e);
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	uu_free(e);
2980Sstevel@tonic-gate 	nvlist_free(attr_list);
2990Sstevel@tonic-gate 	return (ret);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /*
3030Sstevel@tonic-gate  * restarter_bind_handle(uint32_t, char *, int (*)(restarter_event_t *), int,
3040Sstevel@tonic-gate  *     restarter_event_handle_t **)
3050Sstevel@tonic-gate  *
3060Sstevel@tonic-gate  * Bind to a delegated restarter event channel.
3070Sstevel@tonic-gate  * Each delegated restarter gets its own channel for resource management.
3080Sstevel@tonic-gate  *
3090Sstevel@tonic-gate  * Returns 0 on success or
3100Sstevel@tonic-gate  *   ENOTSUP	version mismatch
3110Sstevel@tonic-gate  *   EINVAL	restarter_name or event_handle is NULL
3120Sstevel@tonic-gate  *   ENOMEM	out of memory, too many channels, or too many subscriptions
3130Sstevel@tonic-gate  *   EBUSY	sysevent_evc_bind() could not establish binding
3140Sstevel@tonic-gate  *   EFAULT	internal sysevent_evc_bind()/sysevent_evc_subscribe() error
3150Sstevel@tonic-gate  *   EMFILE	out of file descriptors
3160Sstevel@tonic-gate  *   EPERM	insufficient privilege for sysevent_evc_bind()
3170Sstevel@tonic-gate  *   EEXIST	already subscribed
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate int
3200Sstevel@tonic-gate restarter_bind_handle(uint32_t version, const char *restarter_name,
3210Sstevel@tonic-gate     int (*event_handler)(restarter_event_t *), int flags,
3220Sstevel@tonic-gate     restarter_event_handle_t **rehp)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	restarter_event_handle_t *h;
3250Sstevel@tonic-gate 	size_t sz;
3260Sstevel@tonic-gate 	int err;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (version != RESTARTER_EVENT_VERSION)
3290Sstevel@tonic-gate 		return (ENOTSUP);
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	if (restarter_name == NULL || event_handler == NULL)
3320Sstevel@tonic-gate 		return (EINVAL);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	if (flags & RESTARTER_FLAG_DEBUG)
3350Sstevel@tonic-gate 		ndebug++;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if ((h = uu_zalloc(sizeof (restarter_event_handle_t))) == NULL)
3380Sstevel@tonic-gate 		return (ENOMEM);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	h->reh_delegate_subscriber_id = malloc(MAX_SUBID_LEN);
3410Sstevel@tonic-gate 	h->reh_master_subscriber_id = malloc(MAX_SUBID_LEN);
3420Sstevel@tonic-gate 	h->reh_restarter_name = strdup(restarter_name);
3430Sstevel@tonic-gate 	if (h->reh_delegate_subscriber_id == NULL ||
3440Sstevel@tonic-gate 	    h->reh_master_subscriber_id == NULL ||
3450Sstevel@tonic-gate 	    h->reh_restarter_name == NULL) {
3460Sstevel@tonic-gate 		free_restarter_event_handle(h);
3470Sstevel@tonic-gate 		return (ENOMEM);
3480Sstevel@tonic-gate 	}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	sz = strlcpy(h->reh_delegate_subscriber_id, "del", MAX_SUBID_LEN);
3510Sstevel@tonic-gate 	assert(sz < MAX_SUBID_LEN);
3520Sstevel@tonic-gate 	sz = strlcpy(h->reh_master_subscriber_id, "master", MAX_SUBID_LEN);
3530Sstevel@tonic-gate 	assert(sz < MAX_SUBID_LEN);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	h->reh_delegate_channel_name =
3560Sstevel@tonic-gate 	    _restarter_get_channel_name(restarter_name,
3570Sstevel@tonic-gate 	    RESTARTER_CHANNEL_DELEGATE);
3580Sstevel@tonic-gate 	h->reh_master_channel_name =
3590Sstevel@tonic-gate 	    _restarter_get_channel_name(restarter_name,
3600Sstevel@tonic-gate 	    RESTARTER_CHANNEL_MASTER);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	if (h->reh_delegate_channel_name == NULL ||
3630Sstevel@tonic-gate 	    h->reh_master_channel_name == NULL) {
3640Sstevel@tonic-gate 		free_restarter_event_handle(h);
3650Sstevel@tonic-gate 		return (ENOMEM);
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (sysevent_evc_bind(h->reh_delegate_channel_name,
3690Sstevel@tonic-gate 	    &h->reh_delegate_channel, EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
3700Sstevel@tonic-gate 		err = errno;
3710Sstevel@tonic-gate 		assert(err != EINVAL);
3720Sstevel@tonic-gate 		assert(err != ENOENT);
3730Sstevel@tonic-gate 		free_restarter_event_handle(h);
3740Sstevel@tonic-gate 		return (err);
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	if (sysevent_evc_bind(h->reh_master_channel_name,
3780Sstevel@tonic-gate 	    &h->reh_master_channel, EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
3790Sstevel@tonic-gate 		err = errno;
3800Sstevel@tonic-gate 		assert(err != EINVAL);
3810Sstevel@tonic-gate 		assert(err != ENOENT);
3820Sstevel@tonic-gate 		free_restarter_event_handle(h);
3830Sstevel@tonic-gate 		return (err);
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	h->reh_handler = event_handler;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	assert(strlen(restarter_name) <= MAX_CLASS_LEN - 1);
3890Sstevel@tonic-gate 	assert(strlen(h->reh_delegate_subscriber_id) <= MAX_SUBID_LEN - 1);
3900Sstevel@tonic-gate 	assert(strlen(h->reh_master_subscriber_id) <= MAX_SUBID_LEN - 1);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (sysevent_evc_subscribe(h->reh_delegate_channel,
3930Sstevel@tonic-gate 	    h->reh_delegate_subscriber_id, EC_ALL, cb, h, EVCH_SUB_KEEP) != 0) {
3940Sstevel@tonic-gate 		err = errno;
3950Sstevel@tonic-gate 		assert(err != EINVAL);
3960Sstevel@tonic-gate 		free_restarter_event_handle(h);
3970Sstevel@tonic-gate 		return (err);
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	*rehp = h;
4010Sstevel@tonic-gate 	return (0);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate restarter_event_handle_t *
4050Sstevel@tonic-gate restarter_event_get_handle(restarter_event_t *e)
4060Sstevel@tonic-gate {
4070Sstevel@tonic-gate 	assert(e != NULL && e->re_event_handle != NULL);
4080Sstevel@tonic-gate 	return (e->re_event_handle);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate restarter_event_type_t
4120Sstevel@tonic-gate restarter_event_get_type(restarter_event_t *e)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate 	assert(e != NULL);
4150Sstevel@tonic-gate 	return (e->re_type);
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate ssize_t
4190Sstevel@tonic-gate restarter_event_get_instance(restarter_event_t *e, char *inst, size_t sz)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	assert(e != NULL && inst != NULL);
4220Sstevel@tonic-gate 	return ((ssize_t)strlcpy(inst, e->re_instance_name, sz));
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate int
4260Sstevel@tonic-gate restarter_event_get_current_states(restarter_event_t *e,
4270Sstevel@tonic-gate     restarter_instance_state_t *state, restarter_instance_state_t *next_state)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	if (e == NULL)
4300Sstevel@tonic-gate 		return (-1);
4310Sstevel@tonic-gate 	*state = e->re_state;
4320Sstevel@tonic-gate 	*next_state = e->re_next_state;
4330Sstevel@tonic-gate 	return (0);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate /*
4376045Srm88369  * restarter_event_publish_retry() is a wrapper around sysevent_evc_publish().
4386045Srm88369  * In case, the event cannot be sent at the first attempt (sysevent_evc_publish
4396045Srm88369  * returned EAGAIN - sysevent queue full), this function retries a few time
4406045Srm88369  * and return ENOSPC if it reaches the retry limit.
4416045Srm88369  *
4426045Srm88369  * The arguments to this function map the arguments of sysevent_evc_publish().
4436045Srm88369  *
4446045Srm88369  * On success, return 0. On error, return
4456045Srm88369  *
4466045Srm88369  *   EFAULT - internal sysevent_evc_publish() error
4476045Srm88369  *   ENOMEM - internal sysevent_evc_publish() error
4486045Srm88369  *   EBADF - scp is invalid (sysevent_evc_publish() returned EINVAL)
4496045Srm88369  *   ENOSPC - sysevent queue full (sysevent_evc_publish() returned EAGAIN)
4506045Srm88369  */
4516045Srm88369 int
4526045Srm88369 restarter_event_publish_retry(evchan_t *scp, const char *class,
4536045Srm88369     const char *subclass, const char *vendor, const char *pub_name,
4546045Srm88369     nvlist_t *attr_list, uint32_t flags)
4556045Srm88369 {
4566045Srm88369 	int retries, ret;
4576045Srm88369 	useconds_t retry_int = INITIAL_COMMIT_RETRY_INT;
4586045Srm88369 
4596045Srm88369 	for (retries = 0; retries < MAX_COMMIT_RETRIES; retries++) {
4606045Srm88369 		ret = sysevent_evc_publish(scp, class, subclass, vendor,
4616045Srm88369 		    pub_name, attr_list, flags);
4626045Srm88369 		if (ret == 0)
4636045Srm88369 			break;
4646045Srm88369 
4656045Srm88369 		switch (ret) {
4666045Srm88369 		case EAGAIN:
4676045Srm88369 			/* Queue is full */
4686045Srm88369 			(void) usleep(retry_int);
4696045Srm88369 
4706045Srm88369 			retry_int = min(retry_int * 2, MAX_COMMIT_RETRY_INT);
4716045Srm88369 			break;
4726045Srm88369 
4736045Srm88369 		case EINVAL:
4746045Srm88369 			ret = EBADF;
4756045Srm88369 			/* FALLTHROUGH */
4766045Srm88369 
4776045Srm88369 		case EFAULT:
4786045Srm88369 		case ENOMEM:
4796045Srm88369 			return (ret);
4806045Srm88369 
4816045Srm88369 		case EOVERFLOW:
4826045Srm88369 		default:
4836045Srm88369 			/* internal error - abort */
4846045Srm88369 			bad_fail("sysevent_evc_publish", ret);
4856045Srm88369 		}
4866045Srm88369 	}
4876045Srm88369 
4886045Srm88369 	if (retries == MAX_COMMIT_RETRIES)
4896045Srm88369 		ret = ENOSPC;
4906045Srm88369 
4916045Srm88369 	return (ret);
4926045Srm88369 }
4936045Srm88369 
4946045Srm88369 /*
4950Sstevel@tonic-gate  * Commit the state, next state, and auxiliary state into the repository.
4960Sstevel@tonic-gate  * Let the graph engine know about the state change and error.  On success,
4970Sstevel@tonic-gate  * return 0. On error, return
4980Sstevel@tonic-gate  *   EINVAL - aux has spaces
4990Sstevel@tonic-gate  *	    - inst is invalid or not an instance FMRI
5000Sstevel@tonic-gate  *   EPROTO - librestart compiled against different libscf
5010Sstevel@tonic-gate  *   ENOMEM - out of memory
5020Sstevel@tonic-gate  *	    - repository server out of resources
5030Sstevel@tonic-gate  *   ENOTACTIVE - repository server not running
5040Sstevel@tonic-gate  *   ECONNABORTED - repository connection established, but then broken
5050Sstevel@tonic-gate  *		  - unknown libscf error
5060Sstevel@tonic-gate  *   ENOENT - inst does not exist in the repository
5070Sstevel@tonic-gate  *   EPERM - insufficient permissions
5080Sstevel@tonic-gate  *   EACCESS - backend access denied
5090Sstevel@tonic-gate  *   EROFS - backend is readonly
5100Sstevel@tonic-gate  *   EFAULT - internal sysevent_evc_publish() error
5110Sstevel@tonic-gate  *   EBADF - h is invalid (sysevent_evc_publish() returned EINVAL)
5126045Srm88369  *   ENOSPC - sysevent queue full (sysevent_evc_publish() returned EAGAIN)
5130Sstevel@tonic-gate  */
5140Sstevel@tonic-gate int
5150Sstevel@tonic-gate restarter_set_states(restarter_event_handle_t *h, const char *inst,
5160Sstevel@tonic-gate     restarter_instance_state_t cur_state,
5170Sstevel@tonic-gate     restarter_instance_state_t new_cur_state,
5180Sstevel@tonic-gate     restarter_instance_state_t next_state,
5190Sstevel@tonic-gate     restarter_instance_state_t new_next_state, restarter_error_t e,
5200Sstevel@tonic-gate     const char *aux)
5210Sstevel@tonic-gate {
5220Sstevel@tonic-gate 	nvlist_t *attr;
5230Sstevel@tonic-gate 	scf_handle_t *scf_h;
5240Sstevel@tonic-gate 	instance_data_t id;
5250Sstevel@tonic-gate 	int ret = 0;
5260Sstevel@tonic-gate 	char *p = (char *)aux;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	assert(h->reh_master_channel != NULL);
5290Sstevel@tonic-gate 	assert(h->reh_master_channel_name != NULL);
5300Sstevel@tonic-gate 	assert(h->reh_master_subscriber_id != NULL);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/* Validate format of auxiliary state: no spaces allowed */
533863Slianep 	if (p != NULL) {
534863Slianep 		while (*p != '\0') {
535863Slianep 			if (isspace(*p))
536863Slianep 				return (EINVAL);
537863Slianep 			p++;
538863Slianep 		}
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	if ((scf_h = scf_handle_create(SCF_VERSION)) == NULL) {
5420Sstevel@tonic-gate 		switch (scf_error()) {
5430Sstevel@tonic-gate 		case SCF_ERROR_VERSION_MISMATCH:
5440Sstevel@tonic-gate 			return (EPROTO);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 		case SCF_ERROR_NO_MEMORY:
5470Sstevel@tonic-gate 			return (ENOMEM);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 		default:
5500Sstevel@tonic-gate 			bad_fail("scf_handle_create", scf_error());
5510Sstevel@tonic-gate 		}
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	if (scf_handle_bind(scf_h) == -1) {
5550Sstevel@tonic-gate 		scf_handle_destroy(scf_h);
5560Sstevel@tonic-gate 		switch (scf_error()) {
5570Sstevel@tonic-gate 		case SCF_ERROR_NO_SERVER:
5580Sstevel@tonic-gate 			return (ENOTACTIVE);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 		case SCF_ERROR_NO_RESOURCES:
5610Sstevel@tonic-gate 			return (ENOMEM);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
5640Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
5650Sstevel@tonic-gate 		default:
5660Sstevel@tonic-gate 			bad_fail("scf_handle_bind", scf_error());
5670Sstevel@tonic-gate 		}
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 ||
5710Sstevel@tonic-gate 	    nvlist_add_int32(attr, RESTARTER_NAME_STATE, new_cur_state) != 0 ||
5720Sstevel@tonic-gate 	    nvlist_add_int32(attr, RESTARTER_NAME_NEXT_STATE, new_next_state)
5730Sstevel@tonic-gate 	    != 0 ||
5740Sstevel@tonic-gate 	    nvlist_add_int32(attr, RESTARTER_NAME_ERROR, e) != 0 ||
5750Sstevel@tonic-gate 	    nvlist_add_string(attr, RESTARTER_NAME_INSTANCE, inst) != 0) {
5760Sstevel@tonic-gate 		ret = ENOMEM;
5776045Srm88369 	} else {
5786045Srm88369 		id.i_fmri = inst;
5796045Srm88369 		id.i_state = cur_state;
5806045Srm88369 		id.i_next_state = next_state;
5816045Srm88369 
5826045Srm88369 		ret = _restarter_commit_states(scf_h, &id, new_cur_state,
5836045Srm88369 		    new_next_state, aux);
5846045Srm88369 
5856045Srm88369 		if (ret == 0) {
5866045Srm88369 			ret = restarter_event_publish_retry(
5876045Srm88369 			    h->reh_master_channel, "master", "state_change",
5886045Srm88369 			    "com.sun", "librestart", attr, EVCH_NOSLEEP);
5890Sstevel@tonic-gate 		}
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	nvlist_free(attr);
5930Sstevel@tonic-gate 	(void) scf_handle_unbind(scf_h);
5940Sstevel@tonic-gate 	scf_handle_destroy(scf_h);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	return (ret);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate restarter_instance_state_t
6000Sstevel@tonic-gate restarter_string_to_state(char *string)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	assert(string != NULL);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	if (strcmp(string, SCF_STATE_STRING_NONE) == 0)
6050Sstevel@tonic-gate 		return (RESTARTER_STATE_NONE);
6060Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_UNINIT) == 0)
6070Sstevel@tonic-gate 		return (RESTARTER_STATE_UNINIT);
6080Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_MAINT) == 0)
6090Sstevel@tonic-gate 		return (RESTARTER_STATE_MAINT);
6100Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_OFFLINE) == 0)
6110Sstevel@tonic-gate 		return (RESTARTER_STATE_OFFLINE);
6120Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_DISABLED) == 0)
6130Sstevel@tonic-gate 		return (RESTARTER_STATE_DISABLED);
6140Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_ONLINE) == 0)
6150Sstevel@tonic-gate 		return (RESTARTER_STATE_ONLINE);
6160Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_DEGRADED) == 0)
6170Sstevel@tonic-gate 		return (RESTARTER_STATE_DEGRADED);
6180Sstevel@tonic-gate 	else {
6190Sstevel@tonic-gate 		return (RESTARTER_STATE_NONE);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate ssize_t
6240Sstevel@tonic-gate restarter_state_to_string(restarter_instance_state_t state, char *string,
6250Sstevel@tonic-gate     size_t len)
6260Sstevel@tonic-gate {
6270Sstevel@tonic-gate 	assert(string != NULL);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if (state == RESTARTER_STATE_NONE)
6300Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_NONE, len));
6310Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_UNINIT)
6320Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_UNINIT, len));
6330Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_MAINT)
6340Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_MAINT, len));
6350Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_OFFLINE)
6360Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_OFFLINE,
6370Sstevel@tonic-gate 		    len));
6380Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_DISABLED)
6390Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_DISABLED,
6400Sstevel@tonic-gate 		    len));
6410Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_ONLINE)
6420Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_ONLINE, len));
6430Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_DEGRADED)
6440Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_DEGRADED,
6450Sstevel@tonic-gate 		    len));
6460Sstevel@tonic-gate 	else
6470Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, "unknown", len));
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /*
6510Sstevel@tonic-gate  * Sets pg to the name property group of s_inst.  If it doesn't exist, it is
6520Sstevel@tonic-gate  * added.
6530Sstevel@tonic-gate  *
6540Sstevel@tonic-gate  * Fails with
6550Sstevel@tonic-gate  *   ECONNABORTED - repository disconnection or unknown libscf error
6560Sstevel@tonic-gate  *   EBADF - inst is not set
6570Sstevel@tonic-gate  *   ECANCELED - inst is deleted
6580Sstevel@tonic-gate  *   EPERM - permission is denied
6590Sstevel@tonic-gate  *   EACCES - backend denied access
6600Sstevel@tonic-gate  *   EROFS - backend readonly
6610Sstevel@tonic-gate  */
6620Sstevel@tonic-gate static int
6630Sstevel@tonic-gate instance_get_or_add_pg(scf_instance_t *inst, const char *name,
6640Sstevel@tonic-gate     const char *type, uint32_t flags, scf_propertygroup_t *pg)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate again:
6670Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, name, pg) == 0)
6680Sstevel@tonic-gate 		return (0);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	switch (scf_error()) {
6710Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
6720Sstevel@tonic-gate 	default:
6730Sstevel@tonic-gate 		return (ECONNABORTED);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	case SCF_ERROR_NOT_SET:
6760Sstevel@tonic-gate 		return (EBADF);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
6790Sstevel@tonic-gate 		return (ECANCELED);
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
6820Sstevel@tonic-gate 		break;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	case SCF_ERROR_HANDLE_MISMATCH:
6850Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
6860Sstevel@tonic-gate 		bad_fail("scf_instance_get_pg", scf_error());
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	if (scf_instance_add_pg(inst, name, type, flags, pg) == 0)
6900Sstevel@tonic-gate 		return (0);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	switch (scf_error()) {
6930Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
6940Sstevel@tonic-gate 	default:
6950Sstevel@tonic-gate 		return (ECONNABORTED);
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
6980Sstevel@tonic-gate 		return (ECANCELED);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	case SCF_ERROR_EXISTS:
7010Sstevel@tonic-gate 		goto again;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	case SCF_ERROR_PERMISSION_DENIED:
7040Sstevel@tonic-gate 		return (EPERM);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_ACCESS:
7070Sstevel@tonic-gate 		return (EACCES);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_READONLY:
7100Sstevel@tonic-gate 		return (EROFS);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	case SCF_ERROR_HANDLE_MISMATCH:
7130Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
7140Sstevel@tonic-gate 	case SCF_ERROR_NOT_SET:			/* should be caught above */
7150Sstevel@tonic-gate 		bad_fail("scf_instance_add_pg", scf_error());
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	return (0);
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate /*
7220Sstevel@tonic-gate  * Fails with
7230Sstevel@tonic-gate  *   ECONNABORTED
7240Sstevel@tonic-gate  *   ECANCELED - pg was deleted
7250Sstevel@tonic-gate  */
7260Sstevel@tonic-gate static int
7270Sstevel@tonic-gate tx_set_value(scf_transaction_t *tx, scf_transaction_entry_t *ent,
7280Sstevel@tonic-gate     const char *pname, scf_type_t ty, scf_value_t *val)
7290Sstevel@tonic-gate {
7300Sstevel@tonic-gate 	int r;
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	for (;;) {
7330Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, ent, pname,
7340Sstevel@tonic-gate 		    ty) == 0)
7350Sstevel@tonic-gate 			break;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 		switch (scf_error()) {
7380Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7390Sstevel@tonic-gate 		default:
7400Sstevel@tonic-gate 			return (ECONNABORTED);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7430Sstevel@tonic-gate 			return (ECANCELED);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7460Sstevel@tonic-gate 			break;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7490Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7500Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
7510Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7520Sstevel@tonic-gate 			bad_fail("scf_transaction_property_change_type",
7530Sstevel@tonic-gate 			    scf_error());
7540Sstevel@tonic-gate 		}
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, ent, pname, ty) == 0)
7570Sstevel@tonic-gate 			break;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 		switch (scf_error()) {
7600Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7610Sstevel@tonic-gate 		default:
7620Sstevel@tonic-gate 			return (ECONNABORTED);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
7650Sstevel@tonic-gate 			return (ECANCELED);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
7680Sstevel@tonic-gate 			break;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7710Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7720Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
7730Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
7740Sstevel@tonic-gate 			bad_fail("scf_transaction_property_new", scf_error());
7750Sstevel@tonic-gate 		}
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	r = scf_entry_add_value(ent, val);
7790Sstevel@tonic-gate 	assert(r == 0);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	return (0);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate /*
7850Sstevel@tonic-gate  * Commit new_state, new_next_state, and aux to the repository for id.  If
7860Sstevel@tonic-gate  * successful, also set id's state and next-state as given, and return 0.
7870Sstevel@tonic-gate  * Fails with
7880Sstevel@tonic-gate  *   ENOMEM - out of memory
7890Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
7900Sstevel@tonic-gate  *		  - unknown libscf error
7910Sstevel@tonic-gate  *   EINVAL - id->i_fmri is invalid or not an instance FMRI
7920Sstevel@tonic-gate  *   ENOENT - id->i_fmri does not exist
7930Sstevel@tonic-gate  *   EPERM - insufficient permissions
7940Sstevel@tonic-gate  *   EACCES - backend access denied
7950Sstevel@tonic-gate  *   EROFS - backend is readonly
7960Sstevel@tonic-gate  */
7970Sstevel@tonic-gate int
7980Sstevel@tonic-gate _restarter_commit_states(scf_handle_t *h, instance_data_t *id,
7990Sstevel@tonic-gate     restarter_instance_state_t new_state,
8000Sstevel@tonic-gate     restarter_instance_state_t new_state_next, const char *aux)
8010Sstevel@tonic-gate {
8020Sstevel@tonic-gate 	char str_state[MAX_SCF_STATE_STRING_SZ];
8030Sstevel@tonic-gate 	char str_new_state[MAX_SCF_STATE_STRING_SZ];
8040Sstevel@tonic-gate 	char str_state_next[MAX_SCF_STATE_STRING_SZ];
8050Sstevel@tonic-gate 	char str_new_state_next[MAX_SCF_STATE_STRING_SZ];
8060Sstevel@tonic-gate 	int ret = 0, r;
8070Sstevel@tonic-gate 	struct timeval now;
8080Sstevel@tonic-gate 	ssize_t sz;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	scf_transaction_t *t = NULL;
8110Sstevel@tonic-gate 	scf_transaction_entry_t *t_state = NULL, *t_state_next = NULL;
8120Sstevel@tonic-gate 	scf_transaction_entry_t *t_stime = NULL, *t_aux = NULL;
8130Sstevel@tonic-gate 	scf_value_t *v_state = NULL, *v_state_next = NULL, *v_stime = NULL;
8140Sstevel@tonic-gate 	scf_value_t *v_aux = NULL;
8150Sstevel@tonic-gate 	scf_instance_t *s_inst = NULL;
8160Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	assert(new_state != RESTARTER_STATE_NONE);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	if ((s_inst = scf_instance_create(h)) == NULL ||
8210Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
8220Sstevel@tonic-gate 	    (t = scf_transaction_create(h)) == NULL ||
8230Sstevel@tonic-gate 	    (t_state = scf_entry_create(h)) == NULL ||
8240Sstevel@tonic-gate 	    (t_state_next = scf_entry_create(h)) == NULL ||
8250Sstevel@tonic-gate 	    (t_stime = scf_entry_create(h)) == NULL ||
8260Sstevel@tonic-gate 	    (t_aux = scf_entry_create(h)) == NULL ||
8270Sstevel@tonic-gate 	    (v_state = scf_value_create(h)) == NULL ||
8280Sstevel@tonic-gate 	    (v_state_next = scf_value_create(h)) == NULL ||
8290Sstevel@tonic-gate 	    (v_stime = scf_value_create(h)) == NULL ||
8300Sstevel@tonic-gate 	    (v_aux = scf_value_create(h)) == NULL) {
8310Sstevel@tonic-gate 		ret = ENOMEM;
8320Sstevel@tonic-gate 		goto out;
8330Sstevel@tonic-gate 	}
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	sz = restarter_state_to_string(new_state, str_new_state,
8360Sstevel@tonic-gate 	    sizeof (str_new_state));
8370Sstevel@tonic-gate 	assert(sz < sizeof (str_new_state));
8380Sstevel@tonic-gate 	sz = restarter_state_to_string(new_state_next, str_new_state_next,
8390Sstevel@tonic-gate 	    sizeof (str_new_state_next));
8400Sstevel@tonic-gate 	assert(sz < sizeof (str_new_state_next));
8410Sstevel@tonic-gate 	sz = restarter_state_to_string(id->i_state, str_state,
8420Sstevel@tonic-gate 	    sizeof (str_state));
8430Sstevel@tonic-gate 	assert(sz < sizeof (str_state));
8440Sstevel@tonic-gate 	sz = restarter_state_to_string(id->i_next_state, str_state_next,
8450Sstevel@tonic-gate 	    sizeof (str_state_next));
8460Sstevel@tonic-gate 	assert(sz < sizeof (str_state_next));
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	ret = gettimeofday(&now, NULL);
8490Sstevel@tonic-gate 	assert(ret != -1);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, id->i_fmri, NULL, NULL, s_inst,
8520Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1) {
8530Sstevel@tonic-gate 		switch (scf_error()) {
8540Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
8550Sstevel@tonic-gate 		default:
8560Sstevel@tonic-gate 			ret = ECONNABORTED;
8570Sstevel@tonic-gate 			break;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
8600Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
8610Sstevel@tonic-gate 			ret = EINVAL;
8620Sstevel@tonic-gate 			break;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
8650Sstevel@tonic-gate 			ret = ENOENT;
8660Sstevel@tonic-gate 			break;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
8690Sstevel@tonic-gate 			bad_fail("scf_handle_decode_fmri", scf_error());
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate 		goto out;
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	if (scf_value_set_astring(v_state, str_new_state) != 0 ||
8768823STruong.Q.Nguyen@Sun.COM 	    scf_value_set_astring(v_state_next, str_new_state_next) != 0)
8770Sstevel@tonic-gate 		bad_fail("scf_value_set_astring", scf_error());
8780Sstevel@tonic-gate 
8798823STruong.Q.Nguyen@Sun.COM 	if (aux) {
8808823STruong.Q.Nguyen@Sun.COM 		if (scf_value_set_astring(v_aux, aux) != 0)
8818823STruong.Q.Nguyen@Sun.COM 			bad_fail("scf_value_set_astring", scf_error());
8828823STruong.Q.Nguyen@Sun.COM 	}
8838823STruong.Q.Nguyen@Sun.COM 
8840Sstevel@tonic-gate 	if (scf_value_set_time(v_stime, now.tv_sec, now.tv_usec * 1000) != 0)
8850Sstevel@tonic-gate 		bad_fail("scf_value_set_time", scf_error());
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate add_pg:
8880Sstevel@tonic-gate 	switch (r = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
8890Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg)) {
8900Sstevel@tonic-gate 	case 0:
8910Sstevel@tonic-gate 		break;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	case ECONNABORTED:
8940Sstevel@tonic-gate 	case EPERM:
8950Sstevel@tonic-gate 	case EACCES:
8960Sstevel@tonic-gate 	case EROFS:
8970Sstevel@tonic-gate 		ret = r;
8980Sstevel@tonic-gate 		goto out;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	case ECANCELED:
9010Sstevel@tonic-gate 		ret = ENOENT;
9020Sstevel@tonic-gate 		goto out;
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	case EBADF:
9050Sstevel@tonic-gate 	default:
9060Sstevel@tonic-gate 		bad_fail("instance_get_or_add_pg", r);
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	for (;;) {
9100Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) != 0) {
9110Sstevel@tonic-gate 			switch (scf_error()) {
9120Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
9130Sstevel@tonic-gate 			default:
9140Sstevel@tonic-gate 				ret = ECONNABORTED;
9150Sstevel@tonic-gate 				goto out;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
9180Sstevel@tonic-gate 				goto add_pg;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
9210Sstevel@tonic-gate 				ret = EPERM;
9220Sstevel@tonic-gate 				goto out;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
9250Sstevel@tonic-gate 				ret = EACCES;
9260Sstevel@tonic-gate 				goto out;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
9290Sstevel@tonic-gate 				ret = EROFS;
9300Sstevel@tonic-gate 				goto out;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
9330Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
9340Sstevel@tonic-gate 				bad_fail("scf_transaction_start", scf_error());
9350Sstevel@tonic-gate 			}
9360Sstevel@tonic-gate 		}
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 		if ((r = tx_set_value(t, t_state, SCF_PROPERTY_STATE,
9390Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, v_state)) != 0 ||
9400Sstevel@tonic-gate 		    (r = tx_set_value(t, t_state_next, SCF_PROPERTY_NEXT_STATE,
9410Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, v_state_next)) != 0 ||
9420Sstevel@tonic-gate 		    (r = tx_set_value(t, t_stime, SCF_PROPERTY_STATE_TIMESTAMP,
9430Sstevel@tonic-gate 		    SCF_TYPE_TIME, v_stime)) != 0) {
9440Sstevel@tonic-gate 			switch (r) {
9450Sstevel@tonic-gate 			case ECONNABORTED:
9460Sstevel@tonic-gate 				ret = ECONNABORTED;
9470Sstevel@tonic-gate 				goto out;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 			case ECANCELED:
9500Sstevel@tonic-gate 				scf_transaction_reset(t);
9510Sstevel@tonic-gate 				goto add_pg;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 			default:
9540Sstevel@tonic-gate 				bad_fail("tx_set_value", r);
9550Sstevel@tonic-gate 			}
9560Sstevel@tonic-gate 		}
9570Sstevel@tonic-gate 
9588823STruong.Q.Nguyen@Sun.COM 		if (aux) {
9598823STruong.Q.Nguyen@Sun.COM 			if ((r = tx_set_value(t, t_aux, SCF_PROPERTY_AUX_STATE,
9608823STruong.Q.Nguyen@Sun.COM 			    SCF_TYPE_ASTRING, v_aux)) != 0) {
9618823STruong.Q.Nguyen@Sun.COM 				switch (r) {
9628823STruong.Q.Nguyen@Sun.COM 				case ECONNABORTED:
9638823STruong.Q.Nguyen@Sun.COM 					ret = ECONNABORTED;
9648823STruong.Q.Nguyen@Sun.COM 					goto out;
9658823STruong.Q.Nguyen@Sun.COM 
9668823STruong.Q.Nguyen@Sun.COM 				case ECANCELED:
9678823STruong.Q.Nguyen@Sun.COM 					scf_transaction_reset(t);
9688823STruong.Q.Nguyen@Sun.COM 					goto add_pg;
9698823STruong.Q.Nguyen@Sun.COM 
9708823STruong.Q.Nguyen@Sun.COM 				default:
9718823STruong.Q.Nguyen@Sun.COM 					bad_fail("tx_set_value", r);
9728823STruong.Q.Nguyen@Sun.COM 				}
9738823STruong.Q.Nguyen@Sun.COM 			}
9748823STruong.Q.Nguyen@Sun.COM 		}
9758823STruong.Q.Nguyen@Sun.COM 
9760Sstevel@tonic-gate 		ret = scf_transaction_commit(t);
9770Sstevel@tonic-gate 		if (ret == 1)
9780Sstevel@tonic-gate 			break;
9790Sstevel@tonic-gate 		if (ret == -1) {
9800Sstevel@tonic-gate 			switch (scf_error()) {
9810Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
9820Sstevel@tonic-gate 			default:
9830Sstevel@tonic-gate 				ret = ECONNABORTED;
9840Sstevel@tonic-gate 				goto out;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
9870Sstevel@tonic-gate 				ret = EPERM;
9880Sstevel@tonic-gate 				goto out;
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
9910Sstevel@tonic-gate 				ret = EACCES;
9920Sstevel@tonic-gate 				goto out;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
9950Sstevel@tonic-gate 				ret = EROFS;
9960Sstevel@tonic-gate 				goto out;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
9990Sstevel@tonic-gate 				bad_fail("scf_transaction_commit", scf_error());
10000Sstevel@tonic-gate 			}
10010Sstevel@tonic-gate 		}
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		scf_transaction_reset(t);
10040Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
10050Sstevel@tonic-gate 			switch (scf_error()) {
10060Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
10070Sstevel@tonic-gate 			default:
10080Sstevel@tonic-gate 				ret = ECONNABORTED;
10090Sstevel@tonic-gate 				goto out;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
10120Sstevel@tonic-gate 				goto add_pg;
10130Sstevel@tonic-gate 			}
10140Sstevel@tonic-gate 		}
10150Sstevel@tonic-gate 	}
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	id->i_state = new_state;
10180Sstevel@tonic-gate 	id->i_next_state = new_state_next;
10190Sstevel@tonic-gate 	ret = 0;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate out:
10220Sstevel@tonic-gate 	scf_transaction_destroy(t);
10230Sstevel@tonic-gate 	scf_entry_destroy(t_state);
10240Sstevel@tonic-gate 	scf_entry_destroy(t_state_next);
10250Sstevel@tonic-gate 	scf_entry_destroy(t_stime);
10260Sstevel@tonic-gate 	scf_entry_destroy(t_aux);
10270Sstevel@tonic-gate 	scf_value_destroy(v_state);
10280Sstevel@tonic-gate 	scf_value_destroy(v_state_next);
10290Sstevel@tonic-gate 	scf_value_destroy(v_stime);
10300Sstevel@tonic-gate 	scf_value_destroy(v_aux);
10310Sstevel@tonic-gate 	scf_pg_destroy(pg);
10320Sstevel@tonic-gate 	scf_instance_destroy(s_inst);
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	return (ret);
10350Sstevel@tonic-gate }
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate /*
10380Sstevel@tonic-gate  * Fails with
10390Sstevel@tonic-gate  *   EINVAL - type is invalid
10400Sstevel@tonic-gate  *   ENOMEM
10410Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
10420Sstevel@tonic-gate  *   EBADF - s_inst is not set
10430Sstevel@tonic-gate  *   ECANCELED - s_inst is deleted
10440Sstevel@tonic-gate  *   EPERM - permission denied
10450Sstevel@tonic-gate  *   EACCES - backend access denied
10460Sstevel@tonic-gate  *   EROFS - backend readonly
10470Sstevel@tonic-gate  */
10480Sstevel@tonic-gate int
10490Sstevel@tonic-gate restarter_remove_contract(scf_instance_t *s_inst, ctid_t contract_id,
10500Sstevel@tonic-gate     restarter_contract_type_t type)
10510Sstevel@tonic-gate {
10520Sstevel@tonic-gate 	scf_handle_t *h;
10530Sstevel@tonic-gate 	scf_transaction_t *t = NULL;
10540Sstevel@tonic-gate 	scf_transaction_entry_t *t_cid = NULL;
10550Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
10560Sstevel@tonic-gate 	scf_property_t *prop = NULL;
10570Sstevel@tonic-gate 	scf_value_t *val;
10580Sstevel@tonic-gate 	scf_iter_t *iter = NULL;
10590Sstevel@tonic-gate 	const char *pname;
10600Sstevel@tonic-gate 	int ret = 0, primary;
10610Sstevel@tonic-gate 	uint64_t c;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	switch (type) {
10640Sstevel@tonic-gate 	case RESTARTER_CONTRACT_PRIMARY:
10650Sstevel@tonic-gate 		primary = 1;
10660Sstevel@tonic-gate 		break;
10670Sstevel@tonic-gate 	case RESTARTER_CONTRACT_TRANSIENT:
10680Sstevel@tonic-gate 		primary = 0;
10690Sstevel@tonic-gate 		break;
10700Sstevel@tonic-gate 	default:
10710Sstevel@tonic-gate 		return (EINVAL);
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	h = scf_instance_handle(s_inst);
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	pg = scf_pg_create(h);
10770Sstevel@tonic-gate 	prop = scf_property_create(h);
10780Sstevel@tonic-gate 	iter = scf_iter_create(h);
10790Sstevel@tonic-gate 	t = scf_transaction_create(h);
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	if (pg == NULL || prop == NULL || iter == NULL || t == NULL) {
10820Sstevel@tonic-gate 		ret = ENOMEM;
10830Sstevel@tonic-gate 		goto remove_contract_cleanup;
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate add:
10870Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
10880Sstevel@tonic-gate 	ret = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
10890Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
10900Sstevel@tonic-gate 	if (ret != 0)
10910Sstevel@tonic-gate 		goto remove_contract_cleanup;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	pname = primary? SCF_PROPERTY_CONTRACT :
10940Sstevel@tonic-gate 	    SCF_PROPERTY_TRANSIENT_CONTRACT;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	for (;;) {
10970Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) != 0) {
10980Sstevel@tonic-gate 			switch (scf_error()) {
10990Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
11000Sstevel@tonic-gate 			default:
11010Sstevel@tonic-gate 				ret = ECONNABORTED;
11020Sstevel@tonic-gate 				goto remove_contract_cleanup;
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
11050Sstevel@tonic-gate 				goto add;
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
11080Sstevel@tonic-gate 				ret = EPERM;
11090Sstevel@tonic-gate 				goto remove_contract_cleanup;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
11120Sstevel@tonic-gate 				ret = EACCES;
11130Sstevel@tonic-gate 				goto remove_contract_cleanup;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
11160Sstevel@tonic-gate 				ret = EROFS;
11170Sstevel@tonic-gate 				goto remove_contract_cleanup;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
11200Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
11210Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
11220Sstevel@tonic-gate 				bad_fail("scf_transaction_start", scf_error());
11230Sstevel@tonic-gate 			}
11240Sstevel@tonic-gate 		}
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 		t_cid = scf_entry_create(h);
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 		if (scf_pg_get_property(pg, pname, prop) == 0) {
11290Sstevel@tonic-gate replace:
11300Sstevel@tonic-gate 			if (scf_transaction_property_change_type(t, t_cid,
11310Sstevel@tonic-gate 			    pname, SCF_TYPE_COUNT) != 0) {
11320Sstevel@tonic-gate 				switch (scf_error()) {
11330Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
11340Sstevel@tonic-gate 				default:
11350Sstevel@tonic-gate 					ret = ECONNABORTED;
11360Sstevel@tonic-gate 					goto remove_contract_cleanup;
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
11390Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
11400Sstevel@tonic-gate 					goto add;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
11430Sstevel@tonic-gate 					goto new;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
11460Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
11470Sstevel@tonic-gate 				case SCF_ERROR_IN_USE:
11480Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
11490Sstevel@tonic-gate 					bad_fail(
11500Sstevel@tonic-gate 					"scf_transaction_property_changetype",
11510Sstevel@tonic-gate 					    scf_error());
11520Sstevel@tonic-gate 				}
11530Sstevel@tonic-gate 			}
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) == 0) {
11560Sstevel@tonic-gate 				if (scf_iter_property_values(iter, prop) != 0) {
11570Sstevel@tonic-gate 					switch (scf_error()) {
11580Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
11590Sstevel@tonic-gate 					default:
11600Sstevel@tonic-gate 						ret = ECONNABORTED;
11610Sstevel@tonic-gate 						goto remove_contract_cleanup;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 					case SCF_ERROR_NOT_SET:
11640Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
11650Sstevel@tonic-gate 						bad_fail(
11660Sstevel@tonic-gate 						    "scf_iter_property_values",
11670Sstevel@tonic-gate 						    scf_error());
11680Sstevel@tonic-gate 					}
11690Sstevel@tonic-gate 				}
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate next_val:
11720Sstevel@tonic-gate 				val = scf_value_create(h);
11730Sstevel@tonic-gate 				if (val == NULL) {
11740Sstevel@tonic-gate 					assert(scf_error() ==
11750Sstevel@tonic-gate 					    SCF_ERROR_NO_MEMORY);
11760Sstevel@tonic-gate 					ret = ENOMEM;
11770Sstevel@tonic-gate 					goto remove_contract_cleanup;
11780Sstevel@tonic-gate 				}
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 				ret = scf_iter_next_value(iter, val);
11810Sstevel@tonic-gate 				if (ret == -1) {
11820Sstevel@tonic-gate 					switch (scf_error()) {
11830Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
11840Sstevel@tonic-gate 						ret = ECONNABORTED;
11850Sstevel@tonic-gate 						goto remove_contract_cleanup;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 					case SCF_ERROR_DELETED:
11880Sstevel@tonic-gate 						scf_value_destroy(val);
11890Sstevel@tonic-gate 						goto add;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
11920Sstevel@tonic-gate 					case SCF_ERROR_INVALID_ARGUMENT:
11935040Swesolows 					case SCF_ERROR_PERMISSION_DENIED:
11940Sstevel@tonic-gate 					default:
11950Sstevel@tonic-gate 						bad_fail("scf_iter_next_value",
11960Sstevel@tonic-gate 						    scf_error());
11970Sstevel@tonic-gate 					}
11980Sstevel@tonic-gate 				}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 				if (ret == 1) {
12010Sstevel@tonic-gate 					ret = scf_value_get_count(val, &c);
12020Sstevel@tonic-gate 					assert(ret == 0);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 					if (c != contract_id) {
12050Sstevel@tonic-gate 						ret = scf_entry_add_value(t_cid,
12060Sstevel@tonic-gate 						    val);
12070Sstevel@tonic-gate 						assert(ret == 0);
12080Sstevel@tonic-gate 					} else {
12090Sstevel@tonic-gate 						scf_value_destroy(val);
12100Sstevel@tonic-gate 					}
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 					goto next_val;
12130Sstevel@tonic-gate 				}
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 				scf_value_destroy(val);
12160Sstevel@tonic-gate 			} else {
12170Sstevel@tonic-gate 				switch (scf_error()) {
12180Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
12190Sstevel@tonic-gate 				default:
12200Sstevel@tonic-gate 					ret = ECONNABORTED;
12210Sstevel@tonic-gate 					goto remove_contract_cleanup;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 				case SCF_ERROR_TYPE_MISMATCH:
12240Sstevel@tonic-gate 					break;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
12270Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
12280Sstevel@tonic-gate 					bad_fail("scf_property_is_type",
12290Sstevel@tonic-gate 					    scf_error());
12300Sstevel@tonic-gate 				}
12310Sstevel@tonic-gate 			}
12320Sstevel@tonic-gate 		} else {
12330Sstevel@tonic-gate 			switch (scf_error()) {
12340Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
12350Sstevel@tonic-gate 			default:
12360Sstevel@tonic-gate 				ret = ECONNABORTED;
12370Sstevel@tonic-gate 				goto remove_contract_cleanup;
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
12400Sstevel@tonic-gate 				scf_entry_destroy(t_cid);
12410Sstevel@tonic-gate 				goto add;
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
12440Sstevel@tonic-gate 				break;
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
12470Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
12480Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
12490Sstevel@tonic-gate 				bad_fail("scf_pg_get_property", scf_error());
12500Sstevel@tonic-gate 			}
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate new:
12530Sstevel@tonic-gate 			if (scf_transaction_property_new(t, t_cid, pname,
12540Sstevel@tonic-gate 			    SCF_TYPE_COUNT) != 0) {
12550Sstevel@tonic-gate 				switch (scf_error()) {
12560Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
12570Sstevel@tonic-gate 				default:
12580Sstevel@tonic-gate 					ret = ECONNABORTED;
12590Sstevel@tonic-gate 					goto remove_contract_cleanup;
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
12620Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
12630Sstevel@tonic-gate 					goto add;
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 				case SCF_ERROR_EXISTS:
12660Sstevel@tonic-gate 					goto replace;
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
12690Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
12700Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
12710Sstevel@tonic-gate 					bad_fail("scf_transaction_property_new",
12720Sstevel@tonic-gate 					    scf_error());
12730Sstevel@tonic-gate 				}
12740Sstevel@tonic-gate 			}
12750Sstevel@tonic-gate 		}
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 		ret = scf_transaction_commit(t);
12780Sstevel@tonic-gate 		if (ret == -1) {
12790Sstevel@tonic-gate 			switch (scf_error()) {
12800Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
12810Sstevel@tonic-gate 			default:
12820Sstevel@tonic-gate 				ret = ECONNABORTED;
12830Sstevel@tonic-gate 				goto remove_contract_cleanup;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
12860Sstevel@tonic-gate 				goto add;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
12890Sstevel@tonic-gate 				ret = EPERM;
12900Sstevel@tonic-gate 				goto remove_contract_cleanup;
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
12930Sstevel@tonic-gate 				ret = EACCES;
12940Sstevel@tonic-gate 				goto remove_contract_cleanup;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
12970Sstevel@tonic-gate 				ret = EROFS;
12980Sstevel@tonic-gate 				goto remove_contract_cleanup;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
13010Sstevel@tonic-gate 				bad_fail("scf_transaction_commit", scf_error());
13020Sstevel@tonic-gate 			}
13030Sstevel@tonic-gate 		}
13040Sstevel@tonic-gate 		if (ret == 1) {
13050Sstevel@tonic-gate 			ret = 0;
13060Sstevel@tonic-gate 			break;
13070Sstevel@tonic-gate 		}
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 		scf_transaction_destroy_children(t);
13100Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
13110Sstevel@tonic-gate 			switch (scf_error()) {
13120Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
13130Sstevel@tonic-gate 			default:
13140Sstevel@tonic-gate 				ret = ECONNABORTED;
13150Sstevel@tonic-gate 				goto remove_contract_cleanup;
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
13180Sstevel@tonic-gate 				goto add;
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
13210Sstevel@tonic-gate 				bad_fail("scf_pg_update", scf_error());
13220Sstevel@tonic-gate 			}
13230Sstevel@tonic-gate 		}
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate remove_contract_cleanup:
13270Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
13280Sstevel@tonic-gate 	scf_transaction_destroy(t);
13290Sstevel@tonic-gate 	scf_iter_destroy(iter);
13300Sstevel@tonic-gate 	scf_property_destroy(prop);
13310Sstevel@tonic-gate 	scf_pg_destroy(pg);
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	return (ret);
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate /*
13370Sstevel@tonic-gate  * Fails with
13380Sstevel@tonic-gate  *   EINVAL - type is invalid
13390Sstevel@tonic-gate  *   ENOMEM
13400Sstevel@tonic-gate  *   ECONNABORTED - repository disconnection
13410Sstevel@tonic-gate  *   EBADF - s_inst is not set
13420Sstevel@tonic-gate  *   ECANCELED - s_inst is deleted
13430Sstevel@tonic-gate  *   EPERM
13440Sstevel@tonic-gate  *   EACCES
13450Sstevel@tonic-gate  *   EROFS
13460Sstevel@tonic-gate  */
13470Sstevel@tonic-gate int
13480Sstevel@tonic-gate restarter_store_contract(scf_instance_t *s_inst, ctid_t contract_id,
13490Sstevel@tonic-gate     restarter_contract_type_t type)
13500Sstevel@tonic-gate {
13510Sstevel@tonic-gate 	scf_handle_t *h;
13520Sstevel@tonic-gate 	scf_transaction_t *t = NULL;
13530Sstevel@tonic-gate 	scf_transaction_entry_t *t_cid = NULL;
13540Sstevel@tonic-gate 	scf_value_t *val;
13550Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
13560Sstevel@tonic-gate 	scf_property_t *prop = NULL;
13570Sstevel@tonic-gate 	scf_iter_t *iter = NULL;
13580Sstevel@tonic-gate 	const char *pname;
13590Sstevel@tonic-gate 	int ret = 0, primary;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	if (type == RESTARTER_CONTRACT_PRIMARY)
13620Sstevel@tonic-gate 		primary = 1;
13630Sstevel@tonic-gate 	else if (type == RESTARTER_CONTRACT_TRANSIENT)
13640Sstevel@tonic-gate 		primary = 0;
13650Sstevel@tonic-gate 	else
13660Sstevel@tonic-gate 		return (EINVAL);
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	h = scf_instance_handle(s_inst);
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	pg = scf_pg_create(h);
13710Sstevel@tonic-gate 	prop = scf_property_create(h);
13720Sstevel@tonic-gate 	iter = scf_iter_create(h);
13730Sstevel@tonic-gate 	t = scf_transaction_create(h);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	if (pg == NULL || prop == NULL || iter == NULL || t == NULL) {
13760Sstevel@tonic-gate 		ret = ENOMEM;
13770Sstevel@tonic-gate 		goto out;
13780Sstevel@tonic-gate 	}
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate add:
13810Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
13820Sstevel@tonic-gate 	ret = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
13830Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
13840Sstevel@tonic-gate 	if (ret != 0)
13850Sstevel@tonic-gate 		goto out;
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	pname = primary ? SCF_PROPERTY_CONTRACT :
13880Sstevel@tonic-gate 	    SCF_PROPERTY_TRANSIENT_CONTRACT;
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	for (;;) {
13910Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) != 0) {
13920Sstevel@tonic-gate 			switch (scf_error()) {
13930Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
13940Sstevel@tonic-gate 			default:
13950Sstevel@tonic-gate 				ret = ECONNABORTED;
13960Sstevel@tonic-gate 				goto out;
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
13990Sstevel@tonic-gate 				goto add;
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
14020Sstevel@tonic-gate 				ret = EPERM;
14030Sstevel@tonic-gate 				goto out;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
14060Sstevel@tonic-gate 				ret = EACCES;
14070Sstevel@tonic-gate 				goto out;
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
14100Sstevel@tonic-gate 				ret = EROFS;
14110Sstevel@tonic-gate 				goto out;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
14140Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
14150Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
14160Sstevel@tonic-gate 				bad_fail("scf_transaction_start", scf_error());
14170Sstevel@tonic-gate 			}
14180Sstevel@tonic-gate 		}
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 		t_cid = scf_entry_create(h);
14210Sstevel@tonic-gate 		if (t_cid == NULL) {
14220Sstevel@tonic-gate 			ret = ENOMEM;
14230Sstevel@tonic-gate 			goto out;
14240Sstevel@tonic-gate 		}
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 		if (scf_pg_get_property(pg, pname, prop) == 0) {
14270Sstevel@tonic-gate replace:
14280Sstevel@tonic-gate 			if (scf_transaction_property_change_type(t, t_cid,
14290Sstevel@tonic-gate 			    pname, SCF_TYPE_COUNT) != 0) {
14300Sstevel@tonic-gate 				switch (scf_error()) {
14310Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
14320Sstevel@tonic-gate 				default:
14330Sstevel@tonic-gate 					ret = ECONNABORTED;
14340Sstevel@tonic-gate 					goto out;
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
14370Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
14380Sstevel@tonic-gate 					goto add;
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
14410Sstevel@tonic-gate 					goto new;
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
14440Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
14450Sstevel@tonic-gate 				case SCF_ERROR_IN_USE:
14460Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
14470Sstevel@tonic-gate 					bad_fail(
14480Sstevel@tonic-gate 					"scf_transaction_propert_change_type",
14490Sstevel@tonic-gate 					    scf_error());
14500Sstevel@tonic-gate 				}
14510Sstevel@tonic-gate 			}
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) == 0) {
14540Sstevel@tonic-gate 				if (scf_iter_property_values(iter, prop) != 0) {
14550Sstevel@tonic-gate 					switch (scf_error()) {
14560Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
14570Sstevel@tonic-gate 					default:
14580Sstevel@tonic-gate 						ret = ECONNABORTED;
14590Sstevel@tonic-gate 						goto out;
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 					case SCF_ERROR_NOT_SET:
14620Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
14630Sstevel@tonic-gate 						bad_fail(
14640Sstevel@tonic-gate 						    "scf_iter_property_values",
14650Sstevel@tonic-gate 						    scf_error());
14660Sstevel@tonic-gate 					}
14670Sstevel@tonic-gate 				}
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate next_val:
14700Sstevel@tonic-gate 				val = scf_value_create(h);
14710Sstevel@tonic-gate 				if (val == NULL) {
14720Sstevel@tonic-gate 					assert(scf_error() ==
14730Sstevel@tonic-gate 					    SCF_ERROR_NO_MEMORY);
14740Sstevel@tonic-gate 					ret = ENOMEM;
14750Sstevel@tonic-gate 					goto out;
14760Sstevel@tonic-gate 				}
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 				ret = scf_iter_next_value(iter, val);
14790Sstevel@tonic-gate 				if (ret == -1) {
14800Sstevel@tonic-gate 					switch (scf_error()) {
14810Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
14820Sstevel@tonic-gate 					default:
14830Sstevel@tonic-gate 						ret = ECONNABORTED;
14840Sstevel@tonic-gate 						goto out;
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 					case SCF_ERROR_DELETED:
14870Sstevel@tonic-gate 						scf_value_destroy(val);
14880Sstevel@tonic-gate 						goto add;
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
14910Sstevel@tonic-gate 					case SCF_ERROR_INVALID_ARGUMENT:
14925040Swesolows 					case SCF_ERROR_PERMISSION_DENIED:
14930Sstevel@tonic-gate 						bad_fail(
14940Sstevel@tonic-gate 						    "scf_iter_next_value",
14950Sstevel@tonic-gate 						    scf_error());
14960Sstevel@tonic-gate 					}
14970Sstevel@tonic-gate 				}
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 				if (ret == 1) {
15000Sstevel@tonic-gate 					ret = scf_entry_add_value(t_cid, val);
15010Sstevel@tonic-gate 					assert(ret == 0);
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 					goto next_val;
15040Sstevel@tonic-gate 				}
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 				scf_value_destroy(val);
15070Sstevel@tonic-gate 			} else {
15080Sstevel@tonic-gate 				switch (scf_error()) {
15090Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
15100Sstevel@tonic-gate 				default:
15110Sstevel@tonic-gate 					ret = ECONNABORTED;
15120Sstevel@tonic-gate 					goto out;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 				case SCF_ERROR_TYPE_MISMATCH:
15150Sstevel@tonic-gate 					break;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
15180Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
15190Sstevel@tonic-gate 					bad_fail("scf_property_is_type",
15200Sstevel@tonic-gate 					    scf_error());
15210Sstevel@tonic-gate 				}
15220Sstevel@tonic-gate 			}
15230Sstevel@tonic-gate 		} else {
15240Sstevel@tonic-gate 			switch (scf_error()) {
15250Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
15260Sstevel@tonic-gate 			default:
15270Sstevel@tonic-gate 				ret = ECONNABORTED;
15280Sstevel@tonic-gate 				goto out;
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
15310Sstevel@tonic-gate 				scf_entry_destroy(t_cid);
15320Sstevel@tonic-gate 				goto add;
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
15350Sstevel@tonic-gate 				break;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
15380Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
15390Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
15400Sstevel@tonic-gate 				bad_fail("scf_pg_get_property", scf_error());
15410Sstevel@tonic-gate 			}
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate new:
15440Sstevel@tonic-gate 			if (scf_transaction_property_new(t, t_cid, pname,
15450Sstevel@tonic-gate 			    SCF_TYPE_COUNT) != 0) {
15460Sstevel@tonic-gate 				switch (scf_error()) {
15470Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
15480Sstevel@tonic-gate 				default:
15490Sstevel@tonic-gate 					ret = ECONNABORTED;
15500Sstevel@tonic-gate 					goto out;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
15530Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
15540Sstevel@tonic-gate 					goto add;
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 				case SCF_ERROR_EXISTS:
15570Sstevel@tonic-gate 					goto replace;
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
15600Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
15610Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
15620Sstevel@tonic-gate 					bad_fail("scf_transaction_property_new",
15630Sstevel@tonic-gate 					    scf_error());
15640Sstevel@tonic-gate 				}
15650Sstevel@tonic-gate 			}
15660Sstevel@tonic-gate 		}
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 		val = scf_value_create(h);
15690Sstevel@tonic-gate 		if (val == NULL) {
15700Sstevel@tonic-gate 			assert(scf_error() == SCF_ERROR_NO_MEMORY);
15710Sstevel@tonic-gate 			ret = ENOMEM;
15720Sstevel@tonic-gate 			goto out;
15730Sstevel@tonic-gate 		}
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 		scf_value_set_count(val, contract_id);
15760Sstevel@tonic-gate 		ret = scf_entry_add_value(t_cid, val);
15770Sstevel@tonic-gate 		assert(ret == 0);
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 		ret = scf_transaction_commit(t);
15800Sstevel@tonic-gate 		if (ret == -1) {
15810Sstevel@tonic-gate 			switch (scf_error()) {
15820Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
15830Sstevel@tonic-gate 			default:
15840Sstevel@tonic-gate 				ret = ECONNABORTED;
15850Sstevel@tonic-gate 				goto out;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
15880Sstevel@tonic-gate 				goto add;
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
15910Sstevel@tonic-gate 				ret = EPERM;
15920Sstevel@tonic-gate 				goto out;
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
15950Sstevel@tonic-gate 				ret = EACCES;
15960Sstevel@tonic-gate 				goto out;
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
15990Sstevel@tonic-gate 				ret = EROFS;
16000Sstevel@tonic-gate 				goto out;
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
16030Sstevel@tonic-gate 				bad_fail("scf_transaction_commit", scf_error());
16040Sstevel@tonic-gate 			}
16050Sstevel@tonic-gate 		}
16060Sstevel@tonic-gate 		if (ret == 1) {
16070Sstevel@tonic-gate 			ret = 0;
16080Sstevel@tonic-gate 			break;
16090Sstevel@tonic-gate 		}
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 		scf_transaction_destroy_children(t);
16120Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
16130Sstevel@tonic-gate 			switch (scf_error()) {
16140Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
16150Sstevel@tonic-gate 			default:
16160Sstevel@tonic-gate 				ret = ECONNABORTED;
16170Sstevel@tonic-gate 				goto out;
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
16200Sstevel@tonic-gate 				goto add;
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
16230Sstevel@tonic-gate 				bad_fail("scf_pg_update", scf_error());
16240Sstevel@tonic-gate 			}
16250Sstevel@tonic-gate 		}
16260Sstevel@tonic-gate 	}
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate out:
16290Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
16300Sstevel@tonic-gate 	scf_transaction_destroy(t);
16310Sstevel@tonic-gate 	scf_iter_destroy(iter);
16320Sstevel@tonic-gate 	scf_property_destroy(prop);
16330Sstevel@tonic-gate 	scf_pg_destroy(pg);
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	return (ret);
16360Sstevel@tonic-gate }
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate int
16390Sstevel@tonic-gate restarter_rm_libs_loadable()
16400Sstevel@tonic-gate {
16410Sstevel@tonic-gate 	void *libhndl;
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	if (method_context_safety)
16440Sstevel@tonic-gate 		return (1);
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	if ((libhndl = dlopen("libpool.so", RTLD_LAZY | RTLD_LOCAL)) == NULL)
16470Sstevel@tonic-gate 		return (0);
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	(void) dlclose(libhndl);
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	if ((libhndl = dlopen("libproject.so", RTLD_LAZY | RTLD_LOCAL)) == NULL)
16520Sstevel@tonic-gate 		return (0);
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	(void) dlclose(libhndl);
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	method_context_safety = 1;
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	return (1);
16590Sstevel@tonic-gate }
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate static int
16620Sstevel@tonic-gate get_astring_val(scf_propertygroup_t *pg, const char *name, char *buf,
16630Sstevel@tonic-gate     size_t bufsz, scf_property_t *prop, scf_value_t *val)
16640Sstevel@tonic-gate {
16650Sstevel@tonic-gate 	ssize_t szret;
16660Sstevel@tonic-gate 
16679263SSean.Wilcox@Sun.COM 	if (pg == NULL)
16689263SSean.Wilcox@Sun.COM 		return (-1);
16699263SSean.Wilcox@Sun.COM 
16700Sstevel@tonic-gate 	if (scf_pg_get_property(pg, name, prop) != SCF_SUCCESS) {
16710Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
16720Sstevel@tonic-gate 			uu_die(rcbroken);
16730Sstevel@tonic-gate 		return (-1);
16740Sstevel@tonic-gate 	}
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
16770Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
16780Sstevel@tonic-gate 			uu_die(rcbroken);
16790Sstevel@tonic-gate 		return (-1);
16800Sstevel@tonic-gate 	}
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 	szret = scf_value_get_astring(val, buf, bufsz);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	return (szret >= 0 ? 0 : -1);
16850Sstevel@tonic-gate }
16860Sstevel@tonic-gate 
16878823STruong.Q.Nguyen@Sun.COM static int
16888823STruong.Q.Nguyen@Sun.COM get_boolean_val(scf_propertygroup_t *pg, const char *name, uint8_t *b,
16898823STruong.Q.Nguyen@Sun.COM     scf_property_t *prop, scf_value_t *val)
16908823STruong.Q.Nguyen@Sun.COM {
16918823STruong.Q.Nguyen@Sun.COM 	if (scf_pg_get_property(pg, name, prop) != SCF_SUCCESS) {
16928823STruong.Q.Nguyen@Sun.COM 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
16938823STruong.Q.Nguyen@Sun.COM 			uu_die(rcbroken);
16948823STruong.Q.Nguyen@Sun.COM 		return (-1);
16958823STruong.Q.Nguyen@Sun.COM 	}
16968823STruong.Q.Nguyen@Sun.COM 
16978823STruong.Q.Nguyen@Sun.COM 	if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
16988823STruong.Q.Nguyen@Sun.COM 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
16998823STruong.Q.Nguyen@Sun.COM 			uu_die(rcbroken);
17008823STruong.Q.Nguyen@Sun.COM 		return (-1);
17018823STruong.Q.Nguyen@Sun.COM 	}
17028823STruong.Q.Nguyen@Sun.COM 
17038823STruong.Q.Nguyen@Sun.COM 	if (scf_value_get_boolean(val, b))
17048823STruong.Q.Nguyen@Sun.COM 		return (-1);
17058823STruong.Q.Nguyen@Sun.COM 
17068823STruong.Q.Nguyen@Sun.COM 	return (0);
17078823STruong.Q.Nguyen@Sun.COM }
17088823STruong.Q.Nguyen@Sun.COM 
17090Sstevel@tonic-gate /*
17100Sstevel@tonic-gate  * Try to load mcp->pwd, if it isn't already.
17110Sstevel@tonic-gate  * Fails with
17120Sstevel@tonic-gate  *   ENOMEM - malloc() failed
17130Sstevel@tonic-gate  *   ENOENT - no entry found
17140Sstevel@tonic-gate  *   EIO - I/O error
17150Sstevel@tonic-gate  *   EMFILE - process out of file descriptors
17160Sstevel@tonic-gate  *   ENFILE - system out of file handles
17170Sstevel@tonic-gate  */
17180Sstevel@tonic-gate static int
17190Sstevel@tonic-gate lookup_pwd(struct method_context *mcp)
17200Sstevel@tonic-gate {
17210Sstevel@tonic-gate 	struct passwd *pwdp;
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	if (mcp->pwbuf != NULL && mcp->pwd.pw_uid == mcp->uid)
17240Sstevel@tonic-gate 		return (0);
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	if (mcp->pwbuf == NULL) {
17270Sstevel@tonic-gate 		mcp->pwbufsz = sysconf(_SC_GETPW_R_SIZE_MAX);
17280Sstevel@tonic-gate 		assert(mcp->pwbufsz >= 0);
17290Sstevel@tonic-gate 		mcp->pwbuf = malloc(mcp->pwbufsz);
17300Sstevel@tonic-gate 		if (mcp->pwbuf == NULL)
17310Sstevel@tonic-gate 			return (ENOMEM);
17320Sstevel@tonic-gate 	}
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 	do {
17350Sstevel@tonic-gate 		errno = 0;
17360Sstevel@tonic-gate 		pwdp = getpwuid_r(mcp->uid, &mcp->pwd, mcp->pwbuf,
17370Sstevel@tonic-gate 		    mcp->pwbufsz);
17380Sstevel@tonic-gate 	} while (pwdp == NULL && errno == EINTR);
17390Sstevel@tonic-gate 	if (pwdp != NULL)
17400Sstevel@tonic-gate 		return (0);
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 	free(mcp->pwbuf);
17430Sstevel@tonic-gate 	mcp->pwbuf = NULL;
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	switch (errno) {
17460Sstevel@tonic-gate 	case 0:
17470Sstevel@tonic-gate 	default:
17480Sstevel@tonic-gate 		/*
17490Sstevel@tonic-gate 		 * Until bug 5065780 is fixed, getpwuid_r() can fail with
17500Sstevel@tonic-gate 		 * ENOENT, particularly on the miniroot.  Since the
17510Sstevel@tonic-gate 		 * documentation is inaccurate, we'll return ENOENT for unknown
17520Sstevel@tonic-gate 		 * errors.
17530Sstevel@tonic-gate 		 */
17540Sstevel@tonic-gate 		return (ENOENT);
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	case EIO:
17570Sstevel@tonic-gate 	case EMFILE:
17580Sstevel@tonic-gate 	case ENFILE:
17590Sstevel@tonic-gate 		return (errno);
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 	case ERANGE:
17620Sstevel@tonic-gate 		bad_fail("getpwuid_r", errno);
17630Sstevel@tonic-gate 		/* NOTREACHED */
17640Sstevel@tonic-gate 	}
17650Sstevel@tonic-gate }
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate /*
17680Sstevel@tonic-gate  * Get the user id for str.  Returns 0 on success or
17690Sstevel@tonic-gate  *   ERANGE	the uid is too big
17700Sstevel@tonic-gate  *   EINVAL	the string starts with a digit, but is not a valid uid
17710Sstevel@tonic-gate  *   ENOMEM	out of memory
17720Sstevel@tonic-gate  *   ENOENT	no passwd entry for str
17730Sstevel@tonic-gate  *   EIO	an I/O error has occurred
17740Sstevel@tonic-gate  *   EMFILE/ENFILE  out of file descriptors
17750Sstevel@tonic-gate  */
17760Sstevel@tonic-gate int
17770Sstevel@tonic-gate get_uid(const char *str, struct method_context *ci, uid_t *uidp)
17780Sstevel@tonic-gate {
17790Sstevel@tonic-gate 	if (isdigit(str[0])) {
17800Sstevel@tonic-gate 		uid_t uid;
17810Sstevel@tonic-gate 		char *cp;
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 		errno = 0;
17840Sstevel@tonic-gate 		uid = strtol(str, &cp, 10);
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 		if (uid == 0 && errno != 0) {
17870Sstevel@tonic-gate 			assert(errno != EINVAL);
17880Sstevel@tonic-gate 			return (errno);
17890Sstevel@tonic-gate 		}
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 		for (; *cp != '\0'; ++cp)
17920Sstevel@tonic-gate 			if (*cp != ' ' || *cp != '\t')
17930Sstevel@tonic-gate 				return (EINVAL);
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 		if (uid > UID_MAX)
17960Sstevel@tonic-gate 			return (EINVAL);
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		*uidp = uid;
17990Sstevel@tonic-gate 		return (0);
18000Sstevel@tonic-gate 	} else {
18010Sstevel@tonic-gate 		struct passwd *pwdp;
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 		if (ci->pwbuf == NULL) {
18040Sstevel@tonic-gate 			ci->pwbufsz = sysconf(_SC_GETPW_R_SIZE_MAX);
18050Sstevel@tonic-gate 			ci->pwbuf = malloc(ci->pwbufsz);
18060Sstevel@tonic-gate 			if (ci->pwbuf == NULL)
18070Sstevel@tonic-gate 				return (ENOMEM);
18080Sstevel@tonic-gate 		}
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 		do {
18110Sstevel@tonic-gate 			errno = 0;
18120Sstevel@tonic-gate 			pwdp =
18130Sstevel@tonic-gate 			    getpwnam_r(str, &ci->pwd, ci->pwbuf, ci->pwbufsz);
18140Sstevel@tonic-gate 		} while (pwdp == NULL && errno == EINTR);
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 		if (pwdp != NULL) {
18170Sstevel@tonic-gate 			*uidp = ci->pwd.pw_uid;
18180Sstevel@tonic-gate 			return (0);
18190Sstevel@tonic-gate 		} else {
18200Sstevel@tonic-gate 			free(ci->pwbuf);
18210Sstevel@tonic-gate 			ci->pwbuf = NULL;
18220Sstevel@tonic-gate 			switch (errno) {
18230Sstevel@tonic-gate 			case 0:
18240Sstevel@tonic-gate 				return (ENOENT);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 			case ENOENT:
18270Sstevel@tonic-gate 			case EIO:
18280Sstevel@tonic-gate 			case EMFILE:
18290Sstevel@tonic-gate 			case ENFILE:
18300Sstevel@tonic-gate 				return (errno);
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 			case ERANGE:
18330Sstevel@tonic-gate 			default:
18340Sstevel@tonic-gate 				bad_fail("getpwnam_r", errno);
18350Sstevel@tonic-gate 				/* NOTREACHED */
18360Sstevel@tonic-gate 			}
18370Sstevel@tonic-gate 		}
18380Sstevel@tonic-gate 	}
18390Sstevel@tonic-gate }
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate gid_t
18420Sstevel@tonic-gate get_gid(const char *str)
18430Sstevel@tonic-gate {
18440Sstevel@tonic-gate 	if (isdigit(str[0])) {
18450Sstevel@tonic-gate 		gid_t gid;
18460Sstevel@tonic-gate 		char *cp;
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 		errno = 0;
18490Sstevel@tonic-gate 		gid = strtol(str, &cp, 10);
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 		if (gid == 0 && errno != 0)
18524321Scasper 			return ((gid_t)-1);
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 		for (; *cp != '\0'; ++cp)
18550Sstevel@tonic-gate 			if (*cp != ' ' || *cp != '\t')
18564321Scasper 				return ((gid_t)-1);
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 		return (gid);
18590Sstevel@tonic-gate 	} else {
18600Sstevel@tonic-gate 		struct group grp, *ret;
18610Sstevel@tonic-gate 		char *buffer;
18620Sstevel@tonic-gate 		size_t buflen;
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 		buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
18650Sstevel@tonic-gate 		buffer = malloc(buflen);
18660Sstevel@tonic-gate 		if (buffer == NULL)
18670Sstevel@tonic-gate 			uu_die(allocfail);
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 		errno = 0;
18700Sstevel@tonic-gate 		ret = getgrnam_r(str, &grp, buffer, buflen);
18710Sstevel@tonic-gate 		free(buffer);
18720Sstevel@tonic-gate 
18734321Scasper 		return (ret == NULL ? (gid_t)-1 : grp.gr_gid);
18740Sstevel@tonic-gate 	}
18750Sstevel@tonic-gate }
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate /*
18780Sstevel@tonic-gate  * Fails with
18790Sstevel@tonic-gate  *   ENOMEM - out of memory
18800Sstevel@tonic-gate  *   ENOENT - no passwd entry
18810Sstevel@tonic-gate  *	      no project entry
18820Sstevel@tonic-gate  *   EIO - an I/O error occurred
18830Sstevel@tonic-gate  *   EMFILE - the process is out of file descriptors
18840Sstevel@tonic-gate  *   ENFILE - the system is out of file handles
18850Sstevel@tonic-gate  *   ERANGE - the project id is out of range
18860Sstevel@tonic-gate  *   EINVAL - str is invalid
18870Sstevel@tonic-gate  *   E2BIG - the project entry was too big
18880Sstevel@tonic-gate  *   -1 - the name service switch is misconfigured
18890Sstevel@tonic-gate  */
18900Sstevel@tonic-gate int
18910Sstevel@tonic-gate get_projid(const char *str, struct method_context *cip)
18920Sstevel@tonic-gate {
18930Sstevel@tonic-gate 	int ret;
18940Sstevel@tonic-gate 	void *buf;
18950Sstevel@tonic-gate 	const size_t bufsz = PROJECT_BUFSZ;
18960Sstevel@tonic-gate 	struct project proj, *pp;
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	if (strcmp(str, ":default") == 0) {
18990Sstevel@tonic-gate 		if (cip->uid == 0) {
19000Sstevel@tonic-gate 			/* Don't change project for root services */
19010Sstevel@tonic-gate 			cip->project = NULL;
19020Sstevel@tonic-gate 			return (0);
19030Sstevel@tonic-gate 		}
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 		switch (ret = lookup_pwd(cip)) {
19060Sstevel@tonic-gate 		case 0:
19070Sstevel@tonic-gate 			break;
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 		case ENOMEM:
19100Sstevel@tonic-gate 		case ENOENT:
19110Sstevel@tonic-gate 		case EIO:
19120Sstevel@tonic-gate 		case EMFILE:
19130Sstevel@tonic-gate 		case ENFILE:
19140Sstevel@tonic-gate 			return (ret);
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 		default:
19170Sstevel@tonic-gate 			bad_fail("lookup_pwd", ret);
19180Sstevel@tonic-gate 		}
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 		buf = malloc(bufsz);
19210Sstevel@tonic-gate 		if (buf == NULL)
19220Sstevel@tonic-gate 			return (ENOMEM);
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 		do {
19250Sstevel@tonic-gate 			errno = 0;
19260Sstevel@tonic-gate 			pp = getdefaultproj(cip->pwd.pw_name, &proj, buf,
19270Sstevel@tonic-gate 			    bufsz);
19280Sstevel@tonic-gate 		} while (pp == NULL && errno == EINTR);
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 		/* to be continued ... */
19310Sstevel@tonic-gate 	} else {
19320Sstevel@tonic-gate 		projid_t projid;
19330Sstevel@tonic-gate 		char *cp;
19340Sstevel@tonic-gate 
19350Sstevel@tonic-gate 		if (!isdigit(str[0])) {
19360Sstevel@tonic-gate 			cip->project = strdup(str);
19370Sstevel@tonic-gate 			return (cip->project != NULL ? 0 : ENOMEM);
19380Sstevel@tonic-gate 		}
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate 		errno = 0;
19410Sstevel@tonic-gate 		projid = strtol(str, &cp, 10);
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 		if (projid == 0 && errno != 0) {
19440Sstevel@tonic-gate 			assert(errno == ERANGE);
19450Sstevel@tonic-gate 			return (errno);
19460Sstevel@tonic-gate 		}
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 		for (; *cp != '\0'; ++cp)
19490Sstevel@tonic-gate 			if (*cp != ' ' || *cp != '\t')
19500Sstevel@tonic-gate 				return (EINVAL);
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 		if (projid > MAXPROJID)
19530Sstevel@tonic-gate 			return (ERANGE);
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 		buf = malloc(bufsz);
19560Sstevel@tonic-gate 		if (buf == NULL)
19570Sstevel@tonic-gate 			return (ENOMEM);
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 		do {
19600Sstevel@tonic-gate 			errno = 0;
19610Sstevel@tonic-gate 			pp = getprojbyid(projid, &proj, buf, bufsz);
19620Sstevel@tonic-gate 		} while (pp == NULL && errno == EINTR);
19630Sstevel@tonic-gate 	}
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	if (pp) {
19660Sstevel@tonic-gate 		cip->project = strdup(pp->pj_name);
19670Sstevel@tonic-gate 		free(buf);
19680Sstevel@tonic-gate 		return (cip->project != NULL ? 0 : ENOMEM);
19690Sstevel@tonic-gate 	}
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 	free(buf);
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	switch (errno) {
19740Sstevel@tonic-gate 	case 0:
19750Sstevel@tonic-gate 		return (ENOENT);
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 	case EIO:
19780Sstevel@tonic-gate 	case EMFILE:
19790Sstevel@tonic-gate 	case ENFILE:
19800Sstevel@tonic-gate 		return (errno);
19810Sstevel@tonic-gate 
19820Sstevel@tonic-gate 	case ERANGE:
19830Sstevel@tonic-gate 		return (E2BIG);
19840Sstevel@tonic-gate 
19850Sstevel@tonic-gate 	default:
19860Sstevel@tonic-gate 		return (-1);
19870Sstevel@tonic-gate 	}
19880Sstevel@tonic-gate }
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate /*
19910Sstevel@tonic-gate  * Parse the supp_groups property value and populate ci->groups.  Returns
19920Sstevel@tonic-gate  * EINVAL (get_gid() failed for one of the components), E2BIG (the property has
19930Sstevel@tonic-gate  * more than NGROUPS_MAX-1 groups), or 0 on success.
19940Sstevel@tonic-gate  */
19950Sstevel@tonic-gate int
19960Sstevel@tonic-gate get_groups(char *str, struct method_context *ci)
19970Sstevel@tonic-gate {
19980Sstevel@tonic-gate 	char *cp, *end, *next;
19990Sstevel@tonic-gate 	uint_t i;
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 	const char * const whitespace = " \t";
20020Sstevel@tonic-gate 	const char * const illegal = ", \t";
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	if (str[0] == '\0') {
20050Sstevel@tonic-gate 		ci->ngroups = 0;
20060Sstevel@tonic-gate 		return (0);
20070Sstevel@tonic-gate 	}
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	for (cp = str, i = 0; *cp != '\0'; ) {
20100Sstevel@tonic-gate 		/* skip whitespace */
20110Sstevel@tonic-gate 		cp += strspn(cp, whitespace);
20120Sstevel@tonic-gate 
20130Sstevel@tonic-gate 		/* find the end */
20140Sstevel@tonic-gate 		end = cp + strcspn(cp, illegal);
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 		/* skip whitespace after end */
20170Sstevel@tonic-gate 		next = end + strspn(end, whitespace);
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 		/* if there's a comma, it separates the fields */
20200Sstevel@tonic-gate 		if (*next == ',')
20210Sstevel@tonic-gate 			++next;
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 		*end = '\0';
20240Sstevel@tonic-gate 
20254321Scasper 		if ((ci->groups[i] = get_gid(cp)) == (gid_t)-1) {
20260Sstevel@tonic-gate 			ci->ngroups = 0;
20270Sstevel@tonic-gate 			return (EINVAL);
20280Sstevel@tonic-gate 		}
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 		++i;
20310Sstevel@tonic-gate 		if (i > NGROUPS_MAX - 1) {
20320Sstevel@tonic-gate 			ci->ngroups = 0;
20330Sstevel@tonic-gate 			return (E2BIG);
20340Sstevel@tonic-gate 		}
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 		cp = next;
20370Sstevel@tonic-gate 	}
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	ci->ngroups = i;
20400Sstevel@tonic-gate 	return (0);
20410Sstevel@tonic-gate }
20420Sstevel@tonic-gate 
2043*9765SSean.Wilcox@Sun.COM 
20440Sstevel@tonic-gate /*
2045*9765SSean.Wilcox@Sun.COM  * Return an error message structure containing the error message
2046*9765SSean.Wilcox@Sun.COM  * with context, and the error so the caller can make a decision
2047*9765SSean.Wilcox@Sun.COM  * on what to do next.
2048*9765SSean.Wilcox@Sun.COM  *
2049*9765SSean.Wilcox@Sun.COM  * Because get_ids uses the mc_error_create() function which can
2050*9765SSean.Wilcox@Sun.COM  * reallocate the merr, this function must return the merr pointer
2051*9765SSean.Wilcox@Sun.COM  * in case it was reallocated.
20520Sstevel@tonic-gate  */
2053*9765SSean.Wilcox@Sun.COM static mc_error_t *
20549263SSean.Wilcox@Sun.COM get_profile(scf_propertygroup_t *methpg, scf_propertygroup_t *instpg,
20559263SSean.Wilcox@Sun.COM     scf_property_t *prop, scf_value_t *val, const char *cmdline,
2056*9765SSean.Wilcox@Sun.COM     struct method_context *ci, mc_error_t *merr)
20570Sstevel@tonic-gate {
20580Sstevel@tonic-gate 	char *buf = ci->vbuf;
20590Sstevel@tonic-gate 	ssize_t buf_sz = ci->vbuf_sz;
20600Sstevel@tonic-gate 	char cmd[PATH_MAX];
20610Sstevel@tonic-gate 	char *cp, *value;
20620Sstevel@tonic-gate 	const char *cmdp;
20630Sstevel@tonic-gate 	execattr_t *eap;
2064*9765SSean.Wilcox@Sun.COM 	mc_error_t *err = merr;
2065*9765SSean.Wilcox@Sun.COM 	int r;
20669263SSean.Wilcox@Sun.COM 
20679263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_PROFILE, buf, buf_sz, prop,
20689263SSean.Wilcox@Sun.COM 	    val) == 0 || get_astring_val(instpg, SCF_PROPERTY_PROFILE, buf,
20699263SSean.Wilcox@Sun.COM 	    buf_sz, prop, val) == 0))
2070*9765SSean.Wilcox@Sun.COM 		return (mc_error_create(merr, scf_error(),
2071*9765SSean.Wilcox@Sun.COM 		    "Method context requires a profile, but the  \"%s\" "
2072*9765SSean.Wilcox@Sun.COM 		    "property could not be read. scf_error is %s",
2073*9765SSean.Wilcox@Sun.COM 		    SCF_PROPERTY_PROFILE, scf_strerror(scf_error())));
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	/* Extract the command from the command line. */
20760Sstevel@tonic-gate 	cp = strpbrk(cmdline, " \t");
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	if (cp == NULL) {
20790Sstevel@tonic-gate 		cmdp = cmdline;
20800Sstevel@tonic-gate 	} else {
20810Sstevel@tonic-gate 		(void) strncpy(cmd, cmdline, cp - cmdline);
20820Sstevel@tonic-gate 		cmd[cp - cmdline] = '\0';
20830Sstevel@tonic-gate 		cmdp = cmd;
20840Sstevel@tonic-gate 	}
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	/* Require that cmdp[0] == '/'? */
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	eap = getexecprof(buf, KV_COMMAND, cmdp, GET_ONE);
20890Sstevel@tonic-gate 	if (eap == NULL)
2090*9765SSean.Wilcox@Sun.COM 		return (mc_error_create(merr, ENOENT,
2091*9765SSean.Wilcox@Sun.COM 		    "Could not find the execution profile \"%s\", "
2092*9765SSean.Wilcox@Sun.COM 		    "command %s.", buf, cmdp));
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 	/* Based on pfexec.c */
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate 	/* Get the euid first so we don't override ci->pwd for the uid. */
20970Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_EUID_KW)) != NULL) {
2098*9765SSean.Wilcox@Sun.COM 		if ((r = get_uid(value, ci, &ci->euid)) != 0) {
20994321Scasper 			ci->euid = (uid_t)-1;
2100*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(merr, r,
2101*9765SSean.Wilcox@Sun.COM 			    "Could not interpret profile euid value \"%s\", "
2102*9765SSean.Wilcox@Sun.COM 			    "from the execution profile \"%s\", error %d.",
2103*9765SSean.Wilcox@Sun.COM 			    value, buf, r);
21040Sstevel@tonic-gate 			goto out;
21050Sstevel@tonic-gate 		}
21060Sstevel@tonic-gate 	}
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_UID_KW)) != NULL) {
2109*9765SSean.Wilcox@Sun.COM 		if ((r = get_uid(value, ci, &ci->uid)) != 0) {
21104321Scasper 			ci->euid = ci->uid = (uid_t)-1;
2111*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(merr, r,
2112*9765SSean.Wilcox@Sun.COM 			    "Could not interpret profile uid value \"%s\", "
2113*9765SSean.Wilcox@Sun.COM 			    "from the execution profile \"%s\", error %d.",
2114*9765SSean.Wilcox@Sun.COM 			    value, buf, r);
21150Sstevel@tonic-gate 			goto out;
21160Sstevel@tonic-gate 		}
21170Sstevel@tonic-gate 		ci->euid = ci->uid;
21180Sstevel@tonic-gate 	}
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_GID_KW)) != NULL) {
21210Sstevel@tonic-gate 		ci->egid = ci->gid = get_gid(value);
21224321Scasper 		if (ci->gid == (gid_t)-1) {
2123*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(merr, EINVAL,
2124*9765SSean.Wilcox@Sun.COM 			    "Could not interpret profile gid value \"%s\", "
2125*9765SSean.Wilcox@Sun.COM 			    "from the execution profile \"%s\".", value, buf);
21260Sstevel@tonic-gate 			goto out;
21270Sstevel@tonic-gate 		}
21280Sstevel@tonic-gate 	}
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_EGID_KW)) != NULL) {
21310Sstevel@tonic-gate 		ci->egid = get_gid(value);
21324321Scasper 		if (ci->egid == (gid_t)-1) {
2133*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(merr, EINVAL,
2134*9765SSean.Wilcox@Sun.COM 			    "Could not interpret profile egid value \"%s\", "
2135*9765SSean.Wilcox@Sun.COM 			    "from the execution profile \"%s\".", value, buf);
21360Sstevel@tonic-gate 			goto out;
21370Sstevel@tonic-gate 		}
21380Sstevel@tonic-gate 	}
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_LPRIV_KW)) != NULL) {
21410Sstevel@tonic-gate 		ci->lpriv_set = priv_str_to_set(value, ",", NULL);
21420Sstevel@tonic-gate 		if (ci->lpriv_set == NULL) {
21430Sstevel@tonic-gate 			if (errno != EINVAL)
2144*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(merr, ENOMEM,
2145*9765SSean.Wilcox@Sun.COM 				    ALLOCFAIL);
21460Sstevel@tonic-gate 			else
2147*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(merr, EINVAL,
2148*9765SSean.Wilcox@Sun.COM 				    "Could not interpret profile "
2149*9765SSean.Wilcox@Sun.COM 				    "limitprivs value \"%s\", from "
2150*9765SSean.Wilcox@Sun.COM 				    "the execution profile \"%s\".",
2151*9765SSean.Wilcox@Sun.COM 				    value, buf);
21520Sstevel@tonic-gate 			goto out;
21530Sstevel@tonic-gate 		}
21540Sstevel@tonic-gate 	}
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_IPRIV_KW)) != NULL) {
21570Sstevel@tonic-gate 		ci->priv_set = priv_str_to_set(value, ",", NULL);
21580Sstevel@tonic-gate 		if (ci->priv_set == NULL) {
21590Sstevel@tonic-gate 			if (errno != EINVAL)
2160*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(merr, ENOMEM,
2161*9765SSean.Wilcox@Sun.COM 				    ALLOCFAIL);
21620Sstevel@tonic-gate 			else
2163*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(merr, EINVAL,
2164*9765SSean.Wilcox@Sun.COM 				    "Could not interpret profile privs value "
2165*9765SSean.Wilcox@Sun.COM 				    "\"%s\", from the execution profile "
2166*9765SSean.Wilcox@Sun.COM 				    "\"%s\".", value, buf);
21670Sstevel@tonic-gate 			goto out;
21680Sstevel@tonic-gate 		}
21690Sstevel@tonic-gate 	}
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate out:
21720Sstevel@tonic-gate 	free_execattr(eap);
21730Sstevel@tonic-gate 
2174*9765SSean.Wilcox@Sun.COM 	return (err);
21750Sstevel@tonic-gate }
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate /*
2178*9765SSean.Wilcox@Sun.COM  * Return an error message structure containing the error message
2179*9765SSean.Wilcox@Sun.COM  * with context, and the error so the caller can make a decision
2180*9765SSean.Wilcox@Sun.COM  * on what to do next.
2181*9765SSean.Wilcox@Sun.COM  *
2182*9765SSean.Wilcox@Sun.COM  * Because get_ids uses the mc_error_create() function which can
2183*9765SSean.Wilcox@Sun.COM  * reallocate the merr, this function must return the merr pointer
2184*9765SSean.Wilcox@Sun.COM  * in case it was reallocated.
21850Sstevel@tonic-gate  */
2186*9765SSean.Wilcox@Sun.COM static mc_error_t *
21879263SSean.Wilcox@Sun.COM get_ids(scf_propertygroup_t *methpg, scf_propertygroup_t *instpg,
2188*9765SSean.Wilcox@Sun.COM     scf_property_t *prop, scf_value_t *val, struct method_context *ci,
2189*9765SSean.Wilcox@Sun.COM     mc_error_t *merr)
21900Sstevel@tonic-gate {
21910Sstevel@tonic-gate 	char *vbuf = ci->vbuf;
21920Sstevel@tonic-gate 	ssize_t vbuf_sz = ci->vbuf_sz;
21930Sstevel@tonic-gate 	int r;
21940Sstevel@tonic-gate 
21959263SSean.Wilcox@Sun.COM 	/*
21969263SSean.Wilcox@Sun.COM 	 * This should never happen because the caller should fall through
21979263SSean.Wilcox@Sun.COM 	 * another path of just setting the ids to defaults, instead of
21989263SSean.Wilcox@Sun.COM 	 * attempting to get the ids here.
21999263SSean.Wilcox@Sun.COM 	 */
2200*9765SSean.Wilcox@Sun.COM 	if (methpg == NULL && instpg == NULL)
2201*9765SSean.Wilcox@Sun.COM 		return (mc_error_create(merr, ENOENT,
2202*9765SSean.Wilcox@Sun.COM 		    "No property groups to get ids from."));
22039263SSean.Wilcox@Sun.COM 
22049263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_USER,
22059263SSean.Wilcox@Sun.COM 	    vbuf, vbuf_sz, prop, val) == 0 || get_astring_val(instpg,
22069263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_USER, vbuf, vbuf_sz, prop,
2207*9765SSean.Wilcox@Sun.COM 	    val) == 0))
2208*9765SSean.Wilcox@Sun.COM 		return (mc_error_create(merr, ENOENT,
2209*9765SSean.Wilcox@Sun.COM 		    "Could not get \"%s\" property.", SCF_PROPERTY_USER));
2210*9765SSean.Wilcox@Sun.COM 
2211*9765SSean.Wilcox@Sun.COM 	if ((r = get_uid(vbuf, ci, &ci->uid)) != 0) {
22124321Scasper 		ci->uid = (uid_t)-1;
2213*9765SSean.Wilcox@Sun.COM 		return (mc_error_create(merr, r,
2214*9765SSean.Wilcox@Sun.COM 		    "Could not interpret \"%s\" property value \"%s\", "
2215*9765SSean.Wilcox@Sun.COM 		    "error %d.", SCF_PROPERTY_USER, vbuf, r));
22160Sstevel@tonic-gate 	}
22170Sstevel@tonic-gate 
22189263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_GROUP, vbuf, vbuf_sz, prop,
22199263SSean.Wilcox@Sun.COM 	    val) == 0 || get_astring_val(instpg, SCF_PROPERTY_GROUP, vbuf,
22209263SSean.Wilcox@Sun.COM 	    vbuf_sz, prop, val) == 0)) {
22219263SSean.Wilcox@Sun.COM 		if (scf_error() == SCF_ERROR_NOT_FOUND) {
22229263SSean.Wilcox@Sun.COM 			(void) strcpy(vbuf, ":default");
22239263SSean.Wilcox@Sun.COM 		} else {
2224*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, ENOENT,
2225*9765SSean.Wilcox@Sun.COM 			    "Could not get \"%s\" property.",
2226*9765SSean.Wilcox@Sun.COM 			    SCF_PROPERTY_GROUP));
22279263SSean.Wilcox@Sun.COM 		}
22280Sstevel@tonic-gate 	}
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") != 0) {
22310Sstevel@tonic-gate 		ci->gid = get_gid(vbuf);
22324321Scasper 		if (ci->gid == (gid_t)-1) {
2233*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, ENOENT,
2234*9765SSean.Wilcox@Sun.COM 			    "Could not interpret \"%s\" property value \"%s\".",
2235*9765SSean.Wilcox@Sun.COM 			    SCF_PROPERTY_GROUP, vbuf));
22360Sstevel@tonic-gate 		}
22370Sstevel@tonic-gate 	} else {
22380Sstevel@tonic-gate 		switch (r = lookup_pwd(ci)) {
22390Sstevel@tonic-gate 		case 0:
22400Sstevel@tonic-gate 			ci->gid = ci->pwd.pw_gid;
22410Sstevel@tonic-gate 			break;
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 		case ENOENT:
22444321Scasper 			ci->gid = (gid_t)-1;
2245*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, ENOENT,
2246*9765SSean.Wilcox@Sun.COM 			    "No passwd entry for uid \"%d\".", ci->uid));
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 		case ENOMEM:
2249*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, ENOMEM,
2250*9765SSean.Wilcox@Sun.COM 			    "Out of memory."));
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 		case EIO:
22530Sstevel@tonic-gate 		case EMFILE:
22540Sstevel@tonic-gate 		case ENFILE:
2255*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, ENFILE,
2256*9765SSean.Wilcox@Sun.COM 			    "getpwuid_r() failed, error %d.", r));
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 		default:
22590Sstevel@tonic-gate 			bad_fail("lookup_pwd", r);
22600Sstevel@tonic-gate 		}
22610Sstevel@tonic-gate 	}
22620Sstevel@tonic-gate 
22639263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_SUPP_GROUPS, vbuf, vbuf_sz,
22649263SSean.Wilcox@Sun.COM 	    prop, val) == 0 || get_astring_val(instpg,
22659263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_SUPP_GROUPS, vbuf, vbuf_sz, prop, val) == 0)) {
22669263SSean.Wilcox@Sun.COM 		if (scf_error() == SCF_ERROR_NOT_FOUND) {
22679263SSean.Wilcox@Sun.COM 			(void) strcpy(vbuf, ":default");
22689263SSean.Wilcox@Sun.COM 		} else {
2269*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, ENOENT,
2270*9765SSean.Wilcox@Sun.COM 			    "Could not get supplemental groups (\"%s\") "
2271*9765SSean.Wilcox@Sun.COM 			    "property.", SCF_PROPERTY_SUPP_GROUPS));
22729263SSean.Wilcox@Sun.COM 		}
22730Sstevel@tonic-gate 	}
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") != 0) {
22760Sstevel@tonic-gate 		switch (r = get_groups(vbuf, ci)) {
22770Sstevel@tonic-gate 		case 0:
22780Sstevel@tonic-gate 			break;
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate 		case EINVAL:
2281*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, EINVAL,
2282*9765SSean.Wilcox@Sun.COM 			    "Could not interpret supplemental groups (\"%s\") "
2283*9765SSean.Wilcox@Sun.COM 			    "property value \"%s\".", SCF_PROPERTY_SUPP_GROUPS,
2284*9765SSean.Wilcox@Sun.COM 			    vbuf));
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 		case E2BIG:
2287*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, E2BIG,
2288*9765SSean.Wilcox@Sun.COM 			    "Too many supplemental groups values in \"%s\".",
2289*9765SSean.Wilcox@Sun.COM 			    vbuf));
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 		default:
22920Sstevel@tonic-gate 			bad_fail("get_groups", r);
22930Sstevel@tonic-gate 		}
22940Sstevel@tonic-gate 	} else {
22950Sstevel@tonic-gate 		ci->ngroups = -1;
22960Sstevel@tonic-gate 	}
22970Sstevel@tonic-gate 
22989263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_PRIVILEGES, vbuf, vbuf_sz,
22999263SSean.Wilcox@Sun.COM 	    prop, val) == 0 || get_astring_val(instpg, SCF_PROPERTY_PRIVILEGES,
23009263SSean.Wilcox@Sun.COM 	    vbuf, vbuf_sz, prop, val) == 0)) {
23019263SSean.Wilcox@Sun.COM 		if (scf_error() == SCF_ERROR_NOT_FOUND) {
23029263SSean.Wilcox@Sun.COM 			(void) strcpy(vbuf, ":default");
23039263SSean.Wilcox@Sun.COM 		} else {
2304*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, ENOENT,
2305*9765SSean.Wilcox@Sun.COM 			    "Could not get \"%s\" property.",
2306*9765SSean.Wilcox@Sun.COM 			    SCF_PROPERTY_PRIVILEGES));
23079263SSean.Wilcox@Sun.COM 		}
23080Sstevel@tonic-gate 	}
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 	/*
23110Sstevel@tonic-gate 	 * For default privs, we need to keep priv_set == NULL, as
23120Sstevel@tonic-gate 	 * we use this test elsewhere.
23130Sstevel@tonic-gate 	 */
23140Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") != 0) {
23150Sstevel@tonic-gate 		ci->priv_set = priv_str_to_set(vbuf, ",", NULL);
23160Sstevel@tonic-gate 		if (ci->priv_set == NULL) {
23170Sstevel@tonic-gate 			if (errno != EINVAL) {
2318*9765SSean.Wilcox@Sun.COM 				return (mc_error_create(merr, ENOMEM,
2319*9765SSean.Wilcox@Sun.COM 				    ALLOCFAIL));
23200Sstevel@tonic-gate 			} else {
2321*9765SSean.Wilcox@Sun.COM 				return (mc_error_create(merr, EINVAL,
2322*9765SSean.Wilcox@Sun.COM 				    "Could not interpret \"%s\" "
2323*9765SSean.Wilcox@Sun.COM 				    "property value \"%s\".",
2324*9765SSean.Wilcox@Sun.COM 				    SCF_PROPERTY_PRIVILEGES, vbuf));
23250Sstevel@tonic-gate 			}
23260Sstevel@tonic-gate 		}
23270Sstevel@tonic-gate 	}
23280Sstevel@tonic-gate 
23299263SSean.Wilcox@Sun.COM 	if (!(get_astring_val(methpg, SCF_PROPERTY_LIMIT_PRIVILEGES, vbuf,
23309263SSean.Wilcox@Sun.COM 	    vbuf_sz, prop, val) == 0 || get_astring_val(instpg,
23319263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_LIMIT_PRIVILEGES, vbuf, vbuf_sz, prop, val) == 0)) {
23329263SSean.Wilcox@Sun.COM 		if (scf_error() == SCF_ERROR_NOT_FOUND) {
23339263SSean.Wilcox@Sun.COM 			(void) strcpy(vbuf, ":default");
23349263SSean.Wilcox@Sun.COM 		} else {
2335*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, ENOENT,
2336*9765SSean.Wilcox@Sun.COM 			    "Could not get \"%s\" property.",
2337*9765SSean.Wilcox@Sun.COM 			    SCF_PROPERTY_LIMIT_PRIVILEGES));
23389263SSean.Wilcox@Sun.COM 		}
23390Sstevel@tonic-gate 	}
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") == 0)
23420Sstevel@tonic-gate 		/*
23430Sstevel@tonic-gate 		 * L must default to all privileges so root NPA services see
23440Sstevel@tonic-gate 		 * iE = all.  "zone" is all privileges available in the current
23450Sstevel@tonic-gate 		 * zone, equivalent to "all" in the global zone.
23460Sstevel@tonic-gate 		 */
23470Sstevel@tonic-gate 		(void) strcpy(vbuf, "zone");
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate 	ci->lpriv_set = priv_str_to_set(vbuf, ",", NULL);
23500Sstevel@tonic-gate 	if (ci->lpriv_set == NULL) {
2351*9765SSean.Wilcox@Sun.COM 		if (errno != EINVAL) {
2352*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, ENOMEM, ALLOCFAIL));
2353*9765SSean.Wilcox@Sun.COM 		} else {
2354*9765SSean.Wilcox@Sun.COM 			return (mc_error_create(merr, EINVAL,
2355*9765SSean.Wilcox@Sun.COM 			    "Could not interpret \"%s\" property value \"%s\".",
2356*9765SSean.Wilcox@Sun.COM 			    SCF_PROPERTY_LIMIT_PRIVILEGES, vbuf));
23570Sstevel@tonic-gate 		}
23580Sstevel@tonic-gate 	}
23590Sstevel@tonic-gate 
2360*9765SSean.Wilcox@Sun.COM 	return (merr);
23610Sstevel@tonic-gate }
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate static int
23640Sstevel@tonic-gate get_environment(scf_handle_t *h, scf_propertygroup_t *pg,
23650Sstevel@tonic-gate     struct method_context *mcp, scf_property_t *prop, scf_value_t *val)
23660Sstevel@tonic-gate {
23670Sstevel@tonic-gate 	scf_iter_t *iter;
23680Sstevel@tonic-gate 	scf_type_t type;
23690Sstevel@tonic-gate 	size_t i = 0;
23700Sstevel@tonic-gate 	int ret;
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENVIRONMENT, prop) != 0) {
23730Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
23740Sstevel@tonic-gate 			return (ENOENT);
23750Sstevel@tonic-gate 		return (scf_error());
23760Sstevel@tonic-gate 	}
23770Sstevel@tonic-gate 	if (scf_property_type(prop, &type) != 0)
23780Sstevel@tonic-gate 		return (scf_error());
23790Sstevel@tonic-gate 	if (type != SCF_TYPE_ASTRING)
23800Sstevel@tonic-gate 		return (EINVAL);
23810Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
23820Sstevel@tonic-gate 		return (scf_error());
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) != 0) {
23850Sstevel@tonic-gate 		ret = scf_error();
23860Sstevel@tonic-gate 		scf_iter_destroy(iter);
23870Sstevel@tonic-gate 		return (ret);
23880Sstevel@tonic-gate 	}
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 	mcp->env_sz = 10;
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 	if ((mcp->env = uu_zalloc(sizeof (*mcp->env) * mcp->env_sz)) == NULL) {
23930Sstevel@tonic-gate 		ret = ENOMEM;
23940Sstevel@tonic-gate 		goto out;
23950Sstevel@tonic-gate 	}
23960Sstevel@tonic-gate 
23970Sstevel@tonic-gate 	while ((ret = scf_iter_next_value(iter, val)) == 1) {
23980Sstevel@tonic-gate 		ret = scf_value_get_as_string(val, mcp->vbuf, mcp->vbuf_sz);
23990Sstevel@tonic-gate 		if (ret == -1) {
24000Sstevel@tonic-gate 			ret = scf_error();
24010Sstevel@tonic-gate 			goto out;
24020Sstevel@tonic-gate 		}
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 		if ((mcp->env[i] = strdup(mcp->vbuf)) == NULL) {
24050Sstevel@tonic-gate 			ret = ENOMEM;
24060Sstevel@tonic-gate 			goto out;
24070Sstevel@tonic-gate 		}
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 		if (++i == mcp->env_sz) {
24100Sstevel@tonic-gate 			char **env;
24110Sstevel@tonic-gate 			mcp->env_sz *= 2;
24120Sstevel@tonic-gate 			env = uu_zalloc(sizeof (*mcp->env) * mcp->env_sz);
24130Sstevel@tonic-gate 			if (env == NULL) {
24140Sstevel@tonic-gate 				ret = ENOMEM;
24150Sstevel@tonic-gate 				goto out;
24160Sstevel@tonic-gate 			}
24170Sstevel@tonic-gate 			(void) memcpy(env, mcp->env,
24180Sstevel@tonic-gate 			    sizeof (*mcp->env) * (mcp->env_sz / 2));
24190Sstevel@tonic-gate 			free(mcp->env);
24200Sstevel@tonic-gate 			mcp->env = env;
24210Sstevel@tonic-gate 		}
24220Sstevel@tonic-gate 	}
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	if (ret == -1)
24250Sstevel@tonic-gate 		ret = scf_error();
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate out:
24280Sstevel@tonic-gate 	scf_iter_destroy(iter);
24290Sstevel@tonic-gate 	return (ret);
24300Sstevel@tonic-gate }
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate /*
24330Sstevel@tonic-gate  * Fetch method context information from the repository, allocate and fill
2434*9765SSean.Wilcox@Sun.COM  * a method_context structure, return it in *mcpp, and return NULL.
24350Sstevel@tonic-gate  *
24369263SSean.Wilcox@Sun.COM  * If no method_context is defined, original init context is provided, where
24379263SSean.Wilcox@Sun.COM  * the working directory is '/', and uid/gid are 0/0.  But if a method_context
24389263SSean.Wilcox@Sun.COM  * is defined at any level the smf_method(5) method_context defaults are used.
24399263SSean.Wilcox@Sun.COM  *
2440*9765SSean.Wilcox@Sun.COM  * Return an error message structure containing the error message
2441*9765SSean.Wilcox@Sun.COM  * with context, and the error so the caller can make a decision
2442*9765SSean.Wilcox@Sun.COM  * on what to do next.
2443*9765SSean.Wilcox@Sun.COM  *
2444*9765SSean.Wilcox@Sun.COM  * Error Types :
2445*9765SSean.Wilcox@Sun.COM  * 	E2BIG		Too many values or entry is too big
2446*9765SSean.Wilcox@Sun.COM  * 	EINVAL		Invalid value
2447*9765SSean.Wilcox@Sun.COM  * 	EIO		an I/O error has occured
2448*9765SSean.Wilcox@Sun.COM  * 	ENOENT		no entry for value
2449*9765SSean.Wilcox@Sun.COM  * 	ENOMEM		out of memory
2450*9765SSean.Wilcox@Sun.COM  * 	ENOTSUP		Version mismatch
2451*9765SSean.Wilcox@Sun.COM  * 	ERANGE		value is out of range
2452*9765SSean.Wilcox@Sun.COM  * 	EMFILE/ENFILE	out of file descriptors
2453*9765SSean.Wilcox@Sun.COM  *
2454*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_BACKEND_ACCESS
2455*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_CONNECTION_BROKEN
2456*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_DELETED
2457*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_CONSTRAINT_VIOLATED
2458*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_HANDLE_DESTROYED
2459*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_INTERNAL
2460*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_INVALID_ARGUMENT
2461*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_NO_MEMORY
2462*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_NO_RESOURCES
2463*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_NOT_BOUND
2464*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_NOT_FOUND
2465*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_NOT_SET
2466*9765SSean.Wilcox@Sun.COM  * 	SCF_ERROR_TYPE_MISMATCH
2467*9765SSean.Wilcox@Sun.COM  *
24680Sstevel@tonic-gate  */
2469*9765SSean.Wilcox@Sun.COM mc_error_t *
24700Sstevel@tonic-gate restarter_get_method_context(uint_t version, scf_instance_t *inst,
24710Sstevel@tonic-gate     scf_snapshot_t *snap, const char *mname, const char *cmdline,
24720Sstevel@tonic-gate     struct method_context **mcpp)
24730Sstevel@tonic-gate {
24740Sstevel@tonic-gate 	scf_handle_t *h;
24750Sstevel@tonic-gate 	scf_propertygroup_t *methpg = NULL;
24760Sstevel@tonic-gate 	scf_propertygroup_t *instpg = NULL;
24770Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
24780Sstevel@tonic-gate 	scf_property_t *prop = NULL;
24790Sstevel@tonic-gate 	scf_value_t *val = NULL;
24800Sstevel@tonic-gate 	scf_type_t ty;
24810Sstevel@tonic-gate 	uint8_t use_profile;
2482*9765SSean.Wilcox@Sun.COM 	int ret = 0;
24839263SSean.Wilcox@Sun.COM 	int mc_used = 0;
2484*9765SSean.Wilcox@Sun.COM 	mc_error_t *err = NULL;
24850Sstevel@tonic-gate 	struct method_context *cip;
24860Sstevel@tonic-gate 
2487*9765SSean.Wilcox@Sun.COM 	if ((err = malloc(sizeof (mc_error_t))) == NULL)
2488*9765SSean.Wilcox@Sun.COM 		return (mc_error_create(NULL, ENOMEM, NULL));
2489*9765SSean.Wilcox@Sun.COM 
2490*9765SSean.Wilcox@Sun.COM 	/* Set the type to zero to track if an error occured. */
2491*9765SSean.Wilcox@Sun.COM 	err->type = 0;
2492*9765SSean.Wilcox@Sun.COM 
24930Sstevel@tonic-gate 	if (version != RESTARTER_METHOD_CONTEXT_VERSION)
2494*9765SSean.Wilcox@Sun.COM 		return (mc_error_create(err, ENOTSUP,
2495*9765SSean.Wilcox@Sun.COM 		    "Invalid client version %d. (Expected %d)",
2496*9765SSean.Wilcox@Sun.COM 		    version, RESTARTER_METHOD_CONTEXT_VERSION));
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 	/* Get the handle before we allocate anything. */
24990Sstevel@tonic-gate 	h = scf_instance_handle(inst);
25000Sstevel@tonic-gate 	if (h == NULL)
2501*9765SSean.Wilcox@Sun.COM 		return (mc_error_create(err, scf_error(),
2502*9765SSean.Wilcox@Sun.COM 		    scf_strerror(scf_error())));
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 	cip = malloc(sizeof (*cip));
25050Sstevel@tonic-gate 	if (cip == NULL)
2506*9765SSean.Wilcox@Sun.COM 		return (mc_error_create(err, ENOMEM, ALLOCFAIL));
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate 	(void) memset(cip, 0, sizeof (*cip));
25094321Scasper 	cip->uid = (uid_t)-1;
25104321Scasper 	cip->euid = (uid_t)-1;
25114321Scasper 	cip->gid = (gid_t)-1;
25124321Scasper 	cip->egid = (gid_t)-1;
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	cip->vbuf_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
25150Sstevel@tonic-gate 	assert(cip->vbuf_sz >= 0);
25160Sstevel@tonic-gate 	cip->vbuf = malloc(cip->vbuf_sz);
25170Sstevel@tonic-gate 	if (cip->vbuf == NULL) {
25180Sstevel@tonic-gate 		free(cip);
2519*9765SSean.Wilcox@Sun.COM 		return (mc_error_create(err, ENOMEM, ALLOCFAIL));
25200Sstevel@tonic-gate 	}
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 	if ((instpg = scf_pg_create(h)) == NULL ||
25230Sstevel@tonic-gate 	    (methpg = scf_pg_create(h)) == NULL ||
25240Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
25250Sstevel@tonic-gate 	    (val = scf_value_create(h)) == NULL) {
2526*9765SSean.Wilcox@Sun.COM 		err = mc_error_create(err, scf_error(),
2527*9765SSean.Wilcox@Sun.COM 		    "Failed to create repository object: %s\n",
2528*9765SSean.Wilcox@Sun.COM 		    scf_strerror(scf_error()));
25290Sstevel@tonic-gate 		goto out;
25300Sstevel@tonic-gate 	}
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 	/*
25330Sstevel@tonic-gate 	 * The method environment, and the credentials/profile data,
25340Sstevel@tonic-gate 	 * may be found either in the pg for the method (methpg),
25350Sstevel@tonic-gate 	 * or in the instance/service SCF_PG_METHOD_CONTEXT pg (named
25360Sstevel@tonic-gate 	 * instpg below).
25370Sstevel@tonic-gate 	 */
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, mname, methpg) !=
25400Sstevel@tonic-gate 	    SCF_SUCCESS) {
2541*9765SSean.Wilcox@Sun.COM 		err = mc_error_create(err, scf_error(), "Unable to get the "
2542*9765SSean.Wilcox@Sun.COM 		    "\"%s\" method, %s", mname, scf_strerror(scf_error()));
25430Sstevel@tonic-gate 		goto out;
25440Sstevel@tonic-gate 	}
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, SCF_PG_METHOD_CONTEXT,
25470Sstevel@tonic-gate 	    instpg) != SCF_SUCCESS) {
25480Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND) {
2549*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, scf_error(),
2550*9765SSean.Wilcox@Sun.COM 			    "Unable to retrieve the \"%s\" property group, %s",
2551*9765SSean.Wilcox@Sun.COM 			    SCF_PG_METHOD_CONTEXT, scf_strerror(scf_error()));
25520Sstevel@tonic-gate 			goto out;
25530Sstevel@tonic-gate 		}
25540Sstevel@tonic-gate 		scf_pg_destroy(instpg);
25550Sstevel@tonic-gate 		instpg = NULL;
25569263SSean.Wilcox@Sun.COM 	} else {
25579263SSean.Wilcox@Sun.COM 		mc_used++;
25580Sstevel@tonic-gate 	}
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 	ret = get_environment(h, methpg, cip, prop, val);
25610Sstevel@tonic-gate 	if (ret == ENOENT && instpg != NULL) {
25620Sstevel@tonic-gate 		ret = get_environment(h, instpg, cip, prop, val);
25630Sstevel@tonic-gate 	}
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 	switch (ret) {
25660Sstevel@tonic-gate 	case 0:
25679263SSean.Wilcox@Sun.COM 		mc_used++;
25689263SSean.Wilcox@Sun.COM 		break;
25690Sstevel@tonic-gate 	case ENOENT:
25700Sstevel@tonic-gate 		break;
25710Sstevel@tonic-gate 	case ENOMEM:
2572*9765SSean.Wilcox@Sun.COM 		err = mc_error_create(err, ret, "Out of memory.");
25730Sstevel@tonic-gate 		goto out;
25740Sstevel@tonic-gate 	case EINVAL:
2575*9765SSean.Wilcox@Sun.COM 		err = mc_error_create(err, ret, "Invalid method environment.");
25760Sstevel@tonic-gate 		goto out;
25770Sstevel@tonic-gate 	default:
2578*9765SSean.Wilcox@Sun.COM 		err = mc_error_create(err, ret,
2579*9765SSean.Wilcox@Sun.COM 		    "Get method environment failed : %s\n", scf_strerror(ret));
25800Sstevel@tonic-gate 		goto out;
25810Sstevel@tonic-gate 	}
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 	pg = methpg;
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	ret = scf_pg_get_property(pg, SCF_PROPERTY_USE_PROFILE, prop);
25860Sstevel@tonic-gate 	if (ret && scf_error() == SCF_ERROR_NOT_FOUND && instpg != NULL) {
25879263SSean.Wilcox@Sun.COM 		pg = NULL;
25889263SSean.Wilcox@Sun.COM 		ret = scf_pg_get_property(instpg, SCF_PROPERTY_USE_PROFILE,
25899263SSean.Wilcox@Sun.COM 		    prop);
25900Sstevel@tonic-gate 	}
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 	if (ret) {
25930Sstevel@tonic-gate 		switch (scf_error()) {
25940Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
25959263SSean.Wilcox@Sun.COM 			/* No profile context: use default credentials */
25960Sstevel@tonic-gate 			cip->uid = 0;
25970Sstevel@tonic-gate 			cip->gid = 0;
25989263SSean.Wilcox@Sun.COM 			break;
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
2601*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, SCF_ERROR_CONNECTION_BROKEN,
2602*9765SSean.Wilcox@Sun.COM 			    RCBROKEN);
26030Sstevel@tonic-gate 			goto out;
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
2606*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, SCF_ERROR_NOT_FOUND,
2607*9765SSean.Wilcox@Sun.COM 			    "Could not find property group \"%s\"",
2608*9765SSean.Wilcox@Sun.COM 			    pg == NULL ? SCF_PG_METHOD_CONTEXT : mname);
26090Sstevel@tonic-gate 			goto out;
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
26120Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
26130Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
26140Sstevel@tonic-gate 		default:
26150Sstevel@tonic-gate 			bad_fail("scf_pg_get_property", scf_error());
26160Sstevel@tonic-gate 		}
26179263SSean.Wilcox@Sun.COM 	} else {
26189263SSean.Wilcox@Sun.COM 		if (scf_property_type(prop, &ty) != SCF_SUCCESS) {
2619*9765SSean.Wilcox@Sun.COM 			ret = scf_error();
2620*9765SSean.Wilcox@Sun.COM 			switch (ret) {
26219263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONNECTION_BROKEN:
2622*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err,
2623*9765SSean.Wilcox@Sun.COM 				    SCF_ERROR_CONNECTION_BROKEN, RCBROKEN);
26249263SSean.Wilcox@Sun.COM 				break;
26259263SSean.Wilcox@Sun.COM 
26269263SSean.Wilcox@Sun.COM 			case SCF_ERROR_DELETED:
2627*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err,
2628*9765SSean.Wilcox@Sun.COM 				    SCF_ERROR_NOT_FOUND,
2629*9765SSean.Wilcox@Sun.COM 				    "Could not find property group \"%s\"",
2630*9765SSean.Wilcox@Sun.COM 				    pg == NULL ? SCF_PG_METHOD_CONTEXT : mname);
26319263SSean.Wilcox@Sun.COM 				break;
26329263SSean.Wilcox@Sun.COM 
26339263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_SET:
26349263SSean.Wilcox@Sun.COM 			default:
2635*9765SSean.Wilcox@Sun.COM 				bad_fail("scf_property_type", ret);
26369263SSean.Wilcox@Sun.COM 			}
26379263SSean.Wilcox@Sun.COM 
26389263SSean.Wilcox@Sun.COM 			goto out;
26399263SSean.Wilcox@Sun.COM 		}
26409263SSean.Wilcox@Sun.COM 
26419263SSean.Wilcox@Sun.COM 		if (ty != SCF_TYPE_BOOLEAN) {
2642*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err,
2643*9765SSean.Wilcox@Sun.COM 			    SCF_ERROR_TYPE_MISMATCH,
2644*9765SSean.Wilcox@Sun.COM 			    "\"%s\" property is not boolean in property group "
2645*9765SSean.Wilcox@Sun.COM 			    "\"%s\".", SCF_PROPERTY_USE_PROFILE,
2646*9765SSean.Wilcox@Sun.COM 			    pg == NULL ? SCF_PG_METHOD_CONTEXT : mname);
26479263SSean.Wilcox@Sun.COM 			goto out;
26489263SSean.Wilcox@Sun.COM 		}
26499263SSean.Wilcox@Sun.COM 
26509263SSean.Wilcox@Sun.COM 		if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2651*9765SSean.Wilcox@Sun.COM 			ret = scf_error();
2652*9765SSean.Wilcox@Sun.COM 			switch (ret) {
26539263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONNECTION_BROKEN:
2654*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err,
2655*9765SSean.Wilcox@Sun.COM 				    SCF_ERROR_CONNECTION_BROKEN, RCBROKEN);
26569263SSean.Wilcox@Sun.COM 				break;
26579263SSean.Wilcox@Sun.COM 
26589263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONSTRAINT_VIOLATED:
2659*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err,
2660*9765SSean.Wilcox@Sun.COM 				    SCF_ERROR_CONSTRAINT_VIOLATED,
2661*9765SSean.Wilcox@Sun.COM 				    "\"%s\" property has multiple values.",
2662*9765SSean.Wilcox@Sun.COM 				    SCF_PROPERTY_USE_PROFILE);
26639263SSean.Wilcox@Sun.COM 				break;
26649263SSean.Wilcox@Sun.COM 
26659263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_FOUND:
2666*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err,
2667*9765SSean.Wilcox@Sun.COM 				    SCF_ERROR_NOT_FOUND,
2668*9765SSean.Wilcox@Sun.COM 				    "\"%s\" property has no values.",
2669*9765SSean.Wilcox@Sun.COM 				    SCF_PROPERTY_USE_PROFILE);
26709263SSean.Wilcox@Sun.COM 				break;
26719263SSean.Wilcox@Sun.COM 			default:
2672*9765SSean.Wilcox@Sun.COM 				bad_fail("scf_property_get_value", ret);
26739263SSean.Wilcox@Sun.COM 			}
26749263SSean.Wilcox@Sun.COM 
26759263SSean.Wilcox@Sun.COM 			goto out;
26769263SSean.Wilcox@Sun.COM 		}
26779263SSean.Wilcox@Sun.COM 
26789263SSean.Wilcox@Sun.COM 		mc_used++;
26799263SSean.Wilcox@Sun.COM 		ret = scf_value_get_boolean(val, &use_profile);
26809263SSean.Wilcox@Sun.COM 		assert(ret == SCF_SUCCESS);
26819263SSean.Wilcox@Sun.COM 
26829263SSean.Wilcox@Sun.COM 		/* get ids & privileges */
26839263SSean.Wilcox@Sun.COM 		if (use_profile)
2684*9765SSean.Wilcox@Sun.COM 			err = get_profile(pg, instpg, prop, val, cmdline,
2685*9765SSean.Wilcox@Sun.COM 			    cip, err);
26869263SSean.Wilcox@Sun.COM 		else
2687*9765SSean.Wilcox@Sun.COM 			err = get_ids(pg, instpg, prop, val, cip, err);
2688*9765SSean.Wilcox@Sun.COM 
2689*9765SSean.Wilcox@Sun.COM 		if (err->type != 0)
26909263SSean.Wilcox@Sun.COM 			goto out;
26910Sstevel@tonic-gate 	}
26920Sstevel@tonic-gate 
26939263SSean.Wilcox@Sun.COM 	/* get working directory */
26949263SSean.Wilcox@Sun.COM 	if ((methpg != NULL && scf_pg_get_property(methpg,
26959263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_WORKING_DIRECTORY, prop) == SCF_SUCCESS) ||
26969263SSean.Wilcox@Sun.COM 	    (instpg != NULL && scf_pg_get_property(instpg,
26979263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_WORKING_DIRECTORY, prop) == SCF_SUCCESS)) {
26989263SSean.Wilcox@Sun.COM 		if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2699*9765SSean.Wilcox@Sun.COM 			ret = scf_error();
2700*9765SSean.Wilcox@Sun.COM 			switch (ret) {
27019263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONNECTION_BROKEN:
2702*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err, ret, RCBROKEN);
27039263SSean.Wilcox@Sun.COM 				break;
27049263SSean.Wilcox@Sun.COM 
27059263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONSTRAINT_VIOLATED:
2706*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err, ret,
2707*9765SSean.Wilcox@Sun.COM 				    "\"%s\" property has multiple values.",
2708*9765SSean.Wilcox@Sun.COM 				    SCF_PROPERTY_WORKING_DIRECTORY);
27099263SSean.Wilcox@Sun.COM 				break;
27109263SSean.Wilcox@Sun.COM 
27119263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_FOUND:
2712*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err, ret,
2713*9765SSean.Wilcox@Sun.COM 				    "\"%s\" property has no values.",
2714*9765SSean.Wilcox@Sun.COM 				    SCF_PROPERTY_WORKING_DIRECTORY);
27159263SSean.Wilcox@Sun.COM 				break;
27169263SSean.Wilcox@Sun.COM 
27179263SSean.Wilcox@Sun.COM 			default:
2718*9765SSean.Wilcox@Sun.COM 				bad_fail("scf_property_get_value", ret);
27199263SSean.Wilcox@Sun.COM 			}
27209263SSean.Wilcox@Sun.COM 
27219263SSean.Wilcox@Sun.COM 			goto out;
27229263SSean.Wilcox@Sun.COM 		}
27239263SSean.Wilcox@Sun.COM 
27249263SSean.Wilcox@Sun.COM 		mc_used++;
27259263SSean.Wilcox@Sun.COM 		ret = scf_value_get_astring(val, cip->vbuf, cip->vbuf_sz);
27269263SSean.Wilcox@Sun.COM 		assert(ret != -1);
27279263SSean.Wilcox@Sun.COM 	} else {
2728*9765SSean.Wilcox@Sun.COM 		ret = scf_error();
2729*9765SSean.Wilcox@Sun.COM 		switch (ret) {
27309263SSean.Wilcox@Sun.COM 		case SCF_ERROR_NOT_FOUND:
27319263SSean.Wilcox@Sun.COM 			/* okay if missing. */
27329263SSean.Wilcox@Sun.COM 			(void) strcpy(cip->vbuf, ":default");
27330Sstevel@tonic-gate 			break;
27340Sstevel@tonic-gate 
27350Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
2736*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret, RCBROKEN);
27379263SSean.Wilcox@Sun.COM 			goto out;
27389263SSean.Wilcox@Sun.COM 
27399263SSean.Wilcox@Sun.COM 		case SCF_ERROR_DELETED:
2740*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret,
2741*9765SSean.Wilcox@Sun.COM 			    "Property group could not be found");
27429263SSean.Wilcox@Sun.COM 			goto out;
27439263SSean.Wilcox@Sun.COM 
27449263SSean.Wilcox@Sun.COM 		case SCF_ERROR_HANDLE_MISMATCH:
27459263SSean.Wilcox@Sun.COM 		case SCF_ERROR_INVALID_ARGUMENT:
27469263SSean.Wilcox@Sun.COM 		case SCF_ERROR_NOT_SET:
27470Sstevel@tonic-gate 		default:
2748*9765SSean.Wilcox@Sun.COM 			bad_fail("scf_pg_get_property", ret);
27490Sstevel@tonic-gate 		}
27500Sstevel@tonic-gate 	}
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 	if (strcmp(cip->vbuf, ":default") == 0 ||
27530Sstevel@tonic-gate 	    strcmp(cip->vbuf, ":home") == 0) {
27540Sstevel@tonic-gate 		switch (ret = lookup_pwd(cip)) {
27550Sstevel@tonic-gate 		case 0:
27560Sstevel@tonic-gate 			break;
27570Sstevel@tonic-gate 
27580Sstevel@tonic-gate 		case ENOMEM:
2759*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret, "Out of memory.");
27600Sstevel@tonic-gate 			goto out;
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 		case ENOENT:
27630Sstevel@tonic-gate 		case EIO:
27640Sstevel@tonic-gate 		case EMFILE:
27650Sstevel@tonic-gate 		case ENFILE:
2766*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret,
2767*9765SSean.Wilcox@Sun.COM 			    "Could not get passwd entry.");
27680Sstevel@tonic-gate 			goto out;
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate 		default:
27710Sstevel@tonic-gate 			bad_fail("lookup_pwd", ret);
27720Sstevel@tonic-gate 		}
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 		cip->working_dir = strdup(cip->pwd.pw_dir);
27750Sstevel@tonic-gate 		if (cip->working_dir == NULL) {
2776*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ENOMEM, ALLOCFAIL);
27770Sstevel@tonic-gate 			goto out;
27780Sstevel@tonic-gate 		}
27790Sstevel@tonic-gate 	} else {
27800Sstevel@tonic-gate 		cip->working_dir = strdup(cip->vbuf);
27810Sstevel@tonic-gate 		if (cip->working_dir == NULL) {
2782*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ENOMEM, ALLOCFAIL);
27830Sstevel@tonic-gate 			goto out;
27840Sstevel@tonic-gate 		}
27850Sstevel@tonic-gate 	}
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 	/* get (optional) corefile pattern */
27889263SSean.Wilcox@Sun.COM 	if ((methpg != NULL && scf_pg_get_property(methpg,
27899263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_COREFILE_PATTERN, prop) == SCF_SUCCESS) ||
27909263SSean.Wilcox@Sun.COM 	    (instpg != NULL && scf_pg_get_property(instpg,
27919263SSean.Wilcox@Sun.COM 	    SCF_PROPERTY_COREFILE_PATTERN, prop) == SCF_SUCCESS)) {
27929263SSean.Wilcox@Sun.COM 		if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2793*9765SSean.Wilcox@Sun.COM 			ret = scf_error();
2794*9765SSean.Wilcox@Sun.COM 			switch (ret) {
27959263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONNECTION_BROKEN:
2796*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err, ret, RCBROKEN);
27979263SSean.Wilcox@Sun.COM 				break;
27989263SSean.Wilcox@Sun.COM 
27999263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONSTRAINT_VIOLATED:
2800*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err, ret,
2801*9765SSean.Wilcox@Sun.COM 				    "\"%s\" property has multiple values.",
2802*9765SSean.Wilcox@Sun.COM 				    SCF_PROPERTY_COREFILE_PATTERN);
28039263SSean.Wilcox@Sun.COM 				break;
28049263SSean.Wilcox@Sun.COM 
28059263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_FOUND:
2806*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err, ret,
2807*9765SSean.Wilcox@Sun.COM 				    "\"%s\" property has no values.",
2808*9765SSean.Wilcox@Sun.COM 				    SCF_PROPERTY_COREFILE_PATTERN);
28099263SSean.Wilcox@Sun.COM 				break;
28109263SSean.Wilcox@Sun.COM 
28119263SSean.Wilcox@Sun.COM 			default:
2812*9765SSean.Wilcox@Sun.COM 				bad_fail("scf_property_get_value", ret);
28139263SSean.Wilcox@Sun.COM 			}
28149263SSean.Wilcox@Sun.COM 
28159263SSean.Wilcox@Sun.COM 		} else {
28169263SSean.Wilcox@Sun.COM 
28179263SSean.Wilcox@Sun.COM 			ret = scf_value_get_astring(val, cip->vbuf,
28189263SSean.Wilcox@Sun.COM 			    cip->vbuf_sz);
28199263SSean.Wilcox@Sun.COM 			assert(ret != -1);
28209263SSean.Wilcox@Sun.COM 
28219263SSean.Wilcox@Sun.COM 			cip->corefile_pattern = strdup(cip->vbuf);
28229263SSean.Wilcox@Sun.COM 			if (cip->corefile_pattern == NULL) {
2823*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err, ENOMEM, ALLOCFAIL);
28249263SSean.Wilcox@Sun.COM 				goto out;
28259263SSean.Wilcox@Sun.COM 			}
28260Sstevel@tonic-gate 		}
28270Sstevel@tonic-gate 
28289263SSean.Wilcox@Sun.COM 		mc_used++;
28290Sstevel@tonic-gate 	} else {
2830*9765SSean.Wilcox@Sun.COM 		ret = scf_error();
2831*9765SSean.Wilcox@Sun.COM 		switch (ret) {
28320Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
28330Sstevel@tonic-gate 			/* okay if missing. */
28340Sstevel@tonic-gate 			break;
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
2837*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret, RCBROKEN);
28380Sstevel@tonic-gate 			goto out;
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
2841*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret,
2842*9765SSean.Wilcox@Sun.COM 			    "Property group could not be found");
28430Sstevel@tonic-gate 			goto out;
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
28460Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
28470Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
28480Sstevel@tonic-gate 		default:
2849*9765SSean.Wilcox@Sun.COM 			bad_fail("scf_pg_get_property", ret);
28500Sstevel@tonic-gate 		}
28510Sstevel@tonic-gate 	}
28520Sstevel@tonic-gate 
28530Sstevel@tonic-gate 	if (restarter_rm_libs_loadable()) {
28540Sstevel@tonic-gate 		/* get project */
28559263SSean.Wilcox@Sun.COM 		if ((methpg != NULL && scf_pg_get_property(methpg,
28569263SSean.Wilcox@Sun.COM 		    SCF_PROPERTY_PROJECT, prop) == SCF_SUCCESS) ||
28579263SSean.Wilcox@Sun.COM 		    (instpg != NULL && scf_pg_get_property(instpg,
28589263SSean.Wilcox@Sun.COM 		    SCF_PROPERTY_PROJECT, prop) == SCF_SUCCESS)) {
28599263SSean.Wilcox@Sun.COM 			if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2860*9765SSean.Wilcox@Sun.COM 				ret = scf_error();
2861*9765SSean.Wilcox@Sun.COM 				switch (ret) {
28629263SSean.Wilcox@Sun.COM 				case SCF_ERROR_CONNECTION_BROKEN:
2863*9765SSean.Wilcox@Sun.COM 					err = mc_error_create(err, ret,
2864*9765SSean.Wilcox@Sun.COM 					    RCBROKEN);
28659263SSean.Wilcox@Sun.COM 					break;
28669263SSean.Wilcox@Sun.COM 
28679263SSean.Wilcox@Sun.COM 				case SCF_ERROR_CONSTRAINT_VIOLATED:
2868*9765SSean.Wilcox@Sun.COM 					err = mc_error_create(err, ret,
2869*9765SSean.Wilcox@Sun.COM 					    "\"%s\" property has multiple "
2870*9765SSean.Wilcox@Sun.COM 					    "values.", SCF_PROPERTY_PROJECT);
28719263SSean.Wilcox@Sun.COM 					break;
28729263SSean.Wilcox@Sun.COM 
28739263SSean.Wilcox@Sun.COM 				case SCF_ERROR_NOT_FOUND:
2874*9765SSean.Wilcox@Sun.COM 					err = mc_error_create(err, ret,
2875*9765SSean.Wilcox@Sun.COM 					    "\"%s\" property has no values.",
2876*9765SSean.Wilcox@Sun.COM 					    SCF_PROPERTY_PROJECT);
28779263SSean.Wilcox@Sun.COM 					break;
28789263SSean.Wilcox@Sun.COM 
28799263SSean.Wilcox@Sun.COM 				default:
2880*9765SSean.Wilcox@Sun.COM 					bad_fail("scf_property_get_value", ret);
28819263SSean.Wilcox@Sun.COM 				}
28829263SSean.Wilcox@Sun.COM 
28839263SSean.Wilcox@Sun.COM 				(void) strcpy(cip->vbuf, ":default");
28849263SSean.Wilcox@Sun.COM 			} else {
28859263SSean.Wilcox@Sun.COM 				ret = scf_value_get_astring(val, cip->vbuf,
28869263SSean.Wilcox@Sun.COM 				    cip->vbuf_sz);
28879263SSean.Wilcox@Sun.COM 				assert(ret != -1);
28889263SSean.Wilcox@Sun.COM 			}
28899263SSean.Wilcox@Sun.COM 
28909263SSean.Wilcox@Sun.COM 			mc_used++;
28919263SSean.Wilcox@Sun.COM 		} else {
28929263SSean.Wilcox@Sun.COM 			(void) strcpy(cip->vbuf, ":default");
28930Sstevel@tonic-gate 		}
28940Sstevel@tonic-gate 
28950Sstevel@tonic-gate 		switch (ret = get_projid(cip->vbuf, cip)) {
28960Sstevel@tonic-gate 		case 0:
28970Sstevel@tonic-gate 			break;
28980Sstevel@tonic-gate 
28990Sstevel@tonic-gate 		case ENOMEM:
2900*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret, "Out of memory.");
29010Sstevel@tonic-gate 			goto out;
29020Sstevel@tonic-gate 
29030Sstevel@tonic-gate 		case ENOENT:
2904*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret,
2905*9765SSean.Wilcox@Sun.COM 			    "Missing passwd or project entry for \"%s\".",
2906*9765SSean.Wilcox@Sun.COM 			    cip->vbuf);
29070Sstevel@tonic-gate 			goto out;
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 		case EIO:
2910*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret, "I/O error.");
29110Sstevel@tonic-gate 			goto out;
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate 		case EMFILE:
29140Sstevel@tonic-gate 		case ENFILE:
2915*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret,
2916*9765SSean.Wilcox@Sun.COM 			    "Out of file descriptors.");
29170Sstevel@tonic-gate 			goto out;
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 		case -1:
2920*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret,
2921*9765SSean.Wilcox@Sun.COM 			    "Name service switch is misconfigured.");
29220Sstevel@tonic-gate 			goto out;
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate 		case ERANGE:
2925*9765SSean.Wilcox@Sun.COM 		case E2BIG:
2926*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret,
2927*9765SSean.Wilcox@Sun.COM 			    "Project ID \"%s\" too big.", cip->vbuf);
29280Sstevel@tonic-gate 			goto out;
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate 		case EINVAL:
2931*9765SSean.Wilcox@Sun.COM 			err = mc_error_create(err, ret,
2932*9765SSean.Wilcox@Sun.COM 			    "Project ID \"%s\" is invalid.", cip->vbuf);
29330Sstevel@tonic-gate 			goto out;
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate 		default:
29360Sstevel@tonic-gate 			bad_fail("get_projid", ret);
29370Sstevel@tonic-gate 		}
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate 		/* get resource pool */
29409263SSean.Wilcox@Sun.COM 		if ((methpg != NULL && scf_pg_get_property(methpg,
29419263SSean.Wilcox@Sun.COM 		    SCF_PROPERTY_RESOURCE_POOL, prop) == SCF_SUCCESS) ||
29429263SSean.Wilcox@Sun.COM 		    (instpg != NULL && scf_pg_get_property(instpg,
29439263SSean.Wilcox@Sun.COM 		    SCF_PROPERTY_RESOURCE_POOL, prop) == SCF_SUCCESS)) {
29449263SSean.Wilcox@Sun.COM 			if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
2945*9765SSean.Wilcox@Sun.COM 				ret = scf_error();
2946*9765SSean.Wilcox@Sun.COM 				switch (ret) {
29479263SSean.Wilcox@Sun.COM 				case SCF_ERROR_CONNECTION_BROKEN:
2948*9765SSean.Wilcox@Sun.COM 					err = mc_error_create(err, ret,
2949*9765SSean.Wilcox@Sun.COM 					    RCBROKEN);
29509263SSean.Wilcox@Sun.COM 					break;
29519263SSean.Wilcox@Sun.COM 
29529263SSean.Wilcox@Sun.COM 				case SCF_ERROR_CONSTRAINT_VIOLATED:
2953*9765SSean.Wilcox@Sun.COM 					err = mc_error_create(err, ret,
2954*9765SSean.Wilcox@Sun.COM 					    "\"%s\" property has multiple "
2955*9765SSean.Wilcox@Sun.COM 					    "values.",
2956*9765SSean.Wilcox@Sun.COM 					    SCF_PROPERTY_RESOURCE_POOL);
29579263SSean.Wilcox@Sun.COM 					break;
29589263SSean.Wilcox@Sun.COM 
29599263SSean.Wilcox@Sun.COM 				case SCF_ERROR_NOT_FOUND:
2960*9765SSean.Wilcox@Sun.COM 					err = mc_error_create(err, ret,
2961*9765SSean.Wilcox@Sun.COM 					    "\"%s\" property has no "
2962*9765SSean.Wilcox@Sun.COM 					    "values.",
2963*9765SSean.Wilcox@Sun.COM 					    SCF_PROPERTY_RESOURCE_POOL);
29649263SSean.Wilcox@Sun.COM 					break;
29659263SSean.Wilcox@Sun.COM 
29669263SSean.Wilcox@Sun.COM 				default:
2967*9765SSean.Wilcox@Sun.COM 					bad_fail("scf_property_get_value", ret);
29689263SSean.Wilcox@Sun.COM 				}
29699263SSean.Wilcox@Sun.COM 
29709263SSean.Wilcox@Sun.COM 				(void) strcpy(cip->vbuf, ":default");
29719263SSean.Wilcox@Sun.COM 			} else {
29729263SSean.Wilcox@Sun.COM 				ret = scf_value_get_astring(val, cip->vbuf,
29739263SSean.Wilcox@Sun.COM 				    cip->vbuf_sz);
29749263SSean.Wilcox@Sun.COM 				assert(ret != -1);
29759263SSean.Wilcox@Sun.COM 			}
29769263SSean.Wilcox@Sun.COM 
29779263SSean.Wilcox@Sun.COM 			mc_used++;
29789263SSean.Wilcox@Sun.COM 		} else {
2979*9765SSean.Wilcox@Sun.COM 			ret = scf_error();
2980*9765SSean.Wilcox@Sun.COM 			switch (ret) {
29819263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_FOUND:
29829263SSean.Wilcox@Sun.COM 				/* okay if missing. */
29839263SSean.Wilcox@Sun.COM 				(void) strcpy(cip->vbuf, ":default");
29849263SSean.Wilcox@Sun.COM 				break;
29859263SSean.Wilcox@Sun.COM 
29869263SSean.Wilcox@Sun.COM 			case SCF_ERROR_CONNECTION_BROKEN:
2987*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err, ret, RCBROKEN);
29889263SSean.Wilcox@Sun.COM 				goto out;
29899263SSean.Wilcox@Sun.COM 
29909263SSean.Wilcox@Sun.COM 			case SCF_ERROR_DELETED:
2991*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err, ret,
2992*9765SSean.Wilcox@Sun.COM 				    "property group could not be found.");
29939263SSean.Wilcox@Sun.COM 				goto out;
29949263SSean.Wilcox@Sun.COM 
29959263SSean.Wilcox@Sun.COM 			case SCF_ERROR_HANDLE_MISMATCH:
29969263SSean.Wilcox@Sun.COM 			case SCF_ERROR_INVALID_ARGUMENT:
29979263SSean.Wilcox@Sun.COM 			case SCF_ERROR_NOT_SET:
29989263SSean.Wilcox@Sun.COM 			default:
2999*9765SSean.Wilcox@Sun.COM 				bad_fail("scf_pg_get_property", ret);
30009263SSean.Wilcox@Sun.COM 			}
30010Sstevel@tonic-gate 		}
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 		if (strcmp(cip->vbuf, ":default") != 0) {
30040Sstevel@tonic-gate 			cip->resource_pool = strdup(cip->vbuf);
30050Sstevel@tonic-gate 			if (cip->resource_pool == NULL) {
3006*9765SSean.Wilcox@Sun.COM 				err = mc_error_create(err, ENOMEM, ALLOCFAIL);
30070Sstevel@tonic-gate 				goto out;
30080Sstevel@tonic-gate 			}
30090Sstevel@tonic-gate 		}
30100Sstevel@tonic-gate 	}
30110Sstevel@tonic-gate 
30129263SSean.Wilcox@Sun.COM 	/*
30139263SSean.Wilcox@Sun.COM 	 * A method_context was not used for any configurable
30149263SSean.Wilcox@Sun.COM 	 * elements or attributes, so reset and use the simple
30159263SSean.Wilcox@Sun.COM 	 * defaults that provide historic init behavior.
30169263SSean.Wilcox@Sun.COM 	 */
30179263SSean.Wilcox@Sun.COM 	if (mc_used == 0) {
30189263SSean.Wilcox@Sun.COM 		(void) memset(cip, 0, sizeof (*cip));
30199263SSean.Wilcox@Sun.COM 		cip->uid = 0;
30209263SSean.Wilcox@Sun.COM 		cip->gid = 0;
30219263SSean.Wilcox@Sun.COM 		cip->euid = (uid_t)-1;
30229263SSean.Wilcox@Sun.COM 		cip->egid = (gid_t)-1;
30239263SSean.Wilcox@Sun.COM 	}
30249263SSean.Wilcox@Sun.COM 
30250Sstevel@tonic-gate 	*mcpp = cip;
30260Sstevel@tonic-gate 
30270Sstevel@tonic-gate out:
30280Sstevel@tonic-gate 	(void) scf_value_destroy(val);
30290Sstevel@tonic-gate 	scf_property_destroy(prop);
30300Sstevel@tonic-gate 	scf_pg_destroy(instpg);
30310Sstevel@tonic-gate 	scf_pg_destroy(methpg);
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate 	if (cip->pwbuf != NULL)
30340Sstevel@tonic-gate 		free(cip->pwbuf);
30350Sstevel@tonic-gate 	free(cip->vbuf);
30360Sstevel@tonic-gate 
3037*9765SSean.Wilcox@Sun.COM 	if (err->type != 0) {
30380Sstevel@tonic-gate 		restarter_free_method_context(cip);
3039*9765SSean.Wilcox@Sun.COM 	} else {
3040*9765SSean.Wilcox@Sun.COM 		restarter_mc_error_destroy(err);
3041*9765SSean.Wilcox@Sun.COM 		err = NULL;
3042*9765SSean.Wilcox@Sun.COM 	}
3043*9765SSean.Wilcox@Sun.COM 
3044*9765SSean.Wilcox@Sun.COM 	return (err);
30450Sstevel@tonic-gate }
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate /*
30480Sstevel@tonic-gate  * Modify the current process per the given method_context.  On success, returns
30490Sstevel@tonic-gate  * 0.  Note that the environment is not modified by this function to include the
30500Sstevel@tonic-gate  * environment variables in cip->env.
30510Sstevel@tonic-gate  *
30520Sstevel@tonic-gate  * On failure, sets *fp to NULL or the name of the function which failed,
30530Sstevel@tonic-gate  * and returns one of the following error codes.  The words in parentheses are
30540Sstevel@tonic-gate  * the values to which *fp may be set for the error case.
30550Sstevel@tonic-gate  *   ENOMEM - malloc() failed
30560Sstevel@tonic-gate  *   EIO - an I/O error occurred (getpwuid_r, chdir)
30570Sstevel@tonic-gate  *   EMFILE - process is out of file descriptors (getpwuid_r)
30580Sstevel@tonic-gate  *   ENFILE - system is out of file handles (getpwuid_r)
30590Sstevel@tonic-gate  *   EINVAL - gid or egid is out of range (setregid)
30600Sstevel@tonic-gate  *	      ngroups is too big (setgroups)
30610Sstevel@tonic-gate  *	      project's project id is bad (setproject)
30620Sstevel@tonic-gate  *	      uid or euid is out of range (setreuid)
30631712Srm88369  *	      poolname is invalid (pool_set_binding)
30640Sstevel@tonic-gate  *   EPERM - insufficient privilege (setregid, initgroups, setgroups, setppriv,
30650Sstevel@tonic-gate  *	         setproject, setreuid, settaskid)
30660Sstevel@tonic-gate  *   ENOENT - uid has a passwd entry but no shadow entry
30670Sstevel@tonic-gate  *	      working_dir does not exist (chdir)
30680Sstevel@tonic-gate  *	      uid has no passwd entry
30690Sstevel@tonic-gate  *	      the pool could not be found (pool_set_binding)
30700Sstevel@tonic-gate  *   EFAULT - lpriv_set or priv_set has a bad address (setppriv)
30710Sstevel@tonic-gate  *	      working_dir has a bad address (chdir)
30720Sstevel@tonic-gate  *   EACCES - could not access working_dir (chdir)
30730Sstevel@tonic-gate  *	      in a TASK_FINAL task (setproject, settaskid)
30740Sstevel@tonic-gate  *	      no resource pool accepting default binding exists (setproject)
30750Sstevel@tonic-gate  *   ELOOP - too many symbolic links in working_dir (chdir)
30760Sstevel@tonic-gate  *   ENAMETOOLONG - working_dir is too long (chdir)
30770Sstevel@tonic-gate  *   ENOLINK - working_dir is on an inaccessible remote machine (chdir)
30780Sstevel@tonic-gate  *   ENOTDIR - working_dir is not a directory (chdir)
30790Sstevel@tonic-gate  *   ESRCH - uid is not a user of project (setproject)
30800Sstevel@tonic-gate  *	     project is invalid (setproject)
30810Sstevel@tonic-gate  *	     the resource pool specified for project is unknown (setproject)
30820Sstevel@tonic-gate  *   EBADF - the configuration for the pool is invalid (pool_set_binding)
30830Sstevel@tonic-gate  *   -1 - core_set_process_path() failed (core_set_process_path)
30840Sstevel@tonic-gate  *	  a resource control assignment failed (setproject)
30850Sstevel@tonic-gate  *	  a system error occurred during pool_set_binding (pool_set_binding)
30860Sstevel@tonic-gate  */
30870Sstevel@tonic-gate int
30880Sstevel@tonic-gate restarter_set_method_context(struct method_context *cip, const char **fp)
30890Sstevel@tonic-gate {
30900Sstevel@tonic-gate 	pid_t mypid = -1;
30910Sstevel@tonic-gate 	int r, ret;
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate 	cip->pwbuf = NULL;
30940Sstevel@tonic-gate 	*fp = NULL;
30950Sstevel@tonic-gate 
30964321Scasper 	if (cip->gid != (gid_t)-1) {
30970Sstevel@tonic-gate 		if (setregid(cip->gid,
30984321Scasper 		    cip->egid != (gid_t)-1 ? cip->egid : cip->gid) != 0) {
30990Sstevel@tonic-gate 			*fp = "setregid";
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate 			ret = errno;
31020Sstevel@tonic-gate 			assert(ret == EINVAL || ret == EPERM);
31030Sstevel@tonic-gate 			goto out;
31040Sstevel@tonic-gate 		}
31050Sstevel@tonic-gate 	} else {
31060Sstevel@tonic-gate 		if (cip->pwbuf == NULL) {
31070Sstevel@tonic-gate 			switch (ret = lookup_pwd(cip)) {
31080Sstevel@tonic-gate 			case 0:
31090Sstevel@tonic-gate 				break;
31100Sstevel@tonic-gate 
31110Sstevel@tonic-gate 			case ENOMEM:
31120Sstevel@tonic-gate 			case ENOENT:
31130Sstevel@tonic-gate 				*fp = NULL;
31140Sstevel@tonic-gate 				goto out;
31150Sstevel@tonic-gate 
31160Sstevel@tonic-gate 			case EIO:
31170Sstevel@tonic-gate 			case EMFILE:
31180Sstevel@tonic-gate 			case ENFILE:
31190Sstevel@tonic-gate 				*fp = "getpwuid_r";
31200Sstevel@tonic-gate 				goto out;
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate 			default:
31230Sstevel@tonic-gate 				bad_fail("lookup_pwd", ret);
31240Sstevel@tonic-gate 			}
31250Sstevel@tonic-gate 		}
31260Sstevel@tonic-gate 
31270Sstevel@tonic-gate 		if (setregid(cip->pwd.pw_gid,
31284321Scasper 		    cip->egid != (gid_t)-1 ?
31294321Scasper 		    cip->egid : cip->pwd.pw_gid) != 0) {
31300Sstevel@tonic-gate 			*fp = "setregid";
31310Sstevel@tonic-gate 
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 
31380Sstevel@tonic-gate 	if (cip->ngroups == -1) {
31390Sstevel@tonic-gate 		if (cip->pwbuf == NULL) {
31400Sstevel@tonic-gate 			switch (ret = lookup_pwd(cip)) {
31410Sstevel@tonic-gate 			case 0:
31420Sstevel@tonic-gate 				break;
31430Sstevel@tonic-gate 
31440Sstevel@tonic-gate 			case ENOMEM:
31450Sstevel@tonic-gate 			case ENOENT:
31460Sstevel@tonic-gate 				*fp = NULL;
31470Sstevel@tonic-gate 				goto out;
31480Sstevel@tonic-gate 
31490Sstevel@tonic-gate 			case EIO:
31500Sstevel@tonic-gate 			case EMFILE:
31510Sstevel@tonic-gate 			case ENFILE:
31520Sstevel@tonic-gate 				*fp = "getpwuid_r";
31530Sstevel@tonic-gate 				goto out;
31540Sstevel@tonic-gate 
31550Sstevel@tonic-gate 			default:
31560Sstevel@tonic-gate 				bad_fail("lookup_pwd", ret);
31570Sstevel@tonic-gate 			}
31580Sstevel@tonic-gate 		}
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate 		/* Ok if cip->gid == -1 */
31610Sstevel@tonic-gate 		if (initgroups(cip->pwd.pw_name, cip->gid) != 0) {
31620Sstevel@tonic-gate 			*fp = "initgroups";
31630Sstevel@tonic-gate 			ret = errno;
31640Sstevel@tonic-gate 			assert(ret == EPERM);
31650Sstevel@tonic-gate 			goto out;
31660Sstevel@tonic-gate 		}
31670Sstevel@tonic-gate 	} else if (cip->ngroups > 0 &&
31680Sstevel@tonic-gate 	    setgroups(cip->ngroups, cip->groups) != 0) {
31690Sstevel@tonic-gate 		*fp = "setgroups";
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate 		ret = errno;
31720Sstevel@tonic-gate 		assert(ret == EINVAL || ret == EPERM);
31730Sstevel@tonic-gate 		goto out;
31740Sstevel@tonic-gate 	}
31750Sstevel@tonic-gate 
31760Sstevel@tonic-gate 	if (cip->corefile_pattern != NULL) {
31770Sstevel@tonic-gate 		mypid = getpid();
31780Sstevel@tonic-gate 
31790Sstevel@tonic-gate 		if (core_set_process_path(cip->corefile_pattern,
31800Sstevel@tonic-gate 		    strlen(cip->corefile_pattern) + 1, mypid) != 0) {
31810Sstevel@tonic-gate 			*fp = "core_set_process_path";
31820Sstevel@tonic-gate 			ret = -1;
31830Sstevel@tonic-gate 			goto out;
31840Sstevel@tonic-gate 		}
31850Sstevel@tonic-gate 	}
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 	if (restarter_rm_libs_loadable()) {
31880Sstevel@tonic-gate 		if (cip->project == NULL) {
31890Sstevel@tonic-gate 			if (settaskid(getprojid(), TASK_NORMAL) == -1) {
31900Sstevel@tonic-gate 				switch (errno) {
31910Sstevel@tonic-gate 				case EACCES:
31920Sstevel@tonic-gate 				case EPERM:
31930Sstevel@tonic-gate 					*fp = "settaskid";
31940Sstevel@tonic-gate 					ret = errno;
31950Sstevel@tonic-gate 					goto out;
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate 				case EINVAL:
31980Sstevel@tonic-gate 				default:
31990Sstevel@tonic-gate 					bad_fail("settaskid", errno);
32000Sstevel@tonic-gate 				}
32010Sstevel@tonic-gate 			}
32020Sstevel@tonic-gate 		} else {
32030Sstevel@tonic-gate 			switch (ret = lookup_pwd(cip)) {
32040Sstevel@tonic-gate 			case 0:
32050Sstevel@tonic-gate 				break;
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 			case ENOMEM:
32080Sstevel@tonic-gate 			case ENOENT:
32090Sstevel@tonic-gate 				*fp = NULL;
32100Sstevel@tonic-gate 				goto out;
32110Sstevel@tonic-gate 
32120Sstevel@tonic-gate 			case EIO:
32130Sstevel@tonic-gate 			case EMFILE:
32140Sstevel@tonic-gate 			case ENFILE:
32150Sstevel@tonic-gate 				*fp = "getpwuid_r";
32160Sstevel@tonic-gate 				goto out;
32170Sstevel@tonic-gate 
32180Sstevel@tonic-gate 			default:
32190Sstevel@tonic-gate 				bad_fail("lookup_pwd", ret);
32200Sstevel@tonic-gate 			}
32210Sstevel@tonic-gate 
32220Sstevel@tonic-gate 			*fp = "setproject";
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate 			switch (setproject(cip->project, cip->pwd.pw_name,
32250Sstevel@tonic-gate 			    TASK_NORMAL)) {
32260Sstevel@tonic-gate 			case 0:
32270Sstevel@tonic-gate 				break;
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate 			case SETPROJ_ERR_TASK:
32300Sstevel@tonic-gate 			case SETPROJ_ERR_POOL:
32310Sstevel@tonic-gate 				ret = errno;
32320Sstevel@tonic-gate 				goto out;
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 			default:
32350Sstevel@tonic-gate 				ret = -1;
32360Sstevel@tonic-gate 				goto out;
32370Sstevel@tonic-gate 			}
32380Sstevel@tonic-gate 		}
32390Sstevel@tonic-gate 
32400Sstevel@tonic-gate 		if (cip->resource_pool != NULL) {
32410Sstevel@tonic-gate 			if (mypid == -1)
32420Sstevel@tonic-gate 				mypid = getpid();
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate 			*fp = "pool_set_binding";
32450Sstevel@tonic-gate 
32460Sstevel@tonic-gate 			if (pool_set_binding(cip->resource_pool, P_PID,
32470Sstevel@tonic-gate 			    mypid) != PO_SUCCESS) {
32480Sstevel@tonic-gate 				switch (pool_error()) {
32491712Srm88369 				case POE_INVALID_SEARCH:
32501712Srm88369 					ret = ENOENT;
32511712Srm88369 					break;
32521712Srm88369 
32530Sstevel@tonic-gate 				case POE_BADPARAM:
32541712Srm88369 					ret = EINVAL;
32550Sstevel@tonic-gate 					break;
32560Sstevel@tonic-gate 
32570Sstevel@tonic-gate 				case POE_INVALID_CONF:
32580Sstevel@tonic-gate 					ret = EBADF;
32590Sstevel@tonic-gate 					break;
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate 				case POE_SYSTEM:
32620Sstevel@tonic-gate 					ret = -1;
32630Sstevel@tonic-gate 					break;
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 				default:
32660Sstevel@tonic-gate 					bad_fail("pool_set_binding",
32670Sstevel@tonic-gate 					    pool_error());
32680Sstevel@tonic-gate 				}
32690Sstevel@tonic-gate 
32700Sstevel@tonic-gate 				goto out;
32710Sstevel@tonic-gate 			}
32720Sstevel@tonic-gate 		}
32730Sstevel@tonic-gate 	}
32740Sstevel@tonic-gate 
32750Sstevel@tonic-gate 	/*
32763879Svp157776 	 * Now, we have to assume our ID. If the UID is 0, we want it to be
32773879Svp157776 	 * privilege-aware, otherwise the limit set gets used instead of E/P.
32780Sstevel@tonic-gate 	 * We can do this by setting P as well, which keeps
32790Sstevel@tonic-gate 	 * PA status (see priv_can_clear_PA()).
32800Sstevel@tonic-gate 	 */
32810Sstevel@tonic-gate 
32829263SSean.Wilcox@Sun.COM 	*fp = "setppriv";
32839263SSean.Wilcox@Sun.COM 
32849263SSean.Wilcox@Sun.COM 	if (cip->lpriv_set != NULL) {
32859263SSean.Wilcox@Sun.COM 		if (setppriv(PRIV_SET, PRIV_LIMIT, cip->lpriv_set) != 0) {
32869263SSean.Wilcox@Sun.COM 			ret = errno;
32879263SSean.Wilcox@Sun.COM 			assert(ret == EFAULT || ret == EPERM);
32889263SSean.Wilcox@Sun.COM 			goto out;
32899263SSean.Wilcox@Sun.COM 		}
32909263SSean.Wilcox@Sun.COM 	}
32919263SSean.Wilcox@Sun.COM 	if (cip->priv_set != NULL) {
32929263SSean.Wilcox@Sun.COM 		if (setppriv(PRIV_SET, PRIV_INHERITABLE, cip->priv_set) != 0) {
32939263SSean.Wilcox@Sun.COM 			ret = errno;
32949263SSean.Wilcox@Sun.COM 			assert(ret == EFAULT || ret == EPERM);
32959263SSean.Wilcox@Sun.COM 			goto out;
32969263SSean.Wilcox@Sun.COM 		}
32979263SSean.Wilcox@Sun.COM 	}
32989263SSean.Wilcox@Sun.COM 
32999263SSean.Wilcox@Sun.COM 	/*
33009263SSean.Wilcox@Sun.COM 	 * If the limit privset is already set, then must be privilege
33019263SSean.Wilcox@Sun.COM 	 * aware.  Otherwise, don't assume anything, and force privilege
33029263SSean.Wilcox@Sun.COM 	 * aware status.
33039263SSean.Wilcox@Sun.COM 	 */
33049263SSean.Wilcox@Sun.COM 
33059263SSean.Wilcox@Sun.COM 	if (cip->lpriv_set == NULL && cip->priv_set != NULL) {
33069263SSean.Wilcox@Sun.COM 		ret = setpflags(PRIV_AWARE, 1);
33079263SSean.Wilcox@Sun.COM 		assert(ret == 0);
33089263SSean.Wilcox@Sun.COM 	}
33099263SSean.Wilcox@Sun.COM 
33100Sstevel@tonic-gate 	*fp = "setreuid";
33114321Scasper 	if (setreuid(cip->uid,
33124321Scasper 	    cip->euid != (uid_t)-1 ? cip->euid : cip->uid) != 0) {
33130Sstevel@tonic-gate 		ret = errno;
33140Sstevel@tonic-gate 		assert(ret == EINVAL || ret == EPERM);
33150Sstevel@tonic-gate 		goto out;
33160Sstevel@tonic-gate 	}
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 	*fp = "setppriv";
33190Sstevel@tonic-gate 	if (cip->priv_set != NULL) {
33200Sstevel@tonic-gate 		if (setppriv(PRIV_SET, PRIV_PERMITTED, cip->priv_set) != 0) {
33210Sstevel@tonic-gate 			ret = errno;
33220Sstevel@tonic-gate 			assert(ret == EFAULT || ret == EPERM);
33230Sstevel@tonic-gate 			goto out;
33240Sstevel@tonic-gate 		}
33250Sstevel@tonic-gate 	}
33260Sstevel@tonic-gate 
33273879Svp157776 	/*
33283879Svp157776 	 * The last thing to do is chdir to the specified working directory.
33293879Svp157776 	 * This should come after the uid switching as only the user might
33303879Svp157776 	 * have access to the specified directory.
33313879Svp157776 	 */
33323879Svp157776 	if (cip->working_dir != NULL) {
33335040Swesolows 		do {
33343879Svp157776 			r = chdir(cip->working_dir);
33355040Swesolows 		} while (r != 0 && errno == EINTR);
33363879Svp157776 		if (r != 0) {
33373879Svp157776 			*fp = "chdir";
33383879Svp157776 			ret = errno;
33393879Svp157776 			goto out;
33403879Svp157776 		}
33413879Svp157776 	}
33423879Svp157776 
33430Sstevel@tonic-gate 	ret = 0;
33440Sstevel@tonic-gate out:
33450Sstevel@tonic-gate 	free(cip->pwbuf);
33460Sstevel@tonic-gate 	cip->pwbuf = NULL;
33470Sstevel@tonic-gate 	return (ret);
33480Sstevel@tonic-gate }
33490Sstevel@tonic-gate 
33500Sstevel@tonic-gate void
33510Sstevel@tonic-gate restarter_free_method_context(struct method_context *mcp)
33520Sstevel@tonic-gate {
33530Sstevel@tonic-gate 	size_t i;
33540Sstevel@tonic-gate 
33550Sstevel@tonic-gate 	if (mcp->lpriv_set != NULL)
33560Sstevel@tonic-gate 		priv_freeset(mcp->lpriv_set);
33570Sstevel@tonic-gate 	if (mcp->priv_set != NULL)
33580Sstevel@tonic-gate 		priv_freeset(mcp->priv_set);
33590Sstevel@tonic-gate 
33600Sstevel@tonic-gate 	if (mcp->env != NULL) {
33610Sstevel@tonic-gate 		for (i = 0; i < mcp->env_sz; i++)
33620Sstevel@tonic-gate 			free(mcp->env[i]);
33630Sstevel@tonic-gate 		free(mcp->env);
33640Sstevel@tonic-gate 	}
33650Sstevel@tonic-gate 
33660Sstevel@tonic-gate 	free(mcp->working_dir);
33670Sstevel@tonic-gate 	free(mcp->corefile_pattern);
33680Sstevel@tonic-gate 	free(mcp->project);
33690Sstevel@tonic-gate 	free(mcp->resource_pool);
33700Sstevel@tonic-gate 	free(mcp);
33710Sstevel@tonic-gate }
33720Sstevel@tonic-gate 
33730Sstevel@tonic-gate /*
33740Sstevel@tonic-gate  * Method keyword functions
33750Sstevel@tonic-gate  */
33760Sstevel@tonic-gate 
33770Sstevel@tonic-gate int
33780Sstevel@tonic-gate restarter_is_null_method(const char *meth)
33790Sstevel@tonic-gate {
33800Sstevel@tonic-gate 	return (strcmp(meth, MKW_TRUE) == 0);
33810Sstevel@tonic-gate }
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate static int
33840Sstevel@tonic-gate is_kill_method(const char *method, const char *kill_str,
33850Sstevel@tonic-gate     size_t kill_str_len)
33860Sstevel@tonic-gate {
33870Sstevel@tonic-gate 	const char *cp;
33880Sstevel@tonic-gate 	int sig;
33890Sstevel@tonic-gate 
33900Sstevel@tonic-gate 	if (strncmp(method, kill_str, kill_str_len) != 0 ||
33910Sstevel@tonic-gate 	    (method[kill_str_len] != '\0' &&
33920Sstevel@tonic-gate 	    !isspace(method[kill_str_len])))
33930Sstevel@tonic-gate 		return (-1);
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 	cp = method + kill_str_len;
33960Sstevel@tonic-gate 	while (*cp != '\0' && isspace(*cp))
33970Sstevel@tonic-gate 		++cp;
33980Sstevel@tonic-gate 
33990Sstevel@tonic-gate 	if (*cp == '\0')
34000Sstevel@tonic-gate 		return (SIGTERM);
34010Sstevel@tonic-gate 
34020Sstevel@tonic-gate 	if (*cp != '-')
34030Sstevel@tonic-gate 		return (-1);
34040Sstevel@tonic-gate 
34050Sstevel@tonic-gate 	return (str2sig(cp + 1, &sig) == 0 ? sig : -1);
34060Sstevel@tonic-gate }
34070Sstevel@tonic-gate 
34080Sstevel@tonic-gate int
34090Sstevel@tonic-gate restarter_is_kill_proc_method(const char *method)
34100Sstevel@tonic-gate {
34110Sstevel@tonic-gate 	return (is_kill_method(method, MKW_KILL_PROC,
34120Sstevel@tonic-gate 	    sizeof (MKW_KILL_PROC) - 1));
34130Sstevel@tonic-gate }
34140Sstevel@tonic-gate 
34150Sstevel@tonic-gate int
34160Sstevel@tonic-gate restarter_is_kill_method(const char *method)
34170Sstevel@tonic-gate {
34180Sstevel@tonic-gate 	return (is_kill_method(method, MKW_KILL, sizeof (MKW_KILL) - 1));
34190Sstevel@tonic-gate }
34200Sstevel@tonic-gate 
34210Sstevel@tonic-gate /*
34220Sstevel@tonic-gate  * Stubs for now.
34230Sstevel@tonic-gate  */
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate /* ARGSUSED */
34260Sstevel@tonic-gate int
34270Sstevel@tonic-gate restarter_event_get_enabled(restarter_event_t *e)
34280Sstevel@tonic-gate {
34290Sstevel@tonic-gate 	return (-1);
34300Sstevel@tonic-gate }
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate /* ARGSUSED */
34330Sstevel@tonic-gate uint64_t
34340Sstevel@tonic-gate restarter_event_get_seq(restarter_event_t *e)
34350Sstevel@tonic-gate {
34360Sstevel@tonic-gate 	return (-1);
34370Sstevel@tonic-gate }
34380Sstevel@tonic-gate 
34390Sstevel@tonic-gate /* ARGSUSED */
34400Sstevel@tonic-gate void
34410Sstevel@tonic-gate restarter_event_get_time(restarter_event_t *e, hrtime_t *time)
34420Sstevel@tonic-gate {
34430Sstevel@tonic-gate }
34448823STruong.Q.Nguyen@Sun.COM 
34458823STruong.Q.Nguyen@Sun.COM /*
34468823STruong.Q.Nguyen@Sun.COM  * Check for and validate fmri specified in restarter_actions/auxiliary_fmri
34478823STruong.Q.Nguyen@Sun.COM  * 0 - Success
34488823STruong.Q.Nguyen@Sun.COM  * 1 - Failure
34498823STruong.Q.Nguyen@Sun.COM  */
34508823STruong.Q.Nguyen@Sun.COM int
34518823STruong.Q.Nguyen@Sun.COM restarter_inst_validate_ractions_aux_fmri(scf_instance_t *inst)
34528823STruong.Q.Nguyen@Sun.COM {
34538823STruong.Q.Nguyen@Sun.COM 	scf_handle_t *h;
34548823STruong.Q.Nguyen@Sun.COM 	scf_propertygroup_t *pg;
34558823STruong.Q.Nguyen@Sun.COM 	scf_property_t *prop;
34568823STruong.Q.Nguyen@Sun.COM 	scf_value_t *val;
34578823STruong.Q.Nguyen@Sun.COM 	char *aux_fmri;
34588823STruong.Q.Nguyen@Sun.COM 	size_t size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
34598823STruong.Q.Nguyen@Sun.COM 	int ret = 1;
34608823STruong.Q.Nguyen@Sun.COM 
34618823STruong.Q.Nguyen@Sun.COM 	if ((aux_fmri = malloc(size)) == NULL)
34628823STruong.Q.Nguyen@Sun.COM 		return (1);
34638823STruong.Q.Nguyen@Sun.COM 
34648823STruong.Q.Nguyen@Sun.COM 	h = scf_instance_handle(inst);
34658823STruong.Q.Nguyen@Sun.COM 
34668823STruong.Q.Nguyen@Sun.COM 	pg = scf_pg_create(h);
34678823STruong.Q.Nguyen@Sun.COM 	prop = scf_property_create(h);
34688823STruong.Q.Nguyen@Sun.COM 	val = scf_value_create(h);
34698823STruong.Q.Nguyen@Sun.COM 	if (pg == NULL || prop == NULL || val == NULL)
34708823STruong.Q.Nguyen@Sun.COM 		goto out;
34718823STruong.Q.Nguyen@Sun.COM 
34728823STruong.Q.Nguyen@Sun.COM 	if (instance_get_or_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
34738823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER_ACTIONS_TYPE, SCF_PG_RESTARTER_ACTIONS_FLAGS,
34748823STruong.Q.Nguyen@Sun.COM 	    pg) != SCF_SUCCESS)
34758823STruong.Q.Nguyen@Sun.COM 		goto out;
34768823STruong.Q.Nguyen@Sun.COM 
34778823STruong.Q.Nguyen@Sun.COM 	if (get_astring_val(pg, SCF_PROPERTY_AUX_FMRI, aux_fmri, size,
34788823STruong.Q.Nguyen@Sun.COM 	    prop, val) != SCF_SUCCESS)
34798823STruong.Q.Nguyen@Sun.COM 		goto out;
34808823STruong.Q.Nguyen@Sun.COM 
34818823STruong.Q.Nguyen@Sun.COM 	if (scf_parse_fmri(aux_fmri, NULL, NULL, NULL, NULL, NULL,
34828823STruong.Q.Nguyen@Sun.COM 	    NULL) != SCF_SUCCESS)
34838823STruong.Q.Nguyen@Sun.COM 		goto out;
34848823STruong.Q.Nguyen@Sun.COM 
34858823STruong.Q.Nguyen@Sun.COM 	ret = 0;
34868823STruong.Q.Nguyen@Sun.COM 
34878823STruong.Q.Nguyen@Sun.COM out:
34888823STruong.Q.Nguyen@Sun.COM 	free(aux_fmri);
34898823STruong.Q.Nguyen@Sun.COM 	scf_value_destroy(val);
34908823STruong.Q.Nguyen@Sun.COM 	scf_property_destroy(prop);
34918823STruong.Q.Nguyen@Sun.COM 	scf_pg_destroy(pg);
34928823STruong.Q.Nguyen@Sun.COM 	return (ret);
34938823STruong.Q.Nguyen@Sun.COM }
34948823STruong.Q.Nguyen@Sun.COM 
34958823STruong.Q.Nguyen@Sun.COM /*
34968823STruong.Q.Nguyen@Sun.COM  * Get instance's boolean value in restarter_actions/auxiliary_tty
34978823STruong.Q.Nguyen@Sun.COM  * Return -1 on failure
34988823STruong.Q.Nguyen@Sun.COM  */
34998823STruong.Q.Nguyen@Sun.COM int
35008823STruong.Q.Nguyen@Sun.COM restarter_inst_ractions_from_tty(scf_instance_t *inst)
35018823STruong.Q.Nguyen@Sun.COM {
35028823STruong.Q.Nguyen@Sun.COM 	scf_handle_t *h;
35038823STruong.Q.Nguyen@Sun.COM 	scf_propertygroup_t *pg;
35048823STruong.Q.Nguyen@Sun.COM 	scf_property_t *prop;
35058823STruong.Q.Nguyen@Sun.COM 	scf_value_t *val;
35068823STruong.Q.Nguyen@Sun.COM 	uint8_t	has_tty;
35078823STruong.Q.Nguyen@Sun.COM 	int ret = -1;
35088823STruong.Q.Nguyen@Sun.COM 
35098823STruong.Q.Nguyen@Sun.COM 	h = scf_instance_handle(inst);
35108823STruong.Q.Nguyen@Sun.COM 	pg = scf_pg_create(h);
35118823STruong.Q.Nguyen@Sun.COM 	prop = scf_property_create(h);
35128823STruong.Q.Nguyen@Sun.COM 	val = scf_value_create(h);
35138823STruong.Q.Nguyen@Sun.COM 	if (pg == NULL || prop == NULL || val == NULL)
35148823STruong.Q.Nguyen@Sun.COM 		goto out;
35158823STruong.Q.Nguyen@Sun.COM 
35168823STruong.Q.Nguyen@Sun.COM 	if (instance_get_or_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
35178823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER_ACTIONS_TYPE, SCF_PG_RESTARTER_ACTIONS_FLAGS,
35188823STruong.Q.Nguyen@Sun.COM 	    pg) != SCF_SUCCESS)
35198823STruong.Q.Nguyen@Sun.COM 		goto out;
35208823STruong.Q.Nguyen@Sun.COM 
35218823STruong.Q.Nguyen@Sun.COM 	if (get_boolean_val(pg, SCF_PROPERTY_AUX_TTY, &has_tty, prop,
35228823STruong.Q.Nguyen@Sun.COM 	    val) != SCF_SUCCESS)
35238823STruong.Q.Nguyen@Sun.COM 		goto out;
35248823STruong.Q.Nguyen@Sun.COM 
35258823STruong.Q.Nguyen@Sun.COM 	ret = has_tty;
35268823STruong.Q.Nguyen@Sun.COM 
35278823STruong.Q.Nguyen@Sun.COM out:
35288823STruong.Q.Nguyen@Sun.COM 	scf_value_destroy(val);
35298823STruong.Q.Nguyen@Sun.COM 	scf_property_destroy(prop);
35308823STruong.Q.Nguyen@Sun.COM 	scf_pg_destroy(pg);
35318823STruong.Q.Nguyen@Sun.COM 	return (ret);
35328823STruong.Q.Nguyen@Sun.COM }
35338823STruong.Q.Nguyen@Sun.COM 
35348823STruong.Q.Nguyen@Sun.COM static int
35358823STruong.Q.Nguyen@Sun.COM restarter_inst_set_astring_prop(scf_instance_t *inst, const char *pgname,
35368823STruong.Q.Nguyen@Sun.COM     const char *pgtype, uint32_t pgflags, const char *pname, const char *str)
35378823STruong.Q.Nguyen@Sun.COM {
35388823STruong.Q.Nguyen@Sun.COM 	scf_handle_t *h;
35398823STruong.Q.Nguyen@Sun.COM 	scf_propertygroup_t *pg;
35408823STruong.Q.Nguyen@Sun.COM 	scf_transaction_t *t;
35418823STruong.Q.Nguyen@Sun.COM 	scf_transaction_entry_t *e;
35428823STruong.Q.Nguyen@Sun.COM 	scf_value_t *v;
35438823STruong.Q.Nguyen@Sun.COM 	int ret = 1, r;
35448823STruong.Q.Nguyen@Sun.COM 
35458823STruong.Q.Nguyen@Sun.COM 	h = scf_instance_handle(inst);
35468823STruong.Q.Nguyen@Sun.COM 
35478823STruong.Q.Nguyen@Sun.COM 	pg = scf_pg_create(h);
35488823STruong.Q.Nguyen@Sun.COM 	t = scf_transaction_create(h);
35498823STruong.Q.Nguyen@Sun.COM 	e = scf_entry_create(h);
35508823STruong.Q.Nguyen@Sun.COM 	v = scf_value_create(h);
35518823STruong.Q.Nguyen@Sun.COM 	if (pg == NULL || t == NULL || e == NULL || v == NULL)
35528823STruong.Q.Nguyen@Sun.COM 		goto out;
35538823STruong.Q.Nguyen@Sun.COM 
35548823STruong.Q.Nguyen@Sun.COM 	if (instance_get_or_add_pg(inst, pgname, pgtype, pgflags, pg))
35558823STruong.Q.Nguyen@Sun.COM 		goto out;
35568823STruong.Q.Nguyen@Sun.COM 
35578823STruong.Q.Nguyen@Sun.COM 	if (scf_value_set_astring(v, str) != SCF_SUCCESS)
35588823STruong.Q.Nguyen@Sun.COM 		goto out;
35598823STruong.Q.Nguyen@Sun.COM 
35608823STruong.Q.Nguyen@Sun.COM 	for (;;) {
35618823STruong.Q.Nguyen@Sun.COM 		if (scf_transaction_start(t, pg) != 0)
35628823STruong.Q.Nguyen@Sun.COM 			goto out;
35638823STruong.Q.Nguyen@Sun.COM 
35648823STruong.Q.Nguyen@Sun.COM 		if (tx_set_value(t, e, pname, SCF_TYPE_ASTRING, v) != 0)
35658823STruong.Q.Nguyen@Sun.COM 			goto out;
35668823STruong.Q.Nguyen@Sun.COM 
35678823STruong.Q.Nguyen@Sun.COM 		if ((r = scf_transaction_commit(t)) == 1)
35688823STruong.Q.Nguyen@Sun.COM 			break;
35698823STruong.Q.Nguyen@Sun.COM 
35708823STruong.Q.Nguyen@Sun.COM 		if (r == -1)
35718823STruong.Q.Nguyen@Sun.COM 			goto out;
35728823STruong.Q.Nguyen@Sun.COM 
35738823STruong.Q.Nguyen@Sun.COM 		scf_transaction_reset(t);
35748823STruong.Q.Nguyen@Sun.COM 		if (scf_pg_update(pg) == -1)
35758823STruong.Q.Nguyen@Sun.COM 			goto out;
35768823STruong.Q.Nguyen@Sun.COM 	}
35778823STruong.Q.Nguyen@Sun.COM 	ret = 0;
35788823STruong.Q.Nguyen@Sun.COM 
35798823STruong.Q.Nguyen@Sun.COM out:
35808823STruong.Q.Nguyen@Sun.COM 	scf_transaction_destroy(t);
35818823STruong.Q.Nguyen@Sun.COM 	scf_entry_destroy(e);
35828823STruong.Q.Nguyen@Sun.COM 	scf_value_destroy(v);
35838823STruong.Q.Nguyen@Sun.COM 	scf_pg_destroy(pg);
35848823STruong.Q.Nguyen@Sun.COM 
35858823STruong.Q.Nguyen@Sun.COM 	return (ret);
35868823STruong.Q.Nguyen@Sun.COM }
35878823STruong.Q.Nguyen@Sun.COM 
35888823STruong.Q.Nguyen@Sun.COM int
35898823STruong.Q.Nguyen@Sun.COM restarter_inst_set_aux_fmri(scf_instance_t *inst)
35908823STruong.Q.Nguyen@Sun.COM {
35918823STruong.Q.Nguyen@Sun.COM 	scf_handle_t *h;
35928823STruong.Q.Nguyen@Sun.COM 	scf_propertygroup_t *pg;
35938823STruong.Q.Nguyen@Sun.COM 	scf_property_t *prop;
35948823STruong.Q.Nguyen@Sun.COM 	scf_value_t *val;
35958823STruong.Q.Nguyen@Sun.COM 	char *aux_fmri;
35968823STruong.Q.Nguyen@Sun.COM 	size_t size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
35978823STruong.Q.Nguyen@Sun.COM 	int ret = 1;
35988823STruong.Q.Nguyen@Sun.COM 
35998823STruong.Q.Nguyen@Sun.COM 	if ((aux_fmri = malloc(size)) == NULL)
36008823STruong.Q.Nguyen@Sun.COM 		return (1);
36018823STruong.Q.Nguyen@Sun.COM 
36028823STruong.Q.Nguyen@Sun.COM 	h = scf_instance_handle(inst);
36038823STruong.Q.Nguyen@Sun.COM 
36048823STruong.Q.Nguyen@Sun.COM 	pg = scf_pg_create(h);
36058823STruong.Q.Nguyen@Sun.COM 	prop = scf_property_create(h);
36068823STruong.Q.Nguyen@Sun.COM 	val = scf_value_create(h);
36078823STruong.Q.Nguyen@Sun.COM 	if (pg == NULL || prop == NULL || val == NULL)
36088823STruong.Q.Nguyen@Sun.COM 		goto out;
36098823STruong.Q.Nguyen@Sun.COM 
36108823STruong.Q.Nguyen@Sun.COM 	/*
36118823STruong.Q.Nguyen@Sun.COM 	 * Get auxiliary_fmri value from restarter_actions pg
36128823STruong.Q.Nguyen@Sun.COM 	 */
36138823STruong.Q.Nguyen@Sun.COM 	if (instance_get_or_add_pg(inst, SCF_PG_RESTARTER_ACTIONS,
36148823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER_ACTIONS_TYPE, SCF_PG_RESTARTER_ACTIONS_FLAGS,
36158823STruong.Q.Nguyen@Sun.COM 	    pg) != SCF_SUCCESS)
36168823STruong.Q.Nguyen@Sun.COM 		goto out;
36178823STruong.Q.Nguyen@Sun.COM 
36188823STruong.Q.Nguyen@Sun.COM 	if (get_astring_val(pg, SCF_PROPERTY_AUX_FMRI, aux_fmri, size,
36198823STruong.Q.Nguyen@Sun.COM 	    prop, val) != SCF_SUCCESS)
36208823STruong.Q.Nguyen@Sun.COM 		goto out;
36218823STruong.Q.Nguyen@Sun.COM 
36228823STruong.Q.Nguyen@Sun.COM 	/*
36238823STruong.Q.Nguyen@Sun.COM 	 * Populate restarter/auxiliary_fmri with the obtained fmri.
36248823STruong.Q.Nguyen@Sun.COM 	 */
36258823STruong.Q.Nguyen@Sun.COM 	ret = restarter_inst_set_astring_prop(inst, SCF_PG_RESTARTER,
36268823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS,
36278823STruong.Q.Nguyen@Sun.COM 	    SCF_PROPERTY_AUX_FMRI, aux_fmri);
36288823STruong.Q.Nguyen@Sun.COM 
36298823STruong.Q.Nguyen@Sun.COM out:
36308823STruong.Q.Nguyen@Sun.COM 	free(aux_fmri);
36318823STruong.Q.Nguyen@Sun.COM 	scf_value_destroy(val);
36328823STruong.Q.Nguyen@Sun.COM 	scf_property_destroy(prop);
36338823STruong.Q.Nguyen@Sun.COM 	scf_pg_destroy(pg);
36348823STruong.Q.Nguyen@Sun.COM 	return (ret);
36358823STruong.Q.Nguyen@Sun.COM }
36368823STruong.Q.Nguyen@Sun.COM 
36378823STruong.Q.Nguyen@Sun.COM int
36388823STruong.Q.Nguyen@Sun.COM restarter_inst_reset_aux_fmri(scf_instance_t *inst)
36398823STruong.Q.Nguyen@Sun.COM {
36408823STruong.Q.Nguyen@Sun.COM 	return (scf_instance_delete_prop(inst,
36418823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER, SCF_PROPERTY_AUX_FMRI));
36428823STruong.Q.Nguyen@Sun.COM }
36438823STruong.Q.Nguyen@Sun.COM 
36448823STruong.Q.Nguyen@Sun.COM int
36458823STruong.Q.Nguyen@Sun.COM restarter_inst_reset_ractions_aux_fmri(scf_instance_t *inst)
36468823STruong.Q.Nguyen@Sun.COM {
36478823STruong.Q.Nguyen@Sun.COM 	return (scf_instance_delete_prop(inst,
36488823STruong.Q.Nguyen@Sun.COM 	    SCF_PG_RESTARTER_ACTIONS, SCF_PROPERTY_AUX_FMRI));
36498823STruong.Q.Nguyen@Sun.COM }
3650