xref: /titanic_50/usr/src/lib/libzonecfg/common/libzonecfg.c (revision 39935be5449ce070c6316daad008f3c288c687d4)
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 /*
23f4b3ec61Sdh155122  * Copyright 2007 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>
419acbbeafSnn35248 #include <sys/acl.h>
429acbbeafSnn35248 #include <sys/stat.h>
439acbbeafSnn35248 #include <sys/brand.h>
447c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
457c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
46cf8f45c7Sdstaff #include <sys/nvpair.h>
479acbbeafSnn35248 #include <sys/types.h>
48f4b3ec61Sdh155122 #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>
54*39935be5Sgjelinek #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>
659acbbeafSnn35248 #include <libbrand.h>
66ee519a1fSgjelinek 
677c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
687c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	"/zonecfg.XXXXXX"
71cf8f45c7Sdstaff #define	ZONE_CB_RETRY_COUNT		10
72cf8f45c7Sdstaff #define	ZONE_EVENT_PING_SUBCLASS	"ping"
73cf8f45c7Sdstaff #define	ZONE_EVENT_PING_PUBLISHER	"solaris"
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */
767c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ATTR		(const xmlChar *) "attr"
777c478bd9Sstevel@tonic-gate #define	DTD_ELEM_COMMENT	(const xmlChar *) "comment"
787c478bd9Sstevel@tonic-gate #define	DTD_ELEM_DEVICE		(const xmlChar *) "device"
797c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FS		(const xmlChar *) "filesystem"
807c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FSOPTION	(const xmlChar *) "fsoption"
817c478bd9Sstevel@tonic-gate #define	DTD_ELEM_IPD		(const xmlChar *) "inherited-pkg-dir"
827c478bd9Sstevel@tonic-gate #define	DTD_ELEM_NET		(const xmlChar *) "network"
837c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTL		(const xmlChar *) "rctl"
847c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTLVALUE	(const xmlChar *) "rctl-value"
857c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ZONE		(const xmlChar *) "zone"
86fa9e4066Sahrens #define	DTD_ELEM_DATASET	(const xmlChar *) "dataset"
870209230bSgjelinek #define	DTD_ELEM_TMPPOOL	(const xmlChar *) "tmp_pool"
880209230bSgjelinek #define	DTD_ELEM_PSET		(const xmlChar *) "pset"
890209230bSgjelinek #define	DTD_ELEM_MCAP		(const xmlChar *) "mcap"
90ee519a1fSgjelinek #define	DTD_ELEM_PACKAGE	(const xmlChar *) "package"
91ee519a1fSgjelinek #define	DTD_ELEM_PATCH		(const xmlChar *) "patch"
92ee519a1fSgjelinek #define	DTD_ELEM_DEV_PERM	(const xmlChar *) "dev-perm"
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ACTION		(const xmlChar *) "action"
957c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ADDRESS	(const xmlChar *) "address"
967c478bd9Sstevel@tonic-gate #define	DTD_ATTR_AUTOBOOT	(const xmlChar *) "autoboot"
97f4b3ec61Sdh155122 #define	DTD_ATTR_IPTYPE		(const xmlChar *) "ip-type"
987c478bd9Sstevel@tonic-gate #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
997c478bd9Sstevel@tonic-gate #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
100ffbafc53Scomay #define	DTD_ATTR_LIMITPRIV	(const xmlChar *) "limitpriv"
1013f2f09c1Sdp #define	DTD_ATTR_BOOTARGS	(const xmlChar *) "bootargs"
1020209230bSgjelinek #define	DTD_ATTR_SCHED		(const xmlChar *) "scheduling-class"
1037c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
1047c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
1057c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
1067c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
1077c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
1087c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
1097c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
1107c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
1117c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
1127c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
1130209230bSgjelinek #define	DTD_ATTR_NCPU_MIN	(const xmlChar *) "ncpu_min"
1140209230bSgjelinek #define	DTD_ATTR_NCPU_MAX	(const xmlChar *) "ncpu_max"
1150209230bSgjelinek #define	DTD_ATTR_IMPORTANCE	(const xmlChar *) "importance"
1160209230bSgjelinek #define	DTD_ATTR_PHYSCAP	(const xmlChar *) "physcap"
117ee519a1fSgjelinek #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
118ee519a1fSgjelinek #define	DTD_ATTR_ID		(const xmlChar *) "id"
119ee519a1fSgjelinek #define	DTD_ATTR_UID		(const xmlChar *) "uid"
120ee519a1fSgjelinek #define	DTD_ATTR_GID		(const xmlChar *) "gid"
121ee519a1fSgjelinek #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
122ee519a1fSgjelinek #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
1239acbbeafSnn35248 #define	DTD_ATTR_BRAND		(const xmlChar *) "brand"
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
1267c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
1277c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
1287c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
1297c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
1307c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
1317c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
1327c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
1337c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
1347c478bd9Sstevel@tonic-gate 
135a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
136a1be23daSdp 
137ee519a1fSgjelinek #define	DETACHED	"SUNWdetached.xml"
138ee519a1fSgjelinek #define	ATTACH_FORCED	"SUNWattached.xml"
139ee519a1fSgjelinek #define	PKG_PATH	"/var/sadm/pkg"
140ee519a1fSgjelinek #define	CONTENTS_FILE	"/var/sadm/install/contents"
14145916cd2Sjpk #define	SUNW_PKG_ALL_ZONES	"SUNW_PKG_ALLZONES=true\n"
14245916cd2Sjpk #define	SUNW_PKG_THIS_ZONE	"SUNW_PKG_THISZONE=true\n"
143ee519a1fSgjelinek #define	VERSION		"VERSION="
144ee519a1fSgjelinek #define	PATCHLIST	"PATCHLIST="
145ee519a1fSgjelinek #define	PATCHINFO	"PATCH_INFO_"
146ee519a1fSgjelinek #define	PKGINFO_RD_LEN	128
147ee519a1fSgjelinek 
1480209230bSgjelinek #define	TMP_POOL_NAME	"SUNWtmp_%s"
1490209230bSgjelinek #define	MAX_TMP_POOL_NAME	(ZONENAME_MAX + 9)
1500209230bSgjelinek #define	RCAP_SERVICE	"system/rcap:default"
1510209230bSgjelinek #define	POOLD_SERVICE	"system/pools/dynamic:default"
1520209230bSgjelinek 
153*39935be5Sgjelinek enum zn_ipd_fs {ZONE_IPD, ZONE_FS};
154*39935be5Sgjelinek 
1550209230bSgjelinek /*
1560209230bSgjelinek  * rctl alias definitions
1570209230bSgjelinek  *
1580209230bSgjelinek  * This holds the alias, the full rctl name, the default priv value, action
1590209230bSgjelinek  * and lower limit.  The functions that handle rctl aliases step through
1600209230bSgjelinek  * this table, matching on the alias, and using the full values for setting
1610209230bSgjelinek  * the rctl entry as well the limit for validation.
1620209230bSgjelinek  */
1630209230bSgjelinek static struct alias {
1640209230bSgjelinek 	char *shortname;
1650209230bSgjelinek 	char *realname;
1660209230bSgjelinek 	char *priv;
1670209230bSgjelinek 	char *action;
1680209230bSgjelinek 	uint64_t low_limit;
1690209230bSgjelinek } aliases[] = {
1700209230bSgjelinek 	{ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100},
1710209230bSgjelinek 	{ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0},
1720209230bSgjelinek 	{ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0},
1730209230bSgjelinek 	{ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0},
1740209230bSgjelinek 	{ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0},
1750209230bSgjelinek 	{ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0},
1760209230bSgjelinek 	{ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0},
1770209230bSgjelinek 	{ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0},
178c97ad5cdSakolb 	{ALIAS_CPUCAP, "zone.cpu-cap", "privileged", "deny", 0},
1790209230bSgjelinek 	{NULL, NULL, NULL, NULL, 0}
1800209230bSgjelinek };
1810209230bSgjelinek 
1820209230bSgjelinek /*
1830209230bSgjelinek  * Structure for applying rctls to a running zone.  It allows important
1840209230bSgjelinek  * process values to be passed together easily.
1850209230bSgjelinek  */
1860209230bSgjelinek typedef struct pr_info_handle {
1870209230bSgjelinek 	struct ps_prochandle *pr;
1880209230bSgjelinek 	pid_t pid;
1890209230bSgjelinek } pr_info_handle_t;
1900209230bSgjelinek 
1917c478bd9Sstevel@tonic-gate struct zone_dochandle {
1927c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1937c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1947c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1957c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
196087719fdSdp 	boolean_t	zone_dh_newzone;
197087719fdSdp 	boolean_t	zone_dh_snapshot;
198ee519a1fSgjelinek 	boolean_t	zone_dh_sw_inv;
199087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
2007c478bd9Sstevel@tonic-gate };
2017c478bd9Sstevel@tonic-gate 
202cf8f45c7Sdstaff struct znotify {
203cf8f45c7Sdstaff 	void * zn_private;
204cf8f45c7Sdstaff 	evchan_t *zn_eventchan;
205cf8f45c7Sdstaff 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
206cf8f45c7Sdstaff 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
207cf8f45c7Sdstaff 	pthread_mutex_t zn_mutex;
208cf8f45c7Sdstaff 	pthread_cond_t zn_cond;
209cf8f45c7Sdstaff 	pthread_mutex_t zn_bigmutex;
210cf8f45c7Sdstaff 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
211cf8f45c7Sdstaff 	    ZN_PING_RECEIVED} zn_state;
212cf8f45c7Sdstaff 	char zn_subscriber_id[MAX_SUBID_LEN];
213cf8f45c7Sdstaff 	volatile boolean_t zn_failed;
214cf8f45c7Sdstaff 	int zn_failure_count;
215cf8f45c7Sdstaff };
216cf8f45c7Sdstaff 
217ee519a1fSgjelinek struct zone_pkginfo {
218ee519a1fSgjelinek 	boolean_t	zpi_all_zones;
219ee519a1fSgjelinek 	boolean_t	zpi_this_zone;
220ee519a1fSgjelinek 	int		zpi_patch_cnt;
221ee519a1fSgjelinek 	char		*zpi_version;
222ee519a1fSgjelinek 	char		**zpi_patchinfo;
223ee519a1fSgjelinek };
224ee519a1fSgjelinek 
225*39935be5Sgjelinek typedef struct {
226*39935be5Sgjelinek 	uu_avl_node_t	patch_node;
227*39935be5Sgjelinek 	char		*patch_num;
228*39935be5Sgjelinek 	char		*patch_vers;
229*39935be5Sgjelinek } patch_node_t;
230*39935be5Sgjelinek 
231*39935be5Sgjelinek typedef struct {
232*39935be5Sgjelinek 	uu_avl_t	*obs_patches_avl;
233*39935be5Sgjelinek 	zone_dochandle_t handle;
234*39935be5Sgjelinek 	int		res;
235*39935be5Sgjelinek } patch_parms_t;
236*39935be5Sgjelinek 
237108322fbScarlsonj char *zonecfg_root = "";
238108322fbScarlsonj 
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
2417c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
2427c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
2437c478bd9Sstevel@tonic-gate  * values from this set.
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
247108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
248108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
249108322fbScarlsonj  * configuration inside mounted alternate boot environments.
250108322fbScarlsonj  */
251108322fbScarlsonj void
252108322fbScarlsonj zonecfg_set_root(const char *rootpath)
253108322fbScarlsonj {
254108322fbScarlsonj 	if (*zonecfg_root != '\0')
255108322fbScarlsonj 		free(zonecfg_root);
256108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
257108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
258108322fbScarlsonj 		zonecfg_root = "";
259108322fbScarlsonj }
260108322fbScarlsonj 
261108322fbScarlsonj const char *
262108322fbScarlsonj zonecfg_get_root(void)
263108322fbScarlsonj {
264108322fbScarlsonj 	return (zonecfg_root);
265108322fbScarlsonj }
266108322fbScarlsonj 
267108322fbScarlsonj boolean_t
268108322fbScarlsonj zonecfg_in_alt_root(void)
269108322fbScarlsonj {
270108322fbScarlsonj 	return (*zonecfg_root != '\0');
271108322fbScarlsonj }
272108322fbScarlsonj 
273108322fbScarlsonj /*
2747c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
2757c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate 
278108322fbScarlsonj static boolean_t
2797c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
2807c478bd9Sstevel@tonic-gate {
281108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
282108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
285108322fbScarlsonj static boolean_t
286108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
2877c478bd9Sstevel@tonic-gate {
288108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
289108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2937c478bd9Sstevel@tonic-gate static void
2947c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
2957c478bd9Sstevel@tonic-gate {
2967c478bd9Sstevel@tonic-gate 	/*
2977c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
2987c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
2997c478bd9Sstevel@tonic-gate 	 */
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate zone_dochandle_t
3037c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
3047c478bd9Sstevel@tonic-gate {
305087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
3067c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
3077c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
3087c478bd9Sstevel@tonic-gate 		return (NULL);
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
3127c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
3137c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3147c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
3157c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
3167c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
3177c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	return (handle);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate int
3237c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
3267c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3277c478bd9Sstevel@tonic-gate 	return (Z_OK);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate void
3317c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
3347c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
3357c478bd9Sstevel@tonic-gate 	if (handle != NULL)
3367c478bd9Sstevel@tonic-gate 		free(handle);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate static int
3407c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
3437c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
3447c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
3457c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
3467c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
3477c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 	return (Z_OK);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate int
353087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
356087719fdSdp 	struct zoneent ze;
357087719fdSdp 	int err, state_err;
358087719fdSdp 	zone_state_t state;
3597c478bd9Sstevel@tonic-gate 
360108322fbScarlsonj 	if (!config_file_path(zonename, path))
361108322fbScarlsonj 		return (Z_MISC_FS);
362087719fdSdp 
363087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
364087719fdSdp 	err = access(path, W_OK);
365087719fdSdp 
366087719fdSdp 	/*
367087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
368087719fdSdp 	 * such zone exists.
369087719fdSdp 	 */
370087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
371087719fdSdp 		return (Z_NO_ZONE);
372087719fdSdp 
373087719fdSdp 	/*
374087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
375087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
376087719fdSdp 	 * in which case we plow on.
377087719fdSdp 	 */
378087719fdSdp 	if (err == -1 && errno != ENOENT) {
379087719fdSdp 		if (errno == EACCES)
380087719fdSdp 			return (Z_ACCES);
381087719fdSdp 		else if (!force)
382087719fdSdp 			return (Z_MISC_FS);
383087719fdSdp 	}
384087719fdSdp 
385087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
386087719fdSdp 		return (Z_BAD_ZONE_STATE);
387087719fdSdp 
388087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
389087719fdSdp 		return (Z_BAD_ZONE_STATE);
390087719fdSdp 
391087719fdSdp 	/*
392087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
393087719fdSdp 	 * will fail only if we've had some more severe problem.
394087719fdSdp 	 */
395087719fdSdp 	bzero(&ze, sizeof (ze));
396087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
397087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
398087719fdSdp 		if (!force)
399087719fdSdp 			return (err);
400087719fdSdp 
401087719fdSdp 	err = zonecfg_destroy_impl(path);
402087719fdSdp 
403087719fdSdp 	/*
404087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
405087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
406087719fdSdp 	 */
407087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
408087719fdSdp 		return (Z_OK);
409087719fdSdp 	return (err);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate int
413108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4167c478bd9Sstevel@tonic-gate 
417108322fbScarlsonj 	if (!snap_file_path(zonename, path))
418108322fbScarlsonj 		return (Z_MISC_FS);
4197c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate static int
423a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
4267c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
4277c478bd9Sstevel@tonic-gate 
428a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
429a1be23daSdp 
430a1be23daSdp 	if (*root == NULL)
4317c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
432a1be23daSdp 
433a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
434a1be23daSdp 		return (Z_WRONG_DOC_TYPE);
435a1be23daSdp 
436a1be23daSdp 	return (Z_OK);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
439a1be23daSdp static int
440a1be23daSdp operation_prep(zone_dochandle_t handle)
441a1be23daSdp {
442a1be23daSdp 	xmlNodePtr root;
443a1be23daSdp 	int err;
444a1be23daSdp 
445a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
446a1be23daSdp 		return (err);
447a1be23daSdp 
448a1be23daSdp 	handle->zone_dh_cur = root;
449a1be23daSdp 	handle->zone_dh_top = root;
450a1be23daSdp 	return (Z_OK);
4517c478bd9Sstevel@tonic-gate }
452a1be23daSdp 
453a1be23daSdp static int
454ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
455ffbafc53Scomay {
456ffbafc53Scomay 	xmlChar *property;
457ffbafc53Scomay 	size_t srcsize;
458ffbafc53Scomay 
459ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
460ffbafc53Scomay 		return (Z_BAD_PROPERTY);
461ffbafc53Scomay 	srcsize = strlcpy(dst, (char *)property, dstsize);
462ffbafc53Scomay 	xmlFree(property);
463ffbafc53Scomay 	if (srcsize >= dstsize)
464ffbafc53Scomay 		return (Z_TOO_BIG);
465ffbafc53Scomay 	return (Z_OK);
466ffbafc53Scomay }
467ffbafc53Scomay 
468ffbafc53Scomay static int
469ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
470ffbafc53Scomay {
471ffbafc53Scomay 	xmlChar *property;
472ffbafc53Scomay 
473ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
474ffbafc53Scomay 		return (Z_BAD_PROPERTY);
475ffbafc53Scomay 	if ((*dst = strdup((char *)property)) == NULL) {
476ffbafc53Scomay 		xmlFree(property);
477ffbafc53Scomay 		return (Z_NOMEM);
478ffbafc53Scomay 	}
479ffbafc53Scomay 	xmlFree(property);
480ffbafc53Scomay 	return (Z_OK);
481ffbafc53Scomay }
482ffbafc53Scomay 
483ffbafc53Scomay static int
484a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
485a1be23daSdp     char *propval, size_t propsize)
486a1be23daSdp {
487a1be23daSdp 	xmlNodePtr root;
488a1be23daSdp 	int err;
489a1be23daSdp 
490a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
491a1be23daSdp 		return (err);
492a1be23daSdp 
493ffbafc53Scomay 	return (fetchprop(root, propname, propval, propsize));
494ffbafc53Scomay }
495ffbafc53Scomay 
496ffbafc53Scomay static int
497ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
498ffbafc53Scomay     char **propval)
499ffbafc53Scomay {
500ffbafc53Scomay 	xmlNodePtr root;
501ffbafc53Scomay 	int err;
502ffbafc53Scomay 
503ffbafc53Scomay 	if ((err = getroot(handle, &root)) != 0)
504ffbafc53Scomay 		return (err);
505ffbafc53Scomay 
506ffbafc53Scomay 	return (fetch_alloc_prop(root, propname, propval));
507a1be23daSdp }
508a1be23daSdp 
509a1be23daSdp static int
510108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
511108322fbScarlsonj     const char *propval)
512a1be23daSdp {
513a1be23daSdp 	int err;
514a1be23daSdp 	xmlNodePtr root;
515a1be23daSdp 
516a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
517a1be23daSdp 		return (err);
518a1be23daSdp 
5190209230bSgjelinek 	/*
5200209230bSgjelinek 	 * If we get a null propval remove the property (ignore return since it
5210209230bSgjelinek 	 * may not be set to begin with).
5220209230bSgjelinek 	 */
5230209230bSgjelinek 	if (propval == NULL) {
5240209230bSgjelinek 		(void) xmlUnsetProp(root, propname);
5250209230bSgjelinek 	} else {
5260209230bSgjelinek 		if (xmlSetProp(root, propname, (const xmlChar *) propval)
5270209230bSgjelinek 		    == NULL)
528a1be23daSdp 			return (Z_INVAL);
5290209230bSgjelinek 	}
5307c478bd9Sstevel@tonic-gate 	return (Z_OK);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
533087719fdSdp static void
534087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
535087719fdSdp {
536087719fdSdp 	xmlNodePtr node;
537087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
538087719fdSdp 
539087719fdSdp 	if (node != NULL)
540087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
541087719fdSdp }
542087719fdSdp 
543087719fdSdp static void
544087719fdSdp stripcomments(zone_dochandle_t handle)
545087719fdSdp {
546087719fdSdp 	xmlDocPtr top;
547087719fdSdp 	xmlNodePtr child, next;
548087719fdSdp 
549087719fdSdp 	top = handle->zone_dh_doc;
550087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
551087719fdSdp 		next = child->next;
552087719fdSdp 		if (child->name == NULL)
553087719fdSdp 			continue;
554087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
555087719fdSdp 			next = child->next;
556087719fdSdp 			xmlUnlinkNode(child);
557087719fdSdp 			xmlFreeNode(child);
558087719fdSdp 		}
559087719fdSdp 	}
560087719fdSdp }
561087719fdSdp 
562ee519a1fSgjelinek static void
563ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle)
564ee519a1fSgjelinek {
565ee519a1fSgjelinek 	xmlNodePtr root, child, next;
566ee519a1fSgjelinek 
567ee519a1fSgjelinek 	root = xmlDocGetRootElement(handle->zone_dh_doc);
568ee519a1fSgjelinek 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
569ee519a1fSgjelinek 		next = child->next;
570ee519a1fSgjelinek 		if (child->name == NULL)
571ee519a1fSgjelinek 			continue;
572ee519a1fSgjelinek 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
573ee519a1fSgjelinek 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
574ee519a1fSgjelinek 			next = child->next;
575ee519a1fSgjelinek 			xmlUnlinkNode(child);
576ee519a1fSgjelinek 			xmlFreeNode(child);
577ee519a1fSgjelinek 		}
578ee519a1fSgjelinek 	}
579ee519a1fSgjelinek }
580ee519a1fSgjelinek 
5817c478bd9Sstevel@tonic-gate static int
582108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
583108322fbScarlsonj     zone_dochandle_t handle)
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
5867c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5877c478bd9Sstevel@tonic-gate 	int valid;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
5907c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5919acbbeafSnn35248 
5927c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
5937c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
5947c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
5957c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
5967c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
5997c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
6007c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
6017c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
6027c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
6037c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
6047c478bd9Sstevel@tonic-gate 	if (valid == 0)
6057c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
606087719fdSdp 
6077c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
608087719fdSdp 	stripcomments(handle);
6097c478bd9Sstevel@tonic-gate 	return (Z_OK);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate int
613108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6167c478bd9Sstevel@tonic-gate 
617108322fbScarlsonj 	if (!config_file_path(zonename, path))
618108322fbScarlsonj 		return (Z_MISC_FS);
619087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
620087719fdSdp 
6217c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate int
625ee519a1fSgjelinek zonecfg_get_attach_handle(const char *path, const char *zonename,
626ee519a1fSgjelinek     boolean_t preserve_sw, zone_dochandle_t handle)
627ee519a1fSgjelinek {
628ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
629ee519a1fSgjelinek 	int		err;
630ee519a1fSgjelinek 	struct stat	buf;
631ee519a1fSgjelinek 
632ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
633ee519a1fSgjelinek 	    sizeof (migpath))
634ee519a1fSgjelinek 		return (Z_NOMEM);
635ee519a1fSgjelinek 
636ee519a1fSgjelinek 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
637ee519a1fSgjelinek 		return (Z_NO_ZONE);
638ee519a1fSgjelinek 
639ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
640ee519a1fSgjelinek 	    sizeof (migpath))
641ee519a1fSgjelinek 		return (Z_NOMEM);
642ee519a1fSgjelinek 
643ee519a1fSgjelinek 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
644ee519a1fSgjelinek 		return (err);
645ee519a1fSgjelinek 
646ee519a1fSgjelinek 	if (!preserve_sw)
647ee519a1fSgjelinek 		strip_sw_inv(handle);
648ee519a1fSgjelinek 
649ee519a1fSgjelinek 	handle->zone_dh_newzone = B_TRUE;
650ee519a1fSgjelinek 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
651ee519a1fSgjelinek 		return (err);
652ee519a1fSgjelinek 
653ee519a1fSgjelinek 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
654ee519a1fSgjelinek }
655ee519a1fSgjelinek 
656ee519a1fSgjelinek int
657108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6607c478bd9Sstevel@tonic-gate 
661108322fbScarlsonj 	if (!snap_file_path(zonename, path))
662108322fbScarlsonj 		return (Z_MISC_FS);
663087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
6647c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate int
668108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
669087719fdSdp     zone_dochandle_t handle)
670087719fdSdp {
671087719fdSdp 	char path[MAXPATHLEN];
672087719fdSdp 	int err;
673087719fdSdp 
674108322fbScarlsonj 	if (!config_file_path(template, path))
675108322fbScarlsonj 		return (Z_MISC_FS);
676087719fdSdp 
677087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
678087719fdSdp 		return (err);
679087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
680087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
681087719fdSdp }
682087719fdSdp 
6839acbbeafSnn35248 int
6849acbbeafSnn35248 zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle)
6859acbbeafSnn35248 {
6869acbbeafSnn35248 	struct stat buf;
6879acbbeafSnn35248 	int err;
6889acbbeafSnn35248 
6899acbbeafSnn35248 	if (stat(path, &buf) == -1)
6909acbbeafSnn35248 		return (Z_MISC_FS);
6919acbbeafSnn35248 
6929acbbeafSnn35248 	if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK)
6939acbbeafSnn35248 		return (err);
6949acbbeafSnn35248 	handle->zone_dh_newzone = B_TRUE;
6959acbbeafSnn35248 	return (Z_OK);
6969acbbeafSnn35248 }
6979acbbeafSnn35248 
6988cd327d5Sgjelinek /*
6998cd327d5Sgjelinek  * Initialize two handles from the manifest read on fd.  The rem_handle
7008cd327d5Sgjelinek  * is initialized from the input file, including the sw inventory.  The
7018cd327d5Sgjelinek  * local_handle is initialized with the same zone configuration but with
7028cd327d5Sgjelinek  * no sw inventory.
7038cd327d5Sgjelinek  */
7048cd327d5Sgjelinek int
7058cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
7068cd327d5Sgjelinek     zone_dochandle_t rem_handle)
7078cd327d5Sgjelinek {
7088cd327d5Sgjelinek 	xmlValidCtxtPtr cvp;
7098cd327d5Sgjelinek 	int valid;
7108cd327d5Sgjelinek 
7118cd327d5Sgjelinek 	/* load the manifest into the handle for the remote system */
7128cd327d5Sgjelinek 	if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
7138cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7148cd327d5Sgjelinek 	}
7158cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7168cd327d5Sgjelinek 		return (Z_NOMEM);
7178cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7188cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7198cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc);
7208cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7218cd327d5Sgjelinek 	if (valid == 0)
7228cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7238cd327d5Sgjelinek 
7248cd327d5Sgjelinek 	/* delete any comments such as inherited Sun copyright / ident str */
7258cd327d5Sgjelinek 	stripcomments(rem_handle);
7268cd327d5Sgjelinek 
7278cd327d5Sgjelinek 	rem_handle->zone_dh_newzone = B_TRUE;
7288cd327d5Sgjelinek 	rem_handle->zone_dh_sw_inv = B_TRUE;
7298cd327d5Sgjelinek 
7308cd327d5Sgjelinek 	/*
7318cd327d5Sgjelinek 	 * Now use the remote system handle to generate a local system handle
7328cd327d5Sgjelinek 	 * with an identical zones configuration but no sw inventory.
7338cd327d5Sgjelinek 	 */
7348cd327d5Sgjelinek 	if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
7358cd327d5Sgjelinek 	    1)) == NULL) {
7368cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7378cd327d5Sgjelinek 	}
7388cd327d5Sgjelinek 
7398cd327d5Sgjelinek 	/*
7408cd327d5Sgjelinek 	 * We need to re-run xmlValidateDocument on local_handle to properly
7418cd327d5Sgjelinek 	 * update the in-core representation of the configuration.
7428cd327d5Sgjelinek 	 */
7438cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7448cd327d5Sgjelinek 		return (Z_NOMEM);
7458cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7468cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7478cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
7488cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7498cd327d5Sgjelinek 	if (valid == 0)
7508cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7518cd327d5Sgjelinek 
7528cd327d5Sgjelinek 	strip_sw_inv(local_handle);
7538cd327d5Sgjelinek 
7548cd327d5Sgjelinek 	local_handle->zone_dh_newzone = B_TRUE;
7558cd327d5Sgjelinek 	local_handle->zone_dh_sw_inv = B_FALSE;
7568cd327d5Sgjelinek 
7578cd327d5Sgjelinek 	return (Z_OK);
7588cd327d5Sgjelinek }
7598cd327d5Sgjelinek 
760087719fdSdp static boolean_t
761087719fdSdp is_renaming(zone_dochandle_t handle)
762087719fdSdp {
763087719fdSdp 	if (handle->zone_dh_newzone)
764087719fdSdp 		return (B_FALSE);
765087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
766087719fdSdp 		return (B_TRUE);
767087719fdSdp 	return (B_FALSE);
768087719fdSdp }
769087719fdSdp 
770087719fdSdp static boolean_t
771087719fdSdp is_new(zone_dochandle_t handle)
772087719fdSdp {
773087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
774087719fdSdp }
775087719fdSdp 
776087719fdSdp static boolean_t
777087719fdSdp is_snapshot(zone_dochandle_t handle)
778087719fdSdp {
779087719fdSdp 	return (handle->zone_dh_snapshot);
780087719fdSdp }
781087719fdSdp 
782087719fdSdp /*
783087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
784087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
785087719fdSdp  * safety if this routine had to change the app locale on the fly.
786087719fdSdp  */
787087719fdSdp int
788108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
789087719fdSdp {
790087719fdSdp 	int i;
791087719fdSdp 
792087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
793087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
794087719fdSdp 
795087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
796087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
797087719fdSdp 
798087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
799087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
800087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
801087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
802087719fdSdp 
803087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
804087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
805087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
806087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
807087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
808087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
809087719fdSdp 	}
810087719fdSdp 
811087719fdSdp 	return (Z_OK);
812087719fdSdp }
813087719fdSdp 
814087719fdSdp /*
815087719fdSdp  * Changing the zone name requires us to track both the old and new
816087719fdSdp  * name of the zone until commit time.
817087719fdSdp  */
818087719fdSdp int
8197c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
8207c478bd9Sstevel@tonic-gate {
821a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate 
824a1be23daSdp int
825a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
826a1be23daSdp {
827087719fdSdp 	zone_state_t state;
828087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
829087719fdSdp 	int err;
830087719fdSdp 
831087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
832087719fdSdp 	    sizeof (curname))) != Z_OK)
833087719fdSdp 		return (err);
834087719fdSdp 
835087719fdSdp 	if (strcmp(name, curname) == 0)
836087719fdSdp 		return (Z_OK);
837087719fdSdp 
838087719fdSdp 	/*
839087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
840087719fdSdp 	 */
841087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
842087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
843087719fdSdp 
844087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
845087719fdSdp 		return (err);
846087719fdSdp 
847087719fdSdp 	/*
848087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
849087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
850087719fdSdp 	 * name as "in use," so special case here.
851087719fdSdp 	 */
852087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
853087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
854087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
855087719fdSdp 		return (err);
856087719fdSdp 	}
857087719fdSdp 
858087719fdSdp 	/* Check to see if new name chosen is already in use */
859087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
860087719fdSdp 		return (Z_NAME_IN_USE);
861087719fdSdp 
862087719fdSdp 	/*
863087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
864087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
865087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
866087719fdSdp 	 */
867087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
868087719fdSdp 	    sizeof (old_delname));
869087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
870087719fdSdp 		/*
871087719fdSdp 		 * Name change is allowed only when the zone we're altering
872087719fdSdp 		 * is not ready or running.
873087719fdSdp 		 */
874087719fdSdp 		err = zone_get_state(curname, &state);
875087719fdSdp 		if (err == Z_OK) {
876087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
877087719fdSdp 				return (Z_BAD_ZONE_STATE);
878087719fdSdp 		} else if (err != Z_NO_ZONE) {
879087719fdSdp 			return (err);
880087719fdSdp 		}
881087719fdSdp 
882087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
883087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
884087719fdSdp 		assert(is_renaming(handle));
885087719fdSdp 	} else if (is_renaming(handle)) {
886087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
887087719fdSdp 		if (err == Z_OK) {
888087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
889087719fdSdp 				return (Z_BAD_ZONE_STATE);
890087719fdSdp 		} else if (err != Z_NO_ZONE) {
891087719fdSdp 			return (err);
892087719fdSdp 		}
893087719fdSdp 	}
894087719fdSdp 
895087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
896087719fdSdp 		/*
897087719fdSdp 		 * Restore the deletename to whatever it was at the
898087719fdSdp 		 * top of the routine, since we've had a failure.
899087719fdSdp 		 */
900087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
901087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
902087719fdSdp 		return (err);
903087719fdSdp 	}
904087719fdSdp 
905087719fdSdp 	return (Z_OK);
9067c478bd9Sstevel@tonic-gate }
907a1be23daSdp 
908a1be23daSdp int
909a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
910a1be23daSdp {
911108322fbScarlsonj 	size_t len;
912108322fbScarlsonj 
913108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
914108322fbScarlsonj 		return (Z_TOO_BIG);
915108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
916108322fbScarlsonj 	    pathsize - len));
917a1be23daSdp }
918a1be23daSdp 
919a1be23daSdp int
920a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
921a1be23daSdp {
922555afedfScarlsonj 	size_t len;
923555afedfScarlsonj 
924555afedfScarlsonj 	/*
925555afedfScarlsonj 	 * The user deals in absolute paths in the running global zone, but the
926555afedfScarlsonj 	 * internal configuration files deal with boot environment relative
927555afedfScarlsonj 	 * paths.  Strip out the alternate root when specified.
928555afedfScarlsonj 	 */
929555afedfScarlsonj 	len = strlen(zonecfg_root);
930555afedfScarlsonj 	if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
931555afedfScarlsonj 		return (Z_BAD_PROPERTY);
932555afedfScarlsonj 	zonepath += len;
933a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
934a1be23daSdp }
935a1be23daSdp 
936a1be23daSdp int
9379acbbeafSnn35248 zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
9389acbbeafSnn35248 {
9399acbbeafSnn35248 	int ret, sz;
9409acbbeafSnn35248 
9419acbbeafSnn35248 	ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
9429acbbeafSnn35248 
9439acbbeafSnn35248 	/* If the zone has no brand, it is native. */
9449acbbeafSnn35248 	if (ret == Z_OK && brand[0] == '\0') {
9459acbbeafSnn35248 		sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
9469acbbeafSnn35248 		if (sz >= brandsize)
9479acbbeafSnn35248 			ret = Z_TOO_BIG;
9489acbbeafSnn35248 		else
9499acbbeafSnn35248 			ret = Z_OK;
9509acbbeafSnn35248 	}
9519acbbeafSnn35248 
9529acbbeafSnn35248 	return (ret);
9539acbbeafSnn35248 }
9549acbbeafSnn35248 
9559acbbeafSnn35248 int
9569acbbeafSnn35248 zonecfg_set_brand(zone_dochandle_t handle, char *brand)
9579acbbeafSnn35248 {
9589acbbeafSnn35248 	return (setrootattr(handle, DTD_ATTR_BRAND, brand));
9599acbbeafSnn35248 }
9609acbbeafSnn35248 
9619acbbeafSnn35248 int
962a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
963a1be23daSdp {
964a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
965a1be23daSdp 	int ret;
966a1be23daSdp 
967a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
968a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
969a1be23daSdp 		return (ret);
970a1be23daSdp 
971a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
972a1be23daSdp 		*autoboot = B_TRUE;
973a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
974a1be23daSdp 		*autoboot = B_FALSE;
975a1be23daSdp 	else
976a1be23daSdp 		ret = Z_BAD_PROPERTY;
977a1be23daSdp 	return (ret);
978a1be23daSdp }
979a1be23daSdp 
980a1be23daSdp int
981a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
982a1be23daSdp {
983a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
984a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
985a1be23daSdp }
986a1be23daSdp 
987a1be23daSdp int
988a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
989a1be23daSdp {
990a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
991a1be23daSdp }
992a1be23daSdp 
993a1be23daSdp int
994a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
995a1be23daSdp {
996a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
997a1be23daSdp }
998a1be23daSdp 
999ffbafc53Scomay int
1000ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
1001ffbafc53Scomay {
1002ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
1003ffbafc53Scomay }
1004ffbafc53Scomay 
1005ffbafc53Scomay int
10063f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
1007ffbafc53Scomay {
10083f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
10093f2f09c1Sdp }
10103f2f09c1Sdp 
10113f2f09c1Sdp int
10123f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
10133f2f09c1Sdp {
10143f2f09c1Sdp 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
10153f2f09c1Sdp }
10163f2f09c1Sdp 
10173f2f09c1Sdp int
10183f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
10193f2f09c1Sdp {
10203f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
1021ffbafc53Scomay }
1022ffbafc53Scomay 
10230209230bSgjelinek int
10240209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
10250209230bSgjelinek {
10260209230bSgjelinek 	return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
10270209230bSgjelinek }
10280209230bSgjelinek 
10290209230bSgjelinek int
10300209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched)
10310209230bSgjelinek {
10320209230bSgjelinek 	return (setrootattr(handle, DTD_ATTR_SCHED, sched));
10330209230bSgjelinek }
10340209230bSgjelinek 
1035a1be23daSdp /*
1036a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
1037a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
1038a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
1039a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
1040087719fdSdp  *
1041087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
1042087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
1043087719fdSdp  * In general, the operation of this function should succeed or fail as
1044087719fdSdp  * a unit.
1045a1be23daSdp  */
1046a1be23daSdp int
1047a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
1048a1be23daSdp {
1049a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
1050a1be23daSdp 	struct zoneent ze;
1051a1be23daSdp 	int err;
1052087719fdSdp 	int opcode;
1053087719fdSdp 	char *zn;
1054087719fdSdp 
1055087719fdSdp 	bzero(&ze, sizeof (ze));
1056087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
1057a1be23daSdp 
1058a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
1059a1be23daSdp 		return (err);
1060087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
1061087719fdSdp 
1062a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
1063a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
1064a1be23daSdp 		return (err);
1065555afedfScarlsonj 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
1066555afedfScarlsonj 	    sizeof (ze.zone_path));
1067087719fdSdp 
1068087719fdSdp 	if (is_renaming(handle)) {
1069087719fdSdp 		opcode = PZE_MODIFY;
1070087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
1071087719fdSdp 		    sizeof (ze.zone_name));
1072087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
1073087719fdSdp 	} else if (is_new(handle)) {
1074087719fdSdp 		FILE *cookie;
1075087719fdSdp 		/*
1076087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
1077087719fdSdp 		 * since we might be forcibly overwriting an existing
1078087719fdSdp 		 * configuration with a new one (for example 'create -F'
1079087719fdSdp 		 * in zonecfg).
1080087719fdSdp 		 */
1081087719fdSdp 		opcode = PZE_ADD;
1082087719fdSdp 		cookie = setzoneent();
1083087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
1084087719fdSdp 			if (strcmp(zn, name) == 0) {
1085087719fdSdp 				opcode = PZE_MODIFY;
1086087719fdSdp 				free(zn);
1087087719fdSdp 				break;
1088087719fdSdp 			}
1089087719fdSdp 			free(zn);
1090087719fdSdp 		}
1091087719fdSdp 		endzoneent(cookie);
1092087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
1093087719fdSdp 	} else {
1094087719fdSdp 		opcode = PZE_MODIFY;
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
1097087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
1098087719fdSdp 		return (err);
1099087719fdSdp 
1100087719fdSdp 	return (Z_OK);
1101087719fdSdp }
1102087719fdSdp 
1103087719fdSdp /*
1104087719fdSdp  * The goal of this routine is to cause the index file update and the
1105087719fdSdp  * document save to happen as an atomic operation.  We do the document
1106087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
1107087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
1108087719fdSdp  *
1109087719fdSdp  * Strategy:
1110087719fdSdp  *
1111087719fdSdp  * New zone 'foo' configuration:
1112087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1113087719fdSdp  * 	Write XML to tmpfile
1114087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1115087719fdSdp  * 	Add entry to index file
1116087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
1117087719fdSdp  *
1118087719fdSdp  * Save existing zone 'foo':
1119087719fdSdp  * 	Make backup of foo.xml -> .backup
1120087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1121087719fdSdp  * 	Write XML to tmpfile
1122087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1123087719fdSdp  * 	Modify index file as needed
1124087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
1125087719fdSdp  *
1126087719fdSdp  * Rename 'foo' to 'bar':
1127087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1128087719fdSdp  * 	Write XML to tmpfile
1129087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1130087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1131087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
1132087719fdSdp  */
11337c478bd9Sstevel@tonic-gate static int
11347c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
11357c478bd9Sstevel@tonic-gate {
11367c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
1137087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
11389acbbeafSnn35248 	int tmpfd, err, valid;
11397c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
1140087719fdSdp 	boolean_t backup;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
11437c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
11447c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
11477c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
11487c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
11497c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
11507c478bd9Sstevel@tonic-gate 	}
11517c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
11547c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
11557c478bd9Sstevel@tonic-gate 
1156087719fdSdp 	/*
11579acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
11589acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
11599acbbeafSnn35248 	 * assert() that the doc is valid.
1160087719fdSdp 	 */
11619acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
11629acbbeafSnn35248 	assert(valid != 0);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
11657c478bd9Sstevel@tonic-gate 		goto err;
1166087719fdSdp 
11677c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
11687c478bd9Sstevel@tonic-gate 
1169087719fdSdp 	/*
1170087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
1171087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
1172087719fdSdp 	 * something goes wrong.
1173087719fdSdp 	 */
1174087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
1175087719fdSdp 		backup = B_TRUE;
1176087719fdSdp 
1177087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1178087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1179087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1180087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
1181087719fdSdp 
1182087719fdSdp 		if (link(filename, bakfile) == -1) {
1183087719fdSdp 			err = errno;
11847c478bd9Sstevel@tonic-gate 			(void) unlink(tmpfile);
11857c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
11867c478bd9Sstevel@tonic-gate 				return (Z_ACCES);
11877c478bd9Sstevel@tonic-gate 			return (Z_MISC_FS);
11887c478bd9Sstevel@tonic-gate 		}
1189087719fdSdp 	}
1190a1be23daSdp 
1191087719fdSdp 	/*
1192087719fdSdp 	 * Move the new document over top of the old.
1193087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1194087719fdSdp 	 */
1195087719fdSdp 	if (rename(tmpfile, filename) == -1) {
1196087719fdSdp 		err = errno;
1197087719fdSdp 		(void) unlink(tmpfile);
1198087719fdSdp 		if (backup)
1199087719fdSdp 			(void) unlink(bakfile);
1200087719fdSdp 		if (err == EACCES)
1201087719fdSdp 			return (Z_ACCES);
1202087719fdSdp 		return (Z_MISC_FS);
1203087719fdSdp 	}
1204087719fdSdp 
1205087719fdSdp 	/*
1206087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
1207087719fdSdp 	 */
1208087719fdSdp 	if (is_snapshot(handle))
1209087719fdSdp 		return (Z_OK);
1210087719fdSdp 
1211087719fdSdp 	/* now update the index file to reflect whatever we just did */
1212087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1213087719fdSdp 		if (backup) {
1214087719fdSdp 			/*
1215087719fdSdp 			 * Try to restore from our backup.
1216087719fdSdp 			 */
1217087719fdSdp 			(void) unlink(filename);
1218087719fdSdp 			(void) rename(bakfile, filename);
1219087719fdSdp 		} else {
1220087719fdSdp 			/*
1221087719fdSdp 			 * Either the zone is new, in which case we can delete
1222087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1223087719fdSdp 			 */
1224087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1225087719fdSdp 			(void) unlink(filename);
1226087719fdSdp 		}
1227087719fdSdp 		return (Z_UPDATING_INDEX);
1228087719fdSdp 	}
1229087719fdSdp 
1230087719fdSdp 	if (backup)
1231087719fdSdp 		(void) unlink(bakfile);
1232087719fdSdp 
1233087719fdSdp 	return (Z_OK);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate err:
12367c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
12377c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate int
12417c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
12427c478bd9Sstevel@tonic-gate {
1243087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1244087719fdSdp 	char delpath[MAXPATHLEN];
1245087719fdSdp 	int err = Z_SAVING_FILE;
12467c478bd9Sstevel@tonic-gate 
1247087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1248087719fdSdp 		return (Z_BAD_HANDLE);
1249087719fdSdp 
1250087719fdSdp 	/*
1251ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1252ee519a1fSgjelinek 	 * inventory at this time.
1253087719fdSdp 	 */
1254ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1255087719fdSdp 		return (Z_INVAL);
1256087719fdSdp 
1257087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
12587c478bd9Sstevel@tonic-gate 		return (err);
1259087719fdSdp 
1260108322fbScarlsonj 	if (!config_file_path(zname, path))
1261108322fbScarlsonj 		return (Z_MISC_FS);
1262087719fdSdp 
1263087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1264087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1265087719fdSdp 
1266087719fdSdp 	err = zonecfg_save_impl(handle, path);
1267087719fdSdp 
1268087719fdSdp 	stripcomments(handle);
1269087719fdSdp 
1270087719fdSdp 	if (err != Z_OK)
1271087719fdSdp 		return (err);
1272087719fdSdp 
1273087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1274087719fdSdp 
1275087719fdSdp 	if (is_renaming(handle)) {
1276108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1277087719fdSdp 			(void) unlink(delpath);
1278087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1279087719fdSdp 	}
1280087719fdSdp 
1281087719fdSdp 	return (Z_OK);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
1284ee519a1fSgjelinek int
12859acbbeafSnn35248 zonecfg_verify_save(zone_dochandle_t handle, char *filename)
12869acbbeafSnn35248 {
12879acbbeafSnn35248 	int valid;
12889acbbeafSnn35248 
12899acbbeafSnn35248 	xmlValidCtxt cvp = { NULL };
12909acbbeafSnn35248 
12919acbbeafSnn35248 	if (zonecfg_check_handle(handle) != Z_OK)
12929acbbeafSnn35248 		return (Z_BAD_HANDLE);
12939acbbeafSnn35248 
12949acbbeafSnn35248 	cvp.error = zonecfg_error_func;
12959acbbeafSnn35248 	cvp.warning = zonecfg_error_func;
12969acbbeafSnn35248 
12979acbbeafSnn35248 	/*
12989acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
12999acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
13009acbbeafSnn35248 	 * assert() that the doc is valid.
13019acbbeafSnn35248 	 */
13029acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13039acbbeafSnn35248 	assert(valid != 0);
13049acbbeafSnn35248 
13059acbbeafSnn35248 	if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
13069acbbeafSnn35248 		return (Z_SAVING_FILE);
13079acbbeafSnn35248 
13089acbbeafSnn35248 	return (Z_OK);
13099acbbeafSnn35248 }
13109acbbeafSnn35248 
13119acbbeafSnn35248 int
13128cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1313ee519a1fSgjelinek {
1314ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1315ee519a1fSgjelinek 	char path[MAXPATHLEN];
1316ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1317ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1318ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
13199acbbeafSnn35248 	int valid;
1320ee519a1fSgjelinek 
1321ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1322ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1323ee519a1fSgjelinek 
1324ee519a1fSgjelinek 	/*
1325ee519a1fSgjelinek 	 * We can only detach if we have taken a sw inventory.
1326ee519a1fSgjelinek 	 */
1327ee519a1fSgjelinek 	if (!handle->zone_dh_sw_inv)
1328ee519a1fSgjelinek 		return (Z_INVAL);
1329ee519a1fSgjelinek 
13308cd327d5Sgjelinek 	if (flags & ZONE_DRY_RUN) {
13318cd327d5Sgjelinek 		(void) strlcpy(migpath, "-", sizeof (migpath));
13328cd327d5Sgjelinek 	} else {
13338cd327d5Sgjelinek 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
13348cd327d5Sgjelinek 		    != Z_OK)
1335ee519a1fSgjelinek 			return (err);
1336ee519a1fSgjelinek 
13378cd327d5Sgjelinek 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
13388cd327d5Sgjelinek 		    != Z_OK)
1339ee519a1fSgjelinek 			return (err);
1340ee519a1fSgjelinek 
13418cd327d5Sgjelinek 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED)
13428cd327d5Sgjelinek 		    >= sizeof (migpath))
1343ee519a1fSgjelinek 			return (Z_NOMEM);
13448cd327d5Sgjelinek 	}
1345ee519a1fSgjelinek 
1346ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1347ee519a1fSgjelinek 		return (err);
1348ee519a1fSgjelinek 
1349ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1350ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1351ee519a1fSgjelinek 
1352ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1353ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1354ee519a1fSgjelinek 
1355ee519a1fSgjelinek 	/*
13569acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
13579acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
13589acbbeafSnn35248 	 * assert() that the doc is valid.
1359ee519a1fSgjelinek 	 */
13609acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13619acbbeafSnn35248 	assert(valid != 0);
1362ee519a1fSgjelinek 
1363ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1364ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1365ee519a1fSgjelinek 
13668cd327d5Sgjelinek 	if (!(flags & ZONE_DRY_RUN))
1367ee519a1fSgjelinek 		(void) chmod(migpath, 0644);
1368ee519a1fSgjelinek 
1369ee519a1fSgjelinek 	stripcomments(handle);
1370ee519a1fSgjelinek 
1371ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1372ee519a1fSgjelinek 
1373ee519a1fSgjelinek 	return (Z_OK);
1374ee519a1fSgjelinek }
1375ee519a1fSgjelinek 
1376ee519a1fSgjelinek boolean_t
1377ee519a1fSgjelinek zonecfg_detached(const char *path)
1378ee519a1fSgjelinek {
1379ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1380ee519a1fSgjelinek 	struct stat	buf;
1381ee519a1fSgjelinek 
1382ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
1383ee519a1fSgjelinek 	    sizeof (migpath))
1384ee519a1fSgjelinek 		return (B_FALSE);
1385ee519a1fSgjelinek 
1386ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1387ee519a1fSgjelinek 		return (B_TRUE);
1388ee519a1fSgjelinek 
1389ee519a1fSgjelinek 	return (B_FALSE);
1390ee519a1fSgjelinek }
1391ee519a1fSgjelinek 
1392ee519a1fSgjelinek void
1393ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1394ee519a1fSgjelinek {
1395ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1396ee519a1fSgjelinek 	char path[MAXPATHLEN];
1397ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1398ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1399ee519a1fSgjelinek 
1400ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1401ee519a1fSgjelinek 		return;
1402ee519a1fSgjelinek 
1403ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1404ee519a1fSgjelinek 		return;
1405ee519a1fSgjelinek 
1406ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1407ee519a1fSgjelinek 		return;
1408ee519a1fSgjelinek 
1409ee519a1fSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path, DETACHED);
1410ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1411ee519a1fSgjelinek 	    ATTACH_FORCED);
1412ee519a1fSgjelinek 
1413ee519a1fSgjelinek 	if (forced) {
1414ee519a1fSgjelinek 		(void) rename(detached, attached);
1415ee519a1fSgjelinek 	} else {
1416ee519a1fSgjelinek 		(void) unlink(attached);
1417ee519a1fSgjelinek 		(void) unlink(detached);
1418ee519a1fSgjelinek 	}
1419ee519a1fSgjelinek }
1420ee519a1fSgjelinek 
14217c478bd9Sstevel@tonic-gate /*
14227c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
14237c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
14247c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
14257c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
14267c478bd9Sstevel@tonic-gate  */
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate int
14297c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
14307c478bd9Sstevel@tonic-gate {
14317c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
14327c478bd9Sstevel@tonic-gate 
1433108322fbScarlsonj 	if (!config_file_path(zonename, path))
1434108322fbScarlsonj 		return (Z_INVAL);
14357c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
14367c478bd9Sstevel@tonic-gate 		return (Z_OK);
1437108322fbScarlsonj 	if (errno == ENOENT) {
1438108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1439108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1440108322fbScarlsonj 			return (Z_INVAL);
1441108322fbScarlsonj 		if (access(path, amode) == 0)
14427c478bd9Sstevel@tonic-gate 			return (Z_OK);
1443108322fbScarlsonj 	}
14447c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
14457c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
14467c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
14477c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14487c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
14497c478bd9Sstevel@tonic-gate }
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate int
1452108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
14537c478bd9Sstevel@tonic-gate {
14547c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
14557c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
14567c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
14597c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 
1462087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1463087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1464087719fdSdp 
14657c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
14667c478bd9Sstevel@tonic-gate 		goto out;
14677c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
14687c478bd9Sstevel@tonic-gate 		goto out;
14697c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
14707c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
14717c478bd9Sstevel@tonic-gate 		goto out;
14727c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
14737c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
14747c478bd9Sstevel@tonic-gate 		goto out;
14757c478bd9Sstevel@tonic-gate 	}
14767c478bd9Sstevel@tonic-gate 	/*
14777c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
14787c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
14797c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
14807c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
14817c478bd9Sstevel@tonic-gate 	 * paths.
14827c478bd9Sstevel@tonic-gate 	 */
14837c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
14847c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
14857c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
14867c478bd9Sstevel@tonic-gate 			goto out;
14877c478bd9Sstevel@tonic-gate 	}
1488108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1489108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1490108322fbScarlsonj 		error = Z_MISC_FS;
1491108322fbScarlsonj 		goto out;
1492108322fbScarlsonj 	}
1493108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
14947c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
14957c478bd9Sstevel@tonic-gate 		goto out;
14967c478bd9Sstevel@tonic-gate 	}
14977c478bd9Sstevel@tonic-gate 
1498108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1499108322fbScarlsonj 		error = Z_MISC_FS;
1500108322fbScarlsonj 		goto out;
1501108322fbScarlsonj 	}
1502087719fdSdp 
1503087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1504087719fdSdp 	    "It is a snapshot of running zone state.\n");
1505087719fdSdp 
15067c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
15077c478bd9Sstevel@tonic-gate 
1508087719fdSdp 	stripcomments(handle);
1509087719fdSdp 
15107c478bd9Sstevel@tonic-gate out:
15117c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
15127c478bd9Sstevel@tonic-gate 	return (error);
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate 
1515f4b3ec61Sdh155122 int
1516f4b3ec61Sdh155122 zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep)
1517f4b3ec61Sdh155122 {
1518f4b3ec61Sdh155122 	char property[10]; /* 10 is big enough for "shared"/"exclusive" */
1519f4b3ec61Sdh155122 	int err;
1520f4b3ec61Sdh155122 
1521f4b3ec61Sdh155122 	err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property));
1522f4b3ec61Sdh155122 	if (err == Z_BAD_PROPERTY) {
1523f4b3ec61Sdh155122 		/* Return default value */
1524f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1525f4b3ec61Sdh155122 		return (Z_OK);
1526f4b3ec61Sdh155122 	} else if (err != Z_OK) {
1527f4b3ec61Sdh155122 		return (err);
1528f4b3ec61Sdh155122 	}
1529f4b3ec61Sdh155122 
1530f4b3ec61Sdh155122 	if (strlen(property) == 0 ||
1531f4b3ec61Sdh155122 	    strcmp(property, "shared") == 0)
1532f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1533f4b3ec61Sdh155122 	else if (strcmp(property, "exclusive") == 0)
1534f4b3ec61Sdh155122 		*iptypep = ZS_EXCLUSIVE;
1535f4b3ec61Sdh155122 	else
1536f4b3ec61Sdh155122 		return (Z_INVAL);
1537f4b3ec61Sdh155122 
1538f4b3ec61Sdh155122 	return (Z_OK);
1539f4b3ec61Sdh155122 }
1540f4b3ec61Sdh155122 
1541f4b3ec61Sdh155122 int
1542f4b3ec61Sdh155122 zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype)
1543f4b3ec61Sdh155122 {
1544f4b3ec61Sdh155122 	xmlNodePtr cur;
1545f4b3ec61Sdh155122 
1546f4b3ec61Sdh155122 	if (handle == NULL)
1547f4b3ec61Sdh155122 		return (Z_INVAL);
1548f4b3ec61Sdh155122 
1549f4b3ec61Sdh155122 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
1550f4b3ec61Sdh155122 	if (cur == NULL) {
1551f4b3ec61Sdh155122 		return (Z_EMPTY_DOCUMENT);
1552f4b3ec61Sdh155122 	}
1553f4b3ec61Sdh155122 
1554f4b3ec61Sdh155122 	if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) {
1555f4b3ec61Sdh155122 		return (Z_WRONG_DOC_TYPE);
1556f4b3ec61Sdh155122 	}
1557f4b3ec61Sdh155122 	switch (iptype) {
1558f4b3ec61Sdh155122 	case ZS_SHARED:
1559f4b3ec61Sdh155122 		/*
1560f4b3ec61Sdh155122 		 * Since "shared" is the default, we don't write it to the
1561f4b3ec61Sdh155122 		 * configuration file, so that it's easier to migrate those
1562f4b3ec61Sdh155122 		 * zones elsewhere, eg., to systems which are not IP-Instances
1563f4b3ec61Sdh155122 		 * aware.
1564f4b3ec61Sdh155122 		 * xmlUnsetProp only fails when the attribute doesn't exist,
1565f4b3ec61Sdh155122 		 * which we don't care.
1566f4b3ec61Sdh155122 		 */
1567f4b3ec61Sdh155122 		(void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE);
1568f4b3ec61Sdh155122 		break;
1569f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1570f4b3ec61Sdh155122 		if (xmlSetProp(cur, DTD_ATTR_IPTYPE,
1571f4b3ec61Sdh155122 		    (const xmlChar *) "exclusive") == NULL)
1572f4b3ec61Sdh155122 			return (Z_INVAL);
1573f4b3ec61Sdh155122 		break;
1574f4b3ec61Sdh155122 	}
1575f4b3ec61Sdh155122 	return (Z_OK);
1576f4b3ec61Sdh155122 }
1577f4b3ec61Sdh155122 
15787c478bd9Sstevel@tonic-gate static int
1579a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
15807c478bd9Sstevel@tonic-gate {
15817c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
15847c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
15857c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
15867c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
15877c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 	return (Z_OK);
15907c478bd9Sstevel@tonic-gate }
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate static int
15937c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
15947c478bd9Sstevel@tonic-gate {
15957c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
15967c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
15977c478bd9Sstevel@tonic-gate 	int err;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1600a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
16017c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
16027c478bd9Sstevel@tonic-gate 		return (err);
16037c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1604a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
16057c478bd9Sstevel@tonic-gate 		return (err);
1606a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16077c478bd9Sstevel@tonic-gate 		return (err);
1608a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
16097c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
16107c478bd9Sstevel@tonic-gate 		return (err);
16117c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
16127c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
16137c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
16147c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
16157c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1616a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
16177c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
16187c478bd9Sstevel@tonic-gate 				return (err);
16197c478bd9Sstevel@tonic-gate 		}
16207c478bd9Sstevel@tonic-gate 	}
16217c478bd9Sstevel@tonic-gate 	return (Z_OK);
16227c478bd9Sstevel@tonic-gate }
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate int
16257c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
16267c478bd9Sstevel@tonic-gate {
16277c478bd9Sstevel@tonic-gate 	int err;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16307c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16337c478bd9Sstevel@tonic-gate 		return (err);
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
16367c478bd9Sstevel@tonic-gate 		return (err);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	return (Z_OK);
16397c478bd9Sstevel@tonic-gate }
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate static int
16427c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
16437c478bd9Sstevel@tonic-gate {
16447c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
16457c478bd9Sstevel@tonic-gate 	int err;
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1648a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16497c478bd9Sstevel@tonic-gate 		return (err);
16507c478bd9Sstevel@tonic-gate 	return (Z_OK);
16517c478bd9Sstevel@tonic-gate }
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate int
16547c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
16557c478bd9Sstevel@tonic-gate {
16567c478bd9Sstevel@tonic-gate 	int err;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16597c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16627c478bd9Sstevel@tonic-gate 		return (err);
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
16657c478bd9Sstevel@tonic-gate 		return (err);
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	return (Z_OK);
16687c478bd9Sstevel@tonic-gate }
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate int
16717c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
16727c478bd9Sstevel@tonic-gate {
16737c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16767c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
16777c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
16787c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
16797c478bd9Sstevel@tonic-gate 	if (new == NULL)
16807c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
16817c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
16827c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
16837c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
16847c478bd9Sstevel@tonic-gate 	if (last == NULL)
16857c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
16867c478bd9Sstevel@tonic-gate 	else
16877c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
16887c478bd9Sstevel@tonic-gate 	return (Z_OK);
16897c478bd9Sstevel@tonic-gate }
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate int
16927c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
16937c478bd9Sstevel@tonic-gate {
16947c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16977c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
16987c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
16997c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
17007c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
17017c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
17027c478bd9Sstevel@tonic-gate 			else
17037c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
17047c478bd9Sstevel@tonic-gate 			free(this);
17057c478bd9Sstevel@tonic-gate 			return (Z_OK);
17067c478bd9Sstevel@tonic-gate 		} else
17077c478bd9Sstevel@tonic-gate 			last = this;
17087c478bd9Sstevel@tonic-gate 	}
17097c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
17107c478bd9Sstevel@tonic-gate }
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate void
17137c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
17147c478bd9Sstevel@tonic-gate {
17157c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
17187c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
17197c478bd9Sstevel@tonic-gate 		free(this);
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate void
17247c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
17257c478bd9Sstevel@tonic-gate {
17267c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
17277c478bd9Sstevel@tonic-gate 		return;
17287c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
17297c478bd9Sstevel@tonic-gate 	free(valtab);
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate static boolean_t
17337c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
17347c478bd9Sstevel@tonic-gate {
17357c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
17367c478bd9Sstevel@tonic-gate 	int prop_result;
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
17397c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
17407c478bd9Sstevel@tonic-gate 		return (B_FALSE);
17417c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
17427c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
17437c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
17447c478bd9Sstevel@tonic-gate }
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate static int
17477c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
17487c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
17497c478bd9Sstevel@tonic-gate {
17507c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17517c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17547c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
17557c478bd9Sstevel@tonic-gate 			continue;
17567c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
17577c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
17587c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
17597c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
17607c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
17617c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
17627c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
17637c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
17647c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17657c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17667c478bd9Sstevel@tonic-gate 			return (Z_OK);
17677c478bd9Sstevel@tonic-gate 		}
17687c478bd9Sstevel@tonic-gate 	}
17697c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
17707c478bd9Sstevel@tonic-gate }
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate int
17737c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
17747c478bd9Sstevel@tonic-gate {
17757c478bd9Sstevel@tonic-gate 	int err;
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17787c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17817c478bd9Sstevel@tonic-gate 		return (err);
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
17847c478bd9Sstevel@tonic-gate 		return (err);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	return (Z_OK);
17877c478bd9Sstevel@tonic-gate }
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate int
17907c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
17917c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
17927c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
17937c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
17947c478bd9Sstevel@tonic-gate {
17957c478bd9Sstevel@tonic-gate 	int err;
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
17987c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18017c478bd9Sstevel@tonic-gate 		return (err);
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
18047c478bd9Sstevel@tonic-gate 		return (err);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
18077c478bd9Sstevel@tonic-gate 		return (err);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	return (Z_OK);
18107c478bd9Sstevel@tonic-gate }
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate static int
18137c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
18147c478bd9Sstevel@tonic-gate {
18157c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18187c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
18197c478bd9Sstevel@tonic-gate 			continue;
18207c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
18217c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
18227c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
18237c478bd9Sstevel@tonic-gate 			return (Z_OK);
18247c478bd9Sstevel@tonic-gate 		}
18257c478bd9Sstevel@tonic-gate 	}
18267c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
18277c478bd9Sstevel@tonic-gate }
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate int
18307c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
18317c478bd9Sstevel@tonic-gate {
18327c478bd9Sstevel@tonic-gate 	int err;
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18357c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18387c478bd9Sstevel@tonic-gate 		return (err);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
18417c478bd9Sstevel@tonic-gate 		return (err);
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	return (Z_OK);
18447c478bd9Sstevel@tonic-gate }
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate int
18477c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
18487c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
18497c478bd9Sstevel@tonic-gate {
18507c478bd9Sstevel@tonic-gate 	int err;
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18537c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18567c478bd9Sstevel@tonic-gate 		return (err);
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
18597c478bd9Sstevel@tonic-gate 		return (err);
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
18627c478bd9Sstevel@tonic-gate 		return (err);
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	return (Z_OK);
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate int
18687c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
18697c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
18707c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
18717c478bd9Sstevel@tonic-gate {
18727c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
18737c478bd9Sstevel@tonic-gate 	int err;
18747c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
18757c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
18767c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18797c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18827c478bd9Sstevel@tonic-gate 		return (err);
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	/*
18857c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
18867c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
18877c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
18887c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
18897c478bd9Sstevel@tonic-gate 	 */
18907c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
18917c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
18927c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18937c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
18947c478bd9Sstevel@tonic-gate 			continue;
18957c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
18967c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
18977c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
18987c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
18997c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
19007c478bd9Sstevel@tonic-gate 					firstmatch = cur;
19017c478bd9Sstevel@tonic-gate 				else
19027c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
19037c478bd9Sstevel@tonic-gate 			}
19047c478bd9Sstevel@tonic-gate 		}
19057c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
19067c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
19077c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
19087c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
19097c478bd9Sstevel@tonic-gate 				    special) == 0) {
19107c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19117c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19127c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19137c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19147c478bd9Sstevel@tonic-gate 				} else {
19157c478bd9Sstevel@tonic-gate 					/*
19167c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19177c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19187c478bd9Sstevel@tonic-gate 					 */
19197c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19207c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19217c478bd9Sstevel@tonic-gate 				}
19227c478bd9Sstevel@tonic-gate 			}
19237c478bd9Sstevel@tonic-gate 		}
19247c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
19257c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
19267c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
19277c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
19287c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19297c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19307c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19317c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19327c478bd9Sstevel@tonic-gate 				} else {
19337c478bd9Sstevel@tonic-gate 					/*
19347c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19357c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19367c478bd9Sstevel@tonic-gate 					 */
19377c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19387c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19397c478bd9Sstevel@tonic-gate 				}
19407c478bd9Sstevel@tonic-gate 			}
19417c478bd9Sstevel@tonic-gate 		}
19427c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
19437c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
19447c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
19457c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
19467c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19477c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19487c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19497c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19507c478bd9Sstevel@tonic-gate 				} else {
19517c478bd9Sstevel@tonic-gate 					/*
19527c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19537c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19547c478bd9Sstevel@tonic-gate 					 */
19557c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19567c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19577c478bd9Sstevel@tonic-gate 				}
19587c478bd9Sstevel@tonic-gate 			}
19597c478bd9Sstevel@tonic-gate 		}
19607c478bd9Sstevel@tonic-gate 	}
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
19637c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 	cur = firstmatch;
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
19687c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
19697c478bd9Sstevel@tonic-gate 		return (err);
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
19727c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
19737c478bd9Sstevel@tonic-gate 		return (err);
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
19767c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
19777c478bd9Sstevel@tonic-gate 		return (err);
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
19807c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
19817c478bd9Sstevel@tonic-gate 		return (err);
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	/* options are optional */
19847c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
19857c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
19867c478bd9Sstevel@tonic-gate 	    options = options->next) {
19877c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
19887c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
19897c478bd9Sstevel@tonic-gate 			break;
19907c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
19917c478bd9Sstevel@tonic-gate 			break;
19927c478bd9Sstevel@tonic-gate 	}
19937c478bd9Sstevel@tonic-gate 	return (Z_OK);
19947c478bd9Sstevel@tonic-gate }
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate int
19977c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
19987c478bd9Sstevel@tonic-gate {
19997c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
20007c478bd9Sstevel@tonic-gate 	int err;
20017c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
20047c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20077c478bd9Sstevel@tonic-gate 		return (err);
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	/*
20107c478bd9Sstevel@tonic-gate 	 * General algorithm:
20117c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
20127c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
20137c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
20147c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
20157c478bd9Sstevel@tonic-gate 	 */
20167c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
20177c478bd9Sstevel@tonic-gate 	match = NULL;
20187c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
20197c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
20207c478bd9Sstevel@tonic-gate 			continue;
20217c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
20227c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
20237c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
20247c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
20257c478bd9Sstevel@tonic-gate 				if (match == NULL)
20267c478bd9Sstevel@tonic-gate 					match = cur;
20277c478bd9Sstevel@tonic-gate 				else
20287c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20297c478bd9Sstevel@tonic-gate 			}
20307c478bd9Sstevel@tonic-gate 		}
20317c478bd9Sstevel@tonic-gate 	}
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	if (match == NULL)
20347c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	cur = match;
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
20397c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
20407c478bd9Sstevel@tonic-gate 		return (err);
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	return (Z_OK);
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate /*
20467c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
20477c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
20487c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
20497c478bd9Sstevel@tonic-gate  */
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate boolean_t
20527c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
20537c478bd9Sstevel@tonic-gate {
20547c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
20557c478bd9Sstevel@tonic-gate 	int result;
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
20587c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	/*
20617c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
20627c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
20637c478bd9Sstevel@tonic-gate 	 */
20647c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
20657c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
20667c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
20677c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
20687c478bd9Sstevel@tonic-gate 		return (B_FALSE);
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	/*
20717c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
20727c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
20737c478bd9Sstevel@tonic-gate 	 * result of the comparison.
20747c478bd9Sstevel@tonic-gate 	 */
20757c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
20767c478bd9Sstevel@tonic-gate 	*slashp = '\0';
20777c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
20787c478bd9Sstevel@tonic-gate 	*slashp = '/';
20797c478bd9Sstevel@tonic-gate 	return ((result == 0));
20807c478bd9Sstevel@tonic-gate }
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate int
20837c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
20847c478bd9Sstevel@tonic-gate {
20857c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
20867c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
20877c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
20887c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
20917c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20927c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20937c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
20947c478bd9Sstevel@tonic-gate 		*slashp = '\0';
20957c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
20967c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
20977c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
20987c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
20997c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
21007c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
21017c478bd9Sstevel@tonic-gate 	} else {
21027c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
21037c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
21047c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
21057c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
21067c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
21077c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
2108a1be23daSdp 
21097c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
21107c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
21117c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
21127c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
2113a1be23daSdp 
21147c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
21157c478bd9Sstevel@tonic-gate 	}
21167c478bd9Sstevel@tonic-gate 	return (Z_OK);
21177c478bd9Sstevel@tonic-gate }
21187c478bd9Sstevel@tonic-gate 
2119f4b3ec61Sdh155122 boolean_t
2120f4b3ec61Sdh155122 zonecfg_ifname_exists(sa_family_t af, char *ifname)
2121f4b3ec61Sdh155122 {
2122f4b3ec61Sdh155122 	struct lifreq lifr;
2123f4b3ec61Sdh155122 	int so;
2124f4b3ec61Sdh155122 	int save_errno;
2125f4b3ec61Sdh155122 
2126f4b3ec61Sdh155122 	(void) memset(&lifr, 0, sizeof (lifr));
2127f4b3ec61Sdh155122 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2128f4b3ec61Sdh155122 	lifr.lifr_addr.ss_family = af;
2129f4b3ec61Sdh155122 	if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
2130f4b3ec61Sdh155122 		/* Odd - can't tell if the ifname exists */
2131f4b3ec61Sdh155122 		return (B_FALSE);
2132f4b3ec61Sdh155122 	}
2133f4b3ec61Sdh155122 	if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2134f4b3ec61Sdh155122 		save_errno = errno;
2135f4b3ec61Sdh155122 		(void) close(so);
2136f4b3ec61Sdh155122 		errno = save_errno;
2137f4b3ec61Sdh155122 		return (B_FALSE);
2138f4b3ec61Sdh155122 	}
2139f4b3ec61Sdh155122 	(void) close(so);
2140f4b3ec61Sdh155122 	return (B_TRUE);
2141f4b3ec61Sdh155122 }
2142f4b3ec61Sdh155122 
21437c478bd9Sstevel@tonic-gate int
21447c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21457c478bd9Sstevel@tonic-gate {
21467c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
21477c478bd9Sstevel@tonic-gate 	int err;
21487c478bd9Sstevel@tonic-gate 	char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ];
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21517c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21547c478bd9Sstevel@tonic-gate 		return (err);
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
21577c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
21587c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
21597c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
21607c478bd9Sstevel@tonic-gate 			continue;
21617c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_physical) > 0) {
21627c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
21637c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
21647c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
21657c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
21667c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
21677c478bd9Sstevel@tonic-gate 					firstmatch = cur;
21687c478bd9Sstevel@tonic-gate 				else
21697c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
21707c478bd9Sstevel@tonic-gate 			}
21717c478bd9Sstevel@tonic-gate 		}
21727c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_address) > 0) {
21737c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
21747c478bd9Sstevel@tonic-gate 			    sizeof (address)) == Z_OK)) {
21757c478bd9Sstevel@tonic-gate 				if (zonecfg_same_net_address(
21767c478bd9Sstevel@tonic-gate 				    tabptr->zone_nwif_address, address)) {
21777c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
21787c478bd9Sstevel@tonic-gate 						firstmatch = cur;
21797c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
21807c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
21817c478bd9Sstevel@tonic-gate 				} else {
21827c478bd9Sstevel@tonic-gate 					/*
21837c478bd9Sstevel@tonic-gate 					 * If another property matched but this
21847c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
21857c478bd9Sstevel@tonic-gate 					 */
21867c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
21877c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
21887c478bd9Sstevel@tonic-gate 				}
21897c478bd9Sstevel@tonic-gate 			}
21907c478bd9Sstevel@tonic-gate 		}
21917c478bd9Sstevel@tonic-gate 	}
21927c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
21937c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	cur = firstmatch;
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
21987c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
21997c478bd9Sstevel@tonic-gate 		return (err);
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
22027c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
22037c478bd9Sstevel@tonic-gate 		return (err);
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	return (Z_OK);
22067c478bd9Sstevel@tonic-gate }
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate static int
22097c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22107c478bd9Sstevel@tonic-gate {
22117c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
22127c478bd9Sstevel@tonic-gate 	int err;
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
2215a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
22167c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
22177c478bd9Sstevel@tonic-gate 		return (err);
2218a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
22197c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
22207c478bd9Sstevel@tonic-gate 		return (err);
22217c478bd9Sstevel@tonic-gate 	return (Z_OK);
22227c478bd9Sstevel@tonic-gate }
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate int
22257c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22267c478bd9Sstevel@tonic-gate {
22277c478bd9Sstevel@tonic-gate 	int err;
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22307c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22337c478bd9Sstevel@tonic-gate 		return (err);
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
22367c478bd9Sstevel@tonic-gate 		return (err);
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	return (Z_OK);
22397c478bd9Sstevel@tonic-gate }
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate static int
22427c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22437c478bd9Sstevel@tonic-gate {
22447c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
22457c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
22487c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
22497c478bd9Sstevel@tonic-gate 			continue;
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
22527c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
22537c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
22547c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
22577c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
22587c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
22597c478bd9Sstevel@tonic-gate 			return (Z_OK);
22607c478bd9Sstevel@tonic-gate 		}
22617c478bd9Sstevel@tonic-gate 	}
22627c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
22637c478bd9Sstevel@tonic-gate }
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate int
22667c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22677c478bd9Sstevel@tonic-gate {
22687c478bd9Sstevel@tonic-gate 	int err;
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22717c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22747c478bd9Sstevel@tonic-gate 		return (err);
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
22777c478bd9Sstevel@tonic-gate 		return (err);
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	return (Z_OK);
22807c478bd9Sstevel@tonic-gate }
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate int
22837c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
22847c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
22857c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
22867c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
22877c478bd9Sstevel@tonic-gate {
22887c478bd9Sstevel@tonic-gate 	int err;
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
22917c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22947c478bd9Sstevel@tonic-gate 		return (err);
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
22977c478bd9Sstevel@tonic-gate 		return (err);
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
23007c478bd9Sstevel@tonic-gate 		return (err);
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 	return (Z_OK);
23037c478bd9Sstevel@tonic-gate }
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate int
23067c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23077c478bd9Sstevel@tonic-gate {
23087c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
23097c478bd9Sstevel@tonic-gate 	int err;
23107c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23137c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23167c478bd9Sstevel@tonic-gate 		return (err);
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
23197c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
23207c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23217c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
23227c478bd9Sstevel@tonic-gate 			continue;
23237c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
23247c478bd9Sstevel@tonic-gate 			continue;
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
23277c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
23287c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
23297c478bd9Sstevel@tonic-gate 			    match) == 0) {
23307c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
23317c478bd9Sstevel@tonic-gate 					firstmatch = cur;
23327c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
23337c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
23347c478bd9Sstevel@tonic-gate 			} else {
23357c478bd9Sstevel@tonic-gate 				/*
23367c478bd9Sstevel@tonic-gate 				 * If another property matched but this
23377c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
23387c478bd9Sstevel@tonic-gate 				 */
23397c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
23407c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
23417c478bd9Sstevel@tonic-gate 			}
23427c478bd9Sstevel@tonic-gate 		}
23437c478bd9Sstevel@tonic-gate 	}
23447c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
23457c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	cur = firstmatch;
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
23507c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
23517c478bd9Sstevel@tonic-gate 		return (err);
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	return (Z_OK);
23547c478bd9Sstevel@tonic-gate }
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate static int
23577c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
23587c478bd9Sstevel@tonic-gate {
23597c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
23607c478bd9Sstevel@tonic-gate 	int err;
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
23637c478bd9Sstevel@tonic-gate 
2364a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
23657c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
23667c478bd9Sstevel@tonic-gate 		return (err);
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	return (Z_OK);
23697c478bd9Sstevel@tonic-gate }
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate int
23727c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23737c478bd9Sstevel@tonic-gate {
23747c478bd9Sstevel@tonic-gate 	int err;
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23777c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23807c478bd9Sstevel@tonic-gate 		return (err);
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
23837c478bd9Sstevel@tonic-gate 		return (err);
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	return (Z_OK);
23867c478bd9Sstevel@tonic-gate }
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate static int
23897c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
23907c478bd9Sstevel@tonic-gate {
2391facf4a8dSllai1 	xmlNodePtr cur = handle->zone_dh_cur;
23927c478bd9Sstevel@tonic-gate 	int match_match;
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23957c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
23967c478bd9Sstevel@tonic-gate 			continue;
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
23997c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 		if (match_match) {
24027c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
24037c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
24047c478bd9Sstevel@tonic-gate 			return (Z_OK);
24057c478bd9Sstevel@tonic-gate 		}
24067c478bd9Sstevel@tonic-gate 	}
24077c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
24087c478bd9Sstevel@tonic-gate }
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate int
24117c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
24127c478bd9Sstevel@tonic-gate {
24137c478bd9Sstevel@tonic-gate 	int err;
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
24167c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24197c478bd9Sstevel@tonic-gate 		return (err);
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
24227c478bd9Sstevel@tonic-gate 		return (err);
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	return (Z_OK);
24257c478bd9Sstevel@tonic-gate }
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate int
24287c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
24297c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
24307c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
24317c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
24327c478bd9Sstevel@tonic-gate {
24337c478bd9Sstevel@tonic-gate 	int err;
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
24367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24397c478bd9Sstevel@tonic-gate 		return (err);
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
24427c478bd9Sstevel@tonic-gate 		return (err);
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
24457c478bd9Sstevel@tonic-gate 		return (err);
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	return (Z_OK);
24487c478bd9Sstevel@tonic-gate }
24497c478bd9Sstevel@tonic-gate 
2450ee519a1fSgjelinek /* Lock to serialize all zonecfg_devwalks */
2451ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2452ee519a1fSgjelinek /*
2453ee519a1fSgjelinek  * Global variables used to pass data from zonecfg_devwalk to the nftw
2454ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2455ee519a1fSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from zonecfg_devwalk.
2456ee519a1fSgjelinek  */
2457ee519a1fSgjelinek static void *g_devwalk_data;
2458ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2459ee519a1fSgjelinek     void *);
2460ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2461ee519a1fSgjelinek 
2462ee519a1fSgjelinek /*
2463ee519a1fSgjelinek  * This is the nftw call-back function used by zonecfg_devwalk.  It is
2464ee519a1fSgjelinek  * responsible for calling the actual call-back that is passed in to
2465ee519a1fSgjelinek  * zonecfg_devwalk as the *cb argument.
2466ee519a1fSgjelinek  */
2467ee519a1fSgjelinek /* ARGSUSED2 */
2468ee519a1fSgjelinek static int
2469ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2470ee519a1fSgjelinek     struct FTW *ftw)
2471ee519a1fSgjelinek {
2472ee519a1fSgjelinek 	acl_t *acl;
2473ee519a1fSgjelinek 	char *acl_txt = NULL;
2474ee519a1fSgjelinek 
2475ee519a1fSgjelinek 	/* skip all but character and block devices */
2476ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2477ee519a1fSgjelinek 		return (0);
2478ee519a1fSgjelinek 
2479ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2480ee519a1fSgjelinek 	    acl != NULL) {
2481ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2482ee519a1fSgjelinek 		acl_free(acl);
2483ee519a1fSgjelinek 	}
2484ee519a1fSgjelinek 
2485ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2486ee519a1fSgjelinek 		return (0);
2487ee519a1fSgjelinek 
2488ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2489ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2490ee519a1fSgjelinek 	    g_devwalk_data);
2491ee519a1fSgjelinek 	free(acl_txt);
2492ee519a1fSgjelinek 	return (0);
2493ee519a1fSgjelinek }
2494ee519a1fSgjelinek 
2495ee519a1fSgjelinek /*
2496ee519a1fSgjelinek  * Walk the dev tree for the zone specified by hdl and call the call-back (cb)
2497ee519a1fSgjelinek  * function for each entry in the tree.  The call-back will be passed the
2498ee519a1fSgjelinek  * name, uid, gid, mode, acl string and the void *data input parameter
2499ee519a1fSgjelinek  * for each dev entry.
2500ee519a1fSgjelinek  *
2501ee519a1fSgjelinek  * Data is passed to the zonecfg_devwalk_cb through the global variables
2502ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2503ee519a1fSgjelinek  * zonecfg_devwalk_cb function will actually call *cb.
2504ee519a1fSgjelinek  */
2505ee519a1fSgjelinek int
2506ee519a1fSgjelinek zonecfg_devwalk(zone_dochandle_t hdl,
2507ee519a1fSgjelinek     int (*cb)(const char *, uid_t, gid_t, mode_t, const char *, void *),
2508ee519a1fSgjelinek     void *data)
2509ee519a1fSgjelinek {
2510ee519a1fSgjelinek 	char path[MAXPATHLEN];
2511ee519a1fSgjelinek 	int ret;
2512ee519a1fSgjelinek 
2513ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2514ee519a1fSgjelinek 		return (ret);
2515ee519a1fSgjelinek 
2516ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2517ee519a1fSgjelinek 		return (Z_TOO_BIG);
2518ee519a1fSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2519ee519a1fSgjelinek 
2520ee519a1fSgjelinek 	/*
2521ee519a1fSgjelinek 	 * We have to serialize all zonecfg_devwalks in the same process
2522ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2523ee519a1fSgjelinek 	 */
2524ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2525ee519a1fSgjelinek 
2526ee519a1fSgjelinek 	g_devwalk_data = data;
2527ee519a1fSgjelinek 	g_devwalk_cb = cb;
2528ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2529ee519a1fSgjelinek 
2530ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2531ee519a1fSgjelinek 	return (Z_OK);
2532ee519a1fSgjelinek }
2533ee519a1fSgjelinek 
2534ee519a1fSgjelinek /*
2535ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2536ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2537ee519a1fSgjelinek  * attaching a migrated zone.
2538ee519a1fSgjelinek  */
2539ee519a1fSgjelinek int
2540ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2541ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2542ee519a1fSgjelinek {
2543ee519a1fSgjelinek 	int ret;
2544ee519a1fSgjelinek 	char path[MAXPATHLEN];
2545ee519a1fSgjelinek 	struct stat st;
2546ee519a1fSgjelinek 	acl_t *aclp;
2547ee519a1fSgjelinek 
2548ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2549ee519a1fSgjelinek 		return (ret);
2550ee519a1fSgjelinek 
2551ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2552ee519a1fSgjelinek 		return (Z_TOO_BIG);
2553ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2554ee519a1fSgjelinek 		return (Z_TOO_BIG);
2555ee519a1fSgjelinek 
2556ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2557ee519a1fSgjelinek 		return (Z_INVAL);
2558ee519a1fSgjelinek 
2559ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2560ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2561ee519a1fSgjelinek 		return (Z_INVAL);
2562ee519a1fSgjelinek 
2563ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2564ee519a1fSgjelinek 		return (Z_SYSTEM);
2565ee519a1fSgjelinek 
2566ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2567ee519a1fSgjelinek 		return (Z_SYSTEM);
2568ee519a1fSgjelinek 
2569ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2570ee519a1fSgjelinek 		return (Z_OK);
2571ee519a1fSgjelinek 
2572ee519a1fSgjelinek 	if (acl_fromtext(acltxt, &aclp) != 0)
2573ee519a1fSgjelinek 		return (Z_SYSTEM);
2574ee519a1fSgjelinek 
2575ee519a1fSgjelinek 	errno = 0;
2576ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2577ee519a1fSgjelinek 		free(aclp);
2578ee519a1fSgjelinek 		return (Z_SYSTEM);
2579ee519a1fSgjelinek 	}
2580ee519a1fSgjelinek 
2581ee519a1fSgjelinek 	free(aclp);
2582ee519a1fSgjelinek 	return (Z_OK);
2583ee519a1fSgjelinek }
2584ee519a1fSgjelinek 
25857c478bd9Sstevel@tonic-gate /*
25867c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
25877c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
25887c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
25897c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
25907c478bd9Sstevel@tonic-gate  *
25917c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
25927c478bd9Sstevel@tonic-gate  * aborts with an error.
25937c478bd9Sstevel@tonic-gate  */
25947c478bd9Sstevel@tonic-gate int
25957c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
25967c478bd9Sstevel@tonic-gate     void *priv) {
25977c478bd9Sstevel@tonic-gate 	FILE *mnttab;
25987c478bd9Sstevel@tonic-gate 	struct mnttab m;
25997c478bd9Sstevel@tonic-gate 	size_t l;
260007b574eeSgjelinek 	int zfsl;
26017c478bd9Sstevel@tonic-gate 	int rv = 0;
260207b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
26057c478bd9Sstevel@tonic-gate 
260607b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
260707b574eeSgjelinek 	    >= sizeof (zfs_path))
260807b574eeSgjelinek 		return (-1);
260907b574eeSgjelinek 
26107c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
26157c478bd9Sstevel@tonic-gate 		return (-1);
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
26187c478bd9Sstevel@tonic-gate 		rv = -1;
26197c478bd9Sstevel@tonic-gate 		goto out;
26207c478bd9Sstevel@tonic-gate 	}
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
26237c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
262407b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
262507b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
26267c478bd9Sstevel@tonic-gate 			rv++;
26277c478bd9Sstevel@tonic-gate 			if (callback == NULL)
26287c478bd9Sstevel@tonic-gate 				continue;
26297c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
26307c478bd9Sstevel@tonic-gate 				rv = -1;
26317c478bd9Sstevel@tonic-gate 				goto out;
26327c478bd9Sstevel@tonic-gate 
26337c478bd9Sstevel@tonic-gate 			}
26347c478bd9Sstevel@tonic-gate 		}
26357c478bd9Sstevel@tonic-gate 	}
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate out:
26387c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
26397c478bd9Sstevel@tonic-gate 	return (rv);
26407c478bd9Sstevel@tonic-gate }
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate int
26437c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26447c478bd9Sstevel@tonic-gate {
26457c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
26467c478bd9Sstevel@tonic-gate 	int err;
26477c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
26507c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
26537c478bd9Sstevel@tonic-gate 		return (err);
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
26567c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
26577c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
26587c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
26597c478bd9Sstevel@tonic-gate 			continue;
26607c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
26617c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
26627c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
26637c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
26647c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
26657c478bd9Sstevel@tonic-gate 					firstmatch = cur;
26667c478bd9Sstevel@tonic-gate 				else
26677c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
26687c478bd9Sstevel@tonic-gate 			}
26697c478bd9Sstevel@tonic-gate 		}
26707c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
26717c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
26727c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
26737c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
26747c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
26757c478bd9Sstevel@tonic-gate 						firstmatch = cur;
26767c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
26777c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
26787c478bd9Sstevel@tonic-gate 				} else {
26797c478bd9Sstevel@tonic-gate 					/*
26807c478bd9Sstevel@tonic-gate 					 * If another property matched but this
26817c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
26827c478bd9Sstevel@tonic-gate 					 */
26837c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
26847c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
26857c478bd9Sstevel@tonic-gate 				}
26867c478bd9Sstevel@tonic-gate 			}
26877c478bd9Sstevel@tonic-gate 		}
26887c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
26897c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
26907c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
26917c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
26927c478bd9Sstevel@tonic-gate 				    0) {
26937c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
26947c478bd9Sstevel@tonic-gate 						firstmatch = cur;
26957c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
26967c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
26977c478bd9Sstevel@tonic-gate 				} else {
26987c478bd9Sstevel@tonic-gate 					/*
26997c478bd9Sstevel@tonic-gate 					 * If another property matched but this
27007c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
27017c478bd9Sstevel@tonic-gate 					 */
27027c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
27037c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
27047c478bd9Sstevel@tonic-gate 				}
27057c478bd9Sstevel@tonic-gate 			}
27067c478bd9Sstevel@tonic-gate 		}
27077c478bd9Sstevel@tonic-gate 	}
27087c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
27097c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate 	cur = firstmatch;
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
27147c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
27157c478bd9Sstevel@tonic-gate 		return (err);
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
27187c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
27197c478bd9Sstevel@tonic-gate 		return (err);
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
27227c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
27237c478bd9Sstevel@tonic-gate 		return (err);
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 	return (Z_OK);
27267c478bd9Sstevel@tonic-gate }
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate static int
27297c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27307c478bd9Sstevel@tonic-gate {
27317c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
27327c478bd9Sstevel@tonic-gate 	int err;
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2735a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
27367c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27377c478bd9Sstevel@tonic-gate 		return (err);
2738a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
27397c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27407c478bd9Sstevel@tonic-gate 		return (err);
2741a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
27427c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27437c478bd9Sstevel@tonic-gate 		return (err);
27447c478bd9Sstevel@tonic-gate 	return (Z_OK);
27457c478bd9Sstevel@tonic-gate }
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate int
27487c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27497c478bd9Sstevel@tonic-gate {
27507c478bd9Sstevel@tonic-gate 	int err;
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
27537c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27567c478bd9Sstevel@tonic-gate 		return (err);
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
27597c478bd9Sstevel@tonic-gate 		return (err);
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 	return (Z_OK);
27627c478bd9Sstevel@tonic-gate }
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate static int
27657c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27667c478bd9Sstevel@tonic-gate {
27677c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
27687c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
27717c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
27727c478bd9Sstevel@tonic-gate 			continue;
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
27757c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
27767c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
27777c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
27787c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
27797c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
27827c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
27837c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
27847c478bd9Sstevel@tonic-gate 			return (Z_OK);
27857c478bd9Sstevel@tonic-gate 		}
27867c478bd9Sstevel@tonic-gate 	}
27877c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
27887c478bd9Sstevel@tonic-gate }
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate int
27917c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27927c478bd9Sstevel@tonic-gate {
27937c478bd9Sstevel@tonic-gate 	int err;
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
27967c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27997c478bd9Sstevel@tonic-gate 		return (err);
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
28027c478bd9Sstevel@tonic-gate 		return (err);
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 	return (Z_OK);
28057c478bd9Sstevel@tonic-gate }
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate int
28087c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
28097c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
28107c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
28117c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
28127c478bd9Sstevel@tonic-gate {
28137c478bd9Sstevel@tonic-gate 	int err;
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
28167c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28197c478bd9Sstevel@tonic-gate 		return (err);
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
28227c478bd9Sstevel@tonic-gate 		return (err);
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
28257c478bd9Sstevel@tonic-gate 		return (err);
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 	return (Z_OK);
28287c478bd9Sstevel@tonic-gate }
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate int
28317c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
28327c478bd9Sstevel@tonic-gate {
28337c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28347c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
28377c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
28407c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
28417c478bd9Sstevel@tonic-gate 		return (Z_OK);
28427c478bd9Sstevel@tonic-gate 	}
28437c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
28447c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
28457c478bd9Sstevel@tonic-gate 		return (Z_OK);
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
28487c478bd9Sstevel@tonic-gate }
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate int
28517c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
28527c478bd9Sstevel@tonic-gate {
28537c478bd9Sstevel@tonic-gate 	long long result;
28547c478bd9Sstevel@tonic-gate 	char *endptr;
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
28607c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	errno = 0;
28637c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
28647c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
28657c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28667c478bd9Sstevel@tonic-gate 	*value = result;
28677c478bd9Sstevel@tonic-gate 	return (Z_OK);
28687c478bd9Sstevel@tonic-gate }
28697c478bd9Sstevel@tonic-gate 
28707c478bd9Sstevel@tonic-gate int
28717c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
28727c478bd9Sstevel@tonic-gate     size_t val_sz)
28737c478bd9Sstevel@tonic-gate {
28747c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28757c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
28787c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
28817c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
28827c478bd9Sstevel@tonic-gate 	return (Z_OK);
28837c478bd9Sstevel@tonic-gate }
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate int
28867c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
28877c478bd9Sstevel@tonic-gate {
28887c478bd9Sstevel@tonic-gate 	unsigned long long result;
28897c478bd9Sstevel@tonic-gate 	long long neg_result;
28907c478bd9Sstevel@tonic-gate 	char *endptr;
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28937c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
28967c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	errno = 0;
28997c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
29007c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
29017c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29027c478bd9Sstevel@tonic-gate 	errno = 0;
29037c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
29047c478bd9Sstevel@tonic-gate 	/*
29057c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
29067c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
29077c478bd9Sstevel@tonic-gate 	 * need to look for this here.
29087c478bd9Sstevel@tonic-gate 	 */
29097c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
29107c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29117c478bd9Sstevel@tonic-gate 	*value = result;
29127c478bd9Sstevel@tonic-gate 	return (Z_OK);
29137c478bd9Sstevel@tonic-gate }
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate int
29167c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29177c478bd9Sstevel@tonic-gate {
29187c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
29197c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
29207c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29217c478bd9Sstevel@tonic-gate 	int err;
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	if (tabptr->zone_rctl_name == NULL ||
29247c478bd9Sstevel@tonic-gate 	    strlen(tabptr->zone_rctl_name) == 0)
29257c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29287c478bd9Sstevel@tonic-gate 		return (err);
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
29317c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
29327c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
29337c478bd9Sstevel@tonic-gate 			continue;
29347c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
29357c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
29367c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
29377c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
29387c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
29397c478bd9Sstevel@tonic-gate 			    val = val->next) {
29407c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
29417c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
29427c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
29437c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
29447c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
29457c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
29467c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
29477c478bd9Sstevel@tonic-gate 				    Z_OK))
29487c478bd9Sstevel@tonic-gate 					break;
29497c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
29507c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
29517c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
29527c478bd9Sstevel@tonic-gate 				    Z_OK))
29537c478bd9Sstevel@tonic-gate 					break;
29547c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
29557c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
29567c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
29577c478bd9Sstevel@tonic-gate 				    Z_OK))
29587c478bd9Sstevel@tonic-gate 					break;
29597c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
29607c478bd9Sstevel@tonic-gate 				    Z_OK)
29617c478bd9Sstevel@tonic-gate 					break;
29627c478bd9Sstevel@tonic-gate 			}
29637c478bd9Sstevel@tonic-gate 			return (Z_OK);
29647c478bd9Sstevel@tonic-gate 		}
29657c478bd9Sstevel@tonic-gate 	}
29667c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
29677c478bd9Sstevel@tonic-gate }
29687c478bd9Sstevel@tonic-gate 
29697c478bd9Sstevel@tonic-gate static int
29707c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29717c478bd9Sstevel@tonic-gate {
29727c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
29737c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29747c478bd9Sstevel@tonic-gate 	int err;
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
2977a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
29787c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
29797c478bd9Sstevel@tonic-gate 		return (err);
29807c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
29817c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
29827c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
29837c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
2984a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
29857c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
29867c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
29877c478bd9Sstevel@tonic-gate 			return (err);
2988a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
29897c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
29907c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
29917c478bd9Sstevel@tonic-gate 			return (err);
2992a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
29937c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
29947c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
29957c478bd9Sstevel@tonic-gate 			return (err);
29967c478bd9Sstevel@tonic-gate 	}
29977c478bd9Sstevel@tonic-gate 	return (Z_OK);
29987c478bd9Sstevel@tonic-gate }
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate int
30017c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30027c478bd9Sstevel@tonic-gate {
30037c478bd9Sstevel@tonic-gate 	int err;
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
30067c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30097c478bd9Sstevel@tonic-gate 		return (err);
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
30127c478bd9Sstevel@tonic-gate 		return (err);
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	return (Z_OK);
30157c478bd9Sstevel@tonic-gate }
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate static int
30187c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30197c478bd9Sstevel@tonic-gate {
30207c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
30217c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
30227c478bd9Sstevel@tonic-gate 	int name_result;
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
30257c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
30267c478bd9Sstevel@tonic-gate 			continue;
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
30297c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
30307c478bd9Sstevel@tonic-gate 			continue;
30317c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
30327c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
30337c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
30367c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
30377c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
30387c478bd9Sstevel@tonic-gate 			return (Z_OK);
30397c478bd9Sstevel@tonic-gate 		}
30407c478bd9Sstevel@tonic-gate 	}
30417c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
30427c478bd9Sstevel@tonic-gate }
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate int
30457c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30467c478bd9Sstevel@tonic-gate {
30477c478bd9Sstevel@tonic-gate 	int err;
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
30507c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30537c478bd9Sstevel@tonic-gate 		return (err);
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
30567c478bd9Sstevel@tonic-gate 		return (err);
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 	return (Z_OK);
30597c478bd9Sstevel@tonic-gate }
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate int
30627c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
30637c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
30647c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
30657c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
30667c478bd9Sstevel@tonic-gate {
30677c478bd9Sstevel@tonic-gate 	int err;
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || oldtabptr->zone_rctl_name == NULL ||
30707c478bd9Sstevel@tonic-gate 	    newtabptr == NULL || newtabptr->zone_rctl_name == NULL)
30717c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30747c478bd9Sstevel@tonic-gate 		return (err);
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
30777c478bd9Sstevel@tonic-gate 		return (err);
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
30807c478bd9Sstevel@tonic-gate 		return (err);
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate 	return (Z_OK);
30837c478bd9Sstevel@tonic-gate }
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate int
30867c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
30877c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
30887c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
30897c478bd9Sstevel@tonic-gate {
30907c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
30917c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
30927c478bd9Sstevel@tonic-gate 
30937c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
30947c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
30957c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
30967c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
30977c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
30987c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
30997c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31007c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
31017c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31027c478bd9Sstevel@tonic-gate 	if (last == NULL)
31037c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
31047c478bd9Sstevel@tonic-gate 	else
31057c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
31067c478bd9Sstevel@tonic-gate 	return (Z_OK);
31077c478bd9Sstevel@tonic-gate }
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate int
31107c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
31117c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
31127c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
31137c478bd9Sstevel@tonic-gate {
31147c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
31177c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
31187c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
31197c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
31207c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
31217c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
31227c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
31237c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
31247c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
31257c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
31267c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
31277c478bd9Sstevel@tonic-gate 			else
31287c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
31297c478bd9Sstevel@tonic-gate 			free(this);
31307c478bd9Sstevel@tonic-gate 			return (Z_OK);
31317c478bd9Sstevel@tonic-gate 		} else
31327c478bd9Sstevel@tonic-gate 			last = this;
31337c478bd9Sstevel@tonic-gate 	}
31347c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
31357c478bd9Sstevel@tonic-gate }
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate char *
31387c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
31397c478bd9Sstevel@tonic-gate {
31407c478bd9Sstevel@tonic-gate 	switch (errnum) {
31417c478bd9Sstevel@tonic-gate 	case Z_OK:
31427c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
31437c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
31447c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
31457c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
31467c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
31477c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
31487c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
31497c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
31507c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31517c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
31527c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
31537c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
31547c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
31557c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
31567c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
31577c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
31587c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
31597c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
31607c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
31617c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
31627c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
31637c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
31647c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
31657c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
31667c478bd9Sstevel@tonic-gate 	case Z_INVAL:
31677c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
31687c478bd9Sstevel@tonic-gate 	case Z_ACCES:
31697c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
31707c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
31717c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
31727c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
31737c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31747c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
31757c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
31767c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
31777c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
31787c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
31797c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
31807c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
31817c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
31827c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
31837c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
31847c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3185087719fdSdp 	case Z_BAD_ZONE_STATE:
31867c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
3187087719fdSdp 		    "Zone state is invalid for the requested operation"));
31887c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
31897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3190087719fdSdp 	case Z_NAME_IN_USE:
3191087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
31927c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
31937c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
31947c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
31957c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
31967c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
31977c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
31987c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
31997c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
32007c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
32017c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
32027c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
32037c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
32047c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
32057c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32067c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
32077c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
32087c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32097c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3210ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3211ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3212ffbafc53Scomay 		    "Specified privilege is prohibited"));
3213ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3214ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3215ffbafc53Scomay 		    "Required privilege is missing"));
3216ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3217ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3218ffbafc53Scomay 		    "Specified privilege is unknown"));
32199acbbeafSnn35248 	case Z_BRAND_ERROR:
32209acbbeafSnn35248 		return (dgettext(TEXT_DOMAIN,
32219acbbeafSnn35248 		    "Brand-specific error"));
32220209230bSgjelinek 	case Z_INCOMPATIBLE:
32230209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Incompatible settings"));
32240209230bSgjelinek 	case Z_ALIAS_DISALLOW:
32250209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32260209230bSgjelinek 		    "An incompatible rctl already exists for this property"));
32270209230bSgjelinek 	case Z_CLEAR_DISALLOW:
32280209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32290209230bSgjelinek 		    "Clearing this property is not allowed"));
32300209230bSgjelinek 	case Z_POOL:
32310209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error"));
32320209230bSgjelinek 	case Z_POOLS_NOT_ACTIVE:
32330209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Pools facility not active; "
32340209230bSgjelinek 		    "zone will not be bound to pool"));
32350209230bSgjelinek 	case Z_POOL_ENABLE:
32360209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32370209230bSgjelinek 		    "Could not enable pools facility"));
32380209230bSgjelinek 	case Z_NO_POOL:
32390209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32400209230bSgjelinek 		    "Pool not found; using default pool"));
32410209230bSgjelinek 	case Z_POOL_CREATE:
32420209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32430209230bSgjelinek 		    "Could not create a temporary pool"));
32440209230bSgjelinek 	case Z_POOL_BIND:
32450209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool"));
32467c478bd9Sstevel@tonic-gate 	default:
32477c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
32487c478bd9Sstevel@tonic-gate 	}
32497c478bd9Sstevel@tonic-gate }
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate /*
32527c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
32537c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
32547c478bd9Sstevel@tonic-gate  */
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate static int
32577c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
32587c478bd9Sstevel@tonic-gate {
32597c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
32607c478bd9Sstevel@tonic-gate 	int err;
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32637c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
32667c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
32677c478bd9Sstevel@tonic-gate 		return (err);
32687c478bd9Sstevel@tonic-gate 	}
32697c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
32707c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
32717c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
32727c478bd9Sstevel@tonic-gate 	return (Z_OK);
32737c478bd9Sstevel@tonic-gate }
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate static int
32767c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
32777c478bd9Sstevel@tonic-gate {
32787c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32797c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
32827c478bd9Sstevel@tonic-gate 	return (Z_OK);
32837c478bd9Sstevel@tonic-gate }
32847c478bd9Sstevel@tonic-gate 
32850209230bSgjelinek /*
32860209230bSgjelinek  * Do the work required to manipulate a process through libproc.
32870209230bSgjelinek  * If grab_process() returns no errors (0), then release_process()
32880209230bSgjelinek  * must eventually be called.
32890209230bSgjelinek  *
32900209230bSgjelinek  * Return values:
32910209230bSgjelinek  *      0 Successful creation of agent thread
32920209230bSgjelinek  *      1 Error grabbing
32930209230bSgjelinek  *      2 Error creating agent
32940209230bSgjelinek  */
32950209230bSgjelinek static int
32960209230bSgjelinek grab_process(pr_info_handle_t *p)
32970209230bSgjelinek {
32980209230bSgjelinek 	int ret;
32990209230bSgjelinek 
33000209230bSgjelinek 	if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) {
33010209230bSgjelinek 
33020209230bSgjelinek 		if (Psetflags(p->pr, PR_RLC) != 0) {
33030209230bSgjelinek 			Prelease(p->pr, 0);
33040209230bSgjelinek 			return (1);
33050209230bSgjelinek 		}
33060209230bSgjelinek 		if (Pcreate_agent(p->pr) == 0) {
33070209230bSgjelinek 			return (0);
33080209230bSgjelinek 
33090209230bSgjelinek 		} else {
33100209230bSgjelinek 			Prelease(p->pr, 0);
33110209230bSgjelinek 			return (2);
33120209230bSgjelinek 		}
33130209230bSgjelinek 	} else {
33140209230bSgjelinek 		return (1);
33150209230bSgjelinek 	}
33160209230bSgjelinek }
33170209230bSgjelinek 
33180209230bSgjelinek /*
33190209230bSgjelinek  * Release the specified process. This destroys the agent
33200209230bSgjelinek  * and releases the process. If the process is NULL, nothing
33210209230bSgjelinek  * is done. This function should only be called if grab_process()
33220209230bSgjelinek  * has previously been called and returned success.
33230209230bSgjelinek  *
33240209230bSgjelinek  * This function is Pgrab-safe.
33250209230bSgjelinek  */
33260209230bSgjelinek static void
33270209230bSgjelinek release_process(struct ps_prochandle *Pr)
33280209230bSgjelinek {
33290209230bSgjelinek 	if (Pr == NULL)
33300209230bSgjelinek 		return;
33310209230bSgjelinek 
33320209230bSgjelinek 	Pdestroy_agent(Pr);
33330209230bSgjelinek 	Prelease(Pr, 0);
33340209230bSgjelinek }
33350209230bSgjelinek 
33360209230bSgjelinek static boolean_t
33370209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p)
33380209230bSgjelinek {
33390209230bSgjelinek 	DIR *dirp;
33400209230bSgjelinek 	struct dirent *dentp;
33410209230bSgjelinek 	zoneid_t zoneid;
33420209230bSgjelinek 	int pid_self;
33430209230bSgjelinek 	psinfo_t psinfo;
33440209230bSgjelinek 
33450209230bSgjelinek 	if (zone_get_id(zonename, &zoneid) != 0)
33460209230bSgjelinek 		return (B_FALSE);
33470209230bSgjelinek 
33480209230bSgjelinek 	pid_self = getpid();
33490209230bSgjelinek 
33500209230bSgjelinek 	if ((dirp = opendir("/proc")) == NULL)
33510209230bSgjelinek 		return (B_FALSE);
33520209230bSgjelinek 
33530209230bSgjelinek 	while (dentp = readdir(dirp)) {
33540209230bSgjelinek 		p->pid = atoi(dentp->d_name);
33550209230bSgjelinek 
33560209230bSgjelinek 		/* Skip self */
33570209230bSgjelinek 		if (p->pid == pid_self)
33580209230bSgjelinek 			continue;
33590209230bSgjelinek 
33600209230bSgjelinek 		if (proc_get_psinfo(p->pid, &psinfo) != 0)
33610209230bSgjelinek 			continue;
33620209230bSgjelinek 
33630209230bSgjelinek 		if (psinfo.pr_zoneid != zoneid)
33640209230bSgjelinek 			continue;
33650209230bSgjelinek 
33660209230bSgjelinek 		/* attempt to grab process */
33670209230bSgjelinek 		if (grab_process(p) != 0)
33680209230bSgjelinek 			continue;
33690209230bSgjelinek 
33700209230bSgjelinek 		if (pr_getzoneid(p->pr) != zoneid) {
33710209230bSgjelinek 			release_process(p->pr);
33720209230bSgjelinek 			continue;
33730209230bSgjelinek 		}
33740209230bSgjelinek 
33750209230bSgjelinek 		(void) closedir(dirp);
33760209230bSgjelinek 		return (B_TRUE);
33770209230bSgjelinek 	}
33780209230bSgjelinek 
33790209230bSgjelinek 	(void) closedir(dirp);
33800209230bSgjelinek 	return (B_FALSE);
33810209230bSgjelinek }
33820209230bSgjelinek 
33830209230bSgjelinek static boolean_t
33840209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk)
33850209230bSgjelinek {
33860209230bSgjelinek 	if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST))
33870209230bSgjelinek 		return (B_FALSE);
33880209230bSgjelinek 
33890209230bSgjelinek 	if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
33900209230bSgjelinek 		return (B_TRUE);
33910209230bSgjelinek 
33920209230bSgjelinek 	while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) {
33930209230bSgjelinek 		if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
33940209230bSgjelinek 			return (B_TRUE);
33950209230bSgjelinek 	}
33960209230bSgjelinek 
33970209230bSgjelinek 	return (B_FALSE);
33980209230bSgjelinek }
33990209230bSgjelinek 
34000209230bSgjelinek /*
34010209230bSgjelinek  * Apply the current rctl settings to the specified, running zone.
34020209230bSgjelinek  */
34030209230bSgjelinek int
34040209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle)
34050209230bSgjelinek {
34060209230bSgjelinek 	int err;
34070209230bSgjelinek 	int res = Z_OK;
34080209230bSgjelinek 	rctlblk_t *rblk;
34090209230bSgjelinek 	pr_info_handle_t p;
34100209230bSgjelinek 	struct zone_rctltab rctl;
34110209230bSgjelinek 
34120209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK)
34130209230bSgjelinek 		return (err);
34140209230bSgjelinek 
34150209230bSgjelinek 	if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) {
34160209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
34170209230bSgjelinek 		return (Z_NOMEM);
34180209230bSgjelinek 	}
34190209230bSgjelinek 
34200209230bSgjelinek 	if (!grab_zone_proc(zone_name, &p)) {
34210209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
34220209230bSgjelinek 		free(rblk);
34230209230bSgjelinek 		return (Z_SYSTEM);
34240209230bSgjelinek 	}
34250209230bSgjelinek 
34260209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
34270209230bSgjelinek 		char *rname;
34280209230bSgjelinek 		struct zone_rctlvaltab *valptr;
34290209230bSgjelinek 
34300209230bSgjelinek 		rname = rctl.zone_rctl_name;
34310209230bSgjelinek 
34320209230bSgjelinek 		/* first delete all current privileged settings for this rctl */
34330209230bSgjelinek 		while (get_priv_rctl(p.pr, rname, rblk)) {
34340209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) !=
34350209230bSgjelinek 			    0) {
34360209230bSgjelinek 				res = Z_SYSTEM;
34370209230bSgjelinek 				goto done;
34380209230bSgjelinek 			}
34390209230bSgjelinek 		}
34400209230bSgjelinek 
34410209230bSgjelinek 		/* now set each new value for the rctl */
34420209230bSgjelinek 		for (valptr = rctl.zone_rctl_valptr; valptr != NULL;
34430209230bSgjelinek 		    valptr = valptr->zone_rctlval_next) {
34440209230bSgjelinek 			if ((err = zonecfg_construct_rctlblk(valptr, rblk))
34450209230bSgjelinek 			    != Z_OK) {
34460209230bSgjelinek 				res = errno = err;
34470209230bSgjelinek 				goto done;
34480209230bSgjelinek 			}
34490209230bSgjelinek 
34500209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) {
34510209230bSgjelinek 				res = Z_SYSTEM;
34520209230bSgjelinek 				goto done;
34530209230bSgjelinek 			}
34540209230bSgjelinek 		}
34550209230bSgjelinek 	}
34560209230bSgjelinek 
34570209230bSgjelinek done:
34580209230bSgjelinek 	release_process(p.pr);
34590209230bSgjelinek 	free(rblk);
34600209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
34610209230bSgjelinek 
34620209230bSgjelinek 	return (res);
34630209230bSgjelinek }
34640209230bSgjelinek 
34650209230bSgjelinek static const xmlChar *
34660209230bSgjelinek nm_to_dtd(char *nm)
34670209230bSgjelinek {
34680209230bSgjelinek 	if (strcmp(nm, "device") == 0)
34690209230bSgjelinek 		return (DTD_ELEM_DEVICE);
34700209230bSgjelinek 	if (strcmp(nm, "fs") == 0)
34710209230bSgjelinek 		return (DTD_ELEM_FS);
34720209230bSgjelinek 	if (strcmp(nm, "inherit-pkg-dir") == 0)
34730209230bSgjelinek 		return (DTD_ELEM_IPD);
34740209230bSgjelinek 	if (strcmp(nm, "net") == 0)
34750209230bSgjelinek 		return (DTD_ELEM_NET);
34760209230bSgjelinek 	if (strcmp(nm, "attr") == 0)
34770209230bSgjelinek 		return (DTD_ELEM_ATTR);
34780209230bSgjelinek 	if (strcmp(nm, "rctl") == 0)
34790209230bSgjelinek 		return (DTD_ELEM_RCTL);
34800209230bSgjelinek 	if (strcmp(nm, "dataset") == 0)
34810209230bSgjelinek 		return (DTD_ELEM_DATASET);
34820209230bSgjelinek 
34830209230bSgjelinek 	return (NULL);
34840209230bSgjelinek }
34850209230bSgjelinek 
34860209230bSgjelinek int
34870209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc)
34880209230bSgjelinek {
34890209230bSgjelinek 	int num = 0;
34900209230bSgjelinek 	const xmlChar *dtd;
34910209230bSgjelinek 	xmlNodePtr cur;
34920209230bSgjelinek 
34930209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
34940209230bSgjelinek 		return (num);
34950209230bSgjelinek 
34960209230bSgjelinek 	if (zonecfg_setent(handle) != Z_OK)
34970209230bSgjelinek 		return (num);
34980209230bSgjelinek 
34990209230bSgjelinek 	for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next)
35000209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd) == 0)
35010209230bSgjelinek 			num++;
35020209230bSgjelinek 
35030209230bSgjelinek 	(void) zonecfg_endent(handle);
35040209230bSgjelinek 
35050209230bSgjelinek 	return (num);
35060209230bSgjelinek }
35070209230bSgjelinek 
35080209230bSgjelinek int
35090209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc)
35100209230bSgjelinek {
35110209230bSgjelinek 	int err;
35120209230bSgjelinek 	const xmlChar *dtd;
35130209230bSgjelinek 	xmlNodePtr cur;
35140209230bSgjelinek 
35150209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
35160209230bSgjelinek 		return (Z_NO_RESOURCE_TYPE);
35170209230bSgjelinek 
35180209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
35190209230bSgjelinek 		return (err);
35200209230bSgjelinek 
35210209230bSgjelinek 	cur = handle->zone_dh_cur;
35220209230bSgjelinek 	while (cur != NULL) {
35230209230bSgjelinek 		xmlNodePtr tmp;
35240209230bSgjelinek 
35250209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd)) {
35260209230bSgjelinek 			cur = cur->next;
35270209230bSgjelinek 			continue;
35280209230bSgjelinek 		}
35290209230bSgjelinek 
35300209230bSgjelinek 		tmp = cur->next;
35310209230bSgjelinek 		xmlUnlinkNode(cur);
35320209230bSgjelinek 		xmlFreeNode(cur);
35330209230bSgjelinek 		cur = tmp;
35340209230bSgjelinek 	}
35350209230bSgjelinek 
35360209230bSgjelinek 	(void) zonecfg_endent(handle);
35370209230bSgjelinek 	return (Z_OK);
35380209230bSgjelinek }
35390209230bSgjelinek 
35400209230bSgjelinek static boolean_t
35410209230bSgjelinek valid_uint(char *s, uint64_t *n)
35420209230bSgjelinek {
35430209230bSgjelinek 	char *endp;
35440209230bSgjelinek 
35450209230bSgjelinek 	/* strtoull accepts '-'?! so we want to flag that as an error */
35460209230bSgjelinek 	if (strchr(s, '-') != NULL)
35470209230bSgjelinek 		return (B_FALSE);
35480209230bSgjelinek 
35490209230bSgjelinek 	errno = 0;
35500209230bSgjelinek 	*n = strtoull(s, &endp, 10);
35510209230bSgjelinek 
35520209230bSgjelinek 	if (errno != 0 || *endp != '\0')
35530209230bSgjelinek 		return (B_FALSE);
35540209230bSgjelinek 	return (B_TRUE);
35550209230bSgjelinek }
35560209230bSgjelinek 
35570209230bSgjelinek /*
35580209230bSgjelinek  * Convert a string representing a number (possibly a fraction) into an integer.
35590209230bSgjelinek  * The string can have a modifier (K, M, G or T).   The modifiers are treated
35600209230bSgjelinek  * as powers of two (not 10).
35610209230bSgjelinek  */
35620209230bSgjelinek int
35630209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes)
35640209230bSgjelinek {
35650209230bSgjelinek 	long double val;
35660209230bSgjelinek 	char *unitp;
35670209230bSgjelinek 	uint64_t scale;
35680209230bSgjelinek 
35690209230bSgjelinek 	if ((val = strtold(str, &unitp)) < 0)
35700209230bSgjelinek 		return (-1);
35710209230bSgjelinek 
35720209230bSgjelinek 	/* remove any leading white space from units string */
35730209230bSgjelinek 	while (isspace(*unitp) != 0)
35740209230bSgjelinek 		++unitp;
35750209230bSgjelinek 
35760209230bSgjelinek 	/* if no units explicitly set, error */
35770209230bSgjelinek 	if (unitp == NULL || *unitp == '\0') {
35780209230bSgjelinek 		scale = 1;
35790209230bSgjelinek 	} else {
35800209230bSgjelinek 		int i;
35810209230bSgjelinek 		char *units[] = {"K", "M", "G", "T", NULL};
35820209230bSgjelinek 
35830209230bSgjelinek 		scale = 1024;
35840209230bSgjelinek 
35850209230bSgjelinek 		/* update scale based on units */
35860209230bSgjelinek 		for (i = 0; units[i] != NULL; i++) {
35870209230bSgjelinek 			if (strcasecmp(unitp, units[i]) == 0)
35880209230bSgjelinek 				break;
35890209230bSgjelinek 			scale <<= 10;
35900209230bSgjelinek 		}
35910209230bSgjelinek 
35920209230bSgjelinek 		if (units[i] == NULL)
35930209230bSgjelinek 			return (-1);
35940209230bSgjelinek 	}
35950209230bSgjelinek 
35960209230bSgjelinek 	*bytes = (uint64_t)(val * scale);
35970209230bSgjelinek 	return (0);
35980209230bSgjelinek }
35990209230bSgjelinek 
36000209230bSgjelinek boolean_t
36010209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr)
36020209230bSgjelinek {
36030209230bSgjelinek 	uint64_t low, high;
36040209230bSgjelinek 
36050209230bSgjelinek 	if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) ||
36060209230bSgjelinek 	    low < 1 || low > high)
36070209230bSgjelinek 		return (B_FALSE);
36080209230bSgjelinek 
36090209230bSgjelinek 	return (B_TRUE);
36100209230bSgjelinek }
36110209230bSgjelinek 
36120209230bSgjelinek boolean_t
36130209230bSgjelinek zonecfg_valid_importance(char *impstr)
36140209230bSgjelinek {
36150209230bSgjelinek 	uint64_t num;
36160209230bSgjelinek 
36170209230bSgjelinek 	if (!valid_uint(impstr, &num))
36180209230bSgjelinek 		return (B_FALSE);
36190209230bSgjelinek 
36200209230bSgjelinek 	return (B_TRUE);
36210209230bSgjelinek }
36220209230bSgjelinek 
36230209230bSgjelinek boolean_t
36240209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit)
36250209230bSgjelinek {
36260209230bSgjelinek 	int i;
36270209230bSgjelinek 
36280209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
36290209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
36300209230bSgjelinek 			break;
36310209230bSgjelinek 
36320209230bSgjelinek 	if (aliases[i].shortname == NULL)
36330209230bSgjelinek 		return (B_FALSE);
36340209230bSgjelinek 
36350209230bSgjelinek 	if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit)
36360209230bSgjelinek 		return (B_FALSE);
36370209230bSgjelinek 
36380209230bSgjelinek 	return (B_TRUE);
36390209230bSgjelinek }
36400209230bSgjelinek 
36410209230bSgjelinek boolean_t
36420209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val)
36430209230bSgjelinek {
36440209230bSgjelinek 	if (zonecfg_str_to_bytes(memstr, mem_val) != 0)
36450209230bSgjelinek 		return (B_FALSE);
36460209230bSgjelinek 
36470209230bSgjelinek 	return (B_TRUE);
36480209230bSgjelinek }
36490209230bSgjelinek 
36500209230bSgjelinek static int
36510209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res)
36520209230bSgjelinek {
36530209230bSgjelinek 	(void) strlcpy(pool_err, pool_strerror(pool_error()), err_size);
36540209230bSgjelinek 	return (res);
36550209230bSgjelinek }
36560209230bSgjelinek 
36570209230bSgjelinek static int
36580209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool,
36590209230bSgjelinek     char *name, int min, int max)
36600209230bSgjelinek {
36610209230bSgjelinek 	pool_resource_t *res;
36620209230bSgjelinek 	pool_elem_t *elem;
36630209230bSgjelinek 	pool_value_t *val;
36640209230bSgjelinek 
36650209230bSgjelinek 	if ((res = pool_resource_create(pconf, "pset", name)) == NULL)
36660209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36670209230bSgjelinek 
36680209230bSgjelinek 	if (pool_associate(pconf, pool, res) != PO_SUCCESS)
36690209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36700209230bSgjelinek 
36710209230bSgjelinek 	if ((elem = pool_resource_to_elem(pconf, res)) == NULL)
36720209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36730209230bSgjelinek 
36740209230bSgjelinek 	if ((val = pool_value_alloc()) == NULL)
36750209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36760209230bSgjelinek 
36770209230bSgjelinek 	/* set the maximum number of cpus for the pset */
36780209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)max);
36790209230bSgjelinek 
36800209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) {
36810209230bSgjelinek 		pool_value_free(val);
36820209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36830209230bSgjelinek 	}
36840209230bSgjelinek 
36850209230bSgjelinek 	/* set the minimum number of cpus for the pset */
36860209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)min);
36870209230bSgjelinek 
36880209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) {
36890209230bSgjelinek 		pool_value_free(val);
36900209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36910209230bSgjelinek 	}
36920209230bSgjelinek 
36930209230bSgjelinek 	pool_value_free(val);
36940209230bSgjelinek 
36950209230bSgjelinek 	return (Z_OK);
36960209230bSgjelinek }
36970209230bSgjelinek 
36980209230bSgjelinek static int
36990209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name,
37000209230bSgjelinek     struct zone_psettab *pset_tab)
37010209230bSgjelinek {
37020209230bSgjelinek 	pool_t *pool;
37030209230bSgjelinek 	int res = Z_OK;
37040209230bSgjelinek 
37050209230bSgjelinek 	/* create a temporary pool configuration */
37060209230bSgjelinek 	if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) {
37070209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37080209230bSgjelinek 		return (res);
37090209230bSgjelinek 	}
37100209230bSgjelinek 
37110209230bSgjelinek 	if ((pool = pool_create(pconf, name)) == NULL) {
37120209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
37130209230bSgjelinek 		goto done;
37140209230bSgjelinek 	}
37150209230bSgjelinek 
37160209230bSgjelinek 	/* set pool importance */
37170209230bSgjelinek 	if (pset_tab->zone_importance[0] != '\0') {
37180209230bSgjelinek 		pool_elem_t *elem;
37190209230bSgjelinek 		pool_value_t *val;
37200209230bSgjelinek 
37210209230bSgjelinek 		if ((elem = pool_to_elem(pconf, pool)) == NULL) {
37220209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37230209230bSgjelinek 			goto done;
37240209230bSgjelinek 		}
37250209230bSgjelinek 
37260209230bSgjelinek 		if ((val = pool_value_alloc()) == NULL) {
37270209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37280209230bSgjelinek 			goto done;
37290209230bSgjelinek 		}
37300209230bSgjelinek 
37310209230bSgjelinek 		pool_value_set_int64(val,
37320209230bSgjelinek 		    (int64_t)atoi(pset_tab->zone_importance));
37330209230bSgjelinek 
37340209230bSgjelinek 		if (pool_put_property(pconf, elem, "pool.importance", val)
37350209230bSgjelinek 		    != PO_SUCCESS) {
37360209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37370209230bSgjelinek 			pool_value_free(val);
37380209230bSgjelinek 			goto done;
37390209230bSgjelinek 		}
37400209230bSgjelinek 
37410209230bSgjelinek 		pool_value_free(val);
37420209230bSgjelinek 	}
37430209230bSgjelinek 
37440209230bSgjelinek 	if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name,
37450209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_min),
37460209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_max))) != Z_OK)
37470209230bSgjelinek 		goto done;
37480209230bSgjelinek 
37490209230bSgjelinek 	/* validation */
37500209230bSgjelinek 	if (pool_conf_status(pconf) == POF_INVALID) {
37510209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37520209230bSgjelinek 		goto done;
37530209230bSgjelinek 	}
37540209230bSgjelinek 
37550209230bSgjelinek 	/*
37560209230bSgjelinek 	 * This validation is the one we expect to fail if the user specified
37570209230bSgjelinek 	 * an invalid configuration (too many cpus) for this system.
37580209230bSgjelinek 	 */
37590209230bSgjelinek 	if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) {
37600209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
37610209230bSgjelinek 		goto done;
37620209230bSgjelinek 	}
37630209230bSgjelinek 
37640209230bSgjelinek 	/*
37650209230bSgjelinek 	 * Commit the dynamic configuration but not the pool configuration
37660209230bSgjelinek 	 * file.
37670209230bSgjelinek 	 */
37680209230bSgjelinek 	if (pool_conf_commit(pconf, 1) != PO_SUCCESS)
37690209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37700209230bSgjelinek 
37710209230bSgjelinek done:
37720209230bSgjelinek 	(void) pool_conf_close(pconf);
37730209230bSgjelinek 	return (res);
37740209230bSgjelinek }
37750209230bSgjelinek 
37760209230bSgjelinek static int
37770209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset,
37780209230bSgjelinek     struct zone_psettab *pset_tab)
37790209230bSgjelinek {
37800209230bSgjelinek 	int nfound = 0;
37810209230bSgjelinek 	pool_elem_t *pe;
37820209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
37830209230bSgjelinek 	uint64_t val_uint;
37840209230bSgjelinek 
37850209230bSgjelinek 	if (pool != NULL) {
37860209230bSgjelinek 		pe = pool_to_elem(pconf, pool);
37870209230bSgjelinek 		if (pool_get_property(pconf, pe, "pool.importance", pv)
37880209230bSgjelinek 		    != POC_INVAL) {
37890209230bSgjelinek 			int64_t val_int;
37900209230bSgjelinek 
37910209230bSgjelinek 			(void) pool_value_get_int64(pv, &val_int);
37920209230bSgjelinek 			(void) snprintf(pset_tab->zone_importance,
37930209230bSgjelinek 			    sizeof (pset_tab->zone_importance), "%d", val_int);
37940209230bSgjelinek 			nfound++;
37950209230bSgjelinek 		}
37960209230bSgjelinek 	}
37970209230bSgjelinek 
37980209230bSgjelinek 	if (pset != NULL) {
37990209230bSgjelinek 		pe = pool_resource_to_elem(pconf, pset);
38000209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) {
38010209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
38020209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_min,
38030209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_min), "%u", val_uint);
38040209230bSgjelinek 			nfound++;
38050209230bSgjelinek 		}
38060209230bSgjelinek 
38070209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) {
38080209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
38090209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_max,
38100209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_max), "%u", val_uint);
38110209230bSgjelinek 			nfound++;
38120209230bSgjelinek 		}
38130209230bSgjelinek 	}
38140209230bSgjelinek 
38150209230bSgjelinek 	pool_value_free(pv);
38160209230bSgjelinek 
38170209230bSgjelinek 	if (nfound == 3)
38180209230bSgjelinek 		return (PO_SUCCESS);
38190209230bSgjelinek 
38200209230bSgjelinek 	return (PO_FAIL);
38210209230bSgjelinek }
38220209230bSgjelinek 
38230209230bSgjelinek /*
38240209230bSgjelinek  * Determine if a tmp pool is configured and if so, if the configuration is
38250209230bSgjelinek  * still valid or if it has been changed since the tmp pool was created.
38260209230bSgjelinek  * If the tmp pool configuration is no longer valid, delete the tmp pool.
38270209230bSgjelinek  *
38280209230bSgjelinek  * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration.
38290209230bSgjelinek  */
38300209230bSgjelinek static int
38310209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err,
38320209230bSgjelinek     int err_size, struct zone_psettab *pset_tab, boolean_t *exists)
38330209230bSgjelinek {
38340209230bSgjelinek 	int res = Z_OK;
38350209230bSgjelinek 	pool_t *pool;
38360209230bSgjelinek 	pool_resource_t *pset;
38370209230bSgjelinek 	struct zone_psettab pset_current;
38380209230bSgjelinek 
38390209230bSgjelinek 	*exists = B_FALSE;
38400209230bSgjelinek 
38410209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
38420209230bSgjelinek 	    != PO_SUCCESS) {
38430209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
38440209230bSgjelinek 		return (res);
38450209230bSgjelinek 	}
38460209230bSgjelinek 
38470209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
38480209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
38490209230bSgjelinek 
38500209230bSgjelinek 	if (pool == NULL && pset == NULL) {
38510209230bSgjelinek 		/* no tmp pool configured */
38520209230bSgjelinek 		goto done;
38530209230bSgjelinek 	}
38540209230bSgjelinek 
38550209230bSgjelinek 	/*
38560209230bSgjelinek 	 * If an existing tmp pool for this zone is configured with the proper
38570209230bSgjelinek 	 * settings, then the tmp pool is valid.
38580209230bSgjelinek 	 */
38590209230bSgjelinek 	if (get_running_tmp_pset(pconf, pool, pset, &pset_current)
38600209230bSgjelinek 	    == PO_SUCCESS &&
38610209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_min,
38620209230bSgjelinek 	    pset_current.zone_ncpu_min) == 0 &&
38630209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_max,
38640209230bSgjelinek 	    pset_current.zone_ncpu_max) == 0 &&
38650209230bSgjelinek 	    strcmp(pset_tab->zone_importance,
38660209230bSgjelinek 	    pset_current.zone_importance) == 0) {
38670209230bSgjelinek 		*exists = B_TRUE;
38680209230bSgjelinek 
38690209230bSgjelinek 	} else {
38700209230bSgjelinek 		/*
38710209230bSgjelinek 		 * An out-of-date tmp pool configuration exists.  Delete it
38720209230bSgjelinek 		 * so that we can create the correct tmp pool config.
38730209230bSgjelinek 		 */
38740209230bSgjelinek 		if (pset != NULL &&
38750209230bSgjelinek 		    pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
38760209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38770209230bSgjelinek 			goto done;
38780209230bSgjelinek 		}
38790209230bSgjelinek 
38800209230bSgjelinek 		if (pool != NULL &&
38810209230bSgjelinek 		    pool_destroy(pconf, pool) != PO_SUCCESS) {
38820209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38830209230bSgjelinek 			goto done;
38840209230bSgjelinek 		}
38850209230bSgjelinek 
38860209230bSgjelinek 		/* commit dynamic config */
38870209230bSgjelinek 		if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
38880209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38890209230bSgjelinek 	}
38900209230bSgjelinek 
38910209230bSgjelinek done:
38920209230bSgjelinek 	(void) pool_conf_close(pconf);
38930209230bSgjelinek 
38940209230bSgjelinek 	return (res);
38950209230bSgjelinek }
38960209230bSgjelinek 
38970209230bSgjelinek /*
38980209230bSgjelinek  * Destroy any existing tmp pool.
38990209230bSgjelinek  */
39000209230bSgjelinek int
39010209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size)
39020209230bSgjelinek {
39030209230bSgjelinek 	int status;
39040209230bSgjelinek 	int res = Z_OK;
39050209230bSgjelinek 	pool_conf_t *pconf;
39060209230bSgjelinek 	pool_t *pool;
39070209230bSgjelinek 	pool_resource_t *pset;
39080209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
39090209230bSgjelinek 
39100209230bSgjelinek 	/* if pools not enabled then nothing to do */
39110209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
39120209230bSgjelinek 		return (Z_OK);
39130209230bSgjelinek 
39140209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
39150209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39160209230bSgjelinek 
39170209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
39180209230bSgjelinek 
39190209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
39200209230bSgjelinek 	    != PO_SUCCESS) {
39210209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39220209230bSgjelinek 		pool_conf_free(pconf);
39230209230bSgjelinek 		return (res);
39240209230bSgjelinek 	}
39250209230bSgjelinek 
39260209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
39270209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
39280209230bSgjelinek 
39290209230bSgjelinek 	if (pool == NULL && pset == NULL) {
39300209230bSgjelinek 		/* nothing to destroy, we're done */
39310209230bSgjelinek 		goto done;
39320209230bSgjelinek 	}
39330209230bSgjelinek 
39340209230bSgjelinek 	if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
39350209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39360209230bSgjelinek 		goto done;
39370209230bSgjelinek 	}
39380209230bSgjelinek 
39390209230bSgjelinek 	if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) {
39400209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39410209230bSgjelinek 		goto done;
39420209230bSgjelinek 	}
39430209230bSgjelinek 
39440209230bSgjelinek 	/* commit dynamic config */
39450209230bSgjelinek 	if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
39460209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39470209230bSgjelinek 
39480209230bSgjelinek done:
39490209230bSgjelinek 	(void) pool_conf_close(pconf);
39500209230bSgjelinek 	pool_conf_free(pconf);
39510209230bSgjelinek 
39520209230bSgjelinek 	return (res);
39530209230bSgjelinek }
39540209230bSgjelinek 
39550209230bSgjelinek /*
39560209230bSgjelinek  * Attempt to bind to a tmp pool for this zone.  If there is no tmp pool
39570209230bSgjelinek  * configured, we just return Z_OK.
39580209230bSgjelinek  *
39590209230bSgjelinek  * We either attempt to create the tmp pool for this zone or rebind to an
39600209230bSgjelinek  * existing tmp pool for this zone.
39610209230bSgjelinek  *
39620209230bSgjelinek  * Rebinding is used when a zone with a tmp pool reboots so that we don't have
39630209230bSgjelinek  * to recreate the tmp pool.  To do this we need to be sure we work correctly
39640209230bSgjelinek  * for the following cases:
39650209230bSgjelinek  *
39660209230bSgjelinek  *	- there is an existing, properly configured tmp pool.
39670209230bSgjelinek  *	- zonecfg added tmp pool after zone was booted, must now create.
39680209230bSgjelinek  *	- zonecfg updated tmp pool config after zone was booted, in this case
39690209230bSgjelinek  *	  we destroy the old tmp pool and create a new one.
39700209230bSgjelinek  */
39710209230bSgjelinek int
39720209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
39730209230bSgjelinek     int err_size)
39740209230bSgjelinek {
39750209230bSgjelinek 	struct zone_psettab pset_tab;
39760209230bSgjelinek 	int err;
39770209230bSgjelinek 	int status;
39780209230bSgjelinek 	pool_conf_t *pconf;
39790209230bSgjelinek 	boolean_t exists;
39800209230bSgjelinek 	char zone_name[ZONENAME_MAX];
39810209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
39820209230bSgjelinek 
39830209230bSgjelinek 	(void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name));
39840209230bSgjelinek 
39850209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
39860209230bSgjelinek 
39870209230bSgjelinek 	/* if no temporary pool configured, we're done */
39880209230bSgjelinek 	if (err == Z_NO_ENTRY)
39890209230bSgjelinek 		return (Z_OK);
39900209230bSgjelinek 
39910209230bSgjelinek 	/*
39920209230bSgjelinek 	 * importance might not have a value but we need to validate it here,
39930209230bSgjelinek 	 * so set the default.
39940209230bSgjelinek 	 */
39950209230bSgjelinek 	if (pset_tab.zone_importance[0] == '\0')
39960209230bSgjelinek 		(void) strlcpy(pset_tab.zone_importance, "1",
39970209230bSgjelinek 		    sizeof (pset_tab.zone_importance));
39980209230bSgjelinek 
39990209230bSgjelinek 	/* if pools not enabled, enable them now */
40000209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
40010209230bSgjelinek 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS)
40020209230bSgjelinek 			return (Z_POOL_ENABLE);
40030209230bSgjelinek 	}
40040209230bSgjelinek 
40050209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
40060209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40070209230bSgjelinek 
40080209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
40090209230bSgjelinek 
40100209230bSgjelinek 	/*
40110209230bSgjelinek 	 * Check if a valid tmp pool/pset already exists.  If so, we just
40120209230bSgjelinek 	 * reuse it.
40130209230bSgjelinek 	 */
40140209230bSgjelinek 	if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size,
40150209230bSgjelinek 	    &pset_tab, &exists)) != Z_OK) {
40160209230bSgjelinek 		pool_conf_free(pconf);
40170209230bSgjelinek 		return (err);
40180209230bSgjelinek 	}
40190209230bSgjelinek 
40200209230bSgjelinek 	if (!exists)
40210209230bSgjelinek 		err = create_tmp_pool(pool_err, err_size, pconf, tmp_name,
40220209230bSgjelinek 		    &pset_tab);
40230209230bSgjelinek 
40240209230bSgjelinek 	pool_conf_free(pconf);
40250209230bSgjelinek 
40260209230bSgjelinek 	if (err != Z_OK)
40270209230bSgjelinek 		return (err);
40280209230bSgjelinek 
40290209230bSgjelinek 	/* Bind the zone to the pool. */
40300209230bSgjelinek 	if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS)
40310209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL_BIND));
40320209230bSgjelinek 
40330209230bSgjelinek 	return (Z_OK);
40340209230bSgjelinek }
40350209230bSgjelinek 
40360209230bSgjelinek /*
40370209230bSgjelinek  * Attempt to bind to a permanent pool for this zone.  If there is no
40380209230bSgjelinek  * permanent pool configured, we just return Z_OK.
40390209230bSgjelinek  */
40400209230bSgjelinek int
40410209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
40420209230bSgjelinek     int err_size)
40430209230bSgjelinek {
40440209230bSgjelinek 	pool_conf_t *poolconf;
40450209230bSgjelinek 	pool_t *pool;
40460209230bSgjelinek 	char poolname[MAXPATHLEN];
40470209230bSgjelinek 	int status;
40480209230bSgjelinek 	int error;
40490209230bSgjelinek 
40500209230bSgjelinek 	/*
40510209230bSgjelinek 	 * Find the pool mentioned in the zone configuration, and bind to it.
40520209230bSgjelinek 	 */
40530209230bSgjelinek 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
40540209230bSgjelinek 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
40550209230bSgjelinek 		/*
40560209230bSgjelinek 		 * The property is not set on the zone, so the pool
40570209230bSgjelinek 		 * should be bound to the default pool.  But that's
40580209230bSgjelinek 		 * already done by the kernel, so we can just return.
40590209230bSgjelinek 		 */
40600209230bSgjelinek 		return (Z_OK);
40610209230bSgjelinek 	}
40620209230bSgjelinek 	if (error != Z_OK) {
40630209230bSgjelinek 		/*
40640209230bSgjelinek 		 * Not an error, even though it shouldn't be happening.
40650209230bSgjelinek 		 */
40660209230bSgjelinek 		return (Z_OK);
40670209230bSgjelinek 	}
40680209230bSgjelinek 	/*
40690209230bSgjelinek 	 * Don't do anything if pools aren't enabled.
40700209230bSgjelinek 	 */
40710209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
40720209230bSgjelinek 		return (Z_POOLS_NOT_ACTIVE);
40730209230bSgjelinek 
40740209230bSgjelinek 	/*
40750209230bSgjelinek 	 * Try to provide a sane error message if the requested pool doesn't
40760209230bSgjelinek 	 * exist.
40770209230bSgjelinek 	 */
40780209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
40790209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40800209230bSgjelinek 
40810209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
40820209230bSgjelinek 	    PO_SUCCESS) {
40830209230bSgjelinek 		pool_conf_free(poolconf);
40840209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40850209230bSgjelinek 	}
40860209230bSgjelinek 	pool = pool_get_pool(poolconf, poolname);
40870209230bSgjelinek 	(void) pool_conf_close(poolconf);
40880209230bSgjelinek 	pool_conf_free(poolconf);
40890209230bSgjelinek 	if (pool == NULL)
40900209230bSgjelinek 		return (Z_NO_POOL);
40910209230bSgjelinek 
40920209230bSgjelinek 	/*
40930209230bSgjelinek 	 * Bind the zone to the pool.
40940209230bSgjelinek 	 */
40950209230bSgjelinek 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
40960209230bSgjelinek 		/* if bind fails, return poolname for the error msg */
40970209230bSgjelinek 		(void) strlcpy(pool_err, poolname, err_size);
40980209230bSgjelinek 		return (Z_POOL_BIND);
40990209230bSgjelinek 	}
41000209230bSgjelinek 
41010209230bSgjelinek 	return (Z_OK);
41020209230bSgjelinek }
41030209230bSgjelinek 
41040209230bSgjelinek 
41050209230bSgjelinek static boolean_t
41060209230bSgjelinek svc_enabled(char *svc_name)
41070209230bSgjelinek {
41080209230bSgjelinek 	scf_simple_prop_t	*prop;
41090209230bSgjelinek 	boolean_t		found = B_FALSE;
41100209230bSgjelinek 
41110209230bSgjelinek 	prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL,
41120209230bSgjelinek 	    SCF_PROPERTY_ENABLED);
41130209230bSgjelinek 
41140209230bSgjelinek 	if (scf_simple_prop_numvalues(prop) == 1 &&
41150209230bSgjelinek 	    *scf_simple_prop_next_boolean(prop) != 0)
41160209230bSgjelinek 		found = B_TRUE;
41170209230bSgjelinek 
41180209230bSgjelinek 	scf_simple_prop_free(prop);
41190209230bSgjelinek 
41200209230bSgjelinek 	return (found);
41210209230bSgjelinek }
41220209230bSgjelinek 
41230209230bSgjelinek /*
41240209230bSgjelinek  * If the zone has capped-memory, make sure the rcap service is enabled.
41250209230bSgjelinek  */
41260209230bSgjelinek int
41270209230bSgjelinek zonecfg_enable_rcapd(char *err, int size)
41280209230bSgjelinek {
41290209230bSgjelinek 	if (!svc_enabled(RCAP_SERVICE) &&
41300209230bSgjelinek 	    smf_enable_instance(RCAP_SERVICE, 0) == -1) {
41310209230bSgjelinek 		(void) strlcpy(err, scf_strerror(scf_error()), size);
41320209230bSgjelinek 		return (Z_SYSTEM);
41330209230bSgjelinek 	}
41340209230bSgjelinek 
41350209230bSgjelinek 	return (Z_OK);
41360209230bSgjelinek }
41370209230bSgjelinek 
41380209230bSgjelinek /*
41390209230bSgjelinek  * Return true if pset has cpu range specified and poold is not enabled.
41400209230bSgjelinek  */
41410209230bSgjelinek boolean_t
41420209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle)
41430209230bSgjelinek {
41440209230bSgjelinek 	struct zone_psettab pset_tab;
41450209230bSgjelinek 	int min, max;
41460209230bSgjelinek 	int err;
41470209230bSgjelinek 
41480209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
41490209230bSgjelinek 
41500209230bSgjelinek 	/* if no temporary pool configured, we're done */
41510209230bSgjelinek 	if (err == Z_NO_ENTRY)
41520209230bSgjelinek 		return (B_FALSE);
41530209230bSgjelinek 
41540209230bSgjelinek 	min = atoi(pset_tab.zone_ncpu_min);
41550209230bSgjelinek 	max = atoi(pset_tab.zone_ncpu_max);
41560209230bSgjelinek 
41570209230bSgjelinek 	/* range not specified, no need for poold */
41580209230bSgjelinek 	if (min == max)
41590209230bSgjelinek 		return (B_FALSE);
41600209230bSgjelinek 
41610209230bSgjelinek 	/* we have a range, check if poold service is enabled */
41620209230bSgjelinek 	if (svc_enabled(POOLD_SERVICE))
41630209230bSgjelinek 		return (B_FALSE);
41640209230bSgjelinek 
41650209230bSgjelinek 	return (B_TRUE);
41660209230bSgjelinek }
41670209230bSgjelinek 
41680209230bSgjelinek static int
41690209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize)
41700209230bSgjelinek {
41710209230bSgjelinek 	int status;
41720209230bSgjelinek 	pool_conf_t *poolconf;
41730209230bSgjelinek 	pool_t *pool;
41740209230bSgjelinek 	pool_elem_t *pe;
41750209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
41760209230bSgjelinek 	const char *sched_str;
41770209230bSgjelinek 
41780209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
41790209230bSgjelinek 		return (Z_NO_POOL);
41800209230bSgjelinek 
41810209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
41820209230bSgjelinek 		return (Z_NO_POOL);
41830209230bSgjelinek 
41840209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
41850209230bSgjelinek 	    PO_SUCCESS) {
41860209230bSgjelinek 		pool_conf_free(poolconf);
41870209230bSgjelinek 		return (Z_NO_POOL);
41880209230bSgjelinek 	}
41890209230bSgjelinek 
41900209230bSgjelinek 	if ((pool = pool_get_pool(poolconf, poolname)) == NULL) {
41910209230bSgjelinek 		(void) pool_conf_close(poolconf);
41920209230bSgjelinek 		pool_conf_free(poolconf);
41930209230bSgjelinek 		return (Z_NO_POOL);
41940209230bSgjelinek 	}
41950209230bSgjelinek 
41960209230bSgjelinek 	pe = pool_to_elem(poolconf, pool);
41970209230bSgjelinek 	if (pool_get_property(poolconf, pe, "pool.scheduler", pv)
41980209230bSgjelinek 	    != POC_INVAL) {
41990209230bSgjelinek 		(void) pool_value_get_string(pv, &sched_str);
42000209230bSgjelinek 		if (strlcpy(class, sched_str, clsize) >= clsize)
42010209230bSgjelinek 			return (Z_TOO_BIG);
42020209230bSgjelinek 	}
42030209230bSgjelinek 
42040209230bSgjelinek 	(void) pool_conf_close(poolconf);
42050209230bSgjelinek 	pool_conf_free(poolconf);
42060209230bSgjelinek 	return (Z_OK);
42070209230bSgjelinek }
42080209230bSgjelinek 
42090209230bSgjelinek /*
42100209230bSgjelinek  * Get the default scheduling class for the zone.  This will either be the
42110209230bSgjelinek  * class set on the zone's pool or the system default scheduling class.
42120209230bSgjelinek  */
42130209230bSgjelinek int
42140209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize)
42150209230bSgjelinek {
42160209230bSgjelinek 	char poolname[MAXPATHLEN];
42170209230bSgjelinek 
42180209230bSgjelinek 	if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) {
42190209230bSgjelinek 		/* check if the zone's pool specified a sched class */
42200209230bSgjelinek 		if (get_pool_sched_class(poolname, class, clsize) == Z_OK)
42210209230bSgjelinek 			return (Z_OK);
42220209230bSgjelinek 	}
42230209230bSgjelinek 
42240209230bSgjelinek 	if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1)
42250209230bSgjelinek 		return (Z_TOO_BIG);
42260209230bSgjelinek 
42270209230bSgjelinek 	return (Z_OK);
42280209230bSgjelinek }
42290209230bSgjelinek 
42307c478bd9Sstevel@tonic-gate int
42317c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle)
42327c478bd9Sstevel@tonic-gate {
42337c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
42347c478bd9Sstevel@tonic-gate }
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate int
42377c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
42387c478bd9Sstevel@tonic-gate {
42397c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options;
42407c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
42417c478bd9Sstevel@tonic-gate 	int err;
42427c478bd9Sstevel@tonic-gate 
42437c478bd9Sstevel@tonic-gate 	if (handle == NULL)
42447c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
42477c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
42487c478bd9Sstevel@tonic-gate 
42497c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
42507c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
42517c478bd9Sstevel@tonic-gate 			break;
42527c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
42537c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
42547c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
42557c478bd9Sstevel@tonic-gate 	}
42567c478bd9Sstevel@tonic-gate 
42577c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
42587c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
42597c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
42607c478bd9Sstevel@tonic-gate 		return (err);
42617c478bd9Sstevel@tonic-gate 	}
42627c478bd9Sstevel@tonic-gate 
42637c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
42647c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
42657c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
42667c478bd9Sstevel@tonic-gate 		return (err);
42677c478bd9Sstevel@tonic-gate 	}
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
42707c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
42717c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
42727c478bd9Sstevel@tonic-gate 		return (err);
42737c478bd9Sstevel@tonic-gate 	}
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
42767c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
42777c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
42787c478bd9Sstevel@tonic-gate 		return (err);
42797c478bd9Sstevel@tonic-gate 	}
42807c478bd9Sstevel@tonic-gate 
42817c478bd9Sstevel@tonic-gate 	/* OK for options to be NULL */
42827c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
42837c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
42847c478bd9Sstevel@tonic-gate 	    options = options->next) {
42857c478bd9Sstevel@tonic-gate 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
42867c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK)
42877c478bd9Sstevel@tonic-gate 			break;
42887c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
42897c478bd9Sstevel@tonic-gate 			break;
42907c478bd9Sstevel@tonic-gate 	}
42917c478bd9Sstevel@tonic-gate 
42927c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
42937c478bd9Sstevel@tonic-gate 	return (Z_OK);
42947c478bd9Sstevel@tonic-gate }
42957c478bd9Sstevel@tonic-gate 
42967c478bd9Sstevel@tonic-gate int
42977c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle)
42987c478bd9Sstevel@tonic-gate {
42997c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
43007c478bd9Sstevel@tonic-gate }
43017c478bd9Sstevel@tonic-gate 
43027c478bd9Sstevel@tonic-gate int
43037c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle)
43047c478bd9Sstevel@tonic-gate {
43057c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
43067c478bd9Sstevel@tonic-gate }
43077c478bd9Sstevel@tonic-gate 
43087c478bd9Sstevel@tonic-gate int
43097c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
43107c478bd9Sstevel@tonic-gate {
43117c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
43127c478bd9Sstevel@tonic-gate 	int err;
43137c478bd9Sstevel@tonic-gate 
43147c478bd9Sstevel@tonic-gate 	if (handle == NULL)
43157c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
43167c478bd9Sstevel@tonic-gate 
43177c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
43187c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43197c478bd9Sstevel@tonic-gate 
43207c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
43217c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
43227c478bd9Sstevel@tonic-gate 			break;
43237c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
43247c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43257c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43267c478bd9Sstevel@tonic-gate 	}
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
43297c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
43307c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43317c478bd9Sstevel@tonic-gate 		return (err);
43327c478bd9Sstevel@tonic-gate 	}
43337c478bd9Sstevel@tonic-gate 
43347c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
43357c478bd9Sstevel@tonic-gate 	return (Z_OK);
43367c478bd9Sstevel@tonic-gate }
43377c478bd9Sstevel@tonic-gate 
43387c478bd9Sstevel@tonic-gate int
43397c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle)
43407c478bd9Sstevel@tonic-gate {
43417c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
43427c478bd9Sstevel@tonic-gate }
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate int
43457c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle)
43467c478bd9Sstevel@tonic-gate {
43477c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
43487c478bd9Sstevel@tonic-gate }
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate int
43517c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
43527c478bd9Sstevel@tonic-gate {
43537c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
43547c478bd9Sstevel@tonic-gate 	int err;
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 	if (handle == NULL)
43577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
43587c478bd9Sstevel@tonic-gate 
43597c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
43607c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
43637c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
43647c478bd9Sstevel@tonic-gate 			break;
43657c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
43667c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43677c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43687c478bd9Sstevel@tonic-gate 	}
43697c478bd9Sstevel@tonic-gate 
43707c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
43717c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
43727c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43737c478bd9Sstevel@tonic-gate 		return (err);
43747c478bd9Sstevel@tonic-gate 	}
43757c478bd9Sstevel@tonic-gate 
43767c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
43777c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
43787c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43797c478bd9Sstevel@tonic-gate 		return (err);
43807c478bd9Sstevel@tonic-gate 	}
43817c478bd9Sstevel@tonic-gate 
43827c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
43837c478bd9Sstevel@tonic-gate 	return (Z_OK);
43847c478bd9Sstevel@tonic-gate }
43857c478bd9Sstevel@tonic-gate 
43867c478bd9Sstevel@tonic-gate int
43877c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle)
43887c478bd9Sstevel@tonic-gate {
43897c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
43907c478bd9Sstevel@tonic-gate }
43917c478bd9Sstevel@tonic-gate 
43927c478bd9Sstevel@tonic-gate int
43937c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle)
43947c478bd9Sstevel@tonic-gate {
43957c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
43967c478bd9Sstevel@tonic-gate }
43977c478bd9Sstevel@tonic-gate 
43987c478bd9Sstevel@tonic-gate int
43997c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
44007c478bd9Sstevel@tonic-gate {
44017c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
44027c478bd9Sstevel@tonic-gate 	int err;
44037c478bd9Sstevel@tonic-gate 
44047c478bd9Sstevel@tonic-gate 	if (handle == NULL)
44057c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44067c478bd9Sstevel@tonic-gate 
44077c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
44087c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44117c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
44127c478bd9Sstevel@tonic-gate 			break;
44137c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
44147c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44157c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44167c478bd9Sstevel@tonic-gate 	}
44177c478bd9Sstevel@tonic-gate 
44187c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
44197c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
44207c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44217c478bd9Sstevel@tonic-gate 		return (err);
44227c478bd9Sstevel@tonic-gate 	}
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
44257c478bd9Sstevel@tonic-gate 	return (Z_OK);
44267c478bd9Sstevel@tonic-gate }
44277c478bd9Sstevel@tonic-gate 
44287c478bd9Sstevel@tonic-gate int
44297c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle)
44307c478bd9Sstevel@tonic-gate {
44317c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
44327c478bd9Sstevel@tonic-gate }
44337c478bd9Sstevel@tonic-gate 
44347c478bd9Sstevel@tonic-gate int
44357c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle)
44367c478bd9Sstevel@tonic-gate {
44377c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
44387c478bd9Sstevel@tonic-gate }
44397c478bd9Sstevel@tonic-gate 
44407c478bd9Sstevel@tonic-gate int
44417c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
44427c478bd9Sstevel@tonic-gate {
44437c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
44447c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
44457c478bd9Sstevel@tonic-gate 	int err;
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 	if (handle == NULL)
44487c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44497c478bd9Sstevel@tonic-gate 
44507c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
44517c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44527c478bd9Sstevel@tonic-gate 
44537c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44547c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
44557c478bd9Sstevel@tonic-gate 			break;
44567c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
44577c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44587c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44597c478bd9Sstevel@tonic-gate 	}
44607c478bd9Sstevel@tonic-gate 
44617c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
44627c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
44637c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44647c478bd9Sstevel@tonic-gate 		return (err);
44657c478bd9Sstevel@tonic-gate 	}
44667c478bd9Sstevel@tonic-gate 
44677c478bd9Sstevel@tonic-gate 	tabptr->zone_rctl_valptr = NULL;
44687c478bd9Sstevel@tonic-gate 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
44697c478bd9Sstevel@tonic-gate 		valptr = (struct zone_rctlvaltab *)malloc(
44707c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctlvaltab));
44717c478bd9Sstevel@tonic-gate 		if (valptr == NULL)
44727c478bd9Sstevel@tonic-gate 			return (Z_NOMEM);
44737c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
44747c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
44757c478bd9Sstevel@tonic-gate 			break;
44767c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
44777c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
44787c478bd9Sstevel@tonic-gate 			break;
44797c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
44807c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
44817c478bd9Sstevel@tonic-gate 			break;
44827c478bd9Sstevel@tonic-gate 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
44837c478bd9Sstevel@tonic-gate 			break;
44847c478bd9Sstevel@tonic-gate 	}
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
44877c478bd9Sstevel@tonic-gate 	return (Z_OK);
44887c478bd9Sstevel@tonic-gate }
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate int
44917c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle)
44927c478bd9Sstevel@tonic-gate {
44937c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
44947c478bd9Sstevel@tonic-gate }
44957c478bd9Sstevel@tonic-gate 
44967c478bd9Sstevel@tonic-gate int
44977c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle)
44987c478bd9Sstevel@tonic-gate {
44997c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
45007c478bd9Sstevel@tonic-gate }
45017c478bd9Sstevel@tonic-gate 
45027c478bd9Sstevel@tonic-gate int
45037c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
45047c478bd9Sstevel@tonic-gate {
45057c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
45067c478bd9Sstevel@tonic-gate 	int err;
45077c478bd9Sstevel@tonic-gate 
45087c478bd9Sstevel@tonic-gate 	if (handle == NULL)
45097c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
45107c478bd9Sstevel@tonic-gate 
45117c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
45127c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
45157c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
45167c478bd9Sstevel@tonic-gate 			break;
45177c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
45187c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45197c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45207c478bd9Sstevel@tonic-gate 	}
45217c478bd9Sstevel@tonic-gate 
45227c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
45237c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
45247c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45257c478bd9Sstevel@tonic-gate 		return (err);
45267c478bd9Sstevel@tonic-gate 	}
45277c478bd9Sstevel@tonic-gate 
45287c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
45297c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
45307c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45317c478bd9Sstevel@tonic-gate 		return (err);
45327c478bd9Sstevel@tonic-gate 	}
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
45357c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
45367c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45377c478bd9Sstevel@tonic-gate 		return (err);
45387c478bd9Sstevel@tonic-gate 	}
45397c478bd9Sstevel@tonic-gate 
45407c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45417c478bd9Sstevel@tonic-gate 	return (Z_OK);
45427c478bd9Sstevel@tonic-gate }
45437c478bd9Sstevel@tonic-gate 
45447c478bd9Sstevel@tonic-gate int
45457c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle)
45467c478bd9Sstevel@tonic-gate {
45477c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45487c478bd9Sstevel@tonic-gate }
45497c478bd9Sstevel@tonic-gate 
4550ffbafc53Scomay /*
4551ffbafc53Scomay  * The privileges available on the system and described in privileges(5)
45529acbbeafSnn35248  * fall into four categories with respect to non-global zones:
45539acbbeafSnn35248  *
45549acbbeafSnn35248  *      Default set of privileges considered safe for all non-global
45559acbbeafSnn35248  *      zones.  These privileges are "safe" in the sense that a
45569acbbeafSnn35248  *      privileged process in the zone cannot affect processes in any
45579acbbeafSnn35248  *      other zone on the system.
45589acbbeafSnn35248  *
45599acbbeafSnn35248  *      Set of privileges not currently permitted within a non-global
45609acbbeafSnn35248  *      zone.  These privileges are considered by default, "unsafe,"
45619acbbeafSnn35248  *      and include ones which affect global resources (such as the
45629acbbeafSnn35248  *      system clock or physical memory) or are overly broad and cover
45639acbbeafSnn35248  *      more than one mechanism in the system.  In other cases, there
45649acbbeafSnn35248  *      has not been sufficient virtualization in the parts of the
45659acbbeafSnn35248  *      system the privilege covers to allow its use within a
45669acbbeafSnn35248  *      non-global zone.
45679acbbeafSnn35248  *
45689acbbeafSnn35248  *      Set of privileges required in order to get a zone booted and
45699acbbeafSnn35248  *      init(1M) started.  These cannot be removed from the zone's
45709acbbeafSnn35248  *      privilege set.
45719acbbeafSnn35248  *
45729acbbeafSnn35248  * All other privileges are optional and are potentially useful for
4573ffbafc53Scomay  * processes executing inside a non-global zone.
4574ffbafc53Scomay  *
4575ffbafc53Scomay  * When privileges are added to the system, a determination needs to be
4576ffbafc53Scomay  * made as to which category the privilege belongs to.  Ideally,
4577ffbafc53Scomay  * privileges should be fine-grained enough and the mechanisms they cover
4578ffbafc53Scomay  * virtualized enough so that they can be made available to non-global
4579ffbafc53Scomay  * zones.
4580ffbafc53Scomay  */
4581ffbafc53Scomay 
4582ffbafc53Scomay /*
4583ffbafc53Scomay  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
4584ffbafc53Scomay  * the privilege string separator can be any character, although it is
4585ffbafc53Scomay  * usually a comma character, define these here as well in the event that
4586ffbafc53Scomay  * they change or are augmented in the future.
4587ffbafc53Scomay  */
4588ffbafc53Scomay #define	BASIC_TOKEN		"basic"
4589ffbafc53Scomay #define	DEFAULT_TOKEN		"default"
4590ffbafc53Scomay #define	ZONE_TOKEN		"zone"
4591ffbafc53Scomay #define	TOKEN_PRIV_CHAR		','
4592ffbafc53Scomay #define	TOKEN_PRIV_STR		","
4593ffbafc53Scomay 
45949acbbeafSnn35248 typedef struct priv_node {
45959acbbeafSnn35248 	struct priv_node	*pn_next;	/* Next privilege */
45969acbbeafSnn35248 	char			*pn_priv;	/* Privileges name */
45979acbbeafSnn35248 } priv_node_t;
45989acbbeafSnn35248 
45999acbbeafSnn35248 /* Privileges lists can differ across brands */
46009acbbeafSnn35248 typedef struct priv_lists {
46019acbbeafSnn35248 	/* Privileges considered safe for all non-global zones of a brand */
46029acbbeafSnn35248 	struct priv_node	*pl_default;
46039acbbeafSnn35248 
46049acbbeafSnn35248 	/* Privileges not permitted for all non-global zones of a brand */
46059acbbeafSnn35248 	struct priv_node	*pl_prohibited;
46069acbbeafSnn35248 
46079acbbeafSnn35248 	/* Privileges required for all non-global zones of a brand */
46089acbbeafSnn35248 	struct priv_node	*pl_required;
4609bf1d7e28Sdh155122 
4610bf1d7e28Sdh155122 	/*
4611bf1d7e28Sdh155122 	 * ip-type of the zone these privileges lists apply to.
4612bf1d7e28Sdh155122 	 * It is used to pass ip-type to the callback function,
4613bf1d7e28Sdh155122 	 * priv_lists_cb, which has no way of getting the ip-type.
4614bf1d7e28Sdh155122 	 */
4615bf1d7e28Sdh155122 	const char		*pl_iptype;
46169acbbeafSnn35248 } priv_lists_t;
46179acbbeafSnn35248 
46189acbbeafSnn35248 static int
4619bf1d7e28Sdh155122 priv_lists_cb(void *data, priv_iter_t *priv_iter)
46207c478bd9Sstevel@tonic-gate {
46219acbbeafSnn35248 	priv_lists_t *plp = (priv_lists_t *)data;
46229acbbeafSnn35248 	priv_node_t *pnp;
46239acbbeafSnn35248 
4624bf1d7e28Sdh155122 	/* Skip this privilege if ip-type does not match */
4625bf1d7e28Sdh155122 	if ((strcmp(priv_iter->pi_iptype, "all") != 0) &&
4626bf1d7e28Sdh155122 	    (strcmp(priv_iter->pi_iptype, plp->pl_iptype) != 0))
4627bf1d7e28Sdh155122 		return (0);
4628bf1d7e28Sdh155122 
46299acbbeafSnn35248 	/* Allocate a new priv list node. */
46309acbbeafSnn35248 	if ((pnp = malloc(sizeof (*pnp))) == NULL)
46319acbbeafSnn35248 		return (-1);
4632bf1d7e28Sdh155122 	if ((pnp->pn_priv = strdup(priv_iter->pi_name)) == NULL) {
46339acbbeafSnn35248 		free(pnp);
46349acbbeafSnn35248 		return (-1);
46359acbbeafSnn35248 	}
46369acbbeafSnn35248 
46379acbbeafSnn35248 	/* Insert the new priv list node into the right list */
4638bf1d7e28Sdh155122 	if (strcmp(priv_iter->pi_set, "default") == 0) {
46399acbbeafSnn35248 		pnp->pn_next = plp->pl_default;
46409acbbeafSnn35248 		plp->pl_default = pnp;
4641bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "prohibited") == 0) {
46429acbbeafSnn35248 		pnp->pn_next = plp->pl_prohibited;
46439acbbeafSnn35248 		plp->pl_prohibited = pnp;
4644bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "required") == 0) {
46459acbbeafSnn35248 		pnp->pn_next = plp->pl_required;
46469acbbeafSnn35248 		plp->pl_required = pnp;
46479acbbeafSnn35248 	} else {
46489acbbeafSnn35248 		free(pnp->pn_priv);
46499acbbeafSnn35248 		free(pnp);
46509acbbeafSnn35248 		return (-1);
46519acbbeafSnn35248 	}
46529acbbeafSnn35248 	return (0);
46539acbbeafSnn35248 }
46549acbbeafSnn35248 
46559acbbeafSnn35248 static void
46569acbbeafSnn35248 priv_lists_destroy(priv_lists_t *plp)
46579acbbeafSnn35248 {
46589acbbeafSnn35248 	priv_node_t *pnp;
46599acbbeafSnn35248 
46609acbbeafSnn35248 	assert(plp != NULL);
46619acbbeafSnn35248 
46629acbbeafSnn35248 	while ((pnp = plp->pl_default) != NULL) {
46639acbbeafSnn35248 		plp->pl_default = pnp->pn_next;
46649acbbeafSnn35248 		free(pnp->pn_priv);
46659acbbeafSnn35248 		free(pnp);
46669acbbeafSnn35248 	}
46679acbbeafSnn35248 	while ((pnp = plp->pl_prohibited) != NULL) {
46689acbbeafSnn35248 		plp->pl_prohibited = pnp->pn_next;
46699acbbeafSnn35248 		free(pnp->pn_priv);
46709acbbeafSnn35248 		free(pnp);
46719acbbeafSnn35248 	}
46729acbbeafSnn35248 	while ((pnp = plp->pl_required) != NULL) {
46739acbbeafSnn35248 		plp->pl_required = pnp->pn_next;
46749acbbeafSnn35248 		free(pnp->pn_priv);
46759acbbeafSnn35248 		free(pnp);
46769acbbeafSnn35248 	}
46779acbbeafSnn35248 	free(plp);
46789acbbeafSnn35248 }
46799acbbeafSnn35248 
46809acbbeafSnn35248 static int
4681bf1d7e28Sdh155122 priv_lists_create(zone_dochandle_t handle, priv_lists_t **plpp,
4682bf1d7e28Sdh155122     const char *curr_iptype)
46839acbbeafSnn35248 {
46849acbbeafSnn35248 	priv_lists_t *plp;
4685123807fbSedp 	brand_handle_t bh;
46869acbbeafSnn35248 	char brand[MAXNAMELEN];
46879acbbeafSnn35248 
46889acbbeafSnn35248 	if (handle != NULL) {
46899acbbeafSnn35248 		if (zonecfg_get_brand(handle, brand, sizeof (brand)) != 0)
46909acbbeafSnn35248 			return (Z_BRAND_ERROR);
46919acbbeafSnn35248 	} else {
46929acbbeafSnn35248 		(void) strlcpy(brand, NATIVE_BRAND_NAME, MAXNAMELEN);
46939acbbeafSnn35248 	}
46949acbbeafSnn35248 
4695123807fbSedp 	if ((bh = brand_open(brand)) == NULL)
46969acbbeafSnn35248 		return (Z_BRAND_ERROR);
46979acbbeafSnn35248 
46989acbbeafSnn35248 	if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) {
4699123807fbSedp 		brand_close(bh);
47009acbbeafSnn35248 		return (Z_NOMEM);
47019acbbeafSnn35248 	}
47029acbbeafSnn35248 
4703bf1d7e28Sdh155122 	plp->pl_iptype = curr_iptype;
4704bf1d7e28Sdh155122 
47059acbbeafSnn35248 	/* construct the privilege lists */
4706123807fbSedp 	if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) {
47079acbbeafSnn35248 		priv_lists_destroy(plp);
4708123807fbSedp 		brand_close(bh);
47099acbbeafSnn35248 		return (Z_BRAND_ERROR);
47109acbbeafSnn35248 	}
47119acbbeafSnn35248 
4712123807fbSedp 	brand_close(bh);
47139acbbeafSnn35248 	*plpp = plp;
47149acbbeafSnn35248 	return (Z_OK);
47159acbbeafSnn35248 }
47169acbbeafSnn35248 
47179acbbeafSnn35248 static int
47189acbbeafSnn35248 get_default_privset(priv_set_t *privs, priv_lists_t *plp)
47199acbbeafSnn35248 {
47209acbbeafSnn35248 	priv_node_t *pnp;
4721ffbafc53Scomay 	priv_set_t *basic;
47227c478bd9Sstevel@tonic-gate 
4723ffbafc53Scomay 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
47247c478bd9Sstevel@tonic-gate 	if (basic == NULL)
4725ffbafc53Scomay 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
47267c478bd9Sstevel@tonic-gate 
47277c478bd9Sstevel@tonic-gate 	priv_union(basic, privs);
47287c478bd9Sstevel@tonic-gate 	priv_freeset(basic);
47297c478bd9Sstevel@tonic-gate 
47309acbbeafSnn35248 	for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) {
47319acbbeafSnn35248 		if (priv_addset(privs, pnp->pn_priv) != 0)
47327c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
47337c478bd9Sstevel@tonic-gate 	}
47349acbbeafSnn35248 
47357c478bd9Sstevel@tonic-gate 	return (Z_OK);
47367c478bd9Sstevel@tonic-gate }
47377c478bd9Sstevel@tonic-gate 
47389acbbeafSnn35248 int
4739bf1d7e28Sdh155122 zonecfg_default_privset(priv_set_t *privs, const char *curr_iptype)
47409acbbeafSnn35248 {
47419acbbeafSnn35248 	priv_lists_t *plp;
47429acbbeafSnn35248 	int ret;
47439acbbeafSnn35248 
4744bf1d7e28Sdh155122 	if ((ret = priv_lists_create(NULL, &plp, curr_iptype)) != Z_OK)
47459acbbeafSnn35248 		return (ret);
47469acbbeafSnn35248 	ret = get_default_privset(privs, plp);
47479acbbeafSnn35248 	priv_lists_destroy(plp);
47489acbbeafSnn35248 	return (ret);
47499acbbeafSnn35248 }
47509acbbeafSnn35248 
4751ffbafc53Scomay void
4752ffbafc53Scomay append_priv_token(char *priv, char *str, size_t strlen)
4753ffbafc53Scomay {
4754ffbafc53Scomay 	if (*str != '\0')
4755ffbafc53Scomay 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
4756ffbafc53Scomay 	(void) strlcat(str, priv, strlen);
4757ffbafc53Scomay }
4758ffbafc53Scomay 
4759ffbafc53Scomay /*
4760ffbafc53Scomay  * Verify that the supplied string is a valid privilege limit set for a
4761ffbafc53Scomay  * non-global zone.  This string must not only be acceptable to
4762ffbafc53Scomay  * priv_str_to_set(3C) which parses it, but it also must resolve to a
4763ffbafc53Scomay  * privilege set that includes certain required privileges and lacks
4764ffbafc53Scomay  * certain prohibited privileges.
4765ffbafc53Scomay  */
4766ffbafc53Scomay static int
4767ffbafc53Scomay verify_privset(char *privbuf, priv_set_t *privs, char **privname,
47689acbbeafSnn35248     boolean_t add_default, priv_lists_t *plp)
4769ffbafc53Scomay {
47709acbbeafSnn35248 	priv_node_t *pnp;
47719acbbeafSnn35248 	char *tmp, *cp, *lasts;
4772ffbafc53Scomay 	size_t len;
4773ffbafc53Scomay 	priv_set_t *mergeset;
4774ffbafc53Scomay 	const char *token;
4775ffbafc53Scomay 
4776ffbafc53Scomay 	/*
4777ffbafc53Scomay 	 * The verification of the privilege string occurs in several
4778ffbafc53Scomay 	 * phases.  In the first phase, the supplied string is scanned for
4779ffbafc53Scomay 	 * the ZONE_TOKEN token which is not support as part of the
4780ffbafc53Scomay 	 * "limitpriv" property.
4781ffbafc53Scomay 	 *
4782ffbafc53Scomay 	 * Duplicate the supplied privilege string since strtok_r(3C)
4783ffbafc53Scomay 	 * tokenizes its input by null-terminating the tokens.
4784ffbafc53Scomay 	 */
4785ffbafc53Scomay 	if ((tmp = strdup(privbuf)) == NULL)
4786ffbafc53Scomay 		return (Z_NOMEM);
4787ffbafc53Scomay 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
4788ffbafc53Scomay 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
4789ffbafc53Scomay 		if (strcmp(cp, ZONE_TOKEN) == 0) {
4790ffbafc53Scomay 			free(tmp);
4791ffbafc53Scomay 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
4792ffbafc53Scomay 				return (Z_NOMEM);
4793ffbafc53Scomay 			else
4794ffbafc53Scomay 				return (Z_PRIV_UNKNOWN);
4795ffbafc53Scomay 		}
4796ffbafc53Scomay 	}
4797ffbafc53Scomay 	free(tmp);
4798ffbafc53Scomay 
4799ffbafc53Scomay 	if (add_default) {
4800ffbafc53Scomay 		/*
4801ffbafc53Scomay 		 * If DEFAULT_TOKEN was specified, a string needs to be
4802ffbafc53Scomay 		 * built containing the privileges from the default, safe
4803ffbafc53Scomay 		 * set along with those of the "limitpriv" property.
4804ffbafc53Scomay 		 */
4805ffbafc53Scomay 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
48069acbbeafSnn35248 
48079acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
48089acbbeafSnn35248 			len += strlen(pnp->pn_priv) + 1;
4809ffbafc53Scomay 		tmp = alloca(len);
4810ffbafc53Scomay 		*tmp = '\0';
4811ffbafc53Scomay 
4812ffbafc53Scomay 		append_priv_token(BASIC_TOKEN, tmp, len);
48139acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
48149acbbeafSnn35248 			append_priv_token(pnp->pn_priv, tmp, len);
4815ffbafc53Scomay 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
4816ffbafc53Scomay 		(void) strlcat(tmp, privbuf, len);
4817ffbafc53Scomay 	} else {
4818ffbafc53Scomay 		tmp = privbuf;
4819ffbafc53Scomay 	}
4820ffbafc53Scomay 
4821ffbafc53Scomay 
4822ffbafc53Scomay 	/*
4823ffbafc53Scomay 	 * In the next phase, attempt to convert the merged privilege
4824ffbafc53Scomay 	 * string into a privilege set.  In the case of an error, either
4825ffbafc53Scomay 	 * there was a memory allocation failure or there was an invalid
4826ffbafc53Scomay 	 * privilege token in the string.  In either case, return an
4827ffbafc53Scomay 	 * appropriate error code but in the event of an invalid token,
4828ffbafc53Scomay 	 * allocate a string containing its name and return that back to
4829ffbafc53Scomay 	 * the caller.
4830ffbafc53Scomay 	 */
4831ffbafc53Scomay 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
4832ffbafc53Scomay 	if (mergeset == NULL) {
4833ffbafc53Scomay 		if (token == NULL)
4834ffbafc53Scomay 			return (Z_NOMEM);
4835ffbafc53Scomay 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
4836ffbafc53Scomay 			*cp = '\0';
4837ffbafc53Scomay 		if ((*privname = strdup(token)) == NULL)
4838ffbafc53Scomay 			return (Z_NOMEM);
4839ffbafc53Scomay 		else
4840ffbafc53Scomay 			return (Z_PRIV_UNKNOWN);
4841ffbafc53Scomay 	}
4842ffbafc53Scomay 
4843ffbafc53Scomay 	/*
4844ffbafc53Scomay 	 * Next, verify that none of the prohibited zone privileges are
4845ffbafc53Scomay 	 * present in the merged privilege set.
4846ffbafc53Scomay 	 */
48479acbbeafSnn35248 	for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) {
48489acbbeafSnn35248 		if (priv_ismember(mergeset, pnp->pn_priv)) {
4849ffbafc53Scomay 			priv_freeset(mergeset);
48509acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
4851ffbafc53Scomay 				return (Z_NOMEM);
4852ffbafc53Scomay 			else
4853ffbafc53Scomay 				return (Z_PRIV_PROHIBITED);
4854ffbafc53Scomay 		}
4855ffbafc53Scomay 	}
4856ffbafc53Scomay 
4857ffbafc53Scomay 	/*
4858ffbafc53Scomay 	 * Finally, verify that all of the required zone privileges are
4859ffbafc53Scomay 	 * present in the merged privilege set.
4860ffbafc53Scomay 	 */
48619acbbeafSnn35248 	for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) {
48629acbbeafSnn35248 		if (!priv_ismember(mergeset, pnp->pn_priv)) {
4863ffbafc53Scomay 			priv_freeset(mergeset);
48649acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
4865ffbafc53Scomay 				return (Z_NOMEM);
4866ffbafc53Scomay 			else
4867ffbafc53Scomay 				return (Z_PRIV_REQUIRED);
4868ffbafc53Scomay 		}
4869ffbafc53Scomay 	}
4870ffbafc53Scomay 
4871ffbafc53Scomay 	priv_copyset(mergeset, privs);
4872ffbafc53Scomay 	priv_freeset(mergeset);
4873ffbafc53Scomay 	return (Z_OK);
4874ffbafc53Scomay }
4875ffbafc53Scomay 
4876ffbafc53Scomay /*
4877ffbafc53Scomay  * Fill in the supplied privilege set with either the default, safe set of
4878ffbafc53Scomay  * privileges suitable for a non-global zone, or one based on the
4879ffbafc53Scomay  * "limitpriv" property in the zone's configuration.
4880ffbafc53Scomay  *
4881ffbafc53Scomay  * In the event of an invalid privilege specification in the
4882ffbafc53Scomay  * configuration, a string is allocated and returned containing the
4883ffbafc53Scomay  * "privilege" causing the issue.  It is the caller's responsibility to
4884ffbafc53Scomay  * free this memory when it is done with it.
4885ffbafc53Scomay  */
4886ffbafc53Scomay int
4887ffbafc53Scomay zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
4888ffbafc53Scomay     char **privname)
4889ffbafc53Scomay {
48909acbbeafSnn35248 	priv_lists_t *plp;
48919acbbeafSnn35248 	char *cp, *limitpriv = NULL;
48929acbbeafSnn35248 	int err, limitlen;
4893bf1d7e28Sdh155122 	zone_iptype_t iptype;
4894bf1d7e28Sdh155122 	const char *curr_iptype;
4895ffbafc53Scomay 
4896ffbafc53Scomay 	/*
4897ffbafc53Scomay 	 * Attempt to lookup the "limitpriv" property.  If it does not
4898ffbafc53Scomay 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
4899ffbafc53Scomay 	 * default, safe privilege set is returned.
4900ffbafc53Scomay 	 */
49019acbbeafSnn35248 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK)
4902ffbafc53Scomay 		return (err);
49039acbbeafSnn35248 
4904bf1d7e28Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK)
4905bf1d7e28Sdh155122 		return (err);
4906bf1d7e28Sdh155122 
4907bf1d7e28Sdh155122 	switch (iptype) {
4908bf1d7e28Sdh155122 	case ZS_SHARED:
4909bf1d7e28Sdh155122 		curr_iptype = "shared";
4910bf1d7e28Sdh155122 		break;
4911bf1d7e28Sdh155122 	case ZS_EXCLUSIVE:
4912bf1d7e28Sdh155122 		curr_iptype = "exclusive";
4913bf1d7e28Sdh155122 		break;
4914bf1d7e28Sdh155122 	}
4915bf1d7e28Sdh155122 
4916bf1d7e28Sdh155122 	if ((err = priv_lists_create(handle, &plp, curr_iptype)) != Z_OK)
49179acbbeafSnn35248 		return (err);
49189acbbeafSnn35248 
4919ffbafc53Scomay 	limitlen = strlen(limitpriv);
4920ffbafc53Scomay 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
4921ffbafc53Scomay 		free(limitpriv);
49229acbbeafSnn35248 		err = get_default_privset(privs, plp);
49239acbbeafSnn35248 		priv_lists_destroy(plp);
49249acbbeafSnn35248 		return (err);
4925ffbafc53Scomay 	}
4926ffbafc53Scomay 
4927ffbafc53Scomay 	/*
4928ffbafc53Scomay 	 * Check if the string DEFAULT_TOKEN is the first token in a list
4929ffbafc53Scomay 	 * of privileges.
4930ffbafc53Scomay 	 */
4931ffbafc53Scomay 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
4932ffbafc53Scomay 	if (cp != NULL &&
4933ffbafc53Scomay 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
49349acbbeafSnn35248 		err = verify_privset(cp + 1, privs, privname, B_TRUE, plp);
4935ffbafc53Scomay 	else
49369acbbeafSnn35248 		err = verify_privset(limitpriv, privs, privname, B_FALSE, plp);
4937ffbafc53Scomay 
4938ffbafc53Scomay 	free(limitpriv);
49399acbbeafSnn35248 	priv_lists_destroy(plp);
4940ffbafc53Scomay 	return (err);
4941ffbafc53Scomay }
4942ffbafc53Scomay 
49437c478bd9Sstevel@tonic-gate int
49447c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
49457c478bd9Sstevel@tonic-gate {
49467c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
49477c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
49487c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
49497c478bd9Sstevel@tonic-gate 	FILE *cookie;
49507c478bd9Sstevel@tonic-gate 	int err;
4951108322fbScarlsonj 	char *cp;
49527c478bd9Sstevel@tonic-gate 
49537c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
49547c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
49557c478bd9Sstevel@tonic-gate 
4956108322fbScarlsonj 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
4957108322fbScarlsonj 	cp = zonepath + strlen(zonepath);
4958108322fbScarlsonj 	while (cp > zonepath && cp[-1] == '/')
4959108322fbScarlsonj 		*--cp = '\0';
4960108322fbScarlsonj 
49617c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
4962108322fbScarlsonj 		if (zonepath[0] == '\0')
49637c478bd9Sstevel@tonic-gate 			(void) strlcpy(zonepath, "/", rp_sz);
49647c478bd9Sstevel@tonic-gate 		return (Z_OK);
49657c478bd9Sstevel@tonic-gate 	}
49667c478bd9Sstevel@tonic-gate 
49677c478bd9Sstevel@tonic-gate 	/*
49687c478bd9Sstevel@tonic-gate 	 * First check the index file.  Because older versions did not have
49697c478bd9Sstevel@tonic-gate 	 * a copy of the zone path, allow for it to be zero length, in which
49707c478bd9Sstevel@tonic-gate 	 * case we ignore this result and fall back to the XML files.
49717c478bd9Sstevel@tonic-gate 	 */
49727c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
49737c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
49747c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
49757c478bd9Sstevel@tonic-gate 			found = B_TRUE;
4976108322fbScarlsonj 			if (ze->zone_path[0] != '\0')
4977108322fbScarlsonj 				(void) strlcpy(cp, ze->zone_path,
4978108322fbScarlsonj 				    rp_sz - (cp - zonepath));
49797c478bd9Sstevel@tonic-gate 		}
49807c478bd9Sstevel@tonic-gate 		free(ze);
49817c478bd9Sstevel@tonic-gate 		if (found)
49827c478bd9Sstevel@tonic-gate 			break;
49837c478bd9Sstevel@tonic-gate 	}
49847c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
4985108322fbScarlsonj 	if (found && *cp != '\0')
49867c478bd9Sstevel@tonic-gate 		return (Z_OK);
49877c478bd9Sstevel@tonic-gate 
49887c478bd9Sstevel@tonic-gate 	/* Fall back to the XML files. */
49897c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL)
49907c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
49917c478bd9Sstevel@tonic-gate 
49927c478bd9Sstevel@tonic-gate 	/*
49937c478bd9Sstevel@tonic-gate 	 * Check the snapshot first: if a zone is running, its zonepath
49947c478bd9Sstevel@tonic-gate 	 * may have changed.
49957c478bd9Sstevel@tonic-gate 	 */
49967c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
4997e767a340Sgjelinek 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK) {
4998e767a340Sgjelinek 			zonecfg_fini_handle(handle);
49997c478bd9Sstevel@tonic-gate 			return (err);
50007c478bd9Sstevel@tonic-gate 		}
5001e767a340Sgjelinek 	}
50027c478bd9Sstevel@tonic-gate 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
50037c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
50047c478bd9Sstevel@tonic-gate 	return (err);
50057c478bd9Sstevel@tonic-gate }
50067c478bd9Sstevel@tonic-gate 
50077c478bd9Sstevel@tonic-gate int
50087c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
50097c478bd9Sstevel@tonic-gate {
50107c478bd9Sstevel@tonic-gate 	int err;
50117c478bd9Sstevel@tonic-gate 
50127c478bd9Sstevel@tonic-gate 	/* This function makes sense for non-global zones only. */
50137c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
50147c478bd9Sstevel@tonic-gate 		return (Z_BOGUS_ZONE_NAME);
50157c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
50167c478bd9Sstevel@tonic-gate 		return (err);
50177c478bd9Sstevel@tonic-gate 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
50187c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
50197c478bd9Sstevel@tonic-gate 	return (Z_OK);
50207c478bd9Sstevel@tonic-gate }
50217c478bd9Sstevel@tonic-gate 
50229acbbeafSnn35248 int
50239acbbeafSnn35248 zone_get_brand(char *zone_name, char *brandname, size_t rp_sz)
50249acbbeafSnn35248 {
50259acbbeafSnn35248 	int err;
50269acbbeafSnn35248 	zone_dochandle_t handle;
50279acbbeafSnn35248 	char myzone[MAXNAMELEN];
50289acbbeafSnn35248 	int myzoneid = getzoneid();
50299acbbeafSnn35248 
50309acbbeafSnn35248 	/*
50319acbbeafSnn35248 	 * If we are not in the global zone, then we don't have the zone
50329acbbeafSnn35248 	 * .xml files with the brand name available.  Thus, we are going to
50339acbbeafSnn35248 	 * have to ask the kernel for the information.
50349acbbeafSnn35248 	 */
50359acbbeafSnn35248 	if (myzoneid != GLOBAL_ZONEID) {
50362ec67e04Sgjelinek 		if (is_system_labeled()) {
50372ec67e04Sgjelinek 			(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
50382ec67e04Sgjelinek 			return (Z_OK);
50392ec67e04Sgjelinek 		}
50409acbbeafSnn35248 		if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone,
50419acbbeafSnn35248 		    sizeof (myzone)) < 0)
50429acbbeafSnn35248 			return (Z_NO_ZONE);
50439acbbeafSnn35248 		if (strncmp(zone_name, myzone, MAXNAMELEN) != NULL)
50449acbbeafSnn35248 			return (Z_NO_ZONE);
50459acbbeafSnn35248 		err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz);
50469acbbeafSnn35248 		if (err < 0)
50479acbbeafSnn35248 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
50489acbbeafSnn35248 		return (Z_OK);
50499acbbeafSnn35248 	}
50509acbbeafSnn35248 
50519acbbeafSnn35248 	if (strcmp(zone_name, "global") == NULL) {
50529acbbeafSnn35248 		(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
50532ec67e04Sgjelinek 		return (Z_OK);
50549acbbeafSnn35248 	}
50559acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL)
50569acbbeafSnn35248 		return (Z_NOMEM);
50579acbbeafSnn35248 
50589acbbeafSnn35248 	err = zonecfg_get_handle((char *)zone_name, handle);
50599acbbeafSnn35248 	if (err == Z_OK)
50609acbbeafSnn35248 		err = zonecfg_get_brand(handle, brandname, rp_sz);
50619acbbeafSnn35248 
50629acbbeafSnn35248 	zonecfg_fini_handle(handle);
50639acbbeafSnn35248 	return (err);
50649acbbeafSnn35248 }
50659acbbeafSnn35248 
5066facf4a8dSllai1 /*
5067facf4a8dSllai1  * Return the appropriate root for the active /dev.
5068facf4a8dSllai1  * For normal zone, the path is $ZONEPATH/root;
5069facf4a8dSllai1  * for scratch zone, the dev path is $ZONEPATH/lu.
5070facf4a8dSllai1  */
5071facf4a8dSllai1 int
5072facf4a8dSllai1 zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz)
5073facf4a8dSllai1 {
5074facf4a8dSllai1 	int err;
5075facf4a8dSllai1 	char *suffix;
5076facf4a8dSllai1 	zone_state_t state;
5077facf4a8dSllai1 
5078facf4a8dSllai1 	/* This function makes sense for non-global zones only. */
5079facf4a8dSllai1 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
5080facf4a8dSllai1 		return (Z_BOGUS_ZONE_NAME);
5081facf4a8dSllai1 	if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK)
5082facf4a8dSllai1 		return (err);
5083facf4a8dSllai1 
5084facf4a8dSllai1 	if (zone_get_state(zone_name, &state) == Z_OK &&
5085facf4a8dSllai1 	    state == ZONE_STATE_MOUNTED)
5086facf4a8dSllai1 		suffix = "/lu";
5087facf4a8dSllai1 	else
5088facf4a8dSllai1 		suffix = "/root";
5089facf4a8dSllai1 	if (strlcat(devroot, suffix, rp_sz) >= rp_sz)
5090facf4a8dSllai1 		return (Z_TOO_BIG);
5091facf4a8dSllai1 	return (Z_OK);
5092facf4a8dSllai1 }
5093facf4a8dSllai1 
50947c478bd9Sstevel@tonic-gate static zone_state_t
5095108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
50967c478bd9Sstevel@tonic-gate {
5097108322fbScarlsonj 	char zoneroot[MAXPATHLEN];
5098108322fbScarlsonj 	size_t zlen;
5099108322fbScarlsonj 
51007c478bd9Sstevel@tonic-gate 	assert(kernel_state <= ZONE_MAX_STATE);
51017c478bd9Sstevel@tonic-gate 	switch (kernel_state) {
51027c478bd9Sstevel@tonic-gate 		case ZONE_IS_UNINITIALIZED:
5103108322fbScarlsonj 			return (ZONE_STATE_READY);
51047c478bd9Sstevel@tonic-gate 		case ZONE_IS_READY:
5105108322fbScarlsonj 			/*
5106108322fbScarlsonj 			 * If the zone's root is mounted on $ZONEPATH/lu, then
5107108322fbScarlsonj 			 * it's a mounted scratch zone.
5108108322fbScarlsonj 			 */
5109108322fbScarlsonj 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
5110108322fbScarlsonj 			    sizeof (zoneroot)) >= 0) {
5111108322fbScarlsonj 				zlen = strlen(zoneroot);
5112108322fbScarlsonj 				if (zlen > 3 &&
5113108322fbScarlsonj 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
5114108322fbScarlsonj 					return (ZONE_STATE_MOUNTED);
5115108322fbScarlsonj 			}
51167c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_READY);
51177c478bd9Sstevel@tonic-gate 		case ZONE_IS_BOOTING:
51187c478bd9Sstevel@tonic-gate 		case ZONE_IS_RUNNING:
51197c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_RUNNING);
51207c478bd9Sstevel@tonic-gate 		case ZONE_IS_SHUTTING_DOWN:
51217c478bd9Sstevel@tonic-gate 		case ZONE_IS_EMPTY:
51227c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_SHUTTING_DOWN);
51237c478bd9Sstevel@tonic-gate 		case ZONE_IS_DOWN:
51247c478bd9Sstevel@tonic-gate 		case ZONE_IS_DYING:
51257c478bd9Sstevel@tonic-gate 		case ZONE_IS_DEAD:
51267c478bd9Sstevel@tonic-gate 		default:
51277c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_DOWN);
51287c478bd9Sstevel@tonic-gate 	}
51297c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
51307c478bd9Sstevel@tonic-gate }
51317c478bd9Sstevel@tonic-gate 
51327c478bd9Sstevel@tonic-gate int
51337c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num)
51347c478bd9Sstevel@tonic-gate {
51357c478bd9Sstevel@tonic-gate 	zone_status_t status;
51367c478bd9Sstevel@tonic-gate 	zoneid_t zone_id;
51377c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
51387c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
51397c478bd9Sstevel@tonic-gate 	FILE *cookie;
5140108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
5141108322fbScarlsonj 	FILE *fp;
51427c478bd9Sstevel@tonic-gate 
51437c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
51447c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
51457c478bd9Sstevel@tonic-gate 
5146108322fbScarlsonj 	/*
5147108322fbScarlsonj 	 * If we're looking at an alternate root, then we need to query the
5148108322fbScarlsonj 	 * kernel using the scratch zone name.
5149108322fbScarlsonj 	 */
5150108322fbScarlsonj 	zone_id = -1;
5151108322fbScarlsonj 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
5152108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
5153108322fbScarlsonj 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
5154108322fbScarlsonj 			    kernzone, sizeof (kernzone)) == 0)
5155108322fbScarlsonj 				zone_id = getzoneidbyname(kernzone);
5156108322fbScarlsonj 			zonecfg_close_scratch(fp);
5157108322fbScarlsonj 		}
5158108322fbScarlsonj 	} else {
5159108322fbScarlsonj 		zone_id = getzoneidbyname(zone_name);
5160108322fbScarlsonj 	}
5161108322fbScarlsonj 
51627c478bd9Sstevel@tonic-gate 	/* check to see if zone is running */
5163108322fbScarlsonj 	if (zone_id != -1 &&
51647c478bd9Sstevel@tonic-gate 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
51657c478bd9Sstevel@tonic-gate 	    sizeof (status)) >= 0) {
5166108322fbScarlsonj 		*state_num = kernel_state_to_user_state(zone_id, status);
51677c478bd9Sstevel@tonic-gate 		return (Z_OK);
51687c478bd9Sstevel@tonic-gate 	}
51697c478bd9Sstevel@tonic-gate 
51707c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
51717c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
51727c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
51737c478bd9Sstevel@tonic-gate 			found = B_TRUE;
51747c478bd9Sstevel@tonic-gate 			*state_num = ze->zone_state;
51757c478bd9Sstevel@tonic-gate 		}
51767c478bd9Sstevel@tonic-gate 		free(ze);
51777c478bd9Sstevel@tonic-gate 		if (found)
51787c478bd9Sstevel@tonic-gate 			break;
51797c478bd9Sstevel@tonic-gate 	}
51807c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
51817c478bd9Sstevel@tonic-gate 	return ((found) ? Z_OK : Z_NO_ZONE);
51827c478bd9Sstevel@tonic-gate }
51837c478bd9Sstevel@tonic-gate 
51847c478bd9Sstevel@tonic-gate int
51857c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state)
51867c478bd9Sstevel@tonic-gate {
51877c478bd9Sstevel@tonic-gate 	struct zoneent ze;
51887c478bd9Sstevel@tonic-gate 
51897c478bd9Sstevel@tonic-gate 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
51907c478bd9Sstevel@tonic-gate 	    state != ZONE_STATE_INCOMPLETE)
51917c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
51927c478bd9Sstevel@tonic-gate 
5193087719fdSdp 	bzero(&ze, sizeof (ze));
51947c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
51957c478bd9Sstevel@tonic-gate 	ze.zone_state = state;
51967c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
51977c478bd9Sstevel@tonic-gate 	return (putzoneent(&ze, PZE_MODIFY));
51987c478bd9Sstevel@tonic-gate }
51997c478bd9Sstevel@tonic-gate 
52007c478bd9Sstevel@tonic-gate /*
52017c478bd9Sstevel@tonic-gate  * Get id (if any) for specified zone.  There are four possible outcomes:
52027c478bd9Sstevel@tonic-gate  * - If the string corresponds to the numeric id of an active (booted)
52037c478bd9Sstevel@tonic-gate  *   zone, sets *zip to the zone id and returns 0.
52047c478bd9Sstevel@tonic-gate  * - If the string corresponds to the name of an active (booted) zone,
52057c478bd9Sstevel@tonic-gate  *   sets *zip to the zone id and returns 0.
52067c478bd9Sstevel@tonic-gate  * - If the string is a name in the configuration but is not booted,
52077c478bd9Sstevel@tonic-gate  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
52087c478bd9Sstevel@tonic-gate  * - Otherwise, leaves *zip unchanged and returns -1.
52097c478bd9Sstevel@tonic-gate  *
52107c478bd9Sstevel@tonic-gate  * This function acts as an auxiliary filter on the function of the same
52117c478bd9Sstevel@tonic-gate  * name in libc; the linker binds to this version if libzonecfg exists,
52127c478bd9Sstevel@tonic-gate  * and the libc version if it doesn't.  Any changes to this version of
52137c478bd9Sstevel@tonic-gate  * the function should probably be reflected in the libc version as well.
52147c478bd9Sstevel@tonic-gate  */
52157c478bd9Sstevel@tonic-gate int
52167c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip)
52177c478bd9Sstevel@tonic-gate {
52187c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl;
52197c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
52207c478bd9Sstevel@tonic-gate 	char *cp;
52217c478bd9Sstevel@tonic-gate 	int err;
52227c478bd9Sstevel@tonic-gate 
52237c478bd9Sstevel@tonic-gate 	/* first try looking for active zone by id */
52247c478bd9Sstevel@tonic-gate 	errno = 0;
52257c478bd9Sstevel@tonic-gate 	zoneid = (zoneid_t)strtol(str, &cp, 0);
52267c478bd9Sstevel@tonic-gate 	if (errno == 0 && cp != str && *cp == '\0' &&
52277c478bd9Sstevel@tonic-gate 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
52287c478bd9Sstevel@tonic-gate 		*zip = zoneid;
52297c478bd9Sstevel@tonic-gate 		return (0);
52307c478bd9Sstevel@tonic-gate 	}
52317c478bd9Sstevel@tonic-gate 
52327c478bd9Sstevel@tonic-gate 	/* then look for active zone by name */
52337c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(str)) != -1) {
52347c478bd9Sstevel@tonic-gate 		*zip = zoneid;
52357c478bd9Sstevel@tonic-gate 		return (0);
52367c478bd9Sstevel@tonic-gate 	}
52377c478bd9Sstevel@tonic-gate 
52387c478bd9Sstevel@tonic-gate 	/* if in global zone, try looking up name in configuration database */
52397c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID ||
52407c478bd9Sstevel@tonic-gate 	    (hdl = zonecfg_init_handle()) == NULL)
52417c478bd9Sstevel@tonic-gate 		return (-1);
52427c478bd9Sstevel@tonic-gate 
5243108322fbScarlsonj 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
52447c478bd9Sstevel@tonic-gate 		/* zone exists but isn't active */
52457c478bd9Sstevel@tonic-gate 		*zip = ZONE_ID_UNDEFINED;
52467c478bd9Sstevel@tonic-gate 		err = 0;
52477c478bd9Sstevel@tonic-gate 	} else {
52487c478bd9Sstevel@tonic-gate 		err = -1;
52497c478bd9Sstevel@tonic-gate 	}
52507c478bd9Sstevel@tonic-gate 
52517c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(hdl);
52527c478bd9Sstevel@tonic-gate 	return (err);
52537c478bd9Sstevel@tonic-gate }
52547c478bd9Sstevel@tonic-gate 
52557c478bd9Sstevel@tonic-gate char *
52567c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num)
52577c478bd9Sstevel@tonic-gate {
52587c478bd9Sstevel@tonic-gate 	switch (state_num) {
52597c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
52607c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_CONFIGURED);
52617c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
52627c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INCOMPLETE);
52637c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
52647c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INSTALLED);
52657c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
52667c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_READY);
5267108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
5268108322fbScarlsonj 		return (ZONE_STATE_STR_MOUNTED);
52697c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
52707c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_RUNNING);
52717c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
52727c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_SHUTTING_DOWN);
52737c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
52747c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_DOWN);
52757c478bd9Sstevel@tonic-gate 	default:
52767c478bd9Sstevel@tonic-gate 		return ("unknown");
52777c478bd9Sstevel@tonic-gate 	}
52787c478bd9Sstevel@tonic-gate }
52797c478bd9Sstevel@tonic-gate 
52807c478bd9Sstevel@tonic-gate /*
5281108322fbScarlsonj  * Given a UUID value, find an associated zone name.  This is intended to be
5282108322fbScarlsonj  * used by callers who set up some 'default' name (corresponding to the
5283108322fbScarlsonj  * expected name for the zone) in the zonename buffer, and thus the function
5284108322fbScarlsonj  * doesn't touch this buffer on failure.
5285108322fbScarlsonj  */
5286108322fbScarlsonj int
5287555afedfScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
5288108322fbScarlsonj {
5289108322fbScarlsonj 	FILE *fp;
5290108322fbScarlsonj 	struct zoneent *ze;
5291555afedfScarlsonj 	uchar_t *uuid;
5292108322fbScarlsonj 
5293108322fbScarlsonj 	/*
5294108322fbScarlsonj 	 * A small amount of subterfuge via casts is necessary here because
5295108322fbScarlsonj 	 * libuuid doesn't use const correctly, but we don't want to export
5296108322fbScarlsonj 	 * this brokenness to our clients.
5297108322fbScarlsonj 	 */
5298555afedfScarlsonj 	uuid = (uchar_t *)uuidin;
5299555afedfScarlsonj 	if (uuid_is_null(uuid))
5300108322fbScarlsonj 		return (Z_NO_ZONE);
5301108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5302108322fbScarlsonj 		return (Z_NO_ZONE);
5303108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5304555afedfScarlsonj 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
5305108322fbScarlsonj 			break;
5306108322fbScarlsonj 		free(ze);
5307108322fbScarlsonj 	}
5308108322fbScarlsonj 	endzoneent(fp);
5309108322fbScarlsonj 	if (ze != NULL) {
5310108322fbScarlsonj 		(void) strlcpy(zonename, ze->zone_name, namelen);
5311108322fbScarlsonj 		free(ze);
5312108322fbScarlsonj 		return (Z_OK);
5313108322fbScarlsonj 	} else {
5314108322fbScarlsonj 		return (Z_NO_ZONE);
5315108322fbScarlsonj 	}
5316108322fbScarlsonj }
5317108322fbScarlsonj 
5318108322fbScarlsonj /*
5319108322fbScarlsonj  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
5320108322fbScarlsonj  * exists but the file doesn't have a value set yet.  Returns an error if the
5321108322fbScarlsonj  * zone cannot be located.
5322108322fbScarlsonj  */
5323108322fbScarlsonj int
5324108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid)
5325108322fbScarlsonj {
5326108322fbScarlsonj 	FILE *fp;
5327108322fbScarlsonj 	struct zoneent *ze;
5328108322fbScarlsonj 
5329108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5330108322fbScarlsonj 		return (Z_NO_ZONE);
5331108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5332108322fbScarlsonj 		if (strcmp(ze->zone_name, zonename) == 0)
5333108322fbScarlsonj 			break;
5334108322fbScarlsonj 		free(ze);
5335108322fbScarlsonj 	}
5336108322fbScarlsonj 	endzoneent(fp);
5337108322fbScarlsonj 	if (ze != NULL) {
5338108322fbScarlsonj 		uuid_copy(uuid, ze->zone_uuid);
5339108322fbScarlsonj 		free(ze);
5340108322fbScarlsonj 		return (Z_OK);
5341108322fbScarlsonj 	} else {
5342108322fbScarlsonj 		return (Z_NO_ZONE);
5343108322fbScarlsonj 	}
5344108322fbScarlsonj }
5345108322fbScarlsonj 
5346108322fbScarlsonj /*
53477c478bd9Sstevel@tonic-gate  * File-system convenience functions.
53487c478bd9Sstevel@tonic-gate  */
53497c478bd9Sstevel@tonic-gate boolean_t
53507c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type)
53517c478bd9Sstevel@tonic-gate {
53527c478bd9Sstevel@tonic-gate 	/*
53537c478bd9Sstevel@tonic-gate 	 * We already know which FS types don't work.
53547c478bd9Sstevel@tonic-gate 	 */
53557c478bd9Sstevel@tonic-gate 	if (strcmp(type, "proc") == 0 ||
53567c478bd9Sstevel@tonic-gate 	    strcmp(type, "mntfs") == 0 ||
53577c478bd9Sstevel@tonic-gate 	    strcmp(type, "autofs") == 0 ||
53587c478bd9Sstevel@tonic-gate 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
53597c478bd9Sstevel@tonic-gate 	    strcmp(type, "cachefs") == 0)
53607c478bd9Sstevel@tonic-gate 		return (B_FALSE);
53617c478bd9Sstevel@tonic-gate 	/*
53627c478bd9Sstevel@tonic-gate 	 * The caller may do more detailed verification to make sure other
53637c478bd9Sstevel@tonic-gate 	 * aspects of this filesystem type make sense.
53647c478bd9Sstevel@tonic-gate 	 */
53657c478bd9Sstevel@tonic-gate 	return (B_TRUE);
53667c478bd9Sstevel@tonic-gate }
53677c478bd9Sstevel@tonic-gate 
53687c478bd9Sstevel@tonic-gate /*
53697c478bd9Sstevel@tonic-gate  * Generally uninteresting rctl convenience functions.
53707c478bd9Sstevel@tonic-gate  */
53717c478bd9Sstevel@tonic-gate 
53727c478bd9Sstevel@tonic-gate int
53737c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
53747c478bd9Sstevel@tonic-gate     rctlblk_t *rctlblk)
53757c478bd9Sstevel@tonic-gate {
53767c478bd9Sstevel@tonic-gate 	unsigned long long ull;
53777c478bd9Sstevel@tonic-gate 	char *endp;
53787c478bd9Sstevel@tonic-gate 	rctl_priv_t priv;
53797c478bd9Sstevel@tonic-gate 	rctl_qty_t limit;
53807c478bd9Sstevel@tonic-gate 	uint_t action;
53817c478bd9Sstevel@tonic-gate 
53827c478bd9Sstevel@tonic-gate 	/* Get the privilege */
53837c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
53847c478bd9Sstevel@tonic-gate 		priv = RCPRIV_BASIC;
53857c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
53867c478bd9Sstevel@tonic-gate 		priv = RCPRIV_PRIVILEGED;
53877c478bd9Sstevel@tonic-gate 	} else {
53887c478bd9Sstevel@tonic-gate 		/* Invalid privilege */
53897c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
53907c478bd9Sstevel@tonic-gate 	}
53917c478bd9Sstevel@tonic-gate 
53927c478bd9Sstevel@tonic-gate 	/* deal with negative input; strtoull(3c) doesn't do what we want */
53937c478bd9Sstevel@tonic-gate 	if (rctlval->zone_rctlval_limit[0] == '-')
53947c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
53957c478bd9Sstevel@tonic-gate 	/* Get the limit */
53967c478bd9Sstevel@tonic-gate 	errno = 0;
53977c478bd9Sstevel@tonic-gate 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
53987c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endp != '\0') {
53997c478bd9Sstevel@tonic-gate 		/* parse failed */
54007c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54017c478bd9Sstevel@tonic-gate 	}
54027c478bd9Sstevel@tonic-gate 	limit = (rctl_qty_t)ull;
54037c478bd9Sstevel@tonic-gate 
54047c478bd9Sstevel@tonic-gate 	/* Get the action */
54057c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
54067c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_NOACTION;
54077c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
54087c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_SIGNAL;
54097c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
54107c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_DENY;
54117c478bd9Sstevel@tonic-gate 	} else {
54127c478bd9Sstevel@tonic-gate 		/* Invalid Action */
54137c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54147c478bd9Sstevel@tonic-gate 	}
54157c478bd9Sstevel@tonic-gate 	rctlblk_set_local_action(rctlblk, action, 0);
54167c478bd9Sstevel@tonic-gate 	rctlblk_set_privilege(rctlblk, priv);
54177c478bd9Sstevel@tonic-gate 	rctlblk_set_value(rctlblk, limit);
54187c478bd9Sstevel@tonic-gate 	return (Z_OK);
54197c478bd9Sstevel@tonic-gate }
54207c478bd9Sstevel@tonic-gate 
54217c478bd9Sstevel@tonic-gate static int
54227c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg)
54237c478bd9Sstevel@tonic-gate {
54247c478bd9Sstevel@tonic-gate 	const char *attrname = arg;
54257c478bd9Sstevel@tonic-gate 
54267c478bd9Sstevel@tonic-gate 	/*
54277c478bd9Sstevel@tonic-gate 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
54287c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
54297c478bd9Sstevel@tonic-gate 	 */
54307c478bd9Sstevel@tonic-gate 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
54317c478bd9Sstevel@tonic-gate }
54327c478bd9Sstevel@tonic-gate 
54337c478bd9Sstevel@tonic-gate boolean_t
54347c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name)
54357c478bd9Sstevel@tonic-gate {
54367c478bd9Sstevel@tonic-gate 	return (rctl_walk(rctl_check, (void *)name) == 1);
54377c478bd9Sstevel@tonic-gate }
54387c478bd9Sstevel@tonic-gate 
54397c478bd9Sstevel@tonic-gate boolean_t
54407c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name)
54417c478bd9Sstevel@tonic-gate {
54427c478bd9Sstevel@tonic-gate 	const char *c;
54437c478bd9Sstevel@tonic-gate 
54447c478bd9Sstevel@tonic-gate 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
54457c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54467c478bd9Sstevel@tonic-gate 	if (strlen(name) == sizeof ("zone.") - 1)
54477c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54487c478bd9Sstevel@tonic-gate 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
54497c478bd9Sstevel@tonic-gate 		if (!isalpha(*c) && *c != '-')
54507c478bd9Sstevel@tonic-gate 			return (B_FALSE);
54517c478bd9Sstevel@tonic-gate 	}
54527c478bd9Sstevel@tonic-gate 	return (B_TRUE);
54537c478bd9Sstevel@tonic-gate }
54547c478bd9Sstevel@tonic-gate 
54557c478bd9Sstevel@tonic-gate boolean_t
54567c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
54577c478bd9Sstevel@tonic-gate {
54587c478bd9Sstevel@tonic-gate 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
54597c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
54607c478bd9Sstevel@tonic-gate 
54617c478bd9Sstevel@tonic-gate 	if (priv != RCPRIV_PRIVILEGED)
54627c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54637c478bd9Sstevel@tonic-gate 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
54647c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54657c478bd9Sstevel@tonic-gate 	return (B_TRUE);
54667c478bd9Sstevel@tonic-gate }
54677c478bd9Sstevel@tonic-gate 
54687c478bd9Sstevel@tonic-gate boolean_t
54697c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
54707c478bd9Sstevel@tonic-gate {
54717c478bd9Sstevel@tonic-gate 	rctlblk_t *current, *next;
54727c478bd9Sstevel@tonic-gate 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
54737c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
54747c478bd9Sstevel@tonic-gate 	uint_t global_flags;
54757c478bd9Sstevel@tonic-gate 
54767c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
54777c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54787c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlname(name))
54797c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54807c478bd9Sstevel@tonic-gate 
54817c478bd9Sstevel@tonic-gate 	current = alloca(rctlblk_size());
54827c478bd9Sstevel@tonic-gate 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
54837c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* not an rctl on this system */
54847c478bd9Sstevel@tonic-gate 	/*
54857c478bd9Sstevel@tonic-gate 	 * Make sure the proposed value isn't greater than the current system
54867c478bd9Sstevel@tonic-gate 	 * value.
54877c478bd9Sstevel@tonic-gate 	 */
54887c478bd9Sstevel@tonic-gate 	next = alloca(rctlblk_size());
54897c478bd9Sstevel@tonic-gate 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
54907c478bd9Sstevel@tonic-gate 		rctlblk_t *tmp;
54917c478bd9Sstevel@tonic-gate 
54927c478bd9Sstevel@tonic-gate 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
54937c478bd9Sstevel@tonic-gate 			return (B_FALSE);	/* shouldn't happen */
54947c478bd9Sstevel@tonic-gate 		tmp = current;
54957c478bd9Sstevel@tonic-gate 		current = next;
54967c478bd9Sstevel@tonic-gate 		next = tmp;
54977c478bd9Sstevel@tonic-gate 	}
54987c478bd9Sstevel@tonic-gate 	if (limit > rctlblk_get_value(current))
54997c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55007c478bd9Sstevel@tonic-gate 
55017c478bd9Sstevel@tonic-gate 	/*
55027c478bd9Sstevel@tonic-gate 	 * Make sure the proposed action is allowed.
55037c478bd9Sstevel@tonic-gate 	 */
55047c478bd9Sstevel@tonic-gate 	global_flags = rctlblk_get_global_flags(current);
55057c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
55067c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_DENY)
55077c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55087c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
55097c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_NOACTION)
55107c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55117c478bd9Sstevel@tonic-gate 
55127c478bd9Sstevel@tonic-gate 	return (B_TRUE);
55137c478bd9Sstevel@tonic-gate }
5514fa9e4066Sahrens 
5515cf8f45c7Sdstaff /*
5516cf8f45c7Sdstaff  * There is always a race condition between reading the initial copy of
5517cf8f45c7Sdstaff  * a zones state and its state changing.  We address this by providing
5518cf8f45c7Sdstaff  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
5519cf8f45c7Sdstaff  * When zonecfg_critical_enter is called, sets the state field to LOCKED
5520cf8f45c7Sdstaff  * and aquires biglock. Biglock protects against other threads executing
5521cf8f45c7Sdstaff  * critical_enter and the state field protects against state changes during
5522cf8f45c7Sdstaff  * the critical period.
5523cf8f45c7Sdstaff  *
5524cf8f45c7Sdstaff  * If any state changes occur, zn_cb will set the failed field of the znotify
5525cf8f45c7Sdstaff  * structure.  This will cause the critical_exit function to re-lock the
5526cf8f45c7Sdstaff  * channel and return an error. Since evsnts may be delayed, the critical_exit
5527cf8f45c7Sdstaff  * function "flushes" the queue by putting an event on the queue and waiting for
5528cf8f45c7Sdstaff  * zn_cb to notify critical_exit that it received the ping event.
5529cf8f45c7Sdstaff  */
5530cf8f45c7Sdstaff static const char *
5531cf8f45c7Sdstaff string_get_tok(const char *in, char delim, int num)
5532cf8f45c7Sdstaff {
5533cf8f45c7Sdstaff 	int i = 0;
5534cf8f45c7Sdstaff 
5535cf8f45c7Sdstaff 	for (; i < num; in++) {
5536cf8f45c7Sdstaff 		if (*in == delim)
5537cf8f45c7Sdstaff 			i++;
5538cf8f45c7Sdstaff 		if (*in == 0)
5539cf8f45c7Sdstaff 			return (NULL);
5540cf8f45c7Sdstaff 	}
5541cf8f45c7Sdstaff 	return (in);
5542cf8f45c7Sdstaff }
5543cf8f45c7Sdstaff 
5544cf8f45c7Sdstaff static boolean_t
5545cf8f45c7Sdstaff is_ping(sysevent_t *ev)
5546cf8f45c7Sdstaff {
5547cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5548cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
5549cf8f45c7Sdstaff 		return (B_TRUE);
5550cf8f45c7Sdstaff 	} else {
5551cf8f45c7Sdstaff 		return (B_FALSE);
5552cf8f45c7Sdstaff 	}
5553cf8f45c7Sdstaff }
5554cf8f45c7Sdstaff 
5555cf8f45c7Sdstaff static boolean_t
5556cf8f45c7Sdstaff is_my_ping(sysevent_t *ev)
5557cf8f45c7Sdstaff {
5558cf8f45c7Sdstaff 	const char *sender;
5559cf8f45c7Sdstaff 	char mypid[sizeof (pid_t) * 3 + 1];
5560cf8f45c7Sdstaff 
5561cf8f45c7Sdstaff 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
5562cf8f45c7Sdstaff 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
5563cf8f45c7Sdstaff 	if (sender == NULL)
5564cf8f45c7Sdstaff 		return (B_FALSE);
5565cf8f45c7Sdstaff 	if (strcmp(sender, mypid) != 0)
5566cf8f45c7Sdstaff 		return (B_FALSE);
5567cf8f45c7Sdstaff 	return (B_TRUE);
5568cf8f45c7Sdstaff }
5569cf8f45c7Sdstaff 
5570cf8f45c7Sdstaff static int
5571cf8f45c7Sdstaff do_callback(struct znotify *zevtchan, sysevent_t *ev)
5572cf8f45c7Sdstaff {
5573cf8f45c7Sdstaff 	nvlist_t *l;
5574cf8f45c7Sdstaff 	int zid;
5575cf8f45c7Sdstaff 	char *zonename;
5576cf8f45c7Sdstaff 	char *newstate;
5577cf8f45c7Sdstaff 	char *oldstate;
5578cf8f45c7Sdstaff 	int ret;
5579cf8f45c7Sdstaff 	hrtime_t when;
5580cf8f45c7Sdstaff 
5581cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5582cf8f45c7Sdstaff 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
5583cf8f45c7Sdstaff 
5584cf8f45c7Sdstaff 		if (sysevent_get_attr_list(ev, &l) != 0) {
5585cf8f45c7Sdstaff 			if (errno == ENOMEM) {
5586cf8f45c7Sdstaff 				zevtchan->zn_failure_count++;
5587cf8f45c7Sdstaff 				return (EAGAIN);
5588cf8f45c7Sdstaff 			}
5589cf8f45c7Sdstaff 			return (0);
5590cf8f45c7Sdstaff 		}
5591cf8f45c7Sdstaff 		ret = 0;
5592cf8f45c7Sdstaff 
5593cf8f45c7Sdstaff 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
5594cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
5595cf8f45c7Sdstaff 		    == 0) &&
5596cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
5597cf8f45c7Sdstaff 		    == 0) &&
5598cf8f45c7Sdstaff 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
5599cf8f45c7Sdstaff 		    (uint64_t *)&when) == 0) &&
5600cf8f45c7Sdstaff 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
5601cf8f45c7Sdstaff 			ret = zevtchan->zn_callback(zonename, zid, newstate,
5602cf8f45c7Sdstaff 			    oldstate, when, zevtchan->zn_private);
5603cf8f45c7Sdstaff 		}
5604cf8f45c7Sdstaff 
5605cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5606cf8f45c7Sdstaff 		nvlist_free(l);
5607cf8f45c7Sdstaff 		return (ret);
5608cf8f45c7Sdstaff 	} else {
5609cf8f45c7Sdstaff 		/*
5610cf8f45c7Sdstaff 		 * We have received an event in an unknown subclass. Ignore.
5611cf8f45c7Sdstaff 		 */
5612cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5613cf8f45c7Sdstaff 		return (0);
5614cf8f45c7Sdstaff 	}
5615cf8f45c7Sdstaff }
5616cf8f45c7Sdstaff 
5617cf8f45c7Sdstaff static int
5618cf8f45c7Sdstaff zn_cb(sysevent_t *ev, void *p)
5619cf8f45c7Sdstaff {
5620cf8f45c7Sdstaff 	struct znotify *zevtchan = p;
5621cf8f45c7Sdstaff 	int error;
5622cf8f45c7Sdstaff 
5623cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5624cf8f45c7Sdstaff 
5625cf8f45c7Sdstaff 	if (is_ping(ev) && !is_my_ping(ev)) {
5626cf8f45c7Sdstaff 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
5627cf8f45c7Sdstaff 		return (0);
5628cf8f45c7Sdstaff 	}
5629cf8f45c7Sdstaff 
5630cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_LOCKED) {
5631cf8f45c7Sdstaff 		assert(!is_ping(ev));
5632cf8f45c7Sdstaff 		zevtchan->zn_failed = B_TRUE;
5633cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5634cf8f45c7Sdstaff 		return (0);
5635cf8f45c7Sdstaff 	}
5636cf8f45c7Sdstaff 
5637cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
5638cf8f45c7Sdstaff 		if (is_ping(ev)) {
5639cf8f45c7Sdstaff 			zevtchan->zn_state = ZN_PING_RECEIVED;
5640cf8f45c7Sdstaff 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
5641cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5642cf8f45c7Sdstaff 			return (0);
5643cf8f45c7Sdstaff 		} else {
5644cf8f45c7Sdstaff 			zevtchan->zn_failed = B_TRUE;
5645cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5646cf8f45c7Sdstaff 			return (0);
5647cf8f45c7Sdstaff 		}
5648cf8f45c7Sdstaff 	}
5649cf8f45c7Sdstaff 
5650cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED) {
5651cf8f45c7Sdstaff 
5652cf8f45c7Sdstaff 		error = do_callback(zevtchan, ev);
5653cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5654cf8f45c7Sdstaff 		/*
5655cf8f45c7Sdstaff 		 * Every ENOMEM failure causes do_callback to increment
5656cf8f45c7Sdstaff 		 * zn_failure_count and every success causes it to
5657cf8f45c7Sdstaff 		 * set zn_failure_count to zero.  If we got EAGAIN,
5658cf8f45c7Sdstaff 		 * we will sleep for zn_failure_count seconds and return
5659cf8f45c7Sdstaff 		 * EAGAIN to gpec to try again.
5660cf8f45c7Sdstaff 		 *
5661cf8f45c7Sdstaff 		 * After 55 seconds, or 10 try's we give up and drop the
5662cf8f45c7Sdstaff 		 * event.
5663cf8f45c7Sdstaff 		 */
5664cf8f45c7Sdstaff 		if (error == EAGAIN) {
5665cf8f45c7Sdstaff 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
5666cf8f45c7Sdstaff 				return (0);
5667cf8f45c7Sdstaff 			}
5668cf8f45c7Sdstaff 			(void) sleep(zevtchan->zn_failure_count);
5669cf8f45c7Sdstaff 		}
5670cf8f45c7Sdstaff 		return (error);
5671cf8f45c7Sdstaff 	}
5672cf8f45c7Sdstaff 
5673cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
5674cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5675cf8f45c7Sdstaff 		return (0);
5676cf8f45c7Sdstaff 	}
5677cf8f45c7Sdstaff 
5678cf8f45c7Sdstaff 	abort();
5679cf8f45c7Sdstaff 	return (0);
5680cf8f45c7Sdstaff }
5681cf8f45c7Sdstaff 
5682cf8f45c7Sdstaff void
5683cf8f45c7Sdstaff zonecfg_notify_critical_enter(void *h)
5684cf8f45c7Sdstaff {
5685cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5686cf8f45c7Sdstaff 
5687cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
5688cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_LOCKED;
5689cf8f45c7Sdstaff }
5690cf8f45c7Sdstaff 
5691cf8f45c7Sdstaff int
5692cf8f45c7Sdstaff zonecfg_notify_critical_exit(void * h)
5693cf8f45c7Sdstaff {
5694cf8f45c7Sdstaff 
5695cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5696cf8f45c7Sdstaff 
5697cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED)
5698cf8f45c7Sdstaff 		return (0);
5699cf8f45c7Sdstaff 
5700cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5701cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_PING_INFLIGHT;
5702cf8f45c7Sdstaff 
5703ee519a1fSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
5704ee519a1fSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
5705cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
5706cf8f45c7Sdstaff 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
5707cf8f45c7Sdstaff 
5708cf8f45c7Sdstaff 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
5709cf8f45c7Sdstaff 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
5710cf8f45c7Sdstaff 		    &(zevtchan->zn_mutex));
5711cf8f45c7Sdstaff 	}
5712cf8f45c7Sdstaff 
5713cf8f45c7Sdstaff 	if (zevtchan->zn_failed == B_TRUE) {
5714cf8f45c7Sdstaff 		zevtchan->zn_state = ZN_LOCKED;
5715cf8f45c7Sdstaff 		zevtchan->zn_failed = B_FALSE;
5716cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5717cf8f45c7Sdstaff 		return (1);
5718cf8f45c7Sdstaff 	}
5719cf8f45c7Sdstaff 
5720cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5721cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5722cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5723cf8f45c7Sdstaff 	return (0);
5724cf8f45c7Sdstaff }
5725cf8f45c7Sdstaff 
5726cf8f45c7Sdstaff void
5727cf8f45c7Sdstaff zonecfg_notify_critical_abort(void *h)
5728cf8f45c7Sdstaff {
5729cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5730cf8f45c7Sdstaff 
5731cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5732cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
5733cf8f45c7Sdstaff 	/*
5734cf8f45c7Sdstaff 	 * Don't do anything about zn_lock. If it is held, it could only be
5735cf8f45c7Sdstaff 	 * held by zn_cb and it will be unlocked soon.
5736cf8f45c7Sdstaff 	 */
5737cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5738cf8f45c7Sdstaff }
5739cf8f45c7Sdstaff 
5740cf8f45c7Sdstaff void *
5741cf8f45c7Sdstaff zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
5742cf8f45c7Sdstaff     const char *newstate, const char *oldstate, hrtime_t when, void *p),
5743cf8f45c7Sdstaff     void *p)
5744cf8f45c7Sdstaff {
5745cf8f45c7Sdstaff 	struct znotify *zevtchan;
5746cf8f45c7Sdstaff 	int i = 1;
5747cf8f45c7Sdstaff 	int r;
5748cf8f45c7Sdstaff 
5749cf8f45c7Sdstaff 	zevtchan = malloc(sizeof (struct znotify));
5750cf8f45c7Sdstaff 
5751cf8f45c7Sdstaff 	if (zevtchan == NULL)
5752cf8f45c7Sdstaff 		return (NULL);
5753cf8f45c7Sdstaff 
5754cf8f45c7Sdstaff 	zevtchan->zn_private = p;
5755cf8f45c7Sdstaff 	zevtchan->zn_callback = func;
5756cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5757cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
5758cf8f45c7Sdstaff 
5759cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
57609d4be64eSdstaff 		goto out3;
5761cf8f45c7Sdstaff 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
5762cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
57639d4be64eSdstaff 		goto out3;
5764cf8f45c7Sdstaff 	}
5765cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
5766cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
5767cf8f45c7Sdstaff 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
57689d4be64eSdstaff 		goto out3;
5769cf8f45c7Sdstaff 	}
5770cf8f45c7Sdstaff 
5771cf8f45c7Sdstaff 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
5772cf8f45c7Sdstaff 	    0) != 0)
5773cf8f45c7Sdstaff 		goto out2;
5774cf8f45c7Sdstaff 
5775cf8f45c7Sdstaff 	do {
5776cf8f45c7Sdstaff 		/*
5777cf8f45c7Sdstaff 		 * At 4 digits the subscriber ID gets too long and we have
5778cf8f45c7Sdstaff 		 * no chance of successfully registering.
5779cf8f45c7Sdstaff 		 */
5780cf8f45c7Sdstaff 		if (i > 999)
57819d4be64eSdstaff 			goto out1;
5782cf8f45c7Sdstaff 
5783cf8f45c7Sdstaff 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
5784cf8f45c7Sdstaff 		    getpid() % 999999l, i);
5785cf8f45c7Sdstaff 
5786cf8f45c7Sdstaff 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
5787cf8f45c7Sdstaff 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
5788cf8f45c7Sdstaff 		    zevtchan, 0);
5789cf8f45c7Sdstaff 
5790cf8f45c7Sdstaff 		i++;
5791cf8f45c7Sdstaff 
5792cf8f45c7Sdstaff 	} while (r);
5793cf8f45c7Sdstaff 
5794cf8f45c7Sdstaff 	return (zevtchan);
57959d4be64eSdstaff out1:
5796cf8f45c7Sdstaff 	sysevent_evc_unbind(zevtchan->zn_eventchan);
57979d4be64eSdstaff out2:
5798cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
5799cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
5800cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
58019d4be64eSdstaff out3:
5802cf8f45c7Sdstaff 	free(zevtchan);
5803cf8f45c7Sdstaff 
5804cf8f45c7Sdstaff 	return (NULL);
5805cf8f45c7Sdstaff }
5806cf8f45c7Sdstaff 
5807cf8f45c7Sdstaff void
5808cf8f45c7Sdstaff zonecfg_notify_unbind(void *handle)
5809cf8f45c7Sdstaff {
5810cf8f45c7Sdstaff 
5811cf8f45c7Sdstaff 	int ret;
5812cf8f45c7Sdstaff 
5813cf8f45c7Sdstaff 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
5814cf8f45c7Sdstaff 	/*
5815cf8f45c7Sdstaff 	 * Check that all evc threads have gone away. This should be
5816cf8f45c7Sdstaff 	 * enforced by sysevent_evc_unbind.
5817cf8f45c7Sdstaff 	 */
5818cf8f45c7Sdstaff 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
5819cf8f45c7Sdstaff 
5820cf8f45c7Sdstaff 	if (ret)
5821cf8f45c7Sdstaff 		abort();
5822cf8f45c7Sdstaff 
5823cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
5824cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
5825cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
5826cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
5827cf8f45c7Sdstaff 
5828cf8f45c7Sdstaff 	free(handle);
5829cf8f45c7Sdstaff }
5830cf8f45c7Sdstaff 
5831fa9e4066Sahrens static int
5832fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5833fa9e4066Sahrens {
5834fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
5835fa9e4066Sahrens 	int err;
5836fa9e4066Sahrens 
5837fa9e4066Sahrens 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
5838fa9e4066Sahrens 	if ((err = newprop(newnode, DTD_ATTR_NAME,
5839fa9e4066Sahrens 	    tabptr->zone_dataset_name)) != Z_OK)
5840fa9e4066Sahrens 		return (err);
5841fa9e4066Sahrens 	return (Z_OK);
5842fa9e4066Sahrens }
5843fa9e4066Sahrens 
5844fa9e4066Sahrens int
5845fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5846fa9e4066Sahrens {
5847fa9e4066Sahrens 	int err;
5848fa9e4066Sahrens 
5849fa9e4066Sahrens 	if (tabptr == NULL)
5850fa9e4066Sahrens 		return (Z_INVAL);
5851fa9e4066Sahrens 
5852fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5853fa9e4066Sahrens 		return (err);
5854fa9e4066Sahrens 
5855fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
5856fa9e4066Sahrens 		return (err);
5857fa9e4066Sahrens 
5858fa9e4066Sahrens 	return (Z_OK);
5859fa9e4066Sahrens }
5860fa9e4066Sahrens 
5861fa9e4066Sahrens static int
5862fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5863fa9e4066Sahrens {
5864fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
5865fa9e4066Sahrens 
5866fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
5867fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
5868fa9e4066Sahrens 			continue;
5869fa9e4066Sahrens 
5870fa9e4066Sahrens 		if (match_prop(cur, DTD_ATTR_NAME,
5871fa9e4066Sahrens 		    tabptr->zone_dataset_name)) {
5872fa9e4066Sahrens 			xmlUnlinkNode(cur);
5873fa9e4066Sahrens 			xmlFreeNode(cur);
5874fa9e4066Sahrens 			return (Z_OK);
5875fa9e4066Sahrens 		}
5876fa9e4066Sahrens 	}
5877fa9e4066Sahrens 	return (Z_NO_RESOURCE_ID);
5878fa9e4066Sahrens }
5879fa9e4066Sahrens 
5880fa9e4066Sahrens int
5881fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5882fa9e4066Sahrens {
5883fa9e4066Sahrens 	int err;
5884fa9e4066Sahrens 
5885fa9e4066Sahrens 	if (tabptr == NULL)
5886fa9e4066Sahrens 		return (Z_INVAL);
5887fa9e4066Sahrens 
5888fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5889fa9e4066Sahrens 		return (err);
5890fa9e4066Sahrens 
5891fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
5892fa9e4066Sahrens 		return (err);
5893fa9e4066Sahrens 
5894fa9e4066Sahrens 	return (Z_OK);
5895fa9e4066Sahrens }
5896fa9e4066Sahrens 
5897fa9e4066Sahrens int
5898fa9e4066Sahrens zonecfg_modify_ds(
5899fa9e4066Sahrens 	zone_dochandle_t handle,
5900fa9e4066Sahrens 	struct zone_dstab *oldtabptr,
5901fa9e4066Sahrens 	struct zone_dstab *newtabptr)
5902fa9e4066Sahrens {
5903fa9e4066Sahrens 	int err;
5904fa9e4066Sahrens 
5905fa9e4066Sahrens 	if (oldtabptr == NULL || newtabptr == NULL)
5906fa9e4066Sahrens 		return (Z_INVAL);
5907fa9e4066Sahrens 
5908fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5909fa9e4066Sahrens 		return (err);
5910fa9e4066Sahrens 
5911fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
5912fa9e4066Sahrens 		return (err);
5913fa9e4066Sahrens 
5914fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
5915fa9e4066Sahrens 		return (err);
5916fa9e4066Sahrens 
5917fa9e4066Sahrens 	return (Z_OK);
5918fa9e4066Sahrens }
5919fa9e4066Sahrens 
5920fa9e4066Sahrens int
5921fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5922fa9e4066Sahrens {
5923fa9e4066Sahrens 	xmlNodePtr cur, firstmatch;
5924fa9e4066Sahrens 	int err;
5925fa9e4066Sahrens 	char dataset[MAXNAMELEN];
5926fa9e4066Sahrens 
5927fa9e4066Sahrens 	if (tabptr == NULL)
5928fa9e4066Sahrens 		return (Z_INVAL);
5929fa9e4066Sahrens 
5930fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5931fa9e4066Sahrens 		return (err);
5932fa9e4066Sahrens 
5933fa9e4066Sahrens 	cur = handle->zone_dh_cur;
5934fa9e4066Sahrens 	firstmatch = NULL;
5935fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
5936fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
5937fa9e4066Sahrens 			continue;
5938fa9e4066Sahrens 		if (strlen(tabptr->zone_dataset_name) > 0) {
5939fa9e4066Sahrens 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
5940fa9e4066Sahrens 			    sizeof (dataset)) == Z_OK) &&
5941fa9e4066Sahrens 			    (strcmp(tabptr->zone_dataset_name,
5942fa9e4066Sahrens 			    dataset) == 0)) {
5943fa9e4066Sahrens 				if (firstmatch == NULL)
5944fa9e4066Sahrens 					firstmatch = cur;
5945fa9e4066Sahrens 				else
5946fa9e4066Sahrens 					return (Z_INSUFFICIENT_SPEC);
5947fa9e4066Sahrens 			}
5948fa9e4066Sahrens 		}
5949fa9e4066Sahrens 	}
5950fa9e4066Sahrens 	if (firstmatch == NULL)
5951fa9e4066Sahrens 		return (Z_NO_RESOURCE_ID);
5952fa9e4066Sahrens 
5953fa9e4066Sahrens 	cur = firstmatch;
5954fa9e4066Sahrens 
5955fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
5956fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
5957fa9e4066Sahrens 		return (err);
5958fa9e4066Sahrens 
5959fa9e4066Sahrens 	return (Z_OK);
5960fa9e4066Sahrens }
5961fa9e4066Sahrens 
5962fa9e4066Sahrens int
5963fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle)
5964fa9e4066Sahrens {
5965fa9e4066Sahrens 	return (zonecfg_setent(handle));
5966fa9e4066Sahrens }
5967fa9e4066Sahrens 
5968fa9e4066Sahrens int
5969fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
5970fa9e4066Sahrens {
5971fa9e4066Sahrens 	xmlNodePtr cur;
5972fa9e4066Sahrens 	int err;
5973fa9e4066Sahrens 
5974fa9e4066Sahrens 	if (handle == NULL)
5975fa9e4066Sahrens 		return (Z_INVAL);
5976fa9e4066Sahrens 
5977fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
5978fa9e4066Sahrens 		return (Z_NO_ENTRY);
5979fa9e4066Sahrens 
5980fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
5981fa9e4066Sahrens 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
5982fa9e4066Sahrens 			break;
5983fa9e4066Sahrens 	if (cur == NULL) {
5984fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
5985fa9e4066Sahrens 		return (Z_NO_ENTRY);
5986fa9e4066Sahrens 	}
5987fa9e4066Sahrens 
5988fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
5989fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
5990fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
5991fa9e4066Sahrens 		return (err);
5992fa9e4066Sahrens 	}
5993fa9e4066Sahrens 
5994fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
5995fa9e4066Sahrens 	return (Z_OK);
5996fa9e4066Sahrens }
5997fa9e4066Sahrens 
5998fa9e4066Sahrens int
5999fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle)
6000fa9e4066Sahrens {
6001fa9e4066Sahrens 	return (zonecfg_endent(handle));
6002fa9e4066Sahrens }
6003ee519a1fSgjelinek 
60040209230bSgjelinek /*
60050209230bSgjelinek  * Support for aliased rctls; that is, rctls that have simplified names in
60060209230bSgjelinek  * zonecfg.  For example, max-lwps is an alias for a well defined zone.max-lwps
60070209230bSgjelinek  * rctl.  If there are multiple existing values for one of these rctls or if
60080209230bSgjelinek  * there is a single value that does not match the well defined template (i.e.
60090209230bSgjelinek  * it has a different action) then we cannot treat the rctl as having an alias
60100209230bSgjelinek  * so we return Z_ALIAS_DISALLOW.  That means that the rctl cannot be
60110209230bSgjelinek  * managed in zonecfg via an alias and that the standard rctl syntax must be
60120209230bSgjelinek  * used.
60130209230bSgjelinek  *
60140209230bSgjelinek  * The possible return values are:
60150209230bSgjelinek  *	Z_NO_PROPERTY_ID - invalid alias name
60160209230bSgjelinek  *	Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition
60170209230bSgjelinek  *	Z_NO_ENTRY - no rctl is configured for this alias
60180209230bSgjelinek  *	Z_OK - we got a valid rctl for the specified alias
60190209230bSgjelinek  */
60200209230bSgjelinek int
60210209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval)
60220209230bSgjelinek {
60230209230bSgjelinek 	boolean_t found = B_FALSE;
60240209230bSgjelinek 	boolean_t found_val = B_FALSE;
60250209230bSgjelinek 	xmlNodePtr cur, val;
60260209230bSgjelinek 	char savedname[MAXNAMELEN];
60270209230bSgjelinek 	struct zone_rctlvaltab rctl;
60280209230bSgjelinek 	int i;
60290209230bSgjelinek 	int err;
60300209230bSgjelinek 
60310209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
60320209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
60330209230bSgjelinek 			break;
60340209230bSgjelinek 
60350209230bSgjelinek 	if (aliases[i].shortname == NULL)
60360209230bSgjelinek 		return (Z_NO_PROPERTY_ID);
60370209230bSgjelinek 
60380209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
60390209230bSgjelinek 		return (err);
60400209230bSgjelinek 
60410209230bSgjelinek 	cur = handle->zone_dh_cur;
60420209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
60430209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0)
60440209230bSgjelinek 			continue;
60450209230bSgjelinek 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
60460209230bSgjelinek 		    sizeof (savedname)) == Z_OK) &&
60470209230bSgjelinek 		    (strcmp(savedname, aliases[i].realname) == 0)) {
60480209230bSgjelinek 
60490209230bSgjelinek 			/*
60500209230bSgjelinek 			 * If we already saw one of these, we can't have an
60510209230bSgjelinek 			 * alias since we just found another.
60520209230bSgjelinek 			 */
60530209230bSgjelinek 			if (found)
60540209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
60550209230bSgjelinek 			found = B_TRUE;
60560209230bSgjelinek 
60570209230bSgjelinek 			for (val = cur->xmlChildrenNode; val != NULL;
60580209230bSgjelinek 			    val = val->next) {
60590209230bSgjelinek 				/*
60600209230bSgjelinek 				 * If we already have one value, we can't have
60610209230bSgjelinek 				 * an alias since we just found another.
60620209230bSgjelinek 				 */
60630209230bSgjelinek 				if (found_val)
60640209230bSgjelinek 					return (Z_ALIAS_DISALLOW);
60650209230bSgjelinek 				found_val = B_TRUE;
60660209230bSgjelinek 
60670209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_PRIV,
60680209230bSgjelinek 				    rctl.zone_rctlval_priv,
60690209230bSgjelinek 				    sizeof (rctl.zone_rctlval_priv)) != Z_OK))
60700209230bSgjelinek 					break;
60710209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_LIMIT,
60720209230bSgjelinek 				    rctl.zone_rctlval_limit,
60730209230bSgjelinek 				    sizeof (rctl.zone_rctlval_limit)) != Z_OK))
60740209230bSgjelinek 					break;
60750209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_ACTION,
60760209230bSgjelinek 				    rctl.zone_rctlval_action,
60770209230bSgjelinek 				    sizeof (rctl.zone_rctlval_action)) != Z_OK))
60780209230bSgjelinek 					break;
60790209230bSgjelinek 			}
60800209230bSgjelinek 
60810209230bSgjelinek 			/* check priv and action match the expected vals */
60820209230bSgjelinek 			if (strcmp(rctl.zone_rctlval_priv,
60830209230bSgjelinek 			    aliases[i].priv) != 0 ||
60840209230bSgjelinek 			    strcmp(rctl.zone_rctlval_action,
60850209230bSgjelinek 			    aliases[i].action) != 0)
60860209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
60870209230bSgjelinek 		}
60880209230bSgjelinek 	}
60890209230bSgjelinek 
60900209230bSgjelinek 	if (found) {
60910209230bSgjelinek 		*rval = strtoull(rctl.zone_rctlval_limit, NULL, 10);
60920209230bSgjelinek 		return (Z_OK);
60930209230bSgjelinek 	}
60940209230bSgjelinek 
60950209230bSgjelinek 	return (Z_NO_ENTRY);
60960209230bSgjelinek }
60970209230bSgjelinek 
60980209230bSgjelinek int
60990209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name)
61000209230bSgjelinek {
61010209230bSgjelinek 	int i;
61020209230bSgjelinek 	uint64_t val;
61030209230bSgjelinek 	struct zone_rctltab rctltab;
61040209230bSgjelinek 
61050209230bSgjelinek 	/*
61060209230bSgjelinek 	 * First check that we have a valid aliased rctl to remove.
61070209230bSgjelinek 	 * This will catch an rctl entry with non-standard values or
61080209230bSgjelinek 	 * multiple rctl values for this name.  We need to ignore those
61090209230bSgjelinek 	 * rctl entries.
61100209230bSgjelinek 	 */
61110209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK)
61120209230bSgjelinek 		return (Z_OK);
61130209230bSgjelinek 
61140209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
61150209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
61160209230bSgjelinek 			break;
61170209230bSgjelinek 
61180209230bSgjelinek 	if (aliases[i].shortname == NULL)
61190209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
61200209230bSgjelinek 
61210209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
61220209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
61230209230bSgjelinek 
61240209230bSgjelinek 	return (zonecfg_delete_rctl(handle, &rctltab));
61250209230bSgjelinek }
61260209230bSgjelinek 
61270209230bSgjelinek boolean_t
61280209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name)
61290209230bSgjelinek {
61300209230bSgjelinek 	uint64_t tmp_val;
61310209230bSgjelinek 
61320209230bSgjelinek 	switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) {
61330209230bSgjelinek 	case Z_OK:
61340209230bSgjelinek 		/*FALLTHRU*/
61350209230bSgjelinek 	case Z_NO_ENTRY:
61360209230bSgjelinek 		return (B_TRUE);
61370209230bSgjelinek 	default:
61380209230bSgjelinek 		return (B_FALSE);
61390209230bSgjelinek 	}
61400209230bSgjelinek }
61410209230bSgjelinek 
61420209230bSgjelinek int
61430209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val)
61440209230bSgjelinek {
61450209230bSgjelinek 	int i;
61460209230bSgjelinek 	int err;
61470209230bSgjelinek 	struct zone_rctltab rctltab;
61480209230bSgjelinek 	struct zone_rctlvaltab *rctlvaltab;
61490209230bSgjelinek 	char buf[128];
61500209230bSgjelinek 
61510209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, name))
61520209230bSgjelinek 		return (Z_ALIAS_DISALLOW);
61530209230bSgjelinek 
61540209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
61550209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
61560209230bSgjelinek 			break;
61570209230bSgjelinek 
61580209230bSgjelinek 	if (aliases[i].shortname == NULL)
61590209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
61600209230bSgjelinek 
61610209230bSgjelinek 	/* remove any pre-existing definition for this rctl */
61620209230bSgjelinek 	(void) zonecfg_rm_aliased_rctl(handle, name);
61630209230bSgjelinek 
61640209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
61650209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
61660209230bSgjelinek 
61670209230bSgjelinek 	rctltab.zone_rctl_valptr = NULL;
61680209230bSgjelinek 
61690209230bSgjelinek 	if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL)
61700209230bSgjelinek 		return (Z_NOMEM);
61710209230bSgjelinek 
61720209230bSgjelinek 	(void) snprintf(buf, sizeof (buf), "%llu", (long long)val);
61730209230bSgjelinek 
61740209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv,
61750209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_priv));
61760209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_limit, buf,
61770209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_limit));
61780209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action,
61790209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_action));
61800209230bSgjelinek 
61810209230bSgjelinek 	rctlvaltab->zone_rctlval_next = NULL;
61820209230bSgjelinek 
61830209230bSgjelinek 	if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK)
61840209230bSgjelinek 		return (err);
61850209230bSgjelinek 
61860209230bSgjelinek 	return (zonecfg_add_rctl(handle, &rctltab));
61870209230bSgjelinek }
61880209230bSgjelinek 
61890209230bSgjelinek static int
61900209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle)
61910209230bSgjelinek {
61920209230bSgjelinek 	int err;
61930209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
61940209230bSgjelinek 
61950209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
61960209230bSgjelinek 		return (err);
61970209230bSgjelinek 
61980209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
61990209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
62000209230bSgjelinek 			xmlUnlinkNode(cur);
62010209230bSgjelinek 			xmlFreeNode(cur);
62020209230bSgjelinek 			return (Z_OK);
62030209230bSgjelinek 		}
62040209230bSgjelinek 	}
62050209230bSgjelinek 
62060209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
62070209230bSgjelinek }
62080209230bSgjelinek 
62090209230bSgjelinek static int
62100209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance)
62110209230bSgjelinek {
62120209230bSgjelinek 	int err;
62130209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
62140209230bSgjelinek 	xmlNodePtr newnode;
62150209230bSgjelinek 
62160209230bSgjelinek 	err = delete_tmp_pool(handle);
62170209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
62180209230bSgjelinek 		return (err);
62190209230bSgjelinek 
62200209230bSgjelinek 	if (*pool_importance != '\0') {
62210209230bSgjelinek 		if ((err = operation_prep(handle)) != Z_OK)
62220209230bSgjelinek 			return (err);
62230209230bSgjelinek 
62240209230bSgjelinek 		newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL);
62250209230bSgjelinek 		if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE,
62260209230bSgjelinek 		    pool_importance)) != Z_OK)
62270209230bSgjelinek 			return (err);
62280209230bSgjelinek 	}
62290209230bSgjelinek 
62300209230bSgjelinek 	return (Z_OK);
62310209230bSgjelinek }
62320209230bSgjelinek 
62330209230bSgjelinek static int
62340209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr)
62350209230bSgjelinek {
62360209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
62370209230bSgjelinek 	int err;
62380209230bSgjelinek 
62390209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL);
62400209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN,
62410209230bSgjelinek 	    tabptr->zone_ncpu_min)) != Z_OK)
62420209230bSgjelinek 		return (err);
62430209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX,
62440209230bSgjelinek 	    tabptr->zone_ncpu_max)) != Z_OK)
62450209230bSgjelinek 		return (err);
62460209230bSgjelinek 
62470209230bSgjelinek 	if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK)
62480209230bSgjelinek 		return (err);
62490209230bSgjelinek 
62500209230bSgjelinek 	return (Z_OK);
62510209230bSgjelinek }
62520209230bSgjelinek 
62530209230bSgjelinek int
62540209230bSgjelinek zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
62550209230bSgjelinek {
62560209230bSgjelinek 	int err;
62570209230bSgjelinek 
62580209230bSgjelinek 	if (tabptr == NULL)
62590209230bSgjelinek 		return (Z_INVAL);
62600209230bSgjelinek 
62610209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
62620209230bSgjelinek 		return (err);
62630209230bSgjelinek 
62640209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
62650209230bSgjelinek 		return (err);
62660209230bSgjelinek 
62670209230bSgjelinek 	return (Z_OK);
62680209230bSgjelinek }
62690209230bSgjelinek 
62700209230bSgjelinek int
62710209230bSgjelinek zonecfg_delete_pset(zone_dochandle_t handle)
62720209230bSgjelinek {
62730209230bSgjelinek 	int err;
62740209230bSgjelinek 	int res = Z_NO_RESOURCE_ID;
62750209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
62760209230bSgjelinek 
62770209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
62780209230bSgjelinek 		return (err);
62790209230bSgjelinek 
62800209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
62810209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
62820209230bSgjelinek 			xmlUnlinkNode(cur);
62830209230bSgjelinek 			xmlFreeNode(cur);
62840209230bSgjelinek 			res = Z_OK;
62850209230bSgjelinek 			break;
62860209230bSgjelinek 		}
62870209230bSgjelinek 	}
62880209230bSgjelinek 
62890209230bSgjelinek 	/*
62900209230bSgjelinek 	 * Once we have msets, we should check that a mset
62910209230bSgjelinek 	 * do not exist before we delete the tmp_pool data.
62920209230bSgjelinek 	 */
62930209230bSgjelinek 	err = delete_tmp_pool(handle);
62940209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
62950209230bSgjelinek 		return (err);
62960209230bSgjelinek 
62970209230bSgjelinek 	return (res);
62980209230bSgjelinek }
62990209230bSgjelinek 
63000209230bSgjelinek int
63010209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
63020209230bSgjelinek {
63030209230bSgjelinek 	int err;
63040209230bSgjelinek 
63050209230bSgjelinek 	if (tabptr == NULL)
63060209230bSgjelinek 		return (Z_INVAL);
63070209230bSgjelinek 
63080209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
63090209230bSgjelinek 		return (err);
63100209230bSgjelinek 
63110209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
63120209230bSgjelinek 		return (err);
63130209230bSgjelinek 
63140209230bSgjelinek 	return (Z_OK);
63150209230bSgjelinek }
63160209230bSgjelinek 
63170209230bSgjelinek int
63180209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
63190209230bSgjelinek {
63200209230bSgjelinek 	xmlNodePtr cur;
63210209230bSgjelinek 	int err;
63220209230bSgjelinek 	int res = Z_NO_ENTRY;
63230209230bSgjelinek 
63240209230bSgjelinek 	if (tabptr == NULL)
63250209230bSgjelinek 		return (Z_INVAL);
63260209230bSgjelinek 
63270209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
63280209230bSgjelinek 		return (err);
63290209230bSgjelinek 
63300209230bSgjelinek 	/* this is an optional component */
63310209230bSgjelinek 	tabptr->zone_importance[0] = '\0';
63320209230bSgjelinek 
63330209230bSgjelinek 	cur = handle->zone_dh_cur;
63340209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
63350209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
63360209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN,
63370209230bSgjelinek 			    tabptr->zone_ncpu_min,
63380209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_min))) != Z_OK) {
63390209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
63400209230bSgjelinek 				return (err);
63410209230bSgjelinek 			}
63420209230bSgjelinek 
63430209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX,
63440209230bSgjelinek 			    tabptr->zone_ncpu_max,
63450209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_max))) != Z_OK) {
63460209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
63470209230bSgjelinek 				return (err);
63480209230bSgjelinek 			}
63490209230bSgjelinek 
63500209230bSgjelinek 			res = Z_OK;
63510209230bSgjelinek 
63520209230bSgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
63530209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE,
63540209230bSgjelinek 			    tabptr->zone_importance,
63550209230bSgjelinek 			    sizeof (tabptr->zone_importance))) != Z_OK) {
63560209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
63570209230bSgjelinek 				return (err);
63580209230bSgjelinek 			}
63590209230bSgjelinek 		}
63600209230bSgjelinek 	}
63610209230bSgjelinek 
63620209230bSgjelinek 	return (res);
63630209230bSgjelinek }
63640209230bSgjelinek 
63650209230bSgjelinek int
63660209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr)
63670209230bSgjelinek {
63680209230bSgjelinek 	int err;
63690209230bSgjelinek 
63700209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
63710209230bSgjelinek 		return (err);
63720209230bSgjelinek 
63730209230bSgjelinek 	err = zonecfg_lookup_pset(handle, tabptr);
63740209230bSgjelinek 
63750209230bSgjelinek 	(void) zonecfg_endent(handle);
63760209230bSgjelinek 
63770209230bSgjelinek 	return (err);
63780209230bSgjelinek }
63790209230bSgjelinek 
63800209230bSgjelinek static int
63810209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
63820209230bSgjelinek {
63830209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
63840209230bSgjelinek 	int err;
63850209230bSgjelinek 
63860209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL);
63870209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap))
63880209230bSgjelinek 	    != Z_OK)
63890209230bSgjelinek 		return (err);
63900209230bSgjelinek 
63910209230bSgjelinek 	return (Z_OK);
63920209230bSgjelinek }
63930209230bSgjelinek 
63940209230bSgjelinek int
63950209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle)
63960209230bSgjelinek {
63970209230bSgjelinek 	int err;
63980209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
63990209230bSgjelinek 
64000209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64010209230bSgjelinek 		return (err);
64020209230bSgjelinek 
64030209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64040209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
64050209230bSgjelinek 			continue;
64060209230bSgjelinek 
64070209230bSgjelinek 		xmlUnlinkNode(cur);
64080209230bSgjelinek 		xmlFreeNode(cur);
64090209230bSgjelinek 		return (Z_OK);
64100209230bSgjelinek 	}
64110209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
64120209230bSgjelinek }
64130209230bSgjelinek 
64140209230bSgjelinek int
64150209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
64160209230bSgjelinek {
64170209230bSgjelinek 	int err;
64180209230bSgjelinek 
64190209230bSgjelinek 	if (tabptr == NULL)
64200209230bSgjelinek 		return (Z_INVAL);
64210209230bSgjelinek 
64220209230bSgjelinek 	err = zonecfg_delete_mcap(handle);
64230209230bSgjelinek 	/* it is ok if there is no mcap entry */
64240209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
64250209230bSgjelinek 		return (err);
64260209230bSgjelinek 
64270209230bSgjelinek 	if ((err = add_mcap(handle, tabptr)) != Z_OK)
64280209230bSgjelinek 		return (err);
64290209230bSgjelinek 
64300209230bSgjelinek 	return (Z_OK);
64310209230bSgjelinek }
64320209230bSgjelinek 
64330209230bSgjelinek int
64340209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
64350209230bSgjelinek {
64360209230bSgjelinek 	xmlNodePtr cur;
64370209230bSgjelinek 	int err;
64380209230bSgjelinek 
64390209230bSgjelinek 	if (tabptr == NULL)
64400209230bSgjelinek 		return (Z_INVAL);
64410209230bSgjelinek 
64420209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64430209230bSgjelinek 		return (err);
64440209230bSgjelinek 
64450209230bSgjelinek 	cur = handle->zone_dh_cur;
64460209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64470209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
64480209230bSgjelinek 			continue;
64490209230bSgjelinek 		if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP,
64500209230bSgjelinek 		    tabptr->zone_physmem_cap,
64510209230bSgjelinek 		    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
64520209230bSgjelinek 			handle->zone_dh_cur = handle->zone_dh_top;
64530209230bSgjelinek 			return (err);
64540209230bSgjelinek 		}
64550209230bSgjelinek 
64560209230bSgjelinek 		return (Z_OK);
64570209230bSgjelinek 	}
64580209230bSgjelinek 
64590209230bSgjelinek 	return (Z_NO_ENTRY);
64600209230bSgjelinek }
64610209230bSgjelinek 
64620209230bSgjelinek static int
64630209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
64640209230bSgjelinek {
64650209230bSgjelinek 	xmlNodePtr cur;
64660209230bSgjelinek 	int err;
64670209230bSgjelinek 
64680209230bSgjelinek 	if (handle == NULL)
64690209230bSgjelinek 		return (Z_INVAL);
64700209230bSgjelinek 
64710209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
64720209230bSgjelinek 		return (Z_NO_ENTRY);
64730209230bSgjelinek 
64740209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
64750209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0)
64760209230bSgjelinek 			break;
64770209230bSgjelinek 	if (cur == NULL) {
64780209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
64790209230bSgjelinek 		return (Z_NO_ENTRY);
64800209230bSgjelinek 	}
64810209230bSgjelinek 
64820209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap,
64830209230bSgjelinek 	    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
64840209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
64850209230bSgjelinek 		return (err);
64860209230bSgjelinek 	}
64870209230bSgjelinek 
64880209230bSgjelinek 	handle->zone_dh_cur = cur->next;
64890209230bSgjelinek 	return (Z_OK);
64900209230bSgjelinek }
64910209230bSgjelinek 
64920209230bSgjelinek int
64930209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
64940209230bSgjelinek {
64950209230bSgjelinek 	int err;
64960209230bSgjelinek 
64970209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
64980209230bSgjelinek 		return (err);
64990209230bSgjelinek 
65000209230bSgjelinek 	err = getmcapent_core(handle, tabptr);
65010209230bSgjelinek 
65020209230bSgjelinek 	(void) zonecfg_endent(handle);
65030209230bSgjelinek 
65040209230bSgjelinek 	return (err);
65050209230bSgjelinek }
65060209230bSgjelinek 
6507ee519a1fSgjelinek int
6508ee519a1fSgjelinek zonecfg_setpkgent(zone_dochandle_t handle)
6509ee519a1fSgjelinek {
6510ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6511ee519a1fSgjelinek }
6512ee519a1fSgjelinek 
6513ee519a1fSgjelinek int
6514ee519a1fSgjelinek zonecfg_getpkgent(zone_dochandle_t handle, struct zone_pkgtab *tabptr)
6515ee519a1fSgjelinek {
6516ee519a1fSgjelinek 	xmlNodePtr cur;
6517ee519a1fSgjelinek 	int err;
6518ee519a1fSgjelinek 
6519ee519a1fSgjelinek 	if (handle == NULL)
6520ee519a1fSgjelinek 		return (Z_INVAL);
6521ee519a1fSgjelinek 
6522ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6523ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6524ee519a1fSgjelinek 
6525ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6526ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_PACKAGE))
6527ee519a1fSgjelinek 			break;
6528ee519a1fSgjelinek 	if (cur == NULL) {
6529ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6530ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6531ee519a1fSgjelinek 	}
6532ee519a1fSgjelinek 
6533ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_pkg_name,
6534ee519a1fSgjelinek 	    sizeof (tabptr->zone_pkg_name))) != Z_OK) {
6535ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6536ee519a1fSgjelinek 		return (err);
6537ee519a1fSgjelinek 	}
6538ee519a1fSgjelinek 
6539ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_VERSION, tabptr->zone_pkg_version,
6540ee519a1fSgjelinek 	    sizeof (tabptr->zone_pkg_version))) != Z_OK) {
6541ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6542ee519a1fSgjelinek 		return (err);
6543ee519a1fSgjelinek 	}
6544ee519a1fSgjelinek 
6545ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6546ee519a1fSgjelinek 	return (Z_OK);
6547ee519a1fSgjelinek }
6548ee519a1fSgjelinek 
6549ee519a1fSgjelinek int
6550ee519a1fSgjelinek zonecfg_endpkgent(zone_dochandle_t handle)
6551ee519a1fSgjelinek {
6552ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6553ee519a1fSgjelinek }
6554ee519a1fSgjelinek 
6555ee519a1fSgjelinek int
6556ee519a1fSgjelinek zonecfg_setpatchent(zone_dochandle_t handle)
6557ee519a1fSgjelinek {
6558ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6559ee519a1fSgjelinek }
6560ee519a1fSgjelinek 
6561ee519a1fSgjelinek int
6562ee519a1fSgjelinek zonecfg_getpatchent(zone_dochandle_t handle, struct zone_patchtab *tabptr)
6563ee519a1fSgjelinek {
6564ee519a1fSgjelinek 	xmlNodePtr cur;
6565ee519a1fSgjelinek 	int err;
6566ee519a1fSgjelinek 
6567ee519a1fSgjelinek 	if (handle == NULL)
6568ee519a1fSgjelinek 		return (Z_INVAL);
6569ee519a1fSgjelinek 
6570ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6571ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6572ee519a1fSgjelinek 
6573ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6574ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_PATCH))
6575ee519a1fSgjelinek 			break;
6576ee519a1fSgjelinek 	if (cur == NULL) {
6577ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6578ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6579ee519a1fSgjelinek 	}
6580ee519a1fSgjelinek 
6581ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_ID, tabptr->zone_patch_id,
6582ee519a1fSgjelinek 	    sizeof (tabptr->zone_patch_id))) != Z_OK) {
6583ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6584ee519a1fSgjelinek 		return (err);
6585ee519a1fSgjelinek 	}
6586ee519a1fSgjelinek 
6587ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6588ee519a1fSgjelinek 	return (Z_OK);
6589ee519a1fSgjelinek }
6590ee519a1fSgjelinek 
6591ee519a1fSgjelinek int
6592ee519a1fSgjelinek zonecfg_endpatchent(zone_dochandle_t handle)
6593ee519a1fSgjelinek {
6594ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6595ee519a1fSgjelinek }
6596ee519a1fSgjelinek 
6597ee519a1fSgjelinek int
6598ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
6599ee519a1fSgjelinek {
6600ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6601ee519a1fSgjelinek }
6602ee519a1fSgjelinek 
6603ee519a1fSgjelinek int
6604ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
6605ee519a1fSgjelinek {
6606ee519a1fSgjelinek 	xmlNodePtr cur;
6607ee519a1fSgjelinek 	int err;
6608ee519a1fSgjelinek 	char buf[128];
6609ee519a1fSgjelinek 
6610ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
6611ee519a1fSgjelinek 
6612ee519a1fSgjelinek 	if (handle == NULL)
6613ee519a1fSgjelinek 		return (Z_INVAL);
6614ee519a1fSgjelinek 
6615ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6616ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6617ee519a1fSgjelinek 
6618ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6619ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
6620ee519a1fSgjelinek 			break;
6621ee519a1fSgjelinek 	if (cur == NULL) {
6622ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6623ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6624ee519a1fSgjelinek 	}
6625ee519a1fSgjelinek 
6626ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
6627ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
6628ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6629ee519a1fSgjelinek 		return (err);
6630ee519a1fSgjelinek 	}
6631ee519a1fSgjelinek 
6632ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
6633ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6634ee519a1fSgjelinek 		return (err);
6635ee519a1fSgjelinek 	}
6636ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
6637ee519a1fSgjelinek 
6638ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
6639ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6640ee519a1fSgjelinek 		return (err);
6641ee519a1fSgjelinek 	}
6642ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
6643ee519a1fSgjelinek 
6644ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
6645ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6646ee519a1fSgjelinek 		return (err);
6647ee519a1fSgjelinek 	}
6648ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
6649ee519a1fSgjelinek 
6650ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
6651ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
6652ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6653ee519a1fSgjelinek 		return (err);
6654ee519a1fSgjelinek 	}
6655ee519a1fSgjelinek 
6656ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6657ee519a1fSgjelinek 	return (Z_OK);
6658ee519a1fSgjelinek }
6659ee519a1fSgjelinek 
6660ee519a1fSgjelinek int
6661ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
6662ee519a1fSgjelinek {
6663ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6664ee519a1fSgjelinek }
6665ee519a1fSgjelinek 
6666ee519a1fSgjelinek /*
6667*39935be5Sgjelinek  * Maintain a space separated list of unique pkg names.  PATH_MAX is used in
6668*39935be5Sgjelinek  * the pkg code as the maximum size for a pkg name.
6669*39935be5Sgjelinek  */
6670*39935be5Sgjelinek static int
6671*39935be5Sgjelinek add_pkg_to_str(char **str, char *pkg)
6672*39935be5Sgjelinek {
6673*39935be5Sgjelinek 	int len, newlen;
6674*39935be5Sgjelinek 	char tstr[PATH_MAX + 3];
6675*39935be5Sgjelinek 	char *tmp;
6676*39935be5Sgjelinek 
6677*39935be5Sgjelinek 	len = strlen(pkg);
6678*39935be5Sgjelinek 	if (*str == NULL) {
6679*39935be5Sgjelinek 		/* space for str + 2 spaces + NULL */
6680*39935be5Sgjelinek 		if ((*str = (char *)malloc(len + 3)) == NULL)
6681*39935be5Sgjelinek 			return (Z_NOMEM);
6682*39935be5Sgjelinek 		(void) snprintf(*str, len + 3, " %s ", pkg);
6683*39935be5Sgjelinek 		return (Z_OK);
6684*39935be5Sgjelinek 	}
6685*39935be5Sgjelinek 
6686*39935be5Sgjelinek 	(void) snprintf(tstr, sizeof (tstr), " %s ", pkg);
6687*39935be5Sgjelinek 	if (strstr(*str, tstr) != NULL)
6688*39935be5Sgjelinek 		return (Z_OK);
6689*39935be5Sgjelinek 
6690*39935be5Sgjelinek 	/* space for str + 1 space + NULL */
6691*39935be5Sgjelinek 	newlen = strlen(*str) + len + 2;
6692*39935be5Sgjelinek 	if ((tmp = (char *)realloc(*str, newlen)) == NULL)
6693*39935be5Sgjelinek 		return (Z_NOMEM);
6694*39935be5Sgjelinek 	*str = tmp;
6695*39935be5Sgjelinek 	(void) strlcat(*str, pkg, newlen);
6696*39935be5Sgjelinek 	(void) strlcat(*str, " ", newlen);
6697*39935be5Sgjelinek 	return (Z_OK);
6698*39935be5Sgjelinek }
6699*39935be5Sgjelinek 
6700*39935be5Sgjelinek /*
6701ee519a1fSgjelinek  * Process a list of pkgs from an entry in the contents file, adding each pkg
6702ee519a1fSgjelinek  * name to the list of pkgs.
6703ee519a1fSgjelinek  *
6704ee519a1fSgjelinek  * It is possible for the pkg name to be preceeded by a special character
6705ee519a1fSgjelinek  * which indicates some bookkeeping information for pkging.  Check if the
6706ee519a1fSgjelinek  * first char is not an Alpha char.  If so, skip over it.
6707ee519a1fSgjelinek  */
6708ee519a1fSgjelinek static int
6709*39935be5Sgjelinek add_pkg_list(char *lastp, char ***plist, int *pcnt, char **pkg_warn)
6710ee519a1fSgjelinek {
6711ee519a1fSgjelinek 	char	*p;
6712ee519a1fSgjelinek 	int	pkg_cnt = *pcnt;
6713ee519a1fSgjelinek 	char	**pkgs = *plist;
6714ee519a1fSgjelinek 	int	res = Z_OK;
6715ee519a1fSgjelinek 
6716ee519a1fSgjelinek 	while ((p = strtok_r(NULL, " ", &lastp)) != NULL) {
6717ee519a1fSgjelinek 		char	**tmpp;
6718ee519a1fSgjelinek 		int	i;
6719ee519a1fSgjelinek 
6720ee519a1fSgjelinek 		/* skip over any special pkg bookkeeping char */
6721*39935be5Sgjelinek 		if (!isalpha(*p)) {
6722ee519a1fSgjelinek 			p++;
6723*39935be5Sgjelinek 			if ((res = add_pkg_to_str(pkg_warn, p)) != Z_OK)
6724*39935be5Sgjelinek 				break;
6725*39935be5Sgjelinek 		}
6726ee519a1fSgjelinek 
6727ee519a1fSgjelinek 		/* Check if the pkg is already in the list */
6728ee519a1fSgjelinek 		for (i = 0; i < pkg_cnt; i++) {
6729ee519a1fSgjelinek 			if (strcmp(p, pkgs[i]) == 0)
6730ee519a1fSgjelinek 				break;
6731ee519a1fSgjelinek 		}
6732ee519a1fSgjelinek 
6733ee519a1fSgjelinek 		if (i < pkg_cnt)
6734ee519a1fSgjelinek 			continue;
6735ee519a1fSgjelinek 
6736ee519a1fSgjelinek 		/* The pkg is not in the list; add it. */
6737ee519a1fSgjelinek 		if ((tmpp = (char **)realloc(pkgs,
6738ee519a1fSgjelinek 		    sizeof (char *) * (pkg_cnt + 1))) == NULL) {
6739ee519a1fSgjelinek 			res = Z_NOMEM;
6740ee519a1fSgjelinek 			break;
6741ee519a1fSgjelinek 		}
6742ee519a1fSgjelinek 		pkgs = tmpp;
6743ee519a1fSgjelinek 
6744ee519a1fSgjelinek 		if ((pkgs[pkg_cnt] = strdup(p)) == NULL) {
6745ee519a1fSgjelinek 			res = Z_NOMEM;
6746ee519a1fSgjelinek 			break;
6747ee519a1fSgjelinek 		}
6748ee519a1fSgjelinek 		pkg_cnt++;
6749ee519a1fSgjelinek 	}
6750ee519a1fSgjelinek 
6751ee519a1fSgjelinek 	*plist = pkgs;
6752ee519a1fSgjelinek 	*pcnt = pkg_cnt;
6753ee519a1fSgjelinek 
6754ee519a1fSgjelinek 	return (res);
6755ee519a1fSgjelinek }
6756ee519a1fSgjelinek 
6757ee519a1fSgjelinek /*
6758*39935be5Sgjelinek  * Process an entry from the contents file (type "directory").  If the
6759*39935be5Sgjelinek  * directory path is in the list of ipds and is not under a lofs mount within
6760*39935be5Sgjelinek  * the ipd then add the associated list of pkgs to the pkg list.  The input
6761*39935be5Sgjelinek  * parameter "entry" will be broken up by the parser within this function so
6762*39935be5Sgjelinek  * its value will be modified when this function exits.
6763ee519a1fSgjelinek  *
6764ee519a1fSgjelinek  * The entries we are looking for will look something like:
6765ee519a1fSgjelinek  *	/usr d none 0755 root sys SUNWctpls SUNWidnl SUNWlibCf ....
6766ee519a1fSgjelinek  */
6767ee519a1fSgjelinek static int
6768*39935be5Sgjelinek get_path_pkgs(char *entry, char **ipds, char **fss, char ***pkgs, int *pkg_cnt,
6769*39935be5Sgjelinek     char **pkg_warn)
6770ee519a1fSgjelinek {
6771ee519a1fSgjelinek 	char	*f1;
6772ee519a1fSgjelinek 	char	*f2;
6773ee519a1fSgjelinek 	char	*lastp;
6774ee519a1fSgjelinek 	int	i;
6775*39935be5Sgjelinek 	char	*nlp;
6776ee519a1fSgjelinek 
6777ee519a1fSgjelinek 	if ((f1 = strtok_r(entry, " ", &lastp)) == NULL ||
6778ee519a1fSgjelinek 	    (f2 = strtok_r(NULL, " ", &lastp)) == NULL || strcmp(f2, "d") != 0)
6779ee519a1fSgjelinek 		return (Z_OK);
6780ee519a1fSgjelinek 
6781*39935be5Sgjelinek 	/* Check if this directory entry is in the list of ipds. */
6782*39935be5Sgjelinek 	for (i = 0; ipds[i] != NULL; i++) {
6783*39935be5Sgjelinek 		char wildcard[MAXPATHLEN];
6784ee519a1fSgjelinek 
6785ee519a1fSgjelinek 		/*
6786*39935be5Sgjelinek 		 * We want to match on the path and any other directory
6787*39935be5Sgjelinek 		 * entries under this path.  When we use FNM_PATHNAME then
6788*39935be5Sgjelinek 		 * that means '/' will not be matched by a wildcard (*) so
6789*39935be5Sgjelinek 		 * we omit FNM_PATHNAME on the call with the wildcard matching.
6790ee519a1fSgjelinek 		 */
6791*39935be5Sgjelinek 		(void) snprintf(wildcard, sizeof (wildcard), "%s/*", ipds[i]);
6792*39935be5Sgjelinek 		if (fnmatch(ipds[i], f1, FNM_PATHNAME) == 0 ||
6793*39935be5Sgjelinek 		    fnmatch(wildcard, f1, 0) == 0) {
6794*39935be5Sgjelinek 			/* It looks like we do want the pkgs for this path. */
6795*39935be5Sgjelinek 			break;
6796*39935be5Sgjelinek 		}
6797*39935be5Sgjelinek 	}
6798*39935be5Sgjelinek 
6799*39935be5Sgjelinek 	/* This entry did not match any of the ipds. */
6800*39935be5Sgjelinek 	if (ipds[i] == NULL)
6801*39935be5Sgjelinek 		return (Z_OK);
6802*39935be5Sgjelinek 
6803*39935be5Sgjelinek 	/*
6804*39935be5Sgjelinek 	 * Check if there is a fs mounted under the ipd.  If so, ignore this
6805*39935be5Sgjelinek 	 * entry.
6806*39935be5Sgjelinek 	 */
6807*39935be5Sgjelinek 	for (i = 0; fss[i] != NULL; i++) {
6808*39935be5Sgjelinek 		char wildcard[MAXPATHLEN];
6809*39935be5Sgjelinek 
6810*39935be5Sgjelinek 		(void) snprintf(wildcard, sizeof (wildcard), "%s/*", fss[i]);
6811*39935be5Sgjelinek 		if (fnmatch(fss[i], f1, FNM_PATHNAME) == 0 ||
6812*39935be5Sgjelinek 		    fnmatch(wildcard, f1, 0) == 0) {
6813*39935be5Sgjelinek 			/* We should ignore this path. */
6814*39935be5Sgjelinek 			break;
6815*39935be5Sgjelinek 		}
6816*39935be5Sgjelinek 	}
6817*39935be5Sgjelinek 
6818*39935be5Sgjelinek 	/* If not null, then we matched an fs mount point so ignore entry. */
6819*39935be5Sgjelinek 	if (fss[i] != NULL)
6820*39935be5Sgjelinek 		return (Z_OK);
6821*39935be5Sgjelinek 
6822*39935be5Sgjelinek 	/*
6823*39935be5Sgjelinek 	 * We do want the pkgs for this entry.  First, skip over the next 4
6824*39935be5Sgjelinek 	 * fields in the entry so that we call add_pkg_list starting with the
6825*39935be5Sgjelinek 	 * pkg names.
6826*39935be5Sgjelinek 	 */
6827*39935be5Sgjelinek 	for (i = 0; i < 4 && strtok_r(NULL, " ", &lastp) != NULL; i++)
6828*39935be5Sgjelinek 		;
6829*39935be5Sgjelinek 	/* If there are < 4 fields this entry is corrupt, just skip it. */
6830*39935be5Sgjelinek 	if (i < 4)
6831ee519a1fSgjelinek 		return (Z_OK);
6832ee519a1fSgjelinek 
683307b574eeSgjelinek 	/* strip newline from the line */
683407b574eeSgjelinek 	nlp = (lastp + strlen(lastp) - 1);
683507b574eeSgjelinek 	if (*nlp == '\n')
683607b574eeSgjelinek 		*nlp = '\0';
683707b574eeSgjelinek 
6838*39935be5Sgjelinek 	return (add_pkg_list(lastp, pkgs, pkg_cnt, pkg_warn));
6839ee519a1fSgjelinek }
6840ee519a1fSgjelinek 
6841ee519a1fSgjelinek /*
6842ee519a1fSgjelinek  * Read an entry from a pkginfo or contents file.  Some of these lines can
6843ee519a1fSgjelinek  * either be arbitrarily long or be continued by a backslash at the end of
6844ee519a1fSgjelinek  * the line.  This function coalesces lines that are longer than the read
6845ee519a1fSgjelinek  * buffer, and lines that are continued, into one buffer which is returned.
6846ee519a1fSgjelinek  * The caller must free this memory.  NULL is returned when we hit EOF or
6847ee519a1fSgjelinek  * if we run out of memory (errno is set to ENOMEM).
6848ee519a1fSgjelinek  */
6849ee519a1fSgjelinek static char *
6850ee519a1fSgjelinek read_pkg_data(FILE *fp)
6851ee519a1fSgjelinek {
6852ee519a1fSgjelinek 	char *start;
6853ee519a1fSgjelinek 	char *inp;
6854ee519a1fSgjelinek 	char *p;
6855ee519a1fSgjelinek 	int char_cnt = 0;
6856ee519a1fSgjelinek 
6857ee519a1fSgjelinek 	errno = 0;
6858ee519a1fSgjelinek 	if ((start = (char *)malloc(PKGINFO_RD_LEN)) == NULL) {
6859ee519a1fSgjelinek 		errno = ENOMEM;
6860ee519a1fSgjelinek 		return (NULL);
6861ee519a1fSgjelinek 	}
6862ee519a1fSgjelinek 
6863ee519a1fSgjelinek 	inp = start;
6864ee519a1fSgjelinek 	while ((p = fgets(inp, PKGINFO_RD_LEN, fp)) != NULL) {
6865ee519a1fSgjelinek 		int len;
6866ee519a1fSgjelinek 
6867ee519a1fSgjelinek 		len = strlen(inp);
6868ee519a1fSgjelinek 		if (inp[len - 1] == '\n' &&
6869ee519a1fSgjelinek 		    (len == 1 || inp[len - 2] != '\\')) {
6870ee519a1fSgjelinek 			char_cnt = len;
6871ee519a1fSgjelinek 			break;
6872ee519a1fSgjelinek 		}
6873ee519a1fSgjelinek 
6874ee519a1fSgjelinek 		if (inp[len - 2] == '\\')
6875ee519a1fSgjelinek 			char_cnt += len - 2;
6876ee519a1fSgjelinek 		else
6877ee519a1fSgjelinek 			char_cnt += PKGINFO_RD_LEN - 1;
6878ee519a1fSgjelinek 
6879ee519a1fSgjelinek 		if ((p = realloc(start, char_cnt + PKGINFO_RD_LEN)) == NULL) {
6880ee519a1fSgjelinek 			errno = ENOMEM;
6881ee519a1fSgjelinek 			break;
6882ee519a1fSgjelinek 		}
6883ee519a1fSgjelinek 
6884ee519a1fSgjelinek 		start = p;
6885ee519a1fSgjelinek 		inp = start + char_cnt;
6886ee519a1fSgjelinek 	}
6887ee519a1fSgjelinek 
6888ee519a1fSgjelinek 	if (errno == ENOMEM || (p == NULL && char_cnt == 0)) {
6889ee519a1fSgjelinek 		free(start);
6890ee519a1fSgjelinek 		start = NULL;
6891ee519a1fSgjelinek 	}
6892ee519a1fSgjelinek 
6893ee519a1fSgjelinek 	return (start);
6894ee519a1fSgjelinek }
6895ee519a1fSgjelinek 
6896ee519a1fSgjelinek static void
6897ee519a1fSgjelinek free_ipd_pkgs(char **pkgs, int cnt)
6898ee519a1fSgjelinek {
6899ee519a1fSgjelinek 	int i;
6900ee519a1fSgjelinek 
6901ee519a1fSgjelinek 	for (i = 0; i < cnt; i++)
6902ee519a1fSgjelinek 		free(pkgs[i]);
6903ee519a1fSgjelinek 	free(pkgs);
6904ee519a1fSgjelinek }
6905ee519a1fSgjelinek 
6906ee519a1fSgjelinek /*
6907*39935be5Sgjelinek  * Get a list of the inherited pkg dirs or fs entries configured for the
6908*39935be5Sgjelinek  * zone.  The type parameter will be either ZONE_IPD or ZONE_FS.
6909*39935be5Sgjelinek  */
6910*39935be5Sgjelinek static int
6911*39935be5Sgjelinek get_ipd_fs_list(zone_dochandle_t handle, enum zn_ipd_fs type, char ***list)
6912*39935be5Sgjelinek {
6913*39935be5Sgjelinek 	int	res;
6914*39935be5Sgjelinek 	struct zone_fstab fstab;
6915*39935be5Sgjelinek 	int	cnt = 0;
6916*39935be5Sgjelinek 	char	**entries = NULL;
6917*39935be5Sgjelinek 	int	i;
6918*39935be5Sgjelinek 	int	(*fp)(zone_dochandle_t, struct zone_fstab *);
6919*39935be5Sgjelinek 
6920*39935be5Sgjelinek 	if (type == ZONE_IPD) {
6921*39935be5Sgjelinek 		fp = zonecfg_getipdent;
6922*39935be5Sgjelinek 		res = zonecfg_setipdent(handle);
6923*39935be5Sgjelinek 	} else {
6924*39935be5Sgjelinek 		fp = zonecfg_getfsent;
6925*39935be5Sgjelinek 		res = zonecfg_setfsent(handle);
6926*39935be5Sgjelinek 	}
6927*39935be5Sgjelinek 
6928*39935be5Sgjelinek 	if (res != Z_OK)
6929*39935be5Sgjelinek 		return (res);
6930*39935be5Sgjelinek 
6931*39935be5Sgjelinek 	while (fp(handle, &fstab) == Z_OK) {
6932*39935be5Sgjelinek 		char	**p;
6933*39935be5Sgjelinek 
6934*39935be5Sgjelinek 		if ((p = (char **)realloc(entries,
6935*39935be5Sgjelinek 		    sizeof (char *) * (cnt + 1))) == NULL) {
6936*39935be5Sgjelinek 			res = Z_NOMEM;
6937*39935be5Sgjelinek 			break;
6938*39935be5Sgjelinek 		}
6939*39935be5Sgjelinek 		entries = p;
6940*39935be5Sgjelinek 
6941*39935be5Sgjelinek 		if ((entries[cnt] = strdup(fstab.zone_fs_dir)) == NULL) {
6942*39935be5Sgjelinek 			res = Z_NOMEM;
6943*39935be5Sgjelinek 			break;
6944*39935be5Sgjelinek 		}
6945*39935be5Sgjelinek 
6946*39935be5Sgjelinek 		cnt++;
6947*39935be5Sgjelinek 	}
6948*39935be5Sgjelinek 
6949*39935be5Sgjelinek 	if (type == ZONE_IPD)
6950*39935be5Sgjelinek 		(void) zonecfg_endipdent(handle);
6951*39935be5Sgjelinek 	else
6952*39935be5Sgjelinek 		(void) zonecfg_endfsent(handle);
6953*39935be5Sgjelinek 
6954*39935be5Sgjelinek 	/* Add a NULL terminating element. */
6955*39935be5Sgjelinek 	if (res == Z_OK) {
6956*39935be5Sgjelinek 		char	**p;
6957*39935be5Sgjelinek 
6958*39935be5Sgjelinek 		if ((p = (char **)realloc(entries,
6959*39935be5Sgjelinek 		    sizeof (char *) * (cnt + 1))) == NULL) {
6960*39935be5Sgjelinek 			res = Z_NOMEM;
6961*39935be5Sgjelinek 		} else {
6962*39935be5Sgjelinek 			entries = p;
6963*39935be5Sgjelinek 			entries[cnt] = NULL;
6964*39935be5Sgjelinek 		}
6965*39935be5Sgjelinek 	}
6966*39935be5Sgjelinek 
6967*39935be5Sgjelinek 	if (res != Z_OK) {
6968*39935be5Sgjelinek 		if (entries != NULL) {
6969*39935be5Sgjelinek 			for (i = 0; i < cnt; i++)
6970*39935be5Sgjelinek 				free(entries[i]);
6971*39935be5Sgjelinek 			free(entries);
6972*39935be5Sgjelinek 		}
6973*39935be5Sgjelinek 		return (res);
6974*39935be5Sgjelinek 	}
6975*39935be5Sgjelinek 
6976*39935be5Sgjelinek 	*list = entries;
6977*39935be5Sgjelinek 	return (Z_OK);
6978*39935be5Sgjelinek }
6979*39935be5Sgjelinek 
6980*39935be5Sgjelinek /*
6981ee519a1fSgjelinek  * Get the list of inherited-pkg-dirs (ipd) for the zone and then get the
6982ee519a1fSgjelinek  * list of pkgs that deliver into those dirs.
6983ee519a1fSgjelinek  */
6984ee519a1fSgjelinek static int
6985ee519a1fSgjelinek get_ipd_pkgs(zone_dochandle_t handle, char ***pkg_list, int *cnt)
6986ee519a1fSgjelinek {
6987ee519a1fSgjelinek 	int	res;
6988*39935be5Sgjelinek 	char	**ipds;
6989*39935be5Sgjelinek 	char	**fss;
6990ee519a1fSgjelinek 	int	pkg_cnt = 0;
6991ee519a1fSgjelinek 	char	**pkgs = NULL;
6992ee519a1fSgjelinek 	int	i;
6993ee519a1fSgjelinek 
6994*39935be5Sgjelinek 	if ((res = get_ipd_fs_list(handle, ZONE_IPD, &ipds)) != Z_OK)
6995ee519a1fSgjelinek 		return (res);
6996ee519a1fSgjelinek 
6997*39935be5Sgjelinek 	if ((res = get_ipd_fs_list(handle, ZONE_FS, &fss)) != Z_OK) {
6998*39935be5Sgjelinek 		for (i = 0; ipds[i] != NULL; i++)
6999ee519a1fSgjelinek 			free(ipds[i]);
7000ee519a1fSgjelinek 		free(ipds);
7001ee519a1fSgjelinek 		return (res);
7002ee519a1fSgjelinek 	}
7003ee519a1fSgjelinek 
7004ee519a1fSgjelinek 	/* We only have to process the contents file if we have ipds. */
7005*39935be5Sgjelinek 	if (ipds != NULL) {
7006ee519a1fSgjelinek 		FILE	*fp;
7007ee519a1fSgjelinek 
7008ee519a1fSgjelinek 		if ((fp = fopen(CONTENTS_FILE, "r")) != NULL) {
7009ee519a1fSgjelinek 			char	*buf;
7010*39935be5Sgjelinek 			char	*pkg_warn = NULL;
7011ee519a1fSgjelinek 
7012ee519a1fSgjelinek 			while ((buf = read_pkg_data(fp)) != NULL) {
7013*39935be5Sgjelinek 				res = get_path_pkgs(buf, ipds, fss, &pkgs,
7014*39935be5Sgjelinek 				    &pkg_cnt, &pkg_warn);
7015ee519a1fSgjelinek 				free(buf);
7016ee519a1fSgjelinek 				if (res != Z_OK)
7017ee519a1fSgjelinek 					break;
7018ee519a1fSgjelinek 			}
7019ee519a1fSgjelinek 
7020ee519a1fSgjelinek 			(void) fclose(fp);
7021*39935be5Sgjelinek 
7022*39935be5Sgjelinek 			if (pkg_warn != NULL) {
7023*39935be5Sgjelinek 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
7024*39935be5Sgjelinek 				    "WARNING: package operation in progress "
7025*39935be5Sgjelinek 				    "on the following packages:\n   %s\n"),
7026*39935be5Sgjelinek 				    pkg_warn);
7027*39935be5Sgjelinek 				free(pkg_warn);
7028*39935be5Sgjelinek 			}
7029ee519a1fSgjelinek 		}
7030ee519a1fSgjelinek 	}
7031ee519a1fSgjelinek 
7032*39935be5Sgjelinek 	for (i = 0; ipds[i] != NULL; i++)
7033ee519a1fSgjelinek 		free(ipds[i]);
7034ee519a1fSgjelinek 	free(ipds);
7035ee519a1fSgjelinek 
7036*39935be5Sgjelinek 	for (i = 0; fss[i] != NULL; i++)
7037*39935be5Sgjelinek 		free(fss[i]);
7038*39935be5Sgjelinek 	free(fss);
7039*39935be5Sgjelinek 
7040ee519a1fSgjelinek 	if (res != Z_OK) {
7041ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
7042ee519a1fSgjelinek 	} else {
7043ee519a1fSgjelinek 		*pkg_list = pkgs;
7044ee519a1fSgjelinek 		*cnt = pkg_cnt;
7045ee519a1fSgjelinek 	}
7046ee519a1fSgjelinek 
7047ee519a1fSgjelinek 	return (res);
7048ee519a1fSgjelinek }
7049ee519a1fSgjelinek 
7050ee519a1fSgjelinek /*
7051ee519a1fSgjelinek  * Return true if pkg_name is in the list of pkgs that deliver into an
7052ee519a1fSgjelinek  * inherited pkg directory for the zone.
7053ee519a1fSgjelinek  */
7054ee519a1fSgjelinek static boolean_t
7055ee519a1fSgjelinek dir_pkg(char *pkg_name, char **pkg_list, int cnt)
7056ee519a1fSgjelinek {
7057ee519a1fSgjelinek 	int i;
7058ee519a1fSgjelinek 
7059ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
7060ee519a1fSgjelinek 		if (strcmp(pkg_name, pkg_list[i]) == 0)
7061ee519a1fSgjelinek 			return (B_TRUE);
7062ee519a1fSgjelinek 	}
7063ee519a1fSgjelinek 
7064ee519a1fSgjelinek 	return (B_FALSE);
7065ee519a1fSgjelinek }
7066ee519a1fSgjelinek 
7067ee519a1fSgjelinek /*
7068*39935be5Sgjelinek  * Keep track of obsoleted patches.  We don't need to keep track of the patch
7069*39935be5Sgjelinek  * version since once a patch is obsoleted, all prior versions are also
7070*39935be5Sgjelinek  * obsolete and there won't be any new versions.
7071ee519a1fSgjelinek  */
7072ee519a1fSgjelinek static int
7073*39935be5Sgjelinek save_obs_patch(char *num, uu_avl_pool_t *patches_pool,
7074*39935be5Sgjelinek     uu_avl_t *obs_patches_avl)
7075ee519a1fSgjelinek {
7076*39935be5Sgjelinek 	patch_node_t	*patch;
7077*39935be5Sgjelinek 	uu_avl_index_t where;
7078*39935be5Sgjelinek 
7079*39935be5Sgjelinek 	if ((patch = (patch_node_t *)malloc(sizeof (patch_node_t))) == NULL)
7080*39935be5Sgjelinek 		return (Z_NOMEM);
7081*39935be5Sgjelinek 
7082*39935be5Sgjelinek 	if ((patch->patch_num = strdup(num)) == NULL) {
7083*39935be5Sgjelinek 		free(patch);
7084*39935be5Sgjelinek 		return (Z_NOMEM);
7085*39935be5Sgjelinek 	}
7086*39935be5Sgjelinek 
7087*39935be5Sgjelinek 	patch->patch_vers = NULL;
7088*39935be5Sgjelinek 	uu_avl_node_init(patch, &patch->patch_node, patches_pool);
7089*39935be5Sgjelinek 
7090*39935be5Sgjelinek 	if (uu_avl_find(obs_patches_avl, patch, NULL, &where) != NULL) {
7091*39935be5Sgjelinek 		free(patch->patch_num);
7092*39935be5Sgjelinek 		free(patch);
7093*39935be5Sgjelinek 		return (Z_OK);
7094*39935be5Sgjelinek 	}
7095*39935be5Sgjelinek 
7096*39935be5Sgjelinek 	uu_avl_insert(obs_patches_avl, patch, where);
7097*39935be5Sgjelinek 	return (Z_OK);
7098*39935be5Sgjelinek }
7099*39935be5Sgjelinek 
7100*39935be5Sgjelinek /*
7101*39935be5Sgjelinek  * Keep a list of patches for a pkg.  If we see a newer version of a patch,
7102*39935be5Sgjelinek  * we only keep track of the newer version.
7103*39935be5Sgjelinek  */
7104*39935be5Sgjelinek static void
7105*39935be5Sgjelinek save_patch(patch_node_t *patch, uu_avl_t *patches_avl)
7106*39935be5Sgjelinek {
7107*39935be5Sgjelinek 	patch_node_t *existing;
7108*39935be5Sgjelinek 	uu_avl_index_t where;
7109*39935be5Sgjelinek 
7110*39935be5Sgjelinek 	/* Check if this is a newer version of a patch we already have. */
7111*39935be5Sgjelinek 	if ((existing = (patch_node_t *)uu_avl_find(patches_avl, patch, NULL,
7112*39935be5Sgjelinek 	    &where)) != NULL) {
7113*39935be5Sgjelinek 		char *endptr;
7114*39935be5Sgjelinek 		ulong_t pvers, evers;
7115*39935be5Sgjelinek 
7116*39935be5Sgjelinek 		pvers = strtoul(patch->patch_vers, &endptr, 10);
7117*39935be5Sgjelinek 		evers = strtoul(existing->patch_vers, &endptr, 10);
7118*39935be5Sgjelinek 
7119*39935be5Sgjelinek 		if (pvers > evers) {
7120*39935be5Sgjelinek 			free(existing->patch_vers);
7121*39935be5Sgjelinek 			existing->patch_vers = patch->patch_vers;
7122*39935be5Sgjelinek 			free(patch->patch_num);
7123*39935be5Sgjelinek 			free(patch);
7124*39935be5Sgjelinek 			return;
7125*39935be5Sgjelinek 		}
7126*39935be5Sgjelinek 	}
7127*39935be5Sgjelinek 
7128*39935be5Sgjelinek 	uu_avl_insert(patches_avl, patch, where);
7129*39935be5Sgjelinek }
7130*39935be5Sgjelinek 
7131*39935be5Sgjelinek /*
7132*39935be5Sgjelinek  * Check if a patch is on the list of obsoleted patches.  We don't need to
7133*39935be5Sgjelinek  * check the patch version since once a patch is obsoleted, all prior versions
7134*39935be5Sgjelinek  * are also obsolete and there won't be any new versions.
7135*39935be5Sgjelinek  */
7136*39935be5Sgjelinek static boolean_t
7137*39935be5Sgjelinek obsolete_patch(patch_node_t *patch, uu_avl_t *obs_patches_avl)
7138*39935be5Sgjelinek {
7139*39935be5Sgjelinek 	uu_avl_index_t	where;
7140*39935be5Sgjelinek 
7141*39935be5Sgjelinek 	if (uu_avl_find(obs_patches_avl, patch, NULL, &where) != NULL)
7142*39935be5Sgjelinek 		return (B_TRUE);
7143*39935be5Sgjelinek 
7144*39935be5Sgjelinek 	return (B_FALSE);
7145*39935be5Sgjelinek }
7146*39935be5Sgjelinek 
7147*39935be5Sgjelinek /* ARGSUSED */
7148*39935be5Sgjelinek static int
7149*39935be5Sgjelinek patch_node_compare(const void *l_arg, const void *r_arg, void *private)
7150*39935be5Sgjelinek {
7151*39935be5Sgjelinek 	patch_node_t *l = (patch_node_t *)l_arg;
7152*39935be5Sgjelinek 	patch_node_t *r = (patch_node_t *)r_arg;
7153*39935be5Sgjelinek 	char *endptr;
7154*39935be5Sgjelinek 	ulong_t lnum, rnum;
7155*39935be5Sgjelinek 
7156*39935be5Sgjelinek 	lnum = strtoul(l->patch_num, &endptr, 10);
7157*39935be5Sgjelinek 	rnum = strtoul(r->patch_num, &endptr, 10);
7158*39935be5Sgjelinek 
7159*39935be5Sgjelinek 	if (lnum > rnum)
7160*39935be5Sgjelinek 		return (1);
7161*39935be5Sgjelinek 	if (lnum < rnum)
7162*39935be5Sgjelinek 		return (-1);
7163*39935be5Sgjelinek 	return (0);
7164*39935be5Sgjelinek }
7165*39935be5Sgjelinek 
7166*39935be5Sgjelinek /*
7167*39935be5Sgjelinek  * Parse the patchinfo string for the patch.
7168*39935be5Sgjelinek  *
7169*39935be5Sgjelinek  * We are parsing entries of the form:
7170*39935be5Sgjelinek  * PATCH_INFO_121454-02=Installed: Wed Dec  7 07:13:51 PST 2005 From: mum \
7171*39935be5Sgjelinek  *	Obsoletes: 120777-03 121087-02 119108-07 Requires: 119575-02 \
7172*39935be5Sgjelinek  *	119255-06 Incompatibles:
7173*39935be5Sgjelinek  *
7174*39935be5Sgjelinek  * A backed out patch will have "backed out\n" as the status.  We should
7175*39935be5Sgjelinek  * skip these patches.  We also also ignore any entries that seem to be
7176*39935be5Sgjelinek  * corrupted.
7177*39935be5Sgjelinek  */
7178*39935be5Sgjelinek static int
7179*39935be5Sgjelinek parse_info(char *patchinfo, uu_avl_pool_t *patches_pool, uu_avl_t *patches_avl,
7180*39935be5Sgjelinek     uu_avl_t *obs_patches_avl)
7181*39935be5Sgjelinek {
7182ee519a1fSgjelinek 	char		*p;
7183ee519a1fSgjelinek 	char		*lastp;
7184*39935be5Sgjelinek 	char		*ep;
7185*39935be5Sgjelinek 	char		*pvers;
7186ee519a1fSgjelinek 	boolean_t	add_info = B_FALSE;
7187*39935be5Sgjelinek 	patch_node_t	*patch;
7188ee519a1fSgjelinek 
7189*39935be5Sgjelinek 	if (strlen(patchinfo) < (sizeof (PATCHINFO) - 1))
719007b574eeSgjelinek 		return (Z_OK);
719107b574eeSgjelinek 
7192*39935be5Sgjelinek 	/* Skip over "PATCH_INFO_" to get the patch id. */
7193*39935be5Sgjelinek 	p = patchinfo + sizeof (PATCHINFO) - 1;
7194*39935be5Sgjelinek 	if ((ep = strchr(p, '=')) == NULL)
7195*39935be5Sgjelinek 		return (Z_OK);
7196ee519a1fSgjelinek 
7197*39935be5Sgjelinek 	*ep++ = '\0';
7198*39935be5Sgjelinek 
7199*39935be5Sgjelinek 	/* Ignore all but installed patches. */
7200*39935be5Sgjelinek 	if (strncmp(ep, "Installed:", 10) != 0)
7201*39935be5Sgjelinek 		return (Z_OK);
7202*39935be5Sgjelinek 
7203*39935be5Sgjelinek 	/* remove newline */
7204*39935be5Sgjelinek 	lastp = (ep + strlen(ep) - 1);
7205*39935be5Sgjelinek 	if (*lastp == '\n')
7206*39935be5Sgjelinek 		*lastp = '\0';
7207*39935be5Sgjelinek 
7208*39935be5Sgjelinek 	if ((patch = (patch_node_t *)malloc(sizeof (patch_node_t))) == NULL)
7209*39935be5Sgjelinek 		return (Z_NOMEM);
7210*39935be5Sgjelinek 
7211*39935be5Sgjelinek 	if ((pvers = strchr(p, '-')) != NULL)
7212*39935be5Sgjelinek 		*pvers++ = '\0';
7213*39935be5Sgjelinek 	else
7214*39935be5Sgjelinek 		pvers = "";
7215*39935be5Sgjelinek 
7216*39935be5Sgjelinek 	if ((patch->patch_num = strdup(p)) == NULL) {
7217*39935be5Sgjelinek 		free(patch);
7218*39935be5Sgjelinek 		return (Z_NOMEM);
7219*39935be5Sgjelinek 	}
7220*39935be5Sgjelinek 	if ((patch->patch_vers = strdup(pvers)) == NULL) {
7221*39935be5Sgjelinek 		free(patch->patch_num);
7222*39935be5Sgjelinek 		free(patch);
7223*39935be5Sgjelinek 		return (Z_NOMEM);
7224*39935be5Sgjelinek 	}
7225*39935be5Sgjelinek 
7226*39935be5Sgjelinek 	uu_avl_node_init(patch, &patch->patch_node, patches_pool);
7227*39935be5Sgjelinek 	save_patch(patch, patches_avl);
7228ee519a1fSgjelinek 
7229ee519a1fSgjelinek 	/*
7230ee519a1fSgjelinek 	 * Start with the first token.  This will probably be "Installed:".
7231ee519a1fSgjelinek 	 * If we can't tokenize this entry, just return.
7232ee519a1fSgjelinek 	 */
7233*39935be5Sgjelinek 	if ((p = strtok_r(ep, " ", &lastp)) == NULL)
7234ee519a1fSgjelinek 		return (Z_OK);
7235ee519a1fSgjelinek 
7236ee519a1fSgjelinek 	do {
7237ee519a1fSgjelinek 		if (strcmp(p, "Installed:") == 0 ||
7238ee519a1fSgjelinek 		    strcmp(p, "Requires:") == 0 ||
7239*39935be5Sgjelinek 		    strcmp(p, "From:") == 0 ||
7240*39935be5Sgjelinek 		    strcmp(p, "Incompatibles:") == 0) {
7241ee519a1fSgjelinek 			add_info = B_FALSE;
7242ee519a1fSgjelinek 			continue;
7243ee519a1fSgjelinek 		} else if (strcmp(p, "Obsoletes:") == 0) {
7244ee519a1fSgjelinek 			add_info = B_TRUE;
7245ee519a1fSgjelinek 			continue;
7246ee519a1fSgjelinek 		}
7247ee519a1fSgjelinek 
7248ee519a1fSgjelinek 		if (!add_info)
7249ee519a1fSgjelinek 			continue;
7250ee519a1fSgjelinek 
7251*39935be5Sgjelinek 		if ((pvers = strchr(p, '-')) != NULL)
7252*39935be5Sgjelinek 			*pvers = '\0';
7253ee519a1fSgjelinek 
7254*39935be5Sgjelinek 		if (save_obs_patch(p, patches_pool, obs_patches_avl) != Z_OK)
7255*39935be5Sgjelinek 			return (Z_NOMEM);
7256ee519a1fSgjelinek 	} while ((p = strtok_r(NULL, " ", &lastp)) != NULL);
7257ee519a1fSgjelinek 
7258ee519a1fSgjelinek 	return (Z_OK);
7259ee519a1fSgjelinek }
7260ee519a1fSgjelinek 
7261*39935be5Sgjelinek /*
7262*39935be5Sgjelinek  * AVL walker callback used to add patch to XML manifest.
7263*39935be5Sgjelinek  *
7264*39935be5Sgjelinek  * PATH_MAX is used in the pkg/patch code as the maximum size for the patch
7265*39935be5Sgjelinek  * number/version string.
7266*39935be5Sgjelinek  */
7267*39935be5Sgjelinek static int
7268*39935be5Sgjelinek add_patch(void *e, void *p)
7269ee519a1fSgjelinek {
7270*39935be5Sgjelinek 	xmlNodePtr	node;
7271ee519a1fSgjelinek 	xmlNodePtr	cur;
7272*39935be5Sgjelinek 	char		id[PATH_MAX];
7273*39935be5Sgjelinek 	patch_node_t	*patch;
7274*39935be5Sgjelinek 	patch_parms_t	*args;
7275ee519a1fSgjelinek 
7276*39935be5Sgjelinek 	patch = e;
7277*39935be5Sgjelinek 	args = p;
7278ee519a1fSgjelinek 
7279*39935be5Sgjelinek 	/* skip this patch if it has been obsoleted */
7280*39935be5Sgjelinek 	if (obsolete_patch(patch, args->obs_patches_avl))
7281*39935be5Sgjelinek 		return (UU_WALK_NEXT);
7282*39935be5Sgjelinek 
7283*39935be5Sgjelinek 	if (patch->patch_vers[0] == '\0')
7284*39935be5Sgjelinek 		(void) snprintf(id, sizeof (id), "%s", patch->patch_num);
7285*39935be5Sgjelinek 	else
7286*39935be5Sgjelinek 		(void) snprintf(id, sizeof (id), "%s-%s", patch->patch_num,
7287*39935be5Sgjelinek 		    patch->patch_vers);
7288*39935be5Sgjelinek 
7289*39935be5Sgjelinek 	if ((args->res = operation_prep(args->handle)) != Z_OK)
7290*39935be5Sgjelinek 		return (UU_WALK_DONE);
7291*39935be5Sgjelinek 
7292*39935be5Sgjelinek 	cur = args->handle->zone_dh_cur;
7293*39935be5Sgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
7294*39935be5Sgjelinek 	if ((args->res = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
7295*39935be5Sgjelinek 		return (UU_WALK_DONE);
7296*39935be5Sgjelinek 
7297*39935be5Sgjelinek 	return (UU_WALK_NEXT);
7298ee519a1fSgjelinek }
7299ee519a1fSgjelinek 
7300*39935be5Sgjelinek static void
7301*39935be5Sgjelinek patch_avl_delete(uu_avl_t *patches_avl)
7302*39935be5Sgjelinek {
7303*39935be5Sgjelinek 	if (patches_avl != NULL) {
7304*39935be5Sgjelinek 		patch_node_t *p;
7305*39935be5Sgjelinek 		void *cookie = NULL;
7306*39935be5Sgjelinek 
7307*39935be5Sgjelinek 		while ((p = (patch_node_t *)uu_avl_teardown(patches_avl,
7308*39935be5Sgjelinek 		    &cookie)) != NULL) {
7309*39935be5Sgjelinek 			free(p->patch_num);
7310*39935be5Sgjelinek 			free(p->patch_vers);
7311*39935be5Sgjelinek 			free(p);
7312*39935be5Sgjelinek 		}
7313*39935be5Sgjelinek 
7314*39935be5Sgjelinek 		uu_avl_destroy(patches_avl);
7315*39935be5Sgjelinek 	}
7316ee519a1fSgjelinek }
7317ee519a1fSgjelinek 
7318ee519a1fSgjelinek /*
7319*39935be5Sgjelinek  * Add the unique, highest version patches that are associated with this pkg
7320*39935be5Sgjelinek  * to the sw inventory on the handle.
7321ee519a1fSgjelinek  */
7322ee519a1fSgjelinek static int
7323*39935be5Sgjelinek add_patches(zone_dochandle_t handle, struct zone_pkginfo *infop,
7324*39935be5Sgjelinek     uu_avl_pool_t *patches_pool, uu_avl_t *obs_patches_avl)
7325ee519a1fSgjelinek {
7326ee519a1fSgjelinek 	int		i;
7327*39935be5Sgjelinek 	int		res;
7328*39935be5Sgjelinek 	uu_avl_t 	*patches_avl;
7329*39935be5Sgjelinek 	patch_parms_t	args;
7330*39935be5Sgjelinek 
7331*39935be5Sgjelinek 	if ((patches_avl = uu_avl_create(patches_pool, NULL, UU_DEFAULT))
7332*39935be5Sgjelinek 	    == NULL)
7333*39935be5Sgjelinek 		return (Z_NOMEM);
7334ee519a1fSgjelinek 
7335ee519a1fSgjelinek 	for (i = 0; i < infop->zpi_patch_cnt; i++) {
7336*39935be5Sgjelinek 		if ((res = parse_info(infop->zpi_patchinfo[i], patches_pool,
7337*39935be5Sgjelinek 		    patches_avl, obs_patches_avl)) != Z_OK) {
7338*39935be5Sgjelinek 			patch_avl_delete(patches_avl);
7339*39935be5Sgjelinek 			return (res);
7340*39935be5Sgjelinek 		}
7341ee519a1fSgjelinek 	}
7342ee519a1fSgjelinek 
7343*39935be5Sgjelinek 	args.obs_patches_avl = obs_patches_avl;
7344*39935be5Sgjelinek 	args.handle = handle;
7345*39935be5Sgjelinek 	args.res = Z_OK;
7346*39935be5Sgjelinek 
7347*39935be5Sgjelinek 	(void) uu_avl_walk(patches_avl, add_patch, &args, 0);
7348*39935be5Sgjelinek 
7349*39935be5Sgjelinek 	patch_avl_delete(patches_avl);
7350*39935be5Sgjelinek 	return (args.res);
7351ee519a1fSgjelinek }
7352ee519a1fSgjelinek 
7353ee519a1fSgjelinek /*
7354ee519a1fSgjelinek  * Add the pkg to the sw inventory on the handle.
7355ee519a1fSgjelinek  */
7356ee519a1fSgjelinek static int
7357ee519a1fSgjelinek add_pkg(zone_dochandle_t handle, char *name, char *version)
7358ee519a1fSgjelinek {
7359ee519a1fSgjelinek 	xmlNodePtr newnode;
7360ee519a1fSgjelinek 	xmlNodePtr cur;
7361ee519a1fSgjelinek 	int err;
7362ee519a1fSgjelinek 
7363ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
7364ee519a1fSgjelinek 		return (err);
7365ee519a1fSgjelinek 
7366ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
7367ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
7368ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
7369ee519a1fSgjelinek 		return (err);
7370ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
7371ee519a1fSgjelinek 		return (err);
7372ee519a1fSgjelinek 	return (Z_OK);
7373ee519a1fSgjelinek }
7374ee519a1fSgjelinek 
7375ee519a1fSgjelinek static void
7376ee519a1fSgjelinek free_pkginfo(struct zone_pkginfo *infop)
7377ee519a1fSgjelinek {
7378ee519a1fSgjelinek 	free(infop->zpi_version);
7379ee519a1fSgjelinek 	if (infop->zpi_patch_cnt > 0) {
7380ee519a1fSgjelinek 		int i;
7381ee519a1fSgjelinek 
7382ee519a1fSgjelinek 		for (i = 0; i < infop->zpi_patch_cnt; i++)
7383ee519a1fSgjelinek 			free(infop->zpi_patchinfo[i]);
7384ee519a1fSgjelinek 		free(infop->zpi_patchinfo);
7385ee519a1fSgjelinek 	}
7386ee519a1fSgjelinek }
7387ee519a1fSgjelinek 
7388ee519a1fSgjelinek /*
7389ee519a1fSgjelinek  * Read the pkginfo file and populate the structure with the data we need
7390ee519a1fSgjelinek  * from this pkg for a sw inventory.
7391ee519a1fSgjelinek  */
7392ee519a1fSgjelinek static int
7393ee519a1fSgjelinek get_pkginfo(char *pkginfo, struct zone_pkginfo *infop)
7394ee519a1fSgjelinek {
7395ee519a1fSgjelinek 	FILE	*fp;
7396ee519a1fSgjelinek 	char	*buf;
7397ee519a1fSgjelinek 	int	err = 0;
7398ee519a1fSgjelinek 
7399ee519a1fSgjelinek 	infop->zpi_all_zones = B_FALSE;
7400ee519a1fSgjelinek 	infop->zpi_this_zone = B_FALSE;
7401ee519a1fSgjelinek 	infop->zpi_version = NULL;
7402ee519a1fSgjelinek 	infop->zpi_patch_cnt = 0;
7403ee519a1fSgjelinek 	infop->zpi_patchinfo = NULL;
7404ee519a1fSgjelinek 
7405ee519a1fSgjelinek 	if ((fp = fopen(pkginfo, "r")) == NULL)
7406ee519a1fSgjelinek 		return (errno);
7407ee519a1fSgjelinek 
7408ee519a1fSgjelinek 	while ((buf = read_pkg_data(fp)) != NULL) {
7409ee519a1fSgjelinek 		if (strncmp(buf, VERSION, sizeof (VERSION) - 1) == 0) {
7410ee519a1fSgjelinek 			int len;
7411ee519a1fSgjelinek 
7412ee519a1fSgjelinek 			if ((infop->zpi_version =
7413ee519a1fSgjelinek 			    strdup(buf + sizeof (VERSION) - 1)) == NULL) {
7414ee519a1fSgjelinek 				err = ENOMEM;
7415ee519a1fSgjelinek 				break;
7416ee519a1fSgjelinek 			}
7417ee519a1fSgjelinek 
7418ee519a1fSgjelinek 			/* remove trailing newline */
7419ee519a1fSgjelinek 			len = strlen(infop->zpi_version);
7420ee519a1fSgjelinek 			*(infop->zpi_version + len - 1) = 0;
7421ee519a1fSgjelinek 
742245916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_ALL_ZONES) == 0) {
7423ee519a1fSgjelinek 			infop->zpi_all_zones = B_TRUE;
7424ee519a1fSgjelinek 
742545916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_THIS_ZONE) == 0) {
7426ee519a1fSgjelinek 			infop->zpi_this_zone = B_TRUE;
7427ee519a1fSgjelinek 
7428ee519a1fSgjelinek 		} else if (strncmp(buf, PATCHINFO, sizeof (PATCHINFO) - 1)
7429ee519a1fSgjelinek 		    == 0) {
7430ee519a1fSgjelinek 			char **p;
7431ee519a1fSgjelinek 
7432ee519a1fSgjelinek 			if ((p = (char **)realloc(infop->zpi_patchinfo,
7433ee519a1fSgjelinek 			    sizeof (char *) * (infop->zpi_patch_cnt + 1)))
7434ee519a1fSgjelinek 			    == NULL) {
7435ee519a1fSgjelinek 				err = ENOMEM;
7436ee519a1fSgjelinek 				break;
7437ee519a1fSgjelinek 			}
7438ee519a1fSgjelinek 			infop->zpi_patchinfo = p;
7439ee519a1fSgjelinek 
7440ee519a1fSgjelinek 			if ((infop->zpi_patchinfo[infop->zpi_patch_cnt] =
7441ee519a1fSgjelinek 			    strdup(buf)) == NULL) {
7442ee519a1fSgjelinek 				err = ENOMEM;
7443ee519a1fSgjelinek 				break;
7444ee519a1fSgjelinek 			}
7445ee519a1fSgjelinek 			infop->zpi_patch_cnt++;
7446ee519a1fSgjelinek 		}
7447ee519a1fSgjelinek 
7448ee519a1fSgjelinek 		free(buf);
7449ee519a1fSgjelinek 	}
7450ee519a1fSgjelinek 
7451ee519a1fSgjelinek 	free(buf);
7452ee519a1fSgjelinek 
7453ee519a1fSgjelinek 	if (errno == ENOMEM) {
7454ee519a1fSgjelinek 		err = ENOMEM;
7455ee519a1fSgjelinek 		/* Clean up anything we did manage to allocate. */
7456ee519a1fSgjelinek 		free_pkginfo(infop);
7457ee519a1fSgjelinek 	}
7458ee519a1fSgjelinek 
7459ee519a1fSgjelinek 	(void) fclose(fp);
7460ee519a1fSgjelinek 
7461ee519a1fSgjelinek 	return (err);
7462ee519a1fSgjelinek }
7463ee519a1fSgjelinek 
7464ee519a1fSgjelinek /*
7465ee519a1fSgjelinek  * Take a software inventory of the global zone.  We need to get the set of
7466ee519a1fSgjelinek  * packages and patches that are on the global zone that the specified
7467ee519a1fSgjelinek  * non-global zone depends on.  The packages we need in the inventory are:
7468ee519a1fSgjelinek  *
7469ee519a1fSgjelinek  * - skip the package if SUNW_PKG_THISZONE is 'true'
7470ee519a1fSgjelinek  * otherwise,
7471ee519a1fSgjelinek  * - add the package if
7472ee519a1fSgjelinek  * a) SUNW_PKG_ALLZONES is 'true',
7473ee519a1fSgjelinek  * or
7474ee519a1fSgjelinek  * b) any file delivered by the package is in a file system that is inherited
7475ee519a1fSgjelinek  * from the global zone.
7476ee519a1fSgjelinek  * If the zone does not inherit any file systems (whole root)
7477ee519a1fSgjelinek  * then (b) will be skipped.
7478ee519a1fSgjelinek  *
7479ee519a1fSgjelinek  * For each of the packages that is being added to the inventory, we will also
7480ee519a1fSgjelinek  * add all of the associated, unique patches to the inventory.
7481ee519a1fSgjelinek  */
7482ee519a1fSgjelinek static int
7483ee519a1fSgjelinek zonecfg_sw_inventory(zone_dochandle_t handle)
7484ee519a1fSgjelinek {
7485ee519a1fSgjelinek 	char		pkginfo[MAXPATHLEN];
7486ee519a1fSgjelinek 	int		res;
7487ee519a1fSgjelinek 	struct dirent	*dp;
7488ee519a1fSgjelinek 	DIR		*dirp;
7489ee519a1fSgjelinek 	struct stat	buf;
7490ee519a1fSgjelinek 	struct zone_pkginfo	info;
7491ee519a1fSgjelinek 	int		pkg_cnt = 0;
7492ee519a1fSgjelinek 	char		**pkgs = NULL;
7493*39935be5Sgjelinek 	uu_avl_pool_t 	*patches_pool;
7494*39935be5Sgjelinek 	uu_avl_t 	*obs_patches_avl;
7495ee519a1fSgjelinek 
7496*39935be5Sgjelinek 	if ((patches_pool = uu_avl_pool_create("patches_pool",
7497*39935be5Sgjelinek 	    sizeof (patch_node_t), offsetof(patch_node_t, patch_node),
7498*39935be5Sgjelinek 	    patch_node_compare, UU_DEFAULT)) == NULL) {
7499*39935be5Sgjelinek 		return (Z_NOMEM);
7500*39935be5Sgjelinek 	}
7501*39935be5Sgjelinek 
7502*39935be5Sgjelinek 	if ((obs_patches_avl = uu_avl_create(patches_pool, NULL, UU_DEFAULT))
7503*39935be5Sgjelinek 	    == NULL) {
7504*39935be5Sgjelinek 		uu_avl_pool_destroy(patches_pool);
7505*39935be5Sgjelinek 		return (Z_NOMEM);
7506*39935be5Sgjelinek 	}
7507*39935be5Sgjelinek 
7508*39935be5Sgjelinek 	if ((res = get_ipd_pkgs(handle, &pkgs, &pkg_cnt)) != Z_OK) {
7509*39935be5Sgjelinek 		patch_avl_delete(obs_patches_avl);
7510*39935be5Sgjelinek 		uu_avl_pool_destroy(patches_pool);
7511ee519a1fSgjelinek 		return (res);
7512*39935be5Sgjelinek 	}
7513ee519a1fSgjelinek 
7514ee519a1fSgjelinek 	if ((dirp = opendir(PKG_PATH)) == NULL) {
7515*39935be5Sgjelinek 		patch_avl_delete(obs_patches_avl);
7516*39935be5Sgjelinek 		uu_avl_pool_destroy(patches_pool);
7517ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
7518ee519a1fSgjelinek 		return (Z_OK);
7519ee519a1fSgjelinek 	}
7520ee519a1fSgjelinek 
7521ee519a1fSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
7522ee519a1fSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
7523ee519a1fSgjelinek 		    strcmp(dp->d_name, "..") == 0)
7524ee519a1fSgjelinek 			continue;
7525ee519a1fSgjelinek 
7526ee519a1fSgjelinek 		(void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo",
7527ee519a1fSgjelinek 		    PKG_PATH, dp->d_name);
7528ee519a1fSgjelinek 
7529ee519a1fSgjelinek 		if (stat(pkginfo, &buf) == -1 || !S_ISREG(buf.st_mode))
7530ee519a1fSgjelinek 			continue;
7531ee519a1fSgjelinek 
7532ee519a1fSgjelinek 		if (get_pkginfo(pkginfo, &info) != 0) {
7533ee519a1fSgjelinek 			res = Z_NOMEM;
7534ee519a1fSgjelinek 			break;
7535ee519a1fSgjelinek 		}
7536ee519a1fSgjelinek 
7537ee519a1fSgjelinek 		if (!info.zpi_this_zone &&
7538ee519a1fSgjelinek 		    (info.zpi_all_zones ||
7539ee519a1fSgjelinek 		    dir_pkg(dp->d_name, pkgs, pkg_cnt))) {
7540ee519a1fSgjelinek 			if ((res = add_pkg(handle, dp->d_name,
7541ee519a1fSgjelinek 			    info.zpi_version)) == Z_OK) {
7542ee519a1fSgjelinek 				if (info.zpi_patch_cnt > 0)
7543*39935be5Sgjelinek 					res = add_patches(handle, &info,
7544*39935be5Sgjelinek 					    patches_pool, obs_patches_avl);
7545ee519a1fSgjelinek 			}
7546ee519a1fSgjelinek 		}
7547ee519a1fSgjelinek 
7548ee519a1fSgjelinek 		free_pkginfo(&info);
7549ee519a1fSgjelinek 
7550ee519a1fSgjelinek 		if (res != Z_OK)
7551ee519a1fSgjelinek 			break;
7552ee519a1fSgjelinek 	}
7553ee519a1fSgjelinek 
7554ee519a1fSgjelinek 	(void) closedir(dirp);
7555ee519a1fSgjelinek 
7556*39935be5Sgjelinek 	patch_avl_delete(obs_patches_avl);
7557*39935be5Sgjelinek 	uu_avl_pool_destroy(patches_pool);
7558ee519a1fSgjelinek 	free_ipd_pkgs(pkgs, pkg_cnt);
7559ee519a1fSgjelinek 
7560ee519a1fSgjelinek 	if (res == Z_OK)
7561ee519a1fSgjelinek 		handle->zone_dh_sw_inv = B_TRUE;
7562ee519a1fSgjelinek 
7563ee519a1fSgjelinek 	return (res);
7564ee519a1fSgjelinek }
7565ee519a1fSgjelinek 
7566ee519a1fSgjelinek /*
7567ee519a1fSgjelinek  * zonecfg_devwalk call-back function used during detach to generate the
7568ee519a1fSgjelinek  * dev info in the manifest.
7569ee519a1fSgjelinek  */
7570ee519a1fSgjelinek static int
7571ee519a1fSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
7572ee519a1fSgjelinek     const char *acl, void *hdl)
7573ee519a1fSgjelinek {
7574ee519a1fSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
7575ee519a1fSgjelinek 	xmlNodePtr newnode;
7576ee519a1fSgjelinek 	xmlNodePtr cur;
7577ee519a1fSgjelinek 	int err;
7578ee519a1fSgjelinek 	char buf[128];
7579ee519a1fSgjelinek 
7580ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
7581ee519a1fSgjelinek 		return (err);
7582ee519a1fSgjelinek 
7583ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
7584ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
7585ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
7586ee519a1fSgjelinek 		return (err);
7587ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
7588ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
7589ee519a1fSgjelinek 		return (err);
7590ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
7591ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
7592ee519a1fSgjelinek 		return (err);
7593ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
7594ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
7595ee519a1fSgjelinek 		return (err);
7596ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
7597ee519a1fSgjelinek 		return (err);
7598ee519a1fSgjelinek 	return (Z_OK);
7599ee519a1fSgjelinek }
7600ee519a1fSgjelinek 
7601ee519a1fSgjelinek /*
7602ee519a1fSgjelinek  * Get the information required to support detaching a zone.  This is
7603ee519a1fSgjelinek  * called on the source system when detaching (the detaching parameter should
7604ee519a1fSgjelinek  * be set to true) and on the destination system before attaching (the
7605ee519a1fSgjelinek  * detaching parameter should be false).
7606ee519a1fSgjelinek  *
7607ee519a1fSgjelinek  * For native Solaris zones, the detach/attach process involves validating
7608ee519a1fSgjelinek  * that the software on the global zone can support the zone when we attach.
7609ee519a1fSgjelinek  * To do this we take a software inventory of the global zone.  We also
7610ee519a1fSgjelinek  * have to keep track of the device configuration so that we can properly
7611ee519a1fSgjelinek  * recreate it on the destination.
7612ee519a1fSgjelinek  */
7613ee519a1fSgjelinek int
7614ee519a1fSgjelinek zonecfg_get_detach_info(zone_dochandle_t handle, boolean_t detaching)
7615ee519a1fSgjelinek {
7616ee519a1fSgjelinek 	int		res;
7617ee519a1fSgjelinek 
7618ee519a1fSgjelinek 	if ((res = zonecfg_sw_inventory(handle)) != Z_OK)
7619ee519a1fSgjelinek 		return (res);
7620ee519a1fSgjelinek 
7621ee519a1fSgjelinek 	if (detaching)
7622ee519a1fSgjelinek 		res = zonecfg_devwalk(handle, get_detach_dev_entry, handle);
7623ee519a1fSgjelinek 
7624ee519a1fSgjelinek 	return (res);
7625ee519a1fSgjelinek }
7626