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 /*
23*6cfd72c6Sgjelinek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
29cf8f45c7Sdstaff #include <libsysevent.h>
30cf8f45c7Sdstaff #include <pthread.h>
31cf8f45c7Sdstaff #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <fnmatch.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate #include <libgen.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <alloca.h>
407c478bd9Sstevel@tonic-gate #include <ctype.h>
419acbbeafSnn #include <sys/acl.h>
429acbbeafSnn #include <sys/stat.h>
439acbbeafSnn #include <sys/brand.h>
447c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
457c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
46cf8f45c7Sdstaff #include <sys/nvpair.h>
479acbbeafSnn #include <sys/types.h>
48f4b3ec61Sdh #include <sys/sockio.h>
49ee519a1fSgjelinek #include <ftw.h>
500209230bSgjelinek #include <pool.h>
510209230bSgjelinek #include <libscf.h>
520209230bSgjelinek #include <libproc.h>
530209230bSgjelinek #include <sys/priocntl.h>
5439935be5Sgjelinek #include <libuutil.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>
659acbbeafSnn #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"
92*6cfd72c6Sgjelinek #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"
98f4b3ec61Sdh #define	DTD_ATTR_IPTYPE		(const xmlChar *) "ip-type"
997c478bd9Sstevel@tonic-gate #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
1007c478bd9Sstevel@tonic-gate #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
101ffbafc53Scomay #define	DTD_ATTR_LIMITPRIV	(const xmlChar *) "limitpriv"
1023f2f09c1Sdp #define	DTD_ATTR_BOOTARGS	(const xmlChar *) "bootargs"
1030209230bSgjelinek #define	DTD_ATTR_SCHED		(const xmlChar *) "scheduling-class"
1047c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
1057c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
1067c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
1077c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
1087c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
1097c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
1107c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
1117c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
1127c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
1137c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
1140209230bSgjelinek #define	DTD_ATTR_NCPU_MIN	(const xmlChar *) "ncpu_min"
1150209230bSgjelinek #define	DTD_ATTR_NCPU_MAX	(const xmlChar *) "ncpu_max"
1160209230bSgjelinek #define	DTD_ATTR_IMPORTANCE	(const xmlChar *) "importance"
1170209230bSgjelinek #define	DTD_ATTR_PHYSCAP	(const xmlChar *) "physcap"
118ee519a1fSgjelinek #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
119ee519a1fSgjelinek #define	DTD_ATTR_ID		(const xmlChar *) "id"
120ee519a1fSgjelinek #define	DTD_ATTR_UID		(const xmlChar *) "uid"
121ee519a1fSgjelinek #define	DTD_ATTR_GID		(const xmlChar *) "gid"
122ee519a1fSgjelinek #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
123ee519a1fSgjelinek #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
1249acbbeafSnn #define	DTD_ATTR_BRAND		(const xmlChar *) "brand"
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
1277c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
1287c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
1297c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
1307c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
1317c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
1327c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
1337c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
1347c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
1357c478bd9Sstevel@tonic-gate 
136a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
137a1be23daSdp 
138ee519a1fSgjelinek #define	DETACHED	"SUNWdetached.xml"
139ee519a1fSgjelinek #define	ATTACH_FORCED	"SUNWattached.xml"
140ee519a1fSgjelinek #define	PKG_PATH	"/var/sadm/pkg"
141ee519a1fSgjelinek #define	CONTENTS_FILE	"/var/sadm/install/contents"
14245916cd2Sjpk #define	SUNW_PKG_ALL_ZONES	"SUNW_PKG_ALLZONES=true\n"
14345916cd2Sjpk #define	SUNW_PKG_THIS_ZONE	"SUNW_PKG_THISZONE=true\n"
144ee519a1fSgjelinek #define	VERSION		"VERSION="
145ee519a1fSgjelinek #define	PATCHLIST	"PATCHLIST="
146ee519a1fSgjelinek #define	PATCHINFO	"PATCH_INFO_"
147ee519a1fSgjelinek #define	PKGINFO_RD_LEN	128
148ee519a1fSgjelinek 
1490209230bSgjelinek #define	TMP_POOL_NAME	"SUNWtmp_%s"
1500209230bSgjelinek #define	MAX_TMP_POOL_NAME	(ZONENAME_MAX + 9)
1510209230bSgjelinek #define	RCAP_SERVICE	"system/rcap:default"
1520209230bSgjelinek #define	POOLD_SERVICE	"system/pools/dynamic:default"
1530209230bSgjelinek 
15439935be5Sgjelinek enum zn_ipd_fs {ZONE_IPD, ZONE_FS};
15539935be5Sgjelinek 
1560209230bSgjelinek /*
1570209230bSgjelinek  * rctl alias definitions
1580209230bSgjelinek  *
1590209230bSgjelinek  * This holds the alias, the full rctl name, the default priv value, action
1600209230bSgjelinek  * and lower limit.  The functions that handle rctl aliases step through
1610209230bSgjelinek  * this table, matching on the alias, and using the full values for setting
1620209230bSgjelinek  * the rctl entry as well the limit for validation.
1630209230bSgjelinek  */
1640209230bSgjelinek static struct alias {
1650209230bSgjelinek 	char *shortname;
1660209230bSgjelinek 	char *realname;
1670209230bSgjelinek 	char *priv;
1680209230bSgjelinek 	char *action;
1690209230bSgjelinek 	uint64_t low_limit;
1700209230bSgjelinek } aliases[] = {
1710209230bSgjelinek 	{ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100},
1720209230bSgjelinek 	{ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0},
1730209230bSgjelinek 	{ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0},
1740209230bSgjelinek 	{ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0},
1750209230bSgjelinek 	{ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0},
1760209230bSgjelinek 	{ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0},
1770209230bSgjelinek 	{ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0},
1780209230bSgjelinek 	{ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0},
179c97ad5cdSakolb 	{ALIAS_CPUCAP, "zone.cpu-cap", "privileged", "deny", 0},
1800209230bSgjelinek 	{NULL, NULL, NULL, NULL, 0}
1810209230bSgjelinek };
1820209230bSgjelinek 
1830209230bSgjelinek /*
1840209230bSgjelinek  * Structure for applying rctls to a running zone.  It allows important
1850209230bSgjelinek  * process values to be passed together easily.
1860209230bSgjelinek  */
1870209230bSgjelinek typedef struct pr_info_handle {
1880209230bSgjelinek 	struct ps_prochandle *pr;
1890209230bSgjelinek 	pid_t pid;
1900209230bSgjelinek } pr_info_handle_t;
1910209230bSgjelinek 
1927c478bd9Sstevel@tonic-gate struct zone_dochandle {
1937c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1947c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1957c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1967c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
197087719fdSdp 	boolean_t	zone_dh_newzone;
198087719fdSdp 	boolean_t	zone_dh_snapshot;
199ee519a1fSgjelinek 	boolean_t	zone_dh_sw_inv;
200087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
2017c478bd9Sstevel@tonic-gate };
2027c478bd9Sstevel@tonic-gate 
203cf8f45c7Sdstaff struct znotify {
204cf8f45c7Sdstaff 	void * zn_private;
205cf8f45c7Sdstaff 	evchan_t *zn_eventchan;
206cf8f45c7Sdstaff 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
207cf8f45c7Sdstaff 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
208cf8f45c7Sdstaff 	pthread_mutex_t zn_mutex;
209cf8f45c7Sdstaff 	pthread_cond_t zn_cond;
210cf8f45c7Sdstaff 	pthread_mutex_t zn_bigmutex;
211cf8f45c7Sdstaff 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
212cf8f45c7Sdstaff 	    ZN_PING_RECEIVED} zn_state;
213cf8f45c7Sdstaff 	char zn_subscriber_id[MAX_SUBID_LEN];
214cf8f45c7Sdstaff 	volatile boolean_t zn_failed;
215cf8f45c7Sdstaff 	int zn_failure_count;
216cf8f45c7Sdstaff };
217cf8f45c7Sdstaff 
218ee519a1fSgjelinek struct zone_pkginfo {
219ee519a1fSgjelinek 	boolean_t	zpi_all_zones;
220ee519a1fSgjelinek 	boolean_t	zpi_this_zone;
221ee519a1fSgjelinek 	int		zpi_patch_cnt;
222ee519a1fSgjelinek 	char		*zpi_version;
223ee519a1fSgjelinek 	char		**zpi_patchinfo;
224ee519a1fSgjelinek };
225ee519a1fSgjelinek 
22639935be5Sgjelinek typedef struct {
22739935be5Sgjelinek 	uu_avl_node_t	patch_node;
22839935be5Sgjelinek 	char		*patch_num;
22939935be5Sgjelinek 	char		*patch_vers;
230*6cfd72c6Sgjelinek 	uu_list_t	*obs_patches;
23139935be5Sgjelinek } patch_node_t;
23239935be5Sgjelinek 
233*6cfd72c6Sgjelinek typedef struct {
234*6cfd72c6Sgjelinek 	uu_list_node_t	link;
235*6cfd72c6Sgjelinek 	char		*patch_num;
236*6cfd72c6Sgjelinek } obs_patch_node_t;
237*6cfd72c6Sgjelinek 
23839935be5Sgjelinek typedef struct {
23939935be5Sgjelinek 	uu_avl_t	*obs_patches_avl;
24039935be5Sgjelinek 	zone_dochandle_t handle;
24139935be5Sgjelinek 	int		res;
24239935be5Sgjelinek } patch_parms_t;
24339935be5Sgjelinek 
244108322fbScarlsonj char *zonecfg_root = "";
245108322fbScarlsonj 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
2487c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
2497c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
2507c478bd9Sstevel@tonic-gate  * values from this set.
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate 
253108322fbScarlsonj /*
254108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
255108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
256108322fbScarlsonj  * configuration inside mounted alternate boot environments.
257108322fbScarlsonj  */
258108322fbScarlsonj void
259108322fbScarlsonj zonecfg_set_root(const char *rootpath)
260108322fbScarlsonj {
261108322fbScarlsonj 	if (*zonecfg_root != '\0')
262108322fbScarlsonj 		free(zonecfg_root);
263108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
264108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
265108322fbScarlsonj 		zonecfg_root = "";
266108322fbScarlsonj }
267108322fbScarlsonj 
268108322fbScarlsonj const char *
269108322fbScarlsonj zonecfg_get_root(void)
270108322fbScarlsonj {
271108322fbScarlsonj 	return (zonecfg_root);
272108322fbScarlsonj }
273108322fbScarlsonj 
274108322fbScarlsonj boolean_t
275108322fbScarlsonj zonecfg_in_alt_root(void)
276108322fbScarlsonj {
277108322fbScarlsonj 	return (*zonecfg_root != '\0');
278108322fbScarlsonj }
279108322fbScarlsonj 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
2827c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate 
285108322fbScarlsonj static boolean_t
2867c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
2877c478bd9Sstevel@tonic-gate {
288108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
289108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
292108322fbScarlsonj static boolean_t
293108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
2947c478bd9Sstevel@tonic-gate {
295108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
296108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3007c478bd9Sstevel@tonic-gate static void
3017c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
3057c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate zone_dochandle_t
3107c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
3117c478bd9Sstevel@tonic-gate {
312087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
3137c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
3147c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
3157c478bd9Sstevel@tonic-gate 		return (NULL);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
3197c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
3207c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3217c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
3227c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
3237c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
3247c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	return (handle);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate int
3307c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
3337c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3347c478bd9Sstevel@tonic-gate 	return (Z_OK);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate void
3387c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
3417c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
3427c478bd9Sstevel@tonic-gate 	if (handle != NULL)
3437c478bd9Sstevel@tonic-gate 		free(handle);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate static int
3477c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
3507c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
3517c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
3527c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
3537c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
3547c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 	return (Z_OK);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate int
360087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
363087719fdSdp 	struct zoneent ze;
364087719fdSdp 	int err, state_err;
365087719fdSdp 	zone_state_t state;
3667c478bd9Sstevel@tonic-gate 
367108322fbScarlsonj 	if (!config_file_path(zonename, path))
368108322fbScarlsonj 		return (Z_MISC_FS);
369087719fdSdp 
370087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
371087719fdSdp 	err = access(path, W_OK);
372087719fdSdp 
373087719fdSdp 	/*
374087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
375087719fdSdp 	 * such zone exists.
376087719fdSdp 	 */
377087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
378087719fdSdp 		return (Z_NO_ZONE);
379087719fdSdp 
380087719fdSdp 	/*
381087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
382087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
383087719fdSdp 	 * in which case we plow on.
384087719fdSdp 	 */
385087719fdSdp 	if (err == -1 && errno != ENOENT) {
386087719fdSdp 		if (errno == EACCES)
387087719fdSdp 			return (Z_ACCES);
388087719fdSdp 		else if (!force)
389087719fdSdp 			return (Z_MISC_FS);
390087719fdSdp 	}
391087719fdSdp 
392087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
393087719fdSdp 		return (Z_BAD_ZONE_STATE);
394087719fdSdp 
395087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
396087719fdSdp 		return (Z_BAD_ZONE_STATE);
397087719fdSdp 
398087719fdSdp 	/*
399087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
400087719fdSdp 	 * will fail only if we've had some more severe problem.
401087719fdSdp 	 */
402087719fdSdp 	bzero(&ze, sizeof (ze));
403087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
404087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
405087719fdSdp 		if (!force)
406087719fdSdp 			return (err);
407087719fdSdp 
408087719fdSdp 	err = zonecfg_destroy_impl(path);
409087719fdSdp 
410087719fdSdp 	/*
411087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
412087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
413087719fdSdp 	 */
414087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
415087719fdSdp 		return (Z_OK);
416087719fdSdp 	return (err);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate int
420108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4237c478bd9Sstevel@tonic-gate 
424108322fbScarlsonj 	if (!snap_file_path(zonename, path))
425108322fbScarlsonj 		return (Z_MISC_FS);
4267c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate static int
430a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
4337c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
4347c478bd9Sstevel@tonic-gate 
435a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
436a1be23daSdp 
437a1be23daSdp 	if (*root == NULL)
4387c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
4397c478bd9Sstevel@tonic-gate 
440a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
4417c478bd9Sstevel@tonic-gate 		return (Z_WRONG_DOC_TYPE);
442a1be23daSdp 
443a1be23daSdp 	return (Z_OK);
444a1be23daSdp }
445a1be23daSdp 
446a1be23daSdp static int
447a1be23daSdp operation_prep(zone_dochandle_t handle)
448a1be23daSdp {
449a1be23daSdp 	xmlNodePtr root;
450a1be23daSdp 	int err;
451a1be23daSdp 
452a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
453a1be23daSdp 		return (err);
454a1be23daSdp 
455a1be23daSdp 	handle->zone_dh_cur = root;
456a1be23daSdp 	handle->zone_dh_top = root;
457a1be23daSdp 	return (Z_OK);
458a1be23daSdp }
459a1be23daSdp 
460ffbafc53Scomay static int
461ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
462ffbafc53Scomay {
463ffbafc53Scomay 	xmlChar *property;
464ffbafc53Scomay 	size_t srcsize;
465ffbafc53Scomay 
466ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
467ffbafc53Scomay 		return (Z_BAD_PROPERTY);
468ffbafc53Scomay 	srcsize = strlcpy(dst, (char *)property, dstsize);
469ffbafc53Scomay 	xmlFree(property);
470ffbafc53Scomay 	if (srcsize >= dstsize)
471ffbafc53Scomay 		return (Z_TOO_BIG);
472ffbafc53Scomay 	return (Z_OK);
473ffbafc53Scomay }
474ffbafc53Scomay 
475ffbafc53Scomay static int
476ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
477ffbafc53Scomay {
478ffbafc53Scomay 	xmlChar *property;
479ffbafc53Scomay 
480ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
481ffbafc53Scomay 		return (Z_BAD_PROPERTY);
482ffbafc53Scomay 	if ((*dst = strdup((char *)property)) == NULL) {
483ffbafc53Scomay 		xmlFree(property);
484ffbafc53Scomay 		return (Z_NOMEM);
485ffbafc53Scomay 	}
486ffbafc53Scomay 	xmlFree(property);
487ffbafc53Scomay 	return (Z_OK);
488ffbafc53Scomay }
489ffbafc53Scomay 
490a1be23daSdp static int
491a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
492a1be23daSdp     char *propval, size_t propsize)
493a1be23daSdp {
494a1be23daSdp 	xmlNodePtr root;
495a1be23daSdp 	int err;
496a1be23daSdp 
497a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
498a1be23daSdp 		return (err);
499a1be23daSdp 
500ffbafc53Scomay 	return (fetchprop(root, propname, propval, propsize));
501ffbafc53Scomay }
502ffbafc53Scomay 
503ffbafc53Scomay static int
504ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
505ffbafc53Scomay     char **propval)
506ffbafc53Scomay {
507ffbafc53Scomay 	xmlNodePtr root;
508ffbafc53Scomay 	int err;
509ffbafc53Scomay 
510ffbafc53Scomay 	if ((err = getroot(handle, &root)) != 0)
511ffbafc53Scomay 		return (err);
512ffbafc53Scomay 
513ffbafc53Scomay 	return (fetch_alloc_prop(root, propname, propval));
514a1be23daSdp }
515a1be23daSdp 
516a1be23daSdp static int
517108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
518108322fbScarlsonj     const char *propval)
519a1be23daSdp {
520a1be23daSdp 	int err;
521a1be23daSdp 	xmlNodePtr root;
522a1be23daSdp 
523a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
524a1be23daSdp 		return (err);
525a1be23daSdp 
5260209230bSgjelinek 	/*
5270209230bSgjelinek 	 * If we get a null propval remove the property (ignore return since it
5280209230bSgjelinek 	 * may not be set to begin with).
5290209230bSgjelinek 	 */
5300209230bSgjelinek 	if (propval == NULL) {
5310209230bSgjelinek 		(void) xmlUnsetProp(root, propname);
5320209230bSgjelinek 	} else {
5330209230bSgjelinek 		if (xmlSetProp(root, propname, (const xmlChar *) propval)
5340209230bSgjelinek 		    == NULL)
5350209230bSgjelinek 			return (Z_INVAL);
5360209230bSgjelinek 	}
5377c478bd9Sstevel@tonic-gate 	return (Z_OK);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
540087719fdSdp static void
541087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
542087719fdSdp {
543087719fdSdp 	xmlNodePtr node;
544087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
545087719fdSdp 
546087719fdSdp 	if (node != NULL)
547087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
548087719fdSdp }
549087719fdSdp 
550087719fdSdp static void
551087719fdSdp stripcomments(zone_dochandle_t handle)
552087719fdSdp {
553087719fdSdp 	xmlDocPtr top;
554087719fdSdp 	xmlNodePtr child, next;
555087719fdSdp 
556087719fdSdp 	top = handle->zone_dh_doc;
557087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
558087719fdSdp 		next = child->next;
559087719fdSdp 		if (child->name == NULL)
560087719fdSdp 			continue;
561087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
562087719fdSdp 			next = child->next;
563087719fdSdp 			xmlUnlinkNode(child);
564087719fdSdp 			xmlFreeNode(child);
565087719fdSdp 		}
566087719fdSdp 	}
567087719fdSdp }
568087719fdSdp 
569ee519a1fSgjelinek static void
570ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle)
571ee519a1fSgjelinek {
572ee519a1fSgjelinek 	xmlNodePtr root, child, next;
573ee519a1fSgjelinek 
574ee519a1fSgjelinek 	root = xmlDocGetRootElement(handle->zone_dh_doc);
575ee519a1fSgjelinek 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
576ee519a1fSgjelinek 		next = child->next;
577ee519a1fSgjelinek 		if (child->name == NULL)
578ee519a1fSgjelinek 			continue;
579ee519a1fSgjelinek 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
580ee519a1fSgjelinek 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
581ee519a1fSgjelinek 			next = child->next;
582ee519a1fSgjelinek 			xmlUnlinkNode(child);
583ee519a1fSgjelinek 			xmlFreeNode(child);
584ee519a1fSgjelinek 		}
585ee519a1fSgjelinek 	}
586ee519a1fSgjelinek }
587ee519a1fSgjelinek 
5887c478bd9Sstevel@tonic-gate static int
589108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
590108322fbScarlsonj     zone_dochandle_t handle)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
5937c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5947c478bd9Sstevel@tonic-gate 	int valid;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
5977c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5989acbbeafSnn 
5997c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
6007c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
6017c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
6027c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
6037c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
6067c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
6077c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
6087c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
6097c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
6107c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
6117c478bd9Sstevel@tonic-gate 	if (valid == 0)
6127c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
613087719fdSdp 
6147c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
615087719fdSdp 	stripcomments(handle);
6167c478bd9Sstevel@tonic-gate 	return (Z_OK);
6177c478bd9Sstevel@tonic-gate }
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate int
620108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6237c478bd9Sstevel@tonic-gate 
624108322fbScarlsonj 	if (!config_file_path(zonename, path))
625108322fbScarlsonj 		return (Z_MISC_FS);
626087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
627087719fdSdp 
6287c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
631ee519a1fSgjelinek int
632ee519a1fSgjelinek zonecfg_get_attach_handle(const char *path, const char *zonename,
633ee519a1fSgjelinek     boolean_t preserve_sw, zone_dochandle_t handle)
634ee519a1fSgjelinek {
635ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
636ee519a1fSgjelinek 	int		err;
637ee519a1fSgjelinek 	struct stat	buf;
638ee519a1fSgjelinek 
639ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
640ee519a1fSgjelinek 	    sizeof (migpath))
641ee519a1fSgjelinek 		return (Z_NOMEM);
642ee519a1fSgjelinek 
643ee519a1fSgjelinek 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
644ee519a1fSgjelinek 		return (Z_NO_ZONE);
645ee519a1fSgjelinek 
646ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
647ee519a1fSgjelinek 	    sizeof (migpath))
648ee519a1fSgjelinek 		return (Z_NOMEM);
649ee519a1fSgjelinek 
650ee519a1fSgjelinek 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
651ee519a1fSgjelinek 		return (err);
652ee519a1fSgjelinek 
653ee519a1fSgjelinek 	if (!preserve_sw)
654ee519a1fSgjelinek 		strip_sw_inv(handle);
655ee519a1fSgjelinek 
656ee519a1fSgjelinek 	handle->zone_dh_newzone = B_TRUE;
657ee519a1fSgjelinek 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
658ee519a1fSgjelinek 		return (err);
659ee519a1fSgjelinek 
660ee519a1fSgjelinek 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
661ee519a1fSgjelinek }
662ee519a1fSgjelinek 
6637c478bd9Sstevel@tonic-gate int
664108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6677c478bd9Sstevel@tonic-gate 
668108322fbScarlsonj 	if (!snap_file_path(zonename, path))
669108322fbScarlsonj 		return (Z_MISC_FS);
670087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
6717c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
674087719fdSdp int
675108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
676087719fdSdp     zone_dochandle_t handle)
677087719fdSdp {
678087719fdSdp 	char path[MAXPATHLEN];
679087719fdSdp 	int err;
680087719fdSdp 
681108322fbScarlsonj 	if (!config_file_path(template, path))
682108322fbScarlsonj 		return (Z_MISC_FS);
683087719fdSdp 
684087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
685087719fdSdp 		return (err);
686087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
687087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
688087719fdSdp }
689087719fdSdp 
6909acbbeafSnn int
6919acbbeafSnn zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle)
6929acbbeafSnn {
6939acbbeafSnn 	struct stat buf;
6949acbbeafSnn 	int err;
6959acbbeafSnn 
6969acbbeafSnn 	if (stat(path, &buf) == -1)
6979acbbeafSnn 		return (Z_MISC_FS);
6989acbbeafSnn 
6999acbbeafSnn 	if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK)
7009acbbeafSnn 		return (err);
7019acbbeafSnn 	handle->zone_dh_newzone = B_TRUE;
7029acbbeafSnn 	return (Z_OK);
7039acbbeafSnn }
7049acbbeafSnn 
7058cd327d5Sgjelinek /*
7068cd327d5Sgjelinek  * Initialize two handles from the manifest read on fd.  The rem_handle
7078cd327d5Sgjelinek  * is initialized from the input file, including the sw inventory.  The
7088cd327d5Sgjelinek  * local_handle is initialized with the same zone configuration but with
7098cd327d5Sgjelinek  * no sw inventory.
7108cd327d5Sgjelinek  */
7118cd327d5Sgjelinek int
7128cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
7138cd327d5Sgjelinek     zone_dochandle_t rem_handle)
7148cd327d5Sgjelinek {
7158cd327d5Sgjelinek 	xmlValidCtxtPtr cvp;
7168cd327d5Sgjelinek 	int valid;
7178cd327d5Sgjelinek 
7188cd327d5Sgjelinek 	/* load the manifest into the handle for the remote system */
7198cd327d5Sgjelinek 	if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
7208cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
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, rem_handle->zone_dh_doc);
7278cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7288cd327d5Sgjelinek 	if (valid == 0)
7298cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7308cd327d5Sgjelinek 
7318cd327d5Sgjelinek 	/* delete any comments such as inherited Sun copyright / ident str */
7328cd327d5Sgjelinek 	stripcomments(rem_handle);
7338cd327d5Sgjelinek 
7348cd327d5Sgjelinek 	rem_handle->zone_dh_newzone = B_TRUE;
7358cd327d5Sgjelinek 	rem_handle->zone_dh_sw_inv = B_TRUE;
7368cd327d5Sgjelinek 
7378cd327d5Sgjelinek 	/*
7388cd327d5Sgjelinek 	 * Now use the remote system handle to generate a local system handle
7398cd327d5Sgjelinek 	 * with an identical zones configuration but no sw inventory.
7408cd327d5Sgjelinek 	 */
7418cd327d5Sgjelinek 	if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
7428cd327d5Sgjelinek 	    1)) == NULL) {
7438cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7448cd327d5Sgjelinek 	}
7458cd327d5Sgjelinek 
7468cd327d5Sgjelinek 	/*
7478cd327d5Sgjelinek 	 * We need to re-run xmlValidateDocument on local_handle to properly
7488cd327d5Sgjelinek 	 * update the in-core representation of the configuration.
7498cd327d5Sgjelinek 	 */
7508cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7518cd327d5Sgjelinek 		return (Z_NOMEM);
7528cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7538cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7548cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
7558cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7568cd327d5Sgjelinek 	if (valid == 0)
7578cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7588cd327d5Sgjelinek 
7598cd327d5Sgjelinek 	strip_sw_inv(local_handle);
7608cd327d5Sgjelinek 
7618cd327d5Sgjelinek 	local_handle->zone_dh_newzone = B_TRUE;
7628cd327d5Sgjelinek 	local_handle->zone_dh_sw_inv = B_FALSE;
7638cd327d5Sgjelinek 
7648cd327d5Sgjelinek 	return (Z_OK);
7658cd327d5Sgjelinek }
7668cd327d5Sgjelinek 
767087719fdSdp static boolean_t
768087719fdSdp is_renaming(zone_dochandle_t handle)
769087719fdSdp {
770087719fdSdp 	if (handle->zone_dh_newzone)
771087719fdSdp 		return (B_FALSE);
772087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
773087719fdSdp 		return (B_TRUE);
774087719fdSdp 	return (B_FALSE);
775087719fdSdp }
776087719fdSdp 
777087719fdSdp static boolean_t
778087719fdSdp is_new(zone_dochandle_t handle)
779087719fdSdp {
780087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
781087719fdSdp }
782087719fdSdp 
783087719fdSdp static boolean_t
784087719fdSdp is_snapshot(zone_dochandle_t handle)
785087719fdSdp {
786087719fdSdp 	return (handle->zone_dh_snapshot);
787087719fdSdp }
788087719fdSdp 
789087719fdSdp /*
790087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
791087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
792087719fdSdp  * safety if this routine had to change the app locale on the fly.
793087719fdSdp  */
794087719fdSdp int
795108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
796087719fdSdp {
797087719fdSdp 	int i;
798087719fdSdp 
799087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
800087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
801087719fdSdp 
802087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
803087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
804087719fdSdp 
805087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
806087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
807087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
808087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
809087719fdSdp 
810087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
811087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
812087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
813087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
814087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
815087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
816087719fdSdp 	}
817087719fdSdp 
818087719fdSdp 	return (Z_OK);
819087719fdSdp }
820087719fdSdp 
821087719fdSdp /*
822087719fdSdp  * Changing the zone name requires us to track both the old and new
823087719fdSdp  * name of the zone until commit time.
824087719fdSdp  */
8257c478bd9Sstevel@tonic-gate int
8267c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
8277c478bd9Sstevel@tonic-gate {
828a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
829a1be23daSdp }
8307c478bd9Sstevel@tonic-gate 
831a1be23daSdp int
832a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
833a1be23daSdp {
834087719fdSdp 	zone_state_t state;
835087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
836087719fdSdp 	int err;
837087719fdSdp 
838087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
839087719fdSdp 	    sizeof (curname))) != Z_OK)
840087719fdSdp 		return (err);
841087719fdSdp 
842087719fdSdp 	if (strcmp(name, curname) == 0)
843087719fdSdp 		return (Z_OK);
844087719fdSdp 
845087719fdSdp 	/*
846087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
847087719fdSdp 	 */
848087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
849087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
850087719fdSdp 
851087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
852087719fdSdp 		return (err);
853087719fdSdp 
854087719fdSdp 	/*
855087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
856087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
857087719fdSdp 	 * name as "in use," so special case here.
858087719fdSdp 	 */
859087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
860087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
861087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
862087719fdSdp 		return (err);
863087719fdSdp 	}
864087719fdSdp 
865087719fdSdp 	/* Check to see if new name chosen is already in use */
866087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
867087719fdSdp 		return (Z_NAME_IN_USE);
868087719fdSdp 
869087719fdSdp 	/*
870087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
871087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
872087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
873087719fdSdp 	 */
874087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
875087719fdSdp 	    sizeof (old_delname));
876087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
877087719fdSdp 		/*
878087719fdSdp 		 * Name change is allowed only when the zone we're altering
879087719fdSdp 		 * is not ready or running.
880087719fdSdp 		 */
881087719fdSdp 		err = zone_get_state(curname, &state);
882087719fdSdp 		if (err == Z_OK) {
883087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
884087719fdSdp 				return (Z_BAD_ZONE_STATE);
885087719fdSdp 		} else if (err != Z_NO_ZONE) {
886087719fdSdp 			return (err);
887087719fdSdp 		}
888087719fdSdp 
889087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
890087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
891087719fdSdp 		assert(is_renaming(handle));
892087719fdSdp 	} else if (is_renaming(handle)) {
893087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
894087719fdSdp 		if (err == Z_OK) {
895087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
896087719fdSdp 				return (Z_BAD_ZONE_STATE);
897087719fdSdp 		} else if (err != Z_NO_ZONE) {
898087719fdSdp 			return (err);
899087719fdSdp 		}
900087719fdSdp 	}
901087719fdSdp 
902087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
903087719fdSdp 		/*
904087719fdSdp 		 * Restore the deletename to whatever it was at the
905087719fdSdp 		 * top of the routine, since we've had a failure.
906087719fdSdp 		 */
907087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
908087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
909087719fdSdp 		return (err);
910087719fdSdp 	}
911087719fdSdp 
912087719fdSdp 	return (Z_OK);
913a1be23daSdp }
9147c478bd9Sstevel@tonic-gate 
915a1be23daSdp int
916a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
917a1be23daSdp {
918108322fbScarlsonj 	size_t len;
919108322fbScarlsonj 
920108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
921108322fbScarlsonj 		return (Z_TOO_BIG);
922108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
923108322fbScarlsonj 	    pathsize - len));
924a1be23daSdp }
9257c478bd9Sstevel@tonic-gate 
926a1be23daSdp int
927a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
928a1be23daSdp {
929555afedfScarlsonj 	size_t len;
930555afedfScarlsonj 
931555afedfScarlsonj 	/*
932555afedfScarlsonj 	 * The user deals in absolute paths in the running global zone, but the
933555afedfScarlsonj 	 * internal configuration files deal with boot environment relative
934555afedfScarlsonj 	 * paths.  Strip out the alternate root when specified.
935555afedfScarlsonj 	 */
936555afedfScarlsonj 	len = strlen(zonecfg_root);
937555afedfScarlsonj 	if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
938555afedfScarlsonj 		return (Z_BAD_PROPERTY);
939555afedfScarlsonj 	zonepath += len;
940a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
941a1be23daSdp }
942a1be23daSdp 
9439acbbeafSnn int
9449acbbeafSnn zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
9459acbbeafSnn {
9469acbbeafSnn 	int ret, sz;
9479acbbeafSnn 
9489acbbeafSnn 	ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
9499acbbeafSnn 
9509acbbeafSnn 	/* If the zone has no brand, it is native. */
9519acbbeafSnn 	if (ret == Z_OK && brand[0] == '\0') {
9529acbbeafSnn 		sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
9539acbbeafSnn 		if (sz >= brandsize)
9549acbbeafSnn 			ret = Z_TOO_BIG;
9559acbbeafSnn 		else
9569acbbeafSnn 			ret = Z_OK;
9579acbbeafSnn 	}
9589acbbeafSnn 
9599acbbeafSnn 	return (ret);
9609acbbeafSnn }
9619acbbeafSnn 
9629acbbeafSnn int
9639acbbeafSnn zonecfg_set_brand(zone_dochandle_t handle, char *brand)
9649acbbeafSnn {
9659acbbeafSnn 	return (setrootattr(handle, DTD_ATTR_BRAND, brand));
9669acbbeafSnn }
9679acbbeafSnn 
968a1be23daSdp int
969a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
970a1be23daSdp {
971a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
972a1be23daSdp 	int ret;
973a1be23daSdp 
974a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
975a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
976a1be23daSdp 		return (ret);
977a1be23daSdp 
978a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
979a1be23daSdp 		*autoboot = B_TRUE;
980a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
981a1be23daSdp 		*autoboot = B_FALSE;
982a1be23daSdp 	else
983a1be23daSdp 		ret = Z_BAD_PROPERTY;
984a1be23daSdp 	return (ret);
985a1be23daSdp }
986a1be23daSdp 
987a1be23daSdp int
988a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
989a1be23daSdp {
990a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
991a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
992a1be23daSdp }
993a1be23daSdp 
994a1be23daSdp int
995a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
996a1be23daSdp {
997a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
998a1be23daSdp }
999a1be23daSdp 
1000a1be23daSdp int
1001a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
1002a1be23daSdp {
1003a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
1004a1be23daSdp }
1005a1be23daSdp 
1006ffbafc53Scomay int
1007ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
1008ffbafc53Scomay {
1009ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
1010ffbafc53Scomay }
1011ffbafc53Scomay 
1012ffbafc53Scomay int
10133f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
1014ffbafc53Scomay {
10153f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
10163f2f09c1Sdp }
10173f2f09c1Sdp 
10183f2f09c1Sdp int
10193f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
10203f2f09c1Sdp {
10213f2f09c1Sdp 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
10223f2f09c1Sdp }
10233f2f09c1Sdp 
10243f2f09c1Sdp int
10253f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
10263f2f09c1Sdp {
10273f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
1028ffbafc53Scomay }
1029ffbafc53Scomay 
10300209230bSgjelinek int
10310209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
10320209230bSgjelinek {
10330209230bSgjelinek 	return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
10340209230bSgjelinek }
10350209230bSgjelinek 
10360209230bSgjelinek int
10370209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched)
10380209230bSgjelinek {
10390209230bSgjelinek 	return (setrootattr(handle, DTD_ATTR_SCHED, sched));
10400209230bSgjelinek }
10410209230bSgjelinek 
1042a1be23daSdp /*
1043a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
1044a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
1045a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
1046a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
1047087719fdSdp  *
1048087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
1049087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
1050087719fdSdp  * In general, the operation of this function should succeed or fail as
1051087719fdSdp  * a unit.
1052a1be23daSdp  */
1053a1be23daSdp int
1054a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
1055a1be23daSdp {
1056a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
1057a1be23daSdp 	struct zoneent ze;
1058a1be23daSdp 	int err;
1059087719fdSdp 	int opcode;
1060087719fdSdp 	char *zn;
1061087719fdSdp 
1062087719fdSdp 	bzero(&ze, sizeof (ze));
1063087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
1064a1be23daSdp 
1065a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
1066a1be23daSdp 		return (err);
1067087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
1068087719fdSdp 
1069a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
1070a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
1071a1be23daSdp 		return (err);
1072555afedfScarlsonj 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
1073555afedfScarlsonj 	    sizeof (ze.zone_path));
1074087719fdSdp 
1075087719fdSdp 	if (is_renaming(handle)) {
1076087719fdSdp 		opcode = PZE_MODIFY;
1077087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
1078087719fdSdp 		    sizeof (ze.zone_name));
1079087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
1080087719fdSdp 	} else if (is_new(handle)) {
1081087719fdSdp 		FILE *cookie;
1082087719fdSdp 		/*
1083087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
1084087719fdSdp 		 * since we might be forcibly overwriting an existing
1085087719fdSdp 		 * configuration with a new one (for example 'create -F'
1086087719fdSdp 		 * in zonecfg).
1087087719fdSdp 		 */
1088087719fdSdp 		opcode = PZE_ADD;
1089087719fdSdp 		cookie = setzoneent();
1090087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
1091087719fdSdp 			if (strcmp(zn, name) == 0) {
1092087719fdSdp 				opcode = PZE_MODIFY;
1093087719fdSdp 				free(zn);
1094087719fdSdp 				break;
1095087719fdSdp 			}
1096087719fdSdp 			free(zn);
1097087719fdSdp 		}
1098087719fdSdp 		endzoneent(cookie);
1099087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
1100087719fdSdp 	} else {
1101087719fdSdp 		opcode = PZE_MODIFY;
1102087719fdSdp 	}
1103087719fdSdp 
1104087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
1105087719fdSdp 		return (err);
1106087719fdSdp 
1107087719fdSdp 	return (Z_OK);
11087c478bd9Sstevel@tonic-gate }
11097c478bd9Sstevel@tonic-gate 
1110087719fdSdp /*
1111087719fdSdp  * The goal of this routine is to cause the index file update and the
1112087719fdSdp  * document save to happen as an atomic operation.  We do the document
1113087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
1114087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
1115087719fdSdp  *
1116087719fdSdp  * Strategy:
1117087719fdSdp  *
1118087719fdSdp  * New zone 'foo' configuration:
1119087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1120087719fdSdp  * 	Write XML to tmpfile
1121087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1122087719fdSdp  * 	Add entry to index file
1123087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
1124087719fdSdp  *
1125087719fdSdp  * Save existing zone 'foo':
1126087719fdSdp  * 	Make backup of foo.xml -> .backup
1127087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1128087719fdSdp  * 	Write XML to tmpfile
1129087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1130087719fdSdp  * 	Modify index file as needed
1131087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
1132087719fdSdp  *
1133087719fdSdp  * Rename 'foo' to 'bar':
1134087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1135087719fdSdp  * 	Write XML to tmpfile
1136087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1137087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1138087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
1139087719fdSdp  */
11407c478bd9Sstevel@tonic-gate static int
11417c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
11427c478bd9Sstevel@tonic-gate {
11437c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
1144087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
11459acbbeafSnn 	int tmpfd, err, valid;
11467c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
1147087719fdSdp 	boolean_t backup;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
11507c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
11517c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
11547c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
11557c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
11567c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
11617c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
11627c478bd9Sstevel@tonic-gate 
1163087719fdSdp 	/*
11649acbbeafSnn 	 * We do a final validation of the document.  Since the library has
11659acbbeafSnn 	 * malfunctioned if it fails to validate, we follow-up with an
11669acbbeafSnn 	 * assert() that the doc is valid.
1167087719fdSdp 	 */
11689acbbeafSnn 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
11699acbbeafSnn 	assert(valid != 0);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
11727c478bd9Sstevel@tonic-gate 		goto err;
1173087719fdSdp 
11747c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
11757c478bd9Sstevel@tonic-gate 
1176087719fdSdp 	/*
1177087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
1178087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
1179087719fdSdp 	 * something goes wrong.
1180087719fdSdp 	 */
1181087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
1182087719fdSdp 		backup = B_TRUE;
1183087719fdSdp 
1184087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1185087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1186087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1187087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
1188087719fdSdp 
1189087719fdSdp 		if (link(filename, bakfile) == -1) {
1190087719fdSdp 			err = errno;
1191087719fdSdp 			(void) unlink(tmpfile);
1192087719fdSdp 			if (errno == EACCES)
1193087719fdSdp 				return (Z_ACCES);
1194087719fdSdp 			return (Z_MISC_FS);
1195087719fdSdp 		}
1196087719fdSdp 	}
1197087719fdSdp 
1198087719fdSdp 	/*
1199087719fdSdp 	 * Move the new document over top of the old.
1200087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1201087719fdSdp 	 */
12027c478bd9Sstevel@tonic-gate 	if (rename(tmpfile, filename) == -1) {
1203087719fdSdp 		err = errno;
12047c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
1205087719fdSdp 		if (backup)
1206087719fdSdp 			(void) unlink(bakfile);
1207087719fdSdp 		if (err == EACCES)
12087c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
12097c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
12107c478bd9Sstevel@tonic-gate 	}
1211a1be23daSdp 
1212087719fdSdp 	/*
1213087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
1214087719fdSdp 	 */
1215087719fdSdp 	if (is_snapshot(handle))
1216087719fdSdp 		return (Z_OK);
1217087719fdSdp 
1218087719fdSdp 	/* now update the index file to reflect whatever we just did */
1219087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1220087719fdSdp 		if (backup) {
1221087719fdSdp 			/*
1222087719fdSdp 			 * Try to restore from our backup.
1223087719fdSdp 			 */
1224087719fdSdp 			(void) unlink(filename);
1225087719fdSdp 			(void) rename(bakfile, filename);
1226087719fdSdp 		} else {
1227087719fdSdp 			/*
1228087719fdSdp 			 * Either the zone is new, in which case we can delete
1229087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1230087719fdSdp 			 */
1231087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1232087719fdSdp 			(void) unlink(filename);
1233087719fdSdp 		}
1234087719fdSdp 		return (Z_UPDATING_INDEX);
1235087719fdSdp 	}
1236087719fdSdp 
1237087719fdSdp 	if (backup)
1238087719fdSdp 		(void) unlink(bakfile);
1239087719fdSdp 
1240087719fdSdp 	return (Z_OK);
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate err:
12437c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
12447c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate int
12487c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
12497c478bd9Sstevel@tonic-gate {
1250087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1251087719fdSdp 	char delpath[MAXPATHLEN];
1252087719fdSdp 	int err = Z_SAVING_FILE;
1253087719fdSdp 
1254087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1255087719fdSdp 		return (Z_BAD_HANDLE);
12567c478bd9Sstevel@tonic-gate 
1257087719fdSdp 	/*
1258ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1259ee519a1fSgjelinek 	 * inventory at this time.
1260087719fdSdp 	 */
1261ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1262087719fdSdp 		return (Z_INVAL);
1263087719fdSdp 
1264087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
12657c478bd9Sstevel@tonic-gate 		return (err);
1266087719fdSdp 
1267108322fbScarlsonj 	if (!config_file_path(zname, path))
1268108322fbScarlsonj 		return (Z_MISC_FS);
1269087719fdSdp 
1270087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1271087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1272087719fdSdp 
1273087719fdSdp 	err = zonecfg_save_impl(handle, path);
1274087719fdSdp 
1275087719fdSdp 	stripcomments(handle);
1276087719fdSdp 
1277087719fdSdp 	if (err != Z_OK)
1278087719fdSdp 		return (err);
1279087719fdSdp 
1280087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1281087719fdSdp 
1282087719fdSdp 	if (is_renaming(handle)) {
1283108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1284108322fbScarlsonj 			(void) unlink(delpath);
1285087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1286087719fdSdp 	}
1287087719fdSdp 
1288087719fdSdp 	return (Z_OK);
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12919acbbeafSnn int
12929acbbeafSnn zonecfg_verify_save(zone_dochandle_t handle, char *filename)
12939acbbeafSnn {
12949acbbeafSnn 	int valid;
12959acbbeafSnn 
12969acbbeafSnn 	xmlValidCtxt cvp = { NULL };
12979acbbeafSnn 
12989acbbeafSnn 	if (zonecfg_check_handle(handle) != Z_OK)
12999acbbeafSnn 		return (Z_BAD_HANDLE);
13009acbbeafSnn 
13019acbbeafSnn 	cvp.error = zonecfg_error_func;
13029acbbeafSnn 	cvp.warning = zonecfg_error_func;
13039acbbeafSnn 
13049acbbeafSnn 	/*
13059acbbeafSnn 	 * We do a final validation of the document.  Since the library has
13069acbbeafSnn 	 * malfunctioned if it fails to validate, we follow-up with an
13079acbbeafSnn 	 * assert() that the doc is valid.
13089acbbeafSnn 	 */
13099acbbeafSnn 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13109acbbeafSnn 	assert(valid != 0);
13119acbbeafSnn 
13129acbbeafSnn 	if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
13139acbbeafSnn 		return (Z_SAVING_FILE);
13149acbbeafSnn 
13159acbbeafSnn 	return (Z_OK);
13169acbbeafSnn }
13179acbbeafSnn 
1318ee519a1fSgjelinek int
13198cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1320ee519a1fSgjelinek {
1321ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1322ee519a1fSgjelinek 	char path[MAXPATHLEN];
1323ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1324ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1325ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
13269acbbeafSnn 	int valid;
1327ee519a1fSgjelinek 
1328ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1329ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1330ee519a1fSgjelinek 
1331ee519a1fSgjelinek 	/*
1332ee519a1fSgjelinek 	 * We can only detach if we have taken a sw inventory.
1333ee519a1fSgjelinek 	 */
1334ee519a1fSgjelinek 	if (!handle->zone_dh_sw_inv)
1335ee519a1fSgjelinek 		return (Z_INVAL);
1336ee519a1fSgjelinek 
13378cd327d5Sgjelinek 	if (flags & ZONE_DRY_RUN) {
13388cd327d5Sgjelinek 		(void) strlcpy(migpath, "-", sizeof (migpath));
13398cd327d5Sgjelinek 	} else {
13408cd327d5Sgjelinek 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
13418cd327d5Sgjelinek 		    != Z_OK)
13428cd327d5Sgjelinek 			return (err);
1343ee519a1fSgjelinek 
13448cd327d5Sgjelinek 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
13458cd327d5Sgjelinek 		    != Z_OK)
13468cd327d5Sgjelinek 			return (err);
1347ee519a1fSgjelinek 
13488cd327d5Sgjelinek 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED)
13498cd327d5Sgjelinek 		    >= sizeof (migpath))
13508cd327d5Sgjelinek 			return (Z_NOMEM);
13518cd327d5Sgjelinek 	}
1352ee519a1fSgjelinek 
1353ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1354ee519a1fSgjelinek 		return (err);
1355ee519a1fSgjelinek 
1356ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1357ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1358ee519a1fSgjelinek 
1359ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1360ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1361ee519a1fSgjelinek 
1362ee519a1fSgjelinek 	/*
13639acbbeafSnn 	 * We do a final validation of the document.  Since the library has
13649acbbeafSnn 	 * malfunctioned if it fails to validate, we follow-up with an
13659acbbeafSnn 	 * assert() that the doc is valid.
1366ee519a1fSgjelinek 	 */
13679acbbeafSnn 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13689acbbeafSnn 	assert(valid != 0);
1369ee519a1fSgjelinek 
1370ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1371ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1372ee519a1fSgjelinek 
13738cd327d5Sgjelinek 	if (!(flags & ZONE_DRY_RUN))
13748cd327d5Sgjelinek 		(void) chmod(migpath, 0644);
1375ee519a1fSgjelinek 
1376ee519a1fSgjelinek 	stripcomments(handle);
1377ee519a1fSgjelinek 
1378ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1379ee519a1fSgjelinek 
1380ee519a1fSgjelinek 	return (Z_OK);
1381ee519a1fSgjelinek }
1382ee519a1fSgjelinek 
1383ee519a1fSgjelinek boolean_t
1384ee519a1fSgjelinek zonecfg_detached(const char *path)
1385ee519a1fSgjelinek {
1386ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1387ee519a1fSgjelinek 	struct stat	buf;
1388ee519a1fSgjelinek 
1389ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
1390ee519a1fSgjelinek 	    sizeof (migpath))
1391ee519a1fSgjelinek 		return (B_FALSE);
1392ee519a1fSgjelinek 
1393ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1394ee519a1fSgjelinek 		return (B_TRUE);
1395ee519a1fSgjelinek 
1396ee519a1fSgjelinek 	return (B_FALSE);
1397ee519a1fSgjelinek }
1398ee519a1fSgjelinek 
1399ee519a1fSgjelinek void
1400ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1401ee519a1fSgjelinek {
1402ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1403ee519a1fSgjelinek 	char path[MAXPATHLEN];
1404ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1405ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1406ee519a1fSgjelinek 
1407ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1408ee519a1fSgjelinek 		return;
1409ee519a1fSgjelinek 
1410ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1411ee519a1fSgjelinek 		return;
1412ee519a1fSgjelinek 
1413ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1414ee519a1fSgjelinek 		return;
1415ee519a1fSgjelinek 
1416ee519a1fSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path, DETACHED);
1417ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1418ee519a1fSgjelinek 	    ATTACH_FORCED);
1419ee519a1fSgjelinek 
1420ee519a1fSgjelinek 	if (forced) {
1421ee519a1fSgjelinek 		(void) rename(detached, attached);
1422ee519a1fSgjelinek 	} else {
1423ee519a1fSgjelinek 		(void) unlink(attached);
1424ee519a1fSgjelinek 		(void) unlink(detached);
1425ee519a1fSgjelinek 	}
1426ee519a1fSgjelinek }
1427ee519a1fSgjelinek 
14287c478bd9Sstevel@tonic-gate /*
14297c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
14307c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
14317c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
14327c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
14337c478bd9Sstevel@tonic-gate  */
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate int
14367c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
14377c478bd9Sstevel@tonic-gate {
14387c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
14397c478bd9Sstevel@tonic-gate 
1440108322fbScarlsonj 	if (!config_file_path(zonename, path))
1441108322fbScarlsonj 		return (Z_INVAL);
14427c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
14437c478bd9Sstevel@tonic-gate 		return (Z_OK);
1444108322fbScarlsonj 	if (errno == ENOENT) {
1445108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1446108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1447108322fbScarlsonj 			return (Z_INVAL);
1448108322fbScarlsonj 		if (access(path, amode) == 0)
1449108322fbScarlsonj 			return (Z_OK);
1450108322fbScarlsonj 	}
14517c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
14527c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
14537c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
14547c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14557c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
14567c478bd9Sstevel@tonic-gate }
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate int
1459108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
14607c478bd9Sstevel@tonic-gate {
14617c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
14627c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
14637c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
14667c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate 
1469087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1470087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1471087719fdSdp 
14727c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
14737c478bd9Sstevel@tonic-gate 		goto out;
14747c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
14757c478bd9Sstevel@tonic-gate 		goto out;
14767c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
14777c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
14787c478bd9Sstevel@tonic-gate 		goto out;
14797c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
14807c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
14817c478bd9Sstevel@tonic-gate 		goto out;
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 	/*
14847c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
14857c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
14867c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
14877c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
14887c478bd9Sstevel@tonic-gate 	 * paths.
14897c478bd9Sstevel@tonic-gate 	 */
14907c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
14917c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
14927c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
14937c478bd9Sstevel@tonic-gate 			goto out;
14947c478bd9Sstevel@tonic-gate 	}
1495108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1496108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1497108322fbScarlsonj 		error = Z_MISC_FS;
1498108322fbScarlsonj 		goto out;
1499108322fbScarlsonj 	}
1500108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
15017c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
15027c478bd9Sstevel@tonic-gate 		goto out;
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 
1505108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1506108322fbScarlsonj 		error = Z_MISC_FS;
1507108322fbScarlsonj 		goto out;
1508108322fbScarlsonj 	}
1509087719fdSdp 
1510087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1511087719fdSdp 	    "It is a snapshot of running zone state.\n");
1512087719fdSdp 
15137c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
15147c478bd9Sstevel@tonic-gate 
1515087719fdSdp 	stripcomments(handle);
1516087719fdSdp 
15177c478bd9Sstevel@tonic-gate out:
15187c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
15197c478bd9Sstevel@tonic-gate 	return (error);
15207c478bd9Sstevel@tonic-gate }
15217c478bd9Sstevel@tonic-gate 
1522f4b3ec61Sdh int
1523f4b3ec61Sdh zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep)
1524f4b3ec61Sdh {
1525f4b3ec61Sdh 	char property[10]; /* 10 is big enough for "shared"/"exclusive" */
1526f4b3ec61Sdh 	int err;
1527f4b3ec61Sdh 
1528f4b3ec61Sdh 	err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property));
1529f4b3ec61Sdh 	if (err == Z_BAD_PROPERTY) {
1530f4b3ec61Sdh 		/* Return default value */
1531f4b3ec61Sdh 		*iptypep = ZS_SHARED;
1532f4b3ec61Sdh 		return (Z_OK);
1533f4b3ec61Sdh 	} else if (err != Z_OK) {
1534f4b3ec61Sdh 		return (err);
1535f4b3ec61Sdh 	}
1536f4b3ec61Sdh 
1537f4b3ec61Sdh 	if (strlen(property) == 0 ||
1538f4b3ec61Sdh 	    strcmp(property, "shared") == 0)
1539f4b3ec61Sdh 		*iptypep = ZS_SHARED;
1540f4b3ec61Sdh 	else if (strcmp(property, "exclusive") == 0)
1541f4b3ec61Sdh 		*iptypep = ZS_EXCLUSIVE;
1542f4b3ec61Sdh 	else
1543f4b3ec61Sdh 		return (Z_INVAL);
1544f4b3ec61Sdh 
1545f4b3ec61Sdh 	return (Z_OK);
1546f4b3ec61Sdh }
1547f4b3ec61Sdh 
1548f4b3ec61Sdh int
1549f4b3ec61Sdh zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype)
1550f4b3ec61Sdh {
1551f4b3ec61Sdh 	xmlNodePtr cur;
1552f4b3ec61Sdh 
1553f4b3ec61Sdh 	if (handle == NULL)
1554f4b3ec61Sdh 		return (Z_INVAL);
1555f4b3ec61Sdh 
1556f4b3ec61Sdh 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
1557f4b3ec61Sdh 	if (cur == NULL) {
1558f4b3ec61Sdh 		return (Z_EMPTY_DOCUMENT);
1559f4b3ec61Sdh 	}
1560f4b3ec61Sdh 
1561f4b3ec61Sdh 	if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) {
1562f4b3ec61Sdh 		return (Z_WRONG_DOC_TYPE);
1563f4b3ec61Sdh 	}
1564f4b3ec61Sdh 	switch (iptype) {
1565f4b3ec61Sdh 	case ZS_SHARED:
1566f4b3ec61Sdh 		/*
1567f4b3ec61Sdh 		 * Since "shared" is the default, we don't write it to the
1568f4b3ec61Sdh 		 * configuration file, so that it's easier to migrate those
1569f4b3ec61Sdh 		 * zones elsewhere, eg., to systems which are not IP-Instances
1570f4b3ec61Sdh 		 * aware.
1571f4b3ec61Sdh 		 * xmlUnsetProp only fails when the attribute doesn't exist,
1572f4b3ec61Sdh 		 * which we don't care.
1573f4b3ec61Sdh 		 */
1574f4b3ec61Sdh 		(void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE);
1575f4b3ec61Sdh 		break;
1576f4b3ec61Sdh 	case ZS_EXCLUSIVE:
1577f4b3ec61Sdh 		if (xmlSetProp(cur, DTD_ATTR_IPTYPE,
1578f4b3ec61Sdh 		    (const xmlChar *) "exclusive") == NULL)
1579f4b3ec61Sdh 			return (Z_INVAL);
1580f4b3ec61Sdh 		break;
1581f4b3ec61Sdh 	}
1582f4b3ec61Sdh 	return (Z_OK);
1583f4b3ec61Sdh }
1584f4b3ec61Sdh 
15857c478bd9Sstevel@tonic-gate static int
1586a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
15877c478bd9Sstevel@tonic-gate {
15887c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
15917c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
15927c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
15937c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
15947c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
15957c478bd9Sstevel@tonic-gate 	}
15967c478bd9Sstevel@tonic-gate 	return (Z_OK);
15977c478bd9Sstevel@tonic-gate }
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate static int
16007c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
16017c478bd9Sstevel@tonic-gate {
16027c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
16037c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
16047c478bd9Sstevel@tonic-gate 	int err;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1607a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
16087c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
16097c478bd9Sstevel@tonic-gate 		return (err);
16107c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1611a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
16127c478bd9Sstevel@tonic-gate 		return (err);
1613a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16147c478bd9Sstevel@tonic-gate 		return (err);
1615a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
16167c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
16177c478bd9Sstevel@tonic-gate 		return (err);
16187c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
16197c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
16207c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
16217c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
16227c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1623a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
16247c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
16257c478bd9Sstevel@tonic-gate 				return (err);
16267c478bd9Sstevel@tonic-gate 		}
16277c478bd9Sstevel@tonic-gate 	}
16287c478bd9Sstevel@tonic-gate 	return (Z_OK);
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate int
16327c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate 	int err;
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16377c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16407c478bd9Sstevel@tonic-gate 		return (err);
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
16437c478bd9Sstevel@tonic-gate 		return (err);
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	return (Z_OK);
16467c478bd9Sstevel@tonic-gate }
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate static int
16497c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
16507c478bd9Sstevel@tonic-gate {
16517c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
16527c478bd9Sstevel@tonic-gate 	int err;
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1655a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16567c478bd9Sstevel@tonic-gate 		return (err);
16577c478bd9Sstevel@tonic-gate 	return (Z_OK);
16587c478bd9Sstevel@tonic-gate }
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate int
16617c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
16627c478bd9Sstevel@tonic-gate {
16637c478bd9Sstevel@tonic-gate 	int err;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16667c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16697c478bd9Sstevel@tonic-gate 		return (err);
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
16727c478bd9Sstevel@tonic-gate 		return (err);
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	return (Z_OK);
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate int
16787c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
16797c478bd9Sstevel@tonic-gate {
16807c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16837c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
16847c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
16857c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
16867c478bd9Sstevel@tonic-gate 	if (new == NULL)
16877c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
16887c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
16897c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
16907c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
16917c478bd9Sstevel@tonic-gate 	if (last == NULL)
16927c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
16937c478bd9Sstevel@tonic-gate 	else
16947c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
16957c478bd9Sstevel@tonic-gate 	return (Z_OK);
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate int
16997c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
17007c478bd9Sstevel@tonic-gate {
17017c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
17047c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
17057c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
17067c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
17077c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
17087c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
17097c478bd9Sstevel@tonic-gate 			else
17107c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
17117c478bd9Sstevel@tonic-gate 			free(this);
17127c478bd9Sstevel@tonic-gate 			return (Z_OK);
17137c478bd9Sstevel@tonic-gate 		} else
17147c478bd9Sstevel@tonic-gate 			last = this;
17157c478bd9Sstevel@tonic-gate 	}
17167c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate void
17207c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
17217c478bd9Sstevel@tonic-gate {
17227c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
17257c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
17267c478bd9Sstevel@tonic-gate 		free(this);
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate }
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate void
17317c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
17347c478bd9Sstevel@tonic-gate 		return;
17357c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
17367c478bd9Sstevel@tonic-gate 	free(valtab);
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate static boolean_t
17407c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
17417c478bd9Sstevel@tonic-gate {
17427c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
17437c478bd9Sstevel@tonic-gate 	int prop_result;
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
17467c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
17477c478bd9Sstevel@tonic-gate 		return (B_FALSE);
17487c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
17497c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
17507c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
17517c478bd9Sstevel@tonic-gate }
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate static int
17547c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
17557c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
17567c478bd9Sstevel@tonic-gate {
17577c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17587c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17617c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
17627c478bd9Sstevel@tonic-gate 			continue;
17637c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
17647c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
17657c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
17667c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
17677c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
17687c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
17697c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
17707c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
17717c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17727c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17737c478bd9Sstevel@tonic-gate 			return (Z_OK);
17747c478bd9Sstevel@tonic-gate 		}
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
17777c478bd9Sstevel@tonic-gate }
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate int
17807c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
17817c478bd9Sstevel@tonic-gate {
17827c478bd9Sstevel@tonic-gate 	int err;
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17857c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17887c478bd9Sstevel@tonic-gate 		return (err);
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
17917c478bd9Sstevel@tonic-gate 		return (err);
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	return (Z_OK);
17947c478bd9Sstevel@tonic-gate }
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate int
17977c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
17987c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
17997c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
18007c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
18017c478bd9Sstevel@tonic-gate {
18027c478bd9Sstevel@tonic-gate 	int err;
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18057c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18087c478bd9Sstevel@tonic-gate 		return (err);
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
18117c478bd9Sstevel@tonic-gate 		return (err);
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
18147c478bd9Sstevel@tonic-gate 		return (err);
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	return (Z_OK);
18177c478bd9Sstevel@tonic-gate }
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate static int
18207c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
18217c478bd9Sstevel@tonic-gate {
18227c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18257c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
18267c478bd9Sstevel@tonic-gate 			continue;
18277c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
18287c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
18297c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
18307c478bd9Sstevel@tonic-gate 			return (Z_OK);
18317c478bd9Sstevel@tonic-gate 		}
18327c478bd9Sstevel@tonic-gate 	}
18337c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
18347c478bd9Sstevel@tonic-gate }
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate int
18377c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
18387c478bd9Sstevel@tonic-gate {
18397c478bd9Sstevel@tonic-gate 	int err;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18427c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18457c478bd9Sstevel@tonic-gate 		return (err);
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
18487c478bd9Sstevel@tonic-gate 		return (err);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	return (Z_OK);
18517c478bd9Sstevel@tonic-gate }
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate int
18547c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
18557c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
18567c478bd9Sstevel@tonic-gate {
18577c478bd9Sstevel@tonic-gate 	int err;
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18607c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18637c478bd9Sstevel@tonic-gate 		return (err);
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
18667c478bd9Sstevel@tonic-gate 		return (err);
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
18697c478bd9Sstevel@tonic-gate 		return (err);
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	return (Z_OK);
18727c478bd9Sstevel@tonic-gate }
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate int
18757c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
18767c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
18777c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
18787c478bd9Sstevel@tonic-gate {
18797c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
18807c478bd9Sstevel@tonic-gate 	int err;
18817c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
18827c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
18837c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18867c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18897c478bd9Sstevel@tonic-gate 		return (err);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	/*
18927c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
18937c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
18947c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
18957c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
18967c478bd9Sstevel@tonic-gate 	 */
18977c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
18987c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
18997c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
19007c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
19017c478bd9Sstevel@tonic-gate 			continue;
19027c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
19037c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
19047c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
19057c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
19067c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
19077c478bd9Sstevel@tonic-gate 					firstmatch = cur;
19087c478bd9Sstevel@tonic-gate 				else
19097c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
19107c478bd9Sstevel@tonic-gate 			}
19117c478bd9Sstevel@tonic-gate 		}
19127c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
19137c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
19147c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
19157c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
19167c478bd9Sstevel@tonic-gate 				    special) == 0) {
19177c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19187c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19197c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19207c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19217c478bd9Sstevel@tonic-gate 				} else {
19227c478bd9Sstevel@tonic-gate 					/*
19237c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19247c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19257c478bd9Sstevel@tonic-gate 					 */
19267c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19277c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19287c478bd9Sstevel@tonic-gate 				}
19297c478bd9Sstevel@tonic-gate 			}
19307c478bd9Sstevel@tonic-gate 		}
19317c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
19327c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
19337c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
19347c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
19357c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19367c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19377c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19387c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19397c478bd9Sstevel@tonic-gate 				} else {
19407c478bd9Sstevel@tonic-gate 					/*
19417c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19427c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19437c478bd9Sstevel@tonic-gate 					 */
19447c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19457c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19467c478bd9Sstevel@tonic-gate 				}
19477c478bd9Sstevel@tonic-gate 			}
19487c478bd9Sstevel@tonic-gate 		}
19497c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
19507c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
19517c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
19527c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
19537c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19547c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19557c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19567c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19577c478bd9Sstevel@tonic-gate 				} else {
19587c478bd9Sstevel@tonic-gate 					/*
19597c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19607c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19617c478bd9Sstevel@tonic-gate 					 */
19627c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19637c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19647c478bd9Sstevel@tonic-gate 				}
19657c478bd9Sstevel@tonic-gate 			}
19667c478bd9Sstevel@tonic-gate 		}
19677c478bd9Sstevel@tonic-gate 	}
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
19707c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 	cur = firstmatch;
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
19757c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
19767c478bd9Sstevel@tonic-gate 		return (err);
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
19797c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
19807c478bd9Sstevel@tonic-gate 		return (err);
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
19837c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
19847c478bd9Sstevel@tonic-gate 		return (err);
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
19877c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
19887c478bd9Sstevel@tonic-gate 		return (err);
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	/* options are optional */
19917c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
19927c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
19937c478bd9Sstevel@tonic-gate 	    options = options->next) {
19947c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
19957c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
19967c478bd9Sstevel@tonic-gate 			break;
19977c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
19987c478bd9Sstevel@tonic-gate 			break;
19997c478bd9Sstevel@tonic-gate 	}
20007c478bd9Sstevel@tonic-gate 	return (Z_OK);
20017c478bd9Sstevel@tonic-gate }
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate int
20047c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
20057c478bd9Sstevel@tonic-gate {
20067c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
20077c478bd9Sstevel@tonic-gate 	int err;
20087c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
20117c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20147c478bd9Sstevel@tonic-gate 		return (err);
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	/*
20177c478bd9Sstevel@tonic-gate 	 * General algorithm:
20187c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
20197c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
20207c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
20217c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
20227c478bd9Sstevel@tonic-gate 	 */
20237c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
20247c478bd9Sstevel@tonic-gate 	match = NULL;
20257c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
20267c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
20277c478bd9Sstevel@tonic-gate 			continue;
20287c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
20297c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
20307c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
20317c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
20327c478bd9Sstevel@tonic-gate 				if (match == NULL)
20337c478bd9Sstevel@tonic-gate 					match = cur;
20347c478bd9Sstevel@tonic-gate 				else
20357c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20367c478bd9Sstevel@tonic-gate 			}
20377c478bd9Sstevel@tonic-gate 		}
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	if (match == NULL)
20417c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	cur = match;
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
20467c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
20477c478bd9Sstevel@tonic-gate 		return (err);
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	return (Z_OK);
20507c478bd9Sstevel@tonic-gate }
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate /*
20537c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
20547c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
20557c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
20567c478bd9Sstevel@tonic-gate  */
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate boolean_t
20597c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
20607c478bd9Sstevel@tonic-gate {
20617c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
20627c478bd9Sstevel@tonic-gate 	int result;
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
20657c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	/*
20687c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
20697c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
20707c478bd9Sstevel@tonic-gate 	 */
20717c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
20727c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
20737c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
20747c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
20757c478bd9Sstevel@tonic-gate 		return (B_FALSE);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	/*
20787c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
20797c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
20807c478bd9Sstevel@tonic-gate 	 * result of the comparison.
20817c478bd9Sstevel@tonic-gate 	 */
20827c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
20837c478bd9Sstevel@tonic-gate 	*slashp = '\0';
20847c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
20857c478bd9Sstevel@tonic-gate 	*slashp = '/';
20867c478bd9Sstevel@tonic-gate 	return ((result == 0));
20877c478bd9Sstevel@tonic-gate }
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate int
20907c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
20917c478bd9Sstevel@tonic-gate {
20927c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
20937c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
20947c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
20957c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
20987c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20997c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
21007c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
21017c478bd9Sstevel@tonic-gate 		*slashp = '\0';
21027c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
21037c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
21047c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
21057c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
21067c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
21077c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
21087c478bd9Sstevel@tonic-gate 	} else {
21097c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
21107c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
21117c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
21127c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
21137c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
21147c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
2115a1be23daSdp 
21167c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
21177c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
21187c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
21197c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
2120a1be23daSdp 
21217c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
21227c478bd9Sstevel@tonic-gate 	}
21237c478bd9Sstevel@tonic-gate 	return (Z_OK);
21247c478bd9Sstevel@tonic-gate }
21257c478bd9Sstevel@tonic-gate 
2126f4b3ec61Sdh boolean_t
2127f4b3ec61Sdh zonecfg_ifname_exists(sa_family_t af, char *ifname)
2128f4b3ec61Sdh {
2129f4b3ec61Sdh 	struct lifreq lifr;
2130f4b3ec61Sdh 	int so;
2131f4b3ec61Sdh 	int save_errno;
2132f4b3ec61Sdh 
2133f4b3ec61Sdh 	(void) memset(&lifr, 0, sizeof (lifr));
2134f4b3ec61Sdh 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2135f4b3ec61Sdh 	lifr.lifr_addr.ss_family = af;
2136f4b3ec61Sdh 	if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
2137f4b3ec61Sdh 		/* Odd - can't tell if the ifname exists */
2138f4b3ec61Sdh 		return (B_FALSE);
2139f4b3ec61Sdh 	}
2140f4b3ec61Sdh 	if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2141f4b3ec61Sdh 		save_errno = errno;
2142f4b3ec61Sdh 		(void) close(so);
2143f4b3ec61Sdh 		errno = save_errno;
2144f4b3ec61Sdh 		return (B_FALSE);
2145f4b3ec61Sdh 	}
2146f4b3ec61Sdh 	(void) close(so);
2147f4b3ec61Sdh 	return (B_TRUE);
2148f4b3ec61Sdh }
2149f4b3ec61Sdh 
21507c478bd9Sstevel@tonic-gate int
21517c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21527c478bd9Sstevel@tonic-gate {
21537c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
21547c478bd9Sstevel@tonic-gate 	int err;
21557c478bd9Sstevel@tonic-gate 	char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ];
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21587c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21617c478bd9Sstevel@tonic-gate 		return (err);
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
21647c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
21657c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
21667c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
21677c478bd9Sstevel@tonic-gate 			continue;
21687c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_physical) > 0) {
21697c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
21707c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
21717c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
21727c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
21737c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
21747c478bd9Sstevel@tonic-gate 					firstmatch = cur;
21757c478bd9Sstevel@tonic-gate 				else
21767c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
21777c478bd9Sstevel@tonic-gate 			}
21787c478bd9Sstevel@tonic-gate 		}
21797c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_address) > 0) {
21807c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
21817c478bd9Sstevel@tonic-gate 			    sizeof (address)) == Z_OK)) {
21827c478bd9Sstevel@tonic-gate 				if (zonecfg_same_net_address(
21837c478bd9Sstevel@tonic-gate 				    tabptr->zone_nwif_address, address)) {
21847c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
21857c478bd9Sstevel@tonic-gate 						firstmatch = cur;
21867c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
21877c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
21887c478bd9Sstevel@tonic-gate 				} else {
21897c478bd9Sstevel@tonic-gate 					/*
21907c478bd9Sstevel@tonic-gate 					 * If another property matched but this
21917c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
21927c478bd9Sstevel@tonic-gate 					 */
21937c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
21947c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
21957c478bd9Sstevel@tonic-gate 				}
21967c478bd9Sstevel@tonic-gate 			}
21977c478bd9Sstevel@tonic-gate 		}
21987c478bd9Sstevel@tonic-gate 	}
21997c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
22007c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	cur = firstmatch;
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
22057c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
22067c478bd9Sstevel@tonic-gate 		return (err);
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
22097c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
22107c478bd9Sstevel@tonic-gate 		return (err);
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	return (Z_OK);
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate static int
22167c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22177c478bd9Sstevel@tonic-gate {
22187c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
22197c478bd9Sstevel@tonic-gate 	int err;
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
2222a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
22237c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
22247c478bd9Sstevel@tonic-gate 		return (err);
2225a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
22267c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
22277c478bd9Sstevel@tonic-gate 		return (err);
22287c478bd9Sstevel@tonic-gate 	return (Z_OK);
22297c478bd9Sstevel@tonic-gate }
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate int
22327c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22337c478bd9Sstevel@tonic-gate {
22347c478bd9Sstevel@tonic-gate 	int err;
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22377c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22407c478bd9Sstevel@tonic-gate 		return (err);
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
22437c478bd9Sstevel@tonic-gate 		return (err);
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	return (Z_OK);
22467c478bd9Sstevel@tonic-gate }
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate static int
22497c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22507c478bd9Sstevel@tonic-gate {
22517c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
22527c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
22557c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
22567c478bd9Sstevel@tonic-gate 			continue;
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
22597c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
22607c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
22617c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
22647c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
22657c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
22667c478bd9Sstevel@tonic-gate 			return (Z_OK);
22677c478bd9Sstevel@tonic-gate 		}
22687c478bd9Sstevel@tonic-gate 	}
22697c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
22707c478bd9Sstevel@tonic-gate }
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate int
22737c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22747c478bd9Sstevel@tonic-gate {
22757c478bd9Sstevel@tonic-gate 	int err;
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22787c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22817c478bd9Sstevel@tonic-gate 		return (err);
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
22847c478bd9Sstevel@tonic-gate 		return (err);
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	return (Z_OK);
22877c478bd9Sstevel@tonic-gate }
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate int
22907c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
22917c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
22927c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
22937c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
22947c478bd9Sstevel@tonic-gate {
22957c478bd9Sstevel@tonic-gate 	int err;
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
22987c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23017c478bd9Sstevel@tonic-gate 		return (err);
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
23047c478bd9Sstevel@tonic-gate 		return (err);
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
23077c478bd9Sstevel@tonic-gate 		return (err);
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	return (Z_OK);
23107c478bd9Sstevel@tonic-gate }
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate int
23137c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23147c478bd9Sstevel@tonic-gate {
23157c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
23167c478bd9Sstevel@tonic-gate 	int err;
23177c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23207c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23237c478bd9Sstevel@tonic-gate 		return (err);
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
23267c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
23277c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23287c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
23297c478bd9Sstevel@tonic-gate 			continue;
23307c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
23317c478bd9Sstevel@tonic-gate 			continue;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
23347c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
23357c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
23367c478bd9Sstevel@tonic-gate 			    match) == 0) {
23377c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
23387c478bd9Sstevel@tonic-gate 					firstmatch = cur;
23397c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
23407c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
23417c478bd9Sstevel@tonic-gate 			} else {
23427c478bd9Sstevel@tonic-gate 				/*
23437c478bd9Sstevel@tonic-gate 				 * If another property matched but this
23447c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
23457c478bd9Sstevel@tonic-gate 				 */
23467c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
23477c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
23487c478bd9Sstevel@tonic-gate 			}
23497c478bd9Sstevel@tonic-gate 		}
23507c478bd9Sstevel@tonic-gate 	}
23517c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
23527c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 	cur = firstmatch;
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
23577c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
23587c478bd9Sstevel@tonic-gate 		return (err);
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 	return (Z_OK);
23617c478bd9Sstevel@tonic-gate }
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate static int
23647c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
23657c478bd9Sstevel@tonic-gate {
23667c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
23677c478bd9Sstevel@tonic-gate 	int err;
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
23707c478bd9Sstevel@tonic-gate 
2371a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
23727c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
23737c478bd9Sstevel@tonic-gate 		return (err);
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	return (Z_OK);
23767c478bd9Sstevel@tonic-gate }
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate int
23797c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23807c478bd9Sstevel@tonic-gate {
23817c478bd9Sstevel@tonic-gate 	int err;
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23847c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23877c478bd9Sstevel@tonic-gate 		return (err);
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
23907c478bd9Sstevel@tonic-gate 		return (err);
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	return (Z_OK);
23937c478bd9Sstevel@tonic-gate }
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate static int
23967c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
23977c478bd9Sstevel@tonic-gate {
2398facf4a8dSllai 	xmlNodePtr cur = handle->zone_dh_cur;
23997c478bd9Sstevel@tonic-gate 	int match_match;
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
24027c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
24037c478bd9Sstevel@tonic-gate 			continue;
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
24067c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 		if (match_match) {
24097c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
24107c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
24117c478bd9Sstevel@tonic-gate 			return (Z_OK);
24127c478bd9Sstevel@tonic-gate 		}
24137c478bd9Sstevel@tonic-gate 	}
24147c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
24157c478bd9Sstevel@tonic-gate }
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate int
24187c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
24197c478bd9Sstevel@tonic-gate {
24207c478bd9Sstevel@tonic-gate 	int err;
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
24237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24267c478bd9Sstevel@tonic-gate 		return (err);
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
24297c478bd9Sstevel@tonic-gate 		return (err);
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	return (Z_OK);
24327c478bd9Sstevel@tonic-gate }
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate int
24357c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
24367c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
24377c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
24387c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
24397c478bd9Sstevel@tonic-gate {
24407c478bd9Sstevel@tonic-gate 	int err;
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
24437c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24467c478bd9Sstevel@tonic-gate 		return (err);
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
24497c478bd9Sstevel@tonic-gate 		return (err);
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
24527c478bd9Sstevel@tonic-gate 		return (err);
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 	return (Z_OK);
24557c478bd9Sstevel@tonic-gate }
24567c478bd9Sstevel@tonic-gate 
2457ee519a1fSgjelinek /* Lock to serialize all zonecfg_devwalks */
2458ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2459ee519a1fSgjelinek /*
2460ee519a1fSgjelinek  * Global variables used to pass data from zonecfg_devwalk to the nftw
2461ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2462ee519a1fSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from zonecfg_devwalk.
2463ee519a1fSgjelinek  */
2464ee519a1fSgjelinek static void *g_devwalk_data;
2465ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2466ee519a1fSgjelinek     void *);
2467ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2468ee519a1fSgjelinek 
2469ee519a1fSgjelinek /*
2470ee519a1fSgjelinek  * This is the nftw call-back function used by zonecfg_devwalk.  It is
2471ee519a1fSgjelinek  * responsible for calling the actual call-back that is passed in to
2472ee519a1fSgjelinek  * zonecfg_devwalk as the *cb argument.
2473ee519a1fSgjelinek  */
2474ee519a1fSgjelinek /* ARGSUSED2 */
2475ee519a1fSgjelinek static int
2476ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2477ee519a1fSgjelinek     struct FTW *ftw)
2478ee519a1fSgjelinek {
2479ee519a1fSgjelinek 	acl_t *acl;
2480ee519a1fSgjelinek 	char *acl_txt = NULL;
2481ee519a1fSgjelinek 
2482ee519a1fSgjelinek 	/* skip all but character and block devices */
2483ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2484ee519a1fSgjelinek 		return (0);
2485ee519a1fSgjelinek 
2486ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2487ee519a1fSgjelinek 	    acl != NULL) {
2488ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2489ee519a1fSgjelinek 		acl_free(acl);
2490ee519a1fSgjelinek 	}
2491ee519a1fSgjelinek 
2492ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2493ee519a1fSgjelinek 		return (0);
2494ee519a1fSgjelinek 
2495ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2496ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2497ee519a1fSgjelinek 	    g_devwalk_data);
2498ee519a1fSgjelinek 	free(acl_txt);
2499ee519a1fSgjelinek 	return (0);
2500ee519a1fSgjelinek }
2501ee519a1fSgjelinek 
2502ee519a1fSgjelinek /*
2503ee519a1fSgjelinek  * Walk the dev tree for the zone specified by hdl and call the call-back (cb)
2504ee519a1fSgjelinek  * function for each entry in the tree.  The call-back will be passed the
2505ee519a1fSgjelinek  * name, uid, gid, mode, acl string and the void *data input parameter
2506ee519a1fSgjelinek  * for each dev entry.
2507ee519a1fSgjelinek  *
2508ee519a1fSgjelinek  * Data is passed to the zonecfg_devwalk_cb through the global variables
2509ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2510ee519a1fSgjelinek  * zonecfg_devwalk_cb function will actually call *cb.
2511ee519a1fSgjelinek  */
2512ee519a1fSgjelinek int
2513ee519a1fSgjelinek zonecfg_devwalk(zone_dochandle_t hdl,
2514ee519a1fSgjelinek     int (*cb)(const char *, uid_t, gid_t, mode_t, const char *, void *),
2515ee519a1fSgjelinek     void *data)
2516ee519a1fSgjelinek {
2517ee519a1fSgjelinek 	char path[MAXPATHLEN];
2518ee519a1fSgjelinek 	int ret;
2519ee519a1fSgjelinek 
2520ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2521ee519a1fSgjelinek 		return (ret);
2522ee519a1fSgjelinek 
2523ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2524ee519a1fSgjelinek 		return (Z_TOO_BIG);
2525ee519a1fSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2526ee519a1fSgjelinek 
2527ee519a1fSgjelinek 	/*
2528ee519a1fSgjelinek 	 * We have to serialize all zonecfg_devwalks in the same process
2529ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2530ee519a1fSgjelinek 	 */
2531ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2532ee519a1fSgjelinek 
2533ee519a1fSgjelinek 	g_devwalk_data = data;
2534ee519a1fSgjelinek 	g_devwalk_cb = cb;
2535ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2536ee519a1fSgjelinek 
2537ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2538ee519a1fSgjelinek 	return (Z_OK);
2539ee519a1fSgjelinek }
2540ee519a1fSgjelinek 
2541ee519a1fSgjelinek /*
2542ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2543ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2544ee519a1fSgjelinek  * attaching a migrated zone.
2545ee519a1fSgjelinek  */
2546ee519a1fSgjelinek int
2547ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2548ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2549ee519a1fSgjelinek {
2550ee519a1fSgjelinek 	int ret;
2551ee519a1fSgjelinek 	char path[MAXPATHLEN];
2552ee519a1fSgjelinek 	struct stat st;
2553ee519a1fSgjelinek 	acl_t *aclp;
2554ee519a1fSgjelinek 
2555ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2556ee519a1fSgjelinek 		return (ret);
2557ee519a1fSgjelinek 
2558ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2559ee519a1fSgjelinek 		return (Z_TOO_BIG);
2560ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2561ee519a1fSgjelinek 		return (Z_TOO_BIG);
2562ee519a1fSgjelinek 
2563ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2564ee519a1fSgjelinek 		return (Z_INVAL);
2565ee519a1fSgjelinek 
2566ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2567ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2568ee519a1fSgjelinek 		return (Z_INVAL);
2569ee519a1fSgjelinek 
2570ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2571ee519a1fSgjelinek 		return (Z_SYSTEM);
2572ee519a1fSgjelinek 
2573ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2574ee519a1fSgjelinek 		return (Z_SYSTEM);
2575ee519a1fSgjelinek 
2576ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2577ee519a1fSgjelinek 		return (Z_OK);
2578ee519a1fSgjelinek 
2579ee519a1fSgjelinek 	if (acl_fromtext(acltxt, &aclp) != 0)
2580ee519a1fSgjelinek 		return (Z_SYSTEM);
2581ee519a1fSgjelinek 
2582ee519a1fSgjelinek 	errno = 0;
2583ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2584ee519a1fSgjelinek 		free(aclp);
2585ee519a1fSgjelinek 		return (Z_SYSTEM);
2586ee519a1fSgjelinek 	}
2587ee519a1fSgjelinek 
2588ee519a1fSgjelinek 	free(aclp);
2589ee519a1fSgjelinek 	return (Z_OK);
2590ee519a1fSgjelinek }
2591ee519a1fSgjelinek 
25927c478bd9Sstevel@tonic-gate /*
25937c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
25947c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
25957c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
25967c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
25977c478bd9Sstevel@tonic-gate  *
25987c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
25997c478bd9Sstevel@tonic-gate  * aborts with an error.
26007c478bd9Sstevel@tonic-gate  */
26017c478bd9Sstevel@tonic-gate int
26027c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
26037c478bd9Sstevel@tonic-gate     void *priv) {
26047c478bd9Sstevel@tonic-gate 	FILE *mnttab;
26057c478bd9Sstevel@tonic-gate 	struct mnttab m;
26067c478bd9Sstevel@tonic-gate 	size_t l;
260707b574eeSgjelinek 	int zfsl;
26087c478bd9Sstevel@tonic-gate 	int rv = 0;
260907b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
26127c478bd9Sstevel@tonic-gate 
261307b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
261407b574eeSgjelinek 	    >= sizeof (zfs_path))
261507b574eeSgjelinek 		return (-1);
261607b574eeSgjelinek 
26177c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
26227c478bd9Sstevel@tonic-gate 		return (-1);
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
26257c478bd9Sstevel@tonic-gate 		rv = -1;
26267c478bd9Sstevel@tonic-gate 		goto out;
26277c478bd9Sstevel@tonic-gate 	}
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
26307c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
263107b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
263207b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
26337c478bd9Sstevel@tonic-gate 			rv++;
26347c478bd9Sstevel@tonic-gate 			if (callback == NULL)
26357c478bd9Sstevel@tonic-gate 				continue;
26367c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
26377c478bd9Sstevel@tonic-gate 				rv = -1;
26387c478bd9Sstevel@tonic-gate 				goto out;
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 			}
26417c478bd9Sstevel@tonic-gate 		}
26427c478bd9Sstevel@tonic-gate 	}
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate out:
26457c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
26467c478bd9Sstevel@tonic-gate 	return (rv);
26477c478bd9Sstevel@tonic-gate }
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate int
26507c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26517c478bd9Sstevel@tonic-gate {
26527c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
26537c478bd9Sstevel@tonic-gate 	int err;
26547c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
26577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
26607c478bd9Sstevel@tonic-gate 		return (err);
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
26637c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
26647c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
26657c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
26667c478bd9Sstevel@tonic-gate 			continue;
26677c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
26687c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
26697c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
26707c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
26717c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
26727c478bd9Sstevel@tonic-gate 					firstmatch = cur;
26737c478bd9Sstevel@tonic-gate 				else
26747c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
26757c478bd9Sstevel@tonic-gate 			}
26767c478bd9Sstevel@tonic-gate 		}
26777c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
26787c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
26797c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
26807c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
26817c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
26827c478bd9Sstevel@tonic-gate 						firstmatch = cur;
26837c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
26847c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
26857c478bd9Sstevel@tonic-gate 				} else {
26867c478bd9Sstevel@tonic-gate 					/*
26877c478bd9Sstevel@tonic-gate 					 * If another property matched but this
26887c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
26897c478bd9Sstevel@tonic-gate 					 */
26907c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
26917c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
26927c478bd9Sstevel@tonic-gate 				}
26937c478bd9Sstevel@tonic-gate 			}
26947c478bd9Sstevel@tonic-gate 		}
26957c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
26967c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
26977c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
26987c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
26997c478bd9Sstevel@tonic-gate 				    0) {
27007c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
27017c478bd9Sstevel@tonic-gate 						firstmatch = cur;
27027c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
27037c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
27047c478bd9Sstevel@tonic-gate 				} else {
27057c478bd9Sstevel@tonic-gate 					/*
27067c478bd9Sstevel@tonic-gate 					 * If another property matched but this
27077c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
27087c478bd9Sstevel@tonic-gate 					 */
27097c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
27107c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
27117c478bd9Sstevel@tonic-gate 				}
27127c478bd9Sstevel@tonic-gate 			}
27137c478bd9Sstevel@tonic-gate 		}
27147c478bd9Sstevel@tonic-gate 	}
27157c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
27167c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	cur = firstmatch;
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
27217c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
27227c478bd9Sstevel@tonic-gate 		return (err);
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
27257c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
27267c478bd9Sstevel@tonic-gate 		return (err);
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
27297c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
27307c478bd9Sstevel@tonic-gate 		return (err);
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 	return (Z_OK);
27337c478bd9Sstevel@tonic-gate }
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate static int
27367c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27377c478bd9Sstevel@tonic-gate {
27387c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
27397c478bd9Sstevel@tonic-gate 	int err;
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2742a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
27437c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27447c478bd9Sstevel@tonic-gate 		return (err);
2745a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
27467c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27477c478bd9Sstevel@tonic-gate 		return (err);
2748a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
27497c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27507c478bd9Sstevel@tonic-gate 		return (err);
27517c478bd9Sstevel@tonic-gate 	return (Z_OK);
27527c478bd9Sstevel@tonic-gate }
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate int
27557c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27567c478bd9Sstevel@tonic-gate {
27577c478bd9Sstevel@tonic-gate 	int err;
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
27607c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27637c478bd9Sstevel@tonic-gate 		return (err);
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
27667c478bd9Sstevel@tonic-gate 		return (err);
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 	return (Z_OK);
27697c478bd9Sstevel@tonic-gate }
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate static int
27727c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27737c478bd9Sstevel@tonic-gate {
27747c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
27757c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
27787c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
27797c478bd9Sstevel@tonic-gate 			continue;
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
27827c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
27837c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
27847c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
27857c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
27867c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
27877c478bd9Sstevel@tonic-gate 
27887c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
27897c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
27907c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
27917c478bd9Sstevel@tonic-gate 			return (Z_OK);
27927c478bd9Sstevel@tonic-gate 		}
27937c478bd9Sstevel@tonic-gate 	}
27947c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
27957c478bd9Sstevel@tonic-gate }
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate int
27987c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27997c478bd9Sstevel@tonic-gate {
28007c478bd9Sstevel@tonic-gate 	int err;
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
28037c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28067c478bd9Sstevel@tonic-gate 		return (err);
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
28097c478bd9Sstevel@tonic-gate 		return (err);
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	return (Z_OK);
28127c478bd9Sstevel@tonic-gate }
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate int
28157c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
28167c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
28177c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
28187c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
28197c478bd9Sstevel@tonic-gate {
28207c478bd9Sstevel@tonic-gate 	int err;
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
28237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28267c478bd9Sstevel@tonic-gate 		return (err);
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
28297c478bd9Sstevel@tonic-gate 		return (err);
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
28327c478bd9Sstevel@tonic-gate 		return (err);
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	return (Z_OK);
28357c478bd9Sstevel@tonic-gate }
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate int
28387c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
28397c478bd9Sstevel@tonic-gate {
28407c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28417c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
28447c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
28477c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
28487c478bd9Sstevel@tonic-gate 		return (Z_OK);
28497c478bd9Sstevel@tonic-gate 	}
28507c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
28517c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
28527c478bd9Sstevel@tonic-gate 		return (Z_OK);
28537c478bd9Sstevel@tonic-gate 	}
28547c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
28557c478bd9Sstevel@tonic-gate }
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate int
28587c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
28597c478bd9Sstevel@tonic-gate {
28607c478bd9Sstevel@tonic-gate 	long long result;
28617c478bd9Sstevel@tonic-gate 	char *endptr;
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28647c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
28677c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 	errno = 0;
28707c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
28717c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
28727c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28737c478bd9Sstevel@tonic-gate 	*value = result;
28747c478bd9Sstevel@tonic-gate 	return (Z_OK);
28757c478bd9Sstevel@tonic-gate }
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate int
28787c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
28797c478bd9Sstevel@tonic-gate     size_t val_sz)
28807c478bd9Sstevel@tonic-gate {
28817c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28827c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
28857c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
28887c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
28897c478bd9Sstevel@tonic-gate 	return (Z_OK);
28907c478bd9Sstevel@tonic-gate }
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate int
28937c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
28947c478bd9Sstevel@tonic-gate {
28957c478bd9Sstevel@tonic-gate 	unsigned long long result;
28967c478bd9Sstevel@tonic-gate 	long long neg_result;
28977c478bd9Sstevel@tonic-gate 	char *endptr;
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 	if (attr == NULL)
29007c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
29037c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	errno = 0;
29067c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
29077c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
29087c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29097c478bd9Sstevel@tonic-gate 	errno = 0;
29107c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
29117c478bd9Sstevel@tonic-gate 	/*
29127c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
29137c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
29147c478bd9Sstevel@tonic-gate 	 * need to look for this here.
29157c478bd9Sstevel@tonic-gate 	 */
29167c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
29177c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29187c478bd9Sstevel@tonic-gate 	*value = result;
29197c478bd9Sstevel@tonic-gate 	return (Z_OK);
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate int
29237c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29247c478bd9Sstevel@tonic-gate {
29257c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
29267c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
29277c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29287c478bd9Sstevel@tonic-gate 	int err;
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	if (tabptr->zone_rctl_name == NULL ||
29317c478bd9Sstevel@tonic-gate 	    strlen(tabptr->zone_rctl_name) == 0)
29327c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29337c478bd9Sstevel@tonic-gate 
29347c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29357c478bd9Sstevel@tonic-gate 		return (err);
29367c478bd9Sstevel@tonic-gate 
29377c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
29387c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
29397c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
29407c478bd9Sstevel@tonic-gate 			continue;
29417c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
29427c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
29437c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
29447c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
29457c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
29467c478bd9Sstevel@tonic-gate 			    val = val->next) {
29477c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
29487c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
29497c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
29507c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
29517c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
29527c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
29537c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
29547c478bd9Sstevel@tonic-gate 				    Z_OK))
29557c478bd9Sstevel@tonic-gate 					break;
29567c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
29577c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
29587c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
29597c478bd9Sstevel@tonic-gate 				    Z_OK))
29607c478bd9Sstevel@tonic-gate 					break;
29617c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
29627c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
29637c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
29647c478bd9Sstevel@tonic-gate 				    Z_OK))
29657c478bd9Sstevel@tonic-gate 					break;
29667c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
29677c478bd9Sstevel@tonic-gate 				    Z_OK)
29687c478bd9Sstevel@tonic-gate 					break;
29697c478bd9Sstevel@tonic-gate 			}
29707c478bd9Sstevel@tonic-gate 			return (Z_OK);
29717c478bd9Sstevel@tonic-gate 		}
29727c478bd9Sstevel@tonic-gate 	}
29737c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
29747c478bd9Sstevel@tonic-gate }
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate static int
29777c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29787c478bd9Sstevel@tonic-gate {
29797c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
29807c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29817c478bd9Sstevel@tonic-gate 	int err;
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
2984a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
29857c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
29867c478bd9Sstevel@tonic-gate 		return (err);
29877c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
29887c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
29897c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
29907c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
2991a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
29927c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
29937c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
29947c478bd9Sstevel@tonic-gate 			return (err);
2995a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
29967c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
29977c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
29987c478bd9Sstevel@tonic-gate 			return (err);
2999a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
30007c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
30017c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
30027c478bd9Sstevel@tonic-gate 			return (err);
30037c478bd9Sstevel@tonic-gate 	}
30047c478bd9Sstevel@tonic-gate 	return (Z_OK);
30057c478bd9Sstevel@tonic-gate }
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate int
30087c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30097c478bd9Sstevel@tonic-gate {
30107c478bd9Sstevel@tonic-gate 	int err;
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
30137c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30147c478bd9Sstevel@tonic-gate 
30157c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30167c478bd9Sstevel@tonic-gate 		return (err);
30177c478bd9Sstevel@tonic-gate 
30187c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
30197c478bd9Sstevel@tonic-gate 		return (err);
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 	return (Z_OK);
30227c478bd9Sstevel@tonic-gate }
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate static int
30257c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30267c478bd9Sstevel@tonic-gate {
30277c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
30287c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
30297c478bd9Sstevel@tonic-gate 	int name_result;
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
30327c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
30337c478bd9Sstevel@tonic-gate 			continue;
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
30367c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
30377c478bd9Sstevel@tonic-gate 			continue;
30387c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
30397c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
30407c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
30437c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
30447c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
30457c478bd9Sstevel@tonic-gate 			return (Z_OK);
30467c478bd9Sstevel@tonic-gate 		}
30477c478bd9Sstevel@tonic-gate 	}
30487c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
30497c478bd9Sstevel@tonic-gate }
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate int
30527c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30537c478bd9Sstevel@tonic-gate {
30547c478bd9Sstevel@tonic-gate 	int err;
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
30577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30607c478bd9Sstevel@tonic-gate 		return (err);
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
30637c478bd9Sstevel@tonic-gate 		return (err);
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	return (Z_OK);
30667c478bd9Sstevel@tonic-gate }
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate int
30697c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
30707c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
30717c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
30727c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
30737c478bd9Sstevel@tonic-gate {
30747c478bd9Sstevel@tonic-gate 	int err;
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || oldtabptr->zone_rctl_name == NULL ||
30777c478bd9Sstevel@tonic-gate 	    newtabptr == NULL || newtabptr->zone_rctl_name == NULL)
30787c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30817c478bd9Sstevel@tonic-gate 		return (err);
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
30847c478bd9Sstevel@tonic-gate 		return (err);
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
30877c478bd9Sstevel@tonic-gate 		return (err);
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	return (Z_OK);
30907c478bd9Sstevel@tonic-gate }
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate int
30937c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
30947c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
30957c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
30967c478bd9Sstevel@tonic-gate {
30977c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
30987c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
31017c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
31027c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
31037c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
31047c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
31057c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
31067c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31077c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
31087c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31097c478bd9Sstevel@tonic-gate 	if (last == NULL)
31107c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
31117c478bd9Sstevel@tonic-gate 	else
31127c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
31137c478bd9Sstevel@tonic-gate 	return (Z_OK);
31147c478bd9Sstevel@tonic-gate }
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate int
31177c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
31187c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
31197c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
31207c478bd9Sstevel@tonic-gate {
31217c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
31247c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
31257c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
31267c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
31277c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
31287c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
31297c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
31307c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
31317c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
31327c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
31337c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
31347c478bd9Sstevel@tonic-gate 			else
31357c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
31367c478bd9Sstevel@tonic-gate 			free(this);
31377c478bd9Sstevel@tonic-gate 			return (Z_OK);
31387c478bd9Sstevel@tonic-gate 		} else
31397c478bd9Sstevel@tonic-gate 			last = this;
31407c478bd9Sstevel@tonic-gate 	}
31417c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
31427c478bd9Sstevel@tonic-gate }
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate char *
31457c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
31467c478bd9Sstevel@tonic-gate {
31477c478bd9Sstevel@tonic-gate 	switch (errnum) {
31487c478bd9Sstevel@tonic-gate 	case Z_OK:
31497c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
31507c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
31517c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
31527c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
31537c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
31547c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
31557c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
31567c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
31577c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31587c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
31597c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
31607c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
31617c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
31627c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
31637c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
31647c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
31657c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
31667c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
31677c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
31687c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
31697c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
31707c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
31717c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
31727c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
31737c478bd9Sstevel@tonic-gate 	case Z_INVAL:
31747c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
31757c478bd9Sstevel@tonic-gate 	case Z_ACCES:
31767c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
31777c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
31787c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
31797c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
31807c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31817c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
31827c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
31837c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
31847c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
31857c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
31867c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
31877c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
31887c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
31897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
31907c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
31917c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3192087719fdSdp 	case Z_BAD_ZONE_STATE:
31937c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
3194087719fdSdp 		    "Zone state is invalid for the requested operation"));
31957c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
31967c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3197087719fdSdp 	case Z_NAME_IN_USE:
3198087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
31997c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
32007c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
32017c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
32027c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
32037c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
32047c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
32057c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
32067c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
32077c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
32087c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
32097c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
32107c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
32117c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
32127c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32137c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
32147c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
32157c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32167c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3217ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3218ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3219ffbafc53Scomay 		    "Specified privilege is prohibited"));
3220ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3221ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3222ffbafc53Scomay 		    "Required privilege is missing"));
3223ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3224ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3225ffbafc53Scomay 		    "Specified privilege is unknown"));
32269acbbeafSnn 	case Z_BRAND_ERROR:
32279acbbeafSnn 		return (dgettext(TEXT_DOMAIN,
32289acbbeafSnn 		    "Brand-specific error"));
32290209230bSgjelinek 	case Z_INCOMPATIBLE:
32300209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Incompatible settings"));
32310209230bSgjelinek 	case Z_ALIAS_DISALLOW:
32320209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32330209230bSgjelinek 		    "An incompatible rctl already exists for this property"));
32340209230bSgjelinek 	case Z_CLEAR_DISALLOW:
32350209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32360209230bSgjelinek 		    "Clearing this property is not allowed"));
32370209230bSgjelinek 	case Z_POOL:
32380209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error"));
32390209230bSgjelinek 	case Z_POOLS_NOT_ACTIVE:
32400209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Pools facility not active; "
32410209230bSgjelinek 		    "zone will not be bound to pool"));
32420209230bSgjelinek 	case Z_POOL_ENABLE:
32430209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32440209230bSgjelinek 		    "Could not enable pools facility"));
32450209230bSgjelinek 	case Z_NO_POOL:
32460209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32470209230bSgjelinek 		    "Pool not found; using default pool"));
32480209230bSgjelinek 	case Z_POOL_CREATE:
32490209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32500209230bSgjelinek 		    "Could not create a temporary pool"));
32510209230bSgjelinek 	case Z_POOL_BIND:
32520209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool"));
32537c478bd9Sstevel@tonic-gate 	default:
32547c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
32557c478bd9Sstevel@tonic-gate 	}
32567c478bd9Sstevel@tonic-gate }
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate /*
32597c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
32607c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
32617c478bd9Sstevel@tonic-gate  */
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate static int
32647c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
32657c478bd9Sstevel@tonic-gate {
32667c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
32677c478bd9Sstevel@tonic-gate 	int err;
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32707c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
32737c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
32747c478bd9Sstevel@tonic-gate 		return (err);
32757c478bd9Sstevel@tonic-gate 	}
32767c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
32777c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
32787c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
32797c478bd9Sstevel@tonic-gate 	return (Z_OK);
32807c478bd9Sstevel@tonic-gate }
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate static int
32837c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
32847c478bd9Sstevel@tonic-gate {
32857c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32867c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
32897c478bd9Sstevel@tonic-gate 	return (Z_OK);
32907c478bd9Sstevel@tonic-gate }
32917c478bd9Sstevel@tonic-gate 
32920209230bSgjelinek /*
32930209230bSgjelinek  * Do the work required to manipulate a process through libproc.
32940209230bSgjelinek  * If grab_process() returns no errors (0), then release_process()
32950209230bSgjelinek  * must eventually be called.
32960209230bSgjelinek  *
32970209230bSgjelinek  * Return values:
32980209230bSgjelinek  *      0 Successful creation of agent thread
32990209230bSgjelinek  *      1 Error grabbing
33000209230bSgjelinek  *      2 Error creating agent
33010209230bSgjelinek  */
33020209230bSgjelinek static int
33030209230bSgjelinek grab_process(pr_info_handle_t *p)
33047c478bd9Sstevel@tonic-gate {
33050209230bSgjelinek 	int ret;
33060209230bSgjelinek 
33070209230bSgjelinek 	if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) {
33080209230bSgjelinek 
33090209230bSgjelinek 		if (Psetflags(p->pr, PR_RLC) != 0) {
33100209230bSgjelinek 			Prelease(p->pr, 0);
33110209230bSgjelinek 			return (1);
33120209230bSgjelinek 		}
33130209230bSgjelinek 		if (Pcreate_agent(p->pr) == 0) {
33140209230bSgjelinek 			return (0);
33150209230bSgjelinek 
33160209230bSgjelinek 		} else {
33170209230bSgjelinek 			Prelease(p->pr, 0);
33180209230bSgjelinek 			return (2);
33190209230bSgjelinek 		}
33200209230bSgjelinek 	} else {
33210209230bSgjelinek 		return (1);
33220209230bSgjelinek 	}
33237c478bd9Sstevel@tonic-gate }
33247c478bd9Sstevel@tonic-gate 
33250209230bSgjelinek /*
33260209230bSgjelinek  * Release the specified process. This destroys the agent
33270209230bSgjelinek  * and releases the process. If the process is NULL, nothing
33280209230bSgjelinek  * is done. This function should only be called if grab_process()
33290209230bSgjelinek  * has previously been called and returned success.
33300209230bSgjelinek  *
33310209230bSgjelinek  * This function is Pgrab-safe.
33320209230bSgjelinek  */
33330209230bSgjelinek static void
33340209230bSgjelinek release_process(struct ps_prochandle *Pr)
33357c478bd9Sstevel@tonic-gate {
33360209230bSgjelinek 	if (Pr == NULL)
33370209230bSgjelinek 		return;
33387c478bd9Sstevel@tonic-gate 
33390209230bSgjelinek 	Pdestroy_agent(Pr);
33400209230bSgjelinek 	Prelease(Pr, 0);
33410209230bSgjelinek }
33427c478bd9Sstevel@tonic-gate 
33430209230bSgjelinek static boolean_t
33440209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p)
33450209230bSgjelinek {
33460209230bSgjelinek 	DIR *dirp;
33470209230bSgjelinek 	struct dirent *dentp;
33480209230bSgjelinek 	zoneid_t zoneid;
33490209230bSgjelinek 	int pid_self;
33500209230bSgjelinek 	psinfo_t psinfo;
33517c478bd9Sstevel@tonic-gate 
33520209230bSgjelinek 	if (zone_get_id(zonename, &zoneid) != 0)
33530209230bSgjelinek 		return (B_FALSE);
33547c478bd9Sstevel@tonic-gate 
33550209230bSgjelinek 	pid_self = getpid();
33567c478bd9Sstevel@tonic-gate 
33570209230bSgjelinek 	if ((dirp = opendir("/proc")) == NULL)
33580209230bSgjelinek 		return (B_FALSE);
33597c478bd9Sstevel@tonic-gate 
33600209230bSgjelinek 	while (dentp = readdir(dirp)) {
33610209230bSgjelinek 		p->pid = atoi(dentp->d_name);
33627c478bd9Sstevel@tonic-gate 
33630209230bSgjelinek 		/* Skip self */
33640209230bSgjelinek 		if (p->pid == pid_self)
33650209230bSgjelinek 			continue;
33667c478bd9Sstevel@tonic-gate 
33670209230bSgjelinek 		if (proc_get_psinfo(p->pid, &psinfo) != 0)
33680209230bSgjelinek 			continue;
33690209230bSgjelinek 
33700209230bSgjelinek 		if (psinfo.pr_zoneid != zoneid)
33710209230bSgjelinek 			continue;
33720209230bSgjelinek 
33730209230bSgjelinek 		/* attempt to grab process */
33740209230bSgjelinek 		if (grab_process(p) != 0)
33750209230bSgjelinek 			continue;
33760209230bSgjelinek 
33770209230bSgjelinek 		if (pr_getzoneid(p->pr) != zoneid) {
33780209230bSgjelinek 			release_process(p->pr);
33790209230bSgjelinek 			continue;
33800209230bSgjelinek 		}
33810209230bSgjelinek 
33820209230bSgjelinek 		(void) closedir(dirp);
33830209230bSgjelinek 		return (B_TRUE);
33847c478bd9Sstevel@tonic-gate 	}
33857c478bd9Sstevel@tonic-gate 
33860209230bSgjelinek 	(void) closedir(dirp);
33870209230bSgjelinek 	return (B_FALSE);
33887c478bd9Sstevel@tonic-gate }
33897c478bd9Sstevel@tonic-gate 
33900209230bSgjelinek static boolean_t
33910209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk)
33927c478bd9Sstevel@tonic-gate {
33930209230bSgjelinek 	if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST))
33940209230bSgjelinek 		return (B_FALSE);
33957c478bd9Sstevel@tonic-gate 
33960209230bSgjelinek 	if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
33970209230bSgjelinek 		return (B_TRUE);
33980209230bSgjelinek 
33990209230bSgjelinek 	while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) {
34000209230bSgjelinek 		if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
34010209230bSgjelinek 			return (B_TRUE);
34020209230bSgjelinek 	}
34030209230bSgjelinek 
34040209230bSgjelinek 	return (B_FALSE);
34057c478bd9Sstevel@tonic-gate }
34067c478bd9Sstevel@tonic-gate 
34070209230bSgjelinek /*
34080209230bSgjelinek  * Apply the current rctl settings to the specified, running zone.
34090209230bSgjelinek  */
34107c478bd9Sstevel@tonic-gate int
34110209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle)
34127c478bd9Sstevel@tonic-gate {
34137c478bd9Sstevel@tonic-gate 	int err;
34140209230bSgjelinek 	int res = Z_OK;
34150209230bSgjelinek 	rctlblk_t *rblk;
34160209230bSgjelinek 	pr_info_handle_t p;
34170209230bSgjelinek 	struct zone_rctltab rctl;
34187c478bd9Sstevel@tonic-gate 
34190209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK)
34200209230bSgjelinek 		return (err);
34217c478bd9Sstevel@tonic-gate 
34220209230bSgjelinek 	if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) {
34230209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
34240209230bSgjelinek 		return (Z_NOMEM);
34250209230bSgjelinek 	}
34267c478bd9Sstevel@tonic-gate 
34270209230bSgjelinek 	if (!grab_zone_proc(zone_name, &p)) {
34280209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
34290209230bSgjelinek 		free(rblk);
34300209230bSgjelinek 		return (Z_SYSTEM);
34317c478bd9Sstevel@tonic-gate 	}
34327c478bd9Sstevel@tonic-gate 
34330209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
34340209230bSgjelinek 		char *rname;
34350209230bSgjelinek 		struct zone_rctlvaltab *valptr;
34360209230bSgjelinek 
34370209230bSgjelinek 		rname = rctl.zone_rctl_name;
34380209230bSgjelinek 
34390209230bSgjelinek 		/* first delete all current privileged settings for this rctl */
34400209230bSgjelinek 		while (get_priv_rctl(p.pr, rname, rblk)) {
34410209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) !=
34420209230bSgjelinek 			    0) {
34430209230bSgjelinek 				res = Z_SYSTEM;
34440209230bSgjelinek 				goto done;
34450209230bSgjelinek 			}
34460209230bSgjelinek 		}
34470209230bSgjelinek 
34480209230bSgjelinek 		/* now set each new value for the rctl */
34490209230bSgjelinek 		for (valptr = rctl.zone_rctl_valptr; valptr != NULL;
34500209230bSgjelinek 		    valptr = valptr->zone_rctlval_next) {
34510209230bSgjelinek 			if ((err = zonecfg_construct_rctlblk(valptr, rblk))
34520209230bSgjelinek 			    != Z_OK) {
34530209230bSgjelinek 				res = errno = err;
34540209230bSgjelinek 				goto done;
34550209230bSgjelinek 			}
34560209230bSgjelinek 
34570209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) {
34580209230bSgjelinek 				res = Z_SYSTEM;
34590209230bSgjelinek 				goto done;
34600209230bSgjelinek 			}
34610209230bSgjelinek 		}
34627c478bd9Sstevel@tonic-gate 	}
34637c478bd9Sstevel@tonic-gate 
34640209230bSgjelinek done:
34650209230bSgjelinek 	release_process(p.pr);
34660209230bSgjelinek 	free(rblk);
34670209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
34680209230bSgjelinek 
34690209230bSgjelinek 	return (res);
34707c478bd9Sstevel@tonic-gate }
34717c478bd9Sstevel@tonic-gate 
34720209230bSgjelinek static const xmlChar *
34730209230bSgjelinek nm_to_dtd(char *nm)
34747c478bd9Sstevel@tonic-gate {
34750209230bSgjelinek 	if (strcmp(nm, "device") == 0)
34760209230bSgjelinek 		return (DTD_ELEM_DEVICE);
34770209230bSgjelinek 	if (strcmp(nm, "fs") == 0)
34780209230bSgjelinek 		return (DTD_ELEM_FS);
34790209230bSgjelinek 	if (strcmp(nm, "inherit-pkg-dir") == 0)
34800209230bSgjelinek 		return (DTD_ELEM_IPD);
34810209230bSgjelinek 	if (strcmp(nm, "net") == 0)
34820209230bSgjelinek 		return (DTD_ELEM_NET);
34830209230bSgjelinek 	if (strcmp(nm, "attr") == 0)
34840209230bSgjelinek 		return (DTD_ELEM_ATTR);
34850209230bSgjelinek 	if (strcmp(nm, "rctl") == 0)
34860209230bSgjelinek 		return (DTD_ELEM_RCTL);
34870209230bSgjelinek 	if (strcmp(nm, "dataset") == 0)
34880209230bSgjelinek 		return (DTD_ELEM_DATASET);
34890209230bSgjelinek 
34900209230bSgjelinek 	return (NULL);
34917c478bd9Sstevel@tonic-gate }
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate int
34940209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc)
34957c478bd9Sstevel@tonic-gate {
34960209230bSgjelinek 	int num = 0;
34970209230bSgjelinek 	const xmlChar *dtd;
34980209230bSgjelinek 	xmlNodePtr cur;
34990209230bSgjelinek 
35000209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
35010209230bSgjelinek 		return (num);
35020209230bSgjelinek 
35030209230bSgjelinek 	if (zonecfg_setent(handle) != Z_OK)
35040209230bSgjelinek 		return (num);
35050209230bSgjelinek 
35060209230bSgjelinek 	for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next)
35070209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd) == 0)
35080209230bSgjelinek 			num++;
35090209230bSgjelinek 
35100209230bSgjelinek 	(void) zonecfg_endent(handle);
35110209230bSgjelinek 
35120209230bSgjelinek 	return (num);
35137c478bd9Sstevel@tonic-gate }
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate int
35160209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc)
35177c478bd9Sstevel@tonic-gate {
35187c478bd9Sstevel@tonic-gate 	int err;
35190209230bSgjelinek 	const xmlChar *dtd;
35200209230bSgjelinek 	xmlNodePtr cur;
35217c478bd9Sstevel@tonic-gate 
35220209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
35230209230bSgjelinek 		return (Z_NO_RESOURCE_TYPE);
35247c478bd9Sstevel@tonic-gate 
35250209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
35260209230bSgjelinek 		return (err);
35277c478bd9Sstevel@tonic-gate 
35280209230bSgjelinek 	cur = handle->zone_dh_cur;
35290209230bSgjelinek 	while (cur != NULL) {
35300209230bSgjelinek 		xmlNodePtr tmp;
35317c478bd9Sstevel@tonic-gate 
35320209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd)) {
35330209230bSgjelinek 			cur = cur->next;
35340209230bSgjelinek 			continue;
35350209230bSgjelinek 		}
35367c478bd9Sstevel@tonic-gate 
35370209230bSgjelinek 		tmp = cur->next;
35380209230bSgjelinek 		xmlUnlinkNode(cur);
35390209230bSgjelinek 		xmlFreeNode(cur);
35400209230bSgjelinek 		cur = tmp;
35417c478bd9Sstevel@tonic-gate 	}
35427c478bd9Sstevel@tonic-gate 
35430209230bSgjelinek 	(void) zonecfg_endent(handle);
35447c478bd9Sstevel@tonic-gate 	return (Z_OK);
35457c478bd9Sstevel@tonic-gate }
35467c478bd9Sstevel@tonic-gate 
35470209230bSgjelinek static boolean_t
35480209230bSgjelinek valid_uint(char *s, uint64_t *n)
35497c478bd9Sstevel@tonic-gate {
35500209230bSgjelinek 	char *endp;
35517c478bd9Sstevel@tonic-gate 
35520209230bSgjelinek 	/* strtoull accepts '-'?! so we want to flag that as an error */
35530209230bSgjelinek 	if (strchr(s, '-') != NULL)
35540209230bSgjelinek 		return (B_FALSE);
35550209230bSgjelinek 
35560209230bSgjelinek 	errno = 0;
35570209230bSgjelinek 	*n = strtoull(s, &endp, 10);
35580209230bSgjelinek 
35590209230bSgjelinek 	if (errno != 0 || *endp != '\0')
35600209230bSgjelinek 		return (B_FALSE);
35610209230bSgjelinek 	return (B_TRUE);
35627c478bd9Sstevel@tonic-gate }
35637c478bd9Sstevel@tonic-gate 
35640209230bSgjelinek /*
35650209230bSgjelinek  * Convert a string representing a number (possibly a fraction) into an integer.
35660209230bSgjelinek  * The string can have a modifier (K, M, G or T).   The modifiers are treated
35670209230bSgjelinek  * as powers of two (not 10).
35680209230bSgjelinek  */
35697c478bd9Sstevel@tonic-gate int
35700209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes)
35717c478bd9Sstevel@tonic-gate {
35720209230bSgjelinek 	long double val;
35730209230bSgjelinek 	char *unitp;
35740209230bSgjelinek 	uint64_t scale;
35757c478bd9Sstevel@tonic-gate 
35760209230bSgjelinek 	if ((val = strtold(str, &unitp)) < 0)
35770209230bSgjelinek 		return (-1);
35787c478bd9Sstevel@tonic-gate 
35790209230bSgjelinek 	/* remove any leading white space from units string */
35800209230bSgjelinek 	while (isspace(*unitp) != 0)
35810209230bSgjelinek 		++unitp;
35827c478bd9Sstevel@tonic-gate 
35830209230bSgjelinek 	/* if no units explicitly set, error */
35840209230bSgjelinek 	if (unitp == NULL || *unitp == '\0') {
35850209230bSgjelinek 		scale = 1;
35860209230bSgjelinek 	} else {
35870209230bSgjelinek 		int i;
35880209230bSgjelinek 		char *units[] = {"K", "M", "G", "T", NULL};
35897c478bd9Sstevel@tonic-gate 
35900209230bSgjelinek 		scale = 1024;
35910209230bSgjelinek 
35920209230bSgjelinek 		/* update scale based on units */
35930209230bSgjelinek 		for (i = 0; units[i] != NULL; i++) {
35940209230bSgjelinek 			if (strcasecmp(unitp, units[i]) == 0)
35950209230bSgjelinek 				break;
35960209230bSgjelinek 			scale <<= 10;
35970209230bSgjelinek 		}
35980209230bSgjelinek 
35990209230bSgjelinek 		if (units[i] == NULL)
36000209230bSgjelinek 			return (-1);
36017c478bd9Sstevel@tonic-gate 	}
36027c478bd9Sstevel@tonic-gate 
36030209230bSgjelinek 	*bytes = (uint64_t)(val * scale);
36040209230bSgjelinek 	return (0);
36057c478bd9Sstevel@tonic-gate }
36067c478bd9Sstevel@tonic-gate 
36070209230bSgjelinek boolean_t
36080209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr)
36097c478bd9Sstevel@tonic-gate {
36100209230bSgjelinek 	uint64_t low, high;
36117c478bd9Sstevel@tonic-gate 
36120209230bSgjelinek 	if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) ||
36130209230bSgjelinek 	    low < 1 || low > high)
36140209230bSgjelinek 		return (B_FALSE);
36150209230bSgjelinek 
36160209230bSgjelinek 	return (B_TRUE);
36177c478bd9Sstevel@tonic-gate }
36187c478bd9Sstevel@tonic-gate 
36190209230bSgjelinek boolean_t
36200209230bSgjelinek zonecfg_valid_importance(char *impstr)
36217c478bd9Sstevel@tonic-gate {
36220209230bSgjelinek 	uint64_t num;
36237c478bd9Sstevel@tonic-gate 
36240209230bSgjelinek 	if (!valid_uint(impstr, &num))
36250209230bSgjelinek 		return (B_FALSE);
36267c478bd9Sstevel@tonic-gate 
36270209230bSgjelinek 	return (B_TRUE);
36280209230bSgjelinek }
36297c478bd9Sstevel@tonic-gate 
36300209230bSgjelinek boolean_t
36310209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit)
36320209230bSgjelinek {
36330209230bSgjelinek 	int i;
36340209230bSgjelinek 
36350209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
36360209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
36377c478bd9Sstevel@tonic-gate 			break;
36387c478bd9Sstevel@tonic-gate 
36390209230bSgjelinek 	if (aliases[i].shortname == NULL)
36400209230bSgjelinek 		return (B_FALSE);
36417c478bd9Sstevel@tonic-gate 
36420209230bSgjelinek 	if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit)
36430209230bSgjelinek 		return (B_FALSE);
36447c478bd9Sstevel@tonic-gate 
36450209230bSgjelinek 	return (B_TRUE);
36467c478bd9Sstevel@tonic-gate }
36477c478bd9Sstevel@tonic-gate 
36480209230bSgjelinek boolean_t
36490209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val)
36507c478bd9Sstevel@tonic-gate {
36510209230bSgjelinek 	if (zonecfg_str_to_bytes(memstr, mem_val) != 0)
36520209230bSgjelinek 		return (B_FALSE);
36530209230bSgjelinek 
36540209230bSgjelinek 	return (B_TRUE);
36557c478bd9Sstevel@tonic-gate }
36567c478bd9Sstevel@tonic-gate 
36570209230bSgjelinek static int
36580209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res)
36597c478bd9Sstevel@tonic-gate {
36600209230bSgjelinek 	(void) strlcpy(pool_err, pool_strerror(pool_error()), err_size);
36610209230bSgjelinek 	return (res);
36627c478bd9Sstevel@tonic-gate }
36637c478bd9Sstevel@tonic-gate 
36640209230bSgjelinek static int
36650209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool,
36660209230bSgjelinek     char *name, int min, int max)
36677c478bd9Sstevel@tonic-gate {
36680209230bSgjelinek 	pool_resource_t *res;
36690209230bSgjelinek 	pool_elem_t *elem;
36700209230bSgjelinek 	pool_value_t *val;
36717c478bd9Sstevel@tonic-gate 
36720209230bSgjelinek 	if ((res = pool_resource_create(pconf, "pset", name)) == NULL)
36730209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36747c478bd9Sstevel@tonic-gate 
36750209230bSgjelinek 	if (pool_associate(pconf, pool, res) != PO_SUCCESS)
36760209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36777c478bd9Sstevel@tonic-gate 
36780209230bSgjelinek 	if ((elem = pool_resource_to_elem(pconf, res)) == NULL)
36790209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36807c478bd9Sstevel@tonic-gate 
36810209230bSgjelinek 	if ((val = pool_value_alloc()) == NULL)
36820209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36837c478bd9Sstevel@tonic-gate 
36840209230bSgjelinek 	/* set the maximum number of cpus for the pset */
36850209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)max);
36860209230bSgjelinek 
36870209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) {
36880209230bSgjelinek 		pool_value_free(val);
36890209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36907c478bd9Sstevel@tonic-gate 	}
36917c478bd9Sstevel@tonic-gate 
36920209230bSgjelinek 	/* set the minimum number of cpus for the pset */
36930209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)min);
36940209230bSgjelinek 
36950209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) {
36960209230bSgjelinek 		pool_value_free(val);
36970209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36987c478bd9Sstevel@tonic-gate 	}
36997c478bd9Sstevel@tonic-gate 
37000209230bSgjelinek 	pool_value_free(val);
37010209230bSgjelinek 
37027c478bd9Sstevel@tonic-gate 	return (Z_OK);
37037c478bd9Sstevel@tonic-gate }
37047c478bd9Sstevel@tonic-gate 
37050209230bSgjelinek static int
37060209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name,
37070209230bSgjelinek     struct zone_psettab *pset_tab)
37087c478bd9Sstevel@tonic-gate {
37090209230bSgjelinek 	pool_t *pool;
37100209230bSgjelinek 	int res = Z_OK;
37117c478bd9Sstevel@tonic-gate 
37120209230bSgjelinek 	/* create a temporary pool configuration */
37130209230bSgjelinek 	if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) {
37140209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37150209230bSgjelinek 		return (res);
37160209230bSgjelinek 	}
3717ffbafc53Scomay 
37180209230bSgjelinek 	if ((pool = pool_create(pconf, name)) == NULL) {
37190209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
37200209230bSgjelinek 		goto done;
37210209230bSgjelinek 	}
3722ffbafc53Scomay 
37230209230bSgjelinek 	/* set pool importance */
37240209230bSgjelinek 	if (pset_tab->zone_importance[0] != '\0') {
37250209230bSgjelinek 		pool_elem_t *elem;
37260209230bSgjelinek 		pool_value_t *val;
37279acbbeafSnn 
37280209230bSgjelinek 		if ((elem = pool_to_elem(pconf, pool)) == NULL) {
37290209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37300209230bSgjelinek 			goto done;
37310209230bSgjelinek 		}
37329acbbeafSnn 
37330209230bSgjelinek 		if ((val = pool_value_alloc()) == NULL) {
37340209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37350209230bSgjelinek 			goto done;
37360209230bSgjelinek 		}
37379acbbeafSnn 
37380209230bSgjelinek 		pool_value_set_int64(val,
37390209230bSgjelinek 		    (int64_t)atoi(pset_tab->zone_importance));
37409acbbeafSnn 
37410209230bSgjelinek 		if (pool_put_property(pconf, elem, "pool.importance", val)
37420209230bSgjelinek 		    != PO_SUCCESS) {
37430209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37440209230bSgjelinek 			pool_value_free(val);
37450209230bSgjelinek 			goto done;
37460209230bSgjelinek 		}
37479acbbeafSnn 
37480209230bSgjelinek 		pool_value_free(val);
37499acbbeafSnn 	}
37509acbbeafSnn 
37510209230bSgjelinek 	if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name,
37520209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_min),
37530209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_max))) != Z_OK)
37540209230bSgjelinek 		goto done;
37550209230bSgjelinek 
37560209230bSgjelinek 	/* validation */
37570209230bSgjelinek 	if (pool_conf_status(pconf) == POF_INVALID) {
37580209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37590209230bSgjelinek 		goto done;
37609acbbeafSnn 	}
37619acbbeafSnn 
37620209230bSgjelinek 	/*
37630209230bSgjelinek 	 * This validation is the one we expect to fail if the user specified
37640209230bSgjelinek 	 * an invalid configuration (too many cpus) for this system.
37650209230bSgjelinek 	 */
37660209230bSgjelinek 	if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) {
37670209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
37680209230bSgjelinek 		goto done;
37690209230bSgjelinek 	}
37709acbbeafSnn 
37710209230bSgjelinek 	/*
37720209230bSgjelinek 	 * Commit the dynamic configuration but not the pool configuration
37730209230bSgjelinek 	 * file.
37740209230bSgjelinek 	 */
37750209230bSgjelinek 	if (pool_conf_commit(pconf, 1) != PO_SUCCESS)
37760209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37779acbbeafSnn 
37780209230bSgjelinek done:
37790209230bSgjelinek 	(void) pool_conf_close(pconf);
37800209230bSgjelinek 	return (res);
37819acbbeafSnn }
37829acbbeafSnn 
37839acbbeafSnn static int
37840209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset,
37850209230bSgjelinek     struct zone_psettab *pset_tab)
37860209230bSgjelinek {
37870209230bSgjelinek 	int nfound = 0;
37880209230bSgjelinek 	pool_elem_t *pe;
37890209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
37900209230bSgjelinek 	uint64_t val_uint;
37910209230bSgjelinek 
37920209230bSgjelinek 	if (pool != NULL) {
37930209230bSgjelinek 		pe = pool_to_elem(pconf, pool);
37940209230bSgjelinek 		if (pool_get_property(pconf, pe, "pool.importance", pv)
37950209230bSgjelinek 		    != POC_INVAL) {
37960209230bSgjelinek 			int64_t val_int;
37970209230bSgjelinek 
37980209230bSgjelinek 			(void) pool_value_get_int64(pv, &val_int);
37990209230bSgjelinek 			(void) snprintf(pset_tab->zone_importance,
38000209230bSgjelinek 			    sizeof (pset_tab->zone_importance), "%d", val_int);
38010209230bSgjelinek 			nfound++;
38020209230bSgjelinek 		}
38039acbbeafSnn 	}
38049acbbeafSnn 
38050209230bSgjelinek 	if (pset != NULL) {
38060209230bSgjelinek 		pe = pool_resource_to_elem(pconf, pset);
38070209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) {
38080209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
38090209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_min,
38100209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_min), "%u", val_uint);
38110209230bSgjelinek 			nfound++;
38120209230bSgjelinek 		}
38139acbbeafSnn 
38140209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) {
38150209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
38160209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_max,
38170209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_max), "%u", val_uint);
38180209230bSgjelinek 			nfound++;
38190209230bSgjelinek 		}
38209acbbeafSnn 	}
38219acbbeafSnn 
38220209230bSgjelinek 	pool_value_free(pv);
38239acbbeafSnn 
38240209230bSgjelinek 	if (nfound == 3)
38250209230bSgjelinek 		return (PO_SUCCESS);
38260209230bSgjelinek 
38270209230bSgjelinek 	return (PO_FAIL);
38289acbbeafSnn }
38299acbbeafSnn 
38300209230bSgjelinek /*
38310209230bSgjelinek  * Determine if a tmp pool is configured and if so, if the configuration is
38320209230bSgjelinek  * still valid or if it has been changed since the tmp pool was created.
38330209230bSgjelinek  * If the tmp pool configuration is no longer valid, delete the tmp pool.
38340209230bSgjelinek  *
38350209230bSgjelinek  * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration.
38360209230bSgjelinek  */
38379acbbeafSnn static int
38380209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err,
38390209230bSgjelinek     int err_size, struct zone_psettab *pset_tab, boolean_t *exists)
38409acbbeafSnn {
38410209230bSgjelinek 	int res = Z_OK;
38420209230bSgjelinek 	pool_t *pool;
38430209230bSgjelinek 	pool_resource_t *pset;
38440209230bSgjelinek 	struct zone_psettab pset_current;
38457c478bd9Sstevel@tonic-gate 
38460209230bSgjelinek 	*exists = B_FALSE;
38477c478bd9Sstevel@tonic-gate 
38480209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
38490209230bSgjelinek 	    != PO_SUCCESS) {
38500209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
38510209230bSgjelinek 		return (res);
38520209230bSgjelinek 	}
38537c478bd9Sstevel@tonic-gate 
38540209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
38550209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
38560209230bSgjelinek 
38570209230bSgjelinek 	if (pool == NULL && pset == NULL) {
38580209230bSgjelinek 		/* no tmp pool configured */
38590209230bSgjelinek 		goto done;
38607c478bd9Sstevel@tonic-gate 	}
38619acbbeafSnn 
38620209230bSgjelinek 	/*
38630209230bSgjelinek 	 * If an existing tmp pool for this zone is configured with the proper
38640209230bSgjelinek 	 * settings, then the tmp pool is valid.
38650209230bSgjelinek 	 */
38660209230bSgjelinek 	if (get_running_tmp_pset(pconf, pool, pset, &pset_current)
38670209230bSgjelinek 	    == PO_SUCCESS &&
38680209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_min,
38690209230bSgjelinek 	    pset_current.zone_ncpu_min) == 0 &&
38700209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_max,
38710209230bSgjelinek 	    pset_current.zone_ncpu_max) == 0 &&
38720209230bSgjelinek 	    strcmp(pset_tab->zone_importance,
38730209230bSgjelinek 	    pset_current.zone_importance) == 0) {
38740209230bSgjelinek 		*exists = B_TRUE;
38757c478bd9Sstevel@tonic-gate 
38760209230bSgjelinek 	} else {
38770209230bSgjelinek 		/*
38780209230bSgjelinek 		 * An out-of-date tmp pool configuration exists.  Delete it
38790209230bSgjelinek 		 * so that we can create the correct tmp pool config.
38800209230bSgjelinek 		 */
38810209230bSgjelinek 		if (pset != NULL &&
38820209230bSgjelinek 		    pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
38830209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38840209230bSgjelinek 			goto done;
38850209230bSgjelinek 		}
38869acbbeafSnn 
38870209230bSgjelinek 		if (pool != NULL &&
38880209230bSgjelinek 		    pool_destroy(pconf, pool) != PO_SUCCESS) {
38890209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38900209230bSgjelinek 			goto done;
38910209230bSgjelinek 		}
38929acbbeafSnn 
38930209230bSgjelinek 		/* commit dynamic config */
38940209230bSgjelinek 		if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
38950209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38960209230bSgjelinek 	}
38970209230bSgjelinek 
38980209230bSgjelinek done:
38990209230bSgjelinek 	(void) pool_conf_close(pconf);
39000209230bSgjelinek 
39010209230bSgjelinek 	return (res);
3902ffbafc53Scomay }
3903ffbafc53Scomay 
3904ffbafc53Scomay /*
39050209230bSgjelinek  * Destroy any existing tmp pool.
3906ffbafc53Scomay  */
39070209230bSgjelinek int
39080209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size)
3909ffbafc53Scomay {
39100209230bSgjelinek 	int status;
39110209230bSgjelinek 	int res = Z_OK;
39120209230bSgjelinek 	pool_conf_t *pconf;
39130209230bSgjelinek 	pool_t *pool;
39140209230bSgjelinek 	pool_resource_t *pset;
39150209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
3916ffbafc53Scomay 
39170209230bSgjelinek 	/* if pools not enabled then nothing to do */
39180209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
39190209230bSgjelinek 		return (Z_OK);
3920ffbafc53Scomay 
39210209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
39220209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39239acbbeafSnn 
39240209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
3925ffbafc53Scomay 
39260209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
39270209230bSgjelinek 	    != PO_SUCCESS) {
39280209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39290209230bSgjelinek 		pool_conf_free(pconf);
39300209230bSgjelinek 		return (res);
3931ffbafc53Scomay 	}
3932ffbafc53Scomay 
39330209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
39340209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
3935ffbafc53Scomay 
39360209230bSgjelinek 	if (pool == NULL && pset == NULL) {
39370209230bSgjelinek 		/* nothing to destroy, we're done */
39380209230bSgjelinek 		goto done;
3939ffbafc53Scomay 	}
3940ffbafc53Scomay 
39410209230bSgjelinek 	if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
39420209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39430209230bSgjelinek 		goto done;
3944ffbafc53Scomay 	}
3945ffbafc53Scomay 
39460209230bSgjelinek 	if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) {
39470209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39480209230bSgjelinek 		goto done;
3949ffbafc53Scomay 	}
3950ffbafc53Scomay 
39510209230bSgjelinek 	/* commit dynamic config */
39520209230bSgjelinek 	if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
39530209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39540209230bSgjelinek 
39550209230bSgjelinek done:
39560209230bSgjelinek 	(void) pool_conf_close(pconf);
39570209230bSgjelinek 	pool_conf_free(pconf);
39580209230bSgjelinek 
39590209230bSgjelinek 	return (res);
3960ffbafc53Scomay }
3961ffbafc53Scomay 
3962ffbafc53Scomay /*
39630209230bSgjelinek  * Attempt to bind to a tmp pool for this zone.  If there is no tmp pool
39640209230bSgjelinek  * configured, we just return Z_OK.
3965ffbafc53Scomay  *
39660209230bSgjelinek  * We either attempt to create the tmp pool for this zone or rebind to an
39670209230bSgjelinek  * existing tmp pool for this zone.
39680209230bSgjelinek  *
39690209230bSgjelinek  * Rebinding is used when a zone with a tmp pool reboots so that we don't have
39700209230bSgjelinek  * to recreate the tmp pool.  To do this we need to be sure we work correctly
39710209230bSgjelinek  * for the following cases:
39720209230bSgjelinek  *
39730209230bSgjelinek  *	- there is an existing, properly configured tmp pool.
39740209230bSgjelinek  *	- zonecfg added tmp pool after zone was booted, must now create.
39750209230bSgjelinek  *	- zonecfg updated tmp pool config after zone was booted, in this case
39760209230bSgjelinek  *	  we destroy the old tmp pool and create a new one.
3977ffbafc53Scomay  */
3978ffbafc53Scomay int
39790209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
39800209230bSgjelinek     int err_size)
3981ffbafc53Scomay {
39820209230bSgjelinek 	struct zone_psettab pset_tab;
39830209230bSgjelinek 	int err;
39840209230bSgjelinek 	int status;
39850209230bSgjelinek 	pool_conf_t *pconf;
39860209230bSgjelinek 	boolean_t exists;
39870209230bSgjelinek 	char zone_name[ZONENAME_MAX];
39880209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
39890209230bSgjelinek 
39900209230bSgjelinek 	(void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name));
39910209230bSgjelinek 
39920209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
39930209230bSgjelinek 
39940209230bSgjelinek 	/* if no temporary pool configured, we're done */
39950209230bSgjelinek 	if (err == Z_NO_ENTRY)
39960209230bSgjelinek 		return (Z_OK);
3997ffbafc53Scomay 
3998ffbafc53Scomay 	/*
39990209230bSgjelinek 	 * importance might not have a value but we need to validate it here,
40000209230bSgjelinek 	 * so set the default.
4001ffbafc53Scomay 	 */
40020209230bSgjelinek 	if (pset_tab.zone_importance[0] == '\0')
40030209230bSgjelinek 		(void) strlcpy(pset_tab.zone_importance, "1",
40040209230bSgjelinek 		    sizeof (pset_tab.zone_importance));
40059acbbeafSnn 
40060209230bSgjelinek 	/* if pools not enabled, enable them now */
40070209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
40080209230bSgjelinek 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS)
40090209230bSgjelinek 			return (Z_POOL_ENABLE);
4010ffbafc53Scomay 	}
4011ffbafc53Scomay 
40120209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
40130209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40140209230bSgjelinek 
40150209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
40160209230bSgjelinek 
4017ffbafc53Scomay 	/*
40180209230bSgjelinek 	 * Check if a valid tmp pool/pset already exists.  If so, we just
40190209230bSgjelinek 	 * reuse it.
4020ffbafc53Scomay 	 */
40210209230bSgjelinek 	if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size,
40220209230bSgjelinek 	    &pset_tab, &exists)) != Z_OK) {
40230209230bSgjelinek 		pool_conf_free(pconf);
40240209230bSgjelinek 		return (err);
40250209230bSgjelinek 	}
4026ffbafc53Scomay 
40270209230bSgjelinek 	if (!exists)
40280209230bSgjelinek 		err = create_tmp_pool(pool_err, err_size, pconf, tmp_name,
40290209230bSgjelinek 		    &pset_tab);
40300209230bSgjelinek 
40310209230bSgjelinek 	pool_conf_free(pconf);
40320209230bSgjelinek 
40330209230bSgjelinek 	if (err != Z_OK)
40340209230bSgjelinek 		return (err);
40350209230bSgjelinek 
40360209230bSgjelinek 	/* Bind the zone to the pool. */
40370209230bSgjelinek 	if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS)
40380209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL_BIND));
40390209230bSgjelinek 
40400209230bSgjelinek 	return (Z_OK);
4041ffbafc53Scomay }
4042ffbafc53Scomay 
40430209230bSgjelinek /*
40440209230bSgjelinek  * Attempt to bind to a permanent pool for this zone.  If there is no
40450209230bSgjelinek  * permanent pool configured, we just return Z_OK.
40460209230bSgjelinek  */
40477c478bd9Sstevel@tonic-gate int
40480209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
40490209230bSgjelinek     int err_size)
40507c478bd9Sstevel@tonic-gate {
40510209230bSgjelinek 	pool_conf_t *poolconf;
40520209230bSgjelinek 	pool_t *pool;
40530209230bSgjelinek 	char poolname[MAXPATHLEN];
40540209230bSgjelinek 	int status;
40550209230bSgjelinek 	int error;
4056108322fbScarlsonj 
40570209230bSgjelinek 	/*
40580209230bSgjelinek 	 * Find the pool mentioned in the zone configuration, and bind to it.
40590209230bSgjelinek 	 */
40600209230bSgjelinek 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
40610209230bSgjelinek 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
40620209230bSgjelinek 		/*
40630209230bSgjelinek 		 * The property is not set on the zone, so the pool
40640209230bSgjelinek 		 * should be bound to the default pool.  But that's
40650209230bSgjelinek 		 * already done by the kernel, so we can just return.
40660209230bSgjelinek 		 */
40677c478bd9Sstevel@tonic-gate 		return (Z_OK);
40687c478bd9Sstevel@tonic-gate 	}
40690209230bSgjelinek 	if (error != Z_OK) {
40700209230bSgjelinek 		/*
40710209230bSgjelinek 		 * Not an error, even though it shouldn't be happening.
40720209230bSgjelinek 		 */
40730209230bSgjelinek 		return (Z_OK);
40740209230bSgjelinek 	}
40750209230bSgjelinek 	/*
40760209230bSgjelinek 	 * Don't do anything if pools aren't enabled.
40770209230bSgjelinek 	 */
40780209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
40790209230bSgjelinek 		return (Z_POOLS_NOT_ACTIVE);
40807c478bd9Sstevel@tonic-gate 
40817c478bd9Sstevel@tonic-gate 	/*
40820209230bSgjelinek 	 * Try to provide a sane error message if the requested pool doesn't
40830209230bSgjelinek 	 * exist.
40847c478bd9Sstevel@tonic-gate 	 */
40850209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
40860209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40877c478bd9Sstevel@tonic-gate 
40880209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
40890209230bSgjelinek 	    PO_SUCCESS) {
40900209230bSgjelinek 		pool_conf_free(poolconf);
40910209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40920209230bSgjelinek 	}
40930209230bSgjelinek 	pool = pool_get_pool(poolconf, poolname);
40940209230bSgjelinek 	(void) pool_conf_close(poolconf);
40950209230bSgjelinek 	pool_conf_free(poolconf);
40960209230bSgjelinek 	if (pool == NULL)
40970209230bSgjelinek 		return (Z_NO_POOL);
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate 	/*
41000209230bSgjelinek 	 * Bind the zone to the pool.
41017c478bd9Sstevel@tonic-gate 	 */
41020209230bSgjelinek 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
41030209230bSgjelinek 		/* if bind fails, return poolname for the error msg */
41040209230bSgjelinek 		(void) strlcpy(pool_err, poolname, err_size);
41050209230bSgjelinek 		return (Z_POOL_BIND);
41067c478bd9Sstevel@tonic-gate 	}
41070209230bSgjelinek 
41080209230bSgjelinek 	return (Z_OK);
41090209230bSgjelinek }
41100209230bSgjelinek 
41110209230bSgjelinek 
41120209230bSgjelinek static boolean_t
41130209230bSgjelinek svc_enabled(char *svc_name)
41140209230bSgjelinek {
41150209230bSgjelinek 	scf_simple_prop_t	*prop;
41160209230bSgjelinek 	boolean_t		found = B_FALSE;
41170209230bSgjelinek 
41180209230bSgjelinek 	prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL,
41190209230bSgjelinek 	    SCF_PROPERTY_ENABLED);
41200209230bSgjelinek 
41210209230bSgjelinek 	if (scf_simple_prop_numvalues(prop) == 1 &&
41220209230bSgjelinek 	    *scf_simple_prop_next_boolean(prop) != 0)
41230209230bSgjelinek 		found = B_TRUE;
41240209230bSgjelinek 
41250209230bSgjelinek 	scf_simple_prop_free(prop);
41260209230bSgjelinek 
41270209230bSgjelinek 	return (found);
41287c478bd9Sstevel@tonic-gate }
41297c478bd9Sstevel@tonic-gate 
41300209230bSgjelinek /*
41310209230bSgjelinek  * If the zone has capped-memory, make sure the rcap service is enabled.
41320209230bSgjelinek  */
41337c478bd9Sstevel@tonic-gate int
41340209230bSgjelinek zonecfg_enable_rcapd(char *err, int size)
41357c478bd9Sstevel@tonic-gate {
41360209230bSgjelinek 	if (!svc_enabled(RCAP_SERVICE) &&
41370209230bSgjelinek 	    smf_enable_instance(RCAP_SERVICE, 0) == -1) {
41380209230bSgjelinek 		(void) strlcpy(err, scf_strerror(scf_error()), size);
41390209230bSgjelinek 		return (Z_SYSTEM);
41400209230bSgjelinek 	}
41417c478bd9Sstevel@tonic-gate 
41427c478bd9Sstevel@tonic-gate 	return (Z_OK);
41437c478bd9Sstevel@tonic-gate }
41447c478bd9Sstevel@tonic-gate 
41450209230bSgjelinek /*
41460209230bSgjelinek  * Return true if pset has cpu range specified and poold is not enabled.
41470209230bSgjelinek  */
41480209230bSgjelinek boolean_t
41490209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle)
41509acbbeafSnn {
41510209230bSgjelinek 	struct zone_psettab pset_tab;
41520209230bSgjelinek 	int min, max;
41539acbbeafSnn 	int err;
41549acbbeafSnn 
41550209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
41560209230bSgjelinek 
41570209230bSgjelinek 	/* if no temporary pool configured, we're done */
41580209230bSgjelinek 	if (err == Z_NO_ENTRY)
41590209230bSgjelinek 		return (B_FALSE);
41600209230bSgjelinek 
41610209230bSgjelinek 	min = atoi(pset_tab.zone_ncpu_min);
41620209230bSgjelinek 	max = atoi(pset_tab.zone_ncpu_max);
41630209230bSgjelinek 
41640209230bSgjelinek 	/* range not specified, no need for poold */
41650209230bSgjelinek 	if (min == max)
41660209230bSgjelinek 		return (B_FALSE);
41670209230bSgjelinek 
41680209230bSgjelinek 	/* we have a range, check if poold service is enabled */
41690209230bSgjelinek 	if (svc_enabled(POOLD_SERVICE))
41700209230bSgjelinek 		return (B_FALSE);
41710209230bSgjelinek 
41720209230bSgjelinek 	return (B_TRUE);
41730209230bSgjelinek }
41740209230bSgjelinek 
41750209230bSgjelinek static int
41760209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize)
41770209230bSgjelinek {
41780209230bSgjelinek 	int status;
41790209230bSgjelinek 	pool_conf_t *poolconf;
41800209230bSgjelinek 	pool_t *pool;
41810209230bSgjelinek 	pool_elem_t *pe;
41820209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
41830209230bSgjelinek 	const char *sched_str;
41840209230bSgjelinek 
41850209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
41860209230bSgjelinek 		return (Z_NO_POOL);
41870209230bSgjelinek 
41880209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
41890209230bSgjelinek 		return (Z_NO_POOL);
41900209230bSgjelinek 
41910209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
41920209230bSgjelinek 	    PO_SUCCESS) {
41930209230bSgjelinek 		pool_conf_free(poolconf);
41940209230bSgjelinek 		return (Z_NO_POOL);
41959acbbeafSnn 	}
41969acbbeafSnn 
41970209230bSgjelinek 	if ((pool = pool_get_pool(poolconf, poolname)) == NULL) {
41980209230bSgjelinek 		(void) pool_conf_close(poolconf);
41990209230bSgjelinek 		pool_conf_free(poolconf);
42000209230bSgjelinek 		return (Z_NO_POOL);
42019acbbeafSnn 	}
42029acbbeafSnn 
42030209230bSgjelinek 	pe = pool_to_elem(poolconf, pool);
42040209230bSgjelinek 	if (pool_get_property(poolconf, pe, "pool.scheduler", pv)
42050209230bSgjelinek 	    != POC_INVAL) {
42060209230bSgjelinek 		(void) pool_value_get_string(pv, &sched_str);
42070209230bSgjelinek 		if (strlcpy(class, sched_str, clsize) >= clsize)
42080209230bSgjelinek 			return (Z_TOO_BIG);
42090209230bSgjelinek 	}
42109acbbeafSnn 
42110209230bSgjelinek 	(void) pool_conf_close(poolconf);
42120209230bSgjelinek 	pool_conf_free(poolconf);
42130209230bSgjelinek 	return (Z_OK);
42149acbbeafSnn }
42159acbbeafSnn 
4216facf4a8dSllai /*
42170209230bSgjelinek  * Get the default scheduling class for the zone.  This will either be the
42180209230bSgjelinek  * class set on the zone's pool or the system default scheduling class.
4219facf4a8dSllai  */
4220facf4a8dSllai int
42210209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize)
4222facf4a8dSllai {
42230209230bSgjelinek 	char poolname[MAXPATHLEN];
4224facf4a8dSllai 
42250209230bSgjelinek 	if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) {
42260209230bSgjelinek 		/* check if the zone's pool specified a sched class */
42270209230bSgjelinek 		if (get_pool_sched_class(poolname, class, clsize) == Z_OK)
42280209230bSgjelinek 			return (Z_OK);
42290209230bSgjelinek 	}
4230facf4a8dSllai 
42310209230bSgjelinek 	if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1)
4232facf4a8dSllai 		return (Z_TOO_BIG);
42330209230bSgjelinek 
4234facf4a8dSllai 	return (Z_OK);
4235facf4a8dSllai }
4236facf4a8dSllai 
42370209230bSgjelinek int
42380209230bSgjelinek zonecfg_setfsent(zone_dochandle_t handle)
42397c478bd9Sstevel@tonic-gate {
42400209230bSgjelinek 	return (zonecfg_setent(handle));
42417c478bd9Sstevel@tonic-gate }
42427c478bd9Sstevel@tonic-gate 
42437c478bd9Sstevel@tonic-gate int
42440209230bSgjelinek zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
42457c478bd9Sstevel@tonic-gate {
42460209230bSgjelinek 	xmlNodePtr cur, options;
42470209230bSgjelinek 	char options_str[MAX_MNTOPT_STR];
42480209230bSgjelinek 	int err;
42497c478bd9Sstevel@tonic-gate 
42500209230bSgjelinek 	if (handle == NULL)
42517c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
42527c478bd9Sstevel@tonic-gate 
42530209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
42540209230bSgjelinek 		return (Z_NO_ENTRY);
42550209230bSgjelinek 
42560209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
42570209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
42580209230bSgjelinek 			break;
42590209230bSgjelinek 	if (cur == NULL) {
42600209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
42610209230bSgjelinek 		return (Z_NO_ENTRY);
4262108322fbScarlsonj 	}
4263108322fbScarlsonj 
42640209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
42650209230bSgjelinek 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
42660209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
42670209230bSgjelinek 		return (err);
42687c478bd9Sstevel@tonic-gate 	}
42697c478bd9Sstevel@tonic-gate 
42700209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
42710209230bSgjelinek 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
42720209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
42730209230bSgjelinek 		return (err);
42740209230bSgjelinek 	}
42750209230bSgjelinek 
42760209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
42770209230bSgjelinek 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
42780209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
42790209230bSgjelinek 		return (err);
42800209230bSgjelinek 	}
42810209230bSgjelinek 
42820209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
42830209230bSgjelinek 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
42840209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
42850209230bSgjelinek 		return (err);
42860209230bSgjelinek 	}
42870209230bSgjelinek 
42880209230bSgjelinek 	/* OK for options to be NULL */
42890209230bSgjelinek 	tabptr->zone_fs_options = NULL;
42900209230bSgjelinek 	for (options = cur->xmlChildrenNode; options != NULL;
42910209230bSgjelinek 	    options = options->next) {
42920209230bSgjelinek 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
42930209230bSgjelinek 		    sizeof (options_str)) != Z_OK)
42940209230bSgjelinek 			break;
42950209230bSgjelinek 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
42967c478bd9Sstevel@tonic-gate 			break;
42977c478bd9Sstevel@tonic-gate 	}
42980209230bSgjelinek 
42990209230bSgjelinek 	handle->zone_dh_cur = cur->next;
43000209230bSgjelinek 	return (Z_OK);
43017c478bd9Sstevel@tonic-gate }
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate int
43040209230bSgjelinek zonecfg_endfsent(zone_dochandle_t handle)
43057c478bd9Sstevel@tonic-gate {
43060209230bSgjelinek 	return (zonecfg_endent(handle));
43077c478bd9Sstevel@tonic-gate }
43087c478bd9Sstevel@tonic-gate 
43097c478bd9Sstevel@tonic-gate int
43100209230bSgjelinek zonecfg_setipdent(zone_dochandle_t handle)
43117c478bd9Sstevel@tonic-gate {
43120209230bSgjelinek 	return (zonecfg_setent(handle));
43130209230bSgjelinek }
43140209230bSgjelinek 
43150209230bSgjelinek int
43160209230bSgjelinek zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
43170209230bSgjelinek {
43180209230bSgjelinek 	xmlNodePtr cur;
43197c478bd9Sstevel@tonic-gate 	int err;
43207c478bd9Sstevel@tonic-gate 
43210209230bSgjelinek 	if (handle == NULL)
43220209230bSgjelinek 		return (Z_INVAL);
43237c478bd9Sstevel@tonic-gate 
43240209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
43250209230bSgjelinek 		return (Z_NO_ENTRY);
43267c478bd9Sstevel@tonic-gate 
43270209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
43280209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
43290209230bSgjelinek 			break;
43300209230bSgjelinek 	if (cur == NULL) {
43310209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
43320209230bSgjelinek 		return (Z_NO_ENTRY);
43330209230bSgjelinek 	}
43347c478bd9Sstevel@tonic-gate 
43350209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
43360209230bSgjelinek 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
43370209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
43380209230bSgjelinek 		return (err);
43397c478bd9Sstevel@tonic-gate 	}
43407c478bd9Sstevel@tonic-gate 
43410209230bSgjelinek 	handle->zone_dh_cur = cur->next;
43420209230bSgjelinek 	return (Z_OK);
43437c478bd9Sstevel@tonic-gate }
43447c478bd9Sstevel@tonic-gate 
43450209230bSgjelinek int
43460209230bSgjelinek zonecfg_endipdent(zone_dochandle_t handle)
43477c478bd9Sstevel@tonic-gate {
43480209230bSgjelinek 	return (zonecfg_endent(handle));
43497c478bd9Sstevel@tonic-gate }
43507c478bd9Sstevel@tonic-gate 
4351108322fbScarlsonj int
43520209230bSgjelinek zonecfg_setnwifent(zone_dochandle_t handle)
4353108322fbScarlsonj {
43540209230bSgjelinek 	return (zonecfg_setent(handle));
4355108322fbScarlsonj }
4356108322fbScarlsonj 
4357108322fbScarlsonj int
43580209230bSgjelinek zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
4359108322fbScarlsonj {
43600209230bSgjelinek 	xmlNodePtr cur;
43610209230bSgjelinek 	int err;
4362108322fbScarlsonj 
43630209230bSgjelinek 	if (handle == NULL)
43640209230bSgjelinek 		return (Z_INVAL);
43650209230bSgjelinek 
43660209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
43670209230bSgjelinek 		return (Z_NO_ENTRY);
43680209230bSgjelinek 
43690209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
43700209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
4371108322fbScarlsonj 			break;
43720209230bSgjelinek 	if (cur == NULL) {
43730209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
43740209230bSgjelinek 		return (Z_NO_ENTRY);
4375108322fbScarlsonj 	}
43760209230bSgjelinek 
43770209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
43780209230bSgjelinek 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
43790209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
43800209230bSgjelinek 		return (err);
43810209230bSgjelinek 	}
43820209230bSgjelinek 
43830209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
43840209230bSgjelinek 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
43850209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
43860209230bSgjelinek 		return (err);
4387108322fbScarlsonj 	}
43880209230bSgjelinek 
43890209230bSgjelinek 	handle->zone_dh_cur = cur->next;
43900209230bSgjelinek 	return (Z_OK);
4391108322fbScarlsonj }
4392108322fbScarlsonj 
43930209230bSgjelinek int
43940209230bSgjelinek zonecfg_endnwifent(zone_dochandle_t handle)
43957c478bd9Sstevel@tonic-gate {
43960209230bSgjelinek 	return (zonecfg_endent(handle));
43977c478bd9Sstevel@tonic-gate }
43987c478bd9Sstevel@tonic-gate 
43990209230bSgjelinek int
44000209230bSgjelinek zonecfg_setdevent(zone_dochandle_t handle)
44010209230bSgjelinek {
44020209230bSgjelinek 	return (zonecfg_setent(handle));
44030209230bSgjelinek }
44047c478bd9Sstevel@tonic-gate 
44057c478bd9Sstevel@tonic-gate int
44060209230bSgjelinek zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
44077c478bd9Sstevel@tonic-gate {
44080209230bSgjelinek 	xmlNodePtr cur;
44090209230bSgjelinek 	int err;
44107c478bd9Sstevel@tonic-gate 
44110209230bSgjelinek 	if (handle == NULL)
44127c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44137c478bd9Sstevel@tonic-gate 
44140209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
44150209230bSgjelinek 		return (Z_NO_ENTRY);
44160209230bSgjelinek 
44170209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
44180209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
44190209230bSgjelinek 			break;
44200209230bSgjelinek 	if (cur == NULL) {
44210209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
44220209230bSgjelinek 		return (Z_NO_ENTRY);
44237c478bd9Sstevel@tonic-gate 	}
44247c478bd9Sstevel@tonic-gate 
44250209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
44260209230bSgjelinek 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
44270209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
44280209230bSgjelinek 		return (err);
44297c478bd9Sstevel@tonic-gate 	}
44300209230bSgjelinek 
44310209230bSgjelinek 	handle->zone_dh_cur = cur->next;
44327c478bd9Sstevel@tonic-gate 	return (Z_OK);
44337c478bd9Sstevel@tonic-gate }
44347c478bd9Sstevel@tonic-gate 
44350209230bSgjelinek int
44360209230bSgjelinek zonecfg_enddevent(zone_dochandle_t handle)
44377c478bd9Sstevel@tonic-gate {
44380209230bSgjelinek 	return (zonecfg_endent(handle));
44390209230bSgjelinek }
44407c478bd9Sstevel@tonic-gate 
44410209230bSgjelinek int
44420209230bSgjelinek zonecfg_setrctlent(zone_dochandle_t handle)
44430209230bSgjelinek {
44440209230bSgjelinek 	return (zonecfg_setent(handle));
44450209230bSgjelinek }
44460209230bSgjelinek 
44470209230bSgjelinek int
44480209230bSgjelinek zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
44490209230bSgjelinek {
44500209230bSgjelinek 	xmlNodePtr cur, val;
44510209230bSgjelinek 	struct zone_rctlvaltab *valptr;
44520209230bSgjelinek 	int err;
44530209230bSgjelinek 
44540209230bSgjelinek 	if (handle == NULL)
44550209230bSgjelinek 		return (Z_INVAL);
44560209230bSgjelinek 
44570209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
44580209230bSgjelinek 		return (Z_NO_ENTRY);
44590209230bSgjelinek 
44600209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
44610209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
44620209230bSgjelinek 			break;
44630209230bSgjelinek 	if (cur == NULL) {
44640209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
44650209230bSgjelinek 		return (Z_NO_ENTRY);
44660209230bSgjelinek 	}
44670209230bSgjelinek 
44680209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
44690209230bSgjelinek 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
44700209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
44710209230bSgjelinek 		return (err);
44720209230bSgjelinek 	}
44730209230bSgjelinek 
44740209230bSgjelinek 	tabptr->zone_rctl_valptr = NULL;
44750209230bSgjelinek 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
44760209230bSgjelinek 		valptr = (struct zone_rctlvaltab *)malloc(
44770209230bSgjelinek 		    sizeof (struct zone_rctlvaltab));
44780209230bSgjelinek 		if (valptr == NULL)
44790209230bSgjelinek 			return (Z_NOMEM);
44800209230bSgjelinek 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
44810209230bSgjelinek 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
44820209230bSgjelinek 			break;
44830209230bSgjelinek 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
44840209230bSgjelinek 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
44850209230bSgjelinek 			break;
44860209230bSgjelinek 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
44870209230bSgjelinek 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
44880209230bSgjelinek 			break;
44890209230bSgjelinek 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
44900209230bSgjelinek 			break;
44910209230bSgjelinek 	}
44920209230bSgjelinek 
44930209230bSgjelinek 	handle->zone_dh_cur = cur->next;
44940209230bSgjelinek 	return (Z_OK);
44950209230bSgjelinek }
44960209230bSgjelinek 
44970209230bSgjelinek int
44980209230bSgjelinek zonecfg_endrctlent(zone_dochandle_t handle)
44990209230bSgjelinek {
45000209230bSgjelinek 	return (zonecfg_endent(handle));
45010209230bSgjelinek }
45020209230bSgjelinek 
45030209230bSgjelinek int
45040209230bSgjelinek zonecfg_setattrent(zone_dochandle_t handle)
45050209230bSgjelinek {
45060209230bSgjelinek 	return (zonecfg_setent(handle));
45070209230bSgjelinek }
45080209230bSgjelinek 
45090209230bSgjelinek int
45100209230bSgjelinek zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
45110209230bSgjelinek {
45120209230bSgjelinek 	xmlNodePtr cur;
45130209230bSgjelinek 	int err;
45140209230bSgjelinek 
45150209230bSgjelinek 	if (handle == NULL)
45160209230bSgjelinek 		return (Z_INVAL);
45170209230bSgjelinek 
45180209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
45190209230bSgjelinek 		return (Z_NO_ENTRY);
45200209230bSgjelinek 
45210209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
45220209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
45230209230bSgjelinek 			break;
45240209230bSgjelinek 	if (cur == NULL) {
45250209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
45260209230bSgjelinek 		return (Z_NO_ENTRY);
45270209230bSgjelinek 	}
45280209230bSgjelinek 
45290209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
45300209230bSgjelinek 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
45310209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
45320209230bSgjelinek 		return (err);
45330209230bSgjelinek 	}
45340209230bSgjelinek 
45350209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
45360209230bSgjelinek 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
45370209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
45380209230bSgjelinek 		return (err);
45390209230bSgjelinek 	}
45400209230bSgjelinek 
45410209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
45420209230bSgjelinek 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
45430209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
45440209230bSgjelinek 		return (err);
45450209230bSgjelinek 	}
45460209230bSgjelinek 
45470209230bSgjelinek 	handle->zone_dh_cur = cur->next;
45480209230bSgjelinek 	return (Z_OK);
45490209230bSgjelinek }
45500209230bSgjelinek 
45510209230bSgjelinek int
45520209230bSgjelinek zonecfg_endattrent(zone_dochandle_t handle)
45530209230bSgjelinek {
45540209230bSgjelinek 	return (zonecfg_endent(handle));
45550209230bSgjelinek }
45560209230bSgjelinek 
45570209230bSgjelinek /*
45580209230bSgjelinek  * The privileges available on the system and described in privileges(5)
45590209230bSgjelinek  * fall into four categories with respect to non-global zones:
45600209230bSgjelinek  *
45610209230bSgjelinek  *      Default set of privileges considered safe for all non-global
45620209230bSgjelinek  *      zones.  These privileges are "safe" in the sense that a
45630209230bSgjelinek  *      privileged process in the zone cannot affect processes in any
45640209230bSgjelinek  *      other zone on the system.
45650209230bSgjelinek  *
45660209230bSgjelinek  *      Set of privileges not currently permitted within a non-global
45670209230bSgjelinek  *      zone.  These privileges are considered by default, "unsafe,"
45680209230bSgjelinek  *      and include ones which affect global resources (such as the
45690209230bSgjelinek  *      system clock or physical memory) or are overly broad and cover
45700209230bSgjelinek  *      more than one mechanism in the system.  In other cases, there
45710209230bSgjelinek  *      has not been sufficient virtualization in the parts of the
45720209230bSgjelinek  *      system the privilege covers to allow its use within a
45730209230bSgjelinek  *      non-global zone.
45740209230bSgjelinek  *
45750209230bSgjelinek  *      Set of privileges required in order to get a zone booted and
45760209230bSgjelinek  *      init(1M) started.  These cannot be removed from the zone's
45770209230bSgjelinek  *      privilege set.
45780209230bSgjelinek  *
45790209230bSgjelinek  * All other privileges are optional and are potentially useful for
45800209230bSgjelinek  * processes executing inside a non-global zone.
45810209230bSgjelinek  *
45820209230bSgjelinek  * When privileges are added to the system, a determination needs to be
45830209230bSgjelinek  * made as to which category the privilege belongs to.  Ideally,
45840209230bSgjelinek  * privileges should be fine-grained enough and the mechanisms they cover
45850209230bSgjelinek  * virtualized enough so that they can be made available to non-global
45860209230bSgjelinek  * zones.
45870209230bSgjelinek  */
45880209230bSgjelinek 
45890209230bSgjelinek /*
45900209230bSgjelinek  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
45910209230bSgjelinek  * the privilege string separator can be any character, although it is
45920209230bSgjelinek  * usually a comma character, define these here as well in the event that
45930209230bSgjelinek  * they change or are augmented in the future.
45940209230bSgjelinek  */
45950209230bSgjelinek #define	BASIC_TOKEN		"basic"
45960209230bSgjelinek #define	DEFAULT_TOKEN		"default"
45970209230bSgjelinek #define	ZONE_TOKEN		"zone"
45980209230bSgjelinek #define	TOKEN_PRIV_CHAR		','
45990209230bSgjelinek #define	TOKEN_PRIV_STR		","
46000209230bSgjelinek 
46010209230bSgjelinek typedef struct priv_node {
46020209230bSgjelinek 	struct priv_node	*pn_next;	/* Next privilege */
46030209230bSgjelinek 	char			*pn_priv;	/* Privileges name */
46040209230bSgjelinek } priv_node_t;
46050209230bSgjelinek 
46060209230bSgjelinek /* Privileges lists can differ across brands */
46070209230bSgjelinek typedef struct priv_lists {
46080209230bSgjelinek 	/* Privileges considered safe for all non-global zones of a brand */
46090209230bSgjelinek 	struct priv_node	*pl_default;
46100209230bSgjelinek 
46110209230bSgjelinek 	/* Privileges not permitted for all non-global zones of a brand */
46120209230bSgjelinek 	struct priv_node	*pl_prohibited;
46130209230bSgjelinek 
46140209230bSgjelinek 	/* Privileges required for all non-global zones of a brand */
46150209230bSgjelinek 	struct priv_node	*pl_required;
4616bf1d7e28Sdh 
4617bf1d7e28Sdh 	/*
4618bf1d7e28Sdh 	 * ip-type of the zone these privileges lists apply to.
4619bf1d7e28Sdh 	 * It is used to pass ip-type to the callback function,
4620bf1d7e28Sdh 	 * priv_lists_cb, which has no way of getting the ip-type.
4621bf1d7e28Sdh 	 */
4622bf1d7e28Sdh 	const char		*pl_iptype;
46230209230bSgjelinek } priv_lists_t;
46240209230bSgjelinek 
46250209230bSgjelinek static int
4626bf1d7e28Sdh priv_lists_cb(void *data, priv_iter_t *priv_iter)
46270209230bSgjelinek {
46280209230bSgjelinek 	priv_lists_t *plp = (priv_lists_t *)data;
46290209230bSgjelinek 	priv_node_t *pnp;
46300209230bSgjelinek 
4631bf1d7e28Sdh 	/* Skip this privilege if ip-type does not match */
4632bf1d7e28Sdh 	if ((strcmp(priv_iter->pi_iptype, "all") != 0) &&
4633bf1d7e28Sdh 	    (strcmp(priv_iter->pi_iptype, plp->pl_iptype) != 0))
4634bf1d7e28Sdh 		return (0);
4635bf1d7e28Sdh 
46360209230bSgjelinek 	/* Allocate a new priv list node. */
46370209230bSgjelinek 	if ((pnp = malloc(sizeof (*pnp))) == NULL)
46380209230bSgjelinek 		return (-1);
4639bf1d7e28Sdh 	if ((pnp->pn_priv = strdup(priv_iter->pi_name)) == NULL) {
46400209230bSgjelinek 		free(pnp);
46410209230bSgjelinek 		return (-1);
46420209230bSgjelinek 	}
46430209230bSgjelinek 
46440209230bSgjelinek 	/* Insert the new priv list node into the right list */
4645bf1d7e28Sdh 	if (strcmp(priv_iter->pi_set, "default") == 0) {
46460209230bSgjelinek 		pnp->pn_next = plp->pl_default;
46470209230bSgjelinek 		plp->pl_default = pnp;
4648bf1d7e28Sdh 	} else if (strcmp(priv_iter->pi_set, "prohibited") == 0) {
46490209230bSgjelinek 		pnp->pn_next = plp->pl_prohibited;
46500209230bSgjelinek 		plp->pl_prohibited = pnp;
4651bf1d7e28Sdh 	} else if (strcmp(priv_iter->pi_set, "required") == 0) {
46520209230bSgjelinek 		pnp->pn_next = plp->pl_required;
46530209230bSgjelinek 		plp->pl_required = pnp;
46540209230bSgjelinek 	} else {
46550209230bSgjelinek 		free(pnp->pn_priv);
46560209230bSgjelinek 		free(pnp);
46570209230bSgjelinek 		return (-1);
46580209230bSgjelinek 	}
46590209230bSgjelinek 	return (0);
46600209230bSgjelinek }
46610209230bSgjelinek 
46620209230bSgjelinek static void
46630209230bSgjelinek priv_lists_destroy(priv_lists_t *plp)
46640209230bSgjelinek {
46650209230bSgjelinek 	priv_node_t *pnp;
46660209230bSgjelinek 
46670209230bSgjelinek 	assert(plp != NULL);
46680209230bSgjelinek 
46690209230bSgjelinek 	while ((pnp = plp->pl_default) != NULL) {
46700209230bSgjelinek 		plp->pl_default = pnp->pn_next;
46710209230bSgjelinek 		free(pnp->pn_priv);
46720209230bSgjelinek 		free(pnp);
46730209230bSgjelinek 	}
46740209230bSgjelinek 	while ((pnp = plp->pl_prohibited) != NULL) {
46750209230bSgjelinek 		plp->pl_prohibited = pnp->pn_next;
46760209230bSgjelinek 		free(pnp->pn_priv);
46770209230bSgjelinek 		free(pnp);
46780209230bSgjelinek 	}
46790209230bSgjelinek 	while ((pnp = plp->pl_required) != NULL) {
46800209230bSgjelinek 		plp->pl_required = pnp->pn_next;
46810209230bSgjelinek 		free(pnp->pn_priv);
46820209230bSgjelinek 		free(pnp);
46830209230bSgjelinek 	}
46840209230bSgjelinek 	free(plp);
46850209230bSgjelinek }
46860209230bSgjelinek 
46870209230bSgjelinek static int
4688bf1d7e28Sdh priv_lists_create(zone_dochandle_t handle, priv_lists_t **plpp,
4689bf1d7e28Sdh     const char *curr_iptype)
46900209230bSgjelinek {
46910209230bSgjelinek 	priv_lists_t *plp;
46920209230bSgjelinek 	brand_handle_t bh;
46930209230bSgjelinek 	char brand[MAXNAMELEN];
46940209230bSgjelinek 
46950209230bSgjelinek 	if (handle != NULL) {
46960209230bSgjelinek 		if (zonecfg_get_brand(handle, brand, sizeof (brand)) != 0)
46970209230bSgjelinek 			return (Z_BRAND_ERROR);
46980209230bSgjelinek 	} else {
46990209230bSgjelinek 		(void) strlcpy(brand, NATIVE_BRAND_NAME, MAXNAMELEN);
47000209230bSgjelinek 	}
47010209230bSgjelinek 
47020209230bSgjelinek 	if ((bh = brand_open(brand)) == NULL)
47030209230bSgjelinek 		return (Z_BRAND_ERROR);
47040209230bSgjelinek 
47050209230bSgjelinek 	if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) {
47060209230bSgjelinek 		brand_close(bh);
47070209230bSgjelinek 		return (Z_NOMEM);
47080209230bSgjelinek 	}
47090209230bSgjelinek 
4710bf1d7e28Sdh 	plp->pl_iptype = curr_iptype;
4711bf1d7e28Sdh 
47120209230bSgjelinek 	/* construct the privilege lists */
47130209230bSgjelinek 	if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) {
47140209230bSgjelinek 		priv_lists_destroy(plp);
47150209230bSgjelinek 		brand_close(bh);
47160209230bSgjelinek 		return (Z_BRAND_ERROR);
47170209230bSgjelinek 	}
47180209230bSgjelinek 
47190209230bSgjelinek 	brand_close(bh);
47200209230bSgjelinek 	*plpp = plp;
47210209230bSgjelinek 	return (Z_OK);
47220209230bSgjelinek }
47230209230bSgjelinek 
47240209230bSgjelinek static int
47250209230bSgjelinek get_default_privset(priv_set_t *privs, priv_lists_t *plp)
47260209230bSgjelinek {
47270209230bSgjelinek 	priv_node_t *pnp;
47280209230bSgjelinek 	priv_set_t *basic;
47290209230bSgjelinek 
47300209230bSgjelinek 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
47310209230bSgjelinek 	if (basic == NULL)
47320209230bSgjelinek 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
47330209230bSgjelinek 
47340209230bSgjelinek 	priv_union(basic, privs);
47350209230bSgjelinek 	priv_freeset(basic);
47360209230bSgjelinek 
47370209230bSgjelinek 	for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) {
47380209230bSgjelinek 		if (priv_addset(privs, pnp->pn_priv) != 0)
47390209230bSgjelinek 			return (Z_INVAL);
47400209230bSgjelinek 	}
47410209230bSgjelinek 
47420209230bSgjelinek 	return (Z_OK);
47430209230bSgjelinek }
47440209230bSgjelinek 
47450209230bSgjelinek int
4746bf1d7e28Sdh zonecfg_default_privset(priv_set_t *privs, const char *curr_iptype)
47470209230bSgjelinek {
47480209230bSgjelinek 	priv_lists_t *plp;
47490209230bSgjelinek 	int ret;
47500209230bSgjelinek 
4751bf1d7e28Sdh 	if ((ret = priv_lists_create(NULL, &plp, curr_iptype)) != Z_OK)
47520209230bSgjelinek 		return (ret);
47530209230bSgjelinek 	ret = get_default_privset(privs, plp);
47540209230bSgjelinek 	priv_lists_destroy(plp);
47550209230bSgjelinek 	return (ret);
47560209230bSgjelinek }
47570209230bSgjelinek 
47580209230bSgjelinek void
47590209230bSgjelinek append_priv_token(char *priv, char *str, size_t strlen)
47600209230bSgjelinek {
47610209230bSgjelinek 	if (*str != '\0')
47620209230bSgjelinek 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
47630209230bSgjelinek 	(void) strlcat(str, priv, strlen);
47640209230bSgjelinek }
47650209230bSgjelinek 
47660209230bSgjelinek /*
47670209230bSgjelinek  * Verify that the supplied string is a valid privilege limit set for a
47680209230bSgjelinek  * non-global zone.  This string must not only be acceptable to
47690209230bSgjelinek  * priv_str_to_set(3C) which parses it, but it also must resolve to a
47700209230bSgjelinek  * privilege set that includes certain required privileges and lacks
47710209230bSgjelinek  * certain prohibited privileges.
47720209230bSgjelinek  */
47730209230bSgjelinek static int
47740209230bSgjelinek verify_privset(char *privbuf, priv_set_t *privs, char **privname,
47750209230bSgjelinek     boolean_t add_default, priv_lists_t *plp)
47760209230bSgjelinek {
47770209230bSgjelinek 	priv_node_t *pnp;
47780209230bSgjelinek 	char *tmp, *cp, *lasts;
47790209230bSgjelinek 	size_t len;
47800209230bSgjelinek 	priv_set_t *mergeset;
47810209230bSgjelinek 	const char *token;
47820209230bSgjelinek 
47830209230bSgjelinek 	/*
47840209230bSgjelinek 	 * The verification of the privilege string occurs in several
47850209230bSgjelinek 	 * phases.  In the first phase, the supplied string is scanned for
47860209230bSgjelinek 	 * the ZONE_TOKEN token which is not support as part of the
47870209230bSgjelinek 	 * "limitpriv" property.
47880209230bSgjelinek 	 *
47890209230bSgjelinek 	 * Duplicate the supplied privilege string since strtok_r(3C)
47900209230bSgjelinek 	 * tokenizes its input by null-terminating the tokens.
47910209230bSgjelinek 	 */
47920209230bSgjelinek 	if ((tmp = strdup(privbuf)) == NULL)
47930209230bSgjelinek 		return (Z_NOMEM);
47940209230bSgjelinek 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
47950209230bSgjelinek 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
47960209230bSgjelinek 		if (strcmp(cp, ZONE_TOKEN) == 0) {
47970209230bSgjelinek 			free(tmp);
47980209230bSgjelinek 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
47990209230bSgjelinek 				return (Z_NOMEM);
48000209230bSgjelinek 			else
48010209230bSgjelinek 				return (Z_PRIV_UNKNOWN);
48020209230bSgjelinek 		}
48030209230bSgjelinek 	}
48040209230bSgjelinek 	free(tmp);
48050209230bSgjelinek 
48060209230bSgjelinek 	if (add_default) {
48070209230bSgjelinek 		/*
48080209230bSgjelinek 		 * If DEFAULT_TOKEN was specified, a string needs to be
48090209230bSgjelinek 		 * built containing the privileges from the default, safe
48100209230bSgjelinek 		 * set along with those of the "limitpriv" property.
48110209230bSgjelinek 		 */
48120209230bSgjelinek 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
48130209230bSgjelinek 
48140209230bSgjelinek 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
48150209230bSgjelinek 			len += strlen(pnp->pn_priv) + 1;
48160209230bSgjelinek 		tmp = alloca(len);
48170209230bSgjelinek 		*tmp = '\0';
48180209230bSgjelinek 
48190209230bSgjelinek 		append_priv_token(BASIC_TOKEN, tmp, len);
48200209230bSgjelinek 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
48210209230bSgjelinek 			append_priv_token(pnp->pn_priv, tmp, len);
48220209230bSgjelinek 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
48230209230bSgjelinek 		(void) strlcat(tmp, privbuf, len);
48240209230bSgjelinek 	} else {
48250209230bSgjelinek 		tmp = privbuf;
48260209230bSgjelinek 	}
48270209230bSgjelinek 
48280209230bSgjelinek 
48290209230bSgjelinek 	/*
48300209230bSgjelinek 	 * In the next phase, attempt to convert the merged privilege
48310209230bSgjelinek 	 * string into a privilege set.  In the case of an error, either
48320209230bSgjelinek 	 * there was a memory allocation failure or there was an invalid
48330209230bSgjelinek 	 * privilege token in the string.  In either case, return an
48340209230bSgjelinek 	 * appropriate error code but in the event of an invalid token,
48350209230bSgjelinek 	 * allocate a string containing its name and return that back to
48360209230bSgjelinek 	 * the caller.
48370209230bSgjelinek 	 */
48380209230bSgjelinek 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
48390209230bSgjelinek 	if (mergeset == NULL) {
48400209230bSgjelinek 		if (token == NULL)
48410209230bSgjelinek 			return (Z_NOMEM);
48420209230bSgjelinek 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
48430209230bSgjelinek 			*cp = '\0';
48440209230bSgjelinek 		if ((*privname = strdup(token)) == NULL)
48450209230bSgjelinek 			return (Z_NOMEM);
48460209230bSgjelinek 		else
48470209230bSgjelinek 			return (Z_PRIV_UNKNOWN);
48480209230bSgjelinek 	}
48490209230bSgjelinek 
48500209230bSgjelinek 	/*
48510209230bSgjelinek 	 * Next, verify that none of the prohibited zone privileges are
48520209230bSgjelinek 	 * present in the merged privilege set.
48530209230bSgjelinek 	 */
48540209230bSgjelinek 	for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) {
48550209230bSgjelinek 		if (priv_ismember(mergeset, pnp->pn_priv)) {
48560209230bSgjelinek 			priv_freeset(mergeset);
48570209230bSgjelinek 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
48580209230bSgjelinek 				return (Z_NOMEM);
48590209230bSgjelinek 			else
48600209230bSgjelinek 				return (Z_PRIV_PROHIBITED);
48610209230bSgjelinek 		}
48620209230bSgjelinek 	}
48630209230bSgjelinek 
48640209230bSgjelinek 	/*
48650209230bSgjelinek 	 * Finally, verify that all of the required zone privileges are
48660209230bSgjelinek 	 * present in the merged privilege set.
48670209230bSgjelinek 	 */
48680209230bSgjelinek 	for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) {
48690209230bSgjelinek 		if (!priv_ismember(mergeset, pnp->pn_priv)) {
48700209230bSgjelinek 			priv_freeset(mergeset);
48710209230bSgjelinek 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
48720209230bSgjelinek 				return (Z_NOMEM);
48730209230bSgjelinek 			else
48740209230bSgjelinek 				return (Z_PRIV_REQUIRED);
48750209230bSgjelinek 		}
48760209230bSgjelinek 	}
48770209230bSgjelinek 
48780209230bSgjelinek 	priv_copyset(mergeset, privs);
48790209230bSgjelinek 	priv_freeset(mergeset);
48800209230bSgjelinek 	return (Z_OK);
48810209230bSgjelinek }
48820209230bSgjelinek 
48830209230bSgjelinek /*
48840209230bSgjelinek  * Fill in the supplied privilege set with either the default, safe set of
48850209230bSgjelinek  * privileges suitable for a non-global zone, or one based on the
48860209230bSgjelinek  * "limitpriv" property in the zone's configuration.
48870209230bSgjelinek  *
48880209230bSgjelinek  * In the event of an invalid privilege specification in the
48890209230bSgjelinek  * configuration, a string is allocated and returned containing the
48900209230bSgjelinek  * "privilege" causing the issue.  It is the caller's responsibility to
48910209230bSgjelinek  * free this memory when it is done with it.
48920209230bSgjelinek  */
48930209230bSgjelinek int
48940209230bSgjelinek zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
48950209230bSgjelinek     char **privname)
48960209230bSgjelinek {
48970209230bSgjelinek 	priv_lists_t *plp;
48980209230bSgjelinek 	char *cp, *limitpriv = NULL;
48990209230bSgjelinek 	int err, limitlen;
4900bf1d7e28Sdh 	zone_iptype_t iptype;
4901bf1d7e28Sdh 	const char *curr_iptype;
49020209230bSgjelinek 
49030209230bSgjelinek 	/*
49040209230bSgjelinek 	 * Attempt to lookup the "limitpriv" property.  If it does not
49050209230bSgjelinek 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
49060209230bSgjelinek 	 * default, safe privilege set is returned.
49070209230bSgjelinek 	 */
49080209230bSgjelinek 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK)
49090209230bSgjelinek 		return (err);
49100209230bSgjelinek 
4911bf1d7e28Sdh 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK)
4912bf1d7e28Sdh 		return (err);
4913bf1d7e28Sdh 
4914bf1d7e28Sdh 	switch (iptype) {
4915bf1d7e28Sdh 	case ZS_SHARED:
4916bf1d7e28Sdh 		curr_iptype = "shared";
4917bf1d7e28Sdh 		break;
4918bf1d7e28Sdh 	case ZS_EXCLUSIVE:
4919bf1d7e28Sdh 		curr_iptype = "exclusive";
4920bf1d7e28Sdh 		break;
4921bf1d7e28Sdh 	}
4922bf1d7e28Sdh 
4923bf1d7e28Sdh 	if ((err = priv_lists_create(handle, &plp, curr_iptype)) != Z_OK)
49240209230bSgjelinek 		return (err);
49250209230bSgjelinek 
49260209230bSgjelinek 	limitlen = strlen(limitpriv);
49270209230bSgjelinek 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
49280209230bSgjelinek 		free(limitpriv);
49290209230bSgjelinek 		err = get_default_privset(privs, plp);
49300209230bSgjelinek 		priv_lists_destroy(plp);
49310209230bSgjelinek 		return (err);
49320209230bSgjelinek 	}
49330209230bSgjelinek 
49340209230bSgjelinek 	/*
49350209230bSgjelinek 	 * Check if the string DEFAULT_TOKEN is the first token in a list
49360209230bSgjelinek 	 * of privileges.
49370209230bSgjelinek 	 */
49380209230bSgjelinek 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
49390209230bSgjelinek 	if (cp != NULL &&
49400209230bSgjelinek 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
49410209230bSgjelinek 		err = verify_privset(cp + 1, privs, privname, B_TRUE, plp);
49420209230bSgjelinek 	else
49430209230bSgjelinek 		err = verify_privset(limitpriv, privs, privname, B_FALSE, plp);
49440209230bSgjelinek 
49450209230bSgjelinek 	free(limitpriv);
49460209230bSgjelinek 	priv_lists_destroy(plp);
49470209230bSgjelinek 	return (err);
49480209230bSgjelinek }
49490209230bSgjelinek 
49500209230bSgjelinek int
49510209230bSgjelinek zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
49520209230bSgjelinek {
49530209230bSgjelinek 	zone_dochandle_t handle;
49540209230bSgjelinek 	boolean_t found = B_FALSE;
49550209230bSgjelinek 	struct zoneent *ze;
49560209230bSgjelinek 	FILE *cookie;
49570209230bSgjelinek 	int err;
49580209230bSgjelinek 	char *cp;
49590209230bSgjelinek 
49600209230bSgjelinek 	if (zone_name == NULL)
49610209230bSgjelinek 		return (Z_INVAL);
49620209230bSgjelinek 
49630209230bSgjelinek 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
49640209230bSgjelinek 	cp = zonepath + strlen(zonepath);
49650209230bSgjelinek 	while (cp > zonepath && cp[-1] == '/')
49660209230bSgjelinek 		*--cp = '\0';
49670209230bSgjelinek 
49680209230bSgjelinek 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
49690209230bSgjelinek 		if (zonepath[0] == '\0')
49700209230bSgjelinek 			(void) strlcpy(zonepath, "/", rp_sz);
49710209230bSgjelinek 		return (Z_OK);
49720209230bSgjelinek 	}
49730209230bSgjelinek 
49740209230bSgjelinek 	/*
49750209230bSgjelinek 	 * First check the index file.  Because older versions did not have
49760209230bSgjelinek 	 * a copy of the zone path, allow for it to be zero length, in which
49770209230bSgjelinek 	 * case we ignore this result and fall back to the XML files.
49780209230bSgjelinek 	 */
49790209230bSgjelinek 	cookie = setzoneent();
49800209230bSgjelinek 	while ((ze = getzoneent_private(cookie)) != NULL) {
49810209230bSgjelinek 		if (strcmp(ze->zone_name, zone_name) == 0) {
49820209230bSgjelinek 			found = B_TRUE;
49830209230bSgjelinek 			if (ze->zone_path[0] != '\0')
49840209230bSgjelinek 				(void) strlcpy(cp, ze->zone_path,
49850209230bSgjelinek 				    rp_sz - (cp - zonepath));
49860209230bSgjelinek 		}
49870209230bSgjelinek 		free(ze);
49880209230bSgjelinek 		if (found)
49890209230bSgjelinek 			break;
49900209230bSgjelinek 	}
49910209230bSgjelinek 	endzoneent(cookie);
49920209230bSgjelinek 	if (found && *cp != '\0')
49930209230bSgjelinek 		return (Z_OK);
49940209230bSgjelinek 
49950209230bSgjelinek 	/* Fall back to the XML files. */
49960209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL)
49970209230bSgjelinek 		return (Z_NOMEM);
49980209230bSgjelinek 
49990209230bSgjelinek 	/*
50000209230bSgjelinek 	 * Check the snapshot first: if a zone is running, its zonepath
50010209230bSgjelinek 	 * may have changed.
50020209230bSgjelinek 	 */
50030209230bSgjelinek 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
5004e767a340Sgjelinek 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK) {
5005e767a340Sgjelinek 			zonecfg_fini_handle(handle);
50060209230bSgjelinek 			return (err);
5007e767a340Sgjelinek 		}
50080209230bSgjelinek 	}
50090209230bSgjelinek 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
50100209230bSgjelinek 	zonecfg_fini_handle(handle);
50110209230bSgjelinek 	return (err);
50120209230bSgjelinek }
50130209230bSgjelinek 
50140209230bSgjelinek int
50150209230bSgjelinek zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
50160209230bSgjelinek {
50170209230bSgjelinek 	int err;
50180209230bSgjelinek 
50190209230bSgjelinek 	/* This function makes sense for non-global zones only. */
50200209230bSgjelinek 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
50210209230bSgjelinek 		return (Z_BOGUS_ZONE_NAME);
50220209230bSgjelinek 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
50230209230bSgjelinek 		return (err);
50240209230bSgjelinek 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
50250209230bSgjelinek 		return (Z_TOO_BIG);
50260209230bSgjelinek 	return (Z_OK);
50270209230bSgjelinek }
50280209230bSgjelinek 
50290209230bSgjelinek int
50300209230bSgjelinek zone_get_brand(char *zone_name, char *brandname, size_t rp_sz)
50310209230bSgjelinek {
50320209230bSgjelinek 	int err;
50330209230bSgjelinek 	zone_dochandle_t handle;
50340209230bSgjelinek 	char myzone[MAXNAMELEN];
50350209230bSgjelinek 	int myzoneid = getzoneid();
50360209230bSgjelinek 
50370209230bSgjelinek 	/*
50380209230bSgjelinek 	 * If we are not in the global zone, then we don't have the zone
50390209230bSgjelinek 	 * .xml files with the brand name available.  Thus, we are going to
50400209230bSgjelinek 	 * have to ask the kernel for the information.
50410209230bSgjelinek 	 */
50420209230bSgjelinek 	if (myzoneid != GLOBAL_ZONEID) {
50432ec67e04Sgjelinek 		if (is_system_labeled()) {
50442ec67e04Sgjelinek 			(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
50452ec67e04Sgjelinek 			return (Z_OK);
50462ec67e04Sgjelinek 		}
50470209230bSgjelinek 		if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone,
50480209230bSgjelinek 		    sizeof (myzone)) < 0)
50490209230bSgjelinek 			return (Z_NO_ZONE);
50500209230bSgjelinek 		if (strncmp(zone_name, myzone, MAXNAMELEN) != NULL)
50510209230bSgjelinek 			return (Z_NO_ZONE);
50520209230bSgjelinek 		err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz);
50530209230bSgjelinek 		if (err < 0)
50540209230bSgjelinek 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
50550209230bSgjelinek 		return (Z_OK);
50560209230bSgjelinek 	}
50570209230bSgjelinek 
50580209230bSgjelinek 	if (strcmp(zone_name, "global") == NULL) {
50590209230bSgjelinek 		(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
50602ec67e04Sgjelinek 		return (Z_OK);
50610209230bSgjelinek 	}
50620209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL)
50630209230bSgjelinek 		return (Z_NOMEM);
50640209230bSgjelinek 
50650209230bSgjelinek 	err = zonecfg_get_handle((char *)zone_name, handle);
50660209230bSgjelinek 	if (err == Z_OK)
50670209230bSgjelinek 		err = zonecfg_get_brand(handle, brandname, rp_sz);
50680209230bSgjelinek 
50690209230bSgjelinek 	zonecfg_fini_handle(handle);
50700209230bSgjelinek 	return (err);
50710209230bSgjelinek }
50720209230bSgjelinek 
50730209230bSgjelinek /*
50740209230bSgjelinek  * Return the appropriate root for the active /dev.
50750209230bSgjelinek  * For normal zone, the path is $ZONEPATH/root;
50760209230bSgjelinek  * for scratch zone, the dev path is $ZONEPATH/lu.
50770209230bSgjelinek  */
50780209230bSgjelinek int
50790209230bSgjelinek zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz)
50800209230bSgjelinek {
50810209230bSgjelinek 	int err;
50820209230bSgjelinek 	char *suffix;
50830209230bSgjelinek 	zone_state_t state;
50840209230bSgjelinek 
50850209230bSgjelinek 	/* This function makes sense for non-global zones only. */
50860209230bSgjelinek 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
50870209230bSgjelinek 		return (Z_BOGUS_ZONE_NAME);
50880209230bSgjelinek 	if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK)
50890209230bSgjelinek 		return (err);
50900209230bSgjelinek 
50910209230bSgjelinek 	if (zone_get_state(zone_name, &state) == Z_OK &&
50920209230bSgjelinek 	    state == ZONE_STATE_MOUNTED)
50930209230bSgjelinek 		suffix = "/lu";
50940209230bSgjelinek 	else
50950209230bSgjelinek 		suffix = "/root";
50960209230bSgjelinek 	if (strlcat(devroot, suffix, rp_sz) >= rp_sz)
50970209230bSgjelinek 		return (Z_TOO_BIG);
50980209230bSgjelinek 	return (Z_OK);
50990209230bSgjelinek }
51000209230bSgjelinek 
51010209230bSgjelinek static zone_state_t
51020209230bSgjelinek kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
51030209230bSgjelinek {
51040209230bSgjelinek 	char zoneroot[MAXPATHLEN];
51050209230bSgjelinek 	size_t zlen;
51060209230bSgjelinek 
51070209230bSgjelinek 	assert(kernel_state <= ZONE_MAX_STATE);
51080209230bSgjelinek 	switch (kernel_state) {
51090209230bSgjelinek 		case ZONE_IS_UNINITIALIZED:
51100209230bSgjelinek 			return (ZONE_STATE_READY);
51110209230bSgjelinek 		case ZONE_IS_READY:
51120209230bSgjelinek 			/*
51130209230bSgjelinek 			 * If the zone's root is mounted on $ZONEPATH/lu, then
51140209230bSgjelinek 			 * it's a mounted scratch zone.
51150209230bSgjelinek 			 */
51160209230bSgjelinek 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
51170209230bSgjelinek 			    sizeof (zoneroot)) >= 0) {
51180209230bSgjelinek 				zlen = strlen(zoneroot);
51190209230bSgjelinek 				if (zlen > 3 &&
51200209230bSgjelinek 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
51210209230bSgjelinek 					return (ZONE_STATE_MOUNTED);
51220209230bSgjelinek 			}
51230209230bSgjelinek 			return (ZONE_STATE_READY);
51240209230bSgjelinek 		case ZONE_IS_BOOTING:
51250209230bSgjelinek 		case ZONE_IS_RUNNING:
51260209230bSgjelinek 			return (ZONE_STATE_RUNNING);
51270209230bSgjelinek 		case ZONE_IS_SHUTTING_DOWN:
51280209230bSgjelinek 		case ZONE_IS_EMPTY:
51290209230bSgjelinek 			return (ZONE_STATE_SHUTTING_DOWN);
51300209230bSgjelinek 		case ZONE_IS_DOWN:
51310209230bSgjelinek 		case ZONE_IS_DYING:
51320209230bSgjelinek 		case ZONE_IS_DEAD:
51330209230bSgjelinek 		default:
51340209230bSgjelinek 			return (ZONE_STATE_DOWN);
51350209230bSgjelinek 	}
51360209230bSgjelinek 	/* NOTREACHED */
51370209230bSgjelinek }
51380209230bSgjelinek 
51390209230bSgjelinek int
51400209230bSgjelinek zone_get_state(char *zone_name, zone_state_t *state_num)
51410209230bSgjelinek {
51420209230bSgjelinek 	zone_status_t status;
51430209230bSgjelinek 	zoneid_t zone_id;
51440209230bSgjelinek 	struct zoneent *ze;
51450209230bSgjelinek 	boolean_t found = B_FALSE;
51460209230bSgjelinek 	FILE *cookie;
51470209230bSgjelinek 	char kernzone[ZONENAME_MAX];
51480209230bSgjelinek 	FILE *fp;
51490209230bSgjelinek 
51500209230bSgjelinek 	if (zone_name == NULL)
51510209230bSgjelinek 		return (Z_INVAL);
51520209230bSgjelinek 
51530209230bSgjelinek 	/*
51540209230bSgjelinek 	 * If we're looking at an alternate root, then we need to query the
51550209230bSgjelinek 	 * kernel using the scratch zone name.
51560209230bSgjelinek 	 */
51570209230bSgjelinek 	zone_id = -1;
51580209230bSgjelinek 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
51590209230bSgjelinek 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
51600209230bSgjelinek 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
51610209230bSgjelinek 			    kernzone, sizeof (kernzone)) == 0)
51620209230bSgjelinek 				zone_id = getzoneidbyname(kernzone);
51630209230bSgjelinek 			zonecfg_close_scratch(fp);
51640209230bSgjelinek 		}
51650209230bSgjelinek 	} else {
51660209230bSgjelinek 		zone_id = getzoneidbyname(zone_name);
51670209230bSgjelinek 	}
51680209230bSgjelinek 
51690209230bSgjelinek 	/* check to see if zone is running */
51700209230bSgjelinek 	if (zone_id != -1 &&
51710209230bSgjelinek 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
51720209230bSgjelinek 	    sizeof (status)) >= 0) {
51730209230bSgjelinek 		*state_num = kernel_state_to_user_state(zone_id, status);
51740209230bSgjelinek 		return (Z_OK);
51750209230bSgjelinek 	}
51760209230bSgjelinek 
51770209230bSgjelinek 	cookie = setzoneent();
51780209230bSgjelinek 	while ((ze = getzoneent_private(cookie)) != NULL) {
51790209230bSgjelinek 		if (strcmp(ze->zone_name, zone_name) == 0) {
51800209230bSgjelinek 			found = B_TRUE;
51810209230bSgjelinek 			*state_num = ze->zone_state;
51820209230bSgjelinek 		}
51830209230bSgjelinek 		free(ze);
51840209230bSgjelinek 		if (found)
51850209230bSgjelinek 			break;
51860209230bSgjelinek 	}
51870209230bSgjelinek 	endzoneent(cookie);
51880209230bSgjelinek 	return ((found) ? Z_OK : Z_NO_ZONE);
51890209230bSgjelinek }
51900209230bSgjelinek 
51910209230bSgjelinek int
51920209230bSgjelinek zone_set_state(char *zone, zone_state_t state)
51930209230bSgjelinek {
51940209230bSgjelinek 	struct zoneent ze;
51950209230bSgjelinek 
51960209230bSgjelinek 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
51970209230bSgjelinek 	    state != ZONE_STATE_INCOMPLETE)
51980209230bSgjelinek 		return (Z_INVAL);
51990209230bSgjelinek 
52000209230bSgjelinek 	bzero(&ze, sizeof (ze));
52010209230bSgjelinek 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
52020209230bSgjelinek 	ze.zone_state = state;
52030209230bSgjelinek 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
52040209230bSgjelinek 	return (putzoneent(&ze, PZE_MODIFY));
52050209230bSgjelinek }
52060209230bSgjelinek 
52070209230bSgjelinek /*
52080209230bSgjelinek  * Get id (if any) for specified zone.  There are four possible outcomes:
52090209230bSgjelinek  * - If the string corresponds to the numeric id of an active (booted)
52100209230bSgjelinek  *   zone, sets *zip to the zone id and returns 0.
52110209230bSgjelinek  * - If the string corresponds to the name of an active (booted) zone,
52120209230bSgjelinek  *   sets *zip to the zone id and returns 0.
52130209230bSgjelinek  * - If the string is a name in the configuration but is not booted,
52140209230bSgjelinek  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
52150209230bSgjelinek  * - Otherwise, leaves *zip unchanged and returns -1.
52160209230bSgjelinek  *
52170209230bSgjelinek  * This function acts as an auxiliary filter on the function of the same
52180209230bSgjelinek  * name in libc; the linker binds to this version if libzonecfg exists,
52190209230bSgjelinek  * and the libc version if it doesn't.  Any changes to this version of
52200209230bSgjelinek  * the function should probably be reflected in the libc version as well.
52210209230bSgjelinek  */
52220209230bSgjelinek int
52230209230bSgjelinek zone_get_id(const char *str, zoneid_t *zip)
52240209230bSgjelinek {
52250209230bSgjelinek 	zone_dochandle_t hdl;
52260209230bSgjelinek 	zoneid_t zoneid;
52270209230bSgjelinek 	char *cp;
52280209230bSgjelinek 	int err;
52290209230bSgjelinek 
52300209230bSgjelinek 	/* first try looking for active zone by id */
52310209230bSgjelinek 	errno = 0;
52320209230bSgjelinek 	zoneid = (zoneid_t)strtol(str, &cp, 0);
52330209230bSgjelinek 	if (errno == 0 && cp != str && *cp == '\0' &&
52340209230bSgjelinek 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
52350209230bSgjelinek 		*zip = zoneid;
52360209230bSgjelinek 		return (0);
52370209230bSgjelinek 	}
52380209230bSgjelinek 
52390209230bSgjelinek 	/* then look for active zone by name */
52400209230bSgjelinek 	if ((zoneid = getzoneidbyname(str)) != -1) {
52410209230bSgjelinek 		*zip = zoneid;
52420209230bSgjelinek 		return (0);
52430209230bSgjelinek 	}
52440209230bSgjelinek 
52450209230bSgjelinek 	/* if in global zone, try looking up name in configuration database */
52460209230bSgjelinek 	if (getzoneid() != GLOBAL_ZONEID ||
52470209230bSgjelinek 	    (hdl = zonecfg_init_handle()) == NULL)
52480209230bSgjelinek 		return (-1);
52490209230bSgjelinek 
52500209230bSgjelinek 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
52510209230bSgjelinek 		/* zone exists but isn't active */
52520209230bSgjelinek 		*zip = ZONE_ID_UNDEFINED;
52530209230bSgjelinek 		err = 0;
52540209230bSgjelinek 	} else {
52550209230bSgjelinek 		err = -1;
52560209230bSgjelinek 	}
52570209230bSgjelinek 
52580209230bSgjelinek 	zonecfg_fini_handle(hdl);
52590209230bSgjelinek 	return (err);
52600209230bSgjelinek }
52610209230bSgjelinek 
52620209230bSgjelinek char *
52630209230bSgjelinek zone_state_str(zone_state_t state_num)
52640209230bSgjelinek {
52650209230bSgjelinek 	switch (state_num) {
52660209230bSgjelinek 	case ZONE_STATE_CONFIGURED:
52670209230bSgjelinek 		return (ZONE_STATE_STR_CONFIGURED);
52680209230bSgjelinek 	case ZONE_STATE_INCOMPLETE:
52690209230bSgjelinek 		return (ZONE_STATE_STR_INCOMPLETE);
52700209230bSgjelinek 	case ZONE_STATE_INSTALLED:
52710209230bSgjelinek 		return (ZONE_STATE_STR_INSTALLED);
52720209230bSgjelinek 	case ZONE_STATE_READY:
52730209230bSgjelinek 		return (ZONE_STATE_STR_READY);
52740209230bSgjelinek 	case ZONE_STATE_MOUNTED:
52750209230bSgjelinek 		return (ZONE_STATE_STR_MOUNTED);
52760209230bSgjelinek 	case ZONE_STATE_RUNNING:
52770209230bSgjelinek 		return (ZONE_STATE_STR_RUNNING);
52780209230bSgjelinek 	case ZONE_STATE_SHUTTING_DOWN:
52790209230bSgjelinek 		return (ZONE_STATE_STR_SHUTTING_DOWN);
52800209230bSgjelinek 	case ZONE_STATE_DOWN:
52810209230bSgjelinek 		return (ZONE_STATE_STR_DOWN);
52820209230bSgjelinek 	default:
52830209230bSgjelinek 		return ("unknown");
52840209230bSgjelinek 	}
52850209230bSgjelinek }
52860209230bSgjelinek 
52870209230bSgjelinek /*
52880209230bSgjelinek  * Given a UUID value, find an associated zone name.  This is intended to be
52890209230bSgjelinek  * used by callers who set up some 'default' name (corresponding to the
52900209230bSgjelinek  * expected name for the zone) in the zonename buffer, and thus the function
52910209230bSgjelinek  * doesn't touch this buffer on failure.
52920209230bSgjelinek  */
52930209230bSgjelinek int
52940209230bSgjelinek zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
52950209230bSgjelinek {
52960209230bSgjelinek 	FILE *fp;
52970209230bSgjelinek 	struct zoneent *ze;
52980209230bSgjelinek 	uchar_t *uuid;
52990209230bSgjelinek 
53000209230bSgjelinek 	/*
53010209230bSgjelinek 	 * A small amount of subterfuge via casts is necessary here because
53020209230bSgjelinek 	 * libuuid doesn't use const correctly, but we don't want to export
53030209230bSgjelinek 	 * this brokenness to our clients.
53040209230bSgjelinek 	 */
53050209230bSgjelinek 	uuid = (uchar_t *)uuidin;
53060209230bSgjelinek 	if (uuid_is_null(uuid))
53070209230bSgjelinek 		return (Z_NO_ZONE);
53080209230bSgjelinek 	if ((fp = setzoneent()) == NULL)
53090209230bSgjelinek 		return (Z_NO_ZONE);
53100209230bSgjelinek 	while ((ze = getzoneent_private(fp)) != NULL) {
53110209230bSgjelinek 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
53120209230bSgjelinek 			break;
53130209230bSgjelinek 		free(ze);
53140209230bSgjelinek 	}
53150209230bSgjelinek 	endzoneent(fp);
53160209230bSgjelinek 	if (ze != NULL) {
53170209230bSgjelinek 		(void) strlcpy(zonename, ze->zone_name, namelen);
53180209230bSgjelinek 		free(ze);
53190209230bSgjelinek 		return (Z_OK);
53200209230bSgjelinek 	} else {
53210209230bSgjelinek 		return (Z_NO_ZONE);
53220209230bSgjelinek 	}
53230209230bSgjelinek }
53240209230bSgjelinek 
53250209230bSgjelinek /*
53260209230bSgjelinek  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
53270209230bSgjelinek  * exists but the file doesn't have a value set yet.  Returns an error if the
53280209230bSgjelinek  * zone cannot be located.
53290209230bSgjelinek  */
53300209230bSgjelinek int
53310209230bSgjelinek zonecfg_get_uuid(const char *zonename, uuid_t uuid)
53320209230bSgjelinek {
53330209230bSgjelinek 	FILE *fp;
53340209230bSgjelinek 	struct zoneent *ze;
53350209230bSgjelinek 
53360209230bSgjelinek 	if ((fp = setzoneent()) == NULL)
53370209230bSgjelinek 		return (Z_NO_ZONE);
53380209230bSgjelinek 	while ((ze = getzoneent_private(fp)) != NULL) {
53390209230bSgjelinek 		if (strcmp(ze->zone_name, zonename) == 0)
53400209230bSgjelinek 			break;
53410209230bSgjelinek 		free(ze);
53420209230bSgjelinek 	}
53430209230bSgjelinek 	endzoneent(fp);
53440209230bSgjelinek 	if (ze != NULL) {
53450209230bSgjelinek 		uuid_copy(uuid, ze->zone_uuid);
53460209230bSgjelinek 		free(ze);
53470209230bSgjelinek 		return (Z_OK);
53480209230bSgjelinek 	} else {
53490209230bSgjelinek 		return (Z_NO_ZONE);
53500209230bSgjelinek 	}
53510209230bSgjelinek }
53520209230bSgjelinek 
53530209230bSgjelinek /*
53540209230bSgjelinek  * File-system convenience functions.
53550209230bSgjelinek  */
53560209230bSgjelinek boolean_t
53570209230bSgjelinek zonecfg_valid_fs_type(const char *type)
53580209230bSgjelinek {
53590209230bSgjelinek 	/*
53600209230bSgjelinek 	 * We already know which FS types don't work.
53610209230bSgjelinek 	 */
53620209230bSgjelinek 	if (strcmp(type, "proc") == 0 ||
53630209230bSgjelinek 	    strcmp(type, "mntfs") == 0 ||
53640209230bSgjelinek 	    strcmp(type, "autofs") == 0 ||
53650209230bSgjelinek 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
53660209230bSgjelinek 	    strcmp(type, "cachefs") == 0)
53670209230bSgjelinek 		return (B_FALSE);
53680209230bSgjelinek 	/*
53690209230bSgjelinek 	 * The caller may do more detailed verification to make sure other
53700209230bSgjelinek 	 * aspects of this filesystem type make sense.
53710209230bSgjelinek 	 */
53720209230bSgjelinek 	return (B_TRUE);
53730209230bSgjelinek }
53740209230bSgjelinek 
53750209230bSgjelinek /*
53760209230bSgjelinek  * Generally uninteresting rctl convenience functions.
53770209230bSgjelinek  */
53780209230bSgjelinek 
53790209230bSgjelinek int
53800209230bSgjelinek zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
53810209230bSgjelinek     rctlblk_t *rctlblk)
53820209230bSgjelinek {
53830209230bSgjelinek 	unsigned long long ull;
53840209230bSgjelinek 	char *endp;
53850209230bSgjelinek 	rctl_priv_t priv;
53860209230bSgjelinek 	rctl_qty_t limit;
53870209230bSgjelinek 	uint_t action;
53880209230bSgjelinek 
53890209230bSgjelinek 	/* Get the privilege */
53900209230bSgjelinek 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
53910209230bSgjelinek 		priv = RCPRIV_BASIC;
53920209230bSgjelinek 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
53930209230bSgjelinek 		priv = RCPRIV_PRIVILEGED;
53940209230bSgjelinek 	} else {
53950209230bSgjelinek 		/* Invalid privilege */
53960209230bSgjelinek 		return (Z_INVAL);
53970209230bSgjelinek 	}
53980209230bSgjelinek 
53990209230bSgjelinek 	/* deal with negative input; strtoull(3c) doesn't do what we want */
54000209230bSgjelinek 	if (rctlval->zone_rctlval_limit[0] == '-')
54010209230bSgjelinek 		return (Z_INVAL);
54020209230bSgjelinek 	/* Get the limit */
54030209230bSgjelinek 	errno = 0;
54040209230bSgjelinek 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
54050209230bSgjelinek 	if (errno != 0 || *endp != '\0') {
54060209230bSgjelinek 		/* parse failed */
54070209230bSgjelinek 		return (Z_INVAL);
54080209230bSgjelinek 	}
54090209230bSgjelinek 	limit = (rctl_qty_t)ull;
54100209230bSgjelinek 
54110209230bSgjelinek 	/* Get the action */
54120209230bSgjelinek 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
54130209230bSgjelinek 		action = RCTL_LOCAL_NOACTION;
54140209230bSgjelinek 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
54150209230bSgjelinek 		action = RCTL_LOCAL_SIGNAL;
54160209230bSgjelinek 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
54170209230bSgjelinek 		action = RCTL_LOCAL_DENY;
54180209230bSgjelinek 	} else {
54190209230bSgjelinek 		/* Invalid Action */
54200209230bSgjelinek 		return (Z_INVAL);
54210209230bSgjelinek 	}
54220209230bSgjelinek 	rctlblk_set_local_action(rctlblk, action, 0);
54230209230bSgjelinek 	rctlblk_set_privilege(rctlblk, priv);
54240209230bSgjelinek 	rctlblk_set_value(rctlblk, limit);
54250209230bSgjelinek 	return (Z_OK);
54260209230bSgjelinek }
54270209230bSgjelinek 
54280209230bSgjelinek static int
54290209230bSgjelinek rctl_check(const char *rctlname, void *arg)
54300209230bSgjelinek {
54310209230bSgjelinek 	const char *attrname = arg;
54320209230bSgjelinek 
54330209230bSgjelinek 	/*
54340209230bSgjelinek 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
54357c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
54367c478bd9Sstevel@tonic-gate 	 */
54370209230bSgjelinek 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
54380209230bSgjelinek }
54390209230bSgjelinek 
54400209230bSgjelinek boolean_t
54410209230bSgjelinek zonecfg_is_rctl(const char *name)
54420209230bSgjelinek {
54430209230bSgjelinek 	return (rctl_walk(rctl_check, (void *)name) == 1);
54440209230bSgjelinek }
54450209230bSgjelinek 
54460209230bSgjelinek boolean_t
54470209230bSgjelinek zonecfg_valid_rctlname(const char *name)
54480209230bSgjelinek {
54490209230bSgjelinek 	const char *c;
54500209230bSgjelinek 
54510209230bSgjelinek 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
54520209230bSgjelinek 		return (B_FALSE);
54530209230bSgjelinek 	if (strlen(name) == sizeof ("zone.") - 1)
54540209230bSgjelinek 		return (B_FALSE);
54550209230bSgjelinek 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
54560209230bSgjelinek 		if (!isalpha(*c) && *c != '-')
54570209230bSgjelinek 			return (B_FALSE);
54580209230bSgjelinek 	}
54590209230bSgjelinek 	return (B_TRUE);
54600209230bSgjelinek }
54610209230bSgjelinek 
54620209230bSgjelinek boolean_t
54630209230bSgjelinek zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
54640209230bSgjelinek {
54650209230bSgjelinek 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
54660209230bSgjelinek 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
54670209230bSgjelinek 
54680209230bSgjelinek 	if (priv != RCPRIV_PRIVILEGED)
54690209230bSgjelinek 		return (B_FALSE);
54700209230bSgjelinek 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
54710209230bSgjelinek 		return (B_FALSE);
54720209230bSgjelinek 	return (B_TRUE);
54730209230bSgjelinek }
54740209230bSgjelinek 
54750209230bSgjelinek boolean_t
54760209230bSgjelinek zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
54770209230bSgjelinek {
54780209230bSgjelinek 	rctlblk_t *current, *next;
54790209230bSgjelinek 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
54800209230bSgjelinek 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
54810209230bSgjelinek 	uint_t global_flags;
54820209230bSgjelinek 
54830209230bSgjelinek 	if (!zonecfg_valid_rctlblk(rctlblk))
54840209230bSgjelinek 		return (B_FALSE);
54850209230bSgjelinek 	if (!zonecfg_valid_rctlname(name))
54860209230bSgjelinek 		return (B_FALSE);
54870209230bSgjelinek 
54880209230bSgjelinek 	current = alloca(rctlblk_size());
54890209230bSgjelinek 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
54900209230bSgjelinek 		return (B_TRUE);	/* not an rctl on this system */
54910209230bSgjelinek 	/*
54920209230bSgjelinek 	 * Make sure the proposed value isn't greater than the current system
54930209230bSgjelinek 	 * value.
54940209230bSgjelinek 	 */
54950209230bSgjelinek 	next = alloca(rctlblk_size());
54960209230bSgjelinek 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
54970209230bSgjelinek 		rctlblk_t *tmp;
54980209230bSgjelinek 
54990209230bSgjelinek 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
55000209230bSgjelinek 			return (B_FALSE);	/* shouldn't happen */
55010209230bSgjelinek 		tmp = current;
55020209230bSgjelinek 		current = next;
55030209230bSgjelinek 		next = tmp;
55040209230bSgjelinek 	}
55050209230bSgjelinek 	if (limit > rctlblk_get_value(current))
55060209230bSgjelinek 		return (B_FALSE);
55070209230bSgjelinek 
55080209230bSgjelinek 	/*
55090209230bSgjelinek 	 * Make sure the proposed action is allowed.
55100209230bSgjelinek 	 */
55110209230bSgjelinek 	global_flags = rctlblk_get_global_flags(current);
55120209230bSgjelinek 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
55130209230bSgjelinek 	    action == RCTL_LOCAL_DENY)
55140209230bSgjelinek 		return (B_FALSE);
55150209230bSgjelinek 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
55160209230bSgjelinek 	    action == RCTL_LOCAL_NOACTION)
55170209230bSgjelinek 		return (B_FALSE);
55180209230bSgjelinek 
55190209230bSgjelinek 	return (B_TRUE);
55200209230bSgjelinek }
55210209230bSgjelinek 
55220209230bSgjelinek /*
55230209230bSgjelinek  * There is always a race condition between reading the initial copy of
55240209230bSgjelinek  * a zones state and its state changing.  We address this by providing
55250209230bSgjelinek  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
55260209230bSgjelinek  * When zonecfg_critical_enter is called, sets the state field to LOCKED
55270209230bSgjelinek  * and aquires biglock. Biglock protects against other threads executing
55280209230bSgjelinek  * critical_enter and the state field protects against state changes during
55290209230bSgjelinek  * the critical period.
55300209230bSgjelinek  *
55310209230bSgjelinek  * If any state changes occur, zn_cb will set the failed field of the znotify
55320209230bSgjelinek  * structure.  This will cause the critical_exit function to re-lock the
55330209230bSgjelinek  * channel and return an error. Since evsnts may be delayed, the critical_exit
55340209230bSgjelinek  * function "flushes" the queue by putting an event on the queue and waiting for
55350209230bSgjelinek  * zn_cb to notify critical_exit that it received the ping event.
55360209230bSgjelinek  */
55370209230bSgjelinek static const char *
55380209230bSgjelinek string_get_tok(const char *in, char delim, int num)
55390209230bSgjelinek {
55400209230bSgjelinek 	int i = 0;
55410209230bSgjelinek 
55420209230bSgjelinek 	for (; i < num; in++) {
55430209230bSgjelinek 		if (*in == delim)
55440209230bSgjelinek 			i++;
55450209230bSgjelinek 		if (*in == 0)
55460209230bSgjelinek 			return (NULL);
55470209230bSgjelinek 	}
55480209230bSgjelinek 	return (in);
55490209230bSgjelinek }
55500209230bSgjelinek 
55510209230bSgjelinek static boolean_t
55520209230bSgjelinek is_ping(sysevent_t *ev)
55530209230bSgjelinek {
55540209230bSgjelinek 	if (strcmp(sysevent_get_subclass_name(ev),
55550209230bSgjelinek 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
55560209230bSgjelinek 		return (B_TRUE);
55570209230bSgjelinek 	} else {
55580209230bSgjelinek 		return (B_FALSE);
55590209230bSgjelinek 	}
55600209230bSgjelinek }
55610209230bSgjelinek 
55620209230bSgjelinek static boolean_t
55630209230bSgjelinek is_my_ping(sysevent_t *ev)
55640209230bSgjelinek {
55650209230bSgjelinek 	const char *sender;
55660209230bSgjelinek 	char mypid[sizeof (pid_t) * 3 + 1];
55670209230bSgjelinek 
55680209230bSgjelinek 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
55690209230bSgjelinek 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
55700209230bSgjelinek 	if (sender == NULL)
55710209230bSgjelinek 		return (B_FALSE);
55720209230bSgjelinek 	if (strcmp(sender, mypid) != 0)
55730209230bSgjelinek 		return (B_FALSE);
55740209230bSgjelinek 	return (B_TRUE);
55750209230bSgjelinek }
55760209230bSgjelinek 
55770209230bSgjelinek static int
55780209230bSgjelinek do_callback(struct znotify *zevtchan, sysevent_t *ev)
55790209230bSgjelinek {
55800209230bSgjelinek 	nvlist_t *l;
55810209230bSgjelinek 	int zid;
55820209230bSgjelinek 	char *zonename;
55830209230bSgjelinek 	char *newstate;
55840209230bSgjelinek 	char *oldstate;
55850209230bSgjelinek 	int ret;
55860209230bSgjelinek 	hrtime_t when;
55870209230bSgjelinek 
55880209230bSgjelinek 	if (strcmp(sysevent_get_subclass_name(ev),
55890209230bSgjelinek 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
55900209230bSgjelinek 
55910209230bSgjelinek 		if (sysevent_get_attr_list(ev, &l) != 0) {
55920209230bSgjelinek 			if (errno == ENOMEM) {
55930209230bSgjelinek 				zevtchan->zn_failure_count++;
55940209230bSgjelinek 				return (EAGAIN);
55950209230bSgjelinek 			}
55960209230bSgjelinek 			return (0);
55970209230bSgjelinek 		}
55980209230bSgjelinek 		ret = 0;
55990209230bSgjelinek 
56000209230bSgjelinek 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
56010209230bSgjelinek 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
56020209230bSgjelinek 		    == 0) &&
56030209230bSgjelinek 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
56040209230bSgjelinek 		    == 0) &&
56050209230bSgjelinek 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
56060209230bSgjelinek 		    (uint64_t *)&when) == 0) &&
56070209230bSgjelinek 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
56080209230bSgjelinek 			ret = zevtchan->zn_callback(zonename, zid, newstate,
56090209230bSgjelinek 			    oldstate, when, zevtchan->zn_private);
56100209230bSgjelinek 		}
56110209230bSgjelinek 
56120209230bSgjelinek 		zevtchan->zn_failure_count = 0;
56130209230bSgjelinek 		nvlist_free(l);
56140209230bSgjelinek 		return (ret);
56150209230bSgjelinek 	} else {
56160209230bSgjelinek 		/*
56170209230bSgjelinek 		 * We have received an event in an unknown subclass. Ignore.
56180209230bSgjelinek 		 */
56190209230bSgjelinek 		zevtchan->zn_failure_count = 0;
56200209230bSgjelinek 		return (0);
56210209230bSgjelinek 	}
56220209230bSgjelinek }
56230209230bSgjelinek 
56240209230bSgjelinek static int
56250209230bSgjelinek zn_cb(sysevent_t *ev, void *p)
56260209230bSgjelinek {
56270209230bSgjelinek 	struct znotify *zevtchan = p;
56280209230bSgjelinek 	int error;
56290209230bSgjelinek 
56300209230bSgjelinek 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
56310209230bSgjelinek 
56320209230bSgjelinek 	if (is_ping(ev) && !is_my_ping(ev)) {
56330209230bSgjelinek 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
56340209230bSgjelinek 		return (0);
56350209230bSgjelinek 	}
56360209230bSgjelinek 
56370209230bSgjelinek 	if (zevtchan->zn_state == ZN_LOCKED) {
56380209230bSgjelinek 		assert(!is_ping(ev));
56390209230bSgjelinek 		zevtchan->zn_failed = B_TRUE;
56400209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
56410209230bSgjelinek 		return (0);
56420209230bSgjelinek 	}
56430209230bSgjelinek 
56440209230bSgjelinek 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
56450209230bSgjelinek 		if (is_ping(ev)) {
56460209230bSgjelinek 			zevtchan->zn_state = ZN_PING_RECEIVED;
56470209230bSgjelinek 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
56480209230bSgjelinek 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
56490209230bSgjelinek 			return (0);
56500209230bSgjelinek 		} else {
56510209230bSgjelinek 			zevtchan->zn_failed = B_TRUE;
56520209230bSgjelinek 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
56530209230bSgjelinek 			return (0);
56540209230bSgjelinek 		}
56550209230bSgjelinek 	}
56560209230bSgjelinek 
56570209230bSgjelinek 	if (zevtchan->zn_state == ZN_UNLOCKED) {
56580209230bSgjelinek 
56590209230bSgjelinek 		error = do_callback(zevtchan, ev);
56600209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
56610209230bSgjelinek 		/*
56620209230bSgjelinek 		 * Every ENOMEM failure causes do_callback to increment
56630209230bSgjelinek 		 * zn_failure_count and every success causes it to
56640209230bSgjelinek 		 * set zn_failure_count to zero.  If we got EAGAIN,
56650209230bSgjelinek 		 * we will sleep for zn_failure_count seconds and return
56660209230bSgjelinek 		 * EAGAIN to gpec to try again.
56670209230bSgjelinek 		 *
56680209230bSgjelinek 		 * After 55 seconds, or 10 try's we give up and drop the
56690209230bSgjelinek 		 * event.
56700209230bSgjelinek 		 */
56710209230bSgjelinek 		if (error == EAGAIN) {
56720209230bSgjelinek 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
56730209230bSgjelinek 				return (0);
56740209230bSgjelinek 			}
56750209230bSgjelinek 			(void) sleep(zevtchan->zn_failure_count);
56760209230bSgjelinek 		}
56770209230bSgjelinek 		return (error);
56780209230bSgjelinek 	}
56790209230bSgjelinek 
56800209230bSgjelinek 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
56810209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
56820209230bSgjelinek 		return (0);
56830209230bSgjelinek 	}
56840209230bSgjelinek 
56850209230bSgjelinek 	abort();
56860209230bSgjelinek 	return (0);
56870209230bSgjelinek }
56880209230bSgjelinek 
56890209230bSgjelinek void
56900209230bSgjelinek zonecfg_notify_critical_enter(void *h)
56910209230bSgjelinek {
56920209230bSgjelinek 	struct znotify *zevtchan = h;
56930209230bSgjelinek 
56940209230bSgjelinek 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
56950209230bSgjelinek 	zevtchan->zn_state = ZN_LOCKED;
56960209230bSgjelinek }
56970209230bSgjelinek 
56980209230bSgjelinek int
56990209230bSgjelinek zonecfg_notify_critical_exit(void * h)
57000209230bSgjelinek {
57010209230bSgjelinek 
57020209230bSgjelinek 	struct znotify *zevtchan = h;
57030209230bSgjelinek 
57040209230bSgjelinek 	if (zevtchan->zn_state == ZN_UNLOCKED)
57050209230bSgjelinek 		return (0);
57060209230bSgjelinek 
57070209230bSgjelinek 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
57080209230bSgjelinek 	zevtchan->zn_state = ZN_PING_INFLIGHT;
57090209230bSgjelinek 
57100209230bSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
57110209230bSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
57120209230bSgjelinek 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
57130209230bSgjelinek 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
57140209230bSgjelinek 
57150209230bSgjelinek 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
57160209230bSgjelinek 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
57170209230bSgjelinek 		    &(zevtchan->zn_mutex));
57180209230bSgjelinek 	}
57190209230bSgjelinek 
57200209230bSgjelinek 	if (zevtchan->zn_failed == B_TRUE) {
57210209230bSgjelinek 		zevtchan->zn_state = ZN_LOCKED;
57220209230bSgjelinek 		zevtchan->zn_failed = B_FALSE;
57230209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
57240209230bSgjelinek 		return (1);
57250209230bSgjelinek 	}
57260209230bSgjelinek 
57270209230bSgjelinek 	zevtchan->zn_state = ZN_UNLOCKED;
57280209230bSgjelinek 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
57290209230bSgjelinek 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
57300209230bSgjelinek 	return (0);
57317c478bd9Sstevel@tonic-gate }
57327c478bd9Sstevel@tonic-gate 
57330209230bSgjelinek void
57340209230bSgjelinek zonecfg_notify_critical_abort(void *h)
57357c478bd9Sstevel@tonic-gate {
57360209230bSgjelinek 	struct znotify *zevtchan = h;
57370209230bSgjelinek 
57380209230bSgjelinek 	zevtchan->zn_state = ZN_UNLOCKED;
57390209230bSgjelinek 	zevtchan->zn_failed = B_FALSE;
57400209230bSgjelinek 	/*
57410209230bSgjelinek 	 * Don't do anything about zn_lock. If it is held, it could only be
57420209230bSgjelinek 	 * held by zn_cb and it will be unlocked soon.
57430209230bSgjelinek 	 */
57440209230bSgjelinek 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
57457c478bd9Sstevel@tonic-gate }
57467c478bd9Sstevel@tonic-gate 
57470209230bSgjelinek void *
57480209230bSgjelinek zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
57490209230bSgjelinek     const char *newstate, const char *oldstate, hrtime_t when, void *p),
57500209230bSgjelinek     void *p)
57517c478bd9Sstevel@tonic-gate {
57520209230bSgjelinek 	struct znotify *zevtchan;
57530209230bSgjelinek 	int i = 1;
57540209230bSgjelinek 	int r;
57557c478bd9Sstevel@tonic-gate 
57560209230bSgjelinek 	zevtchan = malloc(sizeof (struct znotify));
57570209230bSgjelinek 
57580209230bSgjelinek 	if (zevtchan == NULL)
57590209230bSgjelinek 		return (NULL);
57600209230bSgjelinek 
57610209230bSgjelinek 	zevtchan->zn_private = p;
57620209230bSgjelinek 	zevtchan->zn_callback = func;
57630209230bSgjelinek 	zevtchan->zn_state = ZN_UNLOCKED;
57640209230bSgjelinek 	zevtchan->zn_failed = B_FALSE;
57650209230bSgjelinek 
57660209230bSgjelinek 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
57670209230bSgjelinek 		goto out3;
57680209230bSgjelinek 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
57690209230bSgjelinek 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
57700209230bSgjelinek 		goto out3;
57710209230bSgjelinek 	}
57720209230bSgjelinek 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
57730209230bSgjelinek 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
57740209230bSgjelinek 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
57750209230bSgjelinek 		goto out3;
57767c478bd9Sstevel@tonic-gate 	}
57777c478bd9Sstevel@tonic-gate 
57780209230bSgjelinek 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
57790209230bSgjelinek 	    0) != 0)
57800209230bSgjelinek 		goto out2;
57817c478bd9Sstevel@tonic-gate 
57820209230bSgjelinek 	do {
57830209230bSgjelinek 		/*
57840209230bSgjelinek 		 * At 4 digits the subscriber ID gets too long and we have
57850209230bSgjelinek 		 * no chance of successfully registering.
57860209230bSgjelinek 		 */
57870209230bSgjelinek 		if (i > 999)
57880209230bSgjelinek 			goto out1;
57890209230bSgjelinek 
57900209230bSgjelinek 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
57910209230bSgjelinek 		    getpid() % 999999l, i);
57920209230bSgjelinek 
57930209230bSgjelinek 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
57940209230bSgjelinek 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
57950209230bSgjelinek 		    zevtchan, 0);
57960209230bSgjelinek 
57970209230bSgjelinek 		i++;
57980209230bSgjelinek 
57990209230bSgjelinek 	} while (r);
58000209230bSgjelinek 
58010209230bSgjelinek 	return (zevtchan);
58020209230bSgjelinek out1:
58030209230bSgjelinek 	sysevent_evc_unbind(zevtchan->zn_eventchan);
58040209230bSgjelinek out2:
58050209230bSgjelinek 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
58060209230bSgjelinek 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
58070209230bSgjelinek 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
58080209230bSgjelinek out3:
58090209230bSgjelinek 	free(zevtchan);
58100209230bSgjelinek 
58110209230bSgjelinek 	return (NULL);
58127c478bd9Sstevel@tonic-gate }
58137c478bd9Sstevel@tonic-gate 
58140209230bSgjelinek void
58150209230bSgjelinek zonecfg_notify_unbind(void *handle)
58167c478bd9Sstevel@tonic-gate {
58177c478bd9Sstevel@tonic-gate 
58180209230bSgjelinek 	int ret;
58197c478bd9Sstevel@tonic-gate 
58200209230bSgjelinek 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
58217c478bd9Sstevel@tonic-gate 	/*
58220209230bSgjelinek 	 * Check that all evc threads have gone away. This should be
58230209230bSgjelinek 	 * enforced by sysevent_evc_unbind.
58247c478bd9Sstevel@tonic-gate 	 */
58250209230bSgjelinek 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
58267c478bd9Sstevel@tonic-gate 
58270209230bSgjelinek 	if (ret)
58280209230bSgjelinek 		abort();
58297c478bd9Sstevel@tonic-gate 
58300209230bSgjelinek 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
58310209230bSgjelinek 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
58320209230bSgjelinek 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
58330209230bSgjelinek 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
58347c478bd9Sstevel@tonic-gate 
58350209230bSgjelinek 	free(handle);
58367c478bd9Sstevel@tonic-gate }
5837fa9e4066Sahrens 
58380209230bSgjelinek static int
58390209230bSgjelinek zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5840cf8f45c7Sdstaff {
58410209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
58420209230bSgjelinek 	int err;
5843cf8f45c7Sdstaff 
58440209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
58450209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME,
58460209230bSgjelinek 	    tabptr->zone_dataset_name)) != Z_OK)
58470209230bSgjelinek 		return (err);
58480209230bSgjelinek 	return (Z_OK);
5849cf8f45c7Sdstaff }
5850cf8f45c7Sdstaff 
58510209230bSgjelinek int
58520209230bSgjelinek zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5853cf8f45c7Sdstaff {
58540209230bSgjelinek 	int err;
58550209230bSgjelinek 
58560209230bSgjelinek 	if (tabptr == NULL)
58570209230bSgjelinek 		return (Z_INVAL);
58580209230bSgjelinek 
58590209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
58600209230bSgjelinek 		return (err);
58610209230bSgjelinek 
58620209230bSgjelinek 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
58630209230bSgjelinek 		return (err);
58640209230bSgjelinek 
58650209230bSgjelinek 	return (Z_OK);
58660209230bSgjelinek }
58670209230bSgjelinek 
58680209230bSgjelinek static int
58690209230bSgjelinek zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
58700209230bSgjelinek {
58710209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
58720209230bSgjelinek 
58730209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
58740209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
58750209230bSgjelinek 			continue;
58760209230bSgjelinek 
58770209230bSgjelinek 		if (match_prop(cur, DTD_ATTR_NAME,
58780209230bSgjelinek 		    tabptr->zone_dataset_name)) {
58790209230bSgjelinek 			xmlUnlinkNode(cur);
58800209230bSgjelinek 			xmlFreeNode(cur);
58810209230bSgjelinek 			return (Z_OK);
58820209230bSgjelinek 		}
5883cf8f45c7Sdstaff 	}
58840209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
58850209230bSgjelinek }
58860209230bSgjelinek 
58870209230bSgjelinek int
58880209230bSgjelinek zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
58890209230bSgjelinek {
58900209230bSgjelinek 	int err;
58910209230bSgjelinek 
58920209230bSgjelinek 	if (tabptr == NULL)
58930209230bSgjelinek 		return (Z_INVAL);
58940209230bSgjelinek 
58950209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
58960209230bSgjelinek 		return (err);
58970209230bSgjelinek 
58980209230bSgjelinek 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
58990209230bSgjelinek 		return (err);
59000209230bSgjelinek 
59010209230bSgjelinek 	return (Z_OK);
5902cf8f45c7Sdstaff }
5903cf8f45c7Sdstaff 
59040209230bSgjelinek int
59050209230bSgjelinek zonecfg_modify_ds(
59060209230bSgjelinek 	zone_dochandle_t handle,
59070209230bSgjelinek 	struct zone_dstab *oldtabptr,
59080209230bSgjelinek 	struct zone_dstab *newtabptr)
59090209230bSgjelinek {
59100209230bSgjelinek 	int err;
59110209230bSgjelinek 
59120209230bSgjelinek 	if (oldtabptr == NULL || newtabptr == NULL)
59130209230bSgjelinek 		return (Z_INVAL);
59140209230bSgjelinek 
59150209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
59160209230bSgjelinek 		return (err);
5917cf8f45c7Sdstaff 
59180209230bSgjelinek 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
59190209230bSgjelinek 		return (err);
59200209230bSgjelinek 
59210209230bSgjelinek 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
59220209230bSgjelinek 		return (err);
59230209230bSgjelinek 
59240209230bSgjelinek 	return (Z_OK);
5925cf8f45c7Sdstaff }
5926cf8f45c7Sdstaff 
59270209230bSgjelinek int
59280209230bSgjelinek zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5929cf8f45c7Sdstaff {
59300209230bSgjelinek 	xmlNodePtr cur, firstmatch;
59310209230bSgjelinek 	int err;
59320209230bSgjelinek 	char dataset[MAXNAMELEN];
5933cf8f45c7Sdstaff 
59340209230bSgjelinek 	if (tabptr == NULL)
59350209230bSgjelinek 		return (Z_INVAL);
5936cf8f45c7Sdstaff 
59370209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
59380209230bSgjelinek 		return (err);
59390209230bSgjelinek 
59400209230bSgjelinek 	cur = handle->zone_dh_cur;
59410209230bSgjelinek 	firstmatch = NULL;
59420209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
59430209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
59440209230bSgjelinek 			continue;
59450209230bSgjelinek 		if (strlen(tabptr->zone_dataset_name) > 0) {
59460209230bSgjelinek 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
59470209230bSgjelinek 			    sizeof (dataset)) == Z_OK) &&
59480209230bSgjelinek 			    (strcmp(tabptr->zone_dataset_name,
59490209230bSgjelinek 			    dataset) == 0)) {
59500209230bSgjelinek 				if (firstmatch == NULL)
59510209230bSgjelinek 					firstmatch = cur;
59520209230bSgjelinek 				else
59530209230bSgjelinek 					return (Z_INSUFFICIENT_SPEC);
5954cf8f45c7Sdstaff 			}
5955cf8f45c7Sdstaff 		}
59560209230bSgjelinek 	}
59570209230bSgjelinek 	if (firstmatch == NULL)
59580209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
5959cf8f45c7Sdstaff 
59600209230bSgjelinek 	cur = firstmatch;
5961cf8f45c7Sdstaff 
59620209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
59630209230bSgjelinek 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
59640209230bSgjelinek 		return (err);
59650209230bSgjelinek 
59660209230bSgjelinek 	return (Z_OK);
5967cf8f45c7Sdstaff }
5968cf8f45c7Sdstaff 
59690209230bSgjelinek int
59700209230bSgjelinek zonecfg_setdsent(zone_dochandle_t handle)
5971cf8f45c7Sdstaff {
59720209230bSgjelinek 	return (zonecfg_setent(handle));
59730209230bSgjelinek }
5974cf8f45c7Sdstaff 
59750209230bSgjelinek int
59760209230bSgjelinek zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
59770209230bSgjelinek {
59780209230bSgjelinek 	xmlNodePtr cur;
59790209230bSgjelinek 	int err;
5980cf8f45c7Sdstaff 
59810209230bSgjelinek 	if (handle == NULL)
59820209230bSgjelinek 		return (Z_INVAL);
5983cf8f45c7Sdstaff 
59840209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
59850209230bSgjelinek 		return (Z_NO_ENTRY);
5986cf8f45c7Sdstaff 
59870209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
59880209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
59890209230bSgjelinek 			break;
59900209230bSgjelinek 	if (cur == NULL) {
59910209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
59920209230bSgjelinek 		return (Z_NO_ENTRY);
5993cf8f45c7Sdstaff 	}
5994cf8f45c7Sdstaff 
59950209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
59960209230bSgjelinek 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
59970209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
59980209230bSgjelinek 		return (err);
5999cf8f45c7Sdstaff 	}
6000cf8f45c7Sdstaff 
60010209230bSgjelinek 	handle->zone_dh_cur = cur->next;
60020209230bSgjelinek 	return (Z_OK);
6003cf8f45c7Sdstaff }
6004cf8f45c7Sdstaff 
60050209230bSgjelinek int
60060209230bSgjelinek zonecfg_enddsent(zone_dochandle_t handle)
6007cf8f45c7Sdstaff {
60080209230bSgjelinek 	return (zonecfg_endent(handle));
6009cf8f45c7Sdstaff }
6010cf8f45c7Sdstaff 
60110209230bSgjelinek /*
60120209230bSgjelinek  * Support for aliased rctls; that is, rctls that have simplified names in
60130209230bSgjelinek  * zonecfg.  For example, max-lwps is an alias for a well defined zone.max-lwps
60140209230bSgjelinek  * rctl.  If there are multiple existing values for one of these rctls or if
60150209230bSgjelinek  * there is a single value that does not match the well defined template (i.e.
60160209230bSgjelinek  * it has a different action) then we cannot treat the rctl as having an alias
60170209230bSgjelinek  * so we return Z_ALIAS_DISALLOW.  That means that the rctl cannot be
60180209230bSgjelinek  * managed in zonecfg via an alias and that the standard rctl syntax must be
60190209230bSgjelinek  * used.
60200209230bSgjelinek  *
60210209230bSgjelinek  * The possible return values are:
60220209230bSgjelinek  *	Z_NO_PROPERTY_ID - invalid alias name
60230209230bSgjelinek  *	Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition
60240209230bSgjelinek  *	Z_NO_ENTRY - no rctl is configured for this alias
60250209230bSgjelinek  *	Z_OK - we got a valid rctl for the specified alias
60260209230bSgjelinek  */
6027cf8f45c7Sdstaff int
60280209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval)
6029cf8f45c7Sdstaff {
60300209230bSgjelinek 	boolean_t found = B_FALSE;
60310209230bSgjelinek 	boolean_t found_val = B_FALSE;
60320209230bSgjelinek 	xmlNodePtr cur, val;
60330209230bSgjelinek 	char savedname[MAXNAMELEN];
60340209230bSgjelinek 	struct zone_rctlvaltab rctl;
60350209230bSgjelinek 	int i;
60360209230bSgjelinek 	int err;
6037cf8f45c7Sdstaff 
60380209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
60390209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
60400209230bSgjelinek 			break;
6041cf8f45c7Sdstaff 
60420209230bSgjelinek 	if (aliases[i].shortname == NULL)
60430209230bSgjelinek 		return (Z_NO_PROPERTY_ID);
6044cf8f45c7Sdstaff 
60450209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
60460209230bSgjelinek 		return (err);
6047cf8f45c7Sdstaff 
60480209230bSgjelinek 	cur = handle->zone_dh_cur;
60490209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
60500209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0)
60510209230bSgjelinek 			continue;
60520209230bSgjelinek 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
60530209230bSgjelinek 		    sizeof (savedname)) == Z_OK) &&
60540209230bSgjelinek 		    (strcmp(savedname, aliases[i].realname) == 0)) {
6055cf8f45c7Sdstaff 
60560209230bSgjelinek 			/*
60570209230bSgjelinek 			 * If we already saw one of these, we can't have an
60580209230bSgjelinek 			 * alias since we just found another.
60590209230bSgjelinek 			 */
60600209230bSgjelinek 			if (found)
60610209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
60620209230bSgjelinek 			found = B_TRUE;
60630209230bSgjelinek 
60640209230bSgjelinek 			for (val = cur->xmlChildrenNode; val != NULL;
60650209230bSgjelinek 			    val = val->next) {
60660209230bSgjelinek 				/*
60670209230bSgjelinek 				 * If we already have one value, we can't have
60680209230bSgjelinek 				 * an alias since we just found another.
60690209230bSgjelinek 				 */
60700209230bSgjelinek 				if (found_val)
60710209230bSgjelinek 					return (Z_ALIAS_DISALLOW);
60720209230bSgjelinek 				found_val = B_TRUE;
60730209230bSgjelinek 
60740209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_PRIV,
60750209230bSgjelinek 				    rctl.zone_rctlval_priv,
60760209230bSgjelinek 				    sizeof (rctl.zone_rctlval_priv)) != Z_OK))
60770209230bSgjelinek 					break;
60780209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_LIMIT,
60790209230bSgjelinek 				    rctl.zone_rctlval_limit,
60800209230bSgjelinek 				    sizeof (rctl.zone_rctlval_limit)) != Z_OK))
60810209230bSgjelinek 					break;
60820209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_ACTION,
60830209230bSgjelinek 				    rctl.zone_rctlval_action,
60840209230bSgjelinek 				    sizeof (rctl.zone_rctlval_action)) != Z_OK))
60850209230bSgjelinek 					break;
60860209230bSgjelinek 			}
60870209230bSgjelinek 
60880209230bSgjelinek 			/* check priv and action match the expected vals */
60890209230bSgjelinek 			if (strcmp(rctl.zone_rctlval_priv,
60900209230bSgjelinek 			    aliases[i].priv) != 0 ||
60910209230bSgjelinek 			    strcmp(rctl.zone_rctlval_action,
60920209230bSgjelinek 			    aliases[i].action) != 0)
60930209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
60940209230bSgjelinek 		}
6095cf8f45c7Sdstaff 	}
6096cf8f45c7Sdstaff 
60970209230bSgjelinek 	if (found) {
60980209230bSgjelinek 		*rval = strtoull(rctl.zone_rctlval_limit, NULL, 10);
60990209230bSgjelinek 		return (Z_OK);
6100cf8f45c7Sdstaff 	}
6101cf8f45c7Sdstaff 
61020209230bSgjelinek 	return (Z_NO_ENTRY);
6103cf8f45c7Sdstaff }
6104cf8f45c7Sdstaff 
61050209230bSgjelinek int
61060209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name)
6107cf8f45c7Sdstaff {
61080209230bSgjelinek 	int i;
61090209230bSgjelinek 	uint64_t val;
61100209230bSgjelinek 	struct zone_rctltab rctltab;
6111cf8f45c7Sdstaff 
6112cf8f45c7Sdstaff 	/*
61130209230bSgjelinek 	 * First check that we have a valid aliased rctl to remove.
61140209230bSgjelinek 	 * This will catch an rctl entry with non-standard values or
61150209230bSgjelinek 	 * multiple rctl values for this name.  We need to ignore those
61160209230bSgjelinek 	 * rctl entries.
6117cf8f45c7Sdstaff 	 */
61180209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK)
61190209230bSgjelinek 		return (Z_OK);
61200209230bSgjelinek 
61210209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
61220209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
61230209230bSgjelinek 			break;
61240209230bSgjelinek 
61250209230bSgjelinek 	if (aliases[i].shortname == NULL)
61260209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
61270209230bSgjelinek 
61280209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
61290209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
61300209230bSgjelinek 
61310209230bSgjelinek 	return (zonecfg_delete_rctl(handle, &rctltab));
61320209230bSgjelinek }
61330209230bSgjelinek 
61340209230bSgjelinek boolean_t
61350209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name)
61360209230bSgjelinek {
61370209230bSgjelinek 	uint64_t tmp_val;
61380209230bSgjelinek 
61390209230bSgjelinek 	switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) {
61400209230bSgjelinek 	case Z_OK:
61410209230bSgjelinek 		/*FALLTHRU*/
61420209230bSgjelinek 	case Z_NO_ENTRY:
61430209230bSgjelinek 		return (B_TRUE);
61440209230bSgjelinek 	default:
61450209230bSgjelinek 		return (B_FALSE);
61460209230bSgjelinek 	}
6147cf8f45c7Sdstaff }
6148cf8f45c7Sdstaff 
61490209230bSgjelinek int
61500209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val)
6151cf8f45c7Sdstaff {
61520209230bSgjelinek 	int i;
61530209230bSgjelinek 	int err;
61540209230bSgjelinek 	struct zone_rctltab rctltab;
61550209230bSgjelinek 	struct zone_rctlvaltab *rctlvaltab;
61560209230bSgjelinek 	char buf[128];
6157cf8f45c7Sdstaff 
61580209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, name))
61590209230bSgjelinek 		return (Z_ALIAS_DISALLOW);
6160cf8f45c7Sdstaff 
61610209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
61620209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
61630209230bSgjelinek 			break;
6164cf8f45c7Sdstaff 
61650209230bSgjelinek 	if (aliases[i].shortname == NULL)
61660209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
6167cf8f45c7Sdstaff 
61680209230bSgjelinek 	/* remove any pre-existing definition for this rctl */
61690209230bSgjelinek 	(void) zonecfg_rm_aliased_rctl(handle, name);
6170cf8f45c7Sdstaff 
61710209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
61720209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
6173cf8f45c7Sdstaff 
61740209230bSgjelinek 	rctltab.zone_rctl_valptr = NULL;
6175cf8f45c7Sdstaff 
61760209230bSgjelinek 	if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL)
61770209230bSgjelinek 		return (Z_NOMEM);
6178cf8f45c7Sdstaff 
61790209230bSgjelinek 	(void) snprintf(buf, sizeof (buf), "%llu", (long long)val);
6180cf8f45c7Sdstaff 
61810209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv,
61820209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_priv));
61830209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_limit, buf,
61840209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_limit));
61850209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action,
61860209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_action));
6187cf8f45c7Sdstaff 
61880209230bSgjelinek 	rctlvaltab->zone_rctlval_next = NULL;
6189cf8f45c7Sdstaff 
61900209230bSgjelinek 	if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK)
61910209230bSgjelinek 		return (err);
6192cf8f45c7Sdstaff 
61930209230bSgjelinek 	return (zonecfg_add_rctl(handle, &rctltab));
6194cf8f45c7Sdstaff }
6195cf8f45c7Sdstaff 
61960209230bSgjelinek static int
61970209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle)
6198cf8f45c7Sdstaff {
61990209230bSgjelinek 	int err;
62000209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
6201cf8f45c7Sdstaff 
62020209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
62030209230bSgjelinek 		return (err);
6204cf8f45c7Sdstaff 
62050209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
62060209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
62070209230bSgjelinek 			xmlUnlinkNode(cur);
62080209230bSgjelinek 			xmlFreeNode(cur);
62090209230bSgjelinek 			return (Z_OK);
62100209230bSgjelinek 		}
62110209230bSgjelinek 	}
6212cf8f45c7Sdstaff 
62130209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
62140209230bSgjelinek }
6215cf8f45c7Sdstaff 
62160209230bSgjelinek static int
62170209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance)
62180209230bSgjelinek {
62190209230bSgjelinek 	int err;
62200209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
62210209230bSgjelinek 	xmlNodePtr newnode;
6222cf8f45c7Sdstaff 
62230209230bSgjelinek 	err = delete_tmp_pool(handle);
62240209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
62250209230bSgjelinek 		return (err);
62260209230bSgjelinek 
62270209230bSgjelinek 	if (*pool_importance != '\0') {
62280209230bSgjelinek 		if ((err = operation_prep(handle)) != Z_OK)
62290209230bSgjelinek 			return (err);
62300209230bSgjelinek 
62310209230bSgjelinek 		newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL);
62320209230bSgjelinek 		if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE,
62330209230bSgjelinek 		    pool_importance)) != Z_OK)
62340209230bSgjelinek 			return (err);
62350209230bSgjelinek 	}
62360209230bSgjelinek 
62370209230bSgjelinek 	return (Z_OK);
6238cf8f45c7Sdstaff }
6239cf8f45c7Sdstaff 
6240fa9e4066Sahrens static int
62410209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr)
6242fa9e4066Sahrens {
6243fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
6244fa9e4066Sahrens 	int err;
6245fa9e4066Sahrens 
62460209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL);
62470209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN,
62480209230bSgjelinek 	    tabptr->zone_ncpu_min)) != Z_OK)
62490209230bSgjelinek 		return (err);
62500209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX,
62510209230bSgjelinek 	    tabptr->zone_ncpu_max)) != Z_OK)
62520209230bSgjelinek 		return (err);
62530209230bSgjelinek 
62540209230bSgjelinek 	if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK)
6255fa9e4066Sahrens 		return (err);
62560209230bSgjelinek 
6257fa9e4066Sahrens 	return (Z_OK);
6258fa9e4066Sahrens }
6259fa9e4066Sahrens 
6260fa9e4066Sahrens int
62610209230bSgjelinek zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6262fa9e4066Sahrens {
6263fa9e4066Sahrens 	int err;
6264fa9e4066Sahrens 
6265fa9e4066Sahrens 	if (tabptr == NULL)
6266fa9e4066Sahrens 		return (Z_INVAL);
6267fa9e4066Sahrens 
6268fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6269fa9e4066Sahrens 		return (err);
6270fa9e4066Sahrens 
62710209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
6272fa9e4066Sahrens 		return (err);
6273fa9e4066Sahrens 
6274fa9e4066Sahrens 	return (Z_OK);
6275fa9e4066Sahrens }
6276fa9e4066Sahrens 
62770209230bSgjelinek int
62780209230bSgjelinek zonecfg_delete_pset(zone_dochandle_t handle)
6279fa9e4066Sahrens {
62800209230bSgjelinek 	int err;
62810209230bSgjelinek 	int res = Z_NO_RESOURCE_ID;
6282fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
6283fa9e4066Sahrens 
62840209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
62850209230bSgjelinek 		return (err);
6286fa9e4066Sahrens 
62870209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
62880209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
6289fa9e4066Sahrens 			xmlUnlinkNode(cur);
6290fa9e4066Sahrens 			xmlFreeNode(cur);
62910209230bSgjelinek 			res = Z_OK;
62920209230bSgjelinek 			break;
6293fa9e4066Sahrens 		}
6294fa9e4066Sahrens 	}
62950209230bSgjelinek 
62960209230bSgjelinek 	/*
62970209230bSgjelinek 	 * Once we have msets, we should check that a mset
62980209230bSgjelinek 	 * do not exist before we delete the tmp_pool data.
62990209230bSgjelinek 	 */
63000209230bSgjelinek 	err = delete_tmp_pool(handle);
63010209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
63020209230bSgjelinek 		return (err);
63030209230bSgjelinek 
63040209230bSgjelinek 	return (res);
6305fa9e4066Sahrens }
6306fa9e4066Sahrens 
6307fa9e4066Sahrens int
63080209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6309fa9e4066Sahrens {
6310fa9e4066Sahrens 	int err;
6311fa9e4066Sahrens 
6312fa9e4066Sahrens 	if (tabptr == NULL)
6313fa9e4066Sahrens 		return (Z_INVAL);
6314fa9e4066Sahrens 
63150209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
6316fa9e4066Sahrens 		return (err);
6317fa9e4066Sahrens 
63180209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
6319fa9e4066Sahrens 		return (err);
6320fa9e4066Sahrens 
6321fa9e4066Sahrens 	return (Z_OK);
6322fa9e4066Sahrens }
6323fa9e4066Sahrens 
6324fa9e4066Sahrens int
63250209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6326fa9e4066Sahrens {
63270209230bSgjelinek 	xmlNodePtr cur;
6328fa9e4066Sahrens 	int err;
63290209230bSgjelinek 	int res = Z_NO_ENTRY;
6330fa9e4066Sahrens 
63310209230bSgjelinek 	if (tabptr == NULL)
6332fa9e4066Sahrens 		return (Z_INVAL);
6333fa9e4066Sahrens 
6334fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6335fa9e4066Sahrens 		return (err);
6336fa9e4066Sahrens 
63370209230bSgjelinek 	/* this is an optional component */
63380209230bSgjelinek 	tabptr->zone_importance[0] = '\0';
63390209230bSgjelinek 
63400209230bSgjelinek 	cur = handle->zone_dh_cur;
63410209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
63420209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
63430209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN,
63440209230bSgjelinek 			    tabptr->zone_ncpu_min,
63450209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_min))) != Z_OK) {
63460209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
63470209230bSgjelinek 				return (err);
63480209230bSgjelinek 			}
63490209230bSgjelinek 
63500209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX,
63510209230bSgjelinek 			    tabptr->zone_ncpu_max,
63520209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_max))) != Z_OK) {
63530209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
63540209230bSgjelinek 				return (err);
63550209230bSgjelinek 			}
63560209230bSgjelinek 
63570209230bSgjelinek 			res = Z_OK;
63580209230bSgjelinek 
63590209230bSgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
63600209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE,
63610209230bSgjelinek 			    tabptr->zone_importance,
63620209230bSgjelinek 			    sizeof (tabptr->zone_importance))) != Z_OK) {
63630209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
63640209230bSgjelinek 				return (err);
63650209230bSgjelinek 			}
63660209230bSgjelinek 		}
63670209230bSgjelinek 	}
63680209230bSgjelinek 
63690209230bSgjelinek 	return (res);
63700209230bSgjelinek }
63710209230bSgjelinek 
63720209230bSgjelinek int
63730209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr)
63740209230bSgjelinek {
63750209230bSgjelinek 	int err;
63760209230bSgjelinek 
63770209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
6378fa9e4066Sahrens 		return (err);
6379fa9e4066Sahrens 
63800209230bSgjelinek 	err = zonecfg_lookup_pset(handle, tabptr);
63810209230bSgjelinek 
63820209230bSgjelinek 	(void) zonecfg_endent(handle);
63830209230bSgjelinek 
63840209230bSgjelinek 	return (err);
63850209230bSgjelinek }
63860209230bSgjelinek 
63870209230bSgjelinek static int
63880209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
63890209230bSgjelinek {
63900209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
63910209230bSgjelinek 	int err;
63920209230bSgjelinek 
63930209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL);
63940209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap))
63950209230bSgjelinek 	    != Z_OK)
6396fa9e4066Sahrens 		return (err);
6397fa9e4066Sahrens 
6398fa9e4066Sahrens 	return (Z_OK);
6399fa9e4066Sahrens }
6400fa9e4066Sahrens 
6401fa9e4066Sahrens int
64020209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle)
6403fa9e4066Sahrens {
6404fa9e4066Sahrens 	int err;
64050209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
6406fa9e4066Sahrens 
6407fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6408fa9e4066Sahrens 		return (err);
6409fa9e4066Sahrens 
6410fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64110209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
6412fa9e4066Sahrens 			continue;
64130209230bSgjelinek 
64140209230bSgjelinek 		xmlUnlinkNode(cur);
64150209230bSgjelinek 		xmlFreeNode(cur);
64160209230bSgjelinek 		return (Z_OK);
6417fa9e4066Sahrens 	}
64180209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
64190209230bSgjelinek }
6420fa9e4066Sahrens 
64210209230bSgjelinek int
64220209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
64230209230bSgjelinek {
64240209230bSgjelinek 	int err;
6425fa9e4066Sahrens 
64260209230bSgjelinek 	if (tabptr == NULL)
64270209230bSgjelinek 		return (Z_INVAL);
64280209230bSgjelinek 
64290209230bSgjelinek 	err = zonecfg_delete_mcap(handle);
64300209230bSgjelinek 	/* it is ok if there is no mcap entry */
64310209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
64320209230bSgjelinek 		return (err);
64330209230bSgjelinek 
64340209230bSgjelinek 	if ((err = add_mcap(handle, tabptr)) != Z_OK)
6435fa9e4066Sahrens 		return (err);
6436fa9e4066Sahrens 
6437fa9e4066Sahrens 	return (Z_OK);
6438fa9e4066Sahrens }
6439fa9e4066Sahrens 
6440fa9e4066Sahrens int
64410209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6442fa9e4066Sahrens {
64430209230bSgjelinek 	xmlNodePtr cur;
64440209230bSgjelinek 	int err;
64450209230bSgjelinek 
64460209230bSgjelinek 	if (tabptr == NULL)
64470209230bSgjelinek 		return (Z_INVAL);
64480209230bSgjelinek 
64490209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64500209230bSgjelinek 		return (err);
64510209230bSgjelinek 
64520209230bSgjelinek 	cur = handle->zone_dh_cur;
64530209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64540209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
64550209230bSgjelinek 			continue;
64560209230bSgjelinek 		if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP,
64570209230bSgjelinek 		    tabptr->zone_physmem_cap,
64580209230bSgjelinek 		    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
64590209230bSgjelinek 			handle->zone_dh_cur = handle->zone_dh_top;
64600209230bSgjelinek 			return (err);
64610209230bSgjelinek 		}
64620209230bSgjelinek 
64630209230bSgjelinek 		return (Z_OK);
64640209230bSgjelinek 	}
64650209230bSgjelinek 
64660209230bSgjelinek 	return (Z_NO_ENTRY);
6467fa9e4066Sahrens }
6468fa9e4066Sahrens 
64690209230bSgjelinek static int
64700209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6471fa9e4066Sahrens {
6472fa9e4066Sahrens 	xmlNodePtr cur;
6473fa9e4066Sahrens 	int err;
6474fa9e4066Sahrens 
6475fa9e4066Sahrens 	if (handle == NULL)
6476fa9e4066Sahrens 		return (Z_INVAL);
6477fa9e4066Sahrens 
6478fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
6479fa9e4066Sahrens 		return (Z_NO_ENTRY);
6480fa9e4066Sahrens 
6481fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
64820209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0)
6483fa9e4066Sahrens 			break;
6484fa9e4066Sahrens 	if (cur == NULL) {
6485fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6486fa9e4066Sahrens 		return (Z_NO_ENTRY);
6487fa9e4066Sahrens 	}
6488fa9e4066Sahrens 
64890209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap,
64900209230bSgjelinek 	    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
6491fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6492fa9e4066Sahrens 		return (err);
6493fa9e4066Sahrens 	}
6494fa9e4066Sahrens 
6495fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
6496fa9e4066Sahrens 	return (Z_OK);
6497fa9e4066Sahrens }
6498fa9e4066Sahrens 
6499fa9e4066Sahrens int
65000209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6501fa9e4066Sahrens {
65020209230bSgjelinek 	int err;
65030209230bSgjelinek 
65040209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
65050209230bSgjelinek 		return (err);
65060209230bSgjelinek 
65070209230bSgjelinek 	err = getmcapent_core(handle, tabptr);
65080209230bSgjelinek 
65090209230bSgjelinek 	(void) zonecfg_endent(handle);
65100209230bSgjelinek 
65110209230bSgjelinek 	return (err);
6512fa9e4066Sahrens }
6513ee519a1fSgjelinek 
6514*6cfd72c6Sgjelinek /*
6515*6cfd72c6Sgjelinek  * Get the full tree of pkg/patch metadata in a set of nested AVL trees.
6516*6cfd72c6Sgjelinek  * pkgs_avl is an AVL tree of pkgs.  Each pkg element contains a
6517*6cfd72c6Sgjelinek  * zpe_patches_avl member which holds an AVL tree of patches for that pkg.
6518*6cfd72c6Sgjelinek  * The patch elements have the same zpe_patches_avl member, each of which can
6519*6cfd72c6Sgjelinek  * hold an AVL tree of patches that are obsoleted by the patch.
6520*6cfd72c6Sgjelinek  *
6521*6cfd72c6Sgjelinek  * The zone xml data contains DTD_ELEM_PACKAGE elements, followed by
6522*6cfd72c6Sgjelinek  * DTD_ELEM_PATCH elements.  The DTD_ELEM_PATCH patch element applies to the
6523*6cfd72c6Sgjelinek  * DTD_ELEM_PACKAGE that precedes it.  The DTD_ELEM_PATCH element may have
6524*6cfd72c6Sgjelinek  * child DTD_ELEM_OBSOLETES nodes associated with it.  The DTD_ELEM_PACKAGE
6525*6cfd72c6Sgjelinek  * really should have had the DTD_ELEM_PATCH elements as children but it
6526*6cfd72c6Sgjelinek  * was not defined that way initially so we are stuck with the DTD definition
6527*6cfd72c6Sgjelinek  * now.  However, we can safely assume the ordering for compatibility.
6528*6cfd72c6Sgjelinek  */
6529ee519a1fSgjelinek int
6530*6cfd72c6Sgjelinek zonecfg_getpkgdata(zone_dochandle_t handle, uu_avl_pool_t *pkg_pool,
6531*6cfd72c6Sgjelinek     uu_avl_t *pkgs_avl)
6532ee519a1fSgjelinek {
6533ee519a1fSgjelinek 	xmlNodePtr cur;
6534*6cfd72c6Sgjelinek 	int res;
6535*6cfd72c6Sgjelinek 	zone_pkg_entry_t *pkg;
6536*6cfd72c6Sgjelinek 	char name[MAXNAMELEN];
6537*6cfd72c6Sgjelinek 	char version[ZONE_PKG_VERSMAX];
6538ee519a1fSgjelinek 
6539ee519a1fSgjelinek 	if (handle == NULL)
6540ee519a1fSgjelinek 		return (Z_INVAL);
6541ee519a1fSgjelinek 
6542*6cfd72c6Sgjelinek 	if ((res = zonecfg_setent(handle)) != Z_OK)
6543*6cfd72c6Sgjelinek 		return (res);
6544ee519a1fSgjelinek 
6545*6cfd72c6Sgjelinek 	if ((cur = handle->zone_dh_cur) == NULL) {
6546*6cfd72c6Sgjelinek 		res = Z_NO_ENTRY;
6547*6cfd72c6Sgjelinek 		goto done;
6548ee519a1fSgjelinek 	}
6549ee519a1fSgjelinek 
6550*6cfd72c6Sgjelinek 	for (; cur != NULL; cur = cur->next) {
6551*6cfd72c6Sgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PACKAGE) == 0) {
6552*6cfd72c6Sgjelinek 			uu_avl_index_t where;
6553ee519a1fSgjelinek 
6554*6cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_NAME, name,
6555*6cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
6556*6cfd72c6Sgjelinek 				goto done;
6557ee519a1fSgjelinek 
6558*6cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_VERSION, version,
6559*6cfd72c6Sgjelinek 			    sizeof (version))) != Z_OK)
6560*6cfd72c6Sgjelinek 				goto done;
6561ee519a1fSgjelinek 
6562*6cfd72c6Sgjelinek 			if ((pkg = (zone_pkg_entry_t *)
6563*6cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
6564*6cfd72c6Sgjelinek 				res = Z_NOMEM;
6565*6cfd72c6Sgjelinek 				goto done;
6566*6cfd72c6Sgjelinek 			}
6567ee519a1fSgjelinek 
6568*6cfd72c6Sgjelinek 			if ((pkg->zpe_name = strdup(name)) == NULL) {
6569*6cfd72c6Sgjelinek 				free(pkg);
6570*6cfd72c6Sgjelinek 				res = Z_NOMEM;
6571*6cfd72c6Sgjelinek 				goto done;
6572*6cfd72c6Sgjelinek 			}
6573ee519a1fSgjelinek 
6574*6cfd72c6Sgjelinek 			if ((pkg->zpe_vers = strdup(version)) == NULL) {
6575*6cfd72c6Sgjelinek 				free(pkg->zpe_name);
6576*6cfd72c6Sgjelinek 				free(pkg);
6577*6cfd72c6Sgjelinek 				res = Z_NOMEM;
6578*6cfd72c6Sgjelinek 				goto done;
6579*6cfd72c6Sgjelinek 			}
6580ee519a1fSgjelinek 
6581*6cfd72c6Sgjelinek 			pkg->zpe_patches_avl = NULL;
6582ee519a1fSgjelinek 
6583*6cfd72c6Sgjelinek 			uu_avl_node_init(pkg, &pkg->zpe_entry, pkg_pool);
6584*6cfd72c6Sgjelinek 			if (uu_avl_find(pkgs_avl, pkg, NULL, &where) != NULL) {
6585*6cfd72c6Sgjelinek 				free(pkg->zpe_name);
6586*6cfd72c6Sgjelinek 				free(pkg->zpe_vers);
6587*6cfd72c6Sgjelinek 				free(pkg);
6588*6cfd72c6Sgjelinek 			} else {
6589*6cfd72c6Sgjelinek 				uu_avl_insert(pkgs_avl, pkg, where);
6590*6cfd72c6Sgjelinek 			}
6591ee519a1fSgjelinek 
6592*6cfd72c6Sgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
6593*6cfd72c6Sgjelinek 			zone_pkg_entry_t *patch;
6594*6cfd72c6Sgjelinek 			uu_avl_index_t where;
6595*6cfd72c6Sgjelinek 			char *p;
6596*6cfd72c6Sgjelinek 			char *dashp = NULL;
6597*6cfd72c6Sgjelinek 			xmlNodePtr child;
6598ee519a1fSgjelinek 
6599*6cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_ID, name,
6600*6cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
6601*6cfd72c6Sgjelinek 				goto done;
6602ee519a1fSgjelinek 
6603*6cfd72c6Sgjelinek 			if ((patch = (zone_pkg_entry_t *)
6604*6cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
6605*6cfd72c6Sgjelinek 				res = Z_NOMEM;
6606*6cfd72c6Sgjelinek 				goto done;
6607*6cfd72c6Sgjelinek 			}
6608ee519a1fSgjelinek 
6609*6cfd72c6Sgjelinek 			if ((p = strchr(name, '-')) != NULL) {
6610*6cfd72c6Sgjelinek 				dashp = p;
6611*6cfd72c6Sgjelinek 				*p++ = '\0';
6612*6cfd72c6Sgjelinek 			} else {
6613*6cfd72c6Sgjelinek 				p = "";
6614*6cfd72c6Sgjelinek 			}
6615*6cfd72c6Sgjelinek 
6616*6cfd72c6Sgjelinek 			if ((patch->zpe_name = strdup(name)) == NULL) {
6617*6cfd72c6Sgjelinek 				free(patch);
6618*6cfd72c6Sgjelinek 				res = Z_NOMEM;
6619*6cfd72c6Sgjelinek 				goto done;
6620*6cfd72c6Sgjelinek 			}
6621*6cfd72c6Sgjelinek 
6622*6cfd72c6Sgjelinek 			if ((patch->zpe_vers = strdup(p)) == NULL) {
6623*6cfd72c6Sgjelinek 				free(patch->zpe_name);
6624*6cfd72c6Sgjelinek 				free(patch);
6625*6cfd72c6Sgjelinek 				res = Z_NOMEM;
6626*6cfd72c6Sgjelinek 				goto done;
6627*6cfd72c6Sgjelinek 			}
6628*6cfd72c6Sgjelinek 
6629*6cfd72c6Sgjelinek 			if (dashp != NULL)
6630*6cfd72c6Sgjelinek 				*dashp = '-';
6631*6cfd72c6Sgjelinek 
6632*6cfd72c6Sgjelinek 			patch->zpe_patches_avl = NULL;
6633*6cfd72c6Sgjelinek 
6634*6cfd72c6Sgjelinek 			if (pkg->zpe_patches_avl == NULL) {
6635*6cfd72c6Sgjelinek 				pkg->zpe_patches_avl = uu_avl_create(pkg_pool,
6636*6cfd72c6Sgjelinek 				    NULL, UU_DEFAULT);
6637*6cfd72c6Sgjelinek 				if (pkg->zpe_patches_avl == NULL) {
6638*6cfd72c6Sgjelinek 					free(patch->zpe_name);
6639*6cfd72c6Sgjelinek 					free(patch->zpe_vers);
6640*6cfd72c6Sgjelinek 					free(patch);
6641*6cfd72c6Sgjelinek 					res = Z_NOMEM;
6642*6cfd72c6Sgjelinek 					goto done;
6643*6cfd72c6Sgjelinek 				}
6644*6cfd72c6Sgjelinek 			}
6645*6cfd72c6Sgjelinek 
6646*6cfd72c6Sgjelinek 			uu_avl_node_init(patch, &patch->zpe_entry, pkg_pool);
6647*6cfd72c6Sgjelinek 			if (uu_avl_find(pkg->zpe_patches_avl, patch, NULL,
6648*6cfd72c6Sgjelinek 			    &where) != NULL) {
6649*6cfd72c6Sgjelinek 				free(patch->zpe_name);
6650*6cfd72c6Sgjelinek 				free(patch->zpe_vers);
6651*6cfd72c6Sgjelinek 				free(patch);
6652*6cfd72c6Sgjelinek 			} else {
6653*6cfd72c6Sgjelinek 				uu_avl_insert(pkg->zpe_patches_avl, patch,
6654*6cfd72c6Sgjelinek 				    where);
6655*6cfd72c6Sgjelinek 			}
6656*6cfd72c6Sgjelinek 
6657*6cfd72c6Sgjelinek 			/* Add any patches this patch obsoletes. */
6658*6cfd72c6Sgjelinek 			for (child = cur->xmlChildrenNode; child != NULL;
6659*6cfd72c6Sgjelinek 			    child = child->next) {
6660*6cfd72c6Sgjelinek 				zone_pkg_entry_t *obs;
6661*6cfd72c6Sgjelinek 
6662*6cfd72c6Sgjelinek 				if (xmlStrcmp(child->name, DTD_ELEM_OBSOLETES)
6663*6cfd72c6Sgjelinek 				    != 0)
6664*6cfd72c6Sgjelinek 					continue;
6665*6cfd72c6Sgjelinek 
6666*6cfd72c6Sgjelinek 				if ((res = fetchprop(child, DTD_ATTR_ID,
6667*6cfd72c6Sgjelinek 				    name, sizeof (name))) != Z_OK)
6668*6cfd72c6Sgjelinek 					goto done;
6669*6cfd72c6Sgjelinek 
6670*6cfd72c6Sgjelinek 				if ((obs = (zone_pkg_entry_t *)malloc(
6671*6cfd72c6Sgjelinek 				    sizeof (zone_pkg_entry_t))) == NULL) {
6672*6cfd72c6Sgjelinek 					res = Z_NOMEM;
6673*6cfd72c6Sgjelinek 					goto done;
6674*6cfd72c6Sgjelinek 				}
6675*6cfd72c6Sgjelinek 
6676*6cfd72c6Sgjelinek 				if ((obs->zpe_name = strdup(name)) == NULL) {
6677*6cfd72c6Sgjelinek 					free(obs);
6678*6cfd72c6Sgjelinek 					res = Z_NOMEM;
6679*6cfd72c6Sgjelinek 					goto done;
6680*6cfd72c6Sgjelinek 				}
6681*6cfd72c6Sgjelinek 				/*
6682*6cfd72c6Sgjelinek 				 * The version doesn't matter for obsoleted
6683*6cfd72c6Sgjelinek 				 * patches.
6684*6cfd72c6Sgjelinek 				 */
6685*6cfd72c6Sgjelinek 				obs->zpe_vers = NULL;
6686*6cfd72c6Sgjelinek 				obs->zpe_patches_avl = NULL;
6687*6cfd72c6Sgjelinek 
6688*6cfd72c6Sgjelinek 				/*
6689*6cfd72c6Sgjelinek 				 * If this is the first obsolete patch, add an
6690*6cfd72c6Sgjelinek 				 * AVL tree to the parent patch element.
6691*6cfd72c6Sgjelinek 				 */
6692*6cfd72c6Sgjelinek 				if (patch->zpe_patches_avl == NULL) {
6693*6cfd72c6Sgjelinek 					patch->zpe_patches_avl =
6694*6cfd72c6Sgjelinek 					    uu_avl_create(pkg_pool, NULL,
6695*6cfd72c6Sgjelinek 					    UU_DEFAULT);
6696*6cfd72c6Sgjelinek 					if (patch->zpe_patches_avl == NULL) {
6697*6cfd72c6Sgjelinek 						free(obs->zpe_name);
6698*6cfd72c6Sgjelinek 						free(obs);
6699*6cfd72c6Sgjelinek 						res = Z_NOMEM;
6700*6cfd72c6Sgjelinek 						goto done;
6701*6cfd72c6Sgjelinek 					}
6702*6cfd72c6Sgjelinek 				}
6703*6cfd72c6Sgjelinek 
6704*6cfd72c6Sgjelinek 				/* Insert obsolete patch into the AVL tree. */
6705*6cfd72c6Sgjelinek 				uu_avl_node_init(obs, &obs->zpe_entry,
6706*6cfd72c6Sgjelinek 				    pkg_pool);
6707*6cfd72c6Sgjelinek 				if (uu_avl_find(patch->zpe_patches_avl, obs,
6708*6cfd72c6Sgjelinek 				    NULL, &where) != NULL) {
6709*6cfd72c6Sgjelinek 					free(obs->zpe_name);
6710*6cfd72c6Sgjelinek 					free(obs);
6711*6cfd72c6Sgjelinek 				} else {
6712*6cfd72c6Sgjelinek 					uu_avl_insert(patch->zpe_patches_avl,
6713*6cfd72c6Sgjelinek 					    obs, where);
6714*6cfd72c6Sgjelinek 				}
6715*6cfd72c6Sgjelinek 			}
6716*6cfd72c6Sgjelinek 		}
6717*6cfd72c6Sgjelinek 	}
6718*6cfd72c6Sgjelinek 
6719*6cfd72c6Sgjelinek done:
6720*6cfd72c6Sgjelinek 	(void) zonecfg_endent(handle);
6721*6cfd72c6Sgjelinek 	return (res);
6722ee519a1fSgjelinek }
6723ee519a1fSgjelinek 
6724ee519a1fSgjelinek int
6725ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
6726ee519a1fSgjelinek {
6727ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6728ee519a1fSgjelinek }
6729ee519a1fSgjelinek 
6730ee519a1fSgjelinek int
6731ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
6732ee519a1fSgjelinek {
6733ee519a1fSgjelinek 	xmlNodePtr cur;
6734ee519a1fSgjelinek 	int err;
6735ee519a1fSgjelinek 	char buf[128];
6736ee519a1fSgjelinek 
6737ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
6738ee519a1fSgjelinek 
6739ee519a1fSgjelinek 	if (handle == NULL)
6740ee519a1fSgjelinek 		return (Z_INVAL);
6741ee519a1fSgjelinek 
6742ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6743ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6744ee519a1fSgjelinek 
6745ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6746ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
6747ee519a1fSgjelinek 			break;
6748ee519a1fSgjelinek 	if (cur == NULL) {
6749ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6750ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6751ee519a1fSgjelinek 	}
6752ee519a1fSgjelinek 
6753ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
6754ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
6755ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6756ee519a1fSgjelinek 		return (err);
6757ee519a1fSgjelinek 	}
6758ee519a1fSgjelinek 
6759ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
6760ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6761ee519a1fSgjelinek 		return (err);
6762ee519a1fSgjelinek 	}
6763ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
6764ee519a1fSgjelinek 
6765ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
6766ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6767ee519a1fSgjelinek 		return (err);
6768ee519a1fSgjelinek 	}
6769ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
6770ee519a1fSgjelinek 
6771ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
6772ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6773ee519a1fSgjelinek 		return (err);
6774ee519a1fSgjelinek 	}
6775ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
6776ee519a1fSgjelinek 
6777ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
6778ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
6779ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6780ee519a1fSgjelinek 		return (err);
6781ee519a1fSgjelinek 	}
6782ee519a1fSgjelinek 
6783ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6784ee519a1fSgjelinek 	return (Z_OK);
6785ee519a1fSgjelinek }
6786ee519a1fSgjelinek 
6787ee519a1fSgjelinek int
6788ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
6789ee519a1fSgjelinek {
6790ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6791ee519a1fSgjelinek }
6792ee519a1fSgjelinek 
679339935be5Sgjelinek /*
679439935be5Sgjelinek  * Maintain a space separated list of unique pkg names.  PATH_MAX is used in
679539935be5Sgjelinek  * the pkg code as the maximum size for a pkg name.
679639935be5Sgjelinek  */
679739935be5Sgjelinek static int
679839935be5Sgjelinek add_pkg_to_str(char **str, char *pkg)
679939935be5Sgjelinek {
680039935be5Sgjelinek 	int len, newlen;
680139935be5Sgjelinek 	char tstr[PATH_MAX + 3];
680239935be5Sgjelinek 	char *tmp;
680339935be5Sgjelinek 
680439935be5Sgjelinek 	len = strlen(pkg);
680539935be5Sgjelinek 	if (*str == NULL) {
680639935be5Sgjelinek 		/* space for str + 2 spaces + NULL */
680739935be5Sgjelinek 		if ((*str = (char *)malloc(len + 3)) == NULL)
680839935be5Sgjelinek 			return (Z_NOMEM);
680939935be5Sgjelinek 		(void) snprintf(*str, len + 3, " %s ", pkg);
681039935be5Sgjelinek 		return (Z_OK);
681139935be5Sgjelinek 	}
681239935be5Sgjelinek 
681339935be5Sgjelinek 	(void) snprintf(tstr, sizeof (tstr), " %s ", pkg);
681439935be5Sgjelinek 	if (strstr(*str, tstr) != NULL)
681539935be5Sgjelinek 		return (Z_OK);
681639935be5Sgjelinek 
681739935be5Sgjelinek 	/* space for str + 1 space + NULL */
681839935be5Sgjelinek 	newlen = strlen(*str) + len + 2;
681939935be5Sgjelinek 	if ((tmp = (char *)realloc(*str, newlen)) == NULL)
682039935be5Sgjelinek 		return (Z_NOMEM);
682139935be5Sgjelinek 	*str = tmp;
682239935be5Sgjelinek 	(void) strlcat(*str, pkg, newlen);
682339935be5Sgjelinek 	(void) strlcat(*str, " ", newlen);
682439935be5Sgjelinek 	return (Z_OK);
682539935be5Sgjelinek }
682639935be5Sgjelinek 
6827ee519a1fSgjelinek /*
6828ee519a1fSgjelinek  * Process a list of pkgs from an entry in the contents file, adding each pkg
6829ee519a1fSgjelinek  * name to the list of pkgs.
6830ee519a1fSgjelinek  *
6831ee519a1fSgjelinek  * It is possible for the pkg name to be preceeded by a special character
6832ee519a1fSgjelinek  * which indicates some bookkeeping information for pkging.  Check if the
6833ee519a1fSgjelinek  * first char is not an Alpha char.  If so, skip over it.
6834ee519a1fSgjelinek  */
6835ee519a1fSgjelinek static int
683639935be5Sgjelinek add_pkg_list(char *lastp, char ***plist, int *pcnt, char **pkg_warn)
6837ee519a1fSgjelinek {
6838ee519a1fSgjelinek 	char	*p;
6839ee519a1fSgjelinek 	int	pkg_cnt = *pcnt;
6840ee519a1fSgjelinek 	char	**pkgs = *plist;
6841ee519a1fSgjelinek 	int	res = Z_OK;
6842ee519a1fSgjelinek 
6843ee519a1fSgjelinek 	while ((p = strtok_r(NULL, " ", &lastp)) != NULL) {
6844ee519a1fSgjelinek 		char	**tmpp;
6845ee519a1fSgjelinek 		int	i;
6846ee519a1fSgjelinek 
6847ee519a1fSgjelinek 		/* skip over any special pkg bookkeeping char */
684839935be5Sgjelinek 		if (!isalpha(*p)) {
6849ee519a1fSgjelinek 			p++;
685039935be5Sgjelinek 			if ((res = add_pkg_to_str(pkg_warn, p)) != Z_OK)
685139935be5Sgjelinek 				break;
685239935be5Sgjelinek 		}
6853ee519a1fSgjelinek 
6854ee519a1fSgjelinek 		/* Check if the pkg is already in the list */
6855ee519a1fSgjelinek 		for (i = 0; i < pkg_cnt; i++) {
6856ee519a1fSgjelinek 			if (strcmp(p, pkgs[i]) == 0)
6857ee519a1fSgjelinek 				break;
6858ee519a1fSgjelinek 		}
6859ee519a1fSgjelinek 
6860ee519a1fSgjelinek 		if (i < pkg_cnt)
6861ee519a1fSgjelinek 			continue;
6862ee519a1fSgjelinek 
6863ee519a1fSgjelinek 		/* The pkg is not in the list; add it. */
6864ee519a1fSgjelinek 		if ((tmpp = (char **)realloc(pkgs,
6865ee519a1fSgjelinek 		    sizeof (char *) * (pkg_cnt + 1))) == NULL) {
6866ee519a1fSgjelinek 			res = Z_NOMEM;
6867ee519a1fSgjelinek 			break;
6868ee519a1fSgjelinek 		}
6869ee519a1fSgjelinek 		pkgs = tmpp;
6870ee519a1fSgjelinek 
6871ee519a1fSgjelinek 		if ((pkgs[pkg_cnt] = strdup(p)) == NULL) {
6872ee519a1fSgjelinek 			res = Z_NOMEM;
6873ee519a1fSgjelinek 			break;
6874ee519a1fSgjelinek 		}
6875ee519a1fSgjelinek 		pkg_cnt++;
6876ee519a1fSgjelinek 	}
6877ee519a1fSgjelinek 
6878ee519a1fSgjelinek 	*plist = pkgs;
6879ee519a1fSgjelinek 	*pcnt = pkg_cnt;
6880ee519a1fSgjelinek 
6881ee519a1fSgjelinek 	return (res);
6882ee519a1fSgjelinek }
6883ee519a1fSgjelinek 
6884ee519a1fSgjelinek /*
688539935be5Sgjelinek  * Process an entry from the contents file (type "directory").  If the
688639935be5Sgjelinek  * directory path is in the list of ipds and is not under a lofs mount within
688739935be5Sgjelinek  * the ipd then add the associated list of pkgs to the pkg list.  The input
688839935be5Sgjelinek  * parameter "entry" will be broken up by the parser within this function so
688939935be5Sgjelinek  * its value will be modified when this function exits.
6890ee519a1fSgjelinek  *
6891ee519a1fSgjelinek  * The entries we are looking for will look something like:
6892ee519a1fSgjelinek  *	/usr d none 0755 root sys SUNWctpls SUNWidnl SUNWlibCf ....
6893ee519a1fSgjelinek  */
6894ee519a1fSgjelinek static int
689539935be5Sgjelinek get_path_pkgs(char *entry, char **ipds, char **fss, char ***pkgs, int *pkg_cnt,
689639935be5Sgjelinek     char **pkg_warn)
6897ee519a1fSgjelinek {
6898ee519a1fSgjelinek 	char	*f1;
6899ee519a1fSgjelinek 	char	*f2;
6900ee519a1fSgjelinek 	char	*lastp;
6901ee519a1fSgjelinek 	int	i;
690239935be5Sgjelinek 	char	*nlp;
6903ee519a1fSgjelinek 
6904ee519a1fSgjelinek 	if ((f1 = strtok_r(entry, " ", &lastp)) == NULL ||
6905ee519a1fSgjelinek 	    (f2 = strtok_r(NULL, " ", &lastp)) == NULL || strcmp(f2, "d") != 0)
6906ee519a1fSgjelinek 		return (Z_OK);
6907ee519a1fSgjelinek 
690839935be5Sgjelinek 	/* Check if this directory entry is in the list of ipds. */
690939935be5Sgjelinek 	for (i = 0; ipds[i] != NULL; i++) {
691039935be5Sgjelinek 		char wildcard[MAXPATHLEN];
6911ee519a1fSgjelinek 
691239935be5Sgjelinek 		/*
691339935be5Sgjelinek 		 * We want to match on the path and any other directory
691439935be5Sgjelinek 		 * entries under this path.  When we use FNM_PATHNAME then
691539935be5Sgjelinek 		 * that means '/' will not be matched by a wildcard (*) so
691639935be5Sgjelinek 		 * we omit FNM_PATHNAME on the call with the wildcard matching.
691739935be5Sgjelinek 		 */
691839935be5Sgjelinek 		(void) snprintf(wildcard, sizeof (wildcard), "%s/*", ipds[i]);
691939935be5Sgjelinek 		if (fnmatch(ipds[i], f1, FNM_PATHNAME) == 0 ||
692039935be5Sgjelinek 		    fnmatch(wildcard, f1, 0) == 0) {
692139935be5Sgjelinek 			/* It looks like we do want the pkgs for this path. */
692239935be5Sgjelinek 			break;
692339935be5Sgjelinek 		}
692439935be5Sgjelinek 	}
6925ee519a1fSgjelinek 
692639935be5Sgjelinek 	/* This entry did not match any of the ipds. */
692739935be5Sgjelinek 	if (ipds[i] == NULL)
692839935be5Sgjelinek 		return (Z_OK);
692907b574eeSgjelinek 
693039935be5Sgjelinek 	/*
693139935be5Sgjelinek 	 * Check if there is a fs mounted under the ipd.  If so, ignore this
693239935be5Sgjelinek 	 * entry.
693339935be5Sgjelinek 	 */
693439935be5Sgjelinek 	for (i = 0; fss[i] != NULL; i++) {
693539935be5Sgjelinek 		char wildcard[MAXPATHLEN];
693639935be5Sgjelinek 
693739935be5Sgjelinek 		(void) snprintf(wildcard, sizeof (wildcard), "%s/*", fss[i]);
693839935be5Sgjelinek 		if (fnmatch(fss[i], f1, FNM_PATHNAME) == 0 ||
693939935be5Sgjelinek 		    fnmatch(wildcard, f1, 0) == 0) {
694039935be5Sgjelinek 			/* We should ignore this path. */
6941ee519a1fSgjelinek 			break;
6942ee519a1fSgjelinek 		}
6943ee519a1fSgjelinek 	}
6944ee519a1fSgjelinek 
694539935be5Sgjelinek 	/* If not null, then we matched an fs mount point so ignore entry. */
694639935be5Sgjelinek 	if (fss[i] != NULL)
694739935be5Sgjelinek 		return (Z_OK);
694839935be5Sgjelinek 
694939935be5Sgjelinek 	/*
695039935be5Sgjelinek 	 * We do want the pkgs for this entry.  First, skip over the next 4
695139935be5Sgjelinek 	 * fields in the entry so that we call add_pkg_list starting with the
695239935be5Sgjelinek 	 * pkg names.
695339935be5Sgjelinek 	 */
695439935be5Sgjelinek 	for (i = 0; i < 4 && strtok_r(NULL, " ", &lastp) != NULL; i++)
695539935be5Sgjelinek 		;
695639935be5Sgjelinek 	/* If there are < 4 fields this entry is corrupt, just skip it. */
695739935be5Sgjelinek 	if (i < 4)
695839935be5Sgjelinek 		return (Z_OK);
695939935be5Sgjelinek 
696039935be5Sgjelinek 	/* strip newline from the line */
696139935be5Sgjelinek 	nlp = (lastp + strlen(lastp) - 1);
696239935be5Sgjelinek 	if (*nlp == '\n')
696339935be5Sgjelinek 		*nlp = '\0';
696439935be5Sgjelinek 
696539935be5Sgjelinek 	return (add_pkg_list(lastp, pkgs, pkg_cnt, pkg_warn));
6966ee519a1fSgjelinek }
6967ee519a1fSgjelinek 
6968ee519a1fSgjelinek /*
6969ee519a1fSgjelinek  * Read an entry from a pkginfo or contents file.  Some of these lines can
6970ee519a1fSgjelinek  * either be arbitrarily long or be continued by a backslash at the end of
6971ee519a1fSgjelinek  * the line.  This function coalesces lines that are longer than the read
6972ee519a1fSgjelinek  * buffer, and lines that are continued, into one buffer which is returned.
6973ee519a1fSgjelinek  * The caller must free this memory.  NULL is returned when we hit EOF or
6974ee519a1fSgjelinek  * if we run out of memory (errno is set to ENOMEM).
6975ee519a1fSgjelinek  */
6976ee519a1fSgjelinek static char *
6977ee519a1fSgjelinek read_pkg_data(FILE *fp)
6978ee519a1fSgjelinek {
6979ee519a1fSgjelinek 	char *start;
6980ee519a1fSgjelinek 	char *inp;
6981ee519a1fSgjelinek 	char *p;
6982ee519a1fSgjelinek 	int char_cnt = 0;
6983ee519a1fSgjelinek 
6984ee519a1fSgjelinek 	errno = 0;
6985ee519a1fSgjelinek 	if ((start = (char *)malloc(PKGINFO_RD_LEN)) == NULL) {
6986ee519a1fSgjelinek 		errno = ENOMEM;
6987ee519a1fSgjelinek 		return (NULL);
6988ee519a1fSgjelinek 	}
6989ee519a1fSgjelinek 
6990ee519a1fSgjelinek 	inp = start;
6991ee519a1fSgjelinek 	while ((p = fgets(inp, PKGINFO_RD_LEN, fp)) != NULL) {
6992ee519a1fSgjelinek 		int len;
6993ee519a1fSgjelinek 
6994ee519a1fSgjelinek 		len = strlen(inp);
6995ee519a1fSgjelinek 		if (inp[len - 1] == '\n' &&
6996ee519a1fSgjelinek 		    (len == 1 || inp[len - 2] != '\\')) {
6997ee519a1fSgjelinek 			char_cnt = len;
6998ee519a1fSgjelinek 			break;
6999ee519a1fSgjelinek 		}
7000ee519a1fSgjelinek 
7001ee519a1fSgjelinek 		if (inp[len - 2] == '\\')
7002ee519a1fSgjelinek 			char_cnt += len - 2;
7003ee519a1fSgjelinek 		else
7004ee519a1fSgjelinek 			char_cnt += PKGINFO_RD_LEN - 1;
7005ee519a1fSgjelinek 
7006ee519a1fSgjelinek 		if ((p = realloc(start, char_cnt + PKGINFO_RD_LEN)) == NULL) {
7007ee519a1fSgjelinek 			errno = ENOMEM;
7008ee519a1fSgjelinek 			break;
7009ee519a1fSgjelinek 		}
7010ee519a1fSgjelinek 
7011ee519a1fSgjelinek 		start = p;
7012ee519a1fSgjelinek 		inp = start + char_cnt;
7013ee519a1fSgjelinek 	}
7014ee519a1fSgjelinek 
7015ee519a1fSgjelinek 	if (errno == ENOMEM || (p == NULL && char_cnt == 0)) {
7016ee519a1fSgjelinek 		free(start);
7017ee519a1fSgjelinek 		start = NULL;
7018ee519a1fSgjelinek 	}
7019ee519a1fSgjelinek 
7020ee519a1fSgjelinek 	return (start);
7021ee519a1fSgjelinek }
7022ee519a1fSgjelinek 
7023ee519a1fSgjelinek static void
7024ee519a1fSgjelinek free_ipd_pkgs(char **pkgs, int cnt)
7025ee519a1fSgjelinek {
7026ee519a1fSgjelinek 	int i;
7027ee519a1fSgjelinek 
7028ee519a1fSgjelinek 	for (i = 0; i < cnt; i++)
7029ee519a1fSgjelinek 		free(pkgs[i]);
7030ee519a1fSgjelinek 	free(pkgs);
7031ee519a1fSgjelinek }
7032ee519a1fSgjelinek 
7033ee519a1fSgjelinek /*
703439935be5Sgjelinek  * Get a list of the inherited pkg dirs or fs entries configured for the
703539935be5Sgjelinek  * zone.  The type parameter will be either ZONE_IPD or ZONE_FS.
7036ee519a1fSgjelinek  */
7037ee519a1fSgjelinek static int
703839935be5Sgjelinek get_ipd_fs_list(zone_dochandle_t handle, enum zn_ipd_fs type, char ***list)
7039ee519a1fSgjelinek {
7040ee519a1fSgjelinek 	int	res;
7041ee519a1fSgjelinek 	struct zone_fstab fstab;
704239935be5Sgjelinek 	int	cnt = 0;
704339935be5Sgjelinek 	char	**entries = NULL;
7044ee519a1fSgjelinek 	int	i;
704539935be5Sgjelinek 	int	(*fp)(zone_dochandle_t, struct zone_fstab *);
704639935be5Sgjelinek 
704739935be5Sgjelinek 	if (type == ZONE_IPD) {
704839935be5Sgjelinek 		fp = zonecfg_getipdent;
704939935be5Sgjelinek 		res = zonecfg_setipdent(handle);
705039935be5Sgjelinek 	} else {
705139935be5Sgjelinek 		fp = zonecfg_getfsent;
705239935be5Sgjelinek 		res = zonecfg_setfsent(handle);
705339935be5Sgjelinek 	}
7054ee519a1fSgjelinek 
705539935be5Sgjelinek 	if (res != Z_OK)
7056ee519a1fSgjelinek 		return (res);
7057ee519a1fSgjelinek 
705839935be5Sgjelinek 	while (fp(handle, &fstab) == Z_OK) {
7059ee519a1fSgjelinek 		char	**p;
7060ee519a1fSgjelinek 
706139935be5Sgjelinek 		if ((p = (char **)realloc(entries,
706239935be5Sgjelinek 		    sizeof (char *) * (cnt + 1))) == NULL) {
7063ee519a1fSgjelinek 			res = Z_NOMEM;
7064ee519a1fSgjelinek 			break;
7065ee519a1fSgjelinek 		}
706639935be5Sgjelinek 		entries = p;
7067ee519a1fSgjelinek 
706839935be5Sgjelinek 		if ((entries[cnt] = strdup(fstab.zone_fs_dir)) == NULL) {
7069ee519a1fSgjelinek 			res = Z_NOMEM;
7070ee519a1fSgjelinek 			break;
7071ee519a1fSgjelinek 		}
7072ee519a1fSgjelinek 
707339935be5Sgjelinek 		cnt++;
707439935be5Sgjelinek 	}
707539935be5Sgjelinek 
707639935be5Sgjelinek 	if (type == ZONE_IPD)
707739935be5Sgjelinek 		(void) zonecfg_endipdent(handle);
707839935be5Sgjelinek 	else
707939935be5Sgjelinek 		(void) zonecfg_endfsent(handle);
708039935be5Sgjelinek 
708139935be5Sgjelinek 	/* Add a NULL terminating element. */
708239935be5Sgjelinek 	if (res == Z_OK) {
708339935be5Sgjelinek 		char	**p;
708439935be5Sgjelinek 
708539935be5Sgjelinek 		if ((p = (char **)realloc(entries,
708639935be5Sgjelinek 		    sizeof (char *) * (cnt + 1))) == NULL) {
7087ee519a1fSgjelinek 			res = Z_NOMEM;
708839935be5Sgjelinek 		} else {
708939935be5Sgjelinek 			entries = p;
709039935be5Sgjelinek 			entries[cnt] = NULL;
7091ee519a1fSgjelinek 		}
709239935be5Sgjelinek 	}
7093ee519a1fSgjelinek 
709439935be5Sgjelinek 	if (res != Z_OK) {
709539935be5Sgjelinek 		if (entries != NULL) {
709639935be5Sgjelinek 			for (i = 0; i < cnt; i++)
709739935be5Sgjelinek 				free(entries[i]);
709839935be5Sgjelinek 			free(entries);
709939935be5Sgjelinek 		}
710039935be5Sgjelinek 		return (res);
7101ee519a1fSgjelinek 	}
7102ee519a1fSgjelinek 
710339935be5Sgjelinek 	*list = entries;
710439935be5Sgjelinek 	return (Z_OK);
710539935be5Sgjelinek }
7106ee519a1fSgjelinek 
710739935be5Sgjelinek /*
710839935be5Sgjelinek  * Get the list of inherited-pkg-dirs (ipd) for the zone and then get the
710939935be5Sgjelinek  * list of pkgs that deliver into those dirs.
711039935be5Sgjelinek  */
711139935be5Sgjelinek static int
711239935be5Sgjelinek get_ipd_pkgs(zone_dochandle_t handle, char ***pkg_list, int *cnt)
711339935be5Sgjelinek {
711439935be5Sgjelinek 	int	res;
711539935be5Sgjelinek 	char	**ipds;
711639935be5Sgjelinek 	char	**fss;
711739935be5Sgjelinek 	int	pkg_cnt = 0;
711839935be5Sgjelinek 	char	**pkgs = NULL;
711939935be5Sgjelinek 	int	i;
712039935be5Sgjelinek 
712139935be5Sgjelinek 	if ((res = get_ipd_fs_list(handle, ZONE_IPD, &ipds)) != Z_OK)
712239935be5Sgjelinek 		return (res);
712339935be5Sgjelinek 
712439935be5Sgjelinek 	if ((res = get_ipd_fs_list(handle, ZONE_FS, &fss)) != Z_OK) {
712539935be5Sgjelinek 		for (i = 0; ipds[i] != NULL; i++)
7126ee519a1fSgjelinek 			free(ipds[i]);
7127ee519a1fSgjelinek 		free(ipds);
7128ee519a1fSgjelinek 		return (res);
7129ee519a1fSgjelinek 	}
7130ee519a1fSgjelinek 
7131ee519a1fSgjelinek 	/* We only have to process the contents file if we have ipds. */
713239935be5Sgjelinek 	if (ipds != NULL) {
7133ee519a1fSgjelinek 		FILE	*fp;
7134ee519a1fSgjelinek 
7135ee519a1fSgjelinek 		if ((fp = fopen(CONTENTS_FILE, "r")) != NULL) {
7136ee519a1fSgjelinek 			char	*buf;
713739935be5Sgjelinek 			char	*pkg_warn = NULL;
7138ee519a1fSgjelinek 
7139ee519a1fSgjelinek 			while ((buf = read_pkg_data(fp)) != NULL) {
714039935be5Sgjelinek 				res = get_path_pkgs(buf, ipds, fss, &pkgs,
714139935be5Sgjelinek 				    &pkg_cnt, &pkg_warn);
7142ee519a1fSgjelinek 				free(buf);
7143ee519a1fSgjelinek 				if (res != Z_OK)
7144ee519a1fSgjelinek 					break;
7145ee519a1fSgjelinek 			}
7146ee519a1fSgjelinek 
7147ee519a1fSgjelinek 			(void) fclose(fp);
714839935be5Sgjelinek 
714939935be5Sgjelinek 			if (pkg_warn != NULL) {
715039935be5Sgjelinek 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
715139935be5Sgjelinek 				    "WARNING: package operation in progress "
715239935be5Sgjelinek 				    "on the following packages:\n   %s\n"),
715339935be5Sgjelinek 				    pkg_warn);
715439935be5Sgjelinek 				free(pkg_warn);
715539935be5Sgjelinek 			}
7156ee519a1fSgjelinek 		}
7157ee519a1fSgjelinek 	}
7158ee519a1fSgjelinek 
715939935be5Sgjelinek 	for (i = 0; ipds[i] != NULL; i++)
7160ee519a1fSgjelinek 		free(ipds[i]);
7161ee519a1fSgjelinek 	free(ipds);
7162ee519a1fSgjelinek 
716339935be5Sgjelinek 	for (i = 0; fss[i] != NULL; i++)
716439935be5Sgjelinek 		free(fss[i]);
716539935be5Sgjelinek 	free(fss);
716639935be5Sgjelinek 
7167ee519a1fSgjelinek 	if (res != Z_OK) {
7168ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
7169ee519a1fSgjelinek 	} else {
7170ee519a1fSgjelinek 		*pkg_list = pkgs;
7171ee519a1fSgjelinek 		*cnt = pkg_cnt;
7172ee519a1fSgjelinek 	}
7173ee519a1fSgjelinek 
7174ee519a1fSgjelinek 	return (res);
7175ee519a1fSgjelinek }
7176ee519a1fSgjelinek 
7177ee519a1fSgjelinek /*
7178ee519a1fSgjelinek  * Return true if pkg_name is in the list of pkgs that deliver into an
7179ee519a1fSgjelinek  * inherited pkg directory for the zone.
7180ee519a1fSgjelinek  */
7181ee519a1fSgjelinek static boolean_t
7182ee519a1fSgjelinek dir_pkg(char *pkg_name, char **pkg_list, int cnt)
7183ee519a1fSgjelinek {
7184ee519a1fSgjelinek 	int i;
7185ee519a1fSgjelinek 
7186ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
7187ee519a1fSgjelinek 		if (strcmp(pkg_name, pkg_list[i]) == 0)
7188ee519a1fSgjelinek 			return (B_TRUE);
7189ee519a1fSgjelinek 	}
7190ee519a1fSgjelinek 
7191ee519a1fSgjelinek 	return (B_FALSE);
7192ee519a1fSgjelinek }
7193ee519a1fSgjelinek 
7194*6cfd72c6Sgjelinek /*
7195*6cfd72c6Sgjelinek  * Keep track of obsoleted patches for this specific patch.  We don't need to
7196*6cfd72c6Sgjelinek  * keep track of the patch version since once a patch is obsoleted, all prior
7197*6cfd72c6Sgjelinek  * versions are also obsolete and there won't be any new versions.
7198*6cfd72c6Sgjelinek  */
7199*6cfd72c6Sgjelinek static int
7200*6cfd72c6Sgjelinek add_obs_patch(patch_node_t *patch, char *num, uu_list_pool_t *patches_pool)
7201*6cfd72c6Sgjelinek {
7202*6cfd72c6Sgjelinek 	obs_patch_node_t *obs;
7203*6cfd72c6Sgjelinek 
7204*6cfd72c6Sgjelinek 	if (patch->obs_patches == NULL) {
7205*6cfd72c6Sgjelinek 		if ((patch->obs_patches = uu_list_create(patches_pool, NULL,
7206*6cfd72c6Sgjelinek 		    0)) == NULL)
7207*6cfd72c6Sgjelinek 			return (Z_NOMEM);
7208*6cfd72c6Sgjelinek 	}
7209*6cfd72c6Sgjelinek 
7210*6cfd72c6Sgjelinek 	if ((obs = (obs_patch_node_t *)malloc(sizeof (obs_patch_node_t)))
7211*6cfd72c6Sgjelinek 	    == NULL)
7212*6cfd72c6Sgjelinek 		return (Z_NOMEM);
7213*6cfd72c6Sgjelinek 
7214*6cfd72c6Sgjelinek 	if ((obs->patch_num = strdup(num)) == NULL) {
7215*6cfd72c6Sgjelinek 		free(obs);
7216*6cfd72c6Sgjelinek 		return (Z_NOMEM);
7217*6cfd72c6Sgjelinek 	}
7218*6cfd72c6Sgjelinek 
7219*6cfd72c6Sgjelinek 	uu_list_node_init(obs, &obs->link, patches_pool);
7220*6cfd72c6Sgjelinek 	(void) uu_list_insert_before(patch->obs_patches, NULL, obs);
7221*6cfd72c6Sgjelinek 
7222*6cfd72c6Sgjelinek 	return (Z_OK);
7223*6cfd72c6Sgjelinek }
7224*6cfd72c6Sgjelinek 
7225ee519a1fSgjelinek /*
722639935be5Sgjelinek  * Keep track of obsoleted patches.  We don't need to keep track of the patch
722739935be5Sgjelinek  * version since once a patch is obsoleted, all prior versions are also
722839935be5Sgjelinek  * obsolete and there won't be any new versions.
722939935be5Sgjelinek  */
723039935be5Sgjelinek static int
7231*6cfd72c6Sgjelinek save_obs_patch(char *num, uu_avl_pool_t *patches_pool, uu_avl_t *obs_patches)
723239935be5Sgjelinek {
723339935be5Sgjelinek 	patch_node_t	*patch;
723439935be5Sgjelinek 	uu_avl_index_t where;
723539935be5Sgjelinek 
723639935be5Sgjelinek 	if ((patch = (patch_node_t *)malloc(sizeof (patch_node_t))) == NULL)
723739935be5Sgjelinek 		return (Z_NOMEM);
723839935be5Sgjelinek 
723939935be5Sgjelinek 	if ((patch->patch_num = strdup(num)) == NULL) {
724039935be5Sgjelinek 		free(patch);
724139935be5Sgjelinek 		return (Z_NOMEM);
724239935be5Sgjelinek 	}
724339935be5Sgjelinek 
724439935be5Sgjelinek 	patch->patch_vers = NULL;
7245*6cfd72c6Sgjelinek 	patch->obs_patches = NULL;
7246*6cfd72c6Sgjelinek 
724739935be5Sgjelinek 	uu_avl_node_init(patch, &patch->patch_node, patches_pool);
724839935be5Sgjelinek 
7249*6cfd72c6Sgjelinek 	if (uu_avl_find(obs_patches, patch, NULL, &where) != NULL) {
725039935be5Sgjelinek 		free(patch->patch_num);
725139935be5Sgjelinek 		free(patch);
725239935be5Sgjelinek 		return (Z_OK);
725339935be5Sgjelinek 	}
725439935be5Sgjelinek 
7255*6cfd72c6Sgjelinek 	uu_avl_insert(obs_patches, patch, where);
725639935be5Sgjelinek 	return (Z_OK);
725739935be5Sgjelinek }
725839935be5Sgjelinek 
725939935be5Sgjelinek /*
726039935be5Sgjelinek  * Keep a list of patches for a pkg.  If we see a newer version of a patch,
726139935be5Sgjelinek  * we only keep track of the newer version.
726239935be5Sgjelinek  */
726339935be5Sgjelinek static void
726439935be5Sgjelinek save_patch(patch_node_t *patch, uu_avl_t *patches_avl)
726539935be5Sgjelinek {
726639935be5Sgjelinek 	patch_node_t *existing;
726739935be5Sgjelinek 	uu_avl_index_t where;
726839935be5Sgjelinek 
726939935be5Sgjelinek 	/* Check if this is a newer version of a patch we already have. */
727039935be5Sgjelinek 	if ((existing = (patch_node_t *)uu_avl_find(patches_avl, patch, NULL,
727139935be5Sgjelinek 	    &where)) != NULL) {
727239935be5Sgjelinek 		char *endptr;
727339935be5Sgjelinek 		ulong_t pvers, evers;
727439935be5Sgjelinek 
727539935be5Sgjelinek 		pvers = strtoul(patch->patch_vers, &endptr, 10);
727639935be5Sgjelinek 		evers = strtoul(existing->patch_vers, &endptr, 10);
727739935be5Sgjelinek 
727839935be5Sgjelinek 		if (pvers > evers) {
727939935be5Sgjelinek 			free(existing->patch_vers);
728039935be5Sgjelinek 			existing->patch_vers = patch->patch_vers;
728139935be5Sgjelinek 			free(patch->patch_num);
728239935be5Sgjelinek 			free(patch);
728339935be5Sgjelinek 			return;
728439935be5Sgjelinek 		}
728539935be5Sgjelinek 	}
728639935be5Sgjelinek 
728739935be5Sgjelinek 	uu_avl_insert(patches_avl, patch, where);
728839935be5Sgjelinek }
728939935be5Sgjelinek 
729039935be5Sgjelinek /*
729139935be5Sgjelinek  * Check if a patch is on the list of obsoleted patches.  We don't need to
729239935be5Sgjelinek  * check the patch version since once a patch is obsoleted, all prior versions
729339935be5Sgjelinek  * are also obsolete and there won't be any new versions.
729439935be5Sgjelinek  */
729539935be5Sgjelinek static boolean_t
7296*6cfd72c6Sgjelinek obsolete_patch(patch_node_t *patch, uu_avl_t *obs_patches)
729739935be5Sgjelinek {
729839935be5Sgjelinek 	uu_avl_index_t	where;
729939935be5Sgjelinek 
7300*6cfd72c6Sgjelinek 	if (uu_avl_find(obs_patches, patch, NULL, &where) != NULL)
730139935be5Sgjelinek 		return (B_TRUE);
730239935be5Sgjelinek 
730339935be5Sgjelinek 	return (B_FALSE);
730439935be5Sgjelinek }
730539935be5Sgjelinek 
730639935be5Sgjelinek /* ARGSUSED */
730739935be5Sgjelinek static int
730839935be5Sgjelinek patch_node_compare(const void *l_arg, const void *r_arg, void *private)
730939935be5Sgjelinek {
731039935be5Sgjelinek 	patch_node_t *l = (patch_node_t *)l_arg;
731139935be5Sgjelinek 	patch_node_t *r = (patch_node_t *)r_arg;
731239935be5Sgjelinek 	char *endptr;
731339935be5Sgjelinek 	ulong_t lnum, rnum;
731439935be5Sgjelinek 
731539935be5Sgjelinek 	lnum = strtoul(l->patch_num, &endptr, 10);
731639935be5Sgjelinek 	rnum = strtoul(r->patch_num, &endptr, 10);
731739935be5Sgjelinek 
731839935be5Sgjelinek 	if (lnum > rnum)
731939935be5Sgjelinek 		return (1);
732039935be5Sgjelinek 	if (lnum < rnum)
732139935be5Sgjelinek 		return (-1);
732239935be5Sgjelinek 	return (0);
732339935be5Sgjelinek }
732439935be5Sgjelinek 
732539935be5Sgjelinek /*
732639935be5Sgjelinek  * Parse the patchinfo string for the patch.
7327ee519a1fSgjelinek  *
732839935be5Sgjelinek  * We are parsing entries of the form:
732939935be5Sgjelinek  * PATCH_INFO_121454-02=Installed: Wed Dec  7 07:13:51 PST 2005 From: mum \
733039935be5Sgjelinek  *	Obsoletes: 120777-03 121087-02 119108-07 Requires: 119575-02 \
733139935be5Sgjelinek  *	119255-06 Incompatibles:
733207b574eeSgjelinek  *
733339935be5Sgjelinek  * A backed out patch will have "backed out\n" as the status.  We should
7334*6cfd72c6Sgjelinek  * skip these patches.  We also ignore any entries that seem to be
7335*6cfd72c6Sgjelinek  * corrupted.  Obsolete patches are saved in the obs_patches parameter
7336*6cfd72c6Sgjelinek  * AVL list.
7337ee519a1fSgjelinek  */
7338ee519a1fSgjelinek static int
733939935be5Sgjelinek parse_info(char *patchinfo, uu_avl_pool_t *patches_pool, uu_avl_t *patches_avl,
7340*6cfd72c6Sgjelinek     uu_avl_t *obs_patches, uu_list_pool_t *list_pool)
7341ee519a1fSgjelinek {
7342ee519a1fSgjelinek 	char		*p;
7343ee519a1fSgjelinek 	char		*lastp;
734439935be5Sgjelinek 	char		*ep;
734539935be5Sgjelinek 	char		*pvers;
7346ee519a1fSgjelinek 	boolean_t	add_info = B_FALSE;
734739935be5Sgjelinek 	patch_node_t	*patch;
7348ee519a1fSgjelinek 
734939935be5Sgjelinek 	if (strlen(patchinfo) < (sizeof (PATCHINFO) - 1))
735007b574eeSgjelinek 		return (Z_OK);
735107b574eeSgjelinek 
735239935be5Sgjelinek 	/* Skip over "PATCH_INFO_" to get the patch id. */
735339935be5Sgjelinek 	p = patchinfo + sizeof (PATCHINFO) - 1;
735439935be5Sgjelinek 	if ((ep = strchr(p, '=')) == NULL)
735539935be5Sgjelinek 		return (Z_OK);
7356ee519a1fSgjelinek 
735739935be5Sgjelinek 	*ep++ = '\0';
735839935be5Sgjelinek 
735939935be5Sgjelinek 	/* Ignore all but installed patches. */
736039935be5Sgjelinek 	if (strncmp(ep, "Installed:", 10) != 0)
736139935be5Sgjelinek 		return (Z_OK);
736239935be5Sgjelinek 
736339935be5Sgjelinek 	/* remove newline */
736439935be5Sgjelinek 	lastp = (ep + strlen(ep) - 1);
736539935be5Sgjelinek 	if (*lastp == '\n')
736639935be5Sgjelinek 		*lastp = '\0';
736739935be5Sgjelinek 
736839935be5Sgjelinek 	if ((patch = (patch_node_t *)malloc(sizeof (patch_node_t))) == NULL)
736939935be5Sgjelinek 		return (Z_NOMEM);
737039935be5Sgjelinek 
737139935be5Sgjelinek 	if ((pvers = strchr(p, '-')) != NULL)
737239935be5Sgjelinek 		*pvers++ = '\0';
737339935be5Sgjelinek 	else
737439935be5Sgjelinek 		pvers = "";
737539935be5Sgjelinek 
737639935be5Sgjelinek 	if ((patch->patch_num = strdup(p)) == NULL) {
737739935be5Sgjelinek 		free(patch);
737839935be5Sgjelinek 		return (Z_NOMEM);
737939935be5Sgjelinek 	}
738039935be5Sgjelinek 	if ((patch->patch_vers = strdup(pvers)) == NULL) {
738139935be5Sgjelinek 		free(patch->patch_num);
738239935be5Sgjelinek 		free(patch);
738339935be5Sgjelinek 		return (Z_NOMEM);
738439935be5Sgjelinek 	}
7385*6cfd72c6Sgjelinek 	patch->obs_patches = NULL;
738639935be5Sgjelinek 
738739935be5Sgjelinek 	uu_avl_node_init(patch, &patch->patch_node, patches_pool);
738839935be5Sgjelinek 	save_patch(patch, patches_avl);
7389ee519a1fSgjelinek 
7390ee519a1fSgjelinek 	/*
7391ee519a1fSgjelinek 	 * Start with the first token.  This will probably be "Installed:".
7392ee519a1fSgjelinek 	 * If we can't tokenize this entry, just return.
7393ee519a1fSgjelinek 	 */
739439935be5Sgjelinek 	if ((p = strtok_r(ep, " ", &lastp)) == NULL)
7395ee519a1fSgjelinek 		return (Z_OK);
7396ee519a1fSgjelinek 
7397ee519a1fSgjelinek 	do {
7398ee519a1fSgjelinek 		if (strcmp(p, "Installed:") == 0 ||
7399ee519a1fSgjelinek 		    strcmp(p, "Requires:") == 0 ||
740039935be5Sgjelinek 		    strcmp(p, "From:") == 0 ||
740139935be5Sgjelinek 		    strcmp(p, "Incompatibles:") == 0) {
7402ee519a1fSgjelinek 			add_info = B_FALSE;
7403ee519a1fSgjelinek 			continue;
7404ee519a1fSgjelinek 		} else if (strcmp(p, "Obsoletes:") == 0) {
7405ee519a1fSgjelinek 			add_info = B_TRUE;
7406ee519a1fSgjelinek 			continue;
7407ee519a1fSgjelinek 		}
7408ee519a1fSgjelinek 
7409ee519a1fSgjelinek 		if (!add_info)
7410ee519a1fSgjelinek 			continue;
7411ee519a1fSgjelinek 
741239935be5Sgjelinek 		if ((pvers = strchr(p, '-')) != NULL)
741339935be5Sgjelinek 			*pvers = '\0';
7414ee519a1fSgjelinek 
7415*6cfd72c6Sgjelinek 		/*
7416*6cfd72c6Sgjelinek 		 * We save all of the obsolete patches in one big list in the
7417*6cfd72c6Sgjelinek 		 * obs_patches AVL tree so that we know not to output those as
7418*6cfd72c6Sgjelinek 		 * part of the sw dependencies.  However, we also need to save
7419*6cfd72c6Sgjelinek 		 * the obsolete patch information for this sepcific patch so
7420*6cfd72c6Sgjelinek 		 * so that we can do the cross manifest patch checking
7421*6cfd72c6Sgjelinek 		 * correctly.
7422*6cfd72c6Sgjelinek 		 */
7423*6cfd72c6Sgjelinek 		if (save_obs_patch(p, patches_pool, obs_patches) != Z_OK)
7424*6cfd72c6Sgjelinek 			return (Z_NOMEM);
7425*6cfd72c6Sgjelinek 		if (add_obs_patch(patch, p, list_pool) != Z_OK)
742639935be5Sgjelinek 			return (Z_NOMEM);
7427ee519a1fSgjelinek 	} while ((p = strtok_r(NULL, " ", &lastp)) != NULL);
7428ee519a1fSgjelinek 
7429ee519a1fSgjelinek 	return (Z_OK);
7430ee519a1fSgjelinek }
7431ee519a1fSgjelinek 
743239935be5Sgjelinek /*
743339935be5Sgjelinek  * AVL walker callback used to add patch to XML manifest.
743439935be5Sgjelinek  *
743539935be5Sgjelinek  * PATH_MAX is used in the pkg/patch code as the maximum size for the patch
743639935be5Sgjelinek  * number/version string.
743739935be5Sgjelinek  */
743839935be5Sgjelinek static int
743939935be5Sgjelinek add_patch(void *e, void *p)
7440ee519a1fSgjelinek {
744139935be5Sgjelinek 	xmlNodePtr	node;
7442ee519a1fSgjelinek 	xmlNodePtr	cur;
744339935be5Sgjelinek 	char		id[PATH_MAX];
744439935be5Sgjelinek 	patch_node_t	*patch;
744539935be5Sgjelinek 	patch_parms_t	*args;
7446ee519a1fSgjelinek 
744739935be5Sgjelinek 	patch = e;
744839935be5Sgjelinek 	args = p;
7449ee519a1fSgjelinek 
745039935be5Sgjelinek 	/* skip this patch if it has been obsoleted */
745139935be5Sgjelinek 	if (obsolete_patch(patch, args->obs_patches_avl))
745239935be5Sgjelinek 		return (UU_WALK_NEXT);
745339935be5Sgjelinek 
745439935be5Sgjelinek 	if (patch->patch_vers[0] == '\0')
745539935be5Sgjelinek 		(void) snprintf(id, sizeof (id), "%s", patch->patch_num);
745639935be5Sgjelinek 	else
745739935be5Sgjelinek 		(void) snprintf(id, sizeof (id), "%s-%s", patch->patch_num,
745839935be5Sgjelinek 		    patch->patch_vers);
745939935be5Sgjelinek 
746039935be5Sgjelinek 	if ((args->res = operation_prep(args->handle)) != Z_OK)
746139935be5Sgjelinek 		return (UU_WALK_DONE);
746239935be5Sgjelinek 
746339935be5Sgjelinek 	cur = args->handle->zone_dh_cur;
746439935be5Sgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
746539935be5Sgjelinek 	if ((args->res = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
746639935be5Sgjelinek 		return (UU_WALK_DONE);
746739935be5Sgjelinek 
7468*6cfd72c6Sgjelinek 	if (patch->obs_patches != NULL) {
7469*6cfd72c6Sgjelinek 		obs_patch_node_t *op;
7470*6cfd72c6Sgjelinek 		xmlNodePtr	node2;
7471*6cfd72c6Sgjelinek 
7472*6cfd72c6Sgjelinek 		for (op = uu_list_first(patch->obs_patches); op != NULL;
7473*6cfd72c6Sgjelinek 		    op = uu_list_next(patch->obs_patches, op)) {
7474*6cfd72c6Sgjelinek 			(void) snprintf(id, sizeof (id), "%s", op->patch_num);
7475*6cfd72c6Sgjelinek 			node2 = xmlNewTextChild(node, NULL, DTD_ELEM_OBSOLETES,
7476*6cfd72c6Sgjelinek 			    NULL);
7477*6cfd72c6Sgjelinek 			if ((args->res = newprop(node2, DTD_ATTR_ID, id))
7478*6cfd72c6Sgjelinek 			    != Z_OK)
7479*6cfd72c6Sgjelinek 				return (UU_WALK_DONE);
7480*6cfd72c6Sgjelinek 		}
7481*6cfd72c6Sgjelinek 	}
7482*6cfd72c6Sgjelinek 
748339935be5Sgjelinek 	return (UU_WALK_NEXT);
748439935be5Sgjelinek }
748539935be5Sgjelinek 
748639935be5Sgjelinek static void
748739935be5Sgjelinek patch_avl_delete(uu_avl_t *patches_avl)
748839935be5Sgjelinek {
748939935be5Sgjelinek 	if (patches_avl != NULL) {
749039935be5Sgjelinek 		patch_node_t *p;
749139935be5Sgjelinek 		void *cookie = NULL;
749239935be5Sgjelinek 
749339935be5Sgjelinek 		while ((p = (patch_node_t *)uu_avl_teardown(patches_avl,
749439935be5Sgjelinek 		    &cookie)) != NULL) {
749539935be5Sgjelinek 			free(p->patch_num);
749639935be5Sgjelinek 			free(p->patch_vers);
7497*6cfd72c6Sgjelinek 
7498*6cfd72c6Sgjelinek 			if (p->obs_patches != NULL) {
7499*6cfd72c6Sgjelinek 				obs_patch_node_t *op;
7500*6cfd72c6Sgjelinek 				void *cookie2 = NULL;
7501*6cfd72c6Sgjelinek 
7502*6cfd72c6Sgjelinek 				while ((op = uu_list_teardown(p->obs_patches,
7503*6cfd72c6Sgjelinek 				    &cookie2)) != NULL) {
7504*6cfd72c6Sgjelinek 					free(op->patch_num);
7505*6cfd72c6Sgjelinek 					free(op);
7506*6cfd72c6Sgjelinek 				}
7507*6cfd72c6Sgjelinek 				uu_list_destroy(p->obs_patches);
7508*6cfd72c6Sgjelinek 			}
7509*6cfd72c6Sgjelinek 
751039935be5Sgjelinek 			free(p);
7511ee519a1fSgjelinek 		}
7512ee519a1fSgjelinek 
751339935be5Sgjelinek 		uu_avl_destroy(patches_avl);
751439935be5Sgjelinek 	}
7515ee519a1fSgjelinek }
7516ee519a1fSgjelinek 
7517ee519a1fSgjelinek /*
751839935be5Sgjelinek  * Add the unique, highest version patches that are associated with this pkg
751939935be5Sgjelinek  * to the sw inventory on the handle.
7520ee519a1fSgjelinek  */
7521ee519a1fSgjelinek static int
752239935be5Sgjelinek add_patches(zone_dochandle_t handle, struct zone_pkginfo *infop,
7523*6cfd72c6Sgjelinek     uu_avl_pool_t *patches_pool, uu_avl_t *obs_patches,
7524*6cfd72c6Sgjelinek     uu_list_pool_t *list_pool)
7525ee519a1fSgjelinek {
752639935be5Sgjelinek 	int		i;
752739935be5Sgjelinek 	int		res;
752839935be5Sgjelinek 	uu_avl_t 	*patches_avl;
752939935be5Sgjelinek 	patch_parms_t	args;
7530ee519a1fSgjelinek 
753139935be5Sgjelinek 	if ((patches_avl = uu_avl_create(patches_pool, NULL, UU_DEFAULT))
753239935be5Sgjelinek 	    == NULL)
753339935be5Sgjelinek 		return (Z_NOMEM);
7534ee519a1fSgjelinek 
753539935be5Sgjelinek 	for (i = 0; i < infop->zpi_patch_cnt; i++) {
753639935be5Sgjelinek 		if ((res = parse_info(infop->zpi_patchinfo[i], patches_pool,
7537*6cfd72c6Sgjelinek 		    patches_avl, obs_patches, list_pool)) != Z_OK) {
753839935be5Sgjelinek 			patch_avl_delete(patches_avl);
753939935be5Sgjelinek 			return (res);
754039935be5Sgjelinek 		}
754139935be5Sgjelinek 	}
7542ee519a1fSgjelinek 
7543*6cfd72c6Sgjelinek 	args.obs_patches_avl = obs_patches;
754439935be5Sgjelinek 	args.handle = handle;
754539935be5Sgjelinek 	args.res = Z_OK;
7546ee519a1fSgjelinek 
754739935be5Sgjelinek 	(void) uu_avl_walk(patches_avl, add_patch, &args, 0);
7548ee519a1fSgjelinek 
754939935be5Sgjelinek 	patch_avl_delete(patches_avl);
755039935be5Sgjelinek 	return (args.res);
7551ee519a1fSgjelinek }
7552ee519a1fSgjelinek 
7553*6cfd72c6Sgjelinek /*
7554*6cfd72c6Sgjelinek  * Keep track of the pkgs we have already processed so that we can quickly
7555*6cfd72c6Sgjelinek  * skip those pkgs while recursively doing dependents.
7556*6cfd72c6Sgjelinek  */
7557*6cfd72c6Sgjelinek static boolean_t
7558*6cfd72c6Sgjelinek pkg_in_manifest(uu_avl_t *saw_pkgs, char *pname, uu_avl_pool_t *pkgs_pool)
7559*6cfd72c6Sgjelinek {
7560*6cfd72c6Sgjelinek 	uu_avl_index_t where;
7561*6cfd72c6Sgjelinek 
7562*6cfd72c6Sgjelinek 	if (uu_avl_find(saw_pkgs, pname, NULL, &where) == NULL) {
7563*6cfd72c6Sgjelinek 		zone_pkg_entry_t *pkg;
7564*6cfd72c6Sgjelinek 
7565*6cfd72c6Sgjelinek 		/*
7566*6cfd72c6Sgjelinek 		 * We need to add it.  If we don't have memory we just skip
7567*6cfd72c6Sgjelinek 		 * this pkg since this routine improves performance but the
7568*6cfd72c6Sgjelinek 		 * algorithm is still correct without it.
7569*6cfd72c6Sgjelinek 		 */
7570*6cfd72c6Sgjelinek 		if ((pkg = (zone_pkg_entry_t *)
7571*6cfd72c6Sgjelinek 		    malloc(sizeof (zone_pkg_entry_t))) == NULL)
7572*6cfd72c6Sgjelinek 			return (B_FALSE);
7573*6cfd72c6Sgjelinek 
7574*6cfd72c6Sgjelinek 		if ((pkg->zpe_name = strdup(pname)) == NULL) {
7575*6cfd72c6Sgjelinek 			free(pkg);
7576*6cfd72c6Sgjelinek 			return (B_FALSE);
7577*6cfd72c6Sgjelinek 		}
7578*6cfd72c6Sgjelinek 
7579*6cfd72c6Sgjelinek 		pkg->zpe_vers = NULL;
7580*6cfd72c6Sgjelinek 		pkg->zpe_patches_avl = NULL;
7581*6cfd72c6Sgjelinek 
7582*6cfd72c6Sgjelinek 		/* Insert pkg into the AVL tree. */
7583*6cfd72c6Sgjelinek 		uu_avl_node_init(pkg, &pkg->zpe_entry, pkgs_pool);
7584*6cfd72c6Sgjelinek 		uu_avl_insert(saw_pkgs, pkg, where);
7585*6cfd72c6Sgjelinek 		return (B_FALSE);
7586*6cfd72c6Sgjelinek 	}
7587*6cfd72c6Sgjelinek 
7588*6cfd72c6Sgjelinek 	return (B_TRUE);
7589*6cfd72c6Sgjelinek }
7590*6cfd72c6Sgjelinek 
7591ee519a1fSgjelinek /*
7592ee519a1fSgjelinek  * Add the pkg to the sw inventory on the handle.
7593ee519a1fSgjelinek  */
7594ee519a1fSgjelinek static int
7595ee519a1fSgjelinek add_pkg(zone_dochandle_t handle, char *name, char *version)
7596ee519a1fSgjelinek {
7597ee519a1fSgjelinek 	xmlNodePtr newnode;
7598ee519a1fSgjelinek 	xmlNodePtr cur;
7599ee519a1fSgjelinek 	int err;
7600ee519a1fSgjelinek 
7601ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
7602ee519a1fSgjelinek 		return (err);
7603ee519a1fSgjelinek 
7604ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
7605ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
7606ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
7607ee519a1fSgjelinek 		return (err);
7608ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
7609ee519a1fSgjelinek 		return (err);
7610ee519a1fSgjelinek 	return (Z_OK);
7611ee519a1fSgjelinek }
7612ee519a1fSgjelinek 
7613ee519a1fSgjelinek static void
7614ee519a1fSgjelinek free_pkginfo(struct zone_pkginfo *infop)
7615ee519a1fSgjelinek {
7616ee519a1fSgjelinek 	free(infop->zpi_version);
7617ee519a1fSgjelinek 	if (infop->zpi_patch_cnt > 0) {
7618ee519a1fSgjelinek 		int i;
7619ee519a1fSgjelinek 
7620ee519a1fSgjelinek 		for (i = 0; i < infop->zpi_patch_cnt; i++)
7621ee519a1fSgjelinek 			free(infop->zpi_patchinfo[i]);
7622ee519a1fSgjelinek 		free(infop->zpi_patchinfo);
7623ee519a1fSgjelinek 	}
7624ee519a1fSgjelinek }
7625ee519a1fSgjelinek 
7626ee519a1fSgjelinek /*
7627ee519a1fSgjelinek  * Read the pkginfo file and populate the structure with the data we need
7628ee519a1fSgjelinek  * from this pkg for a sw inventory.
7629ee519a1fSgjelinek  */
7630ee519a1fSgjelinek static int
7631ee519a1fSgjelinek get_pkginfo(char *pkginfo, struct zone_pkginfo *infop)
7632ee519a1fSgjelinek {
7633ee519a1fSgjelinek 	FILE	*fp;
7634ee519a1fSgjelinek 	char	*buf;
7635ee519a1fSgjelinek 	int	err = 0;
7636ee519a1fSgjelinek 
7637ee519a1fSgjelinek 	infop->zpi_all_zones = B_FALSE;
7638ee519a1fSgjelinek 	infop->zpi_this_zone = B_FALSE;
7639ee519a1fSgjelinek 	infop->zpi_version = NULL;
7640ee519a1fSgjelinek 	infop->zpi_patch_cnt = 0;
7641ee519a1fSgjelinek 	infop->zpi_patchinfo = NULL;
7642ee519a1fSgjelinek 
7643ee519a1fSgjelinek 	if ((fp = fopen(pkginfo, "r")) == NULL)
7644ee519a1fSgjelinek 		return (errno);
7645ee519a1fSgjelinek 
7646ee519a1fSgjelinek 	while ((buf = read_pkg_data(fp)) != NULL) {
7647ee519a1fSgjelinek 		if (strncmp(buf, VERSION, sizeof (VERSION) - 1) == 0) {
7648ee519a1fSgjelinek 			int len;
7649ee519a1fSgjelinek 
7650ee519a1fSgjelinek 			if ((infop->zpi_version =
7651ee519a1fSgjelinek 			    strdup(buf + sizeof (VERSION) - 1)) == NULL) {
7652ee519a1fSgjelinek 				err = ENOMEM;
7653ee519a1fSgjelinek 				break;
7654ee519a1fSgjelinek 			}
7655ee519a1fSgjelinek 
7656ee519a1fSgjelinek 			/* remove trailing newline */
7657ee519a1fSgjelinek 			len = strlen(infop->zpi_version);
7658ee519a1fSgjelinek 			*(infop->zpi_version + len - 1) = 0;
7659ee519a1fSgjelinek 
766045916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_ALL_ZONES) == 0) {
7661ee519a1fSgjelinek 			infop->zpi_all_zones = B_TRUE;
7662ee519a1fSgjelinek 
766345916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_THIS_ZONE) == 0) {
7664ee519a1fSgjelinek 			infop->zpi_this_zone = B_TRUE;
7665ee519a1fSgjelinek 
7666ee519a1fSgjelinek 		} else if (strncmp(buf, PATCHINFO, sizeof (PATCHINFO) - 1)
7667ee519a1fSgjelinek 		    == 0) {
7668ee519a1fSgjelinek 			char **p;
7669ee519a1fSgjelinek 
7670ee519a1fSgjelinek 			if ((p = (char **)realloc(infop->zpi_patchinfo,
7671ee519a1fSgjelinek 			    sizeof (char *) * (infop->zpi_patch_cnt + 1)))
7672ee519a1fSgjelinek 			    == NULL) {
7673ee519a1fSgjelinek 				err = ENOMEM;
7674ee519a1fSgjelinek 				break;
7675ee519a1fSgjelinek 			}
7676ee519a1fSgjelinek 			infop->zpi_patchinfo = p;
7677ee519a1fSgjelinek 
7678ee519a1fSgjelinek 			if ((infop->zpi_patchinfo[infop->zpi_patch_cnt] =
7679ee519a1fSgjelinek 			    strdup(buf)) == NULL) {
7680ee519a1fSgjelinek 				err = ENOMEM;
7681ee519a1fSgjelinek 				break;
7682ee519a1fSgjelinek 			}
7683ee519a1fSgjelinek 			infop->zpi_patch_cnt++;
7684ee519a1fSgjelinek 		}
7685ee519a1fSgjelinek 
7686ee519a1fSgjelinek 		free(buf);
7687ee519a1fSgjelinek 	}
7688ee519a1fSgjelinek 
7689ee519a1fSgjelinek 	free(buf);
7690ee519a1fSgjelinek 
7691ee519a1fSgjelinek 	if (errno == ENOMEM) {
7692ee519a1fSgjelinek 		err = ENOMEM;
7693ee519a1fSgjelinek 		/* Clean up anything we did manage to allocate. */
7694ee519a1fSgjelinek 		free_pkginfo(infop);
7695ee519a1fSgjelinek 	}
7696ee519a1fSgjelinek 
7697ee519a1fSgjelinek 	(void) fclose(fp);
7698ee519a1fSgjelinek 
7699ee519a1fSgjelinek 	return (err);
7700ee519a1fSgjelinek }
7701ee519a1fSgjelinek 
7702*6cfd72c6Sgjelinek /*
7703*6cfd72c6Sgjelinek  * Add any dependent pkgs to the list.  The pkg depend file lists pkg
7704*6cfd72c6Sgjelinek  * dependencies, one per line with an entry that looks like:
7705*6cfd72c6Sgjelinek  *	P SUNWcar       Core Architecture, (Root)
7706*6cfd72c6Sgjelinek  * See the depend(4) man page.
7707*6cfd72c6Sgjelinek  */
7708*6cfd72c6Sgjelinek static int
7709*6cfd72c6Sgjelinek add_dependents(zone_dochandle_t handle, char *pname,
7710*6cfd72c6Sgjelinek     uu_avl_pool_t *patches_pool, uu_avl_t *obs_patches,
7711*6cfd72c6Sgjelinek     uu_list_pool_t *list_pool, uu_avl_t *saw_pkgs, uu_avl_pool_t *pkgs_pool)
7712*6cfd72c6Sgjelinek {
7713*6cfd72c6Sgjelinek 	int		res = Z_OK;
7714*6cfd72c6Sgjelinek 	FILE		*fp;
7715*6cfd72c6Sgjelinek 	char		depend[MAXPATHLEN];
7716*6cfd72c6Sgjelinek 	char		*buf;
7717*6cfd72c6Sgjelinek 	struct stat	sbuf;
7718*6cfd72c6Sgjelinek 
7719*6cfd72c6Sgjelinek 	(void) snprintf(depend, sizeof (depend), "%s/%s/install/depend",
7720*6cfd72c6Sgjelinek 	    PKG_PATH, pname);
7721*6cfd72c6Sgjelinek 
7722*6cfd72c6Sgjelinek 	if (stat(depend, &sbuf) == -1 || !S_ISREG(sbuf.st_mode))
7723*6cfd72c6Sgjelinek 		return (Z_OK);
7724*6cfd72c6Sgjelinek 
7725*6cfd72c6Sgjelinek 	if ((fp = fopen(depend, "r")) == NULL)
7726*6cfd72c6Sgjelinek 		return (Z_OK);
7727*6cfd72c6Sgjelinek 
7728*6cfd72c6Sgjelinek 	while ((buf = read_pkg_data(fp)) != NULL) {
7729*6cfd72c6Sgjelinek 		char *deppkg;
7730*6cfd72c6Sgjelinek 		char *delims = " \t";
7731*6cfd72c6Sgjelinek 		char pkginfo[MAXPATHLEN];
7732*6cfd72c6Sgjelinek 		struct zone_pkginfo info;
7733*6cfd72c6Sgjelinek 
7734*6cfd72c6Sgjelinek 		if (*buf != 'P') {
7735*6cfd72c6Sgjelinek 			free(buf);
7736*6cfd72c6Sgjelinek 			continue;
7737*6cfd72c6Sgjelinek 		}
7738*6cfd72c6Sgjelinek 
7739*6cfd72c6Sgjelinek 		/* Skip past the leading 'P '. */
7740*6cfd72c6Sgjelinek 		if ((deppkg = strtok(buf + 2, delims)) == NULL) {
7741*6cfd72c6Sgjelinek 			free(buf);
7742*6cfd72c6Sgjelinek 			continue;
7743*6cfd72c6Sgjelinek 		}
7744*6cfd72c6Sgjelinek 
7745*6cfd72c6Sgjelinek 		/* If the pkg is already in the manifest don't add it again. */
7746*6cfd72c6Sgjelinek 		if (pkg_in_manifest(saw_pkgs, deppkg, pkgs_pool)) {
7747*6cfd72c6Sgjelinek 			free(buf);
7748*6cfd72c6Sgjelinek 			continue;
7749*6cfd72c6Sgjelinek 		}
7750*6cfd72c6Sgjelinek 
7751*6cfd72c6Sgjelinek 		(void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo",
7752*6cfd72c6Sgjelinek 		    PKG_PATH, deppkg);
7753*6cfd72c6Sgjelinek 
7754*6cfd72c6Sgjelinek 		if (stat(pkginfo, &sbuf) == -1 || !S_ISREG(sbuf.st_mode)) {
7755*6cfd72c6Sgjelinek 			free(buf);
7756*6cfd72c6Sgjelinek 			continue;
7757*6cfd72c6Sgjelinek 		}
7758*6cfd72c6Sgjelinek 
7759*6cfd72c6Sgjelinek 		if (get_pkginfo(pkginfo, &info) != 0) {
7760*6cfd72c6Sgjelinek 			res = Z_NOMEM;
7761*6cfd72c6Sgjelinek 			free(buf);
7762*6cfd72c6Sgjelinek 			break;
7763*6cfd72c6Sgjelinek 		}
7764*6cfd72c6Sgjelinek 
7765*6cfd72c6Sgjelinek 		if ((res = add_dependents(handle, deppkg, patches_pool,
7766*6cfd72c6Sgjelinek 		    obs_patches, list_pool, saw_pkgs, pkgs_pool)) == Z_OK &&
7767*6cfd72c6Sgjelinek 		    (res = add_pkg(handle, deppkg, info.zpi_version)) == Z_OK) {
7768*6cfd72c6Sgjelinek 			if (info.zpi_patch_cnt > 0)
7769*6cfd72c6Sgjelinek 				res = add_patches(handle, &info, patches_pool,
7770*6cfd72c6Sgjelinek 				    obs_patches, list_pool);
7771*6cfd72c6Sgjelinek 		}
7772*6cfd72c6Sgjelinek 
7773*6cfd72c6Sgjelinek 		free(buf);
7774*6cfd72c6Sgjelinek 		free_pkginfo(&info);
7775*6cfd72c6Sgjelinek 
7776*6cfd72c6Sgjelinek 		if (res != Z_OK)
7777*6cfd72c6Sgjelinek 			break;
7778*6cfd72c6Sgjelinek 	}
7779*6cfd72c6Sgjelinek 
7780*6cfd72c6Sgjelinek 	(void) fclose(fp);
7781*6cfd72c6Sgjelinek 	return (res);
7782*6cfd72c6Sgjelinek }
7783*6cfd72c6Sgjelinek 
7784*6cfd72c6Sgjelinek /* ARGSUSED */
7785*6cfd72c6Sgjelinek static int
7786*6cfd72c6Sgjelinek pkg_entry_compare(const void *l_arg, const void *r_arg, void *private)
7787*6cfd72c6Sgjelinek {
7788*6cfd72c6Sgjelinek 	zone_pkg_entry_t *pkg = (zone_pkg_entry_t *)l_arg;
7789*6cfd72c6Sgjelinek 	char *name = (char *)r_arg;
7790*6cfd72c6Sgjelinek 
7791*6cfd72c6Sgjelinek 	return (strcmp(pkg->zpe_name, name));
7792*6cfd72c6Sgjelinek }
7793*6cfd72c6Sgjelinek 
7794*6cfd72c6Sgjelinek static void
7795*6cfd72c6Sgjelinek pkg_avl_delete(uu_avl_t *pavl)
7796*6cfd72c6Sgjelinek {
7797*6cfd72c6Sgjelinek 	if (pavl != NULL) {
7798*6cfd72c6Sgjelinek 		zone_pkg_entry_t *p;
7799*6cfd72c6Sgjelinek 		void *cookie = NULL;
7800*6cfd72c6Sgjelinek 
7801*6cfd72c6Sgjelinek 		while ((p = uu_avl_teardown(pavl, &cookie)) != NULL) {
7802*6cfd72c6Sgjelinek 			free(p->zpe_name);
7803*6cfd72c6Sgjelinek 			free(p);
7804*6cfd72c6Sgjelinek 		}
7805*6cfd72c6Sgjelinek 
7806*6cfd72c6Sgjelinek 		uu_avl_destroy(pavl);
7807*6cfd72c6Sgjelinek 	}
7808*6cfd72c6Sgjelinek }
7809*6cfd72c6Sgjelinek 
7810ee519a1fSgjelinek /*
7811ee519a1fSgjelinek  * Take a software inventory of the global zone.  We need to get the set of
7812ee519a1fSgjelinek  * packages and patches that are on the global zone that the specified
7813ee519a1fSgjelinek  * non-global zone depends on.  The packages we need in the inventory are:
7814ee519a1fSgjelinek  *
7815ee519a1fSgjelinek  * - skip the package if SUNW_PKG_THISZONE is 'true'
7816ee519a1fSgjelinek  * otherwise,
7817ee519a1fSgjelinek  * - add the package if
7818ee519a1fSgjelinek  * a) SUNW_PKG_ALLZONES is 'true',
7819ee519a1fSgjelinek  * or
7820ee519a1fSgjelinek  * b) any file delivered by the package is in a file system that is inherited
7821ee519a1fSgjelinek  * from the global zone.
7822ee519a1fSgjelinek  * If the zone does not inherit any file systems (whole root)
7823ee519a1fSgjelinek  * then (b) will be skipped.
7824ee519a1fSgjelinek  *
7825ee519a1fSgjelinek  * For each of the packages that is being added to the inventory, we will also
7826*6cfd72c6Sgjelinek  * add its dependent packages to the inventory.
7827*6cfd72c6Sgjelinek  *
7828*6cfd72c6Sgjelinek  * For each of the packages that is being added to the inventory, we will also
7829ee519a1fSgjelinek  * add all of the associated, unique patches to the inventory.
7830*6cfd72c6Sgjelinek  *
7831*6cfd72c6Sgjelinek  * See the comment for zonecfg_getpkgdata() for compatability restrictions on
7832*6cfd72c6Sgjelinek  * how we must save the XML representation of the software inventory.
7833ee519a1fSgjelinek  */
7834ee519a1fSgjelinek static int
7835ee519a1fSgjelinek zonecfg_sw_inventory(zone_dochandle_t handle)
7836ee519a1fSgjelinek {
7837ee519a1fSgjelinek 	char		pkginfo[MAXPATHLEN];
7838ee519a1fSgjelinek 	int		res;
7839ee519a1fSgjelinek 	struct dirent	*dp;
7840ee519a1fSgjelinek 	DIR		*dirp;
7841ee519a1fSgjelinek 	struct stat	buf;
7842ee519a1fSgjelinek 	struct zone_pkginfo	info;
7843ee519a1fSgjelinek 	int		pkg_cnt = 0;
7844ee519a1fSgjelinek 	char		**pkgs = NULL;
7845*6cfd72c6Sgjelinek 	uu_avl_pool_t 	*pkgs_pool = NULL;
7846*6cfd72c6Sgjelinek 	uu_avl_pool_t 	*patches_pool = NULL;
7847*6cfd72c6Sgjelinek 	uu_list_pool_t 	*list_pool = NULL;
7848*6cfd72c6Sgjelinek 	uu_avl_t	*saw_pkgs = NULL;
7849*6cfd72c6Sgjelinek 	uu_avl_t 	*obs_patches = NULL;
7850*6cfd72c6Sgjelinek 
7851*6cfd72c6Sgjelinek 	if ((pkgs_pool = uu_avl_pool_create("pkgs_pool",
7852*6cfd72c6Sgjelinek 	    sizeof (zone_pkg_entry_t), offsetof(zone_pkg_entry_t, zpe_entry),
7853*6cfd72c6Sgjelinek 	    pkg_entry_compare, UU_DEFAULT)) == NULL) {
7854*6cfd72c6Sgjelinek 		res = Z_NOMEM;
7855*6cfd72c6Sgjelinek 		goto done;
7856*6cfd72c6Sgjelinek 	}
7857*6cfd72c6Sgjelinek 
7858*6cfd72c6Sgjelinek 	if ((saw_pkgs = uu_avl_create(pkgs_pool, NULL, UU_DEFAULT)) == NULL) {
7859*6cfd72c6Sgjelinek 		res = Z_NOMEM;
7860*6cfd72c6Sgjelinek 		goto done;
7861*6cfd72c6Sgjelinek 	}
7862ee519a1fSgjelinek 
786339935be5Sgjelinek 	if ((patches_pool = uu_avl_pool_create("patches_pool",
786439935be5Sgjelinek 	    sizeof (patch_node_t), offsetof(patch_node_t, patch_node),
786539935be5Sgjelinek 	    patch_node_compare, UU_DEFAULT)) == NULL) {
7866*6cfd72c6Sgjelinek 		res = Z_NOMEM;
7867*6cfd72c6Sgjelinek 		goto done;
786839935be5Sgjelinek 	}
786939935be5Sgjelinek 
7870*6cfd72c6Sgjelinek 	if ((list_pool = uu_list_pool_create("list_pool",
7871*6cfd72c6Sgjelinek 	    sizeof (obs_patch_node_t), offsetof(obs_patch_node_t, link), NULL,
7872*6cfd72c6Sgjelinek 	    UU_DEFAULT)) == NULL) {
7873*6cfd72c6Sgjelinek 		res = Z_NOMEM;
7874*6cfd72c6Sgjelinek 		goto done;
7875*6cfd72c6Sgjelinek 	}
7876*6cfd72c6Sgjelinek 
7877*6cfd72c6Sgjelinek 	/*
7878*6cfd72c6Sgjelinek 	 * The obs_patches AVL tree saves all of the obsolete patches so
7879*6cfd72c6Sgjelinek 	 * that we know not to output those as part of the sw dependencies.
7880*6cfd72c6Sgjelinek 	 */
7881*6cfd72c6Sgjelinek 	if ((obs_patches = uu_avl_create(patches_pool, NULL, UU_DEFAULT))
788239935be5Sgjelinek 	    == NULL) {
7883*6cfd72c6Sgjelinek 		res = Z_NOMEM;
7884*6cfd72c6Sgjelinek 		goto done;
788539935be5Sgjelinek 	}
788639935be5Sgjelinek 
788739935be5Sgjelinek 	if ((res = get_ipd_pkgs(handle, &pkgs, &pkg_cnt)) != Z_OK) {
7888*6cfd72c6Sgjelinek 		res = Z_NOMEM;
7889*6cfd72c6Sgjelinek 		goto done;
789039935be5Sgjelinek 	}
7891ee519a1fSgjelinek 
7892ee519a1fSgjelinek 	if ((dirp = opendir(PKG_PATH)) == NULL) {
7893*6cfd72c6Sgjelinek 		res = Z_NOMEM;
7894*6cfd72c6Sgjelinek 		goto done;
7895ee519a1fSgjelinek 	}
7896ee519a1fSgjelinek 
7897ee519a1fSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
7898ee519a1fSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
7899ee519a1fSgjelinek 		    strcmp(dp->d_name, "..") == 0)
7900ee519a1fSgjelinek 			continue;
7901ee519a1fSgjelinek 
7902ee519a1fSgjelinek 		(void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo",
7903ee519a1fSgjelinek 		    PKG_PATH, dp->d_name);
7904ee519a1fSgjelinek 
7905ee519a1fSgjelinek 		if (stat(pkginfo, &buf) == -1 || !S_ISREG(buf.st_mode))
7906ee519a1fSgjelinek 			continue;
7907ee519a1fSgjelinek 
7908ee519a1fSgjelinek 		if (get_pkginfo(pkginfo, &info) != 0) {
7909ee519a1fSgjelinek 			res = Z_NOMEM;
7910ee519a1fSgjelinek 			break;
7911ee519a1fSgjelinek 		}
7912ee519a1fSgjelinek 
7913ee519a1fSgjelinek 		if (!info.zpi_this_zone &&
7914ee519a1fSgjelinek 		    (info.zpi_all_zones ||
7915*6cfd72c6Sgjelinek 		    dir_pkg(dp->d_name, pkgs, pkg_cnt)) &&
7916*6cfd72c6Sgjelinek 		    !pkg_in_manifest(saw_pkgs, dp->d_name, pkgs_pool)) {
7917*6cfd72c6Sgjelinek 			/*
7918*6cfd72c6Sgjelinek 			 * Add dependents first so any patches will get
7919*6cfd72c6Sgjelinek 			 * associated with the right pkg in the xml file.
7920*6cfd72c6Sgjelinek 			 */
7921*6cfd72c6Sgjelinek 			if ((res = add_dependents(handle, dp->d_name,
7922*6cfd72c6Sgjelinek 			    patches_pool, obs_patches, list_pool, saw_pkgs,
7923*6cfd72c6Sgjelinek 			    pkgs_pool)) == Z_OK &&
7924*6cfd72c6Sgjelinek 			    (res = add_pkg(handle, dp->d_name,
7925ee519a1fSgjelinek 			    info.zpi_version)) == Z_OK) {
7926ee519a1fSgjelinek 				if (info.zpi_patch_cnt > 0)
792739935be5Sgjelinek 					res = add_patches(handle, &info,
7928*6cfd72c6Sgjelinek 					    patches_pool, obs_patches,
7929*6cfd72c6Sgjelinek 					    list_pool);
7930ee519a1fSgjelinek 			}
7931ee519a1fSgjelinek 		}
7932ee519a1fSgjelinek 
7933ee519a1fSgjelinek 		free_pkginfo(&info);
7934ee519a1fSgjelinek 
7935ee519a1fSgjelinek 		if (res != Z_OK)
7936ee519a1fSgjelinek 			break;
7937ee519a1fSgjelinek 	}
7938ee519a1fSgjelinek 
7939ee519a1fSgjelinek 	(void) closedir(dirp);
7940ee519a1fSgjelinek 
7941*6cfd72c6Sgjelinek done:
7942*6cfd72c6Sgjelinek 	pkg_avl_delete(saw_pkgs);
7943*6cfd72c6Sgjelinek 	patch_avl_delete(obs_patches);
7944*6cfd72c6Sgjelinek 	if (pkgs_pool != NULL)
7945*6cfd72c6Sgjelinek 		uu_avl_pool_destroy(pkgs_pool);
7946*6cfd72c6Sgjelinek 	if (patches_pool != NULL)
7947*6cfd72c6Sgjelinek 		uu_avl_pool_destroy(patches_pool);
7948*6cfd72c6Sgjelinek 	if (list_pool != NULL)
7949*6cfd72c6Sgjelinek 		uu_list_pool_destroy(list_pool);
7950ee519a1fSgjelinek 	free_ipd_pkgs(pkgs, pkg_cnt);
7951ee519a1fSgjelinek 
7952ee519a1fSgjelinek 	if (res == Z_OK)
7953ee519a1fSgjelinek 		handle->zone_dh_sw_inv = B_TRUE;
7954ee519a1fSgjelinek 
7955ee519a1fSgjelinek 	return (res);
7956ee519a1fSgjelinek }
7957ee519a1fSgjelinek 
7958ee519a1fSgjelinek /*
7959ee519a1fSgjelinek  * zonecfg_devwalk call-back function used during detach to generate the
7960ee519a1fSgjelinek  * dev info in the manifest.
7961ee519a1fSgjelinek  */
7962ee519a1fSgjelinek static int
7963ee519a1fSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
7964ee519a1fSgjelinek     const char *acl, void *hdl)
7965ee519a1fSgjelinek {
7966ee519a1fSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
7967ee519a1fSgjelinek 	xmlNodePtr newnode;
7968ee519a1fSgjelinek 	xmlNodePtr cur;
7969ee519a1fSgjelinek 	int err;
7970ee519a1fSgjelinek 	char buf[128];
7971ee519a1fSgjelinek 
7972ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
7973ee519a1fSgjelinek 		return (err);
7974ee519a1fSgjelinek 
7975ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
7976ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
7977ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
7978ee519a1fSgjelinek 		return (err);
7979ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
7980ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
7981ee519a1fSgjelinek 		return (err);
7982ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
7983ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
7984ee519a1fSgjelinek 		return (err);
7985ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
7986ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
7987ee519a1fSgjelinek 		return (err);
7988ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
7989ee519a1fSgjelinek 		return (err);
7990ee519a1fSgjelinek 	return (Z_OK);
7991ee519a1fSgjelinek }
7992ee519a1fSgjelinek 
7993ee519a1fSgjelinek /*
7994ee519a1fSgjelinek  * Get the information required to support detaching a zone.  This is
7995ee519a1fSgjelinek  * called on the source system when detaching (the detaching parameter should
7996ee519a1fSgjelinek  * be set to true) and on the destination system before attaching (the
7997ee519a1fSgjelinek  * detaching parameter should be false).
7998ee519a1fSgjelinek  *
7999ee519a1fSgjelinek  * For native Solaris zones, the detach/attach process involves validating
8000ee519a1fSgjelinek  * that the software on the global zone can support the zone when we attach.
8001ee519a1fSgjelinek  * To do this we take a software inventory of the global zone.  We also
8002ee519a1fSgjelinek  * have to keep track of the device configuration so that we can properly
8003ee519a1fSgjelinek  * recreate it on the destination.
8004ee519a1fSgjelinek  */
8005ee519a1fSgjelinek int
8006ee519a1fSgjelinek zonecfg_get_detach_info(zone_dochandle_t handle, boolean_t detaching)
8007ee519a1fSgjelinek {
8008ee519a1fSgjelinek 	int		res;
8009ee519a1fSgjelinek 
8010ee519a1fSgjelinek 	if ((res = zonecfg_sw_inventory(handle)) != Z_OK)
8011ee519a1fSgjelinek 		return (res);
8012ee519a1fSgjelinek 
8013ee519a1fSgjelinek 	if (detaching)
8014ee519a1fSgjelinek 		res = zonecfg_devwalk(handle, get_detach_dev_entry, handle);
8015ee519a1fSgjelinek 
8016ee519a1fSgjelinek 	return (res);
8017ee519a1fSgjelinek }
8018