xref: /onnv-gate/usr/src/lib/librestart/common/librestart.c (revision 6045:7a731d54b2bc)
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  */
210Sstevel@tonic-gate /*
22*6045Srm88369  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <librestart.h>
290Sstevel@tonic-gate #include <librestart_priv.h>
300Sstevel@tonic-gate #include <libscf.h>
310Sstevel@tonic-gate #include <libscf_priv.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <assert.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <dlfcn.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <exec_attr.h>
380Sstevel@tonic-gate #include <grp.h>
390Sstevel@tonic-gate #include <libsysevent.h>
400Sstevel@tonic-gate #include <libuutil.h>
410Sstevel@tonic-gate #include <limits.h>
420Sstevel@tonic-gate #include <link.h>
430Sstevel@tonic-gate #include <malloc.h>
440Sstevel@tonic-gate #include <pool.h>
450Sstevel@tonic-gate #include <priv.h>
460Sstevel@tonic-gate #include <project.h>
470Sstevel@tonic-gate #include <pthread.h>
480Sstevel@tonic-gate #include <pwd.h>
490Sstevel@tonic-gate #include <secdb.h>
500Sstevel@tonic-gate #include <signal.h>
510Sstevel@tonic-gate #include <stdlib.h>
520Sstevel@tonic-gate #include <string.h>
530Sstevel@tonic-gate #include <syslog.h>
540Sstevel@tonic-gate #include <sys/corectl.h>
550Sstevel@tonic-gate #include <sys/machelf.h>
560Sstevel@tonic-gate #include <sys/task.h>
570Sstevel@tonic-gate #include <sys/types.h>
580Sstevel@tonic-gate #include <time.h>
590Sstevel@tonic-gate #include <unistd.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	walkcontext	_walkcontext
620Sstevel@tonic-gate #include <ucontext.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #define	min(a, b)		((a) > (b) ? (b) : (a))
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #define	MKW_TRUE	":true"
670Sstevel@tonic-gate #define	MKW_KILL	":kill"
680Sstevel@tonic-gate #define	MKW_KILL_PROC	":kill_process"
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #define	ALLOCFAIL	((char *)"Allocation failure.")
710Sstevel@tonic-gate #define	RCBROKEN	((char *)"Repository connection broken.")
720Sstevel@tonic-gate 
73*6045Srm88369 #define	MAX_COMMIT_RETRIES		10
740Sstevel@tonic-gate #define	MAX_COMMIT_RETRY_INT		(5 * 1000000)	/* 5 seconds */
750Sstevel@tonic-gate #define	INITIAL_COMMIT_RETRY_INT	(10000)		/* 1/100th second */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * bad_fail() catches bugs in this and lower layers by reporting supposedly
790Sstevel@tonic-gate  * impossible function failures.  The NDEBUG case keeps the strings out of the
800Sstevel@tonic-gate  * library but still calls abort() so we can root-cause from the coredump.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate #ifndef NDEBUG
830Sstevel@tonic-gate #define	bad_fail(func, err)	{					\
840Sstevel@tonic-gate 	(void) fprintf(stderr,						\
850Sstevel@tonic-gate 	    "At %s:%d, %s() failed with unexpected error %d.  Aborting.\n", \
860Sstevel@tonic-gate 	    __FILE__, __LINE__, (func), (err));				\
870Sstevel@tonic-gate 	abort();							\
880Sstevel@tonic-gate }
890Sstevel@tonic-gate #else
900Sstevel@tonic-gate #define	bad_fail(func, err)	abort()
910Sstevel@tonic-gate #endif
920Sstevel@tonic-gate 
930Sstevel@tonic-gate struct restarter_event_handle {
940Sstevel@tonic-gate 	char				*reh_restarter_name;
950Sstevel@tonic-gate 	char				*reh_delegate_channel_name;
960Sstevel@tonic-gate 	evchan_t			*reh_delegate_channel;
970Sstevel@tonic-gate 	char				*reh_delegate_subscriber_id;
980Sstevel@tonic-gate 	char				*reh_master_channel_name;
990Sstevel@tonic-gate 	evchan_t			*reh_master_channel;
1000Sstevel@tonic-gate 	char				*reh_master_subscriber_id;
1010Sstevel@tonic-gate 	int				(*reh_handler)(restarter_event_t *);
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate struct restarter_event {
1050Sstevel@tonic-gate 	sysevent_t			*re_sysevent;
1060Sstevel@tonic-gate 	restarter_event_type_t		re_type;
1070Sstevel@tonic-gate 	char				*re_instance_name;
1080Sstevel@tonic-gate 	restarter_event_handle_t	*re_event_handle;
1090Sstevel@tonic-gate 	restarter_instance_state_t	re_state;
1100Sstevel@tonic-gate 	restarter_instance_state_t	re_next_state;
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static const char * const allocfail = "Allocation failure.\n";
1140Sstevel@tonic-gate static const char * const rcbroken = "Repository connection broken.\n";
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static int method_context_safety = 0;	/* Can safely call pools/projects. */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate int ndebug = 1;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static void
1210Sstevel@tonic-gate free_restarter_event_handle(struct restarter_event_handle *h)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	if (h == NULL)
1240Sstevel@tonic-gate 		return;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	/*
1270Sstevel@tonic-gate 	 * Just free the memory -- don't unbind the sysevent handle,
1280Sstevel@tonic-gate 	 * as otherwise events may be lost if this is just a restarter
1290Sstevel@tonic-gate 	 * restart.
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (h->reh_restarter_name != NULL)
1330Sstevel@tonic-gate 		free(h->reh_restarter_name);
1340Sstevel@tonic-gate 	if (h->reh_delegate_channel_name != NULL)
1350Sstevel@tonic-gate 		free(h->reh_delegate_channel_name);
1360Sstevel@tonic-gate 	if (h->reh_delegate_subscriber_id != NULL)
1370Sstevel@tonic-gate 		free(h->reh_delegate_subscriber_id);
1380Sstevel@tonic-gate 	if (h->reh_master_channel_name != NULL)
1390Sstevel@tonic-gate 		free(h->reh_master_channel_name);
1400Sstevel@tonic-gate 	if (h->reh_master_subscriber_id != NULL)
1410Sstevel@tonic-gate 		free(h->reh_master_subscriber_id);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	free(h);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate static const char *
1470Sstevel@tonic-gate last_part(const char *fmri)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	char *last_part;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	last_part = strrchr(fmri, '/');
1520Sstevel@tonic-gate 	last_part++;
1530Sstevel@tonic-gate 	assert(last_part != NULL);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	return (last_part);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate char *
1590Sstevel@tonic-gate _restarter_get_channel_name(const char *fmri, int type)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	const char *name;
1620Sstevel@tonic-gate 	char *chan_name = malloc(MAX_CHNAME_LEN);
1630Sstevel@tonic-gate 	char prefix_name[3];
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (chan_name == NULL)
1660Sstevel@tonic-gate 		return (NULL);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	if (type == RESTARTER_CHANNEL_DELEGATE)
1690Sstevel@tonic-gate 		(void) strcpy(prefix_name, "d_");
1700Sstevel@tonic-gate 	else if (type == RESTARTER_CHANNEL_MASTER)
1710Sstevel@tonic-gate 		(void) strcpy(prefix_name, "m_");
1720Sstevel@tonic-gate 	else {
1730Sstevel@tonic-gate 		free(chan_name);
1740Sstevel@tonic-gate 		return (NULL);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	name = last_part(fmri);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * Should check for [a-z],[A-Z],[0-9],.,_,-,:
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (snprintf(chan_name, MAX_CHNAME_LEN, "com.sun:scf:%s%s",
1840Sstevel@tonic-gate 	    prefix_name, name) > MAX_CHNAME_LEN) {
1850Sstevel@tonic-gate 		free(chan_name);
1860Sstevel@tonic-gate 		return (NULL);
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	return (chan_name);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate int
1930Sstevel@tonic-gate cb(sysevent_t *syse, void *cookie)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	restarter_event_handle_t *h = (restarter_event_handle_t *)cookie;
1960Sstevel@tonic-gate 	restarter_event_t *e;
1970Sstevel@tonic-gate 	nvlist_t *attr_list = NULL;
1980Sstevel@tonic-gate 	int ret = 0;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	e = uu_zalloc(sizeof (restarter_event_t));
2010Sstevel@tonic-gate 	if (e == NULL)
2020Sstevel@tonic-gate 		uu_die(allocfail);
2030Sstevel@tonic-gate 	e->re_event_handle = h;
2040Sstevel@tonic-gate 	e->re_sysevent = syse;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	if (sysevent_get_attr_list(syse, &attr_list) != 0)
2070Sstevel@tonic-gate 		uu_die(allocfail);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if ((nvlist_lookup_uint32(attr_list, RESTARTER_NAME_TYPE,
2100Sstevel@tonic-gate 	    &(e->re_type)) != 0) ||
2110Sstevel@tonic-gate 	    (nvlist_lookup_string(attr_list,
2120Sstevel@tonic-gate 	    RESTARTER_NAME_INSTANCE, &(e->re_instance_name)) != 0)) {
2130Sstevel@tonic-gate 		uu_warn("%s: Can't decode nvlist for event %p\n",
2140Sstevel@tonic-gate 		    h->reh_restarter_name, (void *)syse);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 		ret = 0;
2170Sstevel@tonic-gate 	} else {
2180Sstevel@tonic-gate 		ret = h->reh_handler(e);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	uu_free(e);
2220Sstevel@tonic-gate 	nvlist_free(attr_list);
2230Sstevel@tonic-gate 	return (ret);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * restarter_bind_handle(uint32_t, char *, int (*)(restarter_event_t *), int,
2280Sstevel@tonic-gate  *     restarter_event_handle_t **)
2290Sstevel@tonic-gate  *
2300Sstevel@tonic-gate  * Bind to a delegated restarter event channel.
2310Sstevel@tonic-gate  * Each delegated restarter gets its own channel for resource management.
2320Sstevel@tonic-gate  *
2330Sstevel@tonic-gate  * Returns 0 on success or
2340Sstevel@tonic-gate  *   ENOTSUP	version mismatch
2350Sstevel@tonic-gate  *   EINVAL	restarter_name or event_handle is NULL
2360Sstevel@tonic-gate  *   ENOMEM	out of memory, too many channels, or too many subscriptions
2370Sstevel@tonic-gate  *   EBUSY	sysevent_evc_bind() could not establish binding
2380Sstevel@tonic-gate  *   EFAULT	internal sysevent_evc_bind()/sysevent_evc_subscribe() error
2390Sstevel@tonic-gate  *   EMFILE	out of file descriptors
2400Sstevel@tonic-gate  *   EPERM	insufficient privilege for sysevent_evc_bind()
2410Sstevel@tonic-gate  *   EEXIST	already subscribed
2420Sstevel@tonic-gate  */
2430Sstevel@tonic-gate int
2440Sstevel@tonic-gate restarter_bind_handle(uint32_t version, const char *restarter_name,
2450Sstevel@tonic-gate     int (*event_handler)(restarter_event_t *), int flags,
2460Sstevel@tonic-gate     restarter_event_handle_t **rehp)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	restarter_event_handle_t *h;
2490Sstevel@tonic-gate 	size_t sz;
2500Sstevel@tonic-gate 	int err;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	if (version != RESTARTER_EVENT_VERSION)
2530Sstevel@tonic-gate 		return (ENOTSUP);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if (restarter_name == NULL || event_handler == NULL)
2560Sstevel@tonic-gate 		return (EINVAL);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if (flags & RESTARTER_FLAG_DEBUG)
2590Sstevel@tonic-gate 		ndebug++;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	if ((h = uu_zalloc(sizeof (restarter_event_handle_t))) == NULL)
2620Sstevel@tonic-gate 		return (ENOMEM);
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	h->reh_delegate_subscriber_id = malloc(MAX_SUBID_LEN);
2650Sstevel@tonic-gate 	h->reh_master_subscriber_id = malloc(MAX_SUBID_LEN);
2660Sstevel@tonic-gate 	h->reh_restarter_name = strdup(restarter_name);
2670Sstevel@tonic-gate 	if (h->reh_delegate_subscriber_id == NULL ||
2680Sstevel@tonic-gate 	    h->reh_master_subscriber_id == NULL ||
2690Sstevel@tonic-gate 	    h->reh_restarter_name == NULL) {
2700Sstevel@tonic-gate 		free_restarter_event_handle(h);
2710Sstevel@tonic-gate 		return (ENOMEM);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	sz = strlcpy(h->reh_delegate_subscriber_id, "del", MAX_SUBID_LEN);
2750Sstevel@tonic-gate 	assert(sz < MAX_SUBID_LEN);
2760Sstevel@tonic-gate 	sz = strlcpy(h->reh_master_subscriber_id, "master", MAX_SUBID_LEN);
2770Sstevel@tonic-gate 	assert(sz < MAX_SUBID_LEN);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	h->reh_delegate_channel_name =
2800Sstevel@tonic-gate 	    _restarter_get_channel_name(restarter_name,
2810Sstevel@tonic-gate 	    RESTARTER_CHANNEL_DELEGATE);
2820Sstevel@tonic-gate 	h->reh_master_channel_name =
2830Sstevel@tonic-gate 	    _restarter_get_channel_name(restarter_name,
2840Sstevel@tonic-gate 	    RESTARTER_CHANNEL_MASTER);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (h->reh_delegate_channel_name == NULL ||
2870Sstevel@tonic-gate 	    h->reh_master_channel_name == NULL) {
2880Sstevel@tonic-gate 		free_restarter_event_handle(h);
2890Sstevel@tonic-gate 		return (ENOMEM);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (sysevent_evc_bind(h->reh_delegate_channel_name,
2930Sstevel@tonic-gate 	    &h->reh_delegate_channel, EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
2940Sstevel@tonic-gate 		err = errno;
2950Sstevel@tonic-gate 		assert(err != EINVAL);
2960Sstevel@tonic-gate 		assert(err != ENOENT);
2970Sstevel@tonic-gate 		free_restarter_event_handle(h);
2980Sstevel@tonic-gate 		return (err);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	if (sysevent_evc_bind(h->reh_master_channel_name,
3020Sstevel@tonic-gate 	    &h->reh_master_channel, EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
3030Sstevel@tonic-gate 		err = errno;
3040Sstevel@tonic-gate 		assert(err != EINVAL);
3050Sstevel@tonic-gate 		assert(err != ENOENT);
3060Sstevel@tonic-gate 		free_restarter_event_handle(h);
3070Sstevel@tonic-gate 		return (err);
3080Sstevel@tonic-gate 	}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	h->reh_handler = event_handler;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	assert(strlen(restarter_name) <= MAX_CLASS_LEN - 1);
3130Sstevel@tonic-gate 	assert(strlen(h->reh_delegate_subscriber_id) <= MAX_SUBID_LEN - 1);
3140Sstevel@tonic-gate 	assert(strlen(h->reh_master_subscriber_id) <= MAX_SUBID_LEN - 1);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (sysevent_evc_subscribe(h->reh_delegate_channel,
3170Sstevel@tonic-gate 	    h->reh_delegate_subscriber_id, EC_ALL, cb, h, EVCH_SUB_KEEP) != 0) {
3180Sstevel@tonic-gate 		err = errno;
3190Sstevel@tonic-gate 		assert(err != EINVAL);
3200Sstevel@tonic-gate 		free_restarter_event_handle(h);
3210Sstevel@tonic-gate 		return (err);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	*rehp = h;
3250Sstevel@tonic-gate 	return (0);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate restarter_event_handle_t *
3290Sstevel@tonic-gate restarter_event_get_handle(restarter_event_t *e)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	assert(e != NULL && e->re_event_handle != NULL);
3320Sstevel@tonic-gate 	return (e->re_event_handle);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate restarter_event_type_t
3360Sstevel@tonic-gate restarter_event_get_type(restarter_event_t *e)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	assert(e != NULL);
3390Sstevel@tonic-gate 	return (e->re_type);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate ssize_t
3430Sstevel@tonic-gate restarter_event_get_instance(restarter_event_t *e, char *inst, size_t sz)
3440Sstevel@tonic-gate {
3450Sstevel@tonic-gate 	assert(e != NULL && inst != NULL);
3460Sstevel@tonic-gate 	return ((ssize_t)strlcpy(inst, e->re_instance_name, sz));
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate int
3500Sstevel@tonic-gate restarter_event_get_current_states(restarter_event_t *e,
3510Sstevel@tonic-gate     restarter_instance_state_t *state, restarter_instance_state_t *next_state)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	if (e == NULL)
3540Sstevel@tonic-gate 		return (-1);
3550Sstevel@tonic-gate 	*state = e->re_state;
3560Sstevel@tonic-gate 	*next_state = e->re_next_state;
3570Sstevel@tonic-gate 	return (0);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate /*
361*6045Srm88369  * restarter_event_publish_retry() is a wrapper around sysevent_evc_publish().
362*6045Srm88369  * In case, the event cannot be sent at the first attempt (sysevent_evc_publish
363*6045Srm88369  * returned EAGAIN - sysevent queue full), this function retries a few time
364*6045Srm88369  * and return ENOSPC if it reaches the retry limit.
365*6045Srm88369  *
366*6045Srm88369  * The arguments to this function map the arguments of sysevent_evc_publish().
367*6045Srm88369  *
368*6045Srm88369  * On success, return 0. On error, return
369*6045Srm88369  *
370*6045Srm88369  *   EFAULT - internal sysevent_evc_publish() error
371*6045Srm88369  *   ENOMEM - internal sysevent_evc_publish() error
372*6045Srm88369  *   EBADF - scp is invalid (sysevent_evc_publish() returned EINVAL)
373*6045Srm88369  *   ENOSPC - sysevent queue full (sysevent_evc_publish() returned EAGAIN)
374*6045Srm88369  */
375*6045Srm88369 int
376*6045Srm88369 restarter_event_publish_retry(evchan_t *scp, const char *class,
377*6045Srm88369     const char *subclass, const char *vendor, const char *pub_name,
378*6045Srm88369     nvlist_t *attr_list, uint32_t flags)
379*6045Srm88369 {
380*6045Srm88369 	int retries, ret;
381*6045Srm88369 	useconds_t retry_int = INITIAL_COMMIT_RETRY_INT;
382*6045Srm88369 
383*6045Srm88369 	for (retries = 0; retries < MAX_COMMIT_RETRIES; retries++) {
384*6045Srm88369 		ret = sysevent_evc_publish(scp, class, subclass, vendor,
385*6045Srm88369 		    pub_name, attr_list, flags);
386*6045Srm88369 		if (ret == 0)
387*6045Srm88369 			break;
388*6045Srm88369 
389*6045Srm88369 		switch (ret) {
390*6045Srm88369 		case EAGAIN:
391*6045Srm88369 			/* Queue is full */
392*6045Srm88369 			(void) usleep(retry_int);
393*6045Srm88369 
394*6045Srm88369 			retry_int = min(retry_int * 2, MAX_COMMIT_RETRY_INT);
395*6045Srm88369 			break;
396*6045Srm88369 
397*6045Srm88369 		case EINVAL:
398*6045Srm88369 			ret = EBADF;
399*6045Srm88369 			/* FALLTHROUGH */
400*6045Srm88369 
401*6045Srm88369 		case EFAULT:
402*6045Srm88369 		case ENOMEM:
403*6045Srm88369 			return (ret);
404*6045Srm88369 
405*6045Srm88369 		case EOVERFLOW:
406*6045Srm88369 		default:
407*6045Srm88369 			/* internal error - abort */
408*6045Srm88369 			bad_fail("sysevent_evc_publish", ret);
409*6045Srm88369 		}
410*6045Srm88369 	}
411*6045Srm88369 
412*6045Srm88369 	if (retries == MAX_COMMIT_RETRIES)
413*6045Srm88369 		ret = ENOSPC;
414*6045Srm88369 
415*6045Srm88369 	return (ret);
416*6045Srm88369 }
417*6045Srm88369 
418*6045Srm88369 /*
4190Sstevel@tonic-gate  * Commit the state, next state, and auxiliary state into the repository.
4200Sstevel@tonic-gate  * Let the graph engine know about the state change and error.  On success,
4210Sstevel@tonic-gate  * return 0. On error, return
4220Sstevel@tonic-gate  *   EINVAL - aux has spaces
4230Sstevel@tonic-gate  *	    - inst is invalid or not an instance FMRI
4240Sstevel@tonic-gate  *   EPROTO - librestart compiled against different libscf
4250Sstevel@tonic-gate  *   ENOMEM - out of memory
4260Sstevel@tonic-gate  *	    - repository server out of resources
4270Sstevel@tonic-gate  *   ENOTACTIVE - repository server not running
4280Sstevel@tonic-gate  *   ECONNABORTED - repository connection established, but then broken
4290Sstevel@tonic-gate  *		  - unknown libscf error
4300Sstevel@tonic-gate  *   ENOENT - inst does not exist in the repository
4310Sstevel@tonic-gate  *   EPERM - insufficient permissions
4320Sstevel@tonic-gate  *   EACCESS - backend access denied
4330Sstevel@tonic-gate  *   EROFS - backend is readonly
4340Sstevel@tonic-gate  *   EFAULT - internal sysevent_evc_publish() error
4350Sstevel@tonic-gate  *   EBADF - h is invalid (sysevent_evc_publish() returned EINVAL)
436*6045Srm88369  *   ENOSPC - sysevent queue full (sysevent_evc_publish() returned EAGAIN)
4370Sstevel@tonic-gate  */
4380Sstevel@tonic-gate int
4390Sstevel@tonic-gate restarter_set_states(restarter_event_handle_t *h, const char *inst,
4400Sstevel@tonic-gate     restarter_instance_state_t cur_state,
4410Sstevel@tonic-gate     restarter_instance_state_t new_cur_state,
4420Sstevel@tonic-gate     restarter_instance_state_t next_state,
4430Sstevel@tonic-gate     restarter_instance_state_t new_next_state, restarter_error_t e,
4440Sstevel@tonic-gate     const char *aux)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	nvlist_t *attr;
4470Sstevel@tonic-gate 	scf_handle_t *scf_h;
4480Sstevel@tonic-gate 	instance_data_t id;
4490Sstevel@tonic-gate 	int ret = 0;
4500Sstevel@tonic-gate 	char *p = (char *)aux;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	assert(h->reh_master_channel != NULL);
4530Sstevel@tonic-gate 	assert(h->reh_master_channel_name != NULL);
4540Sstevel@tonic-gate 	assert(h->reh_master_subscriber_id != NULL);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	/* Validate format of auxiliary state: no spaces allowed */
457863Slianep 	if (p != NULL) {
458863Slianep 		while (*p != '\0') {
459863Slianep 			if (isspace(*p))
460863Slianep 				return (EINVAL);
461863Slianep 			p++;
462863Slianep 		}
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if ((scf_h = scf_handle_create(SCF_VERSION)) == NULL) {
4660Sstevel@tonic-gate 		switch (scf_error()) {
4670Sstevel@tonic-gate 		case SCF_ERROR_VERSION_MISMATCH:
4680Sstevel@tonic-gate 			return (EPROTO);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 		case SCF_ERROR_NO_MEMORY:
4710Sstevel@tonic-gate 			return (ENOMEM);
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 		default:
4740Sstevel@tonic-gate 			bad_fail("scf_handle_create", scf_error());
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	if (scf_handle_bind(scf_h) == -1) {
4790Sstevel@tonic-gate 		scf_handle_destroy(scf_h);
4800Sstevel@tonic-gate 		switch (scf_error()) {
4810Sstevel@tonic-gate 		case SCF_ERROR_NO_SERVER:
4820Sstevel@tonic-gate 			return (ENOTACTIVE);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		case SCF_ERROR_NO_RESOURCES:
4850Sstevel@tonic-gate 			return (ENOMEM);
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4880Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
4890Sstevel@tonic-gate 		default:
4900Sstevel@tonic-gate 			bad_fail("scf_handle_bind", scf_error());
4910Sstevel@tonic-gate 		}
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 ||
4950Sstevel@tonic-gate 	    nvlist_add_int32(attr, RESTARTER_NAME_STATE, new_cur_state) != 0 ||
4960Sstevel@tonic-gate 	    nvlist_add_int32(attr, RESTARTER_NAME_NEXT_STATE, new_next_state)
4970Sstevel@tonic-gate 	    != 0 ||
4980Sstevel@tonic-gate 	    nvlist_add_int32(attr, RESTARTER_NAME_ERROR, e) != 0 ||
4990Sstevel@tonic-gate 	    nvlist_add_string(attr, RESTARTER_NAME_INSTANCE, inst) != 0) {
5000Sstevel@tonic-gate 		ret = ENOMEM;
501*6045Srm88369 	} else {
502*6045Srm88369 		id.i_fmri = inst;
503*6045Srm88369 		id.i_state = cur_state;
504*6045Srm88369 		id.i_next_state = next_state;
505*6045Srm88369 
506*6045Srm88369 		ret = _restarter_commit_states(scf_h, &id, new_cur_state,
507*6045Srm88369 		    new_next_state, aux);
508*6045Srm88369 
509*6045Srm88369 		if (ret == 0) {
510*6045Srm88369 			ret = restarter_event_publish_retry(
511*6045Srm88369 			    h->reh_master_channel, "master", "state_change",
512*6045Srm88369 			    "com.sun", "librestart", attr, EVCH_NOSLEEP);
5130Sstevel@tonic-gate 		}
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	nvlist_free(attr);
5170Sstevel@tonic-gate 	(void) scf_handle_unbind(scf_h);
5180Sstevel@tonic-gate 	scf_handle_destroy(scf_h);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	return (ret);
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate restarter_instance_state_t
5240Sstevel@tonic-gate restarter_string_to_state(char *string)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	assert(string != NULL);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	if (strcmp(string, SCF_STATE_STRING_NONE) == 0)
5290Sstevel@tonic-gate 		return (RESTARTER_STATE_NONE);
5300Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_UNINIT) == 0)
5310Sstevel@tonic-gate 		return (RESTARTER_STATE_UNINIT);
5320Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_MAINT) == 0)
5330Sstevel@tonic-gate 		return (RESTARTER_STATE_MAINT);
5340Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_OFFLINE) == 0)
5350Sstevel@tonic-gate 		return (RESTARTER_STATE_OFFLINE);
5360Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_DISABLED) == 0)
5370Sstevel@tonic-gate 		return (RESTARTER_STATE_DISABLED);
5380Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_ONLINE) == 0)
5390Sstevel@tonic-gate 		return (RESTARTER_STATE_ONLINE);
5400Sstevel@tonic-gate 	else if (strcmp(string, SCF_STATE_STRING_DEGRADED) == 0)
5410Sstevel@tonic-gate 		return (RESTARTER_STATE_DEGRADED);
5420Sstevel@tonic-gate 	else {
5430Sstevel@tonic-gate 		return (RESTARTER_STATE_NONE);
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate ssize_t
5480Sstevel@tonic-gate restarter_state_to_string(restarter_instance_state_t state, char *string,
5490Sstevel@tonic-gate     size_t len)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	assert(string != NULL);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	if (state == RESTARTER_STATE_NONE)
5540Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_NONE, len));
5550Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_UNINIT)
5560Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_UNINIT, len));
5570Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_MAINT)
5580Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_MAINT, len));
5590Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_OFFLINE)
5600Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_OFFLINE,
5610Sstevel@tonic-gate 		    len));
5620Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_DISABLED)
5630Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_DISABLED,
5640Sstevel@tonic-gate 		    len));
5650Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_ONLINE)
5660Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_ONLINE, len));
5670Sstevel@tonic-gate 	else if (state == RESTARTER_STATE_DEGRADED)
5680Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, SCF_STATE_STRING_DEGRADED,
5690Sstevel@tonic-gate 		    len));
5700Sstevel@tonic-gate 	else
5710Sstevel@tonic-gate 		return ((ssize_t)strlcpy(string, "unknown", len));
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate /*
5750Sstevel@tonic-gate  * Sets pg to the name property group of s_inst.  If it doesn't exist, it is
5760Sstevel@tonic-gate  * added.
5770Sstevel@tonic-gate  *
5780Sstevel@tonic-gate  * Fails with
5790Sstevel@tonic-gate  *   ECONNABORTED - repository disconnection or unknown libscf error
5800Sstevel@tonic-gate  *   EBADF - inst is not set
5810Sstevel@tonic-gate  *   ECANCELED - inst is deleted
5820Sstevel@tonic-gate  *   EPERM - permission is denied
5830Sstevel@tonic-gate  *   EACCES - backend denied access
5840Sstevel@tonic-gate  *   EROFS - backend readonly
5850Sstevel@tonic-gate  */
5860Sstevel@tonic-gate static int
5870Sstevel@tonic-gate instance_get_or_add_pg(scf_instance_t *inst, const char *name,
5880Sstevel@tonic-gate     const char *type, uint32_t flags, scf_propertygroup_t *pg)
5890Sstevel@tonic-gate {
5900Sstevel@tonic-gate again:
5910Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, name, pg) == 0)
5920Sstevel@tonic-gate 		return (0);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	switch (scf_error()) {
5950Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
5960Sstevel@tonic-gate 	default:
5970Sstevel@tonic-gate 		return (ECONNABORTED);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	case SCF_ERROR_NOT_SET:
6000Sstevel@tonic-gate 		return (EBADF);
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
6030Sstevel@tonic-gate 		return (ECANCELED);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	case SCF_ERROR_NOT_FOUND:
6060Sstevel@tonic-gate 		break;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	case SCF_ERROR_HANDLE_MISMATCH:
6090Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
6100Sstevel@tonic-gate 		bad_fail("scf_instance_get_pg", scf_error());
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	if (scf_instance_add_pg(inst, name, type, flags, pg) == 0)
6140Sstevel@tonic-gate 		return (0);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	switch (scf_error()) {
6170Sstevel@tonic-gate 	case SCF_ERROR_CONNECTION_BROKEN:
6180Sstevel@tonic-gate 	default:
6190Sstevel@tonic-gate 		return (ECONNABORTED);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	case SCF_ERROR_DELETED:
6220Sstevel@tonic-gate 		return (ECANCELED);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	case SCF_ERROR_EXISTS:
6250Sstevel@tonic-gate 		goto again;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	case SCF_ERROR_PERMISSION_DENIED:
6280Sstevel@tonic-gate 		return (EPERM);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_ACCESS:
6310Sstevel@tonic-gate 		return (EACCES);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	case SCF_ERROR_BACKEND_READONLY:
6340Sstevel@tonic-gate 		return (EROFS);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	case SCF_ERROR_HANDLE_MISMATCH:
6370Sstevel@tonic-gate 	case SCF_ERROR_INVALID_ARGUMENT:
6380Sstevel@tonic-gate 	case SCF_ERROR_NOT_SET:			/* should be caught above */
6390Sstevel@tonic-gate 		bad_fail("scf_instance_add_pg", scf_error());
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	return (0);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate /*
6460Sstevel@tonic-gate  * Fails with
6470Sstevel@tonic-gate  *   ECONNABORTED
6480Sstevel@tonic-gate  *   ECANCELED - pg was deleted
6490Sstevel@tonic-gate  */
6500Sstevel@tonic-gate static int
6510Sstevel@tonic-gate tx_set_value(scf_transaction_t *tx, scf_transaction_entry_t *ent,
6520Sstevel@tonic-gate     const char *pname, scf_type_t ty, scf_value_t *val)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate 	int r;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	for (;;) {
6570Sstevel@tonic-gate 		if (scf_transaction_property_change_type(tx, ent, pname,
6580Sstevel@tonic-gate 		    ty) == 0)
6590Sstevel@tonic-gate 			break;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 		switch (scf_error()) {
6620Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
6630Sstevel@tonic-gate 		default:
6640Sstevel@tonic-gate 			return (ECONNABORTED);
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
6670Sstevel@tonic-gate 			return (ECANCELED);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
6700Sstevel@tonic-gate 			break;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
6730Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
6740Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
6750Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
6760Sstevel@tonic-gate 			bad_fail("scf_transaction_property_change_type",
6770Sstevel@tonic-gate 			    scf_error());
6780Sstevel@tonic-gate 		}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		if (scf_transaction_property_new(tx, ent, pname, ty) == 0)
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		switch (scf_error()) {
6840Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
6850Sstevel@tonic-gate 		default:
6860Sstevel@tonic-gate 			return (ECONNABORTED);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
6890Sstevel@tonic-gate 			return (ECANCELED);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		case SCF_ERROR_EXISTS:
6920Sstevel@tonic-gate 			break;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
6950Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
6960Sstevel@tonic-gate 		case SCF_ERROR_IN_USE:
6970Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
6980Sstevel@tonic-gate 			bad_fail("scf_transaction_property_new", scf_error());
6990Sstevel@tonic-gate 		}
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	r = scf_entry_add_value(ent, val);
7030Sstevel@tonic-gate 	assert(r == 0);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	return (0);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate /*
7090Sstevel@tonic-gate  * Commit new_state, new_next_state, and aux to the repository for id.  If
7100Sstevel@tonic-gate  * successful, also set id's state and next-state as given, and return 0.
7110Sstevel@tonic-gate  * Fails with
7120Sstevel@tonic-gate  *   ENOMEM - out of memory
7130Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
7140Sstevel@tonic-gate  *		  - unknown libscf error
7150Sstevel@tonic-gate  *   EINVAL - id->i_fmri is invalid or not an instance FMRI
7160Sstevel@tonic-gate  *   ENOENT - id->i_fmri does not exist
7170Sstevel@tonic-gate  *   EPERM - insufficient permissions
7180Sstevel@tonic-gate  *   EACCES - backend access denied
7190Sstevel@tonic-gate  *   EROFS - backend is readonly
7200Sstevel@tonic-gate  */
7210Sstevel@tonic-gate int
7220Sstevel@tonic-gate _restarter_commit_states(scf_handle_t *h, instance_data_t *id,
7230Sstevel@tonic-gate     restarter_instance_state_t new_state,
7240Sstevel@tonic-gate     restarter_instance_state_t new_state_next, const char *aux)
7250Sstevel@tonic-gate {
7260Sstevel@tonic-gate 	char str_state[MAX_SCF_STATE_STRING_SZ];
7270Sstevel@tonic-gate 	char str_new_state[MAX_SCF_STATE_STRING_SZ];
7280Sstevel@tonic-gate 	char str_state_next[MAX_SCF_STATE_STRING_SZ];
7290Sstevel@tonic-gate 	char str_new_state_next[MAX_SCF_STATE_STRING_SZ];
7300Sstevel@tonic-gate 	int ret = 0, r;
7310Sstevel@tonic-gate 	struct timeval now;
7320Sstevel@tonic-gate 	ssize_t sz;
7330Sstevel@tonic-gate 	char *default_aux = "none";
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	scf_transaction_t *t = NULL;
7360Sstevel@tonic-gate 	scf_transaction_entry_t *t_state = NULL, *t_state_next = NULL;
7370Sstevel@tonic-gate 	scf_transaction_entry_t *t_stime = NULL, *t_aux = NULL;
7380Sstevel@tonic-gate 	scf_value_t *v_state = NULL, *v_state_next = NULL, *v_stime = NULL;
7390Sstevel@tonic-gate 	scf_value_t *v_aux = NULL;
7400Sstevel@tonic-gate 	scf_instance_t *s_inst = NULL;
7410Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	assert(new_state != RESTARTER_STATE_NONE);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	/* If aux state is unset, set aux to a default string. */
7460Sstevel@tonic-gate 	if (aux == NULL)
7470Sstevel@tonic-gate 		aux = default_aux;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	if ((s_inst = scf_instance_create(h)) == NULL ||
7500Sstevel@tonic-gate 	    (pg = scf_pg_create(h)) == NULL ||
7510Sstevel@tonic-gate 	    (t = scf_transaction_create(h)) == NULL ||
7520Sstevel@tonic-gate 	    (t_state = scf_entry_create(h)) == NULL ||
7530Sstevel@tonic-gate 	    (t_state_next = scf_entry_create(h)) == NULL ||
7540Sstevel@tonic-gate 	    (t_stime = scf_entry_create(h)) == NULL ||
7550Sstevel@tonic-gate 	    (t_aux = scf_entry_create(h)) == NULL ||
7560Sstevel@tonic-gate 	    (v_state = scf_value_create(h)) == NULL ||
7570Sstevel@tonic-gate 	    (v_state_next = scf_value_create(h)) == NULL ||
7580Sstevel@tonic-gate 	    (v_stime = scf_value_create(h)) == NULL ||
7590Sstevel@tonic-gate 	    (v_aux = scf_value_create(h)) == NULL) {
7600Sstevel@tonic-gate 		ret = ENOMEM;
7610Sstevel@tonic-gate 		goto out;
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	sz = restarter_state_to_string(new_state, str_new_state,
7650Sstevel@tonic-gate 	    sizeof (str_new_state));
7660Sstevel@tonic-gate 	assert(sz < sizeof (str_new_state));
7670Sstevel@tonic-gate 	sz = restarter_state_to_string(new_state_next, str_new_state_next,
7680Sstevel@tonic-gate 	    sizeof (str_new_state_next));
7690Sstevel@tonic-gate 	assert(sz < sizeof (str_new_state_next));
7700Sstevel@tonic-gate 	sz = restarter_state_to_string(id->i_state, str_state,
7710Sstevel@tonic-gate 	    sizeof (str_state));
7720Sstevel@tonic-gate 	assert(sz < sizeof (str_state));
7730Sstevel@tonic-gate 	sz = restarter_state_to_string(id->i_next_state, str_state_next,
7740Sstevel@tonic-gate 	    sizeof (str_state_next));
7750Sstevel@tonic-gate 	assert(sz < sizeof (str_state_next));
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	ret = gettimeofday(&now, NULL);
7780Sstevel@tonic-gate 	assert(ret != -1);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, id->i_fmri, NULL, NULL, s_inst,
7810Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == -1) {
7820Sstevel@tonic-gate 		switch (scf_error()) {
7830Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
7840Sstevel@tonic-gate 		default:
7850Sstevel@tonic-gate 			ret = ECONNABORTED;
7860Sstevel@tonic-gate 			break;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
7890Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
7900Sstevel@tonic-gate 			ret = EINVAL;
7910Sstevel@tonic-gate 			break;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
7940Sstevel@tonic-gate 			ret = ENOENT;
7950Sstevel@tonic-gate 			break;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
7980Sstevel@tonic-gate 			bad_fail("scf_handle_decode_fmri", scf_error());
7990Sstevel@tonic-gate 		}
8000Sstevel@tonic-gate 		goto out;
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	if (scf_value_set_astring(v_state, str_new_state) != 0 ||
8050Sstevel@tonic-gate 	    scf_value_set_astring(v_state_next, str_new_state_next) != 0 ||
8060Sstevel@tonic-gate 	    scf_value_set_astring(v_aux, aux) != 0)
8070Sstevel@tonic-gate 		bad_fail("scf_value_set_astring", scf_error());
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if (scf_value_set_time(v_stime, now.tv_sec, now.tv_usec * 1000) != 0)
8100Sstevel@tonic-gate 		bad_fail("scf_value_set_time", scf_error());
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate add_pg:
8130Sstevel@tonic-gate 	switch (r = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
8140Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg)) {
8150Sstevel@tonic-gate 	case 0:
8160Sstevel@tonic-gate 		break;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	case ECONNABORTED:
8190Sstevel@tonic-gate 	case EPERM:
8200Sstevel@tonic-gate 	case EACCES:
8210Sstevel@tonic-gate 	case EROFS:
8220Sstevel@tonic-gate 		ret = r;
8230Sstevel@tonic-gate 		goto out;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	case ECANCELED:
8260Sstevel@tonic-gate 		ret = ENOENT;
8270Sstevel@tonic-gate 		goto out;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	case EBADF:
8300Sstevel@tonic-gate 	default:
8310Sstevel@tonic-gate 		bad_fail("instance_get_or_add_pg", r);
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	for (;;) {
8350Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) != 0) {
8360Sstevel@tonic-gate 			switch (scf_error()) {
8370Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8380Sstevel@tonic-gate 			default:
8390Sstevel@tonic-gate 				ret = ECONNABORTED;
8400Sstevel@tonic-gate 				goto out;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
8430Sstevel@tonic-gate 				goto add_pg;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8460Sstevel@tonic-gate 				ret = EPERM;
8470Sstevel@tonic-gate 				goto out;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
8500Sstevel@tonic-gate 				ret = EACCES;
8510Sstevel@tonic-gate 				goto out;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
8540Sstevel@tonic-gate 				ret = EROFS;
8550Sstevel@tonic-gate 				goto out;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
8580Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
8590Sstevel@tonic-gate 				bad_fail("scf_transaction_start", scf_error());
8600Sstevel@tonic-gate 			}
8610Sstevel@tonic-gate 		}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		if ((r = tx_set_value(t, t_state, SCF_PROPERTY_STATE,
8640Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, v_state)) != 0 ||
8650Sstevel@tonic-gate 		    (r = tx_set_value(t, t_state_next, SCF_PROPERTY_NEXT_STATE,
8660Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, v_state_next)) != 0 ||
8670Sstevel@tonic-gate 		    (r = tx_set_value(t, t_aux, SCF_PROPERTY_AUX_STATE,
8680Sstevel@tonic-gate 		    SCF_TYPE_ASTRING, v_aux)) != 0 ||
8690Sstevel@tonic-gate 		    (r = tx_set_value(t, t_stime, SCF_PROPERTY_STATE_TIMESTAMP,
8700Sstevel@tonic-gate 		    SCF_TYPE_TIME, v_stime)) != 0) {
8710Sstevel@tonic-gate 			switch (r) {
8720Sstevel@tonic-gate 			case ECONNABORTED:
8730Sstevel@tonic-gate 				ret = ECONNABORTED;
8740Sstevel@tonic-gate 				goto out;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 			case ECANCELED:
8770Sstevel@tonic-gate 				scf_transaction_reset(t);
8780Sstevel@tonic-gate 				goto add_pg;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 			default:
8810Sstevel@tonic-gate 				bad_fail("tx_set_value", r);
8820Sstevel@tonic-gate 			}
8830Sstevel@tonic-gate 		}
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 		ret = scf_transaction_commit(t);
8860Sstevel@tonic-gate 		if (ret == 1)
8870Sstevel@tonic-gate 			break;
8880Sstevel@tonic-gate 		if (ret == -1) {
8890Sstevel@tonic-gate 			switch (scf_error()) {
8900Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
8910Sstevel@tonic-gate 			default:
8920Sstevel@tonic-gate 				ret = ECONNABORTED;
8930Sstevel@tonic-gate 				goto out;
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
8960Sstevel@tonic-gate 				ret = EPERM;
8970Sstevel@tonic-gate 				goto out;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
9000Sstevel@tonic-gate 				ret = EACCES;
9010Sstevel@tonic-gate 				goto out;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
9040Sstevel@tonic-gate 				ret = EROFS;
9050Sstevel@tonic-gate 				goto out;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
9080Sstevel@tonic-gate 				bad_fail("scf_transaction_commit", scf_error());
9090Sstevel@tonic-gate 			}
9100Sstevel@tonic-gate 		}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 		scf_transaction_reset(t);
9130Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
9140Sstevel@tonic-gate 			switch (scf_error()) {
9150Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
9160Sstevel@tonic-gate 			default:
9170Sstevel@tonic-gate 				ret = ECONNABORTED;
9180Sstevel@tonic-gate 				goto out;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
9210Sstevel@tonic-gate 				goto add_pg;
9220Sstevel@tonic-gate 			}
9230Sstevel@tonic-gate 		}
9240Sstevel@tonic-gate 	}
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	id->i_state = new_state;
9270Sstevel@tonic-gate 	id->i_next_state = new_state_next;
9280Sstevel@tonic-gate 	ret = 0;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate out:
9310Sstevel@tonic-gate 	scf_transaction_destroy(t);
9320Sstevel@tonic-gate 	scf_entry_destroy(t_state);
9330Sstevel@tonic-gate 	scf_entry_destroy(t_state_next);
9340Sstevel@tonic-gate 	scf_entry_destroy(t_stime);
9350Sstevel@tonic-gate 	scf_entry_destroy(t_aux);
9360Sstevel@tonic-gate 	scf_value_destroy(v_state);
9370Sstevel@tonic-gate 	scf_value_destroy(v_state_next);
9380Sstevel@tonic-gate 	scf_value_destroy(v_stime);
9390Sstevel@tonic-gate 	scf_value_destroy(v_aux);
9400Sstevel@tonic-gate 	scf_pg_destroy(pg);
9410Sstevel@tonic-gate 	scf_instance_destroy(s_inst);
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	return (ret);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate /*
9470Sstevel@tonic-gate  * Fails with
9480Sstevel@tonic-gate  *   EINVAL - type is invalid
9490Sstevel@tonic-gate  *   ENOMEM
9500Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
9510Sstevel@tonic-gate  *   EBADF - s_inst is not set
9520Sstevel@tonic-gate  *   ECANCELED - s_inst is deleted
9530Sstevel@tonic-gate  *   EPERM - permission denied
9540Sstevel@tonic-gate  *   EACCES - backend access denied
9550Sstevel@tonic-gate  *   EROFS - backend readonly
9560Sstevel@tonic-gate  */
9570Sstevel@tonic-gate int
9580Sstevel@tonic-gate restarter_remove_contract(scf_instance_t *s_inst, ctid_t contract_id,
9590Sstevel@tonic-gate     restarter_contract_type_t type)
9600Sstevel@tonic-gate {
9610Sstevel@tonic-gate 	scf_handle_t *h;
9620Sstevel@tonic-gate 	scf_transaction_t *t = NULL;
9630Sstevel@tonic-gate 	scf_transaction_entry_t *t_cid = NULL;
9640Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
9650Sstevel@tonic-gate 	scf_property_t *prop = NULL;
9660Sstevel@tonic-gate 	scf_value_t *val;
9670Sstevel@tonic-gate 	scf_iter_t *iter = NULL;
9680Sstevel@tonic-gate 	const char *pname;
9690Sstevel@tonic-gate 	int ret = 0, primary;
9700Sstevel@tonic-gate 	uint64_t c;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	switch (type) {
9730Sstevel@tonic-gate 	case RESTARTER_CONTRACT_PRIMARY:
9740Sstevel@tonic-gate 		primary = 1;
9750Sstevel@tonic-gate 		break;
9760Sstevel@tonic-gate 	case RESTARTER_CONTRACT_TRANSIENT:
9770Sstevel@tonic-gate 		primary = 0;
9780Sstevel@tonic-gate 		break;
9790Sstevel@tonic-gate 	default:
9800Sstevel@tonic-gate 		return (EINVAL);
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	h = scf_instance_handle(s_inst);
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	pg = scf_pg_create(h);
9860Sstevel@tonic-gate 	prop = scf_property_create(h);
9870Sstevel@tonic-gate 	iter = scf_iter_create(h);
9880Sstevel@tonic-gate 	t = scf_transaction_create(h);
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	if (pg == NULL || prop == NULL || iter == NULL || t == NULL) {
9910Sstevel@tonic-gate 		ret = ENOMEM;
9920Sstevel@tonic-gate 		goto remove_contract_cleanup;
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate add:
9960Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
9970Sstevel@tonic-gate 	ret = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
9980Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
9990Sstevel@tonic-gate 	if (ret != 0)
10000Sstevel@tonic-gate 		goto remove_contract_cleanup;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	pname = primary? SCF_PROPERTY_CONTRACT :
10030Sstevel@tonic-gate 	    SCF_PROPERTY_TRANSIENT_CONTRACT;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	for (;;) {
10060Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) != 0) {
10070Sstevel@tonic-gate 			switch (scf_error()) {
10080Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
10090Sstevel@tonic-gate 			default:
10100Sstevel@tonic-gate 				ret = ECONNABORTED;
10110Sstevel@tonic-gate 				goto remove_contract_cleanup;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
10140Sstevel@tonic-gate 				goto add;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
10170Sstevel@tonic-gate 				ret = EPERM;
10180Sstevel@tonic-gate 				goto remove_contract_cleanup;
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
10210Sstevel@tonic-gate 				ret = EACCES;
10220Sstevel@tonic-gate 				goto remove_contract_cleanup;
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
10250Sstevel@tonic-gate 				ret = EROFS;
10260Sstevel@tonic-gate 				goto remove_contract_cleanup;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
10290Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
10300Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
10310Sstevel@tonic-gate 				bad_fail("scf_transaction_start", scf_error());
10320Sstevel@tonic-gate 			}
10330Sstevel@tonic-gate 		}
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 		t_cid = scf_entry_create(h);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		if (scf_pg_get_property(pg, pname, prop) == 0) {
10380Sstevel@tonic-gate replace:
10390Sstevel@tonic-gate 			if (scf_transaction_property_change_type(t, t_cid,
10400Sstevel@tonic-gate 			    pname, SCF_TYPE_COUNT) != 0) {
10410Sstevel@tonic-gate 				switch (scf_error()) {
10420Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
10430Sstevel@tonic-gate 				default:
10440Sstevel@tonic-gate 					ret = ECONNABORTED;
10450Sstevel@tonic-gate 					goto remove_contract_cleanup;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
10480Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
10490Sstevel@tonic-gate 					goto add;
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
10520Sstevel@tonic-gate 					goto new;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
10550Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
10560Sstevel@tonic-gate 				case SCF_ERROR_IN_USE:
10570Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
10580Sstevel@tonic-gate 					bad_fail(
10590Sstevel@tonic-gate 					"scf_transaction_property_changetype",
10600Sstevel@tonic-gate 					    scf_error());
10610Sstevel@tonic-gate 				}
10620Sstevel@tonic-gate 			}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) == 0) {
10650Sstevel@tonic-gate 				if (scf_iter_property_values(iter, prop) != 0) {
10660Sstevel@tonic-gate 					switch (scf_error()) {
10670Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
10680Sstevel@tonic-gate 					default:
10690Sstevel@tonic-gate 						ret = ECONNABORTED;
10700Sstevel@tonic-gate 						goto remove_contract_cleanup;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 					case SCF_ERROR_NOT_SET:
10730Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
10740Sstevel@tonic-gate 						bad_fail(
10750Sstevel@tonic-gate 						    "scf_iter_property_values",
10760Sstevel@tonic-gate 						    scf_error());
10770Sstevel@tonic-gate 					}
10780Sstevel@tonic-gate 				}
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate next_val:
10810Sstevel@tonic-gate 				val = scf_value_create(h);
10820Sstevel@tonic-gate 				if (val == NULL) {
10830Sstevel@tonic-gate 					assert(scf_error() ==
10840Sstevel@tonic-gate 					    SCF_ERROR_NO_MEMORY);
10850Sstevel@tonic-gate 					ret = ENOMEM;
10860Sstevel@tonic-gate 					goto remove_contract_cleanup;
10870Sstevel@tonic-gate 				}
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 				ret = scf_iter_next_value(iter, val);
10900Sstevel@tonic-gate 				if (ret == -1) {
10910Sstevel@tonic-gate 					switch (scf_error()) {
10920Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
10930Sstevel@tonic-gate 						ret = ECONNABORTED;
10940Sstevel@tonic-gate 						goto remove_contract_cleanup;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 					case SCF_ERROR_DELETED:
10970Sstevel@tonic-gate 						scf_value_destroy(val);
10980Sstevel@tonic-gate 						goto add;
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
11010Sstevel@tonic-gate 					case SCF_ERROR_INVALID_ARGUMENT:
11025040Swesolows 					case SCF_ERROR_PERMISSION_DENIED:
11030Sstevel@tonic-gate 					default:
11040Sstevel@tonic-gate 						bad_fail("scf_iter_next_value",
11050Sstevel@tonic-gate 						    scf_error());
11060Sstevel@tonic-gate 					}
11070Sstevel@tonic-gate 				}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 				if (ret == 1) {
11100Sstevel@tonic-gate 					ret = scf_value_get_count(val, &c);
11110Sstevel@tonic-gate 					assert(ret == 0);
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 					if (c != contract_id) {
11140Sstevel@tonic-gate 						ret = scf_entry_add_value(t_cid,
11150Sstevel@tonic-gate 						    val);
11160Sstevel@tonic-gate 						assert(ret == 0);
11170Sstevel@tonic-gate 					} else {
11180Sstevel@tonic-gate 						scf_value_destroy(val);
11190Sstevel@tonic-gate 					}
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 					goto next_val;
11220Sstevel@tonic-gate 				}
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 				scf_value_destroy(val);
11250Sstevel@tonic-gate 			} else {
11260Sstevel@tonic-gate 				switch (scf_error()) {
11270Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
11280Sstevel@tonic-gate 				default:
11290Sstevel@tonic-gate 					ret = ECONNABORTED;
11300Sstevel@tonic-gate 					goto remove_contract_cleanup;
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 				case SCF_ERROR_TYPE_MISMATCH:
11330Sstevel@tonic-gate 					break;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
11360Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
11370Sstevel@tonic-gate 					bad_fail("scf_property_is_type",
11380Sstevel@tonic-gate 					    scf_error());
11390Sstevel@tonic-gate 				}
11400Sstevel@tonic-gate 			}
11410Sstevel@tonic-gate 		} else {
11420Sstevel@tonic-gate 			switch (scf_error()) {
11430Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
11440Sstevel@tonic-gate 			default:
11450Sstevel@tonic-gate 				ret = ECONNABORTED;
11460Sstevel@tonic-gate 				goto remove_contract_cleanup;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
11490Sstevel@tonic-gate 				scf_entry_destroy(t_cid);
11500Sstevel@tonic-gate 				goto add;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
11530Sstevel@tonic-gate 				break;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
11560Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
11570Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
11580Sstevel@tonic-gate 				bad_fail("scf_pg_get_property", scf_error());
11590Sstevel@tonic-gate 			}
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate new:
11620Sstevel@tonic-gate 			if (scf_transaction_property_new(t, t_cid, pname,
11630Sstevel@tonic-gate 			    SCF_TYPE_COUNT) != 0) {
11640Sstevel@tonic-gate 				switch (scf_error()) {
11650Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
11660Sstevel@tonic-gate 				default:
11670Sstevel@tonic-gate 					ret = ECONNABORTED;
11680Sstevel@tonic-gate 					goto remove_contract_cleanup;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
11710Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
11720Sstevel@tonic-gate 					goto add;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 				case SCF_ERROR_EXISTS:
11750Sstevel@tonic-gate 					goto replace;
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
11780Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
11790Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
11800Sstevel@tonic-gate 					bad_fail("scf_transaction_property_new",
11810Sstevel@tonic-gate 					    scf_error());
11820Sstevel@tonic-gate 				}
11830Sstevel@tonic-gate 			}
11840Sstevel@tonic-gate 		}
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 		ret = scf_transaction_commit(t);
11870Sstevel@tonic-gate 		if (ret == -1) {
11880Sstevel@tonic-gate 			switch (scf_error()) {
11890Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
11900Sstevel@tonic-gate 			default:
11910Sstevel@tonic-gate 				ret = ECONNABORTED;
11920Sstevel@tonic-gate 				goto remove_contract_cleanup;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
11950Sstevel@tonic-gate 				goto add;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
11980Sstevel@tonic-gate 				ret = EPERM;
11990Sstevel@tonic-gate 				goto remove_contract_cleanup;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
12020Sstevel@tonic-gate 				ret = EACCES;
12030Sstevel@tonic-gate 				goto remove_contract_cleanup;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
12060Sstevel@tonic-gate 				ret = EROFS;
12070Sstevel@tonic-gate 				goto remove_contract_cleanup;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
12100Sstevel@tonic-gate 				bad_fail("scf_transaction_commit", scf_error());
12110Sstevel@tonic-gate 			}
12120Sstevel@tonic-gate 		}
12130Sstevel@tonic-gate 		if (ret == 1) {
12140Sstevel@tonic-gate 			ret = 0;
12150Sstevel@tonic-gate 			break;
12160Sstevel@tonic-gate 		}
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 		scf_transaction_destroy_children(t);
12190Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
12200Sstevel@tonic-gate 			switch (scf_error()) {
12210Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
12220Sstevel@tonic-gate 			default:
12230Sstevel@tonic-gate 				ret = ECONNABORTED;
12240Sstevel@tonic-gate 				goto remove_contract_cleanup;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
12270Sstevel@tonic-gate 				goto add;
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
12300Sstevel@tonic-gate 				bad_fail("scf_pg_update", scf_error());
12310Sstevel@tonic-gate 			}
12320Sstevel@tonic-gate 		}
12330Sstevel@tonic-gate 	}
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate remove_contract_cleanup:
12360Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
12370Sstevel@tonic-gate 	scf_transaction_destroy(t);
12380Sstevel@tonic-gate 	scf_iter_destroy(iter);
12390Sstevel@tonic-gate 	scf_property_destroy(prop);
12400Sstevel@tonic-gate 	scf_pg_destroy(pg);
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	return (ret);
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate /*
12460Sstevel@tonic-gate  * Fails with
12470Sstevel@tonic-gate  *   EINVAL - type is invalid
12480Sstevel@tonic-gate  *   ENOMEM
12490Sstevel@tonic-gate  *   ECONNABORTED - repository disconnection
12500Sstevel@tonic-gate  *   EBADF - s_inst is not set
12510Sstevel@tonic-gate  *   ECANCELED - s_inst is deleted
12520Sstevel@tonic-gate  *   EPERM
12530Sstevel@tonic-gate  *   EACCES
12540Sstevel@tonic-gate  *   EROFS
12550Sstevel@tonic-gate  */
12560Sstevel@tonic-gate int
12570Sstevel@tonic-gate restarter_store_contract(scf_instance_t *s_inst, ctid_t contract_id,
12580Sstevel@tonic-gate     restarter_contract_type_t type)
12590Sstevel@tonic-gate {
12600Sstevel@tonic-gate 	scf_handle_t *h;
12610Sstevel@tonic-gate 	scf_transaction_t *t = NULL;
12620Sstevel@tonic-gate 	scf_transaction_entry_t *t_cid = NULL;
12630Sstevel@tonic-gate 	scf_value_t *val;
12640Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
12650Sstevel@tonic-gate 	scf_property_t *prop = NULL;
12660Sstevel@tonic-gate 	scf_iter_t *iter = NULL;
12670Sstevel@tonic-gate 	const char *pname;
12680Sstevel@tonic-gate 	int ret = 0, primary;
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	if (type == RESTARTER_CONTRACT_PRIMARY)
12710Sstevel@tonic-gate 		primary = 1;
12720Sstevel@tonic-gate 	else if (type == RESTARTER_CONTRACT_TRANSIENT)
12730Sstevel@tonic-gate 		primary = 0;
12740Sstevel@tonic-gate 	else
12750Sstevel@tonic-gate 		return (EINVAL);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	h = scf_instance_handle(s_inst);
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	pg = scf_pg_create(h);
12800Sstevel@tonic-gate 	prop = scf_property_create(h);
12810Sstevel@tonic-gate 	iter = scf_iter_create(h);
12820Sstevel@tonic-gate 	t = scf_transaction_create(h);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	if (pg == NULL || prop == NULL || iter == NULL || t == NULL) {
12850Sstevel@tonic-gate 		ret = ENOMEM;
12860Sstevel@tonic-gate 		goto out;
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate add:
12900Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
12910Sstevel@tonic-gate 	ret = instance_get_or_add_pg(s_inst, SCF_PG_RESTARTER,
12920Sstevel@tonic-gate 	    SCF_PG_RESTARTER_TYPE, SCF_PG_RESTARTER_FLAGS, pg);
12930Sstevel@tonic-gate 	if (ret != 0)
12940Sstevel@tonic-gate 		goto out;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	pname = primary ? SCF_PROPERTY_CONTRACT :
12970Sstevel@tonic-gate 	    SCF_PROPERTY_TRANSIENT_CONTRACT;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	for (;;) {
13000Sstevel@tonic-gate 		if (scf_transaction_start(t, pg) != 0) {
13010Sstevel@tonic-gate 			switch (scf_error()) {
13020Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
13030Sstevel@tonic-gate 			default:
13040Sstevel@tonic-gate 				ret = ECONNABORTED;
13050Sstevel@tonic-gate 				goto out;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
13080Sstevel@tonic-gate 				goto add;
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
13110Sstevel@tonic-gate 				ret = EPERM;
13120Sstevel@tonic-gate 				goto out;
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
13150Sstevel@tonic-gate 				ret = EACCES;
13160Sstevel@tonic-gate 				goto out;
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
13190Sstevel@tonic-gate 				ret = EROFS;
13200Sstevel@tonic-gate 				goto out;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
13230Sstevel@tonic-gate 			case SCF_ERROR_IN_USE:
13240Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
13250Sstevel@tonic-gate 				bad_fail("scf_transaction_start", scf_error());
13260Sstevel@tonic-gate 			}
13270Sstevel@tonic-gate 		}
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 		t_cid = scf_entry_create(h);
13300Sstevel@tonic-gate 		if (t_cid == NULL) {
13310Sstevel@tonic-gate 			ret = ENOMEM;
13320Sstevel@tonic-gate 			goto out;
13330Sstevel@tonic-gate 		}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 		if (scf_pg_get_property(pg, pname, prop) == 0) {
13360Sstevel@tonic-gate replace:
13370Sstevel@tonic-gate 			if (scf_transaction_property_change_type(t, t_cid,
13380Sstevel@tonic-gate 			    pname, SCF_TYPE_COUNT) != 0) {
13390Sstevel@tonic-gate 				switch (scf_error()) {
13400Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
13410Sstevel@tonic-gate 				default:
13420Sstevel@tonic-gate 					ret = ECONNABORTED;
13430Sstevel@tonic-gate 					goto out;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
13460Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
13470Sstevel@tonic-gate 					goto add;
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
13500Sstevel@tonic-gate 					goto new;
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
13530Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
13540Sstevel@tonic-gate 				case SCF_ERROR_IN_USE:
13550Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
13560Sstevel@tonic-gate 					bad_fail(
13570Sstevel@tonic-gate 					"scf_transaction_propert_change_type",
13580Sstevel@tonic-gate 					    scf_error());
13590Sstevel@tonic-gate 				}
13600Sstevel@tonic-gate 			}
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 			if (scf_property_is_type(prop, SCF_TYPE_COUNT) == 0) {
13630Sstevel@tonic-gate 				if (scf_iter_property_values(iter, prop) != 0) {
13640Sstevel@tonic-gate 					switch (scf_error()) {
13650Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
13660Sstevel@tonic-gate 					default:
13670Sstevel@tonic-gate 						ret = ECONNABORTED;
13680Sstevel@tonic-gate 						goto out;
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 					case SCF_ERROR_NOT_SET:
13710Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
13720Sstevel@tonic-gate 						bad_fail(
13730Sstevel@tonic-gate 						    "scf_iter_property_values",
13740Sstevel@tonic-gate 						    scf_error());
13750Sstevel@tonic-gate 					}
13760Sstevel@tonic-gate 				}
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate next_val:
13790Sstevel@tonic-gate 				val = scf_value_create(h);
13800Sstevel@tonic-gate 				if (val == NULL) {
13810Sstevel@tonic-gate 					assert(scf_error() ==
13820Sstevel@tonic-gate 					    SCF_ERROR_NO_MEMORY);
13830Sstevel@tonic-gate 					ret = ENOMEM;
13840Sstevel@tonic-gate 					goto out;
13850Sstevel@tonic-gate 				}
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 				ret = scf_iter_next_value(iter, val);
13880Sstevel@tonic-gate 				if (ret == -1) {
13890Sstevel@tonic-gate 					switch (scf_error()) {
13900Sstevel@tonic-gate 					case SCF_ERROR_CONNECTION_BROKEN:
13910Sstevel@tonic-gate 					default:
13920Sstevel@tonic-gate 						ret = ECONNABORTED;
13930Sstevel@tonic-gate 						goto out;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 					case SCF_ERROR_DELETED:
13960Sstevel@tonic-gate 						scf_value_destroy(val);
13970Sstevel@tonic-gate 						goto add;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 					case SCF_ERROR_HANDLE_MISMATCH:
14000Sstevel@tonic-gate 					case SCF_ERROR_INVALID_ARGUMENT:
14015040Swesolows 					case SCF_ERROR_PERMISSION_DENIED:
14020Sstevel@tonic-gate 						bad_fail(
14030Sstevel@tonic-gate 						    "scf_iter_next_value",
14040Sstevel@tonic-gate 						    scf_error());
14050Sstevel@tonic-gate 					}
14060Sstevel@tonic-gate 				}
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 				if (ret == 1) {
14090Sstevel@tonic-gate 					ret = scf_entry_add_value(t_cid, val);
14100Sstevel@tonic-gate 					assert(ret == 0);
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 					goto next_val;
14130Sstevel@tonic-gate 				}
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 				scf_value_destroy(val);
14160Sstevel@tonic-gate 			} else {
14170Sstevel@tonic-gate 				switch (scf_error()) {
14180Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
14190Sstevel@tonic-gate 				default:
14200Sstevel@tonic-gate 					ret = ECONNABORTED;
14210Sstevel@tonic-gate 					goto out;
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 				case SCF_ERROR_TYPE_MISMATCH:
14240Sstevel@tonic-gate 					break;
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
14270Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
14280Sstevel@tonic-gate 					bad_fail("scf_property_is_type",
14290Sstevel@tonic-gate 					    scf_error());
14300Sstevel@tonic-gate 				}
14310Sstevel@tonic-gate 			}
14320Sstevel@tonic-gate 		} else {
14330Sstevel@tonic-gate 			switch (scf_error()) {
14340Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
14350Sstevel@tonic-gate 			default:
14360Sstevel@tonic-gate 				ret = ECONNABORTED;
14370Sstevel@tonic-gate 				goto out;
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
14400Sstevel@tonic-gate 				scf_entry_destroy(t_cid);
14410Sstevel@tonic-gate 				goto add;
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
14440Sstevel@tonic-gate 				break;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
14470Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
14480Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
14490Sstevel@tonic-gate 				bad_fail("scf_pg_get_property", scf_error());
14500Sstevel@tonic-gate 			}
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate new:
14530Sstevel@tonic-gate 			if (scf_transaction_property_new(t, t_cid, pname,
14540Sstevel@tonic-gate 			    SCF_TYPE_COUNT) != 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_DELETED:
14620Sstevel@tonic-gate 					scf_entry_destroy(t_cid);
14630Sstevel@tonic-gate 					goto add;
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 				case SCF_ERROR_EXISTS:
14660Sstevel@tonic-gate 					goto replace;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
14690Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
14700Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
14710Sstevel@tonic-gate 					bad_fail("scf_transaction_property_new",
14720Sstevel@tonic-gate 					    scf_error());
14730Sstevel@tonic-gate 				}
14740Sstevel@tonic-gate 			}
14750Sstevel@tonic-gate 		}
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 		val = scf_value_create(h);
14780Sstevel@tonic-gate 		if (val == NULL) {
14790Sstevel@tonic-gate 			assert(scf_error() == SCF_ERROR_NO_MEMORY);
14800Sstevel@tonic-gate 			ret = ENOMEM;
14810Sstevel@tonic-gate 			goto out;
14820Sstevel@tonic-gate 		}
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		scf_value_set_count(val, contract_id);
14850Sstevel@tonic-gate 		ret = scf_entry_add_value(t_cid, val);
14860Sstevel@tonic-gate 		assert(ret == 0);
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 		ret = scf_transaction_commit(t);
14890Sstevel@tonic-gate 		if (ret == -1) {
14900Sstevel@tonic-gate 			switch (scf_error()) {
14910Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
14920Sstevel@tonic-gate 			default:
14930Sstevel@tonic-gate 				ret = ECONNABORTED;
14940Sstevel@tonic-gate 				goto out;
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
14970Sstevel@tonic-gate 				goto add;
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 			case SCF_ERROR_PERMISSION_DENIED:
15000Sstevel@tonic-gate 				ret = EPERM;
15010Sstevel@tonic-gate 				goto out;
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_ACCESS:
15040Sstevel@tonic-gate 				ret = EACCES;
15050Sstevel@tonic-gate 				goto out;
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 			case SCF_ERROR_BACKEND_READONLY:
15080Sstevel@tonic-gate 				ret = EROFS;
15090Sstevel@tonic-gate 				goto out;
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
15120Sstevel@tonic-gate 				bad_fail("scf_transaction_commit", scf_error());
15130Sstevel@tonic-gate 			}
15140Sstevel@tonic-gate 		}
15150Sstevel@tonic-gate 		if (ret == 1) {
15160Sstevel@tonic-gate 			ret = 0;
15170Sstevel@tonic-gate 			break;
15180Sstevel@tonic-gate 		}
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 		scf_transaction_destroy_children(t);
15210Sstevel@tonic-gate 		if (scf_pg_update(pg) == -1) {
15220Sstevel@tonic-gate 			switch (scf_error()) {
15230Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
15240Sstevel@tonic-gate 			default:
15250Sstevel@tonic-gate 				ret = ECONNABORTED;
15260Sstevel@tonic-gate 				goto out;
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
15290Sstevel@tonic-gate 				goto add;
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
15320Sstevel@tonic-gate 				bad_fail("scf_pg_update", scf_error());
15330Sstevel@tonic-gate 			}
15340Sstevel@tonic-gate 		}
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate out:
15380Sstevel@tonic-gate 	scf_transaction_destroy_children(t);
15390Sstevel@tonic-gate 	scf_transaction_destroy(t);
15400Sstevel@tonic-gate 	scf_iter_destroy(iter);
15410Sstevel@tonic-gate 	scf_property_destroy(prop);
15420Sstevel@tonic-gate 	scf_pg_destroy(pg);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	return (ret);
15450Sstevel@tonic-gate }
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate int
15480Sstevel@tonic-gate restarter_rm_libs_loadable()
15490Sstevel@tonic-gate {
15500Sstevel@tonic-gate 	void *libhndl;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	if (method_context_safety)
15530Sstevel@tonic-gate 		return (1);
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	if ((libhndl = dlopen("libpool.so", RTLD_LAZY | RTLD_LOCAL)) == NULL)
15560Sstevel@tonic-gate 		return (0);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	(void) dlclose(libhndl);
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	if ((libhndl = dlopen("libproject.so", RTLD_LAZY | RTLD_LOCAL)) == NULL)
15610Sstevel@tonic-gate 		return (0);
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	(void) dlclose(libhndl);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	method_context_safety = 1;
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	return (1);
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate static int
15720Sstevel@tonic-gate get_astring_val(scf_propertygroup_t *pg, const char *name, char *buf,
15730Sstevel@tonic-gate     size_t bufsz, scf_property_t *prop, scf_value_t *val)
15740Sstevel@tonic-gate {
15750Sstevel@tonic-gate 	ssize_t szret;
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	if (scf_pg_get_property(pg, name, prop) != SCF_SUCCESS) {
15780Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
15790Sstevel@tonic-gate 			uu_die(rcbroken);
15800Sstevel@tonic-gate 		return (-1);
15810Sstevel@tonic-gate 	}
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
15840Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN)
15850Sstevel@tonic-gate 			uu_die(rcbroken);
15860Sstevel@tonic-gate 		return (-1);
15870Sstevel@tonic-gate 	}
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	szret = scf_value_get_astring(val, buf, bufsz);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	return (szret >= 0 ? 0 : -1);
15920Sstevel@tonic-gate }
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate /*
15950Sstevel@tonic-gate  * Try to load mcp->pwd, if it isn't already.
15960Sstevel@tonic-gate  * Fails with
15970Sstevel@tonic-gate  *   ENOMEM - malloc() failed
15980Sstevel@tonic-gate  *   ENOENT - no entry found
15990Sstevel@tonic-gate  *   EIO - I/O error
16000Sstevel@tonic-gate  *   EMFILE - process out of file descriptors
16010Sstevel@tonic-gate  *   ENFILE - system out of file handles
16020Sstevel@tonic-gate  */
16030Sstevel@tonic-gate static int
16040Sstevel@tonic-gate lookup_pwd(struct method_context *mcp)
16050Sstevel@tonic-gate {
16060Sstevel@tonic-gate 	struct passwd *pwdp;
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	if (mcp->pwbuf != NULL && mcp->pwd.pw_uid == mcp->uid)
16090Sstevel@tonic-gate 		return (0);
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	if (mcp->pwbuf == NULL) {
16120Sstevel@tonic-gate 		mcp->pwbufsz = sysconf(_SC_GETPW_R_SIZE_MAX);
16130Sstevel@tonic-gate 		assert(mcp->pwbufsz >= 0);
16140Sstevel@tonic-gate 		mcp->pwbuf = malloc(mcp->pwbufsz);
16150Sstevel@tonic-gate 		if (mcp->pwbuf == NULL)
16160Sstevel@tonic-gate 			return (ENOMEM);
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	do {
16200Sstevel@tonic-gate 		errno = 0;
16210Sstevel@tonic-gate 		pwdp = getpwuid_r(mcp->uid, &mcp->pwd, mcp->pwbuf,
16220Sstevel@tonic-gate 		    mcp->pwbufsz);
16230Sstevel@tonic-gate 	} while (pwdp == NULL && errno == EINTR);
16240Sstevel@tonic-gate 	if (pwdp != NULL)
16250Sstevel@tonic-gate 		return (0);
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	free(mcp->pwbuf);
16280Sstevel@tonic-gate 	mcp->pwbuf = NULL;
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	switch (errno) {
16310Sstevel@tonic-gate 	case 0:
16320Sstevel@tonic-gate 	default:
16330Sstevel@tonic-gate 		/*
16340Sstevel@tonic-gate 		 * Until bug 5065780 is fixed, getpwuid_r() can fail with
16350Sstevel@tonic-gate 		 * ENOENT, particularly on the miniroot.  Since the
16360Sstevel@tonic-gate 		 * documentation is inaccurate, we'll return ENOENT for unknown
16370Sstevel@tonic-gate 		 * errors.
16380Sstevel@tonic-gate 		 */
16390Sstevel@tonic-gate 		return (ENOENT);
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	case EIO:
16420Sstevel@tonic-gate 	case EMFILE:
16430Sstevel@tonic-gate 	case ENFILE:
16440Sstevel@tonic-gate 		return (errno);
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	case ERANGE:
16470Sstevel@tonic-gate 		bad_fail("getpwuid_r", errno);
16480Sstevel@tonic-gate 		/* NOTREACHED */
16490Sstevel@tonic-gate 	}
16500Sstevel@tonic-gate }
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate /*
16530Sstevel@tonic-gate  * Get the user id for str.  Returns 0 on success or
16540Sstevel@tonic-gate  *   ERANGE	the uid is too big
16550Sstevel@tonic-gate  *   EINVAL	the string starts with a digit, but is not a valid uid
16560Sstevel@tonic-gate  *   ENOMEM	out of memory
16570Sstevel@tonic-gate  *   ENOENT	no passwd entry for str
16580Sstevel@tonic-gate  *   EIO	an I/O error has occurred
16590Sstevel@tonic-gate  *   EMFILE/ENFILE  out of file descriptors
16600Sstevel@tonic-gate  */
16610Sstevel@tonic-gate int
16620Sstevel@tonic-gate get_uid(const char *str, struct method_context *ci, uid_t *uidp)
16630Sstevel@tonic-gate {
16640Sstevel@tonic-gate 	if (isdigit(str[0])) {
16650Sstevel@tonic-gate 		uid_t uid;
16660Sstevel@tonic-gate 		char *cp;
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 		errno = 0;
16690Sstevel@tonic-gate 		uid = strtol(str, &cp, 10);
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 		if (uid == 0 && errno != 0) {
16720Sstevel@tonic-gate 			assert(errno != EINVAL);
16730Sstevel@tonic-gate 			return (errno);
16740Sstevel@tonic-gate 		}
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 		for (; *cp != '\0'; ++cp)
16770Sstevel@tonic-gate 			if (*cp != ' ' || *cp != '\t')
16780Sstevel@tonic-gate 				return (EINVAL);
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 		if (uid > UID_MAX)
16810Sstevel@tonic-gate 			return (EINVAL);
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 		*uidp = uid;
16840Sstevel@tonic-gate 		return (0);
16850Sstevel@tonic-gate 	} else {
16860Sstevel@tonic-gate 		struct passwd *pwdp;
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 		if (ci->pwbuf == NULL) {
16890Sstevel@tonic-gate 			ci->pwbufsz = sysconf(_SC_GETPW_R_SIZE_MAX);
16900Sstevel@tonic-gate 			ci->pwbuf = malloc(ci->pwbufsz);
16910Sstevel@tonic-gate 			if (ci->pwbuf == NULL)
16920Sstevel@tonic-gate 				return (ENOMEM);
16930Sstevel@tonic-gate 		}
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 		do {
16960Sstevel@tonic-gate 			errno = 0;
16970Sstevel@tonic-gate 			pwdp =
16980Sstevel@tonic-gate 			    getpwnam_r(str, &ci->pwd, ci->pwbuf, ci->pwbufsz);
16990Sstevel@tonic-gate 		} while (pwdp == NULL && errno == EINTR);
17000Sstevel@tonic-gate 
17010Sstevel@tonic-gate 		if (pwdp != NULL) {
17020Sstevel@tonic-gate 			*uidp = ci->pwd.pw_uid;
17030Sstevel@tonic-gate 			return (0);
17040Sstevel@tonic-gate 		} else {
17050Sstevel@tonic-gate 			free(ci->pwbuf);
17060Sstevel@tonic-gate 			ci->pwbuf = NULL;
17070Sstevel@tonic-gate 			switch (errno) {
17080Sstevel@tonic-gate 			case 0:
17090Sstevel@tonic-gate 				return (ENOENT);
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 			case ENOENT:
17120Sstevel@tonic-gate 			case EIO:
17130Sstevel@tonic-gate 			case EMFILE:
17140Sstevel@tonic-gate 			case ENFILE:
17150Sstevel@tonic-gate 				return (errno);
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 			case ERANGE:
17180Sstevel@tonic-gate 			default:
17190Sstevel@tonic-gate 				bad_fail("getpwnam_r", errno);
17200Sstevel@tonic-gate 				/* NOTREACHED */
17210Sstevel@tonic-gate 			}
17220Sstevel@tonic-gate 		}
17230Sstevel@tonic-gate 	}
17240Sstevel@tonic-gate }
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate gid_t
17270Sstevel@tonic-gate get_gid(const char *str)
17280Sstevel@tonic-gate {
17290Sstevel@tonic-gate 	if (isdigit(str[0])) {
17300Sstevel@tonic-gate 		gid_t gid;
17310Sstevel@tonic-gate 		char *cp;
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 		errno = 0;
17340Sstevel@tonic-gate 		gid = strtol(str, &cp, 10);
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 		if (gid == 0 && errno != 0)
17374321Scasper 			return ((gid_t)-1);
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 		for (; *cp != '\0'; ++cp)
17400Sstevel@tonic-gate 			if (*cp != ' ' || *cp != '\t')
17414321Scasper 				return ((gid_t)-1);
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 		return (gid);
17440Sstevel@tonic-gate 	} else {
17450Sstevel@tonic-gate 		struct group grp, *ret;
17460Sstevel@tonic-gate 		char *buffer;
17470Sstevel@tonic-gate 		size_t buflen;
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 		buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
17500Sstevel@tonic-gate 		buffer = malloc(buflen);
17510Sstevel@tonic-gate 		if (buffer == NULL)
17520Sstevel@tonic-gate 			uu_die(allocfail);
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 		errno = 0;
17550Sstevel@tonic-gate 		ret = getgrnam_r(str, &grp, buffer, buflen);
17560Sstevel@tonic-gate 		free(buffer);
17570Sstevel@tonic-gate 
17584321Scasper 		return (ret == NULL ? (gid_t)-1 : grp.gr_gid);
17590Sstevel@tonic-gate 	}
17600Sstevel@tonic-gate }
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate /*
17630Sstevel@tonic-gate  * Fails with
17640Sstevel@tonic-gate  *   ENOMEM - out of memory
17650Sstevel@tonic-gate  *   ENOENT - no passwd entry
17660Sstevel@tonic-gate  *	      no project entry
17670Sstevel@tonic-gate  *   EIO - an I/O error occurred
17680Sstevel@tonic-gate  *   EMFILE - the process is out of file descriptors
17690Sstevel@tonic-gate  *   ENFILE - the system is out of file handles
17700Sstevel@tonic-gate  *   ERANGE - the project id is out of range
17710Sstevel@tonic-gate  *   EINVAL - str is invalid
17720Sstevel@tonic-gate  *   E2BIG - the project entry was too big
17730Sstevel@tonic-gate  *   -1 - the name service switch is misconfigured
17740Sstevel@tonic-gate  */
17750Sstevel@tonic-gate int
17760Sstevel@tonic-gate get_projid(const char *str, struct method_context *cip)
17770Sstevel@tonic-gate {
17780Sstevel@tonic-gate 	int ret;
17790Sstevel@tonic-gate 	void *buf;
17800Sstevel@tonic-gate 	const size_t bufsz = PROJECT_BUFSZ;
17810Sstevel@tonic-gate 	struct project proj, *pp;
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	if (strcmp(str, ":default") == 0) {
17840Sstevel@tonic-gate 		if (cip->uid == 0) {
17850Sstevel@tonic-gate 			/* Don't change project for root services */
17860Sstevel@tonic-gate 			cip->project = NULL;
17870Sstevel@tonic-gate 			return (0);
17880Sstevel@tonic-gate 		}
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 		switch (ret = lookup_pwd(cip)) {
17910Sstevel@tonic-gate 		case 0:
17920Sstevel@tonic-gate 			break;
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 		case ENOMEM:
17950Sstevel@tonic-gate 		case ENOENT:
17960Sstevel@tonic-gate 		case EIO:
17970Sstevel@tonic-gate 		case EMFILE:
17980Sstevel@tonic-gate 		case ENFILE:
17990Sstevel@tonic-gate 			return (ret);
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 		default:
18020Sstevel@tonic-gate 			bad_fail("lookup_pwd", ret);
18030Sstevel@tonic-gate 		}
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 		buf = malloc(bufsz);
18060Sstevel@tonic-gate 		if (buf == NULL)
18070Sstevel@tonic-gate 			return (ENOMEM);
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 		do {
18100Sstevel@tonic-gate 			errno = 0;
18110Sstevel@tonic-gate 			pp = getdefaultproj(cip->pwd.pw_name, &proj, buf,
18120Sstevel@tonic-gate 			    bufsz);
18130Sstevel@tonic-gate 		} while (pp == NULL && errno == EINTR);
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 		/* to be continued ... */
18160Sstevel@tonic-gate 	} else {
18170Sstevel@tonic-gate 		projid_t projid;
18180Sstevel@tonic-gate 		char *cp;
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 		if (!isdigit(str[0])) {
18210Sstevel@tonic-gate 			cip->project = strdup(str);
18220Sstevel@tonic-gate 			return (cip->project != NULL ? 0 : ENOMEM);
18230Sstevel@tonic-gate 		}
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 		errno = 0;
18260Sstevel@tonic-gate 		projid = strtol(str, &cp, 10);
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 		if (projid == 0 && errno != 0) {
18290Sstevel@tonic-gate 			assert(errno == ERANGE);
18300Sstevel@tonic-gate 			return (errno);
18310Sstevel@tonic-gate 		}
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 		for (; *cp != '\0'; ++cp)
18340Sstevel@tonic-gate 			if (*cp != ' ' || *cp != '\t')
18350Sstevel@tonic-gate 				return (EINVAL);
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 		if (projid > MAXPROJID)
18380Sstevel@tonic-gate 			return (ERANGE);
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 		buf = malloc(bufsz);
18410Sstevel@tonic-gate 		if (buf == NULL)
18420Sstevel@tonic-gate 			return (ENOMEM);
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 		do {
18450Sstevel@tonic-gate 			errno = 0;
18460Sstevel@tonic-gate 			pp = getprojbyid(projid, &proj, buf, bufsz);
18470Sstevel@tonic-gate 		} while (pp == NULL && errno == EINTR);
18480Sstevel@tonic-gate 	}
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	if (pp) {
18510Sstevel@tonic-gate 		cip->project = strdup(pp->pj_name);
18520Sstevel@tonic-gate 		free(buf);
18530Sstevel@tonic-gate 		return (cip->project != NULL ? 0 : ENOMEM);
18540Sstevel@tonic-gate 	}
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	free(buf);
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	switch (errno) {
18590Sstevel@tonic-gate 	case 0:
18600Sstevel@tonic-gate 		return (ENOENT);
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 	case EIO:
18630Sstevel@tonic-gate 	case EMFILE:
18640Sstevel@tonic-gate 	case ENFILE:
18650Sstevel@tonic-gate 		return (errno);
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	case ERANGE:
18680Sstevel@tonic-gate 		return (E2BIG);
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 	default:
18710Sstevel@tonic-gate 		return (-1);
18720Sstevel@tonic-gate 	}
18730Sstevel@tonic-gate }
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate /*
18760Sstevel@tonic-gate  * Parse the supp_groups property value and populate ci->groups.  Returns
18770Sstevel@tonic-gate  * EINVAL (get_gid() failed for one of the components), E2BIG (the property has
18780Sstevel@tonic-gate  * more than NGROUPS_MAX-1 groups), or 0 on success.
18790Sstevel@tonic-gate  */
18800Sstevel@tonic-gate int
18810Sstevel@tonic-gate get_groups(char *str, struct method_context *ci)
18820Sstevel@tonic-gate {
18830Sstevel@tonic-gate 	char *cp, *end, *next;
18840Sstevel@tonic-gate 	uint_t i;
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	const char * const whitespace = " \t";
18870Sstevel@tonic-gate 	const char * const illegal = ", \t";
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 	if (str[0] == '\0') {
18900Sstevel@tonic-gate 		ci->ngroups = 0;
18910Sstevel@tonic-gate 		return (0);
18920Sstevel@tonic-gate 	}
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	for (cp = str, i = 0; *cp != '\0'; ) {
18950Sstevel@tonic-gate 		/* skip whitespace */
18960Sstevel@tonic-gate 		cp += strspn(cp, whitespace);
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 		/* find the end */
18990Sstevel@tonic-gate 		end = cp + strcspn(cp, illegal);
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 		/* skip whitespace after end */
19020Sstevel@tonic-gate 		next = end + strspn(end, whitespace);
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 		/* if there's a comma, it separates the fields */
19050Sstevel@tonic-gate 		if (*next == ',')
19060Sstevel@tonic-gate 			++next;
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 		*end = '\0';
19090Sstevel@tonic-gate 
19104321Scasper 		if ((ci->groups[i] = get_gid(cp)) == (gid_t)-1) {
19110Sstevel@tonic-gate 			ci->ngroups = 0;
19120Sstevel@tonic-gate 			return (EINVAL);
19130Sstevel@tonic-gate 		}
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 		++i;
19160Sstevel@tonic-gate 		if (i > NGROUPS_MAX - 1) {
19170Sstevel@tonic-gate 			ci->ngroups = 0;
19180Sstevel@tonic-gate 			return (E2BIG);
19190Sstevel@tonic-gate 		}
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 		cp = next;
19220Sstevel@tonic-gate 	}
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	ci->ngroups = i;
19250Sstevel@tonic-gate 	return (0);
19260Sstevel@tonic-gate }
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate /*
19290Sstevel@tonic-gate  * Eventually, we will return a structured error in the case of
19300Sstevel@tonic-gate  * retryable or abortable failures such as memory allocation errors and
19310Sstevel@tonic-gate  * repository connection failures.  For now, these failures are just
19320Sstevel@tonic-gate  * encoded in the failure string.
19330Sstevel@tonic-gate  */
19340Sstevel@tonic-gate static const char *
19350Sstevel@tonic-gate get_profile(scf_propertygroup_t *pg, scf_property_t *prop, scf_value_t *val,
19360Sstevel@tonic-gate     const char *cmdline, struct method_context *ci)
19370Sstevel@tonic-gate {
19380Sstevel@tonic-gate 	char *buf = ci->vbuf;
19390Sstevel@tonic-gate 	ssize_t buf_sz = ci->vbuf_sz;
19400Sstevel@tonic-gate 	char cmd[PATH_MAX];
19410Sstevel@tonic-gate 	char *cp, *value;
19420Sstevel@tonic-gate 	const char *cmdp;
19430Sstevel@tonic-gate 	execattr_t *eap;
19440Sstevel@tonic-gate 	char *errstr = NULL;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	if (get_astring_val(pg, SCF_PROPERTY_PROFILE, buf, buf_sz, prop, val) !=
19470Sstevel@tonic-gate 	    0)
19480Sstevel@tonic-gate 		return ("Could not get profile property.");
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	/* Extract the command from the command line. */
19510Sstevel@tonic-gate 	cp = strpbrk(cmdline, " \t");
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 	if (cp == NULL) {
19540Sstevel@tonic-gate 		cmdp = cmdline;
19550Sstevel@tonic-gate 	} else {
19560Sstevel@tonic-gate 		(void) strncpy(cmd, cmdline, cp - cmdline);
19570Sstevel@tonic-gate 		cmd[cp - cmdline] = '\0';
19580Sstevel@tonic-gate 		cmdp = cmd;
19590Sstevel@tonic-gate 	}
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 	/* Require that cmdp[0] == '/'? */
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 	eap = getexecprof(buf, KV_COMMAND, cmdp, GET_ONE);
19640Sstevel@tonic-gate 	if (eap == NULL)
19650Sstevel@tonic-gate 		return ("Could not find profile.");
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	/* Based on pfexec.c */
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	/* Get the euid first so we don't override ci->pwd for the uid. */
19700Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_EUID_KW)) != NULL) {
19710Sstevel@tonic-gate 		if (get_uid(value, ci, &ci->euid) != 0) {
19724321Scasper 			ci->euid = (uid_t)-1;
19730Sstevel@tonic-gate 			errstr = "Could not interpret profile euid.";
19740Sstevel@tonic-gate 			goto out;
19750Sstevel@tonic-gate 		}
19760Sstevel@tonic-gate 	}
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_UID_KW)) != NULL) {
19790Sstevel@tonic-gate 		if (get_uid(value, ci, &ci->uid) != 0) {
19804321Scasper 			ci->euid = ci->uid = (uid_t)-1;
19810Sstevel@tonic-gate 			errstr = "Could not interpret profile uid.";
19820Sstevel@tonic-gate 			goto out;
19830Sstevel@tonic-gate 		}
19840Sstevel@tonic-gate 		ci->euid = ci->uid;
19850Sstevel@tonic-gate 	}
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_GID_KW)) != NULL) {
19880Sstevel@tonic-gate 		ci->egid = ci->gid = get_gid(value);
19894321Scasper 		if (ci->gid == (gid_t)-1) {
19900Sstevel@tonic-gate 			errstr = "Could not interpret profile gid.";
19910Sstevel@tonic-gate 			goto out;
19920Sstevel@tonic-gate 		}
19930Sstevel@tonic-gate 	}
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_EGID_KW)) != NULL) {
19960Sstevel@tonic-gate 		ci->egid = get_gid(value);
19974321Scasper 		if (ci->egid == (gid_t)-1) {
19980Sstevel@tonic-gate 			errstr = "Could not interpret profile egid.";
19990Sstevel@tonic-gate 			goto out;
20000Sstevel@tonic-gate 		}
20010Sstevel@tonic-gate 	}
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_LPRIV_KW)) != NULL) {
20040Sstevel@tonic-gate 		ci->lpriv_set = priv_str_to_set(value, ",", NULL);
20050Sstevel@tonic-gate 		if (ci->lpriv_set == NULL) {
20060Sstevel@tonic-gate 			if (errno != EINVAL)
20070Sstevel@tonic-gate 				errstr = ALLOCFAIL;
20080Sstevel@tonic-gate 			else
20090Sstevel@tonic-gate 				errstr = "Could not interpret profile "
20100Sstevel@tonic-gate 				    "limitprivs.";
20110Sstevel@tonic-gate 			goto out;
20120Sstevel@tonic-gate 		}
20130Sstevel@tonic-gate 	}
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	if ((value = kva_match(eap->attr, EXECATTR_IPRIV_KW)) != NULL) {
20160Sstevel@tonic-gate 		ci->priv_set = priv_str_to_set(value, ",", NULL);
20170Sstevel@tonic-gate 		if (ci->priv_set == NULL) {
20180Sstevel@tonic-gate 			if (errno != EINVAL)
20190Sstevel@tonic-gate 				errstr = ALLOCFAIL;
20200Sstevel@tonic-gate 			else
20210Sstevel@tonic-gate 				errstr = "Could not interpret profile privs.";
20220Sstevel@tonic-gate 			goto out;
20230Sstevel@tonic-gate 		}
20240Sstevel@tonic-gate 	}
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate out:
20270Sstevel@tonic-gate 	free_execattr(eap);
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	return (errstr);
20300Sstevel@tonic-gate }
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate /*
20330Sstevel@tonic-gate  * Eventually, we will return a structured error in the case of
20340Sstevel@tonic-gate  * retryable or abortable failures such as memory allocation errors and
20350Sstevel@tonic-gate  * repository connection failures.  For now, these failures are just
20360Sstevel@tonic-gate  * encoded in the failure string.
20370Sstevel@tonic-gate  */
20380Sstevel@tonic-gate static const char *
20390Sstevel@tonic-gate get_ids(scf_propertygroup_t *pg, scf_property_t *prop, scf_value_t *val,
20400Sstevel@tonic-gate     struct method_context *ci)
20410Sstevel@tonic-gate {
20420Sstevel@tonic-gate 	const char *errstr = NULL;
20430Sstevel@tonic-gate 	char *vbuf = ci->vbuf;
20440Sstevel@tonic-gate 	ssize_t vbuf_sz = ci->vbuf_sz;
20450Sstevel@tonic-gate 	int r;
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 	if (get_astring_val(pg, SCF_PROPERTY_USER, vbuf, vbuf_sz, prop, val) !=
20480Sstevel@tonic-gate 	    0) {
20490Sstevel@tonic-gate 		errstr = "Could not get user property.";
20500Sstevel@tonic-gate 		goto out;
20510Sstevel@tonic-gate 	}
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	if (get_uid(vbuf, ci, &ci->uid) != 0) {
20544321Scasper 		ci->uid = (uid_t)-1;
20550Sstevel@tonic-gate 		errstr = "Could not interpret user property.";
20560Sstevel@tonic-gate 		goto out;
20570Sstevel@tonic-gate 	}
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	if (get_astring_val(pg, SCF_PROPERTY_GROUP, vbuf, vbuf_sz, prop, val) !=
20600Sstevel@tonic-gate 	    0) {
20610Sstevel@tonic-gate 		errstr = "Could not get group property.";
20620Sstevel@tonic-gate 		goto out;
20630Sstevel@tonic-gate 	}
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") != 0) {
20660Sstevel@tonic-gate 		ci->gid = get_gid(vbuf);
20674321Scasper 		if (ci->gid == (gid_t)-1) {
20680Sstevel@tonic-gate 			errstr = "Could not interpret group property.";
20690Sstevel@tonic-gate 			goto out;
20700Sstevel@tonic-gate 		}
20710Sstevel@tonic-gate 	} else {
20720Sstevel@tonic-gate 		switch (r = lookup_pwd(ci)) {
20730Sstevel@tonic-gate 		case 0:
20740Sstevel@tonic-gate 			ci->gid = ci->pwd.pw_gid;
20750Sstevel@tonic-gate 			break;
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 		case ENOENT:
20784321Scasper 			ci->gid = (gid_t)-1;
20790Sstevel@tonic-gate 			errstr = "No passwd entry.";
20800Sstevel@tonic-gate 			goto out;
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 		case ENOMEM:
20830Sstevel@tonic-gate 			errstr = "Out of memory.";
20840Sstevel@tonic-gate 			goto out;
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 		case EIO:
20870Sstevel@tonic-gate 		case EMFILE:
20880Sstevel@tonic-gate 		case ENFILE:
20890Sstevel@tonic-gate 			errstr = "getpwuid_r() failed.";
20900Sstevel@tonic-gate 			goto out;
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 		default:
20930Sstevel@tonic-gate 			bad_fail("lookup_pwd", r);
20940Sstevel@tonic-gate 		}
20950Sstevel@tonic-gate 	}
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	if (get_astring_val(pg, SCF_PROPERTY_SUPP_GROUPS, vbuf, vbuf_sz, prop,
20980Sstevel@tonic-gate 	    val) != 0) {
20990Sstevel@tonic-gate 		errstr = "Could not get supplemental groups property.";
21000Sstevel@tonic-gate 		goto out;
21010Sstevel@tonic-gate 	}
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") != 0) {
21040Sstevel@tonic-gate 		switch (r = get_groups(vbuf, ci)) {
21050Sstevel@tonic-gate 		case 0:
21060Sstevel@tonic-gate 			break;
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 		case EINVAL:
21090Sstevel@tonic-gate 			errstr =
21100Sstevel@tonic-gate 			    "Could not interpret supplemental groups property.";
21110Sstevel@tonic-gate 			goto out;
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 		case E2BIG:
21140Sstevel@tonic-gate 			errstr = "Too many supplemental groups.";
21150Sstevel@tonic-gate 			goto out;
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 		default:
21180Sstevel@tonic-gate 			bad_fail("get_groups", r);
21190Sstevel@tonic-gate 		}
21200Sstevel@tonic-gate 	} else {
21210Sstevel@tonic-gate 		ci->ngroups = -1;
21220Sstevel@tonic-gate 	}
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 	if (get_astring_val(pg, SCF_PROPERTY_PRIVILEGES, vbuf, vbuf_sz, prop,
21250Sstevel@tonic-gate 	    val) != 0) {
21260Sstevel@tonic-gate 		errstr = "Could not get privileges property.";
21270Sstevel@tonic-gate 		goto out;
21280Sstevel@tonic-gate 	}
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	/*
21310Sstevel@tonic-gate 	 * For default privs, we need to keep priv_set == NULL, as
21320Sstevel@tonic-gate 	 * we use this test elsewhere.
21330Sstevel@tonic-gate 	 */
21340Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") != 0) {
21350Sstevel@tonic-gate 		ci->priv_set = priv_str_to_set(vbuf, ",", NULL);
21360Sstevel@tonic-gate 		if (ci->priv_set == NULL) {
21370Sstevel@tonic-gate 			if (errno != EINVAL) {
21380Sstevel@tonic-gate 				errstr = ALLOCFAIL;
21390Sstevel@tonic-gate 			} else {
21400Sstevel@tonic-gate 				errstr = "Could not interpret privileges "
21410Sstevel@tonic-gate 				    "property.";
21420Sstevel@tonic-gate 			}
21430Sstevel@tonic-gate 			goto out;
21440Sstevel@tonic-gate 		}
21450Sstevel@tonic-gate 	}
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 	if (get_astring_val(pg, SCF_PROPERTY_LIMIT_PRIVILEGES, vbuf, vbuf_sz,
21480Sstevel@tonic-gate 	    prop, val) != 0) {
21490Sstevel@tonic-gate 		errstr = "Could not get limit_privileges property.";
21500Sstevel@tonic-gate 		goto out;
21510Sstevel@tonic-gate 	}
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	if (strcmp(vbuf, ":default") == 0)
21540Sstevel@tonic-gate 		/*
21550Sstevel@tonic-gate 		 * L must default to all privileges so root NPA services see
21560Sstevel@tonic-gate 		 * iE = all.  "zone" is all privileges available in the current
21570Sstevel@tonic-gate 		 * zone, equivalent to "all" in the global zone.
21580Sstevel@tonic-gate 		 */
21590Sstevel@tonic-gate 		(void) strcpy(vbuf, "zone");
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	ci->lpriv_set = priv_str_to_set(vbuf, ",", NULL);
21620Sstevel@tonic-gate 	if (ci->lpriv_set == NULL) {
21630Sstevel@tonic-gate 		if (errno != EINVAL)
21640Sstevel@tonic-gate 			errstr = ALLOCFAIL;
21650Sstevel@tonic-gate 		else {
21660Sstevel@tonic-gate 			errstr = "Could not interpret limit_privileges "
21670Sstevel@tonic-gate 			    "property.";
21680Sstevel@tonic-gate 		}
21690Sstevel@tonic-gate 		goto out;
21700Sstevel@tonic-gate 	}
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate out:
21730Sstevel@tonic-gate 	return (errstr);
21740Sstevel@tonic-gate }
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate static int
21770Sstevel@tonic-gate get_environment(scf_handle_t *h, scf_propertygroup_t *pg,
21780Sstevel@tonic-gate     struct method_context *mcp, scf_property_t *prop, scf_value_t *val)
21790Sstevel@tonic-gate {
21800Sstevel@tonic-gate 	scf_iter_t *iter;
21810Sstevel@tonic-gate 	scf_type_t type;
21820Sstevel@tonic-gate 	size_t i = 0;
21830Sstevel@tonic-gate 	int ret;
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENVIRONMENT, prop) != 0) {
21860Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_NOT_FOUND)
21870Sstevel@tonic-gate 			return (ENOENT);
21880Sstevel@tonic-gate 		return (scf_error());
21890Sstevel@tonic-gate 	}
21900Sstevel@tonic-gate 	if (scf_property_type(prop, &type) != 0)
21910Sstevel@tonic-gate 		return (scf_error());
21920Sstevel@tonic-gate 	if (type != SCF_TYPE_ASTRING)
21930Sstevel@tonic-gate 		return (EINVAL);
21940Sstevel@tonic-gate 	if ((iter = scf_iter_create(h)) == NULL)
21950Sstevel@tonic-gate 		return (scf_error());
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate 	if (scf_iter_property_values(iter, prop) != 0) {
21980Sstevel@tonic-gate 		ret = scf_error();
21990Sstevel@tonic-gate 		scf_iter_destroy(iter);
22000Sstevel@tonic-gate 		return (ret);
22010Sstevel@tonic-gate 	}
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate 	mcp->env_sz = 10;
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	if ((mcp->env = uu_zalloc(sizeof (*mcp->env) * mcp->env_sz)) == NULL) {
22060Sstevel@tonic-gate 		ret = ENOMEM;
22070Sstevel@tonic-gate 		goto out;
22080Sstevel@tonic-gate 	}
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 	while ((ret = scf_iter_next_value(iter, val)) == 1) {
22110Sstevel@tonic-gate 		ret = scf_value_get_as_string(val, mcp->vbuf, mcp->vbuf_sz);
22120Sstevel@tonic-gate 		if (ret == -1) {
22130Sstevel@tonic-gate 			ret = scf_error();
22140Sstevel@tonic-gate 			goto out;
22150Sstevel@tonic-gate 		}
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 		if ((mcp->env[i] = strdup(mcp->vbuf)) == NULL) {
22180Sstevel@tonic-gate 			ret = ENOMEM;
22190Sstevel@tonic-gate 			goto out;
22200Sstevel@tonic-gate 		}
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 		if (++i == mcp->env_sz) {
22230Sstevel@tonic-gate 			char **env;
22240Sstevel@tonic-gate 			mcp->env_sz *= 2;
22250Sstevel@tonic-gate 			env = uu_zalloc(sizeof (*mcp->env) * mcp->env_sz);
22260Sstevel@tonic-gate 			if (env == NULL) {
22270Sstevel@tonic-gate 				ret = ENOMEM;
22280Sstevel@tonic-gate 				goto out;
22290Sstevel@tonic-gate 			}
22300Sstevel@tonic-gate 			(void) memcpy(env, mcp->env,
22310Sstevel@tonic-gate 			    sizeof (*mcp->env) * (mcp->env_sz / 2));
22320Sstevel@tonic-gate 			free(mcp->env);
22330Sstevel@tonic-gate 			mcp->env = env;
22340Sstevel@tonic-gate 		}
22350Sstevel@tonic-gate 	}
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	if (ret == -1)
22380Sstevel@tonic-gate 		ret = scf_error();
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate out:
22410Sstevel@tonic-gate 	scf_iter_destroy(iter);
22420Sstevel@tonic-gate 	return (ret);
22430Sstevel@tonic-gate }
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate /*
22460Sstevel@tonic-gate  * Fetch method context information from the repository, allocate and fill
22470Sstevel@tonic-gate  * a method_context structure, return it in *mcpp, and return NULL.  On error,
22480Sstevel@tonic-gate  * return a human-readable string which indicates the error.
22490Sstevel@tonic-gate  *
22500Sstevel@tonic-gate  * Eventually, we will return a structured error in the case of
22510Sstevel@tonic-gate  * retryable or abortable failures such as memory allocation errors and
22520Sstevel@tonic-gate  * repository connection failures.  For now, these failures are just
22530Sstevel@tonic-gate  * encoded in the failure string.
22540Sstevel@tonic-gate  */
22550Sstevel@tonic-gate const char *
22560Sstevel@tonic-gate restarter_get_method_context(uint_t version, scf_instance_t *inst,
22570Sstevel@tonic-gate     scf_snapshot_t *snap, const char *mname, const char *cmdline,
22580Sstevel@tonic-gate     struct method_context **mcpp)
22590Sstevel@tonic-gate {
22600Sstevel@tonic-gate 	scf_handle_t *h;
22610Sstevel@tonic-gate 	scf_propertygroup_t *methpg = NULL;
22620Sstevel@tonic-gate 	scf_propertygroup_t *instpg = NULL;
22630Sstevel@tonic-gate 	scf_propertygroup_t *pg = NULL;
22640Sstevel@tonic-gate 	scf_property_t *prop = NULL;
22650Sstevel@tonic-gate 	scf_value_t *val = NULL;
22660Sstevel@tonic-gate 	scf_type_t ty;
22670Sstevel@tonic-gate 	uint8_t use_profile;
22680Sstevel@tonic-gate 	int ret;
22690Sstevel@tonic-gate 	const char *errstr = NULL;
22700Sstevel@tonic-gate 	struct method_context *cip;
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 	if (version != RESTARTER_METHOD_CONTEXT_VERSION)
22740Sstevel@tonic-gate 		return ("Unknown method_context version.");
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 	/* Get the handle before we allocate anything. */
22770Sstevel@tonic-gate 	h = scf_instance_handle(inst);
22780Sstevel@tonic-gate 	if (h == NULL)
22790Sstevel@tonic-gate 		return (scf_strerror(scf_error()));
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 	cip = malloc(sizeof (*cip));
22820Sstevel@tonic-gate 	if (cip == NULL)
22830Sstevel@tonic-gate 		return (ALLOCFAIL);
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	(void) memset(cip, 0, sizeof (*cip));
22864321Scasper 	cip->uid = (uid_t)-1;
22874321Scasper 	cip->euid = (uid_t)-1;
22884321Scasper 	cip->gid = (gid_t)-1;
22894321Scasper 	cip->egid = (gid_t)-1;
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	cip->vbuf_sz = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
22920Sstevel@tonic-gate 	assert(cip->vbuf_sz >= 0);
22930Sstevel@tonic-gate 	cip->vbuf = malloc(cip->vbuf_sz);
22940Sstevel@tonic-gate 	if (cip->vbuf == NULL) {
22950Sstevel@tonic-gate 		free(cip);
22960Sstevel@tonic-gate 		return (ALLOCFAIL);
22970Sstevel@tonic-gate 	}
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 	if ((instpg = scf_pg_create(h)) == NULL ||
23000Sstevel@tonic-gate 	    (methpg = scf_pg_create(h)) == NULL ||
23010Sstevel@tonic-gate 	    (prop = scf_property_create(h)) == NULL ||
23020Sstevel@tonic-gate 	    (val = scf_value_create(h)) == NULL) {
23030Sstevel@tonic-gate 		errstr = ALLOCFAIL;
23040Sstevel@tonic-gate 		goto out;
23050Sstevel@tonic-gate 	}
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 	/*
23080Sstevel@tonic-gate 	 * The method environment, and the credentials/profile data,
23090Sstevel@tonic-gate 	 * may be found either in the pg for the method (methpg),
23100Sstevel@tonic-gate 	 * or in the instance/service SCF_PG_METHOD_CONTEXT pg (named
23110Sstevel@tonic-gate 	 * instpg below).
23120Sstevel@tonic-gate 	 */
23130Sstevel@tonic-gate 
23140Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, mname, methpg) !=
23150Sstevel@tonic-gate 	    SCF_SUCCESS) {
23160Sstevel@tonic-gate 		errstr = scf_strerror(scf_error());
23170Sstevel@tonic-gate 		goto out;
23180Sstevel@tonic-gate 	}
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	if (scf_instance_get_pg_composed(inst, snap, SCF_PG_METHOD_CONTEXT,
23210Sstevel@tonic-gate 	    instpg) != SCF_SUCCESS) {
23220Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND) {
23230Sstevel@tonic-gate 			errstr = scf_strerror(scf_error());
23240Sstevel@tonic-gate 			goto out;
23250Sstevel@tonic-gate 		}
23260Sstevel@tonic-gate 		scf_pg_destroy(instpg);
23270Sstevel@tonic-gate 		instpg = NULL;
23280Sstevel@tonic-gate 	}
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	ret = get_environment(h, methpg, cip, prop, val);
23310Sstevel@tonic-gate 	if (ret == ENOENT && instpg != NULL) {
23320Sstevel@tonic-gate 		ret = get_environment(h, instpg, cip, prop, val);
23330Sstevel@tonic-gate 	}
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate 	switch (ret) {
23360Sstevel@tonic-gate 	case 0:
23370Sstevel@tonic-gate 	case ENOENT:
23380Sstevel@tonic-gate 		break;
23390Sstevel@tonic-gate 	case ENOMEM:
23400Sstevel@tonic-gate 		errstr = "Out of memory.";
23410Sstevel@tonic-gate 		goto out;
23420Sstevel@tonic-gate 	case EINVAL:
23430Sstevel@tonic-gate 		errstr = "Invalid method environment.";
23440Sstevel@tonic-gate 		goto out;
23450Sstevel@tonic-gate 	default:
23460Sstevel@tonic-gate 		errstr = scf_strerror(ret);
23470Sstevel@tonic-gate 		goto out;
23480Sstevel@tonic-gate 	}
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	pg = methpg;
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 	ret = scf_pg_get_property(pg, SCF_PROPERTY_USE_PROFILE, prop);
23530Sstevel@tonic-gate 	if (ret && scf_error() == SCF_ERROR_NOT_FOUND && instpg != NULL) {
23540Sstevel@tonic-gate 		pg = instpg;
23550Sstevel@tonic-gate 		ret = scf_pg_get_property(pg, SCF_PROPERTY_USE_PROFILE, prop);
23560Sstevel@tonic-gate 	}
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 	if (ret) {
23590Sstevel@tonic-gate 		switch (scf_error()) {
23600Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
23610Sstevel@tonic-gate 			/* No context: use defaults */
23620Sstevel@tonic-gate 			cip->uid = 0;
23630Sstevel@tonic-gate 			cip->gid = 0;
23640Sstevel@tonic-gate 			*mcpp = cip;
23650Sstevel@tonic-gate 			goto out;
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
23680Sstevel@tonic-gate 			errstr = RCBROKEN;
23690Sstevel@tonic-gate 			goto out;
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
23720Sstevel@tonic-gate 			errstr = "\"use_profile\" property deleted.";
23730Sstevel@tonic-gate 			goto out;
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
23760Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
23770Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
23780Sstevel@tonic-gate 		default:
23790Sstevel@tonic-gate 			bad_fail("scf_pg_get_property", scf_error());
23800Sstevel@tonic-gate 		}
23810Sstevel@tonic-gate 	}
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	if (scf_property_type(prop, &ty) != SCF_SUCCESS) {
23840Sstevel@tonic-gate 		switch (scf_error()) {
23850Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
23860Sstevel@tonic-gate 			errstr = RCBROKEN;
23870Sstevel@tonic-gate 			break;
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
23900Sstevel@tonic-gate 			errstr = "\"use profile\" property deleted.";
23910Sstevel@tonic-gate 			break;
23920Sstevel@tonic-gate 
23930Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
23940Sstevel@tonic-gate 		default:
23950Sstevel@tonic-gate 			bad_fail("scf_property_type", scf_error());
23960Sstevel@tonic-gate 		}
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate 		goto out;
23990Sstevel@tonic-gate 	}
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate 	if (ty != SCF_TYPE_BOOLEAN) {
24020Sstevel@tonic-gate 		errstr = "\"use profile\" property is not boolean.";
24030Sstevel@tonic-gate 		goto out;
24040Sstevel@tonic-gate 	}
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 	if (scf_property_get_value(prop, val) != SCF_SUCCESS) {
24070Sstevel@tonic-gate 		switch (scf_error()) {
24080Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
24090Sstevel@tonic-gate 			errstr = RCBROKEN;
24100Sstevel@tonic-gate 			break;
24110Sstevel@tonic-gate 
24120Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
24130Sstevel@tonic-gate 			errstr =
24140Sstevel@tonic-gate 			    "\"use profile\" property has multiple values.";
24150Sstevel@tonic-gate 			break;
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
24180Sstevel@tonic-gate 			errstr = "\"use profile\" property has no values.";
24190Sstevel@tonic-gate 			break;
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 		default:
24220Sstevel@tonic-gate 			bad_fail("scf_property_get_value", scf_error());
24230Sstevel@tonic-gate 		}
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 		goto out;
24260Sstevel@tonic-gate 	}
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	ret = scf_value_get_boolean(val, &use_profile);
24290Sstevel@tonic-gate 	assert(ret == SCF_SUCCESS);
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 	/* get ids & privileges */
24320Sstevel@tonic-gate 	if (use_profile)
24330Sstevel@tonic-gate 		errstr = get_profile(pg, prop, val, cmdline, cip);
24340Sstevel@tonic-gate 	else
24350Sstevel@tonic-gate 		errstr = get_ids(pg, prop, val, cip);
24360Sstevel@tonic-gate 	if (errstr != NULL)
24370Sstevel@tonic-gate 		goto out;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 	/* get working directory */
24400Sstevel@tonic-gate 	if (get_astring_val(pg, SCF_PROPERTY_WORKING_DIRECTORY, cip->vbuf,
24410Sstevel@tonic-gate 	    cip->vbuf_sz, prop, val) != 0) {
24420Sstevel@tonic-gate 		errstr = "Could not get value for working directory.";
24430Sstevel@tonic-gate 		goto out;
24440Sstevel@tonic-gate 	}
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 	if (strcmp(cip->vbuf, ":default") == 0 ||
24470Sstevel@tonic-gate 	    strcmp(cip->vbuf, ":home") == 0) {
24480Sstevel@tonic-gate 		switch (ret = lookup_pwd(cip)) {
24490Sstevel@tonic-gate 		case 0:
24500Sstevel@tonic-gate 			break;
24510Sstevel@tonic-gate 
24520Sstevel@tonic-gate 		case ENOMEM:
24530Sstevel@tonic-gate 			errstr = "Out of memory.";
24540Sstevel@tonic-gate 			goto out;
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 		case ENOENT:
24570Sstevel@tonic-gate 		case EIO:
24580Sstevel@tonic-gate 		case EMFILE:
24590Sstevel@tonic-gate 		case ENFILE:
24600Sstevel@tonic-gate 			errstr = "Could not get passwd entry.";
24610Sstevel@tonic-gate 			goto out;
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 		default:
24640Sstevel@tonic-gate 			bad_fail("lookup_pwd", ret);
24650Sstevel@tonic-gate 		}
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 		cip->working_dir = strdup(cip->pwd.pw_dir);
24680Sstevel@tonic-gate 		if (cip->working_dir == NULL) {
24690Sstevel@tonic-gate 			errstr = ALLOCFAIL;
24700Sstevel@tonic-gate 			goto out;
24710Sstevel@tonic-gate 		}
24720Sstevel@tonic-gate 	} else {
24730Sstevel@tonic-gate 		cip->working_dir = strdup(cip->vbuf);
24740Sstevel@tonic-gate 		if (cip->working_dir == NULL) {
24750Sstevel@tonic-gate 			errstr = ALLOCFAIL;
24760Sstevel@tonic-gate 			goto out;
24770Sstevel@tonic-gate 		}
24780Sstevel@tonic-gate 	}
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate 	/* get (optional) corefile pattern */
24810Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_COREFILE_PATTERN, prop) ==
24820Sstevel@tonic-gate 	    SCF_SUCCESS) {
24830Sstevel@tonic-gate 		if (get_astring_val(pg, SCF_PROPERTY_COREFILE_PATTERN,
24840Sstevel@tonic-gate 		    cip->vbuf, cip->vbuf_sz, prop, val) != 0) {
24850Sstevel@tonic-gate 			errstr = "Could not get value for corefile pattern.";
24860Sstevel@tonic-gate 			goto out;
24870Sstevel@tonic-gate 		}
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 		cip->corefile_pattern = strdup(cip->vbuf);
24900Sstevel@tonic-gate 		if (cip->corefile_pattern == NULL) {
24910Sstevel@tonic-gate 			errstr = ALLOCFAIL;
24920Sstevel@tonic-gate 			goto out;
24930Sstevel@tonic-gate 		}
24940Sstevel@tonic-gate 	} else {
24950Sstevel@tonic-gate 		switch (scf_error()) {
24960Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
24970Sstevel@tonic-gate 			/* okay if missing. */
24980Sstevel@tonic-gate 			break;
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
25010Sstevel@tonic-gate 			errstr = RCBROKEN;
25020Sstevel@tonic-gate 			goto out;
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
25050Sstevel@tonic-gate 			errstr = "\"corefile_pattern\" property deleted.";
25060Sstevel@tonic-gate 			goto out;
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
25090Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
25100Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
25110Sstevel@tonic-gate 		default:
25120Sstevel@tonic-gate 			bad_fail("scf_pg_get_property", scf_error());
25130Sstevel@tonic-gate 		}
25140Sstevel@tonic-gate 	}
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate 	if (restarter_rm_libs_loadable()) {
25170Sstevel@tonic-gate 		/* get project */
25180Sstevel@tonic-gate 		if (get_astring_val(pg, SCF_PROPERTY_PROJECT, cip->vbuf,
25190Sstevel@tonic-gate 		    cip->vbuf_sz, prop, val) != 0) {
25200Sstevel@tonic-gate 			errstr = "Could not get project.";
25210Sstevel@tonic-gate 			goto out;
25220Sstevel@tonic-gate 		}
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 		switch (ret = get_projid(cip->vbuf, cip)) {
25250Sstevel@tonic-gate 		case 0:
25260Sstevel@tonic-gate 			break;
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 		case ENOMEM:
25290Sstevel@tonic-gate 			errstr = "Out of memory.";
25300Sstevel@tonic-gate 			goto out;
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 		case ENOENT:
25330Sstevel@tonic-gate 			errstr = "Missing passwd or project entry.";
25340Sstevel@tonic-gate 			goto out;
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate 		case EIO:
25370Sstevel@tonic-gate 			errstr = "I/O error.";
25380Sstevel@tonic-gate 			goto out;
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 		case EMFILE:
25410Sstevel@tonic-gate 		case ENFILE:
25420Sstevel@tonic-gate 			errstr = "Out of file descriptors.";
25430Sstevel@tonic-gate 			goto out;
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 		case -1:
25460Sstevel@tonic-gate 			errstr = "Name service switch is misconfigured.";
25470Sstevel@tonic-gate 			goto out;
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 		case ERANGE:
25500Sstevel@tonic-gate 			errstr = "Project ID too big.";
25510Sstevel@tonic-gate 			goto out;
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 		case EINVAL:
25540Sstevel@tonic-gate 			errstr = "Project ID is invalid.";
25550Sstevel@tonic-gate 			goto out;
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 		case E2BIG:
25580Sstevel@tonic-gate 			errstr = "Project entry is too big.";
25590Sstevel@tonic-gate 			goto out;
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 		default:
25620Sstevel@tonic-gate 			bad_fail("get_projid", ret);
25630Sstevel@tonic-gate 		}
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 		/* get resource pool */
25660Sstevel@tonic-gate 		if (get_astring_val(pg, SCF_PROPERTY_RESOURCE_POOL, cip->vbuf,
25675040Swesolows 		    cip->vbuf_sz, prop, val) != 0) {
25680Sstevel@tonic-gate 			errstr = "Could not get value of resource pool.";
25690Sstevel@tonic-gate 			goto out;
25700Sstevel@tonic-gate 		}
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 		if (strcmp(cip->vbuf, ":default") != 0) {
25730Sstevel@tonic-gate 			cip->resource_pool = strdup(cip->vbuf);
25740Sstevel@tonic-gate 			if (cip->resource_pool == NULL) {
25750Sstevel@tonic-gate 				errstr = ALLOCFAIL;
25760Sstevel@tonic-gate 				goto out;
25770Sstevel@tonic-gate 			}
25780Sstevel@tonic-gate 		}
25790Sstevel@tonic-gate 	}
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	*mcpp = cip;
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate out:
25840Sstevel@tonic-gate 	(void) scf_value_destroy(val);
25850Sstevel@tonic-gate 	scf_property_destroy(prop);
25860Sstevel@tonic-gate 	scf_pg_destroy(instpg);
25870Sstevel@tonic-gate 	scf_pg_destroy(methpg);
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	if (cip->pwbuf != NULL)
25900Sstevel@tonic-gate 		free(cip->pwbuf);
25910Sstevel@tonic-gate 	free(cip->vbuf);
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	if (errstr != NULL)
25940Sstevel@tonic-gate 		restarter_free_method_context(cip);
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 	return (errstr);
25970Sstevel@tonic-gate }
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate /*
26000Sstevel@tonic-gate  * Modify the current process per the given method_context.  On success, returns
26010Sstevel@tonic-gate  * 0.  Note that the environment is not modified by this function to include the
26020Sstevel@tonic-gate  * environment variables in cip->env.
26030Sstevel@tonic-gate  *
26040Sstevel@tonic-gate  * On failure, sets *fp to NULL or the name of the function which failed,
26050Sstevel@tonic-gate  * and returns one of the following error codes.  The words in parentheses are
26060Sstevel@tonic-gate  * the values to which *fp may be set for the error case.
26070Sstevel@tonic-gate  *   ENOMEM - malloc() failed
26080Sstevel@tonic-gate  *   EIO - an I/O error occurred (getpwuid_r, chdir)
26090Sstevel@tonic-gate  *   EMFILE - process is out of file descriptors (getpwuid_r)
26100Sstevel@tonic-gate  *   ENFILE - system is out of file handles (getpwuid_r)
26110Sstevel@tonic-gate  *   EINVAL - gid or egid is out of range (setregid)
26120Sstevel@tonic-gate  *	      ngroups is too big (setgroups)
26130Sstevel@tonic-gate  *	      project's project id is bad (setproject)
26140Sstevel@tonic-gate  *	      uid or euid is out of range (setreuid)
26151712Srm88369  *	      poolname is invalid (pool_set_binding)
26160Sstevel@tonic-gate  *   EPERM - insufficient privilege (setregid, initgroups, setgroups, setppriv,
26170Sstevel@tonic-gate  *	         setproject, setreuid, settaskid)
26180Sstevel@tonic-gate  *   ENOENT - uid has a passwd entry but no shadow entry
26190Sstevel@tonic-gate  *	      working_dir does not exist (chdir)
26200Sstevel@tonic-gate  *	      uid has no passwd entry
26210Sstevel@tonic-gate  *	      the pool could not be found (pool_set_binding)
26220Sstevel@tonic-gate  *   EFAULT - lpriv_set or priv_set has a bad address (setppriv)
26230Sstevel@tonic-gate  *	      working_dir has a bad address (chdir)
26240Sstevel@tonic-gate  *   EACCES - could not access working_dir (chdir)
26250Sstevel@tonic-gate  *	      in a TASK_FINAL task (setproject, settaskid)
26260Sstevel@tonic-gate  *	      no resource pool accepting default binding exists (setproject)
26270Sstevel@tonic-gate  *   ELOOP - too many symbolic links in working_dir (chdir)
26280Sstevel@tonic-gate  *   ENAMETOOLONG - working_dir is too long (chdir)
26290Sstevel@tonic-gate  *   ENOLINK - working_dir is on an inaccessible remote machine (chdir)
26300Sstevel@tonic-gate  *   ENOTDIR - working_dir is not a directory (chdir)
26310Sstevel@tonic-gate  *   ESRCH - uid is not a user of project (setproject)
26320Sstevel@tonic-gate  *	     project is invalid (setproject)
26330Sstevel@tonic-gate  *	     the resource pool specified for project is unknown (setproject)
26340Sstevel@tonic-gate  *   EBADF - the configuration for the pool is invalid (pool_set_binding)
26350Sstevel@tonic-gate  *   -1 - core_set_process_path() failed (core_set_process_path)
26360Sstevel@tonic-gate  *	  a resource control assignment failed (setproject)
26370Sstevel@tonic-gate  *	  a system error occurred during pool_set_binding (pool_set_binding)
26380Sstevel@tonic-gate  */
26390Sstevel@tonic-gate int
26400Sstevel@tonic-gate restarter_set_method_context(struct method_context *cip, const char **fp)
26410Sstevel@tonic-gate {
26420Sstevel@tonic-gate 	pid_t mypid = -1;
26430Sstevel@tonic-gate 	int r, ret;
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 	cip->pwbuf = NULL;
26460Sstevel@tonic-gate 	*fp = NULL;
26470Sstevel@tonic-gate 
26484321Scasper 	if (cip->gid != (gid_t)-1) {
26490Sstevel@tonic-gate 		if (setregid(cip->gid,
26504321Scasper 		    cip->egid != (gid_t)-1 ? cip->egid : cip->gid) != 0) {
26510Sstevel@tonic-gate 			*fp = "setregid";
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate 			ret = errno;
26540Sstevel@tonic-gate 			assert(ret == EINVAL || ret == EPERM);
26550Sstevel@tonic-gate 			goto out;
26560Sstevel@tonic-gate 		}
26570Sstevel@tonic-gate 	} else {
26580Sstevel@tonic-gate 		if (cip->pwbuf == NULL) {
26590Sstevel@tonic-gate 			switch (ret = lookup_pwd(cip)) {
26600Sstevel@tonic-gate 			case 0:
26610Sstevel@tonic-gate 				break;
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 			case ENOMEM:
26640Sstevel@tonic-gate 			case ENOENT:
26650Sstevel@tonic-gate 				*fp = NULL;
26660Sstevel@tonic-gate 				goto out;
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 			case EIO:
26690Sstevel@tonic-gate 			case EMFILE:
26700Sstevel@tonic-gate 			case ENFILE:
26710Sstevel@tonic-gate 				*fp = "getpwuid_r";
26720Sstevel@tonic-gate 				goto out;
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 			default:
26750Sstevel@tonic-gate 				bad_fail("lookup_pwd", ret);
26760Sstevel@tonic-gate 			}
26770Sstevel@tonic-gate 		}
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate 		if (setregid(cip->pwd.pw_gid,
26804321Scasper 		    cip->egid != (gid_t)-1 ?
26814321Scasper 		    cip->egid : cip->pwd.pw_gid) != 0) {
26820Sstevel@tonic-gate 			*fp = "setregid";
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate 			ret = errno;
26850Sstevel@tonic-gate 			assert(ret == EINVAL || ret == EPERM);
26860Sstevel@tonic-gate 			goto out;
26870Sstevel@tonic-gate 		}
26880Sstevel@tonic-gate 	}
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 	if (cip->ngroups == -1) {
26910Sstevel@tonic-gate 		if (cip->pwbuf == NULL) {
26920Sstevel@tonic-gate 			switch (ret = lookup_pwd(cip)) {
26930Sstevel@tonic-gate 			case 0:
26940Sstevel@tonic-gate 				break;
26950Sstevel@tonic-gate 
26960Sstevel@tonic-gate 			case ENOMEM:
26970Sstevel@tonic-gate 			case ENOENT:
26980Sstevel@tonic-gate 				*fp = NULL;
26990Sstevel@tonic-gate 				goto out;
27000Sstevel@tonic-gate 
27010Sstevel@tonic-gate 			case EIO:
27020Sstevel@tonic-gate 			case EMFILE:
27030Sstevel@tonic-gate 			case ENFILE:
27040Sstevel@tonic-gate 				*fp = "getpwuid_r";
27050Sstevel@tonic-gate 				goto out;
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 			default:
27080Sstevel@tonic-gate 				bad_fail("lookup_pwd", ret);
27090Sstevel@tonic-gate 			}
27100Sstevel@tonic-gate 		}
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 		/* Ok if cip->gid == -1 */
27130Sstevel@tonic-gate 		if (initgroups(cip->pwd.pw_name, cip->gid) != 0) {
27140Sstevel@tonic-gate 			*fp = "initgroups";
27150Sstevel@tonic-gate 			ret = errno;
27160Sstevel@tonic-gate 			assert(ret == EPERM);
27170Sstevel@tonic-gate 			goto out;
27180Sstevel@tonic-gate 		}
27190Sstevel@tonic-gate 	} else if (cip->ngroups > 0 &&
27200Sstevel@tonic-gate 	    setgroups(cip->ngroups, cip->groups) != 0) {
27210Sstevel@tonic-gate 		*fp = "setgroups";
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 		ret = errno;
27240Sstevel@tonic-gate 		assert(ret == EINVAL || ret == EPERM);
27250Sstevel@tonic-gate 		goto out;
27260Sstevel@tonic-gate 	}
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate 	*fp = "setppriv";
27290Sstevel@tonic-gate 
27300Sstevel@tonic-gate 	if (cip->lpriv_set != NULL) {
27310Sstevel@tonic-gate 		if (setppriv(PRIV_SET, PRIV_LIMIT, cip->lpriv_set) != 0) {
27320Sstevel@tonic-gate 			ret = errno;
27330Sstevel@tonic-gate 			assert(ret == EFAULT || ret == EPERM);
27340Sstevel@tonic-gate 			goto out;
27350Sstevel@tonic-gate 		}
27360Sstevel@tonic-gate 	}
27370Sstevel@tonic-gate 	if (cip->priv_set != NULL) {
27380Sstevel@tonic-gate 		if (setppriv(PRIV_SET, PRIV_INHERITABLE, cip->priv_set) != 0) {
27390Sstevel@tonic-gate 			ret = errno;
27400Sstevel@tonic-gate 			assert(ret == EFAULT || ret == EPERM);
27410Sstevel@tonic-gate 			goto out;
27420Sstevel@tonic-gate 		}
27430Sstevel@tonic-gate 	}
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 	if (cip->corefile_pattern != NULL) {
27460Sstevel@tonic-gate 		mypid = getpid();
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate 		if (core_set_process_path(cip->corefile_pattern,
27490Sstevel@tonic-gate 		    strlen(cip->corefile_pattern) + 1, mypid) != 0) {
27500Sstevel@tonic-gate 			*fp = "core_set_process_path";
27510Sstevel@tonic-gate 			ret = -1;
27520Sstevel@tonic-gate 			goto out;
27530Sstevel@tonic-gate 		}
27540Sstevel@tonic-gate 	}
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate 	if (restarter_rm_libs_loadable()) {
27570Sstevel@tonic-gate 		if (cip->project == NULL) {
27580Sstevel@tonic-gate 			if (settaskid(getprojid(), TASK_NORMAL) == -1) {
27590Sstevel@tonic-gate 				switch (errno) {
27600Sstevel@tonic-gate 				case EACCES:
27610Sstevel@tonic-gate 				case EPERM:
27620Sstevel@tonic-gate 					*fp = "settaskid";
27630Sstevel@tonic-gate 					ret = errno;
27640Sstevel@tonic-gate 					goto out;
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 				case EINVAL:
27670Sstevel@tonic-gate 				default:
27680Sstevel@tonic-gate 					bad_fail("settaskid", errno);
27690Sstevel@tonic-gate 				}
27700Sstevel@tonic-gate 			}
27710Sstevel@tonic-gate 		} else {
27720Sstevel@tonic-gate 			switch (ret = lookup_pwd(cip)) {
27730Sstevel@tonic-gate 			case 0:
27740Sstevel@tonic-gate 				break;
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 			case ENOMEM:
27770Sstevel@tonic-gate 			case ENOENT:
27780Sstevel@tonic-gate 				*fp = NULL;
27790Sstevel@tonic-gate 				goto out;
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 			case EIO:
27820Sstevel@tonic-gate 			case EMFILE:
27830Sstevel@tonic-gate 			case ENFILE:
27840Sstevel@tonic-gate 				*fp = "getpwuid_r";
27850Sstevel@tonic-gate 				goto out;
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 			default:
27880Sstevel@tonic-gate 				bad_fail("lookup_pwd", ret);
27890Sstevel@tonic-gate 			}
27900Sstevel@tonic-gate 
27910Sstevel@tonic-gate 			*fp = "setproject";
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate 			switch (setproject(cip->project, cip->pwd.pw_name,
27940Sstevel@tonic-gate 			    TASK_NORMAL)) {
27950Sstevel@tonic-gate 			case 0:
27960Sstevel@tonic-gate 				break;
27970Sstevel@tonic-gate 
27980Sstevel@tonic-gate 			case SETPROJ_ERR_TASK:
27990Sstevel@tonic-gate 			case SETPROJ_ERR_POOL:
28000Sstevel@tonic-gate 				ret = errno;
28010Sstevel@tonic-gate 				goto out;
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate 			default:
28040Sstevel@tonic-gate 				ret = -1;
28050Sstevel@tonic-gate 				goto out;
28060Sstevel@tonic-gate 			}
28070Sstevel@tonic-gate 		}
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 		if (cip->resource_pool != NULL) {
28100Sstevel@tonic-gate 			if (mypid == -1)
28110Sstevel@tonic-gate 				mypid = getpid();
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 			*fp = "pool_set_binding";
28140Sstevel@tonic-gate 
28150Sstevel@tonic-gate 			if (pool_set_binding(cip->resource_pool, P_PID,
28160Sstevel@tonic-gate 			    mypid) != PO_SUCCESS) {
28170Sstevel@tonic-gate 				switch (pool_error()) {
28181712Srm88369 				case POE_INVALID_SEARCH:
28191712Srm88369 					ret = ENOENT;
28201712Srm88369 					break;
28211712Srm88369 
28220Sstevel@tonic-gate 				case POE_BADPARAM:
28231712Srm88369 					ret = EINVAL;
28240Sstevel@tonic-gate 					break;
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 				case POE_INVALID_CONF:
28270Sstevel@tonic-gate 					ret = EBADF;
28280Sstevel@tonic-gate 					break;
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 				case POE_SYSTEM:
28310Sstevel@tonic-gate 					ret = -1;
28320Sstevel@tonic-gate 					break;
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 				default:
28350Sstevel@tonic-gate 					bad_fail("pool_set_binding",
28360Sstevel@tonic-gate 					    pool_error());
28370Sstevel@tonic-gate 				}
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 				goto out;
28400Sstevel@tonic-gate 			}
28410Sstevel@tonic-gate 		}
28420Sstevel@tonic-gate 	}
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	/*
28453879Svp157776 	 * Now, we have to assume our ID. If the UID is 0, we want it to be
28463879Svp157776 	 * privilege-aware, otherwise the limit set gets used instead of E/P.
28470Sstevel@tonic-gate 	 * We can do this by setting P as well, which keeps
28480Sstevel@tonic-gate 	 * PA status (see priv_can_clear_PA()).
28490Sstevel@tonic-gate 	 */
28500Sstevel@tonic-gate 
28510Sstevel@tonic-gate 	*fp = "setreuid";
28524321Scasper 	if (setreuid(cip->uid,
28534321Scasper 	    cip->euid != (uid_t)-1 ? cip->euid : cip->uid) != 0) {
28540Sstevel@tonic-gate 		ret = errno;
28550Sstevel@tonic-gate 		assert(ret == EINVAL || ret == EPERM);
28560Sstevel@tonic-gate 		goto out;
28570Sstevel@tonic-gate 	}
28580Sstevel@tonic-gate 
28590Sstevel@tonic-gate 	*fp = "setppriv";
28600Sstevel@tonic-gate 	if (cip->priv_set != NULL) {
28610Sstevel@tonic-gate 		if (setppriv(PRIV_SET, PRIV_PERMITTED, cip->priv_set) != 0) {
28620Sstevel@tonic-gate 			ret = errno;
28630Sstevel@tonic-gate 			assert(ret == EFAULT || ret == EPERM);
28640Sstevel@tonic-gate 			goto out;
28650Sstevel@tonic-gate 		}
28660Sstevel@tonic-gate 	}
28670Sstevel@tonic-gate 
28683879Svp157776 	/*
28693879Svp157776 	 * The last thing to do is chdir to the specified working directory.
28703879Svp157776 	 * This should come after the uid switching as only the user might
28713879Svp157776 	 * have access to the specified directory.
28723879Svp157776 	 */
28733879Svp157776 	if (cip->working_dir != NULL) {
28745040Swesolows 		do {
28753879Svp157776 			r = chdir(cip->working_dir);
28765040Swesolows 		} while (r != 0 && errno == EINTR);
28773879Svp157776 		if (r != 0) {
28783879Svp157776 			*fp = "chdir";
28793879Svp157776 			ret = errno;
28803879Svp157776 			goto out;
28813879Svp157776 		}
28823879Svp157776 	}
28833879Svp157776 
28840Sstevel@tonic-gate 	ret = 0;
28850Sstevel@tonic-gate out:
28860Sstevel@tonic-gate 	free(cip->pwbuf);
28870Sstevel@tonic-gate 	cip->pwbuf = NULL;
28880Sstevel@tonic-gate 	return (ret);
28890Sstevel@tonic-gate }
28900Sstevel@tonic-gate 
28910Sstevel@tonic-gate void
28920Sstevel@tonic-gate restarter_free_method_context(struct method_context *mcp)
28930Sstevel@tonic-gate {
28940Sstevel@tonic-gate 	size_t i;
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 	if (mcp->lpriv_set != NULL)
28970Sstevel@tonic-gate 		priv_freeset(mcp->lpriv_set);
28980Sstevel@tonic-gate 	if (mcp->priv_set != NULL)
28990Sstevel@tonic-gate 		priv_freeset(mcp->priv_set);
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate 	if (mcp->env != NULL) {
29020Sstevel@tonic-gate 		for (i = 0; i < mcp->env_sz; i++)
29030Sstevel@tonic-gate 			free(mcp->env[i]);
29040Sstevel@tonic-gate 		free(mcp->env);
29050Sstevel@tonic-gate 	}
29060Sstevel@tonic-gate 
29070Sstevel@tonic-gate 	free(mcp->working_dir);
29080Sstevel@tonic-gate 	free(mcp->corefile_pattern);
29090Sstevel@tonic-gate 	free(mcp->project);
29100Sstevel@tonic-gate 	free(mcp->resource_pool);
29110Sstevel@tonic-gate 	free(mcp);
29120Sstevel@tonic-gate }
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate /*
29150Sstevel@tonic-gate  * Method keyword functions
29160Sstevel@tonic-gate  */
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate int
29190Sstevel@tonic-gate restarter_is_null_method(const char *meth)
29200Sstevel@tonic-gate {
29210Sstevel@tonic-gate 	return (strcmp(meth, MKW_TRUE) == 0);
29220Sstevel@tonic-gate }
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate static int
29250Sstevel@tonic-gate is_kill_method(const char *method, const char *kill_str,
29260Sstevel@tonic-gate     size_t kill_str_len)
29270Sstevel@tonic-gate {
29280Sstevel@tonic-gate 	const char *cp;
29290Sstevel@tonic-gate 	int sig;
29300Sstevel@tonic-gate 
29310Sstevel@tonic-gate 	if (strncmp(method, kill_str, kill_str_len) != 0 ||
29320Sstevel@tonic-gate 	    (method[kill_str_len] != '\0' &&
29330Sstevel@tonic-gate 	    !isspace(method[kill_str_len])))
29340Sstevel@tonic-gate 		return (-1);
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate 	cp = method + kill_str_len;
29370Sstevel@tonic-gate 	while (*cp != '\0' && isspace(*cp))
29380Sstevel@tonic-gate 		++cp;
29390Sstevel@tonic-gate 
29400Sstevel@tonic-gate 	if (*cp == '\0')
29410Sstevel@tonic-gate 		return (SIGTERM);
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	if (*cp != '-')
29440Sstevel@tonic-gate 		return (-1);
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 	return (str2sig(cp + 1, &sig) == 0 ? sig : -1);
29470Sstevel@tonic-gate }
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate int
29500Sstevel@tonic-gate restarter_is_kill_proc_method(const char *method)
29510Sstevel@tonic-gate {
29520Sstevel@tonic-gate 	return (is_kill_method(method, MKW_KILL_PROC,
29530Sstevel@tonic-gate 	    sizeof (MKW_KILL_PROC) - 1));
29540Sstevel@tonic-gate }
29550Sstevel@tonic-gate 
29560Sstevel@tonic-gate int
29570Sstevel@tonic-gate restarter_is_kill_method(const char *method)
29580Sstevel@tonic-gate {
29590Sstevel@tonic-gate 	return (is_kill_method(method, MKW_KILL, sizeof (MKW_KILL) - 1));
29600Sstevel@tonic-gate }
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate /*
29630Sstevel@tonic-gate  * Stubs for now.
29640Sstevel@tonic-gate  */
29650Sstevel@tonic-gate 
29660Sstevel@tonic-gate /* ARGSUSED */
29670Sstevel@tonic-gate int
29680Sstevel@tonic-gate restarter_event_get_enabled(restarter_event_t *e)
29690Sstevel@tonic-gate {
29700Sstevel@tonic-gate 	return (-1);
29710Sstevel@tonic-gate }
29720Sstevel@tonic-gate 
29730Sstevel@tonic-gate /* ARGSUSED */
29740Sstevel@tonic-gate uint64_t
29750Sstevel@tonic-gate restarter_event_get_seq(restarter_event_t *e)
29760Sstevel@tonic-gate {
29770Sstevel@tonic-gate 	return (-1);
29780Sstevel@tonic-gate }
29790Sstevel@tonic-gate 
29800Sstevel@tonic-gate /* ARGSUSED */
29810Sstevel@tonic-gate void
29820Sstevel@tonic-gate restarter_event_get_time(restarter_event_t *e, hrtime_t *time)
29830Sstevel@tonic-gate {
29840Sstevel@tonic-gate }
2985