17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ee519a1fSgjelinek  * Common Development and Distribution License (the "License").
6ee519a1fSgjelinek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
23*f4b3ec61Sdh  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
29cf8f45c7Sdstaff #include <libsysevent.h>
30cf8f45c7Sdstaff #include <pthread.h>
31cf8f45c7Sdstaff #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <fnmatch.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate #include <libgen.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <alloca.h>
407c478bd9Sstevel@tonic-gate #include <ctype.h>
419acbbeafSnn #include <sys/acl.h>
429acbbeafSnn #include <sys/stat.h>
439acbbeafSnn #include <sys/brand.h>
447c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
457c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
46cf8f45c7Sdstaff #include <sys/nvpair.h>
479acbbeafSnn #include <sys/types.h>
48*f4b3ec61Sdh #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>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
567c478bd9Sstevel@tonic-gate #include <netdb.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h>
597c478bd9Sstevel@tonic-gate #include <libxml/parser.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
62108322fbScarlsonj #include <uuid/uuid.h>
63ee519a1fSgjelinek #include <dirent.h>
649acbbeafSnn #include <libbrand.h>
65ee519a1fSgjelinek 
667c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
677c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	"/zonecfg.XXXXXX"
70cf8f45c7Sdstaff #define	ZONE_CB_RETRY_COUNT		10
71cf8f45c7Sdstaff #define	ZONE_EVENT_PING_SUBCLASS	"ping"
72cf8f45c7Sdstaff #define	ZONE_EVENT_PING_PUBLISHER	"solaris"
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */
757c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ATTR		(const xmlChar *) "attr"
767c478bd9Sstevel@tonic-gate #define	DTD_ELEM_COMMENT	(const xmlChar *) "comment"
777c478bd9Sstevel@tonic-gate #define	DTD_ELEM_DEVICE		(const xmlChar *) "device"
787c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FS		(const xmlChar *) "filesystem"
797c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FSOPTION	(const xmlChar *) "fsoption"
807c478bd9Sstevel@tonic-gate #define	DTD_ELEM_IPD		(const xmlChar *) "inherited-pkg-dir"
817c478bd9Sstevel@tonic-gate #define	DTD_ELEM_NET		(const xmlChar *) "network"
827c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTL		(const xmlChar *) "rctl"
837c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTLVALUE	(const xmlChar *) "rctl-value"
847c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ZONE		(const xmlChar *) "zone"
85fa9e4066Sahrens #define	DTD_ELEM_DATASET	(const xmlChar *) "dataset"
860209230bSgjelinek #define	DTD_ELEM_TMPPOOL	(const xmlChar *) "tmp_pool"
870209230bSgjelinek #define	DTD_ELEM_PSET		(const xmlChar *) "pset"
880209230bSgjelinek #define	DTD_ELEM_MCAP		(const xmlChar *) "mcap"
89ee519a1fSgjelinek #define	DTD_ELEM_PACKAGE	(const xmlChar *) "package"
90ee519a1fSgjelinek #define	DTD_ELEM_PATCH		(const xmlChar *) "patch"
91ee519a1fSgjelinek #define	DTD_ELEM_OBSOLETES	(const xmlChar *) "obsoletes"
92ee519a1fSgjelinek #define	DTD_ELEM_INCOMPATIBLE	(const xmlChar *) "incompatible"
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"
98*f4b3ec61Sdh #define	DTD_ATTR_IPTYPE		(const xmlChar *) "ip-type"
997c478bd9Sstevel@tonic-gate #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
1007c478bd9Sstevel@tonic-gate #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
101ffbafc53Scomay #define	DTD_ATTR_LIMITPRIV	(const xmlChar *) "limitpriv"
1023f2f09c1Sdp #define	DTD_ATTR_BOOTARGS	(const xmlChar *) "bootargs"
1030209230bSgjelinek #define	DTD_ATTR_SCHED		(const xmlChar *) "scheduling-class"
1047c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
1057c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
1067c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
1077c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
1087c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
1097c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
1107c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
1117c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
1127c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
1137c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
1140209230bSgjelinek #define	DTD_ATTR_NCPU_MIN	(const xmlChar *) "ncpu_min"
1150209230bSgjelinek #define	DTD_ATTR_NCPU_MAX	(const xmlChar *) "ncpu_max"
1160209230bSgjelinek #define	DTD_ATTR_IMPORTANCE	(const xmlChar *) "importance"
1170209230bSgjelinek #define	DTD_ATTR_PHYSCAP	(const xmlChar *) "physcap"
118ee519a1fSgjelinek #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
119ee519a1fSgjelinek #define	DTD_ATTR_ID		(const xmlChar *) "id"
120ee519a1fSgjelinek #define	DTD_ATTR_UID		(const xmlChar *) "uid"
121ee519a1fSgjelinek #define	DTD_ATTR_GID		(const xmlChar *) "gid"
122ee519a1fSgjelinek #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
123ee519a1fSgjelinek #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
1249acbbeafSnn #define	DTD_ATTR_BRAND		(const xmlChar *) "brand"
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
1277c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
1287c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
1297c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
1307c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
1317c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
1327c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
1337c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
1347c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
1357c478bd9Sstevel@tonic-gate 
136a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
137a1be23daSdp 
138ee519a1fSgjelinek #define	DETACHED	"SUNWdetached.xml"
139ee519a1fSgjelinek #define	ATTACH_FORCED	"SUNWattached.xml"
140ee519a1fSgjelinek #define	PKG_PATH	"/var/sadm/pkg"
141ee519a1fSgjelinek #define	CONTENTS_FILE	"/var/sadm/install/contents"
14245916cd2Sjpk #define	SUNW_PKG_ALL_ZONES	"SUNW_PKG_ALLZONES=true\n"
14345916cd2Sjpk #define	SUNW_PKG_THIS_ZONE	"SUNW_PKG_THISZONE=true\n"
144ee519a1fSgjelinek #define	VERSION		"VERSION="
145ee519a1fSgjelinek #define	PATCHLIST	"PATCHLIST="
146ee519a1fSgjelinek #define	PATCHINFO	"PATCH_INFO_"
147ee519a1fSgjelinek #define	PKGINFO_RD_LEN	128
148ee519a1fSgjelinek 
1490209230bSgjelinek #define	TMP_POOL_NAME	"SUNWtmp_%s"
1500209230bSgjelinek #define	MAX_TMP_POOL_NAME	(ZONENAME_MAX + 9)
1510209230bSgjelinek #define	RCAP_SERVICE	"system/rcap:default"
1520209230bSgjelinek #define	POOLD_SERVICE	"system/pools/dynamic:default"
1530209230bSgjelinek 
1540209230bSgjelinek /*
1550209230bSgjelinek  * rctl alias definitions
1560209230bSgjelinek  *
1570209230bSgjelinek  * This holds the alias, the full rctl name, the default priv value, action
1580209230bSgjelinek  * and lower limit.  The functions that handle rctl aliases step through
1590209230bSgjelinek  * this table, matching on the alias, and using the full values for setting
1600209230bSgjelinek  * the rctl entry as well the limit for validation.
1610209230bSgjelinek  */
1620209230bSgjelinek static struct alias {
1630209230bSgjelinek 	char *shortname;
1640209230bSgjelinek 	char *realname;
1650209230bSgjelinek 	char *priv;
1660209230bSgjelinek 	char *action;
1670209230bSgjelinek 	uint64_t low_limit;
1680209230bSgjelinek } aliases[] = {
1690209230bSgjelinek 	{ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100},
1700209230bSgjelinek 	{ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0},
1710209230bSgjelinek 	{ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0},
1720209230bSgjelinek 	{ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0},
1730209230bSgjelinek 	{ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0},
1740209230bSgjelinek 	{ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0},
1750209230bSgjelinek 	{ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0},
1760209230bSgjelinek 	{ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0},
1770209230bSgjelinek 	{NULL, NULL, NULL, NULL, 0}
1780209230bSgjelinek };
1790209230bSgjelinek 
1800209230bSgjelinek /*
1810209230bSgjelinek  * Structure for applying rctls to a running zone.  It allows important
1820209230bSgjelinek  * process values to be passed together easily.
1830209230bSgjelinek  */
1840209230bSgjelinek typedef struct pr_info_handle {
1850209230bSgjelinek 	struct ps_prochandle *pr;
1860209230bSgjelinek 	pid_t pid;
1870209230bSgjelinek } pr_info_handle_t;
1880209230bSgjelinek 
1897c478bd9Sstevel@tonic-gate struct zone_dochandle {
1907c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1917c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1927c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1937c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
194087719fdSdp 	boolean_t	zone_dh_newzone;
195087719fdSdp 	boolean_t	zone_dh_snapshot;
196ee519a1fSgjelinek 	boolean_t	zone_dh_sw_inv;
197087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
1987c478bd9Sstevel@tonic-gate };
1997c478bd9Sstevel@tonic-gate 
200cf8f45c7Sdstaff struct znotify {
201cf8f45c7Sdstaff 	void * zn_private;
202cf8f45c7Sdstaff 	evchan_t *zn_eventchan;
203cf8f45c7Sdstaff 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
204cf8f45c7Sdstaff 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
205cf8f45c7Sdstaff 	pthread_mutex_t zn_mutex;
206cf8f45c7Sdstaff 	pthread_cond_t zn_cond;
207cf8f45c7Sdstaff 	pthread_mutex_t zn_bigmutex;
208cf8f45c7Sdstaff 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
209cf8f45c7Sdstaff 	    ZN_PING_RECEIVED} zn_state;
210cf8f45c7Sdstaff 	char zn_subscriber_id[MAX_SUBID_LEN];
211cf8f45c7Sdstaff 	volatile boolean_t zn_failed;
212cf8f45c7Sdstaff 	int zn_failure_count;
213cf8f45c7Sdstaff };
214cf8f45c7Sdstaff 
215ee519a1fSgjelinek struct zone_pkginfo {
216ee519a1fSgjelinek 	boolean_t	zpi_all_zones;
217ee519a1fSgjelinek 	boolean_t	zpi_this_zone;
218ee519a1fSgjelinek 	int		zpi_patch_cnt;
219ee519a1fSgjelinek 	char		*zpi_version;
220ee519a1fSgjelinek 	char		**zpi_patchinfo;
221ee519a1fSgjelinek };
222ee519a1fSgjelinek 
223108322fbScarlsonj char *zonecfg_root = "";
224108322fbScarlsonj 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
2277c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
2287c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
2297c478bd9Sstevel@tonic-gate  * values from this set.
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate 
232108322fbScarlsonj /*
233108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
234108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
235108322fbScarlsonj  * configuration inside mounted alternate boot environments.
236108322fbScarlsonj  */
237108322fbScarlsonj void
238108322fbScarlsonj zonecfg_set_root(const char *rootpath)
239108322fbScarlsonj {
240108322fbScarlsonj 	if (*zonecfg_root != '\0')
241108322fbScarlsonj 		free(zonecfg_root);
242108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
243108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
244108322fbScarlsonj 		zonecfg_root = "";
245108322fbScarlsonj }
246108322fbScarlsonj 
247108322fbScarlsonj const char *
248108322fbScarlsonj zonecfg_get_root(void)
249108322fbScarlsonj {
250108322fbScarlsonj 	return (zonecfg_root);
251108322fbScarlsonj }
252108322fbScarlsonj 
253108322fbScarlsonj boolean_t
254108322fbScarlsonj zonecfg_in_alt_root(void)
255108322fbScarlsonj {
256108322fbScarlsonj 	return (*zonecfg_root != '\0');
257108322fbScarlsonj }
258108322fbScarlsonj 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
2617c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate 
264108322fbScarlsonj static boolean_t
2657c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
2667c478bd9Sstevel@tonic-gate {
267108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
268108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
271108322fbScarlsonj static boolean_t
272108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
2737c478bd9Sstevel@tonic-gate {
274108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
275108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2797c478bd9Sstevel@tonic-gate static void
2807c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
2847c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
2857c478bd9Sstevel@tonic-gate 	 */
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate zone_dochandle_t
2897c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
2907c478bd9Sstevel@tonic-gate {
291087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
2927c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
2937c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
2947c478bd9Sstevel@tonic-gate 		return (NULL);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
2987c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
2997c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3007c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
3017c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
3027c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
3037c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	return (handle);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate int
3097c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
3127c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3137c478bd9Sstevel@tonic-gate 	return (Z_OK);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate void
3177c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
3207c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
3217c478bd9Sstevel@tonic-gate 	if (handle != NULL)
3227c478bd9Sstevel@tonic-gate 		free(handle);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate static int
3267c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
3297c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
3307c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
3317c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
3327c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
3337c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	return (Z_OK);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate int
339087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
342087719fdSdp 	struct zoneent ze;
343087719fdSdp 	int err, state_err;
344087719fdSdp 	zone_state_t state;
3457c478bd9Sstevel@tonic-gate 
346108322fbScarlsonj 	if (!config_file_path(zonename, path))
347108322fbScarlsonj 		return (Z_MISC_FS);
348087719fdSdp 
349087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
350087719fdSdp 	err = access(path, W_OK);
351087719fdSdp 
352087719fdSdp 	/*
353087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
354087719fdSdp 	 * such zone exists.
355087719fdSdp 	 */
356087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
357087719fdSdp 		return (Z_NO_ZONE);
358087719fdSdp 
359087719fdSdp 	/*
360087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
361087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
362087719fdSdp 	 * in which case we plow on.
363087719fdSdp 	 */
364087719fdSdp 	if (err == -1 && errno != ENOENT) {
365087719fdSdp 		if (errno == EACCES)
366087719fdSdp 			return (Z_ACCES);
367087719fdSdp 		else if (!force)
368087719fdSdp 			return (Z_MISC_FS);
369087719fdSdp 	}
370087719fdSdp 
371087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
372087719fdSdp 		return (Z_BAD_ZONE_STATE);
373087719fdSdp 
374087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
375087719fdSdp 		return (Z_BAD_ZONE_STATE);
376087719fdSdp 
377087719fdSdp 	/*
378087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
379087719fdSdp 	 * will fail only if we've had some more severe problem.
380087719fdSdp 	 */
381087719fdSdp 	bzero(&ze, sizeof (ze));
382087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
383087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
384087719fdSdp 		if (!force)
385087719fdSdp 			return (err);
386087719fdSdp 
387087719fdSdp 	err = zonecfg_destroy_impl(path);
388087719fdSdp 
389087719fdSdp 	/*
390087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
391087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
392087719fdSdp 	 */
393087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
394087719fdSdp 		return (Z_OK);
395087719fdSdp 	return (err);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate int
399108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
4027c478bd9Sstevel@tonic-gate 
403108322fbScarlsonj 	if (!snap_file_path(zonename, path))
404108322fbScarlsonj 		return (Z_MISC_FS);
4057c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate static int
409a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
4127c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
4137c478bd9Sstevel@tonic-gate 
414a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
415a1be23daSdp 
416a1be23daSdp 	if (*root == NULL)
4177c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
4187c478bd9Sstevel@tonic-gate 
419a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
4207c478bd9Sstevel@tonic-gate 		return (Z_WRONG_DOC_TYPE);
421a1be23daSdp 
422a1be23daSdp 	return (Z_OK);
423a1be23daSdp }
424a1be23daSdp 
425a1be23daSdp static int
426a1be23daSdp operation_prep(zone_dochandle_t handle)
427a1be23daSdp {
428a1be23daSdp 	xmlNodePtr root;
429a1be23daSdp 	int err;
430a1be23daSdp 
431a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
432a1be23daSdp 		return (err);
433a1be23daSdp 
434a1be23daSdp 	handle->zone_dh_cur = root;
435a1be23daSdp 	handle->zone_dh_top = root;
436a1be23daSdp 	return (Z_OK);
437a1be23daSdp }
438a1be23daSdp 
439ffbafc53Scomay static int
440ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
441ffbafc53Scomay {
442ffbafc53Scomay 	xmlChar *property;
443ffbafc53Scomay 	size_t srcsize;
444ffbafc53Scomay 
445ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
446ffbafc53Scomay 		return (Z_BAD_PROPERTY);
447ffbafc53Scomay 	srcsize = strlcpy(dst, (char *)property, dstsize);
448ffbafc53Scomay 	xmlFree(property);
449ffbafc53Scomay 	if (srcsize >= dstsize)
450ffbafc53Scomay 		return (Z_TOO_BIG);
451ffbafc53Scomay 	return (Z_OK);
452ffbafc53Scomay }
453ffbafc53Scomay 
454ffbafc53Scomay static int
455ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
456ffbafc53Scomay {
457ffbafc53Scomay 	xmlChar *property;
458ffbafc53Scomay 
459ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
460ffbafc53Scomay 		return (Z_BAD_PROPERTY);
461ffbafc53Scomay 	if ((*dst = strdup((char *)property)) == NULL) {
462ffbafc53Scomay 		xmlFree(property);
463ffbafc53Scomay 		return (Z_NOMEM);
464ffbafc53Scomay 	}
465ffbafc53Scomay 	xmlFree(property);
466ffbafc53Scomay 	return (Z_OK);
467ffbafc53Scomay }
468ffbafc53Scomay 
469a1be23daSdp static int
470a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
471a1be23daSdp     char *propval, size_t propsize)
472a1be23daSdp {
473a1be23daSdp 	xmlNodePtr root;
474a1be23daSdp 	int err;
475a1be23daSdp 
476a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
477a1be23daSdp 		return (err);
478a1be23daSdp 
479ffbafc53Scomay 	return (fetchprop(root, propname, propval, propsize));
480ffbafc53Scomay }
481ffbafc53Scomay 
482ffbafc53Scomay static int
483ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
484ffbafc53Scomay     char **propval)
485ffbafc53Scomay {
486ffbafc53Scomay 	xmlNodePtr root;
487ffbafc53Scomay 	int err;
488ffbafc53Scomay 
489ffbafc53Scomay 	if ((err = getroot(handle, &root)) != 0)
490ffbafc53Scomay 		return (err);
491ffbafc53Scomay 
492ffbafc53Scomay 	return (fetch_alloc_prop(root, propname, propval));
493a1be23daSdp }
494a1be23daSdp 
495a1be23daSdp static int
496108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
497108322fbScarlsonj     const char *propval)
498a1be23daSdp {
499a1be23daSdp 	int err;
500a1be23daSdp 	xmlNodePtr root;
501a1be23daSdp 
502a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
503a1be23daSdp 		return (err);
504a1be23daSdp 
5050209230bSgjelinek 	/*
5060209230bSgjelinek 	 * If we get a null propval remove the property (ignore return since it
5070209230bSgjelinek 	 * may not be set to begin with).
5080209230bSgjelinek 	 */
5090209230bSgjelinek 	if (propval == NULL) {
5100209230bSgjelinek 		(void) xmlUnsetProp(root, propname);
5110209230bSgjelinek 	} else {
5120209230bSgjelinek 		if (xmlSetProp(root, propname, (const xmlChar *) propval)
5130209230bSgjelinek 		    == NULL)
5140209230bSgjelinek 			return (Z_INVAL);
5150209230bSgjelinek 	}
5167c478bd9Sstevel@tonic-gate 	return (Z_OK);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
519087719fdSdp static void
520087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
521087719fdSdp {
522087719fdSdp 	xmlNodePtr node;
523087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
524087719fdSdp 
525087719fdSdp 	if (node != NULL)
526087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
527087719fdSdp }
528087719fdSdp 
529087719fdSdp static void
530087719fdSdp stripcomments(zone_dochandle_t handle)
531087719fdSdp {
532087719fdSdp 	xmlDocPtr top;
533087719fdSdp 	xmlNodePtr child, next;
534087719fdSdp 
535087719fdSdp 	top = handle->zone_dh_doc;
536087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
537087719fdSdp 		next = child->next;
538087719fdSdp 		if (child->name == NULL)
539087719fdSdp 			continue;
540087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
541087719fdSdp 			next = child->next;
542087719fdSdp 			xmlUnlinkNode(child);
543087719fdSdp 			xmlFreeNode(child);
544087719fdSdp 		}
545087719fdSdp 	}
546087719fdSdp }
547087719fdSdp 
548ee519a1fSgjelinek static void
549ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle)
550ee519a1fSgjelinek {
551ee519a1fSgjelinek 	xmlNodePtr root, child, next;
552ee519a1fSgjelinek 
553ee519a1fSgjelinek 	root = xmlDocGetRootElement(handle->zone_dh_doc);
554ee519a1fSgjelinek 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
555ee519a1fSgjelinek 		next = child->next;
556ee519a1fSgjelinek 		if (child->name == NULL)
557ee519a1fSgjelinek 			continue;
558ee519a1fSgjelinek 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
559ee519a1fSgjelinek 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
560ee519a1fSgjelinek 			next = child->next;
561ee519a1fSgjelinek 			xmlUnlinkNode(child);
562ee519a1fSgjelinek 			xmlFreeNode(child);
563ee519a1fSgjelinek 		}
564ee519a1fSgjelinek 	}
565ee519a1fSgjelinek }
566ee519a1fSgjelinek 
5677c478bd9Sstevel@tonic-gate static int
568108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
569108322fbScarlsonj     zone_dochandle_t handle)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
5727c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5737c478bd9Sstevel@tonic-gate 	int valid;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
5767c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5779acbbeafSnn 
5787c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
5797c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
5807c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
5817c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
5827c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
5857c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
5867c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
5877c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
5887c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
5897c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
5907c478bd9Sstevel@tonic-gate 	if (valid == 0)
5917c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
592087719fdSdp 
5937c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
594087719fdSdp 	stripcomments(handle);
5957c478bd9Sstevel@tonic-gate 	return (Z_OK);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate int
599108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6027c478bd9Sstevel@tonic-gate 
603108322fbScarlsonj 	if (!config_file_path(zonename, path))
604108322fbScarlsonj 		return (Z_MISC_FS);
605087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
606087719fdSdp 
6077c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
610ee519a1fSgjelinek int
611ee519a1fSgjelinek zonecfg_get_attach_handle(const char *path, const char *zonename,
612ee519a1fSgjelinek     boolean_t preserve_sw, zone_dochandle_t handle)
613ee519a1fSgjelinek {
614ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
615ee519a1fSgjelinek 	int		err;
616ee519a1fSgjelinek 	struct stat	buf;
617ee519a1fSgjelinek 
618ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
619ee519a1fSgjelinek 	    sizeof (migpath))
620ee519a1fSgjelinek 		return (Z_NOMEM);
621ee519a1fSgjelinek 
622ee519a1fSgjelinek 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
623ee519a1fSgjelinek 		return (Z_NO_ZONE);
624ee519a1fSgjelinek 
625ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
626ee519a1fSgjelinek 	    sizeof (migpath))
627ee519a1fSgjelinek 		return (Z_NOMEM);
628ee519a1fSgjelinek 
629ee519a1fSgjelinek 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
630ee519a1fSgjelinek 		return (err);
631ee519a1fSgjelinek 
632ee519a1fSgjelinek 	if (!preserve_sw)
633ee519a1fSgjelinek 		strip_sw_inv(handle);
634ee519a1fSgjelinek 
635ee519a1fSgjelinek 	handle->zone_dh_newzone = B_TRUE;
636ee519a1fSgjelinek 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
637ee519a1fSgjelinek 		return (err);
638ee519a1fSgjelinek 
639ee519a1fSgjelinek 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
640ee519a1fSgjelinek }
641ee519a1fSgjelinek 
6427c478bd9Sstevel@tonic-gate int
643108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
6447c478bd9Sstevel@tonic-gate {
6457c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6467c478bd9Sstevel@tonic-gate 
647108322fbScarlsonj 	if (!snap_file_path(zonename, path))
648108322fbScarlsonj 		return (Z_MISC_FS);
649087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
6507c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
653087719fdSdp int
654108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
655087719fdSdp     zone_dochandle_t handle)
656087719fdSdp {
657087719fdSdp 	char path[MAXPATHLEN];
658087719fdSdp 	int err;
659087719fdSdp 
660108322fbScarlsonj 	if (!config_file_path(template, path))
661108322fbScarlsonj 		return (Z_MISC_FS);
662087719fdSdp 
663087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
664087719fdSdp 		return (err);
665087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
666087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
667087719fdSdp }
668087719fdSdp 
6699acbbeafSnn int
6709acbbeafSnn zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle)
6719acbbeafSnn {
6729acbbeafSnn 	struct stat buf;
6739acbbeafSnn 	int err;
6749acbbeafSnn 
6759acbbeafSnn 	if (stat(path, &buf) == -1)
6769acbbeafSnn 		return (Z_MISC_FS);
6779acbbeafSnn 
6789acbbeafSnn 	if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK)
6799acbbeafSnn 		return (err);
6809acbbeafSnn 	handle->zone_dh_newzone = B_TRUE;
6819acbbeafSnn 	return (Z_OK);
6829acbbeafSnn }
6839acbbeafSnn 
6848cd327d5Sgjelinek /*
6858cd327d5Sgjelinek  * Initialize two handles from the manifest read on fd.  The rem_handle
6868cd327d5Sgjelinek  * is initialized from the input file, including the sw inventory.  The
6878cd327d5Sgjelinek  * local_handle is initialized with the same zone configuration but with
6888cd327d5Sgjelinek  * no sw inventory.
6898cd327d5Sgjelinek  */
6908cd327d5Sgjelinek int
6918cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
6928cd327d5Sgjelinek     zone_dochandle_t rem_handle)
6938cd327d5Sgjelinek {
6948cd327d5Sgjelinek 	xmlValidCtxtPtr cvp;
6958cd327d5Sgjelinek 	int valid;
6968cd327d5Sgjelinek 
6978cd327d5Sgjelinek 	/* load the manifest into the handle for the remote system */
6988cd327d5Sgjelinek 	if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
6998cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7008cd327d5Sgjelinek 	}
7018cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7028cd327d5Sgjelinek 		return (Z_NOMEM);
7038cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7048cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7058cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc);
7068cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7078cd327d5Sgjelinek 	if (valid == 0)
7088cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7098cd327d5Sgjelinek 
7108cd327d5Sgjelinek 	/* delete any comments such as inherited Sun copyright / ident str */
7118cd327d5Sgjelinek 	stripcomments(rem_handle);
7128cd327d5Sgjelinek 
7138cd327d5Sgjelinek 	rem_handle->zone_dh_newzone = B_TRUE;
7148cd327d5Sgjelinek 	rem_handle->zone_dh_sw_inv = B_TRUE;
7158cd327d5Sgjelinek 
7168cd327d5Sgjelinek 	/*
7178cd327d5Sgjelinek 	 * Now use the remote system handle to generate a local system handle
7188cd327d5Sgjelinek 	 * with an identical zones configuration but no sw inventory.
7198cd327d5Sgjelinek 	 */
7208cd327d5Sgjelinek 	if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
7218cd327d5Sgjelinek 	    1)) == NULL) {
7228cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7238cd327d5Sgjelinek 	}
7248cd327d5Sgjelinek 
7258cd327d5Sgjelinek 	/*
7268cd327d5Sgjelinek 	 * We need to re-run xmlValidateDocument on local_handle to properly
7278cd327d5Sgjelinek 	 * update the in-core representation of the configuration.
7288cd327d5Sgjelinek 	 */
7298cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7308cd327d5Sgjelinek 		return (Z_NOMEM);
7318cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7328cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7338cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
7348cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7358cd327d5Sgjelinek 	if (valid == 0)
7368cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7378cd327d5Sgjelinek 
7388cd327d5Sgjelinek 	strip_sw_inv(local_handle);
7398cd327d5Sgjelinek 
7408cd327d5Sgjelinek 	local_handle->zone_dh_newzone = B_TRUE;
7418cd327d5Sgjelinek 	local_handle->zone_dh_sw_inv = B_FALSE;
7428cd327d5Sgjelinek 
7438cd327d5Sgjelinek 	return (Z_OK);
7448cd327d5Sgjelinek }
7458cd327d5Sgjelinek 
746087719fdSdp static boolean_t
747087719fdSdp is_renaming(zone_dochandle_t handle)
748087719fdSdp {
749087719fdSdp 	if (handle->zone_dh_newzone)
750087719fdSdp 		return (B_FALSE);
751087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
752087719fdSdp 		return (B_TRUE);
753087719fdSdp 	return (B_FALSE);
754087719fdSdp }
755087719fdSdp 
756087719fdSdp static boolean_t
757087719fdSdp is_new(zone_dochandle_t handle)
758087719fdSdp {
759087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
760087719fdSdp }
761087719fdSdp 
762087719fdSdp static boolean_t
763087719fdSdp is_snapshot(zone_dochandle_t handle)
764087719fdSdp {
765087719fdSdp 	return (handle->zone_dh_snapshot);
766087719fdSdp }
767087719fdSdp 
768087719fdSdp /*
769087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
770087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
771087719fdSdp  * safety if this routine had to change the app locale on the fly.
772087719fdSdp  */
773087719fdSdp int
774108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
775087719fdSdp {
776087719fdSdp 	int i;
777087719fdSdp 
778087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
779087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
780087719fdSdp 
781087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
782087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
783087719fdSdp 
784087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
785087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
786087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
787087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
788087719fdSdp 
789087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
790087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
791087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
792087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
793087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
794087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
795087719fdSdp 	}
796087719fdSdp 
797087719fdSdp 	return (Z_OK);
798087719fdSdp }
799087719fdSdp 
800087719fdSdp /*
801087719fdSdp  * Changing the zone name requires us to track both the old and new
802087719fdSdp  * name of the zone until commit time.
803087719fdSdp  */
8047c478bd9Sstevel@tonic-gate int
8057c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
8067c478bd9Sstevel@tonic-gate {
807a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
808a1be23daSdp }
8097c478bd9Sstevel@tonic-gate 
810a1be23daSdp int
811a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
812a1be23daSdp {
813087719fdSdp 	zone_state_t state;
814087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
815087719fdSdp 	int err;
816087719fdSdp 
817087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
818087719fdSdp 	    sizeof (curname))) != Z_OK)
819087719fdSdp 		return (err);
820087719fdSdp 
821087719fdSdp 	if (strcmp(name, curname) == 0)
822087719fdSdp 		return (Z_OK);
823087719fdSdp 
824087719fdSdp 	/*
825087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
826087719fdSdp 	 */
827087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
828087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
829087719fdSdp 
830087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
831087719fdSdp 		return (err);
832087719fdSdp 
833087719fdSdp 	/*
834087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
835087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
836087719fdSdp 	 * name as "in use," so special case here.
837087719fdSdp 	 */
838087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
839087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
840087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
841087719fdSdp 		return (err);
842087719fdSdp 	}
843087719fdSdp 
844087719fdSdp 	/* Check to see if new name chosen is already in use */
845087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
846087719fdSdp 		return (Z_NAME_IN_USE);
847087719fdSdp 
848087719fdSdp 	/*
849087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
850087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
851087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
852087719fdSdp 	 */
853087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
854087719fdSdp 	    sizeof (old_delname));
855087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
856087719fdSdp 		/*
857087719fdSdp 		 * Name change is allowed only when the zone we're altering
858087719fdSdp 		 * is not ready or running.
859087719fdSdp 		 */
860087719fdSdp 		err = zone_get_state(curname, &state);
861087719fdSdp 		if (err == Z_OK) {
862087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
863087719fdSdp 				return (Z_BAD_ZONE_STATE);
864087719fdSdp 		} else if (err != Z_NO_ZONE) {
865087719fdSdp 			return (err);
866087719fdSdp 		}
867087719fdSdp 
868087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
869087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
870087719fdSdp 		assert(is_renaming(handle));
871087719fdSdp 	} else if (is_renaming(handle)) {
872087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
873087719fdSdp 		if (err == Z_OK) {
874087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
875087719fdSdp 				return (Z_BAD_ZONE_STATE);
876087719fdSdp 		} else if (err != Z_NO_ZONE) {
877087719fdSdp 			return (err);
878087719fdSdp 		}
879087719fdSdp 	}
880087719fdSdp 
881087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
882087719fdSdp 		/*
883087719fdSdp 		 * Restore the deletename to whatever it was at the
884087719fdSdp 		 * top of the routine, since we've had a failure.
885087719fdSdp 		 */
886087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
887087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
888087719fdSdp 		return (err);
889087719fdSdp 	}
890087719fdSdp 
891087719fdSdp 	return (Z_OK);
892a1be23daSdp }
8937c478bd9Sstevel@tonic-gate 
894a1be23daSdp int
895a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
896a1be23daSdp {
897108322fbScarlsonj 	size_t len;
898108322fbScarlsonj 
899108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
900108322fbScarlsonj 		return (Z_TOO_BIG);
901108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
902108322fbScarlsonj 	    pathsize - len));
903a1be23daSdp }
9047c478bd9Sstevel@tonic-gate 
905a1be23daSdp int
906a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
907a1be23daSdp {
908555afedfScarlsonj 	size_t len;
909555afedfScarlsonj 
910555afedfScarlsonj 	/*
911555afedfScarlsonj 	 * The user deals in absolute paths in the running global zone, but the
912555afedfScarlsonj 	 * internal configuration files deal with boot environment relative
913555afedfScarlsonj 	 * paths.  Strip out the alternate root when specified.
914555afedfScarlsonj 	 */
915555afedfScarlsonj 	len = strlen(zonecfg_root);
916555afedfScarlsonj 	if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
917555afedfScarlsonj 		return (Z_BAD_PROPERTY);
918555afedfScarlsonj 	zonepath += len;
919a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
920a1be23daSdp }
921a1be23daSdp 
9229acbbeafSnn int
9239acbbeafSnn zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
9249acbbeafSnn {
9259acbbeafSnn 	int ret, sz;
9269acbbeafSnn 
9279acbbeafSnn 	ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
9289acbbeafSnn 
9299acbbeafSnn 	/* If the zone has no brand, it is native. */
9309acbbeafSnn 	if (ret == Z_OK && brand[0] == '\0') {
9319acbbeafSnn 		sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
9329acbbeafSnn 		if (sz >= brandsize)
9339acbbeafSnn 			ret = Z_TOO_BIG;
9349acbbeafSnn 		else
9359acbbeafSnn 			ret = Z_OK;
9369acbbeafSnn 	}
9379acbbeafSnn 
9389acbbeafSnn 	return (ret);
9399acbbeafSnn }
9409acbbeafSnn 
9419acbbeafSnn int
9429acbbeafSnn zonecfg_set_brand(zone_dochandle_t handle, char *brand)
9439acbbeafSnn {
9449acbbeafSnn 	return (setrootattr(handle, DTD_ATTR_BRAND, brand));
9459acbbeafSnn }
9469acbbeafSnn 
947a1be23daSdp int
948a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
949a1be23daSdp {
950a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
951a1be23daSdp 	int ret;
952a1be23daSdp 
953a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
954a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
955a1be23daSdp 		return (ret);
956a1be23daSdp 
957a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
958a1be23daSdp 		*autoboot = B_TRUE;
959a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
960a1be23daSdp 		*autoboot = B_FALSE;
961a1be23daSdp 	else
962a1be23daSdp 		ret = Z_BAD_PROPERTY;
963a1be23daSdp 	return (ret);
964a1be23daSdp }
965a1be23daSdp 
966a1be23daSdp int
967a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
968a1be23daSdp {
969a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
970a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
971a1be23daSdp }
972a1be23daSdp 
973a1be23daSdp int
974a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
975a1be23daSdp {
976a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
977a1be23daSdp }
978a1be23daSdp 
979a1be23daSdp int
980a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
981a1be23daSdp {
982a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
983a1be23daSdp }
984a1be23daSdp 
985ffbafc53Scomay int
986ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
987ffbafc53Scomay {
988ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
989ffbafc53Scomay }
990ffbafc53Scomay 
991ffbafc53Scomay int
9923f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
993ffbafc53Scomay {
9943f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
9953f2f09c1Sdp }
9963f2f09c1Sdp 
9973f2f09c1Sdp int
9983f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
9993f2f09c1Sdp {
10003f2f09c1Sdp 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
10013f2f09c1Sdp }
10023f2f09c1Sdp 
10033f2f09c1Sdp int
10043f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
10053f2f09c1Sdp {
10063f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
1007ffbafc53Scomay }
1008ffbafc53Scomay 
10090209230bSgjelinek int
10100209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
10110209230bSgjelinek {
10120209230bSgjelinek 	return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
10130209230bSgjelinek }
10140209230bSgjelinek 
10150209230bSgjelinek int
10160209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched)
10170209230bSgjelinek {
10180209230bSgjelinek 	return (setrootattr(handle, DTD_ATTR_SCHED, sched));
10190209230bSgjelinek }
10200209230bSgjelinek 
1021a1be23daSdp /*
1022a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
1023a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
1024a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
1025a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
1026087719fdSdp  *
1027087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
1028087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
1029087719fdSdp  * In general, the operation of this function should succeed or fail as
1030087719fdSdp  * a unit.
1031a1be23daSdp  */
1032a1be23daSdp int
1033a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
1034a1be23daSdp {
1035a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
1036a1be23daSdp 	struct zoneent ze;
1037a1be23daSdp 	int err;
1038087719fdSdp 	int opcode;
1039087719fdSdp 	char *zn;
1040087719fdSdp 
1041087719fdSdp 	bzero(&ze, sizeof (ze));
1042087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
1043a1be23daSdp 
1044a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
1045a1be23daSdp 		return (err);
1046087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
1047087719fdSdp 
1048a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
1049a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
1050a1be23daSdp 		return (err);
1051555afedfScarlsonj 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
1052555afedfScarlsonj 	    sizeof (ze.zone_path));
1053087719fdSdp 
1054087719fdSdp 	if (is_renaming(handle)) {
1055087719fdSdp 		opcode = PZE_MODIFY;
1056087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
1057087719fdSdp 		    sizeof (ze.zone_name));
1058087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
1059087719fdSdp 	} else if (is_new(handle)) {
1060087719fdSdp 		FILE *cookie;
1061087719fdSdp 		/*
1062087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
1063087719fdSdp 		 * since we might be forcibly overwriting an existing
1064087719fdSdp 		 * configuration with a new one (for example 'create -F'
1065087719fdSdp 		 * in zonecfg).
1066087719fdSdp 		 */
1067087719fdSdp 		opcode = PZE_ADD;
1068087719fdSdp 		cookie = setzoneent();
1069087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
1070087719fdSdp 			if (strcmp(zn, name) == 0) {
1071087719fdSdp 				opcode = PZE_MODIFY;
1072087719fdSdp 				free(zn);
1073087719fdSdp 				break;
1074087719fdSdp 			}
1075087719fdSdp 			free(zn);
1076087719fdSdp 		}
1077087719fdSdp 		endzoneent(cookie);
1078087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
1079087719fdSdp 	} else {
1080087719fdSdp 		opcode = PZE_MODIFY;
1081087719fdSdp 	}
1082087719fdSdp 
1083087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
1084087719fdSdp 		return (err);
1085087719fdSdp 
1086087719fdSdp 	return (Z_OK);
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate 
1089087719fdSdp /*
1090087719fdSdp  * The goal of this routine is to cause the index file update and the
1091087719fdSdp  * document save to happen as an atomic operation.  We do the document
1092087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
1093087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
1094087719fdSdp  *
1095087719fdSdp  * Strategy:
1096087719fdSdp  *
1097087719fdSdp  * New zone 'foo' configuration:
1098087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1099087719fdSdp  * 	Write XML to tmpfile
1100087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1101087719fdSdp  * 	Add entry to index file
1102087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
1103087719fdSdp  *
1104087719fdSdp  * Save existing zone 'foo':
1105087719fdSdp  * 	Make backup of foo.xml -> .backup
1106087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1107087719fdSdp  * 	Write XML to tmpfile
1108087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1109087719fdSdp  * 	Modify index file as needed
1110087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
1111087719fdSdp  *
1112087719fdSdp  * Rename 'foo' to 'bar':
1113087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1114087719fdSdp  * 	Write XML to tmpfile
1115087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1116087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1117087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
1118087719fdSdp  */
11197c478bd9Sstevel@tonic-gate static int
11207c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
11217c478bd9Sstevel@tonic-gate {
11227c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
1123087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
11249acbbeafSnn 	int tmpfd, err, valid;
11257c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
1126087719fdSdp 	boolean_t backup;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
11297c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
11307c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
11337c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
11347c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
11357c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
11407c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
11417c478bd9Sstevel@tonic-gate 
1142087719fdSdp 	/*
11439acbbeafSnn 	 * We do a final validation of the document.  Since the library has
11449acbbeafSnn 	 * malfunctioned if it fails to validate, we follow-up with an
11459acbbeafSnn 	 * assert() that the doc is valid.
1146087719fdSdp 	 */
11479acbbeafSnn 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
11489acbbeafSnn 	assert(valid != 0);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
11517c478bd9Sstevel@tonic-gate 		goto err;
1152087719fdSdp 
11537c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
11547c478bd9Sstevel@tonic-gate 
1155087719fdSdp 	/*
1156087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
1157087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
1158087719fdSdp 	 * something goes wrong.
1159087719fdSdp 	 */
1160087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
1161087719fdSdp 		backup = B_TRUE;
1162087719fdSdp 
1163087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1164087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1165087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1166087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
1167087719fdSdp 
1168087719fdSdp 		if (link(filename, bakfile) == -1) {
1169087719fdSdp 			err = errno;
1170087719fdSdp 			(void) unlink(tmpfile);
1171087719fdSdp 			if (errno == EACCES)
1172087719fdSdp 				return (Z_ACCES);
1173087719fdSdp 			return (Z_MISC_FS);
1174087719fdSdp 		}
1175087719fdSdp 	}
1176087719fdSdp 
1177087719fdSdp 	/*
1178087719fdSdp 	 * Move the new document over top of the old.
1179087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1180087719fdSdp 	 */
11817c478bd9Sstevel@tonic-gate 	if (rename(tmpfile, filename) == -1) {
1182087719fdSdp 		err = errno;
11837c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
1184087719fdSdp 		if (backup)
1185087719fdSdp 			(void) unlink(bakfile);
1186087719fdSdp 		if (err == EACCES)
11877c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
11887c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
11897c478bd9Sstevel@tonic-gate 	}
1190a1be23daSdp 
1191087719fdSdp 	/*
1192087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
1193087719fdSdp 	 */
1194087719fdSdp 	if (is_snapshot(handle))
1195087719fdSdp 		return (Z_OK);
1196087719fdSdp 
1197087719fdSdp 	/* now update the index file to reflect whatever we just did */
1198087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1199087719fdSdp 		if (backup) {
1200087719fdSdp 			/*
1201087719fdSdp 			 * Try to restore from our backup.
1202087719fdSdp 			 */
1203087719fdSdp 			(void) unlink(filename);
1204087719fdSdp 			(void) rename(bakfile, filename);
1205087719fdSdp 		} else {
1206087719fdSdp 			/*
1207087719fdSdp 			 * Either the zone is new, in which case we can delete
1208087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1209087719fdSdp 			 */
1210087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1211087719fdSdp 			(void) unlink(filename);
1212087719fdSdp 		}
1213087719fdSdp 		return (Z_UPDATING_INDEX);
1214087719fdSdp 	}
1215087719fdSdp 
1216087719fdSdp 	if (backup)
1217087719fdSdp 		(void) unlink(bakfile);
1218087719fdSdp 
1219087719fdSdp 	return (Z_OK);
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate err:
12227c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
12237c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate int
12277c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
12287c478bd9Sstevel@tonic-gate {
1229087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1230087719fdSdp 	char delpath[MAXPATHLEN];
1231087719fdSdp 	int err = Z_SAVING_FILE;
1232087719fdSdp 
1233087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1234087719fdSdp 		return (Z_BAD_HANDLE);
12357c478bd9Sstevel@tonic-gate 
1236087719fdSdp 	/*
1237ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1238ee519a1fSgjelinek 	 * inventory at this time.
1239087719fdSdp 	 */
1240ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1241087719fdSdp 		return (Z_INVAL);
1242087719fdSdp 
1243087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
12447c478bd9Sstevel@tonic-gate 		return (err);
1245087719fdSdp 
1246108322fbScarlsonj 	if (!config_file_path(zname, path))
1247108322fbScarlsonj 		return (Z_MISC_FS);
1248087719fdSdp 
1249087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1250087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1251087719fdSdp 
1252087719fdSdp 	err = zonecfg_save_impl(handle, path);
1253087719fdSdp 
1254087719fdSdp 	stripcomments(handle);
1255087719fdSdp 
1256087719fdSdp 	if (err != Z_OK)
1257087719fdSdp 		return (err);
1258087719fdSdp 
1259087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1260087719fdSdp 
1261087719fdSdp 	if (is_renaming(handle)) {
1262108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1263108322fbScarlsonj 			(void) unlink(delpath);
1264087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1265087719fdSdp 	}
1266087719fdSdp 
1267087719fdSdp 	return (Z_OK);
12687c478bd9Sstevel@tonic-gate }
12697c478bd9Sstevel@tonic-gate 
12709acbbeafSnn int
12719acbbeafSnn zonecfg_verify_save(zone_dochandle_t handle, char *filename)
12729acbbeafSnn {
12739acbbeafSnn 	int valid;
12749acbbeafSnn 
12759acbbeafSnn 	xmlValidCtxt cvp = { NULL };
12769acbbeafSnn 
12779acbbeafSnn 	if (zonecfg_check_handle(handle) != Z_OK)
12789acbbeafSnn 		return (Z_BAD_HANDLE);
12799acbbeafSnn 
12809acbbeafSnn 	cvp.error = zonecfg_error_func;
12819acbbeafSnn 	cvp.warning = zonecfg_error_func;
12829acbbeafSnn 
12839acbbeafSnn 	/*
12849acbbeafSnn 	 * We do a final validation of the document.  Since the library has
12859acbbeafSnn 	 * malfunctioned if it fails to validate, we follow-up with an
12869acbbeafSnn 	 * assert() that the doc is valid.
12879acbbeafSnn 	 */
12889acbbeafSnn 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
12899acbbeafSnn 	assert(valid != 0);
12909acbbeafSnn 
12919acbbeafSnn 	if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
12929acbbeafSnn 		return (Z_SAVING_FILE);
12939acbbeafSnn 
12949acbbeafSnn 	return (Z_OK);
12959acbbeafSnn }
12969acbbeafSnn 
1297ee519a1fSgjelinek int
12988cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1299ee519a1fSgjelinek {
1300ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1301ee519a1fSgjelinek 	char path[MAXPATHLEN];
1302ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1303ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1304ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
13059acbbeafSnn 	int valid;
1306ee519a1fSgjelinek 
1307ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1308ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1309ee519a1fSgjelinek 
1310ee519a1fSgjelinek 	/*
1311ee519a1fSgjelinek 	 * We can only detach if we have taken a sw inventory.
1312ee519a1fSgjelinek 	 */
1313ee519a1fSgjelinek 	if (!handle->zone_dh_sw_inv)
1314ee519a1fSgjelinek 		return (Z_INVAL);
1315ee519a1fSgjelinek 
13168cd327d5Sgjelinek 	if (flags & ZONE_DRY_RUN) {
13178cd327d5Sgjelinek 		(void) strlcpy(migpath, "-", sizeof (migpath));
13188cd327d5Sgjelinek 	} else {
13198cd327d5Sgjelinek 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
13208cd327d5Sgjelinek 		    != Z_OK)
13218cd327d5Sgjelinek 			return (err);
1322ee519a1fSgjelinek 
13238cd327d5Sgjelinek 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
13248cd327d5Sgjelinek 		    != Z_OK)
13258cd327d5Sgjelinek 			return (err);
1326ee519a1fSgjelinek 
13278cd327d5Sgjelinek 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED)
13288cd327d5Sgjelinek 		    >= sizeof (migpath))
13298cd327d5Sgjelinek 			return (Z_NOMEM);
13308cd327d5Sgjelinek 	}
1331ee519a1fSgjelinek 
1332ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1333ee519a1fSgjelinek 		return (err);
1334ee519a1fSgjelinek 
1335ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1336ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1337ee519a1fSgjelinek 
1338ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1339ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1340ee519a1fSgjelinek 
1341ee519a1fSgjelinek 	/*
13429acbbeafSnn 	 * We do a final validation of the document.  Since the library has
13439acbbeafSnn 	 * malfunctioned if it fails to validate, we follow-up with an
13449acbbeafSnn 	 * assert() that the doc is valid.
1345ee519a1fSgjelinek 	 */
13469acbbeafSnn 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13479acbbeafSnn 	assert(valid != 0);
1348ee519a1fSgjelinek 
1349ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1350ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1351ee519a1fSgjelinek 
13528cd327d5Sgjelinek 	if (!(flags & ZONE_DRY_RUN))
13538cd327d5Sgjelinek 		(void) chmod(migpath, 0644);
1354ee519a1fSgjelinek 
1355ee519a1fSgjelinek 	stripcomments(handle);
1356ee519a1fSgjelinek 
1357ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1358ee519a1fSgjelinek 
1359ee519a1fSgjelinek 	return (Z_OK);
1360ee519a1fSgjelinek }
1361ee519a1fSgjelinek 
1362ee519a1fSgjelinek boolean_t
1363ee519a1fSgjelinek zonecfg_detached(const char *path)
1364ee519a1fSgjelinek {
1365ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1366ee519a1fSgjelinek 	struct stat	buf;
1367ee519a1fSgjelinek 
1368ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, DETACHED) >=
1369ee519a1fSgjelinek 	    sizeof (migpath))
1370ee519a1fSgjelinek 		return (B_FALSE);
1371ee519a1fSgjelinek 
1372ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1373ee519a1fSgjelinek 		return (B_TRUE);
1374ee519a1fSgjelinek 
1375ee519a1fSgjelinek 	return (B_FALSE);
1376ee519a1fSgjelinek }
1377ee519a1fSgjelinek 
1378ee519a1fSgjelinek void
1379ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1380ee519a1fSgjelinek {
1381ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1382ee519a1fSgjelinek 	char path[MAXPATHLEN];
1383ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1384ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1385ee519a1fSgjelinek 
1386ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1387ee519a1fSgjelinek 		return;
1388ee519a1fSgjelinek 
1389ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1390ee519a1fSgjelinek 		return;
1391ee519a1fSgjelinek 
1392ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1393ee519a1fSgjelinek 		return;
1394ee519a1fSgjelinek 
1395ee519a1fSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path, DETACHED);
1396ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1397ee519a1fSgjelinek 	    ATTACH_FORCED);
1398ee519a1fSgjelinek 
1399ee519a1fSgjelinek 	if (forced) {
1400ee519a1fSgjelinek 		(void) rename(detached, attached);
1401ee519a1fSgjelinek 	} else {
1402ee519a1fSgjelinek 		(void) unlink(attached);
1403ee519a1fSgjelinek 		(void) unlink(detached);
1404ee519a1fSgjelinek 	}
1405ee519a1fSgjelinek }
1406ee519a1fSgjelinek 
14077c478bd9Sstevel@tonic-gate /*
14087c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
14097c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
14107c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
14117c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
14127c478bd9Sstevel@tonic-gate  */
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate int
14157c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
14167c478bd9Sstevel@tonic-gate {
14177c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
14187c478bd9Sstevel@tonic-gate 
1419108322fbScarlsonj 	if (!config_file_path(zonename, path))
1420108322fbScarlsonj 		return (Z_INVAL);
14217c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
14227c478bd9Sstevel@tonic-gate 		return (Z_OK);
1423108322fbScarlsonj 	if (errno == ENOENT) {
1424108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1425108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1426108322fbScarlsonj 			return (Z_INVAL);
1427108322fbScarlsonj 		if (access(path, amode) == 0)
1428108322fbScarlsonj 			return (Z_OK);
1429108322fbScarlsonj 	}
14307c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
14317c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
14327c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
14337c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14347c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
14357c478bd9Sstevel@tonic-gate }
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate int
1438108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
14397c478bd9Sstevel@tonic-gate {
14407c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
14417c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
14427c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
14457c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 
1448087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1449087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1450087719fdSdp 
14517c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
14527c478bd9Sstevel@tonic-gate 		goto out;
14537c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
14547c478bd9Sstevel@tonic-gate 		goto out;
14557c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
14567c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
14577c478bd9Sstevel@tonic-gate 		goto out;
14587c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
14597c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
14607c478bd9Sstevel@tonic-gate 		goto out;
14617c478bd9Sstevel@tonic-gate 	}
14627c478bd9Sstevel@tonic-gate 	/*
14637c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
14647c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
14657c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
14667c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
14677c478bd9Sstevel@tonic-gate 	 * paths.
14687c478bd9Sstevel@tonic-gate 	 */
14697c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
14707c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
14717c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
14727c478bd9Sstevel@tonic-gate 			goto out;
14737c478bd9Sstevel@tonic-gate 	}
1474108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1475108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1476108322fbScarlsonj 		error = Z_MISC_FS;
1477108322fbScarlsonj 		goto out;
1478108322fbScarlsonj 	}
1479108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
14807c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
14817c478bd9Sstevel@tonic-gate 		goto out;
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 
1484108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1485108322fbScarlsonj 		error = Z_MISC_FS;
1486108322fbScarlsonj 		goto out;
1487108322fbScarlsonj 	}
1488087719fdSdp 
1489087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1490087719fdSdp 	    "It is a snapshot of running zone state.\n");
1491087719fdSdp 
14927c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
14937c478bd9Sstevel@tonic-gate 
1494087719fdSdp 	stripcomments(handle);
1495087719fdSdp 
14967c478bd9Sstevel@tonic-gate out:
14977c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
14987c478bd9Sstevel@tonic-gate 	return (error);
14997c478bd9Sstevel@tonic-gate }
15007c478bd9Sstevel@tonic-gate 
1501*f4b3ec61Sdh int
1502*f4b3ec61Sdh zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep)
1503*f4b3ec61Sdh {
1504*f4b3ec61Sdh 	char property[10]; /* 10 is big enough for "shared"/"exclusive" */
1505*f4b3ec61Sdh 	int err;
1506*f4b3ec61Sdh 
1507*f4b3ec61Sdh 	err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property));
1508*f4b3ec61Sdh 	if (err == Z_BAD_PROPERTY) {
1509*f4b3ec61Sdh 		/* Return default value */
1510*f4b3ec61Sdh 		*iptypep = ZS_SHARED;
1511*f4b3ec61Sdh 		return (Z_OK);
1512*f4b3ec61Sdh 	} else if (err != Z_OK) {
1513*f4b3ec61Sdh 		return (err);
1514*f4b3ec61Sdh 	}
1515*f4b3ec61Sdh 
1516*f4b3ec61Sdh 	if (strlen(property) == 0 ||
1517*f4b3ec61Sdh 	    strcmp(property, "shared") == 0)
1518*f4b3ec61Sdh 		*iptypep = ZS_SHARED;
1519*f4b3ec61Sdh 	else if (strcmp(property, "exclusive") == 0)
1520*f4b3ec61Sdh 		*iptypep = ZS_EXCLUSIVE;
1521*f4b3ec61Sdh 	else
1522*f4b3ec61Sdh 		return (Z_INVAL);
1523*f4b3ec61Sdh 
1524*f4b3ec61Sdh 	return (Z_OK);
1525*f4b3ec61Sdh }
1526*f4b3ec61Sdh 
1527*f4b3ec61Sdh int
1528*f4b3ec61Sdh zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype)
1529*f4b3ec61Sdh {
1530*f4b3ec61Sdh 	xmlNodePtr cur;
1531*f4b3ec61Sdh 
1532*f4b3ec61Sdh 	if (handle == NULL)
1533*f4b3ec61Sdh 		return (Z_INVAL);
1534*f4b3ec61Sdh 
1535*f4b3ec61Sdh 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
1536*f4b3ec61Sdh 	if (cur == NULL) {
1537*f4b3ec61Sdh 		return (Z_EMPTY_DOCUMENT);
1538*f4b3ec61Sdh 	}
1539*f4b3ec61Sdh 
1540*f4b3ec61Sdh 	if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) {
1541*f4b3ec61Sdh 		return (Z_WRONG_DOC_TYPE);
1542*f4b3ec61Sdh 	}
1543*f4b3ec61Sdh 	switch (iptype) {
1544*f4b3ec61Sdh 	case ZS_SHARED:
1545*f4b3ec61Sdh 		/*
1546*f4b3ec61Sdh 		 * Since "shared" is the default, we don't write it to the
1547*f4b3ec61Sdh 		 * configuration file, so that it's easier to migrate those
1548*f4b3ec61Sdh 		 * zones elsewhere, eg., to systems which are not IP-Instances
1549*f4b3ec61Sdh 		 * aware.
1550*f4b3ec61Sdh 		 * xmlUnsetProp only fails when the attribute doesn't exist,
1551*f4b3ec61Sdh 		 * which we don't care.
1552*f4b3ec61Sdh 		 */
1553*f4b3ec61Sdh 		(void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE);
1554*f4b3ec61Sdh 		break;
1555*f4b3ec61Sdh 	case ZS_EXCLUSIVE:
1556*f4b3ec61Sdh 		if (xmlSetProp(cur, DTD_ATTR_IPTYPE,
1557*f4b3ec61Sdh 		    (const xmlChar *) "exclusive") == NULL)
1558*f4b3ec61Sdh 			return (Z_INVAL);
1559*f4b3ec61Sdh 		break;
1560*f4b3ec61Sdh 	}
1561*f4b3ec61Sdh 	return (Z_OK);
1562*f4b3ec61Sdh }
1563*f4b3ec61Sdh 
15647c478bd9Sstevel@tonic-gate static int
1565a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
15667c478bd9Sstevel@tonic-gate {
15677c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
15707c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
15717c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
15727c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
15737c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
15747c478bd9Sstevel@tonic-gate 	}
15757c478bd9Sstevel@tonic-gate 	return (Z_OK);
15767c478bd9Sstevel@tonic-gate }
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate static int
15797c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
15807c478bd9Sstevel@tonic-gate {
15817c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
15827c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
15837c478bd9Sstevel@tonic-gate 	int err;
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1586a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
15877c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
15887c478bd9Sstevel@tonic-gate 		return (err);
15897c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1590a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
15917c478bd9Sstevel@tonic-gate 		return (err);
1592a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
15937c478bd9Sstevel@tonic-gate 		return (err);
1594a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
15957c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
15967c478bd9Sstevel@tonic-gate 		return (err);
15977c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
15987c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
15997c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
16007c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
16017c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1602a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
16037c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
16047c478bd9Sstevel@tonic-gate 				return (err);
16057c478bd9Sstevel@tonic-gate 		}
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 	return (Z_OK);
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate int
16117c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	int err;
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16167c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16197c478bd9Sstevel@tonic-gate 		return (err);
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
16227c478bd9Sstevel@tonic-gate 		return (err);
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	return (Z_OK);
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate static int
16287c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
16297c478bd9Sstevel@tonic-gate {
16307c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
16317c478bd9Sstevel@tonic-gate 	int err;
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1634a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16357c478bd9Sstevel@tonic-gate 		return (err);
16367c478bd9Sstevel@tonic-gate 	return (Z_OK);
16377c478bd9Sstevel@tonic-gate }
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate int
16407c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
16417c478bd9Sstevel@tonic-gate {
16427c478bd9Sstevel@tonic-gate 	int err;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16457c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16487c478bd9Sstevel@tonic-gate 		return (err);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
16517c478bd9Sstevel@tonic-gate 		return (err);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	return (Z_OK);
16547c478bd9Sstevel@tonic-gate }
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate int
16577c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
16587c478bd9Sstevel@tonic-gate {
16597c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16627c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
16637c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
16647c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
16657c478bd9Sstevel@tonic-gate 	if (new == NULL)
16667c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
16677c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
16687c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
16697c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
16707c478bd9Sstevel@tonic-gate 	if (last == NULL)
16717c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
16727c478bd9Sstevel@tonic-gate 	else
16737c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
16747c478bd9Sstevel@tonic-gate 	return (Z_OK);
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate int
16787c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
16797c478bd9Sstevel@tonic-gate {
16807c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16837c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
16847c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
16857c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
16867c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
16877c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
16887c478bd9Sstevel@tonic-gate 			else
16897c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
16907c478bd9Sstevel@tonic-gate 			free(this);
16917c478bd9Sstevel@tonic-gate 			return (Z_OK);
16927c478bd9Sstevel@tonic-gate 		} else
16937c478bd9Sstevel@tonic-gate 			last = this;
16947c478bd9Sstevel@tonic-gate 	}
16957c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate void
16997c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
17007c478bd9Sstevel@tonic-gate {
17017c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
17047c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
17057c478bd9Sstevel@tonic-gate 		free(this);
17067c478bd9Sstevel@tonic-gate 	}
17077c478bd9Sstevel@tonic-gate }
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate void
17107c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
17117c478bd9Sstevel@tonic-gate {
17127c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
17137c478bd9Sstevel@tonic-gate 		return;
17147c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
17157c478bd9Sstevel@tonic-gate 	free(valtab);
17167c478bd9Sstevel@tonic-gate }
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate static boolean_t
17197c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
17207c478bd9Sstevel@tonic-gate {
17217c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
17227c478bd9Sstevel@tonic-gate 	int prop_result;
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
17257c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
17267c478bd9Sstevel@tonic-gate 		return (B_FALSE);
17277c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
17287c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
17297c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate static int
17337c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
17347c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
17357c478bd9Sstevel@tonic-gate {
17367c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17377c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17407c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
17417c478bd9Sstevel@tonic-gate 			continue;
17427c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
17437c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
17447c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
17457c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
17467c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
17477c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
17487c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
17497c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
17507c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17517c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17527c478bd9Sstevel@tonic-gate 			return (Z_OK);
17537c478bd9Sstevel@tonic-gate 		}
17547c478bd9Sstevel@tonic-gate 	}
17557c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
17567c478bd9Sstevel@tonic-gate }
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate int
17597c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
17607c478bd9Sstevel@tonic-gate {
17617c478bd9Sstevel@tonic-gate 	int err;
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17647c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17677c478bd9Sstevel@tonic-gate 		return (err);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
17707c478bd9Sstevel@tonic-gate 		return (err);
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	return (Z_OK);
17737c478bd9Sstevel@tonic-gate }
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate int
17767c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
17777c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
17787c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
17797c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
17807c478bd9Sstevel@tonic-gate {
17817c478bd9Sstevel@tonic-gate 	int err;
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
17847c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17877c478bd9Sstevel@tonic-gate 		return (err);
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
17907c478bd9Sstevel@tonic-gate 		return (err);
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
17937c478bd9Sstevel@tonic-gate 		return (err);
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	return (Z_OK);
17967c478bd9Sstevel@tonic-gate }
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate static int
17997c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
18007c478bd9Sstevel@tonic-gate {
18017c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18047c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
18057c478bd9Sstevel@tonic-gate 			continue;
18067c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
18077c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
18087c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
18097c478bd9Sstevel@tonic-gate 			return (Z_OK);
18107c478bd9Sstevel@tonic-gate 		}
18117c478bd9Sstevel@tonic-gate 	}
18127c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate int
18167c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
18177c478bd9Sstevel@tonic-gate {
18187c478bd9Sstevel@tonic-gate 	int err;
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18217c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18247c478bd9Sstevel@tonic-gate 		return (err);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
18277c478bd9Sstevel@tonic-gate 		return (err);
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	return (Z_OK);
18307c478bd9Sstevel@tonic-gate }
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate int
18337c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
18347c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
18357c478bd9Sstevel@tonic-gate {
18367c478bd9Sstevel@tonic-gate 	int err;
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18397c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18427c478bd9Sstevel@tonic-gate 		return (err);
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
18457c478bd9Sstevel@tonic-gate 		return (err);
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
18487c478bd9Sstevel@tonic-gate 		return (err);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	return (Z_OK);
18517c478bd9Sstevel@tonic-gate }
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate int
18547c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
18557c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
18567c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
18577c478bd9Sstevel@tonic-gate {
18587c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
18597c478bd9Sstevel@tonic-gate 	int err;
18607c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
18617c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
18627c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18657c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18687c478bd9Sstevel@tonic-gate 		return (err);
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	/*
18717c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
18727c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
18737c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
18747c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
18757c478bd9Sstevel@tonic-gate 	 */
18767c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
18777c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
18787c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18797c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
18807c478bd9Sstevel@tonic-gate 			continue;
18817c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
18827c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
18837c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
18847c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
18857c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
18867c478bd9Sstevel@tonic-gate 					firstmatch = cur;
18877c478bd9Sstevel@tonic-gate 				else
18887c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
18897c478bd9Sstevel@tonic-gate 			}
18907c478bd9Sstevel@tonic-gate 		}
18917c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
18927c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
18937c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
18947c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
18957c478bd9Sstevel@tonic-gate 				    special) == 0) {
18967c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
18977c478bd9Sstevel@tonic-gate 						firstmatch = cur;
18987c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
18997c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19007c478bd9Sstevel@tonic-gate 				} else {
19017c478bd9Sstevel@tonic-gate 					/*
19027c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19037c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19047c478bd9Sstevel@tonic-gate 					 */
19057c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19067c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19077c478bd9Sstevel@tonic-gate 				}
19087c478bd9Sstevel@tonic-gate 			}
19097c478bd9Sstevel@tonic-gate 		}
19107c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
19117c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
19127c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
19137c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
19147c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19157c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19167c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19177c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19187c478bd9Sstevel@tonic-gate 				} else {
19197c478bd9Sstevel@tonic-gate 					/*
19207c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19217c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19227c478bd9Sstevel@tonic-gate 					 */
19237c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19247c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19257c478bd9Sstevel@tonic-gate 				}
19267c478bd9Sstevel@tonic-gate 			}
19277c478bd9Sstevel@tonic-gate 		}
19287c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
19297c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
19307c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
19317c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
19327c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19337c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19347c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19357c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19367c478bd9Sstevel@tonic-gate 				} else {
19377c478bd9Sstevel@tonic-gate 					/*
19387c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19397c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19407c478bd9Sstevel@tonic-gate 					 */
19417c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19427c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19437c478bd9Sstevel@tonic-gate 				}
19447c478bd9Sstevel@tonic-gate 			}
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
19497c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	cur = firstmatch;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
19547c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
19557c478bd9Sstevel@tonic-gate 		return (err);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
19587c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
19597c478bd9Sstevel@tonic-gate 		return (err);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
19627c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
19637c478bd9Sstevel@tonic-gate 		return (err);
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
19667c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
19677c478bd9Sstevel@tonic-gate 		return (err);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	/* options are optional */
19707c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
19717c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
19727c478bd9Sstevel@tonic-gate 	    options = options->next) {
19737c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
19747c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
19757c478bd9Sstevel@tonic-gate 			break;
19767c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
19777c478bd9Sstevel@tonic-gate 			break;
19787c478bd9Sstevel@tonic-gate 	}
19797c478bd9Sstevel@tonic-gate 	return (Z_OK);
19807c478bd9Sstevel@tonic-gate }
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate int
19837c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
19847c478bd9Sstevel@tonic-gate {
19857c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
19867c478bd9Sstevel@tonic-gate 	int err;
19877c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
19907c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19937c478bd9Sstevel@tonic-gate 		return (err);
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 	/*
19967c478bd9Sstevel@tonic-gate 	 * General algorithm:
19977c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
19987c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
19997c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
20007c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
20017c478bd9Sstevel@tonic-gate 	 */
20027c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
20037c478bd9Sstevel@tonic-gate 	match = NULL;
20047c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
20057c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
20067c478bd9Sstevel@tonic-gate 			continue;
20077c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
20087c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
20097c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
20107c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
20117c478bd9Sstevel@tonic-gate 				if (match == NULL)
20127c478bd9Sstevel@tonic-gate 					match = cur;
20137c478bd9Sstevel@tonic-gate 				else
20147c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20157c478bd9Sstevel@tonic-gate 			}
20167c478bd9Sstevel@tonic-gate 		}
20177c478bd9Sstevel@tonic-gate 	}
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	if (match == NULL)
20207c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	cur = match;
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
20257c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
20267c478bd9Sstevel@tonic-gate 		return (err);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	return (Z_OK);
20297c478bd9Sstevel@tonic-gate }
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate /*
20327c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
20337c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
20347c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
20357c478bd9Sstevel@tonic-gate  */
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate boolean_t
20387c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
20397c478bd9Sstevel@tonic-gate {
20407c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
20417c478bd9Sstevel@tonic-gate 	int result;
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
20447c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	/*
20477c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
20487c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
20497c478bd9Sstevel@tonic-gate 	 */
20507c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
20517c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
20527c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
20537c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
20547c478bd9Sstevel@tonic-gate 		return (B_FALSE);
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	/*
20577c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
20587c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
20597c478bd9Sstevel@tonic-gate 	 * result of the comparison.
20607c478bd9Sstevel@tonic-gate 	 */
20617c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
20627c478bd9Sstevel@tonic-gate 	*slashp = '\0';
20637c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
20647c478bd9Sstevel@tonic-gate 	*slashp = '/';
20657c478bd9Sstevel@tonic-gate 	return ((result == 0));
20667c478bd9Sstevel@tonic-gate }
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate int
20697c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
20707c478bd9Sstevel@tonic-gate {
20717c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
20727c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
20737c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
20747c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
20777c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20787c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20797c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
20807c478bd9Sstevel@tonic-gate 		*slashp = '\0';
20817c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
20827c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
20837c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
20847c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
20857c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
20867c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
20877c478bd9Sstevel@tonic-gate 	} else {
20887c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
20897c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
20907c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
20917c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
20927c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
20937c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
2094a1be23daSdp 
20957c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
20967c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
20977c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
20987c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
2099a1be23daSdp 
21007c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 	return (Z_OK);
21037c478bd9Sstevel@tonic-gate }
21047c478bd9Sstevel@tonic-gate 
2105*f4b3ec61Sdh boolean_t
2106*f4b3ec61Sdh zonecfg_ifname_exists(sa_family_t af, char *ifname)
2107*f4b3ec61Sdh {
2108*f4b3ec61Sdh 	struct lifreq lifr;
2109*f4b3ec61Sdh 	int so;
2110*f4b3ec61Sdh 	int save_errno;
2111*f4b3ec61Sdh 
2112*f4b3ec61Sdh 	(void) memset(&lifr, 0, sizeof (lifr));
2113*f4b3ec61Sdh 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2114*f4b3ec61Sdh 	lifr.lifr_addr.ss_family = af;
2115*f4b3ec61Sdh 	if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
2116*f4b3ec61Sdh 		/* Odd - can't tell if the ifname exists */
2117*f4b3ec61Sdh 		return (B_FALSE);
2118*f4b3ec61Sdh 	}
2119*f4b3ec61Sdh 	if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2120*f4b3ec61Sdh 		save_errno = errno;
2121*f4b3ec61Sdh 		(void) close(so);
2122*f4b3ec61Sdh 		errno = save_errno;
2123*f4b3ec61Sdh 		return (B_FALSE);
2124*f4b3ec61Sdh 	}
2125*f4b3ec61Sdh 	(void) close(so);
2126*f4b3ec61Sdh 	return (B_TRUE);
2127*f4b3ec61Sdh }
2128*f4b3ec61Sdh 
21297c478bd9Sstevel@tonic-gate int
21307c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21317c478bd9Sstevel@tonic-gate {
21327c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
21337c478bd9Sstevel@tonic-gate 	int err;
21347c478bd9Sstevel@tonic-gate 	char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ];
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21377c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21407c478bd9Sstevel@tonic-gate 		return (err);
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
21437c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
21447c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
21457c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
21467c478bd9Sstevel@tonic-gate 			continue;
21477c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_physical) > 0) {
21487c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
21497c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
21507c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
21517c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
21527c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
21537c478bd9Sstevel@tonic-gate 					firstmatch = cur;
21547c478bd9Sstevel@tonic-gate 				else
21557c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
21567c478bd9Sstevel@tonic-gate 			}
21577c478bd9Sstevel@tonic-gate 		}
21587c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_address) > 0) {
21597c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
21607c478bd9Sstevel@tonic-gate 			    sizeof (address)) == Z_OK)) {
21617c478bd9Sstevel@tonic-gate 				if (zonecfg_same_net_address(
21627c478bd9Sstevel@tonic-gate 				    tabptr->zone_nwif_address, address)) {
21637c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
21647c478bd9Sstevel@tonic-gate 						firstmatch = cur;
21657c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
21667c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
21677c478bd9Sstevel@tonic-gate 				} else {
21687c478bd9Sstevel@tonic-gate 					/*
21697c478bd9Sstevel@tonic-gate 					 * If another property matched but this
21707c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
21717c478bd9Sstevel@tonic-gate 					 */
21727c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
21737c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
21747c478bd9Sstevel@tonic-gate 				}
21757c478bd9Sstevel@tonic-gate 			}
21767c478bd9Sstevel@tonic-gate 		}
21777c478bd9Sstevel@tonic-gate 	}
21787c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
21797c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	cur = firstmatch;
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
21847c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
21857c478bd9Sstevel@tonic-gate 		return (err);
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
21887c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
21897c478bd9Sstevel@tonic-gate 		return (err);
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 	return (Z_OK);
21927c478bd9Sstevel@tonic-gate }
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate static int
21957c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21967c478bd9Sstevel@tonic-gate {
21977c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
21987c478bd9Sstevel@tonic-gate 	int err;
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
2201a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
22027c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
22037c478bd9Sstevel@tonic-gate 		return (err);
2204a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
22057c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
22067c478bd9Sstevel@tonic-gate 		return (err);
22077c478bd9Sstevel@tonic-gate 	return (Z_OK);
22087c478bd9Sstevel@tonic-gate }
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate int
22117c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22127c478bd9Sstevel@tonic-gate {
22137c478bd9Sstevel@tonic-gate 	int err;
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22167c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22197c478bd9Sstevel@tonic-gate 		return (err);
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
22227c478bd9Sstevel@tonic-gate 		return (err);
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	return (Z_OK);
22257c478bd9Sstevel@tonic-gate }
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate static int
22287c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22297c478bd9Sstevel@tonic-gate {
22307c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
22317c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
22347c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
22357c478bd9Sstevel@tonic-gate 			continue;
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
22387c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
22397c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
22407c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
22437c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
22447c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
22457c478bd9Sstevel@tonic-gate 			return (Z_OK);
22467c478bd9Sstevel@tonic-gate 		}
22477c478bd9Sstevel@tonic-gate 	}
22487c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
22497c478bd9Sstevel@tonic-gate }
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate int
22527c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22537c478bd9Sstevel@tonic-gate {
22547c478bd9Sstevel@tonic-gate 	int err;
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22607c478bd9Sstevel@tonic-gate 		return (err);
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
22637c478bd9Sstevel@tonic-gate 		return (err);
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	return (Z_OK);
22667c478bd9Sstevel@tonic-gate }
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate int
22697c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
22707c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
22717c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
22727c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
22737c478bd9Sstevel@tonic-gate {
22747c478bd9Sstevel@tonic-gate 	int err;
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
22777c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22807c478bd9Sstevel@tonic-gate 		return (err);
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
22837c478bd9Sstevel@tonic-gate 		return (err);
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
22867c478bd9Sstevel@tonic-gate 		return (err);
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 	return (Z_OK);
22897c478bd9Sstevel@tonic-gate }
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate int
22927c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
22937c478bd9Sstevel@tonic-gate {
22947c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
22957c478bd9Sstevel@tonic-gate 	int err;
22967c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22997c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23027c478bd9Sstevel@tonic-gate 		return (err);
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
23057c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
23067c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23077c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
23087c478bd9Sstevel@tonic-gate 			continue;
23097c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
23107c478bd9Sstevel@tonic-gate 			continue;
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
23137c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
23147c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
23157c478bd9Sstevel@tonic-gate 			    match) == 0) {
23167c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
23177c478bd9Sstevel@tonic-gate 					firstmatch = cur;
23187c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
23197c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
23207c478bd9Sstevel@tonic-gate 			} else {
23217c478bd9Sstevel@tonic-gate 				/*
23227c478bd9Sstevel@tonic-gate 				 * If another property matched but this
23237c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
23247c478bd9Sstevel@tonic-gate 				 */
23257c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
23267c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
23277c478bd9Sstevel@tonic-gate 			}
23287c478bd9Sstevel@tonic-gate 		}
23297c478bd9Sstevel@tonic-gate 	}
23307c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
23317c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	cur = firstmatch;
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
23367c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
23377c478bd9Sstevel@tonic-gate 		return (err);
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 	return (Z_OK);
23407c478bd9Sstevel@tonic-gate }
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate static int
23437c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
23467c478bd9Sstevel@tonic-gate 	int err;
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
23497c478bd9Sstevel@tonic-gate 
2350a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
23517c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
23527c478bd9Sstevel@tonic-gate 		return (err);
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 	return (Z_OK);
23557c478bd9Sstevel@tonic-gate }
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate int
23587c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23597c478bd9Sstevel@tonic-gate {
23607c478bd9Sstevel@tonic-gate 	int err;
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23637c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23667c478bd9Sstevel@tonic-gate 		return (err);
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
23697c478bd9Sstevel@tonic-gate 		return (err);
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	return (Z_OK);
23727c478bd9Sstevel@tonic-gate }
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate static int
23757c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
23767c478bd9Sstevel@tonic-gate {
2377facf4a8dSllai 	xmlNodePtr cur = handle->zone_dh_cur;
23787c478bd9Sstevel@tonic-gate 	int match_match;
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23817c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
23827c478bd9Sstevel@tonic-gate 			continue;
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
23857c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 		if (match_match) {
23887c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
23897c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
23907c478bd9Sstevel@tonic-gate 			return (Z_OK);
23917c478bd9Sstevel@tonic-gate 		}
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
23947c478bd9Sstevel@tonic-gate }
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate int
23977c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23987c478bd9Sstevel@tonic-gate {
23997c478bd9Sstevel@tonic-gate 	int err;
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
24027c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24057c478bd9Sstevel@tonic-gate 		return (err);
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
24087c478bd9Sstevel@tonic-gate 		return (err);
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	return (Z_OK);
24117c478bd9Sstevel@tonic-gate }
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate int
24147c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
24157c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
24167c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
24177c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
24187c478bd9Sstevel@tonic-gate {
24197c478bd9Sstevel@tonic-gate 	int err;
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
24227c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24257c478bd9Sstevel@tonic-gate 		return (err);
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
24287c478bd9Sstevel@tonic-gate 		return (err);
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
24317c478bd9Sstevel@tonic-gate 		return (err);
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 	return (Z_OK);
24347c478bd9Sstevel@tonic-gate }
24357c478bd9Sstevel@tonic-gate 
2436ee519a1fSgjelinek /* Lock to serialize all zonecfg_devwalks */
2437ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2438ee519a1fSgjelinek /*
2439ee519a1fSgjelinek  * Global variables used to pass data from zonecfg_devwalk to the nftw
2440ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2441ee519a1fSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from zonecfg_devwalk.
2442ee519a1fSgjelinek  */
2443ee519a1fSgjelinek static void *g_devwalk_data;
2444ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2445ee519a1fSgjelinek     void *);
2446ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2447ee519a1fSgjelinek 
2448ee519a1fSgjelinek /*
2449ee519a1fSgjelinek  * This is the nftw call-back function used by zonecfg_devwalk.  It is
2450ee519a1fSgjelinek  * responsible for calling the actual call-back that is passed in to
2451ee519a1fSgjelinek  * zonecfg_devwalk as the *cb argument.
2452ee519a1fSgjelinek  */
2453ee519a1fSgjelinek /* ARGSUSED2 */
2454ee519a1fSgjelinek static int
2455ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2456ee519a1fSgjelinek     struct FTW *ftw)
2457ee519a1fSgjelinek {
2458ee519a1fSgjelinek 	acl_t *acl;
2459ee519a1fSgjelinek 	char *acl_txt = NULL;
2460ee519a1fSgjelinek 
2461ee519a1fSgjelinek 	/* skip all but character and block devices */
2462ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2463ee519a1fSgjelinek 		return (0);
2464ee519a1fSgjelinek 
2465ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2466ee519a1fSgjelinek 	    acl != NULL) {
2467ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2468ee519a1fSgjelinek 		acl_free(acl);
2469ee519a1fSgjelinek 	}
2470ee519a1fSgjelinek 
2471ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2472ee519a1fSgjelinek 		return (0);
2473ee519a1fSgjelinek 
2474ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2475ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2476ee519a1fSgjelinek 	    g_devwalk_data);
2477ee519a1fSgjelinek 	free(acl_txt);
2478ee519a1fSgjelinek 	return (0);
2479ee519a1fSgjelinek }
2480ee519a1fSgjelinek 
2481ee519a1fSgjelinek /*
2482ee519a1fSgjelinek  * Walk the dev tree for the zone specified by hdl and call the call-back (cb)
2483ee519a1fSgjelinek  * function for each entry in the tree.  The call-back will be passed the
2484ee519a1fSgjelinek  * name, uid, gid, mode, acl string and the void *data input parameter
2485ee519a1fSgjelinek  * for each dev entry.
2486ee519a1fSgjelinek  *
2487ee519a1fSgjelinek  * Data is passed to the zonecfg_devwalk_cb through the global variables
2488ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2489ee519a1fSgjelinek  * zonecfg_devwalk_cb function will actually call *cb.
2490ee519a1fSgjelinek  */
2491ee519a1fSgjelinek int
2492ee519a1fSgjelinek zonecfg_devwalk(zone_dochandle_t hdl,
2493ee519a1fSgjelinek     int (*cb)(const char *, uid_t, gid_t, mode_t, const char *, void *),
2494ee519a1fSgjelinek     void *data)
2495ee519a1fSgjelinek {
2496ee519a1fSgjelinek 	char path[MAXPATHLEN];
2497ee519a1fSgjelinek 	int ret;
2498ee519a1fSgjelinek 
2499ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2500ee519a1fSgjelinek 		return (ret);
2501ee519a1fSgjelinek 
2502ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2503ee519a1fSgjelinek 		return (Z_TOO_BIG);
2504ee519a1fSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2505ee519a1fSgjelinek 
2506ee519a1fSgjelinek 	/*
2507ee519a1fSgjelinek 	 * We have to serialize all zonecfg_devwalks in the same process
2508ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2509ee519a1fSgjelinek 	 */
2510ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2511ee519a1fSgjelinek 
2512ee519a1fSgjelinek 	g_devwalk_data = data;
2513ee519a1fSgjelinek 	g_devwalk_cb = cb;
2514ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2515ee519a1fSgjelinek 
2516ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2517ee519a1fSgjelinek 	return (Z_OK);
2518ee519a1fSgjelinek }
2519ee519a1fSgjelinek 
2520ee519a1fSgjelinek /*
2521ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2522ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2523ee519a1fSgjelinek  * attaching a migrated zone.
2524ee519a1fSgjelinek  */
2525ee519a1fSgjelinek int
2526ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2527ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2528ee519a1fSgjelinek {
2529ee519a1fSgjelinek 	int ret;
2530ee519a1fSgjelinek 	char path[MAXPATHLEN];
2531ee519a1fSgjelinek 	struct stat st;
2532ee519a1fSgjelinek 	acl_t *aclp;
2533ee519a1fSgjelinek 
2534ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2535ee519a1fSgjelinek 		return (ret);
2536ee519a1fSgjelinek 
2537ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2538ee519a1fSgjelinek 		return (Z_TOO_BIG);
2539ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2540ee519a1fSgjelinek 		return (Z_TOO_BIG);
2541ee519a1fSgjelinek 
2542ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2543ee519a1fSgjelinek 		return (Z_INVAL);
2544ee519a1fSgjelinek 
2545ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2546ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2547ee519a1fSgjelinek 		return (Z_INVAL);
2548ee519a1fSgjelinek 
2549ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2550ee519a1fSgjelinek 		return (Z_SYSTEM);
2551ee519a1fSgjelinek 
2552ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2553ee519a1fSgjelinek 		return (Z_SYSTEM);
2554ee519a1fSgjelinek 
2555ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2556ee519a1fSgjelinek 		return (Z_OK);
2557ee519a1fSgjelinek 
2558ee519a1fSgjelinek 	if (acl_fromtext(acltxt, &aclp) != 0)
2559ee519a1fSgjelinek 		return (Z_SYSTEM);
2560ee519a1fSgjelinek 
2561ee519a1fSgjelinek 	errno = 0;
2562ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2563ee519a1fSgjelinek 		free(aclp);
2564ee519a1fSgjelinek 		return (Z_SYSTEM);
2565ee519a1fSgjelinek 	}
2566ee519a1fSgjelinek 
2567ee519a1fSgjelinek 	free(aclp);
2568ee519a1fSgjelinek 	return (Z_OK);
2569ee519a1fSgjelinek }
2570ee519a1fSgjelinek 
25717c478bd9Sstevel@tonic-gate /*
25727c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
25737c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
25747c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
25757c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
25767c478bd9Sstevel@tonic-gate  *
25777c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
25787c478bd9Sstevel@tonic-gate  * aborts with an error.
25797c478bd9Sstevel@tonic-gate  */
25807c478bd9Sstevel@tonic-gate int
25817c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
25827c478bd9Sstevel@tonic-gate     void *priv) {
25837c478bd9Sstevel@tonic-gate 	FILE *mnttab;
25847c478bd9Sstevel@tonic-gate 	struct mnttab m;
25857c478bd9Sstevel@tonic-gate 	size_t l;
258607b574eeSgjelinek 	int zfsl;
25877c478bd9Sstevel@tonic-gate 	int rv = 0;
258807b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
25917c478bd9Sstevel@tonic-gate 
259207b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
259307b574eeSgjelinek 	    >= sizeof (zfs_path))
259407b574eeSgjelinek 		return (-1);
259507b574eeSgjelinek 
25967c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
26017c478bd9Sstevel@tonic-gate 		return (-1);
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
26047c478bd9Sstevel@tonic-gate 		rv = -1;
26057c478bd9Sstevel@tonic-gate 		goto out;
26067c478bd9Sstevel@tonic-gate 	}
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
26097c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
261007b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
261107b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
26127c478bd9Sstevel@tonic-gate 			rv++;
26137c478bd9Sstevel@tonic-gate 			if (callback == NULL)
26147c478bd9Sstevel@tonic-gate 				continue;
26157c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
26167c478bd9Sstevel@tonic-gate 				rv = -1;
26177c478bd9Sstevel@tonic-gate 				goto out;
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 			}
26207c478bd9Sstevel@tonic-gate 		}
26217c478bd9Sstevel@tonic-gate 	}
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate out:
26247c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
26257c478bd9Sstevel@tonic-gate 	return (rv);
26267c478bd9Sstevel@tonic-gate }
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate int
26297c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26307c478bd9Sstevel@tonic-gate {
26317c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
26327c478bd9Sstevel@tonic-gate 	int err;
26337c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
26367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
26397c478bd9Sstevel@tonic-gate 		return (err);
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
26427c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
26437c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
26447c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
26457c478bd9Sstevel@tonic-gate 			continue;
26467c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
26477c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
26487c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
26497c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
26507c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
26517c478bd9Sstevel@tonic-gate 					firstmatch = cur;
26527c478bd9Sstevel@tonic-gate 				else
26537c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
26547c478bd9Sstevel@tonic-gate 			}
26557c478bd9Sstevel@tonic-gate 		}
26567c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
26577c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
26587c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
26597c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
26607c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
26617c478bd9Sstevel@tonic-gate 						firstmatch = cur;
26627c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
26637c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
26647c478bd9Sstevel@tonic-gate 				} else {
26657c478bd9Sstevel@tonic-gate 					/*
26667c478bd9Sstevel@tonic-gate 					 * If another property matched but this
26677c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
26687c478bd9Sstevel@tonic-gate 					 */
26697c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
26707c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
26717c478bd9Sstevel@tonic-gate 				}
26727c478bd9Sstevel@tonic-gate 			}
26737c478bd9Sstevel@tonic-gate 		}
26747c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
26757c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
26767c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
26777c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
26787c478bd9Sstevel@tonic-gate 				    0) {
26797c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
26807c478bd9Sstevel@tonic-gate 						firstmatch = cur;
26817c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
26827c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
26837c478bd9Sstevel@tonic-gate 				} else {
26847c478bd9Sstevel@tonic-gate 					/*
26857c478bd9Sstevel@tonic-gate 					 * If another property matched but this
26867c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
26877c478bd9Sstevel@tonic-gate 					 */
26887c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
26897c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
26907c478bd9Sstevel@tonic-gate 				}
26917c478bd9Sstevel@tonic-gate 			}
26927c478bd9Sstevel@tonic-gate 		}
26937c478bd9Sstevel@tonic-gate 	}
26947c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
26957c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 	cur = firstmatch;
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
27007c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
27017c478bd9Sstevel@tonic-gate 		return (err);
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
27047c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
27057c478bd9Sstevel@tonic-gate 		return (err);
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
27087c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
27097c478bd9Sstevel@tonic-gate 		return (err);
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate 	return (Z_OK);
27127c478bd9Sstevel@tonic-gate }
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate static int
27157c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27167c478bd9Sstevel@tonic-gate {
27177c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
27187c478bd9Sstevel@tonic-gate 	int err;
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2721a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
27227c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27237c478bd9Sstevel@tonic-gate 		return (err);
2724a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
27257c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27267c478bd9Sstevel@tonic-gate 		return (err);
2727a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
27287c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27297c478bd9Sstevel@tonic-gate 		return (err);
27307c478bd9Sstevel@tonic-gate 	return (Z_OK);
27317c478bd9Sstevel@tonic-gate }
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate int
27347c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27357c478bd9Sstevel@tonic-gate {
27367c478bd9Sstevel@tonic-gate 	int err;
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
27397c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27427c478bd9Sstevel@tonic-gate 		return (err);
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
27457c478bd9Sstevel@tonic-gate 		return (err);
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	return (Z_OK);
27487c478bd9Sstevel@tonic-gate }
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate static int
27517c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27527c478bd9Sstevel@tonic-gate {
27537c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
27547c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
27577c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
27587c478bd9Sstevel@tonic-gate 			continue;
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
27617c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
27627c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
27637c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
27647c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
27657c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
27687c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
27697c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
27707c478bd9Sstevel@tonic-gate 			return (Z_OK);
27717c478bd9Sstevel@tonic-gate 		}
27727c478bd9Sstevel@tonic-gate 	}
27737c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
27747c478bd9Sstevel@tonic-gate }
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate int
27777c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27787c478bd9Sstevel@tonic-gate {
27797c478bd9Sstevel@tonic-gate 	int err;
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
27827c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27857c478bd9Sstevel@tonic-gate 		return (err);
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
27887c478bd9Sstevel@tonic-gate 		return (err);
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	return (Z_OK);
27917c478bd9Sstevel@tonic-gate }
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate int
27947c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
27957c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
27967c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
27977c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
27987c478bd9Sstevel@tonic-gate {
27997c478bd9Sstevel@tonic-gate 	int err;
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
28027c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28057c478bd9Sstevel@tonic-gate 		return (err);
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
28087c478bd9Sstevel@tonic-gate 		return (err);
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
28117c478bd9Sstevel@tonic-gate 		return (err);
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate 	return (Z_OK);
28147c478bd9Sstevel@tonic-gate }
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate int
28177c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
28187c478bd9Sstevel@tonic-gate {
28197c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28207c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
28237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
28267c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
28277c478bd9Sstevel@tonic-gate 		return (Z_OK);
28287c478bd9Sstevel@tonic-gate 	}
28297c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
28307c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
28317c478bd9Sstevel@tonic-gate 		return (Z_OK);
28327c478bd9Sstevel@tonic-gate 	}
28337c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
28347c478bd9Sstevel@tonic-gate }
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate int
28377c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
28387c478bd9Sstevel@tonic-gate {
28397c478bd9Sstevel@tonic-gate 	long long result;
28407c478bd9Sstevel@tonic-gate 	char *endptr;
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28437c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
28467c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	errno = 0;
28497c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
28507c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
28517c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28527c478bd9Sstevel@tonic-gate 	*value = result;
28537c478bd9Sstevel@tonic-gate 	return (Z_OK);
28547c478bd9Sstevel@tonic-gate }
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate int
28577c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
28587c478bd9Sstevel@tonic-gate     size_t val_sz)
28597c478bd9Sstevel@tonic-gate {
28607c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28617c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
28647c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
28677c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
28687c478bd9Sstevel@tonic-gate 	return (Z_OK);
28697c478bd9Sstevel@tonic-gate }
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate int
28727c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
28737c478bd9Sstevel@tonic-gate {
28747c478bd9Sstevel@tonic-gate 	unsigned long long result;
28757c478bd9Sstevel@tonic-gate 	long long neg_result;
28767c478bd9Sstevel@tonic-gate 	char *endptr;
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28797c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
28827c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 	errno = 0;
28857c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
28867c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
28877c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28887c478bd9Sstevel@tonic-gate 	errno = 0;
28897c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
28907c478bd9Sstevel@tonic-gate 	/*
28917c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
28927c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
28937c478bd9Sstevel@tonic-gate 	 * need to look for this here.
28947c478bd9Sstevel@tonic-gate 	 */
28957c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
28967c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28977c478bd9Sstevel@tonic-gate 	*value = result;
28987c478bd9Sstevel@tonic-gate 	return (Z_OK);
28997c478bd9Sstevel@tonic-gate }
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate int
29027c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29037c478bd9Sstevel@tonic-gate {
29047c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
29057c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
29067c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29077c478bd9Sstevel@tonic-gate 	int err;
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	if (tabptr->zone_rctl_name == NULL ||
29107c478bd9Sstevel@tonic-gate 	    strlen(tabptr->zone_rctl_name) == 0)
29117c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29147c478bd9Sstevel@tonic-gate 		return (err);
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
29177c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
29187c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
29197c478bd9Sstevel@tonic-gate 			continue;
29207c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
29217c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
29227c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
29237c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
29247c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
29257c478bd9Sstevel@tonic-gate 			    val = val->next) {
29267c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
29277c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
29287c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
29297c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
29307c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
29317c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
29327c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
29337c478bd9Sstevel@tonic-gate 				    Z_OK))
29347c478bd9Sstevel@tonic-gate 					break;
29357c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
29367c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
29377c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
29387c478bd9Sstevel@tonic-gate 				    Z_OK))
29397c478bd9Sstevel@tonic-gate 					break;
29407c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
29417c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
29427c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
29437c478bd9Sstevel@tonic-gate 				    Z_OK))
29447c478bd9Sstevel@tonic-gate 					break;
29457c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
29467c478bd9Sstevel@tonic-gate 				    Z_OK)
29477c478bd9Sstevel@tonic-gate 					break;
29487c478bd9Sstevel@tonic-gate 			}
29497c478bd9Sstevel@tonic-gate 			return (Z_OK);
29507c478bd9Sstevel@tonic-gate 		}
29517c478bd9Sstevel@tonic-gate 	}
29527c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
29537c478bd9Sstevel@tonic-gate }
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate static int
29567c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29577c478bd9Sstevel@tonic-gate {
29587c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
29597c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29607c478bd9Sstevel@tonic-gate 	int err;
29617c478bd9Sstevel@tonic-gate 
29627c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
2963a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
29647c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
29657c478bd9Sstevel@tonic-gate 		return (err);
29667c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
29677c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
29687c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
29697c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
2970a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
29717c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
29727c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
29737c478bd9Sstevel@tonic-gate 			return (err);
2974a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
29757c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
29767c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
29777c478bd9Sstevel@tonic-gate 			return (err);
2978a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
29797c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
29807c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
29817c478bd9Sstevel@tonic-gate 			return (err);
29827c478bd9Sstevel@tonic-gate 	}
29837c478bd9Sstevel@tonic-gate 	return (Z_OK);
29847c478bd9Sstevel@tonic-gate }
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate int
29877c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29887c478bd9Sstevel@tonic-gate {
29897c478bd9Sstevel@tonic-gate 	int err;
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
29927c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29957c478bd9Sstevel@tonic-gate 		return (err);
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
29987c478bd9Sstevel@tonic-gate 		return (err);
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	return (Z_OK);
30017c478bd9Sstevel@tonic-gate }
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate static int
30047c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30057c478bd9Sstevel@tonic-gate {
30067c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
30077c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
30087c478bd9Sstevel@tonic-gate 	int name_result;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
30117c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
30127c478bd9Sstevel@tonic-gate 			continue;
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
30157c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
30167c478bd9Sstevel@tonic-gate 			continue;
30177c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
30187c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
30197c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
30207c478bd9Sstevel@tonic-gate 
30217c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
30227c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
30237c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
30247c478bd9Sstevel@tonic-gate 			return (Z_OK);
30257c478bd9Sstevel@tonic-gate 		}
30267c478bd9Sstevel@tonic-gate 	}
30277c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
30287c478bd9Sstevel@tonic-gate }
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate int
30317c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30327c478bd9Sstevel@tonic-gate {
30337c478bd9Sstevel@tonic-gate 	int err;
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
30367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30397c478bd9Sstevel@tonic-gate 		return (err);
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
30427c478bd9Sstevel@tonic-gate 		return (err);
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 	return (Z_OK);
30457c478bd9Sstevel@tonic-gate }
30467c478bd9Sstevel@tonic-gate 
30477c478bd9Sstevel@tonic-gate int
30487c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
30497c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
30507c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
30517c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
30527c478bd9Sstevel@tonic-gate {
30537c478bd9Sstevel@tonic-gate 	int err;
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || oldtabptr->zone_rctl_name == NULL ||
30567c478bd9Sstevel@tonic-gate 	    newtabptr == NULL || newtabptr->zone_rctl_name == NULL)
30577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30607c478bd9Sstevel@tonic-gate 		return (err);
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
30637c478bd9Sstevel@tonic-gate 		return (err);
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
30667c478bd9Sstevel@tonic-gate 		return (err);
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 	return (Z_OK);
30697c478bd9Sstevel@tonic-gate }
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate int
30727c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
30737c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
30747c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
30757c478bd9Sstevel@tonic-gate {
30767c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
30777c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
30807c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
30817c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
30827c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
30837c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
30847c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
30857c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30867c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
30877c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30887c478bd9Sstevel@tonic-gate 	if (last == NULL)
30897c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
30907c478bd9Sstevel@tonic-gate 	else
30917c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
30927c478bd9Sstevel@tonic-gate 	return (Z_OK);
30937c478bd9Sstevel@tonic-gate }
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate int
30967c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
30977c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
30987c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
30997c478bd9Sstevel@tonic-gate {
31007c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
31037c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
31047c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
31057c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
31067c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
31077c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
31087c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
31097c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
31107c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
31117c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
31127c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
31137c478bd9Sstevel@tonic-gate 			else
31147c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
31157c478bd9Sstevel@tonic-gate 			free(this);
31167c478bd9Sstevel@tonic-gate 			return (Z_OK);
31177c478bd9Sstevel@tonic-gate 		} else
31187c478bd9Sstevel@tonic-gate 			last = this;
31197c478bd9Sstevel@tonic-gate 	}
31207c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
31217c478bd9Sstevel@tonic-gate }
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate char *
31247c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
31257c478bd9Sstevel@tonic-gate {
31267c478bd9Sstevel@tonic-gate 	switch (errnum) {
31277c478bd9Sstevel@tonic-gate 	case Z_OK:
31287c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
31297c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
31307c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
31317c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
31327c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
31337c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
31347c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
31357c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
31367c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31377c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
31387c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
31397c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
31407c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
31417c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
31427c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
31437c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
31447c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
31457c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
31467c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
31477c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
31487c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
31497c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
31507c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
31517c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
31527c478bd9Sstevel@tonic-gate 	case Z_INVAL:
31537c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
31547c478bd9Sstevel@tonic-gate 	case Z_ACCES:
31557c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
31567c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
31577c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
31587c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
31597c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31607c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
31617c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
31627c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
31637c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
31647c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
31657c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
31667c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
31677c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
31687c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
31697c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
31707c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3171087719fdSdp 	case Z_BAD_ZONE_STATE:
31727c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
3173087719fdSdp 		    "Zone state is invalid for the requested operation"));
31747c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
31757c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3176087719fdSdp 	case Z_NAME_IN_USE:
3177087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
31787c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
31797c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
31807c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
31817c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
31827c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
31837c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
31847c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
31857c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
31867c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
31877c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
31887c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
31897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
31907c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
31917c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31927c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
31937c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
31947c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
31957c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3196ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3197ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3198ffbafc53Scomay 		    "Specified privilege is prohibited"));
3199ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3200ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3201ffbafc53Scomay 		    "Required privilege is missing"));
3202ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3203ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3204ffbafc53Scomay 		    "Specified privilege is unknown"));
32059acbbeafSnn 	case Z_BRAND_ERROR:
32069acbbeafSnn 		return (dgettext(TEXT_DOMAIN,
32079acbbeafSnn 		    "Brand-specific error"));
32080209230bSgjelinek 	case Z_INCOMPATIBLE:
32090209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Incompatible settings"));
32100209230bSgjelinek 	case Z_ALIAS_DISALLOW:
32110209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32120209230bSgjelinek 		    "An incompatible rctl already exists for this property"));
32130209230bSgjelinek 	case Z_CLEAR_DISALLOW:
32140209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32150209230bSgjelinek 		    "Clearing this property is not allowed"));
32160209230bSgjelinek 	case Z_POOL:
32170209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error"));
32180209230bSgjelinek 	case Z_POOLS_NOT_ACTIVE:
32190209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Pools facility not active; "
32200209230bSgjelinek 		    "zone will not be bound to pool"));
32210209230bSgjelinek 	case Z_POOL_ENABLE:
32220209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32230209230bSgjelinek 		    "Could not enable pools facility"));
32240209230bSgjelinek 	case Z_NO_POOL:
32250209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32260209230bSgjelinek 		    "Pool not found; using default pool"));
32270209230bSgjelinek 	case Z_POOL_CREATE:
32280209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
32290209230bSgjelinek 		    "Could not create a temporary pool"));
32300209230bSgjelinek 	case Z_POOL_BIND:
32310209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool"));
32327c478bd9Sstevel@tonic-gate 	default:
32337c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
32347c478bd9Sstevel@tonic-gate 	}
32357c478bd9Sstevel@tonic-gate }
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate /*
32387c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
32397c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
32407c478bd9Sstevel@tonic-gate  */
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate static int
32437c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
32447c478bd9Sstevel@tonic-gate {
32457c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
32467c478bd9Sstevel@tonic-gate 	int err;
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32497c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
32527c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
32537c478bd9Sstevel@tonic-gate 		return (err);
32547c478bd9Sstevel@tonic-gate 	}
32557c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
32567c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
32577c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
32587c478bd9Sstevel@tonic-gate 	return (Z_OK);
32597c478bd9Sstevel@tonic-gate }
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate static int
32627c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
32637c478bd9Sstevel@tonic-gate {
32647c478bd9Sstevel@tonic-gate 	if (handle == NULL)
32657c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
32687c478bd9Sstevel@tonic-gate 	return (Z_OK);
32697c478bd9Sstevel@tonic-gate }
32707c478bd9Sstevel@tonic-gate 
32710209230bSgjelinek /*
32720209230bSgjelinek  * Do the work required to manipulate a process through libproc.
32730209230bSgjelinek  * If grab_process() returns no errors (0), then release_process()
32740209230bSgjelinek  * must eventually be called.
32750209230bSgjelinek  *
32760209230bSgjelinek  * Return values:
32770209230bSgjelinek  *      0 Successful creation of agent thread
32780209230bSgjelinek  *      1 Error grabbing
32790209230bSgjelinek  *      2 Error creating agent
32800209230bSgjelinek  */
32810209230bSgjelinek static int
32820209230bSgjelinek grab_process(pr_info_handle_t *p)
32837c478bd9Sstevel@tonic-gate {
32840209230bSgjelinek 	int ret;
32850209230bSgjelinek 
32860209230bSgjelinek 	if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) {
32870209230bSgjelinek 
32880209230bSgjelinek 		if (Psetflags(p->pr, PR_RLC) != 0) {
32890209230bSgjelinek 			Prelease(p->pr, 0);
32900209230bSgjelinek 			return (1);
32910209230bSgjelinek 		}
32920209230bSgjelinek 		if (Pcreate_agent(p->pr) == 0) {
32930209230bSgjelinek 			return (0);
32940209230bSgjelinek 
32950209230bSgjelinek 		} else {
32960209230bSgjelinek 			Prelease(p->pr, 0);
32970209230bSgjelinek 			return (2);
32980209230bSgjelinek 		}
32990209230bSgjelinek 	} else {
33000209230bSgjelinek 		return (1);
33010209230bSgjelinek 	}
33027c478bd9Sstevel@tonic-gate }
33037c478bd9Sstevel@tonic-gate 
33040209230bSgjelinek /*
33050209230bSgjelinek  * Release the specified process. This destroys the agent
33060209230bSgjelinek  * and releases the process. If the process is NULL, nothing
33070209230bSgjelinek  * is done. This function should only be called if grab_process()
33080209230bSgjelinek  * has previously been called and returned success.
33090209230bSgjelinek  *
33100209230bSgjelinek  * This function is Pgrab-safe.
33110209230bSgjelinek  */
33120209230bSgjelinek static void
33130209230bSgjelinek release_process(struct ps_prochandle *Pr)
33147c478bd9Sstevel@tonic-gate {
33150209230bSgjelinek 	if (Pr == NULL)
33160209230bSgjelinek 		return;
33177c478bd9Sstevel@tonic-gate 
33180209230bSgjelinek 	Pdestroy_agent(Pr);
33190209230bSgjelinek 	Prelease(Pr, 0);
33200209230bSgjelinek }
33217c478bd9Sstevel@tonic-gate 
33220209230bSgjelinek static boolean_t
33230209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p)
33240209230bSgjelinek {
33250209230bSgjelinek 	DIR *dirp;
33260209230bSgjelinek 	struct dirent *dentp;
33270209230bSgjelinek 	zoneid_t zoneid;
33280209230bSgjelinek 	int pid_self;
33290209230bSgjelinek 	psinfo_t psinfo;
33307c478bd9Sstevel@tonic-gate 
33310209230bSgjelinek 	if (zone_get_id(zonename, &zoneid) != 0)
33320209230bSgjelinek 		return (B_FALSE);
33337c478bd9Sstevel@tonic-gate 
33340209230bSgjelinek 	pid_self = getpid();
33357c478bd9Sstevel@tonic-gate 
33360209230bSgjelinek 	if ((dirp = opendir("/proc")) == NULL)
33370209230bSgjelinek 		return (B_FALSE);
33387c478bd9Sstevel@tonic-gate 
33390209230bSgjelinek 	while (dentp = readdir(dirp)) {
33400209230bSgjelinek 		p->pid = atoi(dentp->d_name);
33417c478bd9Sstevel@tonic-gate 
33420209230bSgjelinek 		/* Skip self */
33430209230bSgjelinek 		if (p->pid == pid_self)
33440209230bSgjelinek 			continue;
33457c478bd9Sstevel@tonic-gate 
33460209230bSgjelinek 		if (proc_get_psinfo(p->pid, &psinfo) != 0)
33470209230bSgjelinek 			continue;
33480209230bSgjelinek 
33490209230bSgjelinek 		if (psinfo.pr_zoneid != zoneid)
33500209230bSgjelinek 			continue;
33510209230bSgjelinek 
33520209230bSgjelinek 		/* attempt to grab process */
33530209230bSgjelinek 		if (grab_process(p) != 0)
33540209230bSgjelinek 			continue;
33550209230bSgjelinek 
33560209230bSgjelinek 		if (pr_getzoneid(p->pr) != zoneid) {
33570209230bSgjelinek 			release_process(p->pr);
33580209230bSgjelinek 			continue;
33590209230bSgjelinek 		}
33600209230bSgjelinek 
33610209230bSgjelinek 		(void) closedir(dirp);
33620209230bSgjelinek 		return (B_TRUE);
33637c478bd9Sstevel@tonic-gate 	}
33647c478bd9Sstevel@tonic-gate 
33650209230bSgjelinek 	(void) closedir(dirp);
33660209230bSgjelinek 	return (B_FALSE);
33677c478bd9Sstevel@tonic-gate }
33687c478bd9Sstevel@tonic-gate 
33690209230bSgjelinek static boolean_t
33700209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk)
33717c478bd9Sstevel@tonic-gate {
33720209230bSgjelinek 	if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST))
33730209230bSgjelinek 		return (B_FALSE);
33747c478bd9Sstevel@tonic-gate 
33750209230bSgjelinek 	if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
33760209230bSgjelinek 		return (B_TRUE);
33770209230bSgjelinek 
33780209230bSgjelinek 	while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) {
33790209230bSgjelinek 		if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
33800209230bSgjelinek 			return (B_TRUE);
33810209230bSgjelinek 	}
33820209230bSgjelinek 
33830209230bSgjelinek 	return (B_FALSE);
33847c478bd9Sstevel@tonic-gate }
33857c478bd9Sstevel@tonic-gate 
33860209230bSgjelinek /*
33870209230bSgjelinek  * Apply the current rctl settings to the specified, running zone.
33880209230bSgjelinek  */
33897c478bd9Sstevel@tonic-gate int
33900209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle)
33917c478bd9Sstevel@tonic-gate {
33927c478bd9Sstevel@tonic-gate 	int err;
33930209230bSgjelinek 	int res = Z_OK;
33940209230bSgjelinek 	rctlblk_t *rblk;
33950209230bSgjelinek 	pr_info_handle_t p;
33960209230bSgjelinek 	struct zone_rctltab rctl;
33977c478bd9Sstevel@tonic-gate 
33980209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK)
33990209230bSgjelinek 		return (err);
34007c478bd9Sstevel@tonic-gate 
34010209230bSgjelinek 	if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) {
34020209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
34030209230bSgjelinek 		return (Z_NOMEM);
34040209230bSgjelinek 	}
34057c478bd9Sstevel@tonic-gate 
34060209230bSgjelinek 	if (!grab_zone_proc(zone_name, &p)) {
34070209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
34080209230bSgjelinek 		free(rblk);
34090209230bSgjelinek 		return (Z_SYSTEM);
34107c478bd9Sstevel@tonic-gate 	}
34117c478bd9Sstevel@tonic-gate 
34120209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
34130209230bSgjelinek 		char *rname;
34140209230bSgjelinek 		struct zone_rctlvaltab *valptr;
34150209230bSgjelinek 
34160209230bSgjelinek 		rname = rctl.zone_rctl_name;
34170209230bSgjelinek 
34180209230bSgjelinek 		/* first delete all current privileged settings for this rctl */
34190209230bSgjelinek 		while (get_priv_rctl(p.pr, rname, rblk)) {
34200209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) !=
34210209230bSgjelinek 			    0) {
34220209230bSgjelinek 				res = Z_SYSTEM;
34230209230bSgjelinek 				goto done;
34240209230bSgjelinek 			}
34250209230bSgjelinek 		}
34260209230bSgjelinek 
34270209230bSgjelinek 		/* now set each new value for the rctl */
34280209230bSgjelinek 		for (valptr = rctl.zone_rctl_valptr; valptr != NULL;
34290209230bSgjelinek 		    valptr = valptr->zone_rctlval_next) {
34300209230bSgjelinek 			if ((err = zonecfg_construct_rctlblk(valptr, rblk))
34310209230bSgjelinek 			    != Z_OK) {
34320209230bSgjelinek 				res = errno = err;
34330209230bSgjelinek 				goto done;
34340209230bSgjelinek 			}
34350209230bSgjelinek 
34360209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) {
34370209230bSgjelinek 				res = Z_SYSTEM;
34380209230bSgjelinek 				goto done;
34390209230bSgjelinek 			}
34400209230bSgjelinek 		}
34417c478bd9Sstevel@tonic-gate 	}
34427c478bd9Sstevel@tonic-gate 
34430209230bSgjelinek done:
34440209230bSgjelinek 	release_process(p.pr);
34450209230bSgjelinek 	free(rblk);
34460209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
34470209230bSgjelinek 
34480209230bSgjelinek 	return (res);
34497c478bd9Sstevel@tonic-gate }
34507c478bd9Sstevel@tonic-gate 
34510209230bSgjelinek static const xmlChar *
34520209230bSgjelinek nm_to_dtd(char *nm)
34537c478bd9Sstevel@tonic-gate {
34540209230bSgjelinek 	if (strcmp(nm, "device") == 0)
34550209230bSgjelinek 		return (DTD_ELEM_DEVICE);
34560209230bSgjelinek 	if (strcmp(nm, "fs") == 0)
34570209230bSgjelinek 		return (DTD_ELEM_FS);
34580209230bSgjelinek 	if (strcmp(nm, "inherit-pkg-dir") == 0)
34590209230bSgjelinek 		return (DTD_ELEM_IPD);
34600209230bSgjelinek 	if (strcmp(nm, "net") == 0)
34610209230bSgjelinek 		return (DTD_ELEM_NET);
34620209230bSgjelinek 	if (strcmp(nm, "attr") == 0)
34630209230bSgjelinek 		return (DTD_ELEM_ATTR);
34640209230bSgjelinek 	if (strcmp(nm, "rctl") == 0)
34650209230bSgjelinek 		return (DTD_ELEM_RCTL);
34660209230bSgjelinek 	if (strcmp(nm, "dataset") == 0)
34670209230bSgjelinek 		return (DTD_ELEM_DATASET);
34680209230bSgjelinek 
34690209230bSgjelinek 	return (NULL);
34707c478bd9Sstevel@tonic-gate }
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate int
34730209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc)
34747c478bd9Sstevel@tonic-gate {
34750209230bSgjelinek 	int num = 0;
34760209230bSgjelinek 	const xmlChar *dtd;
34770209230bSgjelinek 	xmlNodePtr cur;
34780209230bSgjelinek 
34790209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
34800209230bSgjelinek 		return (num);
34810209230bSgjelinek 
34820209230bSgjelinek 	if (zonecfg_setent(handle) != Z_OK)
34830209230bSgjelinek 		return (num);
34840209230bSgjelinek 
34850209230bSgjelinek 	for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next)
34860209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd) == 0)
34870209230bSgjelinek 			num++;
34880209230bSgjelinek 
34890209230bSgjelinek 	(void) zonecfg_endent(handle);
34900209230bSgjelinek 
34910209230bSgjelinek 	return (num);
34927c478bd9Sstevel@tonic-gate }
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate int
34950209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc)
34967c478bd9Sstevel@tonic-gate {
34977c478bd9Sstevel@tonic-gate 	int err;
34980209230bSgjelinek 	const xmlChar *dtd;
34990209230bSgjelinek 	xmlNodePtr cur;
35007c478bd9Sstevel@tonic-gate 
35010209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
35020209230bSgjelinek 		return (Z_NO_RESOURCE_TYPE);
35037c478bd9Sstevel@tonic-gate 
35040209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
35050209230bSgjelinek 		return (err);
35067c478bd9Sstevel@tonic-gate 
35070209230bSgjelinek 	cur = handle->zone_dh_cur;
35080209230bSgjelinek 	while (cur != NULL) {
35090209230bSgjelinek 		xmlNodePtr tmp;
35107c478bd9Sstevel@tonic-gate 
35110209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd)) {
35120209230bSgjelinek 			cur = cur->next;
35130209230bSgjelinek 			continue;
35140209230bSgjelinek 		}
35157c478bd9Sstevel@tonic-gate 
35160209230bSgjelinek 		tmp = cur->next;
35170209230bSgjelinek 		xmlUnlinkNode(cur);
35180209230bSgjelinek 		xmlFreeNode(cur);
35190209230bSgjelinek 		cur = tmp;
35207c478bd9Sstevel@tonic-gate 	}
35217c478bd9Sstevel@tonic-gate 
35220209230bSgjelinek 	(void) zonecfg_endent(handle);
35237c478bd9Sstevel@tonic-gate 	return (Z_OK);
35247c478bd9Sstevel@tonic-gate }
35257c478bd9Sstevel@tonic-gate 
35260209230bSgjelinek static boolean_t
35270209230bSgjelinek valid_uint(char *s, uint64_t *n)
35287c478bd9Sstevel@tonic-gate {
35290209230bSgjelinek 	char *endp;
35307c478bd9Sstevel@tonic-gate 
35310209230bSgjelinek 	/* strtoull accepts '-'?! so we want to flag that as an error */
35320209230bSgjelinek 	if (strchr(s, '-') != NULL)
35330209230bSgjelinek 		return (B_FALSE);
35340209230bSgjelinek 
35350209230bSgjelinek 	errno = 0;
35360209230bSgjelinek 	*n = strtoull(s, &endp, 10);
35370209230bSgjelinek 
35380209230bSgjelinek 	if (errno != 0 || *endp != '\0')
35390209230bSgjelinek 		return (B_FALSE);
35400209230bSgjelinek 	return (B_TRUE);
35417c478bd9Sstevel@tonic-gate }
35427c478bd9Sstevel@tonic-gate 
35430209230bSgjelinek /*
35440209230bSgjelinek  * Convert a string representing a number (possibly a fraction) into an integer.
35450209230bSgjelinek  * The string can have a modifier (K, M, G or T).   The modifiers are treated
35460209230bSgjelinek  * as powers of two (not 10).
35470209230bSgjelinek  */
35487c478bd9Sstevel@tonic-gate int
35490209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes)
35507c478bd9Sstevel@tonic-gate {
35510209230bSgjelinek 	long double val;
35520209230bSgjelinek 	char *unitp;
35530209230bSgjelinek 	uint64_t scale;
35547c478bd9Sstevel@tonic-gate 
35550209230bSgjelinek 	if ((val = strtold(str, &unitp)) < 0)
35560209230bSgjelinek 		return (-1);
35577c478bd9Sstevel@tonic-gate 
35580209230bSgjelinek 	/* remove any leading white space from units string */
35590209230bSgjelinek 	while (isspace(*unitp) != 0)
35600209230bSgjelinek 		++unitp;
35617c478bd9Sstevel@tonic-gate 
35620209230bSgjelinek 	/* if no units explicitly set, error */
35630209230bSgjelinek 	if (unitp == NULL || *unitp == '\0') {
35640209230bSgjelinek 		scale = 1;
35650209230bSgjelinek 	} else {
35660209230bSgjelinek 		int i;
35670209230bSgjelinek 		char *units[] = {"K", "M", "G", "T", NULL};
35687c478bd9Sstevel@tonic-gate 
35690209230bSgjelinek 		scale = 1024;
35700209230bSgjelinek 
35710209230bSgjelinek 		/* update scale based on units */
35720209230bSgjelinek 		for (i = 0; units[i] != NULL; i++) {
35730209230bSgjelinek 			if (strcasecmp(unitp, units[i]) == 0)
35740209230bSgjelinek 				break;
35750209230bSgjelinek 			scale <<= 10;
35760209230bSgjelinek 		}
35770209230bSgjelinek 
35780209230bSgjelinek 		if (units[i] == NULL)
35790209230bSgjelinek 			return (-1);
35807c478bd9Sstevel@tonic-gate 	}
35817c478bd9Sstevel@tonic-gate 
35820209230bSgjelinek 	*bytes = (uint64_t)(val * scale);
35830209230bSgjelinek 	return (0);
35847c478bd9Sstevel@tonic-gate }
35857c478bd9Sstevel@tonic-gate 
35860209230bSgjelinek boolean_t
35870209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr)
35887c478bd9Sstevel@tonic-gate {
35890209230bSgjelinek 	uint64_t low, high;
35907c478bd9Sstevel@tonic-gate 
35910209230bSgjelinek 	if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) ||
35920209230bSgjelinek 	    low < 1 || low > high)
35930209230bSgjelinek 		return (B_FALSE);
35940209230bSgjelinek 
35950209230bSgjelinek 	return (B_TRUE);
35967c478bd9Sstevel@tonic-gate }
35977c478bd9Sstevel@tonic-gate 
35980209230bSgjelinek boolean_t
35990209230bSgjelinek zonecfg_valid_importance(char *impstr)
36007c478bd9Sstevel@tonic-gate {
36010209230bSgjelinek 	uint64_t num;
36027c478bd9Sstevel@tonic-gate 
36030209230bSgjelinek 	if (!valid_uint(impstr, &num))
36040209230bSgjelinek 		return (B_FALSE);
36057c478bd9Sstevel@tonic-gate 
36060209230bSgjelinek 	return (B_TRUE);
36070209230bSgjelinek }
36087c478bd9Sstevel@tonic-gate 
36090209230bSgjelinek boolean_t
36100209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit)
36110209230bSgjelinek {
36120209230bSgjelinek 	int i;
36130209230bSgjelinek 
36140209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
36150209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
36167c478bd9Sstevel@tonic-gate 			break;
36177c478bd9Sstevel@tonic-gate 
36180209230bSgjelinek 	if (aliases[i].shortname == NULL)
36190209230bSgjelinek 		return (B_FALSE);
36207c478bd9Sstevel@tonic-gate 
36210209230bSgjelinek 	if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit)
36220209230bSgjelinek 		return (B_FALSE);
36237c478bd9Sstevel@tonic-gate 
36240209230bSgjelinek 	return (B_TRUE);
36257c478bd9Sstevel@tonic-gate }
36267c478bd9Sstevel@tonic-gate 
36270209230bSgjelinek boolean_t
36280209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val)
36297c478bd9Sstevel@tonic-gate {
36300209230bSgjelinek 	if (zonecfg_str_to_bytes(memstr, mem_val) != 0)
36310209230bSgjelinek 		return (B_FALSE);
36320209230bSgjelinek 
36330209230bSgjelinek 	return (B_TRUE);
36347c478bd9Sstevel@tonic-gate }
36357c478bd9Sstevel@tonic-gate 
36360209230bSgjelinek static int
36370209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res)
36387c478bd9Sstevel@tonic-gate {
36390209230bSgjelinek 	(void) strlcpy(pool_err, pool_strerror(pool_error()), err_size);
36400209230bSgjelinek 	return (res);
36417c478bd9Sstevel@tonic-gate }
36427c478bd9Sstevel@tonic-gate 
36430209230bSgjelinek static int
36440209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool,
36450209230bSgjelinek     char *name, int min, int max)
36467c478bd9Sstevel@tonic-gate {
36470209230bSgjelinek 	pool_resource_t *res;
36480209230bSgjelinek 	pool_elem_t *elem;
36490209230bSgjelinek 	pool_value_t *val;
36507c478bd9Sstevel@tonic-gate 
36510209230bSgjelinek 	if ((res = pool_resource_create(pconf, "pset", name)) == NULL)
36520209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36537c478bd9Sstevel@tonic-gate 
36540209230bSgjelinek 	if (pool_associate(pconf, pool, res) != PO_SUCCESS)
36550209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36567c478bd9Sstevel@tonic-gate 
36570209230bSgjelinek 	if ((elem = pool_resource_to_elem(pconf, res)) == NULL)
36580209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36597c478bd9Sstevel@tonic-gate 
36600209230bSgjelinek 	if ((val = pool_value_alloc()) == NULL)
36610209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36627c478bd9Sstevel@tonic-gate 
36630209230bSgjelinek 	/* set the maximum number of cpus for the pset */
36640209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)max);
36650209230bSgjelinek 
36660209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) {
36670209230bSgjelinek 		pool_value_free(val);
36680209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36697c478bd9Sstevel@tonic-gate 	}
36707c478bd9Sstevel@tonic-gate 
36710209230bSgjelinek 	/* set the minimum number of cpus for the pset */
36720209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)min);
36730209230bSgjelinek 
36740209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) {
36750209230bSgjelinek 		pool_value_free(val);
36760209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
36777c478bd9Sstevel@tonic-gate 	}
36787c478bd9Sstevel@tonic-gate 
36790209230bSgjelinek 	pool_value_free(val);
36800209230bSgjelinek 
36817c478bd9Sstevel@tonic-gate 	return (Z_OK);
36827c478bd9Sstevel@tonic-gate }
36837c478bd9Sstevel@tonic-gate 
36840209230bSgjelinek static int
36850209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name,
36860209230bSgjelinek     struct zone_psettab *pset_tab)
36877c478bd9Sstevel@tonic-gate {
36880209230bSgjelinek 	pool_t *pool;
36890209230bSgjelinek 	int res = Z_OK;
36907c478bd9Sstevel@tonic-gate 
36910209230bSgjelinek 	/* create a temporary pool configuration */
36920209230bSgjelinek 	if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) {
36930209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
36940209230bSgjelinek 		return (res);
36950209230bSgjelinek 	}
3696ffbafc53Scomay 
36970209230bSgjelinek 	if ((pool = pool_create(pconf, name)) == NULL) {
36980209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
36990209230bSgjelinek 		goto done;
37000209230bSgjelinek 	}
3701ffbafc53Scomay 
37020209230bSgjelinek 	/* set pool importance */
37030209230bSgjelinek 	if (pset_tab->zone_importance[0] != '\0') {
37040209230bSgjelinek 		pool_elem_t *elem;
37050209230bSgjelinek 		pool_value_t *val;
37069acbbeafSnn 
37070209230bSgjelinek 		if ((elem = pool_to_elem(pconf, pool)) == NULL) {
37080209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37090209230bSgjelinek 			goto done;
37100209230bSgjelinek 		}
37119acbbeafSnn 
37120209230bSgjelinek 		if ((val = pool_value_alloc()) == NULL) {
37130209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37140209230bSgjelinek 			goto done;
37150209230bSgjelinek 		}
37169acbbeafSnn 
37170209230bSgjelinek 		pool_value_set_int64(val,
37180209230bSgjelinek 		    (int64_t)atoi(pset_tab->zone_importance));
37199acbbeafSnn 
37200209230bSgjelinek 		if (pool_put_property(pconf, elem, "pool.importance", val)
37210209230bSgjelinek 		    != PO_SUCCESS) {
37220209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
37230209230bSgjelinek 			pool_value_free(val);
37240209230bSgjelinek 			goto done;
37250209230bSgjelinek 		}
37269acbbeafSnn 
37270209230bSgjelinek 		pool_value_free(val);
37289acbbeafSnn 	}
37299acbbeafSnn 
37300209230bSgjelinek 	if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name,
37310209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_min),
37320209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_max))) != Z_OK)
37330209230bSgjelinek 		goto done;
37340209230bSgjelinek 
37350209230bSgjelinek 	/* validation */
37360209230bSgjelinek 	if (pool_conf_status(pconf) == POF_INVALID) {
37370209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37380209230bSgjelinek 		goto done;
37399acbbeafSnn 	}
37409acbbeafSnn 
37410209230bSgjelinek 	/*
37420209230bSgjelinek 	 * This validation is the one we expect to fail if the user specified
37430209230bSgjelinek 	 * an invalid configuration (too many cpus) for this system.
37440209230bSgjelinek 	 */
37450209230bSgjelinek 	if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) {
37460209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
37470209230bSgjelinek 		goto done;
37480209230bSgjelinek 	}
37499acbbeafSnn 
37500209230bSgjelinek 	/*
37510209230bSgjelinek 	 * Commit the dynamic configuration but not the pool configuration
37520209230bSgjelinek 	 * file.
37530209230bSgjelinek 	 */
37540209230bSgjelinek 	if (pool_conf_commit(pconf, 1) != PO_SUCCESS)
37550209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37569acbbeafSnn 
37570209230bSgjelinek done:
37580209230bSgjelinek 	(void) pool_conf_close(pconf);
37590209230bSgjelinek 	return (res);
37609acbbeafSnn }
37619acbbeafSnn 
37629acbbeafSnn static int
37630209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset,
37640209230bSgjelinek     struct zone_psettab *pset_tab)
37650209230bSgjelinek {
37660209230bSgjelinek 	int nfound = 0;
37670209230bSgjelinek 	pool_elem_t *pe;
37680209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
37690209230bSgjelinek 	uint64_t val_uint;
37700209230bSgjelinek 
37710209230bSgjelinek 	if (pool != NULL) {
37720209230bSgjelinek 		pe = pool_to_elem(pconf, pool);
37730209230bSgjelinek 		if (pool_get_property(pconf, pe, "pool.importance", pv)
37740209230bSgjelinek 		    != POC_INVAL) {
37750209230bSgjelinek 			int64_t val_int;
37760209230bSgjelinek 
37770209230bSgjelinek 			(void) pool_value_get_int64(pv, &val_int);
37780209230bSgjelinek 			(void) snprintf(pset_tab->zone_importance,
37790209230bSgjelinek 			    sizeof (pset_tab->zone_importance), "%d", val_int);
37800209230bSgjelinek 			nfound++;
37810209230bSgjelinek 		}
37829acbbeafSnn 	}
37839acbbeafSnn 
37840209230bSgjelinek 	if (pset != NULL) {
37850209230bSgjelinek 		pe = pool_resource_to_elem(pconf, pset);
37860209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) {
37870209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
37880209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_min,
37890209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_min), "%u", val_uint);
37900209230bSgjelinek 			nfound++;
37910209230bSgjelinek 		}
37929acbbeafSnn 
37930209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) {
37940209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
37950209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_max,
37960209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_max), "%u", val_uint);
37970209230bSgjelinek 			nfound++;
37980209230bSgjelinek 		}
37999acbbeafSnn 	}
38009acbbeafSnn 
38010209230bSgjelinek 	pool_value_free(pv);
38029acbbeafSnn 
38030209230bSgjelinek 	if (nfound == 3)
38040209230bSgjelinek 		return (PO_SUCCESS);
38050209230bSgjelinek 
38060209230bSgjelinek 	return (PO_FAIL);
38079acbbeafSnn }
38089acbbeafSnn 
38090209230bSgjelinek /*
38100209230bSgjelinek  * Determine if a tmp pool is configured and if so, if the configuration is
38110209230bSgjelinek  * still valid or if it has been changed since the tmp pool was created.
38120209230bSgjelinek  * If the tmp pool configuration is no longer valid, delete the tmp pool.
38130209230bSgjelinek  *
38140209230bSgjelinek  * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration.
38150209230bSgjelinek  */
38169acbbeafSnn static int
38170209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err,
38180209230bSgjelinek     int err_size, struct zone_psettab *pset_tab, boolean_t *exists)
38199acbbeafSnn {
38200209230bSgjelinek 	int res = Z_OK;
38210209230bSgjelinek 	pool_t *pool;
38220209230bSgjelinek 	pool_resource_t *pset;
38230209230bSgjelinek 	struct zone_psettab pset_current;
38247c478bd9Sstevel@tonic-gate 
38250209230bSgjelinek 	*exists = B_FALSE;
38267c478bd9Sstevel@tonic-gate 
38270209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
38280209230bSgjelinek 	    != PO_SUCCESS) {
38290209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
38300209230bSgjelinek 		return (res);
38310209230bSgjelinek 	}
38327c478bd9Sstevel@tonic-gate 
38330209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
38340209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
38350209230bSgjelinek 
38360209230bSgjelinek 	if (pool == NULL && pset == NULL) {
38370209230bSgjelinek 		/* no tmp pool configured */
38380209230bSgjelinek 		goto done;
38397c478bd9Sstevel@tonic-gate 	}
38409acbbeafSnn 
38410209230bSgjelinek 	/*
38420209230bSgjelinek 	 * If an existing tmp pool for this zone is configured with the proper
38430209230bSgjelinek 	 * settings, then the tmp pool is valid.
38440209230bSgjelinek 	 */
38450209230bSgjelinek 	if (get_running_tmp_pset(pconf, pool, pset, &pset_current)
38460209230bSgjelinek 	    == PO_SUCCESS &&
38470209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_min,
38480209230bSgjelinek 	    pset_current.zone_ncpu_min) == 0 &&
38490209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_max,
38500209230bSgjelinek 	    pset_current.zone_ncpu_max) == 0 &&
38510209230bSgjelinek 	    strcmp(pset_tab->zone_importance,
38520209230bSgjelinek 	    pset_current.zone_importance) == 0) {
38530209230bSgjelinek 		*exists = B_TRUE;
38547c478bd9Sstevel@tonic-gate 
38550209230bSgjelinek 	} else {
38560209230bSgjelinek 		/*
38570209230bSgjelinek 		 * An out-of-date tmp pool configuration exists.  Delete it
38580209230bSgjelinek 		 * so that we can create the correct tmp pool config.
38590209230bSgjelinek 		 */
38600209230bSgjelinek 		if (pset != NULL &&
38610209230bSgjelinek 		    pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
38620209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38630209230bSgjelinek 			goto done;
38640209230bSgjelinek 		}
38659acbbeafSnn 
38660209230bSgjelinek 		if (pool != NULL &&
38670209230bSgjelinek 		    pool_destroy(pconf, pool) != PO_SUCCESS) {
38680209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38690209230bSgjelinek 			goto done;
38700209230bSgjelinek 		}
38719acbbeafSnn 
38720209230bSgjelinek 		/* commit dynamic config */
38730209230bSgjelinek 		if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
38740209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38750209230bSgjelinek 	}
38760209230bSgjelinek 
38770209230bSgjelinek done:
38780209230bSgjelinek 	(void) pool_conf_close(pconf);
38790209230bSgjelinek 
38800209230bSgjelinek 	return (res);
3881ffbafc53Scomay }
3882ffbafc53Scomay 
3883ffbafc53Scomay /*
38840209230bSgjelinek  * Destroy any existing tmp pool.
3885ffbafc53Scomay  */
38860209230bSgjelinek int
38870209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size)
3888ffbafc53Scomay {
38890209230bSgjelinek 	int status;
38900209230bSgjelinek 	int res = Z_OK;
38910209230bSgjelinek 	pool_conf_t *pconf;
38920209230bSgjelinek 	pool_t *pool;
38930209230bSgjelinek 	pool_resource_t *pset;
38940209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
3895ffbafc53Scomay 
38960209230bSgjelinek 	/* if pools not enabled then nothing to do */
38970209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
38980209230bSgjelinek 		return (Z_OK);
3899ffbafc53Scomay 
39000209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
39010209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39029acbbeafSnn 
39030209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
3904ffbafc53Scomay 
39050209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
39060209230bSgjelinek 	    != PO_SUCCESS) {
39070209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39080209230bSgjelinek 		pool_conf_free(pconf);
39090209230bSgjelinek 		return (res);
3910ffbafc53Scomay 	}
3911ffbafc53Scomay 
39120209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
39130209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
3914ffbafc53Scomay 
39150209230bSgjelinek 	if (pool == NULL && pset == NULL) {
39160209230bSgjelinek 		/* nothing to destroy, we're done */
39170209230bSgjelinek 		goto done;
3918ffbafc53Scomay 	}
3919ffbafc53Scomay 
39200209230bSgjelinek 	if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
39210209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39220209230bSgjelinek 		goto done;
3923ffbafc53Scomay 	}
3924ffbafc53Scomay 
39250209230bSgjelinek 	if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) {
39260209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39270209230bSgjelinek 		goto done;
3928ffbafc53Scomay 	}
3929ffbafc53Scomay 
39300209230bSgjelinek 	/* commit dynamic config */
39310209230bSgjelinek 	if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
39320209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39330209230bSgjelinek 
39340209230bSgjelinek done:
39350209230bSgjelinek 	(void) pool_conf_close(pconf);
39360209230bSgjelinek 	pool_conf_free(pconf);
39370209230bSgjelinek 
39380209230bSgjelinek 	return (res);
3939ffbafc53Scomay }
3940ffbafc53Scomay 
3941ffbafc53Scomay /*
39420209230bSgjelinek  * Attempt to bind to a tmp pool for this zone.  If there is no tmp pool
39430209230bSgjelinek  * configured, we just return Z_OK.
3944ffbafc53Scomay  *
39450209230bSgjelinek  * We either attempt to create the tmp pool for this zone or rebind to an
39460209230bSgjelinek  * existing tmp pool for this zone.
39470209230bSgjelinek  *
39480209230bSgjelinek  * Rebinding is used when a zone with a tmp pool reboots so that we don't have
39490209230bSgjelinek  * to recreate the tmp pool.  To do this we need to be sure we work correctly
39500209230bSgjelinek  * for the following cases:
39510209230bSgjelinek  *
39520209230bSgjelinek  *	- there is an existing, properly configured tmp pool.
39530209230bSgjelinek  *	- zonecfg added tmp pool after zone was booted, must now create.
39540209230bSgjelinek  *	- zonecfg updated tmp pool config after zone was booted, in this case
39550209230bSgjelinek  *	  we destroy the old tmp pool and create a new one.
3956ffbafc53Scomay  */
3957ffbafc53Scomay int
39580209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
39590209230bSgjelinek     int err_size)
3960ffbafc53Scomay {
39610209230bSgjelinek 	struct zone_psettab pset_tab;
39620209230bSgjelinek 	int err;
39630209230bSgjelinek 	int status;
39640209230bSgjelinek 	pool_conf_t *pconf;
39650209230bSgjelinek 	boolean_t exists;
39660209230bSgjelinek 	char zone_name[ZONENAME_MAX];
39670209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
39680209230bSgjelinek 
39690209230bSgjelinek 	(void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name));
39700209230bSgjelinek 
39710209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
39720209230bSgjelinek 
39730209230bSgjelinek 	/* if no temporary pool configured, we're done */
39740209230bSgjelinek 	if (err == Z_NO_ENTRY)
39750209230bSgjelinek 		return (Z_OK);
3976ffbafc53Scomay 
3977ffbafc53Scomay 	/*
39780209230bSgjelinek 	 * importance might not have a value but we need to validate it here,
39790209230bSgjelinek 	 * so set the default.
3980ffbafc53Scomay 	 */
39810209230bSgjelinek 	if (pset_tab.zone_importance[0] == '\0')
39820209230bSgjelinek 		(void) strlcpy(pset_tab.zone_importance, "1",
39830209230bSgjelinek 		    sizeof (pset_tab.zone_importance));
39849acbbeafSnn 
39850209230bSgjelinek 	/* if pools not enabled, enable them now */
39860209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
39870209230bSgjelinek 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS)
39880209230bSgjelinek 			return (Z_POOL_ENABLE);
3989ffbafc53Scomay 	}
3990ffbafc53Scomay 
39910209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
39920209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39930209230bSgjelinek 
39940209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
39950209230bSgjelinek 
3996ffbafc53Scomay 	/*
39970209230bSgjelinek 	 * Check if a valid tmp pool/pset already exists.  If so, we just
39980209230bSgjelinek 	 * reuse it.
3999ffbafc53Scomay 	 */
40000209230bSgjelinek 	if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size,
40010209230bSgjelinek 	    &pset_tab, &exists)) != Z_OK) {
40020209230bSgjelinek 		pool_conf_free(pconf);
40030209230bSgjelinek 		return (err);
40040209230bSgjelinek 	}
4005ffbafc53Scomay 
40060209230bSgjelinek 	if (!exists)
40070209230bSgjelinek 		err = create_tmp_pool(pool_err, err_size, pconf, tmp_name,
40080209230bSgjelinek 		    &pset_tab);
40090209230bSgjelinek 
40100209230bSgjelinek 	pool_conf_free(pconf);
40110209230bSgjelinek 
40120209230bSgjelinek 	if (err != Z_OK)
40130209230bSgjelinek 		return (err);
40140209230bSgjelinek 
40150209230bSgjelinek 	/* Bind the zone to the pool. */
40160209230bSgjelinek 	if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS)
40170209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL_BIND));
40180209230bSgjelinek 
40190209230bSgjelinek 	return (Z_OK);
4020ffbafc53Scomay }
4021ffbafc53Scomay 
40220209230bSgjelinek /*
40230209230bSgjelinek  * Attempt to bind to a permanent pool for this zone.  If there is no
40240209230bSgjelinek  * permanent pool configured, we just return Z_OK.
40250209230bSgjelinek  */
40267c478bd9Sstevel@tonic-gate int
40270209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
40280209230bSgjelinek     int err_size)
40297c478bd9Sstevel@tonic-gate {
40300209230bSgjelinek 	pool_conf_t *poolconf;
40310209230bSgjelinek 	pool_t *pool;
40320209230bSgjelinek 	char poolname[MAXPATHLEN];
40330209230bSgjelinek 	int status;
40340209230bSgjelinek 	int error;
4035108322fbScarlsonj 
40360209230bSgjelinek 	/*
40370209230bSgjelinek 	 * Find the pool mentioned in the zone configuration, and bind to it.
40380209230bSgjelinek 	 */
40390209230bSgjelinek 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
40400209230bSgjelinek 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
40410209230bSgjelinek 		/*
40420209230bSgjelinek 		 * The property is not set on the zone, so the pool
40430209230bSgjelinek 		 * should be bound to the default pool.  But that's
40440209230bSgjelinek 		 * already done by the kernel, so we can just return.
40450209230bSgjelinek 		 */
40467c478bd9Sstevel@tonic-gate 		return (Z_OK);
40477c478bd9Sstevel@tonic-gate 	}
40480209230bSgjelinek 	if (error != Z_OK) {
40490209230bSgjelinek 		/*
40500209230bSgjelinek 		 * Not an error, even though it shouldn't be happening.
40510209230bSgjelinek 		 */
40520209230bSgjelinek 		return (Z_OK);
40530209230bSgjelinek 	}
40540209230bSgjelinek 	/*
40550209230bSgjelinek 	 * Don't do anything if pools aren't enabled.
40560209230bSgjelinek 	 */
40570209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
40580209230bSgjelinek 		return (Z_POOLS_NOT_ACTIVE);
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate 	/*
40610209230bSgjelinek 	 * Try to provide a sane error message if the requested pool doesn't
40620209230bSgjelinek 	 * exist.
40637c478bd9Sstevel@tonic-gate 	 */
40640209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
40650209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40667c478bd9Sstevel@tonic-gate 
40670209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
40680209230bSgjelinek 	    PO_SUCCESS) {
40690209230bSgjelinek 		pool_conf_free(poolconf);
40700209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40710209230bSgjelinek 	}
40720209230bSgjelinek 	pool = pool_get_pool(poolconf, poolname);
40730209230bSgjelinek 	(void) pool_conf_close(poolconf);
40740209230bSgjelinek 	pool_conf_free(poolconf);
40750209230bSgjelinek 	if (pool == NULL)
40760209230bSgjelinek 		return (Z_NO_POOL);
40777c478bd9Sstevel@tonic-gate 
40787c478bd9Sstevel@tonic-gate 	/*
40790209230bSgjelinek 	 * Bind the zone to the pool.
40807c478bd9Sstevel@tonic-gate 	 */
40810209230bSgjelinek 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
40820209230bSgjelinek 		/* if bind fails, return poolname for the error msg */
40830209230bSgjelinek 		(void) strlcpy(pool_err, poolname, err_size);
40840209230bSgjelinek 		return (Z_POOL_BIND);
40857c478bd9Sstevel@tonic-gate 	}
40860209230bSgjelinek 
40870209230bSgjelinek 	return (Z_OK);
40880209230bSgjelinek }
40890209230bSgjelinek 
40900209230bSgjelinek 
40910209230bSgjelinek static boolean_t
40920209230bSgjelinek svc_enabled(char *svc_name)
40930209230bSgjelinek {
40940209230bSgjelinek 	scf_simple_prop_t	*prop;
40950209230bSgjelinek 	boolean_t		found = B_FALSE;
40960209230bSgjelinek 
40970209230bSgjelinek 	prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL,
40980209230bSgjelinek 	    SCF_PROPERTY_ENABLED);
40990209230bSgjelinek 
41000209230bSgjelinek 	if (scf_simple_prop_numvalues(prop) == 1 &&
41010209230bSgjelinek 	    *scf_simple_prop_next_boolean(prop) != 0)
41020209230bSgjelinek 		found = B_TRUE;
41030209230bSgjelinek 
41040209230bSgjelinek 	scf_simple_prop_free(prop);
41050209230bSgjelinek 
41060209230bSgjelinek 	return (found);
41077c478bd9Sstevel@tonic-gate }
41087c478bd9Sstevel@tonic-gate 
41090209230bSgjelinek /*
41100209230bSgjelinek  * If the zone has capped-memory, make sure the rcap service is enabled.
41110209230bSgjelinek  */
41127c478bd9Sstevel@tonic-gate int
41130209230bSgjelinek zonecfg_enable_rcapd(char *err, int size)
41147c478bd9Sstevel@tonic-gate {
41150209230bSgjelinek 	if (!svc_enabled(RCAP_SERVICE) &&
41160209230bSgjelinek 	    smf_enable_instance(RCAP_SERVICE, 0) == -1) {
41170209230bSgjelinek 		(void) strlcpy(err, scf_strerror(scf_error()), size);
41180209230bSgjelinek 		return (Z_SYSTEM);
41190209230bSgjelinek 	}
41207c478bd9Sstevel@tonic-gate 
41217c478bd9Sstevel@tonic-gate 	return (Z_OK);
41227c478bd9Sstevel@tonic-gate }
41237c478bd9Sstevel@tonic-gate 
41240209230bSgjelinek /*
41250209230bSgjelinek  * Return true if pset has cpu range specified and poold is not enabled.
41260209230bSgjelinek  */
41270209230bSgjelinek boolean_t
41280209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle)
41299acbbeafSnn {
41300209230bSgjelinek 	struct zone_psettab pset_tab;
41310209230bSgjelinek 	int min, max;
41329acbbeafSnn 	int err;
41339acbbeafSnn 
41340209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
41350209230bSgjelinek 
41360209230bSgjelinek 	/* if no temporary pool configured, we're done */
41370209230bSgjelinek 	if (err == Z_NO_ENTRY)
41380209230bSgjelinek 		return (B_FALSE);
41390209230bSgjelinek 
41400209230bSgjelinek 	min = atoi(pset_tab.zone_ncpu_min);
41410209230bSgjelinek 	max = atoi(pset_tab.zone_ncpu_max);
41420209230bSgjelinek 
41430209230bSgjelinek 	/* range not specified, no need for poold */
41440209230bSgjelinek 	if (min == max)
41450209230bSgjelinek 		return (B_FALSE);
41460209230bSgjelinek 
41470209230bSgjelinek 	/* we have a range, check if poold service is enabled */
41480209230bSgjelinek 	if (svc_enabled(POOLD_SERVICE))
41490209230bSgjelinek 		return (B_FALSE);
41500209230bSgjelinek 
41510209230bSgjelinek 	return (B_TRUE);
41520209230bSgjelinek }
41530209230bSgjelinek 
41540209230bSgjelinek static int
41550209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize)
41560209230bSgjelinek {
41570209230bSgjelinek 	int status;
41580209230bSgjelinek 	pool_conf_t *poolconf;
41590209230bSgjelinek 	pool_t *pool;
41600209230bSgjelinek 	pool_elem_t *pe;
41610209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
41620209230bSgjelinek 	const char *sched_str;
41630209230bSgjelinek 
41640209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
41650209230bSgjelinek 		return (Z_NO_POOL);
41660209230bSgjelinek 
41670209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
41680209230bSgjelinek 		return (Z_NO_POOL);
41690209230bSgjelinek 
41700209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
41710209230bSgjelinek 	    PO_SUCCESS) {
41720209230bSgjelinek 		pool_conf_free(poolconf);
41730209230bSgjelinek 		return (Z_NO_POOL);
41749acbbeafSnn 	}
41759acbbeafSnn 
41760209230bSgjelinek 	if ((pool = pool_get_pool(poolconf, poolname)) == NULL) {
41770209230bSgjelinek 		(void) pool_conf_close(poolconf);
41780209230bSgjelinek 		pool_conf_free(poolconf);
41790209230bSgjelinek 		return (Z_NO_POOL);
41809acbbeafSnn 	}
41819acbbeafSnn 
41820209230bSgjelinek 	pe = pool_to_elem(poolconf, pool);
41830209230bSgjelinek 	if (pool_get_property(poolconf, pe, "pool.scheduler", pv)
41840209230bSgjelinek 	    != POC_INVAL) {
41850209230bSgjelinek 		(void) pool_value_get_string(pv, &sched_str);
41860209230bSgjelinek 		if (strlcpy(class, sched_str, clsize) >= clsize)
41870209230bSgjelinek 			return (Z_TOO_BIG);
41880209230bSgjelinek 	}
41899acbbeafSnn 
41900209230bSgjelinek 	(void) pool_conf_close(poolconf);
41910209230bSgjelinek 	pool_conf_free(poolconf);
41920209230bSgjelinek 	return (Z_OK);
41939acbbeafSnn }
41949acbbeafSnn 
4195facf4a8dSllai /*
41960209230bSgjelinek  * Get the default scheduling class for the zone.  This will either be the
41970209230bSgjelinek  * class set on the zone's pool or the system default scheduling class.
4198facf4a8dSllai  */
4199facf4a8dSllai int
42000209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize)
4201facf4a8dSllai {
42020209230bSgjelinek 	char poolname[MAXPATHLEN];
4203facf4a8dSllai 
42040209230bSgjelinek 	if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) {
42050209230bSgjelinek 		/* check if the zone's pool specified a sched class */
42060209230bSgjelinek 		if (get_pool_sched_class(poolname, class, clsize) == Z_OK)
42070209230bSgjelinek 			return (Z_OK);
42080209230bSgjelinek 	}
4209facf4a8dSllai 
42100209230bSgjelinek 	if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1)
4211facf4a8dSllai 		return (Z_TOO_BIG);
42120209230bSgjelinek 
4213facf4a8dSllai 	return (Z_OK);
4214facf4a8dSllai }
4215facf4a8dSllai 
42160209230bSgjelinek int
42170209230bSgjelinek zonecfg_setfsent(zone_dochandle_t handle)
42187c478bd9Sstevel@tonic-gate {
42190209230bSgjelinek 	return (zonecfg_setent(handle));
42207c478bd9Sstevel@tonic-gate }
42217c478bd9Sstevel@tonic-gate 
42227c478bd9Sstevel@tonic-gate int
42230209230bSgjelinek zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
42247c478bd9Sstevel@tonic-gate {
42250209230bSgjelinek 	xmlNodePtr cur, options;
42260209230bSgjelinek 	char options_str[MAX_MNTOPT_STR];
42270209230bSgjelinek 	int err;
42287c478bd9Sstevel@tonic-gate 
42290209230bSgjelinek 	if (handle == NULL)
42307c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
42317c478bd9Sstevel@tonic-gate 
42320209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
42330209230bSgjelinek 		return (Z_NO_ENTRY);
42340209230bSgjelinek 
42350209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
42360209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
42370209230bSgjelinek 			break;
42380209230bSgjelinek 	if (cur == NULL) {
42390209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
42400209230bSgjelinek 		return (Z_NO_ENTRY);
4241108322fbScarlsonj 	}
4242108322fbScarlsonj 
42430209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
42440209230bSgjelinek 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
42450209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
42460209230bSgjelinek 		return (err);
42477c478bd9Sstevel@tonic-gate 	}
42487c478bd9Sstevel@tonic-gate 
42490209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
42500209230bSgjelinek 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
42510209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
42520209230bSgjelinek 		return (err);
42530209230bSgjelinek 	}
42540209230bSgjelinek 
42550209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
42560209230bSgjelinek 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
42570209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
42580209230bSgjelinek 		return (err);
42590209230bSgjelinek 	}
42600209230bSgjelinek 
42610209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
42620209230bSgjelinek 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
42630209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
42640209230bSgjelinek 		return (err);
42650209230bSgjelinek 	}
42660209230bSgjelinek 
42670209230bSgjelinek 	/* OK for options to be NULL */
42680209230bSgjelinek 	tabptr->zone_fs_options = NULL;
42690209230bSgjelinek 	for (options = cur->xmlChildrenNode; options != NULL;
42700209230bSgjelinek 	    options = options->next) {
42710209230bSgjelinek 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
42720209230bSgjelinek 		    sizeof (options_str)) != Z_OK)
42730209230bSgjelinek 			break;
42740209230bSgjelinek 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
42757c478bd9Sstevel@tonic-gate 			break;
42767c478bd9Sstevel@tonic-gate 	}
42770209230bSgjelinek 
42780209230bSgjelinek 	handle->zone_dh_cur = cur->next;
42790209230bSgjelinek 	return (Z_OK);
42807c478bd9Sstevel@tonic-gate }
42817c478bd9Sstevel@tonic-gate 
42827c478bd9Sstevel@tonic-gate int
42830209230bSgjelinek zonecfg_endfsent(zone_dochandle_t handle)
42847c478bd9Sstevel@tonic-gate {
42850209230bSgjelinek 	return (zonecfg_endent(handle));
42867c478bd9Sstevel@tonic-gate }
42877c478bd9Sstevel@tonic-gate 
42887c478bd9Sstevel@tonic-gate int
42890209230bSgjelinek zonecfg_setipdent(zone_dochandle_t handle)
42907c478bd9Sstevel@tonic-gate {
42910209230bSgjelinek 	return (zonecfg_setent(handle));
42920209230bSgjelinek }
42930209230bSgjelinek 
42940209230bSgjelinek int
42950209230bSgjelinek zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
42960209230bSgjelinek {
42970209230bSgjelinek 	xmlNodePtr cur;
42987c478bd9Sstevel@tonic-gate 	int err;
42997c478bd9Sstevel@tonic-gate 
43000209230bSgjelinek 	if (handle == NULL)
43010209230bSgjelinek 		return (Z_INVAL);
43027c478bd9Sstevel@tonic-gate 
43030209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
43040209230bSgjelinek 		return (Z_NO_ENTRY);
43057c478bd9Sstevel@tonic-gate 
43060209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
43070209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
43080209230bSgjelinek 			break;
43090209230bSgjelinek 	if (cur == NULL) {
43100209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
43110209230bSgjelinek 		return (Z_NO_ENTRY);
43120209230bSgjelinek 	}
43137c478bd9Sstevel@tonic-gate 
43140209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
43150209230bSgjelinek 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
43160209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
43170209230bSgjelinek 		return (err);
43187c478bd9Sstevel@tonic-gate 	}
43197c478bd9Sstevel@tonic-gate 
43200209230bSgjelinek 	handle->zone_dh_cur = cur->next;
43210209230bSgjelinek 	return (Z_OK);
43227c478bd9Sstevel@tonic-gate }
43237c478bd9Sstevel@tonic-gate 
43240209230bSgjelinek int
43250209230bSgjelinek zonecfg_endipdent(zone_dochandle_t handle)
43267c478bd9Sstevel@tonic-gate {
43270209230bSgjelinek 	return (zonecfg_endent(handle));
43287c478bd9Sstevel@tonic-gate }
43297c478bd9Sstevel@tonic-gate 
4330108322fbScarlsonj int
43310209230bSgjelinek zonecfg_setnwifent(zone_dochandle_t handle)
4332108322fbScarlsonj {
43330209230bSgjelinek 	return (zonecfg_setent(handle));
4334108322fbScarlsonj }
4335108322fbScarlsonj 
4336108322fbScarlsonj int
43370209230bSgjelinek zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
4338108322fbScarlsonj {
43390209230bSgjelinek 	xmlNodePtr cur;
43400209230bSgjelinek 	int err;
4341108322fbScarlsonj 
43420209230bSgjelinek 	if (handle == NULL)
43430209230bSgjelinek 		return (Z_INVAL);
43440209230bSgjelinek 
43450209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
43460209230bSgjelinek 		return (Z_NO_ENTRY);
43470209230bSgjelinek 
43480209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
43490209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
4350108322fbScarlsonj 			break;
43510209230bSgjelinek 	if (cur == NULL) {
43520209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
43530209230bSgjelinek 		return (Z_NO_ENTRY);
4354108322fbScarlsonj 	}
43550209230bSgjelinek 
43560209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
43570209230bSgjelinek 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
43580209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
43590209230bSgjelinek 		return (err);
43600209230bSgjelinek 	}
43610209230bSgjelinek 
43620209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
43630209230bSgjelinek 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
43640209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
43650209230bSgjelinek 		return (err);
4366108322fbScarlsonj 	}
43670209230bSgjelinek 
43680209230bSgjelinek 	handle->zone_dh_cur = cur->next;
43690209230bSgjelinek 	return (Z_OK);
4370108322fbScarlsonj }
4371108322fbScarlsonj 
43720209230bSgjelinek int
43730209230bSgjelinek zonecfg_endnwifent(zone_dochandle_t handle)
43747c478bd9Sstevel@tonic-gate {
43750209230bSgjelinek 	return (zonecfg_endent(handle));
43767c478bd9Sstevel@tonic-gate }
43777c478bd9Sstevel@tonic-gate 
43780209230bSgjelinek int
43790209230bSgjelinek zonecfg_setdevent(zone_dochandle_t handle)
43800209230bSgjelinek {
43810209230bSgjelinek 	return (zonecfg_setent(handle));
43820209230bSgjelinek }
43837c478bd9Sstevel@tonic-gate 
43847c478bd9Sstevel@tonic-gate int
43850209230bSgjelinek zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
43867c478bd9Sstevel@tonic-gate {
43870209230bSgjelinek 	xmlNodePtr cur;
43880209230bSgjelinek 	int err;
43897c478bd9Sstevel@tonic-gate 
43900209230bSgjelinek 	if (handle == NULL)
43917c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
43927c478bd9Sstevel@tonic-gate 
43930209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
43940209230bSgjelinek 		return (Z_NO_ENTRY);
43950209230bSgjelinek 
43960209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
43970209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
43980209230bSgjelinek 			break;
43990209230bSgjelinek 	if (cur == NULL) {
44000209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
44010209230bSgjelinek 		return (Z_NO_ENTRY);
44027c478bd9Sstevel@tonic-gate 	}
44037c478bd9Sstevel@tonic-gate 
44040209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
44050209230bSgjelinek 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
44060209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
44070209230bSgjelinek 		return (err);
44087c478bd9Sstevel@tonic-gate 	}
44090209230bSgjelinek 
44100209230bSgjelinek 	handle->zone_dh_cur = cur->next;
44117c478bd9Sstevel@tonic-gate 	return (Z_OK);
44127c478bd9Sstevel@tonic-gate }
44137c478bd9Sstevel@tonic-gate 
44140209230bSgjelinek int
44150209230bSgjelinek zonecfg_enddevent(zone_dochandle_t handle)
44167c478bd9Sstevel@tonic-gate {
44170209230bSgjelinek 	return (zonecfg_endent(handle));
44180209230bSgjelinek }
44197c478bd9Sstevel@tonic-gate 
44200209230bSgjelinek int
44210209230bSgjelinek zonecfg_setrctlent(zone_dochandle_t handle)
44220209230bSgjelinek {
44230209230bSgjelinek 	return (zonecfg_setent(handle));
44240209230bSgjelinek }
44250209230bSgjelinek 
44260209230bSgjelinek int
44270209230bSgjelinek zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
44280209230bSgjelinek {
44290209230bSgjelinek 	xmlNodePtr cur, val;
44300209230bSgjelinek 	struct zone_rctlvaltab *valptr;
44310209230bSgjelinek 	int err;
44320209230bSgjelinek 
44330209230bSgjelinek 	if (handle == NULL)
44340209230bSgjelinek 		return (Z_INVAL);
44350209230bSgjelinek 
44360209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
44370209230bSgjelinek 		return (Z_NO_ENTRY);
44380209230bSgjelinek 
44390209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
44400209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
44410209230bSgjelinek 			break;
44420209230bSgjelinek 	if (cur == NULL) {
44430209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
44440209230bSgjelinek 		return (Z_NO_ENTRY);
44450209230bSgjelinek 	}
44460209230bSgjelinek 
44470209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
44480209230bSgjelinek 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
44490209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
44500209230bSgjelinek 		return (err);
44510209230bSgjelinek 	}
44520209230bSgjelinek 
44530209230bSgjelinek 	tabptr->zone_rctl_valptr = NULL;
44540209230bSgjelinek 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
44550209230bSgjelinek 		valptr = (struct zone_rctlvaltab *)malloc(
44560209230bSgjelinek 		    sizeof (struct zone_rctlvaltab));
44570209230bSgjelinek 		if (valptr == NULL)
44580209230bSgjelinek 			return (Z_NOMEM);
44590209230bSgjelinek 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
44600209230bSgjelinek 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
44610209230bSgjelinek 			break;
44620209230bSgjelinek 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
44630209230bSgjelinek 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
44640209230bSgjelinek 			break;
44650209230bSgjelinek 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
44660209230bSgjelinek 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
44670209230bSgjelinek 			break;
44680209230bSgjelinek 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
44690209230bSgjelinek 			break;
44700209230bSgjelinek 	}
44710209230bSgjelinek 
44720209230bSgjelinek 	handle->zone_dh_cur = cur->next;
44730209230bSgjelinek 	return (Z_OK);
44740209230bSgjelinek }
44750209230bSgjelinek 
44760209230bSgjelinek int
44770209230bSgjelinek zonecfg_endrctlent(zone_dochandle_t handle)
44780209230bSgjelinek {
44790209230bSgjelinek 	return (zonecfg_endent(handle));
44800209230bSgjelinek }
44810209230bSgjelinek 
44820209230bSgjelinek int
44830209230bSgjelinek zonecfg_setattrent(zone_dochandle_t handle)
44840209230bSgjelinek {
44850209230bSgjelinek 	return (zonecfg_setent(handle));
44860209230bSgjelinek }
44870209230bSgjelinek 
44880209230bSgjelinek int
44890209230bSgjelinek zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
44900209230bSgjelinek {
44910209230bSgjelinek 	xmlNodePtr cur;
44920209230bSgjelinek 	int err;
44930209230bSgjelinek 
44940209230bSgjelinek 	if (handle == NULL)
44950209230bSgjelinek 		return (Z_INVAL);
44960209230bSgjelinek 
44970209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
44980209230bSgjelinek 		return (Z_NO_ENTRY);
44990209230bSgjelinek 
45000209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
45010209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
45020209230bSgjelinek 			break;
45030209230bSgjelinek 	if (cur == NULL) {
45040209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
45050209230bSgjelinek 		return (Z_NO_ENTRY);
45060209230bSgjelinek 	}
45070209230bSgjelinek 
45080209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
45090209230bSgjelinek 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
45100209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
45110209230bSgjelinek 		return (err);
45120209230bSgjelinek 	}
45130209230bSgjelinek 
45140209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
45150209230bSgjelinek 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
45160209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
45170209230bSgjelinek 		return (err);
45180209230bSgjelinek 	}
45190209230bSgjelinek 
45200209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
45210209230bSgjelinek 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
45220209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
45230209230bSgjelinek 		return (err);
45240209230bSgjelinek 	}
45250209230bSgjelinek 
45260209230bSgjelinek 	handle->zone_dh_cur = cur->next;
45270209230bSgjelinek 	return (Z_OK);
45280209230bSgjelinek }
45290209230bSgjelinek 
45300209230bSgjelinek int
45310209230bSgjelinek zonecfg_endattrent(zone_dochandle_t handle)
45320209230bSgjelinek {
45330209230bSgjelinek 	return (zonecfg_endent(handle));
45340209230bSgjelinek }
45350209230bSgjelinek 
45360209230bSgjelinek /*
45370209230bSgjelinek  * The privileges available on the system and described in privileges(5)
45380209230bSgjelinek  * fall into four categories with respect to non-global zones:
45390209230bSgjelinek  *
45400209230bSgjelinek  *      Default set of privileges considered safe for all non-global
45410209230bSgjelinek  *      zones.  These privileges are "safe" in the sense that a
45420209230bSgjelinek  *      privileged process in the zone cannot affect processes in any
45430209230bSgjelinek  *      other zone on the system.
45440209230bSgjelinek  *
45450209230bSgjelinek  *      Set of privileges not currently permitted within a non-global
45460209230bSgjelinek  *      zone.  These privileges are considered by default, "unsafe,"
45470209230bSgjelinek  *      and include ones which affect global resources (such as the
45480209230bSgjelinek  *      system clock or physical memory) or are overly broad and cover
45490209230bSgjelinek  *      more than one mechanism in the system.  In other cases, there
45500209230bSgjelinek  *      has not been sufficient virtualization in the parts of the
45510209230bSgjelinek  *      system the privilege covers to allow its use within a
45520209230bSgjelinek  *      non-global zone.
45530209230bSgjelinek  *
45540209230bSgjelinek  *      Set of privileges required in order to get a zone booted and
45550209230bSgjelinek  *      init(1M) started.  These cannot be removed from the zone's
45560209230bSgjelinek  *      privilege set.
45570209230bSgjelinek  *
45580209230bSgjelinek  * All other privileges are optional and are potentially useful for
45590209230bSgjelinek  * processes executing inside a non-global zone.
45600209230bSgjelinek  *
45610209230bSgjelinek  * When privileges are added to the system, a determination needs to be
45620209230bSgjelinek  * made as to which category the privilege belongs to.  Ideally,
45630209230bSgjelinek  * privileges should be fine-grained enough and the mechanisms they cover
45640209230bSgjelinek  * virtualized enough so that they can be made available to non-global
45650209230bSgjelinek  * zones.
45660209230bSgjelinek  */
45670209230bSgjelinek 
45680209230bSgjelinek /*
45690209230bSgjelinek  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
45700209230bSgjelinek  * the privilege string separator can be any character, although it is
45710209230bSgjelinek  * usually a comma character, define these here as well in the event that
45720209230bSgjelinek  * they change or are augmented in the future.
45730209230bSgjelinek  */
45740209230bSgjelinek #define	BASIC_TOKEN		"basic"
45750209230bSgjelinek #define	DEFAULT_TOKEN		"default"
45760209230bSgjelinek #define	ZONE_TOKEN		"zone"
45770209230bSgjelinek #define	TOKEN_PRIV_CHAR		','
45780209230bSgjelinek #define	TOKEN_PRIV_STR		","
45790209230bSgjelinek 
45800209230bSgjelinek typedef struct priv_node {
45810209230bSgjelinek 	struct priv_node	*pn_next;	/* Next privilege */
45820209230bSgjelinek 	char			*pn_priv;	/* Privileges name */
45830209230bSgjelinek } priv_node_t;
45840209230bSgjelinek 
45850209230bSgjelinek /* Privileges lists can differ across brands */
45860209230bSgjelinek typedef struct priv_lists {
45870209230bSgjelinek 	/* Privileges considered safe for all non-global zones of a brand */
45880209230bSgjelinek 	struct priv_node	*pl_default;
45890209230bSgjelinek 
45900209230bSgjelinek 	/* Privileges not permitted for all non-global zones of a brand */
45910209230bSgjelinek 	struct priv_node	*pl_prohibited;
45920209230bSgjelinek 
45930209230bSgjelinek 	/* Privileges required for all non-global zones of a brand */
45940209230bSgjelinek 	struct priv_node	*pl_required;
45950209230bSgjelinek } priv_lists_t;
45960209230bSgjelinek 
45970209230bSgjelinek static int
45980209230bSgjelinek priv_lists_cb(void *data, const char *name, const char *set)
45990209230bSgjelinek {
46000209230bSgjelinek 	priv_lists_t *plp = (priv_lists_t *)data;
46010209230bSgjelinek 	priv_node_t *pnp;
46020209230bSgjelinek 
46030209230bSgjelinek 	/* Allocate a new priv list node. */
46040209230bSgjelinek 	if ((pnp = malloc(sizeof (*pnp))) == NULL)
46050209230bSgjelinek 		return (-1);
46060209230bSgjelinek 	if ((pnp->pn_priv = strdup(name)) == NULL) {
46070209230bSgjelinek 		free(pnp);
46080209230bSgjelinek 		return (-1);
46090209230bSgjelinek 	}
46100209230bSgjelinek 
46110209230bSgjelinek 	/* Insert the new priv list node into the right list */
46120209230bSgjelinek 	if (strcmp(set, "default") == 0) {
46130209230bSgjelinek 		pnp->pn_next = plp->pl_default;
46140209230bSgjelinek 		plp->pl_default = pnp;
46150209230bSgjelinek 	} else if (strcmp(set, "prohibited") == 0) {
46160209230bSgjelinek 		pnp->pn_next = plp->pl_prohibited;
46170209230bSgjelinek 		plp->pl_prohibited = pnp;
46180209230bSgjelinek 	} else if (strcmp(set, "required") == 0) {
46190209230bSgjelinek 		pnp->pn_next = plp->pl_required;
46200209230bSgjelinek 		plp->pl_required = pnp;
46210209230bSgjelinek 	} else {
46220209230bSgjelinek 		free(pnp->pn_priv);
46230209230bSgjelinek 		free(pnp);
46240209230bSgjelinek 		return (-1);
46250209230bSgjelinek 	}
46260209230bSgjelinek 	return (0);
46270209230bSgjelinek }
46280209230bSgjelinek 
46290209230bSgjelinek static void
46300209230bSgjelinek priv_lists_destroy(priv_lists_t *plp)
46310209230bSgjelinek {
46320209230bSgjelinek 	priv_node_t *pnp;
46330209230bSgjelinek 
46340209230bSgjelinek 	assert(plp != NULL);
46350209230bSgjelinek 
46360209230bSgjelinek 	while ((pnp = plp->pl_default) != NULL) {
46370209230bSgjelinek 		plp->pl_default = pnp->pn_next;
46380209230bSgjelinek 		free(pnp->pn_priv);
46390209230bSgjelinek 		free(pnp);
46400209230bSgjelinek 	}
46410209230bSgjelinek 	while ((pnp = plp->pl_prohibited) != NULL) {
46420209230bSgjelinek 		plp->pl_prohibited = pnp->pn_next;
46430209230bSgjelinek 		free(pnp->pn_priv);
46440209230bSgjelinek 		free(pnp);
46450209230bSgjelinek 	}
46460209230bSgjelinek 	while ((pnp = plp->pl_required) != NULL) {
46470209230bSgjelinek 		plp->pl_required = pnp->pn_next;
46480209230bSgjelinek 		free(pnp->pn_priv);
46490209230bSgjelinek 		free(pnp);
46500209230bSgjelinek 	}
46510209230bSgjelinek 	free(plp);
46520209230bSgjelinek }
46530209230bSgjelinek 
46540209230bSgjelinek static int
46550209230bSgjelinek priv_lists_create(zone_dochandle_t handle, priv_lists_t **plpp)
46560209230bSgjelinek {
46570209230bSgjelinek 	priv_lists_t *plp;
46580209230bSgjelinek 	brand_handle_t bh;
46590209230bSgjelinek 	char brand[MAXNAMELEN];
46600209230bSgjelinek 
46610209230bSgjelinek 	if (handle != NULL) {
46620209230bSgjelinek 		if (zonecfg_get_brand(handle, brand, sizeof (brand)) != 0)
46630209230bSgjelinek 			return (Z_BRAND_ERROR);
46640209230bSgjelinek 	} else {
46650209230bSgjelinek 		(void) strlcpy(brand, NATIVE_BRAND_NAME, MAXNAMELEN);
46660209230bSgjelinek 	}
46670209230bSgjelinek 
46680209230bSgjelinek 	if ((bh = brand_open(brand)) == NULL)
46690209230bSgjelinek 		return (Z_BRAND_ERROR);
46700209230bSgjelinek 
46710209230bSgjelinek 	if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) {
46720209230bSgjelinek 		brand_close(bh);
46730209230bSgjelinek 		return (Z_NOMEM);
46740209230bSgjelinek 	}
46750209230bSgjelinek 
46760209230bSgjelinek 	/* construct the privilege lists */
46770209230bSgjelinek 	if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) {
46780209230bSgjelinek 		priv_lists_destroy(plp);
46790209230bSgjelinek 		brand_close(bh);
46800209230bSgjelinek 		return (Z_BRAND_ERROR);
46810209230bSgjelinek 	}
46820209230bSgjelinek 
46830209230bSgjelinek 	brand_close(bh);
46840209230bSgjelinek 	*plpp = plp;
46850209230bSgjelinek 	return (Z_OK);
46860209230bSgjelinek }
46870209230bSgjelinek 
46880209230bSgjelinek static int
46890209230bSgjelinek get_default_privset(priv_set_t *privs, priv_lists_t *plp)
46900209230bSgjelinek {
46910209230bSgjelinek 	priv_node_t *pnp;
46920209230bSgjelinek 	priv_set_t *basic;
46930209230bSgjelinek 
46940209230bSgjelinek 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
46950209230bSgjelinek 	if (basic == NULL)
46960209230bSgjelinek 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
46970209230bSgjelinek 
46980209230bSgjelinek 	priv_union(basic, privs);
46990209230bSgjelinek 	priv_freeset(basic);
47000209230bSgjelinek 
47010209230bSgjelinek 	for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) {
47020209230bSgjelinek 		if (priv_addset(privs, pnp->pn_priv) != 0)
47030209230bSgjelinek 			return (Z_INVAL);
47040209230bSgjelinek 	}
47050209230bSgjelinek 
47060209230bSgjelinek 	return (Z_OK);
47070209230bSgjelinek }
47080209230bSgjelinek 
47090209230bSgjelinek int
47100209230bSgjelinek zonecfg_default_privset(priv_set_t *privs)
47110209230bSgjelinek {
47120209230bSgjelinek 	priv_lists_t *plp;
47130209230bSgjelinek 	int ret;
47140209230bSgjelinek 
47150209230bSgjelinek 	if ((ret = priv_lists_create(NULL, &plp)) != Z_OK)
47160209230bSgjelinek 		return (ret);
47170209230bSgjelinek 	ret = get_default_privset(privs, plp);
47180209230bSgjelinek 	priv_lists_destroy(plp);
47190209230bSgjelinek 	return (ret);
47200209230bSgjelinek }
47210209230bSgjelinek 
47220209230bSgjelinek void
47230209230bSgjelinek append_priv_token(char *priv, char *str, size_t strlen)
47240209230bSgjelinek {
47250209230bSgjelinek 	if (*str != '\0')
47260209230bSgjelinek 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
47270209230bSgjelinek 	(void) strlcat(str, priv, strlen);
47280209230bSgjelinek }
47290209230bSgjelinek 
47300209230bSgjelinek /*
47310209230bSgjelinek  * Verify that the supplied string is a valid privilege limit set for a
47320209230bSgjelinek  * non-global zone.  This string must not only be acceptable to
47330209230bSgjelinek  * priv_str_to_set(3C) which parses it, but it also must resolve to a
47340209230bSgjelinek  * privilege set that includes certain required privileges and lacks
47350209230bSgjelinek  * certain prohibited privileges.
47360209230bSgjelinek  */
47370209230bSgjelinek static int
47380209230bSgjelinek verify_privset(char *privbuf, priv_set_t *privs, char **privname,
47390209230bSgjelinek     boolean_t add_default, priv_lists_t *plp)
47400209230bSgjelinek {
47410209230bSgjelinek 	priv_node_t *pnp;
47420209230bSgjelinek 	char *tmp, *cp, *lasts;
47430209230bSgjelinek 	size_t len;
47440209230bSgjelinek 	priv_set_t *mergeset;
47450209230bSgjelinek 	const char *token;
47460209230bSgjelinek 
47470209230bSgjelinek 	/*
47480209230bSgjelinek 	 * The verification of the privilege string occurs in several
47490209230bSgjelinek 	 * phases.  In the first phase, the supplied string is scanned for
47500209230bSgjelinek 	 * the ZONE_TOKEN token which is not support as part of the
47510209230bSgjelinek 	 * "limitpriv" property.
47520209230bSgjelinek 	 *
47530209230bSgjelinek 	 * Duplicate the supplied privilege string since strtok_r(3C)
47540209230bSgjelinek 	 * tokenizes its input by null-terminating the tokens.
47550209230bSgjelinek 	 */
47560209230bSgjelinek 	if ((tmp = strdup(privbuf)) == NULL)
47570209230bSgjelinek 		return (Z_NOMEM);
47580209230bSgjelinek 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
47590209230bSgjelinek 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
47600209230bSgjelinek 		if (strcmp(cp, ZONE_TOKEN) == 0) {
47610209230bSgjelinek 			free(tmp);
47620209230bSgjelinek 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
47630209230bSgjelinek 				return (Z_NOMEM);
47640209230bSgjelinek 			else
47650209230bSgjelinek 				return (Z_PRIV_UNKNOWN);
47660209230bSgjelinek 		}
47670209230bSgjelinek 	}
47680209230bSgjelinek 	free(tmp);
47690209230bSgjelinek 
47700209230bSgjelinek 	if (add_default) {
47710209230bSgjelinek 		/*
47720209230bSgjelinek 		 * If DEFAULT_TOKEN was specified, a string needs to be
47730209230bSgjelinek 		 * built containing the privileges from the default, safe
47740209230bSgjelinek 		 * set along with those of the "limitpriv" property.
47750209230bSgjelinek 		 */
47760209230bSgjelinek 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
47770209230bSgjelinek 
47780209230bSgjelinek 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
47790209230bSgjelinek 			len += strlen(pnp->pn_priv) + 1;
47800209230bSgjelinek 		tmp = alloca(len);
47810209230bSgjelinek 		*tmp = '\0';
47820209230bSgjelinek 
47830209230bSgjelinek 		append_priv_token(BASIC_TOKEN, tmp, len);
47840209230bSgjelinek 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
47850209230bSgjelinek 			append_priv_token(pnp->pn_priv, tmp, len);
47860209230bSgjelinek 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
47870209230bSgjelinek 		(void) strlcat(tmp, privbuf, len);
47880209230bSgjelinek 	} else {
47890209230bSgjelinek 		tmp = privbuf;
47900209230bSgjelinek 	}
47910209230bSgjelinek 
47920209230bSgjelinek 
47930209230bSgjelinek 	/*
47940209230bSgjelinek 	 * In the next phase, attempt to convert the merged privilege
47950209230bSgjelinek 	 * string into a privilege set.  In the case of an error, either
47960209230bSgjelinek 	 * there was a memory allocation failure or there was an invalid
47970209230bSgjelinek 	 * privilege token in the string.  In either case, return an
47980209230bSgjelinek 	 * appropriate error code but in the event of an invalid token,
47990209230bSgjelinek 	 * allocate a string containing its name and return that back to
48000209230bSgjelinek 	 * the caller.
48010209230bSgjelinek 	 */
48020209230bSgjelinek 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
48030209230bSgjelinek 	if (mergeset == NULL) {
48040209230bSgjelinek 		if (token == NULL)
48050209230bSgjelinek 			return (Z_NOMEM);
48060209230bSgjelinek 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
48070209230bSgjelinek 			*cp = '\0';
48080209230bSgjelinek 		if ((*privname = strdup(token)) == NULL)
48090209230bSgjelinek 			return (Z_NOMEM);
48100209230bSgjelinek 		else
48110209230bSgjelinek 			return (Z_PRIV_UNKNOWN);
48120209230bSgjelinek 	}
48130209230bSgjelinek 
48140209230bSgjelinek 	/*
48150209230bSgjelinek 	 * Next, verify that none of the prohibited zone privileges are
48160209230bSgjelinek 	 * present in the merged privilege set.
48170209230bSgjelinek 	 */
48180209230bSgjelinek 	for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) {
48190209230bSgjelinek 		if (priv_ismember(mergeset, pnp->pn_priv)) {
48200209230bSgjelinek 			priv_freeset(mergeset);
48210209230bSgjelinek 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
48220209230bSgjelinek 				return (Z_NOMEM);
48230209230bSgjelinek 			else
48240209230bSgjelinek 				return (Z_PRIV_PROHIBITED);
48250209230bSgjelinek 		}
48260209230bSgjelinek 	}
48270209230bSgjelinek 
48280209230bSgjelinek 	/*
48290209230bSgjelinek 	 * Finally, verify that all of the required zone privileges are
48300209230bSgjelinek 	 * present in the merged privilege set.
48310209230bSgjelinek 	 */
48320209230bSgjelinek 	for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) {
48330209230bSgjelinek 		if (!priv_ismember(mergeset, pnp->pn_priv)) {
48340209230bSgjelinek 			priv_freeset(mergeset);
48350209230bSgjelinek 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
48360209230bSgjelinek 				return (Z_NOMEM);
48370209230bSgjelinek 			else
48380209230bSgjelinek 				return (Z_PRIV_REQUIRED);
48390209230bSgjelinek 		}
48400209230bSgjelinek 	}
48410209230bSgjelinek 
48420209230bSgjelinek 	priv_copyset(mergeset, privs);
48430209230bSgjelinek 	priv_freeset(mergeset);
48440209230bSgjelinek 	return (Z_OK);
48450209230bSgjelinek }
48460209230bSgjelinek 
48470209230bSgjelinek /*
48480209230bSgjelinek  * Fill in the supplied privilege set with either the default, safe set of
48490209230bSgjelinek  * privileges suitable for a non-global zone, or one based on the
48500209230bSgjelinek  * "limitpriv" property in the zone's configuration.
48510209230bSgjelinek  *
48520209230bSgjelinek  * In the event of an invalid privilege specification in the
48530209230bSgjelinek  * configuration, a string is allocated and returned containing the
48540209230bSgjelinek  * "privilege" causing the issue.  It is the caller's responsibility to
48550209230bSgjelinek  * free this memory when it is done with it.
48560209230bSgjelinek  */
48570209230bSgjelinek int
48580209230bSgjelinek zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
48590209230bSgjelinek     char **privname)
48600209230bSgjelinek {
48610209230bSgjelinek 	priv_lists_t *plp;
48620209230bSgjelinek 	char *cp, *limitpriv = NULL;
48630209230bSgjelinek 	int err, limitlen;
48640209230bSgjelinek 
48650209230bSgjelinek 	/*
48660209230bSgjelinek 	 * Attempt to lookup the "limitpriv" property.  If it does not
48670209230bSgjelinek 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
48680209230bSgjelinek 	 * default, safe privilege set is returned.
48690209230bSgjelinek 	 */
48700209230bSgjelinek 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK)
48710209230bSgjelinek 		return (err);
48720209230bSgjelinek 
48730209230bSgjelinek 	if ((err = priv_lists_create(handle, &plp)) != Z_OK)
48740209230bSgjelinek 		return (err);
48750209230bSgjelinek 
48760209230bSgjelinek 	limitlen = strlen(limitpriv);
48770209230bSgjelinek 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
48780209230bSgjelinek 		free(limitpriv);
48790209230bSgjelinek 		err = get_default_privset(privs, plp);
48800209230bSgjelinek 		priv_lists_destroy(plp);
48810209230bSgjelinek 		return (err);
48820209230bSgjelinek 	}
48830209230bSgjelinek 
48840209230bSgjelinek 	/*
48850209230bSgjelinek 	 * Check if the string DEFAULT_TOKEN is the first token in a list
48860209230bSgjelinek 	 * of privileges.
48870209230bSgjelinek 	 */
48880209230bSgjelinek 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
48890209230bSgjelinek 	if (cp != NULL &&
48900209230bSgjelinek 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
48910209230bSgjelinek 		err = verify_privset(cp + 1, privs, privname, B_TRUE, plp);
48920209230bSgjelinek 	else
48930209230bSgjelinek 		err = verify_privset(limitpriv, privs, privname, B_FALSE, plp);
48940209230bSgjelinek 
48950209230bSgjelinek 	free(limitpriv);
48960209230bSgjelinek 	priv_lists_destroy(plp);
48970209230bSgjelinek 	return (err);
48980209230bSgjelinek }
48990209230bSgjelinek 
49000209230bSgjelinek int
49010209230bSgjelinek zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
49020209230bSgjelinek {
49030209230bSgjelinek 	zone_dochandle_t handle;
49040209230bSgjelinek 	boolean_t found = B_FALSE;
49050209230bSgjelinek 	struct zoneent *ze;
49060209230bSgjelinek 	FILE *cookie;
49070209230bSgjelinek 	int err;
49080209230bSgjelinek 	char *cp;
49090209230bSgjelinek 
49100209230bSgjelinek 	if (zone_name == NULL)
49110209230bSgjelinek 		return (Z_INVAL);
49120209230bSgjelinek 
49130209230bSgjelinek 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
49140209230bSgjelinek 	cp = zonepath + strlen(zonepath);
49150209230bSgjelinek 	while (cp > zonepath && cp[-1] == '/')
49160209230bSgjelinek 		*--cp = '\0';
49170209230bSgjelinek 
49180209230bSgjelinek 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
49190209230bSgjelinek 		if (zonepath[0] == '\0')
49200209230bSgjelinek 			(void) strlcpy(zonepath, "/", rp_sz);
49210209230bSgjelinek 		return (Z_OK);
49220209230bSgjelinek 	}
49230209230bSgjelinek 
49240209230bSgjelinek 	/*
49250209230bSgjelinek 	 * First check the index file.  Because older versions did not have
49260209230bSgjelinek 	 * a copy of the zone path, allow for it to be zero length, in which
49270209230bSgjelinek 	 * case we ignore this result and fall back to the XML files.
49280209230bSgjelinek 	 */
49290209230bSgjelinek 	cookie = setzoneent();
49300209230bSgjelinek 	while ((ze = getzoneent_private(cookie)) != NULL) {
49310209230bSgjelinek 		if (strcmp(ze->zone_name, zone_name) == 0) {
49320209230bSgjelinek 			found = B_TRUE;
49330209230bSgjelinek 			if (ze->zone_path[0] != '\0')
49340209230bSgjelinek 				(void) strlcpy(cp, ze->zone_path,
49350209230bSgjelinek 				    rp_sz - (cp - zonepath));
49360209230bSgjelinek 		}
49370209230bSgjelinek 		free(ze);
49380209230bSgjelinek 		if (found)
49390209230bSgjelinek 			break;
49400209230bSgjelinek 	}
49410209230bSgjelinek 	endzoneent(cookie);
49420209230bSgjelinek 	if (found && *cp != '\0')
49430209230bSgjelinek 		return (Z_OK);
49440209230bSgjelinek 
49450209230bSgjelinek 	/* Fall back to the XML files. */
49460209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL)
49470209230bSgjelinek 		return (Z_NOMEM);
49480209230bSgjelinek 
49490209230bSgjelinek 	/*
49500209230bSgjelinek 	 * Check the snapshot first: if a zone is running, its zonepath
49510209230bSgjelinek 	 * may have changed.
49520209230bSgjelinek 	 */
49530209230bSgjelinek 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
49540209230bSgjelinek 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK)
49550209230bSgjelinek 			return (err);
49560209230bSgjelinek 	}
49570209230bSgjelinek 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
49580209230bSgjelinek 	zonecfg_fini_handle(handle);
49590209230bSgjelinek 	return (err);
49600209230bSgjelinek }
49610209230bSgjelinek 
49620209230bSgjelinek int
49630209230bSgjelinek zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
49640209230bSgjelinek {
49650209230bSgjelinek 	int err;
49660209230bSgjelinek 
49670209230bSgjelinek 	/* This function makes sense for non-global zones only. */
49680209230bSgjelinek 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
49690209230bSgjelinek 		return (Z_BOGUS_ZONE_NAME);
49700209230bSgjelinek 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
49710209230bSgjelinek 		return (err);
49720209230bSgjelinek 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
49730209230bSgjelinek 		return (Z_TOO_BIG);
49740209230bSgjelinek 	return (Z_OK);
49750209230bSgjelinek }
49760209230bSgjelinek 
49770209230bSgjelinek int
49780209230bSgjelinek zone_get_brand(char *zone_name, char *brandname, size_t rp_sz)
49790209230bSgjelinek {
49800209230bSgjelinek 	int err;
49810209230bSgjelinek 	zone_dochandle_t handle;
49820209230bSgjelinek 	char myzone[MAXNAMELEN];
49830209230bSgjelinek 	int myzoneid = getzoneid();
49840209230bSgjelinek 
49850209230bSgjelinek 	/*
49860209230bSgjelinek 	 * If we are not in the global zone, then we don't have the zone
49870209230bSgjelinek 	 * .xml files with the brand name available.  Thus, we are going to
49880209230bSgjelinek 	 * have to ask the kernel for the information.
49890209230bSgjelinek 	 */
49900209230bSgjelinek 	if (myzoneid != GLOBAL_ZONEID) {
49910209230bSgjelinek 		if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone,
49920209230bSgjelinek 		    sizeof (myzone)) < 0)
49930209230bSgjelinek 			return (Z_NO_ZONE);
49940209230bSgjelinek 		if (strncmp(zone_name, myzone, MAXNAMELEN) != NULL)
49950209230bSgjelinek 			return (Z_NO_ZONE);
49960209230bSgjelinek 		err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz);
49970209230bSgjelinek 		if (err < 0)
49980209230bSgjelinek 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
49990209230bSgjelinek 		return (Z_OK);
50000209230bSgjelinek 	}
50010209230bSgjelinek 
50020209230bSgjelinek 	if (strcmp(zone_name, "global") == NULL) {
50030209230bSgjelinek 		(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
50040209230bSgjelinek 		return (0);
50050209230bSgjelinek 	}
50060209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL)
50070209230bSgjelinek 		return (Z_NOMEM);
50080209230bSgjelinek 
50090209230bSgjelinek 	err = zonecfg_get_handle((char *)zone_name, handle);
50100209230bSgjelinek 	if (err == Z_OK)
50110209230bSgjelinek 		err = zonecfg_get_brand(handle, brandname, rp_sz);
50120209230bSgjelinek 
50130209230bSgjelinek 	zonecfg_fini_handle(handle);
50140209230bSgjelinek 	return (err);
50150209230bSgjelinek }
50160209230bSgjelinek 
50170209230bSgjelinek /*
50180209230bSgjelinek  * Return the appropriate root for the active /dev.
50190209230bSgjelinek  * For normal zone, the path is $ZONEPATH/root;
50200209230bSgjelinek  * for scratch zone, the dev path is $ZONEPATH/lu.
50210209230bSgjelinek  */
50220209230bSgjelinek int
50230209230bSgjelinek zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz)
50240209230bSgjelinek {
50250209230bSgjelinek 	int err;
50260209230bSgjelinek 	char *suffix;
50270209230bSgjelinek 	zone_state_t state;
50280209230bSgjelinek 
50290209230bSgjelinek 	/* This function makes sense for non-global zones only. */
50300209230bSgjelinek 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
50310209230bSgjelinek 		return (Z_BOGUS_ZONE_NAME);
50320209230bSgjelinek 	if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK)
50330209230bSgjelinek 		return (err);
50340209230bSgjelinek 
50350209230bSgjelinek 	if (zone_get_state(zone_name, &state) == Z_OK &&
50360209230bSgjelinek 	    state == ZONE_STATE_MOUNTED)
50370209230bSgjelinek 		suffix = "/lu";
50380209230bSgjelinek 	else
50390209230bSgjelinek 		suffix = "/root";
50400209230bSgjelinek 	if (strlcat(devroot, suffix, rp_sz) >= rp_sz)
50410209230bSgjelinek 		return (Z_TOO_BIG);
50420209230bSgjelinek 	return (Z_OK);
50430209230bSgjelinek }
50440209230bSgjelinek 
50450209230bSgjelinek static zone_state_t
50460209230bSgjelinek kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
50470209230bSgjelinek {
50480209230bSgjelinek 	char zoneroot[MAXPATHLEN];
50490209230bSgjelinek 	size_t zlen;
50500209230bSgjelinek 
50510209230bSgjelinek 	assert(kernel_state <= ZONE_MAX_STATE);
50520209230bSgjelinek 	switch (kernel_state) {
50530209230bSgjelinek 		case ZONE_IS_UNINITIALIZED:
50540209230bSgjelinek 			return (ZONE_STATE_READY);
50550209230bSgjelinek 		case ZONE_IS_READY:
50560209230bSgjelinek 			/*
50570209230bSgjelinek 			 * If the zone's root is mounted on $ZONEPATH/lu, then
50580209230bSgjelinek 			 * it's a mounted scratch zone.
50590209230bSgjelinek 			 */
50600209230bSgjelinek 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
50610209230bSgjelinek 			    sizeof (zoneroot)) >= 0) {
50620209230bSgjelinek 				zlen = strlen(zoneroot);
50630209230bSgjelinek 				if (zlen > 3 &&
50640209230bSgjelinek 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
50650209230bSgjelinek 					return (ZONE_STATE_MOUNTED);
50660209230bSgjelinek 			}
50670209230bSgjelinek 			return (ZONE_STATE_READY);
50680209230bSgjelinek 		case ZONE_IS_BOOTING:
50690209230bSgjelinek 		case ZONE_IS_RUNNING:
50700209230bSgjelinek 			return (ZONE_STATE_RUNNING);
50710209230bSgjelinek 		case ZONE_IS_SHUTTING_DOWN:
50720209230bSgjelinek 		case ZONE_IS_EMPTY:
50730209230bSgjelinek 			return (ZONE_STATE_SHUTTING_DOWN);
50740209230bSgjelinek 		case ZONE_IS_DOWN:
50750209230bSgjelinek 		case ZONE_IS_DYING:
50760209230bSgjelinek 		case ZONE_IS_DEAD:
50770209230bSgjelinek 		default:
50780209230bSgjelinek 			return (ZONE_STATE_DOWN);
50790209230bSgjelinek 	}
50800209230bSgjelinek 	/* NOTREACHED */
50810209230bSgjelinek }
50820209230bSgjelinek 
50830209230bSgjelinek int
50840209230bSgjelinek zone_get_state(char *zone_name, zone_state_t *state_num)
50850209230bSgjelinek {
50860209230bSgjelinek 	zone_status_t status;
50870209230bSgjelinek 	zoneid_t zone_id;
50880209230bSgjelinek 	struct zoneent *ze;
50890209230bSgjelinek 	boolean_t found = B_FALSE;
50900209230bSgjelinek 	FILE *cookie;
50910209230bSgjelinek 	char kernzone[ZONENAME_MAX];
50920209230bSgjelinek 	FILE *fp;
50930209230bSgjelinek 
50940209230bSgjelinek 	if (zone_name == NULL)
50950209230bSgjelinek 		return (Z_INVAL);
50960209230bSgjelinek 
50970209230bSgjelinek 	/*
50980209230bSgjelinek 	 * If we're looking at an alternate root, then we need to query the
50990209230bSgjelinek 	 * kernel using the scratch zone name.
51000209230bSgjelinek 	 */
51010209230bSgjelinek 	zone_id = -1;
51020209230bSgjelinek 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
51030209230bSgjelinek 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
51040209230bSgjelinek 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
51050209230bSgjelinek 			    kernzone, sizeof (kernzone)) == 0)
51060209230bSgjelinek 				zone_id = getzoneidbyname(kernzone);
51070209230bSgjelinek 			zonecfg_close_scratch(fp);
51080209230bSgjelinek 		}
51090209230bSgjelinek 	} else {
51100209230bSgjelinek 		zone_id = getzoneidbyname(zone_name);
51110209230bSgjelinek 	}
51120209230bSgjelinek 
51130209230bSgjelinek 	/* check to see if zone is running */
51140209230bSgjelinek 	if (zone_id != -1 &&
51150209230bSgjelinek 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
51160209230bSgjelinek 	    sizeof (status)) >= 0) {
51170209230bSgjelinek 		*state_num = kernel_state_to_user_state(zone_id, status);
51180209230bSgjelinek 		return (Z_OK);
51190209230bSgjelinek 	}
51200209230bSgjelinek 
51210209230bSgjelinek 	cookie = setzoneent();
51220209230bSgjelinek 	while ((ze = getzoneent_private(cookie)) != NULL) {
51230209230bSgjelinek 		if (strcmp(ze->zone_name, zone_name) == 0) {
51240209230bSgjelinek 			found = B_TRUE;
51250209230bSgjelinek 			*state_num = ze->zone_state;
51260209230bSgjelinek 		}
51270209230bSgjelinek 		free(ze);
51280209230bSgjelinek 		if (found)
51290209230bSgjelinek 			break;
51300209230bSgjelinek 	}
51310209230bSgjelinek 	endzoneent(cookie);
51320209230bSgjelinek 	return ((found) ? Z_OK : Z_NO_ZONE);
51330209230bSgjelinek }
51340209230bSgjelinek 
51350209230bSgjelinek int
51360209230bSgjelinek zone_set_state(char *zone, zone_state_t state)
51370209230bSgjelinek {
51380209230bSgjelinek 	struct zoneent ze;
51390209230bSgjelinek 
51400209230bSgjelinek 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
51410209230bSgjelinek 	    state != ZONE_STATE_INCOMPLETE)
51420209230bSgjelinek 		return (Z_INVAL);
51430209230bSgjelinek 
51440209230bSgjelinek 	bzero(&ze, sizeof (ze));
51450209230bSgjelinek 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
51460209230bSgjelinek 	ze.zone_state = state;
51470209230bSgjelinek 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
51480209230bSgjelinek 	return (putzoneent(&ze, PZE_MODIFY));
51490209230bSgjelinek }
51500209230bSgjelinek 
51510209230bSgjelinek /*
51520209230bSgjelinek  * Get id (if any) for specified zone.  There are four possible outcomes:
51530209230bSgjelinek  * - If the string corresponds to the numeric id of an active (booted)
51540209230bSgjelinek  *   zone, sets *zip to the zone id and returns 0.
51550209230bSgjelinek  * - If the string corresponds to the name of an active (booted) zone,
51560209230bSgjelinek  *   sets *zip to the zone id and returns 0.
51570209230bSgjelinek  * - If the string is a name in the configuration but is not booted,
51580209230bSgjelinek  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
51590209230bSgjelinek  * - Otherwise, leaves *zip unchanged and returns -1.
51600209230bSgjelinek  *
51610209230bSgjelinek  * This function acts as an auxiliary filter on the function of the same
51620209230bSgjelinek  * name in libc; the linker binds to this version if libzonecfg exists,
51630209230bSgjelinek  * and the libc version if it doesn't.  Any changes to this version of
51640209230bSgjelinek  * the function should probably be reflected in the libc version as well.
51650209230bSgjelinek  */
51660209230bSgjelinek int
51670209230bSgjelinek zone_get_id(const char *str, zoneid_t *zip)
51680209230bSgjelinek {
51690209230bSgjelinek 	zone_dochandle_t hdl;
51700209230bSgjelinek 	zoneid_t zoneid;
51710209230bSgjelinek 	char *cp;
51720209230bSgjelinek 	int err;
51730209230bSgjelinek 
51740209230bSgjelinek 	/* first try looking for active zone by id */
51750209230bSgjelinek 	errno = 0;
51760209230bSgjelinek 	zoneid = (zoneid_t)strtol(str, &cp, 0);
51770209230bSgjelinek 	if (errno == 0 && cp != str && *cp == '\0' &&
51780209230bSgjelinek 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
51790209230bSgjelinek 		*zip = zoneid;
51800209230bSgjelinek 		return (0);
51810209230bSgjelinek 	}
51820209230bSgjelinek 
51830209230bSgjelinek 	/* then look for active zone by name */
51840209230bSgjelinek 	if ((zoneid = getzoneidbyname(str)) != -1) {
51850209230bSgjelinek 		*zip = zoneid;
51860209230bSgjelinek 		return (0);
51870209230bSgjelinek 	}
51880209230bSgjelinek 
51890209230bSgjelinek 	/* if in global zone, try looking up name in configuration database */
51900209230bSgjelinek 	if (getzoneid() != GLOBAL_ZONEID ||
51910209230bSgjelinek 	    (hdl = zonecfg_init_handle()) == NULL)
51920209230bSgjelinek 		return (-1);
51930209230bSgjelinek 
51940209230bSgjelinek 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
51950209230bSgjelinek 		/* zone exists but isn't active */
51960209230bSgjelinek 		*zip = ZONE_ID_UNDEFINED;
51970209230bSgjelinek 		err = 0;
51980209230bSgjelinek 	} else {
51990209230bSgjelinek 		err = -1;
52000209230bSgjelinek 	}
52010209230bSgjelinek 
52020209230bSgjelinek 	zonecfg_fini_handle(hdl);
52030209230bSgjelinek 	return (err);
52040209230bSgjelinek }
52050209230bSgjelinek 
52060209230bSgjelinek char *
52070209230bSgjelinek zone_state_str(zone_state_t state_num)
52080209230bSgjelinek {
52090209230bSgjelinek 	switch (state_num) {
52100209230bSgjelinek 	case ZONE_STATE_CONFIGURED:
52110209230bSgjelinek 		return (ZONE_STATE_STR_CONFIGURED);
52120209230bSgjelinek 	case ZONE_STATE_INCOMPLETE:
52130209230bSgjelinek 		return (ZONE_STATE_STR_INCOMPLETE);
52140209230bSgjelinek 	case ZONE_STATE_INSTALLED:
52150209230bSgjelinek 		return (ZONE_STATE_STR_INSTALLED);
52160209230bSgjelinek 	case ZONE_STATE_READY:
52170209230bSgjelinek 		return (ZONE_STATE_STR_READY);
52180209230bSgjelinek 	case ZONE_STATE_MOUNTED:
52190209230bSgjelinek 		return (ZONE_STATE_STR_MOUNTED);
52200209230bSgjelinek 	case ZONE_STATE_RUNNING:
52210209230bSgjelinek 		return (ZONE_STATE_STR_RUNNING);
52220209230bSgjelinek 	case ZONE_STATE_SHUTTING_DOWN:
52230209230bSgjelinek 		return (ZONE_STATE_STR_SHUTTING_DOWN);
52240209230bSgjelinek 	case ZONE_STATE_DOWN:
52250209230bSgjelinek 		return (ZONE_STATE_STR_DOWN);
52260209230bSgjelinek 	default:
52270209230bSgjelinek 		return ("unknown");
52280209230bSgjelinek 	}
52290209230bSgjelinek }
52300209230bSgjelinek 
52310209230bSgjelinek /*
52320209230bSgjelinek  * Given a UUID value, find an associated zone name.  This is intended to be
52330209230bSgjelinek  * used by callers who set up some 'default' name (corresponding to the
52340209230bSgjelinek  * expected name for the zone) in the zonename buffer, and thus the function
52350209230bSgjelinek  * doesn't touch this buffer on failure.
52360209230bSgjelinek  */
52370209230bSgjelinek int
52380209230bSgjelinek zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
52390209230bSgjelinek {
52400209230bSgjelinek 	FILE *fp;
52410209230bSgjelinek 	struct zoneent *ze;
52420209230bSgjelinek 	uchar_t *uuid;
52430209230bSgjelinek 
52440209230bSgjelinek 	/*
52450209230bSgjelinek 	 * A small amount of subterfuge via casts is necessary here because
52460209230bSgjelinek 	 * libuuid doesn't use const correctly, but we don't want to export
52470209230bSgjelinek 	 * this brokenness to our clients.
52480209230bSgjelinek 	 */
52490209230bSgjelinek 	uuid = (uchar_t *)uuidin;
52500209230bSgjelinek 	if (uuid_is_null(uuid))
52510209230bSgjelinek 		return (Z_NO_ZONE);
52520209230bSgjelinek 	if ((fp = setzoneent()) == NULL)
52530209230bSgjelinek 		return (Z_NO_ZONE);
52540209230bSgjelinek 	while ((ze = getzoneent_private(fp)) != NULL) {
52550209230bSgjelinek 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
52560209230bSgjelinek 			break;
52570209230bSgjelinek 		free(ze);
52580209230bSgjelinek 	}
52590209230bSgjelinek 	endzoneent(fp);
52600209230bSgjelinek 	if (ze != NULL) {
52610209230bSgjelinek 		(void) strlcpy(zonename, ze->zone_name, namelen);
52620209230bSgjelinek 		free(ze);
52630209230bSgjelinek 		return (Z_OK);
52640209230bSgjelinek 	} else {
52650209230bSgjelinek 		return (Z_NO_ZONE);
52660209230bSgjelinek 	}
52670209230bSgjelinek }
52680209230bSgjelinek 
52690209230bSgjelinek /*
52700209230bSgjelinek  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
52710209230bSgjelinek  * exists but the file doesn't have a value set yet.  Returns an error if the
52720209230bSgjelinek  * zone cannot be located.
52730209230bSgjelinek  */
52740209230bSgjelinek int
52750209230bSgjelinek zonecfg_get_uuid(const char *zonename, uuid_t uuid)
52760209230bSgjelinek {
52770209230bSgjelinek 	FILE *fp;
52780209230bSgjelinek 	struct zoneent *ze;
52790209230bSgjelinek 
52800209230bSgjelinek 	if ((fp = setzoneent()) == NULL)
52810209230bSgjelinek 		return (Z_NO_ZONE);
52820209230bSgjelinek 	while ((ze = getzoneent_private(fp)) != NULL) {
52830209230bSgjelinek 		if (strcmp(ze->zone_name, zonename) == 0)
52840209230bSgjelinek 			break;
52850209230bSgjelinek 		free(ze);
52860209230bSgjelinek 	}
52870209230bSgjelinek 	endzoneent(fp);
52880209230bSgjelinek 	if (ze != NULL) {
52890209230bSgjelinek 		uuid_copy(uuid, ze->zone_uuid);
52900209230bSgjelinek 		free(ze);
52910209230bSgjelinek 		return (Z_OK);
52920209230bSgjelinek 	} else {
52930209230bSgjelinek 		return (Z_NO_ZONE);
52940209230bSgjelinek 	}
52950209230bSgjelinek }
52960209230bSgjelinek 
52970209230bSgjelinek /*
52980209230bSgjelinek  * File-system convenience functions.
52990209230bSgjelinek  */
53000209230bSgjelinek boolean_t
53010209230bSgjelinek zonecfg_valid_fs_type(const char *type)
53020209230bSgjelinek {
53030209230bSgjelinek 	/*
53040209230bSgjelinek 	 * We already know which FS types don't work.
53050209230bSgjelinek 	 */
53060209230bSgjelinek 	if (strcmp(type, "proc") == 0 ||
53070209230bSgjelinek 	    strcmp(type, "mntfs") == 0 ||
53080209230bSgjelinek 	    strcmp(type, "autofs") == 0 ||
53090209230bSgjelinek 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
53100209230bSgjelinek 	    strcmp(type, "cachefs") == 0)
53110209230bSgjelinek 		return (B_FALSE);
53120209230bSgjelinek 	/*
53130209230bSgjelinek 	 * The caller may do more detailed verification to make sure other
53140209230bSgjelinek 	 * aspects of this filesystem type make sense.
53150209230bSgjelinek 	 */
53160209230bSgjelinek 	return (B_TRUE);
53170209230bSgjelinek }
53180209230bSgjelinek 
53190209230bSgjelinek /*
53200209230bSgjelinek  * Generally uninteresting rctl convenience functions.
53210209230bSgjelinek  */
53220209230bSgjelinek 
53230209230bSgjelinek int
53240209230bSgjelinek zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
53250209230bSgjelinek     rctlblk_t *rctlblk)
53260209230bSgjelinek {
53270209230bSgjelinek 	unsigned long long ull;
53280209230bSgjelinek 	char *endp;
53290209230bSgjelinek 	rctl_priv_t priv;
53300209230bSgjelinek 	rctl_qty_t limit;
53310209230bSgjelinek 	uint_t action;
53320209230bSgjelinek 
53330209230bSgjelinek 	/* Get the privilege */
53340209230bSgjelinek 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
53350209230bSgjelinek 		priv = RCPRIV_BASIC;
53360209230bSgjelinek 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
53370209230bSgjelinek 		priv = RCPRIV_PRIVILEGED;
53380209230bSgjelinek 	} else {
53390209230bSgjelinek 		/* Invalid privilege */
53400209230bSgjelinek 		return (Z_INVAL);
53410209230bSgjelinek 	}
53420209230bSgjelinek 
53430209230bSgjelinek 	/* deal with negative input; strtoull(3c) doesn't do what we want */
53440209230bSgjelinek 	if (rctlval->zone_rctlval_limit[0] == '-')
53450209230bSgjelinek 		return (Z_INVAL);
53460209230bSgjelinek 	/* Get the limit */
53470209230bSgjelinek 	errno = 0;
53480209230bSgjelinek 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
53490209230bSgjelinek 	if (errno != 0 || *endp != '\0') {
53500209230bSgjelinek 		/* parse failed */
53510209230bSgjelinek 		return (Z_INVAL);
53520209230bSgjelinek 	}
53530209230bSgjelinek 	limit = (rctl_qty_t)ull;
53540209230bSgjelinek 
53550209230bSgjelinek 	/* Get the action */
53560209230bSgjelinek 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
53570209230bSgjelinek 		action = RCTL_LOCAL_NOACTION;
53580209230bSgjelinek 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
53590209230bSgjelinek 		action = RCTL_LOCAL_SIGNAL;
53600209230bSgjelinek 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
53610209230bSgjelinek 		action = RCTL_LOCAL_DENY;
53620209230bSgjelinek 	} else {
53630209230bSgjelinek 		/* Invalid Action */
53640209230bSgjelinek 		return (Z_INVAL);
53650209230bSgjelinek 	}
53660209230bSgjelinek 	rctlblk_set_local_action(rctlblk, action, 0);
53670209230bSgjelinek 	rctlblk_set_privilege(rctlblk, priv);
53680209230bSgjelinek 	rctlblk_set_value(rctlblk, limit);
53690209230bSgjelinek 	return (Z_OK);
53700209230bSgjelinek }
53710209230bSgjelinek 
53720209230bSgjelinek static int
53730209230bSgjelinek rctl_check(const char *rctlname, void *arg)
53740209230bSgjelinek {
53750209230bSgjelinek 	const char *attrname = arg;
53760209230bSgjelinek 
53770209230bSgjelinek 	/*
53780209230bSgjelinek 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
53797c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
53807c478bd9Sstevel@tonic-gate 	 */
53810209230bSgjelinek 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
53820209230bSgjelinek }
53830209230bSgjelinek 
53840209230bSgjelinek boolean_t
53850209230bSgjelinek zonecfg_is_rctl(const char *name)
53860209230bSgjelinek {
53870209230bSgjelinek 	return (rctl_walk(rctl_check, (void *)name) == 1);
53880209230bSgjelinek }
53890209230bSgjelinek 
53900209230bSgjelinek boolean_t
53910209230bSgjelinek zonecfg_valid_rctlname(const char *name)
53920209230bSgjelinek {
53930209230bSgjelinek 	const char *c;
53940209230bSgjelinek 
53950209230bSgjelinek 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
53960209230bSgjelinek 		return (B_FALSE);
53970209230bSgjelinek 	if (strlen(name) == sizeof ("zone.") - 1)
53980209230bSgjelinek 		return (B_FALSE);
53990209230bSgjelinek 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
54000209230bSgjelinek 		if (!isalpha(*c) && *c != '-')
54010209230bSgjelinek 			return (B_FALSE);
54020209230bSgjelinek 	}
54030209230bSgjelinek 	return (B_TRUE);
54040209230bSgjelinek }
54050209230bSgjelinek 
54060209230bSgjelinek boolean_t
54070209230bSgjelinek zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
54080209230bSgjelinek {
54090209230bSgjelinek 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
54100209230bSgjelinek 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
54110209230bSgjelinek 
54120209230bSgjelinek 	if (priv != RCPRIV_PRIVILEGED)
54130209230bSgjelinek 		return (B_FALSE);
54140209230bSgjelinek 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
54150209230bSgjelinek 		return (B_FALSE);
54160209230bSgjelinek 	return (B_TRUE);
54170209230bSgjelinek }
54180209230bSgjelinek 
54190209230bSgjelinek boolean_t
54200209230bSgjelinek zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
54210209230bSgjelinek {
54220209230bSgjelinek 	rctlblk_t *current, *next;
54230209230bSgjelinek 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
54240209230bSgjelinek 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
54250209230bSgjelinek 	uint_t global_flags;
54260209230bSgjelinek 
54270209230bSgjelinek 	if (!zonecfg_valid_rctlblk(rctlblk))
54280209230bSgjelinek 		return (B_FALSE);
54290209230bSgjelinek 	if (!zonecfg_valid_rctlname(name))
54300209230bSgjelinek 		return (B_FALSE);
54310209230bSgjelinek 
54320209230bSgjelinek 	current = alloca(rctlblk_size());
54330209230bSgjelinek 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
54340209230bSgjelinek 		return (B_TRUE);	/* not an rctl on this system */
54350209230bSgjelinek 	/*
54360209230bSgjelinek 	 * Make sure the proposed value isn't greater than the current system
54370209230bSgjelinek 	 * value.
54380209230bSgjelinek 	 */
54390209230bSgjelinek 	next = alloca(rctlblk_size());
54400209230bSgjelinek 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
54410209230bSgjelinek 		rctlblk_t *tmp;
54420209230bSgjelinek 
54430209230bSgjelinek 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
54440209230bSgjelinek 			return (B_FALSE);	/* shouldn't happen */
54450209230bSgjelinek 		tmp = current;
54460209230bSgjelinek 		current = next;
54470209230bSgjelinek 		next = tmp;
54480209230bSgjelinek 	}
54490209230bSgjelinek 	if (limit > rctlblk_get_value(current))
54500209230bSgjelinek 		return (B_FALSE);
54510209230bSgjelinek 
54520209230bSgjelinek 	/*
54530209230bSgjelinek 	 * Make sure the proposed action is allowed.
54540209230bSgjelinek 	 */
54550209230bSgjelinek 	global_flags = rctlblk_get_global_flags(current);
54560209230bSgjelinek 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
54570209230bSgjelinek 	    action == RCTL_LOCAL_DENY)
54580209230bSgjelinek 		return (B_FALSE);
54590209230bSgjelinek 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
54600209230bSgjelinek 	    action == RCTL_LOCAL_NOACTION)
54610209230bSgjelinek 		return (B_FALSE);
54620209230bSgjelinek 
54630209230bSgjelinek 	return (B_TRUE);
54640209230bSgjelinek }
54650209230bSgjelinek 
54660209230bSgjelinek /*
54670209230bSgjelinek  * There is always a race condition between reading the initial copy of
54680209230bSgjelinek  * a zones state and its state changing.  We address this by providing
54690209230bSgjelinek  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
54700209230bSgjelinek  * When zonecfg_critical_enter is called, sets the state field to LOCKED
54710209230bSgjelinek  * and aquires biglock. Biglock protects against other threads executing
54720209230bSgjelinek  * critical_enter and the state field protects against state changes during
54730209230bSgjelinek  * the critical period.
54740209230bSgjelinek  *
54750209230bSgjelinek  * If any state changes occur, zn_cb will set the failed field of the znotify
54760209230bSgjelinek  * structure.  This will cause the critical_exit function to re-lock the
54770209230bSgjelinek  * channel and return an error. Since evsnts may be delayed, the critical_exit
54780209230bSgjelinek  * function "flushes" the queue by putting an event on the queue and waiting for
54790209230bSgjelinek  * zn_cb to notify critical_exit that it received the ping event.
54800209230bSgjelinek  */
54810209230bSgjelinek static const char *
54820209230bSgjelinek string_get_tok(const char *in, char delim, int num)
54830209230bSgjelinek {
54840209230bSgjelinek 	int i = 0;
54850209230bSgjelinek 
54860209230bSgjelinek 	for (; i < num; in++) {
54870209230bSgjelinek 		if (*in == delim)
54880209230bSgjelinek 			i++;
54890209230bSgjelinek 		if (*in == 0)
54900209230bSgjelinek 			return (NULL);
54910209230bSgjelinek 	}
54920209230bSgjelinek 	return (in);
54930209230bSgjelinek }
54940209230bSgjelinek 
54950209230bSgjelinek static boolean_t
54960209230bSgjelinek is_ping(sysevent_t *ev)
54970209230bSgjelinek {
54980209230bSgjelinek 	if (strcmp(sysevent_get_subclass_name(ev),
54990209230bSgjelinek 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
55000209230bSgjelinek 		return (B_TRUE);
55010209230bSgjelinek 	} else {
55020209230bSgjelinek 		return (B_FALSE);
55030209230bSgjelinek 	}
55040209230bSgjelinek }
55050209230bSgjelinek 
55060209230bSgjelinek static boolean_t
55070209230bSgjelinek is_my_ping(sysevent_t *ev)
55080209230bSgjelinek {
55090209230bSgjelinek 	const char *sender;
55100209230bSgjelinek 	char mypid[sizeof (pid_t) * 3 + 1];
55110209230bSgjelinek 
55120209230bSgjelinek 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
55130209230bSgjelinek 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
55140209230bSgjelinek 	if (sender == NULL)
55150209230bSgjelinek 		return (B_FALSE);
55160209230bSgjelinek 	if (strcmp(sender, mypid) != 0)
55170209230bSgjelinek 		return (B_FALSE);
55180209230bSgjelinek 	return (B_TRUE);
55190209230bSgjelinek }
55200209230bSgjelinek 
55210209230bSgjelinek static int
55220209230bSgjelinek do_callback(struct znotify *zevtchan, sysevent_t *ev)
55230209230bSgjelinek {
55240209230bSgjelinek 	nvlist_t *l;
55250209230bSgjelinek 	int zid;
55260209230bSgjelinek 	char *zonename;
55270209230bSgjelinek 	char *newstate;
55280209230bSgjelinek 	char *oldstate;
55290209230bSgjelinek 	int ret;
55300209230bSgjelinek 	hrtime_t when;
55310209230bSgjelinek 
55320209230bSgjelinek 	if (strcmp(sysevent_get_subclass_name(ev),
55330209230bSgjelinek 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
55340209230bSgjelinek 
55350209230bSgjelinek 		if (sysevent_get_attr_list(ev, &l) != 0) {
55360209230bSgjelinek 			if (errno == ENOMEM) {
55370209230bSgjelinek 				zevtchan->zn_failure_count++;
55380209230bSgjelinek 				return (EAGAIN);
55390209230bSgjelinek 			}
55400209230bSgjelinek 			return (0);
55410209230bSgjelinek 		}
55420209230bSgjelinek 		ret = 0;
55430209230bSgjelinek 
55440209230bSgjelinek 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
55450209230bSgjelinek 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
55460209230bSgjelinek 		    == 0) &&
55470209230bSgjelinek 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
55480209230bSgjelinek 		    == 0) &&
55490209230bSgjelinek 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
55500209230bSgjelinek 		    (uint64_t *)&when) == 0) &&
55510209230bSgjelinek 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
55520209230bSgjelinek 			ret = zevtchan->zn_callback(zonename, zid, newstate,
55530209230bSgjelinek 			    oldstate, when, zevtchan->zn_private);
55540209230bSgjelinek 		}
55550209230bSgjelinek 
55560209230bSgjelinek 		zevtchan->zn_failure_count = 0;
55570209230bSgjelinek 		nvlist_free(l);
55580209230bSgjelinek 		return (ret);
55590209230bSgjelinek 	} else {
55600209230bSgjelinek 		/*
55610209230bSgjelinek 		 * We have received an event in an unknown subclass. Ignore.
55620209230bSgjelinek 		 */
55630209230bSgjelinek 		zevtchan->zn_failure_count = 0;
55640209230bSgjelinek 		return (0);
55650209230bSgjelinek 	}
55660209230bSgjelinek }
55670209230bSgjelinek 
55680209230bSgjelinek static int
55690209230bSgjelinek zn_cb(sysevent_t *ev, void *p)
55700209230bSgjelinek {
55710209230bSgjelinek 	struct znotify *zevtchan = p;
55720209230bSgjelinek 	int error;
55730209230bSgjelinek 
55740209230bSgjelinek 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
55750209230bSgjelinek 
55760209230bSgjelinek 	if (is_ping(ev) && !is_my_ping(ev)) {
55770209230bSgjelinek 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
55780209230bSgjelinek 		return (0);
55790209230bSgjelinek 	}
55800209230bSgjelinek 
55810209230bSgjelinek 	if (zevtchan->zn_state == ZN_LOCKED) {
55820209230bSgjelinek 		assert(!is_ping(ev));
55830209230bSgjelinek 		zevtchan->zn_failed = B_TRUE;
55840209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
55850209230bSgjelinek 		return (0);
55860209230bSgjelinek 	}
55870209230bSgjelinek 
55880209230bSgjelinek 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
55890209230bSgjelinek 		if (is_ping(ev)) {
55900209230bSgjelinek 			zevtchan->zn_state = ZN_PING_RECEIVED;
55910209230bSgjelinek 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
55920209230bSgjelinek 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
55930209230bSgjelinek 			return (0);
55940209230bSgjelinek 		} else {
55950209230bSgjelinek 			zevtchan->zn_failed = B_TRUE;
55960209230bSgjelinek 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
55970209230bSgjelinek 			return (0);
55980209230bSgjelinek 		}
55990209230bSgjelinek 	}
56000209230bSgjelinek 
56010209230bSgjelinek 	if (zevtchan->zn_state == ZN_UNLOCKED) {
56020209230bSgjelinek 
56030209230bSgjelinek 		error = do_callback(zevtchan, ev);
56040209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
56050209230bSgjelinek 		/*
56060209230bSgjelinek 		 * Every ENOMEM failure causes do_callback to increment
56070209230bSgjelinek 		 * zn_failure_count and every success causes it to
56080209230bSgjelinek 		 * set zn_failure_count to zero.  If we got EAGAIN,
56090209230bSgjelinek 		 * we will sleep for zn_failure_count seconds and return
56100209230bSgjelinek 		 * EAGAIN to gpec to try again.
56110209230bSgjelinek 		 *
56120209230bSgjelinek 		 * After 55 seconds, or 10 try's we give up and drop the
56130209230bSgjelinek 		 * event.
56140209230bSgjelinek 		 */
56150209230bSgjelinek 		if (error == EAGAIN) {
56160209230bSgjelinek 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
56170209230bSgjelinek 				return (0);
56180209230bSgjelinek 			}
56190209230bSgjelinek 			(void) sleep(zevtchan->zn_failure_count);
56200209230bSgjelinek 		}
56210209230bSgjelinek 		return (error);
56220209230bSgjelinek 	}
56230209230bSgjelinek 
56240209230bSgjelinek 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
56250209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
56260209230bSgjelinek 		return (0);
56270209230bSgjelinek 	}
56280209230bSgjelinek 
56290209230bSgjelinek 	abort();
56300209230bSgjelinek 	return (0);
56310209230bSgjelinek }
56320209230bSgjelinek 
56330209230bSgjelinek void
56340209230bSgjelinek zonecfg_notify_critical_enter(void *h)
56350209230bSgjelinek {
56360209230bSgjelinek 	struct znotify *zevtchan = h;
56370209230bSgjelinek 
56380209230bSgjelinek 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
56390209230bSgjelinek 	zevtchan->zn_state = ZN_LOCKED;
56400209230bSgjelinek }
56410209230bSgjelinek 
56420209230bSgjelinek int
56430209230bSgjelinek zonecfg_notify_critical_exit(void * h)
56440209230bSgjelinek {
56450209230bSgjelinek 
56460209230bSgjelinek 	struct znotify *zevtchan = h;
56470209230bSgjelinek 
56480209230bSgjelinek 	if (zevtchan->zn_state == ZN_UNLOCKED)
56490209230bSgjelinek 		return (0);
56500209230bSgjelinek 
56510209230bSgjelinek 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
56520209230bSgjelinek 	zevtchan->zn_state = ZN_PING_INFLIGHT;
56530209230bSgjelinek 
56540209230bSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
56550209230bSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
56560209230bSgjelinek 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
56570209230bSgjelinek 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
56580209230bSgjelinek 
56590209230bSgjelinek 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
56600209230bSgjelinek 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
56610209230bSgjelinek 		    &(zevtchan->zn_mutex));
56620209230bSgjelinek 	}
56630209230bSgjelinek 
56640209230bSgjelinek 	if (zevtchan->zn_failed == B_TRUE) {
56650209230bSgjelinek 		zevtchan->zn_state = ZN_LOCKED;
56660209230bSgjelinek 		zevtchan->zn_failed = B_FALSE;
56670209230bSgjelinek 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
56680209230bSgjelinek 		return (1);
56690209230bSgjelinek 	}
56700209230bSgjelinek 
56710209230bSgjelinek 	zevtchan->zn_state = ZN_UNLOCKED;
56720209230bSgjelinek 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
56730209230bSgjelinek 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
56740209230bSgjelinek 	return (0);
56757c478bd9Sstevel@tonic-gate }
56767c478bd9Sstevel@tonic-gate 
56770209230bSgjelinek void
56780209230bSgjelinek zonecfg_notify_critical_abort(void *h)
56797c478bd9Sstevel@tonic-gate {
56800209230bSgjelinek 	struct znotify *zevtchan = h;
56810209230bSgjelinek 
56820209230bSgjelinek 	zevtchan->zn_state = ZN_UNLOCKED;
56830209230bSgjelinek 	zevtchan->zn_failed = B_FALSE;
56840209230bSgjelinek 	/*
56850209230bSgjelinek 	 * Don't do anything about zn_lock. If it is held, it could only be
56860209230bSgjelinek 	 * held by zn_cb and it will be unlocked soon.
56870209230bSgjelinek 	 */
56880209230bSgjelinek 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
56897c478bd9Sstevel@tonic-gate }
56907c478bd9Sstevel@tonic-gate 
56910209230bSgjelinek void *
56920209230bSgjelinek zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
56930209230bSgjelinek     const char *newstate, const char *oldstate, hrtime_t when, void *p),
56940209230bSgjelinek     void *p)
56957c478bd9Sstevel@tonic-gate {
56960209230bSgjelinek 	struct znotify *zevtchan;
56970209230bSgjelinek 	int i = 1;
56980209230bSgjelinek 	int r;
56997c478bd9Sstevel@tonic-gate 
57000209230bSgjelinek 	zevtchan = malloc(sizeof (struct znotify));
57010209230bSgjelinek 
57020209230bSgjelinek 	if (zevtchan == NULL)
57030209230bSgjelinek 		return (NULL);
57040209230bSgjelinek 
57050209230bSgjelinek 	zevtchan->zn_private = p;
57060209230bSgjelinek 	zevtchan->zn_callback = func;
57070209230bSgjelinek 	zevtchan->zn_state = ZN_UNLOCKED;
57080209230bSgjelinek 	zevtchan->zn_failed = B_FALSE;
57090209230bSgjelinek 
57100209230bSgjelinek 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
57110209230bSgjelinek 		goto out3;
57120209230bSgjelinek 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
57130209230bSgjelinek 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
57140209230bSgjelinek 		goto out3;
57150209230bSgjelinek 	}
57160209230bSgjelinek 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
57170209230bSgjelinek 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
57180209230bSgjelinek 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
57190209230bSgjelinek 		goto out3;
57207c478bd9Sstevel@tonic-gate 	}
57217c478bd9Sstevel@tonic-gate 
57220209230bSgjelinek 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
57230209230bSgjelinek 	    0) != 0)
57240209230bSgjelinek 		goto out2;
57257c478bd9Sstevel@tonic-gate 
57260209230bSgjelinek 	do {
57270209230bSgjelinek 		/*
57280209230bSgjelinek 		 * At 4 digits the subscriber ID gets too long and we have
57290209230bSgjelinek 		 * no chance of successfully registering.
57300209230bSgjelinek 		 */
57310209230bSgjelinek 		if (i > 999)
57320209230bSgjelinek 			goto out1;
57330209230bSgjelinek 
57340209230bSgjelinek 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
57350209230bSgjelinek 		    getpid() % 999999l, i);
57360209230bSgjelinek 
57370209230bSgjelinek 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
57380209230bSgjelinek 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
57390209230bSgjelinek 		    zevtchan, 0);
57400209230bSgjelinek 
57410209230bSgjelinek 		i++;
57420209230bSgjelinek 
57430209230bSgjelinek 	} while (r);
57440209230bSgjelinek 
57450209230bSgjelinek 	return (zevtchan);
57460209230bSgjelinek out1:
57470209230bSgjelinek 	sysevent_evc_unbind(zevtchan->zn_eventchan);
57480209230bSgjelinek out2:
57490209230bSgjelinek 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
57500209230bSgjelinek 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
57510209230bSgjelinek 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
57520209230bSgjelinek out3:
57530209230bSgjelinek 	free(zevtchan);
57540209230bSgjelinek 
57550209230bSgjelinek 	return (NULL);
57567c478bd9Sstevel@tonic-gate }
57577c478bd9Sstevel@tonic-gate 
57580209230bSgjelinek void
57590209230bSgjelinek zonecfg_notify_unbind(void *handle)
57607c478bd9Sstevel@tonic-gate {
57617c478bd9Sstevel@tonic-gate 
57620209230bSgjelinek 	int ret;
57637c478bd9Sstevel@tonic-gate 
57640209230bSgjelinek 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
57657c478bd9Sstevel@tonic-gate 	/*
57660209230bSgjelinek 	 * Check that all evc threads have gone away. This should be
57670209230bSgjelinek 	 * enforced by sysevent_evc_unbind.
57687c478bd9Sstevel@tonic-gate 	 */
57690209230bSgjelinek 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
57707c478bd9Sstevel@tonic-gate 
57710209230bSgjelinek 	if (ret)
57720209230bSgjelinek 		abort();
57737c478bd9Sstevel@tonic-gate 
57740209230bSgjelinek 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
57750209230bSgjelinek 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
57760209230bSgjelinek 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
57770209230bSgjelinek 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
57787c478bd9Sstevel@tonic-gate 
57790209230bSgjelinek 	free(handle);
57807c478bd9Sstevel@tonic-gate }
5781fa9e4066Sahrens 
57820209230bSgjelinek static int
57830209230bSgjelinek zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5784cf8f45c7Sdstaff {
57850209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
57860209230bSgjelinek 	int err;
5787cf8f45c7Sdstaff 
57880209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
57890209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME,
57900209230bSgjelinek 	    tabptr->zone_dataset_name)) != Z_OK)
57910209230bSgjelinek 		return (err);
57920209230bSgjelinek 	return (Z_OK);
5793cf8f45c7Sdstaff }
5794cf8f45c7Sdstaff 
57950209230bSgjelinek int
57960209230bSgjelinek zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5797cf8f45c7Sdstaff {
57980209230bSgjelinek 	int err;
57990209230bSgjelinek 
58000209230bSgjelinek 	if (tabptr == NULL)
58010209230bSgjelinek 		return (Z_INVAL);
58020209230bSgjelinek 
58030209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
58040209230bSgjelinek 		return (err);
58050209230bSgjelinek 
58060209230bSgjelinek 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
58070209230bSgjelinek 		return (err);
58080209230bSgjelinek 
58090209230bSgjelinek 	return (Z_OK);
58100209230bSgjelinek }
58110209230bSgjelinek 
58120209230bSgjelinek static int
58130209230bSgjelinek zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
58140209230bSgjelinek {
58150209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
58160209230bSgjelinek 
58170209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
58180209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
58190209230bSgjelinek 			continue;
58200209230bSgjelinek 
58210209230bSgjelinek 		if (match_prop(cur, DTD_ATTR_NAME,
58220209230bSgjelinek 		    tabptr->zone_dataset_name)) {
58230209230bSgjelinek 			xmlUnlinkNode(cur);
58240209230bSgjelinek 			xmlFreeNode(cur);
58250209230bSgjelinek 			return (Z_OK);
58260209230bSgjelinek 		}
5827cf8f45c7Sdstaff 	}
58280209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
58290209230bSgjelinek }
58300209230bSgjelinek 
58310209230bSgjelinek int
58320209230bSgjelinek zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
58330209230bSgjelinek {
58340209230bSgjelinek 	int err;
58350209230bSgjelinek 
58360209230bSgjelinek 	if (tabptr == NULL)
58370209230bSgjelinek 		return (Z_INVAL);
58380209230bSgjelinek 
58390209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
58400209230bSgjelinek 		return (err);
58410209230bSgjelinek 
58420209230bSgjelinek 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
58430209230bSgjelinek 		return (err);
58440209230bSgjelinek 
58450209230bSgjelinek 	return (Z_OK);
5846cf8f45c7Sdstaff }
5847cf8f45c7Sdstaff 
58480209230bSgjelinek int
58490209230bSgjelinek zonecfg_modify_ds(
58500209230bSgjelinek 	zone_dochandle_t handle,
58510209230bSgjelinek 	struct zone_dstab *oldtabptr,
58520209230bSgjelinek 	struct zone_dstab *newtabptr)
58530209230bSgjelinek {
58540209230bSgjelinek 	int err;
58550209230bSgjelinek 
58560209230bSgjelinek 	if (oldtabptr == NULL || newtabptr == NULL)
58570209230bSgjelinek 		return (Z_INVAL);
58580209230bSgjelinek 
58590209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
58600209230bSgjelinek 		return (err);
5861cf8f45c7Sdstaff 
58620209230bSgjelinek 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
58630209230bSgjelinek 		return (err);
58640209230bSgjelinek 
58650209230bSgjelinek 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
58660209230bSgjelinek 		return (err);
58670209230bSgjelinek 
58680209230bSgjelinek 	return (Z_OK);
5869cf8f45c7Sdstaff }
5870cf8f45c7Sdstaff 
58710209230bSgjelinek int
58720209230bSgjelinek zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5873cf8f45c7Sdstaff {
58740209230bSgjelinek 	xmlNodePtr cur, firstmatch;
58750209230bSgjelinek 	int err;
58760209230bSgjelinek 	char dataset[MAXNAMELEN];
5877cf8f45c7Sdstaff 
58780209230bSgjelinek 	if (tabptr == NULL)
58790209230bSgjelinek 		return (Z_INVAL);
5880cf8f45c7Sdstaff 
58810209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
58820209230bSgjelinek 		return (err);
58830209230bSgjelinek 
58840209230bSgjelinek 	cur = handle->zone_dh_cur;
58850209230bSgjelinek 	firstmatch = NULL;
58860209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
58870209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
58880209230bSgjelinek 			continue;
58890209230bSgjelinek 		if (strlen(tabptr->zone_dataset_name) > 0) {
58900209230bSgjelinek 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
58910209230bSgjelinek 			    sizeof (dataset)) == Z_OK) &&
58920209230bSgjelinek 			    (strcmp(tabptr->zone_dataset_name,
58930209230bSgjelinek 			    dataset) == 0)) {
58940209230bSgjelinek 				if (firstmatch == NULL)
58950209230bSgjelinek 					firstmatch = cur;
58960209230bSgjelinek 				else
58970209230bSgjelinek 					return (Z_INSUFFICIENT_SPEC);
5898cf8f45c7Sdstaff 			}
5899cf8f45c7Sdstaff 		}
59000209230bSgjelinek 	}
59010209230bSgjelinek 	if (firstmatch == NULL)
59020209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
5903cf8f45c7Sdstaff 
59040209230bSgjelinek 	cur = firstmatch;
5905cf8f45c7Sdstaff 
59060209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
59070209230bSgjelinek 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
59080209230bSgjelinek 		return (err);
59090209230bSgjelinek 
59100209230bSgjelinek 	return (Z_OK);
5911cf8f45c7Sdstaff }
5912cf8f45c7Sdstaff 
59130209230bSgjelinek int
59140209230bSgjelinek zonecfg_setdsent(zone_dochandle_t handle)
5915cf8f45c7Sdstaff {
59160209230bSgjelinek 	return (zonecfg_setent(handle));
59170209230bSgjelinek }
5918cf8f45c7Sdstaff 
59190209230bSgjelinek int
59200209230bSgjelinek zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
59210209230bSgjelinek {
59220209230bSgjelinek 	xmlNodePtr cur;
59230209230bSgjelinek 	int err;
5924cf8f45c7Sdstaff 
59250209230bSgjelinek 	if (handle == NULL)
59260209230bSgjelinek 		return (Z_INVAL);
5927cf8f45c7Sdstaff 
59280209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
59290209230bSgjelinek 		return (Z_NO_ENTRY);
5930cf8f45c7Sdstaff 
59310209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
59320209230bSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
59330209230bSgjelinek 			break;
59340209230bSgjelinek 	if (cur == NULL) {
59350209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
59360209230bSgjelinek 		return (Z_NO_ENTRY);
5937cf8f45c7Sdstaff 	}
5938cf8f45c7Sdstaff 
59390209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
59400209230bSgjelinek 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
59410209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
59420209230bSgjelinek 		return (err);
5943cf8f45c7Sdstaff 	}
5944cf8f45c7Sdstaff 
59450209230bSgjelinek 	handle->zone_dh_cur = cur->next;
59460209230bSgjelinek 	return (Z_OK);
5947cf8f45c7Sdstaff }
5948cf8f45c7Sdstaff 
59490209230bSgjelinek int
59500209230bSgjelinek zonecfg_enddsent(zone_dochandle_t handle)
5951cf8f45c7Sdstaff {
59520209230bSgjelinek 	return (zonecfg_endent(handle));
5953cf8f45c7Sdstaff }
5954cf8f45c7Sdstaff 
59550209230bSgjelinek /*
59560209230bSgjelinek  * Support for aliased rctls; that is, rctls that have simplified names in
59570209230bSgjelinek  * zonecfg.  For example, max-lwps is an alias for a well defined zone.max-lwps
59580209230bSgjelinek  * rctl.  If there are multiple existing values for one of these rctls or if
59590209230bSgjelinek  * there is a single value that does not match the well defined template (i.e.
59600209230bSgjelinek  * it has a different action) then we cannot treat the rctl as having an alias
59610209230bSgjelinek  * so we return Z_ALIAS_DISALLOW.  That means that the rctl cannot be
59620209230bSgjelinek  * managed in zonecfg via an alias and that the standard rctl syntax must be
59630209230bSgjelinek  * used.
59640209230bSgjelinek  *
59650209230bSgjelinek  * The possible return values are:
59660209230bSgjelinek  *	Z_NO_PROPERTY_ID - invalid alias name
59670209230bSgjelinek  *	Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition
59680209230bSgjelinek  *	Z_NO_ENTRY - no rctl is configured for this alias
59690209230bSgjelinek  *	Z_OK - we got a valid rctl for the specified alias
59700209230bSgjelinek  */
5971cf8f45c7Sdstaff int
59720209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval)
5973cf8f45c7Sdstaff {
59740209230bSgjelinek 	boolean_t found = B_FALSE;
59750209230bSgjelinek 	boolean_t found_val = B_FALSE;
59760209230bSgjelinek 	xmlNodePtr cur, val;
59770209230bSgjelinek 	char savedname[MAXNAMELEN];
59780209230bSgjelinek 	struct zone_rctlvaltab rctl;
59790209230bSgjelinek 	int i;
59800209230bSgjelinek 	int err;
5981cf8f45c7Sdstaff 
59820209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
59830209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
59840209230bSgjelinek 			break;
5985cf8f45c7Sdstaff 
59860209230bSgjelinek 	if (aliases[i].shortname == NULL)
59870209230bSgjelinek 		return (Z_NO_PROPERTY_ID);
5988cf8f45c7Sdstaff 
59890209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
59900209230bSgjelinek 		return (err);
5991cf8f45c7Sdstaff 
59920209230bSgjelinek 	cur = handle->zone_dh_cur;
59930209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
59940209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0)
59950209230bSgjelinek 			continue;
59960209230bSgjelinek 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
59970209230bSgjelinek 		    sizeof (savedname)) == Z_OK) &&
59980209230bSgjelinek 		    (strcmp(savedname, aliases[i].realname) == 0)) {
5999cf8f45c7Sdstaff 
60000209230bSgjelinek 			/*
60010209230bSgjelinek 			 * If we already saw one of these, we can't have an
60020209230bSgjelinek 			 * alias since we just found another.
60030209230bSgjelinek 			 */
60040209230bSgjelinek 			if (found)
60050209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
60060209230bSgjelinek 			found = B_TRUE;
60070209230bSgjelinek 
60080209230bSgjelinek 			for (val = cur->xmlChildrenNode; val != NULL;
60090209230bSgjelinek 			    val = val->next) {
60100209230bSgjelinek 				/*
60110209230bSgjelinek 				 * If we already have one value, we can't have
60120209230bSgjelinek 				 * an alias since we just found another.
60130209230bSgjelinek 				 */
60140209230bSgjelinek 				if (found_val)
60150209230bSgjelinek 					return (Z_ALIAS_DISALLOW);
60160209230bSgjelinek 				found_val = B_TRUE;
60170209230bSgjelinek 
60180209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_PRIV,
60190209230bSgjelinek 				    rctl.zone_rctlval_priv,
60200209230bSgjelinek 				    sizeof (rctl.zone_rctlval_priv)) != Z_OK))
60210209230bSgjelinek 					break;
60220209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_LIMIT,
60230209230bSgjelinek 				    rctl.zone_rctlval_limit,
60240209230bSgjelinek 				    sizeof (rctl.zone_rctlval_limit)) != Z_OK))
60250209230bSgjelinek 					break;
60260209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_ACTION,
60270209230bSgjelinek 				    rctl.zone_rctlval_action,
60280209230bSgjelinek 				    sizeof (rctl.zone_rctlval_action)) != Z_OK))
60290209230bSgjelinek 					break;
60300209230bSgjelinek 			}
60310209230bSgjelinek 
60320209230bSgjelinek 			/* check priv and action match the expected vals */
60330209230bSgjelinek 			if (strcmp(rctl.zone_rctlval_priv,
60340209230bSgjelinek 			    aliases[i].priv) != 0 ||
60350209230bSgjelinek 			    strcmp(rctl.zone_rctlval_action,
60360209230bSgjelinek 			    aliases[i].action) != 0)
60370209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
60380209230bSgjelinek 		}
6039cf8f45c7Sdstaff 	}
6040cf8f45c7Sdstaff 
60410209230bSgjelinek 	if (found) {
60420209230bSgjelinek 		*rval = strtoull(rctl.zone_rctlval_limit, NULL, 10);
60430209230bSgjelinek 		return (Z_OK);
6044cf8f45c7Sdstaff 	}
6045cf8f45c7Sdstaff 
60460209230bSgjelinek 	return (Z_NO_ENTRY);
6047cf8f45c7Sdstaff }
6048cf8f45c7Sdstaff 
60490209230bSgjelinek int
60500209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name)
6051cf8f45c7Sdstaff {
60520209230bSgjelinek 	int i;
60530209230bSgjelinek 	uint64_t val;
60540209230bSgjelinek 	struct zone_rctltab rctltab;
6055cf8f45c7Sdstaff 
6056cf8f45c7Sdstaff 	/*
60570209230bSgjelinek 	 * First check that we have a valid aliased rctl to remove.
60580209230bSgjelinek 	 * This will catch an rctl entry with non-standard values or
60590209230bSgjelinek 	 * multiple rctl values for this name.  We need to ignore those
60600209230bSgjelinek 	 * rctl entries.
6061cf8f45c7Sdstaff 	 */
60620209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK)
60630209230bSgjelinek 		return (Z_OK);
60640209230bSgjelinek 
60650209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
60660209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
60670209230bSgjelinek 			break;
60680209230bSgjelinek 
60690209230bSgjelinek 	if (aliases[i].shortname == NULL)
60700209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
60710209230bSgjelinek 
60720209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
60730209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
60740209230bSgjelinek 
60750209230bSgjelinek 	return (zonecfg_delete_rctl(handle, &rctltab));
60760209230bSgjelinek }
60770209230bSgjelinek 
60780209230bSgjelinek boolean_t
60790209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name)
60800209230bSgjelinek {
60810209230bSgjelinek 	uint64_t tmp_val;
60820209230bSgjelinek 
60830209230bSgjelinek 	switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) {
60840209230bSgjelinek 	case Z_OK:
60850209230bSgjelinek 		/*FALLTHRU*/
60860209230bSgjelinek 	case Z_NO_ENTRY:
60870209230bSgjelinek 		return (B_TRUE);
60880209230bSgjelinek 	default:
60890209230bSgjelinek 		return (B_FALSE);
60900209230bSgjelinek 	}
6091cf8f45c7Sdstaff }
6092cf8f45c7Sdstaff 
60930209230bSgjelinek int
60940209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val)
6095cf8f45c7Sdstaff {
60960209230bSgjelinek 	int i;
60970209230bSgjelinek 	int err;
60980209230bSgjelinek 	struct zone_rctltab rctltab;
60990209230bSgjelinek 	struct zone_rctlvaltab *rctlvaltab;
61000209230bSgjelinek 	char buf[128];
6101cf8f45c7Sdstaff 
61020209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, name))
61030209230bSgjelinek 		return (Z_ALIAS_DISALLOW);
6104cf8f45c7Sdstaff 
61050209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
61060209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
61070209230bSgjelinek 			break;
6108cf8f45c7Sdstaff 
61090209230bSgjelinek 	if (aliases[i].shortname == NULL)
61100209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
6111cf8f45c7Sdstaff 
61120209230bSgjelinek 	/* remove any pre-existing definition for this rctl */
61130209230bSgjelinek 	(void) zonecfg_rm_aliased_rctl(handle, name);
6114cf8f45c7Sdstaff 
61150209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
61160209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
6117cf8f45c7Sdstaff 
61180209230bSgjelinek 	rctltab.zone_rctl_valptr = NULL;
6119cf8f45c7Sdstaff 
61200209230bSgjelinek 	if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL)
61210209230bSgjelinek 		return (Z_NOMEM);
6122cf8f45c7Sdstaff 
61230209230bSgjelinek 	(void) snprintf(buf, sizeof (buf), "%llu", (long long)val);
6124cf8f45c7Sdstaff 
61250209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv,
61260209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_priv));
61270209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_limit, buf,
61280209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_limit));
61290209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action,
61300209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_action));
6131cf8f45c7Sdstaff 
61320209230bSgjelinek 	rctlvaltab->zone_rctlval_next = NULL;
6133cf8f45c7Sdstaff 
61340209230bSgjelinek 	if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK)
61350209230bSgjelinek 		return (err);
6136cf8f45c7Sdstaff 
61370209230bSgjelinek 	return (zonecfg_add_rctl(handle, &rctltab));
6138cf8f45c7Sdstaff }
6139cf8f45c7Sdstaff 
61400209230bSgjelinek static int
61410209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle)
6142cf8f45c7Sdstaff {
61430209230bSgjelinek 	int err;
61440209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
6145cf8f45c7Sdstaff 
61460209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
61470209230bSgjelinek 		return (err);
6148cf8f45c7Sdstaff 
61490209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
61500209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
61510209230bSgjelinek 			xmlUnlinkNode(cur);
61520209230bSgjelinek 			xmlFreeNode(cur);
61530209230bSgjelinek 			return (Z_OK);
61540209230bSgjelinek 		}
61550209230bSgjelinek 	}
6156cf8f45c7Sdstaff 
61570209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
61580209230bSgjelinek }
6159cf8f45c7Sdstaff 
61600209230bSgjelinek static int
61610209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance)
61620209230bSgjelinek {
61630209230bSgjelinek 	int err;
61640209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
61650209230bSgjelinek 	xmlNodePtr newnode;
6166cf8f45c7Sdstaff 
61670209230bSgjelinek 	err = delete_tmp_pool(handle);
61680209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
61690209230bSgjelinek 		return (err);
61700209230bSgjelinek 
61710209230bSgjelinek 	if (*pool_importance != '\0') {
61720209230bSgjelinek 		if ((err = operation_prep(handle)) != Z_OK)
61730209230bSgjelinek 			return (err);
61740209230bSgjelinek 
61750209230bSgjelinek 		newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL);
61760209230bSgjelinek 		if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE,
61770209230bSgjelinek 		    pool_importance)) != Z_OK)
61780209230bSgjelinek 			return (err);
61790209230bSgjelinek 	}
61800209230bSgjelinek 
61810209230bSgjelinek 	return (Z_OK);
6182cf8f45c7Sdstaff }
6183cf8f45c7Sdstaff 
6184fa9e4066Sahrens static int
61850209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr)
6186fa9e4066Sahrens {
6187fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
6188fa9e4066Sahrens 	int err;
6189fa9e4066Sahrens 
61900209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL);
61910209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN,
61920209230bSgjelinek 	    tabptr->zone_ncpu_min)) != Z_OK)
61930209230bSgjelinek 		return (err);
61940209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX,
61950209230bSgjelinek 	    tabptr->zone_ncpu_max)) != Z_OK)
61960209230bSgjelinek 		return (err);
61970209230bSgjelinek 
61980209230bSgjelinek 	if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK)
6199fa9e4066Sahrens 		return (err);
62000209230bSgjelinek 
6201fa9e4066Sahrens 	return (Z_OK);
6202fa9e4066Sahrens }
6203fa9e4066Sahrens 
6204fa9e4066Sahrens int
62050209230bSgjelinek zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6206fa9e4066Sahrens {
6207fa9e4066Sahrens 	int err;
6208fa9e4066Sahrens 
6209fa9e4066Sahrens 	if (tabptr == NULL)
6210fa9e4066Sahrens 		return (Z_INVAL);
6211fa9e4066Sahrens 
6212fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6213fa9e4066Sahrens 		return (err);
6214fa9e4066Sahrens 
62150209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
6216fa9e4066Sahrens 		return (err);
6217fa9e4066Sahrens 
6218fa9e4066Sahrens 	return (Z_OK);
6219fa9e4066Sahrens }
6220fa9e4066Sahrens 
62210209230bSgjelinek int
62220209230bSgjelinek zonecfg_delete_pset(zone_dochandle_t handle)
6223fa9e4066Sahrens {
62240209230bSgjelinek 	int err;
62250209230bSgjelinek 	int res = Z_NO_RESOURCE_ID;
6226fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
6227fa9e4066Sahrens 
62280209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
62290209230bSgjelinek 		return (err);
6230fa9e4066Sahrens 
62310209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
62320209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
6233fa9e4066Sahrens 			xmlUnlinkNode(cur);
6234fa9e4066Sahrens 			xmlFreeNode(cur);
62350209230bSgjelinek 			res = Z_OK;
62360209230bSgjelinek 			break;
6237fa9e4066Sahrens 		}
6238fa9e4066Sahrens 	}
62390209230bSgjelinek 
62400209230bSgjelinek 	/*
62410209230bSgjelinek 	 * Once we have msets, we should check that a mset
62420209230bSgjelinek 	 * do not exist before we delete the tmp_pool data.
62430209230bSgjelinek 	 */
62440209230bSgjelinek 	err = delete_tmp_pool(handle);
62450209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
62460209230bSgjelinek 		return (err);
62470209230bSgjelinek 
62480209230bSgjelinek 	return (res);
6249fa9e4066Sahrens }
6250fa9e4066Sahrens 
6251fa9e4066Sahrens int
62520209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6253fa9e4066Sahrens {
6254fa9e4066Sahrens 	int err;
6255fa9e4066Sahrens 
6256fa9e4066Sahrens 	if (tabptr == NULL)
6257fa9e4066Sahrens 		return (Z_INVAL);
6258fa9e4066Sahrens 
62590209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
6260fa9e4066Sahrens 		return (err);
6261fa9e4066Sahrens 
62620209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
6263fa9e4066Sahrens 		return (err);
6264fa9e4066Sahrens 
6265fa9e4066Sahrens 	return (Z_OK);
6266fa9e4066Sahrens }
6267fa9e4066Sahrens 
6268fa9e4066Sahrens int
62690209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
6270fa9e4066Sahrens {
62710209230bSgjelinek 	xmlNodePtr cur;
6272fa9e4066Sahrens 	int err;
62730209230bSgjelinek 	int res = Z_NO_ENTRY;
6274fa9e4066Sahrens 
62750209230bSgjelinek 	if (tabptr == NULL)
6276fa9e4066Sahrens 		return (Z_INVAL);
6277fa9e4066Sahrens 
6278fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6279fa9e4066Sahrens 		return (err);
6280fa9e4066Sahrens 
62810209230bSgjelinek 	/* this is an optional component */
62820209230bSgjelinek 	tabptr->zone_importance[0] = '\0';
62830209230bSgjelinek 
62840209230bSgjelinek 	cur = handle->zone_dh_cur;
62850209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
62860209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
62870209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN,
62880209230bSgjelinek 			    tabptr->zone_ncpu_min,
62890209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_min))) != Z_OK) {
62900209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
62910209230bSgjelinek 				return (err);
62920209230bSgjelinek 			}
62930209230bSgjelinek 
62940209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX,
62950209230bSgjelinek 			    tabptr->zone_ncpu_max,
62960209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_max))) != Z_OK) {
62970209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
62980209230bSgjelinek 				return (err);
62990209230bSgjelinek 			}
63000209230bSgjelinek 
63010209230bSgjelinek 			res = Z_OK;
63020209230bSgjelinek 
63030209230bSgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
63040209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE,
63050209230bSgjelinek 			    tabptr->zone_importance,
63060209230bSgjelinek 			    sizeof (tabptr->zone_importance))) != Z_OK) {
63070209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
63080209230bSgjelinek 				return (err);
63090209230bSgjelinek 			}
63100209230bSgjelinek 		}
63110209230bSgjelinek 	}
63120209230bSgjelinek 
63130209230bSgjelinek 	return (res);
63140209230bSgjelinek }
63150209230bSgjelinek 
63160209230bSgjelinek int
63170209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr)
63180209230bSgjelinek {
63190209230bSgjelinek 	int err;
63200209230bSgjelinek 
63210209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
6322fa9e4066Sahrens 		return (err);
6323fa9e4066Sahrens 
63240209230bSgjelinek 	err = zonecfg_lookup_pset(handle, tabptr);
63250209230bSgjelinek 
63260209230bSgjelinek 	(void) zonecfg_endent(handle);
63270209230bSgjelinek 
63280209230bSgjelinek 	return (err);
63290209230bSgjelinek }
63300209230bSgjelinek 
63310209230bSgjelinek static int
63320209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
63330209230bSgjelinek {
63340209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
63350209230bSgjelinek 	int err;
63360209230bSgjelinek 
63370209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL);
63380209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap))
63390209230bSgjelinek 	    != Z_OK)
6340fa9e4066Sahrens 		return (err);
6341fa9e4066Sahrens 
6342fa9e4066Sahrens 	return (Z_OK);
6343fa9e4066Sahrens }
6344fa9e4066Sahrens 
6345fa9e4066Sahrens int
63460209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle)
6347fa9e4066Sahrens {
6348fa9e4066Sahrens 	int err;
63490209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
6350fa9e4066Sahrens 
6351fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6352fa9e4066Sahrens 		return (err);
6353fa9e4066Sahrens 
6354fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
63550209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
6356fa9e4066Sahrens 			continue;
63570209230bSgjelinek 
63580209230bSgjelinek 		xmlUnlinkNode(cur);
63590209230bSgjelinek 		xmlFreeNode(cur);
63600209230bSgjelinek 		return (Z_OK);
6361fa9e4066Sahrens 	}
63620209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
63630209230bSgjelinek }
6364fa9e4066Sahrens 
63650209230bSgjelinek int
63660209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
63670209230bSgjelinek {
63680209230bSgjelinek 	int err;
6369fa9e4066Sahrens 
63700209230bSgjelinek 	if (tabptr == NULL)
63710209230bSgjelinek 		return (Z_INVAL);
63720209230bSgjelinek 
63730209230bSgjelinek 	err = zonecfg_delete_mcap(handle);
63740209230bSgjelinek 	/* it is ok if there is no mcap entry */
63750209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
63760209230bSgjelinek 		return (err);
63770209230bSgjelinek 
63780209230bSgjelinek 	if ((err = add_mcap(handle, tabptr)) != Z_OK)
6379fa9e4066Sahrens 		return (err);
6380fa9e4066Sahrens 
6381fa9e4066Sahrens 	return (Z_OK);
6382fa9e4066Sahrens }
6383fa9e4066Sahrens 
6384fa9e4066Sahrens int
63850209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6386fa9e4066Sahrens {
63870209230bSgjelinek 	xmlNodePtr cur;
63880209230bSgjelinek 	int err;
63890209230bSgjelinek 
63900209230bSgjelinek 	if (tabptr == NULL)
63910209230bSgjelinek 		return (Z_INVAL);
63920209230bSgjelinek 
63930209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
63940209230bSgjelinek 		return (err);
63950209230bSgjelinek 
63960209230bSgjelinek 	cur = handle->zone_dh_cur;
63970209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
63980209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
63990209230bSgjelinek 			continue;
64000209230bSgjelinek 		if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP,
64010209230bSgjelinek 		    tabptr->zone_physmem_cap,
64020209230bSgjelinek 		    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
64030209230bSgjelinek 			handle->zone_dh_cur = handle->zone_dh_top;
64040209230bSgjelinek 			return (err);
64050209230bSgjelinek 		}
64060209230bSgjelinek 
64070209230bSgjelinek 		return (Z_OK);
64080209230bSgjelinek 	}
64090209230bSgjelinek 
64100209230bSgjelinek 	return (Z_NO_ENTRY);
6411fa9e4066Sahrens }
6412fa9e4066Sahrens 
64130209230bSgjelinek static int
64140209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6415fa9e4066Sahrens {
6416fa9e4066Sahrens 	xmlNodePtr cur;
6417fa9e4066Sahrens 	int err;
6418fa9e4066Sahrens 
6419fa9e4066Sahrens 	if (handle == NULL)
6420fa9e4066Sahrens 		return (Z_INVAL);
6421fa9e4066Sahrens 
6422fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
6423fa9e4066Sahrens 		return (Z_NO_ENTRY);
6424fa9e4066Sahrens 
6425fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
64260209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0)
6427fa9e4066Sahrens 			break;
6428fa9e4066Sahrens 	if (cur == NULL) {
6429fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6430fa9e4066Sahrens 		return (Z_NO_ENTRY);
6431fa9e4066Sahrens 	}
6432fa9e4066Sahrens 
64330209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap,
64340209230bSgjelinek 	    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
6435fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6436fa9e4066Sahrens 		return (err);
6437fa9e4066Sahrens 	}
6438fa9e4066Sahrens 
6439fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
6440fa9e4066Sahrens 	return (Z_OK);
6441fa9e4066Sahrens }
6442fa9e4066Sahrens 
6443fa9e4066Sahrens int
64440209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
6445fa9e4066Sahrens {
64460209230bSgjelinek 	int err;
64470209230bSgjelinek 
64480209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
64490209230bSgjelinek 		return (err);
64500209230bSgjelinek 
64510209230bSgjelinek 	err = getmcapent_core(handle, tabptr);
64520209230bSgjelinek 
64530209230bSgjelinek 	(void) zonecfg_endent(handle);
64540209230bSgjelinek 
64550209230bSgjelinek 	return (err);
6456fa9e4066Sahrens }
6457ee519a1fSgjelinek 
6458ee519a1fSgjelinek int
6459ee519a1fSgjelinek zonecfg_setpkgent(zone_dochandle_t handle)
6460ee519a1fSgjelinek {
6461ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6462ee519a1fSgjelinek }
6463ee519a1fSgjelinek 
6464ee519a1fSgjelinek int
6465ee519a1fSgjelinek zonecfg_getpkgent(zone_dochandle_t handle, struct zone_pkgtab *tabptr)
6466ee519a1fSgjelinek {
6467ee519a1fSgjelinek 	xmlNodePtr cur;
6468ee519a1fSgjelinek 	int err;
6469ee519a1fSgjelinek 
6470ee519a1fSgjelinek 	if (handle == NULL)
6471ee519a1fSgjelinek 		return (Z_INVAL);
6472ee519a1fSgjelinek 
6473ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6474ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6475ee519a1fSgjelinek 
6476ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6477ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_PACKAGE))
6478ee519a1fSgjelinek 			break;
6479ee519a1fSgjelinek 	if (cur == NULL) {
6480ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6481ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6482ee519a1fSgjelinek 	}
6483ee519a1fSgjelinek 
6484ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_pkg_name,
6485ee519a1fSgjelinek 	    sizeof (tabptr->zone_pkg_name))) != Z_OK) {
6486ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6487ee519a1fSgjelinek 		return (err);
6488ee519a1fSgjelinek 	}
6489ee519a1fSgjelinek 
6490ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_VERSION, tabptr->zone_pkg_version,
6491ee519a1fSgjelinek 	    sizeof (tabptr->zone_pkg_version))) != Z_OK) {
6492ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6493ee519a1fSgjelinek 		return (err);
6494ee519a1fSgjelinek 	}
6495ee519a1fSgjelinek 
6496ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6497ee519a1fSgjelinek 	return (Z_OK);
6498ee519a1fSgjelinek }
6499ee519a1fSgjelinek 
6500ee519a1fSgjelinek int
6501ee519a1fSgjelinek zonecfg_endpkgent(zone_dochandle_t handle)
6502ee519a1fSgjelinek {
6503ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6504ee519a1fSgjelinek }
6505ee519a1fSgjelinek 
6506ee519a1fSgjelinek int
6507ee519a1fSgjelinek zonecfg_setpatchent(zone_dochandle_t handle)
6508ee519a1fSgjelinek {
6509ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6510ee519a1fSgjelinek }
6511ee519a1fSgjelinek 
6512ee519a1fSgjelinek int
6513ee519a1fSgjelinek zonecfg_getpatchent(zone_dochandle_t handle, struct zone_patchtab *tabptr)
6514ee519a1fSgjelinek {
6515ee519a1fSgjelinek 	xmlNodePtr cur;
6516ee519a1fSgjelinek 	int err;
6517ee519a1fSgjelinek 
6518ee519a1fSgjelinek 	if (handle == NULL)
6519ee519a1fSgjelinek 		return (Z_INVAL);
6520ee519a1fSgjelinek 
6521ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6522ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6523ee519a1fSgjelinek 
6524ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6525ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_PATCH))
6526ee519a1fSgjelinek 			break;
6527ee519a1fSgjelinek 	if (cur == NULL) {
6528ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6529ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6530ee519a1fSgjelinek 	}
6531ee519a1fSgjelinek 
6532ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_ID, tabptr->zone_patch_id,
6533ee519a1fSgjelinek 	    sizeof (tabptr->zone_patch_id))) != Z_OK) {
6534ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6535ee519a1fSgjelinek 		return (err);
6536ee519a1fSgjelinek 	}
6537ee519a1fSgjelinek 
6538ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6539ee519a1fSgjelinek 	return (Z_OK);
6540ee519a1fSgjelinek }
6541ee519a1fSgjelinek 
6542ee519a1fSgjelinek int
6543ee519a1fSgjelinek zonecfg_endpatchent(zone_dochandle_t handle)
6544ee519a1fSgjelinek {
6545ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6546ee519a1fSgjelinek }
6547ee519a1fSgjelinek 
6548ee519a1fSgjelinek int
6549ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
6550ee519a1fSgjelinek {
6551ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6552ee519a1fSgjelinek }
6553ee519a1fSgjelinek 
6554ee519a1fSgjelinek int
6555ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
6556ee519a1fSgjelinek {
6557ee519a1fSgjelinek 	xmlNodePtr cur;
6558ee519a1fSgjelinek 	int err;
6559ee519a1fSgjelinek 	char buf[128];
6560ee519a1fSgjelinek 
6561ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
6562ee519a1fSgjelinek 
6563ee519a1fSgjelinek 	if (handle == NULL)
6564ee519a1fSgjelinek 		return (Z_INVAL);
6565ee519a1fSgjelinek 
6566ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6567ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6568ee519a1fSgjelinek 
6569ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6570ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
6571ee519a1fSgjelinek 			break;
6572ee519a1fSgjelinek 	if (cur == NULL) {
6573ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6574ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6575ee519a1fSgjelinek 	}
6576ee519a1fSgjelinek 
6577ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
6578ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
6579ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6580ee519a1fSgjelinek 		return (err);
6581ee519a1fSgjelinek 	}
6582ee519a1fSgjelinek 
6583ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
6584ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6585ee519a1fSgjelinek 		return (err);
6586ee519a1fSgjelinek 	}
6587ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
6588ee519a1fSgjelinek 
6589ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
6590ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6591ee519a1fSgjelinek 		return (err);
6592ee519a1fSgjelinek 	}
6593ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
6594ee519a1fSgjelinek 
6595ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
6596ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6597ee519a1fSgjelinek 		return (err);
6598ee519a1fSgjelinek 	}
6599ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
6600ee519a1fSgjelinek 
6601ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
6602ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
6603ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6604ee519a1fSgjelinek 		return (err);
6605ee519a1fSgjelinek 	}
6606ee519a1fSgjelinek 
6607ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6608ee519a1fSgjelinek 	return (Z_OK);
6609ee519a1fSgjelinek }
6610ee519a1fSgjelinek 
6611ee519a1fSgjelinek int
6612ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
6613ee519a1fSgjelinek {
6614ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6615ee519a1fSgjelinek }
6616ee519a1fSgjelinek 
6617ee519a1fSgjelinek /*
6618ee519a1fSgjelinek  * Process a list of pkgs from an entry in the contents file, adding each pkg
6619ee519a1fSgjelinek  * name to the list of pkgs.
6620ee519a1fSgjelinek  *
6621ee519a1fSgjelinek  * It is possible for the pkg name to be preceeded by a special character
6622ee519a1fSgjelinek  * which indicates some bookkeeping information for pkging.  Check if the
6623ee519a1fSgjelinek  * first char is not an Alpha char.  If so, skip over it.
6624ee519a1fSgjelinek  */
6625ee519a1fSgjelinek static int
6626ee519a1fSgjelinek add_pkg_list(char *lastp, char ***plist, int *pcnt)
6627ee519a1fSgjelinek {
6628ee519a1fSgjelinek 	char	*p;
6629ee519a1fSgjelinek 	int	pkg_cnt = *pcnt;
6630ee519a1fSgjelinek 	char	**pkgs = *plist;
6631ee519a1fSgjelinek 	int	res = Z_OK;
6632ee519a1fSgjelinek 
6633ee519a1fSgjelinek 	while ((p = strtok_r(NULL, " ", &lastp)) != NULL) {
6634ee519a1fSgjelinek 		char	**tmpp;
6635ee519a1fSgjelinek 		int	i;
6636ee519a1fSgjelinek 
6637ee519a1fSgjelinek 		/* skip over any special pkg bookkeeping char */
6638ee519a1fSgjelinek 		if (!isalpha(*p))
6639ee519a1fSgjelinek 			p++;
6640ee519a1fSgjelinek 
6641ee519a1fSgjelinek 		/* Check if the pkg is already in the list */
6642ee519a1fSgjelinek 		for (i = 0; i < pkg_cnt; i++) {
6643ee519a1fSgjelinek 			if (strcmp(p, pkgs[i]) == 0)
6644ee519a1fSgjelinek 				break;
6645ee519a1fSgjelinek 		}
6646ee519a1fSgjelinek 
6647ee519a1fSgjelinek 		if (i < pkg_cnt)
6648ee519a1fSgjelinek 			continue;
6649ee519a1fSgjelinek 
6650ee519a1fSgjelinek 		/* The pkg is not in the list; add it. */
6651ee519a1fSgjelinek 		if ((tmpp = (char **)realloc(pkgs,
6652ee519a1fSgjelinek 		    sizeof (char *) * (pkg_cnt + 1))) == NULL) {
6653ee519a1fSgjelinek 			res = Z_NOMEM;
6654ee519a1fSgjelinek 			break;
6655ee519a1fSgjelinek 		}
6656ee519a1fSgjelinek 		pkgs = tmpp;
6657ee519a1fSgjelinek 
6658ee519a1fSgjelinek 		if ((pkgs[pkg_cnt] = strdup(p)) == NULL) {
6659ee519a1fSgjelinek 			res = Z_NOMEM;
6660ee519a1fSgjelinek 			break;
6661ee519a1fSgjelinek 		}
6662ee519a1fSgjelinek 		pkg_cnt++;
6663ee519a1fSgjelinek 	}
6664ee519a1fSgjelinek 
6665ee519a1fSgjelinek 	*plist = pkgs;
6666ee519a1fSgjelinek 	*pcnt = pkg_cnt;
6667ee519a1fSgjelinek 
6668ee519a1fSgjelinek 	return (res);
6669ee519a1fSgjelinek }
6670ee519a1fSgjelinek 
6671ee519a1fSgjelinek /*
6672ee519a1fSgjelinek  * Process an entry from the contents file (type "directory") and if the
6673ee519a1fSgjelinek  * directory path is in the list of paths, add the associated list of pkgs
6674ee519a1fSgjelinek  * to the pkg list.  The input parameter "entry" will be broken up by
6675ee519a1fSgjelinek  * the parser within this function so its value will be modified when this
6676ee519a1fSgjelinek  * function exits.
6677ee519a1fSgjelinek  *
6678ee519a1fSgjelinek  * The entries we are looking for will look something like:
6679ee519a1fSgjelinek  *	/usr d none 0755 root sys SUNWctpls SUNWidnl SUNWlibCf ....
6680ee519a1fSgjelinek  */
6681ee519a1fSgjelinek static int
6682ee519a1fSgjelinek get_path_pkgs(char *entry, char **paths, int cnt, char ***pkgs, int *pkg_cnt)
6683ee519a1fSgjelinek {
6684ee519a1fSgjelinek 	char	*f1;
6685ee519a1fSgjelinek 	char	*f2;
6686ee519a1fSgjelinek 	char	*lastp;
6687ee519a1fSgjelinek 	int	i;
6688ee519a1fSgjelinek 	int	res = Z_OK;
6689ee519a1fSgjelinek 
6690ee519a1fSgjelinek 	if ((f1 = strtok_r(entry, " ", &lastp)) == NULL ||
6691ee519a1fSgjelinek 	    (f2 = strtok_r(NULL, " ", &lastp)) == NULL || strcmp(f2, "d") != 0)
6692ee519a1fSgjelinek 		return (Z_OK);
6693ee519a1fSgjelinek 
6694ee519a1fSgjelinek 	/* Check if this directory entry is in the list of paths. */
6695ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
6696ee519a1fSgjelinek 		if (fnmatch(paths[i], f1, FNM_PATHNAME) == 0) {
6697ee519a1fSgjelinek 			/*
6698ee519a1fSgjelinek 			 * We do want the pkgs for this path.  First, skip
6699ee519a1fSgjelinek 			 * over the next 4 fields in the entry so that we call
6700ee519a1fSgjelinek 			 * add_pkg_list starting with the pkg names.
6701ee519a1fSgjelinek 			 */
6702ee519a1fSgjelinek 			int j;
670307b574eeSgjelinek 			char	*nlp;
6704ee519a1fSgjelinek 
6705ee519a1fSgjelinek 			for (j = 0; j < 4 &&
6706ffbafc53Scomay 			    strtok_r(NULL, " ", &lastp) != NULL; j++)
6707ffbafc53Scomay 				;
6708ee519a1fSgjelinek 			/*
6709ee519a1fSgjelinek 			 * If there are < 4 fields this entry is corrupt,
6710ee519a1fSgjelinek 			 * just skip it.
6711ee519a1fSgjelinek 			 */
6712ee519a1fSgjelinek 			if (j < 4)
6713ee519a1fSgjelinek 				return (Z_OK);
6714ee519a1fSgjelinek 
671507b574eeSgjelinek 			/* strip newline from the line */
671607b574eeSgjelinek 			nlp = (lastp + strlen(lastp) - 1);
671707b574eeSgjelinek 			if (*nlp == '\n')
671807b574eeSgjelinek 				*nlp = '\0';
671907b574eeSgjelinek 
6720ee519a1fSgjelinek 			res = add_pkg_list(lastp, pkgs, pkg_cnt);
6721ee519a1fSgjelinek 			break;
6722ee519a1fSgjelinek 		}
6723ee519a1fSgjelinek 	}
6724ee519a1fSgjelinek 
6725ee519a1fSgjelinek 	return (res);
6726ee519a1fSgjelinek }
6727ee519a1fSgjelinek 
6728ee519a1fSgjelinek /*
6729ee519a1fSgjelinek  * Read an entry from a pkginfo or contents file.  Some of these lines can
6730ee519a1fSgjelinek  * either be arbitrarily long or be continued by a backslash at the end of
6731ee519a1fSgjelinek  * the line.  This function coalesces lines that are longer than the read
6732ee519a1fSgjelinek  * buffer, and lines that are continued, into one buffer which is returned.
6733ee519a1fSgjelinek  * The caller must free this memory.  NULL is returned when we hit EOF or
6734ee519a1fSgjelinek  * if we run out of memory (errno is set to ENOMEM).
6735ee519a1fSgjelinek  */
6736ee519a1fSgjelinek static char *
6737ee519a1fSgjelinek read_pkg_data(FILE *fp)
6738ee519a1fSgjelinek {
6739ee519a1fSgjelinek 	char *start;
6740ee519a1fSgjelinek 	char *inp;
6741ee519a1fSgjelinek 	char *p;
6742ee519a1fSgjelinek 	int char_cnt = 0;
6743ee519a1fSgjelinek 
6744ee519a1fSgjelinek 	errno = 0;
6745ee519a1fSgjelinek 	if ((start = (char *)malloc(PKGINFO_RD_LEN)) == NULL) {
6746ee519a1fSgjelinek 		errno = ENOMEM;
6747ee519a1fSgjelinek 		return (NULL);
6748ee519a1fSgjelinek 	}
6749ee519a1fSgjelinek 
6750ee519a1fSgjelinek 	inp = start;
6751ee519a1fSgjelinek 	while ((p = fgets(inp, PKGINFO_RD_LEN, fp)) != NULL) {
6752ee519a1fSgjelinek 		int len;
6753ee519a1fSgjelinek 
6754ee519a1fSgjelinek 		len = strlen(inp);
6755ee519a1fSgjelinek 		if (inp[len - 1] == '\n' &&
6756ee519a1fSgjelinek 		    (len == 1 || inp[len - 2] != '\\')) {
6757ee519a1fSgjelinek 			char_cnt = len;
6758ee519a1fSgjelinek 			break;
6759ee519a1fSgjelinek 		}
6760ee519a1fSgjelinek 
6761ee519a1fSgjelinek 		if (inp[len - 2] == '\\')
6762ee519a1fSgjelinek 			char_cnt += len - 2;
6763ee519a1fSgjelinek 		else
6764ee519a1fSgjelinek 			char_cnt += PKGINFO_RD_LEN - 1;
6765ee519a1fSgjelinek 
6766ee519a1fSgjelinek 		if ((p = realloc(start, char_cnt + PKGINFO_RD_LEN)) == NULL) {
6767ee519a1fSgjelinek 			errno = ENOMEM;
6768ee519a1fSgjelinek 			break;
6769ee519a1fSgjelinek 		}
6770ee519a1fSgjelinek 
6771ee519a1fSgjelinek 		start = p;
6772ee519a1fSgjelinek 		inp = start + char_cnt;
6773ee519a1fSgjelinek 	}
6774ee519a1fSgjelinek 
6775ee519a1fSgjelinek 	if (errno == ENOMEM || (p == NULL && char_cnt == 0)) {
6776ee519a1fSgjelinek 		free(start);
6777ee519a1fSgjelinek 		start = NULL;
6778ee519a1fSgjelinek 	}
6779ee519a1fSgjelinek 
6780ee519a1fSgjelinek 	return (start);
6781ee519a1fSgjelinek }
6782ee519a1fSgjelinek 
6783ee519a1fSgjelinek static void
6784ee519a1fSgjelinek free_ipd_pkgs(char **pkgs, int cnt)
6785ee519a1fSgjelinek {
6786ee519a1fSgjelinek 	int i;
6787ee519a1fSgjelinek 
6788ee519a1fSgjelinek 	for (i = 0; i < cnt; i++)
6789ee519a1fSgjelinek 		free(pkgs[i]);
6790ee519a1fSgjelinek 	free(pkgs);
6791ee519a1fSgjelinek }
6792ee519a1fSgjelinek 
6793ee519a1fSgjelinek /*
6794ee519a1fSgjelinek  * Get the list of inherited-pkg-dirs (ipd) for the zone and then get the
6795ee519a1fSgjelinek  * list of pkgs that deliver into those dirs.
6796ee519a1fSgjelinek  */
6797ee519a1fSgjelinek static int
6798ee519a1fSgjelinek get_ipd_pkgs(zone_dochandle_t handle, char ***pkg_list, int *cnt)
6799ee519a1fSgjelinek {
6800ee519a1fSgjelinek 	int	res;
6801ee519a1fSgjelinek 	struct zone_fstab fstab;
6802ee519a1fSgjelinek 	int	ipd_cnt = 0;
6803ee519a1fSgjelinek 	char	**ipds = NULL;
6804ee519a1fSgjelinek 	int	pkg_cnt = 0;
6805ee519a1fSgjelinek 	char	**pkgs = NULL;
6806ee519a1fSgjelinek 	int	i;
6807ee519a1fSgjelinek 
6808ee519a1fSgjelinek 	if ((res = zonecfg_setipdent(handle)) != Z_OK)
6809ee519a1fSgjelinek 		return (res);
6810ee519a1fSgjelinek 
6811ee519a1fSgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
6812ee519a1fSgjelinek 		char	**p;
6813ee519a1fSgjelinek 		int	len;
6814ee519a1fSgjelinek 
6815ee519a1fSgjelinek 		if ((p = (char **)realloc(ipds,
6816ee519a1fSgjelinek 		    sizeof (char *) * (ipd_cnt + 2))) == NULL) {
6817ee519a1fSgjelinek 			res = Z_NOMEM;
6818ee519a1fSgjelinek 			break;
6819ee519a1fSgjelinek 		}
6820ee519a1fSgjelinek 		ipds = p;
6821ee519a1fSgjelinek 
6822ee519a1fSgjelinek 		if ((ipds[ipd_cnt] = strdup(fstab.zone_fs_dir)) == NULL) {
6823ee519a1fSgjelinek 			res = Z_NOMEM;
6824ee519a1fSgjelinek 			break;
6825ee519a1fSgjelinek 		}
6826ee519a1fSgjelinek 		ipd_cnt++;
6827ee519a1fSgjelinek 
6828ee519a1fSgjelinek 		len = strlen(fstab.zone_fs_dir) + 3;
6829ee519a1fSgjelinek 		if ((ipds[ipd_cnt] = malloc(len)) == NULL) {
6830ee519a1fSgjelinek 			res = Z_NOMEM;
6831ee519a1fSgjelinek 			break;
6832ee519a1fSgjelinek 		}
6833ee519a1fSgjelinek 
6834ee519a1fSgjelinek 		(void) snprintf(ipds[ipd_cnt], len, "%s/*", fstab.zone_fs_dir);
6835ee519a1fSgjelinek 		ipd_cnt++;
6836ee519a1fSgjelinek 	}
6837ee519a1fSgjelinek 
6838ee519a1fSgjelinek 	(void) zonecfg_endipdent(handle);
6839ee519a1fSgjelinek 
6840ee519a1fSgjelinek 	if (res != Z_OK) {
6841ee519a1fSgjelinek 		for (i = 0; i < ipd_cnt; i++)
6842ee519a1fSgjelinek 			free(ipds[i]);
6843ee519a1fSgjelinek 		free(ipds);
6844ee519a1fSgjelinek 		return (res);
6845ee519a1fSgjelinek 	}
6846ee519a1fSgjelinek 
6847ee519a1fSgjelinek 	/* We only have to process the contents file if we have ipds. */
6848ee519a1fSgjelinek 	if (ipd_cnt > 0) {
6849ee519a1fSgjelinek 		FILE	*fp;
6850ee519a1fSgjelinek 
6851ee519a1fSgjelinek 		if ((fp = fopen(CONTENTS_FILE, "r")) != NULL) {
6852ee519a1fSgjelinek 			char	*buf;
6853ee519a1fSgjelinek 
6854ee519a1fSgjelinek 			while ((buf = read_pkg_data(fp)) != NULL) {
6855ee519a1fSgjelinek 				res = get_path_pkgs(buf, ipds, ipd_cnt, &pkgs,
6856ee519a1fSgjelinek 				    &pkg_cnt);
6857ee519a1fSgjelinek 				free(buf);
6858ee519a1fSgjelinek 				if (res != Z_OK)
6859ee519a1fSgjelinek 					break;
6860ee519a1fSgjelinek 			}
6861ee519a1fSgjelinek 
6862ee519a1fSgjelinek 			(void) fclose(fp);
6863ee519a1fSgjelinek 		}
6864ee519a1fSgjelinek 	}
6865ee519a1fSgjelinek 
6866ee519a1fSgjelinek 	for (i = 0; i < ipd_cnt; i++)
6867ee519a1fSgjelinek 		free(ipds[i]);
6868ee519a1fSgjelinek 	free(ipds);
6869ee519a1fSgjelinek 
6870ee519a1fSgjelinek 	if (res != Z_OK) {
6871ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
6872ee519a1fSgjelinek 	} else {
6873ee519a1fSgjelinek 		*pkg_list = pkgs;
6874ee519a1fSgjelinek 		*cnt = pkg_cnt;
6875ee519a1fSgjelinek 	}
6876ee519a1fSgjelinek 
6877ee519a1fSgjelinek 	return (res);
6878ee519a1fSgjelinek }
6879ee519a1fSgjelinek 
6880ee519a1fSgjelinek /*
6881ee519a1fSgjelinek  * Return true if pkg_name is in the list of pkgs that deliver into an
6882ee519a1fSgjelinek  * inherited pkg directory for the zone.
6883ee519a1fSgjelinek  */
6884ee519a1fSgjelinek static boolean_t
6885ee519a1fSgjelinek dir_pkg(char *pkg_name, char **pkg_list, int cnt)
6886ee519a1fSgjelinek {
6887ee519a1fSgjelinek 	int i;
6888ee519a1fSgjelinek 
6889ee519a1fSgjelinek 	for (i = 0; i < cnt; i++) {
6890ee519a1fSgjelinek 		if (strcmp(pkg_name, pkg_list[i]) == 0)
6891ee519a1fSgjelinek 			return (B_TRUE);
6892ee519a1fSgjelinek 	}
6893ee519a1fSgjelinek 
6894ee519a1fSgjelinek 	return (B_FALSE);
6895ee519a1fSgjelinek }
6896ee519a1fSgjelinek 
6897ee519a1fSgjelinek /*
6898ee519a1fSgjelinek  * Start by adding the patch to the sw inventory on the handle.
6899ee519a1fSgjelinek  *
6900ee519a1fSgjelinek  * The info parameter will be the portion of the PATCH_INFO_ entry following
6901ee519a1fSgjelinek  * the '='.  For example:
6902ee519a1fSgjelinek  * Installed: Wed Dec  7 07:13:51 PST 2005 From: mum Obsoletes: 120777-03 \
6903ee519a1fSgjelinek  *	121087-02 119108-07 Requires: 119575-02 119255-06 Incompatibles:
6904ee519a1fSgjelinek  *
690507b574eeSgjelinek  * A backed out patch will have an info line of "backed out\n".  We should
690607b574eeSgjelinek  * skip these patches.
690707b574eeSgjelinek  *
6908ee519a1fSgjelinek  * We also want to add the Obsolete and Incompatible patches to the
6909ee519a1fSgjelinek  * sw inventory description of this patch.
6910ee519a1fSgjelinek  */
6911ee519a1fSgjelinek static int
6912ee519a1fSgjelinek add_patch(zone_dochandle_t handle, char *patch, char *info)
6913ee519a1fSgjelinek {
6914ee519a1fSgjelinek 	xmlNodePtr	node;
6915ee519a1fSgjelinek 	xmlNodePtr	cur;
6916ee519a1fSgjelinek 	int		err;
6917ee519a1fSgjelinek 	char		*p;
6918ee519a1fSgjelinek 	char		*lastp;
6919ee519a1fSgjelinek 	boolean_t	add_info = B_FALSE;
6920ee519a1fSgjelinek 	boolean_t	obsolete;
6921ee519a1fSgjelinek 
692207b574eeSgjelinek 	if (strcmp(info, "backed out\n") == 0)
692307b574eeSgjelinek 		return (Z_OK);
692407b574eeSgjelinek 
6925ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
6926ee519a1fSgjelinek 		return (err);
6927ee519a1fSgjelinek 
6928ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
6929ee519a1fSgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
6930ee519a1fSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, patch)) != Z_OK)
6931ee519a1fSgjelinek 		return (err);
6932ee519a1fSgjelinek 
6933ee519a1fSgjelinek 	/*
6934ee519a1fSgjelinek 	 * Start with the first token.  This will probably be "Installed:".
6935ee519a1fSgjelinek 	 * If we can't tokenize this entry, just return.
6936ee519a1fSgjelinek 	 */
6937ee519a1fSgjelinek 	if ((p = strtok_r(info, " ", &lastp)) == NULL)
6938ee519a1fSgjelinek 		return (Z_OK);
6939ee519a1fSgjelinek 
6940ee519a1fSgjelinek 	do {
6941ee519a1fSgjelinek 		xmlNodePtr new_node;
6942ee519a1fSgjelinek 		char	*nlp;
6943ee519a1fSgjelinek 
6944ee519a1fSgjelinek 		if (strcmp(p, "Installed:") == 0 ||
6945ee519a1fSgjelinek 		    strcmp(p, "Requires:") == 0 ||
6946ee519a1fSgjelinek 		    strcmp(p, "From:") == 0) {
6947ee519a1fSgjelinek 			add_info = B_FALSE;
6948ee519a1fSgjelinek 			continue;
6949ee519a1fSgjelinek 		} else if (strcmp(p, "Obsoletes:") == 0) {
6950ee519a1fSgjelinek 			obsolete = B_TRUE;
6951ee519a1fSgjelinek 			add_info = B_TRUE;
6952ee519a1fSgjelinek 			continue;
6953ee519a1fSgjelinek 		} else if (strcmp(p, "Incompatibles:") == 0) {
6954ee519a1fSgjelinek 			obsolete = B_FALSE;
6955ee519a1fSgjelinek 			add_info = B_TRUE;
6956ee519a1fSgjelinek 			continue;
6957ee519a1fSgjelinek 		}
6958ee519a1fSgjelinek 
6959ee519a1fSgjelinek 		if (!add_info)
6960ee519a1fSgjelinek 			continue;
6961ee519a1fSgjelinek 
6962ee519a1fSgjelinek 		/* strip newline from last patch in the line */
6963ee519a1fSgjelinek 		nlp = (p + strlen(p) - 1);
6964ee519a1fSgjelinek 		if (*nlp == '\n')
6965ee519a1fSgjelinek 			*nlp = '\0';
6966ee519a1fSgjelinek 
6967ee519a1fSgjelinek 		if (obsolete)
6968ee519a1fSgjelinek 			new_node = xmlNewTextChild(node, NULL,
6969ee519a1fSgjelinek 			    DTD_ELEM_OBSOLETES, NULL);
6970ee519a1fSgjelinek 		else
6971ee519a1fSgjelinek 			new_node = xmlNewTextChild(node, NULL,
6972ee519a1fSgjelinek 			    DTD_ELEM_INCOMPATIBLE, NULL);
6973ee519a1fSgjelinek 
6974ee519a1fSgjelinek 		if ((err = newprop(new_node, DTD_ATTR_ID, p)) != Z_OK)
6975ee519a1fSgjelinek 			return (err);
6976ee519a1fSgjelinek 
6977ee519a1fSgjelinek 	} while ((p = strtok_r(NULL, " ", &lastp)) != NULL);
6978ee519a1fSgjelinek 
6979ee519a1fSgjelinek 	return (Z_OK);
6980ee519a1fSgjelinek }
6981ee519a1fSgjelinek 
6982ee519a1fSgjelinek static boolean_t
6983ee519a1fSgjelinek unique_patch(zone_dochandle_t handle, char *patch)
6984ee519a1fSgjelinek {
6985ee519a1fSgjelinek 	xmlNodePtr	cur;
6986ee519a1fSgjelinek 	char		id[MAXNAMELEN];
6987ee519a1fSgjelinek 
6988ee519a1fSgjelinek 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
6989ee519a1fSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6990ee519a1fSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
6991ee519a1fSgjelinek 			if (fetchprop(cur, DTD_ATTR_ID, id, sizeof (id))
6992ee519a1fSgjelinek 			    != Z_OK)
6993ee519a1fSgjelinek 				continue;
6994ee519a1fSgjelinek 
6995ee519a1fSgjelinek 			if (strcmp(patch, id) == 0)
6996ee519a1fSgjelinek 				return (B_FALSE);
6997ee519a1fSgjelinek 		}
6998ee519a1fSgjelinek 	}
6999ee519a1fSgjelinek 
7000ee519a1fSgjelinek 	return (B_TRUE);
7001ee519a1fSgjelinek }
7002ee519a1fSgjelinek 
7003ee519a1fSgjelinek /*
7004ee519a1fSgjelinek  * Add the unique patches associated with this pkg to the sw inventory on the
7005ee519a1fSgjelinek  * handle.
7006ee519a1fSgjelinek  *
7007ee519a1fSgjelinek  * We are processing entries of the form:
7008ee519a1fSgjelinek  * PATCH_INFO_121454-02=Installed: Wed Dec  7 07:13:51 PST 2005 From: mum \
7009ee519a1fSgjelinek  *	Obsoletes: 120777-03 121087-02 119108-07 Requires: 119575-02 \
7010ee519a1fSgjelinek  *	119255-06 Incompatibles:
7011ee519a1fSgjelinek  *
7012ee519a1fSgjelinek  */
7013ee519a1fSgjelinek static int
7014ee519a1fSgjelinek add_patches(zone_dochandle_t handle, struct zone_pkginfo *infop)
7015ee519a1fSgjelinek {
7016ee519a1fSgjelinek 	int i;
7017ee519a1fSgjelinek 	int res = Z_OK;
7018ee519a1fSgjelinek 
7019ee519a1fSgjelinek 	for (i = 0; i < infop->zpi_patch_cnt; i++) {
7020ee519a1fSgjelinek 		char *p, *ep;
7021ee519a1fSgjelinek 
7022ee519a1fSgjelinek 		if (strlen(infop->zpi_patchinfo[i]) < (sizeof (PATCHINFO) - 1))
7023ee519a1fSgjelinek 			continue;
7024ee519a1fSgjelinek 
7025ee519a1fSgjelinek 		/* Skip over "PATCH_INFO_" to get the patch id. */
7026ee519a1fSgjelinek 		p = infop->zpi_patchinfo[i] + sizeof (PATCHINFO) - 1;
7027ee519a1fSgjelinek 		if ((ep = strchr(p, '=')) == NULL)
7028ee519a1fSgjelinek 			continue;
7029ee519a1fSgjelinek 
7030ee519a1fSgjelinek 		*ep = '\0';
7031ee519a1fSgjelinek 		if (unique_patch(handle, p))
703207b574eeSgjelinek 			if ((res = add_patch(handle, p, ep + 1)) != Z_OK)
703307b574eeSgjelinek 				break;
7034ee519a1fSgjelinek 	}
7035ee519a1fSgjelinek 
7036ee519a1fSgjelinek 	return (res);
7037ee519a1fSgjelinek }
7038ee519a1fSgjelinek 
7039ee519a1fSgjelinek /*
7040ee519a1fSgjelinek  * Add the pkg to the sw inventory on the handle.
7041ee519a1fSgjelinek  */
7042ee519a1fSgjelinek static int
7043ee519a1fSgjelinek add_pkg(zone_dochandle_t handle, char *name, char *version)
7044ee519a1fSgjelinek {
7045ee519a1fSgjelinek 	xmlNodePtr newnode;
7046ee519a1fSgjelinek 	xmlNodePtr cur;
7047ee519a1fSgjelinek 	int err;
7048ee519a1fSgjelinek 
7049ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
7050ee519a1fSgjelinek 		return (err);
7051ee519a1fSgjelinek 
7052ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
7053ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
7054ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
7055ee519a1fSgjelinek 		return (err);
7056ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
7057ee519a1fSgjelinek 		return (err);
7058ee519a1fSgjelinek 	return (Z_OK);
7059ee519a1fSgjelinek }
7060ee519a1fSgjelinek 
7061ee519a1fSgjelinek static void
7062ee519a1fSgjelinek free_pkginfo(struct zone_pkginfo *infop)
7063ee519a1fSgjelinek {
7064ee519a1fSgjelinek 	free(infop->zpi_version);
7065ee519a1fSgjelinek 	if (infop->zpi_patch_cnt > 0) {
7066ee519a1fSgjelinek 		int i;
7067ee519a1fSgjelinek 
7068ee519a1fSgjelinek 		for (i = 0; i < infop->zpi_patch_cnt; i++)
7069ee519a1fSgjelinek 			free(infop->zpi_patchinfo[i]);
7070ee519a1fSgjelinek 		free(infop->zpi_patchinfo);
7071ee519a1fSgjelinek 	}
7072ee519a1fSgjelinek }
7073ee519a1fSgjelinek 
7074ee519a1fSgjelinek /*
7075ee519a1fSgjelinek  * Read the pkginfo file and populate the structure with the data we need
7076ee519a1fSgjelinek  * from this pkg for a sw inventory.
7077ee519a1fSgjelinek  */
7078ee519a1fSgjelinek static int
7079ee519a1fSgjelinek get_pkginfo(char *pkginfo, struct zone_pkginfo *infop)
7080ee519a1fSgjelinek {
7081ee519a1fSgjelinek 	FILE	*fp;
7082ee519a1fSgjelinek 	char	*buf;
7083ee519a1fSgjelinek 	int	err = 0;
7084ee519a1fSgjelinek 
7085ee519a1fSgjelinek 	infop->zpi_all_zones = B_FALSE;
7086ee519a1fSgjelinek 	infop->zpi_this_zone = B_FALSE;
7087ee519a1fSgjelinek 	infop->zpi_version = NULL;
7088ee519a1fSgjelinek 	infop->zpi_patch_cnt = 0;
7089ee519a1fSgjelinek 	infop->zpi_patchinfo = NULL;
7090ee519a1fSgjelinek 
7091ee519a1fSgjelinek 	if ((fp = fopen(pkginfo, "r")) == NULL)
7092ee519a1fSgjelinek 		return (errno);
7093ee519a1fSgjelinek 
7094ee519a1fSgjelinek 	while ((buf = read_pkg_data(fp)) != NULL) {
7095ee519a1fSgjelinek 		if (strncmp(buf, VERSION, sizeof (VERSION) - 1) == 0) {
7096ee519a1fSgjelinek 			int len;
7097ee519a1fSgjelinek 
7098ee519a1fSgjelinek 			if ((infop->zpi_version =
7099ee519a1fSgjelinek 			    strdup(buf + sizeof (VERSION) - 1)) == NULL) {
7100ee519a1fSgjelinek 				err = ENOMEM;
7101ee519a1fSgjelinek 				break;
7102ee519a1fSgjelinek 			}
7103ee519a1fSgjelinek 
7104ee519a1fSgjelinek 			/* remove trailing newline */
7105ee519a1fSgjelinek 			len = strlen(infop->zpi_version);
7106ee519a1fSgjelinek 			*(infop->zpi_version + len - 1) = 0;
7107ee519a1fSgjelinek 
710845916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_ALL_ZONES) == 0) {
7109ee519a1fSgjelinek 			infop->zpi_all_zones = B_TRUE;
7110ee519a1fSgjelinek 
711145916cd2Sjpk 		} else if (strcmp(buf, SUNW_PKG_THIS_ZONE) == 0) {
7112ee519a1fSgjelinek 			infop->zpi_this_zone = B_TRUE;
7113ee519a1fSgjelinek 
7114ee519a1fSgjelinek 		} else if (strncmp(buf, PATCHINFO, sizeof (PATCHINFO) - 1)
7115ee519a1fSgjelinek 		    == 0) {
7116ee519a1fSgjelinek 			char **p;
7117ee519a1fSgjelinek 
7118ee519a1fSgjelinek 			if ((p = (char **)realloc(infop->zpi_patchinfo,
7119ee519a1fSgjelinek 			    sizeof (char *) * (infop->zpi_patch_cnt + 1)))
7120ee519a1fSgjelinek 			    == NULL) {
7121ee519a1fSgjelinek 				err = ENOMEM;
7122ee519a1fSgjelinek 				break;
7123ee519a1fSgjelinek 			}
7124ee519a1fSgjelinek 			infop->zpi_patchinfo = p;
7125ee519a1fSgjelinek 
7126ee519a1fSgjelinek 			if ((infop->zpi_patchinfo[infop->zpi_patch_cnt] =
7127ee519a1fSgjelinek 			    strdup(buf)) == NULL) {
7128ee519a1fSgjelinek 				err = ENOMEM;
7129ee519a1fSgjelinek 				break;
7130ee519a1fSgjelinek 			}
7131ee519a1fSgjelinek 			infop->zpi_patch_cnt++;
7132ee519a1fSgjelinek 		}
7133ee519a1fSgjelinek 
7134ee519a1fSgjelinek 		free(buf);
7135ee519a1fSgjelinek 	}
7136ee519a1fSgjelinek 
7137ee519a1fSgjelinek 	free(buf);
7138ee519a1fSgjelinek 
7139ee519a1fSgjelinek 	if (errno == ENOMEM) {
7140ee519a1fSgjelinek 		err = ENOMEM;
7141ee519a1fSgjelinek 		/* Clean up anything we did manage to allocate. */
7142ee519a1fSgjelinek 		free_pkginfo(infop);
7143ee519a1fSgjelinek 	}
7144ee519a1fSgjelinek 
7145ee519a1fSgjelinek 	(void) fclose(fp);
7146ee519a1fSgjelinek 
7147ee519a1fSgjelinek 	return (err);
7148ee519a1fSgjelinek }
7149ee519a1fSgjelinek 
7150ee519a1fSgjelinek /*
7151ee519a1fSgjelinek  * Take a software inventory of the global zone.  We need to get the set of
7152ee519a1fSgjelinek  * packages and patches that are on the global zone that the specified
7153ee519a1fSgjelinek  * non-global zone depends on.  The packages we need in the inventory are:
7154ee519a1fSgjelinek  *
7155ee519a1fSgjelinek  * - skip the package if SUNW_PKG_THISZONE is 'true'
7156ee519a1fSgjelinek  * otherwise,
7157ee519a1fSgjelinek  * - add the package if
7158ee519a1fSgjelinek  * a) SUNW_PKG_ALLZONES is 'true',
7159ee519a1fSgjelinek  * or
7160ee519a1fSgjelinek  * b) any file delivered by the package is in a file system that is inherited
7161ee519a1fSgjelinek  * from the global zone.
7162ee519a1fSgjelinek  * If the zone does not inherit any file systems (whole root)
7163ee519a1fSgjelinek  * then (b) will be skipped.
7164ee519a1fSgjelinek  *
7165ee519a1fSgjelinek  * For each of the packages that is being added to the inventory, we will also
7166ee519a1fSgjelinek  * add all of the associated, unique patches to the inventory.
7167ee519a1fSgjelinek  */
7168ee519a1fSgjelinek static int
7169ee519a1fSgjelinek zonecfg_sw_inventory(zone_dochandle_t handle)
7170ee519a1fSgjelinek {
7171ee519a1fSgjelinek 	char		pkginfo[MAXPATHLEN];
7172ee519a1fSgjelinek 	int		res;
7173ee519a1fSgjelinek 	struct dirent	*dp;
7174ee519a1fSgjelinek 	DIR		*dirp;
7175ee519a1fSgjelinek 	struct stat	buf;
7176ee519a1fSgjelinek 	struct zone_pkginfo	info;
7177ee519a1fSgjelinek 	int		pkg_cnt = 0;
7178ee519a1fSgjelinek 	char		**pkgs = NULL;
7179ee519a1fSgjelinek 
7180ee519a1fSgjelinek 	if ((res = get_ipd_pkgs(handle, &pkgs, &pkg_cnt)) != Z_OK)
7181ee519a1fSgjelinek 		return (res);
7182ee519a1fSgjelinek 
7183ee519a1fSgjelinek 	if ((dirp = opendir(PKG_PATH)) == NULL) {
7184ee519a1fSgjelinek 		free_ipd_pkgs(pkgs, pkg_cnt);
7185ee519a1fSgjelinek 		return (Z_OK);
7186ee519a1fSgjelinek 	}
7187ee519a1fSgjelinek 
7188ee519a1fSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
7189ee519a1fSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
7190ee519a1fSgjelinek 		    strcmp(dp->d_name, "..") == 0)
7191ee519a1fSgjelinek 			continue;
7192ee519a1fSgjelinek 
7193ee519a1fSgjelinek 		(void) snprintf(pkginfo, sizeof (pkginfo), "%s/%s/pkginfo",
7194ee519a1fSgjelinek 		    PKG_PATH, dp->d_name);
7195ee519a1fSgjelinek 
7196ee519a1fSgjelinek 		if (stat(pkginfo, &buf) == -1 || !S_ISREG(buf.st_mode))
7197ee519a1fSgjelinek 			continue;
7198ee519a1fSgjelinek 
7199ee519a1fSgjelinek 		if (get_pkginfo(pkginfo, &info) != 0) {
7200ee519a1fSgjelinek 			res = Z_NOMEM;
7201ee519a1fSgjelinek 			break;
7202ee519a1fSgjelinek 		}
7203ee519a1fSgjelinek 
7204ee519a1fSgjelinek 		if (!info.zpi_this_zone &&
7205ee519a1fSgjelinek 		    (info.zpi_all_zones ||
7206ee519a1fSgjelinek 		    dir_pkg(dp->d_name, pkgs, pkg_cnt))) {
7207ee519a1fSgjelinek 			if ((res = add_pkg(handle, dp->d_name,
7208ee519a1fSgjelinek 			    info.zpi_version)) == Z_OK) {
7209ee519a1fSgjelinek 				if (info.zpi_patch_cnt > 0)
7210ee519a1fSgjelinek 					res = add_patches(handle, &info);
7211ee519a1fSgjelinek 			}
7212ee519a1fSgjelinek 		}
7213ee519a1fSgjelinek 
7214ee519a1fSgjelinek 		free_pkginfo(&info);
7215ee519a1fSgjelinek 
7216ee519a1fSgjelinek 		if (res != Z_OK)
7217ee519a1fSgjelinek 			break;
7218ee519a1fSgjelinek 	}
7219ee519a1fSgjelinek 
7220ee519a1fSgjelinek 	(void) closedir(dirp);
7221ee519a1fSgjelinek 
7222ee519a1fSgjelinek 	free_ipd_pkgs(pkgs, pkg_cnt);
7223ee519a1fSgjelinek 
7224ee519a1fSgjelinek 	if (res == Z_OK)
7225ee519a1fSgjelinek 		handle->zone_dh_sw_inv = B_TRUE;
7226ee519a1fSgjelinek 
7227ee519a1fSgjelinek 	return (res);
7228ee519a1fSgjelinek }
7229ee519a1fSgjelinek 
7230ee519a1fSgjelinek /*
7231ee519a1fSgjelinek  * zonecfg_devwalk call-back function used during detach to generate the
7232ee519a1fSgjelinek  * dev info in the manifest.
7233ee519a1fSgjelinek  */
7234ee519a1fSgjelinek static int
7235ee519a1fSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
7236ee519a1fSgjelinek     const char *acl, void *hdl)
7237ee519a1fSgjelinek {
7238ee519a1fSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
7239ee519a1fSgjelinek 	xmlNodePtr newnode;
7240ee519a1fSgjelinek 	xmlNodePtr cur;
7241ee519a1fSgjelinek 	int err;
7242ee519a1fSgjelinek 	char buf[128];
7243ee519a1fSgjelinek 
7244ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
7245ee519a1fSgjelinek 		return (err);
7246ee519a1fSgjelinek 
7247ee519a1fSgjelinek 	cur = handle->zone_dh_cur;
7248ee519a1fSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
7249ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
7250ee519a1fSgjelinek 		return (err);
7251ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
7252ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
7253ee519a1fSgjelinek 		return (err);
7254ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
7255ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
7256ee519a1fSgjelinek 		return (err);
7257ee519a1fSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
7258ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
7259ee519a1fSgjelinek 		return (err);
7260ee519a1fSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
7261ee519a1fSgjelinek 		return (err);
7262ee519a1fSgjelinek 	return (Z_OK);
7263ee519a1fSgjelinek }
7264ee519a1fSgjelinek 
7265ee519a1fSgjelinek /*
7266ee519a1fSgjelinek  * Get the information required to support detaching a zone.  This is
7267ee519a1fSgjelinek  * called on the source system when detaching (the detaching parameter should
7268ee519a1fSgjelinek  * be set to true) and on the destination system before attaching (the
7269ee519a1fSgjelinek  * detaching parameter should be false).
7270ee519a1fSgjelinek  *
7271ee519a1fSgjelinek  * For native Solaris zones, the detach/attach process involves validating
7272ee519a1fSgjelinek  * that the software on the global zone can support the zone when we attach.
7273ee519a1fSgjelinek  * To do this we take a software inventory of the global zone.  We also
7274ee519a1fSgjelinek  * have to keep track of the device configuration so that we can properly
7275ee519a1fSgjelinek  * recreate it on the destination.
7276ee519a1fSgjelinek  */
7277ee519a1fSgjelinek int
7278ee519a1fSgjelinek zonecfg_get_detach_info(zone_dochandle_t handle, boolean_t detaching)
7279ee519a1fSgjelinek {
7280ee519a1fSgjelinek 	int		res;
7281ee519a1fSgjelinek 
7282ee519a1fSgjelinek 	if ((res = zonecfg_sw_inventory(handle)) != Z_OK)
7283ee519a1fSgjelinek 		return (res);
7284ee519a1fSgjelinek 
7285ee519a1fSgjelinek 	if (detaching)
7286ee519a1fSgjelinek 		res = zonecfg_devwalk(handle, get_detach_dev_entry, handle);
7287ee519a1fSgjelinek 
7288ee519a1fSgjelinek 	return (res);
7289ee519a1fSgjelinek }
7290