xref: /titanic_50/usr/src/lib/libzonecfg/common/libzonecfg.c (revision de860bd9529e1034e1666f74f7bc0ec8cd5ca701)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ee519a1fSgjelinek  * Common Development and Distribution License (the "License").
6ee519a1fSgjelinek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
236cfd72c6Sgjelinek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
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>
5439935be5Sgjelinek #include <libuutil.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
577c478bd9Sstevel@tonic-gate #include <netdb.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h>
607c478bd9Sstevel@tonic-gate #include <libxml/parser.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
63108322fbScarlsonj #include <uuid/uuid.h>
64ee519a1fSgjelinek #include <dirent.h>
659acbbeafSnn35248 #include <libbrand.h>
66ee519a1fSgjelinek 
677c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
687c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	"/zonecfg.XXXXXX"
71cf8f45c7Sdstaff #define	ZONE_CB_RETRY_COUNT		10
72cf8f45c7Sdstaff #define	ZONE_EVENT_PING_SUBCLASS	"ping"
73cf8f45c7Sdstaff #define	ZONE_EVENT_PING_PUBLISHER	"solaris"
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */
767c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ATTR		(const xmlChar *) "attr"
777c478bd9Sstevel@tonic-gate #define	DTD_ELEM_COMMENT	(const xmlChar *) "comment"
787c478bd9Sstevel@tonic-gate #define	DTD_ELEM_DEVICE		(const xmlChar *) "device"
797c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FS		(const xmlChar *) "filesystem"
807c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FSOPTION	(const xmlChar *) "fsoption"
817c478bd9Sstevel@tonic-gate #define	DTD_ELEM_IPD		(const xmlChar *) "inherited-pkg-dir"
827c478bd9Sstevel@tonic-gate #define	DTD_ELEM_NET		(const xmlChar *) "network"
837c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTL		(const xmlChar *) "rctl"
847c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTLVALUE	(const xmlChar *) "rctl-value"
857c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ZONE		(const xmlChar *) "zone"
86fa9e4066Sahrens #define	DTD_ELEM_DATASET	(const xmlChar *) "dataset"
870209230bSgjelinek #define	DTD_ELEM_TMPPOOL	(const xmlChar *) "tmp_pool"
880209230bSgjelinek #define	DTD_ELEM_PSET		(const xmlChar *) "pset"
890209230bSgjelinek #define	DTD_ELEM_MCAP		(const xmlChar *) "mcap"
90ee519a1fSgjelinek #define	DTD_ELEM_PACKAGE	(const xmlChar *) "package"
91ee519a1fSgjelinek #define	DTD_ELEM_PATCH		(const xmlChar *) "patch"
926cfd72c6Sgjelinek #define	DTD_ELEM_OBSOLETES	(const xmlChar *) "obsoletes"
93ee519a1fSgjelinek #define	DTD_ELEM_DEV_PERM	(const xmlChar *) "dev-perm"
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ACTION		(const xmlChar *) "action"
967c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ADDRESS	(const xmlChar *) "address"
977c478bd9Sstevel@tonic-gate #define	DTD_ATTR_AUTOBOOT	(const xmlChar *) "autoboot"
98f4b3ec61Sdh155122 #define	DTD_ATTR_IPTYPE		(const xmlChar *) "ip-type"
99*de860bd9Sgfaden #define	DTD_ATTR_DEFROUTER	(const xmlChar *) "defrouter"
1007c478bd9Sstevel@tonic-gate #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
1017c478bd9Sstevel@tonic-gate #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
102ffbafc53Scomay #define	DTD_ATTR_LIMITPRIV	(const xmlChar *) "limitpriv"
1033f2f09c1Sdp #define	DTD_ATTR_BOOTARGS	(const xmlChar *) "bootargs"
1040209230bSgjelinek #define	DTD_ATTR_SCHED		(const xmlChar *) "scheduling-class"
1057c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
1067c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
1077c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
1087c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
1097c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
1107c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
1117c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
1127c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
1137c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
1147c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
1150209230bSgjelinek #define	DTD_ATTR_NCPU_MIN	(const xmlChar *) "ncpu_min"
1160209230bSgjelinek #define	DTD_ATTR_NCPU_MAX	(const xmlChar *) "ncpu_max"
1170209230bSgjelinek #define	DTD_ATTR_IMPORTANCE	(const xmlChar *) "importance"
1180209230bSgjelinek #define	DTD_ATTR_PHYSCAP	(const xmlChar *) "physcap"
119ee519a1fSgjelinek #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
120ee519a1fSgjelinek #define	DTD_ATTR_ID		(const xmlChar *) "id"
121ee519a1fSgjelinek #define	DTD_ATTR_UID		(const xmlChar *) "uid"
122ee519a1fSgjelinek #define	DTD_ATTR_GID		(const xmlChar *) "gid"
123ee519a1fSgjelinek #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
124ee519a1fSgjelinek #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
1259acbbeafSnn35248 #define	DTD_ATTR_BRAND		(const xmlChar *) "brand"
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
1287c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
1297c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
1307c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
1317c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
1327c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
1337c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
1347c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
1357c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
1367c478bd9Sstevel@tonic-gate 
137a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
138a1be23daSdp 
139ee519a1fSgjelinek #define	DETACHED	"SUNWdetached.xml"
140ee519a1fSgjelinek #define	ATTACH_FORCED	"SUNWattached.xml"
141ee519a1fSgjelinek #define	PKG_PATH	"/var/sadm/pkg"
142ee519a1fSgjelinek #define	CONTENTS_FILE	"/var/sadm/install/contents"
14345916cd2Sjpk #define	SUNW_PKG_ALL_ZONES	"SUNW_PKG_ALLZONES=true\n"
14445916cd2Sjpk #define	SUNW_PKG_THIS_ZONE	"SUNW_PKG_THISZONE=true\n"
145ee519a1fSgjelinek #define	VERSION		"VERSION="
146ee519a1fSgjelinek #define	PATCHLIST	"PATCHLIST="
147ee519a1fSgjelinek #define	PATCHINFO	"PATCH_INFO_"
148ee519a1fSgjelinek #define	PKGINFO_RD_LEN	128
149ee519a1fSgjelinek 
1500209230bSgjelinek #define	TMP_POOL_NAME	"SUNWtmp_%s"
1510209230bSgjelinek #define	MAX_TMP_POOL_NAME	(ZONENAME_MAX + 9)
1520209230bSgjelinek #define	RCAP_SERVICE	"system/rcap:default"
1530209230bSgjelinek #define	POOLD_SERVICE	"system/pools/dynamic:default"
1540209230bSgjelinek 
15539935be5Sgjelinek enum zn_ipd_fs {ZONE_IPD, ZONE_FS};
15639935be5Sgjelinek 
1570209230bSgjelinek /*
1580209230bSgjelinek  * rctl alias definitions
1590209230bSgjelinek  *
1600209230bSgjelinek  * This holds the alias, the full rctl name, the default priv value, action
1610209230bSgjelinek  * and lower limit.  The functions that handle rctl aliases step through
1620209230bSgjelinek  * this table, matching on the alias, and using the full values for setting
1630209230bSgjelinek  * the rctl entry as well the limit for validation.
1640209230bSgjelinek  */
1650209230bSgjelinek static struct alias {
1660209230bSgjelinek 	char *shortname;
1670209230bSgjelinek 	char *realname;
1680209230bSgjelinek 	char *priv;
1690209230bSgjelinek 	char *action;
1700209230bSgjelinek 	uint64_t low_limit;
1710209230bSgjelinek } aliases[] = {
1720209230bSgjelinek 	{ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100},
1730209230bSgjelinek 	{ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0},
1740209230bSgjelinek 	{ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0},
1750209230bSgjelinek 	{ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0},
1760209230bSgjelinek 	{ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0},
1770209230bSgjelinek 	{ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0},
1780209230bSgjelinek 	{ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0},
1790209230bSgjelinek 	{ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0},
180c97ad5cdSakolb 	{ALIAS_CPUCAP, "zone.cpu-cap", "privileged", "deny", 0},
1810209230bSgjelinek 	{NULL, NULL, NULL, NULL, 0}
1820209230bSgjelinek };
1830209230bSgjelinek 
1840209230bSgjelinek /*
1850209230bSgjelinek  * Structure for applying rctls to a running zone.  It allows important
1860209230bSgjelinek  * process values to be passed together easily.
1870209230bSgjelinek  */
1880209230bSgjelinek typedef struct pr_info_handle {
1890209230bSgjelinek 	struct ps_prochandle *pr;
1900209230bSgjelinek 	pid_t pid;
1910209230bSgjelinek } pr_info_handle_t;
1920209230bSgjelinek 
1937c478bd9Sstevel@tonic-gate struct zone_dochandle {
1947c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1957c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1967c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1977c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
198087719fdSdp 	boolean_t	zone_dh_newzone;
199087719fdSdp 	boolean_t	zone_dh_snapshot;
200ee519a1fSgjelinek 	boolean_t	zone_dh_sw_inv;
201087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
2027c478bd9Sstevel@tonic-gate };
2037c478bd9Sstevel@tonic-gate 
204cf8f45c7Sdstaff struct znotify {
205cf8f45c7Sdstaff 	void * zn_private;
206cf8f45c7Sdstaff 	evchan_t *zn_eventchan;
207cf8f45c7Sdstaff 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
208cf8f45c7Sdstaff 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
209cf8f45c7Sdstaff 	pthread_mutex_t zn_mutex;
210cf8f45c7Sdstaff 	pthread_cond_t zn_cond;
211cf8f45c7Sdstaff 	pthread_mutex_t zn_bigmutex;
212cf8f45c7Sdstaff 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
213cf8f45c7Sdstaff 	    ZN_PING_RECEIVED} zn_state;
214cf8f45c7Sdstaff 	char zn_subscriber_id[MAX_SUBID_LEN];
215cf8f45c7Sdstaff 	volatile boolean_t zn_failed;
216cf8f45c7Sdstaff 	int zn_failure_count;
217cf8f45c7Sdstaff };
218cf8f45c7Sdstaff 
219ee519a1fSgjelinek struct zone_pkginfo {
220ee519a1fSgjelinek 	boolean_t	zpi_all_zones;
221ee519a1fSgjelinek 	boolean_t	zpi_this_zone;
222ee519a1fSgjelinek 	int		zpi_patch_cnt;
223ee519a1fSgjelinek 	char		*zpi_version;
224ee519a1fSgjelinek 	char		**zpi_patchinfo;
225ee519a1fSgjelinek };
226ee519a1fSgjelinek 
22739935be5Sgjelinek typedef struct {
22839935be5Sgjelinek 	uu_avl_node_t	patch_node;
22939935be5Sgjelinek 	char		*patch_num;
23039935be5Sgjelinek 	char		*patch_vers;
2316cfd72c6Sgjelinek 	uu_list_t	*obs_patches;
23239935be5Sgjelinek } patch_node_t;
23339935be5Sgjelinek 
23439935be5Sgjelinek typedef struct {
2356cfd72c6Sgjelinek 	uu_list_node_t	link;
2366cfd72c6Sgjelinek 	char		*patch_num;
2376cfd72c6Sgjelinek } obs_patch_node_t;
2386cfd72c6Sgjelinek 
2396cfd72c6Sgjelinek typedef struct {
24039935be5Sgjelinek 	uu_avl_t	*obs_patches_avl;
24139935be5Sgjelinek 	zone_dochandle_t handle;
24239935be5Sgjelinek 	int		res;
24339935be5Sgjelinek } patch_parms_t;
24439935be5Sgjelinek 
245108322fbScarlsonj char *zonecfg_root = "";
246108322fbScarlsonj 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
2497c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
2507c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
2517c478bd9Sstevel@tonic-gate  * values from this set.
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
255108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
256108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
257108322fbScarlsonj  * configuration inside mounted alternate boot environments.
258108322fbScarlsonj  */
259108322fbScarlsonj void
260108322fbScarlsonj zonecfg_set_root(const char *rootpath)
261108322fbScarlsonj {
262108322fbScarlsonj 	if (*zonecfg_root != '\0')
263108322fbScarlsonj 		free(zonecfg_root);
264108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
265108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
266108322fbScarlsonj 		zonecfg_root = "";
267108322fbScarlsonj }
268108322fbScarlsonj 
269108322fbScarlsonj const char *
270108322fbScarlsonj zonecfg_get_root(void)
271108322fbScarlsonj {
272108322fbScarlsonj 	return (zonecfg_root);
273108322fbScarlsonj }
274108322fbScarlsonj 
275108322fbScarlsonj boolean_t
276108322fbScarlsonj zonecfg_in_alt_root(void)
277108322fbScarlsonj {
278108322fbScarlsonj 	return (*zonecfg_root != '\0');
279108322fbScarlsonj }
280108322fbScarlsonj 
281108322fbScarlsonj /*
2827c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
2837c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate 
286108322fbScarlsonj static boolean_t
2877c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
2887c478bd9Sstevel@tonic-gate {
289108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
290108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
293108322fbScarlsonj static boolean_t
294108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
2957c478bd9Sstevel@tonic-gate {
296108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
297108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3017c478bd9Sstevel@tonic-gate static void
3027c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
3067c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate zone_dochandle_t
3117c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
3127c478bd9Sstevel@tonic-gate {
313087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
3147c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
3157c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
3167c478bd9Sstevel@tonic-gate 		return (NULL);
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
3207c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
3217c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3227c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
3237c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
3247c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
3257c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	return (handle);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate int
3317c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
3347c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3357c478bd9Sstevel@tonic-gate 	return (Z_OK);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate void
3397c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
3427c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
3437c478bd9Sstevel@tonic-gate 	if (handle != NULL)
3447c478bd9Sstevel@tonic-gate 		free(handle);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate static int
3487c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
3517c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
3527c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
3537c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
3547c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
3557c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	return (Z_OK);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate int
361087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
364087719fdSdp 	struct zoneent ze;
365087719fdSdp 	int err, state_err;
366087719fdSdp 	zone_state_t state;
3677c478bd9Sstevel@tonic-gate 
368108322fbScarlsonj 	if (!config_file_path(zonename, path))
369108322fbScarlsonj 		return (Z_MISC_FS);
370087719fdSdp 
371087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
372087719fdSdp 	err = access(path, W_OK);
373087719fdSdp 
374087719fdSdp 	/*
375087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
376087719fdSdp 	 * such zone exists.
377087719fdSdp 	 */
378087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
379087719fdSdp 		return (Z_NO_ZONE);
380087719fdSdp 
381087719fdSdp 	/*
382087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
383087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
384087719fdSdp 	 * in which case we plow on.
385087719fdSdp 	 */
386087719fdSdp 	if (err == -1 && errno != ENOENT) {
387087719fdSdp 		if (errno == EACCES)
388087719fdSdp 			return (Z_ACCES);
389087719fdSdp 		else if (!force)
390087719fdSdp 			return (Z_MISC_FS);
391087719fdSdp 	}
392087719fdSdp 
393087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
394087719fdSdp 		return (Z_BAD_ZONE_STATE);
395087719fdSdp 
396087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
397087719fdSdp 		return (Z_BAD_ZONE_STATE);
398087719fdSdp 
399087719fdSdp 	/*
400087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
401087719fdSdp 	 * will fail only if we've had some more severe problem.
402087719fdSdp 	 */
403087719fdSdp 	bzero(&ze, sizeof (ze));
404087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
405087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
406087719fdSdp 		if (!force)
407087719fdSdp 			return (err);
408087719fdSdp 
409087719fdSdp 	err = zonecfg_destroy_impl(path);
410087719fdSdp 
411087719fdSdp 	/*
412087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
413087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
414087719fdSdp 	 */
415087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
416087719fdSdp 		return (Z_OK);
417087719fdSdp 	return (err);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate int
421108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4247c478bd9Sstevel@tonic-gate 
425108322fbScarlsonj 	if (!snap_file_path(zonename, path))
426108322fbScarlsonj 		return (Z_MISC_FS);
4277c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate static int
431a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
4347c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
4357c478bd9Sstevel@tonic-gate 
436a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
437a1be23daSdp 
438a1be23daSdp 	if (*root == NULL)
4397c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
440a1be23daSdp 
441a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
442a1be23daSdp 		return (Z_WRONG_DOC_TYPE);
443a1be23daSdp 
444a1be23daSdp 	return (Z_OK);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
447a1be23daSdp static int
448a1be23daSdp operation_prep(zone_dochandle_t handle)
449a1be23daSdp {
450a1be23daSdp 	xmlNodePtr root;
451a1be23daSdp 	int err;
452a1be23daSdp 
453a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
454a1be23daSdp 		return (err);
455a1be23daSdp 
456a1be23daSdp 	handle->zone_dh_cur = root;
457a1be23daSdp 	handle->zone_dh_top = root;
458a1be23daSdp 	return (Z_OK);
4597c478bd9Sstevel@tonic-gate }
460a1be23daSdp 
461a1be23daSdp static int
462ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
463ffbafc53Scomay {
464ffbafc53Scomay 	xmlChar *property;
465ffbafc53Scomay 	size_t srcsize;
466ffbafc53Scomay 
467ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
468ffbafc53Scomay 		return (Z_BAD_PROPERTY);
469ffbafc53Scomay 	srcsize = strlcpy(dst, (char *)property, dstsize);
470ffbafc53Scomay 	xmlFree(property);
471ffbafc53Scomay 	if (srcsize >= dstsize)
472ffbafc53Scomay 		return (Z_TOO_BIG);
473ffbafc53Scomay 	return (Z_OK);
474ffbafc53Scomay }
475ffbafc53Scomay 
476ffbafc53Scomay static int
477ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
478ffbafc53Scomay {
479ffbafc53Scomay 	xmlChar *property;
480ffbafc53Scomay 
481ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
482ffbafc53Scomay 		return (Z_BAD_PROPERTY);
483ffbafc53Scomay 	if ((*dst = strdup((char *)property)) == NULL) {
484ffbafc53Scomay 		xmlFree(property);
485ffbafc53Scomay 		return (Z_NOMEM);
486ffbafc53Scomay 	}
487ffbafc53Scomay 	xmlFree(property);
488ffbafc53Scomay 	return (Z_OK);
489ffbafc53Scomay }
490ffbafc53Scomay 
491ffbafc53Scomay static int
492a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
493a1be23daSdp     char *propval, size_t propsize)
494a1be23daSdp {
495a1be23daSdp 	xmlNodePtr root;
496a1be23daSdp 	int err;
497a1be23daSdp 
498a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
499a1be23daSdp 		return (err);
500a1be23daSdp 
501ffbafc53Scomay 	return (fetchprop(root, propname, propval, propsize));
502ffbafc53Scomay }
503ffbafc53Scomay 
504ffbafc53Scomay static int
505ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
506ffbafc53Scomay     char **propval)
507ffbafc53Scomay {
508ffbafc53Scomay 	xmlNodePtr root;
509ffbafc53Scomay 	int err;
510ffbafc53Scomay 
511ffbafc53Scomay 	if ((err = getroot(handle, &root)) != 0)
512ffbafc53Scomay 		return (err);
513ffbafc53Scomay 
514ffbafc53Scomay 	return (fetch_alloc_prop(root, propname, propval));
515a1be23daSdp }
516a1be23daSdp 
517a1be23daSdp static int
518108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
519108322fbScarlsonj     const char *propval)
520a1be23daSdp {
521a1be23daSdp 	int err;
522a1be23daSdp 	xmlNodePtr root;
523a1be23daSdp 
524a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
525a1be23daSdp 		return (err);
526a1be23daSdp 
5270209230bSgjelinek 	/*
5280209230bSgjelinek 	 * If we get a null propval remove the property (ignore return since it
5290209230bSgjelinek 	 * may not be set to begin with).
5300209230bSgjelinek 	 */
5310209230bSgjelinek 	if (propval == NULL) {
5320209230bSgjelinek 		(void) xmlUnsetProp(root, propname);
5330209230bSgjelinek 	} else {
5340209230bSgjelinek 		if (xmlSetProp(root, propname, (const xmlChar *) propval)
5350209230bSgjelinek 		    == NULL)
536a1be23daSdp 			return (Z_INVAL);
5370209230bSgjelinek 	}
5387c478bd9Sstevel@tonic-gate 	return (Z_OK);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate 
541087719fdSdp static void
542087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
543087719fdSdp {
544087719fdSdp 	xmlNodePtr node;
545087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
546087719fdSdp 
547087719fdSdp 	if (node != NULL)
548087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
549087719fdSdp }
550087719fdSdp 
551087719fdSdp static void
552087719fdSdp stripcomments(zone_dochandle_t handle)
553087719fdSdp {
554087719fdSdp 	xmlDocPtr top;
555087719fdSdp 	xmlNodePtr child, next;
556087719fdSdp 
557087719fdSdp 	top = handle->zone_dh_doc;
558087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
559087719fdSdp 		next = child->next;
560087719fdSdp 		if (child->name == NULL)
561087719fdSdp 			continue;
562087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
563087719fdSdp 			next = child->next;
564087719fdSdp 			xmlUnlinkNode(child);
565087719fdSdp 			xmlFreeNode(child);
566087719fdSdp 		}
567087719fdSdp 	}
568087719fdSdp }
569087719fdSdp 
570ee519a1fSgjelinek static void
571ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle)
572ee519a1fSgjelinek {
573ee519a1fSgjelinek 	xmlNodePtr root, child, next;
574ee519a1fSgjelinek 
575ee519a1fSgjelinek 	root = xmlDocGetRootElement(handle->zone_dh_doc);
576ee519a1fSgjelinek 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
577ee519a1fSgjelinek 		next = child->next;
578ee519a1fSgjelinek 		if (child->name == NULL)
579ee519a1fSgjelinek 			continue;
580ee519a1fSgjelinek 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
581ee519a1fSgjelinek 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
582ee519a1fSgjelinek 			next = child->next;
583ee519a1fSgjelinek 			xmlUnlinkNode(child);
584ee519a1fSgjelinek 			xmlFreeNode(child);
585ee519a1fSgjelinek 		}
586ee519a1fSgjelinek 	}
587ee519a1fSgjelinek }
588ee519a1fSgjelinek 
5897c478bd9Sstevel@tonic-gate static int
590108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
591108322fbScarlsonj     zone_dochandle_t handle)
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
5947c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5957c478bd9Sstevel@tonic-gate 	int valid;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
5987c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5999acbbeafSnn35248 
6007c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
6017c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
6027c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
6037c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
6047c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
6077c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
6087c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
6097c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
6107c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
6117c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
6127c478bd9Sstevel@tonic-gate 	if (valid == 0)
6137c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
614087719fdSdp 
6157c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
616087719fdSdp 	stripcomments(handle);
6177c478bd9Sstevel@tonic-gate 	return (Z_OK);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate int
621108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6247c478bd9Sstevel@tonic-gate 
625108322fbScarlsonj 	if (!config_file_path(zonename, path))
626108322fbScarlsonj 		return (Z_MISC_FS);
627087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
628087719fdSdp 
6297c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate int
633ee519a1fSgjelinek zonecfg_get_attach_handle(const char *path, const char *zonename,
634ee519a1fSgjelinek     boolean_t preserve_sw, zone_dochandle_t handle)
635ee519a1fSgjelinek {
636ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
637ee519a1fSgjelinek 	int		err;
638ee519a1fSgjelinek 	struct stat	buf;
639ee519a1fSgjelinek 
640ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
641ee519a1fSgjelinek 	    sizeof (migpath))
642ee519a1fSgjelinek 		return (Z_NOMEM);
643ee519a1fSgjelinek 
644ee519a1fSgjelinek 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
645ee519a1fSgjelinek 		return (Z_NO_ZONE);
646ee519a1fSgjelinek 
647ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
648ee519a1fSgjelinek 	    sizeof (migpath))
649ee519a1fSgjelinek 		return (Z_NOMEM);
650ee519a1fSgjelinek 
651ee519a1fSgjelinek 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
652ee519a1fSgjelinek 		return (err);
653ee519a1fSgjelinek 
654ee519a1fSgjelinek 	if (!preserve_sw)
655ee519a1fSgjelinek 		strip_sw_inv(handle);
656ee519a1fSgjelinek 
657ee519a1fSgjelinek 	handle->zone_dh_newzone = B_TRUE;
658ee519a1fSgjelinek 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
659ee519a1fSgjelinek 		return (err);
660ee519a1fSgjelinek 
661ee519a1fSgjelinek 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
662ee519a1fSgjelinek }
663ee519a1fSgjelinek 
664ee519a1fSgjelinek int
665108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6687c478bd9Sstevel@tonic-gate 
669108322fbScarlsonj 	if (!snap_file_path(zonename, path))
670108322fbScarlsonj 		return (Z_MISC_FS);
671087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
6727c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate int
676108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
677087719fdSdp     zone_dochandle_t handle)
678087719fdSdp {
679087719fdSdp 	char path[MAXPATHLEN];
680087719fdSdp 	int err;
681087719fdSdp 
682108322fbScarlsonj 	if (!config_file_path(template, path))
683108322fbScarlsonj 		return (Z_MISC_FS);
684087719fdSdp 
685087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
686087719fdSdp 		return (err);
687087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
688087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
689087719fdSdp }
690087719fdSdp 
6919acbbeafSnn35248 int
6929acbbeafSnn35248 zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle)
6939acbbeafSnn35248 {
6949acbbeafSnn35248 	struct stat buf;
6959acbbeafSnn35248 	int err;
6969acbbeafSnn35248 
6979acbbeafSnn35248 	if (stat(path, &buf) == -1)
6989acbbeafSnn35248 		return (Z_MISC_FS);
6999acbbeafSnn35248 
7009acbbeafSnn35248 	if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK)
7019acbbeafSnn35248 		return (err);
7029acbbeafSnn35248 	handle->zone_dh_newzone = B_TRUE;
7039acbbeafSnn35248 	return (Z_OK);
7049acbbeafSnn35248 }
7059acbbeafSnn35248 
7068cd327d5Sgjelinek /*
7078cd327d5Sgjelinek  * Initialize two handles from the manifest read on fd.  The rem_handle
7088cd327d5Sgjelinek  * is initialized from the input file, including the sw inventory.  The
7098cd327d5Sgjelinek  * local_handle is initialized with the same zone configuration but with
7108cd327d5Sgjelinek  * no sw inventory.
7118cd327d5Sgjelinek  */
7128cd327d5Sgjelinek int
7138cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
7148cd327d5Sgjelinek     zone_dochandle_t rem_handle)
7158cd327d5Sgjelinek {
7168cd327d5Sgjelinek 	xmlValidCtxtPtr cvp;
7178cd327d5Sgjelinek 	int valid;
7188cd327d5Sgjelinek 
7198cd327d5Sgjelinek 	/* load the manifest into the handle for the remote system */
7208cd327d5Sgjelinek 	if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
7218cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7228cd327d5Sgjelinek 	}
7238cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7248cd327d5Sgjelinek 		return (Z_NOMEM);
7258cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7268cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7278cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc);
7288cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7298cd327d5Sgjelinek 	if (valid == 0)
7308cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7318cd327d5Sgjelinek 
7328cd327d5Sgjelinek 	/* delete any comments such as inherited Sun copyright / ident str */
7338cd327d5Sgjelinek 	stripcomments(rem_handle);
7348cd327d5Sgjelinek 
7358cd327d5Sgjelinek 	rem_handle->zone_dh_newzone = B_TRUE;
7368cd327d5Sgjelinek 	rem_handle->zone_dh_sw_inv = B_TRUE;
7378cd327d5Sgjelinek 
7388cd327d5Sgjelinek 	/*
7398cd327d5Sgjelinek 	 * Now use the remote system handle to generate a local system handle
7408cd327d5Sgjelinek 	 * with an identical zones configuration but no sw inventory.
7418cd327d5Sgjelinek 	 */
7428cd327d5Sgjelinek 	if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
7438cd327d5Sgjelinek 	    1)) == NULL) {
7448cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7458cd327d5Sgjelinek 	}
7468cd327d5Sgjelinek 
7478cd327d5Sgjelinek 	/*
7488cd327d5Sgjelinek 	 * We need to re-run xmlValidateDocument on local_handle to properly
7498cd327d5Sgjelinek 	 * update the in-core representation of the configuration.
7508cd327d5Sgjelinek 	 */
7518cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7528cd327d5Sgjelinek 		return (Z_NOMEM);
7538cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7548cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7558cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
7568cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7578cd327d5Sgjelinek 	if (valid == 0)
7588cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7598cd327d5Sgjelinek 
7608cd327d5Sgjelinek 	strip_sw_inv(local_handle);
7618cd327d5Sgjelinek 
7628cd327d5Sgjelinek 	local_handle->zone_dh_newzone = B_TRUE;
7638cd327d5Sgjelinek 	local_handle->zone_dh_sw_inv = B_FALSE;
7648cd327d5Sgjelinek 
7658cd327d5Sgjelinek 	return (Z_OK);
7668cd327d5Sgjelinek }
7678cd327d5Sgjelinek 
768087719fdSdp static boolean_t
769087719fdSdp is_renaming(zone_dochandle_t handle)
770087719fdSdp {
771087719fdSdp 	if (handle->zone_dh_newzone)
772087719fdSdp 		return (B_FALSE);
773087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
774087719fdSdp 		return (B_TRUE);
775087719fdSdp 	return (B_FALSE);
776087719fdSdp }
777087719fdSdp 
778087719fdSdp static boolean_t
779087719fdSdp is_new(zone_dochandle_t handle)
780087719fdSdp {
781087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
782087719fdSdp }
783087719fdSdp 
784087719fdSdp static boolean_t
785087719fdSdp is_snapshot(zone_dochandle_t handle)
786087719fdSdp {
787087719fdSdp 	return (handle->zone_dh_snapshot);
788087719fdSdp }
789087719fdSdp 
790087719fdSdp /*
791087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
792087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
793087719fdSdp  * safety if this routine had to change the app locale on the fly.
794087719fdSdp  */
795087719fdSdp int
796108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
797087719fdSdp {
798087719fdSdp 	int i;
799087719fdSdp 
800087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
801087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
802087719fdSdp 
803087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
804087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
805087719fdSdp 
806087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
807087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
808087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
809087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
810087719fdSdp 
811087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
812087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
813087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
814087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
815087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
816087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
817087719fdSdp 	}
818087719fdSdp 
819087719fdSdp 	return (Z_OK);
820087719fdSdp }
821087719fdSdp 
822087719fdSdp /*
823087719fdSdp  * Changing the zone name requires us to track both the old and new
824087719fdSdp  * name of the zone until commit time.
825087719fdSdp  */
826087719fdSdp int
8277c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
8287c478bd9Sstevel@tonic-gate {
829a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
832a1be23daSdp int
833a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
834a1be23daSdp {
835087719fdSdp 	zone_state_t state;
836087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
837087719fdSdp 	int err;
838087719fdSdp 
839087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
840087719fdSdp 	    sizeof (curname))) != Z_OK)
841087719fdSdp 		return (err);
842087719fdSdp 
843087719fdSdp 	if (strcmp(name, curname) == 0)
844087719fdSdp 		return (Z_OK);
845087719fdSdp 
846087719fdSdp 	/*
847087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
848087719fdSdp 	 */
849087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
850087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
851087719fdSdp 
852087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
853087719fdSdp 		return (err);
854087719fdSdp 
855087719fdSdp 	/*
856087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
857087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
858087719fdSdp 	 * name as "in use," so special case here.
859087719fdSdp 	 */
860087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
861087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
862087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
863087719fdSdp 		return (err);
864087719fdSdp 	}
865087719fdSdp 
866087719fdSdp 	/* Check to see if new name chosen is already in use */
867087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
868087719fdSdp 		return (Z_NAME_IN_USE);
869087719fdSdp 
870087719fdSdp 	/*
871087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
872087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
873087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
874087719fdSdp 	 */
875087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
876087719fdSdp 	    sizeof (old_delname));
877087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
878087719fdSdp 		/*
879087719fdSdp 		 * Name change is allowed only when the zone we're altering
880087719fdSdp 		 * is not ready or running.
881087719fdSdp 		 */
882087719fdSdp 		err = zone_get_state(curname, &state);
883087719fdSdp 		if (err == Z_OK) {
884087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
885087719fdSdp 				return (Z_BAD_ZONE_STATE);
886087719fdSdp 		} else if (err != Z_NO_ZONE) {
887087719fdSdp 			return (err);
888087719fdSdp 		}
889087719fdSdp 
890087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
891087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
892087719fdSdp 		assert(is_renaming(handle));
893087719fdSdp 	} else if (is_renaming(handle)) {
894087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
895087719fdSdp 		if (err == Z_OK) {
896087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
897087719fdSdp 				return (Z_BAD_ZONE_STATE);
898087719fdSdp 		} else if (err != Z_NO_ZONE) {
899087719fdSdp 			return (err);
900087719fdSdp 		}
901087719fdSdp 	}
902087719fdSdp 
903087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
904087719fdSdp 		/*
905087719fdSdp 		 * Restore the deletename to whatever it was at the
906087719fdSdp 		 * top of the routine, since we've had a failure.
907087719fdSdp 		 */
908087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
909087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
910087719fdSdp 		return (err);
911087719fdSdp 	}
912087719fdSdp 
913087719fdSdp 	return (Z_OK);
9147c478bd9Sstevel@tonic-gate }
915a1be23daSdp 
916a1be23daSdp int
917a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
918a1be23daSdp {
919108322fbScarlsonj 	size_t len;
920108322fbScarlsonj 
921108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
922108322fbScarlsonj 		return (Z_TOO_BIG);
923108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
924108322fbScarlsonj 	    pathsize - len));
925a1be23daSdp }
926a1be23daSdp 
927a1be23daSdp int
928a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
929a1be23daSdp {
930555afedfScarlsonj 	size_t len;
931555afedfScarlsonj 
932555afedfScarlsonj 	/*
933555afedfScarlsonj 	 * The user deals in absolute paths in the running global zone, but the
934555afedfScarlsonj 	 * internal configuration files deal with boot environment relative
935555afedfScarlsonj 	 * paths.  Strip out the alternate root when specified.
936555afedfScarlsonj 	 */
937555afedfScarlsonj 	len = strlen(zonecfg_root);
938555afedfScarlsonj 	if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
939555afedfScarlsonj 		return (Z_BAD_PROPERTY);
940555afedfScarlsonj 	zonepath += len;
941a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
942a1be23daSdp }
943a1be23daSdp 
944a1be23daSdp int
9459acbbeafSnn35248 zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
9469acbbeafSnn35248 {
9479acbbeafSnn35248 	int ret, sz;
9489acbbeafSnn35248 
9499acbbeafSnn35248 	ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
9509acbbeafSnn35248 
9519acbbeafSnn35248 	/* If the zone has no brand, it is native. */
9529acbbeafSnn35248 	if (ret == Z_OK && brand[0] == '\0') {
9539acbbeafSnn35248 		sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
9549acbbeafSnn35248 		if (sz >= brandsize)
9559acbbeafSnn35248 			ret = Z_TOO_BIG;
9569acbbeafSnn35248 		else
9579acbbeafSnn35248 			ret = Z_OK;
9589acbbeafSnn35248 	}
9599acbbeafSnn35248 
9609acbbeafSnn35248 	return (ret);
9619acbbeafSnn35248 }
9629acbbeafSnn35248 
9639acbbeafSnn35248 int
9649acbbeafSnn35248 zonecfg_set_brand(zone_dochandle_t handle, char *brand)
9659acbbeafSnn35248 {
9669acbbeafSnn35248 	return (setrootattr(handle, DTD_ATTR_BRAND, brand));
9679acbbeafSnn35248 }
9689acbbeafSnn35248 
9699acbbeafSnn35248 int
970a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
971a1be23daSdp {
972a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
973a1be23daSdp 	int ret;
974a1be23daSdp 
975a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
976a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
977a1be23daSdp 		return (ret);
978a1be23daSdp 
979a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
980a1be23daSdp 		*autoboot = B_TRUE;
981a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
982a1be23daSdp 		*autoboot = B_FALSE;
983a1be23daSdp 	else
984a1be23daSdp 		ret = Z_BAD_PROPERTY;
985a1be23daSdp 	return (ret);
986a1be23daSdp }
987a1be23daSdp 
988a1be23daSdp int
989a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
990a1be23daSdp {
991a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
992a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
993a1be23daSdp }
994a1be23daSdp 
995a1be23daSdp int
996a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
997a1be23daSdp {
998a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
999a1be23daSdp }
1000a1be23daSdp 
1001a1be23daSdp int
1002a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
1003a1be23daSdp {
1004a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
1005a1be23daSdp }
1006a1be23daSdp 
1007ffbafc53Scomay int
1008ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
1009ffbafc53Scomay {
1010ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
1011ffbafc53Scomay }
1012ffbafc53Scomay 
1013ffbafc53Scomay int
10143f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
1015ffbafc53Scomay {
10163f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
10173f2f09c1Sdp }
10183f2f09c1Sdp 
10193f2f09c1Sdp int
10203f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
10213f2f09c1Sdp {
10223f2f09c1Sdp 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
10233f2f09c1Sdp }
10243f2f09c1Sdp 
10253f2f09c1Sdp int
10263f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
10273f2f09c1Sdp {
10283f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
1029ffbafc53Scomay }
1030ffbafc53Scomay 
10310209230bSgjelinek int
10320209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
10330209230bSgjelinek {
10340209230bSgjelinek 	return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
10350209230bSgjelinek }
10360209230bSgjelinek 
10370209230bSgjelinek int
10380209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched)
10390209230bSgjelinek {
10400209230bSgjelinek 	return (setrootattr(handle, DTD_ATTR_SCHED, sched));
10410209230bSgjelinek }
10420209230bSgjelinek 
1043a1be23daSdp /*
1044a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
1045a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
1046a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
1047a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
1048087719fdSdp  *
1049087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
1050087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
1051087719fdSdp  * In general, the operation of this function should succeed or fail as
1052087719fdSdp  * a unit.
1053a1be23daSdp  */
1054a1be23daSdp int
1055a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
1056a1be23daSdp {
1057a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
1058a1be23daSdp 	struct zoneent ze;
1059a1be23daSdp 	int err;
1060087719fdSdp 	int opcode;
1061087719fdSdp 	char *zn;
1062087719fdSdp 
1063087719fdSdp 	bzero(&ze, sizeof (ze));
1064087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
1065a1be23daSdp 
1066a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
1067a1be23daSdp 		return (err);
1068087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
1069087719fdSdp 
1070a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
1071a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
1072a1be23daSdp 		return (err);
1073555afedfScarlsonj 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
1074555afedfScarlsonj 	    sizeof (ze.zone_path));
1075087719fdSdp 
1076087719fdSdp 	if (is_renaming(handle)) {
1077087719fdSdp 		opcode = PZE_MODIFY;
1078087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
1079087719fdSdp 		    sizeof (ze.zone_name));
1080087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
1081087719fdSdp 	} else if (is_new(handle)) {
1082087719fdSdp 		FILE *cookie;
1083087719fdSdp 		/*
1084087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
1085087719fdSdp 		 * since we might be forcibly overwriting an existing
1086087719fdSdp 		 * configuration with a new one (for example 'create -F'
1087087719fdSdp 		 * in zonecfg).
1088087719fdSdp 		 */
1089087719fdSdp 		opcode = PZE_ADD;
1090087719fdSdp 		cookie = setzoneent();
1091087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
1092087719fdSdp 			if (strcmp(zn, name) == 0) {
1093087719fdSdp 				opcode = PZE_MODIFY;
1094087719fdSdp 				free(zn);
1095087719fdSdp 				break;
1096087719fdSdp 			}
1097087719fdSdp 			free(zn);
1098087719fdSdp 		}
1099087719fdSdp 		endzoneent(cookie);
1100087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
1101087719fdSdp 	} else {
1102087719fdSdp 		opcode = PZE_MODIFY;
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate 
1105087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
1106087719fdSdp 		return (err);
1107087719fdSdp 
1108087719fdSdp 	return (Z_OK);
1109087719fdSdp }
1110087719fdSdp 
1111087719fdSdp /*
1112087719fdSdp  * The goal of this routine is to cause the index file update and the
1113087719fdSdp  * document save to happen as an atomic operation.  We do the document
1114087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
1115087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
1116087719fdSdp  *
1117087719fdSdp  * Strategy:
1118087719fdSdp  *
1119087719fdSdp  * New zone 'foo' configuration:
1120087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1121087719fdSdp  * 	Write XML to tmpfile
1122087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1123087719fdSdp  * 	Add entry to index file
1124087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
1125087719fdSdp  *
1126087719fdSdp  * Save existing zone 'foo':
1127087719fdSdp  * 	Make backup of foo.xml -> .backup
1128087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1129087719fdSdp  * 	Write XML to tmpfile
1130087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1131087719fdSdp  * 	Modify index file as needed
1132087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
1133087719fdSdp  *
1134087719fdSdp  * Rename 'foo' to 'bar':
1135087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1136087719fdSdp  * 	Write XML to tmpfile
1137087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1138087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1139087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
1140087719fdSdp  */
11417c478bd9Sstevel@tonic-gate static int
11427c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
1145087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
11469acbbeafSnn35248 	int tmpfd, err, valid;
11477c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
1148087719fdSdp 	boolean_t backup;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
11517c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
11527c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
11557c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
11567c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
11577c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
11627c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
11637c478bd9Sstevel@tonic-gate 
1164087719fdSdp 	/*
11659acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
11669acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
11679acbbeafSnn35248 	 * assert() that the doc is valid.
1168087719fdSdp 	 */
11699acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
11709acbbeafSnn35248 	assert(valid != 0);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
11737c478bd9Sstevel@tonic-gate 		goto err;
1174087719fdSdp 
11757c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
11767c478bd9Sstevel@tonic-gate 
1177087719fdSdp 	/*
1178087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
1179087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
1180087719fdSdp 	 * something goes wrong.
1181087719fdSdp 	 */
1182087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
1183087719fdSdp 		backup = B_TRUE;
1184087719fdSdp 
1185087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1186087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1187087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1188087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
1189087719fdSdp 
1190087719fdSdp 		if (link(filename, bakfile) == -1) {
1191087719fdSdp 			err = errno;
11927c478bd9Sstevel@tonic-gate 			(void) unlink(tmpfile);
11937c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
11947c478bd9Sstevel@tonic-gate 				return (Z_ACCES);
11957c478bd9Sstevel@tonic-gate 			return (Z_MISC_FS);
11967c478bd9Sstevel@tonic-gate 		}
1197087719fdSdp 	}
1198a1be23daSdp 
1199087719fdSdp 	/*
1200087719fdSdp 	 * Move the new document over top of the old.
1201087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1202087719fdSdp 	 */
1203087719fdSdp 	if (rename(tmpfile, filename) == -1) {
1204087719fdSdp 		err = errno;
1205087719fdSdp 		(void) unlink(tmpfile);
1206087719fdSdp 		if (backup)
1207087719fdSdp 			(void) unlink(bakfile);
1208087719fdSdp 		if (err == EACCES)
1209087719fdSdp 			return (Z_ACCES);
1210087719fdSdp 		return (Z_MISC_FS);
1211087719fdSdp 	}
1212087719fdSdp 
1213087719fdSdp 	/*
1214087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
1215087719fdSdp 	 */
1216087719fdSdp 	if (is_snapshot(handle))
1217087719fdSdp 		return (Z_OK);
1218087719fdSdp 
1219087719fdSdp 	/* now update the index file to reflect whatever we just did */
1220087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1221087719fdSdp 		if (backup) {
1222087719fdSdp 			/*
1223087719fdSdp 			 * Try to restore from our backup.
1224087719fdSdp 			 */
1225087719fdSdp 			(void) unlink(filename);
1226087719fdSdp 			(void) rename(bakfile, filename);
1227087719fdSdp 		} else {
1228087719fdSdp 			/*
1229087719fdSdp 			 * Either the zone is new, in which case we can delete
1230087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1231087719fdSdp 			 */
1232087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1233087719fdSdp 			(void) unlink(filename);
1234087719fdSdp 		}
1235087719fdSdp 		return (Z_UPDATING_INDEX);
1236087719fdSdp 	}
1237087719fdSdp 
1238087719fdSdp 	if (backup)
1239087719fdSdp 		(void) unlink(bakfile);
1240087719fdSdp 
1241087719fdSdp 	return (Z_OK);
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate err:
12447c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
12457c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate int
12497c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
12507c478bd9Sstevel@tonic-gate {
1251087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1252087719fdSdp 	char delpath[MAXPATHLEN];
1253087719fdSdp 	int err = Z_SAVING_FILE;
12547c478bd9Sstevel@tonic-gate 
1255087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1256087719fdSdp 		return (Z_BAD_HANDLE);
1257087719fdSdp 
1258087719fdSdp 	/*
1259ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1260ee519a1fSgjelinek 	 * inventory at this time.
1261087719fdSdp 	 */
1262ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1263087719fdSdp 		return (Z_INVAL);
1264087719fdSdp 
1265087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
12667c478bd9Sstevel@tonic-gate 		return (err);
1267087719fdSdp 
1268108322fbScarlsonj 	if (!config_file_path(zname, path))
1269108322fbScarlsonj 		return (Z_MISC_FS);
1270087719fdSdp 
1271087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1272087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1273087719fdSdp 
1274087719fdSdp 	err = zonecfg_save_impl(handle, path);
1275087719fdSdp 
1276087719fdSdp 	stripcomments(handle);
1277087719fdSdp 
1278087719fdSdp 	if (err != Z_OK)
1279087719fdSdp 		return (err);
1280087719fdSdp 
1281087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1282087719fdSdp 
1283087719fdSdp 	if (is_renaming(handle)) {
1284108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1285087719fdSdp 			(void) unlink(delpath);
1286087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1287087719fdSdp 	}
1288087719fdSdp 
1289087719fdSdp 	return (Z_OK);
12907c478bd9Sstevel@tonic-gate }
12917c478bd9Sstevel@tonic-gate 
1292ee519a1fSgjelinek int
12939acbbeafSnn35248 zonecfg_verify_save(zone_dochandle_t handle, char *filename)
12949acbbeafSnn35248 {
12959acbbeafSnn35248 	int valid;
12969acbbeafSnn35248 
12979acbbeafSnn35248 	xmlValidCtxt cvp = { NULL };
12989acbbeafSnn35248 
12999acbbeafSnn35248 	if (zonecfg_check_handle(handle) != Z_OK)
13009acbbeafSnn35248 		return (Z_BAD_HANDLE);
13019acbbeafSnn35248 
13029acbbeafSnn35248 	cvp.error = zonecfg_error_func;
13039acbbeafSnn35248 	cvp.warning = zonecfg_error_func;
13049acbbeafSnn35248 
13059acbbeafSnn35248 	/*
13069acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
13079acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
13089acbbeafSnn35248 	 * assert() that the doc is valid.
13099acbbeafSnn35248 	 */
13109acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13119acbbeafSnn35248 	assert(valid != 0);
13129acbbeafSnn35248 
13139acbbeafSnn35248 	if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
13149acbbeafSnn35248 		return (Z_SAVING_FILE);
13159acbbeafSnn35248 
13169acbbeafSnn35248 	return (Z_OK);
13179acbbeafSnn35248 }
13189acbbeafSnn35248 
13199acbbeafSnn35248 int
13208cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1321ee519a1fSgjelinek {
1322ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1323ee519a1fSgjelinek 	char path[MAXPATHLEN];
1324ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1325ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1326ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
13279acbbeafSnn35248 	int valid;
1328ee519a1fSgjelinek 
1329ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1330ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1331ee519a1fSgjelinek 
1332ee519a1fSgjelinek 	/*
1333ee519a1fSgjelinek 	 * We can only detach if we have taken a sw inventory.
1334ee519a1fSgjelinek 	 */
1335ee519a1fSgjelinek 	if (!handle->zone_dh_sw_inv)
1336ee519a1fSgjelinek 		return (Z_INVAL);
1337ee519a1fSgjelinek 
13388cd327d5Sgjelinek 	if (flags & ZONE_DRY_RUN) {
13398cd327d5Sgjelinek 		(void) strlcpy(migpath, "-", sizeof (migpath));
13408cd327d5Sgjelinek 	} else {
13418cd327d5Sgjelinek 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
13428cd327d5Sgjelinek 		    != Z_OK)
1343ee519a1fSgjelinek 			return (err);
1344ee519a1fSgjelinek 
13458cd327d5Sgjelinek 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
13468cd327d5Sgjelinek 		    != Z_OK)
1347ee519a1fSgjelinek 			return (err);
1348ee519a1fSgjelinek 
13498cd327d5Sgjelinek 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED)
13508cd327d5Sgjelinek 		    >= sizeof (migpath))
1351ee519a1fSgjelinek 			return (Z_NOMEM);
13528cd327d5Sgjelinek 	}
1353ee519a1fSgjelinek 
1354ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1355ee519a1fSgjelinek 		return (err);
1356ee519a1fSgjelinek 
1357ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1358ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1359ee519a1fSgjelinek 
1360ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1361ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1362ee519a1fSgjelinek 
1363ee519a1fSgjelinek 	/*
13649acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
13659acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
13669acbbeafSnn35248 	 * assert() that the doc is valid.
1367ee519a1fSgjelinek 	 */
13689acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13699acbbeafSnn35248 	assert(valid != 0);
1370ee519a1fSgjelinek 
1371ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1372ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1373ee519a1fSgjelinek 
13748cd327d5Sgjelinek 	if (!(flags & ZONE_DRY_RUN))
1375ee519a1fSgjelinek 		(void) chmod(migpath, 0644);
1376ee519a1fSgjelinek 
1377ee519a1fSgjelinek 	stripcomments(handle);
1378ee519a1fSgjelinek 
1379ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1380ee519a1fSgjelinek 
1381ee519a1fSgjelinek 	return (Z_OK);
1382ee519a1fSgjelinek }
1383ee519a1fSgjelinek 
1384ee519a1fSgjelinek boolean_t
1385ee519a1fSgjelinek zonecfg_detached(const char *path)
1386ee519a1fSgjelinek {
1387ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1388ee519a1fSgjelinek 	struct stat	buf;
1389ee519a1fSgjelinek 
1390ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
1391ee519a1fSgjelinek 	    sizeof (migpath))
1392ee519a1fSgjelinek 		return (B_FALSE);
1393ee519a1fSgjelinek 
1394ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1395ee519a1fSgjelinek 		return (B_TRUE);
1396ee519a1fSgjelinek 
1397ee519a1fSgjelinek 	return (B_FALSE);
1398ee519a1fSgjelinek }
1399ee519a1fSgjelinek 
1400ee519a1fSgjelinek void
1401ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1402ee519a1fSgjelinek {
1403ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1404ee519a1fSgjelinek 	char path[MAXPATHLEN];
1405ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1406ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1407ee519a1fSgjelinek 
1408ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1409ee519a1fSgjelinek 		return;
1410ee519a1fSgjelinek 
1411ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1412ee519a1fSgjelinek 		return;
1413ee519a1fSgjelinek 
1414ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1415ee519a1fSgjelinek 		return;
1416ee519a1fSgjelinek 
1417ee519a1fSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path, DETACHED);
1418ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1419ee519a1fSgjelinek 	    ATTACH_FORCED);
1420ee519a1fSgjelinek 
1421ee519a1fSgjelinek 	if (forced) {
1422ee519a1fSgjelinek 		(void) rename(detached, attached);
1423ee519a1fSgjelinek 	} else {
1424ee519a1fSgjelinek 		(void) unlink(attached);
1425ee519a1fSgjelinek 		(void) unlink(detached);
1426ee519a1fSgjelinek 	}
1427ee519a1fSgjelinek }
1428ee519a1fSgjelinek 
14297c478bd9Sstevel@tonic-gate /*
14307c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
14317c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
14327c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
14337c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
14347c478bd9Sstevel@tonic-gate  */
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate int
14377c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
14387c478bd9Sstevel@tonic-gate {
14397c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
14407c478bd9Sstevel@tonic-gate 
1441108322fbScarlsonj 	if (!config_file_path(zonename, path))
1442108322fbScarlsonj 		return (Z_INVAL);
14437c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
14447c478bd9Sstevel@tonic-gate 		return (Z_OK);
1445108322fbScarlsonj 	if (errno == ENOENT) {
1446108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1447108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1448108322fbScarlsonj 			return (Z_INVAL);
1449108322fbScarlsonj 		if (access(path, amode) == 0)
14507c478bd9Sstevel@tonic-gate 			return (Z_OK);
1451108322fbScarlsonj 	}
14527c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
14537c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
14547c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
14557c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14567c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate int
1460108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
14617c478bd9Sstevel@tonic-gate {
14627c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
14637c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
14647c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
14677c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
14687c478bd9Sstevel@tonic-gate 	}
14697c478bd9Sstevel@tonic-gate 
1470087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1471087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1472087719fdSdp 
14737c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
14747c478bd9Sstevel@tonic-gate 		goto out;
14757c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
14767c478bd9Sstevel@tonic-gate 		goto out;
14777c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
14787c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
14797c478bd9Sstevel@tonic-gate 		goto out;
14807c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
14817c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
14827c478bd9Sstevel@tonic-gate 		goto out;
14837c478bd9Sstevel@tonic-gate 	}
14847c478bd9Sstevel@tonic-gate 	/*
14857c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
14867c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
14877c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
14887c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
14897c478bd9Sstevel@tonic-gate 	 * paths.
14907c478bd9Sstevel@tonic-gate 	 */
14917c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
14927c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
14937c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
14947c478bd9Sstevel@tonic-gate 			goto out;
14957c478bd9Sstevel@tonic-gate 	}
1496108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1497108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1498108322fbScarlsonj 		error = Z_MISC_FS;
1499108322fbScarlsonj 		goto out;
1500108322fbScarlsonj 	}
1501108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
15027c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
15037c478bd9Sstevel@tonic-gate 		goto out;
15047c478bd9Sstevel@tonic-gate 	}
15057c478bd9Sstevel@tonic-gate 
1506108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1507108322fbScarlsonj 		error = Z_MISC_FS;
1508108322fbScarlsonj 		goto out;
1509108322fbScarlsonj 	}
1510087719fdSdp 
1511087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1512087719fdSdp 	    "It is a snapshot of running zone state.\n");
1513087719fdSdp 
15147c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
15157c478bd9Sstevel@tonic-gate 
1516087719fdSdp 	stripcomments(handle);
1517087719fdSdp 
15187c478bd9Sstevel@tonic-gate out:
15197c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
15207c478bd9Sstevel@tonic-gate 	return (error);
15217c478bd9Sstevel@tonic-gate }
15227c478bd9Sstevel@tonic-gate 
1523f4b3ec61Sdh155122 int
1524f4b3ec61Sdh155122 zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep)
1525f4b3ec61Sdh155122 {
1526f4b3ec61Sdh155122 	char property[10]; /* 10 is big enough for "shared"/"exclusive" */
1527f4b3ec61Sdh155122 	int err;
1528f4b3ec61Sdh155122 
1529f4b3ec61Sdh155122 	err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property));
1530f4b3ec61Sdh155122 	if (err == Z_BAD_PROPERTY) {
1531f4b3ec61Sdh155122 		/* Return default value */
1532f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1533f4b3ec61Sdh155122 		return (Z_OK);
1534f4b3ec61Sdh155122 	} else if (err != Z_OK) {
1535f4b3ec61Sdh155122 		return (err);
1536f4b3ec61Sdh155122 	}
1537f4b3ec61Sdh155122 
1538f4b3ec61Sdh155122 	if (strlen(property) == 0 ||
1539f4b3ec61Sdh155122 	    strcmp(property, "shared") == 0)
1540f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1541f4b3ec61Sdh155122 	else if (strcmp(property, "exclusive") == 0)
1542f4b3ec61Sdh155122 		*iptypep = ZS_EXCLUSIVE;
1543f4b3ec61Sdh155122 	else
1544f4b3ec61Sdh155122 		return (Z_INVAL);
1545f4b3ec61Sdh155122 
1546f4b3ec61Sdh155122 	return (Z_OK);
1547f4b3ec61Sdh155122 }
1548f4b3ec61Sdh155122 
1549f4b3ec61Sdh155122 int
1550f4b3ec61Sdh155122 zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype)
1551f4b3ec61Sdh155122 {
1552f4b3ec61Sdh155122 	xmlNodePtr cur;
1553f4b3ec61Sdh155122 
1554f4b3ec61Sdh155122 	if (handle == NULL)
1555f4b3ec61Sdh155122 		return (Z_INVAL);
1556f4b3ec61Sdh155122 
1557f4b3ec61Sdh155122 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
1558f4b3ec61Sdh155122 	if (cur == NULL) {
1559f4b3ec61Sdh155122 		return (Z_EMPTY_DOCUMENT);
1560f4b3ec61Sdh155122 	}
1561f4b3ec61Sdh155122 
1562f4b3ec61Sdh155122 	if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) {
1563f4b3ec61Sdh155122 		return (Z_WRONG_DOC_TYPE);
1564f4b3ec61Sdh155122 	}
1565f4b3ec61Sdh155122 	switch (iptype) {
1566f4b3ec61Sdh155122 	case ZS_SHARED:
1567f4b3ec61Sdh155122 		/*
1568f4b3ec61Sdh155122 		 * Since "shared" is the default, we don't write it to the
1569f4b3ec61Sdh155122 		 * configuration file, so that it's easier to migrate those
1570f4b3ec61Sdh155122 		 * zones elsewhere, eg., to systems which are not IP-Instances
1571f4b3ec61Sdh155122 		 * aware.
1572f4b3ec61Sdh155122 		 * xmlUnsetProp only fails when the attribute doesn't exist,
1573f4b3ec61Sdh155122 		 * which we don't care.
1574f4b3ec61Sdh155122 		 */
1575f4b3ec61Sdh155122 		(void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE);
1576f4b3ec61Sdh155122 		break;
1577f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1578f4b3ec61Sdh155122 		if (xmlSetProp(cur, DTD_ATTR_IPTYPE,
1579f4b3ec61Sdh155122 		    (const xmlChar *) "exclusive") == NULL)
1580f4b3ec61Sdh155122 			return (Z_INVAL);
1581f4b3ec61Sdh155122 		break;
1582f4b3ec61Sdh155122 	}
1583f4b3ec61Sdh155122 	return (Z_OK);
1584f4b3ec61Sdh155122 }
1585f4b3ec61Sdh155122 
15867c478bd9Sstevel@tonic-gate static int
1587a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
15887c478bd9Sstevel@tonic-gate {
15897c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
15927c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
15937c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
15947c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
15957c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate 	return (Z_OK);
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate static int
16017c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
16027c478bd9Sstevel@tonic-gate {
16037c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
16047c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
16057c478bd9Sstevel@tonic-gate 	int err;
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1608a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
16097c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
16107c478bd9Sstevel@tonic-gate 		return (err);
16117c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1612a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
16137c478bd9Sstevel@tonic-gate 		return (err);
1614a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16157c478bd9Sstevel@tonic-gate 		return (err);
1616a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
16177c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
16187c478bd9Sstevel@tonic-gate 		return (err);
16197c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
16207c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
16217c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
16227c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
16237c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1624a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
16257c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
16267c478bd9Sstevel@tonic-gate 				return (err);
16277c478bd9Sstevel@tonic-gate 		}
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate 	return (Z_OK);
16307c478bd9Sstevel@tonic-gate }
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate int
16337c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
16347c478bd9Sstevel@tonic-gate {
16357c478bd9Sstevel@tonic-gate 	int err;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16387c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16417c478bd9Sstevel@tonic-gate 		return (err);
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
16447c478bd9Sstevel@tonic-gate 		return (err);
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	return (Z_OK);
16477c478bd9Sstevel@tonic-gate }
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate static int
16507c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
16517c478bd9Sstevel@tonic-gate {
16527c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
16537c478bd9Sstevel@tonic-gate 	int err;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1656a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16577c478bd9Sstevel@tonic-gate 		return (err);
16587c478bd9Sstevel@tonic-gate 	return (Z_OK);
16597c478bd9Sstevel@tonic-gate }
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate int
16627c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
16637c478bd9Sstevel@tonic-gate {
16647c478bd9Sstevel@tonic-gate 	int err;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16677c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16707c478bd9Sstevel@tonic-gate 		return (err);
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
16737c478bd9Sstevel@tonic-gate 		return (err);
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	return (Z_OK);
16767c478bd9Sstevel@tonic-gate }
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate int
16797c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
16807c478bd9Sstevel@tonic-gate {
16817c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16847c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
16857c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
16867c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
16877c478bd9Sstevel@tonic-gate 	if (new == NULL)
16887c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
16897c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
16907c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
16917c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
16927c478bd9Sstevel@tonic-gate 	if (last == NULL)
16937c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
16947c478bd9Sstevel@tonic-gate 	else
16957c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
16967c478bd9Sstevel@tonic-gate 	return (Z_OK);
16977c478bd9Sstevel@tonic-gate }
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate int
17007c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
17017c478bd9Sstevel@tonic-gate {
17027c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
17057c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
17067c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
17077c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
17087c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
17097c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
17107c478bd9Sstevel@tonic-gate 			else
17117c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
17127c478bd9Sstevel@tonic-gate 			free(this);
17137c478bd9Sstevel@tonic-gate 			return (Z_OK);
17147c478bd9Sstevel@tonic-gate 		} else
17157c478bd9Sstevel@tonic-gate 			last = this;
17167c478bd9Sstevel@tonic-gate 	}
17177c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
17187c478bd9Sstevel@tonic-gate }
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate void
17217c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
17227c478bd9Sstevel@tonic-gate {
17237c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
17267c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
17277c478bd9Sstevel@tonic-gate 		free(this);
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate }
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate void
17327c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
17337c478bd9Sstevel@tonic-gate {
17347c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
17357c478bd9Sstevel@tonic-gate 		return;
17367c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
17377c478bd9Sstevel@tonic-gate 	free(valtab);
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate static boolean_t
17417c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
17427c478bd9Sstevel@tonic-gate {
17437c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
17447c478bd9Sstevel@tonic-gate 	int prop_result;
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
17477c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
17487c478bd9Sstevel@tonic-gate 		return (B_FALSE);
17497c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
17507c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
17517c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
17527c478bd9Sstevel@tonic-gate }
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate static int
17557c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
17567c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
17577c478bd9Sstevel@tonic-gate {
17587c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17597c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17627c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
17637c478bd9Sstevel@tonic-gate 			continue;
17647c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
17657c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
17667c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
17677c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
17687c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
17697c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
17707c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
17717c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
17727c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17737c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17747c478bd9Sstevel@tonic-gate 			return (Z_OK);
17757c478bd9Sstevel@tonic-gate 		}
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
17787c478bd9Sstevel@tonic-gate }
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate int
17817c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
17827c478bd9Sstevel@tonic-gate {
17837c478bd9Sstevel@tonic-gate 	int err;
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17867c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17897c478bd9Sstevel@tonic-gate 		return (err);
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
17927c478bd9Sstevel@tonic-gate 		return (err);
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	return (Z_OK);
17957c478bd9Sstevel@tonic-gate }
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate int
17987c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
17997c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
18007c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
18017c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
18027c478bd9Sstevel@tonic-gate {
18037c478bd9Sstevel@tonic-gate 	int err;
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18067c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18097c478bd9Sstevel@tonic-gate 		return (err);
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
18127c478bd9Sstevel@tonic-gate 		return (err);
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
18157c478bd9Sstevel@tonic-gate 		return (err);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	return (Z_OK);
18187c478bd9Sstevel@tonic-gate }
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate static int
18217c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18267c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
18277c478bd9Sstevel@tonic-gate 			continue;
18287c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
18297c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
18307c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
18317c478bd9Sstevel@tonic-gate 			return (Z_OK);
18327c478bd9Sstevel@tonic-gate 		}
18337c478bd9Sstevel@tonic-gate 	}
18347c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate int
18387c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
18397c478bd9Sstevel@tonic-gate {
18407c478bd9Sstevel@tonic-gate 	int err;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18437c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18467c478bd9Sstevel@tonic-gate 		return (err);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
18497c478bd9Sstevel@tonic-gate 		return (err);
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	return (Z_OK);
18527c478bd9Sstevel@tonic-gate }
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate int
18557c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
18567c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
18577c478bd9Sstevel@tonic-gate {
18587c478bd9Sstevel@tonic-gate 	int err;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18617c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18647c478bd9Sstevel@tonic-gate 		return (err);
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
18677c478bd9Sstevel@tonic-gate 		return (err);
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
18707c478bd9Sstevel@tonic-gate 		return (err);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	return (Z_OK);
18737c478bd9Sstevel@tonic-gate }
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate int
18767c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
18777c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
18787c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
18797c478bd9Sstevel@tonic-gate {
18807c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
18817c478bd9Sstevel@tonic-gate 	int err;
18827c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
18837c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
18847c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18877c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18907c478bd9Sstevel@tonic-gate 		return (err);
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 	/*
18937c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
18947c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
18957c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
18967c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
18977c478bd9Sstevel@tonic-gate 	 */
18987c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
18997c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
19007c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
19017c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
19027c478bd9Sstevel@tonic-gate 			continue;
19037c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
19047c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
19057c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
19067c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
19077c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
19087c478bd9Sstevel@tonic-gate 					firstmatch = cur;
19097c478bd9Sstevel@tonic-gate 				else
19107c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
19117c478bd9Sstevel@tonic-gate 			}
19127c478bd9Sstevel@tonic-gate 		}
19137c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
19147c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
19157c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
19167c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
19177c478bd9Sstevel@tonic-gate 				    special) == 0) {
19187c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19197c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19207c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19217c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19227c478bd9Sstevel@tonic-gate 				} else {
19237c478bd9Sstevel@tonic-gate 					/*
19247c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19257c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19267c478bd9Sstevel@tonic-gate 					 */
19277c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19287c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19297c478bd9Sstevel@tonic-gate 				}
19307c478bd9Sstevel@tonic-gate 			}
19317c478bd9Sstevel@tonic-gate 		}
19327c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
19337c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
19347c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
19357c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
19367c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19377c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19387c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19397c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19407c478bd9Sstevel@tonic-gate 				} else {
19417c478bd9Sstevel@tonic-gate 					/*
19427c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19437c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19447c478bd9Sstevel@tonic-gate 					 */
19457c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19467c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19477c478bd9Sstevel@tonic-gate 				}
19487c478bd9Sstevel@tonic-gate 			}
19497c478bd9Sstevel@tonic-gate 		}
19507c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
19517c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
19527c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
19537c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
19547c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19557c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19567c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19577c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19587c478bd9Sstevel@tonic-gate 				} else {
19597c478bd9Sstevel@tonic-gate 					/*
19607c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19617c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19627c478bd9Sstevel@tonic-gate 					 */
19637c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19647c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19657c478bd9Sstevel@tonic-gate 				}
19667c478bd9Sstevel@tonic-gate 			}
19677c478bd9Sstevel@tonic-gate 		}
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
19717c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	cur = firstmatch;
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
19767c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
19777c478bd9Sstevel@tonic-gate 		return (err);
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
19807c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
19817c478bd9Sstevel@tonic-gate 		return (err);
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
19847c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
19857c478bd9Sstevel@tonic-gate 		return (err);
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
19887c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
19897c478bd9Sstevel@tonic-gate 		return (err);
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 	/* options are optional */
19927c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
19937c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
19947c478bd9Sstevel@tonic-gate 	    options = options->next) {
19957c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
19967c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
19977c478bd9Sstevel@tonic-gate 			break;
19987c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
19997c478bd9Sstevel@tonic-gate 			break;
20007c478bd9Sstevel@tonic-gate 	}
20017c478bd9Sstevel@tonic-gate 	return (Z_OK);
20027c478bd9Sstevel@tonic-gate }
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate int
20057c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
20067c478bd9Sstevel@tonic-gate {
20077c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
20087c478bd9Sstevel@tonic-gate 	int err;
20097c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
20127c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
20157c478bd9Sstevel@tonic-gate 		return (err);
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	/*
20187c478bd9Sstevel@tonic-gate 	 * General algorithm:
20197c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
20207c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
20217c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
20227c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
20237c478bd9Sstevel@tonic-gate 	 */
20247c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
20257c478bd9Sstevel@tonic-gate 	match = NULL;
20267c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
20277c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
20287c478bd9Sstevel@tonic-gate 			continue;
20297c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
20307c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
20317c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
20327c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
20337c478bd9Sstevel@tonic-gate 				if (match == NULL)
20347c478bd9Sstevel@tonic-gate 					match = cur;
20357c478bd9Sstevel@tonic-gate 				else
20367c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20377c478bd9Sstevel@tonic-gate 			}
20387c478bd9Sstevel@tonic-gate 		}
20397c478bd9Sstevel@tonic-gate 	}
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	if (match == NULL)
20427c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	cur = match;
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
20477c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
20487c478bd9Sstevel@tonic-gate 		return (err);
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	return (Z_OK);
20517c478bd9Sstevel@tonic-gate }
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate /*
20547c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
20557c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
20567c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
20577c478bd9Sstevel@tonic-gate  */
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate boolean_t
20607c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
20617c478bd9Sstevel@tonic-gate {
20627c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
20637c478bd9Sstevel@tonic-gate 	int result;
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
20667c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	/*
20697c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
20707c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
20717c478bd9Sstevel@tonic-gate 	 */
20727c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
20737c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
20747c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
20757c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
20767c478bd9Sstevel@tonic-gate 		return (B_FALSE);
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	/*
20797c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
20807c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
20817c478bd9Sstevel@tonic-gate 	 * result of the comparison.
20827c478bd9Sstevel@tonic-gate 	 */
20837c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
20847c478bd9Sstevel@tonic-gate 	*slashp = '\0';
20857c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
20867c478bd9Sstevel@tonic-gate 	*slashp = '/';
20877c478bd9Sstevel@tonic-gate 	return ((result == 0));
20887c478bd9Sstevel@tonic-gate }
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate int
20917c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
20927c478bd9Sstevel@tonic-gate {
20937c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
20947c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
20957c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
20967c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
20997c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
21007c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
21017c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
21027c478bd9Sstevel@tonic-gate 		*slashp = '\0';
21037c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
21047c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
21057c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
21067c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
21077c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
21087c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
21097c478bd9Sstevel@tonic-gate 	} else {
21107c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
21117c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
21127c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
21137c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
21147c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
21157c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
2116a1be23daSdp 
21177c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
21187c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
21197c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
21207c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
2121a1be23daSdp 
21227c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
21237c478bd9Sstevel@tonic-gate 	}
21247c478bd9Sstevel@tonic-gate 	return (Z_OK);
21257c478bd9Sstevel@tonic-gate }
21267c478bd9Sstevel@tonic-gate 
2127f4b3ec61Sdh155122 boolean_t
2128f4b3ec61Sdh155122 zonecfg_ifname_exists(sa_family_t af, char *ifname)
2129f4b3ec61Sdh155122 {
2130f4b3ec61Sdh155122 	struct lifreq lifr;
2131f4b3ec61Sdh155122 	int so;
2132f4b3ec61Sdh155122 	int save_errno;
2133f4b3ec61Sdh155122 
2134f4b3ec61Sdh155122 	(void) memset(&lifr, 0, sizeof (lifr));
2135f4b3ec61Sdh155122 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2136f4b3ec61Sdh155122 	lifr.lifr_addr.ss_family = af;
2137f4b3ec61Sdh155122 	if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
2138f4b3ec61Sdh155122 		/* Odd - can't tell if the ifname exists */
2139f4b3ec61Sdh155122 		return (B_FALSE);
2140f4b3ec61Sdh155122 	}
2141f4b3ec61Sdh155122 	if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2142f4b3ec61Sdh155122 		save_errno = errno;
2143f4b3ec61Sdh155122 		(void) close(so);
2144f4b3ec61Sdh155122 		errno = save_errno;
2145f4b3ec61Sdh155122 		return (B_FALSE);
2146f4b3ec61Sdh155122 	}
2147f4b3ec61Sdh155122 	(void) close(so);
2148f4b3ec61Sdh155122 	return (B_TRUE);
2149f4b3ec61Sdh155122 }
2150f4b3ec61Sdh155122 
21517c478bd9Sstevel@tonic-gate int
21527c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21537c478bd9Sstevel@tonic-gate {
21547c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
21557c478bd9Sstevel@tonic-gate 	int err;
21567c478bd9Sstevel@tonic-gate 	char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ];
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21597c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21627c478bd9Sstevel@tonic-gate 		return (err);
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
21657c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
21667c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
21677c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
21687c478bd9Sstevel@tonic-gate 			continue;
21697c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_physical) > 0) {
21707c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
21717c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
21727c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
21737c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
21747c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
21757c478bd9Sstevel@tonic-gate 					firstmatch = cur;
21767c478bd9Sstevel@tonic-gate 				else
21777c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
21787c478bd9Sstevel@tonic-gate 			}
21797c478bd9Sstevel@tonic-gate 		}
21807c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_address) > 0) {
21817c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
21827c478bd9Sstevel@tonic-gate 			    sizeof (address)) == Z_OK)) {
21837c478bd9Sstevel@tonic-gate 				if (zonecfg_same_net_address(
21847c478bd9Sstevel@tonic-gate 				    tabptr->zone_nwif_address, address)) {
21857c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
21867c478bd9Sstevel@tonic-gate 						firstmatch = cur;
21877c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
21887c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
21897c478bd9Sstevel@tonic-gate 				} else {
21907c478bd9Sstevel@tonic-gate 					/*
21917c478bd9Sstevel@tonic-gate 					 * If another property matched but this
21927c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
21937c478bd9Sstevel@tonic-gate 					 */
21947c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
21957c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
21967c478bd9Sstevel@tonic-gate 				}
21977c478bd9Sstevel@tonic-gate 			}
21987c478bd9Sstevel@tonic-gate 		}
21997c478bd9Sstevel@tonic-gate 	}
22007c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
22017c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	cur = firstmatch;
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
22067c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
22077c478bd9Sstevel@tonic-gate 		return (err);
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
22107c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
22117c478bd9Sstevel@tonic-gate 		return (err);
22127c478bd9Sstevel@tonic-gate 
2213*de860bd9Sgfaden 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
2214*de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter,
2215*de860bd9Sgfaden 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK)
2216*de860bd9Sgfaden 		return (err);
2217*de860bd9Sgfaden 
22187c478bd9Sstevel@tonic-gate 	return (Z_OK);
22197c478bd9Sstevel@tonic-gate }
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate static int
22227c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22237c478bd9Sstevel@tonic-gate {
22247c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
22257c478bd9Sstevel@tonic-gate 	int err;
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
2228a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
22297c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
22307c478bd9Sstevel@tonic-gate 		return (err);
2231a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
22327c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
22337c478bd9Sstevel@tonic-gate 		return (err);
2234*de860bd9Sgfaden 	if ((err = newprop(newnode, DTD_ATTR_DEFROUTER,
2235*de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter)) != Z_OK)
2236*de860bd9Sgfaden 		return (err);
22377c478bd9Sstevel@tonic-gate 	return (Z_OK);
22387c478bd9Sstevel@tonic-gate }
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate int
22417c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22427c478bd9Sstevel@tonic-gate {
22437c478bd9Sstevel@tonic-gate 	int err;
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22467c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22497c478bd9Sstevel@tonic-gate 		return (err);
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
22527c478bd9Sstevel@tonic-gate 		return (err);
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 	return (Z_OK);
22557c478bd9Sstevel@tonic-gate }
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate static int
22587c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22597c478bd9Sstevel@tonic-gate {
22607c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
22617c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
22647c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
22657c478bd9Sstevel@tonic-gate 			continue;
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
22687c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
22697c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
22707c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
22737c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
22747c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
22757c478bd9Sstevel@tonic-gate 			return (Z_OK);
22767c478bd9Sstevel@tonic-gate 		}
22777c478bd9Sstevel@tonic-gate 	}
22787c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
22797c478bd9Sstevel@tonic-gate }
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate int
22827c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22837c478bd9Sstevel@tonic-gate {
22847c478bd9Sstevel@tonic-gate 	int err;
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22877c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22907c478bd9Sstevel@tonic-gate 		return (err);
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
22937c478bd9Sstevel@tonic-gate 		return (err);
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 	return (Z_OK);
22967c478bd9Sstevel@tonic-gate }
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate int
22997c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
23007c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
23017c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
23027c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
23037c478bd9Sstevel@tonic-gate {
23047c478bd9Sstevel@tonic-gate 	int err;
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
23077c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23107c478bd9Sstevel@tonic-gate 		return (err);
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
23137c478bd9Sstevel@tonic-gate 		return (err);
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
23167c478bd9Sstevel@tonic-gate 		return (err);
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 	return (Z_OK);
23197c478bd9Sstevel@tonic-gate }
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate int
23227c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23237c478bd9Sstevel@tonic-gate {
23247c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
23257c478bd9Sstevel@tonic-gate 	int err;
23267c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23327c478bd9Sstevel@tonic-gate 		return (err);
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
23357c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
23367c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23377c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
23387c478bd9Sstevel@tonic-gate 			continue;
23397c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
23407c478bd9Sstevel@tonic-gate 			continue;
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
23437c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
23447c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
23457c478bd9Sstevel@tonic-gate 			    match) == 0) {
23467c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
23477c478bd9Sstevel@tonic-gate 					firstmatch = cur;
23487c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
23497c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
23507c478bd9Sstevel@tonic-gate 			} else {
23517c478bd9Sstevel@tonic-gate 				/*
23527c478bd9Sstevel@tonic-gate 				 * If another property matched but this
23537c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
23547c478bd9Sstevel@tonic-gate 				 */
23557c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
23567c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
23577c478bd9Sstevel@tonic-gate 			}
23587c478bd9Sstevel@tonic-gate 		}
23597c478bd9Sstevel@tonic-gate 	}
23607c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
23617c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	cur = firstmatch;
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
23667c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
23677c478bd9Sstevel@tonic-gate 		return (err);
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 	return (Z_OK);
23707c478bd9Sstevel@tonic-gate }
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate static int
23737c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
23747c478bd9Sstevel@tonic-gate {
23757c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
23767c478bd9Sstevel@tonic-gate 	int err;
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
23797c478bd9Sstevel@tonic-gate 
2380a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
23817c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
23827c478bd9Sstevel@tonic-gate 		return (err);
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	return (Z_OK);
23857c478bd9Sstevel@tonic-gate }
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate int
23887c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23897c478bd9Sstevel@tonic-gate {
23907c478bd9Sstevel@tonic-gate 	int err;
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23937c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23967c478bd9Sstevel@tonic-gate 		return (err);
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
23997c478bd9Sstevel@tonic-gate 		return (err);
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	return (Z_OK);
24027c478bd9Sstevel@tonic-gate }
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate static int
24057c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
24067c478bd9Sstevel@tonic-gate {
2407facf4a8dSllai1 	xmlNodePtr cur = handle->zone_dh_cur;
24087c478bd9Sstevel@tonic-gate 	int match_match;
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
24117c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
24127c478bd9Sstevel@tonic-gate 			continue;
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
24157c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 		if (match_match) {
24187c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
24197c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
24207c478bd9Sstevel@tonic-gate 			return (Z_OK);
24217c478bd9Sstevel@tonic-gate 		}
24227c478bd9Sstevel@tonic-gate 	}
24237c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
24247c478bd9Sstevel@tonic-gate }
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate int
24277c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
24287c478bd9Sstevel@tonic-gate {
24297c478bd9Sstevel@tonic-gate 	int err;
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
24327c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24357c478bd9Sstevel@tonic-gate 		return (err);
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
24387c478bd9Sstevel@tonic-gate 		return (err);
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 	return (Z_OK);
24417c478bd9Sstevel@tonic-gate }
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate int
24447c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
24457c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
24467c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
24477c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
24487c478bd9Sstevel@tonic-gate {
24497c478bd9Sstevel@tonic-gate 	int err;
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
24527c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24557c478bd9Sstevel@tonic-gate 		return (err);
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
24587c478bd9Sstevel@tonic-gate 		return (err);
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
24617c478bd9Sstevel@tonic-gate 		return (err);
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	return (Z_OK);
24647c478bd9Sstevel@tonic-gate }
24657c478bd9Sstevel@tonic-gate 
2466ee519a1fSgjelinek /* Lock to serialize all zonecfg_devwalks */
2467ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2468ee519a1fSgjelinek /*
2469ee519a1fSgjelinek  * Global variables used to pass data from zonecfg_devwalk to the nftw
2470ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2471ee519a1fSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from zonecfg_devwalk.
2472ee519a1fSgjelinek  */
2473ee519a1fSgjelinek static void *g_devwalk_data;
2474ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2475ee519a1fSgjelinek     void *);
2476ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2477ee519a1fSgjelinek 
2478ee519a1fSgjelinek /*
2479ee519a1fSgjelinek  * This is the nftw call-back function used by zonecfg_devwalk.  It is
2480ee519a1fSgjelinek  * responsible for calling the actual call-back that is passed in to
2481ee519a1fSgjelinek  * zonecfg_devwalk as the *cb argument.
2482ee519a1fSgjelinek  */
2483ee519a1fSgjelinek /* ARGSUSED2 */
2484ee519a1fSgjelinek static int
2485ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2486ee519a1fSgjelinek     struct FTW *ftw)
2487ee519a1fSgjelinek {
2488ee519a1fSgjelinek 	acl_t *acl;
2489ee519a1fSgjelinek 	char *acl_txt = NULL;
2490ee519a1fSgjelinek 
2491ee519a1fSgjelinek 	/* skip all but character and block devices */
2492ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2493ee519a1fSgjelinek 		return (0);
2494ee519a1fSgjelinek 
2495ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2496ee519a1fSgjelinek 	    acl != NULL) {
2497ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2498ee519a1fSgjelinek 		acl_free(acl);
2499ee519a1fSgjelinek 	}
2500ee519a1fSgjelinek 
2501ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2502ee519a1fSgjelinek 		return (0);
2503ee519a1fSgjelinek 
2504ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2505ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2506ee519a1fSgjelinek 	    g_devwalk_data);
2507ee519a1fSgjelinek 	free(acl_txt);
2508ee519a1fSgjelinek 	return (0);
2509ee519a1fSgjelinek }
2510ee519a1fSgjelinek 
2511ee519a1fSgjelinek /*
2512ee519a1fSgjelinek  * Walk the dev tree for the zone specified by hdl and call the call-back (cb)
2513ee519a1fSgjelinek  * function for each entry in the tree.  The call-back will be passed the
2514ee519a1fSgjelinek  * name, uid, gid, mode, acl string and the void *data input parameter
2515ee519a1fSgjelinek  * for each dev entry.
2516ee519a1fSgjelinek  *
2517ee519a1fSgjelinek  * Data is passed to the zonecfg_devwalk_cb through the global variables
2518ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2519ee519a1fSgjelinek  * zonecfg_devwalk_cb function will actually call *cb.
2520ee519a1fSgjelinek  */
2521ee519a1fSgjelinek int
2522ee519a1fSgjelinek zonecfg_devwalk(zone_dochandle_t hdl,
2523ee519a1fSgjelinek     int (*cb)(const char *, uid_t, gid_t, mode_t, const char *, void *),
2524ee519a1fSgjelinek     void *data)
2525ee519a1fSgjelinek {
2526ee519a1fSgjelinek 	char path[MAXPATHLEN];
2527ee519a1fSgjelinek 	int ret;
2528ee519a1fSgjelinek 
2529ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2530ee519a1fSgjelinek 		return (ret);
2531ee519a1fSgjelinek 
2532ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2533ee519a1fSgjelinek 		return (Z_TOO_BIG);
2534ee519a1fSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2535ee519a1fSgjelinek 
2536ee519a1fSgjelinek 	/*
2537ee519a1fSgjelinek 	 * We have to serialize all zonecfg_devwalks in the same process
2538ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2539ee519a1fSgjelinek 	 */
2540ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2541ee519a1fSgjelinek 
2542ee519a1fSgjelinek 	g_devwalk_data = data;
2543ee519a1fSgjelinek 	g_devwalk_cb = cb;
2544ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2545ee519a1fSgjelinek 
2546ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2547ee519a1fSgjelinek 	return (Z_OK);
2548ee519a1fSgjelinek }
2549ee519a1fSgjelinek 
2550ee519a1fSgjelinek /*
2551ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2552ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2553ee519a1fSgjelinek  * attaching a migrated zone.
2554ee519a1fSgjelinek  */
2555ee519a1fSgjelinek int
2556ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2557ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2558ee519a1fSgjelinek {
2559ee519a1fSgjelinek 	int ret;
2560ee519a1fSgjelinek 	char path[MAXPATHLEN];
2561ee519a1fSgjelinek 	struct stat st;
2562ee519a1fSgjelinek 	acl_t *aclp;
2563ee519a1fSgjelinek 
2564ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2565ee519a1fSgjelinek 		return (ret);
2566ee519a1fSgjelinek 
2567ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2568ee519a1fSgjelinek 		return (Z_TOO_BIG);
2569ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2570ee519a1fSgjelinek 		return (Z_TOO_BIG);
2571ee519a1fSgjelinek 
2572ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2573ee519a1fSgjelinek 		return (Z_INVAL);
2574ee519a1fSgjelinek 
2575ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2576ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2577ee519a1fSgjelinek 		return (Z_INVAL);
2578ee519a1fSgjelinek 
2579ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2580ee519a1fSgjelinek 		return (Z_SYSTEM);
2581ee519a1fSgjelinek 
2582ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2583ee519a1fSgjelinek 		return (Z_SYSTEM);
2584ee519a1fSgjelinek 
2585ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2586ee519a1fSgjelinek 		return (Z_OK);
2587ee519a1fSgjelinek 
2588ee519a1fSgjelinek 	if (acl_fromtext(acltxt, &aclp) != 0)
2589ee519a1fSgjelinek 		return (Z_SYSTEM);
2590ee519a1fSgjelinek 
2591ee519a1fSgjelinek 	errno = 0;
2592ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2593ee519a1fSgjelinek 		free(aclp);
2594ee519a1fSgjelinek 		return (Z_SYSTEM);
2595ee519a1fSgjelinek 	}
2596ee519a1fSgjelinek 
2597ee519a1fSgjelinek 	free(aclp);
2598ee519a1fSgjelinek 	return (Z_OK);
2599ee519a1fSgjelinek }
2600ee519a1fSgjelinek 
26017c478bd9Sstevel@tonic-gate /*
26027c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
26037c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
26047c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
26057c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
26067c478bd9Sstevel@tonic-gate  *
26077c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
26087c478bd9Sstevel@tonic-gate  * aborts with an error.
26097c478bd9Sstevel@tonic-gate  */
26107c478bd9Sstevel@tonic-gate int
26117c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
26127c478bd9Sstevel@tonic-gate     void *priv) {
26137c478bd9Sstevel@tonic-gate 	FILE *mnttab;
26147c478bd9Sstevel@tonic-gate 	struct mnttab m;
26157c478bd9Sstevel@tonic-gate 	size_t l;
261607b574eeSgjelinek 	int zfsl;
26177c478bd9Sstevel@tonic-gate 	int rv = 0;
261807b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
26197c478bd9Sstevel@tonic-gate 
26207c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
26217c478bd9Sstevel@tonic-gate 
262207b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
262307b574eeSgjelinek 	    >= sizeof (zfs_path))
262407b574eeSgjelinek 		return (-1);
262507b574eeSgjelinek 
26267c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
26317c478bd9Sstevel@tonic-gate 		return (-1);
26327c478bd9Sstevel@tonic-gate 
26337c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
26347c478bd9Sstevel@tonic-gate 		rv = -1;
26357c478bd9Sstevel@tonic-gate 		goto out;
26367c478bd9Sstevel@tonic-gate 	}
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
26397c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
264007b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
264107b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
26427c478bd9Sstevel@tonic-gate 			rv++;
26437c478bd9Sstevel@tonic-gate 			if (callback == NULL)
26447c478bd9Sstevel@tonic-gate 				continue;
26457c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
26467c478bd9Sstevel@tonic-gate 				rv = -1;
26477c478bd9Sstevel@tonic-gate 				goto out;
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 			}
26507c478bd9Sstevel@tonic-gate 		}
26517c478bd9Sstevel@tonic-gate 	}
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate out:
26547c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
26557c478bd9Sstevel@tonic-gate 	return (rv);
26567c478bd9Sstevel@tonic-gate }
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate int
26597c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26607c478bd9Sstevel@tonic-gate {
26617c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
26627c478bd9Sstevel@tonic-gate 	int err;
26637c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
26667c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
26697c478bd9Sstevel@tonic-gate 		return (err);
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
26727c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
26737c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
26747c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
26757c478bd9Sstevel@tonic-gate 			continue;
26767c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
26777c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
26787c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
26797c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
26807c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
26817c478bd9Sstevel@tonic-gate 					firstmatch = cur;
26827c478bd9Sstevel@tonic-gate 				else
26837c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
26847c478bd9Sstevel@tonic-gate 			}
26857c478bd9Sstevel@tonic-gate 		}
26867c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
26877c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
26887c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
26897c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
26907c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
26917c478bd9Sstevel@tonic-gate 						firstmatch = cur;
26927c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
26937c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
26947c478bd9Sstevel@tonic-gate 				} else {
26957c478bd9Sstevel@tonic-gate 					/*
26967c478bd9Sstevel@tonic-gate 					 * If another property matched but this
26977c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
26987c478bd9Sstevel@tonic-gate 					 */
26997c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
27007c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
27017c478bd9Sstevel@tonic-gate 				}
27027c478bd9Sstevel@tonic-gate 			}
27037c478bd9Sstevel@tonic-gate 		}
27047c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
27057c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
27067c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
27077c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
27087c478bd9Sstevel@tonic-gate 				    0) {
27097c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
27107c478bd9Sstevel@tonic-gate 						firstmatch = cur;
27117c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
27127c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
27137c478bd9Sstevel@tonic-gate 				} else {
27147c478bd9Sstevel@tonic-gate 					/*
27157c478bd9Sstevel@tonic-gate 					 * If another property matched but this
27167c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
27177c478bd9Sstevel@tonic-gate 					 */
27187c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
27197c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
27207c478bd9Sstevel@tonic-gate 				}
27217c478bd9Sstevel@tonic-gate 			}
27227c478bd9Sstevel@tonic-gate 		}
27237c478bd9Sstevel@tonic-gate 	}
27247c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
27257c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 	cur = firstmatch;
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
27307c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
27317c478bd9Sstevel@tonic-gate 		return (err);
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
27347c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
27357c478bd9Sstevel@tonic-gate 		return (err);
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
27387c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
27397c478bd9Sstevel@tonic-gate 		return (err);
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	return (Z_OK);
27427c478bd9Sstevel@tonic-gate }
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate static int
27457c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27467c478bd9Sstevel@tonic-gate {
27477c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
27487c478bd9Sstevel@tonic-gate 	int err;
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2751a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
27527c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27537c478bd9Sstevel@tonic-gate 		return (err);
2754a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
27557c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27567c478bd9Sstevel@tonic-gate 		return (err);
2757a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
27587c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27597c478bd9Sstevel@tonic-gate 		return (err);
27607c478bd9Sstevel@tonic-gate 	return (Z_OK);
27617c478bd9Sstevel@tonic-gate }
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate int
27647c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27657c478bd9Sstevel@tonic-gate {
27667c478bd9Sstevel@tonic-gate 	int err;
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
27697c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27727c478bd9Sstevel@tonic-gate 		return (err);
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
27757c478bd9Sstevel@tonic-gate 		return (err);
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	return (Z_OK);
27787c478bd9Sstevel@tonic-gate }
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate static int
27817c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27827c478bd9Sstevel@tonic-gate {
27837c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
27847c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
27877c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
27887c478bd9Sstevel@tonic-gate 			continue;
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
27917c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
27927c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
27937c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
27947c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
27957c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
27987c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
27997c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
28007c478bd9Sstevel@tonic-gate 			return (Z_OK);
28017c478bd9Sstevel@tonic-gate 		}
28027c478bd9Sstevel@tonic-gate 	}
28037c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
28047c478bd9Sstevel@tonic-gate }
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate int
28077c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
28087c478bd9Sstevel@tonic-gate {
28097c478bd9Sstevel@tonic-gate 	int err;
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
28127c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28157c478bd9Sstevel@tonic-gate 		return (err);
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
28187c478bd9Sstevel@tonic-gate 		return (err);
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 	return (Z_OK);
28217c478bd9Sstevel@tonic-gate }
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate int
28247c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
28257c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
28267c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
28277c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
28287c478bd9Sstevel@tonic-gate {
28297c478bd9Sstevel@tonic-gate 	int err;
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
28327c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28357c478bd9Sstevel@tonic-gate 		return (err);
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
28387c478bd9Sstevel@tonic-gate 		return (err);
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
28417c478bd9Sstevel@tonic-gate 		return (err);
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	return (Z_OK);
28447c478bd9Sstevel@tonic-gate }
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate int
28477c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
28487c478bd9Sstevel@tonic-gate {
28497c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28507c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
28537c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
28567c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
28577c478bd9Sstevel@tonic-gate 		return (Z_OK);
28587c478bd9Sstevel@tonic-gate 	}
28597c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
28607c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
28617c478bd9Sstevel@tonic-gate 		return (Z_OK);
28627c478bd9Sstevel@tonic-gate 	}
28637c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
28647c478bd9Sstevel@tonic-gate }
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate int
28677c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
28687c478bd9Sstevel@tonic-gate {
28697c478bd9Sstevel@tonic-gate 	long long result;
28707c478bd9Sstevel@tonic-gate 	char *endptr;
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28737c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
28767c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	errno = 0;
28797c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
28807c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
28817c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28827c478bd9Sstevel@tonic-gate 	*value = result;
28837c478bd9Sstevel@tonic-gate 	return (Z_OK);
28847c478bd9Sstevel@tonic-gate }
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate int
28877c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
28887c478bd9Sstevel@tonic-gate     size_t val_sz)
28897c478bd9Sstevel@tonic-gate {
28907c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28917c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
28947c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
28977c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
28987c478bd9Sstevel@tonic-gate 	return (Z_OK);
28997c478bd9Sstevel@tonic-gate }
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate int
29027c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
29037c478bd9Sstevel@tonic-gate {
29047c478bd9Sstevel@tonic-gate 	unsigned long long result;
29057c478bd9Sstevel@tonic-gate 	long long neg_result;
29067c478bd9Sstevel@tonic-gate 	char *endptr;
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate 	if (attr == NULL)
29097c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
29127c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate 	errno = 0;
29157c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
29167c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
29177c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29187c478bd9Sstevel@tonic-gate 	errno = 0;
29197c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
29207c478bd9Sstevel@tonic-gate 	/*
29217c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
29227c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
29237c478bd9Sstevel@tonic-gate 	 * need to look for this here.
29247c478bd9Sstevel@tonic-gate 	 */
29257c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
29267c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29277c478bd9Sstevel@tonic-gate 	*value = result;
29287c478bd9Sstevel@tonic-gate 	return (Z_OK);
29297c478bd9Sstevel@tonic-gate }
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate int
29327c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29337c478bd9Sstevel@tonic-gate {
29347c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
29357c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
29367c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29377c478bd9Sstevel@tonic-gate 	int err;
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate 	if (tabptr->zone_rctl_name == NULL ||
29407c478bd9Sstevel@tonic-gate 	    strlen(tabptr->zone_rctl_name) == 0)
29417c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29447c478bd9Sstevel@tonic-gate 		return (err);
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
29477c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
29487c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
29497c478bd9Sstevel@tonic-gate 			continue;
29507c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
29517c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
29527c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
29537c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
29547c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
29557c478bd9Sstevel@tonic-gate 			    val = val->next) {
29567c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
29577c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
29587c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
29597c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
29607c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
29617c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
29627c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
29637c478bd9Sstevel@tonic-gate 				    Z_OK))
29647c478bd9Sstevel@tonic-gate 					break;
29657c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
29667c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
29677c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
29687c478bd9Sstevel@tonic-gate 				    Z_OK))
29697c478bd9Sstevel@tonic-gate 					break;
29707c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
29717c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
29727c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
29737c478bd9Sstevel@tonic-gate 				    Z_OK))
29747c478bd9Sstevel@tonic-gate 					break;
29757c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
29767c478bd9Sstevel@tonic-gate 				    Z_OK)
29777c478bd9Sstevel@tonic-gate 					break;
29787c478bd9Sstevel@tonic-gate 			}
29797c478bd9Sstevel@tonic-gate 			return (Z_OK);
29807c478bd9Sstevel@tonic-gate 		}
29817c478bd9Sstevel@tonic-gate 	}
29827c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
29837c478bd9Sstevel@tonic-gate }
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate static int
29867c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29877c478bd9Sstevel@tonic-gate {
29887c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
29897c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29907c478bd9Sstevel@tonic-gate 	int err;
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
2993a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
29947c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
29957c478bd9Sstevel@tonic-gate 		return (err);
29967c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
29977c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
29987c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
29997c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
3000a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
30017c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
30027c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
30037c478bd9Sstevel@tonic-gate 			return (err);
3004a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
30057c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
30067c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
30077c478bd9Sstevel@tonic-gate 			return (err);
3008a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
30097c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
30107c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
30117c478bd9Sstevel@tonic-gate 			return (err);
30127c478bd9Sstevel@tonic-gate 	}
30137c478bd9Sstevel@tonic-gate 	return (Z_OK);
30147c478bd9Sstevel@tonic-gate }
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate int
30177c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30187c478bd9Sstevel@tonic-gate {
30197c478bd9Sstevel@tonic-gate 	int err;
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
30227c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30257c478bd9Sstevel@tonic-gate 		return (err);
30267c478bd9Sstevel@tonic-gate 
30277c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
30287c478bd9Sstevel@tonic-gate 		return (err);
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	return (Z_OK);
30317c478bd9Sstevel@tonic-gate }
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate static int
30347c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30357c478bd9Sstevel@tonic-gate {
30367c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
30377c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
30387c478bd9Sstevel@tonic-gate 	int name_result;
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
30417c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
30427c478bd9Sstevel@tonic-gate 			continue;
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
30457c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
30467c478bd9Sstevel@tonic-gate 			continue;
30477c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
30487c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
30497c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
30527c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
30537c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
30547c478bd9Sstevel@tonic-gate 			return (Z_OK);
30557c478bd9Sstevel@tonic-gate 		}
30567c478bd9Sstevel@tonic-gate 	}
30577c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
30587c478bd9Sstevel@tonic-gate }
30597c478bd9Sstevel@tonic-gate 
30607c478bd9Sstevel@tonic-gate int
30617c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30627c478bd9Sstevel@tonic-gate {
30637c478bd9Sstevel@tonic-gate 	int err;
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
30667c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30697c478bd9Sstevel@tonic-gate 		return (err);
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
30727c478bd9Sstevel@tonic-gate 		return (err);
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 	return (Z_OK);
30757c478bd9Sstevel@tonic-gate }
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate int
30787c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
30797c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
30807c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
30817c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
30827c478bd9Sstevel@tonic-gate {
30837c478bd9Sstevel@tonic-gate 	int err;
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || oldtabptr->zone_rctl_name == NULL ||
30867c478bd9Sstevel@tonic-gate 	    newtabptr == NULL || newtabptr->zone_rctl_name == NULL)
30877c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30907c478bd9Sstevel@tonic-gate 		return (err);
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
30937c478bd9Sstevel@tonic-gate 		return (err);
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
30967c478bd9Sstevel@tonic-gate 		return (err);
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 	return (Z_OK);
30997c478bd9Sstevel@tonic-gate }
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate int
31027c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
31037c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
31047c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
31057c478bd9Sstevel@tonic-gate {
31067c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
31077c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
31107c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
31117c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
31127c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
31137c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
31147c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
31157c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31167c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
31177c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31187c478bd9Sstevel@tonic-gate 	if (last == NULL)
31197c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
31207c478bd9Sstevel@tonic-gate 	else
31217c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
31227c478bd9Sstevel@tonic-gate 	return (Z_OK);
31237c478bd9Sstevel@tonic-gate }
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate int
31267c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
31277c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
31287c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
31297c478bd9Sstevel@tonic-gate {
31307c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
31337c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
31347c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
31357c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
31367c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
31377c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
31387c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
31397c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
31407c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
31417c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
31427c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
31437c478bd9Sstevel@tonic-gate 			else
31447c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
31457c478bd9Sstevel@tonic-gate 			free(this);
31467c478bd9Sstevel@tonic-gate 			return (Z_OK);
31477c478bd9Sstevel@tonic-gate 		} else
31487c478bd9Sstevel@tonic-gate 			last = this;
31497c478bd9Sstevel@tonic-gate 	}
31507c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
31517c478bd9Sstevel@tonic-gate }
31527c478bd9Sstevel@tonic-gate 
31537c478bd9Sstevel@tonic-gate char *
31547c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
31557c478bd9Sstevel@tonic-gate {
31567c478bd9Sstevel@tonic-gate 	switch (errnum) {
31577c478bd9Sstevel@tonic-gate 	case Z_OK:
31587c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
31597c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
31607c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
31617c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
31627c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
31637c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
31647c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
31657c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
31667c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31677c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
31687c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
31697c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
31707c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
31717c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
31727c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
31737c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
31747c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
31757c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
31767c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
31777c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
31787c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
31797c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
31807c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
31817c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
31827c478bd9Sstevel@tonic-gate 	case Z_INVAL:
31837c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
31847c478bd9Sstevel@tonic-gate 	case Z_ACCES:
31857c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
31867c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
31877c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
31887c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
31897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31907c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
31917c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
31927c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
31937c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
31947c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
31957c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
31967c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
31977c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
31987c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
31997c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
32007c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3201087719fdSdp 	case Z_BAD_ZONE_STATE:
32027c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
3203087719fdSdp 		    "Zone state is invalid for the requested operation"));
32047c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
32057c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3206087719fdSdp 	case Z_NAME_IN_USE:
3207087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
32087c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
32097c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
32107c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
32117c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
32127c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
32137c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
32147c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
32157c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
32167c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
32177c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
32187c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
32197c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
32207c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
32217c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32227c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
32237c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
32247c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32257c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3226ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3227ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3228ffbafc53Scomay 		    "Specified privilege is prohibited"));
3229ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3230ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3231ffbafc53Scomay 		    "Required privilege is missing"));
3232ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3233ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3234ffbafc53Scomay 		    "Specified privilege is unknown"));
32359acbbeafSnn35248 	case Z_BRAND_ERROR:
32369acbbeafSnn35248 		return (dgettext(TEXT_DOMAIN,
32379acbbeafSnn35248 		    "Brand-specific error"));
32380209230bSgjelinek 	case Z_INCOMPATIBLE:
32390209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Incompatible settings"));
32400209230bSgjelinek 	case Z_ALIAS_DISALLOW:
32410209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32420209230bSgjelinek 		    "An incompatible rctl already exists for this property"));
32430209230bSgjelinek 	case Z_CLEAR_DISALLOW:
32440209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32450209230bSgjelinek 		    "Clearing this property is not allowed"));
32460209230bSgjelinek 	case Z_POOL:
32470209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error"));
32480209230bSgjelinek 	case Z_POOLS_NOT_ACTIVE:
32490209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Pools facility not active; "
32500209230bSgjelinek 		    "zone will not be bound to pool"));
32510209230bSgjelinek 	case Z_POOL_ENABLE:
32520209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32530209230bSgjelinek 		    "Could not enable pools facility"));
32540209230bSgjelinek 	case Z_NO_POOL:
32550209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32560209230bSgjelinek 		    "Pool not found; using default pool"));
32570209230bSgjelinek 	case Z_POOL_CREATE:
32580209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32590209230bSgjelinek 		    "Could not create a temporary pool"));
32600209230bSgjelinek 	case Z_POOL_BIND:
32610209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool"));
32627c478bd9Sstevel@tonic-gate 	default:
32637c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
32647c478bd9Sstevel@tonic-gate 	}
32657c478bd9Sstevel@tonic-gate }
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate /*
32687c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
32697c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
32707c478bd9Sstevel@tonic-gate  */
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate static int
32737c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
32747c478bd9Sstevel@tonic-gate {
32757c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
32767c478bd9Sstevel@tonic-gate 	int err;
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32797c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
32827c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
32837c478bd9Sstevel@tonic-gate 		return (err);
32847c478bd9Sstevel@tonic-gate 	}
32857c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
32867c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
32877c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
32887c478bd9Sstevel@tonic-gate 	return (Z_OK);
32897c478bd9Sstevel@tonic-gate }
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate static int
32927c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
32937c478bd9Sstevel@tonic-gate {
32947c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32957c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
32987c478bd9Sstevel@tonic-gate 	return (Z_OK);
32997c478bd9Sstevel@tonic-gate }
33007c478bd9Sstevel@tonic-gate 
33010209230bSgjelinek /*
33020209230bSgjelinek  * Do the work required to manipulate a process through libproc.
33030209230bSgjelinek  * If grab_process() returns no errors (0), then release_process()
33040209230bSgjelinek  * must eventually be called.
33050209230bSgjelinek  *
33060209230bSgjelinek  * Return values:
33070209230bSgjelinek  *      0 Successful creation of agent thread
33080209230bSgjelinek  *      1 Error grabbing
33090209230bSgjelinek  *      2 Error creating agent
33100209230bSgjelinek  */
33110209230bSgjelinek static int
33120209230bSgjelinek grab_process(pr_info_handle_t *p)
33130209230bSgjelinek {
33140209230bSgjelinek 	int ret;
33150209230bSgjelinek 
33160209230bSgjelinek 	if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) {
33170209230bSgjelinek 
33180209230bSgjelinek 		if (Psetflags(p->pr, PR_RLC) != 0) {
33190209230bSgjelinek 			Prelease(p->pr, 0);
33200209230bSgjelinek 			return (1);
33210209230bSgjelinek 		}
33220209230bSgjelinek 		if (Pcreate_agent(p->pr) == 0) {
33230209230bSgjelinek 			return (0);
33240209230bSgjelinek 
33250209230bSgjelinek 		} else {
33260209230bSgjelinek 			Prelease(p->pr, 0);
33270209230bSgjelinek 			return (2);
33280209230bSgjelinek 		}
33290209230bSgjelinek 	} else {
33300209230bSgjelinek 		return (1);
33310209230bSgjelinek 	}
33320209230bSgjelinek }
33330209230bSgjelinek 
33340209230bSgjelinek /*
33350209230bSgjelinek  * Release the specified process. This destroys the agent
33360209230bSgjelinek  * and releases the process. If the process is NULL, nothing
33370209230bSgjelinek  * is done. This function should only be called if grab_process()
33380209230bSgjelinek  * has previously been called and returned success.
33390209230bSgjelinek  *
33400209230bSgjelinek  * This function is Pgrab-safe.
33410209230bSgjelinek  */
33420209230bSgjelinek static void
33430209230bSgjelinek release_process(struct ps_prochandle *Pr)
33440209230bSgjelinek {
33450209230bSgjelinek 	if (Pr == NULL)
33460209230bSgjelinek 		return;
33470209230bSgjelinek 
33480209230bSgjelinek 	Pdestroy_agent(Pr);
33490209230bSgjelinek 	Prelease(Pr, 0);
33500209230bSgjelinek }
33510209230bSgjelinek 
33520209230bSgjelinek static boolean_t
33530209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p)
33540209230bSgjelinek {
33550209230bSgjelinek 	DIR *dirp;
33560209230bSgjelinek 	struct dirent *dentp;
33570209230bSgjelinek 	zoneid_t zoneid;
33580209230bSgjelinek 	int pid_self;
33590209230bSgjelinek 	psinfo_t psinfo;
33600209230bSgjelinek 
33610209230bSgjelinek 	if (zone_get_id(zonename, &zoneid) != 0)
33620209230bSgjelinek 		return (B_FALSE);
33630209230bSgjelinek 
33640209230bSgjelinek 	pid_self = getpid();
33650209230bSgjelinek 
33660209230bSgjelinek 	if ((dirp = opendir("/proc")) == NULL)
33670209230bSgjelinek 		return (B_FALSE);
33680209230bSgjelinek 
33690209230bSgjelinek 	while (dentp = readdir(dirp)) {
33700209230bSgjelinek 		p->pid = atoi(dentp->d_name);
33710209230bSgjelinek 
33720209230bSgjelinek 		/* Skip self */
33730209230bSgjelinek 		if (p->pid == pid_self)
33740209230bSgjelinek 			continue;
33750209230bSgjelinek 
33760209230bSgjelinek 		if (proc_get_psinfo(p->pid, &psinfo) != 0)
33770209230bSgjelinek 			continue;
33780209230bSgjelinek 
33790209230bSgjelinek 		if (psinfo.pr_zoneid != zoneid)
33800209230bSgjelinek 			continue;
33810209230bSgjelinek 
33820209230bSgjelinek 		/* attempt to grab process */
33830209230bSgjelinek 		if (grab_process(p) != 0)
33840209230bSgjelinek 			continue;
33850209230bSgjelinek 
33860209230bSgjelinek 		if (pr_getzoneid(p->pr) != zoneid) {
33870209230bSgjelinek 			release_process(p->pr);
33880209230bSgjelinek 			continue;
33890209230bSgjelinek 		}
33900209230bSgjelinek 
33910209230bSgjelinek 		(void) closedir(dirp);
33920209230bSgjelinek 		return (B_TRUE);
33930209230bSgjelinek 	}
33940209230bSgjelinek 
33950209230bSgjelinek 	(void) closedir(dirp);
33960209230bSgjelinek 	return (B_FALSE);
33970209230bSgjelinek }
33980209230bSgjelinek 
33990209230bSgjelinek static boolean_t
34000209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk)
34010209230bSgjelinek {
34020209230bSgjelinek 	if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST))
34030209230bSgjelinek 		return (B_FALSE);
34040209230bSgjelinek 
34050209230bSgjelinek 	if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
34060209230bSgjelinek 		return (B_TRUE);
34070209230bSgjelinek 
34080209230bSgjelinek 	while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) {
34090209230bSgjelinek 		if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
34100209230bSgjelinek 			return (B_TRUE);
34110209230bSgjelinek 	}
34120209230bSgjelinek 
34130209230bSgjelinek 	return (B_FALSE);
34140209230bSgjelinek }
34150209230bSgjelinek 
34160209230bSgjelinek /*
34170209230bSgjelinek  * Apply the current rctl settings to the specified, running zone.
34180209230bSgjelinek  */
34190209230bSgjelinek int
34200209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle)
34210209230bSgjelinek {
34220209230bSgjelinek 	int err;
34230209230bSgjelinek 	int res = Z_OK;
34240209230bSgjelinek 	rctlblk_t *rblk;
34250209230bSgjelinek 	pr_info_handle_t p;
34260209230bSgjelinek 	struct zone_rctltab rctl;
34270209230bSgjelinek 
34280209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK)
34290209230bSgjelinek 		return (err);
34300209230bSgjelinek 
34310209230bSgjelinek 	if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) {
34320209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
34330209230bSgjelinek 		return (Z_NOMEM);
34340209230bSgjelinek 	}
34350209230bSgjelinek 
34360209230bSgjelinek 	if (!grab_zone_proc(zone_name, &p)) {
34370209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
34380209230bSgjelinek 		free(rblk);
34390209230bSgjelinek 		return (Z_SYSTEM);
34400209230bSgjelinek 	}
34410209230bSgjelinek 
34420209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
34430209230bSgjelinek 		char *rname;
34440209230bSgjelinek 		struct zone_rctlvaltab *valptr;
34450209230bSgjelinek 
34460209230bSgjelinek 		rname = rctl.zone_rctl_name;
34470209230bSgjelinek 
34480209230bSgjelinek 		/* first delete all current privileged settings for this rctl */
34490209230bSgjelinek 		while (get_priv_rctl(p.pr, rname, rblk)) {
34500209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) !=
34510209230bSgjelinek 			    0) {
34520209230bSgjelinek 				res = Z_SYSTEM;
34530209230bSgjelinek 				goto done;
34540209230bSgjelinek 			}
34550209230bSgjelinek 		}
34560209230bSgjelinek 
34570209230bSgjelinek 		/* now set each new value for the rctl */
34580209230bSgjelinek 		for (valptr = rctl.zone_rctl_valptr; valptr != NULL;
34590209230bSgjelinek 		    valptr = valptr->zone_rctlval_next) {
34600209230bSgjelinek 			if ((err = zonecfg_construct_rctlblk(valptr, rblk))
34610209230bSgjelinek 			    != Z_OK) {
34620209230bSgjelinek 				res = errno = err;
34630209230bSgjelinek 				goto done;
34640209230bSgjelinek 			}
34650209230bSgjelinek 
34660209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) {
34670209230bSgjelinek 				res = Z_SYSTEM;
34680209230bSgjelinek 				goto done;
34690209230bSgjelinek 			}
34700209230bSgjelinek 		}
34710209230bSgjelinek 	}
34720209230bSgjelinek 
34730209230bSgjelinek done:
34740209230bSgjelinek 	release_process(p.pr);
34750209230bSgjelinek 	free(rblk);
34760209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
34770209230bSgjelinek 
34780209230bSgjelinek 	return (res);
34790209230bSgjelinek }
34800209230bSgjelinek 
34810209230bSgjelinek static const xmlChar *
34820209230bSgjelinek nm_to_dtd(char *nm)
34830209230bSgjelinek {
34840209230bSgjelinek 	if (strcmp(nm, "device") == 0)
34850209230bSgjelinek 		return (DTD_ELEM_DEVICE);
34860209230bSgjelinek 	if (strcmp(nm, "fs") == 0)
34870209230bSgjelinek 		return (DTD_ELEM_FS);
34880209230bSgjelinek 	if (strcmp(nm, "inherit-pkg-dir") == 0)
34890209230bSgjelinek 		return (DTD_ELEM_IPD);
34900209230bSgjelinek 	if (strcmp(nm, "net") == 0)
34910209230bSgjelinek 		return (DTD_ELEM_NET);
34920209230bSgjelinek 	if (strcmp(nm, "attr") == 0)
34930209230bSgjelinek 		return (DTD_ELEM_ATTR);
34940209230bSgjelinek 	if (strcmp(nm, "rctl") == 0)
34950209230bSgjelinek 		return (DTD_ELEM_RCTL);
34960209230bSgjelinek 	if (strcmp(nm, "dataset") == 0)
34970209230bSgjelinek 		return (DTD_ELEM_DATASET);
34980209230bSgjelinek 
34990209230bSgjelinek 	return (NULL);
35000209230bSgjelinek }
35010209230bSgjelinek 
35020209230bSgjelinek int
35030209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc)
35040209230bSgjelinek {
35050209230bSgjelinek 	int num = 0;
35060209230bSgjelinek 	const xmlChar *dtd;
35070209230bSgjelinek 	xmlNodePtr cur;
35080209230bSgjelinek 
35090209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
35100209230bSgjelinek 		return (num);
35110209230bSgjelinek 
35120209230bSgjelinek 	if (zonecfg_setent(handle) != Z_OK)
35130209230bSgjelinek 		return (num);
35140209230bSgjelinek 
35150209230bSgjelinek 	for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next)
35160209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd) == 0)
35170209230bSgjelinek 			num++;
35180209230bSgjelinek 
35190209230bSgjelinek 	(void) zonecfg_endent(handle);
35200209230bSgjelinek 
35210209230bSgjelinek 	return (num);
35220209230bSgjelinek }
35230209230bSgjelinek 
35240209230bSgjelinek int
35250209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc)
35260209230bSgjelinek {
35270209230bSgjelinek 	int err;
35280209230bSgjelinek 	const xmlChar *dtd;
35290209230bSgjelinek 	xmlNodePtr cur;
35300209230bSgjelinek 
35310209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
35320209230bSgjelinek 		return (Z_NO_RESOURCE_TYPE);
35330209230bSgjelinek 
35340209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
35350209230bSgjelinek 		return (err);
35360209230bSgjelinek 
35370209230bSgjelinek 	cur = handle->zone_dh_cur;
35380209230bSgjelinek 	while (cur != NULL) {
35390209230bSgjelinek 		xmlNodePtr tmp;
35400209230bSgjelinek 
35410209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd)) {
35420209230bSgjelinek 			cur = cur->next;
35430209230bSgjelinek 			continue;
35440209230bSgjelinek 		}
35450209230bSgjelinek 
35460209230bSgjelinek 		tmp = cur->next;
35470209230bSgjelinek 		xmlUnlinkNode(cur);
35480209230bSgjelinek 		xmlFreeNode(cur);
35490209230bSgjelinek 		cur = tmp;
35500209230bSgjelinek 	}
35510209230bSgjelinek 
35520209230bSgjelinek 	(void) zonecfg_endent(handle);
35530209230bSgjelinek 	return (Z_OK);
35540209230bSgjelinek }
35550209230bSgjelinek 
35560209230bSgjelinek static boolean_t
35570209230bSgjelinek valid_uint(char *s, uint64_t *n)
35580209230bSgjelinek {
35590209230bSgjelinek 	char *endp;
35600209230bSgjelinek 
35610209230bSgjelinek 	/* strtoull accepts '-'?! so we want to flag that as an error */
35620209230bSgjelinek 	if (strchr(s, '-') != NULL)
35630209230bSgjelinek 		return (B_FALSE);
35640209230bSgjelinek 
35650209230bSgjelinek 	errno = 0;
35660209230bSgjelinek 	*n = strtoull(s, &endp, 10);
35670209230bSgjelinek 
35680209230bSgjelinek 	if (errno != 0 || *endp != '\0')
35690209230bSgjelinek 		return (B_FALSE);
35700209230bSgjelinek 	return (B_TRUE);
35710209230bSgjelinek }
35720209230bSgjelinek 
35730209230bSgjelinek /*
35740209230bSgjelinek  * Convert a string representing a number (possibly a fraction) into an integer.
35750209230bSgjelinek  * The string can have a modifier (K, M, G or T).   The modifiers are treated
35760209230bSgjelinek  * as powers of two (not 10).
35770209230bSgjelinek  */
35780209230bSgjelinek int
35790209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes)
35800209230bSgjelinek {
35810209230bSgjelinek 	long double val;
35820209230bSgjelinek 	char *unitp;
35830209230bSgjelinek 	uint64_t scale;
35840209230bSgjelinek 
35850209230bSgjelinek 	if ((val = strtold(str, &unitp)) < 0)
35860209230bSgjelinek 		return (-1);
35870209230bSgjelinek 
35880209230bSgjelinek 	/* remove any leading white space from units string */
35890209230bSgjelinek 	while (isspace(*unitp) != 0)
35900209230bSgjelinek 		++unitp;
35910209230bSgjelinek 
35920209230bSgjelinek 	/* if no units explicitly set, error */
35930209230bSgjelinek 	if (unitp == NULL || *unitp == '\0') {
35940209230bSgjelinek 		scale = 1;
35950209230bSgjelinek 	} else {
35960209230bSgjelinek 		int i;
35970209230bSgjelinek 		char *units[] = {"K", "M", "G", "T", NULL};
35980209230bSgjelinek 
35990209230bSgjelinek 		scale = 1024;
36000209230bSgjelinek 
36010209230bSgjelinek 		/* update scale based on units */
36020209230bSgjelinek 		for (i = 0; units[i] != NULL; i++) {
36030209230bSgjelinek 			if (strcasecmp(unitp, units[i]) == 0)
36040209230bSgjelinek 				break;
36050209230bSgjelinek 			scale <<= 10;
36060209230bSgjelinek 		}
36070209230bSgjelinek 
36080209230bSgjelinek 		if (units[i] == NULL)
36090209230bSgjelinek 			return (-1);
36100209230bSgjelinek 	}
36110209230bSgjelinek 
36120209230bSgjelinek 	*bytes = (uint64_t)(val * scale);
36130209230bSgjelinek 	return (0);
36140209230bSgjelinek }
36150209230bSgjelinek 
36160209230bSgjelinek boolean_t
36170209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr)
36180209230bSgjelinek {
36190209230bSgjelinek 	uint64_t low, high;
36200209230bSgjelinek 
36210209230bSgjelinek 	if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) ||
36220209230bSgjelinek 	    low < 1 || low > high)
36230209230bSgjelinek 		return (B_FALSE);
36240209230bSgjelinek 
36250209230bSgjelinek 	return (B_TRUE);
36260209230bSgjelinek }
36270209230bSgjelinek 
36280209230bSgjelinek boolean_t
36290209230bSgjelinek zonecfg_valid_importance(char *impstr)
36300209230bSgjelinek {
36310209230bSgjelinek 	uint64_t num;
36320209230bSgjelinek 
36330209230bSgjelinek 	if (!valid_uint(impstr, &num))
36340209230bSgjelinek 		return (B_FALSE);
36350209230bSgjelinek 
36360209230bSgjelinek 	return (B_TRUE);
36370209230bSgjelinek }
36380209230bSgjelinek 
36390209230bSgjelinek boolean_t
36400209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit)
36410209230bSgjelinek {
36420209230bSgjelinek 	int i;
36430209230bSgjelinek 
36440209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
36450209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
36460209230bSgjelinek 			break;
36470209230bSgjelinek 
36480209230bSgjelinek 	if (aliases[i].shortname == NULL)
36490209230bSgjelinek 		return (B_FALSE);
36500209230bSgjelinek 
36510209230bSgjelinek 	if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit)
36520209230bSgjelinek 		return (B_FALSE);
36530209230bSgjelinek 
36540209230bSgjelinek 	return (B_TRUE);
36550209230bSgjelinek }
36560209230bSgjelinek 
36570209230bSgjelinek boolean_t
36580209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val)
36590209230bSgjelinek {
36600209230bSgjelinek 	if (zonecfg_str_to_bytes(memstr, mem_val) != 0)
36610209230bSgjelinek 		return (B_FALSE);
36620209230bSgjelinek 
36630209230bSgjelinek 	return (B_TRUE);
36640209230bSgjelinek }
36650209230bSgjelinek 
36660209230bSgjelinek static int
36670209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res)
36680209230bSgjelinek {
36690209230bSgjelinek 	(void) strlcpy(pool_err, pool_strerror(pool_error()), err_size);
36700209230bSgjelinek 	return (res);
36710209230bSgjelinek }
36720209230bSgjelinek 
36730209230bSgjelinek static int
36740209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool,
36750209230bSgjelinek     char *name, int min, int max)
36760209230bSgjelinek {
36770209230bSgjelinek 	pool_resource_t *res;
36780209230bSgjelinek 	pool_elem_t *elem;
36790209230bSgjelinek 	pool_value_t *val;
36800209230bSgjelinek 
36810209230bSgjelinek 	if ((res = pool_resource_create(pconf, "pset", name)) == NULL)
36820209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36830209230bSgjelinek 
36840209230bSgjelinek 	if (pool_associate(pconf, pool, res) != PO_SUCCESS)
36850209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36860209230bSgjelinek 
36870209230bSgjelinek 	if ((elem = pool_resource_to_elem(pconf, res)) == NULL)
36880209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36890209230bSgjelinek 
36900209230bSgjelinek 	if ((val = pool_value_alloc()) == NULL)
36910209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36920209230bSgjelinek 
36930209230bSgjelinek 	/* set the maximum number of cpus for the pset */
36940209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)max);
36950209230bSgjelinek 
36960209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) {
36970209230bSgjelinek 		pool_value_free(val);
36980209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36990209230bSgjelinek 	}
37000209230bSgjelinek 
37010209230bSgjelinek 	/* set the minimum number of cpus for the pset */
37020209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)min);
37030209230bSgjelinek 
37040209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) {
37050209230bSgjelinek 		pool_value_free(val);
37060209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
37070209230bSgjelinek 	}
37080209230bSgjelinek 
37090209230bSgjelinek 	pool_value_free(val);
37100209230bSgjelinek 
37110209230bSgjelinek 	return (Z_OK);
37120209230bSgjelinek }
37130209230bSgjelinek 
37140209230bSgjelinek static int
37150209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name,
37160209230bSgjelinek     struct zone_psettab *pset_tab)
37170209230bSgjelinek {
37180209230bSgjelinek 	pool_t *pool;
37190209230bSgjelinek 	int res = Z_OK;
37200209230bSgjelinek 
37210209230bSgjelinek 	/* create a temporary pool configuration */
37220209230bSgjelinek 	if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) {
37230209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37240209230bSgjelinek 		return (res);
37250209230bSgjelinek 	}
37260209230bSgjelinek 
37270209230bSgjelinek 	if ((pool = pool_create(pconf, name)) == NULL) {
37280209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
37290209230bSgjelinek 		goto done;
37300209230bSgjelinek 	}
37310209230bSgjelinek 
37320209230bSgjelinek 	/* set pool importance */
37330209230bSgjelinek 	if (pset_tab->zone_importance[0] != '\0') {
37340209230bSgjelinek 		pool_elem_t *elem;
37350209230bSgjelinek 		pool_value_t *val;
37360209230bSgjelinek 
37370209230bSgjelinek 		if ((elem = pool_to_elem(pconf, pool)) == NULL) {
37380209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37390209230bSgjelinek 			goto done;
37400209230bSgjelinek 		}
37410209230bSgjelinek 
37420209230bSgjelinek 		if ((val = pool_value_alloc()) == NULL) {
37430209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37440209230bSgjelinek 			goto done;
37450209230bSgjelinek 		}
37460209230bSgjelinek 
37470209230bSgjelinek 		pool_value_set_int64(val,
37480209230bSgjelinek 		    (int64_t)atoi(pset_tab->zone_importance));
37490209230bSgjelinek 
37500209230bSgjelinek 		if (pool_put_property(pconf, elem, "pool.importance", val)
37510209230bSgjelinek 		    != PO_SUCCESS) {
37520209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37530209230bSgjelinek 			pool_value_free(val);
37540209230bSgjelinek 			goto done;
37550209230bSgjelinek 		}
37560209230bSgjelinek 
37570209230bSgjelinek 		pool_value_free(val);
37580209230bSgjelinek 	}
37590209230bSgjelinek 
37600209230bSgjelinek 	if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name,
37610209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_min),
37620209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_max))) != Z_OK)
37630209230bSgjelinek 		goto done;
37640209230bSgjelinek 
37650209230bSgjelinek 	/* validation */
37660209230bSgjelinek 	if (pool_conf_status(pconf) == POF_INVALID) {
37670209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37680209230bSgjelinek 		goto done;
37690209230bSgjelinek 	}
37700209230bSgjelinek 
37710209230bSgjelinek 	/*
37720209230bSgjelinek 	 * This validation is the one we expect to fail if the user specified
37730209230bSgjelinek 	 * an invalid configuration (too many cpus) for this system.
37740209230bSgjelinek 	 */
37750209230bSgjelinek 	if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) {
37760209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
37770209230bSgjelinek 		goto done;
37780209230bSgjelinek 	}
37790209230bSgjelinek 
37800209230bSgjelinek 	/*
37810209230bSgjelinek 	 * Commit the dynamic configuration but not the pool configuration
37820209230bSgjelinek 	 * file.
37830209230bSgjelinek 	 */
37840209230bSgjelinek 	if (pool_conf_commit(pconf, 1) != PO_SUCCESS)
37850209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37860209230bSgjelinek 
37870209230bSgjelinek done:
37880209230bSgjelinek 	(void) pool_conf_close(pconf);
37890209230bSgjelinek 	return (res);
37900209230bSgjelinek }
37910209230bSgjelinek 
37920209230bSgjelinek static int
37930209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset,
37940209230bSgjelinek     struct zone_psettab *pset_tab)
37950209230bSgjelinek {
37960209230bSgjelinek 	int nfound = 0;
37970209230bSgjelinek 	pool_elem_t *pe;
37980209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
37990209230bSgjelinek 	uint64_t val_uint;
38000209230bSgjelinek 
38010209230bSgjelinek 	if (pool != NULL) {
38020209230bSgjelinek 		pe = pool_to_elem(pconf, pool);
38030209230bSgjelinek 		if (pool_get_property(pconf, pe, "pool.importance", pv)
38040209230bSgjelinek 		    != POC_INVAL) {
38050209230bSgjelinek 			int64_t val_int;
38060209230bSgjelinek 
38070209230bSgjelinek 			(void) pool_value_get_int64(pv, &val_int);
38080209230bSgjelinek 			(void) snprintf(pset_tab->zone_importance,
38090209230bSgjelinek 			    sizeof (pset_tab->zone_importance), "%d", val_int);
38100209230bSgjelinek 			nfound++;
38110209230bSgjelinek 		}
38120209230bSgjelinek 	}
38130209230bSgjelinek 
38140209230bSgjelinek 	if (pset != NULL) {
38150209230bSgjelinek 		pe = pool_resource_to_elem(pconf, pset);
38160209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) {
38170209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
38180209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_min,
38190209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_min), "%u", val_uint);
38200209230bSgjelinek 			nfound++;
38210209230bSgjelinek 		}
38220209230bSgjelinek 
38230209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) {
38240209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
38250209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_max,
38260209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_max), "%u", val_uint);
38270209230bSgjelinek 			nfound++;
38280209230bSgjelinek 		}
38290209230bSgjelinek 	}
38300209230bSgjelinek 
38310209230bSgjelinek 	pool_value_free(pv);
38320209230bSgjelinek 
38330209230bSgjelinek 	if (nfound == 3)
38340209230bSgjelinek 		return (PO_SUCCESS);
38350209230bSgjelinek 
38360209230bSgjelinek 	return (PO_FAIL);
38370209230bSgjelinek }
38380209230bSgjelinek 
38390209230bSgjelinek /*
38400209230bSgjelinek  * Determine if a tmp pool is configured and if so, if the configuration is
38410209230bSgjelinek  * still valid or if it has been changed since the tmp pool was created.
38420209230bSgjelinek  * If the tmp pool configuration is no longer valid, delete the tmp pool.
38430209230bSgjelinek  *
38440209230bSgjelinek  * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration.
38450209230bSgjelinek  */
38460209230bSgjelinek static int
38470209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err,
38480209230bSgjelinek     int err_size, struct zone_psettab *pset_tab, boolean_t *exists)
38490209230bSgjelinek {
38500209230bSgjelinek 	int res = Z_OK;
38510209230bSgjelinek 	pool_t *pool;
38520209230bSgjelinek 	pool_resource_t *pset;
38530209230bSgjelinek 	struct zone_psettab pset_current;
38540209230bSgjelinek 
38550209230bSgjelinek 	*exists = B_FALSE;
38560209230bSgjelinek 
38570209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
38580209230bSgjelinek 	    != PO_SUCCESS) {
38590209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
38600209230bSgjelinek 		return (res);
38610209230bSgjelinek 	}
38620209230bSgjelinek 
38630209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
38640209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
38650209230bSgjelinek 
38660209230bSgjelinek 	if (pool == NULL && pset == NULL) {
38670209230bSgjelinek 		/* no tmp pool configured */
38680209230bSgjelinek 		goto done;
38690209230bSgjelinek 	}
38700209230bSgjelinek 
38710209230bSgjelinek 	/*
38720209230bSgjelinek 	 * If an existing tmp pool for this zone is configured with the proper
38730209230bSgjelinek 	 * settings, then the tmp pool is valid.
38740209230bSgjelinek 	 */
38750209230bSgjelinek 	if (get_running_tmp_pset(pconf, pool, pset, &pset_current)
38760209230bSgjelinek 	    == PO_SUCCESS &&
38770209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_min,
38780209230bSgjelinek 	    pset_current.zone_ncpu_min) == 0 &&
38790209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_max,
38800209230bSgjelinek 	    pset_current.zone_ncpu_max) == 0 &&
38810209230bSgjelinek 	    strcmp(pset_tab->zone_importance,
38820209230bSgjelinek 	    pset_current.zone_importance) == 0) {
38830209230bSgjelinek 		*exists = B_TRUE;
38840209230bSgjelinek 
38850209230bSgjelinek 	} else {
38860209230bSgjelinek 		/*
38870209230bSgjelinek 		 * An out-of-date tmp pool configuration exists.  Delete it
38880209230bSgjelinek 		 * so that we can create the correct tmp pool config.
38890209230bSgjelinek 		 */
38900209230bSgjelinek 		if (pset != NULL &&
38910209230bSgjelinek 		    pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
38920209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38930209230bSgjelinek 			goto done;
38940209230bSgjelinek 		}
38950209230bSgjelinek 
38960209230bSgjelinek 		if (pool != NULL &&
38970209230bSgjelinek 		    pool_destroy(pconf, pool) != PO_SUCCESS) {
38980209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38990209230bSgjelinek 			goto done;
39000209230bSgjelinek 		}
39010209230bSgjelinek 
39020209230bSgjelinek 		/* commit dynamic config */
39030209230bSgjelinek 		if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
39040209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
39050209230bSgjelinek 	}
39060209230bSgjelinek 
39070209230bSgjelinek done:
39080209230bSgjelinek 	(void) pool_conf_close(pconf);
39090209230bSgjelinek 
39100209230bSgjelinek 	return (res);
39110209230bSgjelinek }
39120209230bSgjelinek 
39130209230bSgjelinek /*
39140209230bSgjelinek  * Destroy any existing tmp pool.
39150209230bSgjelinek  */
39160209230bSgjelinek int
39170209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size)
39180209230bSgjelinek {
39190209230bSgjelinek 	int status;
39200209230bSgjelinek 	int res = Z_OK;
39210209230bSgjelinek 	pool_conf_t *pconf;
39220209230bSgjelinek 	pool_t *pool;
39230209230bSgjelinek 	pool_resource_t *pset;
39240209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
39250209230bSgjelinek 
39260209230bSgjelinek 	/* if pools not enabled then nothing to do */
39270209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
39280209230bSgjelinek 		return (Z_OK);
39290209230bSgjelinek 
39300209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
39310209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39320209230bSgjelinek 
39330209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
39340209230bSgjelinek 
39350209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
39360209230bSgjelinek 	    != PO_SUCCESS) {
39370209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39380209230bSgjelinek 		pool_conf_free(pconf);
39390209230bSgjelinek 		return (res);
39400209230bSgjelinek 	}
39410209230bSgjelinek 
39420209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
39430209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
39440209230bSgjelinek 
39450209230bSgjelinek 	if (pool == NULL && pset == NULL) {
39460209230bSgjelinek 		/* nothing to destroy, we're done */
39470209230bSgjelinek 		goto done;
39480209230bSgjelinek 	}
39490209230bSgjelinek 
39500209230bSgjelinek 	if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
39510209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39520209230bSgjelinek 		goto done;
39530209230bSgjelinek 	}
39540209230bSgjelinek 
39550209230bSgjelinek 	if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) {
39560209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39570209230bSgjelinek 		goto done;
39580209230bSgjelinek 	}
39590209230bSgjelinek 
39600209230bSgjelinek 	/* commit dynamic config */
39610209230bSgjelinek 	if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
39620209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39630209230bSgjelinek 
39640209230bSgjelinek done:
39650209230bSgjelinek 	(void) pool_conf_close(pconf);
39660209230bSgjelinek 	pool_conf_free(pconf);
39670209230bSgjelinek 
39680209230bSgjelinek 	return (res);
39690209230bSgjelinek }
39700209230bSgjelinek 
39710209230bSgjelinek /*
39720209230bSgjelinek  * Attempt to bind to a tmp pool for this zone.  If there is no tmp pool
39730209230bSgjelinek  * configured, we just return Z_OK.
39740209230bSgjelinek  *
39750209230bSgjelinek  * We either attempt to create the tmp pool for this zone or rebind to an
39760209230bSgjelinek  * existing tmp pool for this zone.
39770209230bSgjelinek  *
39780209230bSgjelinek  * Rebinding is used when a zone with a tmp pool reboots so that we don't have
39790209230bSgjelinek  * to recreate the tmp pool.  To do this we need to be sure we work correctly
39800209230bSgjelinek  * for the following cases:
39810209230bSgjelinek  *
39820209230bSgjelinek  *	- there is an existing, properly configured tmp pool.
39830209230bSgjelinek  *	- zonecfg added tmp pool after zone was booted, must now create.
39840209230bSgjelinek  *	- zonecfg updated tmp pool config after zone was booted, in this case
39850209230bSgjelinek  *	  we destroy the old tmp pool and create a new one.
39860209230bSgjelinek  */
39870209230bSgjelinek int
39880209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
39890209230bSgjelinek     int err_size)
39900209230bSgjelinek {
39910209230bSgjelinek 	struct zone_psettab pset_tab;
39920209230bSgjelinek 	int err;
39930209230bSgjelinek 	int status;
39940209230bSgjelinek 	pool_conf_t *pconf;
39950209230bSgjelinek 	boolean_t exists;
39960209230bSgjelinek 	char zone_name[ZONENAME_MAX];
39970209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
39980209230bSgjelinek 
39990209230bSgjelinek 	(void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name));
40000209230bSgjelinek 
40010209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
40020209230bSgjelinek 
40030209230bSgjelinek 	/* if no temporary pool configured, we're done */
40040209230bSgjelinek 	if (err == Z_NO_ENTRY)
40050209230bSgjelinek 		return (Z_OK);
40060209230bSgjelinek 
40070209230bSgjelinek 	/*
40080209230bSgjelinek 	 * importance might not have a value but we need to validate it here,
40090209230bSgjelinek 	 * so set the default.
40100209230bSgjelinek 	 */
40110209230bSgjelinek 	if (pset_tab.zone_importance[0] == '\0')
40120209230bSgjelinek 		(void) strlcpy(pset_tab.zone_importance, "1",
40130209230bSgjelinek 		    sizeof (pset_tab.zone_importance));
40140209230bSgjelinek 
40150209230bSgjelinek 	/* if pools not enabled, enable them now */
40160209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
40170209230bSgjelinek 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS)
40180209230bSgjelinek 			return (Z_POOL_ENABLE);
40190209230bSgjelinek 	}
40200209230bSgjelinek 
40210209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
40220209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40230209230bSgjelinek 
40240209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
40250209230bSgjelinek 
40260209230bSgjelinek 	/*
40270209230bSgjelinek 	 * Check if a valid tmp pool/pset already exists.  If so, we just
40280209230bSgjelinek 	 * reuse it.
40290209230bSgjelinek 	 */
40300209230bSgjelinek 	if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size,
40310209230bSgjelinek 	    &pset_tab, &exists)) != Z_OK) {
40320209230bSgjelinek 		pool_conf_free(pconf);
40330209230bSgjelinek 		return (err);
40340209230bSgjelinek 	}
40350209230bSgjelinek 
40360209230bSgjelinek 	if (!exists)
40370209230bSgjelinek 		err = create_tmp_pool(pool_err, err_size, pconf, tmp_name,
40380209230bSgjelinek 		    &pset_tab);
40390209230bSgjelinek 
40400209230bSgjelinek 	pool_conf_free(pconf);
40410209230bSgjelinek 
40420209230bSgjelinek 	if (err != Z_OK)
40430209230bSgjelinek 		return (err);
40440209230bSgjelinek 
40450209230bSgjelinek 	/* Bind the zone to the pool. */
40460209230bSgjelinek 	if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS)
40470209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL_BIND));
40480209230bSgjelinek 
40490209230bSgjelinek 	return (Z_OK);
40500209230bSgjelinek }
40510209230bSgjelinek 
40520209230bSgjelinek /*
40530209230bSgjelinek  * Attempt to bind to a permanent pool for this zone.  If there is no
40540209230bSgjelinek  * permanent pool configured, we just return Z_OK.
40550209230bSgjelinek  */
40560209230bSgjelinek int
40570209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
40580209230bSgjelinek     int err_size)
40590209230bSgjelinek {
40600209230bSgjelinek 	pool_conf_t *poolconf;
40610209230bSgjelinek 	pool_t *pool;
40620209230bSgjelinek 	char poolname[MAXPATHLEN];
40630209230bSgjelinek 	int status;
40640209230bSgjelinek 	int error;
40650209230bSgjelinek 
40660209230bSgjelinek 	/*
40670209230bSgjelinek 	 * Find the pool mentioned in the zone configuration, and bind to it.
40680209230bSgjelinek 	 */
40690209230bSgjelinek 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
40700209230bSgjelinek 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
40710209230bSgjelinek 		/*
40720209230bSgjelinek 		 * The property is not set on the zone, so the pool
40730209230bSgjelinek 		 * should be bound to the default pool.  But that's
40740209230bSgjelinek 		 * already done by the kernel, so we can just return.
40750209230bSgjelinek 		 */
40760209230bSgjelinek 		return (Z_OK);
40770209230bSgjelinek 	}
40780209230bSgjelinek 	if (error != Z_OK) {
40790209230bSgjelinek 		/*
40800209230bSgjelinek 		 * Not an error, even though it shouldn't be happening.
40810209230bSgjelinek 		 */
40820209230bSgjelinek 		return (Z_OK);
40830209230bSgjelinek 	}
40840209230bSgjelinek 	/*
40850209230bSgjelinek 	 * Don't do anything if pools aren't enabled.
40860209230bSgjelinek 	 */
40870209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
40880209230bSgjelinek 		return (Z_POOLS_NOT_ACTIVE);
40890209230bSgjelinek 
40900209230bSgjelinek 	/*
40910209230bSgjelinek 	 * Try to provide a sane error message if the requested pool doesn't
40920209230bSgjelinek 	 * exist.
40930209230bSgjelinek 	 */
40940209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
40950209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40960209230bSgjelinek 
40970209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
40980209230bSgjelinek 	    PO_SUCCESS) {
40990209230bSgjelinek 		pool_conf_free(poolconf);
41000209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
41010209230bSgjelinek 	}
41020209230bSgjelinek 	pool = pool_get_pool(poolconf, poolname);
41030209230bSgjelinek 	(void) pool_conf_close(poolconf);
41040209230bSgjelinek 	pool_conf_free(poolconf);
41050209230bSgjelinek 	if (pool == NULL)
41060209230bSgjelinek 		return (Z_NO_POOL);
41070209230bSgjelinek 
41080209230bSgjelinek 	/*
41090209230bSgjelinek 	 * Bind the zone to the pool.
41100209230bSgjelinek 	 */
41110209230bSgjelinek 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
41120209230bSgjelinek 		/* if bind fails, return poolname for the error msg */
41130209230bSgjelinek 		(void) strlcpy(pool_err, poolname, err_size);
41140209230bSgjelinek 		return (Z_POOL_BIND);
41150209230bSgjelinek 	}
41160209230bSgjelinek 
41170209230bSgjelinek 	return (Z_OK);
41180209230bSgjelinek }
41190209230bSgjelinek 
41200209230bSgjelinek 
41210209230bSgjelinek static boolean_t
41220209230bSgjelinek svc_enabled(char *svc_name)
41230209230bSgjelinek {
41240209230bSgjelinek 	scf_simple_prop_t	*prop;
41250209230bSgjelinek 	boolean_t		found = B_FALSE;
41260209230bSgjelinek 
41270209230bSgjelinek 	prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL,
41280209230bSgjelinek 	    SCF_PROPERTY_ENABLED);
41290209230bSgjelinek 
41300209230bSgjelinek 	if (scf_simple_prop_numvalues(prop) == 1 &&
41310209230bSgjelinek 	    *scf_simple_prop_next_boolean(prop) != 0)
41320209230bSgjelinek 		found = B_TRUE;
41330209230bSgjelinek 
41340209230bSgjelinek 	scf_simple_prop_free(prop);
41350209230bSgjelinek 
41360209230bSgjelinek 	return (found);
41370209230bSgjelinek }
41380209230bSgjelinek 
41390209230bSgjelinek /*
41400209230bSgjelinek  * If the zone has capped-memory, make sure the rcap service is enabled.
41410209230bSgjelinek  */
41420209230bSgjelinek int
41430209230bSgjelinek zonecfg_enable_rcapd(char *err, int size)
41440209230bSgjelinek {
41450209230bSgjelinek 	if (!svc_enabled(RCAP_SERVICE) &&
41460209230bSgjelinek 	    smf_enable_instance(RCAP_SERVICE, 0) == -1) {
41470209230bSgjelinek 		(void) strlcpy(err, scf_strerror(scf_error()), size);
41480209230bSgjelinek 		return (Z_SYSTEM);
41490209230bSgjelinek 	}
41500209230bSgjelinek 
41510209230bSgjelinek 	return (Z_OK);
41520209230bSgjelinek }
41530209230bSgjelinek 
41540209230bSgjelinek /*
41550209230bSgjelinek  * Return true if pset has cpu range specified and poold is not enabled.
41560209230bSgjelinek  */
41570209230bSgjelinek boolean_t
41580209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle)
41590209230bSgjelinek {
41600209230bSgjelinek 	struct zone_psettab pset_tab;
41610209230bSgjelinek 	int min, max;
41620209230bSgjelinek 	int err;
41630209230bSgjelinek 
41640209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
41650209230bSgjelinek 
41660209230bSgjelinek 	/* if no temporary pool configured, we're done */
41670209230bSgjelinek 	if (err == Z_NO_ENTRY)
41680209230bSgjelinek 		return (B_FALSE);
41690209230bSgjelinek 
41700209230bSgjelinek 	min = atoi(pset_tab.zone_ncpu_min);
41710209230bSgjelinek 	max = atoi(pset_tab.zone_ncpu_max);
41720209230bSgjelinek 
41730209230bSgjelinek 	/* range not specified, no need for poold */
41740209230bSgjelinek 	if (min == max)
41750209230bSgjelinek 		return (B_FALSE);
41760209230bSgjelinek 
41770209230bSgjelinek 	/* we have a range, check if poold service is enabled */
41780209230bSgjelinek 	if (svc_enabled(POOLD_SERVICE))
41790209230bSgjelinek 		return (B_FALSE);
41800209230bSgjelinek 
41810209230bSgjelinek 	return (B_TRUE);
41820209230bSgjelinek }
41830209230bSgjelinek 
41840209230bSgjelinek static int
41850209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize)
41860209230bSgjelinek {
41870209230bSgjelinek 	int status;
41880209230bSgjelinek 	pool_conf_t *poolconf;
41890209230bSgjelinek 	pool_t *pool;
41900209230bSgjelinek 	pool_elem_t *pe;
41910209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
41920209230bSgjelinek 	const char *sched_str;
41930209230bSgjelinek 
41940209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
41950209230bSgjelinek 		return (Z_NO_POOL);
41960209230bSgjelinek 
41970209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
41980209230bSgjelinek 		return (Z_NO_POOL);
41990209230bSgjelinek 
42000209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
42010209230bSgjelinek 	    PO_SUCCESS) {
42020209230bSgjelinek 		pool_conf_free(poolconf);
42030209230bSgjelinek 		return (Z_NO_POOL);
42040209230bSgjelinek 	}
42050209230bSgjelinek 
42060209230bSgjelinek 	if ((pool = pool_get_pool(poolconf, poolname)) == NULL) {
42070209230bSgjelinek 		(void) pool_conf_close(poolconf);
42080209230bSgjelinek 		pool_conf_free(poolconf);
42090209230bSgjelinek 		return (Z_NO_POOL);
42100209230bSgjelinek 	}
42110209230bSgjelinek 
42120209230bSgjelinek 	pe = pool_to_elem(poolconf, pool);
42130209230bSgjelinek 	if (pool_get_property(poolconf, pe, "pool.scheduler", pv)
42140209230bSgjelinek 	    != POC_INVAL) {
42150209230bSgjelinek 		(void) pool_value_get_string(pv, &sched_str);
42160209230bSgjelinek 		if (strlcpy(class, sched_str, clsize) >= clsize)
42170209230bSgjelinek 			return (Z_TOO_BIG);
42180209230bSgjelinek 	}
42190209230bSgjelinek 
42200209230bSgjelinek 	(void) pool_conf_close(poolconf);
42210209230bSgjelinek 	pool_conf_free(poolconf);
42220209230bSgjelinek 	return (Z_OK);
42230209230bSgjelinek }
42240209230bSgjelinek 
42250209230bSgjelinek /*
42260209230bSgjelinek  * Get the default scheduling class for the zone.  This will either be the
42270209230bSgjelinek  * class set on the zone's pool or the system default scheduling class.
42280209230bSgjelinek  */
42290209230bSgjelinek int
42300209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize)
42310209230bSgjelinek {
42320209230bSgjelinek 	char poolname[MAXPATHLEN];
42330209230bSgjelinek 
42340209230bSgjelinek 	if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) {
42350209230bSgjelinek 		/* check if the zone's pool specified a sched class */
42360209230bSgjelinek 		if (get_pool_sched_class(poolname, class, clsize) == Z_OK)
42370209230bSgjelinek 			return (Z_OK);
42380209230bSgjelinek 	}
42390209230bSgjelinek 
42400209230bSgjelinek 	if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1)
42410209230bSgjelinek 		return (Z_TOO_BIG);
42420209230bSgjelinek 
42430209230bSgjelinek 	return (Z_OK);
42440209230bSgjelinek }
42450209230bSgjelinek 
42467c478bd9Sstevel@tonic-gate int
42477c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle)
42487c478bd9Sstevel@tonic-gate {
42497c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
42507c478bd9Sstevel@tonic-gate }
42517c478bd9Sstevel@tonic-gate 
42527c478bd9Sstevel@tonic-gate int
42537c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
42547c478bd9Sstevel@tonic-gate {
42557c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options;
42567c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
42577c478bd9Sstevel@tonic-gate 	int err;
42587c478bd9Sstevel@tonic-gate 
42597c478bd9Sstevel@tonic-gate 	if (handle == NULL)
42607c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
42617c478bd9Sstevel@tonic-gate 
42627c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
42637c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
42667c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
42677c478bd9Sstevel@tonic-gate 			break;
42687c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
42697c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
42707c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
42717c478bd9Sstevel@tonic-gate 	}
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
42747c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
42757c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
42767c478bd9Sstevel@tonic-gate 		return (err);
42777c478bd9Sstevel@tonic-gate 	}
42787c478bd9Sstevel@tonic-gate 
42797c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
42807c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
42817c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
42827c478bd9Sstevel@tonic-gate 		return (err);
42837c478bd9Sstevel@tonic-gate 	}
42847c478bd9Sstevel@tonic-gate 
42857c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
42867c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
42877c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
42887c478bd9Sstevel@tonic-gate 		return (err);
42897c478bd9Sstevel@tonic-gate 	}
42907c478bd9Sstevel@tonic-gate 
42917c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
42927c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
42937c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
42947c478bd9Sstevel@tonic-gate 		return (err);
42957c478bd9Sstevel@tonic-gate 	}
42967c478bd9Sstevel@tonic-gate 
42977c478bd9Sstevel@tonic-gate 	/* OK for options to be NULL */
42987c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
42997c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
43007c478bd9Sstevel@tonic-gate 	    options = options->next) {
43017c478bd9Sstevel@tonic-gate 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
43027c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK)
43037c478bd9Sstevel@tonic-gate 			break;
43047c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
43057c478bd9Sstevel@tonic-gate 			break;
43067c478bd9Sstevel@tonic-gate 	}
43077c478bd9Sstevel@tonic-gate 
43087c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
43097c478bd9Sstevel@tonic-gate 	return (Z_OK);
43107c478bd9Sstevel@tonic-gate }
43117c478bd9Sstevel@tonic-gate 
43127c478bd9Sstevel@tonic-gate int
43137c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle)
43147c478bd9Sstevel@tonic-gate {
43157c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
43167c478bd9Sstevel@tonic-gate }
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate int
43197c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle)
43207c478bd9Sstevel@tonic-gate {
43217c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
43227c478bd9Sstevel@tonic-gate }
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate int
43257c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
43267c478bd9Sstevel@tonic-gate {
43277c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
43287c478bd9Sstevel@tonic-gate 	int err;
43297c478bd9Sstevel@tonic-gate 
43307c478bd9Sstevel@tonic-gate 	if (handle == NULL)
43317c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
43327c478bd9Sstevel@tonic-gate 
43337c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
43347c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
43377c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
43387c478bd9Sstevel@tonic-gate 			break;
43397c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
43407c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43417c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43427c478bd9Sstevel@tonic-gate 	}
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
43457c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
43467c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43477c478bd9Sstevel@tonic-gate 		return (err);
43487c478bd9Sstevel@tonic-gate 	}
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
43517c478bd9Sstevel@tonic-gate 	return (Z_OK);
43527c478bd9Sstevel@tonic-gate }
43537c478bd9Sstevel@tonic-gate 
43547c478bd9Sstevel@tonic-gate int
43557c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle)
43567c478bd9Sstevel@tonic-gate {
43577c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
43587c478bd9Sstevel@tonic-gate }
43597c478bd9Sstevel@tonic-gate 
43607c478bd9Sstevel@tonic-gate int
43617c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle)
43627c478bd9Sstevel@tonic-gate {
43637c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
43647c478bd9Sstevel@tonic-gate }
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate int
43677c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
43687c478bd9Sstevel@tonic-gate {
43697c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
43707c478bd9Sstevel@tonic-gate 	int err;
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate 	if (handle == NULL)
43737c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
43747c478bd9Sstevel@tonic-gate 
43757c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
43767c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43777c478bd9Sstevel@tonic-gate 
43787c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
43797c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
43807c478bd9Sstevel@tonic-gate 			break;
43817c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
43827c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43837c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43847c478bd9Sstevel@tonic-gate 	}
43857c478bd9Sstevel@tonic-gate 
43867c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
43877c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
43887c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43897c478bd9Sstevel@tonic-gate 		return (err);
43907c478bd9Sstevel@tonic-gate 	}
43917c478bd9Sstevel@tonic-gate 
43927c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
43937c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
43947c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43957c478bd9Sstevel@tonic-gate 		return (err);
43967c478bd9Sstevel@tonic-gate 	}
43977c478bd9Sstevel@tonic-gate 
4398*de860bd9Sgfaden 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
4399*de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter,
4400*de860bd9Sgfaden 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK) {
4401*de860bd9Sgfaden 		handle->zone_dh_cur = handle->zone_dh_top;
4402*de860bd9Sgfaden 		return (err);
4403*de860bd9Sgfaden 	}
4404*de860bd9Sgfaden 
44057c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
44067c478bd9Sstevel@tonic-gate 	return (Z_OK);
44077c478bd9Sstevel@tonic-gate }
44087c478bd9Sstevel@tonic-gate 
44097c478bd9Sstevel@tonic-gate int
44107c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle)
44117c478bd9Sstevel@tonic-gate {
44127c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
44137c478bd9Sstevel@tonic-gate }
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate int
44167c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle)
44177c478bd9Sstevel@tonic-gate {
44187c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
44197c478bd9Sstevel@tonic-gate }
44207c478bd9Sstevel@tonic-gate 
44217c478bd9Sstevel@tonic-gate int
44227c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
44237c478bd9Sstevel@tonic-gate {
44247c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
44257c478bd9Sstevel@tonic-gate 	int err;
44267c478bd9Sstevel@tonic-gate 
44277c478bd9Sstevel@tonic-gate 	if (handle == NULL)
44287c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44297c478bd9Sstevel@tonic-gate 
44307c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
44317c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44327c478bd9Sstevel@tonic-gate 
44337c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44347c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
44357c478bd9Sstevel@tonic-gate 			break;
44367c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
44377c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44387c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44397c478bd9Sstevel@tonic-gate 	}
44407c478bd9Sstevel@tonic-gate 
44417c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
44427c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
44437c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44447c478bd9Sstevel@tonic-gate 		return (err);
44457c478bd9Sstevel@tonic-gate 	}
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
44487c478bd9Sstevel@tonic-gate 	return (Z_OK);
44497c478bd9Sstevel@tonic-gate }
44507c478bd9Sstevel@tonic-gate 
44517c478bd9Sstevel@tonic-gate int
44527c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle)
44537c478bd9Sstevel@tonic-gate {
44547c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
44557c478bd9Sstevel@tonic-gate }
44567c478bd9Sstevel@tonic-gate 
44577c478bd9Sstevel@tonic-gate int
44587c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle)
44597c478bd9Sstevel@tonic-gate {
44607c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
44617c478bd9Sstevel@tonic-gate }
44627c478bd9Sstevel@tonic-gate 
44637c478bd9Sstevel@tonic-gate int
44647c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
44657c478bd9Sstevel@tonic-gate {
44667c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
44677c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
44687c478bd9Sstevel@tonic-gate 	int err;
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 	if (handle == NULL)
44717c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44727c478bd9Sstevel@tonic-gate 
44737c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
44747c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44777c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
44787c478bd9Sstevel@tonic-gate 			break;
44797c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
44807c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44817c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44827c478bd9Sstevel@tonic-gate 	}
44837c478bd9Sstevel@tonic-gate 
44847c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
44857c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
44867c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44877c478bd9Sstevel@tonic-gate 		return (err);
44887c478bd9Sstevel@tonic-gate 	}
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate 	tabptr->zone_rctl_valptr = NULL;
44917c478bd9Sstevel@tonic-gate 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
44927c478bd9Sstevel@tonic-gate 		valptr = (struct zone_rctlvaltab *)malloc(
44937c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctlvaltab));
44947c478bd9Sstevel@tonic-gate 		if (valptr == NULL)
44957c478bd9Sstevel@tonic-gate 			return (Z_NOMEM);
44967c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
44977c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
44987c478bd9Sstevel@tonic-gate 			break;
44997c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
45007c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
45017c478bd9Sstevel@tonic-gate 			break;
45027c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
45037c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
45047c478bd9Sstevel@tonic-gate 			break;
45057c478bd9Sstevel@tonic-gate 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
45067c478bd9Sstevel@tonic-gate 			break;
45077c478bd9Sstevel@tonic-gate 	}
45087c478bd9Sstevel@tonic-gate 
45097c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45107c478bd9Sstevel@tonic-gate 	return (Z_OK);
45117c478bd9Sstevel@tonic-gate }
45127c478bd9Sstevel@tonic-gate 
45137c478bd9Sstevel@tonic-gate int
45147c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle)
45157c478bd9Sstevel@tonic-gate {
45167c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45177c478bd9Sstevel@tonic-gate }
45187c478bd9Sstevel@tonic-gate 
45197c478bd9Sstevel@tonic-gate int
45207c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle)
45217c478bd9Sstevel@tonic-gate {
45227c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
45237c478bd9Sstevel@tonic-gate }
45247c478bd9Sstevel@tonic-gate 
45257c478bd9Sstevel@tonic-gate int
45267c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
45277c478bd9Sstevel@tonic-gate {
45287c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
45297c478bd9Sstevel@tonic-gate 	int err;
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate 	if (handle == NULL)
45327c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
45357c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45367c478bd9Sstevel@tonic-gate 
45377c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
45387c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
45397c478bd9Sstevel@tonic-gate 			break;
45407c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
45417c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45427c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45437c478bd9Sstevel@tonic-gate 	}
45447c478bd9Sstevel@tonic-gate 
45457c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
45467c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
45477c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45487c478bd9Sstevel@tonic-gate 		return (err);
45497c478bd9Sstevel@tonic-gate 	}
45507c478bd9Sstevel@tonic-gate 
45517c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
45527c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
45537c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45547c478bd9Sstevel@tonic-gate 		return (err);
45557c478bd9Sstevel@tonic-gate 	}
45567c478bd9Sstevel@tonic-gate 
45577c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
45587c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
45597c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45607c478bd9Sstevel@tonic-gate 		return (err);
45617c478bd9Sstevel@tonic-gate 	}
45627c478bd9Sstevel@tonic-gate 
45637c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45647c478bd9Sstevel@tonic-gate 	return (Z_OK);
45657c478bd9Sstevel@tonic-gate }
45667c478bd9Sstevel@tonic-gate 
45677c478bd9Sstevel@tonic-gate int
45687c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle)
45697c478bd9Sstevel@tonic-gate {
45707c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45717c478bd9Sstevel@tonic-gate }
45727c478bd9Sstevel@tonic-gate 
4573ffbafc53Scomay /*
4574ffbafc53Scomay  * The privileges available on the system and described in privileges(5)
45759acbbeafSnn35248  * fall into four categories with respect to non-global zones:
45769acbbeafSnn35248  *
45779acbbeafSnn35248  *      Default set of privileges considered safe for all non-global
45789acbbeafSnn35248  *      zones.  These privileges are "safe" in the sense that a
45799acbbeafSnn35248  *      privileged process in the zone cannot affect processes in any
45809acbbeafSnn35248  *      other zone on the system.
45819acbbeafSnn35248  *
45829acbbeafSnn35248  *      Set of privileges not currently permitted within a non-global
45839acbbeafSnn35248  *      zone.  These privileges are considered by default, "unsafe,"
45849acbbeafSnn35248  *      and include ones which affect global resources (such as the
45859acbbeafSnn35248  *      system clock or physical memory) or are overly broad and cover
45869acbbeafSnn35248  *      more than one mechanism in the system.  In other cases, there
45879acbbeafSnn35248  *      has not been sufficient virtualization in the parts of the
45889acbbeafSnn35248  *      system the privilege covers to allow its use within a
45899acbbeafSnn35248  *      non-global zone.
45909acbbeafSnn35248  *
45919acbbeafSnn35248  *      Set of privileges required in order to get a zone booted and
45929acbbeafSnn35248  *      init(1M) started.  These cannot be removed from the zone's
45939acbbeafSnn35248  *      privilege set.
45949acbbeafSnn35248  *
45959acbbeafSnn35248  * All other privileges are optional and are potentially useful for
4596ffbafc53Scomay  * processes executing inside a non-global zone.
4597ffbafc53Scomay  *
4598ffbafc53Scomay  * When privileges are added to the system, a determination needs to be
4599ffbafc53Scomay  * made as to which category the privilege belongs to.  Ideally,
4600ffbafc53Scomay  * privileges should be fine-grained enough and the mechanisms they cover
4601ffbafc53Scomay  * virtualized enough so that they can be made available to non-global
4602ffbafc53Scomay  * zones.
4603ffbafc53Scomay  */
4604ffbafc53Scomay 
4605ffbafc53Scomay /*
4606ffbafc53Scomay  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
4607ffbafc53Scomay  * the privilege string separator can be any character, although it is
4608ffbafc53Scomay  * usually a comma character, define these here as well in the event that
4609ffbafc53Scomay  * they change or are augmented in the future.
4610ffbafc53Scomay  */
4611ffbafc53Scomay #define	BASIC_TOKEN		"basic"
4612ffbafc53Scomay #define	DEFAULT_TOKEN		"default"
4613ffbafc53Scomay #define	ZONE_TOKEN		"zone"
4614ffbafc53Scomay #define	TOKEN_PRIV_CHAR		','
4615ffbafc53Scomay #define	TOKEN_PRIV_STR		","
4616ffbafc53Scomay 
46179acbbeafSnn35248 typedef struct priv_node {
46189acbbeafSnn35248 	struct priv_node	*pn_next;	/* Next privilege */
46199acbbeafSnn35248 	char			*pn_priv;	/* Privileges name */
46209acbbeafSnn35248 } priv_node_t;
46219acbbeafSnn35248 
46229acbbeafSnn35248 /* Privileges lists can differ across brands */
46239acbbeafSnn35248 typedef struct priv_lists {
46249acbbeafSnn35248 	/* Privileges considered safe for all non-global zones of a brand */
46259acbbeafSnn35248 	struct priv_node	*pl_default;
46269acbbeafSnn35248 
46279acbbeafSnn35248 	/* Privileges not permitted for all non-global zones of a brand */
46289acbbeafSnn35248 	struct priv_node	*pl_prohibited;
46299acbbeafSnn35248 
46309acbbeafSnn35248 	/* Privileges required for all non-global zones of a brand */
46319acbbeafSnn35248 	struct priv_node	*pl_required;
4632bf1d7e28Sdh155122 
4633bf1d7e28Sdh155122 	/*
4634bf1d7e28Sdh155122 	 * ip-type of the zone these privileges lists apply to.
4635bf1d7e28Sdh155122 	 * It is used to pass ip-type to the callback function,
4636bf1d7e28Sdh155122 	 * priv_lists_cb, which has no way of getting the ip-type.
4637bf1d7e28Sdh155122 	 */
4638bf1d7e28Sdh155122 	const char		*pl_iptype;
46399acbbeafSnn35248 } priv_lists_t;
46409acbbeafSnn35248 
46419acbbeafSnn35248 static int
4642bf1d7e28Sdh155122 priv_lists_cb(void *data, priv_iter_t *priv_iter)
46437c478bd9Sstevel@tonic-gate {
46449acbbeafSnn35248 	priv_lists_t *plp = (priv_lists_t *)data;
46459acbbeafSnn35248 	priv_node_t *pnp;
46469acbbeafSnn35248 
4647bf1d7e28Sdh155122 	/* Skip this privilege if ip-type does not match */
4648bf1d7e28Sdh155122 	if ((strcmp(priv_iter->pi_iptype, "all") != 0) &&
4649bf1d7e28Sdh155122 	    (strcmp(priv_iter->pi_iptype, plp->pl_iptype) != 0))
4650bf1d7e28Sdh155122 		return (0);
4651bf1d7e28Sdh155122 
46529acbbeafSnn35248 	/* Allocate a new priv list node. */
46539acbbeafSnn35248 	if ((pnp = malloc(sizeof (*pnp))) == NULL)
46549acbbeafSnn35248 		return (-1);
4655bf1d7e28Sdh155122 	if ((pnp->pn_priv = strdup(priv_iter->pi_name)) == NULL) {
46569acbbeafSnn35248 		free(pnp);
46579acbbeafSnn35248 		return (-1);
46589acbbeafSnn35248 	}
46599acbbeafSnn35248 
46609acbbeafSnn35248 	/* Insert the new priv list node into the right list */
4661bf1d7e28Sdh155122 	if (strcmp(priv_iter->pi_set, "default") == 0) {
46629acbbeafSnn35248 		pnp->pn_next = plp->pl_default;
46639acbbeafSnn35248 		plp->pl_default = pnp;
4664bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "prohibited") == 0) {
46659acbbeafSnn35248 		pnp->pn_next = plp->pl_prohibited;
46669acbbeafSnn35248 		plp->pl_prohibited = pnp;
4667bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "required") == 0) {
46689acbbeafSnn35248 		pnp->pn_next = plp->pl_required;
46699acbbeafSnn35248 		plp->pl_required = pnp;
46709acbbeafSnn35248 	} else {
46719acbbeafSnn35248 		free(pnp->pn_priv);
46729acbbeafSnn35248 		free(pnp);
46739acbbeafSnn35248 		return (-1);
46749acbbeafSnn35248 	}
46759acbbeafSnn35248 	return (0);
46769acbbeafSnn35248 }
46779acbbeafSnn35248 
46789acbbeafSnn35248 static void
46799acbbeafSnn35248 priv_lists_destroy(priv_lists_t *plp)
46809acbbeafSnn35248 {
46819acbbeafSnn35248 	priv_node_t *pnp;
46829acbbeafSnn35248 
46839acbbeafSnn35248 	assert(plp != NULL);
46849acbbeafSnn35248 
46859acbbeafSnn35248 	while ((pnp = plp->pl_default) != NULL) {
46869acbbeafSnn35248 		plp->pl_default = pnp->pn_next;
46879acbbeafSnn35248 		free(pnp->pn_priv);
46889acbbeafSnn35248 		free(pnp);
46899acbbeafSnn35248 	}
46909acbbeafSnn35248 	while ((pnp = plp->pl_prohibited) != NULL) {
46919acbbeafSnn35248 		plp->pl_prohibited = pnp->pn_next;
46929acbbeafSnn35248 		free(pnp->pn_priv);
46939acbbeafSnn35248 		free(pnp);
46949acbbeafSnn35248 	}
46959acbbeafSnn35248 	while ((pnp = plp->pl_required) != NULL) {
46969acbbeafSnn35248 		plp->pl_required = pnp->pn_next;
46979acbbeafSnn35248 		free(pnp->pn_priv);
46989acbbeafSnn35248 		free(pnp);
46999acbbeafSnn35248 	}
47009acbbeafSnn35248 	free(plp);
47019acbbeafSnn35248 }
47029acbbeafSnn35248 
47039acbbeafSnn35248 static int
4704bf1d7e28Sdh155122 priv_lists_create(zone_dochandle_t handle, priv_lists_t **plpp,
4705bf1d7e28Sdh155122     const char *curr_iptype)
47069acbbeafSnn35248 {
47079acbbeafSnn35248 	priv_lists_t *plp;
4708123807fbSedp 	brand_handle_t bh;
47099acbbeafSnn35248 	char brand[MAXNAMELEN];
47109acbbeafSnn35248 
47119acbbeafSnn35248 	if (handle != NULL) {
47129acbbeafSnn35248 		if (zonecfg_get_brand(handle, brand, sizeof (brand)) != 0)
47139acbbeafSnn35248 			return (Z_BRAND_ERROR);
47149acbbeafSnn35248 	} else {
47159acbbeafSnn35248 		(void) strlcpy(brand, NATIVE_BRAND_NAME, MAXNAMELEN);
47169acbbeafSnn35248 	}
47179acbbeafSnn35248 
4718123807fbSedp 	if ((bh = brand_open(brand)) == NULL)
47199acbbeafSnn35248 		return (Z_BRAND_ERROR);
47209acbbeafSnn35248 
47219acbbeafSnn35248 	if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) {
4722123807fbSedp 		brand_close(bh);
47239acbbeafSnn35248 		return (Z_NOMEM);
47249acbbeafSnn35248 	}
47259acbbeafSnn35248 
4726bf1d7e28Sdh155122 	plp->pl_iptype = curr_iptype;
4727bf1d7e28Sdh155122 
47289acbbeafSnn35248 	/* construct the privilege lists */
4729123807fbSedp 	if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) {
47309acbbeafSnn35248 		priv_lists_destroy(plp);
4731123807fbSedp 		brand_close(bh);
47329acbbeafSnn35248 		return (Z_BRAND_ERROR);
47339acbbeafSnn35248 	}
47349acbbeafSnn35248 
4735123807fbSedp 	brand_close(bh);
47369acbbeafSnn35248 	*plpp = plp;
47379acbbeafSnn35248 	return (Z_OK);
47389acbbeafSnn35248 }
47399acbbeafSnn35248 
47409acbbeafSnn35248 static int
47419acbbeafSnn35248 get_default_privset(priv_set_t *privs, priv_lists_t *plp)
47429acbbeafSnn35248 {
47439acbbeafSnn35248 	priv_node_t *pnp;
4744ffbafc53Scomay 	priv_set_t *basic;
47457c478bd9Sstevel@tonic-gate 
4746ffbafc53Scomay 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
47477c478bd9Sstevel@tonic-gate 	if (basic == NULL)
4748ffbafc53Scomay 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
47497c478bd9Sstevel@tonic-gate 
47507c478bd9Sstevel@tonic-gate 	priv_union(basic, privs);
47517c478bd9Sstevel@tonic-gate 	priv_freeset(basic);
47527c478bd9Sstevel@tonic-gate 
47539acbbeafSnn35248 	for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) {
47549acbbeafSnn35248 		if (priv_addset(privs, pnp->pn_priv) != 0)
47557c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
47567c478bd9Sstevel@tonic-gate 	}
47579acbbeafSnn35248 
47587c478bd9Sstevel@tonic-gate 	return (Z_OK);
47597c478bd9Sstevel@tonic-gate }
47607c478bd9Sstevel@tonic-gate 
47619acbbeafSnn35248 int
4762bf1d7e28Sdh155122 zonecfg_default_privset(priv_set_t *privs, const char *curr_iptype)
47639acbbeafSnn35248 {
47649acbbeafSnn35248 	priv_lists_t *plp;
47659acbbeafSnn35248 	int ret;
47669acbbeafSnn35248 
4767bf1d7e28Sdh155122 	if ((ret = priv_lists_create(NULL, &plp, curr_iptype)) != Z_OK)
47689acbbeafSnn35248 		return (ret);
47699acbbeafSnn35248 	ret = get_default_privset(privs, plp);
47709acbbeafSnn35248 	priv_lists_destroy(plp);
47719acbbeafSnn35248 	return (ret);
47729acbbeafSnn35248 }
47739acbbeafSnn35248 
4774ffbafc53Scomay void
4775ffbafc53Scomay append_priv_token(char *priv, char *str, size_t strlen)
4776ffbafc53Scomay {
4777ffbafc53Scomay 	if (*str != '\0')
4778ffbafc53Scomay 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
4779ffbafc53Scomay 	(void) strlcat(str, priv, strlen);
4780ffbafc53Scomay }
4781ffbafc53Scomay 
4782ffbafc53Scomay /*
4783ffbafc53Scomay  * Verify that the supplied string is a valid privilege limit set for a
4784ffbafc53Scomay  * non-global zone.  This string must not only be acceptable to
4785ffbafc53Scomay  * priv_str_to_set(3C) which parses it, but it also must resolve to a
4786ffbafc53Scomay  * privilege set that includes certain required privileges and lacks
4787ffbafc53Scomay  * certain prohibited privileges.
4788ffbafc53Scomay  */
4789ffbafc53Scomay static int
4790ffbafc53Scomay verify_privset(char *privbuf, priv_set_t *privs, char **privname,
47919acbbeafSnn35248     boolean_t add_default, priv_lists_t *plp)
4792ffbafc53Scomay {
47939acbbeafSnn35248 	priv_node_t *pnp;
47949acbbeafSnn35248 	char *tmp, *cp, *lasts;
4795ffbafc53Scomay 	size_t len;
4796ffbafc53Scomay 	priv_set_t *mergeset;
4797ffbafc53Scomay 	const char *token;
4798ffbafc53Scomay 
4799ffbafc53Scomay 	/*
4800ffbafc53Scomay 	 * The verification of the privilege string occurs in several
4801ffbafc53Scomay 	 * phases.  In the first phase, the supplied string is scanned for
4802ffbafc53Scomay 	 * the ZONE_TOKEN token which is not support as part of the
4803ffbafc53Scomay 	 * "limitpriv" property.
4804ffbafc53Scomay 	 *
4805ffbafc53Scomay 	 * Duplicate the supplied privilege string since strtok_r(3C)
4806ffbafc53Scomay 	 * tokenizes its input by null-terminating the tokens.
4807ffbafc53Scomay 	 */
4808ffbafc53Scomay 	if ((tmp = strdup(privbuf)) == NULL)
4809ffbafc53Scomay 		return (Z_NOMEM);
4810ffbafc53Scomay 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
4811ffbafc53Scomay 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
4812ffbafc53Scomay 		if (strcmp(cp, ZONE_TOKEN) == 0) {
4813ffbafc53Scomay 			free(tmp);
4814ffbafc53Scomay 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
4815ffbafc53Scomay 				return (Z_NOMEM);
4816ffbafc53Scomay 			else
4817ffbafc53Scomay 				return (Z_PRIV_UNKNOWN);
4818ffbafc53Scomay 		}
4819ffbafc53Scomay 	}
4820ffbafc53Scomay 	free(tmp);
4821ffbafc53Scomay 
4822ffbafc53Scomay 	if (add_default) {
4823ffbafc53Scomay 		/*
4824ffbafc53Scomay 		 * If DEFAULT_TOKEN was specified, a string needs to be
4825ffbafc53Scomay 		 * built containing the privileges from the default, safe
4826ffbafc53Scomay 		 * set along with those of the "limitpriv" property.
4827ffbafc53Scomay 		 */
4828ffbafc53Scomay 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
48299acbbeafSnn35248 
48309acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
48319acbbeafSnn35248 			len += strlen(pnp->pn_priv) + 1;
4832ffbafc53Scomay 		tmp = alloca(len);
4833ffbafc53Scomay 		*tmp = '\0';
4834ffbafc53Scomay 
4835ffbafc53Scomay 		append_priv_token(BASIC_TOKEN, tmp, len);
48369acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
48379acbbeafSnn35248 			append_priv_token(pnp->pn_priv, tmp, len);
4838ffbafc53Scomay 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
4839ffbafc53Scomay 		(void) strlcat(tmp, privbuf, len);
4840ffbafc53Scomay 	} else {
4841ffbafc53Scomay 		tmp = privbuf;
4842ffbafc53Scomay 	}
4843ffbafc53Scomay 
4844ffbafc53Scomay 
4845ffbafc53Scomay 	/*
4846ffbafc53Scomay 	 * In the next phase, attempt to convert the merged privilege
4847ffbafc53Scomay 	 * string into a privilege set.  In the case of an error, either
4848ffbafc53Scomay 	 * there was a memory allocation failure or there was an invalid
4849ffbafc53Scomay 	 * privilege token in the string.  In either case, return an
4850ffbafc53Scomay 	 * appropriate error code but in the event of an invalid token,
4851ffbafc53Scomay 	 * allocate a string containing its name and return that back to
4852ffbafc53Scomay 	 * the caller.
4853ffbafc53Scomay 	 */
4854ffbafc53Scomay 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
4855ffbafc53Scomay 	if (mergeset == NULL) {
4856ffbafc53Scomay 		if (token == NULL)
4857ffbafc53Scomay 			return (Z_NOMEM);
4858ffbafc53Scomay 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
4859ffbafc53Scomay 			*cp = '\0';
4860ffbafc53Scomay 		if ((*privname = strdup(token)) == NULL)
4861ffbafc53Scomay 			return (Z_NOMEM);
4862ffbafc53Scomay 		else
4863ffbafc53Scomay 			return (Z_PRIV_UNKNOWN);
4864ffbafc53Scomay 	}
4865ffbafc53Scomay 
4866ffbafc53Scomay 	/*
4867ffbafc53Scomay 	 * Next, verify that none of the prohibited zone privileges are
4868ffbafc53Scomay 	 * present in the merged privilege set.
4869ffbafc53Scomay 	 */
48709acbbeafSnn35248 	for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) {
48719acbbeafSnn35248 		if (priv_ismember(mergeset, pnp->pn_priv)) {
4872ffbafc53Scomay 			priv_freeset(mergeset);
48739acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
4874ffbafc53Scomay 				return (Z_NOMEM);
4875ffbafc53Scomay 			else
4876ffbafc53Scomay 				return (Z_PRIV_PROHIBITED);
4877ffbafc53Scomay 		}
4878ffbafc53Scomay 	}
4879ffbafc53Scomay 
4880ffbafc53Scomay 	/*
4881ffbafc53Scomay 	 * Finally, verify that all of the required zone privileges are
4882ffbafc53Scomay 	 * present in the merged privilege set.
4883ffbafc53Scomay 	 */
48849acbbeafSnn35248 	for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) {
48859acbbeafSnn35248 		if (!priv_ismember(mergeset, pnp->pn_priv)) {
4886ffbafc53Scomay 			priv_freeset(mergeset);
48879acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
4888ffbafc53Scomay 				return (Z_NOMEM);
4889ffbafc53Scomay 			else
4890ffbafc53Scomay 				return (Z_PRIV_REQUIRED);
4891ffbafc53Scomay 		}
4892ffbafc53Scomay 	}
4893ffbafc53Scomay 
4894ffbafc53Scomay 	priv_copyset(mergeset, privs);
4895ffbafc53Scomay 	priv_freeset(mergeset);
4896ffbafc53Scomay 	return (Z_OK);
4897ffbafc53Scomay }
4898ffbafc53Scomay 
4899ffbafc53Scomay /*
4900ffbafc53Scomay  * Fill in the supplied privilege set with either the default, safe set of
4901ffbafc53Scomay  * privileges suitable for a non-global zone, or one based on the
4902ffbafc53Scomay  * "limitpriv" property in the zone's configuration.
4903ffbafc53Scomay  *
4904ffbafc53Scomay  * In the event of an invalid privilege specification in the
4905ffbafc53Scomay  * configuration, a string is allocated and returned containing the
4906ffbafc53Scomay  * "privilege" causing the issue.  It is the caller's responsibility to
4907ffbafc53Scomay  * free this memory when it is done with it.
4908ffbafc53Scomay  */
4909ffbafc53Scomay int
4910ffbafc53Scomay zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
4911ffbafc53Scomay     char **privname)
4912ffbafc53Scomay {
49139acbbeafSnn35248 	priv_lists_t *plp;
49149acbbeafSnn35248 	char *cp, *limitpriv = NULL;
49159acbbeafSnn35248 	int err, limitlen;
4916bf1d7e28Sdh155122 	zone_iptype_t iptype;
4917bf1d7e28Sdh155122 	const char *curr_iptype;
4918ffbafc53Scomay 
4919ffbafc53Scomay 	/*
4920ffbafc53Scomay 	 * Attempt to lookup the "limitpriv" property.  If it does not
4921ffbafc53Scomay 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
4922ffbafc53Scomay 	 * default, safe privilege set is returned.
4923ffbafc53Scomay 	 */
49249acbbeafSnn35248 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK)
4925ffbafc53Scomay 		return (err);
49269acbbeafSnn35248 
4927bf1d7e28Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK)
4928bf1d7e28Sdh155122 		return (err);
4929bf1d7e28Sdh155122 
4930bf1d7e28Sdh155122 	switch (iptype) {
4931bf1d7e28Sdh155122 	case ZS_SHARED:
4932bf1d7e28Sdh155122 		curr_iptype = "shared";
4933bf1d7e28Sdh155122 		break;
4934bf1d7e28Sdh155122 	case ZS_EXCLUSIVE:
4935bf1d7e28Sdh155122 		curr_iptype = "exclusive";
4936bf1d7e28Sdh155122 		break;
4937bf1d7e28Sdh155122 	}
4938bf1d7e28Sdh155122 
4939bf1d7e28Sdh155122 	if ((err = priv_lists_create(handle, &plp, curr_iptype)) != Z_OK)
49409acbbeafSnn35248 		return (err);
49419acbbeafSnn35248 
4942ffbafc53Scomay 	limitlen = strlen(limitpriv);
4943ffbafc53Scomay 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
4944ffbafc53Scomay 		free(limitpriv);
49459acbbeafSnn35248 		err = get_default_privset(privs, plp);
49469acbbeafSnn35248 		priv_lists_destroy(plp);
49479acbbeafSnn35248 		return (err);
4948ffbafc53Scomay 	}
4949ffbafc53Scomay 
4950ffbafc53Scomay 	/*
4951ffbafc53Scomay 	 * Check if the string DEFAULT_TOKEN is the first token in a list
4952ffbafc53Scomay 	 * of privileges.
4953ffbafc53Scomay 	 */
4954ffbafc53Scomay 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
4955ffbafc53Scomay 	if (cp != NULL &&
4956ffbafc53Scomay 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
49579acbbeafSnn35248 		err = verify_privset(cp + 1, privs, privname, B_TRUE, plp);
4958ffbafc53Scomay 	else
49599acbbeafSnn35248 		err = verify_privset(limitpriv, privs, privname, B_FALSE, plp);
4960ffbafc53Scomay 
4961ffbafc53Scomay 	free(limitpriv);
49629acbbeafSnn35248 	priv_lists_destroy(plp);
4963ffbafc53Scomay 	return (err);
4964ffbafc53Scomay }
4965ffbafc53Scomay 
49667c478bd9Sstevel@tonic-gate int
49677c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
49687c478bd9Sstevel@tonic-gate {
49697c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
49707c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
49717c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
49727c478bd9Sstevel@tonic-gate 	FILE *cookie;
49737c478bd9Sstevel@tonic-gate 	int err;
4974108322fbScarlsonj 	char *cp;
49757c478bd9Sstevel@tonic-gate 
49767c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
49777c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
49787c478bd9Sstevel@tonic-gate 
4979108322fbScarlsonj 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
4980108322fbScarlsonj 	cp = zonepath + strlen(zonepath);
4981108322fbScarlsonj 	while (cp > zonepath && cp[-1] == '/')
4982108322fbScarlsonj 		*--cp = '\0';
4983108322fbScarlsonj 
49847c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
4985108322fbScarlsonj 		if (zonepath[0] == '\0')
49867c478bd9Sstevel@tonic-gate 			(void) strlcpy(zonepath, "/", rp_sz);
49877c478bd9Sstevel@tonic-gate 		return (Z_OK);
49887c478bd9Sstevel@tonic-gate 	}
49897c478bd9Sstevel@tonic-gate 
49907c478bd9Sstevel@tonic-gate 	/*
49917c478bd9Sstevel@tonic-gate 	 * First check the index file.  Because older versions did not have
49927c478bd9Sstevel@tonic-gate 	 * a copy of the zone path, allow for it to be zero length, in which
49937c478bd9Sstevel@tonic-gate 	 * case we ignore this result and fall back to the XML files.
49947c478bd9Sstevel@tonic-gate 	 */
49957c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
49967c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
49977c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
49987c478bd9Sstevel@tonic-gate 			found = B_TRUE;
4999108322fbScarlsonj 			if (ze->zone_path[0] != '\0')
5000108322fbScarlsonj 				(void) strlcpy(cp, ze->zone_path,
5001108322fbScarlsonj 				    rp_sz - (cp - zonepath));
50027c478bd9Sstevel@tonic-gate 		}
50037c478bd9Sstevel@tonic-gate 		free(ze);
50047c478bd9Sstevel@tonic-gate 		if (found)
50057c478bd9Sstevel@tonic-gate 			break;
50067c478bd9Sstevel@tonic-gate 	}
50077c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
5008108322fbScarlsonj 	if (found && *cp != '\0')
50097c478bd9Sstevel@tonic-gate 		return (Z_OK);
50107c478bd9Sstevel@tonic-gate 
50117c478bd9Sstevel@tonic-gate 	/* Fall back to the XML files. */
50127c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL)
50137c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
50147c478bd9Sstevel@tonic-gate 
50157c478bd9Sstevel@tonic-gate 	/*
50167c478bd9Sstevel@tonic-gate 	 * Check the snapshot first: if a zone is running, its zonepath
50177c478bd9Sstevel@tonic-gate 	 * may have changed.
50187c478bd9Sstevel@tonic-gate 	 */
50197c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
5020e767a340Sgjelinek 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK) {
5021e767a340Sgjelinek 			zonecfg_fini_handle(handle);
50227c478bd9Sstevel@tonic-gate 			return (err);
50237c478bd9Sstevel@tonic-gate 		}
5024e767a340Sgjelinek 	}
50257c478bd9Sstevel@tonic-gate 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
50267c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
50277c478bd9Sstevel@tonic-gate 	return (err);
50287c478bd9Sstevel@tonic-gate }
50297c478bd9Sstevel@tonic-gate 
50307c478bd9Sstevel@tonic-gate int
50317c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
50327c478bd9Sstevel@tonic-gate {
50337c478bd9Sstevel@tonic-gate 	int err;
50347c478bd9Sstevel@tonic-gate 
50357c478bd9Sstevel@tonic-gate 	/* This function makes sense for non-global zones only. */
50367c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
50377c478bd9Sstevel@tonic-gate 		return (Z_BOGUS_ZONE_NAME);
50387c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
50397c478bd9Sstevel@tonic-gate 		return (err);
50407c478bd9Sstevel@tonic-gate 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
50417c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
50427c478bd9Sstevel@tonic-gate 	return (Z_OK);
50437c478bd9Sstevel@tonic-gate }
50447c478bd9Sstevel@tonic-gate 
50459acbbeafSnn35248 int
50469acbbeafSnn35248 zone_get_brand(char *zone_name, char *brandname, size_t rp_sz)
50479acbbeafSnn35248 {
50489acbbeafSnn35248 	int err;
50499acbbeafSnn35248 	zone_dochandle_t handle;
50509acbbeafSnn35248 	char myzone[MAXNAMELEN];
50519acbbeafSnn35248 	int myzoneid = getzoneid();
50529acbbeafSnn35248 
50539acbbeafSnn35248 	/*
50549acbbeafSnn35248 	 * If we are not in the global zone, then we don't have the zone
50559acbbeafSnn35248 	 * .xml files with the brand name available.  Thus, we are going to
50569acbbeafSnn35248 	 * have to ask the kernel for the information.
50579acbbeafSnn35248 	 */
50589acbbeafSnn35248 	if (myzoneid != GLOBAL_ZONEID) {
50592ec67e04Sgjelinek 		if (is_system_labeled()) {
50602ec67e04Sgjelinek 			(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
50612ec67e04Sgjelinek 			return (Z_OK);
50622ec67e04Sgjelinek 		}
50639acbbeafSnn35248 		if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone,
50649acbbeafSnn35248 		    sizeof (myzone)) < 0)
50659acbbeafSnn35248 			return (Z_NO_ZONE);
50669acbbeafSnn35248 		if (strncmp(zone_name, myzone, MAXNAMELEN) != NULL)
50679acbbeafSnn35248 			return (Z_NO_ZONE);
50689acbbeafSnn35248 		err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz);
50699acbbeafSnn35248 		if (err < 0)
50709acbbeafSnn35248 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
50719acbbeafSnn35248 		return (Z_OK);
50729acbbeafSnn35248 	}
50739acbbeafSnn35248 
50749acbbeafSnn35248 	if (strcmp(zone_name, "global") == NULL) {
50759acbbeafSnn35248 		(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
50762ec67e04Sgjelinek 		return (Z_OK);
50779acbbeafSnn35248 	}
50789acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL)
50799acbbeafSnn35248 		return (Z_NOMEM);
50809acbbeafSnn35248 
50819acbbeafSnn35248 	err = zonecfg_get_handle((char *)zone_name, handle);
50829acbbeafSnn35248 	if (err == Z_OK)
50839acbbeafSnn35248 		err = zonecfg_get_brand(handle, brandname, rp_sz);
50849acbbeafSnn35248 
50859acbbeafSnn35248 	zonecfg_fini_handle(handle);
50869acbbeafSnn35248 	return (err);
50879acbbeafSnn35248 }
50889acbbeafSnn35248 
5089facf4a8dSllai1 /*
5090facf4a8dSllai1  * Return the appropriate root for the active /dev.
5091facf4a8dSllai1  * For normal zone, the path is $ZONEPATH/root;
5092facf4a8dSllai1  * for scratch zone, the dev path is $ZONEPATH/lu.
5093facf4a8dSllai1  */
5094facf4a8dSllai1 int
5095facf4a8dSllai1 zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz)
5096facf4a8dSllai1 {
5097facf4a8dSllai1 	int err;
5098facf4a8dSllai1 	char *suffix;
5099facf4a8dSllai1 	zone_state_t state;
5100facf4a8dSllai1 
5101facf4a8dSllai1 	/* This function makes sense for non-global zones only. */
5102facf4a8dSllai1 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
5103facf4a8dSllai1 		return (Z_BOGUS_ZONE_NAME);
5104facf4a8dSllai1 	if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK)
5105facf4a8dSllai1 		return (err);
5106facf4a8dSllai1 
5107facf4a8dSllai1 	if (zone_get_state(zone_name, &state) == Z_OK &&
5108facf4a8dSllai1 	    state == ZONE_STATE_MOUNTED)
5109facf4a8dSllai1 		suffix = "/lu";
5110facf4a8dSllai1 	else
5111facf4a8dSllai1 		suffix = "/root";
5112facf4a8dSllai1 	if (strlcat(devroot, suffix, rp_sz) >= rp_sz)
5113facf4a8dSllai1 		return (Z_TOO_BIG);
5114facf4a8dSllai1 	return (Z_OK);
5115facf4a8dSllai1 }
5116facf4a8dSllai1 
51177c478bd9Sstevel@tonic-gate static zone_state_t
5118108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
51197c478bd9Sstevel@tonic-gate {
5120108322fbScarlsonj 	char zoneroot[MAXPATHLEN];
5121108322fbScarlsonj 	size_t zlen;
5122108322fbScarlsonj 
51237c478bd9Sstevel@tonic-gate 	assert(kernel_state <= ZONE_MAX_STATE);
51247c478bd9Sstevel@tonic-gate 	switch (kernel_state) {
51257c478bd9Sstevel@tonic-gate 		case ZONE_IS_UNINITIALIZED:
5126bd41d0a8Snordmark 		case ZONE_IS_INITIALIZED:
5127bd41d0a8Snordmark 			/* The kernel will not return these two states */
5128108322fbScarlsonj 			return (ZONE_STATE_READY);
51297c478bd9Sstevel@tonic-gate 		case ZONE_IS_READY:
5130108322fbScarlsonj 			/*
5131108322fbScarlsonj 			 * If the zone's root is mounted on $ZONEPATH/lu, then
5132108322fbScarlsonj 			 * it's a mounted scratch zone.
5133108322fbScarlsonj 			 */
5134108322fbScarlsonj 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
5135108322fbScarlsonj 			    sizeof (zoneroot)) >= 0) {
5136108322fbScarlsonj 				zlen = strlen(zoneroot);
5137108322fbScarlsonj 				if (zlen > 3 &&
5138108322fbScarlsonj 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
5139108322fbScarlsonj 					return (ZONE_STATE_MOUNTED);
5140108322fbScarlsonj 			}
51417c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_READY);
51427c478bd9Sstevel@tonic-gate 		case ZONE_IS_BOOTING:
51437c478bd9Sstevel@tonic-gate 		case ZONE_IS_RUNNING:
51447c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_RUNNING);
51457c478bd9Sstevel@tonic-gate 		case ZONE_IS_SHUTTING_DOWN:
51467c478bd9Sstevel@tonic-gate 		case ZONE_IS_EMPTY:
51477c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_SHUTTING_DOWN);
51487c478bd9Sstevel@tonic-gate 		case ZONE_IS_DOWN:
51497c478bd9Sstevel@tonic-gate 		case ZONE_IS_DYING:
51507c478bd9Sstevel@tonic-gate 		case ZONE_IS_DEAD:
51517c478bd9Sstevel@tonic-gate 		default:
51527c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_DOWN);
51537c478bd9Sstevel@tonic-gate 	}
51547c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
51557c478bd9Sstevel@tonic-gate }
51567c478bd9Sstevel@tonic-gate 
51577c478bd9Sstevel@tonic-gate int
51587c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num)
51597c478bd9Sstevel@tonic-gate {
51607c478bd9Sstevel@tonic-gate 	zone_status_t status;
51617c478bd9Sstevel@tonic-gate 	zoneid_t zone_id;
51627c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
51637c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
51647c478bd9Sstevel@tonic-gate 	FILE *cookie;
5165108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
5166108322fbScarlsonj 	FILE *fp;
51677c478bd9Sstevel@tonic-gate 
51687c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
51697c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
51707c478bd9Sstevel@tonic-gate 
5171108322fbScarlsonj 	/*
5172108322fbScarlsonj 	 * If we're looking at an alternate root, then we need to query the
5173108322fbScarlsonj 	 * kernel using the scratch zone name.
5174108322fbScarlsonj 	 */
5175108322fbScarlsonj 	zone_id = -1;
5176108322fbScarlsonj 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
5177108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
5178108322fbScarlsonj 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
5179108322fbScarlsonj 			    kernzone, sizeof (kernzone)) == 0)
5180108322fbScarlsonj 				zone_id = getzoneidbyname(kernzone);
5181108322fbScarlsonj 			zonecfg_close_scratch(fp);
5182108322fbScarlsonj 		}
5183108322fbScarlsonj 	} else {
5184108322fbScarlsonj 		zone_id = getzoneidbyname(zone_name);
5185108322fbScarlsonj 	}
5186108322fbScarlsonj 
51877c478bd9Sstevel@tonic-gate 	/* check to see if zone is running */
5188108322fbScarlsonj 	if (zone_id != -1 &&
51897c478bd9Sstevel@tonic-gate 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
51907c478bd9Sstevel@tonic-gate 	    sizeof (status)) >= 0) {
5191108322fbScarlsonj 		*state_num = kernel_state_to_user_state(zone_id, status);
51927c478bd9Sstevel@tonic-gate 		return (Z_OK);
51937c478bd9Sstevel@tonic-gate 	}
51947c478bd9Sstevel@tonic-gate 
51957c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
51967c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
51977c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
51987c478bd9Sstevel@tonic-gate 			found = B_TRUE;
51997c478bd9Sstevel@tonic-gate 			*state_num = ze->zone_state;
52007c478bd9Sstevel@tonic-gate 		}
52017c478bd9Sstevel@tonic-gate 		free(ze);
52027c478bd9Sstevel@tonic-gate 		if (found)
52037c478bd9Sstevel@tonic-gate 			break;
52047c478bd9Sstevel@tonic-gate 	}
52057c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
52067c478bd9Sstevel@tonic-gate 	return ((found) ? Z_OK : Z_NO_ZONE);
52077c478bd9Sstevel@tonic-gate }
52087c478bd9Sstevel@tonic-gate 
52097c478bd9Sstevel@tonic-gate int
52107c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state)
52117c478bd9Sstevel@tonic-gate {
52127c478bd9Sstevel@tonic-gate 	struct zoneent ze;
52137c478bd9Sstevel@tonic-gate 
52147c478bd9Sstevel@tonic-gate 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
52157c478bd9Sstevel@tonic-gate 	    state != ZONE_STATE_INCOMPLETE)
52167c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
52177c478bd9Sstevel@tonic-gate 
5218087719fdSdp 	bzero(&ze, sizeof (ze));
52197c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
52207c478bd9Sstevel@tonic-gate 	ze.zone_state = state;
52217c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
52227c478bd9Sstevel@tonic-gate 	return (putzoneent(&ze, PZE_MODIFY));
52237c478bd9Sstevel@tonic-gate }
52247c478bd9Sstevel@tonic-gate 
52257c478bd9Sstevel@tonic-gate /*
52267c478bd9Sstevel@tonic-gate  * Get id (if any) for specified zone.  There are four possible outcomes:
52277c478bd9Sstevel@tonic-gate  * - If the string corresponds to the numeric id of an active (booted)
52287c478bd9Sstevel@tonic-gate  *   zone, sets *zip to the zone id and returns 0.
52297c478bd9Sstevel@tonic-gate  * - If the string corresponds to the name of an active (booted) zone,
52307c478bd9Sstevel@tonic-gate  *   sets *zip to the zone id and returns 0.
52317c478bd9Sstevel@tonic-gate  * - If the string is a name in the configuration but is not booted,
52327c478bd9Sstevel@tonic-gate  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
52337c478bd9Sstevel@tonic-gate  * - Otherwise, leaves *zip unchanged and returns -1.
52347c478bd9Sstevel@tonic-gate  *
52357c478bd9Sstevel@tonic-gate  * This function acts as an auxiliary filter on the function of the same
52367c478bd9Sstevel@tonic-gate  * name in libc; the linker binds to this version if libzonecfg exists,
52377c478bd9Sstevel@tonic-gate  * and the libc version if it doesn't.  Any changes to this version of
52387c478bd9Sstevel@tonic-gate  * the function should probably be reflected in the libc version as well.
52397c478bd9Sstevel@tonic-gate  */
52407c478bd9Sstevel@tonic-gate int
52417c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip)
52427c478bd9Sstevel@tonic-gate {
52437c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl;
52447c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
52457c478bd9Sstevel@tonic-gate 	char *cp;
52467c478bd9Sstevel@tonic-gate 	int err;
52477c478bd9Sstevel@tonic-gate 
52487c478bd9Sstevel@tonic-gate 	/* first try looking for active zone by id */
52497c478bd9Sstevel@tonic-gate 	errno = 0;
52507c478bd9Sstevel@tonic-gate 	zoneid = (zoneid_t)strtol(str, &cp, 0);
52517c478bd9Sstevel@tonic-gate 	if (errno == 0 && cp != str && *cp == '\0' &&
52527c478bd9Sstevel@tonic-gate 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
52537c478bd9Sstevel@tonic-gate 		*zip = zoneid;
52547c478bd9Sstevel@tonic-gate 		return (0);
52557c478bd9Sstevel@tonic-gate 	}
52567c478bd9Sstevel@tonic-gate 
52577c478bd9Sstevel@tonic-gate 	/* then look for active zone by name */
52587c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(str)) != -1) {
52597c478bd9Sstevel@tonic-gate 		*zip = zoneid;
52607c478bd9Sstevel@tonic-gate 		return (0);
52617c478bd9Sstevel@tonic-gate 	}
52627c478bd9Sstevel@tonic-gate 
52637c478bd9Sstevel@tonic-gate 	/* if in global zone, try looking up name in configuration database */
52647c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID ||
52657c478bd9Sstevel@tonic-gate 	    (hdl = zonecfg_init_handle()) == NULL)
52667c478bd9Sstevel@tonic-gate 		return (-1);
52677c478bd9Sstevel@tonic-gate 
5268108322fbScarlsonj 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
52697c478bd9Sstevel@tonic-gate 		/* zone exists but isn't active */
52707c478bd9Sstevel@tonic-gate 		*zip = ZONE_ID_UNDEFINED;
52717c478bd9Sstevel@tonic-gate 		err = 0;
52727c478bd9Sstevel@tonic-gate 	} else {
52737c478bd9Sstevel@tonic-gate 		err = -1;
52747c478bd9Sstevel@tonic-gate 	}
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(hdl);
52777c478bd9Sstevel@tonic-gate 	return (err);
52787c478bd9Sstevel@tonic-gate }
52797c478bd9Sstevel@tonic-gate 
52807c478bd9Sstevel@tonic-gate char *
52817c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num)
52827c478bd9Sstevel@tonic-gate {
52837c478bd9Sstevel@tonic-gate 	switch (state_num) {
52847c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
52857c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_CONFIGURED);
52867c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
52877c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INCOMPLETE);
52887c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
52897c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INSTALLED);
52907c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
52917c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_READY);
5292108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
5293108322fbScarlsonj 		return (ZONE_STATE_STR_MOUNTED);
52947c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
52957c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_RUNNING);
52967c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
52977c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_SHUTTING_DOWN);
52987c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
52997c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_DOWN);
53007c478bd9Sstevel@tonic-gate 	default:
53017c478bd9Sstevel@tonic-gate 		return ("unknown");
53027c478bd9Sstevel@tonic-gate 	}
53037c478bd9Sstevel@tonic-gate }
53047c478bd9Sstevel@tonic-gate 
53057c478bd9Sstevel@tonic-gate /*
5306108322fbScarlsonj  * Given a UUID value, find an associated zone name.  This is intended to be
5307108322fbScarlsonj  * used by callers who set up some 'default' name (corresponding to the
5308108322fbScarlsonj  * expected name for the zone) in the zonename buffer, and thus the function
5309108322fbScarlsonj  * doesn't touch this buffer on failure.
5310108322fbScarlsonj  */
5311108322fbScarlsonj int
5312555afedfScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
5313108322fbScarlsonj {
5314108322fbScarlsonj 	FILE *fp;
5315108322fbScarlsonj 	struct zoneent *ze;
5316555afedfScarlsonj 	uchar_t *uuid;
5317108322fbScarlsonj 
5318108322fbScarlsonj 	/*
5319108322fbScarlsonj 	 * A small amount of subterfuge via casts is necessary here because
5320108322fbScarlsonj 	 * libuuid doesn't use const correctly, but we don't want to export
5321108322fbScarlsonj 	 * this brokenness to our clients.
5322108322fbScarlsonj 	 */
5323555afedfScarlsonj 	uuid = (uchar_t *)uuidin;
5324555afedfScarlsonj 	if (uuid_is_null(uuid))
5325108322fbScarlsonj 		return (Z_NO_ZONE);
5326108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5327108322fbScarlsonj 		return (Z_NO_ZONE);
5328108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5329555afedfScarlsonj 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
5330108322fbScarlsonj 			break;
5331108322fbScarlsonj 		free(ze);
5332108322fbScarlsonj 	}
5333108322fbScarlsonj 	endzoneent(fp);
5334108322fbScarlsonj 	if (ze != NULL) {
5335108322fbScarlsonj 		(void) strlcpy(zonename, ze->zone_name, namelen);
5336108322fbScarlsonj 		free(ze);
5337108322fbScarlsonj 		return (Z_OK);
5338108322fbScarlsonj 	} else {
5339108322fbScarlsonj 		return (Z_NO_ZONE);
5340108322fbScarlsonj 	}
5341108322fbScarlsonj }
5342108322fbScarlsonj 
5343108322fbScarlsonj /*
5344108322fbScarlsonj  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
5345108322fbScarlsonj  * exists but the file doesn't have a value set yet.  Returns an error if the
5346108322fbScarlsonj  * zone cannot be located.
5347108322fbScarlsonj  */
5348108322fbScarlsonj int
5349108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid)
5350108322fbScarlsonj {
5351108322fbScarlsonj 	FILE *fp;
5352108322fbScarlsonj 	struct zoneent *ze;
5353108322fbScarlsonj 
5354108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5355108322fbScarlsonj 		return (Z_NO_ZONE);
5356108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5357108322fbScarlsonj 		if (strcmp(ze->zone_name, zonename) == 0)
5358108322fbScarlsonj 			break;
5359108322fbScarlsonj 		free(ze);
5360108322fbScarlsonj 	}
5361108322fbScarlsonj 	endzoneent(fp);
5362108322fbScarlsonj 	if (ze != NULL) {
5363108322fbScarlsonj 		uuid_copy(uuid, ze->zone_uuid);
5364108322fbScarlsonj 		free(ze);
5365108322fbScarlsonj 		return (Z_OK);
5366108322fbScarlsonj 	} else {
5367108322fbScarlsonj 		return (Z_NO_ZONE);
5368108322fbScarlsonj 	}
5369108322fbScarlsonj }
5370108322fbScarlsonj 
5371108322fbScarlsonj /*
53727c478bd9Sstevel@tonic-gate  * File-system convenience functions.
53737c478bd9Sstevel@tonic-gate  */
53747c478bd9Sstevel@tonic-gate boolean_t
53757c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type)
53767c478bd9Sstevel@tonic-gate {
53777c478bd9Sstevel@tonic-gate 	/*
53787c478bd9Sstevel@tonic-gate 	 * We already know which FS types don't work.
53797c478bd9Sstevel@tonic-gate 	 */
53807c478bd9Sstevel@tonic-gate 	if (strcmp(type, "proc") == 0 ||
53817c478bd9Sstevel@tonic-gate 	    strcmp(type, "mntfs") == 0 ||
53827c478bd9Sstevel@tonic-gate 	    strcmp(type, "autofs") == 0 ||
53837c478bd9Sstevel@tonic-gate 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
53847c478bd9Sstevel@tonic-gate 	    strcmp(type, "cachefs") == 0)
53857c478bd9Sstevel@tonic-gate 		return (B_FALSE);
53867c478bd9Sstevel@tonic-gate 	/*
53877c478bd9Sstevel@tonic-gate 	 * The caller may do more detailed verification to make sure other
53887c478bd9Sstevel@tonic-gate 	 * aspects of this filesystem type make sense.
53897c478bd9Sstevel@tonic-gate 	 */
53907c478bd9Sstevel@tonic-gate 	return (B_TRUE);
53917c478bd9Sstevel@tonic-gate }
53927c478bd9Sstevel@tonic-gate 
53937c478bd9Sstevel@tonic-gate /*
53947c478bd9Sstevel@tonic-gate  * Generally uninteresting rctl convenience functions.
53957c478bd9Sstevel@tonic-gate  */
53967c478bd9Sstevel@tonic-gate 
53977c478bd9Sstevel@tonic-gate int
53987c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
53997c478bd9Sstevel@tonic-gate     rctlblk_t *rctlblk)
54007c478bd9Sstevel@tonic-gate {
54017c478bd9Sstevel@tonic-gate 	unsigned long long ull;
54027c478bd9Sstevel@tonic-gate 	char *endp;
54037c478bd9Sstevel@tonic-gate 	rctl_priv_t priv;
54047c478bd9Sstevel@tonic-gate 	rctl_qty_t limit;
54057c478bd9Sstevel@tonic-gate 	uint_t action;
54067c478bd9Sstevel@tonic-gate 
54077c478bd9Sstevel@tonic-gate 	/* Get the privilege */
54087c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
54097c478bd9Sstevel@tonic-gate 		priv = RCPRIV_BASIC;
54107c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
54117c478bd9Sstevel@tonic-gate 		priv = RCPRIV_PRIVILEGED;
54127c478bd9Sstevel@tonic-gate 	} else {
54137c478bd9Sstevel@tonic-gate 		/* Invalid privilege */
54147c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54157c478bd9Sstevel@tonic-gate 	}
54167c478bd9Sstevel@tonic-gate 
54177c478bd9Sstevel@tonic-gate 	/* deal with negative input; strtoull(3c) doesn't do what we want */
54187c478bd9Sstevel@tonic-gate 	if (rctlval->zone_rctlval_limit[0] == '-')
54197c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54207c478bd9Sstevel@tonic-gate 	/* Get the limit */
54217c478bd9Sstevel@tonic-gate 	errno = 0;
54227c478bd9Sstevel@tonic-gate 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
54237c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endp != '\0') {
54247c478bd9Sstevel@tonic-gate 		/* parse failed */
54257c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54267c478bd9Sstevel@tonic-gate 	}
54277c478bd9Sstevel@tonic-gate 	limit = (rctl_qty_t)ull;
54287c478bd9Sstevel@tonic-gate 
54297c478bd9Sstevel@tonic-gate 	/* Get the action */
54307c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
54317c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_NOACTION;
54327c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
54337c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_SIGNAL;
54347c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
54357c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_DENY;
54367c478bd9Sstevel@tonic-gate 	} else {
54377c478bd9Sstevel@tonic-gate 		/* Invalid Action */
54387c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54397c478bd9Sstevel@tonic-gate 	}
54407c478bd9Sstevel@tonic-gate 	rctlblk_set_local_action(rctlblk, action, 0);
54417c478bd9Sstevel@tonic-gate 	rctlblk_set_privilege(rctlblk, priv);
54427c478bd9Sstevel@tonic-gate 	rctlblk_set_value(rctlblk, limit);
54437c478bd9Sstevel@tonic-gate 	return (Z_OK);
54447c478bd9Sstevel@tonic-gate }
54457c478bd9Sstevel@tonic-gate 
54467c478bd9Sstevel@tonic-gate static int
54477c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg)
54487c478bd9Sstevel@tonic-gate {
54497c478bd9Sstevel@tonic-gate 	const char *attrname = arg;
54507c478bd9Sstevel@tonic-gate 
54517c478bd9Sstevel@tonic-gate 	/*
54527c478bd9Sstevel@tonic-gate 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
54537c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
54547c478bd9Sstevel@tonic-gate 	 */
54557c478bd9Sstevel@tonic-gate 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
54567c478bd9Sstevel@tonic-gate }
54577c478bd9Sstevel@tonic-gate 
54587c478bd9Sstevel@tonic-gate boolean_t
54597c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name)
54607c478bd9Sstevel@tonic-gate {
54617c478bd9Sstevel@tonic-gate 	return (rctl_walk(rctl_check, (void *)name) == 1);
54627c478bd9Sstevel@tonic-gate }
54637c478bd9Sstevel@tonic-gate 
54647c478bd9Sstevel@tonic-gate boolean_t
54657c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name)
54667c478bd9Sstevel@tonic-gate {
54677c478bd9Sstevel@tonic-gate 	const char *c;
54687c478bd9Sstevel@tonic-gate 
54697c478bd9Sstevel@tonic-gate 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
54707c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54717c478bd9Sstevel@tonic-gate 	if (strlen(name) == sizeof ("zone.") - 1)
54727c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54737c478bd9Sstevel@tonic-gate 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
54747c478bd9Sstevel@tonic-gate 		if (!isalpha(*c) && *c != '-')
54757c478bd9Sstevel@tonic-gate 			return (B_FALSE);
54767c478bd9Sstevel@tonic-gate 	}
54777c478bd9Sstevel@tonic-gate 	return (B_TRUE);
54787c478bd9Sstevel@tonic-gate }
54797c478bd9Sstevel@tonic-gate 
54807c478bd9Sstevel@tonic-gate boolean_t
54817c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
54827c478bd9Sstevel@tonic-gate {
54837c478bd9Sstevel@tonic-gate 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
54847c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
54857c478bd9Sstevel@tonic-gate 
54867c478bd9Sstevel@tonic-gate 	if (priv != RCPRIV_PRIVILEGED)
54877c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54887c478bd9Sstevel@tonic-gate 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
54897c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54907c478bd9Sstevel@tonic-gate 	return (B_TRUE);
54917c478bd9Sstevel@tonic-gate }
54927c478bd9Sstevel@tonic-gate 
54937c478bd9Sstevel@tonic-gate boolean_t
54947c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
54957c478bd9Sstevel@tonic-gate {
54967c478bd9Sstevel@tonic-gate 	rctlblk_t *current, *next;
54977c478bd9Sstevel@tonic-gate 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
54987c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
54997c478bd9Sstevel@tonic-gate 	uint_t global_flags;
55007c478bd9Sstevel@tonic-gate 
55017c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
55027c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55037c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlname(name))
55047c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55057c478bd9Sstevel@tonic-gate 
55067c478bd9Sstevel@tonic-gate 	current = alloca(rctlblk_size());
55077c478bd9Sstevel@tonic-gate 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
55087c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* not an rctl on this system */
55097c478bd9Sstevel@tonic-gate 	/*
55107c478bd9Sstevel@tonic-gate 	 * Make sure the proposed value isn't greater than the current system
55117c478bd9Sstevel@tonic-gate 	 * value.
55127c478bd9Sstevel@tonic-gate 	 */
55137c478bd9Sstevel@tonic-gate 	next = alloca(rctlblk_size());
55147c478bd9Sstevel@tonic-gate 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
55157c478bd9Sstevel@tonic-gate 		rctlblk_t *tmp;
55167c478bd9Sstevel@tonic-gate 
55177c478bd9Sstevel@tonic-gate 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
55187c478bd9Sstevel@tonic-gate 			return (B_FALSE);	/* shouldn't happen */
55197c478bd9Sstevel@tonic-gate 		tmp = current;
55207c478bd9Sstevel@tonic-gate 		current = next;
55217c478bd9Sstevel@tonic-gate 		next = tmp;
55227c478bd9Sstevel@tonic-gate 	}
55237c478bd9Sstevel@tonic-gate 	if (limit > rctlblk_get_value(current))
55247c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55257c478bd9Sstevel@tonic-gate 
55267c478bd9Sstevel@tonic-gate 	/*
55277c478bd9Sstevel@tonic-gate 	 * Make sure the proposed action is allowed.
55287c478bd9Sstevel@tonic-gate 	 */
55297c478bd9Sstevel@tonic-gate 	global_flags = rctlblk_get_global_flags(current);
55307c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
55317c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_DENY)
55327c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55337c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
55347c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_NOACTION)
55357c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55367c478bd9Sstevel@tonic-gate 
55377c478bd9Sstevel@tonic-gate 	return (B_TRUE);
55387c478bd9Sstevel@tonic-gate }
5539fa9e4066Sahrens 
5540cf8f45c7Sdstaff /*
5541cf8f45c7Sdstaff  * There is always a race condition between reading the initial copy of
5542cf8f45c7Sdstaff  * a zones state and its state changing.  We address this by providing
5543cf8f45c7Sdstaff  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
5544cf8f45c7Sdstaff  * When zonecfg_critical_enter is called, sets the state field to LOCKED
5545cf8f45c7Sdstaff  * and aquires biglock. Biglock protects against other threads executing
5546cf8f45c7Sdstaff  * critical_enter and the state field protects against state changes during
5547cf8f45c7Sdstaff  * the critical period.
5548cf8f45c7Sdstaff  *
5549cf8f45c7Sdstaff  * If any state changes occur, zn_cb will set the failed field of the znotify
5550cf8f45c7Sdstaff  * structure.  This will cause the critical_exit function to re-lock the
5551cf8f45c7Sdstaff  * channel and return an error. Since evsnts may be delayed, the critical_exit
5552cf8f45c7Sdstaff  * function "flushes" the queue by putting an event on the queue and waiting for
5553cf8f45c7Sdstaff  * zn_cb to notify critical_exit that it received the ping event.
5554cf8f45c7Sdstaff  */
5555cf8f45c7Sdstaff static const char *
5556cf8f45c7Sdstaff string_get_tok(const char *in, char delim, int num)
5557cf8f45c7Sdstaff {
5558cf8f45c7Sdstaff 	int i = 0;
5559cf8f45c7Sdstaff 
5560cf8f45c7Sdstaff 	for (; i < num; in++) {
5561cf8f45c7Sdstaff 		if (*in == delim)
5562cf8f45c7Sdstaff 			i++;
5563cf8f45c7Sdstaff 		if (*in == 0)
5564cf8f45c7Sdstaff 			return (NULL);
5565cf8f45c7Sdstaff 	}
5566cf8f45c7Sdstaff 	return (in);
5567cf8f45c7Sdstaff }
5568cf8f45c7Sdstaff 
5569cf8f45c7Sdstaff static boolean_t
5570cf8f45c7Sdstaff is_ping(sysevent_t *ev)
5571cf8f45c7Sdstaff {
5572cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5573cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
5574cf8f45c7Sdstaff 		return (B_TRUE);
5575cf8f45c7Sdstaff 	} else {
5576cf8f45c7Sdstaff 		return (B_FALSE);
5577cf8f45c7Sdstaff 	}
5578cf8f45c7Sdstaff }
5579cf8f45c7Sdstaff 
5580cf8f45c7Sdstaff static boolean_t
5581cf8f45c7Sdstaff is_my_ping(sysevent_t *ev)
5582cf8f45c7Sdstaff {
5583cf8f45c7Sdstaff 	const char *sender;
5584cf8f45c7Sdstaff 	char mypid[sizeof (pid_t) * 3 + 1];
5585cf8f45c7Sdstaff 
5586cf8f45c7Sdstaff 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
5587cf8f45c7Sdstaff 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
5588cf8f45c7Sdstaff 	if (sender == NULL)
5589cf8f45c7Sdstaff 		return (B_FALSE);
5590cf8f45c7Sdstaff 	if (strcmp(sender, mypid) != 0)
5591cf8f45c7Sdstaff 		return (B_FALSE);
5592cf8f45c7Sdstaff 	return (B_TRUE);
5593cf8f45c7Sdstaff }
5594cf8f45c7Sdstaff 
5595cf8f45c7Sdstaff static int
5596cf8f45c7Sdstaff do_callback(struct znotify *zevtchan, sysevent_t *ev)
5597cf8f45c7Sdstaff {
5598cf8f45c7Sdstaff 	nvlist_t *l;
5599cf8f45c7Sdstaff 	int zid;
5600cf8f45c7Sdstaff 	char *zonename;
5601cf8f45c7Sdstaff 	char *newstate;
5602cf8f45c7Sdstaff 	char *oldstate;
5603cf8f45c7Sdstaff 	int ret;
5604cf8f45c7Sdstaff 	hrtime_t when;
5605cf8f45c7Sdstaff 
5606cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5607cf8f45c7Sdstaff 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
5608cf8f45c7Sdstaff 
5609cf8f45c7Sdstaff 		if (sysevent_get_attr_list(ev, &l) != 0) {
5610cf8f45c7Sdstaff 			if (errno == ENOMEM) {
5611cf8f45c7Sdstaff 				zevtchan->zn_failure_count++;
5612cf8f45c7Sdstaff 				return (EAGAIN);
5613cf8f45c7Sdstaff 			}
5614cf8f45c7Sdstaff 			return (0);
5615cf8f45c7Sdstaff 		}
5616cf8f45c7Sdstaff 		ret = 0;
5617cf8f45c7Sdstaff 
5618cf8f45c7Sdstaff 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
5619cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
5620cf8f45c7Sdstaff 		    == 0) &&
5621cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
5622cf8f45c7Sdstaff 		    == 0) &&
5623cf8f45c7Sdstaff 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
5624cf8f45c7Sdstaff 		    (uint64_t *)&when) == 0) &&
5625cf8f45c7Sdstaff 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
5626cf8f45c7Sdstaff 			ret = zevtchan->zn_callback(zonename, zid, newstate,
5627cf8f45c7Sdstaff 			    oldstate, when, zevtchan->zn_private);
5628cf8f45c7Sdstaff 		}
5629cf8f45c7Sdstaff 
5630cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5631cf8f45c7Sdstaff 		nvlist_free(l);
5632cf8f45c7Sdstaff 		return (ret);
5633cf8f45c7Sdstaff 	} else {
5634cf8f45c7Sdstaff 		/*
5635cf8f45c7Sdstaff 		 * We have received an event in an unknown subclass. Ignore.
5636cf8f45c7Sdstaff 		 */
5637cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5638cf8f45c7Sdstaff 		return (0);
5639cf8f45c7Sdstaff 	}
5640cf8f45c7Sdstaff }
5641cf8f45c7Sdstaff 
5642cf8f45c7Sdstaff static int
5643cf8f45c7Sdstaff zn_cb(sysevent_t *ev, void *p)
5644cf8f45c7Sdstaff {
5645cf8f45c7Sdstaff 	struct znotify *zevtchan = p;
5646cf8f45c7Sdstaff 	int error;
5647cf8f45c7Sdstaff 
5648cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5649cf8f45c7Sdstaff 
5650cf8f45c7Sdstaff 	if (is_ping(ev) && !is_my_ping(ev)) {
5651cf8f45c7Sdstaff 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
5652cf8f45c7Sdstaff 		return (0);
5653cf8f45c7Sdstaff 	}
5654cf8f45c7Sdstaff 
5655cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_LOCKED) {
5656cf8f45c7Sdstaff 		assert(!is_ping(ev));
5657cf8f45c7Sdstaff 		zevtchan->zn_failed = B_TRUE;
5658cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5659cf8f45c7Sdstaff 		return (0);
5660cf8f45c7Sdstaff 	}
5661cf8f45c7Sdstaff 
5662cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
5663cf8f45c7Sdstaff 		if (is_ping(ev)) {
5664cf8f45c7Sdstaff 			zevtchan->zn_state = ZN_PING_RECEIVED;
5665cf8f45c7Sdstaff 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
5666cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5667cf8f45c7Sdstaff 			return (0);
5668cf8f45c7Sdstaff 		} else {
5669cf8f45c7Sdstaff 			zevtchan->zn_failed = B_TRUE;
5670cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5671cf8f45c7Sdstaff 			return (0);
5672cf8f45c7Sdstaff 		}
5673cf8f45c7Sdstaff 	}
5674cf8f45c7Sdstaff 
5675cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED) {
5676cf8f45c7Sdstaff 
5677cf8f45c7Sdstaff 		error = do_callback(zevtchan, ev);
5678cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5679cf8f45c7Sdstaff 		/*
5680cf8f45c7Sdstaff 		 * Every ENOMEM failure causes do_callback to increment
5681cf8f45c7Sdstaff 		 * zn_failure_count and every success causes it to
5682cf8f45c7Sdstaff 		 * set zn_failure_count to zero.  If we got EAGAIN,
5683cf8f45c7Sdstaff 		 * we will sleep for zn_failure_count seconds and return
5684cf8f45c7Sdstaff 		 * EAGAIN to gpec to try again.
5685cf8f45c7Sdstaff 		 *
5686cf8f45c7Sdstaff 		 * After 55 seconds, or 10 try's we give up and drop the
5687cf8f45c7Sdstaff 		 * event.
5688cf8f45c7Sdstaff 		 */
5689cf8f45c7Sdstaff 		if (error == EAGAIN) {
5690cf8f45c7Sdstaff 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
5691cf8f45c7Sdstaff 				return (0);
5692cf8f45c7Sdstaff 			}
5693cf8f45c7Sdstaff 			(void) sleep(zevtchan->zn_failure_count);
5694cf8f45c7Sdstaff 		}
5695cf8f45c7Sdstaff 		return (error);
5696cf8f45c7Sdstaff 	}
5697cf8f45c7Sdstaff 
5698cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
5699cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5700cf8f45c7Sdstaff 		return (0);
5701cf8f45c7Sdstaff 	}
5702cf8f45c7Sdstaff 
5703cf8f45c7Sdstaff 	abort();
5704cf8f45c7Sdstaff 	return (0);
5705cf8f45c7Sdstaff }
5706cf8f45c7Sdstaff 
5707cf8f45c7Sdstaff void
5708cf8f45c7Sdstaff zonecfg_notify_critical_enter(void *h)
5709cf8f45c7Sdstaff {
5710cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5711cf8f45c7Sdstaff 
5712cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
5713cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_LOCKED;
5714cf8f45c7Sdstaff }
5715cf8f45c7Sdstaff 
5716cf8f45c7Sdstaff int
5717cf8f45c7Sdstaff zonecfg_notify_critical_exit(void * h)
5718cf8f45c7Sdstaff {
5719cf8f45c7Sdstaff 
5720cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5721cf8f45c7Sdstaff 
5722cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED)
5723cf8f45c7Sdstaff 		return (0);
5724cf8f45c7Sdstaff 
5725cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5726cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_PING_INFLIGHT;
5727cf8f45c7Sdstaff 
5728ee519a1fSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
5729ee519a1fSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
5730cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
5731cf8f45c7Sdstaff 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
5732cf8f45c7Sdstaff 
5733cf8f45c7Sdstaff 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
5734cf8f45c7Sdstaff 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
5735cf8f45c7Sdstaff 		    &(zevtchan->zn_mutex));
5736cf8f45c7Sdstaff 	}
5737cf8f45c7Sdstaff 
5738cf8f45c7Sdstaff 	if (zevtchan->zn_failed == B_TRUE) {
5739cf8f45c7Sdstaff 		zevtchan->zn_state = ZN_LOCKED;
5740cf8f45c7Sdstaff 		zevtchan->zn_failed = B_FALSE;
5741cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5742cf8f45c7Sdstaff 		return (1);
5743cf8f45c7Sdstaff 	}
5744cf8f45c7Sdstaff 
5745cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5746cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5747cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5748cf8f45c7Sdstaff 	return (0);
5749cf8f45c7Sdstaff }
5750cf8f45c7Sdstaff 
5751cf8f45c7Sdstaff void
5752cf8f45c7Sdstaff zonecfg_notify_critical_abort(void *h)
5753cf8f45c7Sdstaff {
5754cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5755cf8f45c7Sdstaff 
5756cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5757cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
5758cf8f45c7Sdstaff 	/*
5759cf8f45c7Sdstaff 	 * Don't do anything about zn_lock. If it is held, it could only be
5760cf8f45c7Sdstaff 	 * held by zn_cb and it will be unlocked soon.
5761cf8f45c7Sdstaff 	 */
5762cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5763cf8f45c7Sdstaff }
5764cf8f45c7Sdstaff 
5765cf8f45c7Sdstaff void *
5766cf8f45c7Sdstaff zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
5767cf8f45c7Sdstaff     const char *newstate, const char *oldstate, hrtime_t when, void *p),
5768cf8f45c7Sdstaff     void *p)
5769cf8f45c7Sdstaff {
5770cf8f45c7Sdstaff 	struct znotify *zevtchan;
5771cf8f45c7Sdstaff 	int i = 1;
5772cf8f45c7Sdstaff 	int r;
5773cf8f45c7Sdstaff 
5774cf8f45c7Sdstaff 	zevtchan = malloc(sizeof (struct znotify));
5775cf8f45c7Sdstaff 
5776cf8f45c7Sdstaff 	if (zevtchan == NULL)
5777cf8f45c7Sdstaff 		return (NULL);
5778cf8f45c7Sdstaff 
5779cf8f45c7Sdstaff 	zevtchan->zn_private = p;
5780cf8f45c7Sdstaff 	zevtchan->zn_callback = func;
5781cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5782cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
5783cf8f45c7Sdstaff 
5784cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
57859d4be64eSdstaff 		goto out3;
5786cf8f45c7Sdstaff 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
5787cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
57889d4be64eSdstaff 		goto out3;
5789cf8f45c7Sdstaff 	}
5790cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
5791cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
5792cf8f45c7Sdstaff 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
57939d4be64eSdstaff 		goto out3;
5794cf8f45c7Sdstaff 	}
5795cf8f45c7Sdstaff 
5796cf8f45c7Sdstaff 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
5797cf8f45c7Sdstaff 	    0) != 0)
5798cf8f45c7Sdstaff 		goto out2;
5799cf8f45c7Sdstaff 
5800cf8f45c7Sdstaff 	do {
5801cf8f45c7Sdstaff 		/*
5802cf8f45c7Sdstaff 		 * At 4 digits the subscriber ID gets too long and we have
5803cf8f45c7Sdstaff 		 * no chance of successfully registering.
5804cf8f45c7Sdstaff 		 */
5805cf8f45c7Sdstaff 		if (i > 999)
58069d4be64eSdstaff 			goto out1;
5807cf8f45c7Sdstaff 
5808cf8f45c7Sdstaff 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
5809cf8f45c7Sdstaff 		    getpid() % 999999l, i);
5810cf8f45c7Sdstaff 
5811cf8f45c7Sdstaff 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
5812cf8f45c7Sdstaff 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
5813cf8f45c7Sdstaff 		    zevtchan, 0);
5814cf8f45c7Sdstaff 
5815cf8f45c7Sdstaff 		i++;
5816cf8f45c7Sdstaff 
5817cf8f45c7Sdstaff 	} while (r);
5818cf8f45c7Sdstaff 
5819cf8f45c7Sdstaff 	return (zevtchan);
58209d4be64eSdstaff out1:
5821cf8f45c7Sdstaff 	sysevent_evc_unbind(zevtchan->zn_eventchan);
58229d4be64eSdstaff out2:
5823cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
5824cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
5825cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
58269d4be64eSdstaff out3:
5827cf8f45c7Sdstaff 	free(zevtchan);
5828cf8f45c7Sdstaff 
5829cf8f45c7Sdstaff 	return (NULL);
5830cf8f45c7Sdstaff }
5831cf8f45c7Sdstaff 
5832cf8f45c7Sdstaff void
5833cf8f45c7Sdstaff zonecfg_notify_unbind(void *handle)
5834cf8f45c7Sdstaff {
5835cf8f45c7Sdstaff 
5836cf8f45c7Sdstaff 	int ret;
5837cf8f45c7Sdstaff 
5838cf8f45c7Sdstaff 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
5839cf8f45c7Sdstaff 	/*
5840cf8f45c7Sdstaff 	 * Check that all evc threads have gone away. This should be
5841cf8f45c7Sdstaff 	 * enforced by sysevent_evc_unbind.
5842cf8f45c7Sdstaff 	 */
5843cf8f45c7Sdstaff 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
5844cf8f45c7Sdstaff 
5845cf8f45c7Sdstaff 	if (ret)
5846cf8f45c7Sdstaff 		abort();
5847cf8f45c7Sdstaff 
5848cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
5849cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
5850cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
5851cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
5852cf8f45c7Sdstaff 
5853cf8f45c7Sdstaff 	free(handle);
5854cf8f45c7Sdstaff }
5855cf8f45c7Sdstaff 
5856fa9e4066Sahrens static int
5857fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5858fa9e4066Sahrens {
5859fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
5860fa9e4066Sahrens 	int err;
5861fa9e4066Sahrens 
5862fa9e4066Sahrens 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
5863fa9e4066Sahrens 	if ((err = newprop(newnode, DTD_ATTR_NAME,
5864fa9e4066Sahrens 	    tabptr->zone_dataset_name)) != Z_OK)
5865fa9e4066Sahrens 		return (err);
5866fa9e4066Sahrens 	return (Z_OK);
5867fa9e4066Sahrens }
5868fa9e4066Sahrens 
5869fa9e4066Sahrens int
5870fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5871fa9e4066Sahrens {
5872fa9e4066Sahrens 	int err;
5873fa9e4066Sahrens 
5874fa9e4066Sahrens 	if (tabptr == NULL)
5875fa9e4066Sahrens 		return (Z_INVAL);
5876fa9e4066Sahrens 
5877fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5878fa9e4066Sahrens 		return (err);
5879fa9e4066Sahrens 
5880fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
5881fa9e4066Sahrens 		return (err);
5882fa9e4066Sahrens 
5883fa9e4066Sahrens 	return (Z_OK);
5884fa9e4066Sahrens }
5885fa9e4066Sahrens 
5886fa9e4066Sahrens static int
5887fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5888fa9e4066Sahrens {
5889fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
5890fa9e4066Sahrens 
5891fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
5892fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
5893fa9e4066Sahrens 			continue;
5894fa9e4066Sahrens 
5895fa9e4066Sahrens 		if (match_prop(cur, DTD_ATTR_NAME,
5896fa9e4066Sahrens 		    tabptr->zone_dataset_name)) {
5897fa9e4066Sahrens 			xmlUnlinkNode(cur);
5898fa9e4066Sahrens 			xmlFreeNode(cur);
5899fa9e4066Sahrens 			return (Z_OK);
5900fa9e4066Sahrens 		}
5901fa9e4066Sahrens 	}
5902fa9e4066Sahrens 	return (Z_NO_RESOURCE_ID);
5903fa9e4066Sahrens }
5904fa9e4066Sahrens 
5905fa9e4066Sahrens int
5906fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5907fa9e4066Sahrens {
5908fa9e4066Sahrens 	int err;
5909fa9e4066Sahrens 
5910fa9e4066Sahrens 	if (tabptr == NULL)
5911fa9e4066Sahrens 		return (Z_INVAL);
5912fa9e4066Sahrens 
5913fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5914fa9e4066Sahrens 		return (err);
5915fa9e4066Sahrens 
5916fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
5917fa9e4066Sahrens 		return (err);
5918fa9e4066Sahrens 
5919fa9e4066Sahrens 	return (Z_OK);
5920fa9e4066Sahrens }
5921fa9e4066Sahrens 
5922fa9e4066Sahrens int
5923fa9e4066Sahrens zonecfg_modify_ds(
5924fa9e4066Sahrens 	zone_dochandle_t handle,
5925fa9e4066Sahrens 	struct zone_dstab *oldtabptr,
5926fa9e4066Sahrens 	struct zone_dstab *newtabptr)
5927fa9e4066Sahrens {
5928fa9e4066Sahrens 	int err;
5929fa9e4066Sahrens 
5930fa9e4066Sahrens 	if (oldtabptr == NULL || newtabptr == NULL)
5931fa9e4066Sahrens 		return (Z_INVAL);
5932fa9e4066Sahrens 
5933fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5934fa9e4066Sahrens 		return (err);
5935fa9e4066Sahrens 
5936fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
5937fa9e4066Sahrens 		return (err);
5938fa9e4066Sahrens 
5939fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
5940fa9e4066Sahrens 		return (err);
5941fa9e4066Sahrens 
5942fa9e4066Sahrens 	return (Z_OK);
5943fa9e4066Sahrens }
5944fa9e4066Sahrens 
5945fa9e4066Sahrens int
5946fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5947fa9e4066Sahrens {
5948fa9e4066Sahrens 	xmlNodePtr cur, firstmatch;
5949fa9e4066Sahrens 	int err;
5950fa9e4066Sahrens 	char dataset[MAXNAMELEN];
5951fa9e4066Sahrens 
5952fa9e4066Sahrens 	if (tabptr == NULL)
5953fa9e4066Sahrens 		return (Z_INVAL);
5954fa9e4066Sahrens 
5955fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5956fa9e4066Sahrens 		return (err);
5957fa9e4066Sahrens 
5958fa9e4066Sahrens 	cur = handle->zone_dh_cur;
5959fa9e4066Sahrens 	firstmatch = NULL;
5960fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
5961fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
5962fa9e4066Sahrens 			continue;
5963fa9e4066Sahrens 		if (strlen(tabptr->zone_dataset_name) > 0) {
5964fa9e4066Sahrens 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
5965fa9e4066Sahrens 			    sizeof (dataset)) == Z_OK) &&
5966fa9e4066Sahrens 			    (strcmp(tabptr->zone_dataset_name,
5967fa9e4066Sahrens 			    dataset) == 0)) {
5968fa9e4066Sahrens 				if (firstmatch == NULL)
5969fa9e4066Sahrens 					firstmatch = cur;
5970fa9e4066Sahrens 				else
5971fa9e4066Sahrens 					return (Z_INSUFFICIENT_SPEC);
5972fa9e4066Sahrens 			}
5973fa9e4066Sahrens 		}
5974fa9e4066Sahrens 	}
5975fa9e4066Sahrens 	if (firstmatch == NULL)
5976fa9e4066Sahrens 		return (Z_NO_RESOURCE_ID);
5977fa9e4066Sahrens 
5978fa9e4066Sahrens 	cur = firstmatch;
5979fa9e4066Sahrens 
5980fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
5981fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
5982fa9e4066Sahrens 		return (err);
5983fa9e4066Sahrens 
5984fa9e4066Sahrens 	return (Z_OK);
5985fa9e4066Sahrens }
5986fa9e4066Sahrens 
5987fa9e4066Sahrens int
5988fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle)
5989fa9e4066Sahrens {
5990fa9e4066Sahrens 	return (zonecfg_setent(handle));
5991fa9e4066Sahrens }
5992fa9e4066Sahrens 
5993fa9e4066Sahrens int
5994fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
5995fa9e4066Sahrens {
5996fa9e4066Sahrens 	xmlNodePtr cur;
5997fa9e4066Sahrens 	int err;
5998fa9e4066Sahrens 
5999fa9e4066Sahrens 	if (handle == NULL)
6000fa9e4066Sahrens 		return (Z_INVAL);
6001fa9e4066Sahrens 
6002fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
6003fa9e4066Sahrens 		return (Z_NO_ENTRY);
6004fa9e4066Sahrens 
6005fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
6006fa9e4066Sahrens 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6007fa9e4066Sahrens 			break;
6008fa9e4066Sahrens 	if (cur == NULL) {
6009fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6010fa9e4066Sahrens 		return (Z_NO_ENTRY);
6011fa9e4066Sahrens 	}
6012fa9e4066Sahrens 
6013fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6014fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
6015fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6016fa9e4066Sahrens 		return (err);
6017fa9e4066Sahrens 	}
6018fa9e4066Sahrens 
6019fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
6020fa9e4066Sahrens 	return (Z_OK);
6021fa9e4066Sahrens }
6022fa9e4066Sahrens 
6023fa9e4066Sahrens int
6024fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle)
6025fa9e4066Sahrens {
6026fa9e4066Sahrens 	return (zonecfg_endent(handle));
6027fa9e4066Sahrens }
6028ee519a1fSgjelinek 
60290209230bSgjelinek /*
60300209230bSgjelinek  * Support for aliased rctls; that is, rctls that have simplified names in
60310209230bSgjelinek  * zonecfg.  For example, max-lwps is an alias for a well defined zone.max-lwps
60320209230bSgjelinek  * rctl.  If there are multiple existing values for one of these rctls or if
60330209230bSgjelinek  * there is a single value that does not match the well defined template (i.e.
60340209230bSgjelinek  * it has a different action) then we cannot treat the rctl as having an alias
60350209230bSgjelinek  * so we return Z_ALIAS_DISALLOW.  That means that the rctl cannot be
60360209230bSgjelinek  * managed in zonecfg via an alias and that the standard rctl syntax must be
60370209230bSgjelinek  * used.
60380209230bSgjelinek  *
60390209230bSgjelinek  * The possible return values are:
60400209230bSgjelinek  *	Z_NO_PROPERTY_ID - invalid alias name
60410209230bSgjelinek  *	Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition
60420209230bSgjelinek  *	Z_NO_ENTRY - no rctl is configured for this alias
60430209230bSgjelinek  *	Z_OK - we got a valid rctl for the specified alias
60440209230bSgjelinek  */
60450209230bSgjelinek int
60460209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval)
60470209230bSgjelinek {
60480209230bSgjelinek 	boolean_t found = B_FALSE;
60490209230bSgjelinek 	boolean_t found_val = B_FALSE;
60500209230bSgjelinek 	xmlNodePtr cur, val;
60510209230bSgjelinek 	char savedname[MAXNAMELEN];
60520209230bSgjelinek 	struct zone_rctlvaltab rctl;
60530209230bSgjelinek 	int i;
60540209230bSgjelinek 	int err;
60550209230bSgjelinek 
60560209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
60570209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
60580209230bSgjelinek 			break;
60590209230bSgjelinek 
60600209230bSgjelinek 	if (aliases[i].shortname == NULL)
60610209230bSgjelinek 		return (Z_NO_PROPERTY_ID);
60620209230bSgjelinek 
60630209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
60640209230bSgjelinek 		return (err);
60650209230bSgjelinek 
60660209230bSgjelinek 	cur = handle->zone_dh_cur;
60670209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
60680209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0)
60690209230bSgjelinek 			continue;
60700209230bSgjelinek 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
60710209230bSgjelinek 		    sizeof (savedname)) == Z_OK) &&
60720209230bSgjelinek 		    (strcmp(savedname, aliases[i].realname) == 0)) {
60730209230bSgjelinek 
60740209230bSgjelinek 			/*
60750209230bSgjelinek 			 * If we already saw one of these, we can't have an
60760209230bSgjelinek 			 * alias since we just found another.
60770209230bSgjelinek 			 */
60780209230bSgjelinek 			if (found)
60790209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
60800209230bSgjelinek 			found = B_TRUE;
60810209230bSgjelinek 
60820209230bSgjelinek 			for (val = cur->xmlChildrenNode; val != NULL;
60830209230bSgjelinek 			    val = val->next) {
60840209230bSgjelinek 				/*
60850209230bSgjelinek 				 * If we already have one value, we can't have
60860209230bSgjelinek 				 * an alias since we just found another.
60870209230bSgjelinek 				 */
60880209230bSgjelinek 				if (found_val)
60890209230bSgjelinek 					return (Z_ALIAS_DISALLOW);
60900209230bSgjelinek 				found_val = B_TRUE;
60910209230bSgjelinek 
60920209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_PRIV,
60930209230bSgjelinek 				    rctl.zone_rctlval_priv,
60940209230bSgjelinek 				    sizeof (rctl.zone_rctlval_priv)) != Z_OK))
60950209230bSgjelinek 					break;
60960209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_LIMIT,
60970209230bSgjelinek 				    rctl.zone_rctlval_limit,
60980209230bSgjelinek 				    sizeof (rctl.zone_rctlval_limit)) != Z_OK))
60990209230bSgjelinek 					break;
61000209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_ACTION,
61010209230bSgjelinek 				    rctl.zone_rctlval_action,
61020209230bSgjelinek 				    sizeof (rctl.zone_rctlval_action)) != Z_OK))
61030209230bSgjelinek 					break;
61040209230bSgjelinek 			}
61050209230bSgjelinek 
61060209230bSgjelinek 			/* check priv and action match the expected vals */
61070209230bSgjelinek 			if (strcmp(rctl.zone_rctlval_priv,
61080209230bSgjelinek 			    aliases[i].priv) != 0 ||
61090209230bSgjelinek 			    strcmp(rctl.zone_rctlval_action,
61100209230bSgjelinek 			    aliases[i].action) != 0)
61110209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
61120209230bSgjelinek 		}
61130209230bSgjelinek 	}
61140209230bSgjelinek 
61150209230bSgjelinek 	if (found) {
61160209230bSgjelinek 		*rval = strtoull(rctl.zone_rctlval_limit, NULL, 10);
61170209230bSgjelinek 		return (Z_OK);
61180209230bSgjelinek 	}
61190209230bSgjelinek 
61200209230bSgjelinek 	return (Z_NO_ENTRY);
61210209230bSgjelinek }
61220209230bSgjelinek 
61230209230bSgjelinek int
61240209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name)
61250209230bSgjelinek {
61260209230bSgjelinek 	int i;
61270209230bSgjelinek 	uint64_t val;
61280209230bSgjelinek 	struct zone_rctltab rctltab;
61290209230bSgjelinek 
61300209230bSgjelinek 	/*
61310209230bSgjelinek 	 * First check that we have a valid aliased rctl to remove.
61320209230bSgjelinek 	 * This will catch an rctl entry with non-standard values or
61330209230bSgjelinek 	 * multiple rctl values for this name.  We need to ignore those
61340209230bSgjelinek 	 * rctl entries.
61350209230bSgjelinek 	 */
61360209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK)
61370209230bSgjelinek 		return (Z_OK);
61380209230bSgjelinek 
61390209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
61400209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
61410209230bSgjelinek 			break;
61420209230bSgjelinek 
61430209230bSgjelinek 	if (aliases[i].shortname == NULL)
61440209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
61450209230bSgjelinek 
61460209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
61470209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
61480209230bSgjelinek 
61490209230bSgjelinek 	return (zonecfg_delete_rctl(handle, &rctltab));
61500209230bSgjelinek }
61510209230bSgjelinek 
61520209230bSgjelinek boolean_t
61530209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name)
61540209230bSgjelinek {
61550209230bSgjelinek 	uint64_t tmp_val;
61560209230bSgjelinek 
61570209230bSgjelinek 	switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) {
61580209230bSgjelinek 	case Z_OK:
61590209230bSgjelinek 		/*FALLTHRU*/
61600209230bSgjelinek 	case Z_NO_ENTRY:
61610209230bSgjelinek 		return (B_TRUE);
61620209230bSgjelinek 	default:
61630209230bSgjelinek 		return (B_FALSE);
61640209230bSgjelinek 	}
61650209230bSgjelinek }
61660209230bSgjelinek 
61670209230bSgjelinek int
61680209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val)
61690209230bSgjelinek {
61700209230bSgjelinek 	int i;
61710209230bSgjelinek 	int err;
61720209230bSgjelinek 	struct zone_rctltab rctltab;
61730209230bSgjelinek 	struct zone_rctlvaltab *rctlvaltab;
61740209230bSgjelinek 	char buf[128];
61750209230bSgjelinek 
61760209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, name))
61770209230bSgjelinek 		return (Z_ALIAS_DISALLOW);
61780209230bSgjelinek 
61790209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
61800209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
61810209230bSgjelinek 			break;
61820209230bSgjelinek 
61830209230bSgjelinek 	if (aliases[i].shortname == NULL)
61840209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
61850209230bSgjelinek 
61860209230bSgjelinek 	/* remove any pre-existing definition for this rctl */
61870209230bSgjelinek 	(void) zonecfg_rm_aliased_rctl(handle, name);
61880209230bSgjelinek 
61890209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
61900209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
61910209230bSgjelinek 
61920209230bSgjelinek 	rctltab.zone_rctl_valptr = NULL;
61930209230bSgjelinek 
61940209230bSgjelinek 	if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL)
61950209230bSgjelinek 		return (Z_NOMEM);
61960209230bSgjelinek 
61970209230bSgjelinek 	(void) snprintf(buf, sizeof (buf), "%llu", (long long)val);
61980209230bSgjelinek 
61990209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv,
62000209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_priv));
62010209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_limit, buf,
62020209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_limit));
62030209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action,
62040209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_action));
62050209230bSgjelinek 
62060209230bSgjelinek 	rctlvaltab->zone_rctlval_next = NULL;
62070209230bSgjelinek 
62080209230bSgjelinek 	if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK)
62090209230bSgjelinek 		return (err);
62100209230bSgjelinek 
62110209230bSgjelinek 	return (zonecfg_add_rctl(handle, &rctltab));
62120209230bSgjelinek }
62130209230bSgjelinek 
62140209230bSgjelinek static int
62150209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle)
62160209230bSgjelinek {
62170209230bSgjelinek 	int err;
62180209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
62190209230bSgjelinek 
62200209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
62210209230bSgjelinek 		return (err);
62220209230bSgjelinek 
62230209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
62240209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
62250209230bSgjelinek 			xmlUnlinkNode(cur);
62260209230bSgjelinek 			xmlFreeNode(cur);
62270209230bSgjelinek 			return (Z_OK);
62280209230bSgjelinek 		}
62290209230bSgjelinek 	}
62300209230bSgjelinek 
62310209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
62320209230bSgjelinek }
62330209230bSgjelinek 
62340209230bSgjelinek static int
62350209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance)
62360209230bSgjelinek {
62370209230bSgjelinek 	int err;
62380209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
62390209230bSgjelinek 	xmlNodePtr newnode;
62400209230bSgjelinek 
62410209230bSgjelinek 	err = delete_tmp_pool(handle);
62420209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
62430209230bSgjelinek 		return (err);
62440209230bSgjelinek 
62450209230bSgjelinek 	if (*pool_importance != '\0') {
62460209230bSgjelinek 		if ((err = operation_prep(handle)) != Z_OK)
62470209230bSgjelinek 			return (err);
62480209230bSgjelinek 
62490209230bSgjelinek 		newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL);
62500209230bSgjelinek 		if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE,
62510209230bSgjelinek 		    pool_importance)) != Z_OK)
62520209230bSgjelinek 			return (err);
62530209230bSgjelinek 	}
62540209230bSgjelinek 
62550209230bSgjelinek 	return (Z_OK);
62560209230bSgjelinek }
62570209230bSgjelinek 
62580209230bSgjelinek static int
62590209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr)
62600209230bSgjelinek {
62610209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
62620209230bSgjelinek 	int err;
62630209230bSgjelinek 
62640209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL);
62650209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN,
62660209230bSgjelinek 	    tabptr->zone_ncpu_min)) != Z_OK)
62670209230bSgjelinek 		return (err);
62680209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX,
62690209230bSgjelinek 	    tabptr->zone_ncpu_max)) != Z_OK)
62700209230bSgjelinek 		return (err);
62710209230bSgjelinek 
62720209230bSgjelinek 	if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK)
62730209230bSgjelinek 		return (err);
62740209230bSgjelinek 
62750209230bSgjelinek 	return (Z_OK);
62760209230bSgjelinek }
62770209230bSgjelinek 
62780209230bSgjelinek int
62790209230bSgjelinek zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
62800209230bSgjelinek {
62810209230bSgjelinek 	int err;
62820209230bSgjelinek 
62830209230bSgjelinek 	if (tabptr == NULL)
62840209230bSgjelinek 		return (Z_INVAL);
62850209230bSgjelinek 
62860209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
62870209230bSgjelinek 		return (err);
62880209230bSgjelinek 
62890209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
62900209230bSgjelinek 		return (err);
62910209230bSgjelinek 
62920209230bSgjelinek 	return (Z_OK);
62930209230bSgjelinek }
62940209230bSgjelinek 
62950209230bSgjelinek int
62960209230bSgjelinek zonecfg_delete_pset(zone_dochandle_t handle)
62970209230bSgjelinek {
62980209230bSgjelinek 	int err;
62990209230bSgjelinek 	int res = Z_NO_RESOURCE_ID;
63000209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
63010209230bSgjelinek 
63020209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
63030209230bSgjelinek 		return (err);
63040209230bSgjelinek 
63050209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
63060209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
63070209230bSgjelinek 			xmlUnlinkNode(cur);
63080209230bSgjelinek 			xmlFreeNode(cur);
63090209230bSgjelinek 			res = Z_OK;
63100209230bSgjelinek 			break;
63110209230bSgjelinek 		}
63120209230bSgjelinek 	}
63130209230bSgjelinek 
63140209230bSgjelinek 	/*
63150209230bSgjelinek 	 * Once we have msets, we should check that a mset
63160209230bSgjelinek 	 * do not exist before we delete the tmp_pool data.
63170209230bSgjelinek 	 */
63180209230bSgjelinek 	err = delete_tmp_pool(handle);
63190209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
63200209230bSgjelinek 		return (err);
63210209230bSgjelinek 
63220209230bSgjelinek 	return (res);
63230209230bSgjelinek }
63240209230bSgjelinek 
63250209230bSgjelinek int
63260209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
63270209230bSgjelinek {
63280209230bSgjelinek 	int err;
63290209230bSgjelinek 
63300209230bSgjelinek 	if (tabptr == NULL)
63310209230bSgjelinek 		return (Z_INVAL);
63320209230bSgjelinek 
63330209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
63340209230bSgjelinek 		return (err);
63350209230bSgjelinek 
63360209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
63370209230bSgjelinek 		return (err);
63380209230bSgjelinek 
63390209230bSgjelinek 	return (Z_OK);
63400209230bSgjelinek }
63410209230bSgjelinek 
63420209230bSgjelinek int
63430209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
63440209230bSgjelinek {
63450209230bSgjelinek 	xmlNodePtr cur;
63460209230bSgjelinek 	int err;
63470209230bSgjelinek 	int res = Z_NO_ENTRY;
63480209230bSgjelinek 
63490209230bSgjelinek 	if (tabptr == NULL)
63500209230bSgjelinek 		return (Z_INVAL);
63510209230bSgjelinek 
63520209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
63530209230bSgjelinek 		return (err);
63540209230bSgjelinek 
63550209230bSgjelinek 	/* this is an optional component */
63560209230bSgjelinek 	tabptr->zone_importance[0] = '\0';
63570209230bSgjelinek 
63580209230bSgjelinek 	cur = handle->zone_dh_cur;
63590209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
63600209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
63610209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN,
63620209230bSgjelinek 			    tabptr->zone_ncpu_min,
63630209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_min))) != Z_OK) {
63640209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
63650209230bSgjelinek 				return (err);
63660209230bSgjelinek 			}
63670209230bSgjelinek 
63680209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX,
63690209230bSgjelinek 			    tabptr->zone_ncpu_max,
63700209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_max))) != Z_OK) {
63710209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
63720209230bSgjelinek 				return (err);
63730209230bSgjelinek 			}
63740209230bSgjelinek 
63750209230bSgjelinek 			res = Z_OK;
63760209230bSgjelinek 
63770209230bSgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
63780209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE,
63790209230bSgjelinek 			    tabptr->zone_importance,
63800209230bSgjelinek 			    sizeof (tabptr->zone_importance))) != Z_OK) {
63810209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
63820209230bSgjelinek 				return (err);
63830209230bSgjelinek 			}
63840209230bSgjelinek 		}
63850209230bSgjelinek 	}
63860209230bSgjelinek 
63870209230bSgjelinek 	return (res);
63880209230bSgjelinek }
63890209230bSgjelinek 
63900209230bSgjelinek int
63910209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr)
63920209230bSgjelinek {
63930209230bSgjelinek 	int err;
63940209230bSgjelinek 
63950209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
63960209230bSgjelinek 		return (err);
63970209230bSgjelinek 
63980209230bSgjelinek 	err = zonecfg_lookup_pset(handle, tabptr);
63990209230bSgjelinek 
64000209230bSgjelinek 	(void) zonecfg_endent(handle);
64010209230bSgjelinek 
64020209230bSgjelinek 	return (err);
64030209230bSgjelinek }
64040209230bSgjelinek 
64050209230bSgjelinek static int
64060209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
64070209230bSgjelinek {
64080209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
64090209230bSgjelinek 	int err;
64100209230bSgjelinek 
64110209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL);
64120209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap))
64130209230bSgjelinek 	    != Z_OK)
64140209230bSgjelinek 		return (err);
64150209230bSgjelinek 
64160209230bSgjelinek 	return (Z_OK);
64170209230bSgjelinek }
64180209230bSgjelinek 
64190209230bSgjelinek int
64200209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle)
64210209230bSgjelinek {
64220209230bSgjelinek 	int err;
64230209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
64240209230bSgjelinek 
64250209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64260209230bSgjelinek 		return (err);
64270209230bSgjelinek 
64280209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64290209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
64300209230bSgjelinek 			continue;
64310209230bSgjelinek 
64320209230bSgjelinek 		xmlUnlinkNode(cur);
64330209230bSgjelinek 		xmlFreeNode(cur);
64340209230bSgjelinek 		return (Z_OK);
64350209230bSgjelinek 	}
64360209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
64370209230bSgjelinek }
64380209230bSgjelinek 
64390209230bSgjelinek int
64400209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
64410209230bSgjelinek {
64420209230bSgjelinek 	int err;
64430209230bSgjelinek 
64440209230bSgjelinek 	if (tabptr == NULL)
64450209230bSgjelinek 		return (Z_INVAL);
64460209230bSgjelinek 
64470209230bSgjelinek 	err = zonecfg_delete_mcap(handle);
64480209230bSgjelinek 	/* it is ok if there is no mcap entry */
64490209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
64500209230bSgjelinek 		return (err);
64510209230bSgjelinek 
64520209230bSgjelinek 	if ((err = add_mcap(handle, tabptr)) != Z_OK)
64530209230bSgjelinek 		return (err);
64540209230bSgjelinek 
64550209230bSgjelinek 	return (Z_OK);
64560209230bSgjelinek }
64570209230bSgjelinek 
64580209230bSgjelinek int
64590209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
64600209230bSgjelinek {
64610209230bSgjelinek 	xmlNodePtr cur;
64620209230bSgjelinek 	int err;
64630209230bSgjelinek 
64640209230bSgjelinek 	if (tabptr == NULL)
64650209230bSgjelinek 		return (Z_INVAL);
64660209230bSgjelinek 
64670209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64680209230bSgjelinek 		return (err);
64690209230bSgjelinek 
64700209230bSgjelinek 	cur = handle->zone_dh_cur;
64710209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64720209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
64730209230bSgjelinek 			continue;
64740209230bSgjelinek 		if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP,
64750209230bSgjelinek 		    tabptr->zone_physmem_cap,
64760209230bSgjelinek 		    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
64770209230bSgjelinek 			handle->zone_dh_cur = handle->zone_dh_top;
64780209230bSgjelinek 			return (err);
64790209230bSgjelinek 		}
64800209230bSgjelinek 
64810209230bSgjelinek 		return (Z_OK);
64820209230bSgjelinek 	}
64830209230bSgjelinek 
64840209230bSgjelinek 	return (Z_NO_ENTRY);
64850209230bSgjelinek }
64860209230bSgjelinek 
64870209230bSgjelinek static int
64880209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
64890209230bSgjelinek {
64900209230bSgjelinek 	xmlNodePtr cur;
64910209230bSgjelinek 	int err;
64920209230bSgjelinek 
64930209230bSgjelinek 	if (handle == NULL)
64940209230bSgjelinek 		return (Z_INVAL);
64950209230bSgjelinek 
64960209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
64970209230bSgjelinek 		return (Z_NO_ENTRY);
64980209230bSgjelinek 
64990209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
65000209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0)
65010209230bSgjelinek 			break;
65020209230bSgjelinek 	if (cur == NULL) {
65030209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
65040209230bSgjelinek 		return (Z_NO_ENTRY);
65050209230bSgjelinek 	}
65060209230bSgjelinek 
65070209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap,
65080209230bSgjelinek 	    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
65090209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
65100209230bSgjelinek 		return (err);
65110209230bSgjelinek 	}
65120209230bSgjelinek 
65130209230bSgjelinek 	handle->zone_dh_cur = cur->next;
65140209230bSgjelinek 	return (Z_OK);
65150209230bSgjelinek }
65160209230bSgjelinek 
65170209230bSgjelinek int
65180209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
65190209230bSgjelinek {
65200209230bSgjelinek 	int err;
65210209230bSgjelinek 
65220209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
65230209230bSgjelinek 		return (err);
65240209230bSgjelinek 
65250209230bSgjelinek 	err = getmcapent_core(handle, tabptr);
65260209230bSgjelinek 
65270209230bSgjelinek 	(void) zonecfg_endent(handle);
65280209230bSgjelinek 
65290209230bSgjelinek 	return (err);
65300209230bSgjelinek }
65310209230bSgjelinek 
65326cfd72c6Sgjelinek /*
65336cfd72c6Sgjelinek  * Get the full tree of pkg/patch metadata in a set of nested AVL trees.
65346cfd72c6Sgjelinek  * pkgs_avl is an AVL tree of pkgs.  Each pkg element contains a
65356cfd72c6Sgjelinek  * zpe_patches_avl member which holds an AVL tree of patches for that pkg.
65366cfd72c6Sgjelinek  * The patch elements have the same zpe_patches_avl member, each of which can
65376cfd72c6Sgjelinek  * hold an AVL tree of patches that are obsoleted by the patch.
65386cfd72c6Sgjelinek  *
65396cfd72c6Sgjelinek  * The zone xml data contains DTD_ELEM_PACKAGE elements, followed by
65406cfd72c6Sgjelinek  * DTD_ELEM_PATCH elements.  The DTD_ELEM_PATCH patch element applies to the
65416cfd72c6Sgjelinek  * DTD_ELEM_PACKAGE that precedes it.  The DTD_ELEM_PATCH element may have
65426cfd72c6Sgjelinek  * child DTD_ELEM_OBSOLETES nodes associated with it.  The DTD_ELEM_PACKAGE
65436cfd72c6Sgjelinek  * really should have had the DTD_ELEM_PATCH elements as children but it
65446cfd72c6Sgjelinek  * was not defined that way initially so we are stuck with the DTD definition
65456cfd72c6Sgjelinek  * now.  However, we can safely assume the ordering for compatibility.
65466cfd72c6Sgjelinek  */
6547ee519a1fSgjelinek int
65486cfd72c6Sgjelinek zonecfg_getpkgdata(zone_dochandle_t handle, uu_avl_pool_t *pkg_pool,
65496cfd72c6Sgjelinek     uu_avl_t *pkgs_avl)
6550ee519a1fSgjelinek {
6551ee519a1fSgjelinek 	xmlNodePtr cur;
65526cfd72c6Sgjelinek 	int res;
65536cfd72c6Sgjelinek 	zone_pkg_entry_t *pkg;
65546cfd72c6Sgjelinek 	char name[MAXNAMELEN];
65556cfd72c6Sgjelinek 	char version[ZONE_PKG_VERSMAX];
6556ee519a1fSgjelinek 
6557ee519a1fSgjelinek 	if (handle == NULL)
6558ee519a1fSgjelinek 		return (Z_INVAL);
6559ee519a1fSgjelinek 
65606cfd72c6Sgjelinek 	if ((res = zonecfg_setent(handle)) != Z_OK)
65616cfd72c6Sgjelinek 		return (res);
6562ee519a1fSgjelinek 
65636cfd72c6Sgjelinek 	if ((cur = handle->zone_dh_cur) == NULL) {
65646cfd72c6Sgjelinek 		res = Z_NO_ENTRY;
65656cfd72c6Sgjelinek 		goto done;
6566ee519a1fSgjelinek 	}
6567ee519a1fSgjelinek 
65686cfd72c6Sgjelinek 	for (; cur != NULL; cur = cur->next) {
65696cfd72c6Sgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PACKAGE) == 0) {
65706cfd72c6Sgjelinek 			uu_avl_index_t where;
65716cfd72c6Sgjelinek 
65726cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_NAME, name,
65736cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
65746cfd72c6Sgjelinek 				goto done;
65756cfd72c6Sgjelinek 
65766cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_VERSION, version,
65776cfd72c6Sgjelinek 			    sizeof (version))) != Z_OK)
65786cfd72c6Sgjelinek 				goto done;
65796cfd72c6Sgjelinek 
65806cfd72c6Sgjelinek 			if ((pkg = (zone_pkg_entry_t *)
65816cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
65826cfd72c6Sgjelinek 				res = Z_NOMEM;
65836cfd72c6Sgjelinek 				goto done;
6584ee519a1fSgjelinek 			}
6585ee519a1fSgjelinek 
65866cfd72c6Sgjelinek 			if ((pkg->zpe_name = strdup(name)) == NULL) {
65876cfd72c6Sgjelinek 				free(pkg);
65886cfd72c6Sgjelinek 				res = Z_NOMEM;
65896cfd72c6Sgjelinek 				goto done;
6590ee519a1fSgjelinek 			}
6591ee519a1fSgjelinek 
65926cfd72c6Sgjelinek 			if ((pkg->zpe_vers = strdup(version)) == NULL) {
65936cfd72c6Sgjelinek 				free(pkg->zpe_name);
65946cfd72c6Sgjelinek 				free(pkg);
65956cfd72c6Sgjelinek 				res = Z_NOMEM;
65966cfd72c6Sgjelinek 				goto done;
6597ee519a1fSgjelinek 			}
6598ee519a1fSgjelinek 
65996cfd72c6Sgjelinek 			pkg->zpe_patches_avl = NULL;
66006cfd72c6Sgjelinek 
66016cfd72c6Sgjelinek 			uu_avl_node_init(pkg, &pkg->zpe_entry, pkg_pool);
66026cfd72c6Sgjelinek 			if (uu_avl_find(pkgs_avl, pkg, NULL, &where) != NULL) {
66036cfd72c6Sgjelinek 				free(pkg->zpe_name);
66046cfd72c6Sgjelinek 				free(pkg->zpe_vers);
66056cfd72c6Sgjelinek 				free(pkg);
66066cfd72c6Sgjelinek 			} else {
66076cfd72c6Sgjelinek 				uu_avl_insert(pkgs_avl, pkg, where);
6608ee519a1fSgjelinek 			}
6609ee519a1fSgjelinek 
66106cfd72c6Sgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
66116cfd72c6Sgjelinek 			zone_pkg_entry_t *patch;
66126cfd72c6Sgjelinek 			uu_avl_index_t where;
66136cfd72c6Sgjelinek 			char *p;
66146cfd72c6Sgjelinek 			char *dashp = NULL;
66156cfd72c6Sgjelinek 			xmlNodePtr child;
66166cfd72c6Sgjelinek 
66176cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_ID, name,
66186cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
66196cfd72c6Sgjelinek 				goto done;
66206cfd72c6Sgjelinek 
66216cfd72c6Sgjelinek 			if ((patch = (zone_pkg_entry_t *)
66226cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
66236cfd72c6Sgjelinek 				res = Z_NOMEM;
66246cfd72c6Sgjelinek 				goto done;
6625ee519a1fSgjelinek 			}
6626ee519a1fSgjelinek 
66276cfd72c6Sgjelinek 			if ((p = strchr(name, '-')) != NULL) {
66286cfd72c6Sgjelinek 				dashp = p;
66296cfd72c6Sgjelinek 				*p++ = '\0';
66306cfd72c6Sgjelinek 			} else {
66316cfd72c6Sgjelinek 				p = "";
6632ee519a1fSgjelinek 			}
6633ee519a1fSgjelinek 
66346cfd72c6Sgjelinek 			if ((patch->zpe_name = strdup(name)) == NULL) {
66356cfd72c6Sgjelinek 				free(patch);
66366cfd72c6Sgjelinek 				res = Z_NOMEM;
66376cfd72c6Sgjelinek 				goto done;
6638ee519a1fSgjelinek 			}
6639ee519a1fSgjelinek 
66406cfd72c6Sgjelinek 			if ((patch->zpe_vers = strdup(p)) == NULL) {
66416cfd72c6Sgjelinek 				free(patch->zpe_name);
66426cfd72c6Sgjelinek 				free(patch);
66436cfd72c6Sgjelinek 				res = Z_NOMEM;
66446cfd72c6Sgjelinek 				goto done;
6645ee519a1fSgjelinek 			}
6646ee519a1fSgjelinek 
66476cfd72c6Sgjelinek 			if (dashp != NULL)
66486cfd72c6Sgjelinek 				*dashp = '-';
66496cfd72c6Sgjelinek 
66506cfd72c6Sgjelinek 			patch->zpe_patches_avl = NULL;
66516cfd72c6Sgjelinek 
66526cfd72c6Sgjelinek 			if (pkg->zpe_patches_avl == NULL) {
66536cfd72c6Sgjelinek 				pkg->zpe_patches_avl = uu_avl_create(pkg_pool,
66546cfd72c6Sgjelinek 				    NULL, UU_DEFAULT);
66556cfd72c6Sgjelinek 				if (pkg->zpe_patches_avl == NULL) {
66566cfd72c6Sgjelinek 					free(patch->zpe_name);
66576cfd72c6Sgjelinek 					free(patch->zpe_vers);
66586cfd72c6Sgjelinek 					free(patch);
66596cfd72c6Sgjelinek 					res = Z_NOMEM;
66606cfd72c6Sgjelinek 					goto done;
66616cfd72c6Sgjelinek 				}
66626cfd72c6Sgjelinek 			}
66636cfd72c6Sgjelinek 
66646cfd72c6Sgjelinek 			uu_avl_node_init(patch, &patch->zpe_entry, pkg_pool);
66656cfd72c6Sgjelinek 			if (uu_avl_find(pkg->zpe_patches_avl, patch, NULL,
66666cfd72c6Sgjelinek 			    &where) != NULL) {
66676cfd72c6Sgjelinek 				free(patch->zpe_name);
66686cfd72c6Sgjelinek 				free(patch->zpe_vers);
66696cfd72c6Sgjelinek 				free(patch);
66706cfd72c6Sgjelinek 			} else {
66716cfd72c6Sgjelinek 				uu_avl_insert(pkg->zpe_patches_avl, patch,
66726cfd72c6Sgjelinek 				    where);
66736cfd72c6Sgjelinek 			}
66746cfd72c6Sgjelinek 
66756cfd72c6Sgjelinek 			/* Add any patches this patch obsoletes. */
66766cfd72c6Sgjelinek 			for (child = cur->xmlChildrenNode; child != NULL;
66776cfd72c6Sgjelinek 			    child = child->next) {
66786cfd72c6Sgjelinek 				zone_pkg_entry_t *obs;
66796cfd72c6Sgjelinek 
66806cfd72c6Sgjelinek 				if (xmlStrcmp(child->name, DTD_ELEM_OBSOLETES)
66816cfd72c6Sgjelinek 				    != 0)
66826cfd72c6Sgjelinek 					continue;
66836cfd72c6Sgjelinek 
66846cfd72c6Sgjelinek 				if ((res = fetchprop(child, DTD_ATTR_ID,
66856cfd72c6Sgjelinek 				    name, sizeof (name))) != Z_OK)
66866cfd72c6Sgjelinek 					goto done;
66876cfd72c6Sgjelinek 
66886cfd72c6Sgjelinek 				if ((obs = (zone_pkg_entry_t *)malloc(
66896cfd72c6Sgjelinek 				    sizeof (zone_pkg_entry_t))) == NULL) {
66906cfd72c6Sgjelinek 					res = Z_NOMEM;
66916cfd72c6Sgjelinek 					goto done;
66926cfd72c6Sgjelinek 				}
66936cfd72c6Sgjelinek 
66946cfd72c6Sgjelinek 				if ((obs->zpe_name = strdup(name)) == NULL) {
66956cfd72c6Sgjelinek 					free(obs);
66966cfd72c6Sgjelinek 					res = Z_NOMEM;
66976cfd72c6Sgjelinek 					goto done;
66986cfd72c6Sgjelinek 				}
66996cfd72c6Sgjelinek 				/*
67006cfd72c6Sgjelinek 				 * The version doesn't matter for obsoleted
67016cfd72c6Sgjelinek 				 * patches.
67026cfd72c6Sgjelinek 				 */
67036cfd72c6Sgjelinek 				obs->zpe_vers = NULL;
67046cfd72c6Sgjelinek 				obs->zpe_patches_avl = NULL;
67056cfd72c6Sgjelinek 
67066cfd72c6Sgjelinek 				/*
67076cfd72c6Sgjelinek 				 * If this is the first obsolete patch, add an
67086cfd72c6Sgjelinek 				 * AVL tree to the parent patch element.
67096cfd72c6Sgjelinek 				 */
67106cfd72c6Sgjelinek 				if (patch->zpe_patches_avl == NULL) {
67116cfd72c6Sgjelinek 					patch->zpe_patches_avl =
67126cfd72c6Sgjelinek 					    uu_avl_create(pkg_pool, NULL,
67136cfd72c6Sgjelinek 					    UU_DEFAULT);
67146cfd72c6Sgjelinek 					if (patch->zpe_patches_avl == NULL) {
67156cfd72c6Sgjelinek 						free(obs->zpe_name);
67166cfd72c6Sgjelinek 						free(obs);
67176cfd72c6Sgjelinek 						res = Z_NOMEM;
67186cfd72c6Sgjelinek 						goto done;
67196cfd72c6Sgjelinek 					}
67206cfd72c6Sgjelinek 				}
67216cfd72c6Sgjelinek 
67226cfd72c6Sgjelinek 				/* Insert obsolete patch into the AVL tree. */
67236cfd72c6Sgjelinek 				uu_avl_node_init(obs, &obs->zpe_entry,
67246cfd72c6Sgjelinek 				    pkg_pool);
67256cfd72c6Sgjelinek 				if (uu_avl_find(patch->zpe_patches_avl, obs,
67266cfd72c6Sgjelinek 				    NULL, &where) != NULL) {
67276cfd72c6Sgjelinek 					free(obs->zpe_name);
67286cfd72c6Sgjelinek 					free(obs);
67296cfd72c6Sgjelinek 				} else {
67306cfd72c6Sgjelinek 					uu_avl_insert(patch->zpe_patches_avl,
67316cfd72c6Sgjelinek 					    obs, where);
67326cfd72c6Sgjelinek 				}
67336cfd72c6Sgjelinek 			}
67346cfd72c6Sgjelinek 		}
67356cfd72c6Sgjelinek 	}
67366cfd72c6Sgjelinek 
67376cfd72c6Sgjelinek done:
67386cfd72c6Sgjelinek 	(void) zonecfg_endent(handle);
67396cfd72c6Sgjelinek 	return (res);
6740ee519a1fSgjelinek }
6741ee519a1fSgjelinek 
6742ee519a1fSgjelinek int
6743ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
6744ee519a1fSgjelinek {
6745ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6746ee519a1fSgjelinek }
6747ee519a1fSgjelinek 
6748ee519a1fSgjelinek int
6749ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
6750ee519a1fSgjelinek {
6751ee519a1fSgjelinek 	xmlNodePtr cur;
6752ee519a1fSgjelinek 	int err;
6753ee519a1fSgjelinek 	char buf[128];
6754ee519a1fSgjelinek 
6755ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
6756ee519a1fSgjelinek 
6757ee519a1fSgjelinek 	if (handle == NULL)
6758ee519a1fSgjelinek 		return (Z_INVAL);
6759ee519a1fSgjelinek 
6760ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6761ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6762ee519a1fSgjelinek 
6763ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6764ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
6765ee519a1fSgjelinek 			break;
6766ee519a1fSgjelinek 	if (cur == NULL) {
6767ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6768ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6769ee519a1fSgjelinek 	}
6770ee519a1fSgjelinek 
6771ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
6772ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
6773ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6774ee519a1fSgjelinek 		return (err);
6775ee519a1fSgjelinek 	}
6776ee519a1fSgjelinek 
6777ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
6778ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6779ee519a1fSgjelinek 		return (err);
6780ee519a1fSgjelinek 	}
6781ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
6782ee519a1fSgjelinek 
6783ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
6784ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6785ee519a1fSgjelinek 		return (err);
6786ee519a1fSgjelinek 	}
6787ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
6788ee519a1fSgjelinek 
6789ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
6790ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6791ee519a1fSgjelinek 		return (err);
6792ee519a1fSgjelinek 	}
6793ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
6794ee519a1fSgjelinek 
6795ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
6796ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
6797ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6798ee519a1fSgjelinek 		return (err);
6799ee519a1fSgjelinek 	}
6800ee519a1fSgjelinek 
6801ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6802ee519a1fSgjelinek 	return (Z_OK);
6803ee519a1fSgjelinek }
6804ee519a1fSgjelinek 
6805ee519a1fSgjelinek int
6806ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
6807ee519a1fSgjelinek {
6808ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6809ee519a1fSgjelinek }
6810ee519a1fSgjelinek 
6811ee519a1fSgjelinek /*
681239935be5Sgjelinek  * Maintain a space separated list of unique pkg names.  PATH_MAX is used in
681339935be5Sgjelinek  * the pkg code as the maximum size for a pkg name.
681439935be5Sgjelinek  */
681539935be5Sgjelinek static int
681639935be5Sgjelinek add_pkg_to_str(char **str, char *pkg)
681739935be5Sgjelinek {
681839935be5Sgjelinek 	int len, newlen;
681939935be5Sgjelinek 	char tstr[PATH_MAX + 3];
682039935be5Sgjelinek 	char *tmp;
682139935be5Sgjelinek 
682239935be5Sgjelinek 	len = strlen(pkg);
682339935be5Sgjelinek 	if (*str == NULL) {
682439935be5Sgjelinek 		/* space for str + 2 spaces + NULL */
682539935be5Sgjelinek 		if ((*str = (char *)malloc(len + 3)) == NULL)
682639935be5Sgjelinek 			return (Z_NOMEM);
682739935be5Sgjelinek 		(void) snprintf(*str, len + 3, " %s ", pkg);
682839935be5Sgjelinek 		return (Z_OK);
682939935be5Sgjelinek 	}
683039935be5Sgjelinek 
683139935be5Sgjelinek 	(void) snprintf(tstr, sizeof (tstr), " %s ", pkg);
683239935be5Sgjelinek 	if (strstr(*str, tstr) != NULL)
683339935be5Sgjelinek 		return (Z_OK);
683439935be5Sgjelinek 
683539935be5Sgjelinek 	/* space for str + 1 space + NULL */
683639935be5Sgjelinek 	newlen = strlen(*str) + len + 2;
683739935be5Sgjelinek 	if ((tmp = (char *)realloc(*str, newlen)) == NULL)
683839935be5Sgjelinek 		return (Z_NOMEM);
683939935be5Sgjelinek 	*str = tmp;
684039935be5Sgjelinek 	(void) strlcat(*str, pkg, newlen);
684139935be5Sgjelinek 	(void) strlcat(*str, " ", newlen);
684239935be5Sgjelinek 	return (Z_OK);
684339935be5Sgjelinek }
684439935be5Sgjelinek 
684539935be5Sgjelinek /*
6846ee519a1fSgjelinek  * Process a list of pkgs from an entry in the contents file, adding each pkg
6847ee519a1fSgjelinek  * name to the list of pkgs.
6848ee519a1fSgjelinek  *
6849ee519a1fSgjelinek  * It is possible for the pkg name to be preceeded by a special character
6850ee519a1fSgjelinek  * which indicates some bookkeeping information for pkging.  Check if the
6851ee519a1fSgjelinek  * first char is not an Alpha char.  If so, skip over it.
6852ee519a1fSgjelinek  */
6853ee519a1fSgjelinek static int
685439935be5Sgjelinek add_pkg_list(char *lastp, char ***plist, int *pcnt, char **pkg_warn)
6855ee519a1fSgjelinek {
6856ee519a1fSgjelinek 	char	*p;
6857ee519a1fSgjelinek 	int	pkg_cnt = *pcnt;
6858ee519a1fSgjelinek 	char	**pkgs = *plist;
6859ee519a1fSgjelinek 	int	res = Z_OK;
6860ee519a1fSgjelinek 
6861ee519a1fSgjelinek 	while ((p = strtok_r(NULL, " ", &lastp)) != NULL) {
6862ee519a1fSgjelinek 		char	**tmpp;
6863ee519a1fSgjelinek 		int	i;
6864ee519a1fSgjelinek 
6865ee519a1fSgjelinek 		/* skip over any special pkg bookkeeping char */
686639935be5Sgjelinek 		if (!isalpha(*p)) {
6867ee519a1fSgjelinek 			p++;
686839935be5Sgjelinek 			if ((res = add_pkg_to_str(pkg_warn, p)) != Z_OK)
686939935be5Sgjelinek 				break;
687039935be5Sgjelinek 		}
6871ee519a1fSgjelinek 
6872ee519a1fSgjelinek 		/* Check if the pkg is already in the list */
6873ee519a1fSgjelinek 		for (i = 0; i < pkg_cnt; i++) {
6874ee519a1fSgjelinek 			if (strcmp(p, pkgs[i]) == 0)
6875ee519a1fSgjelinek 				break;
6876ee519a1fSgjelinek 		}
6877ee519a1fSgjelinek 
6878ee519a1fSgjelinek 		if (i < pkg_cnt)
6879ee519a1fSgjelinek 			continue;
6880ee519a1fSgjelinek 
6881ee519a1fSgjelinek 		/* The pkg is not in the list; add it. */
6882ee519a1fSgjelinek 		if ((tmpp = (char **)realloc(pkgs,
6883ee519a1fSgjelinek 		    sizeof (char *) * (pkg_cnt + 1))) == NULL) {
6884ee519a1fSgjelinek 			res = Z_NOMEM;
6885ee519a1fSgjelinek 			break;
6886ee519a1fSgjelinek 		}
6887ee519a1fSgjelinek 		pkgs = tmpp;
6888ee519a1fSgjelinek 
6889ee519a1fSgjelinek 		if ((pkgs[pkg_cnt] = strdup(p)) == NULL) {
6890ee519a1fSgjelinek 			res = Z_NOMEM;
6891ee519a1fSgjelinek 			break;
6892ee519a1fSgjelinek 		}
6893ee519a1fSgjelinek 		pkg_cnt++;
6894ee519a1fSgjelinek 	}
6895ee519a1fSgjelinek 
6896ee519a1fSgjelinek 	*plist = pkgs;
6897ee519a1fSgjelinek 	*pcnt = pkg_cnt;
6898ee519a1fSgjelinek 
6899ee519a1fSgjelinek 	return (res);
6900ee519a1fSgjelinek }
6901ee519a1fSgjelinek 
6902ee519a1fSgjelinek /*
690339935be5Sgjelinek  * Process an entry from the contents file (type "directory").  If the
690439935be5Sgjelinek  * directory path is in the list of ipds and is not under a lofs mount within
690539935be5Sgjelinek  * the ipd then add the associated list of pkgs to the pkg list.  The input
690639935be5Sgjelinek  * parameter "entry" will be broken up by the parser within this function so
690739935be5Sgjelinek  * its value will be modified when this function exits.
6908ee519a1fSgjelinek  *
6909ee519a1fSgjelinek  * The entries we are looking for will look something like:
6910ee519a1fSgjelinek  *	/usr d none 0755 root sys SUNWctpls SUNWidnl SUNWlibCf ....
6911ee519a1fSgjelinek  */
6912ee519a1fSgjelinek static int
691339935be5Sgjelinek get_path_pkgs(char *entry, char **ipds, char **fss, char ***pkgs, int *pkg_cnt,
691439935be5Sgjelinek     char **pkg_warn)
6915ee519a1fSgjelinek {
6916ee519a1fSgjelinek 	char	*f1;
6917ee519a1fSgjelinek 	char	*f2;
6918ee519a1fSgjelinek 	char	*lastp;
6919ee519a1fSgjelinek 	int	i;
692039935be5Sgjelinek 	char	*nlp;
6921ee519a1fSgjelinek 
6922ee519a1fSgjelinek 	if ((f1 = strtok_r(entry, " ", &lastp)) == NULL ||
6923ee519a1fSgjelinek 	    (f2 = strtok_r(NULL, " ", &lastp)) == NULL || strcmp(f2, "d") != 0)
6924ee519a1fSgjelinek 		return (Z_OK);
6925ee519a1fSgjelinek 
692639935be5Sgjelinek 	/* Check if this directory entry is in the list of ipds. */
692739935be5Sgjelinek 	for (i = 0; ipds[i] != NULL; i++) {
692839935be5Sgjelinek 		char wildcard[MAXPATHLEN];
6929ee519a1fSgjelinek 
6930ee519a1fSgjelinek 		/*
693139935be5Sgjelinek 		 * We want to match on the path and any other directory
693239935be5Sgjelinek 		 * entries under this path.  When we use FNM_PATHNAME then
693339935be5Sgjelinek 		 * that means '/' will not be matched by a wildcard (*) so
693439935be5Sgjelinek 		 * we omit FNM_PATHNAME on the call with the wildcard matching.
6935ee519a1fSgjelinek 		 */
693639935be5Sgjelinek 		(void) snprintf(wildcard, sizeof (wildcard), "%s/*", ipds[i]);
693739935be5Sgjelinek 		if (fnmatch(ipds[i], f1, FNM_PATHNAME) == 0 ||
693839935be5Sgjelinek 		    fnmatch(wildcard, f1, 0) == 0) {
693939935be5Sgjelinek 			/* It looks like we do want the pkgs for this path. */
694039935be5Sgjelinek 			break;
694139935be5Sgjelinek 		}
694239935be5Sgjelinek 	}
694339935be5Sgjelinek 
694439935be5Sgjelinek 	/* This entry did not match any of the ipds. */
694539935be5Sgjelinek 	if (ipds[i] == NULL)
694639935be5Sgjelinek 		return (Z_OK);
694739935be5Sgjelinek 
694839935be5Sgjelinek 	/*
694939935be5Sgjelinek 	 * Check if there is a fs mounted under the ipd.  If so, ignore this
695039935be5Sgjelinek 	 * entry.
695139935be5Sgjelinek 	 */
695239935be5Sgjelinek 	for (i = 0; fss[i] != NULL; i++) {
695339935be5Sgjelinek 		char wildcard[MAXPATHLEN];
695439935be5Sgjelinek 
695539935be5Sgjelinek 		(void) snprintf(wildcard, sizeof (wildcard), "%s/*", fss[i]);
695639935be5Sgjelinek 		if (fnmatch(fss[i], f1, FNM_PATHNAME) == 0 ||
695739935be5Sgjelinek 		    fnmatch(wildcard, f1, 0) == 0) {
695839935be5Sgjelinek 			/* We should ignore this path. */
695939935be5Sgjelinek 			break;
696039935be5Sgjelinek 		}
696139935be5Sgjelinek 	}
696239935be5Sgjelinek 
696339935be5Sgjelinek 	/* If not null, then we matched an fs mount point so ignore entry. */
696439935be5Sgjelinek 	if (fss[i] != NULL)
696539935be5Sgjelinek 		return (Z_OK);
696639935be5Sgjelinek 
696739935be5Sgjelinek 	/*
696839935be5Sgjelinek 	 * We do want the pkgs for this entry.  First, skip over the next 4
696939935be5Sgjelinek 	 * fields in the entry so that we call add_pkg_list starting with the
697039935be5Sgjelinek 	 * pkg names.
697139935be5Sgjelinek 	 */
697239935be5Sgjelinek 	for (i = 0; i < 4 && strtok_r(NULL, " ", &lastp) != NULL; i++)
697339935be5Sgjelinek 		;
697439935be5Sgjelinek 	/* If there are < 4 fields this entry is corrupt, just skip it. */
697539935be5Sgjelinek 	if (i < 4)
6976ee519a1fSgjelinek 		return (Z_OK);
6977ee519a1fSgjelinek 
697807b574eeSgjelinek 	/* strip newline from the line */
697907b574eeSgjelinek 	nlp = (lastp + strlen(lastp) - 1);
698007b574eeSgjelinek 	if (*nlp == '\n')
698107b574eeSgjelinek 		*nlp = '\0';
698207b574eeSgjelinek 
698339935be5Sgjelinek 	return (add_pkg_list(lastp, pkgs, pkg_cnt, pkg_warn));
6984ee519a1fSgjelinek }
6985ee519a1fSgjelinek 
6986ee519a1fSgjelinek /*
6987ee519a1fSgjelinek  * Read an entry from a pkginfo or contents file.  Some of these lines can
6988ee519a1fSgjelinek  * either be arbitrarily long or be continued by a backslash at the end of
6989ee519a1fSgjelinek  * the line.  This function coalesces lines that are longer than the read
6990ee519a1fSgjelinek  * buffer, and lines that are continued, into one buffer which is returned.
6991ee519a1fSgjelinek  * The caller must free this memory.  NULL is returned when we hit EOF or
6992ee519a1fSgjelinek  * if we run out of memory (errno is set to ENOMEM).
6993ee519a1fSgjelinek  */
6994ee519a1fSgjelinek static char *
6995ee519a1fSgjelinek read_pkg_data(FILE *fp)
6996ee519a1fSgjelinek {
6997ee519a1fSgjelinek 	char *start;
6998ee519a1fSgjelinek 	char *inp;
6999ee519a1fSgjelinek 	char *p;
7000ee519a1fSgjelinek 	int char_cnt = 0;
7001ee519a1fSgjelinek 
7002ee519a1fSgjelinek 	errno = 0;
7003ee519a1fSgjelinek 	if ((start = (char *)malloc(PKGINFO_RD_LEN)) == NULL) {
7004ee519a1fSgjelinek 		errno = ENOMEM;
7005ee519a1fSgjelinek 		return (NULL);
7006ee519a1fSgjelinek 	}
7007ee519a1fSgjelinek 
7008ee519a1fSgjelinek 	inp = start;
7009ee519a1fSgjelinek 	while ((p = fgets(inp, PKGINFO_RD_LEN, fp)) != NULL) {
7010ee519a1fSgjelinek 		int len;
7011ee519a1fSgjelinek 
7012ee519a1fSgjelinek 		len = strlen(inp);
7013ee519a1fSgjelinek 		if (inp[len - 1] == '\n' &&
7014ee519a1fSgjelinek 		    (len == 1 || inp[len - 2] != '\\')) {
7015ee519a1fSgjelinek 			char_cnt = len;
7016ee519a1fSgjelinek 			break;
7017ee519a1fSgjelinek 		}
7018ee519a1fSgjelinek 
7019ee519a1fSgjelinek 		if (inp[len - 2] == '\\')
7020ee519a1fSgjelinek 			char_cnt += len - 2;
7021ee519a1fSgjelinek 		else
7022ee519a1fSgjelinek 			char_cnt += PKGINFO_RD_LEN - 1;
7023ee519a1fSgjelinek 
7024ee519a1fSgjelinek 		if ((p = realloc(start, char_cnt + PKGINFO_RD_LEN)) == NULL) {
7025ee519a1fSgjelinek 			errno = ENOMEM;
7026ee519a1fSgjelinek 			break;
7027ee519a1fSgjelinek 		}
7028ee519a1fSgjelinek 
7029ee519a1fSgjelinek 		start = p;
7030ee519a1fSgjelinek 		inp = start + char_cnt;
7031ee519a1fSgjelinek 	}
7032ee519a1fSgjelinek 
7033ee519a1fSgjelinek 	if (errno == ENOMEM || (p == NULL && char_cnt == 0)) {
7034ee519a1fSgjelinek 		free(start);
7035ee519a1fSgjelinek 		start = NULL;
7036ee519a1fSgjelinek 	}
7037ee519a1fSgjelinek 
7038ee519a1fSgjelinek 	return (start);
7039ee519a1fSgjelinek }
7040ee519a1fSgjelinek 
7041ee519a1fSgjelinek static void
7042ee519a1fSgjelinek free_ipd_pkgs(char **pkgs, int cnt)
7043ee519a1fSgjelinek {
7044ee519a1fSgjelinek 	int i;
7045ee519a1fSgjelinek 
7046ee519a1fSgjelinek 	for (i = 0; i < cnt; i++)
7047ee519a1fSgjelinek 		free(pkgs[i]);
7048ee519a1fSgjelinek 	free(pkgs);
7049ee519a1fSgjelinek }
7050ee519a1fSgjelinek 
7051ee519a1fSgjelinek /*
705239935be5Sgjelinek  * Get a list of the inherited pkg dirs or fs entries configured for the
705339935be5Sgjelinek  * zone.  The type parameter will be either ZONE_IPD or ZONE_FS.
705439935be5Sgjelinek  */
705539935be5Sgjelinek static int
705639935be5Sgjelinek get_ipd_fs_list(zone_dochandle_t handle, enum zn_ipd_fs type, char ***list)
705739935be5Sgjelinek {
705839935be5Sgjelinek 	int	res;
705939935be5Sgjelinek 	struct zone_fstab fstab;
706039935be5Sgjelinek 	int	cnt = 0;
706139935be5Sgjelinek 	char	**entries = NULL;
706239935be5Sgjelinek 	int	i;
706339935be5Sgjelinek 	int	(*fp)(zone_dochandle_t, struct zone_fstab *);
706439935be5Sgjelinek 
706539935be5Sgjelinek 	if (type == ZONE_IPD) {
706639935be5Sgjelinek 		fp = zonecfg_getipdent;
706739935be5Sgjelinek 		res = zonecfg_setipdent(handle);
706839935be5Sgjelinek 	} else {
706939935be5Sgjelinek 		fp = zonecfg_getfsent;
707039935be5Sgjelinek 		res = zonecfg_setfsent(handle);
707139935be5Sgjelinek 	}
707239935be5Sgjelinek 
707339935be5Sgjelinek 	if (res != Z_OK)
707439935be5Sgjelinek 		return (res);
707539935be5Sgjelinek 
707639935be5Sgjelinek 	while (fp(handle, &fstab) == Z_OK) {
707739935be5Sgjelinek 		char	**p;
707839935be5Sgjelinek 
707939935be5Sgjelinek 		if ((p = (char **)realloc(entries,
708039935be5Sgjelinek 		    sizeof (char *) * (cnt + 1))) == NULL) {
708139935be5Sgjelinek 			res = Z_NOMEM;
708239935be5Sgjelinek 			break;
708339935be5Sgjelinek 		}
708439935be5Sgjelinek 		entries = p;
708539935be5Sgjelinek 
708639935be5Sgjelinek 		if ((entries[cnt] = strdup(fstab.zone_fs_dir)) == NULL) {
708739935be5Sgjelinek 			res = Z_NOMEM;
708839935be5Sgjelinek 			break;
708939935be5Sgjelinek 		}
709039935be5Sgjelinek 
709139935be5Sgjelinek 		cnt++;
709239935be5Sgjelinek 	}
709339935be5Sgjelinek 
709439935be5Sgjelinek 	if (type == ZONE_IPD)
709539935be5Sgjelinek 		(void) zonecfg_endipdent(handle);
709639935be5Sgjelinek 	else
709739935be5Sgjelinek 		(void) zonecfg_endfsent(handle);
709839935be5Sgjelinek 
709939935be5Sgjelinek 	/* Add a NULL terminating element. */
710039935be5Sgjelinek 	if (res == Z_OK) {
710139935be5Sgjelinek 		char	**p;
710239935be5Sgjelinek 
710339935be5Sgjelinek 		if ((p = (char **)realloc(entries,
710439935be5Sgjelinek 		    sizeof (char *) * (cnt + 1))) == NULL) {
710539935be5Sgjelinek 			res = Z_NOMEM;
710639935be5Sgjelinek 		} else {
710739935be5Sgjelinek 			entries = p;
710839935be5Sgjelinek 			entries[cnt] = NULL;
710939935be5Sgjelinek 		}
711039935be5Sgjelinek 	}
711139935be5Sgjelinek 
711239935be5Sgjelinek 	if (res != Z_OK) {
711339935be5Sgjelinek 		if (entries != NULL) {
711439935be5Sgjelinek 			for (i = 0; i < cnt; i++)
711539935be5Sgjelinek 				free(entries[i]);
711639935be5Sgjelinek 			free(entries);
711739935be5Sgjelinek 		}
711839935be5Sgjelinek 		return (res);
711939935be5Sgjelinek 	}
712039935be5Sgjelinek 
712139935be5Sgjelinek 	*list = entries;
712239935be5Sgjelinek 	return (Z_OK);
712339935be5Sgjelinek }
712439935be5Sgjelinek 
712539935be5Sgjelinek /*
7126ee519a1fSgjelinek  * Get the list of inherited-pkg-dirs (ipd) for the zone and then get the
7127ee519a1fSgjelinek  * list of pkgs that deliver into those dirs.
7128ee519a1fSgjelinek  */
7129ee519a1fSgjelinek static int
7130ee519a1fSgjelinek get_ipd_pkgs(zone_dochandle_t handle, char ***pkg_list, int *cnt)
7131ee519a1fSgjelinek {
7132ee519a1fSgjelinek 	int	res;
713339935be5Sgjelinek 	char	**ipds;
713439935be5Sgjelinek 	char	**fss;
7135ee519a1fSgjelinek 	int	pkg_cnt = 0;
7136ee519a1fSgjelinek 	char	**pkgs = NULL;
7137ee519a1fSgjelinek 	int	i;
7138ee519a1fSgjelinek 
713939935be5Sgjelinek 	if ((res = get_ipd_fs_list(handle, ZONE_IPD, &ipds)) != Z_OK)
7140ee519a1fSgjelinek 		return (res);
7141ee519a1fSgjelinek 
714239935be5Sgjelinek 	if ((res = get_ipd_fs_list(handle, ZONE_FS, &fss)) != Z_OK) {
714339935be5Sgjelinek 		for (i = 0; ipds[i] != NULL; i++)
7144ee519a1fSgjelinek 			free(ipds[i]);
7145ee519a1fSgjelinek 		free(ipds);
7146ee519a1fSgjelinek 		return (res);
7147ee519a1fSgjelinek 	}
7148ee519a1fSgjelinek 
7149ee519a1fSgjelinek 	/* We only have to process the contents file if we have ipds. */
715039935be5Sgjelinek 	if (ipds != NULL) {
7151ee519a1fSgjelinek 		FILE	*fp;
7152ee519a1fSgjelinek 
7153ee519a1fSgjelinek 		if ((fp = fopen(CONTENTS_FILE, "r")) != NULL) {
7154ee519a1fSgjelinek 			char	*buf;
715539935be5Sgjelinek 			char	*pkg_warn = NULL;
7156ee519a1fSgjelinek 
7157ee519a1fSgjelinek 			while ((buf = read_pkg_data(fp)) != NULL) {
715839935be5Sgjelinek 				res = get_path_pkgs(buf, ipds, fss, &pkgs,
715939935be5Sgjelinek 				    &pkg_cnt, &pkg_warn);
7160ee519a1fSgjelinek 				free(buf);
7161ee519a1fSgjelinek 				if (res != Z_OK)
7162ee519a1fSgjelinek 					break;
7163ee519a1fSgjelinek 			}
7164ee519a1fSgjelinek 
7165ee519a1fSgjelinek 			(void) fclose(fp);
716639935be5Sgjelinek 
716739935be5Sgjelinek 			if (pkg_warn != NULL) {
716839935be5Sgjelinek 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
716939935be5Sgjelinek 				    "WARNING: package operation in progress "
717039935be5Sgjelinek 				    "on the following packages:\n   %s\n"),
717139935be5Sgjelinek 				    pkg_warn);
717239935be5Sgjelinek 				free(pkg_warn);
717339935be5Sgjelinek 			}
7174ee519a1fSgjelinek 		}
7175ee519a1fSgjelinek 	}
7176ee519a1fSgjelinek 
717739935be5Sgjelinek 	for (i = 0; ipds[i] != NULL; i++)
7178ee519a1fSgjelinek 		free(ipds[i]);
7179ee519a1fSgjelinek 	free(ipds);
7180ee519a1fSgjelinek 
718139935be5Sgjelinek 	for (i = 0; fss[i] != NULL; i++)
718239935be5Sgjelinek 		free(fss[i]);
718339935be5Sgjelinek 	free(fss);
718439935be5Sgjelinek 
7185ee519a1fSgjelinek 	if (res != Z_OK) {
7186ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
7187ee519a1fSgjelinek 	} else {
7188ee519a1fSgjelinek 		*pkg_list = pkgs;
7189ee519a1fSgjelinek 		*cnt = pkg_cnt;
7190ee519a1fSgjelinek 	}
7191ee519a1fSgjelinek 
7192ee519a1fSgjelinek 	return (res);
7193ee519a1fSgjelinek }
7194ee519a1fSgjelinek 
7195ee519a1fSgjelinek /*
7196ee519a1fSgjelinek  * Return true if pkg_name is in the list of pkgs that deliver into an
7197ee519a1fSgjelinek  * inherited pkg directory for the zone.
7198ee519a1fSgjelinek  */
7199ee519a1fSgjelinek static boolean_t
7200ee519a1fSgjelinek dir_pkg(char *pkg_name, char **pkg_list, int cnt)
7201ee519a1fSgjelinek {
7202ee519a1fSgjelinek 	int i;
7203ee519a1fSgjelinek 
7204ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
7205ee519a1fSgjelinek 		if (strcmp(pkg_name, pkg_list[i]) == 0)
7206ee519a1fSgjelinek 			return (B_TRUE);
7207ee519a1fSgjelinek 	}
7208ee519a1fSgjelinek 
7209ee519a1fSgjelinek 	return (B_FALSE);
7210ee519a1fSgjelinek }
7211ee519a1fSgjelinek 
7212ee519a1fSgjelinek /*
72136cfd72c6Sgjelinek  * Keep track of obsoleted patches for this specific patch.  We don't need to
72146cfd72c6Sgjelinek  * keep track of the patch version since once a patch is obsoleted, all prior
72156cfd72c6Sgjelinek  * versions are also obsolete and there won't be any new versions.
72166cfd72c6Sgjelinek  */
72176cfd72c6Sgjelinek static int
72186cfd72c6Sgjelinek add_obs_patch(patch_node_t *patch, char *num, uu_list_pool_t *patches_pool)
72196cfd72c6Sgjelinek {
72206cfd72c6Sgjelinek 	obs_patch_node_t *obs;
72216cfd72c6Sgjelinek 
72226cfd72c6Sgjelinek 	if (patch->obs_patches == NULL) {
72236cfd72c6Sgjelinek 		if ((patch->obs_patches = uu_list_create(patches_pool, NULL,
72246cfd72c6Sgjelinek 		    0)) == NULL)
72256cfd72c6Sgjelinek 			return (Z_NOMEM);
72266cfd72c6Sgjelinek 	}
72276cfd72c6Sgjelinek 
72286cfd72c6Sgjelinek 	if ((obs = (obs_patch_node_t *)malloc(sizeof (obs_patch_node_t)))
72296cfd72c6Sgjelinek 	    == NULL)
72306cfd72c6Sgjelinek 		return (Z_NOMEM);
72316cfd72c6Sgjelinek 
72326cfd72c6Sgjelinek 	if ((obs->patch_num = strdup(num)) == NULL) {
72336cfd72c6Sgjelinek 		free(obs);
72346cfd72c6Sgjelinek 		return (Z_NOMEM);
72356cfd72c6Sgjelinek 	}
72366cfd72c6Sgjelinek 
72376cfd72c6Sgjelinek 	uu_list_node_init(obs, &obs->link, patches_pool);
72386cfd72c6Sgjelinek 	(void) uu_list_insert_before(patch->obs_patches, NULL, obs);
72396cfd72c6Sgjelinek 
72406cfd72c6Sgjelinek 	return (Z_OK);
72416cfd72c6Sgjelinek }
72426cfd72c6Sgjelinek 
72436cfd72c6Sgjelinek /*
724439935be5Sgjelinek  * Keep track of obsoleted patches.  We don't need to keep track of the patch
724539935be5Sgjelinek  * version since once a patch is obsoleted, all prior versions are also
724639935be5Sgjelinek  * obsolete and there won't be any new versions.
7247ee519a1fSgjelinek  */
7248ee519a1fSgjelinek static int
72496cfd72c6Sgjelinek save_obs_patch(char *num, uu_avl_pool_t *patches_pool, uu_avl_t *obs_patches)
7250ee519a1fSgjelinek {
725139935be5Sgjelinek 	patch_node_t	*patch;
725239935be5Sgjelinek 	uu_avl_index_t where;
725339935be5Sgjelinek 
725439935be5Sgjelinek 	if ((patch = (patch_node_t *)malloc(sizeof (patch_node_t))) == NULL)
725539935be5Sgjelinek 		return (Z_NOMEM);
725639935be5Sgjelinek 
725739935be5Sgjelinek 	if ((patch->patch_num = strdup(num)) == NULL) {
725839935be5Sgjelinek 		free(patch);
725939935be5Sgjelinek 		return (Z_NOMEM);
726039935be5Sgjelinek 	}
726139935be5Sgjelinek 
726239935be5Sgjelinek 	patch->patch_vers = NULL;
72636cfd72c6Sgjelinek 	patch->obs_patches = NULL;
72646cfd72c6Sgjelinek 
726539935be5Sgjelinek 	uu_avl_node_init(patch, &patch->patch_node, patches_pool);
726639935be5Sgjelinek 
72676cfd72c6Sgjelinek 	if (uu_avl_find(obs_patches, patch, NULL, &where) != NULL) {
726839935be5Sgjelinek 		free(patch->patch_num);
726939935be5Sgjelinek 		free(patch);
727039935be5Sgjelinek 		return (Z_OK);
727139935be5Sgjelinek 	}
727239935be5Sgjelinek 
72736cfd72c6Sgjelinek 	uu_avl_insert(obs_patches, patch, where);
727439935be5Sgjelinek 	return (Z_OK);
727539935be5Sgjelinek }
727639935be5Sgjelinek 
727739935be5Sgjelinek /*
727839935be5Sgjelinek  * Keep a list of patches for a pkg.  If we see a newer version of a patch,
727939935be5Sgjelinek  * we only keep track of the newer version.
728039935be5Sgjelinek  */
728139935be5Sgjelinek static void
728239935be5Sgjelinek save_patch(patch_node_t *patch, uu_avl_t *patches_avl)
728339935be5Sgjelinek {
728439935be5Sgjelinek 	patch_node_t *existing;
728539935be5Sgjelinek 	uu_avl_index_t where;
728639935be5Sgjelinek 
728739935be5Sgjelinek 	/* Check if this is a newer version of a patch we already have. */
728839935be5Sgjelinek 	if ((existing = (patch_node_t *)uu_avl_find(patches_avl, patch, NULL,
728939935be5Sgjelinek 	    &where)) != NULL) {
729039935be5Sgjelinek 		char *endptr;
729139935be5Sgjelinek 		ulong_t pvers, evers;
729239935be5Sgjelinek 
729339935be5Sgjelinek 		pvers = strtoul(patch->patch_vers, &endptr, 10);
729439935be5Sgjelinek 		evers = strtoul(existing->patch_vers, &endptr, 10);
729539935be5Sgjelinek 
729639935be5Sgjelinek 		if (pvers > evers) {
729739935be5Sgjelinek 			free(existing->patch_vers);
729839935be5Sgjelinek 			existing->patch_vers = patch->patch_vers;
729939935be5Sgjelinek 			free(patch->patch_num);
730039935be5Sgjelinek 			free(patch);
730139935be5Sgjelinek 			return;
730239935be5Sgjelinek 		}
730339935be5Sgjelinek 	}
730439935be5Sgjelinek 
730539935be5Sgjelinek 	uu_avl_insert(patches_avl, patch, where);
730639935be5Sgjelinek }
730739935be5Sgjelinek 
730839935be5Sgjelinek /*
730939935be5Sgjelinek  * Check if a patch is on the list of obsoleted patches.  We don't need to
731039935be5Sgjelinek  * check the patch version since once a patch is obsoleted, all prior versions
731139935be5Sgjelinek  * are also obsolete and there won't be any new versions.
731239935be5Sgjelinek  */
731339935be5Sgjelinek static boolean_t
73146cfd72c6Sgjelinek obsolete_patch(patch_node_t *patch, uu_avl_t *obs_patches)
731539935be5Sgjelinek {
731639935be5Sgjelinek 	uu_avl_index_t	where;
731739935be5Sgjelinek 
73186cfd72c6Sgjelinek 	if (uu_avl_find(obs_patches, patch, NULL, &where) != NULL)
731939935be5Sgjelinek 		return (B_TRUE);
732039935be5Sgjelinek 
732139935be5Sgjelinek 	return (B_FALSE);
732239935be5Sgjelinek }
732339935be5Sgjelinek 
732439935be5Sgjelinek /* ARGSUSED */
732539935be5Sgjelinek static int
732639935be5Sgjelinek patch_node_compare(const void *l_arg, const void *r_arg, void *private)
732739935be5Sgjelinek {
732839935be5Sgjelinek 	patch_node_t *l = (patch_node_t *)l_arg;
732939935be5Sgjelinek 	patch_node_t *r = (patch_node_t *)r_arg;
733039935be5Sgjelinek 	char *endptr;
733139935be5Sgjelinek 	ulong_t lnum, rnum;
733239935be5Sgjelinek 
733339935be5Sgjelinek 	lnum = strtoul(l->patch_num, &endptr, 10);
733439935be5Sgjelinek 	rnum = strtoul(r->patch_num, &endptr, 10);
733539935be5Sgjelinek 
733639935be5Sgjelinek 	if (lnum > rnum)
733739935be5Sgjelinek 		return (1);
733839935be5Sgjelinek 	if (lnum < rnum)
733939935be5Sgjelinek 		return (-1);
734039935be5Sgjelinek 	return (0);
734139935be5Sgjelinek }
734239935be5Sgjelinek 
734339935be5Sgjelinek /*
734439935be5Sgjelinek  * Parse the patchinfo string for the patch.
734539935be5Sgjelinek  *
734639935be5Sgjelinek  * We are parsing entries of the form:
734739935be5Sgjelinek  * PATCH_INFO_121454-02=Installed: Wed Dec  7 07:13:51 PST 2005 From: mum \
734839935be5Sgjelinek  *	Obsoletes: 120777-03 121087-02 119108-07 Requires: 119575-02 \
734939935be5Sgjelinek  *	119255-06 Incompatibles:
735039935be5Sgjelinek  *
735139935be5Sgjelinek  * A backed out patch will have "backed out\n" as the status.  We should
73526cfd72c6Sgjelinek  * skip these patches.  We also ignore any entries that seem to be
73536cfd72c6Sgjelinek  * corrupted.  Obsolete patches are saved in the obs_patches parameter
73546cfd72c6Sgjelinek  * AVL list.
735539935be5Sgjelinek  */
735639935be5Sgjelinek static int
735739935be5Sgjelinek parse_info(char *patchinfo, uu_avl_pool_t *patches_pool, uu_avl_t *patches_avl,
73586cfd72c6Sgjelinek     uu_avl_t *obs_patches, uu_list_pool_t *list_pool)
735939935be5Sgjelinek {
7360ee519a1fSgjelinek 	char		*p;
7361ee519a1fSgjelinek 	char		*lastp;
736239935be5Sgjelinek 	char		*ep;
736339935be5Sgjelinek 	char		*pvers;
7364ee519a1fSgjelinek 	boolean_t	add_info = B_FALSE;
736539935be5Sgjelinek 	patch_node_t	*patch;
7366ee519a1fSgjelinek 
736739935be5Sgjelinek 	if (strlen(patchinfo) < (sizeof (PATCHINFO) - 1))
736807b574eeSgjelinek 		return (Z_OK);
736907b574eeSgjelinek 
737039935be5Sgjelinek 	/* Skip over "PATCH_INFO_" to get the patch id. */
737139935be5Sgjelinek 	p = patchinfo + sizeof (PATCHINFO) - 1;
737239935be5Sgjelinek 	if ((ep = strchr(p, '=')) == NULL)
737339935be5Sgjelinek 		return (Z_OK);
7374ee519a1fSgjelinek 
737539935be5Sgjelinek 	*ep++ = '\0';
737639935be5Sgjelinek 
737739935be5Sgjelinek 	/* Ignore all but installed patches. */
737839935be5Sgjelinek 	if (strncmp(ep, "Installed:", 10) != 0)
737939935be5Sgjelinek 		return (Z_OK);
738039935be5Sgjelinek 
738139935be5Sgjelinek 	/* remove newline */
738239935be5Sgjelinek 	lastp = (ep + strlen(ep) - 1);
738339935be5Sgjelinek 	if (*lastp == '\n')
738439935be5Sgjelinek 		*lastp = '\0';
738539935be5Sgjelinek 
738639935be5Sgjelinek 	if ((patch = (patch_node_t *)malloc(sizeof (patch_node_t))) == NULL)
738739935be5Sgjelinek 		return (Z_NOMEM);
738839935be5Sgjelinek 
738939935be5Sgjelinek 	if ((pvers = strchr(p, '-')) != NULL)
739039935be5Sgjelinek 		*pvers++ = '\0';
739139935be5Sgjelinek 	else
739239935be5Sgjelinek 		pvers = "";
739339935be5Sgjelinek 
739439935be5Sgjelinek 	if ((patch->patch_num = strdup(p)) == NULL) {
739539935be5Sgjelinek 		free(patch);
739639935be5Sgjelinek 		return (Z_NOMEM);
739739935be5Sgjelinek 	}
739839935be5Sgjelinek 	if ((patch->patch_vers = strdup(pvers)) == NULL) {
739939935be5Sgjelinek 		free(patch->patch_num);
740039935be5Sgjelinek 		free(patch);
740139935be5Sgjelinek 		return (Z_NOMEM);
740239935be5Sgjelinek 	}
74036cfd72c6Sgjelinek 	patch->obs_patches = NULL;
740439935be5Sgjelinek 
740539935be5Sgjelinek 	uu_avl_node_init(patch, &patch->patch_node, patches_pool);
740639935be5Sgjelinek 	save_patch(patch, patches_avl);
7407ee519a1fSgjelinek 
7408ee519a1fSgjelinek 	/*
7409ee519a1fSgjelinek 	 * Start with the first token.  This will probably be "Installed:".
7410ee519a1fSgjelinek 	 * If we can't tokenize this entry, just return.
7411ee519a1fSgjelinek 	 */
741239935be5Sgjelinek 	if ((p = strtok_r(ep, " ", &lastp)) == NULL)
7413ee519a1fSgjelinek 		return (Z_OK);
7414ee519a1fSgjelinek 
7415ee519a1fSgjelinek 	do {
7416ee519a1fSgjelinek 		if (strcmp(p, "Installed:") == 0 ||
7417ee519a1fSgjelinek 		    strcmp(p, "Requires:") == 0 ||
741839935be5Sgjelinek 		    strcmp(p, "From:") == 0 ||
741939935be5Sgjelinek 		    strcmp(p, "Incompatibles:") == 0) {
7420ee519a1fSgjelinek 			add_info = B_FALSE;
7421ee519a1fSgjelinek 			continue;
7422ee519a1fSgjelinek 		} else if (strcmp(p, "Obsoletes:") == 0) {
7423ee519a1fSgjelinek 			add_info = B_TRUE;
7424ee519a1fSgjelinek 			continue;
7425ee519a1fSgjelinek 		}
7426ee519a1fSgjelinek 
7427ee519a1fSgjelinek 		if (!add_info)
7428ee519a1fSgjelinek 			continue;
7429ee519a1fSgjelinek 
743039935be5Sgjelinek 		if ((pvers = strchr(p, '-')) != NULL)
743139935be5Sgjelinek 			*pvers = '\0';
7432ee519a1fSgjelinek 
74336cfd72c6Sgjelinek 		/*
74346cfd72c6Sgjelinek 		 * We save all of the obsolete patches in one big list in the
74356cfd72c6Sgjelinek 		 * obs_patches AVL tree so that we know not to output those as
74366cfd72c6Sgjelinek 		 * part of the sw dependencies.  However, we also need to save
74376cfd72c6Sgjelinek 		 * the obsolete patch information for this sepcific patch so
74386cfd72c6Sgjelinek 		 * so that we can do the cross manifest patch checking
74396cfd72c6Sgjelinek 		 * correctly.
74406cfd72c6Sgjelinek 		 */
74416cfd72c6Sgjelinek 		if (save_obs_patch(p, patches_pool, obs_patches) != Z_OK)
74426cfd72c6Sgjelinek 			return (Z_NOMEM);
74436cfd72c6Sgjelinek 		if (add_obs_patch(patch, p, list_pool) != Z_OK)
744439935be5Sgjelinek 			return (Z_NOMEM);
7445ee519a1fSgjelinek 	} while ((p = strtok_r(NULL, " ", &lastp)) != NULL);
7446ee519a1fSgjelinek 
7447ee519a1fSgjelinek 	return (Z_OK);
7448ee519a1fSgjelinek }
7449ee519a1fSgjelinek 
745039935be5Sgjelinek /*
745139935be5Sgjelinek  * AVL walker callback used to add patch to XML manifest.
745239935be5Sgjelinek  *
745339935be5Sgjelinek  * PATH_MAX is used in the pkg/patch code as the maximum size for the patch
745439935be5Sgjelinek  * number/version string.
745539935be5Sgjelinek  */
745639935be5Sgjelinek static int
745739935be5Sgjelinek add_patch(void *e, void *p)
7458ee519a1fSgjelinek {
745939935be5Sgjelinek 	xmlNodePtr	node;
7460ee519a1fSgjelinek 	xmlNodePtr	cur;
746139935be5Sgjelinek 	char		id[PATH_MAX];
746239935be5Sgjelinek 	patch_node_t	*patch;
746339935be5Sgjelinek 	patch_parms_t	*args;
7464ee519a1fSgjelinek 
746539935be5Sgjelinek 	patch = e;
746639935be5Sgjelinek 	args = p;
7467ee519a1fSgjelinek 
746839935be5Sgjelinek 	/* skip this patch if it has been obsoleted */
746939935be5Sgjelinek 	if (obsolete_patch(patch, args->obs_patches_avl))
747039935be5Sgjelinek 		return (UU_WALK_NEXT);
747139935be5Sgjelinek 
747239935be5Sgjelinek 	if (patch->patch_vers[0] == '\0')
747339935be5Sgjelinek 		(void) snprintf(id, sizeof (id), "%s", patch->patch_num);
747439935be5Sgjelinek 	else
747539935be5Sgjelinek 		(void) snprintf(id, sizeof (id), "%s-%s", patch->patch_num,
747639935be5Sgjelinek 		    patch->patch_vers);
747739935be5Sgjelinek 
747839935be5Sgjelinek 	if ((args->res = operation_prep(args->handle)) != Z_OK)
747939935be5Sgjelinek 		return (UU_WALK_DONE);
748039935be5Sgjelinek 
748139935be5Sgjelinek 	cur = args->handle->zone_dh_cur;
748239935be5Sgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
748339935be5Sgjelinek 	if ((args->res = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
748439935be5Sgjelinek 		return (UU_WALK_DONE);
748539935be5Sgjelinek 
74866cfd72c6Sgjelinek 	if (patch->obs_patches != NULL) {
74876cfd72c6Sgjelinek 		obs_patch_node_t *op;
74886cfd72c6Sgjelinek 		xmlNodePtr	node2;
74896cfd72c6Sgjelinek 
74906cfd72c6Sgjelinek 		for (op = uu_list_first(patch->obs_patches); op != NULL;
74916cfd72c6Sgjelinek 		    op = uu_list_next(patch->obs_patches, op)) {
74926cfd72c6Sgjelinek 			(void) snprintf(id, sizeof (id), "%s", op->patch_num);
74936cfd72c6Sgjelinek 			node2 = xmlNewTextChild(node, NULL, DTD_ELEM_OBSOLETES,
74946cfd72c6Sgjelinek 			    NULL);
74956cfd72c6Sgjelinek 			if ((args->res = newprop(node2, DTD_ATTR_ID, id))
74966cfd72c6Sgjelinek 			    != Z_OK)
74976cfd72c6Sgjelinek 				return (UU_WALK_DONE);
74986cfd72c6Sgjelinek 		}
74996cfd72c6Sgjelinek 	}
75006cfd72c6Sgjelinek 
750139935be5Sgjelinek 	return (UU_WALK_NEXT);
7502ee519a1fSgjelinek }
7503ee519a1fSgjelinek 
750439935be5Sgjelinek static void
750539935be5Sgjelinek patch_avl_delete(uu_avl_t *patches_avl)
750639935be5Sgjelinek {
750739935be5Sgjelinek 	if (patches_avl != NULL) {
750839935be5Sgjelinek 		patch_node_t *p;
750939935be5Sgjelinek 		void *cookie = NULL;
751039935be5Sgjelinek 
751139935be5Sgjelinek 		while ((p = (patch_node_t *)uu_avl_teardown(patches_avl,
751239935be5Sgjelinek 		    &cookie)) != NULL) {
751339935be5Sgjelinek 			free(p->patch_num);
751439935be5Sgjelinek 			free(p->patch_vers);
75156cfd72c6Sgjelinek 
75166cfd72c6Sgjelinek 			if (p->obs_patches != NULL) {
75176cfd72c6Sgjelinek 				obs_patch_node_t *op;
75186cfd72c6Sgjelinek 				void *cookie2 = NULL;
75196cfd72c6Sgjelinek 
75206cfd72c6Sgjelinek 				while ((op = uu_list_teardown(p->obs_patches,
75216cfd72c6Sgjelinek 				    &cookie2)) != NULL) {
75226cfd72c6Sgjelinek 					free(op->patch_num);
75236cfd72c6Sgjelinek 					free(op);
75246cfd72c6Sgjelinek 				}
75256cfd72c6Sgjelinek 				uu_list_destroy(p->obs_patches);
75266cfd72c6Sgjelinek 			}
75276cfd72c6Sgjelinek 
752839935be5Sgjelinek 			free(p);
752939935be5Sgjelinek 		}
753039935be5Sgjelinek 
753139935be5Sgjelinek 		uu_avl_destroy(patches_avl);
753239935be5Sgjelinek 	}
7533ee519a1fSgjelinek }
7534ee519a1fSgjelinek 
7535ee519a1fSgjelinek /*
753639935be5Sgjelinek  * Add the unique, highest version patches that are associated with this pkg
753739935be5Sgjelinek  * to the sw inventory on the handle.
7538ee519a1fSgjelinek  */
7539ee519a1fSgjelinek static int
754039935be5Sgjelinek add_patches(zone_dochandle_t handle, struct zone_pkginfo *infop,
75416cfd72c6Sgjelinek     uu_avl_pool_t *patches_pool, uu_avl_t *obs_patches,
75426cfd72c6Sgjelinek     uu_list_pool_t *list_pool)
7543ee519a1fSgjelinek {
7544ee519a1fSgjelinek 	int		i;
754539935be5Sgjelinek 	int		res;
754639935be5Sgjelinek 	uu_avl_t 	*patches_avl;
754739935be5Sgjelinek 	patch_parms_t	args;
754839935be5Sgjelinek 
754939935be5Sgjelinek 	if ((patches_avl = uu_avl_create(patches_pool, NULL, UU_DEFAULT))
755039935be5Sgjelinek 	    == NULL)
755139935be5Sgjelinek 		return (Z_NOMEM);
7552ee519a1fSgjelinek 
7553ee519a1fSgjelinek 	for (i = 0; i < infop->zpi_patch_cnt; i++) {
755439935be5Sgjelinek 		if ((res = parse_info(infop->zpi_patchinfo[i], patches_pool,
75556cfd72c6Sgjelinek 		    patches_avl, obs_patches, list_pool)) != Z_OK) {
755639935be5Sgjelinek 			patch_avl_delete(patches_avl);
755739935be5Sgjelinek 			return (res);
755839935be5Sgjelinek 		}
7559ee519a1fSgjelinek 	}
7560ee519a1fSgjelinek 
75616cfd72c6Sgjelinek 	args.obs_patches_avl = obs_patches;
756239935be5Sgjelinek 	args.handle = handle;
756339935be5Sgjelinek 	args.res = Z_OK;
756439935be5Sgjelinek 
756539935be5Sgjelinek 	(void) uu_avl_walk(patches_avl, add_patch, &args, 0);
756639935be5Sgjelinek 
756739935be5Sgjelinek 	patch_avl_delete(patches_avl);
756839935be5Sgjelinek 	return (args.res);
7569ee519a1fSgjelinek }
7570ee519a1fSgjelinek 
7571ee519a1fSgjelinek /*
75726cfd72c6Sgjelinek  * Keep track of the pkgs we have already processed so that we can quickly
75736cfd72c6Sgjelinek  * skip those pkgs while recursively doing dependents.
75746cfd72c6Sgjelinek  */
75756cfd72c6Sgjelinek static boolean_t
75766cfd72c6Sgjelinek pkg_in_manifest(uu_avl_t *saw_pkgs, char *pname, uu_avl_pool_t *pkgs_pool)
75776cfd72c6Sgjelinek {
75786cfd72c6Sgjelinek 	uu_avl_index_t where;
75796cfd72c6Sgjelinek 
75806cfd72c6Sgjelinek 	if (uu_avl_find(saw_pkgs, pname, NULL, &where) == NULL) {
75816cfd72c6Sgjelinek 		zone_pkg_entry_t *pkg;
75826cfd72c6Sgjelinek 
75836cfd72c6Sgjelinek 		/*
75846cfd72c6Sgjelinek 		 * We need to add it.  If we don't have memory we just skip
75856cfd72c6Sgjelinek 		 * this pkg since this routine improves performance but the
75866cfd72c6Sgjelinek 		 * algorithm is still correct without it.
75876cfd72c6Sgjelinek 		 */
75886cfd72c6Sgjelinek 		if ((pkg = (zone_pkg_entry_t *)
75896cfd72c6Sgjelinek 		    malloc(sizeof (zone_pkg_entry_t))) == NULL)
75906cfd72c6Sgjelinek 			return (B_FALSE);
75916cfd72c6Sgjelinek 
75926cfd72c6Sgjelinek 		if ((pkg->zpe_name = strdup(pname)) == NULL) {
75936cfd72c6Sgjelinek 			free(pkg);
75946cfd72c6Sgjelinek 			return (B_FALSE);
75956cfd72c6Sgjelinek 		}
75966cfd72c6Sgjelinek 
75976cfd72c6Sgjelinek 		pkg->zpe_vers = NULL;
75986cfd72c6Sgjelinek 		pkg->zpe_patches_avl = NULL;
75996cfd72c6Sgjelinek 
76006cfd72c6Sgjelinek 		/* Insert pkg into the AVL tree. */
76016cfd72c6Sgjelinek 		uu_avl_node_init(pkg, &pkg->zpe_entry, pkgs_pool);
76026cfd72c6Sgjelinek 		uu_avl_insert(saw_pkgs, pkg, where);
76036cfd72c6Sgjelinek 		return (B_FALSE);
76046cfd72c6Sgjelinek 	}
76056cfd72c6Sgjelinek 
76066cfd72c6Sgjelinek 	return (B_TRUE);
76076cfd72c6Sgjelinek }
76086cfd72c6Sgjelinek 
76096cfd72c6Sgjelinek /*
7610ee519a1fSgjelinek  * Add the pkg to the sw inventory on the handle.
7611ee519a1fSgjelinek  */
7612ee519a1fSgjelinek static int
7613ee519a1fSgjelinek add_pkg(zone_dochandle_t handle, char *name, char *version)
7614ee519a1fSgjelinek {
7615ee519a1fSgjelinek 	xmlNodePtr newnode;
7616ee519a1fSgjelinek 	xmlNodePtr cur;
7617ee519a1fSgjelinek 	int err;
7618ee519a1fSgjelinek 
7619ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
7620ee519a1fSgjelinek 		return (err);
7621ee519a1fSgjelinek 
7622ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
7623ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
7624ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
7625ee519a1fSgjelinek 		return (err);
7626ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
7627ee519a1fSgjelinek 		return (err);
7628ee519a1fSgjelinek 	return (Z_OK);
7629ee519a1fSgjelinek }
7630ee519a1fSgjelinek 
7631ee519a1fSgjelinek static void
7632ee519a1fSgjelinek free_pkginfo(struct zone_pkginfo *infop)
7633ee519a1fSgjelinek {
7634ee519a1fSgjelinek 	free(infop->zpi_version);
7635ee519a1fSgjelinek 	if (infop->zpi_patch_cnt > 0) {
7636ee519a1fSgjelinek 		int i;
7637ee519a1fSgjelinek 
7638ee519a1fSgjelinek 		for (i = 0; i < infop->zpi_patch_cnt; i++)
7639ee519a1fSgjelinek 			free(infop->zpi_patchinfo[i]);
7640ee519a1fSgjelinek 		free(infop->zpi_patchinfo);
7641ee519a1fSgjelinek 	}
7642ee519a1fSgjelinek }
7643ee519a1fSgjelinek 
7644ee519a1fSgjelinek /*
7645ee519a1fSgjelinek  * Read the pkginfo file and populate the structure with the data we need
7646ee519a1fSgjelinek  * from this pkg for a sw inventory.
7647ee519a1fSgjelinek  */
7648ee519a1fSgjelinek static int
7649ee519a1fSgjelinek get_pkginfo(char *pkginfo, struct zone_pkginfo *infop)
7650ee519a1fSgjelinek {
7651ee519a1fSgjelinek 	FILE	*fp;
7652ee519a1fSgjelinek 	char	*buf;
7653ee519a1fSgjelinek 	int	err = 0;
7654ee519a1fSgjelinek 
7655ee519a1fSgjelinek 	infop->zpi_all_zones = B_FALSE;
7656ee519a1fSgjelinek 	infop->zpi_this_zone = B_FALSE;
7657ee519a1fSgjelinek 	infop->zpi_version = NULL;
7658ee519a1fSgjelinek 	infop->zpi_patch_cnt = 0;
7659ee519a1fSgjelinek 	infop->zpi_patchinfo = NULL;
7660ee519a1fSgjelinek 
7661ee519a1fSgjelinek 	if ((fp = fopen(pkginfo, "r")) == NULL)
7662ee519a1fSgjelinek 		return (errno);
7663ee519a1fSgjelinek 
7664ee519a1fSgjelinek 	while ((buf = read_pkg_data(fp)) != NULL) {
7665ee519a1fSgjelinek 		if (strncmp(buf, VERSION, sizeof (VERSION) - 1) == 0) {
7666ee519a1fSgjelinek 			int len;
7667ee519a1fSgjelinek 
7668ee519a1fSgjelinek 			if ((infop->zpi_version =
7669ee519a1fSgjelinek 			    strdup(buf + sizeof (VERSION) - 1)) == NULL) {
7670ee519a1fSgjelinek 				err = ENOMEM;
7671ee519a1fSgjelinek 				break;
7672ee519a1fSgjelinek 			}
7673ee519a1fSgjelinek 
7674ee519a1fSgjelinek 			/* remove trailing newline */
7675ee519a1fSgjelinek 			len = strlen(infop->zpi_version);
7676ee519a1fSgjelinek 			*(infop->zpi_version + len - 1) = 0;
7677ee519a1fSgjelinek 
767845916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_ALL_ZONES) == 0) {
7679ee519a1fSgjelinek 			infop->zpi_all_zones = B_TRUE;
7680ee519a1fSgjelinek 
768145916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_THIS_ZONE) == 0) {
7682ee519a1fSgjelinek 			infop->zpi_this_zone = B_TRUE;
7683ee519a1fSgjelinek 
7684ee519a1fSgjelinek 		} else if (strncmp(buf, PATCHINFO, sizeof (PATCHINFO) - 1)
7685ee519a1fSgjelinek 		    == 0) {
7686ee519a1fSgjelinek 			char **p;
7687ee519a1fSgjelinek 
7688ee519a1fSgjelinek 			if ((p = (char **)realloc(infop->zpi_patchinfo,
7689ee519a1fSgjelinek 			    sizeof (char *) * (infop->zpi_patch_cnt + 1)))
7690ee519a1fSgjelinek 			    == NULL) {
7691ee519a1fSgjelinek 				err = ENOMEM;
7692ee519a1fSgjelinek 				break;
7693ee519a1fSgjelinek 			}
7694ee519a1fSgjelinek 			infop->zpi_patchinfo = p;
7695ee519a1fSgjelinek 
7696ee519a1fSgjelinek 			if ((infop->zpi_patchinfo[infop->zpi_patch_cnt] =
7697ee519a1fSgjelinek 			    strdup(buf)) == NULL) {
7698ee519a1fSgjelinek 				err = ENOMEM;
7699ee519a1fSgjelinek 				break;
7700ee519a1fSgjelinek 			}
7701ee519a1fSgjelinek 			infop->zpi_patch_cnt++;
7702ee519a1fSgjelinek 		}
7703ee519a1fSgjelinek 
7704ee519a1fSgjelinek 		free(buf);
7705ee519a1fSgjelinek 	}
7706ee519a1fSgjelinek 
7707ee519a1fSgjelinek 	free(buf);
7708ee519a1fSgjelinek 
7709ee519a1fSgjelinek 	if (errno == ENOMEM) {
7710ee519a1fSgjelinek 		err = ENOMEM;
7711ee519a1fSgjelinek 		/* Clean up anything we did manage to allocate. */
7712ee519a1fSgjelinek 		free_pkginfo(infop);
7713ee519a1fSgjelinek 	}
7714ee519a1fSgjelinek 
7715ee519a1fSgjelinek 	(void) fclose(fp);
7716ee519a1fSgjelinek 
7717ee519a1fSgjelinek 	return (err);
7718ee519a1fSgjelinek }
7719ee519a1fSgjelinek 
7720ee519a1fSgjelinek /*
77216cfd72c6Sgjelinek  * Add any dependent pkgs to the list.  The pkg depend file lists pkg
77226cfd72c6Sgjelinek  * dependencies, one per line with an entry that looks like:
77236cfd72c6Sgjelinek  *	P SUNWcar       Core Architecture, (Root)
77246cfd72c6Sgjelinek  * See the depend(4) man page.
77256cfd72c6Sgjelinek  */
77266cfd72c6Sgjelinek static int
77276cfd72c6Sgjelinek add_dependents(zone_dochandle_t handle, char *pname,
77286cfd72c6Sgjelinek     uu_avl_pool_t *patches_pool, uu_avl_t *obs_patches,
77296cfd72c6Sgjelinek     uu_list_pool_t *list_pool, uu_avl_t *saw_pkgs, uu_avl_pool_t *pkgs_pool)
77306cfd72c6Sgjelinek {
77316cfd72c6Sgjelinek 	int		res = Z_OK;
77326cfd72c6Sgjelinek 	FILE		*fp;
77336cfd72c6Sgjelinek 	char		depend[MAXPATHLEN];
77346cfd72c6Sgjelinek 	char		*buf;
77356cfd72c6Sgjelinek 	struct stat	sbuf;
77366cfd72c6Sgjelinek 
77376cfd72c6Sgjelinek 	(void) snprintf(depend, sizeof (depend), "%s/%s/install/depend",
77386cfd72c6Sgjelinek 	    PKG_PATH, pname);
77396cfd72c6Sgjelinek 
77406cfd72c6Sgjelinek 	if (stat(depend, &sbuf) == -1 || !S_ISREG(sbuf.st_mode))
77416cfd72c6Sgjelinek 		return (Z_OK);
77426cfd72c6Sgjelinek 
77436cfd72c6Sgjelinek 	if ((fp = fopen(depend, "r")) == NULL)
77446cfd72c6Sgjelinek 		return (Z_OK);
77456cfd72c6Sgjelinek 
77466cfd72c6Sgjelinek 	while ((buf = read_pkg_data(fp)) != NULL) {
77476cfd72c6Sgjelinek 		char *deppkg;
77486cfd72c6Sgjelinek 		char *delims = " \t";
77496cfd72c6Sgjelinek 		char pkginfo[MAXPATHLEN];
77506cfd72c6Sgjelinek 		struct zone_pkginfo info;
77516cfd72c6Sgjelinek 
77526cfd72c6Sgjelinek 		if (*buf != 'P') {
77536cfd72c6Sgjelinek 			free(buf);
77546cfd72c6Sgjelinek 			continue;
77556cfd72c6Sgjelinek 		}
77566cfd72c6Sgjelinek 
77576cfd72c6Sgjelinek 		/* Skip past the leading 'P '. */
77586cfd72c6Sgjelinek 		if ((deppkg = strtok(buf + 2, delims)) == NULL) {
77596cfd72c6Sgjelinek 			free(buf);
77606cfd72c6Sgjelinek 			continue;
77616cfd72c6Sgjelinek 		}
77626cfd72c6Sgjelinek 
77636cfd72c6Sgjelinek 		/* If the pkg is already in the manifest don't add it again. */
77646cfd72c6Sgjelinek 		if (pkg_in_manifest(saw_pkgs, deppkg, pkgs_pool)) {
77656cfd72c6Sgjelinek 			free(buf);
77666cfd72c6Sgjelinek 			continue;
77676cfd72c6Sgjelinek 		}
77686cfd72c6Sgjelinek 
77696cfd72c6Sgjelinek 		(void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo",
77706cfd72c6Sgjelinek 		    PKG_PATH, deppkg);
77716cfd72c6Sgjelinek 
77726cfd72c6Sgjelinek 		if (stat(pkginfo, &sbuf) == -1 || !S_ISREG(sbuf.st_mode)) {
77736cfd72c6Sgjelinek 			free(buf);
77746cfd72c6Sgjelinek 			continue;
77756cfd72c6Sgjelinek 		}
77766cfd72c6Sgjelinek 
77776cfd72c6Sgjelinek 		if (get_pkginfo(pkginfo, &info) != 0) {
77786cfd72c6Sgjelinek 			res = Z_NOMEM;
77796cfd72c6Sgjelinek 			free(buf);
77806cfd72c6Sgjelinek 			break;
77816cfd72c6Sgjelinek 		}
77826cfd72c6Sgjelinek 
77836cfd72c6Sgjelinek 		if ((res = add_dependents(handle, deppkg, patches_pool,
77846cfd72c6Sgjelinek 		    obs_patches, list_pool, saw_pkgs, pkgs_pool)) == Z_OK &&
77856cfd72c6Sgjelinek 		    (res = add_pkg(handle, deppkg, info.zpi_version)) == Z_OK) {
77866cfd72c6Sgjelinek 			if (info.zpi_patch_cnt > 0)
77876cfd72c6Sgjelinek 				res = add_patches(handle, &info, patches_pool,
77886cfd72c6Sgjelinek 				    obs_patches, list_pool);
77896cfd72c6Sgjelinek 		}
77906cfd72c6Sgjelinek 
77916cfd72c6Sgjelinek 		free(buf);
77926cfd72c6Sgjelinek 		free_pkginfo(&info);
77936cfd72c6Sgjelinek 
77946cfd72c6Sgjelinek 		if (res != Z_OK)
77956cfd72c6Sgjelinek 			break;
77966cfd72c6Sgjelinek 	}
77976cfd72c6Sgjelinek 
77986cfd72c6Sgjelinek 	(void) fclose(fp);
77996cfd72c6Sgjelinek 	return (res);
78006cfd72c6Sgjelinek }
78016cfd72c6Sgjelinek 
78026cfd72c6Sgjelinek /* ARGSUSED */
78036cfd72c6Sgjelinek static int
78046cfd72c6Sgjelinek pkg_entry_compare(const void *l_arg, const void *r_arg, void *private)
78056cfd72c6Sgjelinek {
78066cfd72c6Sgjelinek 	zone_pkg_entry_t *pkg = (zone_pkg_entry_t *)l_arg;
78076cfd72c6Sgjelinek 	char *name = (char *)r_arg;
78086cfd72c6Sgjelinek 
78096cfd72c6Sgjelinek 	return (strcmp(pkg->zpe_name, name));
78106cfd72c6Sgjelinek }
78116cfd72c6Sgjelinek 
78126cfd72c6Sgjelinek static void
78136cfd72c6Sgjelinek pkg_avl_delete(uu_avl_t *pavl)
78146cfd72c6Sgjelinek {
78156cfd72c6Sgjelinek 	if (pavl != NULL) {
78166cfd72c6Sgjelinek 		zone_pkg_entry_t *p;
78176cfd72c6Sgjelinek 		void *cookie = NULL;
78186cfd72c6Sgjelinek 
78196cfd72c6Sgjelinek 		while ((p = uu_avl_teardown(pavl, &cookie)) != NULL) {
78206cfd72c6Sgjelinek 			free(p->zpe_name);
78216cfd72c6Sgjelinek 			free(p);
78226cfd72c6Sgjelinek 		}
78236cfd72c6Sgjelinek 
78246cfd72c6Sgjelinek 		uu_avl_destroy(pavl);
78256cfd72c6Sgjelinek 	}
78266cfd72c6Sgjelinek }
78276cfd72c6Sgjelinek 
78286cfd72c6Sgjelinek /*
7829ee519a1fSgjelinek  * Take a software inventory of the global zone.  We need to get the set of
7830ee519a1fSgjelinek  * packages and patches that are on the global zone that the specified
7831ee519a1fSgjelinek  * non-global zone depends on.  The packages we need in the inventory are:
7832ee519a1fSgjelinek  *
7833ee519a1fSgjelinek  * - skip the package if SUNW_PKG_THISZONE is 'true'
7834ee519a1fSgjelinek  * otherwise,
7835ee519a1fSgjelinek  * - add the package if
7836ee519a1fSgjelinek  * a) SUNW_PKG_ALLZONES is 'true',
7837ee519a1fSgjelinek  * or
7838ee519a1fSgjelinek  * b) any file delivered by the package is in a file system that is inherited
7839ee519a1fSgjelinek  * from the global zone.
7840ee519a1fSgjelinek  * If the zone does not inherit any file systems (whole root)
7841ee519a1fSgjelinek  * then (b) will be skipped.
7842ee519a1fSgjelinek  *
7843ee519a1fSgjelinek  * For each of the packages that is being added to the inventory, we will also
78446cfd72c6Sgjelinek  * add its dependent packages to the inventory.
78456cfd72c6Sgjelinek  *
78466cfd72c6Sgjelinek  * For each of the packages that is being added to the inventory, we will also
7847ee519a1fSgjelinek  * add all of the associated, unique patches to the inventory.
78486cfd72c6Sgjelinek  *
78496cfd72c6Sgjelinek  * See the comment for zonecfg_getpkgdata() for compatability restrictions on
78506cfd72c6Sgjelinek  * how we must save the XML representation of the software inventory.
7851ee519a1fSgjelinek  */
7852ee519a1fSgjelinek static int
7853ee519a1fSgjelinek zonecfg_sw_inventory(zone_dochandle_t handle)
7854ee519a1fSgjelinek {
7855ee519a1fSgjelinek 	char		pkginfo[MAXPATHLEN];
7856ee519a1fSgjelinek 	int		res;
7857ee519a1fSgjelinek 	struct dirent	*dp;
7858ee519a1fSgjelinek 	DIR		*dirp;
7859ee519a1fSgjelinek 	struct stat	buf;
7860ee519a1fSgjelinek 	struct zone_pkginfo	info;
7861ee519a1fSgjelinek 	int		pkg_cnt = 0;
7862ee519a1fSgjelinek 	char		**pkgs = NULL;
78636cfd72c6Sgjelinek 	uu_avl_pool_t 	*pkgs_pool = NULL;
78646cfd72c6Sgjelinek 	uu_avl_pool_t 	*patches_pool = NULL;
78656cfd72c6Sgjelinek 	uu_list_pool_t 	*list_pool = NULL;
78666cfd72c6Sgjelinek 	uu_avl_t	*saw_pkgs = NULL;
78676cfd72c6Sgjelinek 	uu_avl_t 	*obs_patches = NULL;
78686cfd72c6Sgjelinek 
78696cfd72c6Sgjelinek 	if ((pkgs_pool = uu_avl_pool_create("pkgs_pool",
78706cfd72c6Sgjelinek 	    sizeof (zone_pkg_entry_t), offsetof(zone_pkg_entry_t, zpe_entry),
78716cfd72c6Sgjelinek 	    pkg_entry_compare, UU_DEFAULT)) == NULL) {
78726cfd72c6Sgjelinek 		res = Z_NOMEM;
78736cfd72c6Sgjelinek 		goto done;
78746cfd72c6Sgjelinek 	}
78756cfd72c6Sgjelinek 
78766cfd72c6Sgjelinek 	if ((saw_pkgs = uu_avl_create(pkgs_pool, NULL, UU_DEFAULT)) == NULL) {
78776cfd72c6Sgjelinek 		res = Z_NOMEM;
78786cfd72c6Sgjelinek 		goto done;
78796cfd72c6Sgjelinek 	}
7880ee519a1fSgjelinek 
788139935be5Sgjelinek 	if ((patches_pool = uu_avl_pool_create("patches_pool",
788239935be5Sgjelinek 	    sizeof (patch_node_t), offsetof(patch_node_t, patch_node),
788339935be5Sgjelinek 	    patch_node_compare, UU_DEFAULT)) == NULL) {
78846cfd72c6Sgjelinek 		res = Z_NOMEM;
78856cfd72c6Sgjelinek 		goto done;
788639935be5Sgjelinek 	}
788739935be5Sgjelinek 
78886cfd72c6Sgjelinek 	if ((list_pool = uu_list_pool_create("list_pool",
78896cfd72c6Sgjelinek 	    sizeof (obs_patch_node_t), offsetof(obs_patch_node_t, link), NULL,
78906cfd72c6Sgjelinek 	    UU_DEFAULT)) == NULL) {
78916cfd72c6Sgjelinek 		res = Z_NOMEM;
78926cfd72c6Sgjelinek 		goto done;
78936cfd72c6Sgjelinek 	}
78946cfd72c6Sgjelinek 
78956cfd72c6Sgjelinek 	/*
78966cfd72c6Sgjelinek 	 * The obs_patches AVL tree saves all of the obsolete patches so
78976cfd72c6Sgjelinek 	 * that we know not to output those as part of the sw dependencies.
78986cfd72c6Sgjelinek 	 */
78996cfd72c6Sgjelinek 	if ((obs_patches = uu_avl_create(patches_pool, NULL, UU_DEFAULT))
790039935be5Sgjelinek 	    == NULL) {
79016cfd72c6Sgjelinek 		res = Z_NOMEM;
79026cfd72c6Sgjelinek 		goto done;
790339935be5Sgjelinek 	}
790439935be5Sgjelinek 
790539935be5Sgjelinek 	if ((res = get_ipd_pkgs(handle, &pkgs, &pkg_cnt)) != Z_OK) {
79066cfd72c6Sgjelinek 		res = Z_NOMEM;
79076cfd72c6Sgjelinek 		goto done;
790839935be5Sgjelinek 	}
7909ee519a1fSgjelinek 
7910ee519a1fSgjelinek 	if ((dirp = opendir(PKG_PATH)) == NULL) {
79116cfd72c6Sgjelinek 		res = Z_NOMEM;
79126cfd72c6Sgjelinek 		goto done;
7913ee519a1fSgjelinek 	}
7914ee519a1fSgjelinek 
7915ee519a1fSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
7916ee519a1fSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
7917ee519a1fSgjelinek 		    strcmp(dp->d_name, "..") == 0)
7918ee519a1fSgjelinek 			continue;
7919ee519a1fSgjelinek 
7920ee519a1fSgjelinek 		(void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo",
7921ee519a1fSgjelinek 		    PKG_PATH, dp->d_name);
7922ee519a1fSgjelinek 
7923ee519a1fSgjelinek 		if (stat(pkginfo, &buf) == -1 || !S_ISREG(buf.st_mode))
7924ee519a1fSgjelinek 			continue;
7925ee519a1fSgjelinek 
7926ee519a1fSgjelinek 		if (get_pkginfo(pkginfo, &info) != 0) {
7927ee519a1fSgjelinek 			res = Z_NOMEM;
7928ee519a1fSgjelinek 			break;
7929ee519a1fSgjelinek 		}
7930ee519a1fSgjelinek 
7931ee519a1fSgjelinek 		if (!info.zpi_this_zone &&
7932ee519a1fSgjelinek 		    (info.zpi_all_zones ||
79336cfd72c6Sgjelinek 		    dir_pkg(dp->d_name, pkgs, pkg_cnt)) &&
79346cfd72c6Sgjelinek 		    !pkg_in_manifest(saw_pkgs, dp->d_name, pkgs_pool)) {
79356cfd72c6Sgjelinek 			/*
79366cfd72c6Sgjelinek 			 * Add dependents first so any patches will get
79376cfd72c6Sgjelinek 			 * associated with the right pkg in the xml file.
79386cfd72c6Sgjelinek 			 */
79396cfd72c6Sgjelinek 			if ((res = add_dependents(handle, dp->d_name,
79406cfd72c6Sgjelinek 			    patches_pool, obs_patches, list_pool, saw_pkgs,
79416cfd72c6Sgjelinek 			    pkgs_pool)) == Z_OK &&
79426cfd72c6Sgjelinek 			    (res = add_pkg(handle, dp->d_name,
7943ee519a1fSgjelinek 			    info.zpi_version)) == Z_OK) {
7944ee519a1fSgjelinek 				if (info.zpi_patch_cnt > 0)
794539935be5Sgjelinek 					res = add_patches(handle, &info,
79466cfd72c6Sgjelinek 					    patches_pool, obs_patches,
79476cfd72c6Sgjelinek 					    list_pool);
7948ee519a1fSgjelinek 			}
7949ee519a1fSgjelinek 		}
7950ee519a1fSgjelinek 
7951ee519a1fSgjelinek 		free_pkginfo(&info);
7952ee519a1fSgjelinek 
7953ee519a1fSgjelinek 		if (res != Z_OK)
7954ee519a1fSgjelinek 			break;
7955ee519a1fSgjelinek 	}
7956ee519a1fSgjelinek 
7957ee519a1fSgjelinek 	(void) closedir(dirp);
7958ee519a1fSgjelinek 
79596cfd72c6Sgjelinek done:
79606cfd72c6Sgjelinek 	pkg_avl_delete(saw_pkgs);
79616cfd72c6Sgjelinek 	patch_avl_delete(obs_patches);
79626cfd72c6Sgjelinek 	if (pkgs_pool != NULL)
79636cfd72c6Sgjelinek 		uu_avl_pool_destroy(pkgs_pool);
79646cfd72c6Sgjelinek 	if (patches_pool != NULL)
796539935be5Sgjelinek 		uu_avl_pool_destroy(patches_pool);
79666cfd72c6Sgjelinek 	if (list_pool != NULL)
79676cfd72c6Sgjelinek 		uu_list_pool_destroy(list_pool);
7968ee519a1fSgjelinek 	free_ipd_pkgs(pkgs, pkg_cnt);
7969ee519a1fSgjelinek 
7970ee519a1fSgjelinek 	if (res == Z_OK)
7971ee519a1fSgjelinek 		handle->zone_dh_sw_inv = B_TRUE;
7972ee519a1fSgjelinek 
7973ee519a1fSgjelinek 	return (res);
7974ee519a1fSgjelinek }
7975ee519a1fSgjelinek 
7976ee519a1fSgjelinek /*
7977ee519a1fSgjelinek  * zonecfg_devwalk call-back function used during detach to generate the
7978ee519a1fSgjelinek  * dev info in the manifest.
7979ee519a1fSgjelinek  */
7980ee519a1fSgjelinek static int
7981ee519a1fSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
7982ee519a1fSgjelinek     const char *acl, void *hdl)
7983ee519a1fSgjelinek {
7984ee519a1fSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
7985ee519a1fSgjelinek 	xmlNodePtr newnode;
7986ee519a1fSgjelinek 	xmlNodePtr cur;
7987ee519a1fSgjelinek 	int err;
7988ee519a1fSgjelinek 	char buf[128];
7989ee519a1fSgjelinek 
7990ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
7991ee519a1fSgjelinek 		return (err);
7992ee519a1fSgjelinek 
7993ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
7994ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
7995ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
7996ee519a1fSgjelinek 		return (err);
7997ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
7998ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
7999ee519a1fSgjelinek 		return (err);
8000ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
8001ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
8002ee519a1fSgjelinek 		return (err);
8003ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
8004ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
8005ee519a1fSgjelinek 		return (err);
8006ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
8007ee519a1fSgjelinek 		return (err);
8008ee519a1fSgjelinek 	return (Z_OK);
8009ee519a1fSgjelinek }
8010ee519a1fSgjelinek 
8011ee519a1fSgjelinek /*
8012ee519a1fSgjelinek  * Get the information required to support detaching a zone.  This is
8013ee519a1fSgjelinek  * called on the source system when detaching (the detaching parameter should
8014ee519a1fSgjelinek  * be set to true) and on the destination system before attaching (the
8015ee519a1fSgjelinek  * detaching parameter should be false).
8016ee519a1fSgjelinek  *
8017ee519a1fSgjelinek  * For native Solaris zones, the detach/attach process involves validating
8018ee519a1fSgjelinek  * that the software on the global zone can support the zone when we attach.
8019ee519a1fSgjelinek  * To do this we take a software inventory of the global zone.  We also
8020ee519a1fSgjelinek  * have to keep track of the device configuration so that we can properly
8021ee519a1fSgjelinek  * recreate it on the destination.
8022ee519a1fSgjelinek  */
8023ee519a1fSgjelinek int
8024ee519a1fSgjelinek zonecfg_get_detach_info(zone_dochandle_t handle, boolean_t detaching)
8025ee519a1fSgjelinek {
8026ee519a1fSgjelinek 	int		res;
8027ee519a1fSgjelinek 
8028ee519a1fSgjelinek 	if ((res = zonecfg_sw_inventory(handle)) != Z_OK)
8029ee519a1fSgjelinek 		return (res);
8030ee519a1fSgjelinek 
8031ee519a1fSgjelinek 	if (detaching)
8032ee519a1fSgjelinek 		res = zonecfg_devwalk(handle, get_detach_dev_entry, handle);
8033ee519a1fSgjelinek 
8034ee519a1fSgjelinek 	return (res);
8035ee519a1fSgjelinek }
8036