xref: /titanic_50/usr/src/lib/libzonecfg/common/libzonecfg.c (revision 2ad45a84935be6f65ad11ebefa3108f8b882a1c7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ee519a1fSgjelinek  * Common Development and Distribution License (the "License").
6ee519a1fSgjelinek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
236cfd72c6Sgjelinek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27cf8f45c7Sdstaff #include <libsysevent.h>
28cf8f45c7Sdstaff #include <pthread.h>
29cf8f45c7Sdstaff #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <fnmatch.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <assert.h>
357c478bd9Sstevel@tonic-gate #include <libgen.h>
367c478bd9Sstevel@tonic-gate #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <alloca.h>
387c478bd9Sstevel@tonic-gate #include <ctype.h>
399acbbeafSnn35248 #include <sys/acl.h>
409acbbeafSnn35248 #include <sys/stat.h>
419acbbeafSnn35248 #include <sys/brand.h>
427c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
437c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
44cf8f45c7Sdstaff #include <sys/nvpair.h>
459acbbeafSnn35248 #include <sys/types.h>
46f4b3ec61Sdh155122 #include <sys/sockio.h>
47ee519a1fSgjelinek #include <ftw.h>
480209230bSgjelinek #include <pool.h>
490209230bSgjelinek #include <libscf.h>
500209230bSgjelinek #include <libproc.h>
510209230bSgjelinek #include <sys/priocntl.h>
5239935be5Sgjelinek #include <libuutil.h>
53ff17c8bfSgjelinek #include <wait.h>
54ff17c8bfSgjelinek #include <bsm/adt.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
577c478bd9Sstevel@tonic-gate #include <netdb.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h>
607c478bd9Sstevel@tonic-gate #include <libxml/parser.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
63108322fbScarlsonj #include <uuid/uuid.h>
64ee519a1fSgjelinek #include <dirent.h>
659acbbeafSnn35248 #include <libbrand.h>
66ee519a1fSgjelinek 
677c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
687c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	"/zonecfg.XXXXXX"
71cf8f45c7Sdstaff #define	ZONE_CB_RETRY_COUNT		10
72cf8f45c7Sdstaff #define	ZONE_EVENT_PING_SUBCLASS	"ping"
73cf8f45c7Sdstaff #define	ZONE_EVENT_PING_PUBLISHER	"solaris"
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */
767c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ATTR		(const xmlChar *) "attr"
777c478bd9Sstevel@tonic-gate #define	DTD_ELEM_COMMENT	(const xmlChar *) "comment"
787c478bd9Sstevel@tonic-gate #define	DTD_ELEM_DEVICE		(const xmlChar *) "device"
797c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FS		(const xmlChar *) "filesystem"
807c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FSOPTION	(const xmlChar *) "fsoption"
817c478bd9Sstevel@tonic-gate #define	DTD_ELEM_IPD		(const xmlChar *) "inherited-pkg-dir"
827c478bd9Sstevel@tonic-gate #define	DTD_ELEM_NET		(const xmlChar *) "network"
837c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTL		(const xmlChar *) "rctl"
847c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTLVALUE	(const xmlChar *) "rctl-value"
857c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ZONE		(const xmlChar *) "zone"
86fa9e4066Sahrens #define	DTD_ELEM_DATASET	(const xmlChar *) "dataset"
870209230bSgjelinek #define	DTD_ELEM_TMPPOOL	(const xmlChar *) "tmp_pool"
880209230bSgjelinek #define	DTD_ELEM_PSET		(const xmlChar *) "pset"
890209230bSgjelinek #define	DTD_ELEM_MCAP		(const xmlChar *) "mcap"
90ee519a1fSgjelinek #define	DTD_ELEM_PACKAGE	(const xmlChar *) "package"
91ee519a1fSgjelinek #define	DTD_ELEM_PATCH		(const xmlChar *) "patch"
926cfd72c6Sgjelinek #define	DTD_ELEM_OBSOLETES	(const xmlChar *) "obsoletes"
93ee519a1fSgjelinek #define	DTD_ELEM_DEV_PERM	(const xmlChar *) "dev-perm"
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ACTION		(const xmlChar *) "action"
967c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ADDRESS	(const xmlChar *) "address"
977c478bd9Sstevel@tonic-gate #define	DTD_ATTR_AUTOBOOT	(const xmlChar *) "autoboot"
98f4b3ec61Sdh155122 #define	DTD_ATTR_IPTYPE		(const xmlChar *) "ip-type"
99de860bd9Sgfaden #define	DTD_ATTR_DEFROUTER	(const xmlChar *) "defrouter"
1007c478bd9Sstevel@tonic-gate #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
1017c478bd9Sstevel@tonic-gate #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
102ffbafc53Scomay #define	DTD_ATTR_LIMITPRIV	(const xmlChar *) "limitpriv"
1033f2f09c1Sdp #define	DTD_ATTR_BOOTARGS	(const xmlChar *) "bootargs"
1040209230bSgjelinek #define	DTD_ATTR_SCHED		(const xmlChar *) "scheduling-class"
1057c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
1067c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
1077c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
1087c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
1097c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
1107c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
1117c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
1127c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
1137c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
1147c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
1150209230bSgjelinek #define	DTD_ATTR_NCPU_MIN	(const xmlChar *) "ncpu_min"
1160209230bSgjelinek #define	DTD_ATTR_NCPU_MAX	(const xmlChar *) "ncpu_max"
1170209230bSgjelinek #define	DTD_ATTR_IMPORTANCE	(const xmlChar *) "importance"
1180209230bSgjelinek #define	DTD_ATTR_PHYSCAP	(const xmlChar *) "physcap"
119ee519a1fSgjelinek #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
120ee519a1fSgjelinek #define	DTD_ATTR_ID		(const xmlChar *) "id"
121ee519a1fSgjelinek #define	DTD_ATTR_UID		(const xmlChar *) "uid"
122ee519a1fSgjelinek #define	DTD_ATTR_GID		(const xmlChar *) "gid"
123ee519a1fSgjelinek #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
124ee519a1fSgjelinek #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
1259acbbeafSnn35248 #define	DTD_ATTR_BRAND		(const xmlChar *) "brand"
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
1287c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
1297c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
1307c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
1317c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
1327c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
1337c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
1347c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
1357c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
1367c478bd9Sstevel@tonic-gate 
137a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
138a1be23daSdp 
139ee519a1fSgjelinek #define	ATTACH_FORCED	"SUNWattached.xml"
140ee519a1fSgjelinek 
1410209230bSgjelinek #define	TMP_POOL_NAME	"SUNWtmp_%s"
1420209230bSgjelinek #define	MAX_TMP_POOL_NAME	(ZONENAME_MAX + 9)
1430209230bSgjelinek #define	RCAP_SERVICE	"system/rcap:default"
1440209230bSgjelinek #define	POOLD_SERVICE	"system/pools/dynamic:default"
1450209230bSgjelinek 
1460209230bSgjelinek /*
1470209230bSgjelinek  * rctl alias definitions
1480209230bSgjelinek  *
1490209230bSgjelinek  * This holds the alias, the full rctl name, the default priv value, action
1500209230bSgjelinek  * and lower limit.  The functions that handle rctl aliases step through
1510209230bSgjelinek  * this table, matching on the alias, and using the full values for setting
1520209230bSgjelinek  * the rctl entry as well the limit for validation.
1530209230bSgjelinek  */
1540209230bSgjelinek static struct alias {
1550209230bSgjelinek 	char *shortname;
1560209230bSgjelinek 	char *realname;
1570209230bSgjelinek 	char *priv;
1580209230bSgjelinek 	char *action;
1590209230bSgjelinek 	uint64_t low_limit;
1600209230bSgjelinek } aliases[] = {
1610209230bSgjelinek 	{ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100},
1620209230bSgjelinek 	{ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0},
1630209230bSgjelinek 	{ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0},
1640209230bSgjelinek 	{ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0},
1650209230bSgjelinek 	{ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0},
1660209230bSgjelinek 	{ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0},
1670209230bSgjelinek 	{ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0},
1680209230bSgjelinek 	{ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0},
169c97ad5cdSakolb 	{ALIAS_CPUCAP, "zone.cpu-cap", "privileged", "deny", 0},
1700209230bSgjelinek 	{NULL, NULL, NULL, NULL, 0}
1710209230bSgjelinek };
1720209230bSgjelinek 
1730209230bSgjelinek /*
1740209230bSgjelinek  * Structure for applying rctls to a running zone.  It allows important
1750209230bSgjelinek  * process values to be passed together easily.
1760209230bSgjelinek  */
1770209230bSgjelinek typedef struct pr_info_handle {
1780209230bSgjelinek 	struct ps_prochandle *pr;
1790209230bSgjelinek 	pid_t pid;
1800209230bSgjelinek } pr_info_handle_t;
1810209230bSgjelinek 
1827c478bd9Sstevel@tonic-gate struct zone_dochandle {
1837c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1847c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1857c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1867c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
187087719fdSdp 	boolean_t	zone_dh_newzone;
188087719fdSdp 	boolean_t	zone_dh_snapshot;
189ee519a1fSgjelinek 	boolean_t	zone_dh_sw_inv;
190087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
1917c478bd9Sstevel@tonic-gate };
1927c478bd9Sstevel@tonic-gate 
193cf8f45c7Sdstaff struct znotify {
194cf8f45c7Sdstaff 	void * zn_private;
195cf8f45c7Sdstaff 	evchan_t *zn_eventchan;
196cf8f45c7Sdstaff 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
197cf8f45c7Sdstaff 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
198cf8f45c7Sdstaff 	pthread_mutex_t zn_mutex;
199cf8f45c7Sdstaff 	pthread_cond_t zn_cond;
200cf8f45c7Sdstaff 	pthread_mutex_t zn_bigmutex;
201cf8f45c7Sdstaff 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
202cf8f45c7Sdstaff 	    ZN_PING_RECEIVED} zn_state;
203cf8f45c7Sdstaff 	char zn_subscriber_id[MAX_SUBID_LEN];
204cf8f45c7Sdstaff 	volatile boolean_t zn_failed;
205cf8f45c7Sdstaff 	int zn_failure_count;
206cf8f45c7Sdstaff };
207cf8f45c7Sdstaff 
208ff17c8bfSgjelinek /* used to track nested zone-lock operations */
209ff17c8bfSgjelinek static int zone_lock_cnt = 0;
210ee519a1fSgjelinek 
211ff17c8bfSgjelinek /* used to communicate lock status to children */
212ff17c8bfSgjelinek #define	LOCK_ENV_VAR	"_ZONEADM_LOCK_HELD"
213ff17c8bfSgjelinek static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1";
214ff17c8bfSgjelinek static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0";
21539935be5Sgjelinek 
216108322fbScarlsonj char *zonecfg_root = "";
217108322fbScarlsonj 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
2207c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
2217c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
2227c478bd9Sstevel@tonic-gate  * values from this set.
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
226108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
227108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
228108322fbScarlsonj  * configuration inside mounted alternate boot environments.
229108322fbScarlsonj  */
230108322fbScarlsonj void
231108322fbScarlsonj zonecfg_set_root(const char *rootpath)
232108322fbScarlsonj {
233108322fbScarlsonj 	if (*zonecfg_root != '\0')
234108322fbScarlsonj 		free(zonecfg_root);
235108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
236108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
237108322fbScarlsonj 		zonecfg_root = "";
238108322fbScarlsonj }
239108322fbScarlsonj 
240108322fbScarlsonj const char *
241108322fbScarlsonj zonecfg_get_root(void)
242108322fbScarlsonj {
243108322fbScarlsonj 	return (zonecfg_root);
244108322fbScarlsonj }
245108322fbScarlsonj 
246108322fbScarlsonj boolean_t
247108322fbScarlsonj zonecfg_in_alt_root(void)
248108322fbScarlsonj {
249108322fbScarlsonj 	return (*zonecfg_root != '\0');
250108322fbScarlsonj }
251108322fbScarlsonj 
252108322fbScarlsonj /*
2537c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
2547c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate 
257108322fbScarlsonj static boolean_t
2587c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
2597c478bd9Sstevel@tonic-gate {
260108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
261108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
264108322fbScarlsonj static boolean_t
265108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
2667c478bd9Sstevel@tonic-gate {
267108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
268108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2727c478bd9Sstevel@tonic-gate static void
2737c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
2777c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
2787c478bd9Sstevel@tonic-gate 	 */
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate zone_dochandle_t
2827c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
2837c478bd9Sstevel@tonic-gate {
284087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
2857c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
2867c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
2877c478bd9Sstevel@tonic-gate 		return (NULL);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
2917c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
2927c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
2937c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
2947c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
2957c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
2967c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	return (handle);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate int
3027c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
3057c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3067c478bd9Sstevel@tonic-gate 	return (Z_OK);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate void
3107c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
3137c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
3147c478bd9Sstevel@tonic-gate 	if (handle != NULL)
3157c478bd9Sstevel@tonic-gate 		free(handle);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static int
3197c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
3227c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
3237c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
3247c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
3257c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
3267c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 	return (Z_OK);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate int
332087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
335087719fdSdp 	struct zoneent ze;
336087719fdSdp 	int err, state_err;
337087719fdSdp 	zone_state_t state;
3387c478bd9Sstevel@tonic-gate 
339108322fbScarlsonj 	if (!config_file_path(zonename, path))
340108322fbScarlsonj 		return (Z_MISC_FS);
341087719fdSdp 
342087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
343087719fdSdp 	err = access(path, W_OK);
344087719fdSdp 
345087719fdSdp 	/*
346087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
347087719fdSdp 	 * such zone exists.
348087719fdSdp 	 */
349087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
350087719fdSdp 		return (Z_NO_ZONE);
351087719fdSdp 
352087719fdSdp 	/*
353087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
354087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
355087719fdSdp 	 * in which case we plow on.
356087719fdSdp 	 */
357087719fdSdp 	if (err == -1 && errno != ENOENT) {
358087719fdSdp 		if (errno == EACCES)
359087719fdSdp 			return (Z_ACCES);
360087719fdSdp 		else if (!force)
361087719fdSdp 			return (Z_MISC_FS);
362087719fdSdp 	}
363087719fdSdp 
364087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
365087719fdSdp 		return (Z_BAD_ZONE_STATE);
366087719fdSdp 
367087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
368087719fdSdp 		return (Z_BAD_ZONE_STATE);
369087719fdSdp 
370087719fdSdp 	/*
371087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
372087719fdSdp 	 * will fail only if we've had some more severe problem.
373087719fdSdp 	 */
374087719fdSdp 	bzero(&ze, sizeof (ze));
375087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
376087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
377087719fdSdp 		if (!force)
378087719fdSdp 			return (err);
379087719fdSdp 
380087719fdSdp 	err = zonecfg_destroy_impl(path);
381087719fdSdp 
382087719fdSdp 	/*
383087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
384087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
385087719fdSdp 	 */
386087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
387087719fdSdp 		return (Z_OK);
388087719fdSdp 	return (err);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate int
392108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
3957c478bd9Sstevel@tonic-gate 
396108322fbScarlsonj 	if (!snap_file_path(zonename, path))
397108322fbScarlsonj 		return (Z_MISC_FS);
3987c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate static int
402a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
4057c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
4067c478bd9Sstevel@tonic-gate 
407a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
408a1be23daSdp 
409a1be23daSdp 	if (*root == NULL)
4107c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
411a1be23daSdp 
412a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
413a1be23daSdp 		return (Z_WRONG_DOC_TYPE);
414a1be23daSdp 
415a1be23daSdp 	return (Z_OK);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
418a1be23daSdp static int
419a1be23daSdp operation_prep(zone_dochandle_t handle)
420a1be23daSdp {
421a1be23daSdp 	xmlNodePtr root;
422a1be23daSdp 	int err;
423a1be23daSdp 
424a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
425a1be23daSdp 		return (err);
426a1be23daSdp 
427a1be23daSdp 	handle->zone_dh_cur = root;
428a1be23daSdp 	handle->zone_dh_top = root;
429a1be23daSdp 	return (Z_OK);
4307c478bd9Sstevel@tonic-gate }
431a1be23daSdp 
432a1be23daSdp static int
433ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
434ffbafc53Scomay {
435ffbafc53Scomay 	xmlChar *property;
436ffbafc53Scomay 	size_t srcsize;
437ffbafc53Scomay 
438ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
439ffbafc53Scomay 		return (Z_BAD_PROPERTY);
440ffbafc53Scomay 	srcsize = strlcpy(dst, (char *)property, dstsize);
441ffbafc53Scomay 	xmlFree(property);
442ffbafc53Scomay 	if (srcsize >= dstsize)
443ffbafc53Scomay 		return (Z_TOO_BIG);
444ffbafc53Scomay 	return (Z_OK);
445ffbafc53Scomay }
446ffbafc53Scomay 
447ffbafc53Scomay static int
448ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
449ffbafc53Scomay {
450ffbafc53Scomay 	xmlChar *property;
451ffbafc53Scomay 
452ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
453ffbafc53Scomay 		return (Z_BAD_PROPERTY);
454ffbafc53Scomay 	if ((*dst = strdup((char *)property)) == NULL) {
455ffbafc53Scomay 		xmlFree(property);
456ffbafc53Scomay 		return (Z_NOMEM);
457ffbafc53Scomay 	}
458ffbafc53Scomay 	xmlFree(property);
459ffbafc53Scomay 	return (Z_OK);
460ffbafc53Scomay }
461ffbafc53Scomay 
462ffbafc53Scomay static int
463a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
464a1be23daSdp     char *propval, size_t propsize)
465a1be23daSdp {
466a1be23daSdp 	xmlNodePtr root;
467a1be23daSdp 	int err;
468a1be23daSdp 
469a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
470a1be23daSdp 		return (err);
471a1be23daSdp 
472ffbafc53Scomay 	return (fetchprop(root, propname, propval, propsize));
473ffbafc53Scomay }
474ffbafc53Scomay 
475ffbafc53Scomay static int
476ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
477ffbafc53Scomay     char **propval)
478ffbafc53Scomay {
479ffbafc53Scomay 	xmlNodePtr root;
480ffbafc53Scomay 	int err;
481ffbafc53Scomay 
482ffbafc53Scomay 	if ((err = getroot(handle, &root)) != 0)
483ffbafc53Scomay 		return (err);
484ffbafc53Scomay 
485ffbafc53Scomay 	return (fetch_alloc_prop(root, propname, propval));
486a1be23daSdp }
487a1be23daSdp 
488a1be23daSdp static int
489108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
490108322fbScarlsonj     const char *propval)
491a1be23daSdp {
492a1be23daSdp 	int err;
493a1be23daSdp 	xmlNodePtr root;
494a1be23daSdp 
495a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
496a1be23daSdp 		return (err);
497a1be23daSdp 
4980209230bSgjelinek 	/*
4990209230bSgjelinek 	 * If we get a null propval remove the property (ignore return since it
5000209230bSgjelinek 	 * may not be set to begin with).
5010209230bSgjelinek 	 */
5020209230bSgjelinek 	if (propval == NULL) {
5030209230bSgjelinek 		(void) xmlUnsetProp(root, propname);
5040209230bSgjelinek 	} else {
5050209230bSgjelinek 		if (xmlSetProp(root, propname, (const xmlChar *) propval)
5060209230bSgjelinek 		    == NULL)
507a1be23daSdp 			return (Z_INVAL);
5080209230bSgjelinek 	}
5097c478bd9Sstevel@tonic-gate 	return (Z_OK);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
512087719fdSdp static void
513087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
514087719fdSdp {
515087719fdSdp 	xmlNodePtr node;
516087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
517087719fdSdp 
518087719fdSdp 	if (node != NULL)
519087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
520087719fdSdp }
521087719fdSdp 
522087719fdSdp static void
523087719fdSdp stripcomments(zone_dochandle_t handle)
524087719fdSdp {
525087719fdSdp 	xmlDocPtr top;
526087719fdSdp 	xmlNodePtr child, next;
527087719fdSdp 
528087719fdSdp 	top = handle->zone_dh_doc;
529087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
530087719fdSdp 		next = child->next;
531087719fdSdp 		if (child->name == NULL)
532087719fdSdp 			continue;
533087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
534087719fdSdp 			next = child->next;
535087719fdSdp 			xmlUnlinkNode(child);
536087719fdSdp 			xmlFreeNode(child);
537087719fdSdp 		}
538087719fdSdp 	}
539087719fdSdp }
540087719fdSdp 
541ee519a1fSgjelinek static void
542ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle)
543ee519a1fSgjelinek {
544ee519a1fSgjelinek 	xmlNodePtr root, child, next;
545ee519a1fSgjelinek 
546ee519a1fSgjelinek 	root = xmlDocGetRootElement(handle->zone_dh_doc);
547ee519a1fSgjelinek 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
548ee519a1fSgjelinek 		next = child->next;
549ee519a1fSgjelinek 		if (child->name == NULL)
550ee519a1fSgjelinek 			continue;
551ee519a1fSgjelinek 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
552ee519a1fSgjelinek 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
553ee519a1fSgjelinek 			next = child->next;
554ee519a1fSgjelinek 			xmlUnlinkNode(child);
555ee519a1fSgjelinek 			xmlFreeNode(child);
556ee519a1fSgjelinek 		}
557ee519a1fSgjelinek 	}
558ee519a1fSgjelinek }
559ee519a1fSgjelinek 
5607c478bd9Sstevel@tonic-gate static int
561108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
562108322fbScarlsonj     zone_dochandle_t handle)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
5657c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5667c478bd9Sstevel@tonic-gate 	int valid;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
5697c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5709acbbeafSnn35248 
5717c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
5727c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
5737c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
5747c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
5757c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
5787c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
5797c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
5807c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
5817c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
5827c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
5837c478bd9Sstevel@tonic-gate 	if (valid == 0)
5847c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
585087719fdSdp 
5867c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
587087719fdSdp 	stripcomments(handle);
5887c478bd9Sstevel@tonic-gate 	return (Z_OK);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate int
592108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
5957c478bd9Sstevel@tonic-gate 
596108322fbScarlsonj 	if (!config_file_path(zonename, path))
597108322fbScarlsonj 		return (Z_MISC_FS);
598087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
599087719fdSdp 
6007c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate int
60416ab8c7bSgjelinek zonecfg_get_attach_handle(const char *path, const char *fname,
60516ab8c7bSgjelinek     const char *zonename, boolean_t preserve_sw, zone_dochandle_t handle)
606ee519a1fSgjelinek {
607ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
608ee519a1fSgjelinek 	int		err;
609ee519a1fSgjelinek 	struct stat	buf;
610ee519a1fSgjelinek 
611ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
612ee519a1fSgjelinek 	    sizeof (migpath))
613ee519a1fSgjelinek 		return (Z_NOMEM);
614ee519a1fSgjelinek 
615ee519a1fSgjelinek 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
616ee519a1fSgjelinek 		return (Z_NO_ZONE);
617ee519a1fSgjelinek 
61816ab8c7bSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, fname) >=
619ee519a1fSgjelinek 	    sizeof (migpath))
620ee519a1fSgjelinek 		return (Z_NOMEM);
621ee519a1fSgjelinek 
622ee519a1fSgjelinek 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
623ee519a1fSgjelinek 		return (err);
624ee519a1fSgjelinek 
625ee519a1fSgjelinek 	if (!preserve_sw)
626ee519a1fSgjelinek 		strip_sw_inv(handle);
627ee519a1fSgjelinek 
628ee519a1fSgjelinek 	handle->zone_dh_newzone = B_TRUE;
629ee519a1fSgjelinek 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
630ee519a1fSgjelinek 		return (err);
631ee519a1fSgjelinek 
632ee519a1fSgjelinek 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
633ee519a1fSgjelinek }
634ee519a1fSgjelinek 
635ee519a1fSgjelinek int
636108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6397c478bd9Sstevel@tonic-gate 
640108322fbScarlsonj 	if (!snap_file_path(zonename, path))
641108322fbScarlsonj 		return (Z_MISC_FS);
642087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
6437c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate int
647108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
648087719fdSdp     zone_dochandle_t handle)
649087719fdSdp {
650087719fdSdp 	char path[MAXPATHLEN];
651087719fdSdp 	int err;
652087719fdSdp 
653108322fbScarlsonj 	if (!config_file_path(template, path))
654108322fbScarlsonj 		return (Z_MISC_FS);
655087719fdSdp 
656087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
657087719fdSdp 		return (err);
658087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
659087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
660087719fdSdp }
661087719fdSdp 
6629acbbeafSnn35248 int
6639acbbeafSnn35248 zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle)
6649acbbeafSnn35248 {
6659acbbeafSnn35248 	struct stat buf;
6669acbbeafSnn35248 	int err;
6679acbbeafSnn35248 
6689acbbeafSnn35248 	if (stat(path, &buf) == -1)
6699acbbeafSnn35248 		return (Z_MISC_FS);
6709acbbeafSnn35248 
6719acbbeafSnn35248 	if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK)
6729acbbeafSnn35248 		return (err);
6739acbbeafSnn35248 	handle->zone_dh_newzone = B_TRUE;
6749acbbeafSnn35248 	return (Z_OK);
6759acbbeafSnn35248 }
6769acbbeafSnn35248 
6778cd327d5Sgjelinek /*
6788cd327d5Sgjelinek  * Initialize two handles from the manifest read on fd.  The rem_handle
6798cd327d5Sgjelinek  * is initialized from the input file, including the sw inventory.  The
6808cd327d5Sgjelinek  * local_handle is initialized with the same zone configuration but with
6818cd327d5Sgjelinek  * no sw inventory.
6828cd327d5Sgjelinek  */
6838cd327d5Sgjelinek int
6848cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
6858cd327d5Sgjelinek     zone_dochandle_t rem_handle)
6868cd327d5Sgjelinek {
6878cd327d5Sgjelinek 	xmlValidCtxtPtr cvp;
6888cd327d5Sgjelinek 	int valid;
6898cd327d5Sgjelinek 
6908cd327d5Sgjelinek 	/* load the manifest into the handle for the remote system */
6918cd327d5Sgjelinek 	if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
6928cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
6938cd327d5Sgjelinek 	}
6948cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
6958cd327d5Sgjelinek 		return (Z_NOMEM);
6968cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
6978cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
6988cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc);
6998cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7008cd327d5Sgjelinek 	if (valid == 0)
7018cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7028cd327d5Sgjelinek 
7038cd327d5Sgjelinek 	/* delete any comments such as inherited Sun copyright / ident str */
7048cd327d5Sgjelinek 	stripcomments(rem_handle);
7058cd327d5Sgjelinek 
7068cd327d5Sgjelinek 	rem_handle->zone_dh_newzone = B_TRUE;
7078cd327d5Sgjelinek 	rem_handle->zone_dh_sw_inv = B_TRUE;
7088cd327d5Sgjelinek 
7098cd327d5Sgjelinek 	/*
7108cd327d5Sgjelinek 	 * Now use the remote system handle to generate a local system handle
7118cd327d5Sgjelinek 	 * with an identical zones configuration but no sw inventory.
7128cd327d5Sgjelinek 	 */
7138cd327d5Sgjelinek 	if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
7148cd327d5Sgjelinek 	    1)) == NULL) {
7158cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7168cd327d5Sgjelinek 	}
7178cd327d5Sgjelinek 
7188cd327d5Sgjelinek 	/*
7198cd327d5Sgjelinek 	 * We need to re-run xmlValidateDocument on local_handle to properly
7208cd327d5Sgjelinek 	 * update the in-core representation of the configuration.
7218cd327d5Sgjelinek 	 */
7228cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7238cd327d5Sgjelinek 		return (Z_NOMEM);
7248cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7258cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7268cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
7278cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7288cd327d5Sgjelinek 	if (valid == 0)
7298cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7308cd327d5Sgjelinek 
7318cd327d5Sgjelinek 	strip_sw_inv(local_handle);
7328cd327d5Sgjelinek 
7338cd327d5Sgjelinek 	local_handle->zone_dh_newzone = B_TRUE;
7348cd327d5Sgjelinek 	local_handle->zone_dh_sw_inv = B_FALSE;
7358cd327d5Sgjelinek 
7368cd327d5Sgjelinek 	return (Z_OK);
7378cd327d5Sgjelinek }
7388cd327d5Sgjelinek 
739087719fdSdp static boolean_t
740087719fdSdp is_renaming(zone_dochandle_t handle)
741087719fdSdp {
742087719fdSdp 	if (handle->zone_dh_newzone)
743087719fdSdp 		return (B_FALSE);
744087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
745087719fdSdp 		return (B_TRUE);
746087719fdSdp 	return (B_FALSE);
747087719fdSdp }
748087719fdSdp 
749087719fdSdp static boolean_t
750087719fdSdp is_new(zone_dochandle_t handle)
751087719fdSdp {
752087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
753087719fdSdp }
754087719fdSdp 
755087719fdSdp static boolean_t
756087719fdSdp is_snapshot(zone_dochandle_t handle)
757087719fdSdp {
758087719fdSdp 	return (handle->zone_dh_snapshot);
759087719fdSdp }
760087719fdSdp 
761087719fdSdp /*
762087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
763087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
764087719fdSdp  * safety if this routine had to change the app locale on the fly.
765087719fdSdp  */
766087719fdSdp int
767108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
768087719fdSdp {
769087719fdSdp 	int i;
770087719fdSdp 
771087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
772087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
773087719fdSdp 
774087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
775087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
776087719fdSdp 
777087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
778087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
779087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
780087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
781087719fdSdp 
782087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
783087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
784087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
785087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
786087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
787087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
788087719fdSdp 	}
789087719fdSdp 
790087719fdSdp 	return (Z_OK);
791087719fdSdp }
792087719fdSdp 
793087719fdSdp /*
794087719fdSdp  * Changing the zone name requires us to track both the old and new
795087719fdSdp  * name of the zone until commit time.
796087719fdSdp  */
797087719fdSdp int
7987c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
7997c478bd9Sstevel@tonic-gate {
800a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
803a1be23daSdp int
804a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
805a1be23daSdp {
806087719fdSdp 	zone_state_t state;
807087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
808087719fdSdp 	int err;
809087719fdSdp 
810087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
811087719fdSdp 	    sizeof (curname))) != Z_OK)
812087719fdSdp 		return (err);
813087719fdSdp 
814087719fdSdp 	if (strcmp(name, curname) == 0)
815087719fdSdp 		return (Z_OK);
816087719fdSdp 
817087719fdSdp 	/*
818087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
819087719fdSdp 	 */
820087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
821087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
822087719fdSdp 
823087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
824087719fdSdp 		return (err);
825087719fdSdp 
826087719fdSdp 	/*
827087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
828087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
829087719fdSdp 	 * name as "in use," so special case here.
830087719fdSdp 	 */
831087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
832087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
833087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
834087719fdSdp 		return (err);
835087719fdSdp 	}
836087719fdSdp 
837087719fdSdp 	/* Check to see if new name chosen is already in use */
838087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
839087719fdSdp 		return (Z_NAME_IN_USE);
840087719fdSdp 
841087719fdSdp 	/*
842087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
843087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
844087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
845087719fdSdp 	 */
846087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
847087719fdSdp 	    sizeof (old_delname));
848087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
849087719fdSdp 		/*
850087719fdSdp 		 * Name change is allowed only when the zone we're altering
851087719fdSdp 		 * is not ready or running.
852087719fdSdp 		 */
853087719fdSdp 		err = zone_get_state(curname, &state);
854087719fdSdp 		if (err == Z_OK) {
855087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
856087719fdSdp 				return (Z_BAD_ZONE_STATE);
857087719fdSdp 		} else if (err != Z_NO_ZONE) {
858087719fdSdp 			return (err);
859087719fdSdp 		}
860087719fdSdp 
861087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
862087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
863087719fdSdp 		assert(is_renaming(handle));
864087719fdSdp 	} else if (is_renaming(handle)) {
865087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
866087719fdSdp 		if (err == Z_OK) {
867087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
868087719fdSdp 				return (Z_BAD_ZONE_STATE);
869087719fdSdp 		} else if (err != Z_NO_ZONE) {
870087719fdSdp 			return (err);
871087719fdSdp 		}
872087719fdSdp 	}
873087719fdSdp 
874087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
875087719fdSdp 		/*
876087719fdSdp 		 * Restore the deletename to whatever it was at the
877087719fdSdp 		 * top of the routine, since we've had a failure.
878087719fdSdp 		 */
879087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
880087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
881087719fdSdp 		return (err);
882087719fdSdp 	}
883087719fdSdp 
884087719fdSdp 	return (Z_OK);
8857c478bd9Sstevel@tonic-gate }
886a1be23daSdp 
887a1be23daSdp int
888a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
889a1be23daSdp {
890108322fbScarlsonj 	size_t len;
891108322fbScarlsonj 
892108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
893108322fbScarlsonj 		return (Z_TOO_BIG);
894108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
895108322fbScarlsonj 	    pathsize - len));
896a1be23daSdp }
897a1be23daSdp 
898a1be23daSdp int
899a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
900a1be23daSdp {
901555afedfScarlsonj 	size_t len;
902555afedfScarlsonj 
903555afedfScarlsonj 	/*
904555afedfScarlsonj 	 * The user deals in absolute paths in the running global zone, but the
905555afedfScarlsonj 	 * internal configuration files deal with boot environment relative
906555afedfScarlsonj 	 * paths.  Strip out the alternate root when specified.
907555afedfScarlsonj 	 */
908555afedfScarlsonj 	len = strlen(zonecfg_root);
909555afedfScarlsonj 	if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
910555afedfScarlsonj 		return (Z_BAD_PROPERTY);
911555afedfScarlsonj 	zonepath += len;
912a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
913a1be23daSdp }
914a1be23daSdp 
915a1be23daSdp int
9169acbbeafSnn35248 zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
9179acbbeafSnn35248 {
9189acbbeafSnn35248 	int ret, sz;
9199acbbeafSnn35248 
9209acbbeafSnn35248 	ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
9219acbbeafSnn35248 
9229acbbeafSnn35248 	/* If the zone has no brand, it is native. */
9239acbbeafSnn35248 	if (ret == Z_OK && brand[0] == '\0') {
9249acbbeafSnn35248 		sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
9259acbbeafSnn35248 		if (sz >= brandsize)
9269acbbeafSnn35248 			ret = Z_TOO_BIG;
9279acbbeafSnn35248 		else
9289acbbeafSnn35248 			ret = Z_OK;
9299acbbeafSnn35248 	}
9309acbbeafSnn35248 
9319acbbeafSnn35248 	return (ret);
9329acbbeafSnn35248 }
9339acbbeafSnn35248 
9349acbbeafSnn35248 int
9359acbbeafSnn35248 zonecfg_set_brand(zone_dochandle_t handle, char *brand)
9369acbbeafSnn35248 {
9379acbbeafSnn35248 	return (setrootattr(handle, DTD_ATTR_BRAND, brand));
9389acbbeafSnn35248 }
9399acbbeafSnn35248 
9409acbbeafSnn35248 int
941a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
942a1be23daSdp {
943a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
944a1be23daSdp 	int ret;
945a1be23daSdp 
946a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
947a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
948a1be23daSdp 		return (ret);
949a1be23daSdp 
950a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
951a1be23daSdp 		*autoboot = B_TRUE;
952a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
953a1be23daSdp 		*autoboot = B_FALSE;
954a1be23daSdp 	else
955a1be23daSdp 		ret = Z_BAD_PROPERTY;
956a1be23daSdp 	return (ret);
957a1be23daSdp }
958a1be23daSdp 
959a1be23daSdp int
960a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
961a1be23daSdp {
962a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
963a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
964a1be23daSdp }
965a1be23daSdp 
966a1be23daSdp int
967a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
968a1be23daSdp {
969a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
970a1be23daSdp }
971a1be23daSdp 
972a1be23daSdp int
973a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
974a1be23daSdp {
975a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
976a1be23daSdp }
977a1be23daSdp 
978ffbafc53Scomay int
979ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
980ffbafc53Scomay {
981ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
982ffbafc53Scomay }
983ffbafc53Scomay 
984ffbafc53Scomay int
9853f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
986ffbafc53Scomay {
9873f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
9883f2f09c1Sdp }
9893f2f09c1Sdp 
9903f2f09c1Sdp int
9913f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
9923f2f09c1Sdp {
9933f2f09c1Sdp 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
9943f2f09c1Sdp }
9953f2f09c1Sdp 
9963f2f09c1Sdp int
9973f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
9983f2f09c1Sdp {
9993f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
1000ffbafc53Scomay }
1001ffbafc53Scomay 
10020209230bSgjelinek int
10030209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
10040209230bSgjelinek {
10050209230bSgjelinek 	return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
10060209230bSgjelinek }
10070209230bSgjelinek 
10080209230bSgjelinek int
10090209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched)
10100209230bSgjelinek {
10110209230bSgjelinek 	return (setrootattr(handle, DTD_ATTR_SCHED, sched));
10120209230bSgjelinek }
10130209230bSgjelinek 
1014a1be23daSdp /*
1015a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
1016a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
1017a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
1018a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
1019087719fdSdp  *
1020087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
1021087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
1022087719fdSdp  * In general, the operation of this function should succeed or fail as
1023087719fdSdp  * a unit.
1024a1be23daSdp  */
1025a1be23daSdp int
1026a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
1027a1be23daSdp {
1028a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
1029a1be23daSdp 	struct zoneent ze;
1030a1be23daSdp 	int err;
1031087719fdSdp 	int opcode;
1032087719fdSdp 	char *zn;
1033087719fdSdp 
1034087719fdSdp 	bzero(&ze, sizeof (ze));
1035087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
1036a1be23daSdp 
1037a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
1038a1be23daSdp 		return (err);
1039087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
1040087719fdSdp 
1041a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
1042a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
1043a1be23daSdp 		return (err);
1044555afedfScarlsonj 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
1045555afedfScarlsonj 	    sizeof (ze.zone_path));
1046087719fdSdp 
1047087719fdSdp 	if (is_renaming(handle)) {
1048087719fdSdp 		opcode = PZE_MODIFY;
1049087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
1050087719fdSdp 		    sizeof (ze.zone_name));
1051087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
1052087719fdSdp 	} else if (is_new(handle)) {
1053087719fdSdp 		FILE *cookie;
1054087719fdSdp 		/*
1055087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
1056087719fdSdp 		 * since we might be forcibly overwriting an existing
1057087719fdSdp 		 * configuration with a new one (for example 'create -F'
1058087719fdSdp 		 * in zonecfg).
1059087719fdSdp 		 */
1060087719fdSdp 		opcode = PZE_ADD;
1061087719fdSdp 		cookie = setzoneent();
1062087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
1063087719fdSdp 			if (strcmp(zn, name) == 0) {
1064087719fdSdp 				opcode = PZE_MODIFY;
1065087719fdSdp 				free(zn);
1066087719fdSdp 				break;
1067087719fdSdp 			}
1068087719fdSdp 			free(zn);
1069087719fdSdp 		}
1070087719fdSdp 		endzoneent(cookie);
1071087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
1072087719fdSdp 	} else {
1073087719fdSdp 		opcode = PZE_MODIFY;
10747c478bd9Sstevel@tonic-gate 	}
10757c478bd9Sstevel@tonic-gate 
1076087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
1077087719fdSdp 		return (err);
1078087719fdSdp 
1079087719fdSdp 	return (Z_OK);
1080087719fdSdp }
1081087719fdSdp 
1082087719fdSdp /*
1083087719fdSdp  * The goal of this routine is to cause the index file update and the
1084087719fdSdp  * document save to happen as an atomic operation.  We do the document
1085087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
1086087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
1087087719fdSdp  *
1088087719fdSdp  * Strategy:
1089087719fdSdp  *
1090087719fdSdp  * New zone 'foo' configuration:
1091087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1092087719fdSdp  * 	Write XML to tmpfile
1093087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1094087719fdSdp  * 	Add entry to index file
1095087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
1096087719fdSdp  *
1097087719fdSdp  * Save existing zone 'foo':
1098087719fdSdp  * 	Make backup of foo.xml -> .backup
1099087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1100087719fdSdp  * 	Write XML to tmpfile
1101087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1102087719fdSdp  * 	Modify index file as needed
1103087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
1104087719fdSdp  *
1105087719fdSdp  * Rename 'foo' to 'bar':
1106087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1107087719fdSdp  * 	Write XML to tmpfile
1108087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1109087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1110087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
1111087719fdSdp  */
11127c478bd9Sstevel@tonic-gate static int
11137c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
1116087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
11179acbbeafSnn35248 	int tmpfd, err, valid;
11187c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
1119087719fdSdp 	boolean_t backup;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
11227c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
11237c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
11267c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
11277c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
11287c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
11337c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
11347c478bd9Sstevel@tonic-gate 
1135087719fdSdp 	/*
11369acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
11379acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
11389acbbeafSnn35248 	 * assert() that the doc is valid.
1139087719fdSdp 	 */
11409acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
11419acbbeafSnn35248 	assert(valid != 0);
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
11447c478bd9Sstevel@tonic-gate 		goto err;
1145087719fdSdp 
11467c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
11477c478bd9Sstevel@tonic-gate 
1148087719fdSdp 	/*
1149087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
1150087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
1151087719fdSdp 	 * something goes wrong.
1152087719fdSdp 	 */
1153087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
1154087719fdSdp 		backup = B_TRUE;
1155087719fdSdp 
1156087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1157087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1158087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1159087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
1160087719fdSdp 
1161087719fdSdp 		if (link(filename, bakfile) == -1) {
1162087719fdSdp 			err = errno;
11637c478bd9Sstevel@tonic-gate 			(void) unlink(tmpfile);
11647c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
11657c478bd9Sstevel@tonic-gate 				return (Z_ACCES);
11667c478bd9Sstevel@tonic-gate 			return (Z_MISC_FS);
11677c478bd9Sstevel@tonic-gate 		}
1168087719fdSdp 	}
1169a1be23daSdp 
1170087719fdSdp 	/*
1171087719fdSdp 	 * Move the new document over top of the old.
1172087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1173087719fdSdp 	 */
1174087719fdSdp 	if (rename(tmpfile, filename) == -1) {
1175087719fdSdp 		err = errno;
1176087719fdSdp 		(void) unlink(tmpfile);
1177087719fdSdp 		if (backup)
1178087719fdSdp 			(void) unlink(bakfile);
1179087719fdSdp 		if (err == EACCES)
1180087719fdSdp 			return (Z_ACCES);
1181087719fdSdp 		return (Z_MISC_FS);
1182087719fdSdp 	}
1183087719fdSdp 
1184087719fdSdp 	/*
1185087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
1186087719fdSdp 	 */
1187087719fdSdp 	if (is_snapshot(handle))
1188087719fdSdp 		return (Z_OK);
1189087719fdSdp 
1190087719fdSdp 	/* now update the index file to reflect whatever we just did */
1191087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1192087719fdSdp 		if (backup) {
1193087719fdSdp 			/*
1194087719fdSdp 			 * Try to restore from our backup.
1195087719fdSdp 			 */
1196087719fdSdp 			(void) unlink(filename);
1197087719fdSdp 			(void) rename(bakfile, filename);
1198087719fdSdp 		} else {
1199087719fdSdp 			/*
1200087719fdSdp 			 * Either the zone is new, in which case we can delete
1201087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1202087719fdSdp 			 */
1203087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1204087719fdSdp 			(void) unlink(filename);
1205087719fdSdp 		}
1206087719fdSdp 		return (Z_UPDATING_INDEX);
1207087719fdSdp 	}
1208087719fdSdp 
1209087719fdSdp 	if (backup)
1210087719fdSdp 		(void) unlink(bakfile);
1211087719fdSdp 
1212087719fdSdp 	return (Z_OK);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate err:
12157c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
12167c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
12177c478bd9Sstevel@tonic-gate }
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate int
12207c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
12217c478bd9Sstevel@tonic-gate {
1222087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1223087719fdSdp 	char delpath[MAXPATHLEN];
1224087719fdSdp 	int err = Z_SAVING_FILE;
12257c478bd9Sstevel@tonic-gate 
1226087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1227087719fdSdp 		return (Z_BAD_HANDLE);
1228087719fdSdp 
1229087719fdSdp 	/*
1230ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1231ee519a1fSgjelinek 	 * inventory at this time.
1232087719fdSdp 	 */
1233ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1234087719fdSdp 		return (Z_INVAL);
1235087719fdSdp 
1236087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
12377c478bd9Sstevel@tonic-gate 		return (err);
1238087719fdSdp 
1239108322fbScarlsonj 	if (!config_file_path(zname, path))
1240108322fbScarlsonj 		return (Z_MISC_FS);
1241087719fdSdp 
1242087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1243087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1244087719fdSdp 
1245087719fdSdp 	err = zonecfg_save_impl(handle, path);
1246087719fdSdp 
1247087719fdSdp 	stripcomments(handle);
1248087719fdSdp 
1249087719fdSdp 	if (err != Z_OK)
1250087719fdSdp 		return (err);
1251087719fdSdp 
1252087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1253087719fdSdp 
1254087719fdSdp 	if (is_renaming(handle)) {
1255108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1256087719fdSdp 			(void) unlink(delpath);
1257087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1258087719fdSdp 	}
1259087719fdSdp 
1260087719fdSdp 	return (Z_OK);
12617c478bd9Sstevel@tonic-gate }
12627c478bd9Sstevel@tonic-gate 
1263ee519a1fSgjelinek int
12649acbbeafSnn35248 zonecfg_verify_save(zone_dochandle_t handle, char *filename)
12659acbbeafSnn35248 {
12669acbbeafSnn35248 	int valid;
12679acbbeafSnn35248 
12689acbbeafSnn35248 	xmlValidCtxt cvp = { NULL };
12699acbbeafSnn35248 
12709acbbeafSnn35248 	if (zonecfg_check_handle(handle) != Z_OK)
12719acbbeafSnn35248 		return (Z_BAD_HANDLE);
12729acbbeafSnn35248 
12739acbbeafSnn35248 	cvp.error = zonecfg_error_func;
12749acbbeafSnn35248 	cvp.warning = zonecfg_error_func;
12759acbbeafSnn35248 
12769acbbeafSnn35248 	/*
12779acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
12789acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
12799acbbeafSnn35248 	 * assert() that the doc is valid.
12809acbbeafSnn35248 	 */
12819acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
12829acbbeafSnn35248 	assert(valid != 0);
12839acbbeafSnn35248 
12849acbbeafSnn35248 	if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
12859acbbeafSnn35248 		return (Z_SAVING_FILE);
12869acbbeafSnn35248 
12879acbbeafSnn35248 	return (Z_OK);
12889acbbeafSnn35248 }
12899acbbeafSnn35248 
12909acbbeafSnn35248 int
12918cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1292ee519a1fSgjelinek {
1293ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1294ee519a1fSgjelinek 	char path[MAXPATHLEN];
1295ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1296ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1297ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
12989acbbeafSnn35248 	int valid;
1299ee519a1fSgjelinek 
1300ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1301ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1302ee519a1fSgjelinek 
13038cd327d5Sgjelinek 	if (flags & ZONE_DRY_RUN) {
13048cd327d5Sgjelinek 		(void) strlcpy(migpath, "-", sizeof (migpath));
13058cd327d5Sgjelinek 	} else {
13068cd327d5Sgjelinek 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
13078cd327d5Sgjelinek 		    != Z_OK)
1308ee519a1fSgjelinek 			return (err);
1309ee519a1fSgjelinek 
13108cd327d5Sgjelinek 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
13118cd327d5Sgjelinek 		    != Z_OK)
1312ee519a1fSgjelinek 			return (err);
1313ee519a1fSgjelinek 
131416ab8c7bSgjelinek 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path,
131516ab8c7bSgjelinek 		    ZONE_DETACHED) >= sizeof (migpath))
1316ee519a1fSgjelinek 			return (Z_NOMEM);
13178cd327d5Sgjelinek 	}
1318ee519a1fSgjelinek 
1319ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1320ee519a1fSgjelinek 		return (err);
1321ee519a1fSgjelinek 
1322ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1323ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1324ee519a1fSgjelinek 
1325ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1326ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1327ee519a1fSgjelinek 
1328ee519a1fSgjelinek 	/*
13299acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
13309acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
13319acbbeafSnn35248 	 * assert() that the doc is valid.
1332ee519a1fSgjelinek 	 */
13339acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13349acbbeafSnn35248 	assert(valid != 0);
1335ee519a1fSgjelinek 
1336ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1337ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1338ee519a1fSgjelinek 
13398cd327d5Sgjelinek 	if (!(flags & ZONE_DRY_RUN))
1340ee519a1fSgjelinek 		(void) chmod(migpath, 0644);
1341ee519a1fSgjelinek 
1342ee519a1fSgjelinek 	stripcomments(handle);
1343ee519a1fSgjelinek 
1344ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1345ee519a1fSgjelinek 
1346ee519a1fSgjelinek 	return (Z_OK);
1347ee519a1fSgjelinek }
1348ee519a1fSgjelinek 
1349ee519a1fSgjelinek boolean_t
1350ee519a1fSgjelinek zonecfg_detached(const char *path)
1351ee519a1fSgjelinek {
1352ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1353ee519a1fSgjelinek 	struct stat	buf;
1354ee519a1fSgjelinek 
135516ab8c7bSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, ZONE_DETACHED) >=
1356ee519a1fSgjelinek 	    sizeof (migpath))
1357ee519a1fSgjelinek 		return (B_FALSE);
1358ee519a1fSgjelinek 
1359ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1360ee519a1fSgjelinek 		return (B_TRUE);
1361ee519a1fSgjelinek 
1362ee519a1fSgjelinek 	return (B_FALSE);
1363ee519a1fSgjelinek }
1364ee519a1fSgjelinek 
1365ee519a1fSgjelinek void
1366ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1367ee519a1fSgjelinek {
1368ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1369ee519a1fSgjelinek 	char path[MAXPATHLEN];
1370ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1371ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1372ee519a1fSgjelinek 
1373ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1374ee519a1fSgjelinek 		return;
1375ee519a1fSgjelinek 
1376ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1377ee519a1fSgjelinek 		return;
1378ee519a1fSgjelinek 
1379ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1380ee519a1fSgjelinek 		return;
1381ee519a1fSgjelinek 
138216ab8c7bSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path,
138316ab8c7bSgjelinek 	    ZONE_DETACHED);
1384ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1385ee519a1fSgjelinek 	    ATTACH_FORCED);
1386ee519a1fSgjelinek 
1387ee519a1fSgjelinek 	if (forced) {
1388ee519a1fSgjelinek 		(void) rename(detached, attached);
1389ee519a1fSgjelinek 	} else {
1390ee519a1fSgjelinek 		(void) unlink(attached);
1391ee519a1fSgjelinek 		(void) unlink(detached);
1392ee519a1fSgjelinek 	}
1393ee519a1fSgjelinek }
1394ee519a1fSgjelinek 
13957c478bd9Sstevel@tonic-gate /*
13967c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
13977c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
13987c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
13997c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
14007c478bd9Sstevel@tonic-gate  */
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate int
14037c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
14047c478bd9Sstevel@tonic-gate {
14057c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
14067c478bd9Sstevel@tonic-gate 
1407108322fbScarlsonj 	if (!config_file_path(zonename, path))
1408108322fbScarlsonj 		return (Z_INVAL);
14097c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
14107c478bd9Sstevel@tonic-gate 		return (Z_OK);
1411108322fbScarlsonj 	if (errno == ENOENT) {
1412108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1413108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1414108322fbScarlsonj 			return (Z_INVAL);
1415108322fbScarlsonj 		if (access(path, amode) == 0)
14167c478bd9Sstevel@tonic-gate 			return (Z_OK);
1417108322fbScarlsonj 	}
14187c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
14197c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
14207c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
14217c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14227c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate int
1426108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
14277c478bd9Sstevel@tonic-gate {
14287c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
14297c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
14307c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
14337c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
1436087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1437087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1438087719fdSdp 
14397c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
14407c478bd9Sstevel@tonic-gate 		goto out;
14417c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
14427c478bd9Sstevel@tonic-gate 		goto out;
14437c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
14447c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
14457c478bd9Sstevel@tonic-gate 		goto out;
14467c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
14477c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
14487c478bd9Sstevel@tonic-gate 		goto out;
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 	/*
14517c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
14527c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
14537c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
14547c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
14557c478bd9Sstevel@tonic-gate 	 * paths.
14567c478bd9Sstevel@tonic-gate 	 */
14577c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
14587c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
14597c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
14607c478bd9Sstevel@tonic-gate 			goto out;
14617c478bd9Sstevel@tonic-gate 	}
1462108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1463108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1464108322fbScarlsonj 		error = Z_MISC_FS;
1465108322fbScarlsonj 		goto out;
1466108322fbScarlsonj 	}
1467108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
14687c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
14697c478bd9Sstevel@tonic-gate 		goto out;
14707c478bd9Sstevel@tonic-gate 	}
14717c478bd9Sstevel@tonic-gate 
1472108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1473108322fbScarlsonj 		error = Z_MISC_FS;
1474108322fbScarlsonj 		goto out;
1475108322fbScarlsonj 	}
1476087719fdSdp 
1477087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1478087719fdSdp 	    "It is a snapshot of running zone state.\n");
1479087719fdSdp 
14807c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
14817c478bd9Sstevel@tonic-gate 
1482087719fdSdp 	stripcomments(handle);
1483087719fdSdp 
14847c478bd9Sstevel@tonic-gate out:
14857c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
14867c478bd9Sstevel@tonic-gate 	return (error);
14877c478bd9Sstevel@tonic-gate }
14887c478bd9Sstevel@tonic-gate 
1489f4b3ec61Sdh155122 int
1490f4b3ec61Sdh155122 zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep)
1491f4b3ec61Sdh155122 {
1492f4b3ec61Sdh155122 	char property[10]; /* 10 is big enough for "shared"/"exclusive" */
1493f4b3ec61Sdh155122 	int err;
1494f4b3ec61Sdh155122 
1495f4b3ec61Sdh155122 	err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property));
1496f4b3ec61Sdh155122 	if (err == Z_BAD_PROPERTY) {
1497f4b3ec61Sdh155122 		/* Return default value */
1498f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1499f4b3ec61Sdh155122 		return (Z_OK);
1500f4b3ec61Sdh155122 	} else if (err != Z_OK) {
1501f4b3ec61Sdh155122 		return (err);
1502f4b3ec61Sdh155122 	}
1503f4b3ec61Sdh155122 
1504f4b3ec61Sdh155122 	if (strlen(property) == 0 ||
1505f4b3ec61Sdh155122 	    strcmp(property, "shared") == 0)
1506f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1507f4b3ec61Sdh155122 	else if (strcmp(property, "exclusive") == 0)
1508f4b3ec61Sdh155122 		*iptypep = ZS_EXCLUSIVE;
1509f4b3ec61Sdh155122 	else
1510f4b3ec61Sdh155122 		return (Z_INVAL);
1511f4b3ec61Sdh155122 
1512f4b3ec61Sdh155122 	return (Z_OK);
1513f4b3ec61Sdh155122 }
1514f4b3ec61Sdh155122 
1515f4b3ec61Sdh155122 int
1516f4b3ec61Sdh155122 zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype)
1517f4b3ec61Sdh155122 {
1518f4b3ec61Sdh155122 	xmlNodePtr cur;
1519f4b3ec61Sdh155122 
1520f4b3ec61Sdh155122 	if (handle == NULL)
1521f4b3ec61Sdh155122 		return (Z_INVAL);
1522f4b3ec61Sdh155122 
1523f4b3ec61Sdh155122 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
1524f4b3ec61Sdh155122 	if (cur == NULL) {
1525f4b3ec61Sdh155122 		return (Z_EMPTY_DOCUMENT);
1526f4b3ec61Sdh155122 	}
1527f4b3ec61Sdh155122 
1528f4b3ec61Sdh155122 	if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) {
1529f4b3ec61Sdh155122 		return (Z_WRONG_DOC_TYPE);
1530f4b3ec61Sdh155122 	}
1531f4b3ec61Sdh155122 	switch (iptype) {
1532f4b3ec61Sdh155122 	case ZS_SHARED:
1533f4b3ec61Sdh155122 		/*
1534f4b3ec61Sdh155122 		 * Since "shared" is the default, we don't write it to the
1535f4b3ec61Sdh155122 		 * configuration file, so that it's easier to migrate those
1536f4b3ec61Sdh155122 		 * zones elsewhere, eg., to systems which are not IP-Instances
1537f4b3ec61Sdh155122 		 * aware.
1538f4b3ec61Sdh155122 		 * xmlUnsetProp only fails when the attribute doesn't exist,
1539f4b3ec61Sdh155122 		 * which we don't care.
1540f4b3ec61Sdh155122 		 */
1541f4b3ec61Sdh155122 		(void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE);
1542f4b3ec61Sdh155122 		break;
1543f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1544f4b3ec61Sdh155122 		if (xmlSetProp(cur, DTD_ATTR_IPTYPE,
1545f4b3ec61Sdh155122 		    (const xmlChar *) "exclusive") == NULL)
1546f4b3ec61Sdh155122 			return (Z_INVAL);
1547f4b3ec61Sdh155122 		break;
1548f4b3ec61Sdh155122 	}
1549f4b3ec61Sdh155122 	return (Z_OK);
1550f4b3ec61Sdh155122 }
1551f4b3ec61Sdh155122 
15527c478bd9Sstevel@tonic-gate static int
1553a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
15547c478bd9Sstevel@tonic-gate {
15557c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
15587c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
15597c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
15607c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
15617c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate 	return (Z_OK);
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate static int
15677c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
15687c478bd9Sstevel@tonic-gate {
15697c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
15707c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
15717c478bd9Sstevel@tonic-gate 	int err;
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1574a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
15757c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
15767c478bd9Sstevel@tonic-gate 		return (err);
15777c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1578a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
15797c478bd9Sstevel@tonic-gate 		return (err);
1580a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
15817c478bd9Sstevel@tonic-gate 		return (err);
1582a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
15837c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
15847c478bd9Sstevel@tonic-gate 		return (err);
15857c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
15867c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
15877c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
15887c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
15897c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1590a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
15917c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
15927c478bd9Sstevel@tonic-gate 				return (err);
15937c478bd9Sstevel@tonic-gate 		}
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 	return (Z_OK);
15967c478bd9Sstevel@tonic-gate }
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate int
15997c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
16007c478bd9Sstevel@tonic-gate {
16017c478bd9Sstevel@tonic-gate 	int err;
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16047c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16077c478bd9Sstevel@tonic-gate 		return (err);
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
16107c478bd9Sstevel@tonic-gate 		return (err);
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	return (Z_OK);
16137c478bd9Sstevel@tonic-gate }
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate static int
16167c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
16177c478bd9Sstevel@tonic-gate {
16187c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
16197c478bd9Sstevel@tonic-gate 	int err;
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1622a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16237c478bd9Sstevel@tonic-gate 		return (err);
16247c478bd9Sstevel@tonic-gate 	return (Z_OK);
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate int
16287c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
16297c478bd9Sstevel@tonic-gate {
16307c478bd9Sstevel@tonic-gate 	int err;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16337c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16367c478bd9Sstevel@tonic-gate 		return (err);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
16397c478bd9Sstevel@tonic-gate 		return (err);
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	return (Z_OK);
16427c478bd9Sstevel@tonic-gate }
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate int
16457c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
16467c478bd9Sstevel@tonic-gate {
16477c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16507c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
16517c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
16527c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
16537c478bd9Sstevel@tonic-gate 	if (new == NULL)
16547c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
16557c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
16567c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
16577c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
16587c478bd9Sstevel@tonic-gate 	if (last == NULL)
16597c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
16607c478bd9Sstevel@tonic-gate 	else
16617c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
16627c478bd9Sstevel@tonic-gate 	return (Z_OK);
16637c478bd9Sstevel@tonic-gate }
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate int
16667c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
16677c478bd9Sstevel@tonic-gate {
16687c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16717c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
16727c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
16737c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
16747c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
16757c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
16767c478bd9Sstevel@tonic-gate 			else
16777c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
16787c478bd9Sstevel@tonic-gate 			free(this);
16797c478bd9Sstevel@tonic-gate 			return (Z_OK);
16807c478bd9Sstevel@tonic-gate 		} else
16817c478bd9Sstevel@tonic-gate 			last = this;
16827c478bd9Sstevel@tonic-gate 	}
16837c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
16847c478bd9Sstevel@tonic-gate }
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate void
16877c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
16887c478bd9Sstevel@tonic-gate {
16897c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
16927c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
16937c478bd9Sstevel@tonic-gate 		free(this);
16947c478bd9Sstevel@tonic-gate 	}
16957c478bd9Sstevel@tonic-gate }
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate void
16987c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
16997c478bd9Sstevel@tonic-gate {
17007c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
17017c478bd9Sstevel@tonic-gate 		return;
17027c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
17037c478bd9Sstevel@tonic-gate 	free(valtab);
17047c478bd9Sstevel@tonic-gate }
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate static boolean_t
17077c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
17107c478bd9Sstevel@tonic-gate 	int prop_result;
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
17137c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
17147c478bd9Sstevel@tonic-gate 		return (B_FALSE);
17157c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
17167c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
17177c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
17187c478bd9Sstevel@tonic-gate }
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate static int
17217c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
17227c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
17237c478bd9Sstevel@tonic-gate {
17247c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17257c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17287c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
17297c478bd9Sstevel@tonic-gate 			continue;
17307c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
17317c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
17327c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
17337c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
17347c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
17357c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
17367c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
17377c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
17387c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17397c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17407c478bd9Sstevel@tonic-gate 			return (Z_OK);
17417c478bd9Sstevel@tonic-gate 		}
17427c478bd9Sstevel@tonic-gate 	}
17437c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
17447c478bd9Sstevel@tonic-gate }
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate int
17477c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
17487c478bd9Sstevel@tonic-gate {
17497c478bd9Sstevel@tonic-gate 	int err;
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17527c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17557c478bd9Sstevel@tonic-gate 		return (err);
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
17587c478bd9Sstevel@tonic-gate 		return (err);
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	return (Z_OK);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate int
17647c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
17657c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
17667c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
17677c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
17687c478bd9Sstevel@tonic-gate {
17697c478bd9Sstevel@tonic-gate 	int err;
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
17727c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17757c478bd9Sstevel@tonic-gate 		return (err);
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
17787c478bd9Sstevel@tonic-gate 		return (err);
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
17817c478bd9Sstevel@tonic-gate 		return (err);
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	return (Z_OK);
17847c478bd9Sstevel@tonic-gate }
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate static int
17877c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
17887c478bd9Sstevel@tonic-gate {
17897c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17927c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
17937c478bd9Sstevel@tonic-gate 			continue;
17947c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
17957c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17967c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17977c478bd9Sstevel@tonic-gate 			return (Z_OK);
17987c478bd9Sstevel@tonic-gate 		}
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
18017c478bd9Sstevel@tonic-gate }
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate int
18047c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
18057c478bd9Sstevel@tonic-gate {
18067c478bd9Sstevel@tonic-gate 	int err;
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18097c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18127c478bd9Sstevel@tonic-gate 		return (err);
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
18157c478bd9Sstevel@tonic-gate 		return (err);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	return (Z_OK);
18187c478bd9Sstevel@tonic-gate }
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate int
18217c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
18227c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
18237c478bd9Sstevel@tonic-gate {
18247c478bd9Sstevel@tonic-gate 	int err;
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18277c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18307c478bd9Sstevel@tonic-gate 		return (err);
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
18337c478bd9Sstevel@tonic-gate 		return (err);
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
18367c478bd9Sstevel@tonic-gate 		return (err);
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	return (Z_OK);
18397c478bd9Sstevel@tonic-gate }
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate int
18427c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
18437c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
18447c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
18457c478bd9Sstevel@tonic-gate {
18467c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
18477c478bd9Sstevel@tonic-gate 	int err;
18487c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
18497c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
18507c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18537c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18567c478bd9Sstevel@tonic-gate 		return (err);
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	/*
18597c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
18607c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
18617c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
18627c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
18637c478bd9Sstevel@tonic-gate 	 */
18647c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
18657c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
18667c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18677c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
18687c478bd9Sstevel@tonic-gate 			continue;
18697c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
18707c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
18717c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
18727c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
18737c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
18747c478bd9Sstevel@tonic-gate 					firstmatch = cur;
18757c478bd9Sstevel@tonic-gate 				else
18767c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
18777c478bd9Sstevel@tonic-gate 			}
18787c478bd9Sstevel@tonic-gate 		}
18797c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
18807c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
18817c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
18827c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
18837c478bd9Sstevel@tonic-gate 				    special) == 0) {
18847c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
18857c478bd9Sstevel@tonic-gate 						firstmatch = cur;
18867c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
18877c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
18887c478bd9Sstevel@tonic-gate 				} else {
18897c478bd9Sstevel@tonic-gate 					/*
18907c478bd9Sstevel@tonic-gate 					 * If another property matched but this
18917c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
18927c478bd9Sstevel@tonic-gate 					 */
18937c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
18947c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
18957c478bd9Sstevel@tonic-gate 				}
18967c478bd9Sstevel@tonic-gate 			}
18977c478bd9Sstevel@tonic-gate 		}
18987c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
18997c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
19007c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
19017c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
19027c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19037c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19047c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19057c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19067c478bd9Sstevel@tonic-gate 				} else {
19077c478bd9Sstevel@tonic-gate 					/*
19087c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19097c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19107c478bd9Sstevel@tonic-gate 					 */
19117c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19127c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19137c478bd9Sstevel@tonic-gate 				}
19147c478bd9Sstevel@tonic-gate 			}
19157c478bd9Sstevel@tonic-gate 		}
19167c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
19177c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
19187c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
19197c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
19207c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19217c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19227c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19237c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19247c478bd9Sstevel@tonic-gate 				} else {
19257c478bd9Sstevel@tonic-gate 					/*
19267c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19277c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19287c478bd9Sstevel@tonic-gate 					 */
19297c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19307c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19317c478bd9Sstevel@tonic-gate 				}
19327c478bd9Sstevel@tonic-gate 			}
19337c478bd9Sstevel@tonic-gate 		}
19347c478bd9Sstevel@tonic-gate 	}
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
19377c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	cur = firstmatch;
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
19427c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
19437c478bd9Sstevel@tonic-gate 		return (err);
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
19467c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
19477c478bd9Sstevel@tonic-gate 		return (err);
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
19507c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
19517c478bd9Sstevel@tonic-gate 		return (err);
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
19547c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
19557c478bd9Sstevel@tonic-gate 		return (err);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	/* options are optional */
19587c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
19597c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
19607c478bd9Sstevel@tonic-gate 	    options = options->next) {
19617c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
19627c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
19637c478bd9Sstevel@tonic-gate 			break;
19647c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
19657c478bd9Sstevel@tonic-gate 			break;
19667c478bd9Sstevel@tonic-gate 	}
19677c478bd9Sstevel@tonic-gate 	return (Z_OK);
19687c478bd9Sstevel@tonic-gate }
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate int
19717c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
19727c478bd9Sstevel@tonic-gate {
19737c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
19747c478bd9Sstevel@tonic-gate 	int err;
19757c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
19787c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19817c478bd9Sstevel@tonic-gate 		return (err);
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	/*
19847c478bd9Sstevel@tonic-gate 	 * General algorithm:
19857c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
19867c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
19877c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
19887c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
19897c478bd9Sstevel@tonic-gate 	 */
19907c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
19917c478bd9Sstevel@tonic-gate 	match = NULL;
19927c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
19937c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
19947c478bd9Sstevel@tonic-gate 			continue;
19957c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
19967c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
19977c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
19987c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
19997c478bd9Sstevel@tonic-gate 				if (match == NULL)
20007c478bd9Sstevel@tonic-gate 					match = cur;
20017c478bd9Sstevel@tonic-gate 				else
20027c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20037c478bd9Sstevel@tonic-gate 			}
20047c478bd9Sstevel@tonic-gate 		}
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	if (match == NULL)
20087c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	cur = match;
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
20137c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
20147c478bd9Sstevel@tonic-gate 		return (err);
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	return (Z_OK);
20177c478bd9Sstevel@tonic-gate }
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate /*
20207c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
20217c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
20227c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
20237c478bd9Sstevel@tonic-gate  */
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate boolean_t
20267c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
20277c478bd9Sstevel@tonic-gate {
20287c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
20297c478bd9Sstevel@tonic-gate 	int result;
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
20327c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	/*
20357c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
20367c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
20377c478bd9Sstevel@tonic-gate 	 */
20387c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
20397c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
20407c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
20417c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
20427c478bd9Sstevel@tonic-gate 		return (B_FALSE);
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	/*
20457c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
20467c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
20477c478bd9Sstevel@tonic-gate 	 * result of the comparison.
20487c478bd9Sstevel@tonic-gate 	 */
20497c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
20507c478bd9Sstevel@tonic-gate 	*slashp = '\0';
20517c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
20527c478bd9Sstevel@tonic-gate 	*slashp = '/';
20537c478bd9Sstevel@tonic-gate 	return ((result == 0));
20547c478bd9Sstevel@tonic-gate }
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate int
20577c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
20587c478bd9Sstevel@tonic-gate {
20597c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
20607c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
20617c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
20627c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
20657c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20667c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20677c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
20687c478bd9Sstevel@tonic-gate 		*slashp = '\0';
20697c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
20707c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
20717c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
20727c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
20737c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
20747c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
20757c478bd9Sstevel@tonic-gate 	} else {
20767c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
20777c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
20787c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
20797c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
20807c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
20817c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
2082a1be23daSdp 
20837c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
20847c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
20857c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
20867c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
2087a1be23daSdp 
20887c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
20897c478bd9Sstevel@tonic-gate 	}
20907c478bd9Sstevel@tonic-gate 	return (Z_OK);
20917c478bd9Sstevel@tonic-gate }
20927c478bd9Sstevel@tonic-gate 
2093f4b3ec61Sdh155122 boolean_t
2094f4b3ec61Sdh155122 zonecfg_ifname_exists(sa_family_t af, char *ifname)
2095f4b3ec61Sdh155122 {
2096f4b3ec61Sdh155122 	struct lifreq lifr;
2097f4b3ec61Sdh155122 	int so;
2098f4b3ec61Sdh155122 	int save_errno;
2099f4b3ec61Sdh155122 
2100f4b3ec61Sdh155122 	(void) memset(&lifr, 0, sizeof (lifr));
2101f4b3ec61Sdh155122 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2102f4b3ec61Sdh155122 	lifr.lifr_addr.ss_family = af;
2103f4b3ec61Sdh155122 	if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
2104f4b3ec61Sdh155122 		/* Odd - can't tell if the ifname exists */
2105f4b3ec61Sdh155122 		return (B_FALSE);
2106f4b3ec61Sdh155122 	}
2107f4b3ec61Sdh155122 	if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2108f4b3ec61Sdh155122 		save_errno = errno;
2109f4b3ec61Sdh155122 		(void) close(so);
2110f4b3ec61Sdh155122 		errno = save_errno;
2111f4b3ec61Sdh155122 		return (B_FALSE);
2112f4b3ec61Sdh155122 	}
2113f4b3ec61Sdh155122 	(void) close(so);
2114f4b3ec61Sdh155122 	return (B_TRUE);
2115f4b3ec61Sdh155122 }
2116f4b3ec61Sdh155122 
2117a8053275Sjv227347 /*
2118a8053275Sjv227347  * Determines if the physical interface and IP address specified by 'tabptr'
2119a8053275Sjv227347  * are in the zone document to which 'handle' refers.  'tabptr' must have an
2120a8053275Sjv227347  * interface or an address or both.  If it contains both, then Z_OK is
2121a8053275Sjv227347  * returned iff there is exactly one match.  If it contains an interface
2122a8053275Sjv227347  * or an address, but not both, then Z_OK is returned iff there is exactly
2123a8053275Sjv227347  * one entry with that interface or address.  If there are multiple entries
2124a8053275Sjv227347  * matching the query, then Z_INSUFFICIENT_SPEC is returned.  If there
2125a8053275Sjv227347  * are no matches, then Z_NO_RESOURCE_ID is returned.
2126a8053275Sjv227347  *
2127a8053275Sjv227347  * Errors might also be returned if the entry that exactly matches the
2128a8053275Sjv227347  * query lacks critical network resource information.
2129a8053275Sjv227347  *
2130a8053275Sjv227347  * If there is a single exact match, then the matching entry's physical
2131a8053275Sjv227347  * interface, IP address, and router information is stored in 'tabptr'.
2132a8053275Sjv227347  */
21337c478bd9Sstevel@tonic-gate int
21347c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21357c478bd9Sstevel@tonic-gate {
2136a8053275Sjv227347 	xmlNodePtr cur;
2137a8053275Sjv227347 	xmlNodePtr firstmatch;
2138a8053275Sjv227347 	boolean_t physfound;
21397c478bd9Sstevel@tonic-gate 	int err;
2140a8053275Sjv227347 	char address[INET6_ADDRSTRLEN];
2141a8053275Sjv227347 	char physical[LIFNAMSIZ];
2142a8053275Sjv227347 	size_t addrspec;		/* nonzero if tabptr has IP addr */
2143a8053275Sjv227347 	size_t physspec;		/* nonzero if tabptr has interface */
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21467c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21477c478bd9Sstevel@tonic-gate 
2148a8053275Sjv227347 	/*
2149a8053275Sjv227347 	 * zone_nwif_address and zone_nwif_physical are arrays, so no NULL
2150a8053275Sjv227347 	 * checks are necessary.
2151a8053275Sjv227347 	 */
2152a8053275Sjv227347 	addrspec = strlen(tabptr->zone_nwif_address);
2153a8053275Sjv227347 	physspec = strlen(tabptr->zone_nwif_physical);
2154a8053275Sjv227347 	assert(addrspec > 0 || physspec > 0);
2155a8053275Sjv227347 
21567c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21577c478bd9Sstevel@tonic-gate 		return (err);
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
2160a8053275Sjv227347 	cur = handle->zone_dh_cur;
21617c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
2162a8053275Sjv227347 		/* Skip non-net elements */
21637c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
21647c478bd9Sstevel@tonic-gate 			continue;
2165a8053275Sjv227347 
2166a8053275Sjv227347 		/*
2167a8053275Sjv227347 		 * If an interface is specified, then first check if the current
2168a8053275Sjv227347 		 * element's interface matches the query's interface.
2169a8053275Sjv227347 		 */
2170a8053275Sjv227347 		if (physspec > 0) {
2171a8053275Sjv227347 			physfound = B_FALSE;
21727c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
21737c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
21747c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
21757c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
2176a8053275Sjv227347 				if (addrspec == 0) {
21777c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
21787c478bd9Sstevel@tonic-gate 						firstmatch = cur;
21797c478bd9Sstevel@tonic-gate 					else
21807c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
21817c478bd9Sstevel@tonic-gate 				} else {
21827c478bd9Sstevel@tonic-gate 					/*
2183a8053275Sjv227347 					 * We're also matching based on IP
2184a8053275Sjv227347 					 * address, so we can't say that the
2185a8053275Sjv227347 					 * current element matches the query
2186a8053275Sjv227347 					 * yet.  Indicate that the interfaces
2187a8053275Sjv227347 					 * match.
21887c478bd9Sstevel@tonic-gate 					 */
2189a8053275Sjv227347 					physfound = B_TRUE;
2190a8053275Sjv227347 				}
2191a8053275Sjv227347 			}
2192a8053275Sjv227347 		}
2193a8053275Sjv227347 		if (addrspec > 0) {
2194a8053275Sjv227347 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
2195a8053275Sjv227347 			    sizeof (address)) == Z_OK) &&
2196a8053275Sjv227347 			    (zonecfg_same_net_address(
2197a8053275Sjv227347 			    tabptr->zone_nwif_address, address))) {
2198a8053275Sjv227347 				if (physspec == 0) {
2199a8053275Sjv227347 					/* We're only matching IP addresses. */
2200a8053275Sjv227347 					if (firstmatch == NULL)
2201a8053275Sjv227347 						firstmatch = cur;
2202a8053275Sjv227347 					else
2203a8053275Sjv227347 						return (Z_INSUFFICIENT_SPEC);
2204a8053275Sjv227347 				} else if (physfound) {
2205a8053275Sjv227347 					/*
2206a8053275Sjv227347 					 * Both the interfaces and the addresses
2207a8053275Sjv227347 					 * match.
2208a8053275Sjv227347 					 */
2209a8053275Sjv227347 					if (firstmatch == NULL)
2210a8053275Sjv227347 						firstmatch = cur;
2211a8053275Sjv227347 					else
2212a8053275Sjv227347 						return (Z_INSUFFICIENT_SPEC);
22137c478bd9Sstevel@tonic-gate 				}
22147c478bd9Sstevel@tonic-gate 			}
22157c478bd9Sstevel@tonic-gate 		}
22167c478bd9Sstevel@tonic-gate 	}
22177c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
22187c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	cur = firstmatch;
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
22237c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
22247c478bd9Sstevel@tonic-gate 		return (err);
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
22277c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
22287c478bd9Sstevel@tonic-gate 		return (err);
22297c478bd9Sstevel@tonic-gate 
2230de860bd9Sgfaden 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
2231de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter,
2232de860bd9Sgfaden 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK)
2233de860bd9Sgfaden 		return (err);
2234de860bd9Sgfaden 
22357c478bd9Sstevel@tonic-gate 	return (Z_OK);
22367c478bd9Sstevel@tonic-gate }
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate static int
22397c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22407c478bd9Sstevel@tonic-gate {
22417c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
22427c478bd9Sstevel@tonic-gate 	int err;
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
2245a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
22467c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
22477c478bd9Sstevel@tonic-gate 		return (err);
2248a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
22497c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
22507c478bd9Sstevel@tonic-gate 		return (err);
225144ce818bSjparcel 	/*
225244ce818bSjparcel 	 * Do not add this property when it is not set, for backwards
225344ce818bSjparcel 	 * compatibility and because it is optional.
225444ce818bSjparcel 	 */
225544ce818bSjparcel 	if ((strlen(tabptr->zone_nwif_defrouter) > 0) &&
225644ce818bSjparcel 	    ((err = newprop(newnode, DTD_ATTR_DEFROUTER,
225744ce818bSjparcel 	    tabptr->zone_nwif_defrouter)) != Z_OK))
2258de860bd9Sgfaden 		return (err);
22597c478bd9Sstevel@tonic-gate 	return (Z_OK);
22607c478bd9Sstevel@tonic-gate }
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate int
22637c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22647c478bd9Sstevel@tonic-gate {
22657c478bd9Sstevel@tonic-gate 	int err;
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22687c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22717c478bd9Sstevel@tonic-gate 		return (err);
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
22747c478bd9Sstevel@tonic-gate 		return (err);
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 	return (Z_OK);
22777c478bd9Sstevel@tonic-gate }
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate static int
22807c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22817c478bd9Sstevel@tonic-gate {
22827c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
22837c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
22867c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
22877c478bd9Sstevel@tonic-gate 			continue;
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
22907c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
22917c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
22927c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
22957c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
22967c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
22977c478bd9Sstevel@tonic-gate 			return (Z_OK);
22987c478bd9Sstevel@tonic-gate 		}
22997c478bd9Sstevel@tonic-gate 	}
23007c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
23017c478bd9Sstevel@tonic-gate }
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate int
23047c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
23057c478bd9Sstevel@tonic-gate {
23067c478bd9Sstevel@tonic-gate 	int err;
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23097c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23127c478bd9Sstevel@tonic-gate 		return (err);
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
23157c478bd9Sstevel@tonic-gate 		return (err);
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	return (Z_OK);
23187c478bd9Sstevel@tonic-gate }
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate int
23217c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
23227c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
23237c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
23247c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
23257c478bd9Sstevel@tonic-gate {
23267c478bd9Sstevel@tonic-gate 	int err;
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
23297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23327c478bd9Sstevel@tonic-gate 		return (err);
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
23357c478bd9Sstevel@tonic-gate 		return (err);
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
23387c478bd9Sstevel@tonic-gate 		return (err);
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	return (Z_OK);
23417c478bd9Sstevel@tonic-gate }
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate int
23447c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23457c478bd9Sstevel@tonic-gate {
23467c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
23477c478bd9Sstevel@tonic-gate 	int err;
23487c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23517c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23547c478bd9Sstevel@tonic-gate 		return (err);
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
23577c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
23587c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23597c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
23607c478bd9Sstevel@tonic-gate 			continue;
23617c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
23627c478bd9Sstevel@tonic-gate 			continue;
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
23657c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
23667c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
23677c478bd9Sstevel@tonic-gate 			    match) == 0) {
23687c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
23697c478bd9Sstevel@tonic-gate 					firstmatch = cur;
23707c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
23717c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
23727c478bd9Sstevel@tonic-gate 			} else {
23737c478bd9Sstevel@tonic-gate 				/*
23747c478bd9Sstevel@tonic-gate 				 * If another property matched but this
23757c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
23767c478bd9Sstevel@tonic-gate 				 */
23777c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
23787c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
23797c478bd9Sstevel@tonic-gate 			}
23807c478bd9Sstevel@tonic-gate 		}
23817c478bd9Sstevel@tonic-gate 	}
23827c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
23837c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	cur = firstmatch;
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
23887c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
23897c478bd9Sstevel@tonic-gate 		return (err);
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	return (Z_OK);
23927c478bd9Sstevel@tonic-gate }
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate static int
23957c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
23967c478bd9Sstevel@tonic-gate {
23977c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
23987c478bd9Sstevel@tonic-gate 	int err;
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
24017c478bd9Sstevel@tonic-gate 
2402a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
24037c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
24047c478bd9Sstevel@tonic-gate 		return (err);
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	return (Z_OK);
24077c478bd9Sstevel@tonic-gate }
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate int
24107c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
24117c478bd9Sstevel@tonic-gate {
24127c478bd9Sstevel@tonic-gate 	int err;
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
24157c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24187c478bd9Sstevel@tonic-gate 		return (err);
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
24217c478bd9Sstevel@tonic-gate 		return (err);
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	return (Z_OK);
24247c478bd9Sstevel@tonic-gate }
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate static int
24277c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
24287c478bd9Sstevel@tonic-gate {
2429facf4a8dSllai1 	xmlNodePtr cur = handle->zone_dh_cur;
24307c478bd9Sstevel@tonic-gate 	int match_match;
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
24337c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
24347c478bd9Sstevel@tonic-gate 			continue;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
24377c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
24387c478bd9Sstevel@tonic-gate 
24397c478bd9Sstevel@tonic-gate 		if (match_match) {
24407c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
24417c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
24427c478bd9Sstevel@tonic-gate 			return (Z_OK);
24437c478bd9Sstevel@tonic-gate 		}
24447c478bd9Sstevel@tonic-gate 	}
24457c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
24467c478bd9Sstevel@tonic-gate }
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate int
24497c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
24507c478bd9Sstevel@tonic-gate {
24517c478bd9Sstevel@tonic-gate 	int err;
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
24547c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24577c478bd9Sstevel@tonic-gate 		return (err);
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
24607c478bd9Sstevel@tonic-gate 		return (err);
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	return (Z_OK);
24637c478bd9Sstevel@tonic-gate }
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate int
24667c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
24677c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
24687c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
24697c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
24707c478bd9Sstevel@tonic-gate {
24717c478bd9Sstevel@tonic-gate 	int err;
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
24747c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24777c478bd9Sstevel@tonic-gate 		return (err);
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
24807c478bd9Sstevel@tonic-gate 		return (err);
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
24837c478bd9Sstevel@tonic-gate 		return (err);
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate 	return (Z_OK);
24867c478bd9Sstevel@tonic-gate }
24877c478bd9Sstevel@tonic-gate 
2488ff17c8bfSgjelinek /* Lock to serialize all devwalks */
2489ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2490ee519a1fSgjelinek /*
2491ff17c8bfSgjelinek  * Global variables used to pass data from zonecfg_dev_manifest to the nftw
2492ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2493ff17c8bfSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from
2494ff17c8bfSgjelinek  * zonecfg_dev_manifest.
2495ee519a1fSgjelinek  */
2496ff17c8bfSgjelinek typedef struct __g_devwalk_data *g_devwalk_data_t;
2497ff17c8bfSgjelinek static g_devwalk_data_t g_devwalk_data;
2498ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2499ee519a1fSgjelinek     void *);
2500ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2501ee519a1fSgjelinek 
2502ee519a1fSgjelinek /*
2503ff17c8bfSgjelinek  * zonecfg_dev_manifest call-back function used during detach to generate the
2504ff17c8bfSgjelinek  * dev info in the manifest.
2505ff17c8bfSgjelinek  */
2506ff17c8bfSgjelinek static int
2507ff17c8bfSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
2508ff17c8bfSgjelinek     const char *acl, void *hdl)
2509ff17c8bfSgjelinek {
2510ff17c8bfSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
2511ff17c8bfSgjelinek 	xmlNodePtr newnode;
2512ff17c8bfSgjelinek 	xmlNodePtr cur;
2513ff17c8bfSgjelinek 	int err;
2514ff17c8bfSgjelinek 	char buf[128];
2515ff17c8bfSgjelinek 
2516ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
2517ff17c8bfSgjelinek 		return (err);
2518ff17c8bfSgjelinek 
2519ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
2520ff17c8bfSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
2521ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
2522ff17c8bfSgjelinek 		return (err);
2523ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
2524ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
2525ff17c8bfSgjelinek 		return (err);
2526ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
2527ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
2528ff17c8bfSgjelinek 		return (err);
2529ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
2530ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
2531ff17c8bfSgjelinek 		return (err);
2532ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
2533ff17c8bfSgjelinek 		return (err);
2534ff17c8bfSgjelinek 	return (Z_OK);
2535ff17c8bfSgjelinek }
2536ff17c8bfSgjelinek 
2537ff17c8bfSgjelinek /*
2538ff17c8bfSgjelinek  * This is the nftw call-back function used by zonecfg_dev_manifest.  It is
2539ff17c8bfSgjelinek  * responsible for calling the actual call-back.
2540ee519a1fSgjelinek  */
2541ee519a1fSgjelinek /* ARGSUSED2 */
2542ee519a1fSgjelinek static int
2543ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2544ee519a1fSgjelinek     struct FTW *ftw)
2545ee519a1fSgjelinek {
2546ee519a1fSgjelinek 	acl_t *acl;
2547ee519a1fSgjelinek 	char *acl_txt = NULL;
2548ee519a1fSgjelinek 
2549ee519a1fSgjelinek 	/* skip all but character and block devices */
2550ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2551ee519a1fSgjelinek 		return (0);
2552ee519a1fSgjelinek 
2553ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2554ee519a1fSgjelinek 	    acl != NULL) {
2555ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2556ee519a1fSgjelinek 		acl_free(acl);
2557ee519a1fSgjelinek 	}
2558ee519a1fSgjelinek 
2559ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2560ee519a1fSgjelinek 		return (0);
2561ee519a1fSgjelinek 
2562ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2563ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2564ee519a1fSgjelinek 	    g_devwalk_data);
2565ee519a1fSgjelinek 	free(acl_txt);
2566ee519a1fSgjelinek 	return (0);
2567ee519a1fSgjelinek }
2568ee519a1fSgjelinek 
2569ee519a1fSgjelinek /*
2570ff17c8bfSgjelinek  * Walk the dev tree for the zone specified by hdl and call the
2571ff17c8bfSgjelinek  * get_detach_dev_entry call-back function for each entry in the tree.  The
2572ff17c8bfSgjelinek  * call-back will be passed the name, uid, gid, mode, acl string and the
2573ff17c8bfSgjelinek  * handle input parameter for each dev entry.
2574ee519a1fSgjelinek  *
2575ff17c8bfSgjelinek  * Data is passed to get_detach_dev_entry through the global variables
2576ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2577ff17c8bfSgjelinek  * zonecfg_devwalk_cb function will actually call get_detach_dev_entry.
2578ee519a1fSgjelinek  */
2579ee519a1fSgjelinek int
2580ff17c8bfSgjelinek zonecfg_dev_manifest(zone_dochandle_t hdl)
2581ee519a1fSgjelinek {
2582ee519a1fSgjelinek 	char path[MAXPATHLEN];
2583ee519a1fSgjelinek 	int ret;
2584ee519a1fSgjelinek 
2585ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2586ee519a1fSgjelinek 		return (ret);
2587ee519a1fSgjelinek 
2588ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2589ee519a1fSgjelinek 		return (Z_TOO_BIG);
2590ee519a1fSgjelinek 
2591ee519a1fSgjelinek 	/*
2592ff17c8bfSgjelinek 	 * We have to serialize all devwalks in the same process
2593ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2594ee519a1fSgjelinek 	 */
2595ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2596ee519a1fSgjelinek 
2597ff17c8bfSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2598ff17c8bfSgjelinek 	g_devwalk_data = (g_devwalk_data_t)hdl;
2599ff17c8bfSgjelinek 	g_devwalk_cb = get_detach_dev_entry;
2600ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2601ee519a1fSgjelinek 
2602ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2603ee519a1fSgjelinek 	return (Z_OK);
2604ee519a1fSgjelinek }
2605ee519a1fSgjelinek 
2606ee519a1fSgjelinek /*
2607ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2608ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2609ee519a1fSgjelinek  * attaching a migrated zone.
2610ee519a1fSgjelinek  */
2611ee519a1fSgjelinek int
2612ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2613ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2614ee519a1fSgjelinek {
2615ee519a1fSgjelinek 	int ret;
2616ee519a1fSgjelinek 	char path[MAXPATHLEN];
2617ee519a1fSgjelinek 	struct stat st;
2618ee519a1fSgjelinek 	acl_t *aclp;
2619ee519a1fSgjelinek 
2620ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2621ee519a1fSgjelinek 		return (ret);
2622ee519a1fSgjelinek 
2623ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2624ee519a1fSgjelinek 		return (Z_TOO_BIG);
2625ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2626ee519a1fSgjelinek 		return (Z_TOO_BIG);
2627ee519a1fSgjelinek 
2628ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2629ee519a1fSgjelinek 		return (Z_INVAL);
2630ee519a1fSgjelinek 
2631ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2632ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2633ee519a1fSgjelinek 		return (Z_INVAL);
2634ee519a1fSgjelinek 
2635ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2636ee519a1fSgjelinek 		return (Z_SYSTEM);
2637ee519a1fSgjelinek 
2638ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2639ee519a1fSgjelinek 		return (Z_SYSTEM);
2640ee519a1fSgjelinek 
2641ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2642ee519a1fSgjelinek 		return (Z_OK);
2643ee519a1fSgjelinek 
2644*2ad45a84Sjv227347 	if (acl_fromtext(acltxt, &aclp) != 0) {
2645*2ad45a84Sjv227347 		errno = EINVAL;
2646ee519a1fSgjelinek 		return (Z_SYSTEM);
2647*2ad45a84Sjv227347 	}
2648ee519a1fSgjelinek 
2649ee519a1fSgjelinek 	errno = 0;
2650ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2651ee519a1fSgjelinek 		free(aclp);
2652ee519a1fSgjelinek 		return (Z_SYSTEM);
2653ee519a1fSgjelinek 	}
2654ee519a1fSgjelinek 
2655ee519a1fSgjelinek 	free(aclp);
2656ee519a1fSgjelinek 	return (Z_OK);
2657ee519a1fSgjelinek }
2658ee519a1fSgjelinek 
26597c478bd9Sstevel@tonic-gate /*
26607c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
26617c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
26627c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
26637c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
26647c478bd9Sstevel@tonic-gate  *
26657c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
26667c478bd9Sstevel@tonic-gate  * aborts with an error.
26677c478bd9Sstevel@tonic-gate  */
26687c478bd9Sstevel@tonic-gate int
26697c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
26707c478bd9Sstevel@tonic-gate     void *priv) {
26717c478bd9Sstevel@tonic-gate 	FILE *mnttab;
26727c478bd9Sstevel@tonic-gate 	struct mnttab m;
26737c478bd9Sstevel@tonic-gate 	size_t l;
267407b574eeSgjelinek 	int zfsl;
26757c478bd9Sstevel@tonic-gate 	int rv = 0;
267607b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
26797c478bd9Sstevel@tonic-gate 
268007b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
268107b574eeSgjelinek 	    >= sizeof (zfs_path))
268207b574eeSgjelinek 		return (-1);
268307b574eeSgjelinek 
26847c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
26857c478bd9Sstevel@tonic-gate 
26867c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
26897c478bd9Sstevel@tonic-gate 		return (-1);
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
26927c478bd9Sstevel@tonic-gate 		rv = -1;
26937c478bd9Sstevel@tonic-gate 		goto out;
26947c478bd9Sstevel@tonic-gate 	}
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
26977c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
269807b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
269907b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
27007c478bd9Sstevel@tonic-gate 			rv++;
27017c478bd9Sstevel@tonic-gate 			if (callback == NULL)
27027c478bd9Sstevel@tonic-gate 				continue;
27037c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
27047c478bd9Sstevel@tonic-gate 				rv = -1;
27057c478bd9Sstevel@tonic-gate 				goto out;
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 			}
27087c478bd9Sstevel@tonic-gate 		}
27097c478bd9Sstevel@tonic-gate 	}
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate out:
27127c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
27137c478bd9Sstevel@tonic-gate 	return (rv);
27147c478bd9Sstevel@tonic-gate }
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate int
27177c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27187c478bd9Sstevel@tonic-gate {
27197c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
27207c478bd9Sstevel@tonic-gate 	int err;
27217c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
27247c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27277c478bd9Sstevel@tonic-gate 		return (err);
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
27307c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
27317c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
27327c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
27337c478bd9Sstevel@tonic-gate 			continue;
27347c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
27357c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
27367c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
27377c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
27387c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
27397c478bd9Sstevel@tonic-gate 					firstmatch = cur;
27407c478bd9Sstevel@tonic-gate 				else
27417c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
27427c478bd9Sstevel@tonic-gate 			}
27437c478bd9Sstevel@tonic-gate 		}
27447c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
27457c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
27467c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
27477c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
27487c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
27497c478bd9Sstevel@tonic-gate 						firstmatch = cur;
27507c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
27517c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
27527c478bd9Sstevel@tonic-gate 				} else {
27537c478bd9Sstevel@tonic-gate 					/*
27547c478bd9Sstevel@tonic-gate 					 * If another property matched but this
27557c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
27567c478bd9Sstevel@tonic-gate 					 */
27577c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
27587c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
27597c478bd9Sstevel@tonic-gate 				}
27607c478bd9Sstevel@tonic-gate 			}
27617c478bd9Sstevel@tonic-gate 		}
27627c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
27637c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
27647c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
27657c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
27667c478bd9Sstevel@tonic-gate 				    0) {
27677c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
27687c478bd9Sstevel@tonic-gate 						firstmatch = cur;
27697c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
27707c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
27717c478bd9Sstevel@tonic-gate 				} else {
27727c478bd9Sstevel@tonic-gate 					/*
27737c478bd9Sstevel@tonic-gate 					 * If another property matched but this
27747c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
27757c478bd9Sstevel@tonic-gate 					 */
27767c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
27777c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
27787c478bd9Sstevel@tonic-gate 				}
27797c478bd9Sstevel@tonic-gate 			}
27807c478bd9Sstevel@tonic-gate 		}
27817c478bd9Sstevel@tonic-gate 	}
27827c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
27837c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 	cur = firstmatch;
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
27887c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
27897c478bd9Sstevel@tonic-gate 		return (err);
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
27927c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
27937c478bd9Sstevel@tonic-gate 		return (err);
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
27967c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
27977c478bd9Sstevel@tonic-gate 		return (err);
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	return (Z_OK);
28007c478bd9Sstevel@tonic-gate }
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate static int
28037c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
28047c478bd9Sstevel@tonic-gate {
28057c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
28067c478bd9Sstevel@tonic-gate 	int err;
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2809a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
28107c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
28117c478bd9Sstevel@tonic-gate 		return (err);
2812a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
28137c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
28147c478bd9Sstevel@tonic-gate 		return (err);
2815a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
28167c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
28177c478bd9Sstevel@tonic-gate 		return (err);
28187c478bd9Sstevel@tonic-gate 	return (Z_OK);
28197c478bd9Sstevel@tonic-gate }
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate int
28227c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
28237c478bd9Sstevel@tonic-gate {
28247c478bd9Sstevel@tonic-gate 	int err;
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
28277c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28307c478bd9Sstevel@tonic-gate 		return (err);
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
28337c478bd9Sstevel@tonic-gate 		return (err);
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	return (Z_OK);
28367c478bd9Sstevel@tonic-gate }
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate static int
28397c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
28407c478bd9Sstevel@tonic-gate {
28417c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
28427c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
28457c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
28467c478bd9Sstevel@tonic-gate 			continue;
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
28497c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
28507c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
28517c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
28527c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
28537c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
28567c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
28577c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
28587c478bd9Sstevel@tonic-gate 			return (Z_OK);
28597c478bd9Sstevel@tonic-gate 		}
28607c478bd9Sstevel@tonic-gate 	}
28617c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
28627c478bd9Sstevel@tonic-gate }
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate int
28657c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
28667c478bd9Sstevel@tonic-gate {
28677c478bd9Sstevel@tonic-gate 	int err;
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
28707c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28737c478bd9Sstevel@tonic-gate 		return (err);
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
28767c478bd9Sstevel@tonic-gate 		return (err);
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	return (Z_OK);
28797c478bd9Sstevel@tonic-gate }
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate int
28827c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
28837c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
28847c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
28857c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
28867c478bd9Sstevel@tonic-gate {
28877c478bd9Sstevel@tonic-gate 	int err;
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
28907c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28937c478bd9Sstevel@tonic-gate 		return (err);
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
28967c478bd9Sstevel@tonic-gate 		return (err);
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
28997c478bd9Sstevel@tonic-gate 		return (err);
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 	return (Z_OK);
29027c478bd9Sstevel@tonic-gate }
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate int
29057c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
29067c478bd9Sstevel@tonic-gate {
29077c478bd9Sstevel@tonic-gate 	if (attr == NULL)
29087c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
29117c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
29147c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
29157c478bd9Sstevel@tonic-gate 		return (Z_OK);
29167c478bd9Sstevel@tonic-gate 	}
29177c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
29187c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
29197c478bd9Sstevel@tonic-gate 		return (Z_OK);
29207c478bd9Sstevel@tonic-gate 	}
29217c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
29227c478bd9Sstevel@tonic-gate }
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate int
29257c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
29267c478bd9Sstevel@tonic-gate {
29277c478bd9Sstevel@tonic-gate 	long long result;
29287c478bd9Sstevel@tonic-gate 	char *endptr;
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	if (attr == NULL)
29317c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
29347c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29357c478bd9Sstevel@tonic-gate 
29367c478bd9Sstevel@tonic-gate 	errno = 0;
29377c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
29387c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
29397c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29407c478bd9Sstevel@tonic-gate 	*value = result;
29417c478bd9Sstevel@tonic-gate 	return (Z_OK);
29427c478bd9Sstevel@tonic-gate }
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate int
29457c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
29467c478bd9Sstevel@tonic-gate     size_t val_sz)
29477c478bd9Sstevel@tonic-gate {
29487c478bd9Sstevel@tonic-gate 	if (attr == NULL)
29497c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
29527c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29537c478bd9Sstevel@tonic-gate 
29547c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
29557c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
29567c478bd9Sstevel@tonic-gate 	return (Z_OK);
29577c478bd9Sstevel@tonic-gate }
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate int
29607c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
29617c478bd9Sstevel@tonic-gate {
29627c478bd9Sstevel@tonic-gate 	unsigned long long result;
29637c478bd9Sstevel@tonic-gate 	long long neg_result;
29647c478bd9Sstevel@tonic-gate 	char *endptr;
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 	if (attr == NULL)
29677c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29687c478bd9Sstevel@tonic-gate 
29697c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
29707c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 	errno = 0;
29737c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
29747c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
29757c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29767c478bd9Sstevel@tonic-gate 	errno = 0;
29777c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
29787c478bd9Sstevel@tonic-gate 	/*
29797c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
29807c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
29817c478bd9Sstevel@tonic-gate 	 * need to look for this here.
29827c478bd9Sstevel@tonic-gate 	 */
29837c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
29847c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29857c478bd9Sstevel@tonic-gate 	*value = result;
29867c478bd9Sstevel@tonic-gate 	return (Z_OK);
29877c478bd9Sstevel@tonic-gate }
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate int
29907c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29917c478bd9Sstevel@tonic-gate {
29927c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
29937c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
29947c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29957c478bd9Sstevel@tonic-gate 	int err;
29967c478bd9Sstevel@tonic-gate 
2997*2ad45a84Sjv227347 	if (strlen(tabptr->zone_rctl_name) == 0)
29987c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30017c478bd9Sstevel@tonic-gate 		return (err);
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
30047c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
30057c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
30067c478bd9Sstevel@tonic-gate 			continue;
30077c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
30087c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
30097c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
30107c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
30117c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
30127c478bd9Sstevel@tonic-gate 			    val = val->next) {
30137c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
30147c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
30157c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
30167c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
30177c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
30187c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
30197c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
30207c478bd9Sstevel@tonic-gate 				    Z_OK))
30217c478bd9Sstevel@tonic-gate 					break;
30227c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
30237c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
30247c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
30257c478bd9Sstevel@tonic-gate 				    Z_OK))
30267c478bd9Sstevel@tonic-gate 					break;
30277c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
30287c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
30297c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
30307c478bd9Sstevel@tonic-gate 				    Z_OK))
30317c478bd9Sstevel@tonic-gate 					break;
30327c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
30337c478bd9Sstevel@tonic-gate 				    Z_OK)
30347c478bd9Sstevel@tonic-gate 					break;
30357c478bd9Sstevel@tonic-gate 			}
30367c478bd9Sstevel@tonic-gate 			return (Z_OK);
30377c478bd9Sstevel@tonic-gate 		}
30387c478bd9Sstevel@tonic-gate 	}
30397c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
30407c478bd9Sstevel@tonic-gate }
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate static int
30437c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30447c478bd9Sstevel@tonic-gate {
30457c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
30467c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
30477c478bd9Sstevel@tonic-gate 	int err;
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
3050a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
30517c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
30527c478bd9Sstevel@tonic-gate 		return (err);
30537c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
30547c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
30557c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
30567c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
3057a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
30587c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
30597c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
30607c478bd9Sstevel@tonic-gate 			return (err);
3061a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
30627c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
30637c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
30647c478bd9Sstevel@tonic-gate 			return (err);
3065a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
30667c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
30677c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
30687c478bd9Sstevel@tonic-gate 			return (err);
30697c478bd9Sstevel@tonic-gate 	}
30707c478bd9Sstevel@tonic-gate 	return (Z_OK);
30717c478bd9Sstevel@tonic-gate }
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate int
30747c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30757c478bd9Sstevel@tonic-gate {
30767c478bd9Sstevel@tonic-gate 	int err;
30777c478bd9Sstevel@tonic-gate 
3078*2ad45a84Sjv227347 	if (tabptr == NULL)
30797c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30827c478bd9Sstevel@tonic-gate 		return (err);
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
30857c478bd9Sstevel@tonic-gate 		return (err);
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	return (Z_OK);
30887c478bd9Sstevel@tonic-gate }
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate static int
30917c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30927c478bd9Sstevel@tonic-gate {
30937c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
30947c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
30957c478bd9Sstevel@tonic-gate 	int name_result;
30967c478bd9Sstevel@tonic-gate 
30977c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
30987c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
30997c478bd9Sstevel@tonic-gate 			continue;
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
31027c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
31037c478bd9Sstevel@tonic-gate 			continue;
31047c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
31057c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
31067c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
31097c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
31107c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
31117c478bd9Sstevel@tonic-gate 			return (Z_OK);
31127c478bd9Sstevel@tonic-gate 		}
31137c478bd9Sstevel@tonic-gate 	}
31147c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
31157c478bd9Sstevel@tonic-gate }
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate int
31187c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
31197c478bd9Sstevel@tonic-gate {
31207c478bd9Sstevel@tonic-gate 	int err;
31217c478bd9Sstevel@tonic-gate 
3122*2ad45a84Sjv227347 	if (tabptr == NULL)
31237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
31267c478bd9Sstevel@tonic-gate 		return (err);
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
31297c478bd9Sstevel@tonic-gate 		return (err);
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 	return (Z_OK);
31327c478bd9Sstevel@tonic-gate }
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate int
31357c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
31367c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
31377c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
31387c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
31397c478bd9Sstevel@tonic-gate {
31407c478bd9Sstevel@tonic-gate 	int err;
31417c478bd9Sstevel@tonic-gate 
3142*2ad45a84Sjv227347 	if (oldtabptr == NULL || newtabptr == NULL)
31437c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
31467c478bd9Sstevel@tonic-gate 		return (err);
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
31497c478bd9Sstevel@tonic-gate 		return (err);
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
31527c478bd9Sstevel@tonic-gate 		return (err);
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 	return (Z_OK);
31557c478bd9Sstevel@tonic-gate }
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate int
31587c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
31597c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
31607c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
31617c478bd9Sstevel@tonic-gate {
31627c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
31637c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
31667c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
31677c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
31687c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
31697c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
31707c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
31717c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31727c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
31737c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31747c478bd9Sstevel@tonic-gate 	if (last == NULL)
31757c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
31767c478bd9Sstevel@tonic-gate 	else
31777c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
31787c478bd9Sstevel@tonic-gate 	return (Z_OK);
31797c478bd9Sstevel@tonic-gate }
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate int
31827c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
31837c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
31847c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
31857c478bd9Sstevel@tonic-gate {
31867c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
31897c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
31907c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
31917c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
31927c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
31937c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
31947c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
31957c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
31967c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
31977c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
31987c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
31997c478bd9Sstevel@tonic-gate 			else
32007c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
32017c478bd9Sstevel@tonic-gate 			free(this);
32027c478bd9Sstevel@tonic-gate 			return (Z_OK);
32037c478bd9Sstevel@tonic-gate 		} else
32047c478bd9Sstevel@tonic-gate 			last = this;
32057c478bd9Sstevel@tonic-gate 	}
32067c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
32077c478bd9Sstevel@tonic-gate }
32087c478bd9Sstevel@tonic-gate 
3209ff17c8bfSgjelinek void
3210ff17c8bfSgjelinek zonecfg_set_swinv(zone_dochandle_t handle)
3211ff17c8bfSgjelinek {
3212ff17c8bfSgjelinek 	handle->zone_dh_sw_inv = B_TRUE;
3213ff17c8bfSgjelinek }
3214ff17c8bfSgjelinek 
3215ff17c8bfSgjelinek /*
3216ff17c8bfSgjelinek  * Add the pkg to the sw inventory on the handle.
3217ff17c8bfSgjelinek  */
3218ff17c8bfSgjelinek int
3219ff17c8bfSgjelinek zonecfg_add_pkg(zone_dochandle_t handle, char *name, char *version)
3220ff17c8bfSgjelinek {
3221ff17c8bfSgjelinek 	xmlNodePtr newnode;
3222ff17c8bfSgjelinek 	xmlNodePtr cur;
3223ff17c8bfSgjelinek 	int err;
3224ff17c8bfSgjelinek 
3225ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
3226ff17c8bfSgjelinek 		return (err);
3227ff17c8bfSgjelinek 
3228ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
3229ff17c8bfSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
3230ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
3231ff17c8bfSgjelinek 		return (err);
3232ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
3233ff17c8bfSgjelinek 		return (err);
3234ff17c8bfSgjelinek 	return (Z_OK);
3235ff17c8bfSgjelinek }
3236ff17c8bfSgjelinek 
3237ff17c8bfSgjelinek int
3238ff17c8bfSgjelinek zonecfg_add_patch(zone_dochandle_t handle, char *id, void **pnode)
3239ff17c8bfSgjelinek {
3240ff17c8bfSgjelinek 	xmlNodePtr node = (xmlNodePtr)*pnode;
3241ff17c8bfSgjelinek 	xmlNodePtr cur;
3242ff17c8bfSgjelinek 	int err;
3243ff17c8bfSgjelinek 
3244ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
3245ff17c8bfSgjelinek 		return (err);
3246ff17c8bfSgjelinek 
3247ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
3248ff17c8bfSgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
3249ff17c8bfSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
3250ff17c8bfSgjelinek 		return (err);
3251ff17c8bfSgjelinek 	*pnode = (void *)node;
3252ff17c8bfSgjelinek 	return (Z_OK);
3253ff17c8bfSgjelinek }
3254ff17c8bfSgjelinek 
3255ff17c8bfSgjelinek int
3256ff17c8bfSgjelinek zonecfg_add_patch_obs(char *id, void *cur)
3257ff17c8bfSgjelinek {
3258ff17c8bfSgjelinek 	xmlNodePtr	node;
3259ff17c8bfSgjelinek 	int err;
3260ff17c8bfSgjelinek 
3261ff17c8bfSgjelinek 	node = xmlNewTextChild((xmlNodePtr)cur, NULL, DTD_ELEM_OBSOLETES, NULL);
3262ff17c8bfSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
3263ff17c8bfSgjelinek 		return (err);
3264ff17c8bfSgjelinek 	return (Z_OK);
3265ff17c8bfSgjelinek }
3266ff17c8bfSgjelinek 
32677c478bd9Sstevel@tonic-gate char *
32687c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
32697c478bd9Sstevel@tonic-gate {
32707c478bd9Sstevel@tonic-gate 	switch (errnum) {
32717c478bd9Sstevel@tonic-gate 	case Z_OK:
32727c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
32737c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
32747c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
32757c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
32767c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
32777c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
32787c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
32797c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
32807c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32817c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
32827c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
32837c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
32847c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
32857c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
32867c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
32877c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
32887c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
32897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
32907c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
32917c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
32927c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
32937c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
32947c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
32957c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
32967c478bd9Sstevel@tonic-gate 	case Z_INVAL:
32977c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
32987c478bd9Sstevel@tonic-gate 	case Z_ACCES:
32997c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
33007c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
33017c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
33027c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
33037c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
33047c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
33057c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
33067c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
33077c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
33087c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
33097c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
33107c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
33117c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
33127c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
33137c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
33147c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3315087719fdSdp 	case Z_BAD_ZONE_STATE:
33167c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
3317087719fdSdp 		    "Zone state is invalid for the requested operation"));
33187c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
33197c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3320087719fdSdp 	case Z_NAME_IN_USE:
3321087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
33227c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
33237c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
33247c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
33257c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
33267c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
33277c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
33287c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
33297c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
33307c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
33317c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
33327c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
33337c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
33347c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
33357c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
33367c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
33377c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
33387c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
33397c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3340ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3341ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3342ffbafc53Scomay 		    "Specified privilege is prohibited"));
3343ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3344ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3345ffbafc53Scomay 		    "Required privilege is missing"));
3346ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3347ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3348ffbafc53Scomay 		    "Specified privilege is unknown"));
33499acbbeafSnn35248 	case Z_BRAND_ERROR:
33509acbbeafSnn35248 		return (dgettext(TEXT_DOMAIN,
33519acbbeafSnn35248 		    "Brand-specific error"));
33520209230bSgjelinek 	case Z_INCOMPATIBLE:
33530209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Incompatible settings"));
33540209230bSgjelinek 	case Z_ALIAS_DISALLOW:
33550209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
33560209230bSgjelinek 		    "An incompatible rctl already exists for this property"));
33570209230bSgjelinek 	case Z_CLEAR_DISALLOW:
33580209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
33590209230bSgjelinek 		    "Clearing this property is not allowed"));
33600209230bSgjelinek 	case Z_POOL:
33610209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error"));
33620209230bSgjelinek 	case Z_POOLS_NOT_ACTIVE:
33630209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Pools facility not active; "
33640209230bSgjelinek 		    "zone will not be bound to pool"));
33650209230bSgjelinek 	case Z_POOL_ENABLE:
33660209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
33670209230bSgjelinek 		    "Could not enable pools facility"));
33680209230bSgjelinek 	case Z_NO_POOL:
33690209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
33700209230bSgjelinek 		    "Pool not found; using default pool"));
33710209230bSgjelinek 	case Z_POOL_CREATE:
33720209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
33730209230bSgjelinek 		    "Could not create a temporary pool"));
33740209230bSgjelinek 	case Z_POOL_BIND:
33750209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool"));
3376*2ad45a84Sjv227347 	case Z_SYSTEM:
3377*2ad45a84Sjv227347 		return (strerror(errno));
33787c478bd9Sstevel@tonic-gate 	default:
33797c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
33807c478bd9Sstevel@tonic-gate 	}
33817c478bd9Sstevel@tonic-gate }
33827c478bd9Sstevel@tonic-gate 
33837c478bd9Sstevel@tonic-gate /*
33847c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
33857c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
33867c478bd9Sstevel@tonic-gate  */
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate static int
33897c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
33907c478bd9Sstevel@tonic-gate {
33917c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
33927c478bd9Sstevel@tonic-gate 	int err;
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate 	if (handle == NULL)
33957c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
33987c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
33997c478bd9Sstevel@tonic-gate 		return (err);
34007c478bd9Sstevel@tonic-gate 	}
34017c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
34027c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
34037c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
34047c478bd9Sstevel@tonic-gate 	return (Z_OK);
34057c478bd9Sstevel@tonic-gate }
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate static int
34087c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
34097c478bd9Sstevel@tonic-gate {
34107c478bd9Sstevel@tonic-gate 	if (handle == NULL)
34117c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
34147c478bd9Sstevel@tonic-gate 	return (Z_OK);
34157c478bd9Sstevel@tonic-gate }
34167c478bd9Sstevel@tonic-gate 
34170209230bSgjelinek /*
34180209230bSgjelinek  * Do the work required to manipulate a process through libproc.
34190209230bSgjelinek  * If grab_process() returns no errors (0), then release_process()
34200209230bSgjelinek  * must eventually be called.
34210209230bSgjelinek  *
34220209230bSgjelinek  * Return values:
34230209230bSgjelinek  *      0 Successful creation of agent thread
34240209230bSgjelinek  *      1 Error grabbing
34250209230bSgjelinek  *      2 Error creating agent
34260209230bSgjelinek  */
34270209230bSgjelinek static int
34280209230bSgjelinek grab_process(pr_info_handle_t *p)
34290209230bSgjelinek {
34300209230bSgjelinek 	int ret;
34310209230bSgjelinek 
34320209230bSgjelinek 	if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) {
34330209230bSgjelinek 
34340209230bSgjelinek 		if (Psetflags(p->pr, PR_RLC) != 0) {
34350209230bSgjelinek 			Prelease(p->pr, 0);
34360209230bSgjelinek 			return (1);
34370209230bSgjelinek 		}
34380209230bSgjelinek 		if (Pcreate_agent(p->pr) == 0) {
34390209230bSgjelinek 			return (0);
34400209230bSgjelinek 
34410209230bSgjelinek 		} else {
34420209230bSgjelinek 			Prelease(p->pr, 0);
34430209230bSgjelinek 			return (2);
34440209230bSgjelinek 		}
34450209230bSgjelinek 	} else {
34460209230bSgjelinek 		return (1);
34470209230bSgjelinek 	}
34480209230bSgjelinek }
34490209230bSgjelinek 
34500209230bSgjelinek /*
34510209230bSgjelinek  * Release the specified process. This destroys the agent
34520209230bSgjelinek  * and releases the process. If the process is NULL, nothing
34530209230bSgjelinek  * is done. This function should only be called if grab_process()
34540209230bSgjelinek  * has previously been called and returned success.
34550209230bSgjelinek  *
34560209230bSgjelinek  * This function is Pgrab-safe.
34570209230bSgjelinek  */
34580209230bSgjelinek static void
34590209230bSgjelinek release_process(struct ps_prochandle *Pr)
34600209230bSgjelinek {
34610209230bSgjelinek 	if (Pr == NULL)
34620209230bSgjelinek 		return;
34630209230bSgjelinek 
34640209230bSgjelinek 	Pdestroy_agent(Pr);
34650209230bSgjelinek 	Prelease(Pr, 0);
34660209230bSgjelinek }
34670209230bSgjelinek 
34680209230bSgjelinek static boolean_t
34690209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p)
34700209230bSgjelinek {
34710209230bSgjelinek 	DIR *dirp;
34720209230bSgjelinek 	struct dirent *dentp;
34730209230bSgjelinek 	zoneid_t zoneid;
34740209230bSgjelinek 	int pid_self;
34750209230bSgjelinek 	psinfo_t psinfo;
34760209230bSgjelinek 
34770209230bSgjelinek 	if (zone_get_id(zonename, &zoneid) != 0)
34780209230bSgjelinek 		return (B_FALSE);
34790209230bSgjelinek 
34800209230bSgjelinek 	pid_self = getpid();
34810209230bSgjelinek 
34820209230bSgjelinek 	if ((dirp = opendir("/proc")) == NULL)
34830209230bSgjelinek 		return (B_FALSE);
34840209230bSgjelinek 
34850209230bSgjelinek 	while (dentp = readdir(dirp)) {
34860209230bSgjelinek 		p->pid = atoi(dentp->d_name);
34870209230bSgjelinek 
34880209230bSgjelinek 		/* Skip self */
34890209230bSgjelinek 		if (p->pid == pid_self)
34900209230bSgjelinek 			continue;
34910209230bSgjelinek 
34920209230bSgjelinek 		if (proc_get_psinfo(p->pid, &psinfo) != 0)
34930209230bSgjelinek 			continue;
34940209230bSgjelinek 
34950209230bSgjelinek 		if (psinfo.pr_zoneid != zoneid)
34960209230bSgjelinek 			continue;
34970209230bSgjelinek 
34980209230bSgjelinek 		/* attempt to grab process */
34990209230bSgjelinek 		if (grab_process(p) != 0)
35000209230bSgjelinek 			continue;
35010209230bSgjelinek 
35020209230bSgjelinek 		if (pr_getzoneid(p->pr) != zoneid) {
35030209230bSgjelinek 			release_process(p->pr);
35040209230bSgjelinek 			continue;
35050209230bSgjelinek 		}
35060209230bSgjelinek 
35070209230bSgjelinek 		(void) closedir(dirp);
35080209230bSgjelinek 		return (B_TRUE);
35090209230bSgjelinek 	}
35100209230bSgjelinek 
35110209230bSgjelinek 	(void) closedir(dirp);
35120209230bSgjelinek 	return (B_FALSE);
35130209230bSgjelinek }
35140209230bSgjelinek 
35150209230bSgjelinek static boolean_t
35160209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk)
35170209230bSgjelinek {
35180209230bSgjelinek 	if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST))
35190209230bSgjelinek 		return (B_FALSE);
35200209230bSgjelinek 
35210209230bSgjelinek 	if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
35220209230bSgjelinek 		return (B_TRUE);
35230209230bSgjelinek 
35240209230bSgjelinek 	while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) {
35250209230bSgjelinek 		if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
35260209230bSgjelinek 			return (B_TRUE);
35270209230bSgjelinek 	}
35280209230bSgjelinek 
35290209230bSgjelinek 	return (B_FALSE);
35300209230bSgjelinek }
35310209230bSgjelinek 
35320209230bSgjelinek /*
35330209230bSgjelinek  * Apply the current rctl settings to the specified, running zone.
35340209230bSgjelinek  */
35350209230bSgjelinek int
35360209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle)
35370209230bSgjelinek {
35380209230bSgjelinek 	int err;
35390209230bSgjelinek 	int res = Z_OK;
35400209230bSgjelinek 	rctlblk_t *rblk;
35410209230bSgjelinek 	pr_info_handle_t p;
35420209230bSgjelinek 	struct zone_rctltab rctl;
35430209230bSgjelinek 
35440209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK)
35450209230bSgjelinek 		return (err);
35460209230bSgjelinek 
35470209230bSgjelinek 	if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) {
35480209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
35490209230bSgjelinek 		return (Z_NOMEM);
35500209230bSgjelinek 	}
35510209230bSgjelinek 
35520209230bSgjelinek 	if (!grab_zone_proc(zone_name, &p)) {
35530209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
35540209230bSgjelinek 		free(rblk);
35550209230bSgjelinek 		return (Z_SYSTEM);
35560209230bSgjelinek 	}
35570209230bSgjelinek 
35580209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
35590209230bSgjelinek 		char *rname;
35600209230bSgjelinek 		struct zone_rctlvaltab *valptr;
35610209230bSgjelinek 
35620209230bSgjelinek 		rname = rctl.zone_rctl_name;
35630209230bSgjelinek 
35640209230bSgjelinek 		/* first delete all current privileged settings for this rctl */
35650209230bSgjelinek 		while (get_priv_rctl(p.pr, rname, rblk)) {
35660209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) !=
35670209230bSgjelinek 			    0) {
35680209230bSgjelinek 				res = Z_SYSTEM;
35690209230bSgjelinek 				goto done;
35700209230bSgjelinek 			}
35710209230bSgjelinek 		}
35720209230bSgjelinek 
35730209230bSgjelinek 		/* now set each new value for the rctl */
35740209230bSgjelinek 		for (valptr = rctl.zone_rctl_valptr; valptr != NULL;
35750209230bSgjelinek 		    valptr = valptr->zone_rctlval_next) {
35760209230bSgjelinek 			if ((err = zonecfg_construct_rctlblk(valptr, rblk))
35770209230bSgjelinek 			    != Z_OK) {
35780209230bSgjelinek 				res = errno = err;
35790209230bSgjelinek 				goto done;
35800209230bSgjelinek 			}
35810209230bSgjelinek 
35820209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) {
35830209230bSgjelinek 				res = Z_SYSTEM;
35840209230bSgjelinek 				goto done;
35850209230bSgjelinek 			}
35860209230bSgjelinek 		}
35870209230bSgjelinek 	}
35880209230bSgjelinek 
35890209230bSgjelinek done:
35900209230bSgjelinek 	release_process(p.pr);
35910209230bSgjelinek 	free(rblk);
35920209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
35930209230bSgjelinek 
35940209230bSgjelinek 	return (res);
35950209230bSgjelinek }
35960209230bSgjelinek 
35970209230bSgjelinek static const xmlChar *
35980209230bSgjelinek nm_to_dtd(char *nm)
35990209230bSgjelinek {
36000209230bSgjelinek 	if (strcmp(nm, "device") == 0)
36010209230bSgjelinek 		return (DTD_ELEM_DEVICE);
36020209230bSgjelinek 	if (strcmp(nm, "fs") == 0)
36030209230bSgjelinek 		return (DTD_ELEM_FS);
36040209230bSgjelinek 	if (strcmp(nm, "inherit-pkg-dir") == 0)
36050209230bSgjelinek 		return (DTD_ELEM_IPD);
36060209230bSgjelinek 	if (strcmp(nm, "net") == 0)
36070209230bSgjelinek 		return (DTD_ELEM_NET);
36080209230bSgjelinek 	if (strcmp(nm, "attr") == 0)
36090209230bSgjelinek 		return (DTD_ELEM_ATTR);
36100209230bSgjelinek 	if (strcmp(nm, "rctl") == 0)
36110209230bSgjelinek 		return (DTD_ELEM_RCTL);
36120209230bSgjelinek 	if (strcmp(nm, "dataset") == 0)
36130209230bSgjelinek 		return (DTD_ELEM_DATASET);
36140209230bSgjelinek 
36150209230bSgjelinek 	return (NULL);
36160209230bSgjelinek }
36170209230bSgjelinek 
36180209230bSgjelinek int
36190209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc)
36200209230bSgjelinek {
36210209230bSgjelinek 	int num = 0;
36220209230bSgjelinek 	const xmlChar *dtd;
36230209230bSgjelinek 	xmlNodePtr cur;
36240209230bSgjelinek 
36250209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
36260209230bSgjelinek 		return (num);
36270209230bSgjelinek 
36280209230bSgjelinek 	if (zonecfg_setent(handle) != Z_OK)
36290209230bSgjelinek 		return (num);
36300209230bSgjelinek 
36310209230bSgjelinek 	for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next)
36320209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd) == 0)
36330209230bSgjelinek 			num++;
36340209230bSgjelinek 
36350209230bSgjelinek 	(void) zonecfg_endent(handle);
36360209230bSgjelinek 
36370209230bSgjelinek 	return (num);
36380209230bSgjelinek }
36390209230bSgjelinek 
36400209230bSgjelinek int
36410209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc)
36420209230bSgjelinek {
36430209230bSgjelinek 	int err;
36440209230bSgjelinek 	const xmlChar *dtd;
36450209230bSgjelinek 	xmlNodePtr cur;
36460209230bSgjelinek 
36470209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
36480209230bSgjelinek 		return (Z_NO_RESOURCE_TYPE);
36490209230bSgjelinek 
36500209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
36510209230bSgjelinek 		return (err);
36520209230bSgjelinek 
36530209230bSgjelinek 	cur = handle->zone_dh_cur;
36540209230bSgjelinek 	while (cur != NULL) {
36550209230bSgjelinek 		xmlNodePtr tmp;
36560209230bSgjelinek 
36570209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd)) {
36580209230bSgjelinek 			cur = cur->next;
36590209230bSgjelinek 			continue;
36600209230bSgjelinek 		}
36610209230bSgjelinek 
36620209230bSgjelinek 		tmp = cur->next;
36630209230bSgjelinek 		xmlUnlinkNode(cur);
36640209230bSgjelinek 		xmlFreeNode(cur);
36650209230bSgjelinek 		cur = tmp;
36660209230bSgjelinek 	}
36670209230bSgjelinek 
36680209230bSgjelinek 	(void) zonecfg_endent(handle);
36690209230bSgjelinek 	return (Z_OK);
36700209230bSgjelinek }
36710209230bSgjelinek 
36720209230bSgjelinek static boolean_t
36730209230bSgjelinek valid_uint(char *s, uint64_t *n)
36740209230bSgjelinek {
36750209230bSgjelinek 	char *endp;
36760209230bSgjelinek 
36770209230bSgjelinek 	/* strtoull accepts '-'?! so we want to flag that as an error */
36780209230bSgjelinek 	if (strchr(s, '-') != NULL)
36790209230bSgjelinek 		return (B_FALSE);
36800209230bSgjelinek 
36810209230bSgjelinek 	errno = 0;
36820209230bSgjelinek 	*n = strtoull(s, &endp, 10);
36830209230bSgjelinek 
36840209230bSgjelinek 	if (errno != 0 || *endp != '\0')
36850209230bSgjelinek 		return (B_FALSE);
36860209230bSgjelinek 	return (B_TRUE);
36870209230bSgjelinek }
36880209230bSgjelinek 
36890209230bSgjelinek /*
36900209230bSgjelinek  * Convert a string representing a number (possibly a fraction) into an integer.
36910209230bSgjelinek  * The string can have a modifier (K, M, G or T).   The modifiers are treated
36920209230bSgjelinek  * as powers of two (not 10).
36930209230bSgjelinek  */
36940209230bSgjelinek int
36950209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes)
36960209230bSgjelinek {
36970209230bSgjelinek 	long double val;
36980209230bSgjelinek 	char *unitp;
36990209230bSgjelinek 	uint64_t scale;
37000209230bSgjelinek 
37010209230bSgjelinek 	if ((val = strtold(str, &unitp)) < 0)
37020209230bSgjelinek 		return (-1);
37030209230bSgjelinek 
37040209230bSgjelinek 	/* remove any leading white space from units string */
37050209230bSgjelinek 	while (isspace(*unitp) != 0)
37060209230bSgjelinek 		++unitp;
37070209230bSgjelinek 
37080209230bSgjelinek 	/* if no units explicitly set, error */
37090209230bSgjelinek 	if (unitp == NULL || *unitp == '\0') {
37100209230bSgjelinek 		scale = 1;
37110209230bSgjelinek 	} else {
37120209230bSgjelinek 		int i;
37130209230bSgjelinek 		char *units[] = {"K", "M", "G", "T", NULL};
37140209230bSgjelinek 
37150209230bSgjelinek 		scale = 1024;
37160209230bSgjelinek 
37170209230bSgjelinek 		/* update scale based on units */
37180209230bSgjelinek 		for (i = 0; units[i] != NULL; i++) {
37190209230bSgjelinek 			if (strcasecmp(unitp, units[i]) == 0)
37200209230bSgjelinek 				break;
37210209230bSgjelinek 			scale <<= 10;
37220209230bSgjelinek 		}
37230209230bSgjelinek 
37240209230bSgjelinek 		if (units[i] == NULL)
37250209230bSgjelinek 			return (-1);
37260209230bSgjelinek 	}
37270209230bSgjelinek 
37280209230bSgjelinek 	*bytes = (uint64_t)(val * scale);
37290209230bSgjelinek 	return (0);
37300209230bSgjelinek }
37310209230bSgjelinek 
37320209230bSgjelinek boolean_t
37330209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr)
37340209230bSgjelinek {
37350209230bSgjelinek 	uint64_t low, high;
37360209230bSgjelinek 
37370209230bSgjelinek 	if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) ||
37380209230bSgjelinek 	    low < 1 || low > high)
37390209230bSgjelinek 		return (B_FALSE);
37400209230bSgjelinek 
37410209230bSgjelinek 	return (B_TRUE);
37420209230bSgjelinek }
37430209230bSgjelinek 
37440209230bSgjelinek boolean_t
37450209230bSgjelinek zonecfg_valid_importance(char *impstr)
37460209230bSgjelinek {
37470209230bSgjelinek 	uint64_t num;
37480209230bSgjelinek 
37490209230bSgjelinek 	if (!valid_uint(impstr, &num))
37500209230bSgjelinek 		return (B_FALSE);
37510209230bSgjelinek 
37520209230bSgjelinek 	return (B_TRUE);
37530209230bSgjelinek }
37540209230bSgjelinek 
37550209230bSgjelinek boolean_t
37560209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit)
37570209230bSgjelinek {
37580209230bSgjelinek 	int i;
37590209230bSgjelinek 
37600209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
37610209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
37620209230bSgjelinek 			break;
37630209230bSgjelinek 
37640209230bSgjelinek 	if (aliases[i].shortname == NULL)
37650209230bSgjelinek 		return (B_FALSE);
37660209230bSgjelinek 
37670209230bSgjelinek 	if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit)
37680209230bSgjelinek 		return (B_FALSE);
37690209230bSgjelinek 
37700209230bSgjelinek 	return (B_TRUE);
37710209230bSgjelinek }
37720209230bSgjelinek 
37730209230bSgjelinek boolean_t
37740209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val)
37750209230bSgjelinek {
37760209230bSgjelinek 	if (zonecfg_str_to_bytes(memstr, mem_val) != 0)
37770209230bSgjelinek 		return (B_FALSE);
37780209230bSgjelinek 
37790209230bSgjelinek 	return (B_TRUE);
37800209230bSgjelinek }
37810209230bSgjelinek 
37820209230bSgjelinek static int
37830209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res)
37840209230bSgjelinek {
37850209230bSgjelinek 	(void) strlcpy(pool_err, pool_strerror(pool_error()), err_size);
37860209230bSgjelinek 	return (res);
37870209230bSgjelinek }
37880209230bSgjelinek 
37890209230bSgjelinek static int
37900209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool,
37910209230bSgjelinek     char *name, int min, int max)
37920209230bSgjelinek {
37930209230bSgjelinek 	pool_resource_t *res;
37940209230bSgjelinek 	pool_elem_t *elem;
37950209230bSgjelinek 	pool_value_t *val;
37960209230bSgjelinek 
37970209230bSgjelinek 	if ((res = pool_resource_create(pconf, "pset", name)) == NULL)
37980209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
37990209230bSgjelinek 
38000209230bSgjelinek 	if (pool_associate(pconf, pool, res) != PO_SUCCESS)
38010209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
38020209230bSgjelinek 
38030209230bSgjelinek 	if ((elem = pool_resource_to_elem(pconf, res)) == NULL)
38040209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
38050209230bSgjelinek 
38060209230bSgjelinek 	if ((val = pool_value_alloc()) == NULL)
38070209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
38080209230bSgjelinek 
38090209230bSgjelinek 	/* set the maximum number of cpus for the pset */
38100209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)max);
38110209230bSgjelinek 
38120209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) {
38130209230bSgjelinek 		pool_value_free(val);
38140209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
38150209230bSgjelinek 	}
38160209230bSgjelinek 
38170209230bSgjelinek 	/* set the minimum number of cpus for the pset */
38180209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)min);
38190209230bSgjelinek 
38200209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) {
38210209230bSgjelinek 		pool_value_free(val);
38220209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
38230209230bSgjelinek 	}
38240209230bSgjelinek 
38250209230bSgjelinek 	pool_value_free(val);
38260209230bSgjelinek 
38270209230bSgjelinek 	return (Z_OK);
38280209230bSgjelinek }
38290209230bSgjelinek 
38300209230bSgjelinek static int
38310209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name,
38320209230bSgjelinek     struct zone_psettab *pset_tab)
38330209230bSgjelinek {
38340209230bSgjelinek 	pool_t *pool;
38350209230bSgjelinek 	int res = Z_OK;
38360209230bSgjelinek 
38370209230bSgjelinek 	/* create a temporary pool configuration */
38380209230bSgjelinek 	if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) {
38390209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
38400209230bSgjelinek 		return (res);
38410209230bSgjelinek 	}
38420209230bSgjelinek 
38430209230bSgjelinek 	if ((pool = pool_create(pconf, name)) == NULL) {
38440209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
38450209230bSgjelinek 		goto done;
38460209230bSgjelinek 	}
38470209230bSgjelinek 
38480209230bSgjelinek 	/* set pool importance */
38490209230bSgjelinek 	if (pset_tab->zone_importance[0] != '\0') {
38500209230bSgjelinek 		pool_elem_t *elem;
38510209230bSgjelinek 		pool_value_t *val;
38520209230bSgjelinek 
38530209230bSgjelinek 		if ((elem = pool_to_elem(pconf, pool)) == NULL) {
38540209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38550209230bSgjelinek 			goto done;
38560209230bSgjelinek 		}
38570209230bSgjelinek 
38580209230bSgjelinek 		if ((val = pool_value_alloc()) == NULL) {
38590209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38600209230bSgjelinek 			goto done;
38610209230bSgjelinek 		}
38620209230bSgjelinek 
38630209230bSgjelinek 		pool_value_set_int64(val,
38640209230bSgjelinek 		    (int64_t)atoi(pset_tab->zone_importance));
38650209230bSgjelinek 
38660209230bSgjelinek 		if (pool_put_property(pconf, elem, "pool.importance", val)
38670209230bSgjelinek 		    != PO_SUCCESS) {
38680209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38690209230bSgjelinek 			pool_value_free(val);
38700209230bSgjelinek 			goto done;
38710209230bSgjelinek 		}
38720209230bSgjelinek 
38730209230bSgjelinek 		pool_value_free(val);
38740209230bSgjelinek 	}
38750209230bSgjelinek 
38760209230bSgjelinek 	if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name,
38770209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_min),
38780209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_max))) != Z_OK)
38790209230bSgjelinek 		goto done;
38800209230bSgjelinek 
38810209230bSgjelinek 	/* validation */
38820209230bSgjelinek 	if (pool_conf_status(pconf) == POF_INVALID) {
38830209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
38840209230bSgjelinek 		goto done;
38850209230bSgjelinek 	}
38860209230bSgjelinek 
38870209230bSgjelinek 	/*
38880209230bSgjelinek 	 * This validation is the one we expect to fail if the user specified
38890209230bSgjelinek 	 * an invalid configuration (too many cpus) for this system.
38900209230bSgjelinek 	 */
38910209230bSgjelinek 	if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) {
38920209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
38930209230bSgjelinek 		goto done;
38940209230bSgjelinek 	}
38950209230bSgjelinek 
38960209230bSgjelinek 	/*
38970209230bSgjelinek 	 * Commit the dynamic configuration but not the pool configuration
38980209230bSgjelinek 	 * file.
38990209230bSgjelinek 	 */
39000209230bSgjelinek 	if (pool_conf_commit(pconf, 1) != PO_SUCCESS)
39010209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39020209230bSgjelinek 
39030209230bSgjelinek done:
39040209230bSgjelinek 	(void) pool_conf_close(pconf);
39050209230bSgjelinek 	return (res);
39060209230bSgjelinek }
39070209230bSgjelinek 
39080209230bSgjelinek static int
39090209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset,
39100209230bSgjelinek     struct zone_psettab *pset_tab)
39110209230bSgjelinek {
39120209230bSgjelinek 	int nfound = 0;
39130209230bSgjelinek 	pool_elem_t *pe;
39140209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
39150209230bSgjelinek 	uint64_t val_uint;
39160209230bSgjelinek 
39170209230bSgjelinek 	if (pool != NULL) {
39180209230bSgjelinek 		pe = pool_to_elem(pconf, pool);
39190209230bSgjelinek 		if (pool_get_property(pconf, pe, "pool.importance", pv)
39200209230bSgjelinek 		    != POC_INVAL) {
39210209230bSgjelinek 			int64_t val_int;
39220209230bSgjelinek 
39230209230bSgjelinek 			(void) pool_value_get_int64(pv, &val_int);
39240209230bSgjelinek 			(void) snprintf(pset_tab->zone_importance,
39250209230bSgjelinek 			    sizeof (pset_tab->zone_importance), "%d", val_int);
39260209230bSgjelinek 			nfound++;
39270209230bSgjelinek 		}
39280209230bSgjelinek 	}
39290209230bSgjelinek 
39300209230bSgjelinek 	if (pset != NULL) {
39310209230bSgjelinek 		pe = pool_resource_to_elem(pconf, pset);
39320209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) {
39330209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
39340209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_min,
39350209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_min), "%u", val_uint);
39360209230bSgjelinek 			nfound++;
39370209230bSgjelinek 		}
39380209230bSgjelinek 
39390209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) {
39400209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
39410209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_max,
39420209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_max), "%u", val_uint);
39430209230bSgjelinek 			nfound++;
39440209230bSgjelinek 		}
39450209230bSgjelinek 	}
39460209230bSgjelinek 
39470209230bSgjelinek 	pool_value_free(pv);
39480209230bSgjelinek 
39490209230bSgjelinek 	if (nfound == 3)
39500209230bSgjelinek 		return (PO_SUCCESS);
39510209230bSgjelinek 
39520209230bSgjelinek 	return (PO_FAIL);
39530209230bSgjelinek }
39540209230bSgjelinek 
39550209230bSgjelinek /*
39560209230bSgjelinek  * Determine if a tmp pool is configured and if so, if the configuration is
39570209230bSgjelinek  * still valid or if it has been changed since the tmp pool was created.
39580209230bSgjelinek  * If the tmp pool configuration is no longer valid, delete the tmp pool.
39590209230bSgjelinek  *
39600209230bSgjelinek  * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration.
39610209230bSgjelinek  */
39620209230bSgjelinek static int
39630209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err,
39640209230bSgjelinek     int err_size, struct zone_psettab *pset_tab, boolean_t *exists)
39650209230bSgjelinek {
39660209230bSgjelinek 	int res = Z_OK;
39670209230bSgjelinek 	pool_t *pool;
39680209230bSgjelinek 	pool_resource_t *pset;
39690209230bSgjelinek 	struct zone_psettab pset_current;
39700209230bSgjelinek 
39710209230bSgjelinek 	*exists = B_FALSE;
39720209230bSgjelinek 
39730209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
39740209230bSgjelinek 	    != PO_SUCCESS) {
39750209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39760209230bSgjelinek 		return (res);
39770209230bSgjelinek 	}
39780209230bSgjelinek 
39790209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
39800209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
39810209230bSgjelinek 
39820209230bSgjelinek 	if (pool == NULL && pset == NULL) {
39830209230bSgjelinek 		/* no tmp pool configured */
39840209230bSgjelinek 		goto done;
39850209230bSgjelinek 	}
39860209230bSgjelinek 
39870209230bSgjelinek 	/*
39880209230bSgjelinek 	 * If an existing tmp pool for this zone is configured with the proper
39890209230bSgjelinek 	 * settings, then the tmp pool is valid.
39900209230bSgjelinek 	 */
39910209230bSgjelinek 	if (get_running_tmp_pset(pconf, pool, pset, &pset_current)
39920209230bSgjelinek 	    == PO_SUCCESS &&
39930209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_min,
39940209230bSgjelinek 	    pset_current.zone_ncpu_min) == 0 &&
39950209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_max,
39960209230bSgjelinek 	    pset_current.zone_ncpu_max) == 0 &&
39970209230bSgjelinek 	    strcmp(pset_tab->zone_importance,
39980209230bSgjelinek 	    pset_current.zone_importance) == 0) {
39990209230bSgjelinek 		*exists = B_TRUE;
40000209230bSgjelinek 
40010209230bSgjelinek 	} else {
40020209230bSgjelinek 		/*
40030209230bSgjelinek 		 * An out-of-date tmp pool configuration exists.  Delete it
40040209230bSgjelinek 		 * so that we can create the correct tmp pool config.
40050209230bSgjelinek 		 */
40060209230bSgjelinek 		if (pset != NULL &&
40070209230bSgjelinek 		    pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
40080209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
40090209230bSgjelinek 			goto done;
40100209230bSgjelinek 		}
40110209230bSgjelinek 
40120209230bSgjelinek 		if (pool != NULL &&
40130209230bSgjelinek 		    pool_destroy(pconf, pool) != PO_SUCCESS) {
40140209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
40150209230bSgjelinek 			goto done;
40160209230bSgjelinek 		}
40170209230bSgjelinek 
40180209230bSgjelinek 		/* commit dynamic config */
40190209230bSgjelinek 		if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
40200209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
40210209230bSgjelinek 	}
40220209230bSgjelinek 
40230209230bSgjelinek done:
40240209230bSgjelinek 	(void) pool_conf_close(pconf);
40250209230bSgjelinek 
40260209230bSgjelinek 	return (res);
40270209230bSgjelinek }
40280209230bSgjelinek 
40290209230bSgjelinek /*
40300209230bSgjelinek  * Destroy any existing tmp pool.
40310209230bSgjelinek  */
40320209230bSgjelinek int
40330209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size)
40340209230bSgjelinek {
40350209230bSgjelinek 	int status;
40360209230bSgjelinek 	int res = Z_OK;
40370209230bSgjelinek 	pool_conf_t *pconf;
40380209230bSgjelinek 	pool_t *pool;
40390209230bSgjelinek 	pool_resource_t *pset;
40400209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
40410209230bSgjelinek 
40420209230bSgjelinek 	/* if pools not enabled then nothing to do */
40430209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
40440209230bSgjelinek 		return (Z_OK);
40450209230bSgjelinek 
40460209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
40470209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40480209230bSgjelinek 
40490209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
40500209230bSgjelinek 
40510209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
40520209230bSgjelinek 	    != PO_SUCCESS) {
40530209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40540209230bSgjelinek 		pool_conf_free(pconf);
40550209230bSgjelinek 		return (res);
40560209230bSgjelinek 	}
40570209230bSgjelinek 
40580209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
40590209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
40600209230bSgjelinek 
40610209230bSgjelinek 	if (pool == NULL && pset == NULL) {
40620209230bSgjelinek 		/* nothing to destroy, we're done */
40630209230bSgjelinek 		goto done;
40640209230bSgjelinek 	}
40650209230bSgjelinek 
40660209230bSgjelinek 	if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
40670209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40680209230bSgjelinek 		goto done;
40690209230bSgjelinek 	}
40700209230bSgjelinek 
40710209230bSgjelinek 	if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) {
40720209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40730209230bSgjelinek 		goto done;
40740209230bSgjelinek 	}
40750209230bSgjelinek 
40760209230bSgjelinek 	/* commit dynamic config */
40770209230bSgjelinek 	if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
40780209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40790209230bSgjelinek 
40800209230bSgjelinek done:
40810209230bSgjelinek 	(void) pool_conf_close(pconf);
40820209230bSgjelinek 	pool_conf_free(pconf);
40830209230bSgjelinek 
40840209230bSgjelinek 	return (res);
40850209230bSgjelinek }
40860209230bSgjelinek 
40870209230bSgjelinek /*
40880209230bSgjelinek  * Attempt to bind to a tmp pool for this zone.  If there is no tmp pool
40890209230bSgjelinek  * configured, we just return Z_OK.
40900209230bSgjelinek  *
40910209230bSgjelinek  * We either attempt to create the tmp pool for this zone or rebind to an
40920209230bSgjelinek  * existing tmp pool for this zone.
40930209230bSgjelinek  *
40940209230bSgjelinek  * Rebinding is used when a zone with a tmp pool reboots so that we don't have
40950209230bSgjelinek  * to recreate the tmp pool.  To do this we need to be sure we work correctly
40960209230bSgjelinek  * for the following cases:
40970209230bSgjelinek  *
40980209230bSgjelinek  *	- there is an existing, properly configured tmp pool.
40990209230bSgjelinek  *	- zonecfg added tmp pool after zone was booted, must now create.
41000209230bSgjelinek  *	- zonecfg updated tmp pool config after zone was booted, in this case
41010209230bSgjelinek  *	  we destroy the old tmp pool and create a new one.
41020209230bSgjelinek  */
41030209230bSgjelinek int
41040209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
41050209230bSgjelinek     int err_size)
41060209230bSgjelinek {
41070209230bSgjelinek 	struct zone_psettab pset_tab;
41080209230bSgjelinek 	int err;
41090209230bSgjelinek 	int status;
41100209230bSgjelinek 	pool_conf_t *pconf;
41110209230bSgjelinek 	boolean_t exists;
41120209230bSgjelinek 	char zone_name[ZONENAME_MAX];
41130209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
41140209230bSgjelinek 
41150209230bSgjelinek 	(void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name));
41160209230bSgjelinek 
41170209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
41180209230bSgjelinek 
41190209230bSgjelinek 	/* if no temporary pool configured, we're done */
41200209230bSgjelinek 	if (err == Z_NO_ENTRY)
41210209230bSgjelinek 		return (Z_OK);
41220209230bSgjelinek 
41230209230bSgjelinek 	/*
41240209230bSgjelinek 	 * importance might not have a value but we need to validate it here,
41250209230bSgjelinek 	 * so set the default.
41260209230bSgjelinek 	 */
41270209230bSgjelinek 	if (pset_tab.zone_importance[0] == '\0')
41280209230bSgjelinek 		(void) strlcpy(pset_tab.zone_importance, "1",
41290209230bSgjelinek 		    sizeof (pset_tab.zone_importance));
41300209230bSgjelinek 
41310209230bSgjelinek 	/* if pools not enabled, enable them now */
41320209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
41330209230bSgjelinek 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS)
41340209230bSgjelinek 			return (Z_POOL_ENABLE);
41350209230bSgjelinek 	}
41360209230bSgjelinek 
41370209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
41380209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
41390209230bSgjelinek 
41400209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
41410209230bSgjelinek 
41420209230bSgjelinek 	/*
41430209230bSgjelinek 	 * Check if a valid tmp pool/pset already exists.  If so, we just
41440209230bSgjelinek 	 * reuse it.
41450209230bSgjelinek 	 */
41460209230bSgjelinek 	if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size,
41470209230bSgjelinek 	    &pset_tab, &exists)) != Z_OK) {
41480209230bSgjelinek 		pool_conf_free(pconf);
41490209230bSgjelinek 		return (err);
41500209230bSgjelinek 	}
41510209230bSgjelinek 
41520209230bSgjelinek 	if (!exists)
41530209230bSgjelinek 		err = create_tmp_pool(pool_err, err_size, pconf, tmp_name,
41540209230bSgjelinek 		    &pset_tab);
41550209230bSgjelinek 
41560209230bSgjelinek 	pool_conf_free(pconf);
41570209230bSgjelinek 
41580209230bSgjelinek 	if (err != Z_OK)
41590209230bSgjelinek 		return (err);
41600209230bSgjelinek 
41610209230bSgjelinek 	/* Bind the zone to the pool. */
41620209230bSgjelinek 	if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS)
41630209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL_BIND));
41640209230bSgjelinek 
41650209230bSgjelinek 	return (Z_OK);
41660209230bSgjelinek }
41670209230bSgjelinek 
41680209230bSgjelinek /*
41690209230bSgjelinek  * Attempt to bind to a permanent pool for this zone.  If there is no
41700209230bSgjelinek  * permanent pool configured, we just return Z_OK.
41710209230bSgjelinek  */
41720209230bSgjelinek int
41730209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
41740209230bSgjelinek     int err_size)
41750209230bSgjelinek {
41760209230bSgjelinek 	pool_conf_t *poolconf;
41770209230bSgjelinek 	pool_t *pool;
41780209230bSgjelinek 	char poolname[MAXPATHLEN];
41790209230bSgjelinek 	int status;
41800209230bSgjelinek 	int error;
41810209230bSgjelinek 
41820209230bSgjelinek 	/*
41830209230bSgjelinek 	 * Find the pool mentioned in the zone configuration, and bind to it.
41840209230bSgjelinek 	 */
41850209230bSgjelinek 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
41860209230bSgjelinek 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
41870209230bSgjelinek 		/*
41880209230bSgjelinek 		 * The property is not set on the zone, so the pool
41890209230bSgjelinek 		 * should be bound to the default pool.  But that's
41900209230bSgjelinek 		 * already done by the kernel, so we can just return.
41910209230bSgjelinek 		 */
41920209230bSgjelinek 		return (Z_OK);
41930209230bSgjelinek 	}
41940209230bSgjelinek 	if (error != Z_OK) {
41950209230bSgjelinek 		/*
41960209230bSgjelinek 		 * Not an error, even though it shouldn't be happening.
41970209230bSgjelinek 		 */
41980209230bSgjelinek 		return (Z_OK);
41990209230bSgjelinek 	}
42000209230bSgjelinek 	/*
42010209230bSgjelinek 	 * Don't do anything if pools aren't enabled.
42020209230bSgjelinek 	 */
42030209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
42040209230bSgjelinek 		return (Z_POOLS_NOT_ACTIVE);
42050209230bSgjelinek 
42060209230bSgjelinek 	/*
42070209230bSgjelinek 	 * Try to provide a sane error message if the requested pool doesn't
42080209230bSgjelinek 	 * exist.
42090209230bSgjelinek 	 */
42100209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
42110209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
42120209230bSgjelinek 
42130209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
42140209230bSgjelinek 	    PO_SUCCESS) {
42150209230bSgjelinek 		pool_conf_free(poolconf);
42160209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
42170209230bSgjelinek 	}
42180209230bSgjelinek 	pool = pool_get_pool(poolconf, poolname);
42190209230bSgjelinek 	(void) pool_conf_close(poolconf);
42200209230bSgjelinek 	pool_conf_free(poolconf);
42210209230bSgjelinek 	if (pool == NULL)
42220209230bSgjelinek 		return (Z_NO_POOL);
42230209230bSgjelinek 
42240209230bSgjelinek 	/*
42250209230bSgjelinek 	 * Bind the zone to the pool.
42260209230bSgjelinek 	 */
42270209230bSgjelinek 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
42280209230bSgjelinek 		/* if bind fails, return poolname for the error msg */
42290209230bSgjelinek 		(void) strlcpy(pool_err, poolname, err_size);
42300209230bSgjelinek 		return (Z_POOL_BIND);
42310209230bSgjelinek 	}
42320209230bSgjelinek 
42330209230bSgjelinek 	return (Z_OK);
42340209230bSgjelinek }
42350209230bSgjelinek 
42360209230bSgjelinek 
42370209230bSgjelinek static boolean_t
42380209230bSgjelinek svc_enabled(char *svc_name)
42390209230bSgjelinek {
42400209230bSgjelinek 	scf_simple_prop_t	*prop;
42410209230bSgjelinek 	boolean_t		found = B_FALSE;
42420209230bSgjelinek 
42430209230bSgjelinek 	prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL,
42440209230bSgjelinek 	    SCF_PROPERTY_ENABLED);
42450209230bSgjelinek 
42460209230bSgjelinek 	if (scf_simple_prop_numvalues(prop) == 1 &&
42470209230bSgjelinek 	    *scf_simple_prop_next_boolean(prop) != 0)
42480209230bSgjelinek 		found = B_TRUE;
42490209230bSgjelinek 
42500209230bSgjelinek 	scf_simple_prop_free(prop);
42510209230bSgjelinek 
42520209230bSgjelinek 	return (found);
42530209230bSgjelinek }
42540209230bSgjelinek 
42550209230bSgjelinek /*
42560209230bSgjelinek  * If the zone has capped-memory, make sure the rcap service is enabled.
42570209230bSgjelinek  */
42580209230bSgjelinek int
42590209230bSgjelinek zonecfg_enable_rcapd(char *err, int size)
42600209230bSgjelinek {
42610209230bSgjelinek 	if (!svc_enabled(RCAP_SERVICE) &&
42620209230bSgjelinek 	    smf_enable_instance(RCAP_SERVICE, 0) == -1) {
42630209230bSgjelinek 		(void) strlcpy(err, scf_strerror(scf_error()), size);
42640209230bSgjelinek 		return (Z_SYSTEM);
42650209230bSgjelinek 	}
42660209230bSgjelinek 
42670209230bSgjelinek 	return (Z_OK);
42680209230bSgjelinek }
42690209230bSgjelinek 
42700209230bSgjelinek /*
42710209230bSgjelinek  * Return true if pset has cpu range specified and poold is not enabled.
42720209230bSgjelinek  */
42730209230bSgjelinek boolean_t
42740209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle)
42750209230bSgjelinek {
42760209230bSgjelinek 	struct zone_psettab pset_tab;
42770209230bSgjelinek 	int min, max;
42780209230bSgjelinek 	int err;
42790209230bSgjelinek 
42800209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
42810209230bSgjelinek 
42820209230bSgjelinek 	/* if no temporary pool configured, we're done */
42830209230bSgjelinek 	if (err == Z_NO_ENTRY)
42840209230bSgjelinek 		return (B_FALSE);
42850209230bSgjelinek 
42860209230bSgjelinek 	min = atoi(pset_tab.zone_ncpu_min);
42870209230bSgjelinek 	max = atoi(pset_tab.zone_ncpu_max);
42880209230bSgjelinek 
42890209230bSgjelinek 	/* range not specified, no need for poold */
42900209230bSgjelinek 	if (min == max)
42910209230bSgjelinek 		return (B_FALSE);
42920209230bSgjelinek 
42930209230bSgjelinek 	/* we have a range, check if poold service is enabled */
42940209230bSgjelinek 	if (svc_enabled(POOLD_SERVICE))
42950209230bSgjelinek 		return (B_FALSE);
42960209230bSgjelinek 
42970209230bSgjelinek 	return (B_TRUE);
42980209230bSgjelinek }
42990209230bSgjelinek 
43000209230bSgjelinek static int
43010209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize)
43020209230bSgjelinek {
43030209230bSgjelinek 	int status;
43040209230bSgjelinek 	pool_conf_t *poolconf;
43050209230bSgjelinek 	pool_t *pool;
43060209230bSgjelinek 	pool_elem_t *pe;
43070209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
43080209230bSgjelinek 	const char *sched_str;
43090209230bSgjelinek 
43100209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
43110209230bSgjelinek 		return (Z_NO_POOL);
43120209230bSgjelinek 
43130209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
43140209230bSgjelinek 		return (Z_NO_POOL);
43150209230bSgjelinek 
43160209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
43170209230bSgjelinek 	    PO_SUCCESS) {
43180209230bSgjelinek 		pool_conf_free(poolconf);
43190209230bSgjelinek 		return (Z_NO_POOL);
43200209230bSgjelinek 	}
43210209230bSgjelinek 
43220209230bSgjelinek 	if ((pool = pool_get_pool(poolconf, poolname)) == NULL) {
43230209230bSgjelinek 		(void) pool_conf_close(poolconf);
43240209230bSgjelinek 		pool_conf_free(poolconf);
43250209230bSgjelinek 		return (Z_NO_POOL);
43260209230bSgjelinek 	}
43270209230bSgjelinek 
43280209230bSgjelinek 	pe = pool_to_elem(poolconf, pool);
43290209230bSgjelinek 	if (pool_get_property(poolconf, pe, "pool.scheduler", pv)
43300209230bSgjelinek 	    != POC_INVAL) {
43310209230bSgjelinek 		(void) pool_value_get_string(pv, &sched_str);
43320209230bSgjelinek 		if (strlcpy(class, sched_str, clsize) >= clsize)
43330209230bSgjelinek 			return (Z_TOO_BIG);
43340209230bSgjelinek 	}
43350209230bSgjelinek 
43360209230bSgjelinek 	(void) pool_conf_close(poolconf);
43370209230bSgjelinek 	pool_conf_free(poolconf);
43380209230bSgjelinek 	return (Z_OK);
43390209230bSgjelinek }
43400209230bSgjelinek 
43410209230bSgjelinek /*
43420209230bSgjelinek  * Get the default scheduling class for the zone.  This will either be the
43430209230bSgjelinek  * class set on the zone's pool or the system default scheduling class.
43440209230bSgjelinek  */
43450209230bSgjelinek int
43460209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize)
43470209230bSgjelinek {
43480209230bSgjelinek 	char poolname[MAXPATHLEN];
43490209230bSgjelinek 
43500209230bSgjelinek 	if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) {
43510209230bSgjelinek 		/* check if the zone's pool specified a sched class */
43520209230bSgjelinek 		if (get_pool_sched_class(poolname, class, clsize) == Z_OK)
43530209230bSgjelinek 			return (Z_OK);
43540209230bSgjelinek 	}
43550209230bSgjelinek 
43560209230bSgjelinek 	if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1)
43570209230bSgjelinek 		return (Z_TOO_BIG);
43580209230bSgjelinek 
43590209230bSgjelinek 	return (Z_OK);
43600209230bSgjelinek }
43610209230bSgjelinek 
43627c478bd9Sstevel@tonic-gate int
43637c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle)
43647c478bd9Sstevel@tonic-gate {
43657c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
43667c478bd9Sstevel@tonic-gate }
43677c478bd9Sstevel@tonic-gate 
43687c478bd9Sstevel@tonic-gate int
43697c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
43707c478bd9Sstevel@tonic-gate {
43717c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options;
43727c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
43737c478bd9Sstevel@tonic-gate 	int err;
43747c478bd9Sstevel@tonic-gate 
43757c478bd9Sstevel@tonic-gate 	if (handle == NULL)
43767c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
43777c478bd9Sstevel@tonic-gate 
43787c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
43797c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43807c478bd9Sstevel@tonic-gate 
43817c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
43827c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
43837c478bd9Sstevel@tonic-gate 			break;
43847c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
43857c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43867c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43877c478bd9Sstevel@tonic-gate 	}
43887c478bd9Sstevel@tonic-gate 
43897c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
43907c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
43917c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43927c478bd9Sstevel@tonic-gate 		return (err);
43937c478bd9Sstevel@tonic-gate 	}
43947c478bd9Sstevel@tonic-gate 
43957c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
43967c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
43977c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43987c478bd9Sstevel@tonic-gate 		return (err);
43997c478bd9Sstevel@tonic-gate 	}
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
44027c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
44037c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44047c478bd9Sstevel@tonic-gate 		return (err);
44057c478bd9Sstevel@tonic-gate 	}
44067c478bd9Sstevel@tonic-gate 
44077c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
44087c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
44097c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44107c478bd9Sstevel@tonic-gate 		return (err);
44117c478bd9Sstevel@tonic-gate 	}
44127c478bd9Sstevel@tonic-gate 
44137c478bd9Sstevel@tonic-gate 	/* OK for options to be NULL */
44147c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
44157c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
44167c478bd9Sstevel@tonic-gate 	    options = options->next) {
44177c478bd9Sstevel@tonic-gate 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
44187c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK)
44197c478bd9Sstevel@tonic-gate 			break;
44207c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
44217c478bd9Sstevel@tonic-gate 			break;
44227c478bd9Sstevel@tonic-gate 	}
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
44257c478bd9Sstevel@tonic-gate 	return (Z_OK);
44267c478bd9Sstevel@tonic-gate }
44277c478bd9Sstevel@tonic-gate 
44287c478bd9Sstevel@tonic-gate int
44297c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle)
44307c478bd9Sstevel@tonic-gate {
44317c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
44327c478bd9Sstevel@tonic-gate }
44337c478bd9Sstevel@tonic-gate 
44347c478bd9Sstevel@tonic-gate int
44357c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle)
44367c478bd9Sstevel@tonic-gate {
44377c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
44387c478bd9Sstevel@tonic-gate }
44397c478bd9Sstevel@tonic-gate 
44407c478bd9Sstevel@tonic-gate int
44417c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
44427c478bd9Sstevel@tonic-gate {
44437c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
44447c478bd9Sstevel@tonic-gate 	int err;
44457c478bd9Sstevel@tonic-gate 
44467c478bd9Sstevel@tonic-gate 	if (handle == NULL)
44477c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44487c478bd9Sstevel@tonic-gate 
44497c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
44507c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44517c478bd9Sstevel@tonic-gate 
44527c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44537c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
44547c478bd9Sstevel@tonic-gate 			break;
44557c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
44567c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44577c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44587c478bd9Sstevel@tonic-gate 	}
44597c478bd9Sstevel@tonic-gate 
44607c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
44617c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
44627c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44637c478bd9Sstevel@tonic-gate 		return (err);
44647c478bd9Sstevel@tonic-gate 	}
44657c478bd9Sstevel@tonic-gate 
44667c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
44677c478bd9Sstevel@tonic-gate 	return (Z_OK);
44687c478bd9Sstevel@tonic-gate }
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate int
44717c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle)
44727c478bd9Sstevel@tonic-gate {
44737c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
44747c478bd9Sstevel@tonic-gate }
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate int
44777c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle)
44787c478bd9Sstevel@tonic-gate {
44797c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
44807c478bd9Sstevel@tonic-gate }
44817c478bd9Sstevel@tonic-gate 
44827c478bd9Sstevel@tonic-gate int
44837c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
44847c478bd9Sstevel@tonic-gate {
44857c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
44867c478bd9Sstevel@tonic-gate 	int err;
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate 	if (handle == NULL)
44897c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44907c478bd9Sstevel@tonic-gate 
44917c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
44927c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44937c478bd9Sstevel@tonic-gate 
44947c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44957c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
44967c478bd9Sstevel@tonic-gate 			break;
44977c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
44987c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44997c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45007c478bd9Sstevel@tonic-gate 	}
45017c478bd9Sstevel@tonic-gate 
45027c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
45037c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
45047c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45057c478bd9Sstevel@tonic-gate 		return (err);
45067c478bd9Sstevel@tonic-gate 	}
45077c478bd9Sstevel@tonic-gate 
45087c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
45097c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
45107c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45117c478bd9Sstevel@tonic-gate 		return (err);
45127c478bd9Sstevel@tonic-gate 	}
45137c478bd9Sstevel@tonic-gate 
4514de860bd9Sgfaden 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
4515de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter,
4516de860bd9Sgfaden 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK) {
4517de860bd9Sgfaden 		handle->zone_dh_cur = handle->zone_dh_top;
4518de860bd9Sgfaden 		return (err);
4519de860bd9Sgfaden 	}
4520de860bd9Sgfaden 
45217c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45227c478bd9Sstevel@tonic-gate 	return (Z_OK);
45237c478bd9Sstevel@tonic-gate }
45247c478bd9Sstevel@tonic-gate 
45257c478bd9Sstevel@tonic-gate int
45267c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle)
45277c478bd9Sstevel@tonic-gate {
45287c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45297c478bd9Sstevel@tonic-gate }
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate int
45327c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle)
45337c478bd9Sstevel@tonic-gate {
45347c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
45357c478bd9Sstevel@tonic-gate }
45367c478bd9Sstevel@tonic-gate 
45377c478bd9Sstevel@tonic-gate int
45387c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
45397c478bd9Sstevel@tonic-gate {
45407c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
45417c478bd9Sstevel@tonic-gate 	int err;
45427c478bd9Sstevel@tonic-gate 
45437c478bd9Sstevel@tonic-gate 	if (handle == NULL)
45447c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
45457c478bd9Sstevel@tonic-gate 
45467c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
45477c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
45507c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
45517c478bd9Sstevel@tonic-gate 			break;
45527c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
45537c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45547c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45557c478bd9Sstevel@tonic-gate 	}
45567c478bd9Sstevel@tonic-gate 
45577c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
45587c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
45597c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45607c478bd9Sstevel@tonic-gate 		return (err);
45617c478bd9Sstevel@tonic-gate 	}
45627c478bd9Sstevel@tonic-gate 
45637c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45647c478bd9Sstevel@tonic-gate 	return (Z_OK);
45657c478bd9Sstevel@tonic-gate }
45667c478bd9Sstevel@tonic-gate 
45677c478bd9Sstevel@tonic-gate int
45687c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle)
45697c478bd9Sstevel@tonic-gate {
45707c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45717c478bd9Sstevel@tonic-gate }
45727c478bd9Sstevel@tonic-gate 
45737c478bd9Sstevel@tonic-gate int
45747c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle)
45757c478bd9Sstevel@tonic-gate {
45767c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
45777c478bd9Sstevel@tonic-gate }
45787c478bd9Sstevel@tonic-gate 
45797c478bd9Sstevel@tonic-gate int
45807c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
45817c478bd9Sstevel@tonic-gate {
45827c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
45837c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
45847c478bd9Sstevel@tonic-gate 	int err;
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate 	if (handle == NULL)
45877c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
45887c478bd9Sstevel@tonic-gate 
45897c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
45907c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45917c478bd9Sstevel@tonic-gate 
45927c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
45937c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
45947c478bd9Sstevel@tonic-gate 			break;
45957c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
45967c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45977c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45987c478bd9Sstevel@tonic-gate 	}
45997c478bd9Sstevel@tonic-gate 
46007c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
46017c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
46027c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46037c478bd9Sstevel@tonic-gate 		return (err);
46047c478bd9Sstevel@tonic-gate 	}
46057c478bd9Sstevel@tonic-gate 
46067c478bd9Sstevel@tonic-gate 	tabptr->zone_rctl_valptr = NULL;
46077c478bd9Sstevel@tonic-gate 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
46087c478bd9Sstevel@tonic-gate 		valptr = (struct zone_rctlvaltab *)malloc(
46097c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctlvaltab));
46107c478bd9Sstevel@tonic-gate 		if (valptr == NULL)
46117c478bd9Sstevel@tonic-gate 			return (Z_NOMEM);
46127c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
46137c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
46147c478bd9Sstevel@tonic-gate 			break;
46157c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
46167c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
46177c478bd9Sstevel@tonic-gate 			break;
46187c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
46197c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
46207c478bd9Sstevel@tonic-gate 			break;
46217c478bd9Sstevel@tonic-gate 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
46227c478bd9Sstevel@tonic-gate 			break;
46237c478bd9Sstevel@tonic-gate 	}
46247c478bd9Sstevel@tonic-gate 
46257c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
46267c478bd9Sstevel@tonic-gate 	return (Z_OK);
46277c478bd9Sstevel@tonic-gate }
46287c478bd9Sstevel@tonic-gate 
46297c478bd9Sstevel@tonic-gate int
46307c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle)
46317c478bd9Sstevel@tonic-gate {
46327c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
46337c478bd9Sstevel@tonic-gate }
46347c478bd9Sstevel@tonic-gate 
46357c478bd9Sstevel@tonic-gate int
46367c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle)
46377c478bd9Sstevel@tonic-gate {
46387c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
46397c478bd9Sstevel@tonic-gate }
46407c478bd9Sstevel@tonic-gate 
46417c478bd9Sstevel@tonic-gate int
46427c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
46437c478bd9Sstevel@tonic-gate {
46447c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
46457c478bd9Sstevel@tonic-gate 	int err;
46467c478bd9Sstevel@tonic-gate 
46477c478bd9Sstevel@tonic-gate 	if (handle == NULL)
46487c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
46497c478bd9Sstevel@tonic-gate 
46507c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
46517c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46527c478bd9Sstevel@tonic-gate 
46537c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
46547c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
46557c478bd9Sstevel@tonic-gate 			break;
46567c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
46577c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46587c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46597c478bd9Sstevel@tonic-gate 	}
46607c478bd9Sstevel@tonic-gate 
46617c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
46627c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
46637c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46647c478bd9Sstevel@tonic-gate 		return (err);
46657c478bd9Sstevel@tonic-gate 	}
46667c478bd9Sstevel@tonic-gate 
46677c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
46687c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
46697c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46707c478bd9Sstevel@tonic-gate 		return (err);
46717c478bd9Sstevel@tonic-gate 	}
46727c478bd9Sstevel@tonic-gate 
46737c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
46747c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
46757c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46767c478bd9Sstevel@tonic-gate 		return (err);
46777c478bd9Sstevel@tonic-gate 	}
46787c478bd9Sstevel@tonic-gate 
46797c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
46807c478bd9Sstevel@tonic-gate 	return (Z_OK);
46817c478bd9Sstevel@tonic-gate }
46827c478bd9Sstevel@tonic-gate 
46837c478bd9Sstevel@tonic-gate int
46847c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle)
46857c478bd9Sstevel@tonic-gate {
46867c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
46877c478bd9Sstevel@tonic-gate }
46887c478bd9Sstevel@tonic-gate 
4689ffbafc53Scomay /*
4690ffbafc53Scomay  * The privileges available on the system and described in privileges(5)
46919acbbeafSnn35248  * fall into four categories with respect to non-global zones:
46929acbbeafSnn35248  *
46939acbbeafSnn35248  *      Default set of privileges considered safe for all non-global
46949acbbeafSnn35248  *      zones.  These privileges are "safe" in the sense that a
46959acbbeafSnn35248  *      privileged process in the zone cannot affect processes in any
46969acbbeafSnn35248  *      other zone on the system.
46979acbbeafSnn35248  *
46989acbbeafSnn35248  *      Set of privileges not currently permitted within a non-global
46999acbbeafSnn35248  *      zone.  These privileges are considered by default, "unsafe,"
47009acbbeafSnn35248  *      and include ones which affect global resources (such as the
47019acbbeafSnn35248  *      system clock or physical memory) or are overly broad and cover
47029acbbeafSnn35248  *      more than one mechanism in the system.  In other cases, there
47039acbbeafSnn35248  *      has not been sufficient virtualization in the parts of the
47049acbbeafSnn35248  *      system the privilege covers to allow its use within a
47059acbbeafSnn35248  *      non-global zone.
47069acbbeafSnn35248  *
47079acbbeafSnn35248  *      Set of privileges required in order to get a zone booted and
47089acbbeafSnn35248  *      init(1M) started.  These cannot be removed from the zone's
47099acbbeafSnn35248  *      privilege set.
47109acbbeafSnn35248  *
47119acbbeafSnn35248  * All other privileges are optional and are potentially useful for
4712ffbafc53Scomay  * processes executing inside a non-global zone.
4713ffbafc53Scomay  *
4714ffbafc53Scomay  * When privileges are added to the system, a determination needs to be
4715ffbafc53Scomay  * made as to which category the privilege belongs to.  Ideally,
4716ffbafc53Scomay  * privileges should be fine-grained enough and the mechanisms they cover
4717ffbafc53Scomay  * virtualized enough so that they can be made available to non-global
4718ffbafc53Scomay  * zones.
4719ffbafc53Scomay  */
4720ffbafc53Scomay 
4721ffbafc53Scomay /*
4722ffbafc53Scomay  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
4723ffbafc53Scomay  * the privilege string separator can be any character, although it is
4724ffbafc53Scomay  * usually a comma character, define these here as well in the event that
4725ffbafc53Scomay  * they change or are augmented in the future.
4726ffbafc53Scomay  */
4727ffbafc53Scomay #define	BASIC_TOKEN		"basic"
4728ffbafc53Scomay #define	DEFAULT_TOKEN		"default"
4729ffbafc53Scomay #define	ZONE_TOKEN		"zone"
4730ffbafc53Scomay #define	TOKEN_PRIV_CHAR		','
4731ffbafc53Scomay #define	TOKEN_PRIV_STR		","
4732ffbafc53Scomay 
47339acbbeafSnn35248 typedef struct priv_node {
47349acbbeafSnn35248 	struct priv_node	*pn_next;	/* Next privilege */
47359acbbeafSnn35248 	char			*pn_priv;	/* Privileges name */
47369acbbeafSnn35248 } priv_node_t;
47379acbbeafSnn35248 
47389acbbeafSnn35248 /* Privileges lists can differ across brands */
47399acbbeafSnn35248 typedef struct priv_lists {
47409acbbeafSnn35248 	/* Privileges considered safe for all non-global zones of a brand */
47419acbbeafSnn35248 	struct priv_node	*pl_default;
47429acbbeafSnn35248 
47439acbbeafSnn35248 	/* Privileges not permitted for all non-global zones of a brand */
47449acbbeafSnn35248 	struct priv_node	*pl_prohibited;
47459acbbeafSnn35248 
47469acbbeafSnn35248 	/* Privileges required for all non-global zones of a brand */
47479acbbeafSnn35248 	struct priv_node	*pl_required;
4748bf1d7e28Sdh155122 
4749bf1d7e28Sdh155122 	/*
4750bf1d7e28Sdh155122 	 * ip-type of the zone these privileges lists apply to.
4751bf1d7e28Sdh155122 	 * It is used to pass ip-type to the callback function,
4752bf1d7e28Sdh155122 	 * priv_lists_cb, which has no way of getting the ip-type.
4753bf1d7e28Sdh155122 	 */
4754bf1d7e28Sdh155122 	const char		*pl_iptype;
47559acbbeafSnn35248 } priv_lists_t;
47569acbbeafSnn35248 
47579acbbeafSnn35248 static int
4758bf1d7e28Sdh155122 priv_lists_cb(void *data, priv_iter_t *priv_iter)
47597c478bd9Sstevel@tonic-gate {
47609acbbeafSnn35248 	priv_lists_t *plp = (priv_lists_t *)data;
47619acbbeafSnn35248 	priv_node_t *pnp;
47629acbbeafSnn35248 
4763bf1d7e28Sdh155122 	/* Skip this privilege if ip-type does not match */
4764bf1d7e28Sdh155122 	if ((strcmp(priv_iter->pi_iptype, "all") != 0) &&
4765bf1d7e28Sdh155122 	    (strcmp(priv_iter->pi_iptype, plp->pl_iptype) != 0))
4766bf1d7e28Sdh155122 		return (0);
4767bf1d7e28Sdh155122 
47689acbbeafSnn35248 	/* Allocate a new priv list node. */
47699acbbeafSnn35248 	if ((pnp = malloc(sizeof (*pnp))) == NULL)
47709acbbeafSnn35248 		return (-1);
4771bf1d7e28Sdh155122 	if ((pnp->pn_priv = strdup(priv_iter->pi_name)) == NULL) {
47729acbbeafSnn35248 		free(pnp);
47739acbbeafSnn35248 		return (-1);
47749acbbeafSnn35248 	}
47759acbbeafSnn35248 
47769acbbeafSnn35248 	/* Insert the new priv list node into the right list */
4777bf1d7e28Sdh155122 	if (strcmp(priv_iter->pi_set, "default") == 0) {
47789acbbeafSnn35248 		pnp->pn_next = plp->pl_default;
47799acbbeafSnn35248 		plp->pl_default = pnp;
4780bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "prohibited") == 0) {
47819acbbeafSnn35248 		pnp->pn_next = plp->pl_prohibited;
47829acbbeafSnn35248 		plp->pl_prohibited = pnp;
4783bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "required") == 0) {
47849acbbeafSnn35248 		pnp->pn_next = plp->pl_required;
47859acbbeafSnn35248 		plp->pl_required = pnp;
47869acbbeafSnn35248 	} else {
47879acbbeafSnn35248 		free(pnp->pn_priv);
47889acbbeafSnn35248 		free(pnp);
47899acbbeafSnn35248 		return (-1);
47909acbbeafSnn35248 	}
47919acbbeafSnn35248 	return (0);
47929acbbeafSnn35248 }
47939acbbeafSnn35248 
47949acbbeafSnn35248 static void
47959acbbeafSnn35248 priv_lists_destroy(priv_lists_t *plp)
47969acbbeafSnn35248 {
47979acbbeafSnn35248 	priv_node_t *pnp;
47989acbbeafSnn35248 
47999acbbeafSnn35248 	assert(plp != NULL);
48009acbbeafSnn35248 
48019acbbeafSnn35248 	while ((pnp = plp->pl_default) != NULL) {
48029acbbeafSnn35248 		plp->pl_default = pnp->pn_next;
48039acbbeafSnn35248 		free(pnp->pn_priv);
48049acbbeafSnn35248 		free(pnp);
48059acbbeafSnn35248 	}
48069acbbeafSnn35248 	while ((pnp = plp->pl_prohibited) != NULL) {
48079acbbeafSnn35248 		plp->pl_prohibited = pnp->pn_next;
48089acbbeafSnn35248 		free(pnp->pn_priv);
48099acbbeafSnn35248 		free(pnp);
48109acbbeafSnn35248 	}
48119acbbeafSnn35248 	while ((pnp = plp->pl_required) != NULL) {
48129acbbeafSnn35248 		plp->pl_required = pnp->pn_next;
48139acbbeafSnn35248 		free(pnp->pn_priv);
48149acbbeafSnn35248 		free(pnp);
48159acbbeafSnn35248 	}
48169acbbeafSnn35248 	free(plp);
48179acbbeafSnn35248 }
48189acbbeafSnn35248 
48199acbbeafSnn35248 static int
4820bf1d7e28Sdh155122 priv_lists_create(zone_dochandle_t handle, priv_lists_t **plpp,
4821bf1d7e28Sdh155122     const char *curr_iptype)
48229acbbeafSnn35248 {
48239acbbeafSnn35248 	priv_lists_t *plp;
4824123807fbSedp 	brand_handle_t bh;
48259acbbeafSnn35248 	char brand[MAXNAMELEN];
48269acbbeafSnn35248 
48279acbbeafSnn35248 	if (handle != NULL) {
48289acbbeafSnn35248 		if (zonecfg_get_brand(handle, brand, sizeof (brand)) != 0)
48299acbbeafSnn35248 			return (Z_BRAND_ERROR);
48309acbbeafSnn35248 	} else {
48319acbbeafSnn35248 		(void) strlcpy(brand, NATIVE_BRAND_NAME, MAXNAMELEN);
48329acbbeafSnn35248 	}
48339acbbeafSnn35248 
4834123807fbSedp 	if ((bh = brand_open(brand)) == NULL)
48359acbbeafSnn35248 		return (Z_BRAND_ERROR);
48369acbbeafSnn35248 
48379acbbeafSnn35248 	if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) {
4838123807fbSedp 		brand_close(bh);
48399acbbeafSnn35248 		return (Z_NOMEM);
48409acbbeafSnn35248 	}
48419acbbeafSnn35248 
4842bf1d7e28Sdh155122 	plp->pl_iptype = curr_iptype;
4843bf1d7e28Sdh155122 
48449acbbeafSnn35248 	/* construct the privilege lists */
4845123807fbSedp 	if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) {
48469acbbeafSnn35248 		priv_lists_destroy(plp);
4847123807fbSedp 		brand_close(bh);
48489acbbeafSnn35248 		return (Z_BRAND_ERROR);
48499acbbeafSnn35248 	}
48509acbbeafSnn35248 
4851123807fbSedp 	brand_close(bh);
48529acbbeafSnn35248 	*plpp = plp;
48539acbbeafSnn35248 	return (Z_OK);
48549acbbeafSnn35248 }
48559acbbeafSnn35248 
48569acbbeafSnn35248 static int
48579acbbeafSnn35248 get_default_privset(priv_set_t *privs, priv_lists_t *plp)
48589acbbeafSnn35248 {
48599acbbeafSnn35248 	priv_node_t *pnp;
4860ffbafc53Scomay 	priv_set_t *basic;
48617c478bd9Sstevel@tonic-gate 
4862ffbafc53Scomay 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
48637c478bd9Sstevel@tonic-gate 	if (basic == NULL)
4864ffbafc53Scomay 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
48657c478bd9Sstevel@tonic-gate 
48667c478bd9Sstevel@tonic-gate 	priv_union(basic, privs);
48677c478bd9Sstevel@tonic-gate 	priv_freeset(basic);
48687c478bd9Sstevel@tonic-gate 
48699acbbeafSnn35248 	for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) {
48709acbbeafSnn35248 		if (priv_addset(privs, pnp->pn_priv) != 0)
48717c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
48727c478bd9Sstevel@tonic-gate 	}
48739acbbeafSnn35248 
48747c478bd9Sstevel@tonic-gate 	return (Z_OK);
48757c478bd9Sstevel@tonic-gate }
48767c478bd9Sstevel@tonic-gate 
48779acbbeafSnn35248 int
4878bf1d7e28Sdh155122 zonecfg_default_privset(priv_set_t *privs, const char *curr_iptype)
48799acbbeafSnn35248 {
48809acbbeafSnn35248 	priv_lists_t *plp;
48819acbbeafSnn35248 	int ret;
48829acbbeafSnn35248 
4883bf1d7e28Sdh155122 	if ((ret = priv_lists_create(NULL, &plp, curr_iptype)) != Z_OK)
48849acbbeafSnn35248 		return (ret);
48859acbbeafSnn35248 	ret = get_default_privset(privs, plp);
48869acbbeafSnn35248 	priv_lists_destroy(plp);
48879acbbeafSnn35248 	return (ret);
48889acbbeafSnn35248 }
48899acbbeafSnn35248 
4890ffbafc53Scomay void
4891ffbafc53Scomay append_priv_token(char *priv, char *str, size_t strlen)
4892ffbafc53Scomay {
4893ffbafc53Scomay 	if (*str != '\0')
4894ffbafc53Scomay 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
4895ffbafc53Scomay 	(void) strlcat(str, priv, strlen);
4896ffbafc53Scomay }
4897ffbafc53Scomay 
4898ffbafc53Scomay /*
4899ffbafc53Scomay  * Verify that the supplied string is a valid privilege limit set for a
4900ffbafc53Scomay  * non-global zone.  This string must not only be acceptable to
4901ffbafc53Scomay  * priv_str_to_set(3C) which parses it, but it also must resolve to a
4902ffbafc53Scomay  * privilege set that includes certain required privileges and lacks
4903ffbafc53Scomay  * certain prohibited privileges.
4904ffbafc53Scomay  */
4905ffbafc53Scomay static int
4906ffbafc53Scomay verify_privset(char *privbuf, priv_set_t *privs, char **privname,
49079acbbeafSnn35248     boolean_t add_default, priv_lists_t *plp)
4908ffbafc53Scomay {
49099acbbeafSnn35248 	priv_node_t *pnp;
49109acbbeafSnn35248 	char *tmp, *cp, *lasts;
4911ffbafc53Scomay 	size_t len;
4912ffbafc53Scomay 	priv_set_t *mergeset;
4913ffbafc53Scomay 	const char *token;
4914ffbafc53Scomay 
4915ffbafc53Scomay 	/*
4916ffbafc53Scomay 	 * The verification of the privilege string occurs in several
4917ffbafc53Scomay 	 * phases.  In the first phase, the supplied string is scanned for
4918ffbafc53Scomay 	 * the ZONE_TOKEN token which is not support as part of the
4919ffbafc53Scomay 	 * "limitpriv" property.
4920ffbafc53Scomay 	 *
4921ffbafc53Scomay 	 * Duplicate the supplied privilege string since strtok_r(3C)
4922ffbafc53Scomay 	 * tokenizes its input by null-terminating the tokens.
4923ffbafc53Scomay 	 */
4924ffbafc53Scomay 	if ((tmp = strdup(privbuf)) == NULL)
4925ffbafc53Scomay 		return (Z_NOMEM);
4926ffbafc53Scomay 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
4927ffbafc53Scomay 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
4928ffbafc53Scomay 		if (strcmp(cp, ZONE_TOKEN) == 0) {
4929ffbafc53Scomay 			free(tmp);
4930ffbafc53Scomay 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
4931ffbafc53Scomay 				return (Z_NOMEM);
4932ffbafc53Scomay 			else
4933ffbafc53Scomay 				return (Z_PRIV_UNKNOWN);
4934ffbafc53Scomay 		}
4935ffbafc53Scomay 	}
4936ffbafc53Scomay 	free(tmp);
4937ffbafc53Scomay 
4938ffbafc53Scomay 	if (add_default) {
4939ffbafc53Scomay 		/*
4940ffbafc53Scomay 		 * If DEFAULT_TOKEN was specified, a string needs to be
4941ffbafc53Scomay 		 * built containing the privileges from the default, safe
4942ffbafc53Scomay 		 * set along with those of the "limitpriv" property.
4943ffbafc53Scomay 		 */
4944ffbafc53Scomay 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
49459acbbeafSnn35248 
49469acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
49479acbbeafSnn35248 			len += strlen(pnp->pn_priv) + 1;
4948ffbafc53Scomay 		tmp = alloca(len);
4949ffbafc53Scomay 		*tmp = '\0';
4950ffbafc53Scomay 
4951ffbafc53Scomay 		append_priv_token(BASIC_TOKEN, tmp, len);
49529acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
49539acbbeafSnn35248 			append_priv_token(pnp->pn_priv, tmp, len);
4954ffbafc53Scomay 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
4955ffbafc53Scomay 		(void) strlcat(tmp, privbuf, len);
4956ffbafc53Scomay 	} else {
4957ffbafc53Scomay 		tmp = privbuf;
4958ffbafc53Scomay 	}
4959ffbafc53Scomay 
4960ffbafc53Scomay 
4961ffbafc53Scomay 	/*
4962ffbafc53Scomay 	 * In the next phase, attempt to convert the merged privilege
4963ffbafc53Scomay 	 * string into a privilege set.  In the case of an error, either
4964ffbafc53Scomay 	 * there was a memory allocation failure or there was an invalid
4965ffbafc53Scomay 	 * privilege token in the string.  In either case, return an
4966ffbafc53Scomay 	 * appropriate error code but in the event of an invalid token,
4967ffbafc53Scomay 	 * allocate a string containing its name and return that back to
4968ffbafc53Scomay 	 * the caller.
4969ffbafc53Scomay 	 */
4970ffbafc53Scomay 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
4971ffbafc53Scomay 	if (mergeset == NULL) {
4972ffbafc53Scomay 		if (token == NULL)
4973ffbafc53Scomay 			return (Z_NOMEM);
4974ffbafc53Scomay 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
4975ffbafc53Scomay 			*cp = '\0';
4976ffbafc53Scomay 		if ((*privname = strdup(token)) == NULL)
4977ffbafc53Scomay 			return (Z_NOMEM);
4978ffbafc53Scomay 		else
4979ffbafc53Scomay 			return (Z_PRIV_UNKNOWN);
4980ffbafc53Scomay 	}
4981ffbafc53Scomay 
4982ffbafc53Scomay 	/*
4983ffbafc53Scomay 	 * Next, verify that none of the prohibited zone privileges are
4984ffbafc53Scomay 	 * present in the merged privilege set.
4985ffbafc53Scomay 	 */
49869acbbeafSnn35248 	for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) {
49879acbbeafSnn35248 		if (priv_ismember(mergeset, pnp->pn_priv)) {
4988ffbafc53Scomay 			priv_freeset(mergeset);
49899acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
4990ffbafc53Scomay 				return (Z_NOMEM);
4991ffbafc53Scomay 			else
4992ffbafc53Scomay 				return (Z_PRIV_PROHIBITED);
4993ffbafc53Scomay 		}
4994ffbafc53Scomay 	}
4995ffbafc53Scomay 
4996ffbafc53Scomay 	/*
4997ffbafc53Scomay 	 * Finally, verify that all of the required zone privileges are
4998ffbafc53Scomay 	 * present in the merged privilege set.
4999ffbafc53Scomay 	 */
50009acbbeafSnn35248 	for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) {
50019acbbeafSnn35248 		if (!priv_ismember(mergeset, pnp->pn_priv)) {
5002ffbafc53Scomay 			priv_freeset(mergeset);
50039acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
5004ffbafc53Scomay 				return (Z_NOMEM);
5005ffbafc53Scomay 			else
5006ffbafc53Scomay 				return (Z_PRIV_REQUIRED);
5007ffbafc53Scomay 		}
5008ffbafc53Scomay 	}
5009ffbafc53Scomay 
5010ffbafc53Scomay 	priv_copyset(mergeset, privs);
5011ffbafc53Scomay 	priv_freeset(mergeset);
5012ffbafc53Scomay 	return (Z_OK);
5013ffbafc53Scomay }
5014ffbafc53Scomay 
5015ffbafc53Scomay /*
5016ffbafc53Scomay  * Fill in the supplied privilege set with either the default, safe set of
5017ffbafc53Scomay  * privileges suitable for a non-global zone, or one based on the
5018ffbafc53Scomay  * "limitpriv" property in the zone's configuration.
5019ffbafc53Scomay  *
5020ffbafc53Scomay  * In the event of an invalid privilege specification in the
5021ffbafc53Scomay  * configuration, a string is allocated and returned containing the
5022ffbafc53Scomay  * "privilege" causing the issue.  It is the caller's responsibility to
5023ffbafc53Scomay  * free this memory when it is done with it.
5024ffbafc53Scomay  */
5025ffbafc53Scomay int
5026ffbafc53Scomay zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
5027ffbafc53Scomay     char **privname)
5028ffbafc53Scomay {
50299acbbeafSnn35248 	priv_lists_t *plp;
50309acbbeafSnn35248 	char *cp, *limitpriv = NULL;
50319acbbeafSnn35248 	int err, limitlen;
5032bf1d7e28Sdh155122 	zone_iptype_t iptype;
5033bf1d7e28Sdh155122 	const char *curr_iptype;
5034ffbafc53Scomay 
5035ffbafc53Scomay 	/*
5036ffbafc53Scomay 	 * Attempt to lookup the "limitpriv" property.  If it does not
5037ffbafc53Scomay 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
5038ffbafc53Scomay 	 * default, safe privilege set is returned.
5039ffbafc53Scomay 	 */
50409acbbeafSnn35248 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK)
5041ffbafc53Scomay 		return (err);
50429acbbeafSnn35248 
5043bf1d7e28Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK)
5044bf1d7e28Sdh155122 		return (err);
5045bf1d7e28Sdh155122 
5046bf1d7e28Sdh155122 	switch (iptype) {
5047bf1d7e28Sdh155122 	case ZS_SHARED:
5048bf1d7e28Sdh155122 		curr_iptype = "shared";
5049bf1d7e28Sdh155122 		break;
5050bf1d7e28Sdh155122 	case ZS_EXCLUSIVE:
5051bf1d7e28Sdh155122 		curr_iptype = "exclusive";
5052bf1d7e28Sdh155122 		break;
5053bf1d7e28Sdh155122 	}
5054bf1d7e28Sdh155122 
5055bf1d7e28Sdh155122 	if ((err = priv_lists_create(handle, &plp, curr_iptype)) != Z_OK)
50569acbbeafSnn35248 		return (err);
50579acbbeafSnn35248 
5058ffbafc53Scomay 	limitlen = strlen(limitpriv);
5059ffbafc53Scomay 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
5060ffbafc53Scomay 		free(limitpriv);
50619acbbeafSnn35248 		err = get_default_privset(privs, plp);
50629acbbeafSnn35248 		priv_lists_destroy(plp);
50639acbbeafSnn35248 		return (err);
5064ffbafc53Scomay 	}
5065ffbafc53Scomay 
5066ffbafc53Scomay 	/*
5067ffbafc53Scomay 	 * Check if the string DEFAULT_TOKEN is the first token in a list
5068ffbafc53Scomay 	 * of privileges.
5069ffbafc53Scomay 	 */
5070ffbafc53Scomay 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
5071ffbafc53Scomay 	if (cp != NULL &&
5072ffbafc53Scomay 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
50739acbbeafSnn35248 		err = verify_privset(cp + 1, privs, privname, B_TRUE, plp);
5074ffbafc53Scomay 	else
50759acbbeafSnn35248 		err = verify_privset(limitpriv, privs, privname, B_FALSE, plp);
5076ffbafc53Scomay 
5077ffbafc53Scomay 	free(limitpriv);
50789acbbeafSnn35248 	priv_lists_destroy(plp);
5079ffbafc53Scomay 	return (err);
5080ffbafc53Scomay }
5081ffbafc53Scomay 
50827c478bd9Sstevel@tonic-gate int
50837c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
50847c478bd9Sstevel@tonic-gate {
50857c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
50867c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
50877c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
50887c478bd9Sstevel@tonic-gate 	FILE *cookie;
50897c478bd9Sstevel@tonic-gate 	int err;
5090108322fbScarlsonj 	char *cp;
50917c478bd9Sstevel@tonic-gate 
50927c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
50937c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
50947c478bd9Sstevel@tonic-gate 
5095108322fbScarlsonj 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
5096108322fbScarlsonj 	cp = zonepath + strlen(zonepath);
5097108322fbScarlsonj 	while (cp > zonepath && cp[-1] == '/')
5098108322fbScarlsonj 		*--cp = '\0';
5099108322fbScarlsonj 
51007c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
5101108322fbScarlsonj 		if (zonepath[0] == '\0')
51027c478bd9Sstevel@tonic-gate 			(void) strlcpy(zonepath, "/", rp_sz);
51037c478bd9Sstevel@tonic-gate 		return (Z_OK);
51047c478bd9Sstevel@tonic-gate 	}
51057c478bd9Sstevel@tonic-gate 
51067c478bd9Sstevel@tonic-gate 	/*
51077c478bd9Sstevel@tonic-gate 	 * First check the index file.  Because older versions did not have
51087c478bd9Sstevel@tonic-gate 	 * a copy of the zone path, allow for it to be zero length, in which
51097c478bd9Sstevel@tonic-gate 	 * case we ignore this result and fall back to the XML files.
51107c478bd9Sstevel@tonic-gate 	 */
51117c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
51127c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
51137c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
51147c478bd9Sstevel@tonic-gate 			found = B_TRUE;
5115108322fbScarlsonj 			if (ze->zone_path[0] != '\0')
5116108322fbScarlsonj 				(void) strlcpy(cp, ze->zone_path,
5117108322fbScarlsonj 				    rp_sz - (cp - zonepath));
51187c478bd9Sstevel@tonic-gate 		}
51197c478bd9Sstevel@tonic-gate 		free(ze);
51207c478bd9Sstevel@tonic-gate 		if (found)
51217c478bd9Sstevel@tonic-gate 			break;
51227c478bd9Sstevel@tonic-gate 	}
51237c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
5124108322fbScarlsonj 	if (found && *cp != '\0')
51257c478bd9Sstevel@tonic-gate 		return (Z_OK);
51267c478bd9Sstevel@tonic-gate 
51277c478bd9Sstevel@tonic-gate 	/* Fall back to the XML files. */
51287c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL)
51297c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
51307c478bd9Sstevel@tonic-gate 
51317c478bd9Sstevel@tonic-gate 	/*
51327c478bd9Sstevel@tonic-gate 	 * Check the snapshot first: if a zone is running, its zonepath
51337c478bd9Sstevel@tonic-gate 	 * may have changed.
51347c478bd9Sstevel@tonic-gate 	 */
51357c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
5136e767a340Sgjelinek 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK) {
5137e767a340Sgjelinek 			zonecfg_fini_handle(handle);
51387c478bd9Sstevel@tonic-gate 			return (err);
51397c478bd9Sstevel@tonic-gate 		}
5140e767a340Sgjelinek 	}
51417c478bd9Sstevel@tonic-gate 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
51427c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
51437c478bd9Sstevel@tonic-gate 	return (err);
51447c478bd9Sstevel@tonic-gate }
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate int
51477c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
51487c478bd9Sstevel@tonic-gate {
51497c478bd9Sstevel@tonic-gate 	int err;
51507c478bd9Sstevel@tonic-gate 
51517c478bd9Sstevel@tonic-gate 	/* This function makes sense for non-global zones only. */
51527c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
51537c478bd9Sstevel@tonic-gate 		return (Z_BOGUS_ZONE_NAME);
51547c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
51557c478bd9Sstevel@tonic-gate 		return (err);
51567c478bd9Sstevel@tonic-gate 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
51577c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
51587c478bd9Sstevel@tonic-gate 	return (Z_OK);
51597c478bd9Sstevel@tonic-gate }
51607c478bd9Sstevel@tonic-gate 
51619acbbeafSnn35248 int
51629acbbeafSnn35248 zone_get_brand(char *zone_name, char *brandname, size_t rp_sz)
51639acbbeafSnn35248 {
51649acbbeafSnn35248 	int err;
51659acbbeafSnn35248 	zone_dochandle_t handle;
51669acbbeafSnn35248 	char myzone[MAXNAMELEN];
51679acbbeafSnn35248 	int myzoneid = getzoneid();
51689acbbeafSnn35248 
51699acbbeafSnn35248 	/*
51709acbbeafSnn35248 	 * If we are not in the global zone, then we don't have the zone
51719acbbeafSnn35248 	 * .xml files with the brand name available.  Thus, we are going to
51729acbbeafSnn35248 	 * have to ask the kernel for the information.
51739acbbeafSnn35248 	 */
51749acbbeafSnn35248 	if (myzoneid != GLOBAL_ZONEID) {
51752ec67e04Sgjelinek 		if (is_system_labeled()) {
51762ec67e04Sgjelinek 			(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
51772ec67e04Sgjelinek 			return (Z_OK);
51782ec67e04Sgjelinek 		}
51799acbbeafSnn35248 		if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone,
51809acbbeafSnn35248 		    sizeof (myzone)) < 0)
51819acbbeafSnn35248 			return (Z_NO_ZONE);
51829acbbeafSnn35248 		if (strncmp(zone_name, myzone, MAXNAMELEN) != NULL)
51839acbbeafSnn35248 			return (Z_NO_ZONE);
51849acbbeafSnn35248 		err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz);
51859acbbeafSnn35248 		if (err < 0)
51869acbbeafSnn35248 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
51879acbbeafSnn35248 		return (Z_OK);
51889acbbeafSnn35248 	}
51899acbbeafSnn35248 
51909acbbeafSnn35248 	if (strcmp(zone_name, "global") == NULL) {
51919acbbeafSnn35248 		(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
51922ec67e04Sgjelinek 		return (Z_OK);
51939acbbeafSnn35248 	}
51949acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL)
51959acbbeafSnn35248 		return (Z_NOMEM);
51969acbbeafSnn35248 
51979acbbeafSnn35248 	err = zonecfg_get_handle((char *)zone_name, handle);
51989acbbeafSnn35248 	if (err == Z_OK)
51999acbbeafSnn35248 		err = zonecfg_get_brand(handle, brandname, rp_sz);
52009acbbeafSnn35248 
52019acbbeafSnn35248 	zonecfg_fini_handle(handle);
52029acbbeafSnn35248 	return (err);
52039acbbeafSnn35248 }
52049acbbeafSnn35248 
5205facf4a8dSllai1 /*
5206facf4a8dSllai1  * Return the appropriate root for the active /dev.
5207facf4a8dSllai1  * For normal zone, the path is $ZONEPATH/root;
5208facf4a8dSllai1  * for scratch zone, the dev path is $ZONEPATH/lu.
5209facf4a8dSllai1  */
5210facf4a8dSllai1 int
5211facf4a8dSllai1 zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz)
5212facf4a8dSllai1 {
5213facf4a8dSllai1 	int err;
5214facf4a8dSllai1 	char *suffix;
5215facf4a8dSllai1 	zone_state_t state;
5216facf4a8dSllai1 
5217facf4a8dSllai1 	/* This function makes sense for non-global zones only. */
5218facf4a8dSllai1 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
5219facf4a8dSllai1 		return (Z_BOGUS_ZONE_NAME);
5220facf4a8dSllai1 	if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK)
5221facf4a8dSllai1 		return (err);
5222facf4a8dSllai1 
5223facf4a8dSllai1 	if (zone_get_state(zone_name, &state) == Z_OK &&
5224facf4a8dSllai1 	    state == ZONE_STATE_MOUNTED)
5225facf4a8dSllai1 		suffix = "/lu";
5226facf4a8dSllai1 	else
5227facf4a8dSllai1 		suffix = "/root";
5228facf4a8dSllai1 	if (strlcat(devroot, suffix, rp_sz) >= rp_sz)
5229facf4a8dSllai1 		return (Z_TOO_BIG);
5230facf4a8dSllai1 	return (Z_OK);
5231facf4a8dSllai1 }
5232facf4a8dSllai1 
52337c478bd9Sstevel@tonic-gate static zone_state_t
5234108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
52357c478bd9Sstevel@tonic-gate {
5236108322fbScarlsonj 	char zoneroot[MAXPATHLEN];
5237108322fbScarlsonj 	size_t zlen;
5238108322fbScarlsonj 
52397c478bd9Sstevel@tonic-gate 	assert(kernel_state <= ZONE_MAX_STATE);
52407c478bd9Sstevel@tonic-gate 	switch (kernel_state) {
52417c478bd9Sstevel@tonic-gate 		case ZONE_IS_UNINITIALIZED:
5242bd41d0a8Snordmark 		case ZONE_IS_INITIALIZED:
5243bd41d0a8Snordmark 			/* The kernel will not return these two states */
5244108322fbScarlsonj 			return (ZONE_STATE_READY);
52457c478bd9Sstevel@tonic-gate 		case ZONE_IS_READY:
5246108322fbScarlsonj 			/*
5247108322fbScarlsonj 			 * If the zone's root is mounted on $ZONEPATH/lu, then
5248108322fbScarlsonj 			 * it's a mounted scratch zone.
5249108322fbScarlsonj 			 */
5250108322fbScarlsonj 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
5251108322fbScarlsonj 			    sizeof (zoneroot)) >= 0) {
5252108322fbScarlsonj 				zlen = strlen(zoneroot);
5253108322fbScarlsonj 				if (zlen > 3 &&
5254108322fbScarlsonj 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
5255108322fbScarlsonj 					return (ZONE_STATE_MOUNTED);
5256108322fbScarlsonj 			}
52577c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_READY);
52587c478bd9Sstevel@tonic-gate 		case ZONE_IS_BOOTING:
52597c478bd9Sstevel@tonic-gate 		case ZONE_IS_RUNNING:
52607c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_RUNNING);
52617c478bd9Sstevel@tonic-gate 		case ZONE_IS_SHUTTING_DOWN:
52627c478bd9Sstevel@tonic-gate 		case ZONE_IS_EMPTY:
52637c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_SHUTTING_DOWN);
52647c478bd9Sstevel@tonic-gate 		case ZONE_IS_DOWN:
52657c478bd9Sstevel@tonic-gate 		case ZONE_IS_DYING:
52667c478bd9Sstevel@tonic-gate 		case ZONE_IS_DEAD:
52677c478bd9Sstevel@tonic-gate 		default:
52687c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_DOWN);
52697c478bd9Sstevel@tonic-gate 	}
52707c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
52717c478bd9Sstevel@tonic-gate }
52727c478bd9Sstevel@tonic-gate 
52737c478bd9Sstevel@tonic-gate int
52747c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num)
52757c478bd9Sstevel@tonic-gate {
52767c478bd9Sstevel@tonic-gate 	zone_status_t status;
52777c478bd9Sstevel@tonic-gate 	zoneid_t zone_id;
52787c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
52797c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
52807c478bd9Sstevel@tonic-gate 	FILE *cookie;
5281108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
5282108322fbScarlsonj 	FILE *fp;
52837c478bd9Sstevel@tonic-gate 
52847c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
52857c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
52867c478bd9Sstevel@tonic-gate 
5287108322fbScarlsonj 	/*
5288108322fbScarlsonj 	 * If we're looking at an alternate root, then we need to query the
5289108322fbScarlsonj 	 * kernel using the scratch zone name.
5290108322fbScarlsonj 	 */
5291108322fbScarlsonj 	zone_id = -1;
5292108322fbScarlsonj 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
5293108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
5294108322fbScarlsonj 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
5295108322fbScarlsonj 			    kernzone, sizeof (kernzone)) == 0)
5296108322fbScarlsonj 				zone_id = getzoneidbyname(kernzone);
5297108322fbScarlsonj 			zonecfg_close_scratch(fp);
5298108322fbScarlsonj 		}
5299108322fbScarlsonj 	} else {
5300108322fbScarlsonj 		zone_id = getzoneidbyname(zone_name);
5301108322fbScarlsonj 	}
5302108322fbScarlsonj 
53037c478bd9Sstevel@tonic-gate 	/* check to see if zone is running */
5304108322fbScarlsonj 	if (zone_id != -1 &&
53057c478bd9Sstevel@tonic-gate 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
53067c478bd9Sstevel@tonic-gate 	    sizeof (status)) >= 0) {
5307108322fbScarlsonj 		*state_num = kernel_state_to_user_state(zone_id, status);
53087c478bd9Sstevel@tonic-gate 		return (Z_OK);
53097c478bd9Sstevel@tonic-gate 	}
53107c478bd9Sstevel@tonic-gate 
53117c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
53127c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
53137c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
53147c478bd9Sstevel@tonic-gate 			found = B_TRUE;
53157c478bd9Sstevel@tonic-gate 			*state_num = ze->zone_state;
53167c478bd9Sstevel@tonic-gate 		}
53177c478bd9Sstevel@tonic-gate 		free(ze);
53187c478bd9Sstevel@tonic-gate 		if (found)
53197c478bd9Sstevel@tonic-gate 			break;
53207c478bd9Sstevel@tonic-gate 	}
53217c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
53227c478bd9Sstevel@tonic-gate 	return ((found) ? Z_OK : Z_NO_ZONE);
53237c478bd9Sstevel@tonic-gate }
53247c478bd9Sstevel@tonic-gate 
53257c478bd9Sstevel@tonic-gate int
53267c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state)
53277c478bd9Sstevel@tonic-gate {
53287c478bd9Sstevel@tonic-gate 	struct zoneent ze;
53297c478bd9Sstevel@tonic-gate 
53307c478bd9Sstevel@tonic-gate 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
53317c478bd9Sstevel@tonic-gate 	    state != ZONE_STATE_INCOMPLETE)
53327c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
53337c478bd9Sstevel@tonic-gate 
5334087719fdSdp 	bzero(&ze, sizeof (ze));
53357c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
53367c478bd9Sstevel@tonic-gate 	ze.zone_state = state;
53377c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
53387c478bd9Sstevel@tonic-gate 	return (putzoneent(&ze, PZE_MODIFY));
53397c478bd9Sstevel@tonic-gate }
53407c478bd9Sstevel@tonic-gate 
53417c478bd9Sstevel@tonic-gate /*
53427c478bd9Sstevel@tonic-gate  * Get id (if any) for specified zone.  There are four possible outcomes:
53437c478bd9Sstevel@tonic-gate  * - If the string corresponds to the numeric id of an active (booted)
53447c478bd9Sstevel@tonic-gate  *   zone, sets *zip to the zone id and returns 0.
53457c478bd9Sstevel@tonic-gate  * - If the string corresponds to the name of an active (booted) zone,
53467c478bd9Sstevel@tonic-gate  *   sets *zip to the zone id and returns 0.
53477c478bd9Sstevel@tonic-gate  * - If the string is a name in the configuration but is not booted,
53487c478bd9Sstevel@tonic-gate  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
53497c478bd9Sstevel@tonic-gate  * - Otherwise, leaves *zip unchanged and returns -1.
53507c478bd9Sstevel@tonic-gate  *
53517c478bd9Sstevel@tonic-gate  * This function acts as an auxiliary filter on the function of the same
53527c478bd9Sstevel@tonic-gate  * name in libc; the linker binds to this version if libzonecfg exists,
53537c478bd9Sstevel@tonic-gate  * and the libc version if it doesn't.  Any changes to this version of
53547c478bd9Sstevel@tonic-gate  * the function should probably be reflected in the libc version as well.
53557c478bd9Sstevel@tonic-gate  */
53567c478bd9Sstevel@tonic-gate int
53577c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip)
53587c478bd9Sstevel@tonic-gate {
53597c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl;
53607c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
53617c478bd9Sstevel@tonic-gate 	char *cp;
53627c478bd9Sstevel@tonic-gate 	int err;
53637c478bd9Sstevel@tonic-gate 
53647c478bd9Sstevel@tonic-gate 	/* first try looking for active zone by id */
53657c478bd9Sstevel@tonic-gate 	errno = 0;
53667c478bd9Sstevel@tonic-gate 	zoneid = (zoneid_t)strtol(str, &cp, 0);
53677c478bd9Sstevel@tonic-gate 	if (errno == 0 && cp != str && *cp == '\0' &&
53687c478bd9Sstevel@tonic-gate 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
53697c478bd9Sstevel@tonic-gate 		*zip = zoneid;
53707c478bd9Sstevel@tonic-gate 		return (0);
53717c478bd9Sstevel@tonic-gate 	}
53727c478bd9Sstevel@tonic-gate 
53737c478bd9Sstevel@tonic-gate 	/* then look for active zone by name */
53747c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(str)) != -1) {
53757c478bd9Sstevel@tonic-gate 		*zip = zoneid;
53767c478bd9Sstevel@tonic-gate 		return (0);
53777c478bd9Sstevel@tonic-gate 	}
53787c478bd9Sstevel@tonic-gate 
53797c478bd9Sstevel@tonic-gate 	/* if in global zone, try looking up name in configuration database */
53807c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID ||
53817c478bd9Sstevel@tonic-gate 	    (hdl = zonecfg_init_handle()) == NULL)
53827c478bd9Sstevel@tonic-gate 		return (-1);
53837c478bd9Sstevel@tonic-gate 
5384108322fbScarlsonj 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
53857c478bd9Sstevel@tonic-gate 		/* zone exists but isn't active */
53867c478bd9Sstevel@tonic-gate 		*zip = ZONE_ID_UNDEFINED;
53877c478bd9Sstevel@tonic-gate 		err = 0;
53887c478bd9Sstevel@tonic-gate 	} else {
53897c478bd9Sstevel@tonic-gate 		err = -1;
53907c478bd9Sstevel@tonic-gate 	}
53917c478bd9Sstevel@tonic-gate 
53927c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(hdl);
53937c478bd9Sstevel@tonic-gate 	return (err);
53947c478bd9Sstevel@tonic-gate }
53957c478bd9Sstevel@tonic-gate 
53967c478bd9Sstevel@tonic-gate char *
53977c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num)
53987c478bd9Sstevel@tonic-gate {
53997c478bd9Sstevel@tonic-gate 	switch (state_num) {
54007c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
54017c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_CONFIGURED);
54027c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
54037c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INCOMPLETE);
54047c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
54057c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INSTALLED);
54067c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
54077c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_READY);
5408108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
5409108322fbScarlsonj 		return (ZONE_STATE_STR_MOUNTED);
54107c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
54117c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_RUNNING);
54127c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
54137c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_SHUTTING_DOWN);
54147c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
54157c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_DOWN);
54167c478bd9Sstevel@tonic-gate 	default:
54177c478bd9Sstevel@tonic-gate 		return ("unknown");
54187c478bd9Sstevel@tonic-gate 	}
54197c478bd9Sstevel@tonic-gate }
54207c478bd9Sstevel@tonic-gate 
54217c478bd9Sstevel@tonic-gate /*
5422108322fbScarlsonj  * Given a UUID value, find an associated zone name.  This is intended to be
5423108322fbScarlsonj  * used by callers who set up some 'default' name (corresponding to the
5424108322fbScarlsonj  * expected name for the zone) in the zonename buffer, and thus the function
5425108322fbScarlsonj  * doesn't touch this buffer on failure.
5426108322fbScarlsonj  */
5427108322fbScarlsonj int
5428555afedfScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
5429108322fbScarlsonj {
5430108322fbScarlsonj 	FILE *fp;
5431108322fbScarlsonj 	struct zoneent *ze;
5432555afedfScarlsonj 	uchar_t *uuid;
5433108322fbScarlsonj 
5434108322fbScarlsonj 	/*
5435108322fbScarlsonj 	 * A small amount of subterfuge via casts is necessary here because
5436108322fbScarlsonj 	 * libuuid doesn't use const correctly, but we don't want to export
5437108322fbScarlsonj 	 * this brokenness to our clients.
5438108322fbScarlsonj 	 */
5439555afedfScarlsonj 	uuid = (uchar_t *)uuidin;
5440555afedfScarlsonj 	if (uuid_is_null(uuid))
5441108322fbScarlsonj 		return (Z_NO_ZONE);
5442108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5443108322fbScarlsonj 		return (Z_NO_ZONE);
5444108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5445555afedfScarlsonj 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
5446108322fbScarlsonj 			break;
5447108322fbScarlsonj 		free(ze);
5448108322fbScarlsonj 	}
5449108322fbScarlsonj 	endzoneent(fp);
5450108322fbScarlsonj 	if (ze != NULL) {
5451108322fbScarlsonj 		(void) strlcpy(zonename, ze->zone_name, namelen);
5452108322fbScarlsonj 		free(ze);
5453108322fbScarlsonj 		return (Z_OK);
5454108322fbScarlsonj 	} else {
5455108322fbScarlsonj 		return (Z_NO_ZONE);
5456108322fbScarlsonj 	}
5457108322fbScarlsonj }
5458108322fbScarlsonj 
5459108322fbScarlsonj /*
5460108322fbScarlsonj  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
5461108322fbScarlsonj  * exists but the file doesn't have a value set yet.  Returns an error if the
5462108322fbScarlsonj  * zone cannot be located.
5463108322fbScarlsonj  */
5464108322fbScarlsonj int
5465108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid)
5466108322fbScarlsonj {
5467108322fbScarlsonj 	FILE *fp;
5468108322fbScarlsonj 	struct zoneent *ze;
5469108322fbScarlsonj 
5470108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5471108322fbScarlsonj 		return (Z_NO_ZONE);
5472108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5473108322fbScarlsonj 		if (strcmp(ze->zone_name, zonename) == 0)
5474108322fbScarlsonj 			break;
5475108322fbScarlsonj 		free(ze);
5476108322fbScarlsonj 	}
5477108322fbScarlsonj 	endzoneent(fp);
5478108322fbScarlsonj 	if (ze != NULL) {
5479108322fbScarlsonj 		uuid_copy(uuid, ze->zone_uuid);
5480108322fbScarlsonj 		free(ze);
5481108322fbScarlsonj 		return (Z_OK);
5482108322fbScarlsonj 	} else {
5483108322fbScarlsonj 		return (Z_NO_ZONE);
5484108322fbScarlsonj 	}
5485108322fbScarlsonj }
5486108322fbScarlsonj 
5487108322fbScarlsonj /*
54887c478bd9Sstevel@tonic-gate  * File-system convenience functions.
54897c478bd9Sstevel@tonic-gate  */
54907c478bd9Sstevel@tonic-gate boolean_t
54917c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type)
54927c478bd9Sstevel@tonic-gate {
54937c478bd9Sstevel@tonic-gate 	/*
54947c478bd9Sstevel@tonic-gate 	 * We already know which FS types don't work.
54957c478bd9Sstevel@tonic-gate 	 */
54967c478bd9Sstevel@tonic-gate 	if (strcmp(type, "proc") == 0 ||
54977c478bd9Sstevel@tonic-gate 	    strcmp(type, "mntfs") == 0 ||
54987c478bd9Sstevel@tonic-gate 	    strcmp(type, "autofs") == 0 ||
54997c478bd9Sstevel@tonic-gate 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
55007c478bd9Sstevel@tonic-gate 	    strcmp(type, "cachefs") == 0)
55017c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55027c478bd9Sstevel@tonic-gate 	/*
55037c478bd9Sstevel@tonic-gate 	 * The caller may do more detailed verification to make sure other
55047c478bd9Sstevel@tonic-gate 	 * aspects of this filesystem type make sense.
55057c478bd9Sstevel@tonic-gate 	 */
55067c478bd9Sstevel@tonic-gate 	return (B_TRUE);
55077c478bd9Sstevel@tonic-gate }
55087c478bd9Sstevel@tonic-gate 
55097c478bd9Sstevel@tonic-gate /*
55107c478bd9Sstevel@tonic-gate  * Generally uninteresting rctl convenience functions.
55117c478bd9Sstevel@tonic-gate  */
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate int
55147c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
55157c478bd9Sstevel@tonic-gate     rctlblk_t *rctlblk)
55167c478bd9Sstevel@tonic-gate {
55177c478bd9Sstevel@tonic-gate 	unsigned long long ull;
55187c478bd9Sstevel@tonic-gate 	char *endp;
55197c478bd9Sstevel@tonic-gate 	rctl_priv_t priv;
55207c478bd9Sstevel@tonic-gate 	rctl_qty_t limit;
55217c478bd9Sstevel@tonic-gate 	uint_t action;
55227c478bd9Sstevel@tonic-gate 
55237c478bd9Sstevel@tonic-gate 	/* Get the privilege */
55247c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
55257c478bd9Sstevel@tonic-gate 		priv = RCPRIV_BASIC;
55267c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
55277c478bd9Sstevel@tonic-gate 		priv = RCPRIV_PRIVILEGED;
55287c478bd9Sstevel@tonic-gate 	} else {
55297c478bd9Sstevel@tonic-gate 		/* Invalid privilege */
55307c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
55317c478bd9Sstevel@tonic-gate 	}
55327c478bd9Sstevel@tonic-gate 
55337c478bd9Sstevel@tonic-gate 	/* deal with negative input; strtoull(3c) doesn't do what we want */
55347c478bd9Sstevel@tonic-gate 	if (rctlval->zone_rctlval_limit[0] == '-')
55357c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
55367c478bd9Sstevel@tonic-gate 	/* Get the limit */
55377c478bd9Sstevel@tonic-gate 	errno = 0;
55387c478bd9Sstevel@tonic-gate 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
55397c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endp != '\0') {
55407c478bd9Sstevel@tonic-gate 		/* parse failed */
55417c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
55427c478bd9Sstevel@tonic-gate 	}
55437c478bd9Sstevel@tonic-gate 	limit = (rctl_qty_t)ull;
55447c478bd9Sstevel@tonic-gate 
55457c478bd9Sstevel@tonic-gate 	/* Get the action */
55467c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
55477c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_NOACTION;
55487c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
55497c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_SIGNAL;
55507c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
55517c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_DENY;
55527c478bd9Sstevel@tonic-gate 	} else {
55537c478bd9Sstevel@tonic-gate 		/* Invalid Action */
55547c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
55557c478bd9Sstevel@tonic-gate 	}
55567c478bd9Sstevel@tonic-gate 	rctlblk_set_local_action(rctlblk, action, 0);
55577c478bd9Sstevel@tonic-gate 	rctlblk_set_privilege(rctlblk, priv);
55587c478bd9Sstevel@tonic-gate 	rctlblk_set_value(rctlblk, limit);
55597c478bd9Sstevel@tonic-gate 	return (Z_OK);
55607c478bd9Sstevel@tonic-gate }
55617c478bd9Sstevel@tonic-gate 
55627c478bd9Sstevel@tonic-gate static int
55637c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg)
55647c478bd9Sstevel@tonic-gate {
55657c478bd9Sstevel@tonic-gate 	const char *attrname = arg;
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate 	/*
55687c478bd9Sstevel@tonic-gate 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
55697c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
55707c478bd9Sstevel@tonic-gate 	 */
55717c478bd9Sstevel@tonic-gate 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
55727c478bd9Sstevel@tonic-gate }
55737c478bd9Sstevel@tonic-gate 
55747c478bd9Sstevel@tonic-gate boolean_t
55757c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name)
55767c478bd9Sstevel@tonic-gate {
55777c478bd9Sstevel@tonic-gate 	return (rctl_walk(rctl_check, (void *)name) == 1);
55787c478bd9Sstevel@tonic-gate }
55797c478bd9Sstevel@tonic-gate 
55807c478bd9Sstevel@tonic-gate boolean_t
55817c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name)
55827c478bd9Sstevel@tonic-gate {
55837c478bd9Sstevel@tonic-gate 	const char *c;
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
55867c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55877c478bd9Sstevel@tonic-gate 	if (strlen(name) == sizeof ("zone.") - 1)
55887c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55897c478bd9Sstevel@tonic-gate 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
55907c478bd9Sstevel@tonic-gate 		if (!isalpha(*c) && *c != '-')
55917c478bd9Sstevel@tonic-gate 			return (B_FALSE);
55927c478bd9Sstevel@tonic-gate 	}
55937c478bd9Sstevel@tonic-gate 	return (B_TRUE);
55947c478bd9Sstevel@tonic-gate }
55957c478bd9Sstevel@tonic-gate 
55967c478bd9Sstevel@tonic-gate boolean_t
55977c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
55987c478bd9Sstevel@tonic-gate {
55997c478bd9Sstevel@tonic-gate 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
56007c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
56017c478bd9Sstevel@tonic-gate 
56027c478bd9Sstevel@tonic-gate 	if (priv != RCPRIV_PRIVILEGED)
56037c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56047c478bd9Sstevel@tonic-gate 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
56057c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56067c478bd9Sstevel@tonic-gate 	return (B_TRUE);
56077c478bd9Sstevel@tonic-gate }
56087c478bd9Sstevel@tonic-gate 
56097c478bd9Sstevel@tonic-gate boolean_t
56107c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
56117c478bd9Sstevel@tonic-gate {
56127c478bd9Sstevel@tonic-gate 	rctlblk_t *current, *next;
56137c478bd9Sstevel@tonic-gate 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
56147c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
56157c478bd9Sstevel@tonic-gate 	uint_t global_flags;
56167c478bd9Sstevel@tonic-gate 
56177c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
56187c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56197c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlname(name))
56207c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56217c478bd9Sstevel@tonic-gate 
56227c478bd9Sstevel@tonic-gate 	current = alloca(rctlblk_size());
56237c478bd9Sstevel@tonic-gate 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
56247c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* not an rctl on this system */
56257c478bd9Sstevel@tonic-gate 	/*
56267c478bd9Sstevel@tonic-gate 	 * Make sure the proposed value isn't greater than the current system
56277c478bd9Sstevel@tonic-gate 	 * value.
56287c478bd9Sstevel@tonic-gate 	 */
56297c478bd9Sstevel@tonic-gate 	next = alloca(rctlblk_size());
56307c478bd9Sstevel@tonic-gate 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
56317c478bd9Sstevel@tonic-gate 		rctlblk_t *tmp;
56327c478bd9Sstevel@tonic-gate 
56337c478bd9Sstevel@tonic-gate 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
56347c478bd9Sstevel@tonic-gate 			return (B_FALSE);	/* shouldn't happen */
56357c478bd9Sstevel@tonic-gate 		tmp = current;
56367c478bd9Sstevel@tonic-gate 		current = next;
56377c478bd9Sstevel@tonic-gate 		next = tmp;
56387c478bd9Sstevel@tonic-gate 	}
56397c478bd9Sstevel@tonic-gate 	if (limit > rctlblk_get_value(current))
56407c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56417c478bd9Sstevel@tonic-gate 
56427c478bd9Sstevel@tonic-gate 	/*
56437c478bd9Sstevel@tonic-gate 	 * Make sure the proposed action is allowed.
56447c478bd9Sstevel@tonic-gate 	 */
56457c478bd9Sstevel@tonic-gate 	global_flags = rctlblk_get_global_flags(current);
56467c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
56477c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_DENY)
56487c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56497c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
56507c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_NOACTION)
56517c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56527c478bd9Sstevel@tonic-gate 
56537c478bd9Sstevel@tonic-gate 	return (B_TRUE);
56547c478bd9Sstevel@tonic-gate }
5655fa9e4066Sahrens 
5656cf8f45c7Sdstaff /*
5657cf8f45c7Sdstaff  * There is always a race condition between reading the initial copy of
5658cf8f45c7Sdstaff  * a zones state and its state changing.  We address this by providing
5659cf8f45c7Sdstaff  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
5660cf8f45c7Sdstaff  * When zonecfg_critical_enter is called, sets the state field to LOCKED
5661cf8f45c7Sdstaff  * and aquires biglock. Biglock protects against other threads executing
5662cf8f45c7Sdstaff  * critical_enter and the state field protects against state changes during
5663cf8f45c7Sdstaff  * the critical period.
5664cf8f45c7Sdstaff  *
5665cf8f45c7Sdstaff  * If any state changes occur, zn_cb will set the failed field of the znotify
5666cf8f45c7Sdstaff  * structure.  This will cause the critical_exit function to re-lock the
5667cf8f45c7Sdstaff  * channel and return an error. Since evsnts may be delayed, the critical_exit
5668cf8f45c7Sdstaff  * function "flushes" the queue by putting an event on the queue and waiting for
5669cf8f45c7Sdstaff  * zn_cb to notify critical_exit that it received the ping event.
5670cf8f45c7Sdstaff  */
5671cf8f45c7Sdstaff static const char *
5672cf8f45c7Sdstaff string_get_tok(const char *in, char delim, int num)
5673cf8f45c7Sdstaff {
5674cf8f45c7Sdstaff 	int i = 0;
5675cf8f45c7Sdstaff 
5676cf8f45c7Sdstaff 	for (; i < num; in++) {
5677cf8f45c7Sdstaff 		if (*in == delim)
5678cf8f45c7Sdstaff 			i++;
5679cf8f45c7Sdstaff 		if (*in == 0)
5680cf8f45c7Sdstaff 			return (NULL);
5681cf8f45c7Sdstaff 	}
5682cf8f45c7Sdstaff 	return (in);
5683cf8f45c7Sdstaff }
5684cf8f45c7Sdstaff 
5685cf8f45c7Sdstaff static boolean_t
5686cf8f45c7Sdstaff is_ping(sysevent_t *ev)
5687cf8f45c7Sdstaff {
5688cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5689cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
5690cf8f45c7Sdstaff 		return (B_TRUE);
5691cf8f45c7Sdstaff 	} else {
5692cf8f45c7Sdstaff 		return (B_FALSE);
5693cf8f45c7Sdstaff 	}
5694cf8f45c7Sdstaff }
5695cf8f45c7Sdstaff 
5696cf8f45c7Sdstaff static boolean_t
5697cf8f45c7Sdstaff is_my_ping(sysevent_t *ev)
5698cf8f45c7Sdstaff {
5699cf8f45c7Sdstaff 	const char *sender;
5700cf8f45c7Sdstaff 	char mypid[sizeof (pid_t) * 3 + 1];
5701cf8f45c7Sdstaff 
5702cf8f45c7Sdstaff 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
5703cf8f45c7Sdstaff 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
5704cf8f45c7Sdstaff 	if (sender == NULL)
5705cf8f45c7Sdstaff 		return (B_FALSE);
5706cf8f45c7Sdstaff 	if (strcmp(sender, mypid) != 0)
5707cf8f45c7Sdstaff 		return (B_FALSE);
5708cf8f45c7Sdstaff 	return (B_TRUE);
5709cf8f45c7Sdstaff }
5710cf8f45c7Sdstaff 
5711cf8f45c7Sdstaff static int
5712cf8f45c7Sdstaff do_callback(struct znotify *zevtchan, sysevent_t *ev)
5713cf8f45c7Sdstaff {
5714cf8f45c7Sdstaff 	nvlist_t *l;
5715cf8f45c7Sdstaff 	int zid;
5716cf8f45c7Sdstaff 	char *zonename;
5717cf8f45c7Sdstaff 	char *newstate;
5718cf8f45c7Sdstaff 	char *oldstate;
5719cf8f45c7Sdstaff 	int ret;
5720cf8f45c7Sdstaff 	hrtime_t when;
5721cf8f45c7Sdstaff 
5722cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5723cf8f45c7Sdstaff 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
5724cf8f45c7Sdstaff 
5725cf8f45c7Sdstaff 		if (sysevent_get_attr_list(ev, &l) != 0) {
5726cf8f45c7Sdstaff 			if (errno == ENOMEM) {
5727cf8f45c7Sdstaff 				zevtchan->zn_failure_count++;
5728cf8f45c7Sdstaff 				return (EAGAIN);
5729cf8f45c7Sdstaff 			}
5730cf8f45c7Sdstaff 			return (0);
5731cf8f45c7Sdstaff 		}
5732cf8f45c7Sdstaff 		ret = 0;
5733cf8f45c7Sdstaff 
5734cf8f45c7Sdstaff 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
5735cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
5736cf8f45c7Sdstaff 		    == 0) &&
5737cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
5738cf8f45c7Sdstaff 		    == 0) &&
5739cf8f45c7Sdstaff 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
5740cf8f45c7Sdstaff 		    (uint64_t *)&when) == 0) &&
5741cf8f45c7Sdstaff 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
5742cf8f45c7Sdstaff 			ret = zevtchan->zn_callback(zonename, zid, newstate,
5743cf8f45c7Sdstaff 			    oldstate, when, zevtchan->zn_private);
5744cf8f45c7Sdstaff 		}
5745cf8f45c7Sdstaff 
5746cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5747cf8f45c7Sdstaff 		nvlist_free(l);
5748cf8f45c7Sdstaff 		return (ret);
5749cf8f45c7Sdstaff 	} else {
5750cf8f45c7Sdstaff 		/*
5751cf8f45c7Sdstaff 		 * We have received an event in an unknown subclass. Ignore.
5752cf8f45c7Sdstaff 		 */
5753cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5754cf8f45c7Sdstaff 		return (0);
5755cf8f45c7Sdstaff 	}
5756cf8f45c7Sdstaff }
5757cf8f45c7Sdstaff 
5758cf8f45c7Sdstaff static int
5759cf8f45c7Sdstaff zn_cb(sysevent_t *ev, void *p)
5760cf8f45c7Sdstaff {
5761cf8f45c7Sdstaff 	struct znotify *zevtchan = p;
5762cf8f45c7Sdstaff 	int error;
5763cf8f45c7Sdstaff 
5764cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5765cf8f45c7Sdstaff 
5766cf8f45c7Sdstaff 	if (is_ping(ev) && !is_my_ping(ev)) {
5767cf8f45c7Sdstaff 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
5768cf8f45c7Sdstaff 		return (0);
5769cf8f45c7Sdstaff 	}
5770cf8f45c7Sdstaff 
5771cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_LOCKED) {
5772cf8f45c7Sdstaff 		assert(!is_ping(ev));
5773cf8f45c7Sdstaff 		zevtchan->zn_failed = B_TRUE;
5774cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5775cf8f45c7Sdstaff 		return (0);
5776cf8f45c7Sdstaff 	}
5777cf8f45c7Sdstaff 
5778cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
5779cf8f45c7Sdstaff 		if (is_ping(ev)) {
5780cf8f45c7Sdstaff 			zevtchan->zn_state = ZN_PING_RECEIVED;
5781cf8f45c7Sdstaff 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
5782cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5783cf8f45c7Sdstaff 			return (0);
5784cf8f45c7Sdstaff 		} else {
5785cf8f45c7Sdstaff 			zevtchan->zn_failed = B_TRUE;
5786cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5787cf8f45c7Sdstaff 			return (0);
5788cf8f45c7Sdstaff 		}
5789cf8f45c7Sdstaff 	}
5790cf8f45c7Sdstaff 
5791cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED) {
5792cf8f45c7Sdstaff 
5793cf8f45c7Sdstaff 		error = do_callback(zevtchan, ev);
5794cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5795cf8f45c7Sdstaff 		/*
5796cf8f45c7Sdstaff 		 * Every ENOMEM failure causes do_callback to increment
5797cf8f45c7Sdstaff 		 * zn_failure_count and every success causes it to
5798cf8f45c7Sdstaff 		 * set zn_failure_count to zero.  If we got EAGAIN,
5799cf8f45c7Sdstaff 		 * we will sleep for zn_failure_count seconds and return
5800cf8f45c7Sdstaff 		 * EAGAIN to gpec to try again.
5801cf8f45c7Sdstaff 		 *
5802cf8f45c7Sdstaff 		 * After 55 seconds, or 10 try's we give up and drop the
5803cf8f45c7Sdstaff 		 * event.
5804cf8f45c7Sdstaff 		 */
5805cf8f45c7Sdstaff 		if (error == EAGAIN) {
5806cf8f45c7Sdstaff 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
5807cf8f45c7Sdstaff 				return (0);
5808cf8f45c7Sdstaff 			}
5809cf8f45c7Sdstaff 			(void) sleep(zevtchan->zn_failure_count);
5810cf8f45c7Sdstaff 		}
5811cf8f45c7Sdstaff 		return (error);
5812cf8f45c7Sdstaff 	}
5813cf8f45c7Sdstaff 
5814cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
5815cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5816cf8f45c7Sdstaff 		return (0);
5817cf8f45c7Sdstaff 	}
5818cf8f45c7Sdstaff 
5819cf8f45c7Sdstaff 	abort();
5820cf8f45c7Sdstaff 	return (0);
5821cf8f45c7Sdstaff }
5822cf8f45c7Sdstaff 
5823cf8f45c7Sdstaff void
5824cf8f45c7Sdstaff zonecfg_notify_critical_enter(void *h)
5825cf8f45c7Sdstaff {
5826cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5827cf8f45c7Sdstaff 
5828cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
5829cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_LOCKED;
5830cf8f45c7Sdstaff }
5831cf8f45c7Sdstaff 
5832cf8f45c7Sdstaff int
5833cf8f45c7Sdstaff zonecfg_notify_critical_exit(void * h)
5834cf8f45c7Sdstaff {
5835cf8f45c7Sdstaff 
5836cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5837cf8f45c7Sdstaff 
5838cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED)
5839cf8f45c7Sdstaff 		return (0);
5840cf8f45c7Sdstaff 
5841cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5842cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_PING_INFLIGHT;
5843cf8f45c7Sdstaff 
5844ee519a1fSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
5845ee519a1fSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
5846cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
5847cf8f45c7Sdstaff 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
5848cf8f45c7Sdstaff 
5849cf8f45c7Sdstaff 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
5850cf8f45c7Sdstaff 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
5851cf8f45c7Sdstaff 		    &(zevtchan->zn_mutex));
5852cf8f45c7Sdstaff 	}
5853cf8f45c7Sdstaff 
5854cf8f45c7Sdstaff 	if (zevtchan->zn_failed == B_TRUE) {
5855cf8f45c7Sdstaff 		zevtchan->zn_state = ZN_LOCKED;
5856cf8f45c7Sdstaff 		zevtchan->zn_failed = B_FALSE;
5857cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5858cf8f45c7Sdstaff 		return (1);
5859cf8f45c7Sdstaff 	}
5860cf8f45c7Sdstaff 
5861cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5862cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5863cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5864cf8f45c7Sdstaff 	return (0);
5865cf8f45c7Sdstaff }
5866cf8f45c7Sdstaff 
5867cf8f45c7Sdstaff void
5868cf8f45c7Sdstaff zonecfg_notify_critical_abort(void *h)
5869cf8f45c7Sdstaff {
5870cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5871cf8f45c7Sdstaff 
5872cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5873cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
5874cf8f45c7Sdstaff 	/*
5875cf8f45c7Sdstaff 	 * Don't do anything about zn_lock. If it is held, it could only be
5876cf8f45c7Sdstaff 	 * held by zn_cb and it will be unlocked soon.
5877cf8f45c7Sdstaff 	 */
5878cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5879cf8f45c7Sdstaff }
5880cf8f45c7Sdstaff 
5881cf8f45c7Sdstaff void *
5882cf8f45c7Sdstaff zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
5883cf8f45c7Sdstaff     const char *newstate, const char *oldstate, hrtime_t when, void *p),
5884cf8f45c7Sdstaff     void *p)
5885cf8f45c7Sdstaff {
5886cf8f45c7Sdstaff 	struct znotify *zevtchan;
5887cf8f45c7Sdstaff 	int i = 1;
5888cf8f45c7Sdstaff 	int r;
5889cf8f45c7Sdstaff 
5890cf8f45c7Sdstaff 	zevtchan = malloc(sizeof (struct znotify));
5891cf8f45c7Sdstaff 
5892cf8f45c7Sdstaff 	if (zevtchan == NULL)
5893cf8f45c7Sdstaff 		return (NULL);
5894cf8f45c7Sdstaff 
5895cf8f45c7Sdstaff 	zevtchan->zn_private = p;
5896cf8f45c7Sdstaff 	zevtchan->zn_callback = func;
5897cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5898cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
5899cf8f45c7Sdstaff 
5900cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
59019d4be64eSdstaff 		goto out3;
5902cf8f45c7Sdstaff 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
5903cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
59049d4be64eSdstaff 		goto out3;
5905cf8f45c7Sdstaff 	}
5906cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
5907cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
5908cf8f45c7Sdstaff 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
59099d4be64eSdstaff 		goto out3;
5910cf8f45c7Sdstaff 	}
5911cf8f45c7Sdstaff 
5912cf8f45c7Sdstaff 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
5913cf8f45c7Sdstaff 	    0) != 0)
5914cf8f45c7Sdstaff 		goto out2;
5915cf8f45c7Sdstaff 
5916cf8f45c7Sdstaff 	do {
5917cf8f45c7Sdstaff 		/*
5918cf8f45c7Sdstaff 		 * At 4 digits the subscriber ID gets too long and we have
5919cf8f45c7Sdstaff 		 * no chance of successfully registering.
5920cf8f45c7Sdstaff 		 */
5921cf8f45c7Sdstaff 		if (i > 999)
59229d4be64eSdstaff 			goto out1;
5923cf8f45c7Sdstaff 
5924cf8f45c7Sdstaff 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
5925cf8f45c7Sdstaff 		    getpid() % 999999l, i);
5926cf8f45c7Sdstaff 
5927cf8f45c7Sdstaff 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
5928cf8f45c7Sdstaff 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
5929cf8f45c7Sdstaff 		    zevtchan, 0);
5930cf8f45c7Sdstaff 
5931cf8f45c7Sdstaff 		i++;
5932cf8f45c7Sdstaff 
5933cf8f45c7Sdstaff 	} while (r);
5934cf8f45c7Sdstaff 
5935cf8f45c7Sdstaff 	return (zevtchan);
59369d4be64eSdstaff out1:
5937cf8f45c7Sdstaff 	sysevent_evc_unbind(zevtchan->zn_eventchan);
59389d4be64eSdstaff out2:
5939cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
5940cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
5941cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
59429d4be64eSdstaff out3:
5943cf8f45c7Sdstaff 	free(zevtchan);
5944cf8f45c7Sdstaff 
5945cf8f45c7Sdstaff 	return (NULL);
5946cf8f45c7Sdstaff }
5947cf8f45c7Sdstaff 
5948cf8f45c7Sdstaff void
5949cf8f45c7Sdstaff zonecfg_notify_unbind(void *handle)
5950cf8f45c7Sdstaff {
5951cf8f45c7Sdstaff 
5952cf8f45c7Sdstaff 	int ret;
5953cf8f45c7Sdstaff 
5954cf8f45c7Sdstaff 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
5955cf8f45c7Sdstaff 	/*
5956cf8f45c7Sdstaff 	 * Check that all evc threads have gone away. This should be
5957cf8f45c7Sdstaff 	 * enforced by sysevent_evc_unbind.
5958cf8f45c7Sdstaff 	 */
5959cf8f45c7Sdstaff 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
5960cf8f45c7Sdstaff 
5961cf8f45c7Sdstaff 	if (ret)
5962cf8f45c7Sdstaff 		abort();
5963cf8f45c7Sdstaff 
5964cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
5965cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
5966cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
5967cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
5968cf8f45c7Sdstaff 
5969cf8f45c7Sdstaff 	free(handle);
5970cf8f45c7Sdstaff }
5971cf8f45c7Sdstaff 
5972fa9e4066Sahrens static int
5973fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5974fa9e4066Sahrens {
5975fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
5976fa9e4066Sahrens 	int err;
5977fa9e4066Sahrens 
5978fa9e4066Sahrens 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
5979fa9e4066Sahrens 	if ((err = newprop(newnode, DTD_ATTR_NAME,
5980fa9e4066Sahrens 	    tabptr->zone_dataset_name)) != Z_OK)
5981fa9e4066Sahrens 		return (err);
5982fa9e4066Sahrens 	return (Z_OK);
5983fa9e4066Sahrens }
5984fa9e4066Sahrens 
5985fa9e4066Sahrens int
5986fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5987fa9e4066Sahrens {
5988fa9e4066Sahrens 	int err;
5989fa9e4066Sahrens 
5990fa9e4066Sahrens 	if (tabptr == NULL)
5991fa9e4066Sahrens 		return (Z_INVAL);
5992fa9e4066Sahrens 
5993fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5994fa9e4066Sahrens 		return (err);
5995fa9e4066Sahrens 
5996fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
5997fa9e4066Sahrens 		return (err);
5998fa9e4066Sahrens 
5999fa9e4066Sahrens 	return (Z_OK);
6000fa9e4066Sahrens }
6001fa9e4066Sahrens 
6002fa9e4066Sahrens static int
6003fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
6004fa9e4066Sahrens {
6005fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
6006fa9e4066Sahrens 
6007fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6008fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6009fa9e4066Sahrens 			continue;
6010fa9e4066Sahrens 
6011fa9e4066Sahrens 		if (match_prop(cur, DTD_ATTR_NAME,
6012fa9e4066Sahrens 		    tabptr->zone_dataset_name)) {
6013fa9e4066Sahrens 			xmlUnlinkNode(cur);
6014fa9e4066Sahrens 			xmlFreeNode(cur);
6015fa9e4066Sahrens 			return (Z_OK);
6016fa9e4066Sahrens 		}
6017fa9e4066Sahrens 	}
6018fa9e4066Sahrens 	return (Z_NO_RESOURCE_ID);
6019fa9e4066Sahrens }
6020fa9e4066Sahrens 
6021fa9e4066Sahrens int
6022fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6023fa9e4066Sahrens {
6024fa9e4066Sahrens 	int err;
6025fa9e4066Sahrens 
6026fa9e4066Sahrens 	if (tabptr == NULL)
6027fa9e4066Sahrens 		return (Z_INVAL);
6028fa9e4066Sahrens 
6029fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6030fa9e4066Sahrens 		return (err);
6031fa9e4066Sahrens 
6032fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
6033fa9e4066Sahrens 		return (err);
6034fa9e4066Sahrens 
6035fa9e4066Sahrens 	return (Z_OK);
6036fa9e4066Sahrens }
6037fa9e4066Sahrens 
6038fa9e4066Sahrens int
6039fa9e4066Sahrens zonecfg_modify_ds(
6040fa9e4066Sahrens 	zone_dochandle_t handle,
6041fa9e4066Sahrens 	struct zone_dstab *oldtabptr,
6042fa9e4066Sahrens 	struct zone_dstab *newtabptr)
6043fa9e4066Sahrens {
6044fa9e4066Sahrens 	int err;
6045fa9e4066Sahrens 
6046fa9e4066Sahrens 	if (oldtabptr == NULL || newtabptr == NULL)
6047fa9e4066Sahrens 		return (Z_INVAL);
6048fa9e4066Sahrens 
6049fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6050fa9e4066Sahrens 		return (err);
6051fa9e4066Sahrens 
6052fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
6053fa9e4066Sahrens 		return (err);
6054fa9e4066Sahrens 
6055fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
6056fa9e4066Sahrens 		return (err);
6057fa9e4066Sahrens 
6058fa9e4066Sahrens 	return (Z_OK);
6059fa9e4066Sahrens }
6060fa9e4066Sahrens 
6061fa9e4066Sahrens int
6062fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6063fa9e4066Sahrens {
6064fa9e4066Sahrens 	xmlNodePtr cur, firstmatch;
6065fa9e4066Sahrens 	int err;
6066fa9e4066Sahrens 	char dataset[MAXNAMELEN];
6067fa9e4066Sahrens 
6068fa9e4066Sahrens 	if (tabptr == NULL)
6069fa9e4066Sahrens 		return (Z_INVAL);
6070fa9e4066Sahrens 
6071fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6072fa9e4066Sahrens 		return (err);
6073fa9e4066Sahrens 
6074fa9e4066Sahrens 	cur = handle->zone_dh_cur;
6075fa9e4066Sahrens 	firstmatch = NULL;
6076fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6077fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6078fa9e4066Sahrens 			continue;
6079fa9e4066Sahrens 		if (strlen(tabptr->zone_dataset_name) > 0) {
6080fa9e4066Sahrens 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
6081fa9e4066Sahrens 			    sizeof (dataset)) == Z_OK) &&
6082fa9e4066Sahrens 			    (strcmp(tabptr->zone_dataset_name,
6083fa9e4066Sahrens 			    dataset) == 0)) {
6084fa9e4066Sahrens 				if (firstmatch == NULL)
6085fa9e4066Sahrens 					firstmatch = cur;
6086fa9e4066Sahrens 				else
6087fa9e4066Sahrens 					return (Z_INSUFFICIENT_SPEC);
6088fa9e4066Sahrens 			}
6089fa9e4066Sahrens 		}
6090fa9e4066Sahrens 	}
6091fa9e4066Sahrens 	if (firstmatch == NULL)
6092fa9e4066Sahrens 		return (Z_NO_RESOURCE_ID);
6093fa9e4066Sahrens 
6094fa9e4066Sahrens 	cur = firstmatch;
6095fa9e4066Sahrens 
6096fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6097fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
6098fa9e4066Sahrens 		return (err);
6099fa9e4066Sahrens 
6100fa9e4066Sahrens 	return (Z_OK);
6101fa9e4066Sahrens }
6102fa9e4066Sahrens 
6103fa9e4066Sahrens int
6104fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle)
6105fa9e4066Sahrens {
6106fa9e4066Sahrens 	return (zonecfg_setent(handle));
6107fa9e4066Sahrens }
6108fa9e4066Sahrens 
6109fa9e4066Sahrens int
6110fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
6111fa9e4066Sahrens {
6112fa9e4066Sahrens 	xmlNodePtr cur;
6113fa9e4066Sahrens 	int err;
6114fa9e4066Sahrens 
6115fa9e4066Sahrens 	if (handle == NULL)
6116fa9e4066Sahrens 		return (Z_INVAL);
6117fa9e4066Sahrens 
6118fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
6119fa9e4066Sahrens 		return (Z_NO_ENTRY);
6120fa9e4066Sahrens 
6121fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
6122fa9e4066Sahrens 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6123fa9e4066Sahrens 			break;
6124fa9e4066Sahrens 	if (cur == NULL) {
6125fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6126fa9e4066Sahrens 		return (Z_NO_ENTRY);
6127fa9e4066Sahrens 	}
6128fa9e4066Sahrens 
6129fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6130fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
6131fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6132fa9e4066Sahrens 		return (err);
6133fa9e4066Sahrens 	}
6134fa9e4066Sahrens 
6135fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
6136fa9e4066Sahrens 	return (Z_OK);
6137fa9e4066Sahrens }
6138fa9e4066Sahrens 
6139fa9e4066Sahrens int
6140fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle)
6141fa9e4066Sahrens {
6142fa9e4066Sahrens 	return (zonecfg_endent(handle));
6143fa9e4066Sahrens }
6144ee519a1fSgjelinek 
61450209230bSgjelinek /*
61460209230bSgjelinek  * Support for aliased rctls; that is, rctls that have simplified names in
61470209230bSgjelinek  * zonecfg.  For example, max-lwps is an alias for a well defined zone.max-lwps
61480209230bSgjelinek  * rctl.  If there are multiple existing values for one of these rctls or if
61490209230bSgjelinek  * there is a single value that does not match the well defined template (i.e.
61500209230bSgjelinek  * it has a different action) then we cannot treat the rctl as having an alias
61510209230bSgjelinek  * so we return Z_ALIAS_DISALLOW.  That means that the rctl cannot be
61520209230bSgjelinek  * managed in zonecfg via an alias and that the standard rctl syntax must be
61530209230bSgjelinek  * used.
61540209230bSgjelinek  *
61550209230bSgjelinek  * The possible return values are:
61560209230bSgjelinek  *	Z_NO_PROPERTY_ID - invalid alias name
61570209230bSgjelinek  *	Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition
61580209230bSgjelinek  *	Z_NO_ENTRY - no rctl is configured for this alias
61590209230bSgjelinek  *	Z_OK - we got a valid rctl for the specified alias
61600209230bSgjelinek  */
61610209230bSgjelinek int
61620209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval)
61630209230bSgjelinek {
61640209230bSgjelinek 	boolean_t found = B_FALSE;
61650209230bSgjelinek 	boolean_t found_val = B_FALSE;
61660209230bSgjelinek 	xmlNodePtr cur, val;
61670209230bSgjelinek 	char savedname[MAXNAMELEN];
61680209230bSgjelinek 	struct zone_rctlvaltab rctl;
61690209230bSgjelinek 	int i;
61700209230bSgjelinek 	int err;
61710209230bSgjelinek 
61720209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
61730209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
61740209230bSgjelinek 			break;
61750209230bSgjelinek 
61760209230bSgjelinek 	if (aliases[i].shortname == NULL)
61770209230bSgjelinek 		return (Z_NO_PROPERTY_ID);
61780209230bSgjelinek 
61790209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
61800209230bSgjelinek 		return (err);
61810209230bSgjelinek 
61820209230bSgjelinek 	cur = handle->zone_dh_cur;
61830209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
61840209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0)
61850209230bSgjelinek 			continue;
61860209230bSgjelinek 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
61870209230bSgjelinek 		    sizeof (savedname)) == Z_OK) &&
61880209230bSgjelinek 		    (strcmp(savedname, aliases[i].realname) == 0)) {
61890209230bSgjelinek 
61900209230bSgjelinek 			/*
61910209230bSgjelinek 			 * If we already saw one of these, we can't have an
61920209230bSgjelinek 			 * alias since we just found another.
61930209230bSgjelinek 			 */
61940209230bSgjelinek 			if (found)
61950209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
61960209230bSgjelinek 			found = B_TRUE;
61970209230bSgjelinek 
61980209230bSgjelinek 			for (val = cur->xmlChildrenNode; val != NULL;
61990209230bSgjelinek 			    val = val->next) {
62000209230bSgjelinek 				/*
62010209230bSgjelinek 				 * If we already have one value, we can't have
62020209230bSgjelinek 				 * an alias since we just found another.
62030209230bSgjelinek 				 */
62040209230bSgjelinek 				if (found_val)
62050209230bSgjelinek 					return (Z_ALIAS_DISALLOW);
62060209230bSgjelinek 				found_val = B_TRUE;
62070209230bSgjelinek 
62080209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_PRIV,
62090209230bSgjelinek 				    rctl.zone_rctlval_priv,
62100209230bSgjelinek 				    sizeof (rctl.zone_rctlval_priv)) != Z_OK))
62110209230bSgjelinek 					break;
62120209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_LIMIT,
62130209230bSgjelinek 				    rctl.zone_rctlval_limit,
62140209230bSgjelinek 				    sizeof (rctl.zone_rctlval_limit)) != Z_OK))
62150209230bSgjelinek 					break;
62160209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_ACTION,
62170209230bSgjelinek 				    rctl.zone_rctlval_action,
62180209230bSgjelinek 				    sizeof (rctl.zone_rctlval_action)) != Z_OK))
62190209230bSgjelinek 					break;
62200209230bSgjelinek 			}
62210209230bSgjelinek 
62220209230bSgjelinek 			/* check priv and action match the expected vals */
62230209230bSgjelinek 			if (strcmp(rctl.zone_rctlval_priv,
62240209230bSgjelinek 			    aliases[i].priv) != 0 ||
62250209230bSgjelinek 			    strcmp(rctl.zone_rctlval_action,
62260209230bSgjelinek 			    aliases[i].action) != 0)
62270209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
62280209230bSgjelinek 		}
62290209230bSgjelinek 	}
62300209230bSgjelinek 
62310209230bSgjelinek 	if (found) {
62320209230bSgjelinek 		*rval = strtoull(rctl.zone_rctlval_limit, NULL, 10);
62330209230bSgjelinek 		return (Z_OK);
62340209230bSgjelinek 	}
62350209230bSgjelinek 
62360209230bSgjelinek 	return (Z_NO_ENTRY);
62370209230bSgjelinek }
62380209230bSgjelinek 
62390209230bSgjelinek int
62400209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name)
62410209230bSgjelinek {
62420209230bSgjelinek 	int i;
62430209230bSgjelinek 	uint64_t val;
62440209230bSgjelinek 	struct zone_rctltab rctltab;
62450209230bSgjelinek 
62460209230bSgjelinek 	/*
62470209230bSgjelinek 	 * First check that we have a valid aliased rctl to remove.
62480209230bSgjelinek 	 * This will catch an rctl entry with non-standard values or
62490209230bSgjelinek 	 * multiple rctl values for this name.  We need to ignore those
62500209230bSgjelinek 	 * rctl entries.
62510209230bSgjelinek 	 */
62520209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK)
62530209230bSgjelinek 		return (Z_OK);
62540209230bSgjelinek 
62550209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
62560209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
62570209230bSgjelinek 			break;
62580209230bSgjelinek 
62590209230bSgjelinek 	if (aliases[i].shortname == NULL)
62600209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
62610209230bSgjelinek 
62620209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
62630209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
62640209230bSgjelinek 
62650209230bSgjelinek 	return (zonecfg_delete_rctl(handle, &rctltab));
62660209230bSgjelinek }
62670209230bSgjelinek 
62680209230bSgjelinek boolean_t
62690209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name)
62700209230bSgjelinek {
62710209230bSgjelinek 	uint64_t tmp_val;
62720209230bSgjelinek 
62730209230bSgjelinek 	switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) {
62740209230bSgjelinek 	case Z_OK:
62750209230bSgjelinek 		/*FALLTHRU*/
62760209230bSgjelinek 	case Z_NO_ENTRY:
62770209230bSgjelinek 		return (B_TRUE);
62780209230bSgjelinek 	default:
62790209230bSgjelinek 		return (B_FALSE);
62800209230bSgjelinek 	}
62810209230bSgjelinek }
62820209230bSgjelinek 
62830209230bSgjelinek int
62840209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val)
62850209230bSgjelinek {
62860209230bSgjelinek 	int i;
62870209230bSgjelinek 	int err;
62880209230bSgjelinek 	struct zone_rctltab rctltab;
62890209230bSgjelinek 	struct zone_rctlvaltab *rctlvaltab;
62900209230bSgjelinek 	char buf[128];
62910209230bSgjelinek 
62920209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, name))
62930209230bSgjelinek 		return (Z_ALIAS_DISALLOW);
62940209230bSgjelinek 
62950209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
62960209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
62970209230bSgjelinek 			break;
62980209230bSgjelinek 
62990209230bSgjelinek 	if (aliases[i].shortname == NULL)
63000209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
63010209230bSgjelinek 
63020209230bSgjelinek 	/* remove any pre-existing definition for this rctl */
63030209230bSgjelinek 	(void) zonecfg_rm_aliased_rctl(handle, name);
63040209230bSgjelinek 
63050209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
63060209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
63070209230bSgjelinek 
63080209230bSgjelinek 	rctltab.zone_rctl_valptr = NULL;
63090209230bSgjelinek 
63100209230bSgjelinek 	if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL)
63110209230bSgjelinek 		return (Z_NOMEM);
63120209230bSgjelinek 
63130209230bSgjelinek 	(void) snprintf(buf, sizeof (buf), "%llu", (long long)val);
63140209230bSgjelinek 
63150209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv,
63160209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_priv));
63170209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_limit, buf,
63180209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_limit));
63190209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action,
63200209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_action));
63210209230bSgjelinek 
63220209230bSgjelinek 	rctlvaltab->zone_rctlval_next = NULL;
63230209230bSgjelinek 
63240209230bSgjelinek 	if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK)
63250209230bSgjelinek 		return (err);
63260209230bSgjelinek 
63270209230bSgjelinek 	return (zonecfg_add_rctl(handle, &rctltab));
63280209230bSgjelinek }
63290209230bSgjelinek 
63300209230bSgjelinek static int
63310209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle)
63320209230bSgjelinek {
63330209230bSgjelinek 	int err;
63340209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
63350209230bSgjelinek 
63360209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
63370209230bSgjelinek 		return (err);
63380209230bSgjelinek 
63390209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
63400209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
63410209230bSgjelinek 			xmlUnlinkNode(cur);
63420209230bSgjelinek 			xmlFreeNode(cur);
63430209230bSgjelinek 			return (Z_OK);
63440209230bSgjelinek 		}
63450209230bSgjelinek 	}
63460209230bSgjelinek 
63470209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
63480209230bSgjelinek }
63490209230bSgjelinek 
63500209230bSgjelinek static int
63510209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance)
63520209230bSgjelinek {
63530209230bSgjelinek 	int err;
63540209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
63550209230bSgjelinek 	xmlNodePtr newnode;
63560209230bSgjelinek 
63570209230bSgjelinek 	err = delete_tmp_pool(handle);
63580209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
63590209230bSgjelinek 		return (err);
63600209230bSgjelinek 
63610209230bSgjelinek 	if (*pool_importance != '\0') {
63620209230bSgjelinek 		if ((err = operation_prep(handle)) != Z_OK)
63630209230bSgjelinek 			return (err);
63640209230bSgjelinek 
63650209230bSgjelinek 		newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL);
63660209230bSgjelinek 		if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE,
63670209230bSgjelinek 		    pool_importance)) != Z_OK)
63680209230bSgjelinek 			return (err);
63690209230bSgjelinek 	}
63700209230bSgjelinek 
63710209230bSgjelinek 	return (Z_OK);
63720209230bSgjelinek }
63730209230bSgjelinek 
63740209230bSgjelinek static int
63750209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr)
63760209230bSgjelinek {
63770209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
63780209230bSgjelinek 	int err;
63790209230bSgjelinek 
63800209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL);
63810209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN,
63820209230bSgjelinek 	    tabptr->zone_ncpu_min)) != Z_OK)
63830209230bSgjelinek 		return (err);
63840209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX,
63850209230bSgjelinek 	    tabptr->zone_ncpu_max)) != Z_OK)
63860209230bSgjelinek 		return (err);
63870209230bSgjelinek 
63880209230bSgjelinek 	if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK)
63890209230bSgjelinek 		return (err);
63900209230bSgjelinek 
63910209230bSgjelinek 	return (Z_OK);
63920209230bSgjelinek }
63930209230bSgjelinek 
63940209230bSgjelinek int
63950209230bSgjelinek zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
63960209230bSgjelinek {
63970209230bSgjelinek 	int err;
63980209230bSgjelinek 
63990209230bSgjelinek 	if (tabptr == NULL)
64000209230bSgjelinek 		return (Z_INVAL);
64010209230bSgjelinek 
64020209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64030209230bSgjelinek 		return (err);
64040209230bSgjelinek 
64050209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
64060209230bSgjelinek 		return (err);
64070209230bSgjelinek 
64080209230bSgjelinek 	return (Z_OK);
64090209230bSgjelinek }
64100209230bSgjelinek 
64110209230bSgjelinek int
64120209230bSgjelinek zonecfg_delete_pset(zone_dochandle_t handle)
64130209230bSgjelinek {
64140209230bSgjelinek 	int err;
64150209230bSgjelinek 	int res = Z_NO_RESOURCE_ID;
64160209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
64170209230bSgjelinek 
64180209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64190209230bSgjelinek 		return (err);
64200209230bSgjelinek 
64210209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64220209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
64230209230bSgjelinek 			xmlUnlinkNode(cur);
64240209230bSgjelinek 			xmlFreeNode(cur);
64250209230bSgjelinek 			res = Z_OK;
64260209230bSgjelinek 			break;
64270209230bSgjelinek 		}
64280209230bSgjelinek 	}
64290209230bSgjelinek 
64300209230bSgjelinek 	/*
64310209230bSgjelinek 	 * Once we have msets, we should check that a mset
64320209230bSgjelinek 	 * do not exist before we delete the tmp_pool data.
64330209230bSgjelinek 	 */
64340209230bSgjelinek 	err = delete_tmp_pool(handle);
64350209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
64360209230bSgjelinek 		return (err);
64370209230bSgjelinek 
64380209230bSgjelinek 	return (res);
64390209230bSgjelinek }
64400209230bSgjelinek 
64410209230bSgjelinek int
64420209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
64430209230bSgjelinek {
64440209230bSgjelinek 	int err;
64450209230bSgjelinek 
64460209230bSgjelinek 	if (tabptr == NULL)
64470209230bSgjelinek 		return (Z_INVAL);
64480209230bSgjelinek 
64490209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
64500209230bSgjelinek 		return (err);
64510209230bSgjelinek 
64520209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
64530209230bSgjelinek 		return (err);
64540209230bSgjelinek 
64550209230bSgjelinek 	return (Z_OK);
64560209230bSgjelinek }
64570209230bSgjelinek 
64580209230bSgjelinek int
64590209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
64600209230bSgjelinek {
64610209230bSgjelinek 	xmlNodePtr cur;
64620209230bSgjelinek 	int err;
64630209230bSgjelinek 	int res = Z_NO_ENTRY;
64640209230bSgjelinek 
64650209230bSgjelinek 	if (tabptr == NULL)
64660209230bSgjelinek 		return (Z_INVAL);
64670209230bSgjelinek 
64680209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64690209230bSgjelinek 		return (err);
64700209230bSgjelinek 
64710209230bSgjelinek 	/* this is an optional component */
64720209230bSgjelinek 	tabptr->zone_importance[0] = '\0';
64730209230bSgjelinek 
64740209230bSgjelinek 	cur = handle->zone_dh_cur;
64750209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64760209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
64770209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN,
64780209230bSgjelinek 			    tabptr->zone_ncpu_min,
64790209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_min))) != Z_OK) {
64800209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
64810209230bSgjelinek 				return (err);
64820209230bSgjelinek 			}
64830209230bSgjelinek 
64840209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX,
64850209230bSgjelinek 			    tabptr->zone_ncpu_max,
64860209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_max))) != Z_OK) {
64870209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
64880209230bSgjelinek 				return (err);
64890209230bSgjelinek 			}
64900209230bSgjelinek 
64910209230bSgjelinek 			res = Z_OK;
64920209230bSgjelinek 
64930209230bSgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
64940209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE,
64950209230bSgjelinek 			    tabptr->zone_importance,
64960209230bSgjelinek 			    sizeof (tabptr->zone_importance))) != Z_OK) {
64970209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
64980209230bSgjelinek 				return (err);
64990209230bSgjelinek 			}
65000209230bSgjelinek 		}
65010209230bSgjelinek 	}
65020209230bSgjelinek 
65030209230bSgjelinek 	return (res);
65040209230bSgjelinek }
65050209230bSgjelinek 
65060209230bSgjelinek int
65070209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr)
65080209230bSgjelinek {
65090209230bSgjelinek 	int err;
65100209230bSgjelinek 
65110209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
65120209230bSgjelinek 		return (err);
65130209230bSgjelinek 
65140209230bSgjelinek 	err = zonecfg_lookup_pset(handle, tabptr);
65150209230bSgjelinek 
65160209230bSgjelinek 	(void) zonecfg_endent(handle);
65170209230bSgjelinek 
65180209230bSgjelinek 	return (err);
65190209230bSgjelinek }
65200209230bSgjelinek 
65210209230bSgjelinek static int
65220209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
65230209230bSgjelinek {
65240209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
65250209230bSgjelinek 	int err;
65260209230bSgjelinek 
65270209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL);
65280209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap))
65290209230bSgjelinek 	    != Z_OK)
65300209230bSgjelinek 		return (err);
65310209230bSgjelinek 
65320209230bSgjelinek 	return (Z_OK);
65330209230bSgjelinek }
65340209230bSgjelinek 
65350209230bSgjelinek int
65360209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle)
65370209230bSgjelinek {
65380209230bSgjelinek 	int err;
65390209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
65400209230bSgjelinek 
65410209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
65420209230bSgjelinek 		return (err);
65430209230bSgjelinek 
65440209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
65450209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
65460209230bSgjelinek 			continue;
65470209230bSgjelinek 
65480209230bSgjelinek 		xmlUnlinkNode(cur);
65490209230bSgjelinek 		xmlFreeNode(cur);
65500209230bSgjelinek 		return (Z_OK);
65510209230bSgjelinek 	}
65520209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
65530209230bSgjelinek }
65540209230bSgjelinek 
65550209230bSgjelinek int
65560209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
65570209230bSgjelinek {
65580209230bSgjelinek 	int err;
65590209230bSgjelinek 
65600209230bSgjelinek 	if (tabptr == NULL)
65610209230bSgjelinek 		return (Z_INVAL);
65620209230bSgjelinek 
65630209230bSgjelinek 	err = zonecfg_delete_mcap(handle);
65640209230bSgjelinek 	/* it is ok if there is no mcap entry */
65650209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
65660209230bSgjelinek 		return (err);
65670209230bSgjelinek 
65680209230bSgjelinek 	if ((err = add_mcap(handle, tabptr)) != Z_OK)
65690209230bSgjelinek 		return (err);
65700209230bSgjelinek 
65710209230bSgjelinek 	return (Z_OK);
65720209230bSgjelinek }
65730209230bSgjelinek 
65740209230bSgjelinek int
65750209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
65760209230bSgjelinek {
65770209230bSgjelinek 	xmlNodePtr cur;
65780209230bSgjelinek 	int err;
65790209230bSgjelinek 
65800209230bSgjelinek 	if (tabptr == NULL)
65810209230bSgjelinek 		return (Z_INVAL);
65820209230bSgjelinek 
65830209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
65840209230bSgjelinek 		return (err);
65850209230bSgjelinek 
65860209230bSgjelinek 	cur = handle->zone_dh_cur;
65870209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
65880209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
65890209230bSgjelinek 			continue;
65900209230bSgjelinek 		if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP,
65910209230bSgjelinek 		    tabptr->zone_physmem_cap,
65920209230bSgjelinek 		    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
65930209230bSgjelinek 			handle->zone_dh_cur = handle->zone_dh_top;
65940209230bSgjelinek 			return (err);
65950209230bSgjelinek 		}
65960209230bSgjelinek 
65970209230bSgjelinek 		return (Z_OK);
65980209230bSgjelinek 	}
65990209230bSgjelinek 
66000209230bSgjelinek 	return (Z_NO_ENTRY);
66010209230bSgjelinek }
66020209230bSgjelinek 
66030209230bSgjelinek static int
66040209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
66050209230bSgjelinek {
66060209230bSgjelinek 	xmlNodePtr cur;
66070209230bSgjelinek 	int err;
66080209230bSgjelinek 
66090209230bSgjelinek 	if (handle == NULL)
66100209230bSgjelinek 		return (Z_INVAL);
66110209230bSgjelinek 
66120209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
66130209230bSgjelinek 		return (Z_NO_ENTRY);
66140209230bSgjelinek 
66150209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
66160209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0)
66170209230bSgjelinek 			break;
66180209230bSgjelinek 	if (cur == NULL) {
66190209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
66200209230bSgjelinek 		return (Z_NO_ENTRY);
66210209230bSgjelinek 	}
66220209230bSgjelinek 
66230209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap,
66240209230bSgjelinek 	    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
66250209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
66260209230bSgjelinek 		return (err);
66270209230bSgjelinek 	}
66280209230bSgjelinek 
66290209230bSgjelinek 	handle->zone_dh_cur = cur->next;
66300209230bSgjelinek 	return (Z_OK);
66310209230bSgjelinek }
66320209230bSgjelinek 
66330209230bSgjelinek int
66340209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
66350209230bSgjelinek {
66360209230bSgjelinek 	int err;
66370209230bSgjelinek 
66380209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
66390209230bSgjelinek 		return (err);
66400209230bSgjelinek 
66410209230bSgjelinek 	err = getmcapent_core(handle, tabptr);
66420209230bSgjelinek 
66430209230bSgjelinek 	(void) zonecfg_endent(handle);
66440209230bSgjelinek 
66450209230bSgjelinek 	return (err);
66460209230bSgjelinek }
66470209230bSgjelinek 
66486cfd72c6Sgjelinek /*
66496cfd72c6Sgjelinek  * Get the full tree of pkg/patch metadata in a set of nested AVL trees.
66506cfd72c6Sgjelinek  * pkgs_avl is an AVL tree of pkgs.  Each pkg element contains a
66516cfd72c6Sgjelinek  * zpe_patches_avl member which holds an AVL tree of patches for that pkg.
66526cfd72c6Sgjelinek  * The patch elements have the same zpe_patches_avl member, each of which can
66536cfd72c6Sgjelinek  * hold an AVL tree of patches that are obsoleted by the patch.
66546cfd72c6Sgjelinek  *
66556cfd72c6Sgjelinek  * The zone xml data contains DTD_ELEM_PACKAGE elements, followed by
66566cfd72c6Sgjelinek  * DTD_ELEM_PATCH elements.  The DTD_ELEM_PATCH patch element applies to the
66576cfd72c6Sgjelinek  * DTD_ELEM_PACKAGE that precedes it.  The DTD_ELEM_PATCH element may have
66586cfd72c6Sgjelinek  * child DTD_ELEM_OBSOLETES nodes associated with it.  The DTD_ELEM_PACKAGE
66596cfd72c6Sgjelinek  * really should have had the DTD_ELEM_PATCH elements as children but it
66606cfd72c6Sgjelinek  * was not defined that way initially so we are stuck with the DTD definition
66616cfd72c6Sgjelinek  * now.  However, we can safely assume the ordering for compatibility.
66626cfd72c6Sgjelinek  */
6663ee519a1fSgjelinek int
66646cfd72c6Sgjelinek zonecfg_getpkgdata(zone_dochandle_t handle, uu_avl_pool_t *pkg_pool,
66656cfd72c6Sgjelinek     uu_avl_t *pkgs_avl)
6666ee519a1fSgjelinek {
6667ee519a1fSgjelinek 	xmlNodePtr cur;
66686cfd72c6Sgjelinek 	int res;
66696cfd72c6Sgjelinek 	zone_pkg_entry_t *pkg;
66706cfd72c6Sgjelinek 	char name[MAXNAMELEN];
66716cfd72c6Sgjelinek 	char version[ZONE_PKG_VERSMAX];
6672ee519a1fSgjelinek 
6673ee519a1fSgjelinek 	if (handle == NULL)
6674ee519a1fSgjelinek 		return (Z_INVAL);
6675ee519a1fSgjelinek 
66766cfd72c6Sgjelinek 	if ((res = zonecfg_setent(handle)) != Z_OK)
66776cfd72c6Sgjelinek 		return (res);
6678ee519a1fSgjelinek 
66796cfd72c6Sgjelinek 	if ((cur = handle->zone_dh_cur) == NULL) {
66806cfd72c6Sgjelinek 		res = Z_NO_ENTRY;
66816cfd72c6Sgjelinek 		goto done;
6682ee519a1fSgjelinek 	}
6683ee519a1fSgjelinek 
66846cfd72c6Sgjelinek 	for (; cur != NULL; cur = cur->next) {
66856cfd72c6Sgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PACKAGE) == 0) {
66866cfd72c6Sgjelinek 			uu_avl_index_t where;
66876cfd72c6Sgjelinek 
66886cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_NAME, name,
66896cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
66906cfd72c6Sgjelinek 				goto done;
66916cfd72c6Sgjelinek 
66926cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_VERSION, version,
66936cfd72c6Sgjelinek 			    sizeof (version))) != Z_OK)
66946cfd72c6Sgjelinek 				goto done;
66956cfd72c6Sgjelinek 
66966cfd72c6Sgjelinek 			if ((pkg = (zone_pkg_entry_t *)
66976cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
66986cfd72c6Sgjelinek 				res = Z_NOMEM;
66996cfd72c6Sgjelinek 				goto done;
6700ee519a1fSgjelinek 			}
6701ee519a1fSgjelinek 
67026cfd72c6Sgjelinek 			if ((pkg->zpe_name = strdup(name)) == NULL) {
67036cfd72c6Sgjelinek 				free(pkg);
67046cfd72c6Sgjelinek 				res = Z_NOMEM;
67056cfd72c6Sgjelinek 				goto done;
6706ee519a1fSgjelinek 			}
6707ee519a1fSgjelinek 
67086cfd72c6Sgjelinek 			if ((pkg->zpe_vers = strdup(version)) == NULL) {
67096cfd72c6Sgjelinek 				free(pkg->zpe_name);
67106cfd72c6Sgjelinek 				free(pkg);
67116cfd72c6Sgjelinek 				res = Z_NOMEM;
67126cfd72c6Sgjelinek 				goto done;
6713ee519a1fSgjelinek 			}
6714ee519a1fSgjelinek 
67156cfd72c6Sgjelinek 			pkg->zpe_patches_avl = NULL;
67166cfd72c6Sgjelinek 
67176cfd72c6Sgjelinek 			uu_avl_node_init(pkg, &pkg->zpe_entry, pkg_pool);
67186cfd72c6Sgjelinek 			if (uu_avl_find(pkgs_avl, pkg, NULL, &where) != NULL) {
67196cfd72c6Sgjelinek 				free(pkg->zpe_name);
67206cfd72c6Sgjelinek 				free(pkg->zpe_vers);
67216cfd72c6Sgjelinek 				free(pkg);
67226cfd72c6Sgjelinek 			} else {
67236cfd72c6Sgjelinek 				uu_avl_insert(pkgs_avl, pkg, where);
6724ee519a1fSgjelinek 			}
6725ee519a1fSgjelinek 
67266cfd72c6Sgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
67276cfd72c6Sgjelinek 			zone_pkg_entry_t *patch;
67286cfd72c6Sgjelinek 			uu_avl_index_t where;
67296cfd72c6Sgjelinek 			char *p;
67306cfd72c6Sgjelinek 			char *dashp = NULL;
67316cfd72c6Sgjelinek 			xmlNodePtr child;
67326cfd72c6Sgjelinek 
67336cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_ID, name,
67346cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
67356cfd72c6Sgjelinek 				goto done;
67366cfd72c6Sgjelinek 
67376cfd72c6Sgjelinek 			if ((patch = (zone_pkg_entry_t *)
67386cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
67396cfd72c6Sgjelinek 				res = Z_NOMEM;
67406cfd72c6Sgjelinek 				goto done;
6741ee519a1fSgjelinek 			}
6742ee519a1fSgjelinek 
67436cfd72c6Sgjelinek 			if ((p = strchr(name, '-')) != NULL) {
67446cfd72c6Sgjelinek 				dashp = p;
67456cfd72c6Sgjelinek 				*p++ = '\0';
67466cfd72c6Sgjelinek 			} else {
67476cfd72c6Sgjelinek 				p = "";
6748ee519a1fSgjelinek 			}
6749ee519a1fSgjelinek 
67506cfd72c6Sgjelinek 			if ((patch->zpe_name = strdup(name)) == NULL) {
67516cfd72c6Sgjelinek 				free(patch);
67526cfd72c6Sgjelinek 				res = Z_NOMEM;
67536cfd72c6Sgjelinek 				goto done;
6754ee519a1fSgjelinek 			}
6755ee519a1fSgjelinek 
67566cfd72c6Sgjelinek 			if ((patch->zpe_vers = strdup(p)) == NULL) {
67576cfd72c6Sgjelinek 				free(patch->zpe_name);
67586cfd72c6Sgjelinek 				free(patch);
67596cfd72c6Sgjelinek 				res = Z_NOMEM;
67606cfd72c6Sgjelinek 				goto done;
6761ee519a1fSgjelinek 			}
6762ee519a1fSgjelinek 
67636cfd72c6Sgjelinek 			if (dashp != NULL)
67646cfd72c6Sgjelinek 				*dashp = '-';
67656cfd72c6Sgjelinek 
67666cfd72c6Sgjelinek 			patch->zpe_patches_avl = NULL;
67676cfd72c6Sgjelinek 
67686cfd72c6Sgjelinek 			if (pkg->zpe_patches_avl == NULL) {
67696cfd72c6Sgjelinek 				pkg->zpe_patches_avl = uu_avl_create(pkg_pool,
67706cfd72c6Sgjelinek 				    NULL, UU_DEFAULT);
67716cfd72c6Sgjelinek 				if (pkg->zpe_patches_avl == NULL) {
67726cfd72c6Sgjelinek 					free(patch->zpe_name);
67736cfd72c6Sgjelinek 					free(patch->zpe_vers);
67746cfd72c6Sgjelinek 					free(patch);
67756cfd72c6Sgjelinek 					res = Z_NOMEM;
67766cfd72c6Sgjelinek 					goto done;
67776cfd72c6Sgjelinek 				}
67786cfd72c6Sgjelinek 			}
67796cfd72c6Sgjelinek 
67806cfd72c6Sgjelinek 			uu_avl_node_init(patch, &patch->zpe_entry, pkg_pool);
67816cfd72c6Sgjelinek 			if (uu_avl_find(pkg->zpe_patches_avl, patch, NULL,
67826cfd72c6Sgjelinek 			    &where) != NULL) {
67836cfd72c6Sgjelinek 				free(patch->zpe_name);
67846cfd72c6Sgjelinek 				free(patch->zpe_vers);
67856cfd72c6Sgjelinek 				free(patch);
67866cfd72c6Sgjelinek 			} else {
67876cfd72c6Sgjelinek 				uu_avl_insert(pkg->zpe_patches_avl, patch,
67886cfd72c6Sgjelinek 				    where);
67896cfd72c6Sgjelinek 			}
67906cfd72c6Sgjelinek 
67916cfd72c6Sgjelinek 			/* Add any patches this patch obsoletes. */
67926cfd72c6Sgjelinek 			for (child = cur->xmlChildrenNode; child != NULL;
67936cfd72c6Sgjelinek 			    child = child->next) {
67946cfd72c6Sgjelinek 				zone_pkg_entry_t *obs;
67956cfd72c6Sgjelinek 
67966cfd72c6Sgjelinek 				if (xmlStrcmp(child->name, DTD_ELEM_OBSOLETES)
67976cfd72c6Sgjelinek 				    != 0)
67986cfd72c6Sgjelinek 					continue;
67996cfd72c6Sgjelinek 
68006cfd72c6Sgjelinek 				if ((res = fetchprop(child, DTD_ATTR_ID,
68016cfd72c6Sgjelinek 				    name, sizeof (name))) != Z_OK)
68026cfd72c6Sgjelinek 					goto done;
68036cfd72c6Sgjelinek 
68046cfd72c6Sgjelinek 				if ((obs = (zone_pkg_entry_t *)malloc(
68056cfd72c6Sgjelinek 				    sizeof (zone_pkg_entry_t))) == NULL) {
68066cfd72c6Sgjelinek 					res = Z_NOMEM;
68076cfd72c6Sgjelinek 					goto done;
68086cfd72c6Sgjelinek 				}
68096cfd72c6Sgjelinek 
68106cfd72c6Sgjelinek 				if ((obs->zpe_name = strdup(name)) == NULL) {
68116cfd72c6Sgjelinek 					free(obs);
68126cfd72c6Sgjelinek 					res = Z_NOMEM;
68136cfd72c6Sgjelinek 					goto done;
68146cfd72c6Sgjelinek 				}
68156cfd72c6Sgjelinek 				/*
68166cfd72c6Sgjelinek 				 * The version doesn't matter for obsoleted
68176cfd72c6Sgjelinek 				 * patches.
68186cfd72c6Sgjelinek 				 */
68196cfd72c6Sgjelinek 				obs->zpe_vers = NULL;
68206cfd72c6Sgjelinek 				obs->zpe_patches_avl = NULL;
68216cfd72c6Sgjelinek 
68226cfd72c6Sgjelinek 				/*
68236cfd72c6Sgjelinek 				 * If this is the first obsolete patch, add an
68246cfd72c6Sgjelinek 				 * AVL tree to the parent patch element.
68256cfd72c6Sgjelinek 				 */
68266cfd72c6Sgjelinek 				if (patch->zpe_patches_avl == NULL) {
68276cfd72c6Sgjelinek 					patch->zpe_patches_avl =
68286cfd72c6Sgjelinek 					    uu_avl_create(pkg_pool, NULL,
68296cfd72c6Sgjelinek 					    UU_DEFAULT);
68306cfd72c6Sgjelinek 					if (patch->zpe_patches_avl == NULL) {
68316cfd72c6Sgjelinek 						free(obs->zpe_name);
68326cfd72c6Sgjelinek 						free(obs);
68336cfd72c6Sgjelinek 						res = Z_NOMEM;
68346cfd72c6Sgjelinek 						goto done;
68356cfd72c6Sgjelinek 					}
68366cfd72c6Sgjelinek 				}
68376cfd72c6Sgjelinek 
68386cfd72c6Sgjelinek 				/* Insert obsolete patch into the AVL tree. */
68396cfd72c6Sgjelinek 				uu_avl_node_init(obs, &obs->zpe_entry,
68406cfd72c6Sgjelinek 				    pkg_pool);
68416cfd72c6Sgjelinek 				if (uu_avl_find(patch->zpe_patches_avl, obs,
68426cfd72c6Sgjelinek 				    NULL, &where) != NULL) {
68436cfd72c6Sgjelinek 					free(obs->zpe_name);
68446cfd72c6Sgjelinek 					free(obs);
68456cfd72c6Sgjelinek 				} else {
68466cfd72c6Sgjelinek 					uu_avl_insert(patch->zpe_patches_avl,
68476cfd72c6Sgjelinek 					    obs, where);
68486cfd72c6Sgjelinek 				}
68496cfd72c6Sgjelinek 			}
68506cfd72c6Sgjelinek 		}
68516cfd72c6Sgjelinek 	}
68526cfd72c6Sgjelinek 
68536cfd72c6Sgjelinek done:
68546cfd72c6Sgjelinek 	(void) zonecfg_endent(handle);
68556cfd72c6Sgjelinek 	return (res);
6856ee519a1fSgjelinek }
6857ee519a1fSgjelinek 
6858ee519a1fSgjelinek int
6859ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
6860ee519a1fSgjelinek {
6861ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6862ee519a1fSgjelinek }
6863ee519a1fSgjelinek 
6864ee519a1fSgjelinek int
6865ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
6866ee519a1fSgjelinek {
6867ee519a1fSgjelinek 	xmlNodePtr cur;
6868ee519a1fSgjelinek 	int err;
6869ee519a1fSgjelinek 	char buf[128];
6870ee519a1fSgjelinek 
6871ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
6872ee519a1fSgjelinek 
6873ee519a1fSgjelinek 	if (handle == NULL)
6874ee519a1fSgjelinek 		return (Z_INVAL);
6875ee519a1fSgjelinek 
6876ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6877ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6878ee519a1fSgjelinek 
6879ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6880ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
6881ee519a1fSgjelinek 			break;
6882ee519a1fSgjelinek 	if (cur == NULL) {
6883ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6884ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6885ee519a1fSgjelinek 	}
6886ee519a1fSgjelinek 
6887ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
6888ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
6889ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6890ee519a1fSgjelinek 		return (err);
6891ee519a1fSgjelinek 	}
6892ee519a1fSgjelinek 
6893ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
6894ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6895ee519a1fSgjelinek 		return (err);
6896ee519a1fSgjelinek 	}
6897ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
6898ee519a1fSgjelinek 
6899ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
6900ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6901ee519a1fSgjelinek 		return (err);
6902ee519a1fSgjelinek 	}
6903ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
6904ee519a1fSgjelinek 
6905ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
6906ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6907ee519a1fSgjelinek 		return (err);
6908ee519a1fSgjelinek 	}
6909ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
6910ee519a1fSgjelinek 
6911ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
6912ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
6913ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6914ee519a1fSgjelinek 		return (err);
6915ee519a1fSgjelinek 	}
6916ee519a1fSgjelinek 
6917ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6918ee519a1fSgjelinek 	return (Z_OK);
6919ee519a1fSgjelinek }
6920ee519a1fSgjelinek 
6921ee519a1fSgjelinek int
6922ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
6923ee519a1fSgjelinek {
6924ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6925ee519a1fSgjelinek }
6926ee519a1fSgjelinek 
6927ff17c8bfSgjelinek /* PRINTFLIKE1 */
6928ff17c8bfSgjelinek static void
6929ff17c8bfSgjelinek zerror(const char *zone_name, const char *fmt, ...)
693039935be5Sgjelinek {
6931ff17c8bfSgjelinek 	va_list alist;
693239935be5Sgjelinek 
6933ff17c8bfSgjelinek 	va_start(alist, fmt);
6934ff17c8bfSgjelinek 	(void) fprintf(stderr, "zone '%s': ", zone_name);
6935ff17c8bfSgjelinek 	(void) vfprintf(stderr, fmt, alist);
6936ff17c8bfSgjelinek 	(void) fprintf(stderr, "\n");
6937ff17c8bfSgjelinek 	va_end(alist);
6938ee519a1fSgjelinek }
6939ee519a1fSgjelinek 
6940ee519a1fSgjelinek static void
6941ff17c8bfSgjelinek zperror(const char *str)
6942ee519a1fSgjelinek {
6943ff17c8bfSgjelinek 	(void) fprintf(stderr, "%s: %s\n", str, strerror(errno));
6944ee519a1fSgjelinek }
6945ee519a1fSgjelinek 
6946ee519a1fSgjelinek /*
6947ff17c8bfSgjelinek  * The following three routines implement a simple locking mechanism to
6948ff17c8bfSgjelinek  * ensure that only one instance of zoneadm at a time is able to manipulate
6949ff17c8bfSgjelinek  * a given zone.  The lock is built on top of an fcntl(2) lock of
6950ff17c8bfSgjelinek  * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock.  If a zoneadm instance
6951ff17c8bfSgjelinek  * can grab that lock, it is allowed to manipulate the zone.
6952ff17c8bfSgjelinek  *
6953ff17c8bfSgjelinek  * Since zoneadm may call external applications which in turn invoke
6954ff17c8bfSgjelinek  * zoneadm again, we introduce the notion of "lock inheritance".  Any
6955ff17c8bfSgjelinek  * instance of zoneadm that has another instance in its ancestry is assumed
6956ff17c8bfSgjelinek  * to be acting on behalf of the original zoneadm, and is thus allowed to
6957ff17c8bfSgjelinek  * manipulate its zone.
6958ff17c8bfSgjelinek  *
6959ff17c8bfSgjelinek  * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment
6960ff17c8bfSgjelinek  * variable.  When zoneadm is granted a lock on its zone, this environment
6961ff17c8bfSgjelinek  * variable is set to 1.  When it releases the lock, the variable is set to
6962ff17c8bfSgjelinek  * 0.  Since a child process inherits its parent's environment, checking
6963ff17c8bfSgjelinek  * the state of this variable indicates whether or not any ancestor owns
6964ff17c8bfSgjelinek  * the lock.
696539935be5Sgjelinek  */
6966ff17c8bfSgjelinek void
6967ff17c8bfSgjelinek zonecfg_init_lock_file(const char *zone_name, char **lock_env)
696839935be5Sgjelinek {
6969ff17c8bfSgjelinek 	*lock_env = getenv(LOCK_ENV_VAR);
6970ff17c8bfSgjelinek 	if (*lock_env == NULL) {
6971ff17c8bfSgjelinek 		if (putenv(zoneadm_lock_not_held) != 0) {
6972ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not set env: %s"),
6973ff17c8bfSgjelinek 			    strerror(errno));
6974ff17c8bfSgjelinek 			exit(1);
6975ff17c8bfSgjelinek 		}
697639935be5Sgjelinek 	} else {
6977ff17c8bfSgjelinek 		if (atoi(*lock_env) == 1)
6978ff17c8bfSgjelinek 			zone_lock_cnt = 1;
697939935be5Sgjelinek 	}
698039935be5Sgjelinek }
698139935be5Sgjelinek 
6982ff17c8bfSgjelinek void
6983ff17c8bfSgjelinek zonecfg_release_lock_file(const char *zone_name, int lockfd)
6984ff17c8bfSgjelinek {
6985ff17c8bfSgjelinek 	/*
6986ff17c8bfSgjelinek 	 * If we are cleaning up from a failed attempt to lock the zone for
6987ff17c8bfSgjelinek 	 * the first time, we might have a zone_lock_cnt of 0.  In that
6988ff17c8bfSgjelinek 	 * error case, we don't want to do anything but close the lock
6989ff17c8bfSgjelinek 	 * file.
6990ff17c8bfSgjelinek 	 */
6991ff17c8bfSgjelinek 	assert(zone_lock_cnt >= 0);
6992ff17c8bfSgjelinek 	if (zone_lock_cnt > 0) {
6993ff17c8bfSgjelinek 		assert(getenv(LOCK_ENV_VAR) != NULL);
6994ff17c8bfSgjelinek 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
6995ff17c8bfSgjelinek 		if (--zone_lock_cnt > 0) {
6996ff17c8bfSgjelinek 			assert(lockfd == -1);
6997ff17c8bfSgjelinek 			return;
699839935be5Sgjelinek 		}
6999ff17c8bfSgjelinek 		if (putenv(zoneadm_lock_not_held) != 0) {
7000ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not set env: %s"),
7001ff17c8bfSgjelinek 			    strerror(errno));
7002ff17c8bfSgjelinek 			exit(1);
7003ff17c8bfSgjelinek 		}
7004ff17c8bfSgjelinek 	}
7005ff17c8bfSgjelinek 	assert(lockfd >= 0);
7006ff17c8bfSgjelinek 	(void) close(lockfd);
700739935be5Sgjelinek }
700839935be5Sgjelinek 
7009ff17c8bfSgjelinek int
7010ff17c8bfSgjelinek zonecfg_grab_lock_file(const char *zone_name, int *lockfd)
7011ff17c8bfSgjelinek {
7012ff17c8bfSgjelinek 	char pathbuf[PATH_MAX];
7013ff17c8bfSgjelinek 	struct flock flock;
7014ff17c8bfSgjelinek 
7015ff17c8bfSgjelinek 	/*
7016ff17c8bfSgjelinek 	 * If we already have the lock, we can skip this expensive song
7017ff17c8bfSgjelinek 	 * and dance.
7018ff17c8bfSgjelinek 	 */
7019ff17c8bfSgjelinek 	assert(zone_lock_cnt >= 0);
7020ff17c8bfSgjelinek 	assert(getenv(LOCK_ENV_VAR) != NULL);
7021ff17c8bfSgjelinek 	if (zone_lock_cnt > 0) {
7022ff17c8bfSgjelinek 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
7023ff17c8bfSgjelinek 		zone_lock_cnt++;
7024ff17c8bfSgjelinek 		*lockfd = -1;
702539935be5Sgjelinek 		return (Z_OK);
702639935be5Sgjelinek 	}
7027ff17c8bfSgjelinek 	assert(getenv(LOCK_ENV_VAR) != NULL);
7028ff17c8bfSgjelinek 	assert(atoi(getenv(LOCK_ENV_VAR)) == 0);
702939935be5Sgjelinek 
7030ff17c8bfSgjelinek 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
7031ff17c8bfSgjelinek 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
7032ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
703339935be5Sgjelinek 		return (-1);
703439935be5Sgjelinek 	}
7035ff17c8bfSgjelinek 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
7036ff17c8bfSgjelinek 		zerror(zone_name, gettext("could not mkdir %s: %s"), pathbuf,
7037ff17c8bfSgjelinek 		    strerror(errno));
7038ff17c8bfSgjelinek 		return (-1);
7039ff17c8bfSgjelinek 	}
7040ff17c8bfSgjelinek 	(void) chmod(pathbuf, S_IRWXU);
704139935be5Sgjelinek 
704239935be5Sgjelinek 	/*
7043ff17c8bfSgjelinek 	 * One of these lock files is created for each zone (when needed).
7044ff17c8bfSgjelinek 	 * The lock files are not cleaned up (except on system reboot),
7045ff17c8bfSgjelinek 	 * but since there is only one per zone, there is no resource
7046ff17c8bfSgjelinek 	 * starvation issue.
704739935be5Sgjelinek 	 */
7048ff17c8bfSgjelinek 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
7049ff17c8bfSgjelinek 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
7050ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
7051ff17c8bfSgjelinek 		return (-1);
7052ff17c8bfSgjelinek 	}
7053ff17c8bfSgjelinek 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
7054ff17c8bfSgjelinek 		zerror(zone_name, gettext("could not open %s: %s"), pathbuf,
7055ff17c8bfSgjelinek 		    strerror(errno));
7056ff17c8bfSgjelinek 		return (-1);
7057ff17c8bfSgjelinek 	}
7058ff17c8bfSgjelinek 	/*
7059ff17c8bfSgjelinek 	 * Lock the file to synchronize with other zoneadmds
7060ff17c8bfSgjelinek 	 */
7061ff17c8bfSgjelinek 	flock.l_type = F_WRLCK;
7062ff17c8bfSgjelinek 	flock.l_whence = SEEK_SET;
7063ff17c8bfSgjelinek 	flock.l_start = (off_t)0;
7064ff17c8bfSgjelinek 	flock.l_len = (off_t)0;
7065ff17c8bfSgjelinek 	if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) ||
7066ff17c8bfSgjelinek 	    (putenv(zoneadm_lock_held) != 0)) {
7067ff17c8bfSgjelinek 		zerror(zone_name, gettext("unable to lock %s: %s"), pathbuf,
7068ff17c8bfSgjelinek 		    strerror(errno));
7069ff17c8bfSgjelinek 		zonecfg_release_lock_file(zone_name, *lockfd);
7070ff17c8bfSgjelinek 		return (-1);
7071ff17c8bfSgjelinek 	}
7072ff17c8bfSgjelinek 	zone_lock_cnt = 1;
7073ff17c8bfSgjelinek 	return (Z_OK);
7074ff17c8bfSgjelinek }
7075ff17c8bfSgjelinek 
7076ff17c8bfSgjelinek static boolean_t
7077ff17c8bfSgjelinek get_doorname(const char *zone_name, char *buffer)
707839935be5Sgjelinek {
7079ff17c8bfSgjelinek 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
7080ff17c8bfSgjelinek 	    zonecfg_get_root(), zone_name) < PATH_MAX);
7081e66285fdSgjelinek }
7082ee519a1fSgjelinek 
7083ee519a1fSgjelinek /*
7084ff17c8bfSgjelinek  * system daemons are not audited.  For the global zone, this occurs
7085ff17c8bfSgjelinek  * "naturally" since init is started with the default audit
7086ff17c8bfSgjelinek  * characteristics.  Since zoneadmd is a system daemon and it starts
7087ff17c8bfSgjelinek  * init for a zone, it is necessary to clear out the audit
7088ff17c8bfSgjelinek  * characteristics inherited from whomever started zoneadmd.  This is
7089ff17c8bfSgjelinek  * indicated by the audit id, which is set from the ruid parameter of
7090ff17c8bfSgjelinek  * adt_set_user(), below.
7091ee519a1fSgjelinek  */
7092ff17c8bfSgjelinek 
7093ff17c8bfSgjelinek static void
7094ff17c8bfSgjelinek prepare_audit_context(const char *zone_name)
7095ff17c8bfSgjelinek {
7096ff17c8bfSgjelinek 	adt_session_data_t	*ah;
7097ff17c8bfSgjelinek 	char			*failure = gettext("audit failure: %s");
7098ff17c8bfSgjelinek 
7099ff17c8bfSgjelinek 	if (adt_start_session(&ah, NULL, 0)) {
7100ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7101ff17c8bfSgjelinek 		return;
7102ff17c8bfSgjelinek 	}
7103ff17c8bfSgjelinek 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
7104ff17c8bfSgjelinek 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
7105ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7106ff17c8bfSgjelinek 		(void) adt_end_session(ah);
7107ff17c8bfSgjelinek 		return;
7108ff17c8bfSgjelinek 	}
7109ff17c8bfSgjelinek 	if (adt_set_proc(ah))
7110ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7111ff17c8bfSgjelinek 
7112ff17c8bfSgjelinek 	(void) adt_end_session(ah);
7113ff17c8bfSgjelinek }
7114ff17c8bfSgjelinek 
7115ff17c8bfSgjelinek static int
7116ff17c8bfSgjelinek start_zoneadmd(const char *zone_name, boolean_t lock)
7117ff17c8bfSgjelinek {
7118ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7119ff17c8bfSgjelinek 	pid_t child_pid;
7120ff17c8bfSgjelinek 	int error = -1;
7121ff17c8bfSgjelinek 	int doorfd, lockfd;
7122ff17c8bfSgjelinek 	struct door_info info;
7123ff17c8bfSgjelinek 
7124ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath))
7125ff17c8bfSgjelinek 		return (-1);
7126ff17c8bfSgjelinek 
7127ff17c8bfSgjelinek 	if (lock)
7128ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(zone_name, &lockfd) != Z_OK)
7129ff17c8bfSgjelinek 			return (-1);
7130ff17c8bfSgjelinek 
7131ff17c8bfSgjelinek 	/*
7132ff17c8bfSgjelinek 	 * Now that we have the lock, re-confirm that the daemon is
7133ff17c8bfSgjelinek 	 * *not* up and working fine.  If it is still down, we have a green
7134ff17c8bfSgjelinek 	 * light to start it.
7135ff17c8bfSgjelinek 	 */
7136ff17c8bfSgjelinek 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7137ff17c8bfSgjelinek 		if (errno != ENOENT) {
7138ff17c8bfSgjelinek 			zperror(doorpath);
7139ff17c8bfSgjelinek 			goto out;
7140ff17c8bfSgjelinek 		}
7141ff17c8bfSgjelinek 	} else {
7142ff17c8bfSgjelinek 		if (door_info(doorfd, &info) == 0 &&
7143ff17c8bfSgjelinek 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
7144ff17c8bfSgjelinek 			error = Z_OK;
7145ff17c8bfSgjelinek 			(void) close(doorfd);
7146ff17c8bfSgjelinek 			goto out;
7147ff17c8bfSgjelinek 		}
7148ff17c8bfSgjelinek 		(void) close(doorfd);
7149ff17c8bfSgjelinek 	}
7150ff17c8bfSgjelinek 
7151ff17c8bfSgjelinek 	if ((child_pid = fork()) == -1) {
7152ff17c8bfSgjelinek 		zperror(gettext("could not fork"));
7153ff17c8bfSgjelinek 		goto out;
7154ff17c8bfSgjelinek 	}
7155ff17c8bfSgjelinek 
7156ff17c8bfSgjelinek 	if (child_pid == 0) {
7157ff17c8bfSgjelinek 		const char *argv[6], **ap;
7158ff17c8bfSgjelinek 
7159ff17c8bfSgjelinek 		/* child process */
7160ff17c8bfSgjelinek 		prepare_audit_context(zone_name);
7161ff17c8bfSgjelinek 
7162ff17c8bfSgjelinek 		ap = argv;
7163ff17c8bfSgjelinek 		*ap++ = "zoneadmd";
7164ff17c8bfSgjelinek 		*ap++ = "-z";
7165ff17c8bfSgjelinek 		*ap++ = zone_name;
7166ff17c8bfSgjelinek 		if (zonecfg_in_alt_root()) {
7167ff17c8bfSgjelinek 			*ap++ = "-R";
7168ff17c8bfSgjelinek 			*ap++ = zonecfg_get_root();
7169ff17c8bfSgjelinek 		}
7170ff17c8bfSgjelinek 		*ap = NULL;
7171ff17c8bfSgjelinek 
7172ff17c8bfSgjelinek 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
7173ff17c8bfSgjelinek 		/*
7174ff17c8bfSgjelinek 		 * TRANSLATION_NOTE
7175ff17c8bfSgjelinek 		 * zoneadmd is a literal that should not be translated.
7176ff17c8bfSgjelinek 		 */
7177ff17c8bfSgjelinek 		zperror(gettext("could not exec zoneadmd"));
7178ff17c8bfSgjelinek 		_exit(1);
7179ff17c8bfSgjelinek 	} else {
7180ff17c8bfSgjelinek 		/* parent process */
7181ff17c8bfSgjelinek 		pid_t retval;
7182ff17c8bfSgjelinek 		int pstatus = 0;
7183ee519a1fSgjelinek 
7184ee519a1fSgjelinek 		do {
7185ff17c8bfSgjelinek 			retval = waitpid(child_pid, &pstatus, 0);
7186ff17c8bfSgjelinek 		} while (retval != child_pid);
7187ff17c8bfSgjelinek 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
7188ff17c8bfSgjelinek 		    WEXITSTATUS(pstatus) != 0)) {
7189ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not start %s"),
7190ff17c8bfSgjelinek 			    "zoneadmd");
7191ff17c8bfSgjelinek 			goto out;
71926cfd72c6Sgjelinek 		}
71936cfd72c6Sgjelinek 	}
7194ff17c8bfSgjelinek 	error = Z_OK;
7195ff17c8bfSgjelinek out:
7196ff17c8bfSgjelinek 	if (lock)
7197ff17c8bfSgjelinek 		zonecfg_release_lock_file(zone_name, lockfd);
7198ff17c8bfSgjelinek 	return (error);
7199ee519a1fSgjelinek }
7200ee519a1fSgjelinek 
7201ee519a1fSgjelinek int
7202ff17c8bfSgjelinek zonecfg_ping_zoneadmd(const char *zone_name)
7203ee519a1fSgjelinek {
7204ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7205ff17c8bfSgjelinek 	int doorfd;
7206ff17c8bfSgjelinek 	struct door_info info;
7207ee519a1fSgjelinek 
7208ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath))
7209ff17c8bfSgjelinek 		return (-1);
7210ee519a1fSgjelinek 
7211ff17c8bfSgjelinek 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7212ff17c8bfSgjelinek 		return (-1);
7213ff17c8bfSgjelinek 	}
7214ff17c8bfSgjelinek 	if (door_info(doorfd, &info) == 0 &&
7215ff17c8bfSgjelinek 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
7216ff17c8bfSgjelinek 		(void) close(doorfd);
7217ff17c8bfSgjelinek 		return (Z_OK);
7218ff17c8bfSgjelinek 	}
7219ff17c8bfSgjelinek 	(void) close(doorfd);
7220ff17c8bfSgjelinek 	return (-1);
7221ff17c8bfSgjelinek }
7222ee519a1fSgjelinek 
7223ff17c8bfSgjelinek int
7224ff17c8bfSgjelinek zonecfg_call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg, char *locale,
7225ff17c8bfSgjelinek     boolean_t lock)
7226ff17c8bfSgjelinek {
7227ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7228ff17c8bfSgjelinek 	int doorfd, result;
7229ff17c8bfSgjelinek 	door_arg_t darg;
7230ff17c8bfSgjelinek 
7231ff17c8bfSgjelinek 	zoneid_t zoneid;
7232ff17c8bfSgjelinek 	uint64_t uniqid = 0;
7233ff17c8bfSgjelinek 
7234ff17c8bfSgjelinek 	zone_cmd_rval_t *rvalp;
7235ff17c8bfSgjelinek 	size_t rlen;
7236ff17c8bfSgjelinek 	char *cp, *errbuf;
7237ff17c8bfSgjelinek 
7238ff17c8bfSgjelinek 	rlen = getpagesize();
7239ff17c8bfSgjelinek 	if ((rvalp = malloc(rlen)) == NULL) {
7240ff17c8bfSgjelinek 		zerror(zone_name, gettext("failed to allocate %lu bytes: %s"),
7241ff17c8bfSgjelinek 		    rlen, strerror(errno));
7242ff17c8bfSgjelinek 		return (-1);
7243ff17c8bfSgjelinek 	}
7244ff17c8bfSgjelinek 
7245ff17c8bfSgjelinek 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
7246ff17c8bfSgjelinek 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
7247ff17c8bfSgjelinek 		    sizeof (uniqid));
7248ff17c8bfSgjelinek 	}
7249ff17c8bfSgjelinek 	arg->uniqid = uniqid;
7250ff17c8bfSgjelinek 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
7251ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath)) {
7252ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
7253ff17c8bfSgjelinek 		free(rvalp);
7254ff17c8bfSgjelinek 		return (-1);
7255ff17c8bfSgjelinek 	}
7256ff17c8bfSgjelinek 
7257ff17c8bfSgjelinek 	/*
7258ff17c8bfSgjelinek 	 * Loop trying to start zoneadmd; if something goes seriously
7259ff17c8bfSgjelinek 	 * wrong we break out and fail.
7260ff17c8bfSgjelinek 	 */
7261ff17c8bfSgjelinek 	for (;;) {
7262ff17c8bfSgjelinek 		if (start_zoneadmd(zone_name, lock) != Z_OK)
7263ff17c8bfSgjelinek 			break;
7264ff17c8bfSgjelinek 
7265ff17c8bfSgjelinek 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7266ff17c8bfSgjelinek 			zperror(gettext("failed to open zone door"));
7267ff17c8bfSgjelinek 			break;
7268ff17c8bfSgjelinek 		}
7269ff17c8bfSgjelinek 
7270ff17c8bfSgjelinek 		darg.data_ptr = (char *)arg;
7271ff17c8bfSgjelinek 		darg.data_size = sizeof (*arg);
7272ff17c8bfSgjelinek 		darg.desc_ptr = NULL;
7273ff17c8bfSgjelinek 		darg.desc_num = 0;
7274ff17c8bfSgjelinek 		darg.rbuf = (char *)rvalp;
7275ff17c8bfSgjelinek 		darg.rsize = rlen;
7276ff17c8bfSgjelinek 		if (door_call(doorfd, &darg) != 0) {
7277ff17c8bfSgjelinek 			(void) close(doorfd);
7278ff17c8bfSgjelinek 			/*
7279ff17c8bfSgjelinek 			 * We'll get EBADF if the door has been revoked.
7280ff17c8bfSgjelinek 			 */
7281ff17c8bfSgjelinek 			if (errno != EBADF) {
7282ff17c8bfSgjelinek 				zperror(gettext("door_call failed"));
7283ff17c8bfSgjelinek 				break;
7284ff17c8bfSgjelinek 			}
7285ff17c8bfSgjelinek 			continue;	/* take another lap */
7286ff17c8bfSgjelinek 		}
7287ff17c8bfSgjelinek 		(void) close(doorfd);
7288ff17c8bfSgjelinek 
7289ff17c8bfSgjelinek 		if (darg.data_size == 0) {
7290ff17c8bfSgjelinek 			/* Door server is going away; kick it again. */
7291ff17c8bfSgjelinek 			continue;
7292ff17c8bfSgjelinek 		}
7293ff17c8bfSgjelinek 
7294ff17c8bfSgjelinek 		errbuf = rvalp->errbuf;
7295ff17c8bfSgjelinek 		while (*errbuf != '\0') {
7296ff17c8bfSgjelinek 			/*
7297ff17c8bfSgjelinek 			 * Remove any newlines since zerror()
7298ff17c8bfSgjelinek 			 * will append one automatically.
7299ff17c8bfSgjelinek 			 */
7300ff17c8bfSgjelinek 			cp = strchr(errbuf, '\n');
7301ff17c8bfSgjelinek 			if (cp != NULL)
7302ff17c8bfSgjelinek 				*cp = '\0';
7303ff17c8bfSgjelinek 			zerror(zone_name, "%s", errbuf);
7304ff17c8bfSgjelinek 			if (cp == NULL)
7305ff17c8bfSgjelinek 				break;
7306ff17c8bfSgjelinek 			errbuf = cp + 1;
7307ff17c8bfSgjelinek 		}
7308ff17c8bfSgjelinek 		result = rvalp->rval == 0 ? 0 : -1;
7309ff17c8bfSgjelinek 		free(rvalp);
7310ff17c8bfSgjelinek 		return (result);
7311ff17c8bfSgjelinek 	}
7312ff17c8bfSgjelinek 
7313ff17c8bfSgjelinek 	free(rvalp);
7314ff17c8bfSgjelinek 	return (-1);
7315ee519a1fSgjelinek }
7316