xref: /titanic_50/usr/src/lib/libzonecfg/common/libzonecfg.c (revision 16ab8c7b0738497a3fa85cb1afebcfd7844517d8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ee519a1fSgjelinek  * Common Development and Distribution License (the "License").
6ee519a1fSgjelinek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21ffbafc53Scomay 
227c478bd9Sstevel@tonic-gate /*
236cfd72c6Sgjelinek  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
29cf8f45c7Sdstaff #include <libsysevent.h>
30cf8f45c7Sdstaff #include <pthread.h>
31cf8f45c7Sdstaff #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <fnmatch.h>
347c478bd9Sstevel@tonic-gate #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <assert.h>
377c478bd9Sstevel@tonic-gate #include <libgen.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <alloca.h>
407c478bd9Sstevel@tonic-gate #include <ctype.h>
419acbbeafSnn35248 #include <sys/acl.h>
429acbbeafSnn35248 #include <sys/stat.h>
439acbbeafSnn35248 #include <sys/brand.h>
447c478bd9Sstevel@tonic-gate #include <sys/mntio.h>
457c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
46cf8f45c7Sdstaff #include <sys/nvpair.h>
479acbbeafSnn35248 #include <sys/types.h>
48f4b3ec61Sdh155122 #include <sys/sockio.h>
49ee519a1fSgjelinek #include <ftw.h>
500209230bSgjelinek #include <pool.h>
510209230bSgjelinek #include <libscf.h>
520209230bSgjelinek #include <libproc.h>
530209230bSgjelinek #include <sys/priocntl.h>
5439935be5Sgjelinek #include <libuutil.h>
55ff17c8bfSgjelinek #include <wait.h>
56ff17c8bfSgjelinek #include <bsm/adt.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
597c478bd9Sstevel@tonic-gate #include <netdb.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <libxml/xmlmemory.h>
627c478bd9Sstevel@tonic-gate #include <libxml/parser.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
65108322fbScarlsonj #include <uuid/uuid.h>
66ee519a1fSgjelinek #include <dirent.h>
679acbbeafSnn35248 #include <libbrand.h>
68ee519a1fSgjelinek 
697c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
707c478bd9Sstevel@tonic-gate #include "zonecfg_impl.h"
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	_PATH_TMPFILE	"/zonecfg.XXXXXX"
73cf8f45c7Sdstaff #define	ZONE_CB_RETRY_COUNT		10
74cf8f45c7Sdstaff #define	ZONE_EVENT_PING_SUBCLASS	"ping"
75cf8f45c7Sdstaff #define	ZONE_EVENT_PING_PUBLISHER	"solaris"
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* Hard-code the DTD element/attribute/entity names just once, here. */
787c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ATTR		(const xmlChar *) "attr"
797c478bd9Sstevel@tonic-gate #define	DTD_ELEM_COMMENT	(const xmlChar *) "comment"
807c478bd9Sstevel@tonic-gate #define	DTD_ELEM_DEVICE		(const xmlChar *) "device"
817c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FS		(const xmlChar *) "filesystem"
827c478bd9Sstevel@tonic-gate #define	DTD_ELEM_FSOPTION	(const xmlChar *) "fsoption"
837c478bd9Sstevel@tonic-gate #define	DTD_ELEM_IPD		(const xmlChar *) "inherited-pkg-dir"
847c478bd9Sstevel@tonic-gate #define	DTD_ELEM_NET		(const xmlChar *) "network"
857c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTL		(const xmlChar *) "rctl"
867c478bd9Sstevel@tonic-gate #define	DTD_ELEM_RCTLVALUE	(const xmlChar *) "rctl-value"
877c478bd9Sstevel@tonic-gate #define	DTD_ELEM_ZONE		(const xmlChar *) "zone"
88fa9e4066Sahrens #define	DTD_ELEM_DATASET	(const xmlChar *) "dataset"
890209230bSgjelinek #define	DTD_ELEM_TMPPOOL	(const xmlChar *) "tmp_pool"
900209230bSgjelinek #define	DTD_ELEM_PSET		(const xmlChar *) "pset"
910209230bSgjelinek #define	DTD_ELEM_MCAP		(const xmlChar *) "mcap"
92ee519a1fSgjelinek #define	DTD_ELEM_PACKAGE	(const xmlChar *) "package"
93ee519a1fSgjelinek #define	DTD_ELEM_PATCH		(const xmlChar *) "patch"
946cfd72c6Sgjelinek #define	DTD_ELEM_OBSOLETES	(const xmlChar *) "obsoletes"
95ee519a1fSgjelinek #define	DTD_ELEM_DEV_PERM	(const xmlChar *) "dev-perm"
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ACTION		(const xmlChar *) "action"
987c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ADDRESS	(const xmlChar *) "address"
997c478bd9Sstevel@tonic-gate #define	DTD_ATTR_AUTOBOOT	(const xmlChar *) "autoboot"
100f4b3ec61Sdh155122 #define	DTD_ATTR_IPTYPE		(const xmlChar *) "ip-type"
101de860bd9Sgfaden #define	DTD_ATTR_DEFROUTER	(const xmlChar *) "defrouter"
1027c478bd9Sstevel@tonic-gate #define	DTD_ATTR_DIR		(const xmlChar *) "directory"
1037c478bd9Sstevel@tonic-gate #define	DTD_ATTR_LIMIT		(const xmlChar *) "limit"
104ffbafc53Scomay #define	DTD_ATTR_LIMITPRIV	(const xmlChar *) "limitpriv"
1053f2f09c1Sdp #define	DTD_ATTR_BOOTARGS	(const xmlChar *) "bootargs"
1060209230bSgjelinek #define	DTD_ATTR_SCHED		(const xmlChar *) "scheduling-class"
1077c478bd9Sstevel@tonic-gate #define	DTD_ATTR_MATCH		(const xmlChar *) "match"
1087c478bd9Sstevel@tonic-gate #define	DTD_ATTR_NAME		(const xmlChar *) "name"
1097c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PHYSICAL	(const xmlChar *) "physical"
1107c478bd9Sstevel@tonic-gate #define	DTD_ATTR_POOL		(const xmlChar *) "pool"
1117c478bd9Sstevel@tonic-gate #define	DTD_ATTR_PRIV		(const xmlChar *) "priv"
1127c478bd9Sstevel@tonic-gate #define	DTD_ATTR_RAW		(const xmlChar *) "raw"
1137c478bd9Sstevel@tonic-gate #define	DTD_ATTR_SPECIAL	(const xmlChar *) "special"
1147c478bd9Sstevel@tonic-gate #define	DTD_ATTR_TYPE		(const xmlChar *) "type"
1157c478bd9Sstevel@tonic-gate #define	DTD_ATTR_VALUE		(const xmlChar *) "value"
1167c478bd9Sstevel@tonic-gate #define	DTD_ATTR_ZONEPATH	(const xmlChar *) "zonepath"
1170209230bSgjelinek #define	DTD_ATTR_NCPU_MIN	(const xmlChar *) "ncpu_min"
1180209230bSgjelinek #define	DTD_ATTR_NCPU_MAX	(const xmlChar *) "ncpu_max"
1190209230bSgjelinek #define	DTD_ATTR_IMPORTANCE	(const xmlChar *) "importance"
1200209230bSgjelinek #define	DTD_ATTR_PHYSCAP	(const xmlChar *) "physcap"
121ee519a1fSgjelinek #define	DTD_ATTR_VERSION	(const xmlChar *) "version"
122ee519a1fSgjelinek #define	DTD_ATTR_ID		(const xmlChar *) "id"
123ee519a1fSgjelinek #define	DTD_ATTR_UID		(const xmlChar *) "uid"
124ee519a1fSgjelinek #define	DTD_ATTR_GID		(const xmlChar *) "gid"
125ee519a1fSgjelinek #define	DTD_ATTR_MODE		(const xmlChar *) "mode"
126ee519a1fSgjelinek #define	DTD_ATTR_ACL		(const xmlChar *) "acl"
1279acbbeafSnn35248 #define	DTD_ATTR_BRAND		(const xmlChar *) "brand"
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_BOOLEAN	"boolean"
1307c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DEVPATH	"devpath"
1317c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRIVER	"driver"
1327c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_DRVMIN	"drv_min"
1337c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_FALSE	"false"
1347c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_INT		"int"
1357c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_STRING	"string"
1367c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_TRUE		"true"
1377c478bd9Sstevel@tonic-gate #define	DTD_ENTITY_UINT		"uint"
1387c478bd9Sstevel@tonic-gate 
139a1be23daSdp #define	DTD_ENTITY_BOOL_LEN	6	/* "false" */
140a1be23daSdp 
141ee519a1fSgjelinek #define	ATTACH_FORCED	"SUNWattached.xml"
142ee519a1fSgjelinek 
1430209230bSgjelinek #define	TMP_POOL_NAME	"SUNWtmp_%s"
1440209230bSgjelinek #define	MAX_TMP_POOL_NAME	(ZONENAME_MAX + 9)
1450209230bSgjelinek #define	RCAP_SERVICE	"system/rcap:default"
1460209230bSgjelinek #define	POOLD_SERVICE	"system/pools/dynamic:default"
1470209230bSgjelinek 
1480209230bSgjelinek /*
1490209230bSgjelinek  * rctl alias definitions
1500209230bSgjelinek  *
1510209230bSgjelinek  * This holds the alias, the full rctl name, the default priv value, action
1520209230bSgjelinek  * and lower limit.  The functions that handle rctl aliases step through
1530209230bSgjelinek  * this table, matching on the alias, and using the full values for setting
1540209230bSgjelinek  * the rctl entry as well the limit for validation.
1550209230bSgjelinek  */
1560209230bSgjelinek static struct alias {
1570209230bSgjelinek 	char *shortname;
1580209230bSgjelinek 	char *realname;
1590209230bSgjelinek 	char *priv;
1600209230bSgjelinek 	char *action;
1610209230bSgjelinek 	uint64_t low_limit;
1620209230bSgjelinek } aliases[] = {
1630209230bSgjelinek 	{ALIAS_MAXLWPS, "zone.max-lwps", "privileged", "deny", 100},
1640209230bSgjelinek 	{ALIAS_MAXSHMMEM, "zone.max-shm-memory", "privileged", "deny", 0},
1650209230bSgjelinek 	{ALIAS_MAXSHMIDS, "zone.max-shm-ids", "privileged", "deny", 0},
1660209230bSgjelinek 	{ALIAS_MAXMSGIDS, "zone.max-msg-ids", "privileged", "deny", 0},
1670209230bSgjelinek 	{ALIAS_MAXSEMIDS, "zone.max-sem-ids", "privileged", "deny", 0},
1680209230bSgjelinek 	{ALIAS_MAXLOCKEDMEM, "zone.max-locked-memory", "privileged", "deny", 0},
1690209230bSgjelinek 	{ALIAS_MAXSWAP, "zone.max-swap", "privileged", "deny", 0},
1700209230bSgjelinek 	{ALIAS_SHARES, "zone.cpu-shares", "privileged", "none", 0},
171c97ad5cdSakolb 	{ALIAS_CPUCAP, "zone.cpu-cap", "privileged", "deny", 0},
1720209230bSgjelinek 	{NULL, NULL, NULL, NULL, 0}
1730209230bSgjelinek };
1740209230bSgjelinek 
1750209230bSgjelinek /*
1760209230bSgjelinek  * Structure for applying rctls to a running zone.  It allows important
1770209230bSgjelinek  * process values to be passed together easily.
1780209230bSgjelinek  */
1790209230bSgjelinek typedef struct pr_info_handle {
1800209230bSgjelinek 	struct ps_prochandle *pr;
1810209230bSgjelinek 	pid_t pid;
1820209230bSgjelinek } pr_info_handle_t;
1830209230bSgjelinek 
1847c478bd9Sstevel@tonic-gate struct zone_dochandle {
1857c478bd9Sstevel@tonic-gate 	char		*zone_dh_rootdir;
1867c478bd9Sstevel@tonic-gate 	xmlDocPtr	zone_dh_doc;
1877c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_cur;
1887c478bd9Sstevel@tonic-gate 	xmlNodePtr	zone_dh_top;
189087719fdSdp 	boolean_t	zone_dh_newzone;
190087719fdSdp 	boolean_t	zone_dh_snapshot;
191ee519a1fSgjelinek 	boolean_t	zone_dh_sw_inv;
192087719fdSdp 	char		zone_dh_delete_name[ZONENAME_MAX];
1937c478bd9Sstevel@tonic-gate };
1947c478bd9Sstevel@tonic-gate 
195cf8f45c7Sdstaff struct znotify {
196cf8f45c7Sdstaff 	void * zn_private;
197cf8f45c7Sdstaff 	evchan_t *zn_eventchan;
198cf8f45c7Sdstaff 	int (*zn_callback)(const  char *zonename, zoneid_t zid,
199cf8f45c7Sdstaff 	    const char *newstate, const char *oldstate, hrtime_t when, void *p);
200cf8f45c7Sdstaff 	pthread_mutex_t zn_mutex;
201cf8f45c7Sdstaff 	pthread_cond_t zn_cond;
202cf8f45c7Sdstaff 	pthread_mutex_t zn_bigmutex;
203cf8f45c7Sdstaff 	volatile enum {ZN_UNLOCKED, ZN_LOCKED, ZN_PING_INFLIGHT,
204cf8f45c7Sdstaff 	    ZN_PING_RECEIVED} zn_state;
205cf8f45c7Sdstaff 	char zn_subscriber_id[MAX_SUBID_LEN];
206cf8f45c7Sdstaff 	volatile boolean_t zn_failed;
207cf8f45c7Sdstaff 	int zn_failure_count;
208cf8f45c7Sdstaff };
209cf8f45c7Sdstaff 
210ff17c8bfSgjelinek /* used to track nested zone-lock operations */
211ff17c8bfSgjelinek static int zone_lock_cnt = 0;
212ee519a1fSgjelinek 
213ff17c8bfSgjelinek /* used to communicate lock status to children */
214ff17c8bfSgjelinek #define	LOCK_ENV_VAR	"_ZONEADM_LOCK_HELD"
215ff17c8bfSgjelinek static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1";
216ff17c8bfSgjelinek static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0";
21739935be5Sgjelinek 
218108322fbScarlsonj char *zonecfg_root = "";
219108322fbScarlsonj 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * For functions which return int, which is most of the functions herein,
2227c478bd9Sstevel@tonic-gate  * the return values should be from the Z_foo set defined in <libzonecfg.h>.
2237c478bd9Sstevel@tonic-gate  * In some instances, we take pains mapping some libc errno values to Z_foo
2247c478bd9Sstevel@tonic-gate  * values from this set.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
228108322fbScarlsonj  * Set the root (/) path for all zonecfg configuration files.  This is a
229108322fbScarlsonj  * private interface used by Live Upgrade extensions to access zone
230108322fbScarlsonj  * configuration inside mounted alternate boot environments.
231108322fbScarlsonj  */
232108322fbScarlsonj void
233108322fbScarlsonj zonecfg_set_root(const char *rootpath)
234108322fbScarlsonj {
235108322fbScarlsonj 	if (*zonecfg_root != '\0')
236108322fbScarlsonj 		free(zonecfg_root);
237108322fbScarlsonj 	if (rootpath == NULL || rootpath[0] == '\0' || rootpath[1] == '\0' ||
238108322fbScarlsonj 	    (zonecfg_root = strdup(rootpath)) == NULL)
239108322fbScarlsonj 		zonecfg_root = "";
240108322fbScarlsonj }
241108322fbScarlsonj 
242108322fbScarlsonj const char *
243108322fbScarlsonj zonecfg_get_root(void)
244108322fbScarlsonj {
245108322fbScarlsonj 	return (zonecfg_root);
246108322fbScarlsonj }
247108322fbScarlsonj 
248108322fbScarlsonj boolean_t
249108322fbScarlsonj zonecfg_in_alt_root(void)
250108322fbScarlsonj {
251108322fbScarlsonj 	return (*zonecfg_root != '\0');
252108322fbScarlsonj }
253108322fbScarlsonj 
254108322fbScarlsonj /*
2557c478bd9Sstevel@tonic-gate  * Callers of the _file_path() functions are expected to have the second
2567c478bd9Sstevel@tonic-gate  * parameter be a (char foo[MAXPATHLEN]).
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate 
259108322fbScarlsonj static boolean_t
2607c478bd9Sstevel@tonic-gate config_file_path(const char *zonename, char *answer)
2617c478bd9Sstevel@tonic-gate {
262108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.xml", zonecfg_root,
263108322fbScarlsonj 	    ZONE_CONFIG_ROOT, zonename) < MAXPATHLEN);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
266108322fbScarlsonj static boolean_t
267108322fbScarlsonj snap_file_path(const char *zonename, char *answer)
2687c478bd9Sstevel@tonic-gate {
269108322fbScarlsonj 	return (snprintf(answer, MAXPATHLEN, "%s%s/%s.snapshot.xml",
270108322fbScarlsonj 	    zonecfg_root, ZONE_SNAPSHOT_ROOT, zonename) < MAXPATHLEN);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2747c478bd9Sstevel@tonic-gate static void
2757c478bd9Sstevel@tonic-gate zonecfg_error_func(void *ctx, const char *msg, ...)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * This function does nothing by design.  Its purpose is to prevent
2797c478bd9Sstevel@tonic-gate 	 * libxml from dumping unwanted messages to stdout/stderr.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate zone_dochandle_t
2847c478bd9Sstevel@tonic-gate zonecfg_init_handle(void)
2857c478bd9Sstevel@tonic-gate {
286087719fdSdp 	zone_dochandle_t handle = calloc(1, sizeof (struct zone_dochandle));
2877c478bd9Sstevel@tonic-gate 	if (handle == NULL) {
2887c478bd9Sstevel@tonic-gate 		errno = Z_NOMEM;
2897c478bd9Sstevel@tonic-gate 		return (NULL);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/* generic libxml initialization */
2937c478bd9Sstevel@tonic-gate 	xmlLineNumbersDefault(1);
2947c478bd9Sstevel@tonic-gate 	xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
2957c478bd9Sstevel@tonic-gate 	xmlDoValidityCheckingDefaultValue = 1;
2967c478bd9Sstevel@tonic-gate 	(void) xmlKeepBlanksDefault(0);
2977c478bd9Sstevel@tonic-gate 	xmlGetWarningsDefaultValue = 0;
2987c478bd9Sstevel@tonic-gate 	xmlSetGenericErrorFunc(NULL, zonecfg_error_func);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	return (handle);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate int
3047c478bd9Sstevel@tonic-gate zonecfg_check_handle(zone_dochandle_t handle)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	if (handle == NULL || handle->zone_dh_doc == NULL)
3077c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
3087c478bd9Sstevel@tonic-gate 	return (Z_OK);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate void
3127c478bd9Sstevel@tonic-gate zonecfg_fini_handle(zone_dochandle_t handle)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_OK)
3157c478bd9Sstevel@tonic-gate 		xmlFreeDoc(handle->zone_dh_doc);
3167c478bd9Sstevel@tonic-gate 	if (handle != NULL)
3177c478bd9Sstevel@tonic-gate 		free(handle);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate static int
3217c478bd9Sstevel@tonic-gate zonecfg_destroy_impl(char *filename)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
3247c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
3257c478bd9Sstevel@tonic-gate 			return (Z_ACCES);
3267c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
3277c478bd9Sstevel@tonic-gate 			return (Z_NO_ZONE);
3287c478bd9Sstevel@tonic-gate 		return (Z_MISC_FS);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 	return (Z_OK);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate int
334087719fdSdp zonecfg_destroy(const char *zonename, boolean_t force)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
337087719fdSdp 	struct zoneent ze;
338087719fdSdp 	int err, state_err;
339087719fdSdp 	zone_state_t state;
3407c478bd9Sstevel@tonic-gate 
341108322fbScarlsonj 	if (!config_file_path(zonename, path))
342108322fbScarlsonj 		return (Z_MISC_FS);
343087719fdSdp 
344087719fdSdp 	state_err = zone_get_state((char *)zonename, &state);
345087719fdSdp 	err = access(path, W_OK);
346087719fdSdp 
347087719fdSdp 	/*
348087719fdSdp 	 * If there is no file, and no index entry, reliably indicate that no
349087719fdSdp 	 * such zone exists.
350087719fdSdp 	 */
351087719fdSdp 	if ((state_err == Z_NO_ZONE) && (err == -1) && (errno == ENOENT))
352087719fdSdp 		return (Z_NO_ZONE);
353087719fdSdp 
354087719fdSdp 	/*
355087719fdSdp 	 * Handle any other filesystem related errors (except if the XML
356087719fdSdp 	 * file is missing, which we treat silently), unless we're forcing,
357087719fdSdp 	 * in which case we plow on.
358087719fdSdp 	 */
359087719fdSdp 	if (err == -1 && errno != ENOENT) {
360087719fdSdp 		if (errno == EACCES)
361087719fdSdp 			return (Z_ACCES);
362087719fdSdp 		else if (!force)
363087719fdSdp 			return (Z_MISC_FS);
364087719fdSdp 	}
365087719fdSdp 
366087719fdSdp 	if (state > ZONE_STATE_INSTALLED)
367087719fdSdp 		return (Z_BAD_ZONE_STATE);
368087719fdSdp 
369087719fdSdp 	if (!force && state > ZONE_STATE_CONFIGURED)
370087719fdSdp 		return (Z_BAD_ZONE_STATE);
371087719fdSdp 
372087719fdSdp 	/*
373087719fdSdp 	 * Index deletion succeeds even if the entry doesn't exist.  So this
374087719fdSdp 	 * will fail only if we've had some more severe problem.
375087719fdSdp 	 */
376087719fdSdp 	bzero(&ze, sizeof (ze));
377087719fdSdp 	(void) strlcpy(ze.zone_name, zonename, sizeof (ze.zone_name));
378087719fdSdp 	if ((err = putzoneent(&ze, PZE_REMOVE)) != Z_OK)
379087719fdSdp 		if (!force)
380087719fdSdp 			return (err);
381087719fdSdp 
382087719fdSdp 	err = zonecfg_destroy_impl(path);
383087719fdSdp 
384087719fdSdp 	/*
385087719fdSdp 	 * Treat failure to find the XML file silently, since, well, it's
386087719fdSdp 	 * gone, and with the index file cleaned up, we're done.
387087719fdSdp 	 */
388087719fdSdp 	if (err == Z_OK || err == Z_NO_ZONE)
389087719fdSdp 		return (Z_OK);
390087719fdSdp 	return (err);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate int
394108322fbScarlsonj zonecfg_destroy_snapshot(const char *zonename)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
3977c478bd9Sstevel@tonic-gate 
398108322fbScarlsonj 	if (!snap_file_path(zonename, path))
399108322fbScarlsonj 		return (Z_MISC_FS);
4007c478bd9Sstevel@tonic-gate 	return (zonecfg_destroy_impl(path));
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static int
404a1be23daSdp getroot(zone_dochandle_t handle, xmlNodePtr *root)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) == Z_BAD_HANDLE)
4077c478bd9Sstevel@tonic-gate 		return (Z_BAD_HANDLE);
4087c478bd9Sstevel@tonic-gate 
409a1be23daSdp 	*root = xmlDocGetRootElement(handle->zone_dh_doc);
410a1be23daSdp 
411a1be23daSdp 	if (*root == NULL)
4127c478bd9Sstevel@tonic-gate 		return (Z_EMPTY_DOCUMENT);
413a1be23daSdp 
414a1be23daSdp 	if (xmlStrcmp((*root)->name, DTD_ELEM_ZONE))
415a1be23daSdp 		return (Z_WRONG_DOC_TYPE);
416a1be23daSdp 
417a1be23daSdp 	return (Z_OK);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
420a1be23daSdp static int
421a1be23daSdp operation_prep(zone_dochandle_t handle)
422a1be23daSdp {
423a1be23daSdp 	xmlNodePtr root;
424a1be23daSdp 	int err;
425a1be23daSdp 
426a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
427a1be23daSdp 		return (err);
428a1be23daSdp 
429a1be23daSdp 	handle->zone_dh_cur = root;
430a1be23daSdp 	handle->zone_dh_top = root;
431a1be23daSdp 	return (Z_OK);
4327c478bd9Sstevel@tonic-gate }
433a1be23daSdp 
434a1be23daSdp static int
435ffbafc53Scomay fetchprop(xmlNodePtr cur, const xmlChar *propname, char *dst, size_t dstsize)
436ffbafc53Scomay {
437ffbafc53Scomay 	xmlChar *property;
438ffbafc53Scomay 	size_t srcsize;
439ffbafc53Scomay 
440ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
441ffbafc53Scomay 		return (Z_BAD_PROPERTY);
442ffbafc53Scomay 	srcsize = strlcpy(dst, (char *)property, dstsize);
443ffbafc53Scomay 	xmlFree(property);
444ffbafc53Scomay 	if (srcsize >= dstsize)
445ffbafc53Scomay 		return (Z_TOO_BIG);
446ffbafc53Scomay 	return (Z_OK);
447ffbafc53Scomay }
448ffbafc53Scomay 
449ffbafc53Scomay static int
450ffbafc53Scomay fetch_alloc_prop(xmlNodePtr cur, const xmlChar *propname, char **dst)
451ffbafc53Scomay {
452ffbafc53Scomay 	xmlChar *property;
453ffbafc53Scomay 
454ffbafc53Scomay 	if ((property = xmlGetProp(cur, propname)) == NULL)
455ffbafc53Scomay 		return (Z_BAD_PROPERTY);
456ffbafc53Scomay 	if ((*dst = strdup((char *)property)) == NULL) {
457ffbafc53Scomay 		xmlFree(property);
458ffbafc53Scomay 		return (Z_NOMEM);
459ffbafc53Scomay 	}
460ffbafc53Scomay 	xmlFree(property);
461ffbafc53Scomay 	return (Z_OK);
462ffbafc53Scomay }
463ffbafc53Scomay 
464ffbafc53Scomay static int
465a1be23daSdp getrootattr(zone_dochandle_t handle, const xmlChar *propname,
466a1be23daSdp     char *propval, size_t propsize)
467a1be23daSdp {
468a1be23daSdp 	xmlNodePtr root;
469a1be23daSdp 	int err;
470a1be23daSdp 
471a1be23daSdp 	if ((err = getroot(handle, &root)) != 0)
472a1be23daSdp 		return (err);
473a1be23daSdp 
474ffbafc53Scomay 	return (fetchprop(root, propname, propval, propsize));
475ffbafc53Scomay }
476ffbafc53Scomay 
477ffbafc53Scomay static int
478ffbafc53Scomay get_alloc_rootattr(zone_dochandle_t handle, const xmlChar *propname,
479ffbafc53Scomay     char **propval)
480ffbafc53Scomay {
481ffbafc53Scomay 	xmlNodePtr root;
482ffbafc53Scomay 	int err;
483ffbafc53Scomay 
484ffbafc53Scomay 	if ((err = getroot(handle, &root)) != 0)
485ffbafc53Scomay 		return (err);
486ffbafc53Scomay 
487ffbafc53Scomay 	return (fetch_alloc_prop(root, propname, propval));
488a1be23daSdp }
489a1be23daSdp 
490a1be23daSdp static int
491108322fbScarlsonj setrootattr(zone_dochandle_t handle, const xmlChar *propname,
492108322fbScarlsonj     const char *propval)
493a1be23daSdp {
494a1be23daSdp 	int err;
495a1be23daSdp 	xmlNodePtr root;
496a1be23daSdp 
497a1be23daSdp 	if ((err = getroot(handle, &root)) != Z_OK)
498a1be23daSdp 		return (err);
499a1be23daSdp 
5000209230bSgjelinek 	/*
5010209230bSgjelinek 	 * If we get a null propval remove the property (ignore return since it
5020209230bSgjelinek 	 * may not be set to begin with).
5030209230bSgjelinek 	 */
5040209230bSgjelinek 	if (propval == NULL) {
5050209230bSgjelinek 		(void) xmlUnsetProp(root, propname);
5060209230bSgjelinek 	} else {
5070209230bSgjelinek 		if (xmlSetProp(root, propname, (const xmlChar *) propval)
5080209230bSgjelinek 		    == NULL)
509a1be23daSdp 			return (Z_INVAL);
5100209230bSgjelinek 	}
5117c478bd9Sstevel@tonic-gate 	return (Z_OK);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
514087719fdSdp static void
515087719fdSdp addcomment(zone_dochandle_t handle, const char *comment)
516087719fdSdp {
517087719fdSdp 	xmlNodePtr node;
518087719fdSdp 	node = xmlNewComment((xmlChar *) comment);
519087719fdSdp 
520087719fdSdp 	if (node != NULL)
521087719fdSdp 		(void) xmlAddPrevSibling(handle->zone_dh_top, node);
522087719fdSdp }
523087719fdSdp 
524087719fdSdp static void
525087719fdSdp stripcomments(zone_dochandle_t handle)
526087719fdSdp {
527087719fdSdp 	xmlDocPtr top;
528087719fdSdp 	xmlNodePtr child, next;
529087719fdSdp 
530087719fdSdp 	top = handle->zone_dh_doc;
531087719fdSdp 	for (child = top->xmlChildrenNode; child != NULL; child = next) {
532087719fdSdp 		next = child->next;
533087719fdSdp 		if (child->name == NULL)
534087719fdSdp 			continue;
535087719fdSdp 		if (xmlStrcmp(child->name, DTD_ELEM_COMMENT) == 0) {
536087719fdSdp 			next = child->next;
537087719fdSdp 			xmlUnlinkNode(child);
538087719fdSdp 			xmlFreeNode(child);
539087719fdSdp 		}
540087719fdSdp 	}
541087719fdSdp }
542087719fdSdp 
543ee519a1fSgjelinek static void
544ee519a1fSgjelinek strip_sw_inv(zone_dochandle_t handle)
545ee519a1fSgjelinek {
546ee519a1fSgjelinek 	xmlNodePtr root, child, next;
547ee519a1fSgjelinek 
548ee519a1fSgjelinek 	root = xmlDocGetRootElement(handle->zone_dh_doc);
549ee519a1fSgjelinek 	for (child = root->xmlChildrenNode; child != NULL; child = next) {
550ee519a1fSgjelinek 		next = child->next;
551ee519a1fSgjelinek 		if (child->name == NULL)
552ee519a1fSgjelinek 			continue;
553ee519a1fSgjelinek 		if (xmlStrcmp(child->name, DTD_ELEM_PACKAGE) == 0 ||
554ee519a1fSgjelinek 		    xmlStrcmp(child->name, DTD_ELEM_PATCH) == 0) {
555ee519a1fSgjelinek 			next = child->next;
556ee519a1fSgjelinek 			xmlUnlinkNode(child);
557ee519a1fSgjelinek 			xmlFreeNode(child);
558ee519a1fSgjelinek 		}
559ee519a1fSgjelinek 	}
560ee519a1fSgjelinek }
561ee519a1fSgjelinek 
5627c478bd9Sstevel@tonic-gate static int
563108322fbScarlsonj zonecfg_get_handle_impl(const char *zonename, const char *filename,
564108322fbScarlsonj     zone_dochandle_t handle)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	xmlValidCtxtPtr cvp;
5677c478bd9Sstevel@tonic-gate 	struct stat statbuf;
5687c478bd9Sstevel@tonic-gate 	int valid;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (zonename == NULL)
5717c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5729acbbeafSnn35248 
5737c478bd9Sstevel@tonic-gate 	if ((handle->zone_dh_doc = xmlParseFile(filename)) == NULL) {
5747c478bd9Sstevel@tonic-gate 		/* distinguish file not found vs. found but not parsed */
5757c478bd9Sstevel@tonic-gate 		if (stat(filename, &statbuf) == 0)
5767c478bd9Sstevel@tonic-gate 			return (Z_INVALID_DOCUMENT);
5777c478bd9Sstevel@tonic-gate 		return (Z_NO_ZONE);
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 	if ((cvp = xmlNewValidCtxt()) == NULL)
5807c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
5817c478bd9Sstevel@tonic-gate 	cvp->error = zonecfg_error_func;
5827c478bd9Sstevel@tonic-gate 	cvp->warning = zonecfg_error_func;
5837c478bd9Sstevel@tonic-gate 	valid = xmlValidateDocument(cvp, handle->zone_dh_doc);
5847c478bd9Sstevel@tonic-gate 	xmlFreeValidCtxt(cvp);
5857c478bd9Sstevel@tonic-gate 	if (valid == 0)
5867c478bd9Sstevel@tonic-gate 		return (Z_INVALID_DOCUMENT);
587087719fdSdp 
5887c478bd9Sstevel@tonic-gate 	/* delete any comments such as inherited Sun copyright / ident str */
589087719fdSdp 	stripcomments(handle);
5907c478bd9Sstevel@tonic-gate 	return (Z_OK);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate int
594108322fbScarlsonj zonecfg_get_handle(const char *zonename, zone_dochandle_t handle)
5957c478bd9Sstevel@tonic-gate {
5967c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
5977c478bd9Sstevel@tonic-gate 
598108322fbScarlsonj 	if (!config_file_path(zonename, path))
599108322fbScarlsonj 		return (Z_MISC_FS);
600087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
601087719fdSdp 
6027c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate int
606*16ab8c7bSgjelinek zonecfg_get_attach_handle(const char *path, const char *fname,
607*16ab8c7bSgjelinek     const char *zonename, boolean_t preserve_sw, zone_dochandle_t handle)
608ee519a1fSgjelinek {
609ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
610ee519a1fSgjelinek 	int		err;
611ee519a1fSgjelinek 	struct stat	buf;
612ee519a1fSgjelinek 
613ee519a1fSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/root", path) >=
614ee519a1fSgjelinek 	    sizeof (migpath))
615ee519a1fSgjelinek 		return (Z_NOMEM);
616ee519a1fSgjelinek 
617ee519a1fSgjelinek 	if (stat(migpath, &buf) == -1 || !S_ISDIR(buf.st_mode))
618ee519a1fSgjelinek 		return (Z_NO_ZONE);
619ee519a1fSgjelinek 
620*16ab8c7bSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, fname) >=
621ee519a1fSgjelinek 	    sizeof (migpath))
622ee519a1fSgjelinek 		return (Z_NOMEM);
623ee519a1fSgjelinek 
624ee519a1fSgjelinek 	if ((err = zonecfg_get_handle_impl(zonename, migpath, handle)) != Z_OK)
625ee519a1fSgjelinek 		return (err);
626ee519a1fSgjelinek 
627ee519a1fSgjelinek 	if (!preserve_sw)
628ee519a1fSgjelinek 		strip_sw_inv(handle);
629ee519a1fSgjelinek 
630ee519a1fSgjelinek 	handle->zone_dh_newzone = B_TRUE;
631ee519a1fSgjelinek 	if ((err = setrootattr(handle, DTD_ATTR_ZONEPATH, path)) != Z_OK)
632ee519a1fSgjelinek 		return (err);
633ee519a1fSgjelinek 
634ee519a1fSgjelinek 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
635ee519a1fSgjelinek }
636ee519a1fSgjelinek 
637ee519a1fSgjelinek int
638108322fbScarlsonj zonecfg_get_snapshot_handle(const char *zonename, zone_dochandle_t handle)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
6417c478bd9Sstevel@tonic-gate 
642108322fbScarlsonj 	if (!snap_file_path(zonename, path))
643108322fbScarlsonj 		return (Z_MISC_FS);
644087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
6457c478bd9Sstevel@tonic-gate 	return (zonecfg_get_handle_impl(zonename, path, handle));
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate int
649108322fbScarlsonj zonecfg_get_template_handle(const char *template, const char *zonename,
650087719fdSdp     zone_dochandle_t handle)
651087719fdSdp {
652087719fdSdp 	char path[MAXPATHLEN];
653087719fdSdp 	int err;
654087719fdSdp 
655108322fbScarlsonj 	if (!config_file_path(template, path))
656108322fbScarlsonj 		return (Z_MISC_FS);
657087719fdSdp 
658087719fdSdp 	if ((err = zonecfg_get_handle_impl(template, path, handle)) != Z_OK)
659087719fdSdp 		return (err);
660087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
661087719fdSdp 	return (setrootattr(handle, DTD_ATTR_NAME, zonename));
662087719fdSdp }
663087719fdSdp 
6649acbbeafSnn35248 int
6659acbbeafSnn35248 zonecfg_get_xml_handle(const char *path, zone_dochandle_t handle)
6669acbbeafSnn35248 {
6679acbbeafSnn35248 	struct stat buf;
6689acbbeafSnn35248 	int err;
6699acbbeafSnn35248 
6709acbbeafSnn35248 	if (stat(path, &buf) == -1)
6719acbbeafSnn35248 		return (Z_MISC_FS);
6729acbbeafSnn35248 
6739acbbeafSnn35248 	if ((err = zonecfg_get_handle_impl("xml", path, handle)) != Z_OK)
6749acbbeafSnn35248 		return (err);
6759acbbeafSnn35248 	handle->zone_dh_newzone = B_TRUE;
6769acbbeafSnn35248 	return (Z_OK);
6779acbbeafSnn35248 }
6789acbbeafSnn35248 
6798cd327d5Sgjelinek /*
6808cd327d5Sgjelinek  * Initialize two handles from the manifest read on fd.  The rem_handle
6818cd327d5Sgjelinek  * is initialized from the input file, including the sw inventory.  The
6828cd327d5Sgjelinek  * local_handle is initialized with the same zone configuration but with
6838cd327d5Sgjelinek  * no sw inventory.
6848cd327d5Sgjelinek  */
6858cd327d5Sgjelinek int
6868cd327d5Sgjelinek zonecfg_attach_manifest(int fd, zone_dochandle_t local_handle,
6878cd327d5Sgjelinek     zone_dochandle_t rem_handle)
6888cd327d5Sgjelinek {
6898cd327d5Sgjelinek 	xmlValidCtxtPtr cvp;
6908cd327d5Sgjelinek 	int valid;
6918cd327d5Sgjelinek 
6928cd327d5Sgjelinek 	/* load the manifest into the handle for the remote system */
6938cd327d5Sgjelinek 	if ((rem_handle->zone_dh_doc = xmlReadFd(fd, NULL, NULL, 0)) == NULL) {
6948cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
6958cd327d5Sgjelinek 	}
6968cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
6978cd327d5Sgjelinek 		return (Z_NOMEM);
6988cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
6998cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7008cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, rem_handle->zone_dh_doc);
7018cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7028cd327d5Sgjelinek 	if (valid == 0)
7038cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7048cd327d5Sgjelinek 
7058cd327d5Sgjelinek 	/* delete any comments such as inherited Sun copyright / ident str */
7068cd327d5Sgjelinek 	stripcomments(rem_handle);
7078cd327d5Sgjelinek 
7088cd327d5Sgjelinek 	rem_handle->zone_dh_newzone = B_TRUE;
7098cd327d5Sgjelinek 	rem_handle->zone_dh_sw_inv = B_TRUE;
7108cd327d5Sgjelinek 
7118cd327d5Sgjelinek 	/*
7128cd327d5Sgjelinek 	 * Now use the remote system handle to generate a local system handle
7138cd327d5Sgjelinek 	 * with an identical zones configuration but no sw inventory.
7148cd327d5Sgjelinek 	 */
7158cd327d5Sgjelinek 	if ((local_handle->zone_dh_doc = xmlCopyDoc(rem_handle->zone_dh_doc,
7168cd327d5Sgjelinek 	    1)) == NULL) {
7178cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7188cd327d5Sgjelinek 	}
7198cd327d5Sgjelinek 
7208cd327d5Sgjelinek 	/*
7218cd327d5Sgjelinek 	 * We need to re-run xmlValidateDocument on local_handle to properly
7228cd327d5Sgjelinek 	 * update the in-core representation of the configuration.
7238cd327d5Sgjelinek 	 */
7248cd327d5Sgjelinek 	if ((cvp = xmlNewValidCtxt()) == NULL)
7258cd327d5Sgjelinek 		return (Z_NOMEM);
7268cd327d5Sgjelinek 	cvp->error = zonecfg_error_func;
7278cd327d5Sgjelinek 	cvp->warning = zonecfg_error_func;
7288cd327d5Sgjelinek 	valid = xmlValidateDocument(cvp, local_handle->zone_dh_doc);
7298cd327d5Sgjelinek 	xmlFreeValidCtxt(cvp);
7308cd327d5Sgjelinek 	if (valid == 0)
7318cd327d5Sgjelinek 		return (Z_INVALID_DOCUMENT);
7328cd327d5Sgjelinek 
7338cd327d5Sgjelinek 	strip_sw_inv(local_handle);
7348cd327d5Sgjelinek 
7358cd327d5Sgjelinek 	local_handle->zone_dh_newzone = B_TRUE;
7368cd327d5Sgjelinek 	local_handle->zone_dh_sw_inv = B_FALSE;
7378cd327d5Sgjelinek 
7388cd327d5Sgjelinek 	return (Z_OK);
7398cd327d5Sgjelinek }
7408cd327d5Sgjelinek 
741087719fdSdp static boolean_t
742087719fdSdp is_renaming(zone_dochandle_t handle)
743087719fdSdp {
744087719fdSdp 	if (handle->zone_dh_newzone)
745087719fdSdp 		return (B_FALSE);
746087719fdSdp 	if (strlen(handle->zone_dh_delete_name) > 0)
747087719fdSdp 		return (B_TRUE);
748087719fdSdp 	return (B_FALSE);
749087719fdSdp }
750087719fdSdp 
751087719fdSdp static boolean_t
752087719fdSdp is_new(zone_dochandle_t handle)
753087719fdSdp {
754087719fdSdp 	return (handle->zone_dh_newzone || handle->zone_dh_snapshot);
755087719fdSdp }
756087719fdSdp 
757087719fdSdp static boolean_t
758087719fdSdp is_snapshot(zone_dochandle_t handle)
759087719fdSdp {
760087719fdSdp 	return (handle->zone_dh_snapshot);
761087719fdSdp }
762087719fdSdp 
763087719fdSdp /*
764087719fdSdp  * It would be great to be able to use libc's ctype(3c) macros, but we
765087719fdSdp  * can't, as they are locale sensitive, and it would break our limited thread
766087719fdSdp  * safety if this routine had to change the app locale on the fly.
767087719fdSdp  */
768087719fdSdp int
769108322fbScarlsonj zonecfg_validate_zonename(const char *zone)
770087719fdSdp {
771087719fdSdp 	int i;
772087719fdSdp 
773087719fdSdp 	if (strcmp(zone, GLOBAL_ZONENAME) == 0)
774087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
775087719fdSdp 
776087719fdSdp 	if (strlen(zone) >= ZONENAME_MAX)
777087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
778087719fdSdp 
779087719fdSdp 	if (!((zone[0] >= 'a' && zone[0] <= 'z') ||
780087719fdSdp 	    (zone[0] >= 'A' && zone[0] <= 'Z') ||
781087719fdSdp 	    (zone[0] >= '0' && zone[0] <= '9')))
782087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
783087719fdSdp 
784087719fdSdp 	for (i = 1; zone[i] != '\0'; i++) {
785087719fdSdp 		if (!((zone[i] >= 'a' && zone[i] <= 'z') ||
786087719fdSdp 		    (zone[i] >= 'A' && zone[i] <= 'Z') ||
787087719fdSdp 		    (zone[i] >= '0' && zone[i] <= '9') ||
788087719fdSdp 		    (zone[i] == '-') || (zone[i] == '_') || (zone[i] == '.')))
789087719fdSdp 			return (Z_BOGUS_ZONE_NAME);
790087719fdSdp 	}
791087719fdSdp 
792087719fdSdp 	return (Z_OK);
793087719fdSdp }
794087719fdSdp 
795087719fdSdp /*
796087719fdSdp  * Changing the zone name requires us to track both the old and new
797087719fdSdp  * name of the zone until commit time.
798087719fdSdp  */
799087719fdSdp int
8007c478bd9Sstevel@tonic-gate zonecfg_get_name(zone_dochandle_t handle, char *name, size_t namesize)
8017c478bd9Sstevel@tonic-gate {
802a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_NAME, name, namesize));
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
805a1be23daSdp int
806a1be23daSdp zonecfg_set_name(zone_dochandle_t handle, char *name)
807a1be23daSdp {
808087719fdSdp 	zone_state_t state;
809087719fdSdp 	char curname[ZONENAME_MAX], old_delname[ZONENAME_MAX];
810087719fdSdp 	int err;
811087719fdSdp 
812087719fdSdp 	if ((err = getrootattr(handle, DTD_ATTR_NAME, curname,
813087719fdSdp 	    sizeof (curname))) != Z_OK)
814087719fdSdp 		return (err);
815087719fdSdp 
816087719fdSdp 	if (strcmp(name, curname) == 0)
817087719fdSdp 		return (Z_OK);
818087719fdSdp 
819087719fdSdp 	/*
820087719fdSdp 	 * Switching zone names to one beginning with SUNW is not permitted.
821087719fdSdp 	 */
822087719fdSdp 	if (strncmp(name, "SUNW", 4) == 0)
823087719fdSdp 		return (Z_BOGUS_ZONE_NAME);
824087719fdSdp 
825087719fdSdp 	if ((err = zonecfg_validate_zonename(name)) != Z_OK)
826087719fdSdp 		return (err);
827087719fdSdp 
828087719fdSdp 	/*
829087719fdSdp 	 * Setting the name back to the original name (effectively a revert of
830087719fdSdp 	 * the name) is fine.  But if we carry on, we'll falsely identify the
831087719fdSdp 	 * name as "in use," so special case here.
832087719fdSdp 	 */
833087719fdSdp 	if (strcmp(name, handle->zone_dh_delete_name) == 0) {
834087719fdSdp 		err = setrootattr(handle, DTD_ATTR_NAME, name);
835087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
836087719fdSdp 		return (err);
837087719fdSdp 	}
838087719fdSdp 
839087719fdSdp 	/* Check to see if new name chosen is already in use */
840087719fdSdp 	if (zone_get_state(name, &state) != Z_NO_ZONE)
841087719fdSdp 		return (Z_NAME_IN_USE);
842087719fdSdp 
843087719fdSdp 	/*
844087719fdSdp 	 * If this isn't already "new" or in a renaming transition, then
845087719fdSdp 	 * we're initiating a rename here; so stash the "delete name"
846087719fdSdp 	 * (i.e. the name of the zone we'll be removing) for the rename.
847087719fdSdp 	 */
848087719fdSdp 	(void) strlcpy(old_delname, handle->zone_dh_delete_name,
849087719fdSdp 	    sizeof (old_delname));
850087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
851087719fdSdp 		/*
852087719fdSdp 		 * Name change is allowed only when the zone we're altering
853087719fdSdp 		 * is not ready or running.
854087719fdSdp 		 */
855087719fdSdp 		err = zone_get_state(curname, &state);
856087719fdSdp 		if (err == Z_OK) {
857087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
858087719fdSdp 				return (Z_BAD_ZONE_STATE);
859087719fdSdp 		} else if (err != Z_NO_ZONE) {
860087719fdSdp 			return (err);
861087719fdSdp 		}
862087719fdSdp 
863087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, curname,
864087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
865087719fdSdp 		assert(is_renaming(handle));
866087719fdSdp 	} else if (is_renaming(handle)) {
867087719fdSdp 		err = zone_get_state(handle->zone_dh_delete_name, &state);
868087719fdSdp 		if (err == Z_OK) {
869087719fdSdp 			if (state > ZONE_STATE_INSTALLED)
870087719fdSdp 				return (Z_BAD_ZONE_STATE);
871087719fdSdp 		} else if (err != Z_NO_ZONE) {
872087719fdSdp 			return (err);
873087719fdSdp 		}
874087719fdSdp 	}
875087719fdSdp 
876087719fdSdp 	if ((err = setrootattr(handle, DTD_ATTR_NAME, name)) != Z_OK) {
877087719fdSdp 		/*
878087719fdSdp 		 * Restore the deletename to whatever it was at the
879087719fdSdp 		 * top of the routine, since we've had a failure.
880087719fdSdp 		 */
881087719fdSdp 		(void) strlcpy(handle->zone_dh_delete_name, old_delname,
882087719fdSdp 		    sizeof (handle->zone_dh_delete_name));
883087719fdSdp 		return (err);
884087719fdSdp 	}
885087719fdSdp 
886087719fdSdp 	return (Z_OK);
8877c478bd9Sstevel@tonic-gate }
888a1be23daSdp 
889a1be23daSdp int
890a1be23daSdp zonecfg_get_zonepath(zone_dochandle_t handle, char *path, size_t pathsize)
891a1be23daSdp {
892108322fbScarlsonj 	size_t len;
893108322fbScarlsonj 
894108322fbScarlsonj 	if ((len = strlcpy(path, zonecfg_root, pathsize)) >= pathsize)
895108322fbScarlsonj 		return (Z_TOO_BIG);
896108322fbScarlsonj 	return (getrootattr(handle, DTD_ATTR_ZONEPATH, path + len,
897108322fbScarlsonj 	    pathsize - len));
898a1be23daSdp }
899a1be23daSdp 
900a1be23daSdp int
901a1be23daSdp zonecfg_set_zonepath(zone_dochandle_t handle, char *zonepath)
902a1be23daSdp {
903555afedfScarlsonj 	size_t len;
904555afedfScarlsonj 
905555afedfScarlsonj 	/*
906555afedfScarlsonj 	 * The user deals in absolute paths in the running global zone, but the
907555afedfScarlsonj 	 * internal configuration files deal with boot environment relative
908555afedfScarlsonj 	 * paths.  Strip out the alternate root when specified.
909555afedfScarlsonj 	 */
910555afedfScarlsonj 	len = strlen(zonecfg_root);
911555afedfScarlsonj 	if (strncmp(zonepath, zonecfg_root, len) != 0 || zonepath[len] != '/')
912555afedfScarlsonj 		return (Z_BAD_PROPERTY);
913555afedfScarlsonj 	zonepath += len;
914a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_ZONEPATH, zonepath));
915a1be23daSdp }
916a1be23daSdp 
917a1be23daSdp int
9189acbbeafSnn35248 zonecfg_get_brand(zone_dochandle_t handle, char *brand, size_t brandsize)
9199acbbeafSnn35248 {
9209acbbeafSnn35248 	int ret, sz;
9219acbbeafSnn35248 
9229acbbeafSnn35248 	ret = getrootattr(handle, DTD_ATTR_BRAND, brand, brandsize);
9239acbbeafSnn35248 
9249acbbeafSnn35248 	/* If the zone has no brand, it is native. */
9259acbbeafSnn35248 	if (ret == Z_OK && brand[0] == '\0') {
9269acbbeafSnn35248 		sz = strlcpy(brand, NATIVE_BRAND_NAME, brandsize);
9279acbbeafSnn35248 		if (sz >= brandsize)
9289acbbeafSnn35248 			ret = Z_TOO_BIG;
9299acbbeafSnn35248 		else
9309acbbeafSnn35248 			ret = Z_OK;
9319acbbeafSnn35248 	}
9329acbbeafSnn35248 
9339acbbeafSnn35248 	return (ret);
9349acbbeafSnn35248 }
9359acbbeafSnn35248 
9369acbbeafSnn35248 int
9379acbbeafSnn35248 zonecfg_set_brand(zone_dochandle_t handle, char *brand)
9389acbbeafSnn35248 {
9399acbbeafSnn35248 	return (setrootattr(handle, DTD_ATTR_BRAND, brand));
9409acbbeafSnn35248 }
9419acbbeafSnn35248 
9429acbbeafSnn35248 int
943a1be23daSdp zonecfg_get_autoboot(zone_dochandle_t handle, boolean_t *autoboot)
944a1be23daSdp {
945a1be23daSdp 	char autobootstr[DTD_ENTITY_BOOL_LEN];
946a1be23daSdp 	int ret;
947a1be23daSdp 
948a1be23daSdp 	if ((ret = getrootattr(handle, DTD_ATTR_AUTOBOOT, autobootstr,
949a1be23daSdp 	    sizeof (autobootstr))) != Z_OK)
950a1be23daSdp 		return (ret);
951a1be23daSdp 
952a1be23daSdp 	if (strcmp(autobootstr, DTD_ENTITY_TRUE) == 0)
953a1be23daSdp 		*autoboot = B_TRUE;
954a1be23daSdp 	else if (strcmp(autobootstr, DTD_ENTITY_FALSE) == 0)
955a1be23daSdp 		*autoboot = B_FALSE;
956a1be23daSdp 	else
957a1be23daSdp 		ret = Z_BAD_PROPERTY;
958a1be23daSdp 	return (ret);
959a1be23daSdp }
960a1be23daSdp 
961a1be23daSdp int
962a1be23daSdp zonecfg_set_autoboot(zone_dochandle_t handle, boolean_t autoboot)
963a1be23daSdp {
964a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_AUTOBOOT,
965a1be23daSdp 	    autoboot ? DTD_ENTITY_TRUE : DTD_ENTITY_FALSE));
966a1be23daSdp }
967a1be23daSdp 
968a1be23daSdp int
969a1be23daSdp zonecfg_get_pool(zone_dochandle_t handle, char *pool, size_t poolsize)
970a1be23daSdp {
971a1be23daSdp 	return (getrootattr(handle, DTD_ATTR_POOL, pool, poolsize));
972a1be23daSdp }
973a1be23daSdp 
974a1be23daSdp int
975a1be23daSdp zonecfg_set_pool(zone_dochandle_t handle, char *pool)
976a1be23daSdp {
977a1be23daSdp 	return (setrootattr(handle, DTD_ATTR_POOL, pool));
978a1be23daSdp }
979a1be23daSdp 
980ffbafc53Scomay int
981ffbafc53Scomay zonecfg_get_limitpriv(zone_dochandle_t handle, char **limitpriv)
982ffbafc53Scomay {
983ffbafc53Scomay 	return (get_alloc_rootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
984ffbafc53Scomay }
985ffbafc53Scomay 
986ffbafc53Scomay int
9873f2f09c1Sdp zonecfg_set_limitpriv(zone_dochandle_t handle, char *limitpriv)
988ffbafc53Scomay {
9893f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_LIMITPRIV, limitpriv));
9903f2f09c1Sdp }
9913f2f09c1Sdp 
9923f2f09c1Sdp int
9933f2f09c1Sdp zonecfg_get_bootargs(zone_dochandle_t handle, char *bargs, size_t bargssize)
9943f2f09c1Sdp {
9953f2f09c1Sdp 	return (getrootattr(handle, DTD_ATTR_BOOTARGS, bargs, bargssize));
9963f2f09c1Sdp }
9973f2f09c1Sdp 
9983f2f09c1Sdp int
9993f2f09c1Sdp zonecfg_set_bootargs(zone_dochandle_t handle, char *bargs)
10003f2f09c1Sdp {
10013f2f09c1Sdp 	return (setrootattr(handle, DTD_ATTR_BOOTARGS, bargs));
1002ffbafc53Scomay }
1003ffbafc53Scomay 
10040209230bSgjelinek int
10050209230bSgjelinek zonecfg_get_sched_class(zone_dochandle_t handle, char *sched, size_t schedsize)
10060209230bSgjelinek {
10070209230bSgjelinek 	return (getrootattr(handle, DTD_ATTR_SCHED, sched, schedsize));
10080209230bSgjelinek }
10090209230bSgjelinek 
10100209230bSgjelinek int
10110209230bSgjelinek zonecfg_set_sched(zone_dochandle_t handle, char *sched)
10120209230bSgjelinek {
10130209230bSgjelinek 	return (setrootattr(handle, DTD_ATTR_SCHED, sched));
10140209230bSgjelinek }
10150209230bSgjelinek 
1016a1be23daSdp /*
1017a1be23daSdp  * /etc/zones/index caches a vital piece of information which is also
1018a1be23daSdp  * in the <zonename>.xml file: the path to the zone.  This is for performance,
1019a1be23daSdp  * since we need to walk all zonepath's in order to be able to detect conflicts
1020a1be23daSdp  * (see crosscheck_zonepaths() in the zoneadm command).
1021087719fdSdp  *
1022087719fdSdp  * An additional complexity is that when doing a rename, we'd like the entire
1023087719fdSdp  * index update operation (rename, and potential state changes) to be atomic.
1024087719fdSdp  * In general, the operation of this function should succeed or fail as
1025087719fdSdp  * a unit.
1026a1be23daSdp  */
1027a1be23daSdp int
1028a1be23daSdp zonecfg_refresh_index_file(zone_dochandle_t handle)
1029a1be23daSdp {
1030a1be23daSdp 	char name[ZONENAME_MAX], zonepath[MAXPATHLEN];
1031a1be23daSdp 	struct zoneent ze;
1032a1be23daSdp 	int err;
1033087719fdSdp 	int opcode;
1034087719fdSdp 	char *zn;
1035087719fdSdp 
1036087719fdSdp 	bzero(&ze, sizeof (ze));
1037087719fdSdp 	ze.zone_state = -1;	/* Preserve existing state in index */
1038a1be23daSdp 
1039a1be23daSdp 	if ((err = zonecfg_get_name(handle, name, sizeof (name))) != Z_OK)
1040a1be23daSdp 		return (err);
1041087719fdSdp 	(void) strlcpy(ze.zone_name, name, sizeof (ze.zone_name));
1042087719fdSdp 
1043a1be23daSdp 	if ((err = zonecfg_get_zonepath(handle, zonepath,
1044a1be23daSdp 	    sizeof (zonepath))) != Z_OK)
1045a1be23daSdp 		return (err);
1046555afedfScarlsonj 	(void) strlcpy(ze.zone_path, zonepath + strlen(zonecfg_root),
1047555afedfScarlsonj 	    sizeof (ze.zone_path));
1048087719fdSdp 
1049087719fdSdp 	if (is_renaming(handle)) {
1050087719fdSdp 		opcode = PZE_MODIFY;
1051087719fdSdp 		(void) strlcpy(ze.zone_name, handle->zone_dh_delete_name,
1052087719fdSdp 		    sizeof (ze.zone_name));
1053087719fdSdp 		(void) strlcpy(ze.zone_newname, name, sizeof (ze.zone_newname));
1054087719fdSdp 	} else if (is_new(handle)) {
1055087719fdSdp 		FILE *cookie;
1056087719fdSdp 		/*
1057087719fdSdp 		 * Be tolerant of the zone already existing in the index file,
1058087719fdSdp 		 * since we might be forcibly overwriting an existing
1059087719fdSdp 		 * configuration with a new one (for example 'create -F'
1060087719fdSdp 		 * in zonecfg).
1061087719fdSdp 		 */
1062087719fdSdp 		opcode = PZE_ADD;
1063087719fdSdp 		cookie = setzoneent();
1064087719fdSdp 		while ((zn = getzoneent(cookie)) != NULL) {
1065087719fdSdp 			if (strcmp(zn, name) == 0) {
1066087719fdSdp 				opcode = PZE_MODIFY;
1067087719fdSdp 				free(zn);
1068087719fdSdp 				break;
1069087719fdSdp 			}
1070087719fdSdp 			free(zn);
1071087719fdSdp 		}
1072087719fdSdp 		endzoneent(cookie);
1073087719fdSdp 		ze.zone_state = ZONE_STATE_CONFIGURED;
1074087719fdSdp 	} else {
1075087719fdSdp 		opcode = PZE_MODIFY;
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
1078087719fdSdp 	if ((err = putzoneent(&ze, opcode)) != Z_OK)
1079087719fdSdp 		return (err);
1080087719fdSdp 
1081087719fdSdp 	return (Z_OK);
1082087719fdSdp }
1083087719fdSdp 
1084087719fdSdp /*
1085087719fdSdp  * The goal of this routine is to cause the index file update and the
1086087719fdSdp  * document save to happen as an atomic operation.  We do the document
1087087719fdSdp  * first, saving a backup copy using a hard link; if that succeeds, we go
1088087719fdSdp  * on to the index.  If that fails, we roll the document back into place.
1089087719fdSdp  *
1090087719fdSdp  * Strategy:
1091087719fdSdp  *
1092087719fdSdp  * New zone 'foo' configuration:
1093087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1094087719fdSdp  * 	Write XML to tmpfile
1095087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1096087719fdSdp  * 	Add entry to index file
1097087719fdSdp  * 	If it fails, delete foo.xml, leaving nothing behind.
1098087719fdSdp  *
1099087719fdSdp  * Save existing zone 'foo':
1100087719fdSdp  * 	Make backup of foo.xml -> .backup
1101087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1102087719fdSdp  * 	Write XML to tmpfile
1103087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> foo.xml)
1104087719fdSdp  * 	Modify index file as needed
1105087719fdSdp  * 	If it fails, recover from .backup -> foo.xml
1106087719fdSdp  *
1107087719fdSdp  * Rename 'foo' to 'bar':
1108087719fdSdp  * 	Create tmpfile (zonecfg.xxxxxx)
1109087719fdSdp  * 	Write XML to tmpfile
1110087719fdSdp  * 	Rename tmpfile to xmlfile (zonecfg.xxxxxx -> bar.xml)
1111087719fdSdp  * 	Add entry for 'bar' to index file, Remove entry for 'foo' (refresh)
1112087719fdSdp  * 	If it fails, delete bar.xml; foo.xml is left behind.
1113087719fdSdp  */
11147c478bd9Sstevel@tonic-gate static int
11157c478bd9Sstevel@tonic-gate zonecfg_save_impl(zone_dochandle_t handle, char *filename)
11167c478bd9Sstevel@tonic-gate {
11177c478bd9Sstevel@tonic-gate 	char tmpfile[MAXPATHLEN];
1118087719fdSdp 	char bakdir[MAXPATHLEN], bakbase[MAXPATHLEN], bakfile[MAXPATHLEN];
11199acbbeafSnn35248 	int tmpfd, err, valid;
11207c478bd9Sstevel@tonic-gate 	xmlValidCtxt cvp = { NULL };
1121087719fdSdp 	boolean_t backup;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	(void) strlcpy(tmpfile, filename, sizeof (tmpfile));
11247c478bd9Sstevel@tonic-gate 	(void) dirname(tmpfile);
11257c478bd9Sstevel@tonic-gate 	(void) strlcat(tmpfile, _PATH_TMPFILE, sizeof (tmpfile));
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	tmpfd = mkstemp(tmpfile);
11287c478bd9Sstevel@tonic-gate 	if (tmpfd == -1) {
11297c478bd9Sstevel@tonic-gate 		(void) unlink(tmpfile);
11307c478bd9Sstevel@tonic-gate 		return (Z_TEMP_FILE);
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	(void) close(tmpfd);
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	cvp.error = zonecfg_error_func;
11357c478bd9Sstevel@tonic-gate 	cvp.warning = zonecfg_error_func;
11367c478bd9Sstevel@tonic-gate 
1137087719fdSdp 	/*
11389acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
11399acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
11409acbbeafSnn35248 	 * assert() that the doc is valid.
1141087719fdSdp 	 */
11429acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
11439acbbeafSnn35248 	assert(valid != 0);
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	if (xmlSaveFormatFile(tmpfile, handle->zone_dh_doc, 1) <= 0)
11467c478bd9Sstevel@tonic-gate 		goto err;
1147087719fdSdp 
11487c478bd9Sstevel@tonic-gate 	(void) chmod(tmpfile, 0644);
11497c478bd9Sstevel@tonic-gate 
1150087719fdSdp 	/*
1151087719fdSdp 	 * In the event we are doing a standard save, hard link a copy of the
1152087719fdSdp 	 * original file in .backup.<pid>.filename so we can restore it if
1153087719fdSdp 	 * something goes wrong.
1154087719fdSdp 	 */
1155087719fdSdp 	if (!is_new(handle) && !is_renaming(handle)) {
1156087719fdSdp 		backup = B_TRUE;
1157087719fdSdp 
1158087719fdSdp 		(void) strlcpy(bakdir, filename, sizeof (bakdir));
1159087719fdSdp 		(void) strlcpy(bakbase, filename, sizeof (bakbase));
1160087719fdSdp 		(void) snprintf(bakfile, sizeof (bakfile), "%s/.backup.%d.%s",
1161087719fdSdp 		    dirname(bakdir), getpid(), basename(bakbase));
1162087719fdSdp 
1163087719fdSdp 		if (link(filename, bakfile) == -1) {
1164087719fdSdp 			err = errno;
11657c478bd9Sstevel@tonic-gate 			(void) unlink(tmpfile);
11667c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
11677c478bd9Sstevel@tonic-gate 				return (Z_ACCES);
11687c478bd9Sstevel@tonic-gate 			return (Z_MISC_FS);
11697c478bd9Sstevel@tonic-gate 		}
1170087719fdSdp 	}
1171a1be23daSdp 
1172087719fdSdp 	/*
1173087719fdSdp 	 * Move the new document over top of the old.
1174087719fdSdp 	 * i.e.:   zonecfg.XXXXXX  ->  myzone.xml
1175087719fdSdp 	 */
1176087719fdSdp 	if (rename(tmpfile, filename) == -1) {
1177087719fdSdp 		err = errno;
1178087719fdSdp 		(void) unlink(tmpfile);
1179087719fdSdp 		if (backup)
1180087719fdSdp 			(void) unlink(bakfile);
1181087719fdSdp 		if (err == EACCES)
1182087719fdSdp 			return (Z_ACCES);
1183087719fdSdp 		return (Z_MISC_FS);
1184087719fdSdp 	}
1185087719fdSdp 
1186087719fdSdp 	/*
1187087719fdSdp 	 * If this is a snapshot, we're done-- don't add an index entry.
1188087719fdSdp 	 */
1189087719fdSdp 	if (is_snapshot(handle))
1190087719fdSdp 		return (Z_OK);
1191087719fdSdp 
1192087719fdSdp 	/* now update the index file to reflect whatever we just did */
1193087719fdSdp 	if ((err = zonecfg_refresh_index_file(handle)) != Z_OK) {
1194087719fdSdp 		if (backup) {
1195087719fdSdp 			/*
1196087719fdSdp 			 * Try to restore from our backup.
1197087719fdSdp 			 */
1198087719fdSdp 			(void) unlink(filename);
1199087719fdSdp 			(void) rename(bakfile, filename);
1200087719fdSdp 		} else {
1201087719fdSdp 			/*
1202087719fdSdp 			 * Either the zone is new, in which case we can delete
1203087719fdSdp 			 * new.xml, or we're doing a rename, so ditto.
1204087719fdSdp 			 */
1205087719fdSdp 			assert(is_new(handle) || is_renaming(handle));
1206087719fdSdp 			(void) unlink(filename);
1207087719fdSdp 		}
1208087719fdSdp 		return (Z_UPDATING_INDEX);
1209087719fdSdp 	}
1210087719fdSdp 
1211087719fdSdp 	if (backup)
1212087719fdSdp 		(void) unlink(bakfile);
1213087719fdSdp 
1214087719fdSdp 	return (Z_OK);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate err:
12177c478bd9Sstevel@tonic-gate 	(void) unlink(tmpfile);
12187c478bd9Sstevel@tonic-gate 	return (Z_SAVING_FILE);
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate int
12227c478bd9Sstevel@tonic-gate zonecfg_save(zone_dochandle_t handle)
12237c478bd9Sstevel@tonic-gate {
1224087719fdSdp 	char zname[ZONENAME_MAX], path[MAXPATHLEN];
1225087719fdSdp 	char delpath[MAXPATHLEN];
1226087719fdSdp 	int err = Z_SAVING_FILE;
12277c478bd9Sstevel@tonic-gate 
1228087719fdSdp 	if (zonecfg_check_handle(handle) != Z_OK)
1229087719fdSdp 		return (Z_BAD_HANDLE);
1230087719fdSdp 
1231087719fdSdp 	/*
1232ee519a1fSgjelinek 	 * We don't support saving snapshots or a tree containing a sw
1233ee519a1fSgjelinek 	 * inventory at this time.
1234087719fdSdp 	 */
1235ee519a1fSgjelinek 	if (handle->zone_dh_snapshot || handle->zone_dh_sw_inv)
1236087719fdSdp 		return (Z_INVAL);
1237087719fdSdp 
1238087719fdSdp 	if ((err = zonecfg_get_name(handle, zname, sizeof (zname))) != Z_OK)
12397c478bd9Sstevel@tonic-gate 		return (err);
1240087719fdSdp 
1241108322fbScarlsonj 	if (!config_file_path(zname, path))
1242108322fbScarlsonj 		return (Z_MISC_FS);
1243087719fdSdp 
1244087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS "
1245087719fdSdp 	    "FILE.  Use zonecfg(1M) instead.\n");
1246087719fdSdp 
1247087719fdSdp 	err = zonecfg_save_impl(handle, path);
1248087719fdSdp 
1249087719fdSdp 	stripcomments(handle);
1250087719fdSdp 
1251087719fdSdp 	if (err != Z_OK)
1252087719fdSdp 		return (err);
1253087719fdSdp 
1254087719fdSdp 	handle->zone_dh_newzone = B_FALSE;
1255087719fdSdp 
1256087719fdSdp 	if (is_renaming(handle)) {
1257108322fbScarlsonj 		if (config_file_path(handle->zone_dh_delete_name, delpath))
1258087719fdSdp 			(void) unlink(delpath);
1259087719fdSdp 		handle->zone_dh_delete_name[0] = '\0';
1260087719fdSdp 	}
1261087719fdSdp 
1262087719fdSdp 	return (Z_OK);
12637c478bd9Sstevel@tonic-gate }
12647c478bd9Sstevel@tonic-gate 
1265ee519a1fSgjelinek int
12669acbbeafSnn35248 zonecfg_verify_save(zone_dochandle_t handle, char *filename)
12679acbbeafSnn35248 {
12689acbbeafSnn35248 	int valid;
12699acbbeafSnn35248 
12709acbbeafSnn35248 	xmlValidCtxt cvp = { NULL };
12719acbbeafSnn35248 
12729acbbeafSnn35248 	if (zonecfg_check_handle(handle) != Z_OK)
12739acbbeafSnn35248 		return (Z_BAD_HANDLE);
12749acbbeafSnn35248 
12759acbbeafSnn35248 	cvp.error = zonecfg_error_func;
12769acbbeafSnn35248 	cvp.warning = zonecfg_error_func;
12779acbbeafSnn35248 
12789acbbeafSnn35248 	/*
12799acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
12809acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
12819acbbeafSnn35248 	 * assert() that the doc is valid.
12829acbbeafSnn35248 	 */
12839acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
12849acbbeafSnn35248 	assert(valid != 0);
12859acbbeafSnn35248 
12869acbbeafSnn35248 	if (xmlSaveFormatFile(filename, handle->zone_dh_doc, 1) <= 0)
12879acbbeafSnn35248 		return (Z_SAVING_FILE);
12889acbbeafSnn35248 
12899acbbeafSnn35248 	return (Z_OK);
12909acbbeafSnn35248 }
12919acbbeafSnn35248 
12929acbbeafSnn35248 int
12938cd327d5Sgjelinek zonecfg_detach_save(zone_dochandle_t handle, uint_t flags)
1294ee519a1fSgjelinek {
1295ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1296ee519a1fSgjelinek 	char path[MAXPATHLEN];
1297ee519a1fSgjelinek 	char migpath[MAXPATHLEN];
1298ee519a1fSgjelinek 	xmlValidCtxt cvp = { NULL };
1299ee519a1fSgjelinek 	int err = Z_SAVING_FILE;
13009acbbeafSnn35248 	int valid;
1301ee519a1fSgjelinek 
1302ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1303ee519a1fSgjelinek 		return (Z_BAD_HANDLE);
1304ee519a1fSgjelinek 
13058cd327d5Sgjelinek 	if (flags & ZONE_DRY_RUN) {
13068cd327d5Sgjelinek 		(void) strlcpy(migpath, "-", sizeof (migpath));
13078cd327d5Sgjelinek 	} else {
13088cd327d5Sgjelinek 		if ((err = zonecfg_get_name(handle, zname, sizeof (zname)))
13098cd327d5Sgjelinek 		    != Z_OK)
1310ee519a1fSgjelinek 			return (err);
1311ee519a1fSgjelinek 
13128cd327d5Sgjelinek 		if ((err = zone_get_zonepath(zname, path, sizeof (path)))
13138cd327d5Sgjelinek 		    != Z_OK)
1314ee519a1fSgjelinek 			return (err);
1315ee519a1fSgjelinek 
1316*16ab8c7bSgjelinek 		if (snprintf(migpath, sizeof (migpath), "%s/%s", path,
1317*16ab8c7bSgjelinek 		    ZONE_DETACHED) >= sizeof (migpath))
1318ee519a1fSgjelinek 			return (Z_NOMEM);
13198cd327d5Sgjelinek 	}
1320ee519a1fSgjelinek 
1321ee519a1fSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
1322ee519a1fSgjelinek 		return (err);
1323ee519a1fSgjelinek 
1324ee519a1fSgjelinek 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1325ee519a1fSgjelinek 	    "Use zonecfg(1M) and zoneadm(1M) attach.\n");
1326ee519a1fSgjelinek 
1327ee519a1fSgjelinek 	cvp.error = zonecfg_error_func;
1328ee519a1fSgjelinek 	cvp.warning = zonecfg_error_func;
1329ee519a1fSgjelinek 
1330ee519a1fSgjelinek 	/*
13319acbbeafSnn35248 	 * We do a final validation of the document.  Since the library has
13329acbbeafSnn35248 	 * malfunctioned if it fails to validate, we follow-up with an
13339acbbeafSnn35248 	 * assert() that the doc is valid.
1334ee519a1fSgjelinek 	 */
13359acbbeafSnn35248 	valid = xmlValidateDocument(&cvp, handle->zone_dh_doc);
13369acbbeafSnn35248 	assert(valid != 0);
1337ee519a1fSgjelinek 
1338ee519a1fSgjelinek 	if (xmlSaveFormatFile(migpath, handle->zone_dh_doc, 1) <= 0)
1339ee519a1fSgjelinek 		return (Z_SAVING_FILE);
1340ee519a1fSgjelinek 
13418cd327d5Sgjelinek 	if (!(flags & ZONE_DRY_RUN))
1342ee519a1fSgjelinek 		(void) chmod(migpath, 0644);
1343ee519a1fSgjelinek 
1344ee519a1fSgjelinek 	stripcomments(handle);
1345ee519a1fSgjelinek 
1346ee519a1fSgjelinek 	handle->zone_dh_newzone = B_FALSE;
1347ee519a1fSgjelinek 
1348ee519a1fSgjelinek 	return (Z_OK);
1349ee519a1fSgjelinek }
1350ee519a1fSgjelinek 
1351ee519a1fSgjelinek boolean_t
1352ee519a1fSgjelinek zonecfg_detached(const char *path)
1353ee519a1fSgjelinek {
1354ee519a1fSgjelinek 	char		migpath[MAXPATHLEN];
1355ee519a1fSgjelinek 	struct stat	buf;
1356ee519a1fSgjelinek 
1357*16ab8c7bSgjelinek 	if (snprintf(migpath, sizeof (migpath), "%s/%s", path, ZONE_DETACHED) >=
1358ee519a1fSgjelinek 	    sizeof (migpath))
1359ee519a1fSgjelinek 		return (B_FALSE);
1360ee519a1fSgjelinek 
1361ee519a1fSgjelinek 	if (stat(migpath, &buf) != -1)
1362ee519a1fSgjelinek 		return (B_TRUE);
1363ee519a1fSgjelinek 
1364ee519a1fSgjelinek 	return (B_FALSE);
1365ee519a1fSgjelinek }
1366ee519a1fSgjelinek 
1367ee519a1fSgjelinek void
1368ee519a1fSgjelinek zonecfg_rm_detached(zone_dochandle_t handle, boolean_t forced)
1369ee519a1fSgjelinek {
1370ee519a1fSgjelinek 	char zname[ZONENAME_MAX];
1371ee519a1fSgjelinek 	char path[MAXPATHLEN];
1372ee519a1fSgjelinek 	char detached[MAXPATHLEN];
1373ee519a1fSgjelinek 	char attached[MAXPATHLEN];
1374ee519a1fSgjelinek 
1375ee519a1fSgjelinek 	if (zonecfg_check_handle(handle) != Z_OK)
1376ee519a1fSgjelinek 		return;
1377ee519a1fSgjelinek 
1378ee519a1fSgjelinek 	if (zonecfg_get_name(handle, zname, sizeof (zname)) != Z_OK)
1379ee519a1fSgjelinek 		return;
1380ee519a1fSgjelinek 
1381ee519a1fSgjelinek 	if (zone_get_zonepath(zname, path, sizeof (path)) != Z_OK)
1382ee519a1fSgjelinek 		return;
1383ee519a1fSgjelinek 
1384*16ab8c7bSgjelinek 	(void) snprintf(detached, sizeof (detached), "%s/%s", path,
1385*16ab8c7bSgjelinek 	    ZONE_DETACHED);
1386ee519a1fSgjelinek 	(void) snprintf(attached, sizeof (attached), "%s/%s", path,
1387ee519a1fSgjelinek 	    ATTACH_FORCED);
1388ee519a1fSgjelinek 
1389ee519a1fSgjelinek 	if (forced) {
1390ee519a1fSgjelinek 		(void) rename(detached, attached);
1391ee519a1fSgjelinek 	} else {
1392ee519a1fSgjelinek 		(void) unlink(attached);
1393ee519a1fSgjelinek 		(void) unlink(detached);
1394ee519a1fSgjelinek 	}
1395ee519a1fSgjelinek }
1396ee519a1fSgjelinek 
13977c478bd9Sstevel@tonic-gate /*
13987c478bd9Sstevel@tonic-gate  * Special case: if access(2) fails with ENOENT, then try again using
13997c478bd9Sstevel@tonic-gate  * ZONE_CONFIG_ROOT instead of config_file_path(zonename).  This is how we
14007c478bd9Sstevel@tonic-gate  * work around the case of a config file which has not been created yet:
14017c478bd9Sstevel@tonic-gate  * the user will need access to the directory so use that as a heuristic.
14027c478bd9Sstevel@tonic-gate  */
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate int
14057c478bd9Sstevel@tonic-gate zonecfg_access(const char *zonename, int amode)
14067c478bd9Sstevel@tonic-gate {
14077c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
14087c478bd9Sstevel@tonic-gate 
1409108322fbScarlsonj 	if (!config_file_path(zonename, path))
1410108322fbScarlsonj 		return (Z_INVAL);
14117c478bd9Sstevel@tonic-gate 	if (access(path, amode) == 0)
14127c478bd9Sstevel@tonic-gate 		return (Z_OK);
1413108322fbScarlsonj 	if (errno == ENOENT) {
1414108322fbScarlsonj 		if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1415108322fbScarlsonj 		    ZONE_CONFIG_ROOT) >= sizeof (path))
1416108322fbScarlsonj 			return (Z_INVAL);
1417108322fbScarlsonj 		if (access(path, amode) == 0)
14187c478bd9Sstevel@tonic-gate 			return (Z_OK);
1419108322fbScarlsonj 	}
14207c478bd9Sstevel@tonic-gate 	if (errno == EACCES)
14217c478bd9Sstevel@tonic-gate 		return (Z_ACCES);
14227c478bd9Sstevel@tonic-gate 	if (errno == EINVAL)
14237c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
14247c478bd9Sstevel@tonic-gate 	return (Z_MISC_FS);
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate int
1428108322fbScarlsonj zonecfg_create_snapshot(const char *zonename)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
14317c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], zonepath[MAXPATHLEN], rpath[MAXPATHLEN];
14327c478bd9Sstevel@tonic-gate 	int error = Z_OK, res;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
14357c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 
1438087719fdSdp 	handle->zone_dh_newzone = B_TRUE;
1439087719fdSdp 	handle->zone_dh_snapshot = B_TRUE;
1440087719fdSdp 
14417c478bd9Sstevel@tonic-gate 	if ((error = zonecfg_get_handle(zonename, handle)) != Z_OK)
14427c478bd9Sstevel@tonic-gate 		goto out;
14437c478bd9Sstevel@tonic-gate 	if ((error = operation_prep(handle)) != Z_OK)
14447c478bd9Sstevel@tonic-gate 		goto out;
14457c478bd9Sstevel@tonic-gate 	error = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath));
14467c478bd9Sstevel@tonic-gate 	if (error != Z_OK)
14477c478bd9Sstevel@tonic-gate 		goto out;
14487c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(zonepath, rpath, sizeof (rpath))) == -1) {
14497c478bd9Sstevel@tonic-gate 		error = Z_RESOLVED_PATH;
14507c478bd9Sstevel@tonic-gate 		goto out;
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 	/*
14537c478bd9Sstevel@tonic-gate 	 * If the resolved path is not the same as the original path, then
14547c478bd9Sstevel@tonic-gate 	 * save the resolved path in the snapshot, thus preventing any
14557c478bd9Sstevel@tonic-gate 	 * potential problems down the line when zoneadmd goes to unmount
14567c478bd9Sstevel@tonic-gate 	 * file systems and depends on initial string matches with resolved
14577c478bd9Sstevel@tonic-gate 	 * paths.
14587c478bd9Sstevel@tonic-gate 	 */
14597c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
14607c478bd9Sstevel@tonic-gate 	if (strcmp(zonepath, rpath) != 0) {
14617c478bd9Sstevel@tonic-gate 		if ((error = zonecfg_set_zonepath(handle, rpath)) != Z_OK)
14627c478bd9Sstevel@tonic-gate 			goto out;
14637c478bd9Sstevel@tonic-gate 	}
1464108322fbScarlsonj 	if (snprintf(path, sizeof (path), "%s%s", zonecfg_root,
1465108322fbScarlsonj 	    ZONE_SNAPSHOT_ROOT) >= sizeof (path)) {
1466108322fbScarlsonj 		error = Z_MISC_FS;
1467108322fbScarlsonj 		goto out;
1468108322fbScarlsonj 	}
1469108322fbScarlsonj 	if ((mkdir(path, S_IRWXU) == -1) && (errno != EEXIST)) {
14707c478bd9Sstevel@tonic-gate 		error = Z_MISC_FS;
14717c478bd9Sstevel@tonic-gate 		goto out;
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 
1474108322fbScarlsonj 	if (!snap_file_path(zonename, path)) {
1475108322fbScarlsonj 		error = Z_MISC_FS;
1476108322fbScarlsonj 		goto out;
1477108322fbScarlsonj 	}
1478087719fdSdp 
1479087719fdSdp 	addcomment(handle, "\n    DO NOT EDIT THIS FILE.  "
1480087719fdSdp 	    "It is a snapshot of running zone state.\n");
1481087719fdSdp 
14827c478bd9Sstevel@tonic-gate 	error = zonecfg_save_impl(handle, path);
14837c478bd9Sstevel@tonic-gate 
1484087719fdSdp 	stripcomments(handle);
1485087719fdSdp 
14867c478bd9Sstevel@tonic-gate out:
14877c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
14887c478bd9Sstevel@tonic-gate 	return (error);
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
1491f4b3ec61Sdh155122 int
1492f4b3ec61Sdh155122 zonecfg_get_iptype(zone_dochandle_t handle, zone_iptype_t *iptypep)
1493f4b3ec61Sdh155122 {
1494f4b3ec61Sdh155122 	char property[10]; /* 10 is big enough for "shared"/"exclusive" */
1495f4b3ec61Sdh155122 	int err;
1496f4b3ec61Sdh155122 
1497f4b3ec61Sdh155122 	err = getrootattr(handle, DTD_ATTR_IPTYPE, property, sizeof (property));
1498f4b3ec61Sdh155122 	if (err == Z_BAD_PROPERTY) {
1499f4b3ec61Sdh155122 		/* Return default value */
1500f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1501f4b3ec61Sdh155122 		return (Z_OK);
1502f4b3ec61Sdh155122 	} else if (err != Z_OK) {
1503f4b3ec61Sdh155122 		return (err);
1504f4b3ec61Sdh155122 	}
1505f4b3ec61Sdh155122 
1506f4b3ec61Sdh155122 	if (strlen(property) == 0 ||
1507f4b3ec61Sdh155122 	    strcmp(property, "shared") == 0)
1508f4b3ec61Sdh155122 		*iptypep = ZS_SHARED;
1509f4b3ec61Sdh155122 	else if (strcmp(property, "exclusive") == 0)
1510f4b3ec61Sdh155122 		*iptypep = ZS_EXCLUSIVE;
1511f4b3ec61Sdh155122 	else
1512f4b3ec61Sdh155122 		return (Z_INVAL);
1513f4b3ec61Sdh155122 
1514f4b3ec61Sdh155122 	return (Z_OK);
1515f4b3ec61Sdh155122 }
1516f4b3ec61Sdh155122 
1517f4b3ec61Sdh155122 int
1518f4b3ec61Sdh155122 zonecfg_set_iptype(zone_dochandle_t handle, zone_iptype_t iptype)
1519f4b3ec61Sdh155122 {
1520f4b3ec61Sdh155122 	xmlNodePtr cur;
1521f4b3ec61Sdh155122 
1522f4b3ec61Sdh155122 	if (handle == NULL)
1523f4b3ec61Sdh155122 		return (Z_INVAL);
1524f4b3ec61Sdh155122 
1525f4b3ec61Sdh155122 	cur = xmlDocGetRootElement(handle->zone_dh_doc);
1526f4b3ec61Sdh155122 	if (cur == NULL) {
1527f4b3ec61Sdh155122 		return (Z_EMPTY_DOCUMENT);
1528f4b3ec61Sdh155122 	}
1529f4b3ec61Sdh155122 
1530f4b3ec61Sdh155122 	if (xmlStrcmp(cur->name, DTD_ELEM_ZONE) != 0) {
1531f4b3ec61Sdh155122 		return (Z_WRONG_DOC_TYPE);
1532f4b3ec61Sdh155122 	}
1533f4b3ec61Sdh155122 	switch (iptype) {
1534f4b3ec61Sdh155122 	case ZS_SHARED:
1535f4b3ec61Sdh155122 		/*
1536f4b3ec61Sdh155122 		 * Since "shared" is the default, we don't write it to the
1537f4b3ec61Sdh155122 		 * configuration file, so that it's easier to migrate those
1538f4b3ec61Sdh155122 		 * zones elsewhere, eg., to systems which are not IP-Instances
1539f4b3ec61Sdh155122 		 * aware.
1540f4b3ec61Sdh155122 		 * xmlUnsetProp only fails when the attribute doesn't exist,
1541f4b3ec61Sdh155122 		 * which we don't care.
1542f4b3ec61Sdh155122 		 */
1543f4b3ec61Sdh155122 		(void) xmlUnsetProp(cur, DTD_ATTR_IPTYPE);
1544f4b3ec61Sdh155122 		break;
1545f4b3ec61Sdh155122 	case ZS_EXCLUSIVE:
1546f4b3ec61Sdh155122 		if (xmlSetProp(cur, DTD_ATTR_IPTYPE,
1547f4b3ec61Sdh155122 		    (const xmlChar *) "exclusive") == NULL)
1548f4b3ec61Sdh155122 			return (Z_INVAL);
1549f4b3ec61Sdh155122 		break;
1550f4b3ec61Sdh155122 	}
1551f4b3ec61Sdh155122 	return (Z_OK);
1552f4b3ec61Sdh155122 }
1553f4b3ec61Sdh155122 
15547c478bd9Sstevel@tonic-gate static int
1555a1be23daSdp newprop(xmlNodePtr node, const xmlChar *attrname, char *src)
15567c478bd9Sstevel@tonic-gate {
15577c478bd9Sstevel@tonic-gate 	xmlAttrPtr newattr;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	newattr = xmlNewProp(node, attrname, (xmlChar *)src);
15607c478bd9Sstevel@tonic-gate 	if (newattr == NULL) {
15617c478bd9Sstevel@tonic-gate 		xmlUnlinkNode(node);
15627c478bd9Sstevel@tonic-gate 		xmlFreeNode(node);
15637c478bd9Sstevel@tonic-gate 		return (Z_BAD_PROPERTY);
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 	return (Z_OK);
15667c478bd9Sstevel@tonic-gate }
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate static int
15697c478bd9Sstevel@tonic-gate zonecfg_add_filesystem_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
15707c478bd9Sstevel@tonic-gate {
15717c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, options_node;
15727c478bd9Sstevel@tonic-gate 	zone_fsopt_t *ptr;
15737c478bd9Sstevel@tonic-gate 	int err;
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_FS, NULL);
1576a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_SPECIAL,
15777c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_special)) != Z_OK)
15787c478bd9Sstevel@tonic-gate 		return (err);
15797c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_raw[0] != '\0' &&
1580a1be23daSdp 	    (err = newprop(newnode, DTD_ATTR_RAW, tabptr->zone_fs_raw)) != Z_OK)
15817c478bd9Sstevel@tonic-gate 		return (err);
1582a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
15837c478bd9Sstevel@tonic-gate 		return (err);
1584a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_TYPE,
15857c478bd9Sstevel@tonic-gate 	    tabptr->zone_fs_type)) != Z_OK)
15867c478bd9Sstevel@tonic-gate 		return (err);
15877c478bd9Sstevel@tonic-gate 	if (tabptr->zone_fs_options != NULL) {
15887c478bd9Sstevel@tonic-gate 		for (ptr = tabptr->zone_fs_options; ptr != NULL;
15897c478bd9Sstevel@tonic-gate 		    ptr = ptr->zone_fsopt_next) {
15907c478bd9Sstevel@tonic-gate 			options_node = xmlNewTextChild(newnode, NULL,
15917c478bd9Sstevel@tonic-gate 			    DTD_ELEM_FSOPTION, NULL);
1592a1be23daSdp 			if ((err = newprop(options_node, DTD_ATTR_NAME,
15937c478bd9Sstevel@tonic-gate 			    ptr->zone_fsopt_opt)) != Z_OK)
15947c478bd9Sstevel@tonic-gate 				return (err);
15957c478bd9Sstevel@tonic-gate 		}
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate 	return (Z_OK);
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate int
16017c478bd9Sstevel@tonic-gate zonecfg_add_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
16027c478bd9Sstevel@tonic-gate {
16037c478bd9Sstevel@tonic-gate 	int err;
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16067c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16097c478bd9Sstevel@tonic-gate 		return (err);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, tabptr)) != Z_OK)
16127c478bd9Sstevel@tonic-gate 		return (err);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	return (Z_OK);
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate static int
16187c478bd9Sstevel@tonic-gate zonecfg_add_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
16197c478bd9Sstevel@tonic-gate {
16207c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
16217c478bd9Sstevel@tonic-gate 	int err;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_IPD, NULL);
1624a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_DIR, tabptr->zone_fs_dir)) != Z_OK)
16257c478bd9Sstevel@tonic-gate 		return (err);
16267c478bd9Sstevel@tonic-gate 	return (Z_OK);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate int
16307c478bd9Sstevel@tonic-gate zonecfg_add_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
16317c478bd9Sstevel@tonic-gate {
16327c478bd9Sstevel@tonic-gate 	int err;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
16357c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
16387c478bd9Sstevel@tonic-gate 		return (err);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, tabptr)) != Z_OK)
16417c478bd9Sstevel@tonic-gate 		return (err);
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	return (Z_OK);
16447c478bd9Sstevel@tonic-gate }
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate int
16477c478bd9Sstevel@tonic-gate zonecfg_add_fs_option(struct zone_fstab *tabptr, char *option)
16487c478bd9Sstevel@tonic-gate {
16497c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *old, *new;
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16527c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_fsopt_next)
16537c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
16547c478bd9Sstevel@tonic-gate 	new = (zone_fsopt_t *)malloc(sizeof (zone_fsopt_t));
16557c478bd9Sstevel@tonic-gate 	if (new == NULL)
16567c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
16577c478bd9Sstevel@tonic-gate 	(void) strlcpy(new->zone_fsopt_opt, option,
16587c478bd9Sstevel@tonic-gate 	    sizeof (new->zone_fsopt_opt));
16597c478bd9Sstevel@tonic-gate 	new->zone_fsopt_next = NULL;
16607c478bd9Sstevel@tonic-gate 	if (last == NULL)
16617c478bd9Sstevel@tonic-gate 		tabptr->zone_fs_options = new;
16627c478bd9Sstevel@tonic-gate 	else
16637c478bd9Sstevel@tonic-gate 		last->zone_fsopt_next = new;
16647c478bd9Sstevel@tonic-gate 	return (Z_OK);
16657c478bd9Sstevel@tonic-gate }
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate int
16687c478bd9Sstevel@tonic-gate zonecfg_remove_fs_option(struct zone_fstab *tabptr, char *option)
16697c478bd9Sstevel@tonic-gate {
16707c478bd9Sstevel@tonic-gate 	zone_fsopt_t *last, *this, *next;
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	last = tabptr->zone_fs_options;
16737c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_fsopt_next) {
16747c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_fsopt_opt, option) == 0) {
16757c478bd9Sstevel@tonic-gate 			next = this->zone_fsopt_next;
16767c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_fs_options)
16777c478bd9Sstevel@tonic-gate 				tabptr->zone_fs_options = next;
16787c478bd9Sstevel@tonic-gate 			else
16797c478bd9Sstevel@tonic-gate 				last->zone_fsopt_next = next;
16807c478bd9Sstevel@tonic-gate 			free(this);
16817c478bd9Sstevel@tonic-gate 			return (Z_OK);
16827c478bd9Sstevel@tonic-gate 		} else
16837c478bd9Sstevel@tonic-gate 			last = this;
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
16867c478bd9Sstevel@tonic-gate }
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate void
16897c478bd9Sstevel@tonic-gate zonecfg_free_fs_option_list(zone_fsopt_t *list)
16907c478bd9Sstevel@tonic-gate {
16917c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this, *next;
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	for (this = list; this != NULL; this = next) {
16947c478bd9Sstevel@tonic-gate 		next = this->zone_fsopt_next;
16957c478bd9Sstevel@tonic-gate 		free(this);
16967c478bd9Sstevel@tonic-gate 	}
16977c478bd9Sstevel@tonic-gate }
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate void
17007c478bd9Sstevel@tonic-gate zonecfg_free_rctl_value_list(struct zone_rctlvaltab *valtab)
17017c478bd9Sstevel@tonic-gate {
17027c478bd9Sstevel@tonic-gate 	if (valtab == NULL)
17037c478bd9Sstevel@tonic-gate 		return;
17047c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(valtab->zone_rctlval_next);
17057c478bd9Sstevel@tonic-gate 	free(valtab);
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate static boolean_t
17097c478bd9Sstevel@tonic-gate match_prop(xmlNodePtr cur, const xmlChar *attr, char *user_prop)
17107c478bd9Sstevel@tonic-gate {
17117c478bd9Sstevel@tonic-gate 	xmlChar *gotten_prop;
17127c478bd9Sstevel@tonic-gate 	int prop_result;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	gotten_prop = xmlGetProp(cur, attr);
17157c478bd9Sstevel@tonic-gate 	if (gotten_prop == NULL)	/* shouldn't happen */
17167c478bd9Sstevel@tonic-gate 		return (B_FALSE);
17177c478bd9Sstevel@tonic-gate 	prop_result = xmlStrcmp(gotten_prop, (const xmlChar *) user_prop);
17187c478bd9Sstevel@tonic-gate 	xmlFree(gotten_prop);
17197c478bd9Sstevel@tonic-gate 	return ((prop_result == 0));
17207c478bd9Sstevel@tonic-gate }
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate static int
17237c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem_core(zone_dochandle_t handle,
17247c478bd9Sstevel@tonic-gate     struct zone_fstab *tabptr)
17257c478bd9Sstevel@tonic-gate {
17267c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17277c478bd9Sstevel@tonic-gate 	boolean_t dir_match, spec_match, raw_match, type_match;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17307c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
17317c478bd9Sstevel@tonic-gate 			continue;
17327c478bd9Sstevel@tonic-gate 		dir_match = match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir);
17337c478bd9Sstevel@tonic-gate 		spec_match = match_prop(cur, DTD_ATTR_SPECIAL,
17347c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_special);
17357c478bd9Sstevel@tonic-gate 		raw_match = match_prop(cur, DTD_ATTR_RAW,
17367c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_raw);
17377c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
17387c478bd9Sstevel@tonic-gate 		    tabptr->zone_fs_type);
17397c478bd9Sstevel@tonic-gate 		if (dir_match && spec_match && raw_match && type_match) {
17407c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17417c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17427c478bd9Sstevel@tonic-gate 			return (Z_OK);
17437c478bd9Sstevel@tonic-gate 		}
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
17467c478bd9Sstevel@tonic-gate }
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate int
17497c478bd9Sstevel@tonic-gate zonecfg_delete_filesystem(zone_dochandle_t handle, struct zone_fstab *tabptr)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	int err;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
17547c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17577c478bd9Sstevel@tonic-gate 		return (err);
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, tabptr)) != Z_OK)
17607c478bd9Sstevel@tonic-gate 		return (err);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	return (Z_OK);
17637c478bd9Sstevel@tonic-gate }
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate int
17667c478bd9Sstevel@tonic-gate zonecfg_modify_filesystem(
17677c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
17687c478bd9Sstevel@tonic-gate 	struct zone_fstab *oldtabptr,
17697c478bd9Sstevel@tonic-gate 	struct zone_fstab *newtabptr)
17707c478bd9Sstevel@tonic-gate {
17717c478bd9Sstevel@tonic-gate 	int err;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
17747c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
17777c478bd9Sstevel@tonic-gate 		return (err);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_filesystem_core(handle, oldtabptr)) != Z_OK)
17807c478bd9Sstevel@tonic-gate 		return (err);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_filesystem_core(handle, newtabptr)) != Z_OK)
17837c478bd9Sstevel@tonic-gate 		return (err);
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	return (Z_OK);
17867c478bd9Sstevel@tonic-gate }
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate static int
17897c478bd9Sstevel@tonic-gate zonecfg_delete_ipd_core(zone_dochandle_t handle, struct zone_fstab *tabptr)
17907c478bd9Sstevel@tonic-gate {
17917c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
17947c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
17957c478bd9Sstevel@tonic-gate 			continue;
17967c478bd9Sstevel@tonic-gate 		if (match_prop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir)) {
17977c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
17987c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
17997c478bd9Sstevel@tonic-gate 			return (Z_OK);
18007c478bd9Sstevel@tonic-gate 		}
18017c478bd9Sstevel@tonic-gate 	}
18027c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
18037c478bd9Sstevel@tonic-gate }
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate int
18067c478bd9Sstevel@tonic-gate zonecfg_delete_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
18077c478bd9Sstevel@tonic-gate {
18087c478bd9Sstevel@tonic-gate 	int err;
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18117c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18147c478bd9Sstevel@tonic-gate 		return (err);
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, tabptr)) != Z_OK)
18177c478bd9Sstevel@tonic-gate 		return (err);
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	return (Z_OK);
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate int
18237c478bd9Sstevel@tonic-gate zonecfg_modify_ipd(zone_dochandle_t handle, struct zone_fstab *oldtabptr,
18247c478bd9Sstevel@tonic-gate     struct zone_fstab *newtabptr)
18257c478bd9Sstevel@tonic-gate {
18267c478bd9Sstevel@tonic-gate 	int err;
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
18297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18327c478bd9Sstevel@tonic-gate 		return (err);
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_ipd_core(handle, oldtabptr)) != Z_OK)
18357c478bd9Sstevel@tonic-gate 		return (err);
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_ipd_core(handle, newtabptr)) != Z_OK)
18387c478bd9Sstevel@tonic-gate 		return (err);
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	return (Z_OK);
18417c478bd9Sstevel@tonic-gate }
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate int
18447c478bd9Sstevel@tonic-gate zonecfg_lookup_filesystem(
18457c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
18467c478bd9Sstevel@tonic-gate 	struct zone_fstab *tabptr)
18477c478bd9Sstevel@tonic-gate {
18487c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options, firstmatch;
18497c478bd9Sstevel@tonic-gate 	int err;
18507c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN], special[MAXPATHLEN], raw[MAXPATHLEN];
18517c478bd9Sstevel@tonic-gate 	char type[FSTYPSZ];
18527c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
18557c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
18587c478bd9Sstevel@tonic-gate 		return (err);
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 	/*
18617c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
18627c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
18637c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
18647c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
18657c478bd9Sstevel@tonic-gate 	 */
18667c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
18677c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
18687c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
18697c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_FS))
18707c478bd9Sstevel@tonic-gate 			continue;
18717c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
18727c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
18737c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
18747c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
18757c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
18767c478bd9Sstevel@tonic-gate 					firstmatch = cur;
18777c478bd9Sstevel@tonic-gate 				else
18787c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
18797c478bd9Sstevel@tonic-gate 			}
18807c478bd9Sstevel@tonic-gate 		}
18817c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_special) > 0) {
18827c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_SPECIAL, special,
18837c478bd9Sstevel@tonic-gate 			    sizeof (special)) == Z_OK)) {
18847c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_special,
18857c478bd9Sstevel@tonic-gate 				    special) == 0) {
18867c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
18877c478bd9Sstevel@tonic-gate 						firstmatch = cur;
18887c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
18897c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
18907c478bd9Sstevel@tonic-gate 				} else {
18917c478bd9Sstevel@tonic-gate 					/*
18927c478bd9Sstevel@tonic-gate 					 * If another property matched but this
18937c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
18947c478bd9Sstevel@tonic-gate 					 */
18957c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
18967c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
18977c478bd9Sstevel@tonic-gate 				}
18987c478bd9Sstevel@tonic-gate 			}
18997c478bd9Sstevel@tonic-gate 		}
19007c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_raw) > 0) {
19017c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_RAW, raw,
19027c478bd9Sstevel@tonic-gate 			    sizeof (raw)) == Z_OK)) {
19037c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_raw, raw) == 0) {
19047c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19057c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19067c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19077c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19087c478bd9Sstevel@tonic-gate 				} else {
19097c478bd9Sstevel@tonic-gate 					/*
19107c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19117c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19127c478bd9Sstevel@tonic-gate 					 */
19137c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19147c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19157c478bd9Sstevel@tonic-gate 				}
19167c478bd9Sstevel@tonic-gate 			}
19177c478bd9Sstevel@tonic-gate 		}
19187c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_type) > 0) {
19197c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
19207c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
19217c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_fs_type, type) == 0) {
19227c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
19237c478bd9Sstevel@tonic-gate 						firstmatch = cur;
19247c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
19257c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
19267c478bd9Sstevel@tonic-gate 				} else {
19277c478bd9Sstevel@tonic-gate 					/*
19287c478bd9Sstevel@tonic-gate 					 * If another property matched but this
19297c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
19307c478bd9Sstevel@tonic-gate 					 */
19317c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
19327c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
19337c478bd9Sstevel@tonic-gate 				}
19347c478bd9Sstevel@tonic-gate 			}
19357c478bd9Sstevel@tonic-gate 		}
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
19397c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	cur = firstmatch;
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
19447c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
19457c478bd9Sstevel@tonic-gate 		return (err);
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
19487c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK)
19497c478bd9Sstevel@tonic-gate 		return (err);
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
19527c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK)
19537c478bd9Sstevel@tonic-gate 		return (err);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
19567c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK)
19577c478bd9Sstevel@tonic-gate 		return (err);
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	/* options are optional */
19607c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
19617c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
19627c478bd9Sstevel@tonic-gate 	    options = options->next) {
19637c478bd9Sstevel@tonic-gate 		if ((fetchprop(options, DTD_ATTR_NAME, options_str,
19647c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK))
19657c478bd9Sstevel@tonic-gate 			break;
19667c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
19677c478bd9Sstevel@tonic-gate 			break;
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 	return (Z_OK);
19707c478bd9Sstevel@tonic-gate }
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate int
19737c478bd9Sstevel@tonic-gate zonecfg_lookup_ipd(zone_dochandle_t handle, struct zone_fstab *tabptr)
19747c478bd9Sstevel@tonic-gate {
19757c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, match;
19767c478bd9Sstevel@tonic-gate 	int err;
19777c478bd9Sstevel@tonic-gate 	char dirname[MAXPATHLEN];
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
19807c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
19837c478bd9Sstevel@tonic-gate 		return (err);
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	/*
19867c478bd9Sstevel@tonic-gate 	 * General algorithm:
19877c478bd9Sstevel@tonic-gate 	 * Walk the list of children looking for matches on any properties
19887c478bd9Sstevel@tonic-gate 	 * specified in the fstab parameter.  If more than one resource
19897c478bd9Sstevel@tonic-gate 	 * matches, we return Z_INSUFFICIENT_SPEC; if none match, we return
19907c478bd9Sstevel@tonic-gate 	 * Z_NO_RESOURCE_ID.
19917c478bd9Sstevel@tonic-gate 	 */
19927c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
19937c478bd9Sstevel@tonic-gate 	match = NULL;
19947c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
19957c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_IPD))
19967c478bd9Sstevel@tonic-gate 			continue;
19977c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_fs_dir) > 0) {
19987c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_DIR, dirname,
19997c478bd9Sstevel@tonic-gate 			    sizeof (dirname)) == Z_OK) &&
20007c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_fs_dir, dirname) == 0)) {
20017c478bd9Sstevel@tonic-gate 				if (match == NULL)
20027c478bd9Sstevel@tonic-gate 					match = cur;
20037c478bd9Sstevel@tonic-gate 				else
20047c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
20057c478bd9Sstevel@tonic-gate 			}
20067c478bd9Sstevel@tonic-gate 		}
20077c478bd9Sstevel@tonic-gate 	}
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	if (match == NULL)
20107c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	cur = match;
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
20157c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK)
20167c478bd9Sstevel@tonic-gate 		return (err);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	return (Z_OK);
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate /*
20227c478bd9Sstevel@tonic-gate  * Compare two IP addresses in string form.  Allow for the possibility that
20237c478bd9Sstevel@tonic-gate  * one might have "/<prefix-length>" at the end: allow a match on just the
20247c478bd9Sstevel@tonic-gate  * IP address (or host name) part.
20257c478bd9Sstevel@tonic-gate  */
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate boolean_t
20287c478bd9Sstevel@tonic-gate zonecfg_same_net_address(char *a1, char *a2)
20297c478bd9Sstevel@tonic-gate {
20307c478bd9Sstevel@tonic-gate 	char *slashp, *slashp1, *slashp2;
20317c478bd9Sstevel@tonic-gate 	int result;
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	if (strcmp(a1, a2) == 0)
20347c478bd9Sstevel@tonic-gate 		return (B_TRUE);
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	/*
20377c478bd9Sstevel@tonic-gate 	 * If neither has a slash or both do, they need to match to be
20387c478bd9Sstevel@tonic-gate 	 * considered the same, but they did not match above, so fail.
20397c478bd9Sstevel@tonic-gate 	 */
20407c478bd9Sstevel@tonic-gate 	slashp1 = strchr(a1, '/');
20417c478bd9Sstevel@tonic-gate 	slashp2 = strchr(a2, '/');
20427c478bd9Sstevel@tonic-gate 	if ((slashp1 == NULL && slashp2 == NULL) ||
20437c478bd9Sstevel@tonic-gate 	    (slashp1 != NULL && slashp2 != NULL))
20447c478bd9Sstevel@tonic-gate 		return (B_FALSE);
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	/*
20477c478bd9Sstevel@tonic-gate 	 * Only one had a slash: pick that one, zero out the slash, compare
20487c478bd9Sstevel@tonic-gate 	 * the "address only" strings, restore the slash, and return the
20497c478bd9Sstevel@tonic-gate 	 * result of the comparison.
20507c478bd9Sstevel@tonic-gate 	 */
20517c478bd9Sstevel@tonic-gate 	slashp = (slashp1 == NULL) ? slashp2 : slashp1;
20527c478bd9Sstevel@tonic-gate 	*slashp = '\0';
20537c478bd9Sstevel@tonic-gate 	result = strcmp(a1, a2);
20547c478bd9Sstevel@tonic-gate 	*slashp = '/';
20557c478bd9Sstevel@tonic-gate 	return ((result == 0));
20567c478bd9Sstevel@tonic-gate }
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate int
20597c478bd9Sstevel@tonic-gate zonecfg_valid_net_address(char *address, struct lifreq *lifr)
20607c478bd9Sstevel@tonic-gate {
20617c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin4;
20627c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
20637c478bd9Sstevel@tonic-gate 	struct addrinfo hints, *result;
20647c478bd9Sstevel@tonic-gate 	char *slashp = strchr(address, '/');
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	bzero(lifr, sizeof (struct lifreq));
20677c478bd9Sstevel@tonic-gate 	sin4 = (struct sockaddr_in *)&lifr->lifr_addr;
20687c478bd9Sstevel@tonic-gate 	sin6 = (struct sockaddr_in6 *)&lifr->lifr_addr;
20697c478bd9Sstevel@tonic-gate 	if (slashp != NULL)
20707c478bd9Sstevel@tonic-gate 		*slashp = '\0';
20717c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, address, &sin4->sin_addr) == 1) {
20727c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
20737c478bd9Sstevel@tonic-gate 	} else if (inet_pton(AF_INET6, address, &sin6->sin6_addr) == 1) {
20747c478bd9Sstevel@tonic-gate 		if (slashp == NULL)
20757c478bd9Sstevel@tonic-gate 			return (Z_IPV6_ADDR_PREFIX_LEN);
20767c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
20777c478bd9Sstevel@tonic-gate 	} else {
20787c478bd9Sstevel@tonic-gate 		/* "address" may be a host name */
20797c478bd9Sstevel@tonic-gate 		(void) memset(&hints, 0, sizeof (hints));
20807c478bd9Sstevel@tonic-gate 		hints.ai_family = PF_INET;
20817c478bd9Sstevel@tonic-gate 		if (getaddrinfo(address, NULL, &hints, &result) != 0)
20827c478bd9Sstevel@tonic-gate 			return (Z_BOGUS_ADDRESS);
20837c478bd9Sstevel@tonic-gate 		sin4->sin_family = result->ai_family;
2084a1be23daSdp 
20857c478bd9Sstevel@tonic-gate 		(void) memcpy(&sin4->sin_addr,
20867c478bd9Sstevel@tonic-gate 		    /* LINTED E_BAD_PTR_CAST_ALIGN */
20877c478bd9Sstevel@tonic-gate 		    &((struct sockaddr_in *)result->ai_addr)->sin_addr,
20887c478bd9Sstevel@tonic-gate 		    sizeof (struct in_addr));
2089a1be23daSdp 
20907c478bd9Sstevel@tonic-gate 		freeaddrinfo(result);
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 	return (Z_OK);
20937c478bd9Sstevel@tonic-gate }
20947c478bd9Sstevel@tonic-gate 
2095f4b3ec61Sdh155122 boolean_t
2096f4b3ec61Sdh155122 zonecfg_ifname_exists(sa_family_t af, char *ifname)
2097f4b3ec61Sdh155122 {
2098f4b3ec61Sdh155122 	struct lifreq lifr;
2099f4b3ec61Sdh155122 	int so;
2100f4b3ec61Sdh155122 	int save_errno;
2101f4b3ec61Sdh155122 
2102f4b3ec61Sdh155122 	(void) memset(&lifr, 0, sizeof (lifr));
2103f4b3ec61Sdh155122 	(void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2104f4b3ec61Sdh155122 	lifr.lifr_addr.ss_family = af;
2105f4b3ec61Sdh155122 	if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
2106f4b3ec61Sdh155122 		/* Odd - can't tell if the ifname exists */
2107f4b3ec61Sdh155122 		return (B_FALSE);
2108f4b3ec61Sdh155122 	}
2109f4b3ec61Sdh155122 	if (ioctl(so, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2110f4b3ec61Sdh155122 		save_errno = errno;
2111f4b3ec61Sdh155122 		(void) close(so);
2112f4b3ec61Sdh155122 		errno = save_errno;
2113f4b3ec61Sdh155122 		return (B_FALSE);
2114f4b3ec61Sdh155122 	}
2115f4b3ec61Sdh155122 	(void) close(so);
2116f4b3ec61Sdh155122 	return (B_TRUE);
2117f4b3ec61Sdh155122 }
2118f4b3ec61Sdh155122 
21197c478bd9Sstevel@tonic-gate int
21207c478bd9Sstevel@tonic-gate zonecfg_lookup_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21217c478bd9Sstevel@tonic-gate {
21227c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
21237c478bd9Sstevel@tonic-gate 	int err;
21247c478bd9Sstevel@tonic-gate 	char address[INET6_ADDRSTRLEN], physical[LIFNAMSIZ];
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
21277c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
21307c478bd9Sstevel@tonic-gate 		return (err);
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
21337c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
21347c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
21357c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
21367c478bd9Sstevel@tonic-gate 			continue;
21377c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_physical) > 0) {
21387c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_PHYSICAL, physical,
21397c478bd9Sstevel@tonic-gate 			    sizeof (physical)) == Z_OK) &&
21407c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_nwif_physical,
21417c478bd9Sstevel@tonic-gate 			    physical) == 0)) {
21427c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
21437c478bd9Sstevel@tonic-gate 					firstmatch = cur;
21447c478bd9Sstevel@tonic-gate 				else
21457c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
21467c478bd9Sstevel@tonic-gate 			}
21477c478bd9Sstevel@tonic-gate 		}
21487c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_nwif_address) > 0) {
21497c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_ADDRESS, address,
21507c478bd9Sstevel@tonic-gate 			    sizeof (address)) == Z_OK)) {
21517c478bd9Sstevel@tonic-gate 				if (zonecfg_same_net_address(
21527c478bd9Sstevel@tonic-gate 				    tabptr->zone_nwif_address, address)) {
21537c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
21547c478bd9Sstevel@tonic-gate 						firstmatch = cur;
21557c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
21567c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
21577c478bd9Sstevel@tonic-gate 				} else {
21587c478bd9Sstevel@tonic-gate 					/*
21597c478bd9Sstevel@tonic-gate 					 * If another property matched but this
21607c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
21617c478bd9Sstevel@tonic-gate 					 */
21627c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
21637c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
21647c478bd9Sstevel@tonic-gate 				}
21657c478bd9Sstevel@tonic-gate 			}
21667c478bd9Sstevel@tonic-gate 		}
21677c478bd9Sstevel@tonic-gate 	}
21687c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
21697c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 	cur = firstmatch;
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
21747c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK)
21757c478bd9Sstevel@tonic-gate 		return (err);
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
21787c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK)
21797c478bd9Sstevel@tonic-gate 		return (err);
21807c478bd9Sstevel@tonic-gate 
2181de860bd9Sgfaden 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
2182de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter,
2183de860bd9Sgfaden 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK)
2184de860bd9Sgfaden 		return (err);
2185de860bd9Sgfaden 
21867c478bd9Sstevel@tonic-gate 	return (Z_OK);
21877c478bd9Sstevel@tonic-gate }
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate static int
21907c478bd9Sstevel@tonic-gate zonecfg_add_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
21917c478bd9Sstevel@tonic-gate {
21927c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
21937c478bd9Sstevel@tonic-gate 	int err;
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_NET, NULL);
2196a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_ADDRESS,
21977c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_address)) != Z_OK)
21987c478bd9Sstevel@tonic-gate 		return (err);
2199a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_PHYSICAL,
22007c478bd9Sstevel@tonic-gate 	    tabptr->zone_nwif_physical)) != Z_OK)
22017c478bd9Sstevel@tonic-gate 		return (err);
220244ce818bSjparcel 	/*
220344ce818bSjparcel 	 * Do not add this property when it is not set, for backwards
220444ce818bSjparcel 	 * compatibility and because it is optional.
220544ce818bSjparcel 	 */
220644ce818bSjparcel 	if ((strlen(tabptr->zone_nwif_defrouter) > 0) &&
220744ce818bSjparcel 	    ((err = newprop(newnode, DTD_ATTR_DEFROUTER,
220844ce818bSjparcel 	    tabptr->zone_nwif_defrouter)) != Z_OK))
2209de860bd9Sgfaden 		return (err);
22107c478bd9Sstevel@tonic-gate 	return (Z_OK);
22117c478bd9Sstevel@tonic-gate }
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate int
22147c478bd9Sstevel@tonic-gate zonecfg_add_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22157c478bd9Sstevel@tonic-gate {
22167c478bd9Sstevel@tonic-gate 	int err;
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22197c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22227c478bd9Sstevel@tonic-gate 		return (err);
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, tabptr)) != Z_OK)
22257c478bd9Sstevel@tonic-gate 		return (err);
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	return (Z_OK);
22287c478bd9Sstevel@tonic-gate }
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate static int
22317c478bd9Sstevel@tonic-gate zonecfg_delete_nwif_core(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22327c478bd9Sstevel@tonic-gate {
22337c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
22347c478bd9Sstevel@tonic-gate 	boolean_t addr_match, phys_match;
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
22377c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_NET))
22387c478bd9Sstevel@tonic-gate 			continue;
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 		addr_match = match_prop(cur, DTD_ATTR_ADDRESS,
22417c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_address);
22427c478bd9Sstevel@tonic-gate 		phys_match = match_prop(cur, DTD_ATTR_PHYSICAL,
22437c478bd9Sstevel@tonic-gate 		    tabptr->zone_nwif_physical);
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 		if (addr_match && phys_match) {
22467c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
22477c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
22487c478bd9Sstevel@tonic-gate 			return (Z_OK);
22497c478bd9Sstevel@tonic-gate 		}
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
22527c478bd9Sstevel@tonic-gate }
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate int
22557c478bd9Sstevel@tonic-gate zonecfg_delete_nwif(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
22567c478bd9Sstevel@tonic-gate {
22577c478bd9Sstevel@tonic-gate 	int err;
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
22607c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22637c478bd9Sstevel@tonic-gate 		return (err);
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, tabptr)) != Z_OK)
22667c478bd9Sstevel@tonic-gate 		return (err);
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 	return (Z_OK);
22697c478bd9Sstevel@tonic-gate }
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate int
22727c478bd9Sstevel@tonic-gate zonecfg_modify_nwif(
22737c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
22747c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *oldtabptr,
22757c478bd9Sstevel@tonic-gate 	struct zone_nwiftab *newtabptr)
22767c478bd9Sstevel@tonic-gate {
22777c478bd9Sstevel@tonic-gate 	int err;
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
22807c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
22837c478bd9Sstevel@tonic-gate 		return (err);
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_nwif_core(handle, oldtabptr)) != Z_OK)
22867c478bd9Sstevel@tonic-gate 		return (err);
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_nwif_core(handle, newtabptr)) != Z_OK)
22897c478bd9Sstevel@tonic-gate 		return (err);
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate 	return (Z_OK);
22927c478bd9Sstevel@tonic-gate }
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate int
22957c478bd9Sstevel@tonic-gate zonecfg_lookup_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
22967c478bd9Sstevel@tonic-gate {
22977c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
22987c478bd9Sstevel@tonic-gate 	int err;
22997c478bd9Sstevel@tonic-gate 	char match[MAXPATHLEN];
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23027c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23057c478bd9Sstevel@tonic-gate 		return (err);
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
23087c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
23097c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23107c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
23117c478bd9Sstevel@tonic-gate 			continue;
23127c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_dev_match) == 0)
23137c478bd9Sstevel@tonic-gate 			continue;
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_MATCH, match,
23167c478bd9Sstevel@tonic-gate 		    sizeof (match)) == Z_OK)) {
23177c478bd9Sstevel@tonic-gate 			if (strcmp(tabptr->zone_dev_match,
23187c478bd9Sstevel@tonic-gate 			    match) == 0) {
23197c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
23207c478bd9Sstevel@tonic-gate 					firstmatch = cur;
23217c478bd9Sstevel@tonic-gate 				else if (firstmatch != cur)
23227c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
23237c478bd9Sstevel@tonic-gate 			} else {
23247c478bd9Sstevel@tonic-gate 				/*
23257c478bd9Sstevel@tonic-gate 				 * If another property matched but this
23267c478bd9Sstevel@tonic-gate 				 * one doesn't then reset firstmatch.
23277c478bd9Sstevel@tonic-gate 				 */
23287c478bd9Sstevel@tonic-gate 				if (firstmatch == cur)
23297c478bd9Sstevel@tonic-gate 					firstmatch = NULL;
23307c478bd9Sstevel@tonic-gate 			}
23317c478bd9Sstevel@tonic-gate 		}
23327c478bd9Sstevel@tonic-gate 	}
23337c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
23347c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	cur = firstmatch;
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
23397c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK)
23407c478bd9Sstevel@tonic-gate 		return (err);
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	return (Z_OK);
23437c478bd9Sstevel@tonic-gate }
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate static int
23467c478bd9Sstevel@tonic-gate zonecfg_add_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
23477c478bd9Sstevel@tonic-gate {
23487c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
23497c478bd9Sstevel@tonic-gate 	int err;
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEVICE, NULL);
23527c478bd9Sstevel@tonic-gate 
2353a1be23daSdp 	if ((err = newprop(newnode, DTD_ATTR_MATCH,
23547c478bd9Sstevel@tonic-gate 	    tabptr->zone_dev_match)) != Z_OK)
23557c478bd9Sstevel@tonic-gate 		return (err);
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	return (Z_OK);
23587c478bd9Sstevel@tonic-gate }
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate int
23617c478bd9Sstevel@tonic-gate zonecfg_add_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
23627c478bd9Sstevel@tonic-gate {
23637c478bd9Sstevel@tonic-gate 	int err;
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
23667c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
23697c478bd9Sstevel@tonic-gate 		return (err);
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, tabptr)) != Z_OK)
23727c478bd9Sstevel@tonic-gate 		return (err);
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 	return (Z_OK);
23757c478bd9Sstevel@tonic-gate }
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate static int
23787c478bd9Sstevel@tonic-gate zonecfg_delete_dev_core(zone_dochandle_t handle, struct zone_devtab *tabptr)
23797c478bd9Sstevel@tonic-gate {
2380facf4a8dSllai1 	xmlNodePtr cur = handle->zone_dh_cur;
23817c478bd9Sstevel@tonic-gate 	int match_match;
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
23847c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
23857c478bd9Sstevel@tonic-gate 			continue;
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 		match_match = match_prop(cur, DTD_ATTR_MATCH,
23887c478bd9Sstevel@tonic-gate 		    tabptr->zone_dev_match);
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 		if (match_match) {
23917c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
23927c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
23937c478bd9Sstevel@tonic-gate 			return (Z_OK);
23947c478bd9Sstevel@tonic-gate 		}
23957c478bd9Sstevel@tonic-gate 	}
23967c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
23977c478bd9Sstevel@tonic-gate }
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate int
24007c478bd9Sstevel@tonic-gate zonecfg_delete_dev(zone_dochandle_t handle, struct zone_devtab *tabptr)
24017c478bd9Sstevel@tonic-gate {
24027c478bd9Sstevel@tonic-gate 	int err;
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
24057c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24087c478bd9Sstevel@tonic-gate 		return (err);
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, tabptr)) != Z_OK)
24117c478bd9Sstevel@tonic-gate 		return (err);
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 	return (Z_OK);
24147c478bd9Sstevel@tonic-gate }
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate int
24177c478bd9Sstevel@tonic-gate zonecfg_modify_dev(
24187c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
24197c478bd9Sstevel@tonic-gate 	struct zone_devtab *oldtabptr,
24207c478bd9Sstevel@tonic-gate 	struct zone_devtab *newtabptr)
24217c478bd9Sstevel@tonic-gate {
24227c478bd9Sstevel@tonic-gate 	int err;
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
24257c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
24287c478bd9Sstevel@tonic-gate 		return (err);
24297c478bd9Sstevel@tonic-gate 
24307c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_dev_core(handle, oldtabptr)) != Z_OK)
24317c478bd9Sstevel@tonic-gate 		return (err);
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_dev_core(handle, newtabptr)) != Z_OK)
24347c478bd9Sstevel@tonic-gate 		return (err);
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	return (Z_OK);
24377c478bd9Sstevel@tonic-gate }
24387c478bd9Sstevel@tonic-gate 
2439ff17c8bfSgjelinek /* Lock to serialize all devwalks */
2440ee519a1fSgjelinek static pthread_mutex_t zonecfg_devwalk_lock = PTHREAD_MUTEX_INITIALIZER;
2441ee519a1fSgjelinek /*
2442ff17c8bfSgjelinek  * Global variables used to pass data from zonecfg_dev_manifest to the nftw
2443ee519a1fSgjelinek  * call-back (zonecfg_devwalk_cb).  g_devwalk_data is really the void*
2444ff17c8bfSgjelinek  * parameter and g_devwalk_cb is really the *cb parameter from
2445ff17c8bfSgjelinek  * zonecfg_dev_manifest.
2446ee519a1fSgjelinek  */
2447ff17c8bfSgjelinek typedef struct __g_devwalk_data *g_devwalk_data_t;
2448ff17c8bfSgjelinek static g_devwalk_data_t g_devwalk_data;
2449ee519a1fSgjelinek static int (*g_devwalk_cb)(const char *, uid_t, gid_t, mode_t, const char *,
2450ee519a1fSgjelinek     void *);
2451ee519a1fSgjelinek static size_t g_devwalk_skip_prefix;
2452ee519a1fSgjelinek 
2453ee519a1fSgjelinek /*
2454ff17c8bfSgjelinek  * zonecfg_dev_manifest call-back function used during detach to generate the
2455ff17c8bfSgjelinek  * dev info in the manifest.
2456ff17c8bfSgjelinek  */
2457ff17c8bfSgjelinek static int
2458ff17c8bfSgjelinek get_detach_dev_entry(const char *name, uid_t uid, gid_t gid, mode_t mode,
2459ff17c8bfSgjelinek     const char *acl, void *hdl)
2460ff17c8bfSgjelinek {
2461ff17c8bfSgjelinek 	zone_dochandle_t handle = (zone_dochandle_t)hdl;
2462ff17c8bfSgjelinek 	xmlNodePtr newnode;
2463ff17c8bfSgjelinek 	xmlNodePtr cur;
2464ff17c8bfSgjelinek 	int err;
2465ff17c8bfSgjelinek 	char buf[128];
2466ff17c8bfSgjelinek 
2467ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
2468ff17c8bfSgjelinek 		return (err);
2469ff17c8bfSgjelinek 
2470ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
2471ff17c8bfSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DEV_PERM, NULL);
2472ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, (char *)name)) != Z_OK)
2473ff17c8bfSgjelinek 		return (err);
2474ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", uid);
2475ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_UID, buf)) != Z_OK)
2476ff17c8bfSgjelinek 		return (err);
2477ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%lu", gid);
2478ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_GID, buf)) != Z_OK)
2479ff17c8bfSgjelinek 		return (err);
2480ff17c8bfSgjelinek 	(void) snprintf(buf, sizeof (buf), "%o", mode);
2481ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_MODE, buf)) != Z_OK)
2482ff17c8bfSgjelinek 		return (err);
2483ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_ACL, (char *)acl)) != Z_OK)
2484ff17c8bfSgjelinek 		return (err);
2485ff17c8bfSgjelinek 	return (Z_OK);
2486ff17c8bfSgjelinek }
2487ff17c8bfSgjelinek 
2488ff17c8bfSgjelinek /*
2489ff17c8bfSgjelinek  * This is the nftw call-back function used by zonecfg_dev_manifest.  It is
2490ff17c8bfSgjelinek  * responsible for calling the actual call-back.
2491ee519a1fSgjelinek  */
2492ee519a1fSgjelinek /* ARGSUSED2 */
2493ee519a1fSgjelinek static int
2494ee519a1fSgjelinek zonecfg_devwalk_cb(const char *path, const struct stat *st, int f,
2495ee519a1fSgjelinek     struct FTW *ftw)
2496ee519a1fSgjelinek {
2497ee519a1fSgjelinek 	acl_t *acl;
2498ee519a1fSgjelinek 	char *acl_txt = NULL;
2499ee519a1fSgjelinek 
2500ee519a1fSgjelinek 	/* skip all but character and block devices */
2501ee519a1fSgjelinek 	if (!S_ISBLK(st->st_mode) && !S_ISCHR(st->st_mode))
2502ee519a1fSgjelinek 		return (0);
2503ee519a1fSgjelinek 
2504ee519a1fSgjelinek 	if ((acl_get(path, ACL_NO_TRIVIAL, &acl) == 0) &&
2505ee519a1fSgjelinek 	    acl != NULL) {
2506ee519a1fSgjelinek 		acl_txt = acl_totext(acl, ACL_NORESOLVE);
2507ee519a1fSgjelinek 		acl_free(acl);
2508ee519a1fSgjelinek 	}
2509ee519a1fSgjelinek 
2510ee519a1fSgjelinek 	if (strlen(path) <= g_devwalk_skip_prefix)
2511ee519a1fSgjelinek 		return (0);
2512ee519a1fSgjelinek 
2513ee519a1fSgjelinek 	g_devwalk_cb(path + g_devwalk_skip_prefix, st->st_uid, st->st_gid,
2514ee519a1fSgjelinek 	    st->st_mode & S_IAMB, acl_txt != NULL ? acl_txt : "",
2515ee519a1fSgjelinek 	    g_devwalk_data);
2516ee519a1fSgjelinek 	free(acl_txt);
2517ee519a1fSgjelinek 	return (0);
2518ee519a1fSgjelinek }
2519ee519a1fSgjelinek 
2520ee519a1fSgjelinek /*
2521ff17c8bfSgjelinek  * Walk the dev tree for the zone specified by hdl and call the
2522ff17c8bfSgjelinek  * get_detach_dev_entry call-back function for each entry in the tree.  The
2523ff17c8bfSgjelinek  * call-back will be passed the name, uid, gid, mode, acl string and the
2524ff17c8bfSgjelinek  * handle input parameter for each dev entry.
2525ee519a1fSgjelinek  *
2526ff17c8bfSgjelinek  * Data is passed to get_detach_dev_entry through the global variables
2527ee519a1fSgjelinek  * g_devwalk_data, *g_devwalk_cb, and g_devwalk_skip_prefix.  The
2528ff17c8bfSgjelinek  * zonecfg_devwalk_cb function will actually call get_detach_dev_entry.
2529ee519a1fSgjelinek  */
2530ee519a1fSgjelinek int
2531ff17c8bfSgjelinek zonecfg_dev_manifest(zone_dochandle_t hdl)
2532ee519a1fSgjelinek {
2533ee519a1fSgjelinek 	char path[MAXPATHLEN];
2534ee519a1fSgjelinek 	int ret;
2535ee519a1fSgjelinek 
2536ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2537ee519a1fSgjelinek 		return (ret);
2538ee519a1fSgjelinek 
2539ee519a1fSgjelinek 	if (strlcat(path, "/dev", sizeof (path)) >= sizeof (path))
2540ee519a1fSgjelinek 		return (Z_TOO_BIG);
2541ee519a1fSgjelinek 
2542ee519a1fSgjelinek 	/*
2543ff17c8bfSgjelinek 	 * We have to serialize all devwalks in the same process
2544ee519a1fSgjelinek 	 * (which should be fine), since nftw() is so badly designed.
2545ee519a1fSgjelinek 	 */
2546ee519a1fSgjelinek 	(void) pthread_mutex_lock(&zonecfg_devwalk_lock);
2547ee519a1fSgjelinek 
2548ff17c8bfSgjelinek 	g_devwalk_skip_prefix = strlen(path) + 1;
2549ff17c8bfSgjelinek 	g_devwalk_data = (g_devwalk_data_t)hdl;
2550ff17c8bfSgjelinek 	g_devwalk_cb = get_detach_dev_entry;
2551ee519a1fSgjelinek 	(void) nftw(path, zonecfg_devwalk_cb, 0, FTW_PHYS);
2552ee519a1fSgjelinek 
2553ee519a1fSgjelinek 	(void) pthread_mutex_unlock(&zonecfg_devwalk_lock);
2554ee519a1fSgjelinek 	return (Z_OK);
2555ee519a1fSgjelinek }
2556ee519a1fSgjelinek 
2557ee519a1fSgjelinek /*
2558ee519a1fSgjelinek  * Update the owner, group, mode and acl on the specified dev (inpath) for
2559ee519a1fSgjelinek  * the zone (hdl).  This function can be used to fix up the dev tree after
2560ee519a1fSgjelinek  * attaching a migrated zone.
2561ee519a1fSgjelinek  */
2562ee519a1fSgjelinek int
2563ee519a1fSgjelinek zonecfg_devperms_apply(zone_dochandle_t hdl, const char *inpath, uid_t owner,
2564ee519a1fSgjelinek     gid_t group, mode_t mode, const char *acltxt)
2565ee519a1fSgjelinek {
2566ee519a1fSgjelinek 	int ret;
2567ee519a1fSgjelinek 	char path[MAXPATHLEN];
2568ee519a1fSgjelinek 	struct stat st;
2569ee519a1fSgjelinek 	acl_t *aclp;
2570ee519a1fSgjelinek 
2571ee519a1fSgjelinek 	if ((ret = zonecfg_get_zonepath(hdl, path, sizeof (path))) != Z_OK)
2572ee519a1fSgjelinek 		return (ret);
2573ee519a1fSgjelinek 
2574ee519a1fSgjelinek 	if (strlcat(path, "/dev/", sizeof (path)) >= sizeof (path))
2575ee519a1fSgjelinek 		return (Z_TOO_BIG);
2576ee519a1fSgjelinek 	if (strlcat(path, inpath, sizeof (path)) >= sizeof (path))
2577ee519a1fSgjelinek 		return (Z_TOO_BIG);
2578ee519a1fSgjelinek 
2579ee519a1fSgjelinek 	if (stat(path, &st) == -1)
2580ee519a1fSgjelinek 		return (Z_INVAL);
2581ee519a1fSgjelinek 
2582ee519a1fSgjelinek 	/* make sure we're only touching device nodes */
2583ee519a1fSgjelinek 	if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
2584ee519a1fSgjelinek 		return (Z_INVAL);
2585ee519a1fSgjelinek 
2586ee519a1fSgjelinek 	if (chown(path, owner, group) == -1)
2587ee519a1fSgjelinek 		return (Z_SYSTEM);
2588ee519a1fSgjelinek 
2589ee519a1fSgjelinek 	if (chmod(path, mode) == -1)
2590ee519a1fSgjelinek 		return (Z_SYSTEM);
2591ee519a1fSgjelinek 
2592ee519a1fSgjelinek 	if ((acltxt == NULL) || (strcmp(acltxt, "") == 0))
2593ee519a1fSgjelinek 		return (Z_OK);
2594ee519a1fSgjelinek 
2595ee519a1fSgjelinek 	if (acl_fromtext(acltxt, &aclp) != 0)
2596ee519a1fSgjelinek 		return (Z_SYSTEM);
2597ee519a1fSgjelinek 
2598ee519a1fSgjelinek 	errno = 0;
2599ee519a1fSgjelinek 	if (acl_set(path, aclp) == -1) {
2600ee519a1fSgjelinek 		free(aclp);
2601ee519a1fSgjelinek 		return (Z_SYSTEM);
2602ee519a1fSgjelinek 	}
2603ee519a1fSgjelinek 
2604ee519a1fSgjelinek 	free(aclp);
2605ee519a1fSgjelinek 	return (Z_OK);
2606ee519a1fSgjelinek }
2607ee519a1fSgjelinek 
26087c478bd9Sstevel@tonic-gate /*
26097c478bd9Sstevel@tonic-gate  * This function finds everything mounted under a zone's rootpath.
26107c478bd9Sstevel@tonic-gate  * This returns the number of mounts under rootpath, or -1 on error.
26117c478bd9Sstevel@tonic-gate  * callback is called once per mount found with the first argument
26127c478bd9Sstevel@tonic-gate  * pointing to the  mount point.
26137c478bd9Sstevel@tonic-gate  *
26147c478bd9Sstevel@tonic-gate  * If the callback function returns non-zero zonecfg_find_mounts
26157c478bd9Sstevel@tonic-gate  * aborts with an error.
26167c478bd9Sstevel@tonic-gate  */
26177c478bd9Sstevel@tonic-gate int
26187c478bd9Sstevel@tonic-gate zonecfg_find_mounts(char *rootpath, int (*callback)(const char *, void *),
26197c478bd9Sstevel@tonic-gate     void *priv) {
26207c478bd9Sstevel@tonic-gate 	FILE *mnttab;
26217c478bd9Sstevel@tonic-gate 	struct mnttab m;
26227c478bd9Sstevel@tonic-gate 	size_t l;
262307b574eeSgjelinek 	int zfsl;
26247c478bd9Sstevel@tonic-gate 	int rv = 0;
262507b574eeSgjelinek 	char zfs_path[MAXPATHLEN];
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	assert(rootpath != NULL);
26287c478bd9Sstevel@tonic-gate 
262907b574eeSgjelinek 	if ((zfsl = snprintf(zfs_path, sizeof (zfs_path), "%s/.zfs/", rootpath))
263007b574eeSgjelinek 	    >= sizeof (zfs_path))
263107b574eeSgjelinek 		return (-1);
263207b574eeSgjelinek 
26337c478bd9Sstevel@tonic-gate 	l = strlen(rootpath);
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 	mnttab = fopen("/etc/mnttab", "r");
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate 	if (mnttab == NULL)
26387c478bd9Sstevel@tonic-gate 		return (-1);
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(mnttab), MNTIOC_SHOWHIDDEN, NULL) < 0)  {
26417c478bd9Sstevel@tonic-gate 		rv = -1;
26427c478bd9Sstevel@tonic-gate 		goto out;
26437c478bd9Sstevel@tonic-gate 	}
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	while (!getmntent(mnttab, &m)) {
26467c478bd9Sstevel@tonic-gate 		if ((strncmp(rootpath, m.mnt_mountp, l) == 0) &&
264707b574eeSgjelinek 		    (m.mnt_mountp[l] == '/') &&
264807b574eeSgjelinek 		    (strncmp(zfs_path, m.mnt_mountp, zfsl) != 0)) {
26497c478bd9Sstevel@tonic-gate 			rv++;
26507c478bd9Sstevel@tonic-gate 			if (callback == NULL)
26517c478bd9Sstevel@tonic-gate 				continue;
26527c478bd9Sstevel@tonic-gate 			if (callback(m.mnt_mountp, priv)) {
26537c478bd9Sstevel@tonic-gate 				rv = -1;
26547c478bd9Sstevel@tonic-gate 				goto out;
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 			}
26577c478bd9Sstevel@tonic-gate 		}
26587c478bd9Sstevel@tonic-gate 	}
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate out:
26617c478bd9Sstevel@tonic-gate 	(void) fclose(mnttab);
26627c478bd9Sstevel@tonic-gate 	return (rv);
26637c478bd9Sstevel@tonic-gate }
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate int
26667c478bd9Sstevel@tonic-gate zonecfg_lookup_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
26677c478bd9Sstevel@tonic-gate {
26687c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, firstmatch;
26697c478bd9Sstevel@tonic-gate 	int err;
26707c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN], type[MAXNAMELEN], value[MAXNAMELEN];
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
26737c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
26747c478bd9Sstevel@tonic-gate 
26757c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
26767c478bd9Sstevel@tonic-gate 		return (err);
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
26797c478bd9Sstevel@tonic-gate 	firstmatch = NULL;
26807c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
26817c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
26827c478bd9Sstevel@tonic-gate 			continue;
26837c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_name) > 0) {
26847c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_NAME, name,
26857c478bd9Sstevel@tonic-gate 			    sizeof (name)) == Z_OK) &&
26867c478bd9Sstevel@tonic-gate 			    (strcmp(tabptr->zone_attr_name, name) == 0)) {
26877c478bd9Sstevel@tonic-gate 				if (firstmatch == NULL)
26887c478bd9Sstevel@tonic-gate 					firstmatch = cur;
26897c478bd9Sstevel@tonic-gate 				else
26907c478bd9Sstevel@tonic-gate 					return (Z_INSUFFICIENT_SPEC);
26917c478bd9Sstevel@tonic-gate 			}
26927c478bd9Sstevel@tonic-gate 		}
26937c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_type) > 0) {
26947c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_TYPE, type,
26957c478bd9Sstevel@tonic-gate 			    sizeof (type)) == Z_OK)) {
26967c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_type, type) == 0) {
26977c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
26987c478bd9Sstevel@tonic-gate 						firstmatch = cur;
26997c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
27007c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
27017c478bd9Sstevel@tonic-gate 				} else {
27027c478bd9Sstevel@tonic-gate 					/*
27037c478bd9Sstevel@tonic-gate 					 * If another property matched but this
27047c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
27057c478bd9Sstevel@tonic-gate 					 */
27067c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
27077c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
27087c478bd9Sstevel@tonic-gate 				}
27097c478bd9Sstevel@tonic-gate 			}
27107c478bd9Sstevel@tonic-gate 		}
27117c478bd9Sstevel@tonic-gate 		if (strlen(tabptr->zone_attr_value) > 0) {
27127c478bd9Sstevel@tonic-gate 			if ((fetchprop(cur, DTD_ATTR_VALUE, value,
27137c478bd9Sstevel@tonic-gate 			    sizeof (value)) == Z_OK)) {
27147c478bd9Sstevel@tonic-gate 				if (strcmp(tabptr->zone_attr_value, value) ==
27157c478bd9Sstevel@tonic-gate 				    0) {
27167c478bd9Sstevel@tonic-gate 					if (firstmatch == NULL)
27177c478bd9Sstevel@tonic-gate 						firstmatch = cur;
27187c478bd9Sstevel@tonic-gate 					else if (firstmatch != cur)
27197c478bd9Sstevel@tonic-gate 						return (Z_INSUFFICIENT_SPEC);
27207c478bd9Sstevel@tonic-gate 				} else {
27217c478bd9Sstevel@tonic-gate 					/*
27227c478bd9Sstevel@tonic-gate 					 * If another property matched but this
27237c478bd9Sstevel@tonic-gate 					 * one doesn't then reset firstmatch.
27247c478bd9Sstevel@tonic-gate 					 */
27257c478bd9Sstevel@tonic-gate 					if (firstmatch == cur)
27267c478bd9Sstevel@tonic-gate 						firstmatch = NULL;
27277c478bd9Sstevel@tonic-gate 				}
27287c478bd9Sstevel@tonic-gate 			}
27297c478bd9Sstevel@tonic-gate 		}
27307c478bd9Sstevel@tonic-gate 	}
27317c478bd9Sstevel@tonic-gate 	if (firstmatch == NULL)
27327c478bd9Sstevel@tonic-gate 		return (Z_NO_RESOURCE_ID);
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	cur = firstmatch;
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
27377c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK)
27387c478bd9Sstevel@tonic-gate 		return (err);
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
27417c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK)
27427c478bd9Sstevel@tonic-gate 		return (err);
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
27457c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK)
27467c478bd9Sstevel@tonic-gate 		return (err);
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	return (Z_OK);
27497c478bd9Sstevel@tonic-gate }
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate static int
27527c478bd9Sstevel@tonic-gate zonecfg_add_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27537c478bd9Sstevel@tonic-gate {
27547c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
27557c478bd9Sstevel@tonic-gate 	int err;
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_ATTR, NULL);
2758a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_attr_name);
27597c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27607c478bd9Sstevel@tonic-gate 		return (err);
2761a1be23daSdp 	err = newprop(newnode, DTD_ATTR_TYPE, tabptr->zone_attr_type);
27627c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27637c478bd9Sstevel@tonic-gate 		return (err);
2764a1be23daSdp 	err = newprop(newnode, DTD_ATTR_VALUE, tabptr->zone_attr_value);
27657c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
27667c478bd9Sstevel@tonic-gate 		return (err);
27677c478bd9Sstevel@tonic-gate 	return (Z_OK);
27687c478bd9Sstevel@tonic-gate }
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate int
27717c478bd9Sstevel@tonic-gate zonecfg_add_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27727c478bd9Sstevel@tonic-gate {
27737c478bd9Sstevel@tonic-gate 	int err;
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
27767c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
27797c478bd9Sstevel@tonic-gate 		return (err);
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, tabptr)) != Z_OK)
27827c478bd9Sstevel@tonic-gate 		return (err);
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate 	return (Z_OK);
27857c478bd9Sstevel@tonic-gate }
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate static int
27887c478bd9Sstevel@tonic-gate zonecfg_delete_attr_core(zone_dochandle_t handle, struct zone_attrtab *tabptr)
27897c478bd9Sstevel@tonic-gate {
27907c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
27917c478bd9Sstevel@tonic-gate 	int name_match, type_match, value_match;
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
27947c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_ATTR))
27957c478bd9Sstevel@tonic-gate 			continue;
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 		name_match = match_prop(cur, DTD_ATTR_NAME,
27987c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_name);
27997c478bd9Sstevel@tonic-gate 		type_match = match_prop(cur, DTD_ATTR_TYPE,
28007c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_type);
28017c478bd9Sstevel@tonic-gate 		value_match = match_prop(cur, DTD_ATTR_VALUE,
28027c478bd9Sstevel@tonic-gate 		    tabptr->zone_attr_value);
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 		if (name_match && type_match && value_match) {
28057c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
28067c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
28077c478bd9Sstevel@tonic-gate 			return (Z_OK);
28087c478bd9Sstevel@tonic-gate 		}
28097c478bd9Sstevel@tonic-gate 	}
28107c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
28117c478bd9Sstevel@tonic-gate }
28127c478bd9Sstevel@tonic-gate 
28137c478bd9Sstevel@tonic-gate int
28147c478bd9Sstevel@tonic-gate zonecfg_delete_attr(zone_dochandle_t handle, struct zone_attrtab *tabptr)
28157c478bd9Sstevel@tonic-gate {
28167c478bd9Sstevel@tonic-gate 	int err;
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 	if (tabptr == NULL)
28197c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28227c478bd9Sstevel@tonic-gate 		return (err);
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, tabptr)) != Z_OK)
28257c478bd9Sstevel@tonic-gate 		return (err);
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 	return (Z_OK);
28287c478bd9Sstevel@tonic-gate }
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate int
28317c478bd9Sstevel@tonic-gate zonecfg_modify_attr(
28327c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
28337c478bd9Sstevel@tonic-gate 	struct zone_attrtab *oldtabptr,
28347c478bd9Sstevel@tonic-gate 	struct zone_attrtab *newtabptr)
28357c478bd9Sstevel@tonic-gate {
28367c478bd9Sstevel@tonic-gate 	int err;
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || newtabptr == NULL)
28397c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
28427c478bd9Sstevel@tonic-gate 		return (err);
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_attr_core(handle, oldtabptr)) != Z_OK)
28457c478bd9Sstevel@tonic-gate 		return (err);
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_attr_core(handle, newtabptr)) != Z_OK)
28487c478bd9Sstevel@tonic-gate 		return (err);
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 	return (Z_OK);
28517c478bd9Sstevel@tonic-gate }
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate int
28547c478bd9Sstevel@tonic-gate zonecfg_get_attr_boolean(const struct zone_attrtab *attr, boolean_t *value)
28557c478bd9Sstevel@tonic-gate {
28567c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28577c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_BOOLEAN) != 0)
28607c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_TRUE) == 0) {
28637c478bd9Sstevel@tonic-gate 		*value = B_TRUE;
28647c478bd9Sstevel@tonic-gate 		return (Z_OK);
28657c478bd9Sstevel@tonic-gate 	}
28667c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_value, DTD_ENTITY_FALSE) == 0) {
28677c478bd9Sstevel@tonic-gate 		*value = B_FALSE;
28687c478bd9Sstevel@tonic-gate 		return (Z_OK);
28697c478bd9Sstevel@tonic-gate 	}
28707c478bd9Sstevel@tonic-gate 	return (Z_INVAL);
28717c478bd9Sstevel@tonic-gate }
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate int
28747c478bd9Sstevel@tonic-gate zonecfg_get_attr_int(const struct zone_attrtab *attr, int64_t *value)
28757c478bd9Sstevel@tonic-gate {
28767c478bd9Sstevel@tonic-gate 	long long result;
28777c478bd9Sstevel@tonic-gate 	char *endptr;
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28807c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_INT) != 0)
28837c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	errno = 0;
28867c478bd9Sstevel@tonic-gate 	result = strtoll(attr->zone_attr_value, &endptr, 10);
28877c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
28887c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28897c478bd9Sstevel@tonic-gate 	*value = result;
28907c478bd9Sstevel@tonic-gate 	return (Z_OK);
28917c478bd9Sstevel@tonic-gate }
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate int
28947c478bd9Sstevel@tonic-gate zonecfg_get_attr_string(const struct zone_attrtab *attr, char *value,
28957c478bd9Sstevel@tonic-gate     size_t val_sz)
28967c478bd9Sstevel@tonic-gate {
28977c478bd9Sstevel@tonic-gate 	if (attr == NULL)
28987c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
28997c478bd9Sstevel@tonic-gate 
29007c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_STRING) != 0)
29017c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 	if (strlcpy(value, attr->zone_attr_value, val_sz) >= val_sz)
29047c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
29057c478bd9Sstevel@tonic-gate 	return (Z_OK);
29067c478bd9Sstevel@tonic-gate }
29077c478bd9Sstevel@tonic-gate 
29087c478bd9Sstevel@tonic-gate int
29097c478bd9Sstevel@tonic-gate zonecfg_get_attr_uint(const struct zone_attrtab *attr, uint64_t *value)
29107c478bd9Sstevel@tonic-gate {
29117c478bd9Sstevel@tonic-gate 	unsigned long long result;
29127c478bd9Sstevel@tonic-gate 	long long neg_result;
29137c478bd9Sstevel@tonic-gate 	char *endptr;
29147c478bd9Sstevel@tonic-gate 
29157c478bd9Sstevel@tonic-gate 	if (attr == NULL)
29167c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 	if (strcmp(attr->zone_attr_type, DTD_ENTITY_UINT) != 0)
29197c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate 	errno = 0;
29227c478bd9Sstevel@tonic-gate 	result = strtoull(attr->zone_attr_value, &endptr, 10);
29237c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endptr != '\0')
29247c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29257c478bd9Sstevel@tonic-gate 	errno = 0;
29267c478bd9Sstevel@tonic-gate 	neg_result = strtoll(attr->zone_attr_value, &endptr, 10);
29277c478bd9Sstevel@tonic-gate 	/*
29287c478bd9Sstevel@tonic-gate 	 * Incredibly, strtoull("<negative number>", ...) will not fail but
29297c478bd9Sstevel@tonic-gate 	 * return whatever (negative) number cast as a u_longlong_t, so we
29307c478bd9Sstevel@tonic-gate 	 * need to look for this here.
29317c478bd9Sstevel@tonic-gate 	 */
29327c478bd9Sstevel@tonic-gate 	if (errno == 0 && neg_result < 0)
29337c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29347c478bd9Sstevel@tonic-gate 	*value = result;
29357c478bd9Sstevel@tonic-gate 	return (Z_OK);
29367c478bd9Sstevel@tonic-gate }
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate int
29397c478bd9Sstevel@tonic-gate zonecfg_lookup_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29407c478bd9Sstevel@tonic-gate {
29417c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
29427c478bd9Sstevel@tonic-gate 	char savedname[MAXNAMELEN];
29437c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29447c478bd9Sstevel@tonic-gate 	int err;
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	if (tabptr->zone_rctl_name == NULL ||
29477c478bd9Sstevel@tonic-gate 	    strlen(tabptr->zone_rctl_name) == 0)
29487c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
29517c478bd9Sstevel@tonic-gate 		return (err);
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
29547c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
29557c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
29567c478bd9Sstevel@tonic-gate 			continue;
29577c478bd9Sstevel@tonic-gate 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
29587c478bd9Sstevel@tonic-gate 		    sizeof (savedname)) == Z_OK) &&
29597c478bd9Sstevel@tonic-gate 		    (strcmp(savedname, tabptr->zone_rctl_name) == 0)) {
29607c478bd9Sstevel@tonic-gate 			tabptr->zone_rctl_valptr = NULL;
29617c478bd9Sstevel@tonic-gate 			for (val = cur->xmlChildrenNode; val != NULL;
29627c478bd9Sstevel@tonic-gate 			    val = val->next) {
29637c478bd9Sstevel@tonic-gate 				valptr = (struct zone_rctlvaltab *)malloc(
29647c478bd9Sstevel@tonic-gate 				    sizeof (struct zone_rctlvaltab));
29657c478bd9Sstevel@tonic-gate 				if (valptr == NULL)
29667c478bd9Sstevel@tonic-gate 					return (Z_NOMEM);
29677c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_PRIV,
29687c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_priv,
29697c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_priv)) !=
29707c478bd9Sstevel@tonic-gate 				    Z_OK))
29717c478bd9Sstevel@tonic-gate 					break;
29727c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_LIMIT,
29737c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_limit,
29747c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_limit)) !=
29757c478bd9Sstevel@tonic-gate 				    Z_OK))
29767c478bd9Sstevel@tonic-gate 					break;
29777c478bd9Sstevel@tonic-gate 				if ((fetchprop(val, DTD_ATTR_ACTION,
29787c478bd9Sstevel@tonic-gate 				    valptr->zone_rctlval_action,
29797c478bd9Sstevel@tonic-gate 				    sizeof (valptr->zone_rctlval_action)) !=
29807c478bd9Sstevel@tonic-gate 				    Z_OK))
29817c478bd9Sstevel@tonic-gate 					break;
29827c478bd9Sstevel@tonic-gate 				if (zonecfg_add_rctl_value(tabptr, valptr) !=
29837c478bd9Sstevel@tonic-gate 				    Z_OK)
29847c478bd9Sstevel@tonic-gate 					break;
29857c478bd9Sstevel@tonic-gate 			}
29867c478bd9Sstevel@tonic-gate 			return (Z_OK);
29877c478bd9Sstevel@tonic-gate 		}
29887c478bd9Sstevel@tonic-gate 	}
29897c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
29907c478bd9Sstevel@tonic-gate }
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate static int
29937c478bd9Sstevel@tonic-gate zonecfg_add_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
29947c478bd9Sstevel@tonic-gate {
29957c478bd9Sstevel@tonic-gate 	xmlNodePtr newnode, cur = handle->zone_dh_cur, valnode;
29967c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
29977c478bd9Sstevel@tonic-gate 	int err;
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_RCTL, NULL);
3000a1be23daSdp 	err = newprop(newnode, DTD_ATTR_NAME, tabptr->zone_rctl_name);
30017c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
30027c478bd9Sstevel@tonic-gate 		return (err);
30037c478bd9Sstevel@tonic-gate 	for (valptr = tabptr->zone_rctl_valptr; valptr != NULL;
30047c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
30057c478bd9Sstevel@tonic-gate 		valnode = xmlNewTextChild(newnode, NULL,
30067c478bd9Sstevel@tonic-gate 		    DTD_ELEM_RCTLVALUE, NULL);
3007a1be23daSdp 		err = newprop(valnode, DTD_ATTR_PRIV,
30087c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_priv);
30097c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
30107c478bd9Sstevel@tonic-gate 			return (err);
3011a1be23daSdp 		err = newprop(valnode, DTD_ATTR_LIMIT,
30127c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_limit);
30137c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
30147c478bd9Sstevel@tonic-gate 			return (err);
3015a1be23daSdp 		err = newprop(valnode, DTD_ATTR_ACTION,
30167c478bd9Sstevel@tonic-gate 		    valptr->zone_rctlval_action);
30177c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
30187c478bd9Sstevel@tonic-gate 			return (err);
30197c478bd9Sstevel@tonic-gate 	}
30207c478bd9Sstevel@tonic-gate 	return (Z_OK);
30217c478bd9Sstevel@tonic-gate }
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate int
30247c478bd9Sstevel@tonic-gate zonecfg_add_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30257c478bd9Sstevel@tonic-gate {
30267c478bd9Sstevel@tonic-gate 	int err;
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
30297c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30327c478bd9Sstevel@tonic-gate 		return (err);
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, tabptr)) != Z_OK)
30357c478bd9Sstevel@tonic-gate 		return (err);
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	return (Z_OK);
30387c478bd9Sstevel@tonic-gate }
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate static int
30417c478bd9Sstevel@tonic-gate zonecfg_delete_rctl_core(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30427c478bd9Sstevel@tonic-gate {
30437c478bd9Sstevel@tonic-gate 	xmlNodePtr cur = handle->zone_dh_cur;
30447c478bd9Sstevel@tonic-gate 	xmlChar *savedname;
30457c478bd9Sstevel@tonic-gate 	int name_result;
30467c478bd9Sstevel@tonic-gate 
30477c478bd9Sstevel@tonic-gate 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
30487c478bd9Sstevel@tonic-gate 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL))
30497c478bd9Sstevel@tonic-gate 			continue;
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 		savedname = xmlGetProp(cur, DTD_ATTR_NAME);
30527c478bd9Sstevel@tonic-gate 		if (savedname == NULL)	/* shouldn't happen */
30537c478bd9Sstevel@tonic-gate 			continue;
30547c478bd9Sstevel@tonic-gate 		name_result = xmlStrcmp(savedname,
30557c478bd9Sstevel@tonic-gate 		    (const xmlChar *) tabptr->zone_rctl_name);
30567c478bd9Sstevel@tonic-gate 		xmlFree(savedname);
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 		if (name_result == 0) {
30597c478bd9Sstevel@tonic-gate 			xmlUnlinkNode(cur);
30607c478bd9Sstevel@tonic-gate 			xmlFreeNode(cur);
30617c478bd9Sstevel@tonic-gate 			return (Z_OK);
30627c478bd9Sstevel@tonic-gate 		}
30637c478bd9Sstevel@tonic-gate 	}
30647c478bd9Sstevel@tonic-gate 	return (Z_NO_RESOURCE_ID);
30657c478bd9Sstevel@tonic-gate }
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate int
30687c478bd9Sstevel@tonic-gate zonecfg_delete_rctl(zone_dochandle_t handle, struct zone_rctltab *tabptr)
30697c478bd9Sstevel@tonic-gate {
30707c478bd9Sstevel@tonic-gate 	int err;
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate 	if (tabptr == NULL || tabptr->zone_rctl_name == NULL)
30737c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30767c478bd9Sstevel@tonic-gate 		return (err);
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, tabptr)) != Z_OK)
30797c478bd9Sstevel@tonic-gate 		return (err);
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	return (Z_OK);
30827c478bd9Sstevel@tonic-gate }
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate int
30857c478bd9Sstevel@tonic-gate zonecfg_modify_rctl(
30867c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle,
30877c478bd9Sstevel@tonic-gate 	struct zone_rctltab *oldtabptr,
30887c478bd9Sstevel@tonic-gate 	struct zone_rctltab *newtabptr)
30897c478bd9Sstevel@tonic-gate {
30907c478bd9Sstevel@tonic-gate 	int err;
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate 	if (oldtabptr == NULL || oldtabptr->zone_rctl_name == NULL ||
30937c478bd9Sstevel@tonic-gate 	    newtabptr == NULL || newtabptr->zone_rctl_name == NULL)
30947c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK)
30977c478bd9Sstevel@tonic-gate 		return (err);
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_delete_rctl_core(handle, oldtabptr)) != Z_OK)
31007c478bd9Sstevel@tonic-gate 		return (err);
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_add_rctl_core(handle, newtabptr)) != Z_OK)
31037c478bd9Sstevel@tonic-gate 		return (err);
31047c478bd9Sstevel@tonic-gate 
31057c478bd9Sstevel@tonic-gate 	return (Z_OK);
31067c478bd9Sstevel@tonic-gate }
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate int
31097c478bd9Sstevel@tonic-gate zonecfg_add_rctl_value(
31107c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
31117c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
31127c478bd9Sstevel@tonic-gate {
31137c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *old, *new;
31147c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk = alloca(rctlblk_size());
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
31177c478bd9Sstevel@tonic-gate 	for (old = last; old != NULL; old = old->zone_rctlval_next)
31187c478bd9Sstevel@tonic-gate 		last = old;	/* walk to the end of the list */
31197c478bd9Sstevel@tonic-gate 	new = valtabptr;	/* alloc'd by caller */
31207c478bd9Sstevel@tonic-gate 	new->zone_rctlval_next = NULL;
31217c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(valtabptr, rctlblk) != Z_OK)
31227c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31237c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
31247c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
31257c478bd9Sstevel@tonic-gate 	if (last == NULL)
31267c478bd9Sstevel@tonic-gate 		tabptr->zone_rctl_valptr = new;
31277c478bd9Sstevel@tonic-gate 	else
31287c478bd9Sstevel@tonic-gate 		last->zone_rctlval_next = new;
31297c478bd9Sstevel@tonic-gate 	return (Z_OK);
31307c478bd9Sstevel@tonic-gate }
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate int
31337c478bd9Sstevel@tonic-gate zonecfg_remove_rctl_value(
31347c478bd9Sstevel@tonic-gate 	struct zone_rctltab *tabptr,
31357c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valtabptr)
31367c478bd9Sstevel@tonic-gate {
31377c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *last, *this, *next;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	last = tabptr->zone_rctl_valptr;
31407c478bd9Sstevel@tonic-gate 	for (this = last; this != NULL; this = this->zone_rctlval_next) {
31417c478bd9Sstevel@tonic-gate 		if (strcmp(this->zone_rctlval_priv,
31427c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_priv) == 0 &&
31437c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_limit,
31447c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_limit) == 0 &&
31457c478bd9Sstevel@tonic-gate 		    strcmp(this->zone_rctlval_action,
31467c478bd9Sstevel@tonic-gate 		    valtabptr->zone_rctlval_action) == 0) {
31477c478bd9Sstevel@tonic-gate 			next = this->zone_rctlval_next;
31487c478bd9Sstevel@tonic-gate 			if (this == tabptr->zone_rctl_valptr)
31497c478bd9Sstevel@tonic-gate 				tabptr->zone_rctl_valptr = next;
31507c478bd9Sstevel@tonic-gate 			else
31517c478bd9Sstevel@tonic-gate 				last->zone_rctlval_next = next;
31527c478bd9Sstevel@tonic-gate 			free(this);
31537c478bd9Sstevel@tonic-gate 			return (Z_OK);
31547c478bd9Sstevel@tonic-gate 		} else
31557c478bd9Sstevel@tonic-gate 			last = this;
31567c478bd9Sstevel@tonic-gate 	}
31577c478bd9Sstevel@tonic-gate 	return (Z_NO_PROPERTY_ID);
31587c478bd9Sstevel@tonic-gate }
31597c478bd9Sstevel@tonic-gate 
3160ff17c8bfSgjelinek void
3161ff17c8bfSgjelinek zonecfg_set_swinv(zone_dochandle_t handle)
3162ff17c8bfSgjelinek {
3163ff17c8bfSgjelinek 	handle->zone_dh_sw_inv = B_TRUE;
3164ff17c8bfSgjelinek }
3165ff17c8bfSgjelinek 
3166ff17c8bfSgjelinek /*
3167ff17c8bfSgjelinek  * Add the pkg to the sw inventory on the handle.
3168ff17c8bfSgjelinek  */
3169ff17c8bfSgjelinek int
3170ff17c8bfSgjelinek zonecfg_add_pkg(zone_dochandle_t handle, char *name, char *version)
3171ff17c8bfSgjelinek {
3172ff17c8bfSgjelinek 	xmlNodePtr newnode;
3173ff17c8bfSgjelinek 	xmlNodePtr cur;
3174ff17c8bfSgjelinek 	int err;
3175ff17c8bfSgjelinek 
3176ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
3177ff17c8bfSgjelinek 		return (err);
3178ff17c8bfSgjelinek 
3179ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
3180ff17c8bfSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PACKAGE, NULL);
3181ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NAME, name)) != Z_OK)
3182ff17c8bfSgjelinek 		return (err);
3183ff17c8bfSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_VERSION, version)) != Z_OK)
3184ff17c8bfSgjelinek 		return (err);
3185ff17c8bfSgjelinek 	return (Z_OK);
3186ff17c8bfSgjelinek }
3187ff17c8bfSgjelinek 
3188ff17c8bfSgjelinek int
3189ff17c8bfSgjelinek zonecfg_add_patch(zone_dochandle_t handle, char *id, void **pnode)
3190ff17c8bfSgjelinek {
3191ff17c8bfSgjelinek 	xmlNodePtr node = (xmlNodePtr)*pnode;
3192ff17c8bfSgjelinek 	xmlNodePtr cur;
3193ff17c8bfSgjelinek 	int err;
3194ff17c8bfSgjelinek 
3195ff17c8bfSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
3196ff17c8bfSgjelinek 		return (err);
3197ff17c8bfSgjelinek 
3198ff17c8bfSgjelinek 	cur = handle->zone_dh_cur;
3199ff17c8bfSgjelinek 	node = xmlNewTextChild(cur, NULL, DTD_ELEM_PATCH, NULL);
3200ff17c8bfSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
3201ff17c8bfSgjelinek 		return (err);
3202ff17c8bfSgjelinek 	*pnode = (void *)node;
3203ff17c8bfSgjelinek 	return (Z_OK);
3204ff17c8bfSgjelinek }
3205ff17c8bfSgjelinek 
3206ff17c8bfSgjelinek int
3207ff17c8bfSgjelinek zonecfg_add_patch_obs(char *id, void *cur)
3208ff17c8bfSgjelinek {
3209ff17c8bfSgjelinek 	xmlNodePtr	node;
3210ff17c8bfSgjelinek 	int err;
3211ff17c8bfSgjelinek 
3212ff17c8bfSgjelinek 	node = xmlNewTextChild((xmlNodePtr)cur, NULL, DTD_ELEM_OBSOLETES, NULL);
3213ff17c8bfSgjelinek 	if ((err = newprop(node, DTD_ATTR_ID, id)) != Z_OK)
3214ff17c8bfSgjelinek 		return (err);
3215ff17c8bfSgjelinek 	return (Z_OK);
3216ff17c8bfSgjelinek }
3217ff17c8bfSgjelinek 
32187c478bd9Sstevel@tonic-gate char *
32197c478bd9Sstevel@tonic-gate zonecfg_strerror(int errnum)
32207c478bd9Sstevel@tonic-gate {
32217c478bd9Sstevel@tonic-gate 	switch (errnum) {
32227c478bd9Sstevel@tonic-gate 	case Z_OK:
32237c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "OK"));
32247c478bd9Sstevel@tonic-gate 	case Z_EMPTY_DOCUMENT:
32257c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Empty document"));
32267c478bd9Sstevel@tonic-gate 	case Z_WRONG_DOC_TYPE:
32277c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Wrong document type"));
32287c478bd9Sstevel@tonic-gate 	case Z_BAD_PROPERTY:
32297c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad document property"));
32307c478bd9Sstevel@tonic-gate 	case Z_TEMP_FILE:
32317c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32327c478bd9Sstevel@tonic-gate 		    "Problem creating temporary file"));
32337c478bd9Sstevel@tonic-gate 	case Z_SAVING_FILE:
32347c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem saving file"));
32357c478bd9Sstevel@tonic-gate 	case Z_NO_ENTRY:
32367c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such entry"));
32377c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ZONE_NAME:
32387c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bogus zone name"));
32397c478bd9Sstevel@tonic-gate 	case Z_REQD_RESOURCE_MISSING:
32407c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required resource missing"));
32417c478bd9Sstevel@tonic-gate 	case Z_REQD_PROPERTY_MISSING:
32427c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Required property missing"));
32437c478bd9Sstevel@tonic-gate 	case Z_BAD_HANDLE:
32447c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Bad handle"));
32457c478bd9Sstevel@tonic-gate 	case Z_NOMEM:
32467c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Out of memory"));
32477c478bd9Sstevel@tonic-gate 	case Z_INVAL:
32487c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid argument"));
32497c478bd9Sstevel@tonic-gate 	case Z_ACCES:
32507c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Permission denied"));
32517c478bd9Sstevel@tonic-gate 	case Z_TOO_BIG:
32527c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Argument list too long"));
32537c478bd9Sstevel@tonic-gate 	case Z_MISC_FS:
32547c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32557c478bd9Sstevel@tonic-gate 		    "Miscellaneous file system error"));
32567c478bd9Sstevel@tonic-gate 	case Z_NO_ZONE:
32577c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone configured"));
32587c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_TYPE:
32597c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource type"));
32607c478bd9Sstevel@tonic-gate 	case Z_NO_RESOURCE_ID:
32617c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such resource with that id"));
32627c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_TYPE:
32637c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property type"));
32647c478bd9Sstevel@tonic-gate 	case Z_NO_PROPERTY_ID:
32657c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such property with that id"));
3266087719fdSdp 	case Z_BAD_ZONE_STATE:
32677c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
3268087719fdSdp 		    "Zone state is invalid for the requested operation"));
32697c478bd9Sstevel@tonic-gate 	case Z_INVALID_DOCUMENT:
32707c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Invalid document"));
3271087719fdSdp 	case Z_NAME_IN_USE:
3272087719fdSdp 		return (dgettext(TEXT_DOMAIN, "Zone name already in use"));
32737c478bd9Sstevel@tonic-gate 	case Z_NO_SUCH_ID:
32747c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "No such zone ID"));
32757c478bd9Sstevel@tonic-gate 	case Z_UPDATING_INDEX:
32767c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Problem updating index file"));
32777c478bd9Sstevel@tonic-gate 	case Z_LOCKING_FILE:
32787c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Locking index file"));
32797c478bd9Sstevel@tonic-gate 	case Z_UNLOCKING_FILE:
32807c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unlocking index file"));
32817c478bd9Sstevel@tonic-gate 	case Z_INSUFFICIENT_SPEC:
32827c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Insufficient specification"));
32837c478bd9Sstevel@tonic-gate 	case Z_RESOLVED_PATH:
32847c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Resolved path mismatch"));
32857c478bd9Sstevel@tonic-gate 	case Z_IPV6_ADDR_PREFIX_LEN:
32867c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32877c478bd9Sstevel@tonic-gate 		    "IPv6 address missing required prefix length"));
32887c478bd9Sstevel@tonic-gate 	case Z_BOGUS_ADDRESS:
32897c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN,
32907c478bd9Sstevel@tonic-gate 		    "Neither an IPv4 nor an IPv6 address nor a host name"));
3291ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
3292ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3293ffbafc53Scomay 		    "Specified privilege is prohibited"));
3294ffbafc53Scomay 	case Z_PRIV_REQUIRED:
3295ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3296ffbafc53Scomay 		    "Required privilege is missing"));
3297ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
3298ffbafc53Scomay 		return (dgettext(TEXT_DOMAIN,
3299ffbafc53Scomay 		    "Specified privilege is unknown"));
33009acbbeafSnn35248 	case Z_BRAND_ERROR:
33019acbbeafSnn35248 		return (dgettext(TEXT_DOMAIN,
33029acbbeafSnn35248 		    "Brand-specific error"));
33030209230bSgjelinek 	case Z_INCOMPATIBLE:
33040209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Incompatible settings"));
33050209230bSgjelinek 	case Z_ALIAS_DISALLOW:
33060209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
33070209230bSgjelinek 		    "An incompatible rctl already exists for this property"));
33080209230bSgjelinek 	case Z_CLEAR_DISALLOW:
33090209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
33100209230bSgjelinek 		    "Clearing this property is not allowed"));
33110209230bSgjelinek 	case Z_POOL:
33120209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "libpool(3LIB) error"));
33130209230bSgjelinek 	case Z_POOLS_NOT_ACTIVE:
33140209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Pools facility not active; "
33150209230bSgjelinek 		    "zone will not be bound to pool"));
33160209230bSgjelinek 	case Z_POOL_ENABLE:
33170209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
33180209230bSgjelinek 		    "Could not enable pools facility"));
33190209230bSgjelinek 	case Z_NO_POOL:
33200209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
33210209230bSgjelinek 		    "Pool not found; using default pool"));
33220209230bSgjelinek 	case Z_POOL_CREATE:
33230209230bSgjelinek 		return (dgettext(TEXT_DOMAIN,
33240209230bSgjelinek 		    "Could not create a temporary pool"));
33250209230bSgjelinek 	case Z_POOL_BIND:
33260209230bSgjelinek 		return (dgettext(TEXT_DOMAIN, "Could not bind zone to pool"));
33277c478bd9Sstevel@tonic-gate 	default:
33287c478bd9Sstevel@tonic-gate 		return (dgettext(TEXT_DOMAIN, "Unknown error"));
33297c478bd9Sstevel@tonic-gate 	}
33307c478bd9Sstevel@tonic-gate }
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate /*
33337c478bd9Sstevel@tonic-gate  * Note that the zonecfg_setXent() and zonecfg_endXent() calls are all the
33347c478bd9Sstevel@tonic-gate  * same, as they just turn around and call zonecfg_setent() / zonecfg_endent().
33357c478bd9Sstevel@tonic-gate  */
33367c478bd9Sstevel@tonic-gate 
33377c478bd9Sstevel@tonic-gate static int
33387c478bd9Sstevel@tonic-gate zonecfg_setent(zone_dochandle_t handle)
33397c478bd9Sstevel@tonic-gate {
33407c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
33417c478bd9Sstevel@tonic-gate 	int err;
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate 	if (handle == NULL)
33447c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 	if ((err = operation_prep(handle)) != Z_OK) {
33477c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = NULL;
33487c478bd9Sstevel@tonic-gate 		return (err);
33497c478bd9Sstevel@tonic-gate 	}
33507c478bd9Sstevel@tonic-gate 	cur = handle->zone_dh_cur;
33517c478bd9Sstevel@tonic-gate 	cur = cur->xmlChildrenNode;
33527c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur;
33537c478bd9Sstevel@tonic-gate 	return (Z_OK);
33547c478bd9Sstevel@tonic-gate }
33557c478bd9Sstevel@tonic-gate 
33567c478bd9Sstevel@tonic-gate static int
33577c478bd9Sstevel@tonic-gate zonecfg_endent(zone_dochandle_t handle)
33587c478bd9Sstevel@tonic-gate {
33597c478bd9Sstevel@tonic-gate 	if (handle == NULL)
33607c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = handle->zone_dh_top;
33637c478bd9Sstevel@tonic-gate 	return (Z_OK);
33647c478bd9Sstevel@tonic-gate }
33657c478bd9Sstevel@tonic-gate 
33660209230bSgjelinek /*
33670209230bSgjelinek  * Do the work required to manipulate a process through libproc.
33680209230bSgjelinek  * If grab_process() returns no errors (0), then release_process()
33690209230bSgjelinek  * must eventually be called.
33700209230bSgjelinek  *
33710209230bSgjelinek  * Return values:
33720209230bSgjelinek  *      0 Successful creation of agent thread
33730209230bSgjelinek  *      1 Error grabbing
33740209230bSgjelinek  *      2 Error creating agent
33750209230bSgjelinek  */
33760209230bSgjelinek static int
33770209230bSgjelinek grab_process(pr_info_handle_t *p)
33780209230bSgjelinek {
33790209230bSgjelinek 	int ret;
33800209230bSgjelinek 
33810209230bSgjelinek 	if ((p->pr = Pgrab(p->pid, 0, &ret)) != NULL) {
33820209230bSgjelinek 
33830209230bSgjelinek 		if (Psetflags(p->pr, PR_RLC) != 0) {
33840209230bSgjelinek 			Prelease(p->pr, 0);
33850209230bSgjelinek 			return (1);
33860209230bSgjelinek 		}
33870209230bSgjelinek 		if (Pcreate_agent(p->pr) == 0) {
33880209230bSgjelinek 			return (0);
33890209230bSgjelinek 
33900209230bSgjelinek 		} else {
33910209230bSgjelinek 			Prelease(p->pr, 0);
33920209230bSgjelinek 			return (2);
33930209230bSgjelinek 		}
33940209230bSgjelinek 	} else {
33950209230bSgjelinek 		return (1);
33960209230bSgjelinek 	}
33970209230bSgjelinek }
33980209230bSgjelinek 
33990209230bSgjelinek /*
34000209230bSgjelinek  * Release the specified process. This destroys the agent
34010209230bSgjelinek  * and releases the process. If the process is NULL, nothing
34020209230bSgjelinek  * is done. This function should only be called if grab_process()
34030209230bSgjelinek  * has previously been called and returned success.
34040209230bSgjelinek  *
34050209230bSgjelinek  * This function is Pgrab-safe.
34060209230bSgjelinek  */
34070209230bSgjelinek static void
34080209230bSgjelinek release_process(struct ps_prochandle *Pr)
34090209230bSgjelinek {
34100209230bSgjelinek 	if (Pr == NULL)
34110209230bSgjelinek 		return;
34120209230bSgjelinek 
34130209230bSgjelinek 	Pdestroy_agent(Pr);
34140209230bSgjelinek 	Prelease(Pr, 0);
34150209230bSgjelinek }
34160209230bSgjelinek 
34170209230bSgjelinek static boolean_t
34180209230bSgjelinek grab_zone_proc(char *zonename, pr_info_handle_t *p)
34190209230bSgjelinek {
34200209230bSgjelinek 	DIR *dirp;
34210209230bSgjelinek 	struct dirent *dentp;
34220209230bSgjelinek 	zoneid_t zoneid;
34230209230bSgjelinek 	int pid_self;
34240209230bSgjelinek 	psinfo_t psinfo;
34250209230bSgjelinek 
34260209230bSgjelinek 	if (zone_get_id(zonename, &zoneid) != 0)
34270209230bSgjelinek 		return (B_FALSE);
34280209230bSgjelinek 
34290209230bSgjelinek 	pid_self = getpid();
34300209230bSgjelinek 
34310209230bSgjelinek 	if ((dirp = opendir("/proc")) == NULL)
34320209230bSgjelinek 		return (B_FALSE);
34330209230bSgjelinek 
34340209230bSgjelinek 	while (dentp = readdir(dirp)) {
34350209230bSgjelinek 		p->pid = atoi(dentp->d_name);
34360209230bSgjelinek 
34370209230bSgjelinek 		/* Skip self */
34380209230bSgjelinek 		if (p->pid == pid_self)
34390209230bSgjelinek 			continue;
34400209230bSgjelinek 
34410209230bSgjelinek 		if (proc_get_psinfo(p->pid, &psinfo) != 0)
34420209230bSgjelinek 			continue;
34430209230bSgjelinek 
34440209230bSgjelinek 		if (psinfo.pr_zoneid != zoneid)
34450209230bSgjelinek 			continue;
34460209230bSgjelinek 
34470209230bSgjelinek 		/* attempt to grab process */
34480209230bSgjelinek 		if (grab_process(p) != 0)
34490209230bSgjelinek 			continue;
34500209230bSgjelinek 
34510209230bSgjelinek 		if (pr_getzoneid(p->pr) != zoneid) {
34520209230bSgjelinek 			release_process(p->pr);
34530209230bSgjelinek 			continue;
34540209230bSgjelinek 		}
34550209230bSgjelinek 
34560209230bSgjelinek 		(void) closedir(dirp);
34570209230bSgjelinek 		return (B_TRUE);
34580209230bSgjelinek 	}
34590209230bSgjelinek 
34600209230bSgjelinek 	(void) closedir(dirp);
34610209230bSgjelinek 	return (B_FALSE);
34620209230bSgjelinek }
34630209230bSgjelinek 
34640209230bSgjelinek static boolean_t
34650209230bSgjelinek get_priv_rctl(struct ps_prochandle *pr, char *name, rctlblk_t *rblk)
34660209230bSgjelinek {
34670209230bSgjelinek 	if (pr_getrctl(pr, name, NULL, rblk, RCTL_FIRST))
34680209230bSgjelinek 		return (B_FALSE);
34690209230bSgjelinek 
34700209230bSgjelinek 	if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
34710209230bSgjelinek 		return (B_TRUE);
34720209230bSgjelinek 
34730209230bSgjelinek 	while (pr_getrctl(pr, name, rblk, rblk, RCTL_NEXT) == 0) {
34740209230bSgjelinek 		if (rctlblk_get_privilege(rblk) == RCPRIV_PRIVILEGED)
34750209230bSgjelinek 			return (B_TRUE);
34760209230bSgjelinek 	}
34770209230bSgjelinek 
34780209230bSgjelinek 	return (B_FALSE);
34790209230bSgjelinek }
34800209230bSgjelinek 
34810209230bSgjelinek /*
34820209230bSgjelinek  * Apply the current rctl settings to the specified, running zone.
34830209230bSgjelinek  */
34840209230bSgjelinek int
34850209230bSgjelinek zonecfg_apply_rctls(char *zone_name, zone_dochandle_t handle)
34860209230bSgjelinek {
34870209230bSgjelinek 	int err;
34880209230bSgjelinek 	int res = Z_OK;
34890209230bSgjelinek 	rctlblk_t *rblk;
34900209230bSgjelinek 	pr_info_handle_t p;
34910209230bSgjelinek 	struct zone_rctltab rctl;
34920209230bSgjelinek 
34930209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK)
34940209230bSgjelinek 		return (err);
34950209230bSgjelinek 
34960209230bSgjelinek 	if ((rblk = (rctlblk_t *)malloc(rctlblk_size())) == NULL) {
34970209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
34980209230bSgjelinek 		return (Z_NOMEM);
34990209230bSgjelinek 	}
35000209230bSgjelinek 
35010209230bSgjelinek 	if (!grab_zone_proc(zone_name, &p)) {
35020209230bSgjelinek 		(void) zonecfg_endrctlent(handle);
35030209230bSgjelinek 		free(rblk);
35040209230bSgjelinek 		return (Z_SYSTEM);
35050209230bSgjelinek 	}
35060209230bSgjelinek 
35070209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
35080209230bSgjelinek 		char *rname;
35090209230bSgjelinek 		struct zone_rctlvaltab *valptr;
35100209230bSgjelinek 
35110209230bSgjelinek 		rname = rctl.zone_rctl_name;
35120209230bSgjelinek 
35130209230bSgjelinek 		/* first delete all current privileged settings for this rctl */
35140209230bSgjelinek 		while (get_priv_rctl(p.pr, rname, rblk)) {
35150209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_DELETE) !=
35160209230bSgjelinek 			    0) {
35170209230bSgjelinek 				res = Z_SYSTEM;
35180209230bSgjelinek 				goto done;
35190209230bSgjelinek 			}
35200209230bSgjelinek 		}
35210209230bSgjelinek 
35220209230bSgjelinek 		/* now set each new value for the rctl */
35230209230bSgjelinek 		for (valptr = rctl.zone_rctl_valptr; valptr != NULL;
35240209230bSgjelinek 		    valptr = valptr->zone_rctlval_next) {
35250209230bSgjelinek 			if ((err = zonecfg_construct_rctlblk(valptr, rblk))
35260209230bSgjelinek 			    != Z_OK) {
35270209230bSgjelinek 				res = errno = err;
35280209230bSgjelinek 				goto done;
35290209230bSgjelinek 			}
35300209230bSgjelinek 
35310209230bSgjelinek 			if (pr_setrctl(p.pr, rname, NULL, rblk, RCTL_INSERT)) {
35320209230bSgjelinek 				res = Z_SYSTEM;
35330209230bSgjelinek 				goto done;
35340209230bSgjelinek 			}
35350209230bSgjelinek 		}
35360209230bSgjelinek 	}
35370209230bSgjelinek 
35380209230bSgjelinek done:
35390209230bSgjelinek 	release_process(p.pr);
35400209230bSgjelinek 	free(rblk);
35410209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
35420209230bSgjelinek 
35430209230bSgjelinek 	return (res);
35440209230bSgjelinek }
35450209230bSgjelinek 
35460209230bSgjelinek static const xmlChar *
35470209230bSgjelinek nm_to_dtd(char *nm)
35480209230bSgjelinek {
35490209230bSgjelinek 	if (strcmp(nm, "device") == 0)
35500209230bSgjelinek 		return (DTD_ELEM_DEVICE);
35510209230bSgjelinek 	if (strcmp(nm, "fs") == 0)
35520209230bSgjelinek 		return (DTD_ELEM_FS);
35530209230bSgjelinek 	if (strcmp(nm, "inherit-pkg-dir") == 0)
35540209230bSgjelinek 		return (DTD_ELEM_IPD);
35550209230bSgjelinek 	if (strcmp(nm, "net") == 0)
35560209230bSgjelinek 		return (DTD_ELEM_NET);
35570209230bSgjelinek 	if (strcmp(nm, "attr") == 0)
35580209230bSgjelinek 		return (DTD_ELEM_ATTR);
35590209230bSgjelinek 	if (strcmp(nm, "rctl") == 0)
35600209230bSgjelinek 		return (DTD_ELEM_RCTL);
35610209230bSgjelinek 	if (strcmp(nm, "dataset") == 0)
35620209230bSgjelinek 		return (DTD_ELEM_DATASET);
35630209230bSgjelinek 
35640209230bSgjelinek 	return (NULL);
35650209230bSgjelinek }
35660209230bSgjelinek 
35670209230bSgjelinek int
35680209230bSgjelinek zonecfg_num_resources(zone_dochandle_t handle, char *rsrc)
35690209230bSgjelinek {
35700209230bSgjelinek 	int num = 0;
35710209230bSgjelinek 	const xmlChar *dtd;
35720209230bSgjelinek 	xmlNodePtr cur;
35730209230bSgjelinek 
35740209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
35750209230bSgjelinek 		return (num);
35760209230bSgjelinek 
35770209230bSgjelinek 	if (zonecfg_setent(handle) != Z_OK)
35780209230bSgjelinek 		return (num);
35790209230bSgjelinek 
35800209230bSgjelinek 	for (cur = handle->zone_dh_cur; cur != NULL; cur = cur->next)
35810209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd) == 0)
35820209230bSgjelinek 			num++;
35830209230bSgjelinek 
35840209230bSgjelinek 	(void) zonecfg_endent(handle);
35850209230bSgjelinek 
35860209230bSgjelinek 	return (num);
35870209230bSgjelinek }
35880209230bSgjelinek 
35890209230bSgjelinek int
35900209230bSgjelinek zonecfg_del_all_resources(zone_dochandle_t handle, char *rsrc)
35910209230bSgjelinek {
35920209230bSgjelinek 	int err;
35930209230bSgjelinek 	const xmlChar *dtd;
35940209230bSgjelinek 	xmlNodePtr cur;
35950209230bSgjelinek 
35960209230bSgjelinek 	if ((dtd = nm_to_dtd(rsrc)) == NULL)
35970209230bSgjelinek 		return (Z_NO_RESOURCE_TYPE);
35980209230bSgjelinek 
35990209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
36000209230bSgjelinek 		return (err);
36010209230bSgjelinek 
36020209230bSgjelinek 	cur = handle->zone_dh_cur;
36030209230bSgjelinek 	while (cur != NULL) {
36040209230bSgjelinek 		xmlNodePtr tmp;
36050209230bSgjelinek 
36060209230bSgjelinek 		if (xmlStrcmp(cur->name, dtd)) {
36070209230bSgjelinek 			cur = cur->next;
36080209230bSgjelinek 			continue;
36090209230bSgjelinek 		}
36100209230bSgjelinek 
36110209230bSgjelinek 		tmp = cur->next;
36120209230bSgjelinek 		xmlUnlinkNode(cur);
36130209230bSgjelinek 		xmlFreeNode(cur);
36140209230bSgjelinek 		cur = tmp;
36150209230bSgjelinek 	}
36160209230bSgjelinek 
36170209230bSgjelinek 	(void) zonecfg_endent(handle);
36180209230bSgjelinek 	return (Z_OK);
36190209230bSgjelinek }
36200209230bSgjelinek 
36210209230bSgjelinek static boolean_t
36220209230bSgjelinek valid_uint(char *s, uint64_t *n)
36230209230bSgjelinek {
36240209230bSgjelinek 	char *endp;
36250209230bSgjelinek 
36260209230bSgjelinek 	/* strtoull accepts '-'?! so we want to flag that as an error */
36270209230bSgjelinek 	if (strchr(s, '-') != NULL)
36280209230bSgjelinek 		return (B_FALSE);
36290209230bSgjelinek 
36300209230bSgjelinek 	errno = 0;
36310209230bSgjelinek 	*n = strtoull(s, &endp, 10);
36320209230bSgjelinek 
36330209230bSgjelinek 	if (errno != 0 || *endp != '\0')
36340209230bSgjelinek 		return (B_FALSE);
36350209230bSgjelinek 	return (B_TRUE);
36360209230bSgjelinek }
36370209230bSgjelinek 
36380209230bSgjelinek /*
36390209230bSgjelinek  * Convert a string representing a number (possibly a fraction) into an integer.
36400209230bSgjelinek  * The string can have a modifier (K, M, G or T).   The modifiers are treated
36410209230bSgjelinek  * as powers of two (not 10).
36420209230bSgjelinek  */
36430209230bSgjelinek int
36440209230bSgjelinek zonecfg_str_to_bytes(char *str, uint64_t *bytes)
36450209230bSgjelinek {
36460209230bSgjelinek 	long double val;
36470209230bSgjelinek 	char *unitp;
36480209230bSgjelinek 	uint64_t scale;
36490209230bSgjelinek 
36500209230bSgjelinek 	if ((val = strtold(str, &unitp)) < 0)
36510209230bSgjelinek 		return (-1);
36520209230bSgjelinek 
36530209230bSgjelinek 	/* remove any leading white space from units string */
36540209230bSgjelinek 	while (isspace(*unitp) != 0)
36550209230bSgjelinek 		++unitp;
36560209230bSgjelinek 
36570209230bSgjelinek 	/* if no units explicitly set, error */
36580209230bSgjelinek 	if (unitp == NULL || *unitp == '\0') {
36590209230bSgjelinek 		scale = 1;
36600209230bSgjelinek 	} else {
36610209230bSgjelinek 		int i;
36620209230bSgjelinek 		char *units[] = {"K", "M", "G", "T", NULL};
36630209230bSgjelinek 
36640209230bSgjelinek 		scale = 1024;
36650209230bSgjelinek 
36660209230bSgjelinek 		/* update scale based on units */
36670209230bSgjelinek 		for (i = 0; units[i] != NULL; i++) {
36680209230bSgjelinek 			if (strcasecmp(unitp, units[i]) == 0)
36690209230bSgjelinek 				break;
36700209230bSgjelinek 			scale <<= 10;
36710209230bSgjelinek 		}
36720209230bSgjelinek 
36730209230bSgjelinek 		if (units[i] == NULL)
36740209230bSgjelinek 			return (-1);
36750209230bSgjelinek 	}
36760209230bSgjelinek 
36770209230bSgjelinek 	*bytes = (uint64_t)(val * scale);
36780209230bSgjelinek 	return (0);
36790209230bSgjelinek }
36800209230bSgjelinek 
36810209230bSgjelinek boolean_t
36820209230bSgjelinek zonecfg_valid_ncpus(char *lowstr, char *highstr)
36830209230bSgjelinek {
36840209230bSgjelinek 	uint64_t low, high;
36850209230bSgjelinek 
36860209230bSgjelinek 	if (!valid_uint(lowstr, &low) || !valid_uint(highstr, &high) ||
36870209230bSgjelinek 	    low < 1 || low > high)
36880209230bSgjelinek 		return (B_FALSE);
36890209230bSgjelinek 
36900209230bSgjelinek 	return (B_TRUE);
36910209230bSgjelinek }
36920209230bSgjelinek 
36930209230bSgjelinek boolean_t
36940209230bSgjelinek zonecfg_valid_importance(char *impstr)
36950209230bSgjelinek {
36960209230bSgjelinek 	uint64_t num;
36970209230bSgjelinek 
36980209230bSgjelinek 	if (!valid_uint(impstr, &num))
36990209230bSgjelinek 		return (B_FALSE);
37000209230bSgjelinek 
37010209230bSgjelinek 	return (B_TRUE);
37020209230bSgjelinek }
37030209230bSgjelinek 
37040209230bSgjelinek boolean_t
37050209230bSgjelinek zonecfg_valid_alias_limit(char *name, char *limitstr, uint64_t *limit)
37060209230bSgjelinek {
37070209230bSgjelinek 	int i;
37080209230bSgjelinek 
37090209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
37100209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
37110209230bSgjelinek 			break;
37120209230bSgjelinek 
37130209230bSgjelinek 	if (aliases[i].shortname == NULL)
37140209230bSgjelinek 		return (B_FALSE);
37150209230bSgjelinek 
37160209230bSgjelinek 	if (!valid_uint(limitstr, limit) || *limit < aliases[i].low_limit)
37170209230bSgjelinek 		return (B_FALSE);
37180209230bSgjelinek 
37190209230bSgjelinek 	return (B_TRUE);
37200209230bSgjelinek }
37210209230bSgjelinek 
37220209230bSgjelinek boolean_t
37230209230bSgjelinek zonecfg_valid_memlimit(char *memstr, uint64_t *mem_val)
37240209230bSgjelinek {
37250209230bSgjelinek 	if (zonecfg_str_to_bytes(memstr, mem_val) != 0)
37260209230bSgjelinek 		return (B_FALSE);
37270209230bSgjelinek 
37280209230bSgjelinek 	return (B_TRUE);
37290209230bSgjelinek }
37300209230bSgjelinek 
37310209230bSgjelinek static int
37320209230bSgjelinek zerr_pool(char *pool_err, int err_size, int res)
37330209230bSgjelinek {
37340209230bSgjelinek 	(void) strlcpy(pool_err, pool_strerror(pool_error()), err_size);
37350209230bSgjelinek 	return (res);
37360209230bSgjelinek }
37370209230bSgjelinek 
37380209230bSgjelinek static int
37390209230bSgjelinek create_tmp_pset(char *pool_err, int err_size, pool_conf_t *pconf, pool_t *pool,
37400209230bSgjelinek     char *name, int min, int max)
37410209230bSgjelinek {
37420209230bSgjelinek 	pool_resource_t *res;
37430209230bSgjelinek 	pool_elem_t *elem;
37440209230bSgjelinek 	pool_value_t *val;
37450209230bSgjelinek 
37460209230bSgjelinek 	if ((res = pool_resource_create(pconf, "pset", name)) == NULL)
37470209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
37480209230bSgjelinek 
37490209230bSgjelinek 	if (pool_associate(pconf, pool, res) != PO_SUCCESS)
37500209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
37510209230bSgjelinek 
37520209230bSgjelinek 	if ((elem = pool_resource_to_elem(pconf, res)) == NULL)
37530209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
37540209230bSgjelinek 
37550209230bSgjelinek 	if ((val = pool_value_alloc()) == NULL)
37560209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
37570209230bSgjelinek 
37580209230bSgjelinek 	/* set the maximum number of cpus for the pset */
37590209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)max);
37600209230bSgjelinek 
37610209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.max", val) != PO_SUCCESS) {
37620209230bSgjelinek 		pool_value_free(val);
37630209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
37640209230bSgjelinek 	}
37650209230bSgjelinek 
37660209230bSgjelinek 	/* set the minimum number of cpus for the pset */
37670209230bSgjelinek 	pool_value_set_uint64(val, (uint64_t)min);
37680209230bSgjelinek 
37690209230bSgjelinek 	if (pool_put_property(pconf, elem, "pset.min", val) != PO_SUCCESS) {
37700209230bSgjelinek 		pool_value_free(val);
37710209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
37720209230bSgjelinek 	}
37730209230bSgjelinek 
37740209230bSgjelinek 	pool_value_free(val);
37750209230bSgjelinek 
37760209230bSgjelinek 	return (Z_OK);
37770209230bSgjelinek }
37780209230bSgjelinek 
37790209230bSgjelinek static int
37800209230bSgjelinek create_tmp_pool(char *pool_err, int err_size, pool_conf_t *pconf, char *name,
37810209230bSgjelinek     struct zone_psettab *pset_tab)
37820209230bSgjelinek {
37830209230bSgjelinek 	pool_t *pool;
37840209230bSgjelinek 	int res = Z_OK;
37850209230bSgjelinek 
37860209230bSgjelinek 	/* create a temporary pool configuration */
37870209230bSgjelinek 	if (pool_conf_open(pconf, NULL, PO_TEMP) != PO_SUCCESS) {
37880209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
37890209230bSgjelinek 		return (res);
37900209230bSgjelinek 	}
37910209230bSgjelinek 
37920209230bSgjelinek 	if ((pool = pool_create(pconf, name)) == NULL) {
37930209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
37940209230bSgjelinek 		goto done;
37950209230bSgjelinek 	}
37960209230bSgjelinek 
37970209230bSgjelinek 	/* set pool importance */
37980209230bSgjelinek 	if (pset_tab->zone_importance[0] != '\0') {
37990209230bSgjelinek 		pool_elem_t *elem;
38000209230bSgjelinek 		pool_value_t *val;
38010209230bSgjelinek 
38020209230bSgjelinek 		if ((elem = pool_to_elem(pconf, pool)) == NULL) {
38030209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38040209230bSgjelinek 			goto done;
38050209230bSgjelinek 		}
38060209230bSgjelinek 
38070209230bSgjelinek 		if ((val = pool_value_alloc()) == NULL) {
38080209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38090209230bSgjelinek 			goto done;
38100209230bSgjelinek 		}
38110209230bSgjelinek 
38120209230bSgjelinek 		pool_value_set_int64(val,
38130209230bSgjelinek 		    (int64_t)atoi(pset_tab->zone_importance));
38140209230bSgjelinek 
38150209230bSgjelinek 		if (pool_put_property(pconf, elem, "pool.importance", val)
38160209230bSgjelinek 		    != PO_SUCCESS) {
38170209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
38180209230bSgjelinek 			pool_value_free(val);
38190209230bSgjelinek 			goto done;
38200209230bSgjelinek 		}
38210209230bSgjelinek 
38220209230bSgjelinek 		pool_value_free(val);
38230209230bSgjelinek 	}
38240209230bSgjelinek 
38250209230bSgjelinek 	if ((res = create_tmp_pset(pool_err, err_size, pconf, pool, name,
38260209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_min),
38270209230bSgjelinek 	    atoi(pset_tab->zone_ncpu_max))) != Z_OK)
38280209230bSgjelinek 		goto done;
38290209230bSgjelinek 
38300209230bSgjelinek 	/* validation */
38310209230bSgjelinek 	if (pool_conf_status(pconf) == POF_INVALID) {
38320209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
38330209230bSgjelinek 		goto done;
38340209230bSgjelinek 	}
38350209230bSgjelinek 
38360209230bSgjelinek 	/*
38370209230bSgjelinek 	 * This validation is the one we expect to fail if the user specified
38380209230bSgjelinek 	 * an invalid configuration (too many cpus) for this system.
38390209230bSgjelinek 	 */
38400209230bSgjelinek 	if (pool_conf_validate(pconf, POV_RUNTIME) != PO_SUCCESS) {
38410209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL_CREATE);
38420209230bSgjelinek 		goto done;
38430209230bSgjelinek 	}
38440209230bSgjelinek 
38450209230bSgjelinek 	/*
38460209230bSgjelinek 	 * Commit the dynamic configuration but not the pool configuration
38470209230bSgjelinek 	 * file.
38480209230bSgjelinek 	 */
38490209230bSgjelinek 	if (pool_conf_commit(pconf, 1) != PO_SUCCESS)
38500209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
38510209230bSgjelinek 
38520209230bSgjelinek done:
38530209230bSgjelinek 	(void) pool_conf_close(pconf);
38540209230bSgjelinek 	return (res);
38550209230bSgjelinek }
38560209230bSgjelinek 
38570209230bSgjelinek static int
38580209230bSgjelinek get_running_tmp_pset(pool_conf_t *pconf, pool_t *pool, pool_resource_t *pset,
38590209230bSgjelinek     struct zone_psettab *pset_tab)
38600209230bSgjelinek {
38610209230bSgjelinek 	int nfound = 0;
38620209230bSgjelinek 	pool_elem_t *pe;
38630209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
38640209230bSgjelinek 	uint64_t val_uint;
38650209230bSgjelinek 
38660209230bSgjelinek 	if (pool != NULL) {
38670209230bSgjelinek 		pe = pool_to_elem(pconf, pool);
38680209230bSgjelinek 		if (pool_get_property(pconf, pe, "pool.importance", pv)
38690209230bSgjelinek 		    != POC_INVAL) {
38700209230bSgjelinek 			int64_t val_int;
38710209230bSgjelinek 
38720209230bSgjelinek 			(void) pool_value_get_int64(pv, &val_int);
38730209230bSgjelinek 			(void) snprintf(pset_tab->zone_importance,
38740209230bSgjelinek 			    sizeof (pset_tab->zone_importance), "%d", val_int);
38750209230bSgjelinek 			nfound++;
38760209230bSgjelinek 		}
38770209230bSgjelinek 	}
38780209230bSgjelinek 
38790209230bSgjelinek 	if (pset != NULL) {
38800209230bSgjelinek 		pe = pool_resource_to_elem(pconf, pset);
38810209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.min", pv) != POC_INVAL) {
38820209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
38830209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_min,
38840209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_min), "%u", val_uint);
38850209230bSgjelinek 			nfound++;
38860209230bSgjelinek 		}
38870209230bSgjelinek 
38880209230bSgjelinek 		if (pool_get_property(pconf, pe, "pset.max", pv) != POC_INVAL) {
38890209230bSgjelinek 			(void) pool_value_get_uint64(pv, &val_uint);
38900209230bSgjelinek 			(void) snprintf(pset_tab->zone_ncpu_max,
38910209230bSgjelinek 			    sizeof (pset_tab->zone_ncpu_max), "%u", val_uint);
38920209230bSgjelinek 			nfound++;
38930209230bSgjelinek 		}
38940209230bSgjelinek 	}
38950209230bSgjelinek 
38960209230bSgjelinek 	pool_value_free(pv);
38970209230bSgjelinek 
38980209230bSgjelinek 	if (nfound == 3)
38990209230bSgjelinek 		return (PO_SUCCESS);
39000209230bSgjelinek 
39010209230bSgjelinek 	return (PO_FAIL);
39020209230bSgjelinek }
39030209230bSgjelinek 
39040209230bSgjelinek /*
39050209230bSgjelinek  * Determine if a tmp pool is configured and if so, if the configuration is
39060209230bSgjelinek  * still valid or if it has been changed since the tmp pool was created.
39070209230bSgjelinek  * If the tmp pool configuration is no longer valid, delete the tmp pool.
39080209230bSgjelinek  *
39090209230bSgjelinek  * Set *valid=B_TRUE if there is an existing, valid tmp pool configuration.
39100209230bSgjelinek  */
39110209230bSgjelinek static int
39120209230bSgjelinek verify_del_tmp_pool(pool_conf_t *pconf, char *tmp_name, char *pool_err,
39130209230bSgjelinek     int err_size, struct zone_psettab *pset_tab, boolean_t *exists)
39140209230bSgjelinek {
39150209230bSgjelinek 	int res = Z_OK;
39160209230bSgjelinek 	pool_t *pool;
39170209230bSgjelinek 	pool_resource_t *pset;
39180209230bSgjelinek 	struct zone_psettab pset_current;
39190209230bSgjelinek 
39200209230bSgjelinek 	*exists = B_FALSE;
39210209230bSgjelinek 
39220209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
39230209230bSgjelinek 	    != PO_SUCCESS) {
39240209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
39250209230bSgjelinek 		return (res);
39260209230bSgjelinek 	}
39270209230bSgjelinek 
39280209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
39290209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
39300209230bSgjelinek 
39310209230bSgjelinek 	if (pool == NULL && pset == NULL) {
39320209230bSgjelinek 		/* no tmp pool configured */
39330209230bSgjelinek 		goto done;
39340209230bSgjelinek 	}
39350209230bSgjelinek 
39360209230bSgjelinek 	/*
39370209230bSgjelinek 	 * If an existing tmp pool for this zone is configured with the proper
39380209230bSgjelinek 	 * settings, then the tmp pool is valid.
39390209230bSgjelinek 	 */
39400209230bSgjelinek 	if (get_running_tmp_pset(pconf, pool, pset, &pset_current)
39410209230bSgjelinek 	    == PO_SUCCESS &&
39420209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_min,
39430209230bSgjelinek 	    pset_current.zone_ncpu_min) == 0 &&
39440209230bSgjelinek 	    strcmp(pset_tab->zone_ncpu_max,
39450209230bSgjelinek 	    pset_current.zone_ncpu_max) == 0 &&
39460209230bSgjelinek 	    strcmp(pset_tab->zone_importance,
39470209230bSgjelinek 	    pset_current.zone_importance) == 0) {
39480209230bSgjelinek 		*exists = B_TRUE;
39490209230bSgjelinek 
39500209230bSgjelinek 	} else {
39510209230bSgjelinek 		/*
39520209230bSgjelinek 		 * An out-of-date tmp pool configuration exists.  Delete it
39530209230bSgjelinek 		 * so that we can create the correct tmp pool config.
39540209230bSgjelinek 		 */
39550209230bSgjelinek 		if (pset != NULL &&
39560209230bSgjelinek 		    pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
39570209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
39580209230bSgjelinek 			goto done;
39590209230bSgjelinek 		}
39600209230bSgjelinek 
39610209230bSgjelinek 		if (pool != NULL &&
39620209230bSgjelinek 		    pool_destroy(pconf, pool) != PO_SUCCESS) {
39630209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
39640209230bSgjelinek 			goto done;
39650209230bSgjelinek 		}
39660209230bSgjelinek 
39670209230bSgjelinek 		/* commit dynamic config */
39680209230bSgjelinek 		if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
39690209230bSgjelinek 			res = zerr_pool(pool_err, err_size, Z_POOL);
39700209230bSgjelinek 	}
39710209230bSgjelinek 
39720209230bSgjelinek done:
39730209230bSgjelinek 	(void) pool_conf_close(pconf);
39740209230bSgjelinek 
39750209230bSgjelinek 	return (res);
39760209230bSgjelinek }
39770209230bSgjelinek 
39780209230bSgjelinek /*
39790209230bSgjelinek  * Destroy any existing tmp pool.
39800209230bSgjelinek  */
39810209230bSgjelinek int
39820209230bSgjelinek zonecfg_destroy_tmp_pool(char *zone_name, char *pool_err, int err_size)
39830209230bSgjelinek {
39840209230bSgjelinek 	int status;
39850209230bSgjelinek 	int res = Z_OK;
39860209230bSgjelinek 	pool_conf_t *pconf;
39870209230bSgjelinek 	pool_t *pool;
39880209230bSgjelinek 	pool_resource_t *pset;
39890209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
39900209230bSgjelinek 
39910209230bSgjelinek 	/* if pools not enabled then nothing to do */
39920209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
39930209230bSgjelinek 		return (Z_OK);
39940209230bSgjelinek 
39950209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
39960209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
39970209230bSgjelinek 
39980209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
39990209230bSgjelinek 
40000209230bSgjelinek 	if (pool_conf_open(pconf, pool_dynamic_location(), PO_RDWR)
40010209230bSgjelinek 	    != PO_SUCCESS) {
40020209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40030209230bSgjelinek 		pool_conf_free(pconf);
40040209230bSgjelinek 		return (res);
40050209230bSgjelinek 	}
40060209230bSgjelinek 
40070209230bSgjelinek 	pool = pool_get_pool(pconf, tmp_name);
40080209230bSgjelinek 	pset = pool_get_resource(pconf, "pset", tmp_name);
40090209230bSgjelinek 
40100209230bSgjelinek 	if (pool == NULL && pset == NULL) {
40110209230bSgjelinek 		/* nothing to destroy, we're done */
40120209230bSgjelinek 		goto done;
40130209230bSgjelinek 	}
40140209230bSgjelinek 
40150209230bSgjelinek 	if (pset != NULL && pool_resource_destroy(pconf, pset) != PO_SUCCESS) {
40160209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40170209230bSgjelinek 		goto done;
40180209230bSgjelinek 	}
40190209230bSgjelinek 
40200209230bSgjelinek 	if (pool != NULL && pool_destroy(pconf, pool) != PO_SUCCESS) {
40210209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40220209230bSgjelinek 		goto done;
40230209230bSgjelinek 	}
40240209230bSgjelinek 
40250209230bSgjelinek 	/* commit dynamic config */
40260209230bSgjelinek 	if (pool_conf_commit(pconf, 0) != PO_SUCCESS)
40270209230bSgjelinek 		res = zerr_pool(pool_err, err_size, Z_POOL);
40280209230bSgjelinek 
40290209230bSgjelinek done:
40300209230bSgjelinek 	(void) pool_conf_close(pconf);
40310209230bSgjelinek 	pool_conf_free(pconf);
40320209230bSgjelinek 
40330209230bSgjelinek 	return (res);
40340209230bSgjelinek }
40350209230bSgjelinek 
40360209230bSgjelinek /*
40370209230bSgjelinek  * Attempt to bind to a tmp pool for this zone.  If there is no tmp pool
40380209230bSgjelinek  * configured, we just return Z_OK.
40390209230bSgjelinek  *
40400209230bSgjelinek  * We either attempt to create the tmp pool for this zone or rebind to an
40410209230bSgjelinek  * existing tmp pool for this zone.
40420209230bSgjelinek  *
40430209230bSgjelinek  * Rebinding is used when a zone with a tmp pool reboots so that we don't have
40440209230bSgjelinek  * to recreate the tmp pool.  To do this we need to be sure we work correctly
40450209230bSgjelinek  * for the following cases:
40460209230bSgjelinek  *
40470209230bSgjelinek  *	- there is an existing, properly configured tmp pool.
40480209230bSgjelinek  *	- zonecfg added tmp pool after zone was booted, must now create.
40490209230bSgjelinek  *	- zonecfg updated tmp pool config after zone was booted, in this case
40500209230bSgjelinek  *	  we destroy the old tmp pool and create a new one.
40510209230bSgjelinek  */
40520209230bSgjelinek int
40530209230bSgjelinek zonecfg_bind_tmp_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
40540209230bSgjelinek     int err_size)
40550209230bSgjelinek {
40560209230bSgjelinek 	struct zone_psettab pset_tab;
40570209230bSgjelinek 	int err;
40580209230bSgjelinek 	int status;
40590209230bSgjelinek 	pool_conf_t *pconf;
40600209230bSgjelinek 	boolean_t exists;
40610209230bSgjelinek 	char zone_name[ZONENAME_MAX];
40620209230bSgjelinek 	char tmp_name[MAX_TMP_POOL_NAME];
40630209230bSgjelinek 
40640209230bSgjelinek 	(void) getzonenamebyid(zoneid, zone_name, sizeof (zone_name));
40650209230bSgjelinek 
40660209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
40670209230bSgjelinek 
40680209230bSgjelinek 	/* if no temporary pool configured, we're done */
40690209230bSgjelinek 	if (err == Z_NO_ENTRY)
40700209230bSgjelinek 		return (Z_OK);
40710209230bSgjelinek 
40720209230bSgjelinek 	/*
40730209230bSgjelinek 	 * importance might not have a value but we need to validate it here,
40740209230bSgjelinek 	 * so set the default.
40750209230bSgjelinek 	 */
40760209230bSgjelinek 	if (pset_tab.zone_importance[0] == '\0')
40770209230bSgjelinek 		(void) strlcpy(pset_tab.zone_importance, "1",
40780209230bSgjelinek 		    sizeof (pset_tab.zone_importance));
40790209230bSgjelinek 
40800209230bSgjelinek 	/* if pools not enabled, enable them now */
40810209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
40820209230bSgjelinek 		if (pool_set_status(POOL_ENABLED) != PO_SUCCESS)
40830209230bSgjelinek 			return (Z_POOL_ENABLE);
40840209230bSgjelinek 	}
40850209230bSgjelinek 
40860209230bSgjelinek 	if ((pconf = pool_conf_alloc()) == NULL)
40870209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
40880209230bSgjelinek 
40890209230bSgjelinek 	(void) snprintf(tmp_name, sizeof (tmp_name), TMP_POOL_NAME, zone_name);
40900209230bSgjelinek 
40910209230bSgjelinek 	/*
40920209230bSgjelinek 	 * Check if a valid tmp pool/pset already exists.  If so, we just
40930209230bSgjelinek 	 * reuse it.
40940209230bSgjelinek 	 */
40950209230bSgjelinek 	if ((err = verify_del_tmp_pool(pconf, tmp_name, pool_err, err_size,
40960209230bSgjelinek 	    &pset_tab, &exists)) != Z_OK) {
40970209230bSgjelinek 		pool_conf_free(pconf);
40980209230bSgjelinek 		return (err);
40990209230bSgjelinek 	}
41000209230bSgjelinek 
41010209230bSgjelinek 	if (!exists)
41020209230bSgjelinek 		err = create_tmp_pool(pool_err, err_size, pconf, tmp_name,
41030209230bSgjelinek 		    &pset_tab);
41040209230bSgjelinek 
41050209230bSgjelinek 	pool_conf_free(pconf);
41060209230bSgjelinek 
41070209230bSgjelinek 	if (err != Z_OK)
41080209230bSgjelinek 		return (err);
41090209230bSgjelinek 
41100209230bSgjelinek 	/* Bind the zone to the pool. */
41110209230bSgjelinek 	if (pool_set_binding(tmp_name, P_ZONEID, zoneid) != PO_SUCCESS)
41120209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL_BIND));
41130209230bSgjelinek 
41140209230bSgjelinek 	return (Z_OK);
41150209230bSgjelinek }
41160209230bSgjelinek 
41170209230bSgjelinek /*
41180209230bSgjelinek  * Attempt to bind to a permanent pool for this zone.  If there is no
41190209230bSgjelinek  * permanent pool configured, we just return Z_OK.
41200209230bSgjelinek  */
41210209230bSgjelinek int
41220209230bSgjelinek zonecfg_bind_pool(zone_dochandle_t handle, zoneid_t zoneid, char *pool_err,
41230209230bSgjelinek     int err_size)
41240209230bSgjelinek {
41250209230bSgjelinek 	pool_conf_t *poolconf;
41260209230bSgjelinek 	pool_t *pool;
41270209230bSgjelinek 	char poolname[MAXPATHLEN];
41280209230bSgjelinek 	int status;
41290209230bSgjelinek 	int error;
41300209230bSgjelinek 
41310209230bSgjelinek 	/*
41320209230bSgjelinek 	 * Find the pool mentioned in the zone configuration, and bind to it.
41330209230bSgjelinek 	 */
41340209230bSgjelinek 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
41350209230bSgjelinek 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
41360209230bSgjelinek 		/*
41370209230bSgjelinek 		 * The property is not set on the zone, so the pool
41380209230bSgjelinek 		 * should be bound to the default pool.  But that's
41390209230bSgjelinek 		 * already done by the kernel, so we can just return.
41400209230bSgjelinek 		 */
41410209230bSgjelinek 		return (Z_OK);
41420209230bSgjelinek 	}
41430209230bSgjelinek 	if (error != Z_OK) {
41440209230bSgjelinek 		/*
41450209230bSgjelinek 		 * Not an error, even though it shouldn't be happening.
41460209230bSgjelinek 		 */
41470209230bSgjelinek 		return (Z_OK);
41480209230bSgjelinek 	}
41490209230bSgjelinek 	/*
41500209230bSgjelinek 	 * Don't do anything if pools aren't enabled.
41510209230bSgjelinek 	 */
41520209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
41530209230bSgjelinek 		return (Z_POOLS_NOT_ACTIVE);
41540209230bSgjelinek 
41550209230bSgjelinek 	/*
41560209230bSgjelinek 	 * Try to provide a sane error message if the requested pool doesn't
41570209230bSgjelinek 	 * exist.
41580209230bSgjelinek 	 */
41590209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
41600209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
41610209230bSgjelinek 
41620209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
41630209230bSgjelinek 	    PO_SUCCESS) {
41640209230bSgjelinek 		pool_conf_free(poolconf);
41650209230bSgjelinek 		return (zerr_pool(pool_err, err_size, Z_POOL));
41660209230bSgjelinek 	}
41670209230bSgjelinek 	pool = pool_get_pool(poolconf, poolname);
41680209230bSgjelinek 	(void) pool_conf_close(poolconf);
41690209230bSgjelinek 	pool_conf_free(poolconf);
41700209230bSgjelinek 	if (pool == NULL)
41710209230bSgjelinek 		return (Z_NO_POOL);
41720209230bSgjelinek 
41730209230bSgjelinek 	/*
41740209230bSgjelinek 	 * Bind the zone to the pool.
41750209230bSgjelinek 	 */
41760209230bSgjelinek 	if (pool_set_binding(poolname, P_ZONEID, zoneid) != PO_SUCCESS) {
41770209230bSgjelinek 		/* if bind fails, return poolname for the error msg */
41780209230bSgjelinek 		(void) strlcpy(pool_err, poolname, err_size);
41790209230bSgjelinek 		return (Z_POOL_BIND);
41800209230bSgjelinek 	}
41810209230bSgjelinek 
41820209230bSgjelinek 	return (Z_OK);
41830209230bSgjelinek }
41840209230bSgjelinek 
41850209230bSgjelinek 
41860209230bSgjelinek static boolean_t
41870209230bSgjelinek svc_enabled(char *svc_name)
41880209230bSgjelinek {
41890209230bSgjelinek 	scf_simple_prop_t	*prop;
41900209230bSgjelinek 	boolean_t		found = B_FALSE;
41910209230bSgjelinek 
41920209230bSgjelinek 	prop = scf_simple_prop_get(NULL, svc_name, SCF_PG_GENERAL,
41930209230bSgjelinek 	    SCF_PROPERTY_ENABLED);
41940209230bSgjelinek 
41950209230bSgjelinek 	if (scf_simple_prop_numvalues(prop) == 1 &&
41960209230bSgjelinek 	    *scf_simple_prop_next_boolean(prop) != 0)
41970209230bSgjelinek 		found = B_TRUE;
41980209230bSgjelinek 
41990209230bSgjelinek 	scf_simple_prop_free(prop);
42000209230bSgjelinek 
42010209230bSgjelinek 	return (found);
42020209230bSgjelinek }
42030209230bSgjelinek 
42040209230bSgjelinek /*
42050209230bSgjelinek  * If the zone has capped-memory, make sure the rcap service is enabled.
42060209230bSgjelinek  */
42070209230bSgjelinek int
42080209230bSgjelinek zonecfg_enable_rcapd(char *err, int size)
42090209230bSgjelinek {
42100209230bSgjelinek 	if (!svc_enabled(RCAP_SERVICE) &&
42110209230bSgjelinek 	    smf_enable_instance(RCAP_SERVICE, 0) == -1) {
42120209230bSgjelinek 		(void) strlcpy(err, scf_strerror(scf_error()), size);
42130209230bSgjelinek 		return (Z_SYSTEM);
42140209230bSgjelinek 	}
42150209230bSgjelinek 
42160209230bSgjelinek 	return (Z_OK);
42170209230bSgjelinek }
42180209230bSgjelinek 
42190209230bSgjelinek /*
42200209230bSgjelinek  * Return true if pset has cpu range specified and poold is not enabled.
42210209230bSgjelinek  */
42220209230bSgjelinek boolean_t
42230209230bSgjelinek zonecfg_warn_poold(zone_dochandle_t handle)
42240209230bSgjelinek {
42250209230bSgjelinek 	struct zone_psettab pset_tab;
42260209230bSgjelinek 	int min, max;
42270209230bSgjelinek 	int err;
42280209230bSgjelinek 
42290209230bSgjelinek 	err = zonecfg_lookup_pset(handle, &pset_tab);
42300209230bSgjelinek 
42310209230bSgjelinek 	/* if no temporary pool configured, we're done */
42320209230bSgjelinek 	if (err == Z_NO_ENTRY)
42330209230bSgjelinek 		return (B_FALSE);
42340209230bSgjelinek 
42350209230bSgjelinek 	min = atoi(pset_tab.zone_ncpu_min);
42360209230bSgjelinek 	max = atoi(pset_tab.zone_ncpu_max);
42370209230bSgjelinek 
42380209230bSgjelinek 	/* range not specified, no need for poold */
42390209230bSgjelinek 	if (min == max)
42400209230bSgjelinek 		return (B_FALSE);
42410209230bSgjelinek 
42420209230bSgjelinek 	/* we have a range, check if poold service is enabled */
42430209230bSgjelinek 	if (svc_enabled(POOLD_SERVICE))
42440209230bSgjelinek 		return (B_FALSE);
42450209230bSgjelinek 
42460209230bSgjelinek 	return (B_TRUE);
42470209230bSgjelinek }
42480209230bSgjelinek 
42490209230bSgjelinek static int
42500209230bSgjelinek get_pool_sched_class(char *poolname, char *class, int clsize)
42510209230bSgjelinek {
42520209230bSgjelinek 	int status;
42530209230bSgjelinek 	pool_conf_t *poolconf;
42540209230bSgjelinek 	pool_t *pool;
42550209230bSgjelinek 	pool_elem_t *pe;
42560209230bSgjelinek 	pool_value_t *pv = pool_value_alloc();
42570209230bSgjelinek 	const char *sched_str;
42580209230bSgjelinek 
42590209230bSgjelinek 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED)
42600209230bSgjelinek 		return (Z_NO_POOL);
42610209230bSgjelinek 
42620209230bSgjelinek 	if ((poolconf = pool_conf_alloc()) == NULL)
42630209230bSgjelinek 		return (Z_NO_POOL);
42640209230bSgjelinek 
42650209230bSgjelinek 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
42660209230bSgjelinek 	    PO_SUCCESS) {
42670209230bSgjelinek 		pool_conf_free(poolconf);
42680209230bSgjelinek 		return (Z_NO_POOL);
42690209230bSgjelinek 	}
42700209230bSgjelinek 
42710209230bSgjelinek 	if ((pool = pool_get_pool(poolconf, poolname)) == NULL) {
42720209230bSgjelinek 		(void) pool_conf_close(poolconf);
42730209230bSgjelinek 		pool_conf_free(poolconf);
42740209230bSgjelinek 		return (Z_NO_POOL);
42750209230bSgjelinek 	}
42760209230bSgjelinek 
42770209230bSgjelinek 	pe = pool_to_elem(poolconf, pool);
42780209230bSgjelinek 	if (pool_get_property(poolconf, pe, "pool.scheduler", pv)
42790209230bSgjelinek 	    != POC_INVAL) {
42800209230bSgjelinek 		(void) pool_value_get_string(pv, &sched_str);
42810209230bSgjelinek 		if (strlcpy(class, sched_str, clsize) >= clsize)
42820209230bSgjelinek 			return (Z_TOO_BIG);
42830209230bSgjelinek 	}
42840209230bSgjelinek 
42850209230bSgjelinek 	(void) pool_conf_close(poolconf);
42860209230bSgjelinek 	pool_conf_free(poolconf);
42870209230bSgjelinek 	return (Z_OK);
42880209230bSgjelinek }
42890209230bSgjelinek 
42900209230bSgjelinek /*
42910209230bSgjelinek  * Get the default scheduling class for the zone.  This will either be the
42920209230bSgjelinek  * class set on the zone's pool or the system default scheduling class.
42930209230bSgjelinek  */
42940209230bSgjelinek int
42950209230bSgjelinek zonecfg_get_dflt_sched_class(zone_dochandle_t handle, char *class, int clsize)
42960209230bSgjelinek {
42970209230bSgjelinek 	char poolname[MAXPATHLEN];
42980209230bSgjelinek 
42990209230bSgjelinek 	if (zonecfg_get_pool(handle, poolname, sizeof (poolname)) == Z_OK) {
43000209230bSgjelinek 		/* check if the zone's pool specified a sched class */
43010209230bSgjelinek 		if (get_pool_sched_class(poolname, class, clsize) == Z_OK)
43020209230bSgjelinek 			return (Z_OK);
43030209230bSgjelinek 	}
43040209230bSgjelinek 
43050209230bSgjelinek 	if (priocntl(0, 0, PC_GETDFLCL, class, (uint64_t)clsize) == -1)
43060209230bSgjelinek 		return (Z_TOO_BIG);
43070209230bSgjelinek 
43080209230bSgjelinek 	return (Z_OK);
43090209230bSgjelinek }
43100209230bSgjelinek 
43117c478bd9Sstevel@tonic-gate int
43127c478bd9Sstevel@tonic-gate zonecfg_setfsent(zone_dochandle_t handle)
43137c478bd9Sstevel@tonic-gate {
43147c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
43157c478bd9Sstevel@tonic-gate }
43167c478bd9Sstevel@tonic-gate 
43177c478bd9Sstevel@tonic-gate int
43187c478bd9Sstevel@tonic-gate zonecfg_getfsent(zone_dochandle_t handle, struct zone_fstab *tabptr)
43197c478bd9Sstevel@tonic-gate {
43207c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, options;
43217c478bd9Sstevel@tonic-gate 	char options_str[MAX_MNTOPT_STR];
43227c478bd9Sstevel@tonic-gate 	int err;
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 	if (handle == NULL)
43257c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
43267c478bd9Sstevel@tonic-gate 
43277c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
43287c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43297c478bd9Sstevel@tonic-gate 
43307c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
43317c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_FS))
43327c478bd9Sstevel@tonic-gate 			break;
43337c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
43347c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43357c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
43367c478bd9Sstevel@tonic-gate 	}
43377c478bd9Sstevel@tonic-gate 
43387c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_SPECIAL, tabptr->zone_fs_special,
43397c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_special))) != Z_OK) {
43407c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43417c478bd9Sstevel@tonic-gate 		return (err);
43427c478bd9Sstevel@tonic-gate 	}
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_RAW, tabptr->zone_fs_raw,
43457c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_raw))) != Z_OK) {
43467c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43477c478bd9Sstevel@tonic-gate 		return (err);
43487c478bd9Sstevel@tonic-gate 	}
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
43517c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
43527c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43537c478bd9Sstevel@tonic-gate 		return (err);
43547c478bd9Sstevel@tonic-gate 	}
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_fs_type,
43577c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_type))) != Z_OK) {
43587c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
43597c478bd9Sstevel@tonic-gate 		return (err);
43607c478bd9Sstevel@tonic-gate 	}
43617c478bd9Sstevel@tonic-gate 
43627c478bd9Sstevel@tonic-gate 	/* OK for options to be NULL */
43637c478bd9Sstevel@tonic-gate 	tabptr->zone_fs_options = NULL;
43647c478bd9Sstevel@tonic-gate 	for (options = cur->xmlChildrenNode; options != NULL;
43657c478bd9Sstevel@tonic-gate 	    options = options->next) {
43667c478bd9Sstevel@tonic-gate 		if (fetchprop(options, DTD_ATTR_NAME, options_str,
43677c478bd9Sstevel@tonic-gate 		    sizeof (options_str)) != Z_OK)
43687c478bd9Sstevel@tonic-gate 			break;
43697c478bd9Sstevel@tonic-gate 		if (zonecfg_add_fs_option(tabptr, options_str) != Z_OK)
43707c478bd9Sstevel@tonic-gate 			break;
43717c478bd9Sstevel@tonic-gate 	}
43727c478bd9Sstevel@tonic-gate 
43737c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
43747c478bd9Sstevel@tonic-gate 	return (Z_OK);
43757c478bd9Sstevel@tonic-gate }
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate int
43787c478bd9Sstevel@tonic-gate zonecfg_endfsent(zone_dochandle_t handle)
43797c478bd9Sstevel@tonic-gate {
43807c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
43817c478bd9Sstevel@tonic-gate }
43827c478bd9Sstevel@tonic-gate 
43837c478bd9Sstevel@tonic-gate int
43847c478bd9Sstevel@tonic-gate zonecfg_setipdent(zone_dochandle_t handle)
43857c478bd9Sstevel@tonic-gate {
43867c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
43877c478bd9Sstevel@tonic-gate }
43887c478bd9Sstevel@tonic-gate 
43897c478bd9Sstevel@tonic-gate int
43907c478bd9Sstevel@tonic-gate zonecfg_getipdent(zone_dochandle_t handle, struct zone_fstab *tabptr)
43917c478bd9Sstevel@tonic-gate {
43927c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
43937c478bd9Sstevel@tonic-gate 	int err;
43947c478bd9Sstevel@tonic-gate 
43957c478bd9Sstevel@tonic-gate 	if (handle == NULL)
43967c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
43977c478bd9Sstevel@tonic-gate 
43987c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
43997c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44027c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_IPD))
44037c478bd9Sstevel@tonic-gate 			break;
44047c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
44057c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44067c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44077c478bd9Sstevel@tonic-gate 	}
44087c478bd9Sstevel@tonic-gate 
44097c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_DIR, tabptr->zone_fs_dir,
44107c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_fs_dir))) != Z_OK) {
44117c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44127c478bd9Sstevel@tonic-gate 		return (err);
44137c478bd9Sstevel@tonic-gate 	}
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
44167c478bd9Sstevel@tonic-gate 	return (Z_OK);
44177c478bd9Sstevel@tonic-gate }
44187c478bd9Sstevel@tonic-gate 
44197c478bd9Sstevel@tonic-gate int
44207c478bd9Sstevel@tonic-gate zonecfg_endipdent(zone_dochandle_t handle)
44217c478bd9Sstevel@tonic-gate {
44227c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
44237c478bd9Sstevel@tonic-gate }
44247c478bd9Sstevel@tonic-gate 
44257c478bd9Sstevel@tonic-gate int
44267c478bd9Sstevel@tonic-gate zonecfg_setnwifent(zone_dochandle_t handle)
44277c478bd9Sstevel@tonic-gate {
44287c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
44297c478bd9Sstevel@tonic-gate }
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate int
44327c478bd9Sstevel@tonic-gate zonecfg_getnwifent(zone_dochandle_t handle, struct zone_nwiftab *tabptr)
44337c478bd9Sstevel@tonic-gate {
44347c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
44357c478bd9Sstevel@tonic-gate 	int err;
44367c478bd9Sstevel@tonic-gate 
44377c478bd9Sstevel@tonic-gate 	if (handle == NULL)
44387c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44397c478bd9Sstevel@tonic-gate 
44407c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
44417c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44427c478bd9Sstevel@tonic-gate 
44437c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44447c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_NET))
44457c478bd9Sstevel@tonic-gate 			break;
44467c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
44477c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44487c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44497c478bd9Sstevel@tonic-gate 	}
44507c478bd9Sstevel@tonic-gate 
44517c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_ADDRESS, tabptr->zone_nwif_address,
44527c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_address))) != Z_OK) {
44537c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44547c478bd9Sstevel@tonic-gate 		return (err);
44557c478bd9Sstevel@tonic-gate 	}
44567c478bd9Sstevel@tonic-gate 
44577c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_PHYSICAL, tabptr->zone_nwif_physical,
44587c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_nwif_physical))) != Z_OK) {
44597c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
44607c478bd9Sstevel@tonic-gate 		return (err);
44617c478bd9Sstevel@tonic-gate 	}
44627c478bd9Sstevel@tonic-gate 
4463de860bd9Sgfaden 	if ((err = fetchprop(cur, DTD_ATTR_DEFROUTER,
4464de860bd9Sgfaden 	    tabptr->zone_nwif_defrouter,
4465de860bd9Sgfaden 	    sizeof (tabptr->zone_nwif_defrouter))) != Z_OK) {
4466de860bd9Sgfaden 		handle->zone_dh_cur = handle->zone_dh_top;
4467de860bd9Sgfaden 		return (err);
4468de860bd9Sgfaden 	}
4469de860bd9Sgfaden 
44707c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
44717c478bd9Sstevel@tonic-gate 	return (Z_OK);
44727c478bd9Sstevel@tonic-gate }
44737c478bd9Sstevel@tonic-gate 
44747c478bd9Sstevel@tonic-gate int
44757c478bd9Sstevel@tonic-gate zonecfg_endnwifent(zone_dochandle_t handle)
44767c478bd9Sstevel@tonic-gate {
44777c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
44787c478bd9Sstevel@tonic-gate }
44797c478bd9Sstevel@tonic-gate 
44807c478bd9Sstevel@tonic-gate int
44817c478bd9Sstevel@tonic-gate zonecfg_setdevent(zone_dochandle_t handle)
44827c478bd9Sstevel@tonic-gate {
44837c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
44847c478bd9Sstevel@tonic-gate }
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate int
44877c478bd9Sstevel@tonic-gate zonecfg_getdevent(zone_dochandle_t handle, struct zone_devtab *tabptr)
44887c478bd9Sstevel@tonic-gate {
44897c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
44907c478bd9Sstevel@tonic-gate 	int err;
44917c478bd9Sstevel@tonic-gate 
44927c478bd9Sstevel@tonic-gate 	if (handle == NULL)
44937c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
44947c478bd9Sstevel@tonic-gate 
44957c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
44967c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
44977c478bd9Sstevel@tonic-gate 
44987c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
44997c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEVICE))
45007c478bd9Sstevel@tonic-gate 			break;
45017c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
45027c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45037c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45047c478bd9Sstevel@tonic-gate 	}
45057c478bd9Sstevel@tonic-gate 
45067c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_MATCH, tabptr->zone_dev_match,
45077c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_dev_match))) != Z_OK) {
45087c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45097c478bd9Sstevel@tonic-gate 		return (err);
45107c478bd9Sstevel@tonic-gate 	}
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45137c478bd9Sstevel@tonic-gate 	return (Z_OK);
45147c478bd9Sstevel@tonic-gate }
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate int
45177c478bd9Sstevel@tonic-gate zonecfg_enddevent(zone_dochandle_t handle)
45187c478bd9Sstevel@tonic-gate {
45197c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45207c478bd9Sstevel@tonic-gate }
45217c478bd9Sstevel@tonic-gate 
45227c478bd9Sstevel@tonic-gate int
45237c478bd9Sstevel@tonic-gate zonecfg_setrctlent(zone_dochandle_t handle)
45247c478bd9Sstevel@tonic-gate {
45257c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
45267c478bd9Sstevel@tonic-gate }
45277c478bd9Sstevel@tonic-gate 
45287c478bd9Sstevel@tonic-gate int
45297c478bd9Sstevel@tonic-gate zonecfg_getrctlent(zone_dochandle_t handle, struct zone_rctltab *tabptr)
45307c478bd9Sstevel@tonic-gate {
45317c478bd9Sstevel@tonic-gate 	xmlNodePtr cur, val;
45327c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
45337c478bd9Sstevel@tonic-gate 	int err;
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 	if (handle == NULL)
45367c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
45397c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45407c478bd9Sstevel@tonic-gate 
45417c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
45427c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_RCTL))
45437c478bd9Sstevel@tonic-gate 			break;
45447c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
45457c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45467c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
45477c478bd9Sstevel@tonic-gate 	}
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_rctl_name,
45507c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_rctl_name))) != Z_OK) {
45517c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
45527c478bd9Sstevel@tonic-gate 		return (err);
45537c478bd9Sstevel@tonic-gate 	}
45547c478bd9Sstevel@tonic-gate 
45557c478bd9Sstevel@tonic-gate 	tabptr->zone_rctl_valptr = NULL;
45567c478bd9Sstevel@tonic-gate 	for (val = cur->xmlChildrenNode; val != NULL; val = val->next) {
45577c478bd9Sstevel@tonic-gate 		valptr = (struct zone_rctlvaltab *)malloc(
45587c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctlvaltab));
45597c478bd9Sstevel@tonic-gate 		if (valptr == NULL)
45607c478bd9Sstevel@tonic-gate 			return (Z_NOMEM);
45617c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_PRIV, valptr->zone_rctlval_priv,
45627c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_priv)) != Z_OK)
45637c478bd9Sstevel@tonic-gate 			break;
45647c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_LIMIT, valptr->zone_rctlval_limit,
45657c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_limit)) != Z_OK)
45667c478bd9Sstevel@tonic-gate 			break;
45677c478bd9Sstevel@tonic-gate 		if (fetchprop(val, DTD_ATTR_ACTION, valptr->zone_rctlval_action,
45687c478bd9Sstevel@tonic-gate 		    sizeof (valptr->zone_rctlval_action)) != Z_OK)
45697c478bd9Sstevel@tonic-gate 			break;
45707c478bd9Sstevel@tonic-gate 		if (zonecfg_add_rctl_value(tabptr, valptr) != Z_OK)
45717c478bd9Sstevel@tonic-gate 			break;
45727c478bd9Sstevel@tonic-gate 	}
45737c478bd9Sstevel@tonic-gate 
45747c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
45757c478bd9Sstevel@tonic-gate 	return (Z_OK);
45767c478bd9Sstevel@tonic-gate }
45777c478bd9Sstevel@tonic-gate 
45787c478bd9Sstevel@tonic-gate int
45797c478bd9Sstevel@tonic-gate zonecfg_endrctlent(zone_dochandle_t handle)
45807c478bd9Sstevel@tonic-gate {
45817c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
45827c478bd9Sstevel@tonic-gate }
45837c478bd9Sstevel@tonic-gate 
45847c478bd9Sstevel@tonic-gate int
45857c478bd9Sstevel@tonic-gate zonecfg_setattrent(zone_dochandle_t handle)
45867c478bd9Sstevel@tonic-gate {
45877c478bd9Sstevel@tonic-gate 	return (zonecfg_setent(handle));
45887c478bd9Sstevel@tonic-gate }
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate int
45917c478bd9Sstevel@tonic-gate zonecfg_getattrent(zone_dochandle_t handle, struct zone_attrtab *tabptr)
45927c478bd9Sstevel@tonic-gate {
45937c478bd9Sstevel@tonic-gate 	xmlNodePtr cur;
45947c478bd9Sstevel@tonic-gate 	int err;
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate 	if (handle == NULL)
45977c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
45987c478bd9Sstevel@tonic-gate 
45997c478bd9Sstevel@tonic-gate 	if ((cur = handle->zone_dh_cur) == NULL)
46007c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46017c478bd9Sstevel@tonic-gate 
46027c478bd9Sstevel@tonic-gate 	for (; cur != NULL; cur = cur->next)
46037c478bd9Sstevel@tonic-gate 		if (!xmlStrcmp(cur->name, DTD_ELEM_ATTR))
46047c478bd9Sstevel@tonic-gate 			break;
46057c478bd9Sstevel@tonic-gate 	if (cur == NULL) {
46067c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46077c478bd9Sstevel@tonic-gate 		return (Z_NO_ENTRY);
46087c478bd9Sstevel@tonic-gate 	}
46097c478bd9Sstevel@tonic-gate 
46107c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_attr_name,
46117c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_name))) != Z_OK) {
46127c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46137c478bd9Sstevel@tonic-gate 		return (err);
46147c478bd9Sstevel@tonic-gate 	}
46157c478bd9Sstevel@tonic-gate 
46167c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_TYPE, tabptr->zone_attr_type,
46177c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_type))) != Z_OK) {
46187c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46197c478bd9Sstevel@tonic-gate 		return (err);
46207c478bd9Sstevel@tonic-gate 	}
46217c478bd9Sstevel@tonic-gate 
46227c478bd9Sstevel@tonic-gate 	if ((err = fetchprop(cur, DTD_ATTR_VALUE, tabptr->zone_attr_value,
46237c478bd9Sstevel@tonic-gate 	    sizeof (tabptr->zone_attr_value))) != Z_OK) {
46247c478bd9Sstevel@tonic-gate 		handle->zone_dh_cur = handle->zone_dh_top;
46257c478bd9Sstevel@tonic-gate 		return (err);
46267c478bd9Sstevel@tonic-gate 	}
46277c478bd9Sstevel@tonic-gate 
46287c478bd9Sstevel@tonic-gate 	handle->zone_dh_cur = cur->next;
46297c478bd9Sstevel@tonic-gate 	return (Z_OK);
46307c478bd9Sstevel@tonic-gate }
46317c478bd9Sstevel@tonic-gate 
46327c478bd9Sstevel@tonic-gate int
46337c478bd9Sstevel@tonic-gate zonecfg_endattrent(zone_dochandle_t handle)
46347c478bd9Sstevel@tonic-gate {
46357c478bd9Sstevel@tonic-gate 	return (zonecfg_endent(handle));
46367c478bd9Sstevel@tonic-gate }
46377c478bd9Sstevel@tonic-gate 
4638ffbafc53Scomay /*
4639ffbafc53Scomay  * The privileges available on the system and described in privileges(5)
46409acbbeafSnn35248  * fall into four categories with respect to non-global zones:
46419acbbeafSnn35248  *
46429acbbeafSnn35248  *      Default set of privileges considered safe for all non-global
46439acbbeafSnn35248  *      zones.  These privileges are "safe" in the sense that a
46449acbbeafSnn35248  *      privileged process in the zone cannot affect processes in any
46459acbbeafSnn35248  *      other zone on the system.
46469acbbeafSnn35248  *
46479acbbeafSnn35248  *      Set of privileges not currently permitted within a non-global
46489acbbeafSnn35248  *      zone.  These privileges are considered by default, "unsafe,"
46499acbbeafSnn35248  *      and include ones which affect global resources (such as the
46509acbbeafSnn35248  *      system clock or physical memory) or are overly broad and cover
46519acbbeafSnn35248  *      more than one mechanism in the system.  In other cases, there
46529acbbeafSnn35248  *      has not been sufficient virtualization in the parts of the
46539acbbeafSnn35248  *      system the privilege covers to allow its use within a
46549acbbeafSnn35248  *      non-global zone.
46559acbbeafSnn35248  *
46569acbbeafSnn35248  *      Set of privileges required in order to get a zone booted and
46579acbbeafSnn35248  *      init(1M) started.  These cannot be removed from the zone's
46589acbbeafSnn35248  *      privilege set.
46599acbbeafSnn35248  *
46609acbbeafSnn35248  * All other privileges are optional and are potentially useful for
4661ffbafc53Scomay  * processes executing inside a non-global zone.
4662ffbafc53Scomay  *
4663ffbafc53Scomay  * When privileges are added to the system, a determination needs to be
4664ffbafc53Scomay  * made as to which category the privilege belongs to.  Ideally,
4665ffbafc53Scomay  * privileges should be fine-grained enough and the mechanisms they cover
4666ffbafc53Scomay  * virtualized enough so that they can be made available to non-global
4667ffbafc53Scomay  * zones.
4668ffbafc53Scomay  */
4669ffbafc53Scomay 
4670ffbafc53Scomay /*
4671ffbafc53Scomay  * Define some of the tokens that priv_str_to_set(3C) recognizes.  Since
4672ffbafc53Scomay  * the privilege string separator can be any character, although it is
4673ffbafc53Scomay  * usually a comma character, define these here as well in the event that
4674ffbafc53Scomay  * they change or are augmented in the future.
4675ffbafc53Scomay  */
4676ffbafc53Scomay #define	BASIC_TOKEN		"basic"
4677ffbafc53Scomay #define	DEFAULT_TOKEN		"default"
4678ffbafc53Scomay #define	ZONE_TOKEN		"zone"
4679ffbafc53Scomay #define	TOKEN_PRIV_CHAR		','
4680ffbafc53Scomay #define	TOKEN_PRIV_STR		","
4681ffbafc53Scomay 
46829acbbeafSnn35248 typedef struct priv_node {
46839acbbeafSnn35248 	struct priv_node	*pn_next;	/* Next privilege */
46849acbbeafSnn35248 	char			*pn_priv;	/* Privileges name */
46859acbbeafSnn35248 } priv_node_t;
46869acbbeafSnn35248 
46879acbbeafSnn35248 /* Privileges lists can differ across brands */
46889acbbeafSnn35248 typedef struct priv_lists {
46899acbbeafSnn35248 	/* Privileges considered safe for all non-global zones of a brand */
46909acbbeafSnn35248 	struct priv_node	*pl_default;
46919acbbeafSnn35248 
46929acbbeafSnn35248 	/* Privileges not permitted for all non-global zones of a brand */
46939acbbeafSnn35248 	struct priv_node	*pl_prohibited;
46949acbbeafSnn35248 
46959acbbeafSnn35248 	/* Privileges required for all non-global zones of a brand */
46969acbbeafSnn35248 	struct priv_node	*pl_required;
4697bf1d7e28Sdh155122 
4698bf1d7e28Sdh155122 	/*
4699bf1d7e28Sdh155122 	 * ip-type of the zone these privileges lists apply to.
4700bf1d7e28Sdh155122 	 * It is used to pass ip-type to the callback function,
4701bf1d7e28Sdh155122 	 * priv_lists_cb, which has no way of getting the ip-type.
4702bf1d7e28Sdh155122 	 */
4703bf1d7e28Sdh155122 	const char		*pl_iptype;
47049acbbeafSnn35248 } priv_lists_t;
47059acbbeafSnn35248 
47069acbbeafSnn35248 static int
4707bf1d7e28Sdh155122 priv_lists_cb(void *data, priv_iter_t *priv_iter)
47087c478bd9Sstevel@tonic-gate {
47099acbbeafSnn35248 	priv_lists_t *plp = (priv_lists_t *)data;
47109acbbeafSnn35248 	priv_node_t *pnp;
47119acbbeafSnn35248 
4712bf1d7e28Sdh155122 	/* Skip this privilege if ip-type does not match */
4713bf1d7e28Sdh155122 	if ((strcmp(priv_iter->pi_iptype, "all") != 0) &&
4714bf1d7e28Sdh155122 	    (strcmp(priv_iter->pi_iptype, plp->pl_iptype) != 0))
4715bf1d7e28Sdh155122 		return (0);
4716bf1d7e28Sdh155122 
47179acbbeafSnn35248 	/* Allocate a new priv list node. */
47189acbbeafSnn35248 	if ((pnp = malloc(sizeof (*pnp))) == NULL)
47199acbbeafSnn35248 		return (-1);
4720bf1d7e28Sdh155122 	if ((pnp->pn_priv = strdup(priv_iter->pi_name)) == NULL) {
47219acbbeafSnn35248 		free(pnp);
47229acbbeafSnn35248 		return (-1);
47239acbbeafSnn35248 	}
47249acbbeafSnn35248 
47259acbbeafSnn35248 	/* Insert the new priv list node into the right list */
4726bf1d7e28Sdh155122 	if (strcmp(priv_iter->pi_set, "default") == 0) {
47279acbbeafSnn35248 		pnp->pn_next = plp->pl_default;
47289acbbeafSnn35248 		plp->pl_default = pnp;
4729bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "prohibited") == 0) {
47309acbbeafSnn35248 		pnp->pn_next = plp->pl_prohibited;
47319acbbeafSnn35248 		plp->pl_prohibited = pnp;
4732bf1d7e28Sdh155122 	} else if (strcmp(priv_iter->pi_set, "required") == 0) {
47339acbbeafSnn35248 		pnp->pn_next = plp->pl_required;
47349acbbeafSnn35248 		plp->pl_required = pnp;
47359acbbeafSnn35248 	} else {
47369acbbeafSnn35248 		free(pnp->pn_priv);
47379acbbeafSnn35248 		free(pnp);
47389acbbeafSnn35248 		return (-1);
47399acbbeafSnn35248 	}
47409acbbeafSnn35248 	return (0);
47419acbbeafSnn35248 }
47429acbbeafSnn35248 
47439acbbeafSnn35248 static void
47449acbbeafSnn35248 priv_lists_destroy(priv_lists_t *plp)
47459acbbeafSnn35248 {
47469acbbeafSnn35248 	priv_node_t *pnp;
47479acbbeafSnn35248 
47489acbbeafSnn35248 	assert(plp != NULL);
47499acbbeafSnn35248 
47509acbbeafSnn35248 	while ((pnp = plp->pl_default) != NULL) {
47519acbbeafSnn35248 		plp->pl_default = pnp->pn_next;
47529acbbeafSnn35248 		free(pnp->pn_priv);
47539acbbeafSnn35248 		free(pnp);
47549acbbeafSnn35248 	}
47559acbbeafSnn35248 	while ((pnp = plp->pl_prohibited) != NULL) {
47569acbbeafSnn35248 		plp->pl_prohibited = pnp->pn_next;
47579acbbeafSnn35248 		free(pnp->pn_priv);
47589acbbeafSnn35248 		free(pnp);
47599acbbeafSnn35248 	}
47609acbbeafSnn35248 	while ((pnp = plp->pl_required) != NULL) {
47619acbbeafSnn35248 		plp->pl_required = pnp->pn_next;
47629acbbeafSnn35248 		free(pnp->pn_priv);
47639acbbeafSnn35248 		free(pnp);
47649acbbeafSnn35248 	}
47659acbbeafSnn35248 	free(plp);
47669acbbeafSnn35248 }
47679acbbeafSnn35248 
47689acbbeafSnn35248 static int
4769bf1d7e28Sdh155122 priv_lists_create(zone_dochandle_t handle, priv_lists_t **plpp,
4770bf1d7e28Sdh155122     const char *curr_iptype)
47719acbbeafSnn35248 {
47729acbbeafSnn35248 	priv_lists_t *plp;
4773123807fbSedp 	brand_handle_t bh;
47749acbbeafSnn35248 	char brand[MAXNAMELEN];
47759acbbeafSnn35248 
47769acbbeafSnn35248 	if (handle != NULL) {
47779acbbeafSnn35248 		if (zonecfg_get_brand(handle, brand, sizeof (brand)) != 0)
47789acbbeafSnn35248 			return (Z_BRAND_ERROR);
47799acbbeafSnn35248 	} else {
47809acbbeafSnn35248 		(void) strlcpy(brand, NATIVE_BRAND_NAME, MAXNAMELEN);
47819acbbeafSnn35248 	}
47829acbbeafSnn35248 
4783123807fbSedp 	if ((bh = brand_open(brand)) == NULL)
47849acbbeafSnn35248 		return (Z_BRAND_ERROR);
47859acbbeafSnn35248 
47869acbbeafSnn35248 	if ((plp = calloc(1, sizeof (priv_lists_t))) == NULL) {
4787123807fbSedp 		brand_close(bh);
47889acbbeafSnn35248 		return (Z_NOMEM);
47899acbbeafSnn35248 	}
47909acbbeafSnn35248 
4791bf1d7e28Sdh155122 	plp->pl_iptype = curr_iptype;
4792bf1d7e28Sdh155122 
47939acbbeafSnn35248 	/* construct the privilege lists */
4794123807fbSedp 	if (brand_config_iter_privilege(bh, priv_lists_cb, plp) != 0) {
47959acbbeafSnn35248 		priv_lists_destroy(plp);
4796123807fbSedp 		brand_close(bh);
47979acbbeafSnn35248 		return (Z_BRAND_ERROR);
47989acbbeafSnn35248 	}
47999acbbeafSnn35248 
4800123807fbSedp 	brand_close(bh);
48019acbbeafSnn35248 	*plpp = plp;
48029acbbeafSnn35248 	return (Z_OK);
48039acbbeafSnn35248 }
48049acbbeafSnn35248 
48059acbbeafSnn35248 static int
48069acbbeafSnn35248 get_default_privset(priv_set_t *privs, priv_lists_t *plp)
48079acbbeafSnn35248 {
48089acbbeafSnn35248 	priv_node_t *pnp;
4809ffbafc53Scomay 	priv_set_t *basic;
48107c478bd9Sstevel@tonic-gate 
4811ffbafc53Scomay 	basic = priv_str_to_set(BASIC_TOKEN, TOKEN_PRIV_STR, NULL);
48127c478bd9Sstevel@tonic-gate 	if (basic == NULL)
4813ffbafc53Scomay 		return (errno == ENOMEM ? Z_NOMEM : Z_INVAL);
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate 	priv_union(basic, privs);
48167c478bd9Sstevel@tonic-gate 	priv_freeset(basic);
48177c478bd9Sstevel@tonic-gate 
48189acbbeafSnn35248 	for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next) {
48199acbbeafSnn35248 		if (priv_addset(privs, pnp->pn_priv) != 0)
48207c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
48217c478bd9Sstevel@tonic-gate 	}
48229acbbeafSnn35248 
48237c478bd9Sstevel@tonic-gate 	return (Z_OK);
48247c478bd9Sstevel@tonic-gate }
48257c478bd9Sstevel@tonic-gate 
48269acbbeafSnn35248 int
4827bf1d7e28Sdh155122 zonecfg_default_privset(priv_set_t *privs, const char *curr_iptype)
48289acbbeafSnn35248 {
48299acbbeafSnn35248 	priv_lists_t *plp;
48309acbbeafSnn35248 	int ret;
48319acbbeafSnn35248 
4832bf1d7e28Sdh155122 	if ((ret = priv_lists_create(NULL, &plp, curr_iptype)) != Z_OK)
48339acbbeafSnn35248 		return (ret);
48349acbbeafSnn35248 	ret = get_default_privset(privs, plp);
48359acbbeafSnn35248 	priv_lists_destroy(plp);
48369acbbeafSnn35248 	return (ret);
48379acbbeafSnn35248 }
48389acbbeafSnn35248 
4839ffbafc53Scomay void
4840ffbafc53Scomay append_priv_token(char *priv, char *str, size_t strlen)
4841ffbafc53Scomay {
4842ffbafc53Scomay 	if (*str != '\0')
4843ffbafc53Scomay 		(void) strlcat(str, TOKEN_PRIV_STR, strlen);
4844ffbafc53Scomay 	(void) strlcat(str, priv, strlen);
4845ffbafc53Scomay }
4846ffbafc53Scomay 
4847ffbafc53Scomay /*
4848ffbafc53Scomay  * Verify that the supplied string is a valid privilege limit set for a
4849ffbafc53Scomay  * non-global zone.  This string must not only be acceptable to
4850ffbafc53Scomay  * priv_str_to_set(3C) which parses it, but it also must resolve to a
4851ffbafc53Scomay  * privilege set that includes certain required privileges and lacks
4852ffbafc53Scomay  * certain prohibited privileges.
4853ffbafc53Scomay  */
4854ffbafc53Scomay static int
4855ffbafc53Scomay verify_privset(char *privbuf, priv_set_t *privs, char **privname,
48569acbbeafSnn35248     boolean_t add_default, priv_lists_t *plp)
4857ffbafc53Scomay {
48589acbbeafSnn35248 	priv_node_t *pnp;
48599acbbeafSnn35248 	char *tmp, *cp, *lasts;
4860ffbafc53Scomay 	size_t len;
4861ffbafc53Scomay 	priv_set_t *mergeset;
4862ffbafc53Scomay 	const char *token;
4863ffbafc53Scomay 
4864ffbafc53Scomay 	/*
4865ffbafc53Scomay 	 * The verification of the privilege string occurs in several
4866ffbafc53Scomay 	 * phases.  In the first phase, the supplied string is scanned for
4867ffbafc53Scomay 	 * the ZONE_TOKEN token which is not support as part of the
4868ffbafc53Scomay 	 * "limitpriv" property.
4869ffbafc53Scomay 	 *
4870ffbafc53Scomay 	 * Duplicate the supplied privilege string since strtok_r(3C)
4871ffbafc53Scomay 	 * tokenizes its input by null-terminating the tokens.
4872ffbafc53Scomay 	 */
4873ffbafc53Scomay 	if ((tmp = strdup(privbuf)) == NULL)
4874ffbafc53Scomay 		return (Z_NOMEM);
4875ffbafc53Scomay 	for (cp = strtok_r(tmp, TOKEN_PRIV_STR, &lasts); cp != NULL;
4876ffbafc53Scomay 	    cp = strtok_r(NULL, TOKEN_PRIV_STR, &lasts)) {
4877ffbafc53Scomay 		if (strcmp(cp, ZONE_TOKEN) == 0) {
4878ffbafc53Scomay 			free(tmp);
4879ffbafc53Scomay 			if ((*privname = strdup(ZONE_TOKEN)) == NULL)
4880ffbafc53Scomay 				return (Z_NOMEM);
4881ffbafc53Scomay 			else
4882ffbafc53Scomay 				return (Z_PRIV_UNKNOWN);
4883ffbafc53Scomay 		}
4884ffbafc53Scomay 	}
4885ffbafc53Scomay 	free(tmp);
4886ffbafc53Scomay 
4887ffbafc53Scomay 	if (add_default) {
4888ffbafc53Scomay 		/*
4889ffbafc53Scomay 		 * If DEFAULT_TOKEN was specified, a string needs to be
4890ffbafc53Scomay 		 * built containing the privileges from the default, safe
4891ffbafc53Scomay 		 * set along with those of the "limitpriv" property.
4892ffbafc53Scomay 		 */
4893ffbafc53Scomay 		len = strlen(privbuf) + sizeof (BASIC_TOKEN) + 2;
48949acbbeafSnn35248 
48959acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
48969acbbeafSnn35248 			len += strlen(pnp->pn_priv) + 1;
4897ffbafc53Scomay 		tmp = alloca(len);
4898ffbafc53Scomay 		*tmp = '\0';
4899ffbafc53Scomay 
4900ffbafc53Scomay 		append_priv_token(BASIC_TOKEN, tmp, len);
49019acbbeafSnn35248 		for (pnp = plp->pl_default; pnp != NULL; pnp = pnp->pn_next)
49029acbbeafSnn35248 			append_priv_token(pnp->pn_priv, tmp, len);
4903ffbafc53Scomay 		(void) strlcat(tmp, TOKEN_PRIV_STR, len);
4904ffbafc53Scomay 		(void) strlcat(tmp, privbuf, len);
4905ffbafc53Scomay 	} else {
4906ffbafc53Scomay 		tmp = privbuf;
4907ffbafc53Scomay 	}
4908ffbafc53Scomay 
4909ffbafc53Scomay 
4910ffbafc53Scomay 	/*
4911ffbafc53Scomay 	 * In the next phase, attempt to convert the merged privilege
4912ffbafc53Scomay 	 * string into a privilege set.  In the case of an error, either
4913ffbafc53Scomay 	 * there was a memory allocation failure or there was an invalid
4914ffbafc53Scomay 	 * privilege token in the string.  In either case, return an
4915ffbafc53Scomay 	 * appropriate error code but in the event of an invalid token,
4916ffbafc53Scomay 	 * allocate a string containing its name and return that back to
4917ffbafc53Scomay 	 * the caller.
4918ffbafc53Scomay 	 */
4919ffbafc53Scomay 	mergeset = priv_str_to_set(tmp, TOKEN_PRIV_STR, &token);
4920ffbafc53Scomay 	if (mergeset == NULL) {
4921ffbafc53Scomay 		if (token == NULL)
4922ffbafc53Scomay 			return (Z_NOMEM);
4923ffbafc53Scomay 		if ((cp = strchr(token, TOKEN_PRIV_CHAR)) != NULL)
4924ffbafc53Scomay 			*cp = '\0';
4925ffbafc53Scomay 		if ((*privname = strdup(token)) == NULL)
4926ffbafc53Scomay 			return (Z_NOMEM);
4927ffbafc53Scomay 		else
4928ffbafc53Scomay 			return (Z_PRIV_UNKNOWN);
4929ffbafc53Scomay 	}
4930ffbafc53Scomay 
4931ffbafc53Scomay 	/*
4932ffbafc53Scomay 	 * Next, verify that none of the prohibited zone privileges are
4933ffbafc53Scomay 	 * present in the merged privilege set.
4934ffbafc53Scomay 	 */
49359acbbeafSnn35248 	for (pnp = plp->pl_prohibited; pnp != NULL; pnp = pnp->pn_next) {
49369acbbeafSnn35248 		if (priv_ismember(mergeset, pnp->pn_priv)) {
4937ffbafc53Scomay 			priv_freeset(mergeset);
49389acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
4939ffbafc53Scomay 				return (Z_NOMEM);
4940ffbafc53Scomay 			else
4941ffbafc53Scomay 				return (Z_PRIV_PROHIBITED);
4942ffbafc53Scomay 		}
4943ffbafc53Scomay 	}
4944ffbafc53Scomay 
4945ffbafc53Scomay 	/*
4946ffbafc53Scomay 	 * Finally, verify that all of the required zone privileges are
4947ffbafc53Scomay 	 * present in the merged privilege set.
4948ffbafc53Scomay 	 */
49499acbbeafSnn35248 	for (pnp = plp->pl_required; pnp != NULL; pnp = pnp->pn_next) {
49509acbbeafSnn35248 		if (!priv_ismember(mergeset, pnp->pn_priv)) {
4951ffbafc53Scomay 			priv_freeset(mergeset);
49529acbbeafSnn35248 			if ((*privname = strdup(pnp->pn_priv)) == NULL)
4953ffbafc53Scomay 				return (Z_NOMEM);
4954ffbafc53Scomay 			else
4955ffbafc53Scomay 				return (Z_PRIV_REQUIRED);
4956ffbafc53Scomay 		}
4957ffbafc53Scomay 	}
4958ffbafc53Scomay 
4959ffbafc53Scomay 	priv_copyset(mergeset, privs);
4960ffbafc53Scomay 	priv_freeset(mergeset);
4961ffbafc53Scomay 	return (Z_OK);
4962ffbafc53Scomay }
4963ffbafc53Scomay 
4964ffbafc53Scomay /*
4965ffbafc53Scomay  * Fill in the supplied privilege set with either the default, safe set of
4966ffbafc53Scomay  * privileges suitable for a non-global zone, or one based on the
4967ffbafc53Scomay  * "limitpriv" property in the zone's configuration.
4968ffbafc53Scomay  *
4969ffbafc53Scomay  * In the event of an invalid privilege specification in the
4970ffbafc53Scomay  * configuration, a string is allocated and returned containing the
4971ffbafc53Scomay  * "privilege" causing the issue.  It is the caller's responsibility to
4972ffbafc53Scomay  * free this memory when it is done with it.
4973ffbafc53Scomay  */
4974ffbafc53Scomay int
4975ffbafc53Scomay zonecfg_get_privset(zone_dochandle_t handle, priv_set_t *privs,
4976ffbafc53Scomay     char **privname)
4977ffbafc53Scomay {
49789acbbeafSnn35248 	priv_lists_t *plp;
49799acbbeafSnn35248 	char *cp, *limitpriv = NULL;
49809acbbeafSnn35248 	int err, limitlen;
4981bf1d7e28Sdh155122 	zone_iptype_t iptype;
4982bf1d7e28Sdh155122 	const char *curr_iptype;
4983ffbafc53Scomay 
4984ffbafc53Scomay 	/*
4985ffbafc53Scomay 	 * Attempt to lookup the "limitpriv" property.  If it does not
4986ffbafc53Scomay 	 * exist or matches the string DEFAULT_TOKEN exactly, then the
4987ffbafc53Scomay 	 * default, safe privilege set is returned.
4988ffbafc53Scomay 	 */
49899acbbeafSnn35248 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) != Z_OK)
4990ffbafc53Scomay 		return (err);
49919acbbeafSnn35248 
4992bf1d7e28Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK)
4993bf1d7e28Sdh155122 		return (err);
4994bf1d7e28Sdh155122 
4995bf1d7e28Sdh155122 	switch (iptype) {
4996bf1d7e28Sdh155122 	case ZS_SHARED:
4997bf1d7e28Sdh155122 		curr_iptype = "shared";
4998bf1d7e28Sdh155122 		break;
4999bf1d7e28Sdh155122 	case ZS_EXCLUSIVE:
5000bf1d7e28Sdh155122 		curr_iptype = "exclusive";
5001bf1d7e28Sdh155122 		break;
5002bf1d7e28Sdh155122 	}
5003bf1d7e28Sdh155122 
5004bf1d7e28Sdh155122 	if ((err = priv_lists_create(handle, &plp, curr_iptype)) != Z_OK)
50059acbbeafSnn35248 		return (err);
50069acbbeafSnn35248 
5007ffbafc53Scomay 	limitlen = strlen(limitpriv);
5008ffbafc53Scomay 	if (limitlen == 0 || strcmp(limitpriv, DEFAULT_TOKEN) == 0) {
5009ffbafc53Scomay 		free(limitpriv);
50109acbbeafSnn35248 		err = get_default_privset(privs, plp);
50119acbbeafSnn35248 		priv_lists_destroy(plp);
50129acbbeafSnn35248 		return (err);
5013ffbafc53Scomay 	}
5014ffbafc53Scomay 
5015ffbafc53Scomay 	/*
5016ffbafc53Scomay 	 * Check if the string DEFAULT_TOKEN is the first token in a list
5017ffbafc53Scomay 	 * of privileges.
5018ffbafc53Scomay 	 */
5019ffbafc53Scomay 	cp = strchr(limitpriv, TOKEN_PRIV_CHAR);
5020ffbafc53Scomay 	if (cp != NULL &&
5021ffbafc53Scomay 	    strncmp(limitpriv, DEFAULT_TOKEN, cp - limitpriv) == 0)
50229acbbeafSnn35248 		err = verify_privset(cp + 1, privs, privname, B_TRUE, plp);
5023ffbafc53Scomay 	else
50249acbbeafSnn35248 		err = verify_privset(limitpriv, privs, privname, B_FALSE, plp);
5025ffbafc53Scomay 
5026ffbafc53Scomay 	free(limitpriv);
50279acbbeafSnn35248 	priv_lists_destroy(plp);
5028ffbafc53Scomay 	return (err);
5029ffbafc53Scomay }
5030ffbafc53Scomay 
50317c478bd9Sstevel@tonic-gate int
50327c478bd9Sstevel@tonic-gate zone_get_zonepath(char *zone_name, char *zonepath, size_t rp_sz)
50337c478bd9Sstevel@tonic-gate {
50347c478bd9Sstevel@tonic-gate 	zone_dochandle_t handle;
50357c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
50367c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
50377c478bd9Sstevel@tonic-gate 	FILE *cookie;
50387c478bd9Sstevel@tonic-gate 	int err;
5039108322fbScarlsonj 	char *cp;
50407c478bd9Sstevel@tonic-gate 
50417c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
50427c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
50437c478bd9Sstevel@tonic-gate 
5044108322fbScarlsonj 	(void) strlcpy(zonepath, zonecfg_root, rp_sz);
5045108322fbScarlsonj 	cp = zonepath + strlen(zonepath);
5046108322fbScarlsonj 	while (cp > zonepath && cp[-1] == '/')
5047108322fbScarlsonj 		*--cp = '\0';
5048108322fbScarlsonj 
50497c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
5050108322fbScarlsonj 		if (zonepath[0] == '\0')
50517c478bd9Sstevel@tonic-gate 			(void) strlcpy(zonepath, "/", rp_sz);
50527c478bd9Sstevel@tonic-gate 		return (Z_OK);
50537c478bd9Sstevel@tonic-gate 	}
50547c478bd9Sstevel@tonic-gate 
50557c478bd9Sstevel@tonic-gate 	/*
50567c478bd9Sstevel@tonic-gate 	 * First check the index file.  Because older versions did not have
50577c478bd9Sstevel@tonic-gate 	 * a copy of the zone path, allow for it to be zero length, in which
50587c478bd9Sstevel@tonic-gate 	 * case we ignore this result and fall back to the XML files.
50597c478bd9Sstevel@tonic-gate 	 */
50607c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
50617c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
50627c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
50637c478bd9Sstevel@tonic-gate 			found = B_TRUE;
5064108322fbScarlsonj 			if (ze->zone_path[0] != '\0')
5065108322fbScarlsonj 				(void) strlcpy(cp, ze->zone_path,
5066108322fbScarlsonj 				    rp_sz - (cp - zonepath));
50677c478bd9Sstevel@tonic-gate 		}
50687c478bd9Sstevel@tonic-gate 		free(ze);
50697c478bd9Sstevel@tonic-gate 		if (found)
50707c478bd9Sstevel@tonic-gate 			break;
50717c478bd9Sstevel@tonic-gate 	}
50727c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
5073108322fbScarlsonj 	if (found && *cp != '\0')
50747c478bd9Sstevel@tonic-gate 		return (Z_OK);
50757c478bd9Sstevel@tonic-gate 
50767c478bd9Sstevel@tonic-gate 	/* Fall back to the XML files. */
50777c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL)
50787c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
50797c478bd9Sstevel@tonic-gate 
50807c478bd9Sstevel@tonic-gate 	/*
50817c478bd9Sstevel@tonic-gate 	 * Check the snapshot first: if a zone is running, its zonepath
50827c478bd9Sstevel@tonic-gate 	 * may have changed.
50837c478bd9Sstevel@tonic-gate 	 */
50847c478bd9Sstevel@tonic-gate 	if (zonecfg_get_snapshot_handle(zone_name, handle) != Z_OK) {
5085e767a340Sgjelinek 		if ((err = zonecfg_get_handle(zone_name, handle)) != Z_OK) {
5086e767a340Sgjelinek 			zonecfg_fini_handle(handle);
50877c478bd9Sstevel@tonic-gate 			return (err);
50887c478bd9Sstevel@tonic-gate 		}
5089e767a340Sgjelinek 	}
50907c478bd9Sstevel@tonic-gate 	err = zonecfg_get_zonepath(handle, zonepath, rp_sz);
50917c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
50927c478bd9Sstevel@tonic-gate 	return (err);
50937c478bd9Sstevel@tonic-gate }
50947c478bd9Sstevel@tonic-gate 
50957c478bd9Sstevel@tonic-gate int
50967c478bd9Sstevel@tonic-gate zone_get_rootpath(char *zone_name, char *rootpath, size_t rp_sz)
50977c478bd9Sstevel@tonic-gate {
50987c478bd9Sstevel@tonic-gate 	int err;
50997c478bd9Sstevel@tonic-gate 
51007c478bd9Sstevel@tonic-gate 	/* This function makes sense for non-global zones only. */
51017c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
51027c478bd9Sstevel@tonic-gate 		return (Z_BOGUS_ZONE_NAME);
51037c478bd9Sstevel@tonic-gate 	if ((err = zone_get_zonepath(zone_name, rootpath, rp_sz)) != Z_OK)
51047c478bd9Sstevel@tonic-gate 		return (err);
51057c478bd9Sstevel@tonic-gate 	if (strlcat(rootpath, "/root", rp_sz) >= rp_sz)
51067c478bd9Sstevel@tonic-gate 		return (Z_TOO_BIG);
51077c478bd9Sstevel@tonic-gate 	return (Z_OK);
51087c478bd9Sstevel@tonic-gate }
51097c478bd9Sstevel@tonic-gate 
51109acbbeafSnn35248 int
51119acbbeafSnn35248 zone_get_brand(char *zone_name, char *brandname, size_t rp_sz)
51129acbbeafSnn35248 {
51139acbbeafSnn35248 	int err;
51149acbbeafSnn35248 	zone_dochandle_t handle;
51159acbbeafSnn35248 	char myzone[MAXNAMELEN];
51169acbbeafSnn35248 	int myzoneid = getzoneid();
51179acbbeafSnn35248 
51189acbbeafSnn35248 	/*
51199acbbeafSnn35248 	 * If we are not in the global zone, then we don't have the zone
51209acbbeafSnn35248 	 * .xml files with the brand name available.  Thus, we are going to
51219acbbeafSnn35248 	 * have to ask the kernel for the information.
51229acbbeafSnn35248 	 */
51239acbbeafSnn35248 	if (myzoneid != GLOBAL_ZONEID) {
51242ec67e04Sgjelinek 		if (is_system_labeled()) {
51252ec67e04Sgjelinek 			(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
51262ec67e04Sgjelinek 			return (Z_OK);
51272ec67e04Sgjelinek 		}
51289acbbeafSnn35248 		if (zone_getattr(myzoneid, ZONE_ATTR_NAME, myzone,
51299acbbeafSnn35248 		    sizeof (myzone)) < 0)
51309acbbeafSnn35248 			return (Z_NO_ZONE);
51319acbbeafSnn35248 		if (strncmp(zone_name, myzone, MAXNAMELEN) != NULL)
51329acbbeafSnn35248 			return (Z_NO_ZONE);
51339acbbeafSnn35248 		err = zone_getattr(myzoneid, ZONE_ATTR_BRAND, brandname, rp_sz);
51349acbbeafSnn35248 		if (err < 0)
51359acbbeafSnn35248 			return ((errno == EFAULT) ? Z_TOO_BIG : Z_INVAL);
51369acbbeafSnn35248 		return (Z_OK);
51379acbbeafSnn35248 	}
51389acbbeafSnn35248 
51399acbbeafSnn35248 	if (strcmp(zone_name, "global") == NULL) {
51409acbbeafSnn35248 		(void) strlcpy(brandname, NATIVE_BRAND_NAME, rp_sz);
51412ec67e04Sgjelinek 		return (Z_OK);
51429acbbeafSnn35248 	}
51439acbbeafSnn35248 	if ((handle = zonecfg_init_handle()) == NULL)
51449acbbeafSnn35248 		return (Z_NOMEM);
51459acbbeafSnn35248 
51469acbbeafSnn35248 	err = zonecfg_get_handle((char *)zone_name, handle);
51479acbbeafSnn35248 	if (err == Z_OK)
51489acbbeafSnn35248 		err = zonecfg_get_brand(handle, brandname, rp_sz);
51499acbbeafSnn35248 
51509acbbeafSnn35248 	zonecfg_fini_handle(handle);
51519acbbeafSnn35248 	return (err);
51529acbbeafSnn35248 }
51539acbbeafSnn35248 
5154facf4a8dSllai1 /*
5155facf4a8dSllai1  * Return the appropriate root for the active /dev.
5156facf4a8dSllai1  * For normal zone, the path is $ZONEPATH/root;
5157facf4a8dSllai1  * for scratch zone, the dev path is $ZONEPATH/lu.
5158facf4a8dSllai1  */
5159facf4a8dSllai1 int
5160facf4a8dSllai1 zone_get_devroot(char *zone_name, char *devroot, size_t rp_sz)
5161facf4a8dSllai1 {
5162facf4a8dSllai1 	int err;
5163facf4a8dSllai1 	char *suffix;
5164facf4a8dSllai1 	zone_state_t state;
5165facf4a8dSllai1 
5166facf4a8dSllai1 	/* This function makes sense for non-global zones only. */
5167facf4a8dSllai1 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0)
5168facf4a8dSllai1 		return (Z_BOGUS_ZONE_NAME);
5169facf4a8dSllai1 	if ((err = zone_get_zonepath(zone_name, devroot, rp_sz)) != Z_OK)
5170facf4a8dSllai1 		return (err);
5171facf4a8dSllai1 
5172facf4a8dSllai1 	if (zone_get_state(zone_name, &state) == Z_OK &&
5173facf4a8dSllai1 	    state == ZONE_STATE_MOUNTED)
5174facf4a8dSllai1 		suffix = "/lu";
5175facf4a8dSllai1 	else
5176facf4a8dSllai1 		suffix = "/root";
5177facf4a8dSllai1 	if (strlcat(devroot, suffix, rp_sz) >= rp_sz)
5178facf4a8dSllai1 		return (Z_TOO_BIG);
5179facf4a8dSllai1 	return (Z_OK);
5180facf4a8dSllai1 }
5181facf4a8dSllai1 
51827c478bd9Sstevel@tonic-gate static zone_state_t
5183108322fbScarlsonj kernel_state_to_user_state(zoneid_t zoneid, zone_status_t kernel_state)
51847c478bd9Sstevel@tonic-gate {
5185108322fbScarlsonj 	char zoneroot[MAXPATHLEN];
5186108322fbScarlsonj 	size_t zlen;
5187108322fbScarlsonj 
51887c478bd9Sstevel@tonic-gate 	assert(kernel_state <= ZONE_MAX_STATE);
51897c478bd9Sstevel@tonic-gate 	switch (kernel_state) {
51907c478bd9Sstevel@tonic-gate 		case ZONE_IS_UNINITIALIZED:
5191bd41d0a8Snordmark 		case ZONE_IS_INITIALIZED:
5192bd41d0a8Snordmark 			/* The kernel will not return these two states */
5193108322fbScarlsonj 			return (ZONE_STATE_READY);
51947c478bd9Sstevel@tonic-gate 		case ZONE_IS_READY:
5195108322fbScarlsonj 			/*
5196108322fbScarlsonj 			 * If the zone's root is mounted on $ZONEPATH/lu, then
5197108322fbScarlsonj 			 * it's a mounted scratch zone.
5198108322fbScarlsonj 			 */
5199108322fbScarlsonj 			if (zone_getattr(zoneid, ZONE_ATTR_ROOT, zoneroot,
5200108322fbScarlsonj 			    sizeof (zoneroot)) >= 0) {
5201108322fbScarlsonj 				zlen = strlen(zoneroot);
5202108322fbScarlsonj 				if (zlen > 3 &&
5203108322fbScarlsonj 				    strcmp(zoneroot + zlen - 3, "/lu") == 0)
5204108322fbScarlsonj 					return (ZONE_STATE_MOUNTED);
5205108322fbScarlsonj 			}
52067c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_READY);
52077c478bd9Sstevel@tonic-gate 		case ZONE_IS_BOOTING:
52087c478bd9Sstevel@tonic-gate 		case ZONE_IS_RUNNING:
52097c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_RUNNING);
52107c478bd9Sstevel@tonic-gate 		case ZONE_IS_SHUTTING_DOWN:
52117c478bd9Sstevel@tonic-gate 		case ZONE_IS_EMPTY:
52127c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_SHUTTING_DOWN);
52137c478bd9Sstevel@tonic-gate 		case ZONE_IS_DOWN:
52147c478bd9Sstevel@tonic-gate 		case ZONE_IS_DYING:
52157c478bd9Sstevel@tonic-gate 		case ZONE_IS_DEAD:
52167c478bd9Sstevel@tonic-gate 		default:
52177c478bd9Sstevel@tonic-gate 			return (ZONE_STATE_DOWN);
52187c478bd9Sstevel@tonic-gate 	}
52197c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
52207c478bd9Sstevel@tonic-gate }
52217c478bd9Sstevel@tonic-gate 
52227c478bd9Sstevel@tonic-gate int
52237c478bd9Sstevel@tonic-gate zone_get_state(char *zone_name, zone_state_t *state_num)
52247c478bd9Sstevel@tonic-gate {
52257c478bd9Sstevel@tonic-gate 	zone_status_t status;
52267c478bd9Sstevel@tonic-gate 	zoneid_t zone_id;
52277c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
52287c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
52297c478bd9Sstevel@tonic-gate 	FILE *cookie;
5230108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
5231108322fbScarlsonj 	FILE *fp;
52327c478bd9Sstevel@tonic-gate 
52337c478bd9Sstevel@tonic-gate 	if (zone_name == NULL)
52347c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
52357c478bd9Sstevel@tonic-gate 
5236108322fbScarlsonj 	/*
5237108322fbScarlsonj 	 * If we're looking at an alternate root, then we need to query the
5238108322fbScarlsonj 	 * kernel using the scratch zone name.
5239108322fbScarlsonj 	 */
5240108322fbScarlsonj 	zone_id = -1;
5241108322fbScarlsonj 	if (*zonecfg_root != '\0' && !zonecfg_is_scratch(zone_name)) {
5242108322fbScarlsonj 		if ((fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
5243108322fbScarlsonj 			if (zonecfg_find_scratch(fp, zone_name, zonecfg_root,
5244108322fbScarlsonj 			    kernzone, sizeof (kernzone)) == 0)
5245108322fbScarlsonj 				zone_id = getzoneidbyname(kernzone);
5246108322fbScarlsonj 			zonecfg_close_scratch(fp);
5247108322fbScarlsonj 		}
5248108322fbScarlsonj 	} else {
5249108322fbScarlsonj 		zone_id = getzoneidbyname(zone_name);
5250108322fbScarlsonj 	}
5251108322fbScarlsonj 
52527c478bd9Sstevel@tonic-gate 	/* check to see if zone is running */
5253108322fbScarlsonj 	if (zone_id != -1 &&
52547c478bd9Sstevel@tonic-gate 	    zone_getattr(zone_id, ZONE_ATTR_STATUS, &status,
52557c478bd9Sstevel@tonic-gate 	    sizeof (status)) >= 0) {
5256108322fbScarlsonj 		*state_num = kernel_state_to_user_state(zone_id, status);
52577c478bd9Sstevel@tonic-gate 		return (Z_OK);
52587c478bd9Sstevel@tonic-gate 	}
52597c478bd9Sstevel@tonic-gate 
52607c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
52617c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
52627c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, zone_name) == 0) {
52637c478bd9Sstevel@tonic-gate 			found = B_TRUE;
52647c478bd9Sstevel@tonic-gate 			*state_num = ze->zone_state;
52657c478bd9Sstevel@tonic-gate 		}
52667c478bd9Sstevel@tonic-gate 		free(ze);
52677c478bd9Sstevel@tonic-gate 		if (found)
52687c478bd9Sstevel@tonic-gate 			break;
52697c478bd9Sstevel@tonic-gate 	}
52707c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
52717c478bd9Sstevel@tonic-gate 	return ((found) ? Z_OK : Z_NO_ZONE);
52727c478bd9Sstevel@tonic-gate }
52737c478bd9Sstevel@tonic-gate 
52747c478bd9Sstevel@tonic-gate int
52757c478bd9Sstevel@tonic-gate zone_set_state(char *zone, zone_state_t state)
52767c478bd9Sstevel@tonic-gate {
52777c478bd9Sstevel@tonic-gate 	struct zoneent ze;
52787c478bd9Sstevel@tonic-gate 
52797c478bd9Sstevel@tonic-gate 	if (state != ZONE_STATE_CONFIGURED && state != ZONE_STATE_INSTALLED &&
52807c478bd9Sstevel@tonic-gate 	    state != ZONE_STATE_INCOMPLETE)
52817c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
52827c478bd9Sstevel@tonic-gate 
5283087719fdSdp 	bzero(&ze, sizeof (ze));
52847c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_name, zone, sizeof (ze.zone_name));
52857c478bd9Sstevel@tonic-gate 	ze.zone_state = state;
52867c478bd9Sstevel@tonic-gate 	(void) strlcpy(ze.zone_path, "", sizeof (ze.zone_path));
52877c478bd9Sstevel@tonic-gate 	return (putzoneent(&ze, PZE_MODIFY));
52887c478bd9Sstevel@tonic-gate }
52897c478bd9Sstevel@tonic-gate 
52907c478bd9Sstevel@tonic-gate /*
52917c478bd9Sstevel@tonic-gate  * Get id (if any) for specified zone.  There are four possible outcomes:
52927c478bd9Sstevel@tonic-gate  * - If the string corresponds to the numeric id of an active (booted)
52937c478bd9Sstevel@tonic-gate  *   zone, sets *zip to the zone id and returns 0.
52947c478bd9Sstevel@tonic-gate  * - If the string corresponds to the name of an active (booted) zone,
52957c478bd9Sstevel@tonic-gate  *   sets *zip to the zone id and returns 0.
52967c478bd9Sstevel@tonic-gate  * - If the string is a name in the configuration but is not booted,
52977c478bd9Sstevel@tonic-gate  *   sets *zip to ZONE_ID_UNDEFINED and returns 0.
52987c478bd9Sstevel@tonic-gate  * - Otherwise, leaves *zip unchanged and returns -1.
52997c478bd9Sstevel@tonic-gate  *
53007c478bd9Sstevel@tonic-gate  * This function acts as an auxiliary filter on the function of the same
53017c478bd9Sstevel@tonic-gate  * name in libc; the linker binds to this version if libzonecfg exists,
53027c478bd9Sstevel@tonic-gate  * and the libc version if it doesn't.  Any changes to this version of
53037c478bd9Sstevel@tonic-gate  * the function should probably be reflected in the libc version as well.
53047c478bd9Sstevel@tonic-gate  */
53057c478bd9Sstevel@tonic-gate int
53067c478bd9Sstevel@tonic-gate zone_get_id(const char *str, zoneid_t *zip)
53077c478bd9Sstevel@tonic-gate {
53087c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl;
53097c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
53107c478bd9Sstevel@tonic-gate 	char *cp;
53117c478bd9Sstevel@tonic-gate 	int err;
53127c478bd9Sstevel@tonic-gate 
53137c478bd9Sstevel@tonic-gate 	/* first try looking for active zone by id */
53147c478bd9Sstevel@tonic-gate 	errno = 0;
53157c478bd9Sstevel@tonic-gate 	zoneid = (zoneid_t)strtol(str, &cp, 0);
53167c478bd9Sstevel@tonic-gate 	if (errno == 0 && cp != str && *cp == '\0' &&
53177c478bd9Sstevel@tonic-gate 	    getzonenamebyid(zoneid, NULL, 0) != -1) {
53187c478bd9Sstevel@tonic-gate 		*zip = zoneid;
53197c478bd9Sstevel@tonic-gate 		return (0);
53207c478bd9Sstevel@tonic-gate 	}
53217c478bd9Sstevel@tonic-gate 
53227c478bd9Sstevel@tonic-gate 	/* then look for active zone by name */
53237c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(str)) != -1) {
53247c478bd9Sstevel@tonic-gate 		*zip = zoneid;
53257c478bd9Sstevel@tonic-gate 		return (0);
53267c478bd9Sstevel@tonic-gate 	}
53277c478bd9Sstevel@tonic-gate 
53287c478bd9Sstevel@tonic-gate 	/* if in global zone, try looking up name in configuration database */
53297c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID ||
53307c478bd9Sstevel@tonic-gate 	    (hdl = zonecfg_init_handle()) == NULL)
53317c478bd9Sstevel@tonic-gate 		return (-1);
53327c478bd9Sstevel@tonic-gate 
5333108322fbScarlsonj 	if (zonecfg_get_handle(str, hdl) == Z_OK) {
53347c478bd9Sstevel@tonic-gate 		/* zone exists but isn't active */
53357c478bd9Sstevel@tonic-gate 		*zip = ZONE_ID_UNDEFINED;
53367c478bd9Sstevel@tonic-gate 		err = 0;
53377c478bd9Sstevel@tonic-gate 	} else {
53387c478bd9Sstevel@tonic-gate 		err = -1;
53397c478bd9Sstevel@tonic-gate 	}
53407c478bd9Sstevel@tonic-gate 
53417c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(hdl);
53427c478bd9Sstevel@tonic-gate 	return (err);
53437c478bd9Sstevel@tonic-gate }
53447c478bd9Sstevel@tonic-gate 
53457c478bd9Sstevel@tonic-gate char *
53467c478bd9Sstevel@tonic-gate zone_state_str(zone_state_t state_num)
53477c478bd9Sstevel@tonic-gate {
53487c478bd9Sstevel@tonic-gate 	switch (state_num) {
53497c478bd9Sstevel@tonic-gate 	case ZONE_STATE_CONFIGURED:
53507c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_CONFIGURED);
53517c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INCOMPLETE:
53527c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INCOMPLETE);
53537c478bd9Sstevel@tonic-gate 	case ZONE_STATE_INSTALLED:
53547c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_INSTALLED);
53557c478bd9Sstevel@tonic-gate 	case ZONE_STATE_READY:
53567c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_READY);
5357108322fbScarlsonj 	case ZONE_STATE_MOUNTED:
5358108322fbScarlsonj 		return (ZONE_STATE_STR_MOUNTED);
53597c478bd9Sstevel@tonic-gate 	case ZONE_STATE_RUNNING:
53607c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_RUNNING);
53617c478bd9Sstevel@tonic-gate 	case ZONE_STATE_SHUTTING_DOWN:
53627c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_SHUTTING_DOWN);
53637c478bd9Sstevel@tonic-gate 	case ZONE_STATE_DOWN:
53647c478bd9Sstevel@tonic-gate 		return (ZONE_STATE_STR_DOWN);
53657c478bd9Sstevel@tonic-gate 	default:
53667c478bd9Sstevel@tonic-gate 		return ("unknown");
53677c478bd9Sstevel@tonic-gate 	}
53687c478bd9Sstevel@tonic-gate }
53697c478bd9Sstevel@tonic-gate 
53707c478bd9Sstevel@tonic-gate /*
5371108322fbScarlsonj  * Given a UUID value, find an associated zone name.  This is intended to be
5372108322fbScarlsonj  * used by callers who set up some 'default' name (corresponding to the
5373108322fbScarlsonj  * expected name for the zone) in the zonename buffer, and thus the function
5374108322fbScarlsonj  * doesn't touch this buffer on failure.
5375108322fbScarlsonj  */
5376108322fbScarlsonj int
5377555afedfScarlsonj zonecfg_get_name_by_uuid(const uuid_t uuidin, char *zonename, size_t namelen)
5378108322fbScarlsonj {
5379108322fbScarlsonj 	FILE *fp;
5380108322fbScarlsonj 	struct zoneent *ze;
5381555afedfScarlsonj 	uchar_t *uuid;
5382108322fbScarlsonj 
5383108322fbScarlsonj 	/*
5384108322fbScarlsonj 	 * A small amount of subterfuge via casts is necessary here because
5385108322fbScarlsonj 	 * libuuid doesn't use const correctly, but we don't want to export
5386108322fbScarlsonj 	 * this brokenness to our clients.
5387108322fbScarlsonj 	 */
5388555afedfScarlsonj 	uuid = (uchar_t *)uuidin;
5389555afedfScarlsonj 	if (uuid_is_null(uuid))
5390108322fbScarlsonj 		return (Z_NO_ZONE);
5391108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5392108322fbScarlsonj 		return (Z_NO_ZONE);
5393108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5394555afedfScarlsonj 		if (uuid_compare(uuid, ze->zone_uuid) == 0)
5395108322fbScarlsonj 			break;
5396108322fbScarlsonj 		free(ze);
5397108322fbScarlsonj 	}
5398108322fbScarlsonj 	endzoneent(fp);
5399108322fbScarlsonj 	if (ze != NULL) {
5400108322fbScarlsonj 		(void) strlcpy(zonename, ze->zone_name, namelen);
5401108322fbScarlsonj 		free(ze);
5402108322fbScarlsonj 		return (Z_OK);
5403108322fbScarlsonj 	} else {
5404108322fbScarlsonj 		return (Z_NO_ZONE);
5405108322fbScarlsonj 	}
5406108322fbScarlsonj }
5407108322fbScarlsonj 
5408108322fbScarlsonj /*
5409108322fbScarlsonj  * Given a zone name, get its UUID.  Returns a "NULL" UUID value if the zone
5410108322fbScarlsonj  * exists but the file doesn't have a value set yet.  Returns an error if the
5411108322fbScarlsonj  * zone cannot be located.
5412108322fbScarlsonj  */
5413108322fbScarlsonj int
5414108322fbScarlsonj zonecfg_get_uuid(const char *zonename, uuid_t uuid)
5415108322fbScarlsonj {
5416108322fbScarlsonj 	FILE *fp;
5417108322fbScarlsonj 	struct zoneent *ze;
5418108322fbScarlsonj 
5419108322fbScarlsonj 	if ((fp = setzoneent()) == NULL)
5420108322fbScarlsonj 		return (Z_NO_ZONE);
5421108322fbScarlsonj 	while ((ze = getzoneent_private(fp)) != NULL) {
5422108322fbScarlsonj 		if (strcmp(ze->zone_name, zonename) == 0)
5423108322fbScarlsonj 			break;
5424108322fbScarlsonj 		free(ze);
5425108322fbScarlsonj 	}
5426108322fbScarlsonj 	endzoneent(fp);
5427108322fbScarlsonj 	if (ze != NULL) {
5428108322fbScarlsonj 		uuid_copy(uuid, ze->zone_uuid);
5429108322fbScarlsonj 		free(ze);
5430108322fbScarlsonj 		return (Z_OK);
5431108322fbScarlsonj 	} else {
5432108322fbScarlsonj 		return (Z_NO_ZONE);
5433108322fbScarlsonj 	}
5434108322fbScarlsonj }
5435108322fbScarlsonj 
5436108322fbScarlsonj /*
54377c478bd9Sstevel@tonic-gate  * File-system convenience functions.
54387c478bd9Sstevel@tonic-gate  */
54397c478bd9Sstevel@tonic-gate boolean_t
54407c478bd9Sstevel@tonic-gate zonecfg_valid_fs_type(const char *type)
54417c478bd9Sstevel@tonic-gate {
54427c478bd9Sstevel@tonic-gate 	/*
54437c478bd9Sstevel@tonic-gate 	 * We already know which FS types don't work.
54447c478bd9Sstevel@tonic-gate 	 */
54457c478bd9Sstevel@tonic-gate 	if (strcmp(type, "proc") == 0 ||
54467c478bd9Sstevel@tonic-gate 	    strcmp(type, "mntfs") == 0 ||
54477c478bd9Sstevel@tonic-gate 	    strcmp(type, "autofs") == 0 ||
54487c478bd9Sstevel@tonic-gate 	    strncmp(type, "nfs", sizeof ("nfs") - 1) == 0 ||
54497c478bd9Sstevel@tonic-gate 	    strcmp(type, "cachefs") == 0)
54507c478bd9Sstevel@tonic-gate 		return (B_FALSE);
54517c478bd9Sstevel@tonic-gate 	/*
54527c478bd9Sstevel@tonic-gate 	 * The caller may do more detailed verification to make sure other
54537c478bd9Sstevel@tonic-gate 	 * aspects of this filesystem type make sense.
54547c478bd9Sstevel@tonic-gate 	 */
54557c478bd9Sstevel@tonic-gate 	return (B_TRUE);
54567c478bd9Sstevel@tonic-gate }
54577c478bd9Sstevel@tonic-gate 
54587c478bd9Sstevel@tonic-gate /*
54597c478bd9Sstevel@tonic-gate  * Generally uninteresting rctl convenience functions.
54607c478bd9Sstevel@tonic-gate  */
54617c478bd9Sstevel@tonic-gate 
54627c478bd9Sstevel@tonic-gate int
54637c478bd9Sstevel@tonic-gate zonecfg_construct_rctlblk(const struct zone_rctlvaltab *rctlval,
54647c478bd9Sstevel@tonic-gate     rctlblk_t *rctlblk)
54657c478bd9Sstevel@tonic-gate {
54667c478bd9Sstevel@tonic-gate 	unsigned long long ull;
54677c478bd9Sstevel@tonic-gate 	char *endp;
54687c478bd9Sstevel@tonic-gate 	rctl_priv_t priv;
54697c478bd9Sstevel@tonic-gate 	rctl_qty_t limit;
54707c478bd9Sstevel@tonic-gate 	uint_t action;
54717c478bd9Sstevel@tonic-gate 
54727c478bd9Sstevel@tonic-gate 	/* Get the privilege */
54737c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_priv, "basic") == 0) {
54747c478bd9Sstevel@tonic-gate 		priv = RCPRIV_BASIC;
54757c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_priv, "privileged") == 0) {
54767c478bd9Sstevel@tonic-gate 		priv = RCPRIV_PRIVILEGED;
54777c478bd9Sstevel@tonic-gate 	} else {
54787c478bd9Sstevel@tonic-gate 		/* Invalid privilege */
54797c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54807c478bd9Sstevel@tonic-gate 	}
54817c478bd9Sstevel@tonic-gate 
54827c478bd9Sstevel@tonic-gate 	/* deal with negative input; strtoull(3c) doesn't do what we want */
54837c478bd9Sstevel@tonic-gate 	if (rctlval->zone_rctlval_limit[0] == '-')
54847c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54857c478bd9Sstevel@tonic-gate 	/* Get the limit */
54867c478bd9Sstevel@tonic-gate 	errno = 0;
54877c478bd9Sstevel@tonic-gate 	ull = strtoull(rctlval->zone_rctlval_limit, &endp, 0);
54887c478bd9Sstevel@tonic-gate 	if (errno != 0 || *endp != '\0') {
54897c478bd9Sstevel@tonic-gate 		/* parse failed */
54907c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
54917c478bd9Sstevel@tonic-gate 	}
54927c478bd9Sstevel@tonic-gate 	limit = (rctl_qty_t)ull;
54937c478bd9Sstevel@tonic-gate 
54947c478bd9Sstevel@tonic-gate 	/* Get the action */
54957c478bd9Sstevel@tonic-gate 	if (strcmp(rctlval->zone_rctlval_action, "none") == 0) {
54967c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_NOACTION;
54977c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "signal") == 0) {
54987c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_SIGNAL;
54997c478bd9Sstevel@tonic-gate 	} else if (strcmp(rctlval->zone_rctlval_action, "deny") == 0) {
55007c478bd9Sstevel@tonic-gate 		action = RCTL_LOCAL_DENY;
55017c478bd9Sstevel@tonic-gate 	} else {
55027c478bd9Sstevel@tonic-gate 		/* Invalid Action */
55037c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
55047c478bd9Sstevel@tonic-gate 	}
55057c478bd9Sstevel@tonic-gate 	rctlblk_set_local_action(rctlblk, action, 0);
55067c478bd9Sstevel@tonic-gate 	rctlblk_set_privilege(rctlblk, priv);
55077c478bd9Sstevel@tonic-gate 	rctlblk_set_value(rctlblk, limit);
55087c478bd9Sstevel@tonic-gate 	return (Z_OK);
55097c478bd9Sstevel@tonic-gate }
55107c478bd9Sstevel@tonic-gate 
55117c478bd9Sstevel@tonic-gate static int
55127c478bd9Sstevel@tonic-gate rctl_check(const char *rctlname, void *arg)
55137c478bd9Sstevel@tonic-gate {
55147c478bd9Sstevel@tonic-gate 	const char *attrname = arg;
55157c478bd9Sstevel@tonic-gate 
55167c478bd9Sstevel@tonic-gate 	/*
55177c478bd9Sstevel@tonic-gate 	 * Returning 1 here is our signal to zonecfg_is_rctl() that it is
55187c478bd9Sstevel@tonic-gate 	 * indeed an rctl name recognized by the system.
55197c478bd9Sstevel@tonic-gate 	 */
55207c478bd9Sstevel@tonic-gate 	return (strcmp(rctlname, attrname) == 0 ? 1 : 0);
55217c478bd9Sstevel@tonic-gate }
55227c478bd9Sstevel@tonic-gate 
55237c478bd9Sstevel@tonic-gate boolean_t
55247c478bd9Sstevel@tonic-gate zonecfg_is_rctl(const char *name)
55257c478bd9Sstevel@tonic-gate {
55267c478bd9Sstevel@tonic-gate 	return (rctl_walk(rctl_check, (void *)name) == 1);
55277c478bd9Sstevel@tonic-gate }
55287c478bd9Sstevel@tonic-gate 
55297c478bd9Sstevel@tonic-gate boolean_t
55307c478bd9Sstevel@tonic-gate zonecfg_valid_rctlname(const char *name)
55317c478bd9Sstevel@tonic-gate {
55327c478bd9Sstevel@tonic-gate 	const char *c;
55337c478bd9Sstevel@tonic-gate 
55347c478bd9Sstevel@tonic-gate 	if (strncmp(name, "zone.", sizeof ("zone.") - 1) != 0)
55357c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55367c478bd9Sstevel@tonic-gate 	if (strlen(name) == sizeof ("zone.") - 1)
55377c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55387c478bd9Sstevel@tonic-gate 	for (c = name + sizeof ("zone.") - 1; *c != '\0'; c++) {
55397c478bd9Sstevel@tonic-gate 		if (!isalpha(*c) && *c != '-')
55407c478bd9Sstevel@tonic-gate 			return (B_FALSE);
55417c478bd9Sstevel@tonic-gate 	}
55427c478bd9Sstevel@tonic-gate 	return (B_TRUE);
55437c478bd9Sstevel@tonic-gate }
55447c478bd9Sstevel@tonic-gate 
55457c478bd9Sstevel@tonic-gate boolean_t
55467c478bd9Sstevel@tonic-gate zonecfg_valid_rctlblk(const rctlblk_t *rctlblk)
55477c478bd9Sstevel@tonic-gate {
55487c478bd9Sstevel@tonic-gate 	rctl_priv_t priv = rctlblk_get_privilege((rctlblk_t *)rctlblk);
55497c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
55507c478bd9Sstevel@tonic-gate 
55517c478bd9Sstevel@tonic-gate 	if (priv != RCPRIV_PRIVILEGED)
55527c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55537c478bd9Sstevel@tonic-gate 	if (action != RCTL_LOCAL_NOACTION && action != RCTL_LOCAL_DENY)
55547c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55557c478bd9Sstevel@tonic-gate 	return (B_TRUE);
55567c478bd9Sstevel@tonic-gate }
55577c478bd9Sstevel@tonic-gate 
55587c478bd9Sstevel@tonic-gate boolean_t
55597c478bd9Sstevel@tonic-gate zonecfg_valid_rctl(const char *name, const rctlblk_t *rctlblk)
55607c478bd9Sstevel@tonic-gate {
55617c478bd9Sstevel@tonic-gate 	rctlblk_t *current, *next;
55627c478bd9Sstevel@tonic-gate 	rctl_qty_t limit = rctlblk_get_value((rctlblk_t *)rctlblk);
55637c478bd9Sstevel@tonic-gate 	uint_t action = rctlblk_get_local_action((rctlblk_t *)rctlblk, NULL);
55647c478bd9Sstevel@tonic-gate 	uint_t global_flags;
55657c478bd9Sstevel@tonic-gate 
55667c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlblk(rctlblk))
55677c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55687c478bd9Sstevel@tonic-gate 	if (!zonecfg_valid_rctlname(name))
55697c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55707c478bd9Sstevel@tonic-gate 
55717c478bd9Sstevel@tonic-gate 	current = alloca(rctlblk_size());
55727c478bd9Sstevel@tonic-gate 	if (getrctl(name, NULL, current, RCTL_FIRST) != 0)
55737c478bd9Sstevel@tonic-gate 		return (B_TRUE);	/* not an rctl on this system */
55747c478bd9Sstevel@tonic-gate 	/*
55757c478bd9Sstevel@tonic-gate 	 * Make sure the proposed value isn't greater than the current system
55767c478bd9Sstevel@tonic-gate 	 * value.
55777c478bd9Sstevel@tonic-gate 	 */
55787c478bd9Sstevel@tonic-gate 	next = alloca(rctlblk_size());
55797c478bd9Sstevel@tonic-gate 	while (rctlblk_get_privilege(current) != RCPRIV_SYSTEM) {
55807c478bd9Sstevel@tonic-gate 		rctlblk_t *tmp;
55817c478bd9Sstevel@tonic-gate 
55827c478bd9Sstevel@tonic-gate 		if (getrctl(name, current, next, RCTL_NEXT) != 0)
55837c478bd9Sstevel@tonic-gate 			return (B_FALSE);	/* shouldn't happen */
55847c478bd9Sstevel@tonic-gate 		tmp = current;
55857c478bd9Sstevel@tonic-gate 		current = next;
55867c478bd9Sstevel@tonic-gate 		next = tmp;
55877c478bd9Sstevel@tonic-gate 	}
55887c478bd9Sstevel@tonic-gate 	if (limit > rctlblk_get_value(current))
55897c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55907c478bd9Sstevel@tonic-gate 
55917c478bd9Sstevel@tonic-gate 	/*
55927c478bd9Sstevel@tonic-gate 	 * Make sure the proposed action is allowed.
55937c478bd9Sstevel@tonic-gate 	 */
55947c478bd9Sstevel@tonic-gate 	global_flags = rctlblk_get_global_flags(current);
55957c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_NEVER) &&
55967c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_DENY)
55977c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55987c478bd9Sstevel@tonic-gate 	if ((global_flags & RCTL_GLOBAL_DENY_ALWAYS) &&
55997c478bd9Sstevel@tonic-gate 	    action == RCTL_LOCAL_NOACTION)
56007c478bd9Sstevel@tonic-gate 		return (B_FALSE);
56017c478bd9Sstevel@tonic-gate 
56027c478bd9Sstevel@tonic-gate 	return (B_TRUE);
56037c478bd9Sstevel@tonic-gate }
5604fa9e4066Sahrens 
5605cf8f45c7Sdstaff /*
5606cf8f45c7Sdstaff  * There is always a race condition between reading the initial copy of
5607cf8f45c7Sdstaff  * a zones state and its state changing.  We address this by providing
5608cf8f45c7Sdstaff  * zonecfg_notify_critical_enter and zonecfg_noticy_critical_exit functions.
5609cf8f45c7Sdstaff  * When zonecfg_critical_enter is called, sets the state field to LOCKED
5610cf8f45c7Sdstaff  * and aquires biglock. Biglock protects against other threads executing
5611cf8f45c7Sdstaff  * critical_enter and the state field protects against state changes during
5612cf8f45c7Sdstaff  * the critical period.
5613cf8f45c7Sdstaff  *
5614cf8f45c7Sdstaff  * If any state changes occur, zn_cb will set the failed field of the znotify
5615cf8f45c7Sdstaff  * structure.  This will cause the critical_exit function to re-lock the
5616cf8f45c7Sdstaff  * channel and return an error. Since evsnts may be delayed, the critical_exit
5617cf8f45c7Sdstaff  * function "flushes" the queue by putting an event on the queue and waiting for
5618cf8f45c7Sdstaff  * zn_cb to notify critical_exit that it received the ping event.
5619cf8f45c7Sdstaff  */
5620cf8f45c7Sdstaff static const char *
5621cf8f45c7Sdstaff string_get_tok(const char *in, char delim, int num)
5622cf8f45c7Sdstaff {
5623cf8f45c7Sdstaff 	int i = 0;
5624cf8f45c7Sdstaff 
5625cf8f45c7Sdstaff 	for (; i < num; in++) {
5626cf8f45c7Sdstaff 		if (*in == delim)
5627cf8f45c7Sdstaff 			i++;
5628cf8f45c7Sdstaff 		if (*in == 0)
5629cf8f45c7Sdstaff 			return (NULL);
5630cf8f45c7Sdstaff 	}
5631cf8f45c7Sdstaff 	return (in);
5632cf8f45c7Sdstaff }
5633cf8f45c7Sdstaff 
5634cf8f45c7Sdstaff static boolean_t
5635cf8f45c7Sdstaff is_ping(sysevent_t *ev)
5636cf8f45c7Sdstaff {
5637cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5638cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS) == 0) {
5639cf8f45c7Sdstaff 		return (B_TRUE);
5640cf8f45c7Sdstaff 	} else {
5641cf8f45c7Sdstaff 		return (B_FALSE);
5642cf8f45c7Sdstaff 	}
5643cf8f45c7Sdstaff }
5644cf8f45c7Sdstaff 
5645cf8f45c7Sdstaff static boolean_t
5646cf8f45c7Sdstaff is_my_ping(sysevent_t *ev)
5647cf8f45c7Sdstaff {
5648cf8f45c7Sdstaff 	const char *sender;
5649cf8f45c7Sdstaff 	char mypid[sizeof (pid_t) * 3 + 1];
5650cf8f45c7Sdstaff 
5651cf8f45c7Sdstaff 	(void) snprintf(mypid, sizeof (mypid), "%i", getpid());
5652cf8f45c7Sdstaff 	sender = string_get_tok(sysevent_get_pub(ev), ':', 3);
5653cf8f45c7Sdstaff 	if (sender == NULL)
5654cf8f45c7Sdstaff 		return (B_FALSE);
5655cf8f45c7Sdstaff 	if (strcmp(sender, mypid) != 0)
5656cf8f45c7Sdstaff 		return (B_FALSE);
5657cf8f45c7Sdstaff 	return (B_TRUE);
5658cf8f45c7Sdstaff }
5659cf8f45c7Sdstaff 
5660cf8f45c7Sdstaff static int
5661cf8f45c7Sdstaff do_callback(struct znotify *zevtchan, sysevent_t *ev)
5662cf8f45c7Sdstaff {
5663cf8f45c7Sdstaff 	nvlist_t *l;
5664cf8f45c7Sdstaff 	int zid;
5665cf8f45c7Sdstaff 	char *zonename;
5666cf8f45c7Sdstaff 	char *newstate;
5667cf8f45c7Sdstaff 	char *oldstate;
5668cf8f45c7Sdstaff 	int ret;
5669cf8f45c7Sdstaff 	hrtime_t when;
5670cf8f45c7Sdstaff 
5671cf8f45c7Sdstaff 	if (strcmp(sysevent_get_subclass_name(ev),
5672cf8f45c7Sdstaff 	    ZONE_EVENT_STATUS_SUBCLASS) == 0) {
5673cf8f45c7Sdstaff 
5674cf8f45c7Sdstaff 		if (sysevent_get_attr_list(ev, &l) != 0) {
5675cf8f45c7Sdstaff 			if (errno == ENOMEM) {
5676cf8f45c7Sdstaff 				zevtchan->zn_failure_count++;
5677cf8f45c7Sdstaff 				return (EAGAIN);
5678cf8f45c7Sdstaff 			}
5679cf8f45c7Sdstaff 			return (0);
5680cf8f45c7Sdstaff 		}
5681cf8f45c7Sdstaff 		ret = 0;
5682cf8f45c7Sdstaff 
5683cf8f45c7Sdstaff 		if ((nvlist_lookup_string(l, ZONE_CB_NAME, &zonename) == 0) &&
5684cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_NEWSTATE, &newstate)
5685cf8f45c7Sdstaff 		    == 0) &&
5686cf8f45c7Sdstaff 		    (nvlist_lookup_string(l, ZONE_CB_OLDSTATE, &oldstate)
5687cf8f45c7Sdstaff 		    == 0) &&
5688cf8f45c7Sdstaff 		    (nvlist_lookup_uint64(l, ZONE_CB_TIMESTAMP,
5689cf8f45c7Sdstaff 		    (uint64_t *)&when) == 0) &&
5690cf8f45c7Sdstaff 		    (nvlist_lookup_int32(l, ZONE_CB_ZONEID, &zid) == 0)) {
5691cf8f45c7Sdstaff 			ret = zevtchan->zn_callback(zonename, zid, newstate,
5692cf8f45c7Sdstaff 			    oldstate, when, zevtchan->zn_private);
5693cf8f45c7Sdstaff 		}
5694cf8f45c7Sdstaff 
5695cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5696cf8f45c7Sdstaff 		nvlist_free(l);
5697cf8f45c7Sdstaff 		return (ret);
5698cf8f45c7Sdstaff 	} else {
5699cf8f45c7Sdstaff 		/*
5700cf8f45c7Sdstaff 		 * We have received an event in an unknown subclass. Ignore.
5701cf8f45c7Sdstaff 		 */
5702cf8f45c7Sdstaff 		zevtchan->zn_failure_count = 0;
5703cf8f45c7Sdstaff 		return (0);
5704cf8f45c7Sdstaff 	}
5705cf8f45c7Sdstaff }
5706cf8f45c7Sdstaff 
5707cf8f45c7Sdstaff static int
5708cf8f45c7Sdstaff zn_cb(sysevent_t *ev, void *p)
5709cf8f45c7Sdstaff {
5710cf8f45c7Sdstaff 	struct znotify *zevtchan = p;
5711cf8f45c7Sdstaff 	int error;
5712cf8f45c7Sdstaff 
5713cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5714cf8f45c7Sdstaff 
5715cf8f45c7Sdstaff 	if (is_ping(ev) && !is_my_ping(ev)) {
5716cf8f45c7Sdstaff 		(void) pthread_mutex_unlock((&zevtchan->zn_mutex));
5717cf8f45c7Sdstaff 		return (0);
5718cf8f45c7Sdstaff 	}
5719cf8f45c7Sdstaff 
5720cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_LOCKED) {
5721cf8f45c7Sdstaff 		assert(!is_ping(ev));
5722cf8f45c7Sdstaff 		zevtchan->zn_failed = B_TRUE;
5723cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5724cf8f45c7Sdstaff 		return (0);
5725cf8f45c7Sdstaff 	}
5726cf8f45c7Sdstaff 
5727cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_INFLIGHT) {
5728cf8f45c7Sdstaff 		if (is_ping(ev)) {
5729cf8f45c7Sdstaff 			zevtchan->zn_state = ZN_PING_RECEIVED;
5730cf8f45c7Sdstaff 			(void) pthread_cond_signal(&(zevtchan->zn_cond));
5731cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5732cf8f45c7Sdstaff 			return (0);
5733cf8f45c7Sdstaff 		} else {
5734cf8f45c7Sdstaff 			zevtchan->zn_failed = B_TRUE;
5735cf8f45c7Sdstaff 			(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5736cf8f45c7Sdstaff 			return (0);
5737cf8f45c7Sdstaff 		}
5738cf8f45c7Sdstaff 	}
5739cf8f45c7Sdstaff 
5740cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED) {
5741cf8f45c7Sdstaff 
5742cf8f45c7Sdstaff 		error = do_callback(zevtchan, ev);
5743cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5744cf8f45c7Sdstaff 		/*
5745cf8f45c7Sdstaff 		 * Every ENOMEM failure causes do_callback to increment
5746cf8f45c7Sdstaff 		 * zn_failure_count and every success causes it to
5747cf8f45c7Sdstaff 		 * set zn_failure_count to zero.  If we got EAGAIN,
5748cf8f45c7Sdstaff 		 * we will sleep for zn_failure_count seconds and return
5749cf8f45c7Sdstaff 		 * EAGAIN to gpec to try again.
5750cf8f45c7Sdstaff 		 *
5751cf8f45c7Sdstaff 		 * After 55 seconds, or 10 try's we give up and drop the
5752cf8f45c7Sdstaff 		 * event.
5753cf8f45c7Sdstaff 		 */
5754cf8f45c7Sdstaff 		if (error == EAGAIN) {
5755cf8f45c7Sdstaff 			if (zevtchan->zn_failure_count > ZONE_CB_RETRY_COUNT) {
5756cf8f45c7Sdstaff 				return (0);
5757cf8f45c7Sdstaff 			}
5758cf8f45c7Sdstaff 			(void) sleep(zevtchan->zn_failure_count);
5759cf8f45c7Sdstaff 		}
5760cf8f45c7Sdstaff 		return (error);
5761cf8f45c7Sdstaff 	}
5762cf8f45c7Sdstaff 
5763cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_PING_RECEIVED) {
5764cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5765cf8f45c7Sdstaff 		return (0);
5766cf8f45c7Sdstaff 	}
5767cf8f45c7Sdstaff 
5768cf8f45c7Sdstaff 	abort();
5769cf8f45c7Sdstaff 	return (0);
5770cf8f45c7Sdstaff }
5771cf8f45c7Sdstaff 
5772cf8f45c7Sdstaff void
5773cf8f45c7Sdstaff zonecfg_notify_critical_enter(void *h)
5774cf8f45c7Sdstaff {
5775cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5776cf8f45c7Sdstaff 
5777cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_bigmutex));
5778cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_LOCKED;
5779cf8f45c7Sdstaff }
5780cf8f45c7Sdstaff 
5781cf8f45c7Sdstaff int
5782cf8f45c7Sdstaff zonecfg_notify_critical_exit(void * h)
5783cf8f45c7Sdstaff {
5784cf8f45c7Sdstaff 
5785cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5786cf8f45c7Sdstaff 
5787cf8f45c7Sdstaff 	if (zevtchan->zn_state == ZN_UNLOCKED)
5788cf8f45c7Sdstaff 		return (0);
5789cf8f45c7Sdstaff 
5790cf8f45c7Sdstaff 	(void) pthread_mutex_lock(&(zevtchan->zn_mutex));
5791cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_PING_INFLIGHT;
5792cf8f45c7Sdstaff 
5793ee519a1fSgjelinek 	(void) sysevent_evc_publish(zevtchan->zn_eventchan,
5794ee519a1fSgjelinek 	    ZONE_EVENT_STATUS_CLASS,
5795cf8f45c7Sdstaff 	    ZONE_EVENT_PING_SUBCLASS, ZONE_EVENT_PING_PUBLISHER,
5796cf8f45c7Sdstaff 	    zevtchan->zn_subscriber_id, NULL, EVCH_SLEEP);
5797cf8f45c7Sdstaff 
5798cf8f45c7Sdstaff 	while (zevtchan->zn_state != ZN_PING_RECEIVED) {
5799cf8f45c7Sdstaff 		(void) pthread_cond_wait(&(zevtchan->zn_cond),
5800cf8f45c7Sdstaff 		    &(zevtchan->zn_mutex));
5801cf8f45c7Sdstaff 	}
5802cf8f45c7Sdstaff 
5803cf8f45c7Sdstaff 	if (zevtchan->zn_failed == B_TRUE) {
5804cf8f45c7Sdstaff 		zevtchan->zn_state = ZN_LOCKED;
5805cf8f45c7Sdstaff 		zevtchan->zn_failed = B_FALSE;
5806cf8f45c7Sdstaff 		(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5807cf8f45c7Sdstaff 		return (1);
5808cf8f45c7Sdstaff 	}
5809cf8f45c7Sdstaff 
5810cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5811cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_mutex));
5812cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5813cf8f45c7Sdstaff 	return (0);
5814cf8f45c7Sdstaff }
5815cf8f45c7Sdstaff 
5816cf8f45c7Sdstaff void
5817cf8f45c7Sdstaff zonecfg_notify_critical_abort(void *h)
5818cf8f45c7Sdstaff {
5819cf8f45c7Sdstaff 	struct znotify *zevtchan = h;
5820cf8f45c7Sdstaff 
5821cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5822cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
5823cf8f45c7Sdstaff 	/*
5824cf8f45c7Sdstaff 	 * Don't do anything about zn_lock. If it is held, it could only be
5825cf8f45c7Sdstaff 	 * held by zn_cb and it will be unlocked soon.
5826cf8f45c7Sdstaff 	 */
5827cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&(zevtchan->zn_bigmutex));
5828cf8f45c7Sdstaff }
5829cf8f45c7Sdstaff 
5830cf8f45c7Sdstaff void *
5831cf8f45c7Sdstaff zonecfg_notify_bind(int(*func)(const char *zonename, zoneid_t zid,
5832cf8f45c7Sdstaff     const char *newstate, const char *oldstate, hrtime_t when, void *p),
5833cf8f45c7Sdstaff     void *p)
5834cf8f45c7Sdstaff {
5835cf8f45c7Sdstaff 	struct znotify *zevtchan;
5836cf8f45c7Sdstaff 	int i = 1;
5837cf8f45c7Sdstaff 	int r;
5838cf8f45c7Sdstaff 
5839cf8f45c7Sdstaff 	zevtchan = malloc(sizeof (struct znotify));
5840cf8f45c7Sdstaff 
5841cf8f45c7Sdstaff 	if (zevtchan == NULL)
5842cf8f45c7Sdstaff 		return (NULL);
5843cf8f45c7Sdstaff 
5844cf8f45c7Sdstaff 	zevtchan->zn_private = p;
5845cf8f45c7Sdstaff 	zevtchan->zn_callback = func;
5846cf8f45c7Sdstaff 	zevtchan->zn_state = ZN_UNLOCKED;
5847cf8f45c7Sdstaff 	zevtchan->zn_failed = B_FALSE;
5848cf8f45c7Sdstaff 
5849cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_mutex), NULL))
58509d4be64eSdstaff 		goto out3;
5851cf8f45c7Sdstaff 	if (pthread_cond_init(&(zevtchan->zn_cond), NULL)) {
5852cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
58539d4be64eSdstaff 		goto out3;
5854cf8f45c7Sdstaff 	}
5855cf8f45c7Sdstaff 	if (pthread_mutex_init(&(zevtchan->zn_bigmutex), NULL)) {
5856cf8f45c7Sdstaff 		(void) pthread_mutex_destroy(&(zevtchan->zn_mutex));
5857cf8f45c7Sdstaff 		(void) pthread_cond_destroy(&(zevtchan->zn_cond));
58589d4be64eSdstaff 		goto out3;
5859cf8f45c7Sdstaff 	}
5860cf8f45c7Sdstaff 
5861cf8f45c7Sdstaff 	if (sysevent_evc_bind(ZONE_EVENT_CHANNEL, &(zevtchan->zn_eventchan),
5862cf8f45c7Sdstaff 	    0) != 0)
5863cf8f45c7Sdstaff 		goto out2;
5864cf8f45c7Sdstaff 
5865cf8f45c7Sdstaff 	do {
5866cf8f45c7Sdstaff 		/*
5867cf8f45c7Sdstaff 		 * At 4 digits the subscriber ID gets too long and we have
5868cf8f45c7Sdstaff 		 * no chance of successfully registering.
5869cf8f45c7Sdstaff 		 */
5870cf8f45c7Sdstaff 		if (i > 999)
58719d4be64eSdstaff 			goto out1;
5872cf8f45c7Sdstaff 
5873cf8f45c7Sdstaff 		(void) sprintf(zevtchan->zn_subscriber_id, "zone_%li_%i",
5874cf8f45c7Sdstaff 		    getpid() % 999999l, i);
5875cf8f45c7Sdstaff 
5876cf8f45c7Sdstaff 		r = sysevent_evc_subscribe(zevtchan->zn_eventchan,
5877cf8f45c7Sdstaff 		    zevtchan->zn_subscriber_id, ZONE_EVENT_STATUS_CLASS, zn_cb,
5878cf8f45c7Sdstaff 		    zevtchan, 0);
5879cf8f45c7Sdstaff 
5880cf8f45c7Sdstaff 		i++;
5881cf8f45c7Sdstaff 
5882cf8f45c7Sdstaff 	} while (r);
5883cf8f45c7Sdstaff 
5884cf8f45c7Sdstaff 	return (zevtchan);
58859d4be64eSdstaff out1:
5886cf8f45c7Sdstaff 	sysevent_evc_unbind(zevtchan->zn_eventchan);
58879d4be64eSdstaff out2:
5888cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&zevtchan->zn_mutex);
5889cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&zevtchan->zn_cond);
5890cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&(zevtchan->zn_bigmutex));
58919d4be64eSdstaff out3:
5892cf8f45c7Sdstaff 	free(zevtchan);
5893cf8f45c7Sdstaff 
5894cf8f45c7Sdstaff 	return (NULL);
5895cf8f45c7Sdstaff }
5896cf8f45c7Sdstaff 
5897cf8f45c7Sdstaff void
5898cf8f45c7Sdstaff zonecfg_notify_unbind(void *handle)
5899cf8f45c7Sdstaff {
5900cf8f45c7Sdstaff 
5901cf8f45c7Sdstaff 	int ret;
5902cf8f45c7Sdstaff 
5903cf8f45c7Sdstaff 	sysevent_evc_unbind(((struct znotify *)handle)->zn_eventchan);
5904cf8f45c7Sdstaff 	/*
5905cf8f45c7Sdstaff 	 * Check that all evc threads have gone away. This should be
5906cf8f45c7Sdstaff 	 * enforced by sysevent_evc_unbind.
5907cf8f45c7Sdstaff 	 */
5908cf8f45c7Sdstaff 	ret = pthread_mutex_trylock(&((struct znotify *)handle)->zn_mutex);
5909cf8f45c7Sdstaff 
5910cf8f45c7Sdstaff 	if (ret)
5911cf8f45c7Sdstaff 		abort();
5912cf8f45c7Sdstaff 
5913cf8f45c7Sdstaff 	(void) pthread_mutex_unlock(&((struct znotify *)handle)->zn_mutex);
5914cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_mutex);
5915cf8f45c7Sdstaff 	(void) pthread_cond_destroy(&((struct znotify *)handle)->zn_cond);
5916cf8f45c7Sdstaff 	(void) pthread_mutex_destroy(&((struct znotify *)handle)->zn_bigmutex);
5917cf8f45c7Sdstaff 
5918cf8f45c7Sdstaff 	free(handle);
5919cf8f45c7Sdstaff }
5920cf8f45c7Sdstaff 
5921fa9e4066Sahrens static int
5922fa9e4066Sahrens zonecfg_add_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5923fa9e4066Sahrens {
5924fa9e4066Sahrens 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
5925fa9e4066Sahrens 	int err;
5926fa9e4066Sahrens 
5927fa9e4066Sahrens 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_DATASET, NULL);
5928fa9e4066Sahrens 	if ((err = newprop(newnode, DTD_ATTR_NAME,
5929fa9e4066Sahrens 	    tabptr->zone_dataset_name)) != Z_OK)
5930fa9e4066Sahrens 		return (err);
5931fa9e4066Sahrens 	return (Z_OK);
5932fa9e4066Sahrens }
5933fa9e4066Sahrens 
5934fa9e4066Sahrens int
5935fa9e4066Sahrens zonecfg_add_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5936fa9e4066Sahrens {
5937fa9e4066Sahrens 	int err;
5938fa9e4066Sahrens 
5939fa9e4066Sahrens 	if (tabptr == NULL)
5940fa9e4066Sahrens 		return (Z_INVAL);
5941fa9e4066Sahrens 
5942fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5943fa9e4066Sahrens 		return (err);
5944fa9e4066Sahrens 
5945fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, tabptr)) != Z_OK)
5946fa9e4066Sahrens 		return (err);
5947fa9e4066Sahrens 
5948fa9e4066Sahrens 	return (Z_OK);
5949fa9e4066Sahrens }
5950fa9e4066Sahrens 
5951fa9e4066Sahrens static int
5952fa9e4066Sahrens zonecfg_delete_ds_core(zone_dochandle_t handle, struct zone_dstab *tabptr)
5953fa9e4066Sahrens {
5954fa9e4066Sahrens 	xmlNodePtr cur = handle->zone_dh_cur;
5955fa9e4066Sahrens 
5956fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
5957fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
5958fa9e4066Sahrens 			continue;
5959fa9e4066Sahrens 
5960fa9e4066Sahrens 		if (match_prop(cur, DTD_ATTR_NAME,
5961fa9e4066Sahrens 		    tabptr->zone_dataset_name)) {
5962fa9e4066Sahrens 			xmlUnlinkNode(cur);
5963fa9e4066Sahrens 			xmlFreeNode(cur);
5964fa9e4066Sahrens 			return (Z_OK);
5965fa9e4066Sahrens 		}
5966fa9e4066Sahrens 	}
5967fa9e4066Sahrens 	return (Z_NO_RESOURCE_ID);
5968fa9e4066Sahrens }
5969fa9e4066Sahrens 
5970fa9e4066Sahrens int
5971fa9e4066Sahrens zonecfg_delete_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
5972fa9e4066Sahrens {
5973fa9e4066Sahrens 	int err;
5974fa9e4066Sahrens 
5975fa9e4066Sahrens 	if (tabptr == NULL)
5976fa9e4066Sahrens 		return (Z_INVAL);
5977fa9e4066Sahrens 
5978fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5979fa9e4066Sahrens 		return (err);
5980fa9e4066Sahrens 
5981fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, tabptr)) != Z_OK)
5982fa9e4066Sahrens 		return (err);
5983fa9e4066Sahrens 
5984fa9e4066Sahrens 	return (Z_OK);
5985fa9e4066Sahrens }
5986fa9e4066Sahrens 
5987fa9e4066Sahrens int
5988fa9e4066Sahrens zonecfg_modify_ds(
5989fa9e4066Sahrens 	zone_dochandle_t handle,
5990fa9e4066Sahrens 	struct zone_dstab *oldtabptr,
5991fa9e4066Sahrens 	struct zone_dstab *newtabptr)
5992fa9e4066Sahrens {
5993fa9e4066Sahrens 	int err;
5994fa9e4066Sahrens 
5995fa9e4066Sahrens 	if (oldtabptr == NULL || newtabptr == NULL)
5996fa9e4066Sahrens 		return (Z_INVAL);
5997fa9e4066Sahrens 
5998fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
5999fa9e4066Sahrens 		return (err);
6000fa9e4066Sahrens 
6001fa9e4066Sahrens 	if ((err = zonecfg_delete_ds_core(handle, oldtabptr)) != Z_OK)
6002fa9e4066Sahrens 		return (err);
6003fa9e4066Sahrens 
6004fa9e4066Sahrens 	if ((err = zonecfg_add_ds_core(handle, newtabptr)) != Z_OK)
6005fa9e4066Sahrens 		return (err);
6006fa9e4066Sahrens 
6007fa9e4066Sahrens 	return (Z_OK);
6008fa9e4066Sahrens }
6009fa9e4066Sahrens 
6010fa9e4066Sahrens int
6011fa9e4066Sahrens zonecfg_lookup_ds(zone_dochandle_t handle, struct zone_dstab *tabptr)
6012fa9e4066Sahrens {
6013fa9e4066Sahrens 	xmlNodePtr cur, firstmatch;
6014fa9e4066Sahrens 	int err;
6015fa9e4066Sahrens 	char dataset[MAXNAMELEN];
6016fa9e4066Sahrens 
6017fa9e4066Sahrens 	if (tabptr == NULL)
6018fa9e4066Sahrens 		return (Z_INVAL);
6019fa9e4066Sahrens 
6020fa9e4066Sahrens 	if ((err = operation_prep(handle)) != Z_OK)
6021fa9e4066Sahrens 		return (err);
6022fa9e4066Sahrens 
6023fa9e4066Sahrens 	cur = handle->zone_dh_cur;
6024fa9e4066Sahrens 	firstmatch = NULL;
6025fa9e4066Sahrens 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
6026fa9e4066Sahrens 		if (xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6027fa9e4066Sahrens 			continue;
6028fa9e4066Sahrens 		if (strlen(tabptr->zone_dataset_name) > 0) {
6029fa9e4066Sahrens 			if ((fetchprop(cur, DTD_ATTR_NAME, dataset,
6030fa9e4066Sahrens 			    sizeof (dataset)) == Z_OK) &&
6031fa9e4066Sahrens 			    (strcmp(tabptr->zone_dataset_name,
6032fa9e4066Sahrens 			    dataset) == 0)) {
6033fa9e4066Sahrens 				if (firstmatch == NULL)
6034fa9e4066Sahrens 					firstmatch = cur;
6035fa9e4066Sahrens 				else
6036fa9e4066Sahrens 					return (Z_INSUFFICIENT_SPEC);
6037fa9e4066Sahrens 			}
6038fa9e4066Sahrens 		}
6039fa9e4066Sahrens 	}
6040fa9e4066Sahrens 	if (firstmatch == NULL)
6041fa9e4066Sahrens 		return (Z_NO_RESOURCE_ID);
6042fa9e4066Sahrens 
6043fa9e4066Sahrens 	cur = firstmatch;
6044fa9e4066Sahrens 
6045fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6046fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK)
6047fa9e4066Sahrens 		return (err);
6048fa9e4066Sahrens 
6049fa9e4066Sahrens 	return (Z_OK);
6050fa9e4066Sahrens }
6051fa9e4066Sahrens 
6052fa9e4066Sahrens int
6053fa9e4066Sahrens zonecfg_setdsent(zone_dochandle_t handle)
6054fa9e4066Sahrens {
6055fa9e4066Sahrens 	return (zonecfg_setent(handle));
6056fa9e4066Sahrens }
6057fa9e4066Sahrens 
6058fa9e4066Sahrens int
6059fa9e4066Sahrens zonecfg_getdsent(zone_dochandle_t handle, struct zone_dstab *tabptr)
6060fa9e4066Sahrens {
6061fa9e4066Sahrens 	xmlNodePtr cur;
6062fa9e4066Sahrens 	int err;
6063fa9e4066Sahrens 
6064fa9e4066Sahrens 	if (handle == NULL)
6065fa9e4066Sahrens 		return (Z_INVAL);
6066fa9e4066Sahrens 
6067fa9e4066Sahrens 	if ((cur = handle->zone_dh_cur) == NULL)
6068fa9e4066Sahrens 		return (Z_NO_ENTRY);
6069fa9e4066Sahrens 
6070fa9e4066Sahrens 	for (; cur != NULL; cur = cur->next)
6071fa9e4066Sahrens 		if (!xmlStrcmp(cur->name, DTD_ELEM_DATASET))
6072fa9e4066Sahrens 			break;
6073fa9e4066Sahrens 	if (cur == NULL) {
6074fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6075fa9e4066Sahrens 		return (Z_NO_ENTRY);
6076fa9e4066Sahrens 	}
6077fa9e4066Sahrens 
6078fa9e4066Sahrens 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_dataset_name,
6079fa9e4066Sahrens 	    sizeof (tabptr->zone_dataset_name))) != Z_OK) {
6080fa9e4066Sahrens 		handle->zone_dh_cur = handle->zone_dh_top;
6081fa9e4066Sahrens 		return (err);
6082fa9e4066Sahrens 	}
6083fa9e4066Sahrens 
6084fa9e4066Sahrens 	handle->zone_dh_cur = cur->next;
6085fa9e4066Sahrens 	return (Z_OK);
6086fa9e4066Sahrens }
6087fa9e4066Sahrens 
6088fa9e4066Sahrens int
6089fa9e4066Sahrens zonecfg_enddsent(zone_dochandle_t handle)
6090fa9e4066Sahrens {
6091fa9e4066Sahrens 	return (zonecfg_endent(handle));
6092fa9e4066Sahrens }
6093ee519a1fSgjelinek 
60940209230bSgjelinek /*
60950209230bSgjelinek  * Support for aliased rctls; that is, rctls that have simplified names in
60960209230bSgjelinek  * zonecfg.  For example, max-lwps is an alias for a well defined zone.max-lwps
60970209230bSgjelinek  * rctl.  If there are multiple existing values for one of these rctls or if
60980209230bSgjelinek  * there is a single value that does not match the well defined template (i.e.
60990209230bSgjelinek  * it has a different action) then we cannot treat the rctl as having an alias
61000209230bSgjelinek  * so we return Z_ALIAS_DISALLOW.  That means that the rctl cannot be
61010209230bSgjelinek  * managed in zonecfg via an alias and that the standard rctl syntax must be
61020209230bSgjelinek  * used.
61030209230bSgjelinek  *
61040209230bSgjelinek  * The possible return values are:
61050209230bSgjelinek  *	Z_NO_PROPERTY_ID - invalid alias name
61060209230bSgjelinek  *	Z_ALIAS_DISALLOW - pre-existing, incompatible rctl definition
61070209230bSgjelinek  *	Z_NO_ENTRY - no rctl is configured for this alias
61080209230bSgjelinek  *	Z_OK - we got a valid rctl for the specified alias
61090209230bSgjelinek  */
61100209230bSgjelinek int
61110209230bSgjelinek zonecfg_get_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t *rval)
61120209230bSgjelinek {
61130209230bSgjelinek 	boolean_t found = B_FALSE;
61140209230bSgjelinek 	boolean_t found_val = B_FALSE;
61150209230bSgjelinek 	xmlNodePtr cur, val;
61160209230bSgjelinek 	char savedname[MAXNAMELEN];
61170209230bSgjelinek 	struct zone_rctlvaltab rctl;
61180209230bSgjelinek 	int i;
61190209230bSgjelinek 	int err;
61200209230bSgjelinek 
61210209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
61220209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
61230209230bSgjelinek 			break;
61240209230bSgjelinek 
61250209230bSgjelinek 	if (aliases[i].shortname == NULL)
61260209230bSgjelinek 		return (Z_NO_PROPERTY_ID);
61270209230bSgjelinek 
61280209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
61290209230bSgjelinek 		return (err);
61300209230bSgjelinek 
61310209230bSgjelinek 	cur = handle->zone_dh_cur;
61320209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
61330209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_RCTL) != 0)
61340209230bSgjelinek 			continue;
61350209230bSgjelinek 		if ((fetchprop(cur, DTD_ATTR_NAME, savedname,
61360209230bSgjelinek 		    sizeof (savedname)) == Z_OK) &&
61370209230bSgjelinek 		    (strcmp(savedname, aliases[i].realname) == 0)) {
61380209230bSgjelinek 
61390209230bSgjelinek 			/*
61400209230bSgjelinek 			 * If we already saw one of these, we can't have an
61410209230bSgjelinek 			 * alias since we just found another.
61420209230bSgjelinek 			 */
61430209230bSgjelinek 			if (found)
61440209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
61450209230bSgjelinek 			found = B_TRUE;
61460209230bSgjelinek 
61470209230bSgjelinek 			for (val = cur->xmlChildrenNode; val != NULL;
61480209230bSgjelinek 			    val = val->next) {
61490209230bSgjelinek 				/*
61500209230bSgjelinek 				 * If we already have one value, we can't have
61510209230bSgjelinek 				 * an alias since we just found another.
61520209230bSgjelinek 				 */
61530209230bSgjelinek 				if (found_val)
61540209230bSgjelinek 					return (Z_ALIAS_DISALLOW);
61550209230bSgjelinek 				found_val = B_TRUE;
61560209230bSgjelinek 
61570209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_PRIV,
61580209230bSgjelinek 				    rctl.zone_rctlval_priv,
61590209230bSgjelinek 				    sizeof (rctl.zone_rctlval_priv)) != Z_OK))
61600209230bSgjelinek 					break;
61610209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_LIMIT,
61620209230bSgjelinek 				    rctl.zone_rctlval_limit,
61630209230bSgjelinek 				    sizeof (rctl.zone_rctlval_limit)) != Z_OK))
61640209230bSgjelinek 					break;
61650209230bSgjelinek 				if ((fetchprop(val, DTD_ATTR_ACTION,
61660209230bSgjelinek 				    rctl.zone_rctlval_action,
61670209230bSgjelinek 				    sizeof (rctl.zone_rctlval_action)) != Z_OK))
61680209230bSgjelinek 					break;
61690209230bSgjelinek 			}
61700209230bSgjelinek 
61710209230bSgjelinek 			/* check priv and action match the expected vals */
61720209230bSgjelinek 			if (strcmp(rctl.zone_rctlval_priv,
61730209230bSgjelinek 			    aliases[i].priv) != 0 ||
61740209230bSgjelinek 			    strcmp(rctl.zone_rctlval_action,
61750209230bSgjelinek 			    aliases[i].action) != 0)
61760209230bSgjelinek 				return (Z_ALIAS_DISALLOW);
61770209230bSgjelinek 		}
61780209230bSgjelinek 	}
61790209230bSgjelinek 
61800209230bSgjelinek 	if (found) {
61810209230bSgjelinek 		*rval = strtoull(rctl.zone_rctlval_limit, NULL, 10);
61820209230bSgjelinek 		return (Z_OK);
61830209230bSgjelinek 	}
61840209230bSgjelinek 
61850209230bSgjelinek 	return (Z_NO_ENTRY);
61860209230bSgjelinek }
61870209230bSgjelinek 
61880209230bSgjelinek int
61890209230bSgjelinek zonecfg_rm_aliased_rctl(zone_dochandle_t handle, char *name)
61900209230bSgjelinek {
61910209230bSgjelinek 	int i;
61920209230bSgjelinek 	uint64_t val;
61930209230bSgjelinek 	struct zone_rctltab rctltab;
61940209230bSgjelinek 
61950209230bSgjelinek 	/*
61960209230bSgjelinek 	 * First check that we have a valid aliased rctl to remove.
61970209230bSgjelinek 	 * This will catch an rctl entry with non-standard values or
61980209230bSgjelinek 	 * multiple rctl values for this name.  We need to ignore those
61990209230bSgjelinek 	 * rctl entries.
62000209230bSgjelinek 	 */
62010209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, name, &val) != Z_OK)
62020209230bSgjelinek 		return (Z_OK);
62030209230bSgjelinek 
62040209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
62050209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
62060209230bSgjelinek 			break;
62070209230bSgjelinek 
62080209230bSgjelinek 	if (aliases[i].shortname == NULL)
62090209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
62100209230bSgjelinek 
62110209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
62120209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
62130209230bSgjelinek 
62140209230bSgjelinek 	return (zonecfg_delete_rctl(handle, &rctltab));
62150209230bSgjelinek }
62160209230bSgjelinek 
62170209230bSgjelinek boolean_t
62180209230bSgjelinek zonecfg_aliased_rctl_ok(zone_dochandle_t handle, char *name)
62190209230bSgjelinek {
62200209230bSgjelinek 	uint64_t tmp_val;
62210209230bSgjelinek 
62220209230bSgjelinek 	switch (zonecfg_get_aliased_rctl(handle, name, &tmp_val)) {
62230209230bSgjelinek 	case Z_OK:
62240209230bSgjelinek 		/*FALLTHRU*/
62250209230bSgjelinek 	case Z_NO_ENTRY:
62260209230bSgjelinek 		return (B_TRUE);
62270209230bSgjelinek 	default:
62280209230bSgjelinek 		return (B_FALSE);
62290209230bSgjelinek 	}
62300209230bSgjelinek }
62310209230bSgjelinek 
62320209230bSgjelinek int
62330209230bSgjelinek zonecfg_set_aliased_rctl(zone_dochandle_t handle, char *name, uint64_t val)
62340209230bSgjelinek {
62350209230bSgjelinek 	int i;
62360209230bSgjelinek 	int err;
62370209230bSgjelinek 	struct zone_rctltab rctltab;
62380209230bSgjelinek 	struct zone_rctlvaltab *rctlvaltab;
62390209230bSgjelinek 	char buf[128];
62400209230bSgjelinek 
62410209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, name))
62420209230bSgjelinek 		return (Z_ALIAS_DISALLOW);
62430209230bSgjelinek 
62440209230bSgjelinek 	for (i = 0; aliases[i].shortname != NULL; i++)
62450209230bSgjelinek 		if (strcmp(name, aliases[i].shortname) == 0)
62460209230bSgjelinek 			break;
62470209230bSgjelinek 
62480209230bSgjelinek 	if (aliases[i].shortname == NULL)
62490209230bSgjelinek 		return (Z_NO_RESOURCE_ID);
62500209230bSgjelinek 
62510209230bSgjelinek 	/* remove any pre-existing definition for this rctl */
62520209230bSgjelinek 	(void) zonecfg_rm_aliased_rctl(handle, name);
62530209230bSgjelinek 
62540209230bSgjelinek 	(void) strlcpy(rctltab.zone_rctl_name, aliases[i].realname,
62550209230bSgjelinek 	    sizeof (rctltab.zone_rctl_name));
62560209230bSgjelinek 
62570209230bSgjelinek 	rctltab.zone_rctl_valptr = NULL;
62580209230bSgjelinek 
62590209230bSgjelinek 	if ((rctlvaltab = calloc(1, sizeof (struct zone_rctlvaltab))) == NULL)
62600209230bSgjelinek 		return (Z_NOMEM);
62610209230bSgjelinek 
62620209230bSgjelinek 	(void) snprintf(buf, sizeof (buf), "%llu", (long long)val);
62630209230bSgjelinek 
62640209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_priv, aliases[i].priv,
62650209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_priv));
62660209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_limit, buf,
62670209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_limit));
62680209230bSgjelinek 	(void) strlcpy(rctlvaltab->zone_rctlval_action, aliases[i].action,
62690209230bSgjelinek 	    sizeof (rctlvaltab->zone_rctlval_action));
62700209230bSgjelinek 
62710209230bSgjelinek 	rctlvaltab->zone_rctlval_next = NULL;
62720209230bSgjelinek 
62730209230bSgjelinek 	if ((err = zonecfg_add_rctl_value(&rctltab, rctlvaltab)) != Z_OK)
62740209230bSgjelinek 		return (err);
62750209230bSgjelinek 
62760209230bSgjelinek 	return (zonecfg_add_rctl(handle, &rctltab));
62770209230bSgjelinek }
62780209230bSgjelinek 
62790209230bSgjelinek static int
62800209230bSgjelinek delete_tmp_pool(zone_dochandle_t handle)
62810209230bSgjelinek {
62820209230bSgjelinek 	int err;
62830209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
62840209230bSgjelinek 
62850209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
62860209230bSgjelinek 		return (err);
62870209230bSgjelinek 
62880209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
62890209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
62900209230bSgjelinek 			xmlUnlinkNode(cur);
62910209230bSgjelinek 			xmlFreeNode(cur);
62920209230bSgjelinek 			return (Z_OK);
62930209230bSgjelinek 		}
62940209230bSgjelinek 	}
62950209230bSgjelinek 
62960209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
62970209230bSgjelinek }
62980209230bSgjelinek 
62990209230bSgjelinek static int
63000209230bSgjelinek modify_tmp_pool(zone_dochandle_t handle, char *pool_importance)
63010209230bSgjelinek {
63020209230bSgjelinek 	int err;
63030209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
63040209230bSgjelinek 	xmlNodePtr newnode;
63050209230bSgjelinek 
63060209230bSgjelinek 	err = delete_tmp_pool(handle);
63070209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
63080209230bSgjelinek 		return (err);
63090209230bSgjelinek 
63100209230bSgjelinek 	if (*pool_importance != '\0') {
63110209230bSgjelinek 		if ((err = operation_prep(handle)) != Z_OK)
63120209230bSgjelinek 			return (err);
63130209230bSgjelinek 
63140209230bSgjelinek 		newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_TMPPOOL, NULL);
63150209230bSgjelinek 		if ((err = newprop(newnode, DTD_ATTR_IMPORTANCE,
63160209230bSgjelinek 		    pool_importance)) != Z_OK)
63170209230bSgjelinek 			return (err);
63180209230bSgjelinek 	}
63190209230bSgjelinek 
63200209230bSgjelinek 	return (Z_OK);
63210209230bSgjelinek }
63220209230bSgjelinek 
63230209230bSgjelinek static int
63240209230bSgjelinek add_pset_core(zone_dochandle_t handle, struct zone_psettab *tabptr)
63250209230bSgjelinek {
63260209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
63270209230bSgjelinek 	int err;
63280209230bSgjelinek 
63290209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_PSET, NULL);
63300209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MIN,
63310209230bSgjelinek 	    tabptr->zone_ncpu_min)) != Z_OK)
63320209230bSgjelinek 		return (err);
63330209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_NCPU_MAX,
63340209230bSgjelinek 	    tabptr->zone_ncpu_max)) != Z_OK)
63350209230bSgjelinek 		return (err);
63360209230bSgjelinek 
63370209230bSgjelinek 	if ((err = modify_tmp_pool(handle, tabptr->zone_importance)) != Z_OK)
63380209230bSgjelinek 		return (err);
63390209230bSgjelinek 
63400209230bSgjelinek 	return (Z_OK);
63410209230bSgjelinek }
63420209230bSgjelinek 
63430209230bSgjelinek int
63440209230bSgjelinek zonecfg_add_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
63450209230bSgjelinek {
63460209230bSgjelinek 	int err;
63470209230bSgjelinek 
63480209230bSgjelinek 	if (tabptr == NULL)
63490209230bSgjelinek 		return (Z_INVAL);
63500209230bSgjelinek 
63510209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
63520209230bSgjelinek 		return (err);
63530209230bSgjelinek 
63540209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
63550209230bSgjelinek 		return (err);
63560209230bSgjelinek 
63570209230bSgjelinek 	return (Z_OK);
63580209230bSgjelinek }
63590209230bSgjelinek 
63600209230bSgjelinek int
63610209230bSgjelinek zonecfg_delete_pset(zone_dochandle_t handle)
63620209230bSgjelinek {
63630209230bSgjelinek 	int err;
63640209230bSgjelinek 	int res = Z_NO_RESOURCE_ID;
63650209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
63660209230bSgjelinek 
63670209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
63680209230bSgjelinek 		return (err);
63690209230bSgjelinek 
63700209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
63710209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
63720209230bSgjelinek 			xmlUnlinkNode(cur);
63730209230bSgjelinek 			xmlFreeNode(cur);
63740209230bSgjelinek 			res = Z_OK;
63750209230bSgjelinek 			break;
63760209230bSgjelinek 		}
63770209230bSgjelinek 	}
63780209230bSgjelinek 
63790209230bSgjelinek 	/*
63800209230bSgjelinek 	 * Once we have msets, we should check that a mset
63810209230bSgjelinek 	 * do not exist before we delete the tmp_pool data.
63820209230bSgjelinek 	 */
63830209230bSgjelinek 	err = delete_tmp_pool(handle);
63840209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
63850209230bSgjelinek 		return (err);
63860209230bSgjelinek 
63870209230bSgjelinek 	return (res);
63880209230bSgjelinek }
63890209230bSgjelinek 
63900209230bSgjelinek int
63910209230bSgjelinek zonecfg_modify_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
63920209230bSgjelinek {
63930209230bSgjelinek 	int err;
63940209230bSgjelinek 
63950209230bSgjelinek 	if (tabptr == NULL)
63960209230bSgjelinek 		return (Z_INVAL);
63970209230bSgjelinek 
63980209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
63990209230bSgjelinek 		return (err);
64000209230bSgjelinek 
64010209230bSgjelinek 	if ((err = add_pset_core(handle, tabptr)) != Z_OK)
64020209230bSgjelinek 		return (err);
64030209230bSgjelinek 
64040209230bSgjelinek 	return (Z_OK);
64050209230bSgjelinek }
64060209230bSgjelinek 
64070209230bSgjelinek int
64080209230bSgjelinek zonecfg_lookup_pset(zone_dochandle_t handle, struct zone_psettab *tabptr)
64090209230bSgjelinek {
64100209230bSgjelinek 	xmlNodePtr cur;
64110209230bSgjelinek 	int err;
64120209230bSgjelinek 	int res = Z_NO_ENTRY;
64130209230bSgjelinek 
64140209230bSgjelinek 	if (tabptr == NULL)
64150209230bSgjelinek 		return (Z_INVAL);
64160209230bSgjelinek 
64170209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64180209230bSgjelinek 		return (err);
64190209230bSgjelinek 
64200209230bSgjelinek 	/* this is an optional component */
64210209230bSgjelinek 	tabptr->zone_importance[0] = '\0';
64220209230bSgjelinek 
64230209230bSgjelinek 	cur = handle->zone_dh_cur;
64240209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64250209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PSET) == 0) {
64260209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MIN,
64270209230bSgjelinek 			    tabptr->zone_ncpu_min,
64280209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_min))) != Z_OK) {
64290209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
64300209230bSgjelinek 				return (err);
64310209230bSgjelinek 			}
64320209230bSgjelinek 
64330209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_NCPU_MAX,
64340209230bSgjelinek 			    tabptr->zone_ncpu_max,
64350209230bSgjelinek 			    sizeof (tabptr->zone_ncpu_max))) != Z_OK) {
64360209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
64370209230bSgjelinek 				return (err);
64380209230bSgjelinek 			}
64390209230bSgjelinek 
64400209230bSgjelinek 			res = Z_OK;
64410209230bSgjelinek 
64420209230bSgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_TMPPOOL) == 0) {
64430209230bSgjelinek 			if ((err = fetchprop(cur, DTD_ATTR_IMPORTANCE,
64440209230bSgjelinek 			    tabptr->zone_importance,
64450209230bSgjelinek 			    sizeof (tabptr->zone_importance))) != Z_OK) {
64460209230bSgjelinek 				handle->zone_dh_cur = handle->zone_dh_top;
64470209230bSgjelinek 				return (err);
64480209230bSgjelinek 			}
64490209230bSgjelinek 		}
64500209230bSgjelinek 	}
64510209230bSgjelinek 
64520209230bSgjelinek 	return (res);
64530209230bSgjelinek }
64540209230bSgjelinek 
64550209230bSgjelinek int
64560209230bSgjelinek zonecfg_getpsetent(zone_dochandle_t handle, struct zone_psettab *tabptr)
64570209230bSgjelinek {
64580209230bSgjelinek 	int err;
64590209230bSgjelinek 
64600209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
64610209230bSgjelinek 		return (err);
64620209230bSgjelinek 
64630209230bSgjelinek 	err = zonecfg_lookup_pset(handle, tabptr);
64640209230bSgjelinek 
64650209230bSgjelinek 	(void) zonecfg_endent(handle);
64660209230bSgjelinek 
64670209230bSgjelinek 	return (err);
64680209230bSgjelinek }
64690209230bSgjelinek 
64700209230bSgjelinek static int
64710209230bSgjelinek add_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
64720209230bSgjelinek {
64730209230bSgjelinek 	xmlNodePtr newnode, cur = handle->zone_dh_cur;
64740209230bSgjelinek 	int err;
64750209230bSgjelinek 
64760209230bSgjelinek 	newnode = xmlNewTextChild(cur, NULL, DTD_ELEM_MCAP, NULL);
64770209230bSgjelinek 	if ((err = newprop(newnode, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap))
64780209230bSgjelinek 	    != Z_OK)
64790209230bSgjelinek 		return (err);
64800209230bSgjelinek 
64810209230bSgjelinek 	return (Z_OK);
64820209230bSgjelinek }
64830209230bSgjelinek 
64840209230bSgjelinek int
64850209230bSgjelinek zonecfg_delete_mcap(zone_dochandle_t handle)
64860209230bSgjelinek {
64870209230bSgjelinek 	int err;
64880209230bSgjelinek 	xmlNodePtr cur = handle->zone_dh_cur;
64890209230bSgjelinek 
64900209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
64910209230bSgjelinek 		return (err);
64920209230bSgjelinek 
64930209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
64940209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
64950209230bSgjelinek 			continue;
64960209230bSgjelinek 
64970209230bSgjelinek 		xmlUnlinkNode(cur);
64980209230bSgjelinek 		xmlFreeNode(cur);
64990209230bSgjelinek 		return (Z_OK);
65000209230bSgjelinek 	}
65010209230bSgjelinek 	return (Z_NO_RESOURCE_ID);
65020209230bSgjelinek }
65030209230bSgjelinek 
65040209230bSgjelinek int
65050209230bSgjelinek zonecfg_modify_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
65060209230bSgjelinek {
65070209230bSgjelinek 	int err;
65080209230bSgjelinek 
65090209230bSgjelinek 	if (tabptr == NULL)
65100209230bSgjelinek 		return (Z_INVAL);
65110209230bSgjelinek 
65120209230bSgjelinek 	err = zonecfg_delete_mcap(handle);
65130209230bSgjelinek 	/* it is ok if there is no mcap entry */
65140209230bSgjelinek 	if (err != Z_OK && err != Z_NO_RESOURCE_ID)
65150209230bSgjelinek 		return (err);
65160209230bSgjelinek 
65170209230bSgjelinek 	if ((err = add_mcap(handle, tabptr)) != Z_OK)
65180209230bSgjelinek 		return (err);
65190209230bSgjelinek 
65200209230bSgjelinek 	return (Z_OK);
65210209230bSgjelinek }
65220209230bSgjelinek 
65230209230bSgjelinek int
65240209230bSgjelinek zonecfg_lookup_mcap(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
65250209230bSgjelinek {
65260209230bSgjelinek 	xmlNodePtr cur;
65270209230bSgjelinek 	int err;
65280209230bSgjelinek 
65290209230bSgjelinek 	if (tabptr == NULL)
65300209230bSgjelinek 		return (Z_INVAL);
65310209230bSgjelinek 
65320209230bSgjelinek 	if ((err = operation_prep(handle)) != Z_OK)
65330209230bSgjelinek 		return (err);
65340209230bSgjelinek 
65350209230bSgjelinek 	cur = handle->zone_dh_cur;
65360209230bSgjelinek 	for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) {
65370209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) != 0)
65380209230bSgjelinek 			continue;
65390209230bSgjelinek 		if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP,
65400209230bSgjelinek 		    tabptr->zone_physmem_cap,
65410209230bSgjelinek 		    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
65420209230bSgjelinek 			handle->zone_dh_cur = handle->zone_dh_top;
65430209230bSgjelinek 			return (err);
65440209230bSgjelinek 		}
65450209230bSgjelinek 
65460209230bSgjelinek 		return (Z_OK);
65470209230bSgjelinek 	}
65480209230bSgjelinek 
65490209230bSgjelinek 	return (Z_NO_ENTRY);
65500209230bSgjelinek }
65510209230bSgjelinek 
65520209230bSgjelinek static int
65530209230bSgjelinek getmcapent_core(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
65540209230bSgjelinek {
65550209230bSgjelinek 	xmlNodePtr cur;
65560209230bSgjelinek 	int err;
65570209230bSgjelinek 
65580209230bSgjelinek 	if (handle == NULL)
65590209230bSgjelinek 		return (Z_INVAL);
65600209230bSgjelinek 
65610209230bSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
65620209230bSgjelinek 		return (Z_NO_ENTRY);
65630209230bSgjelinek 
65640209230bSgjelinek 	for (; cur != NULL; cur = cur->next)
65650209230bSgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_MCAP) == 0)
65660209230bSgjelinek 			break;
65670209230bSgjelinek 	if (cur == NULL) {
65680209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
65690209230bSgjelinek 		return (Z_NO_ENTRY);
65700209230bSgjelinek 	}
65710209230bSgjelinek 
65720209230bSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_PHYSCAP, tabptr->zone_physmem_cap,
65730209230bSgjelinek 	    sizeof (tabptr->zone_physmem_cap))) != Z_OK) {
65740209230bSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
65750209230bSgjelinek 		return (err);
65760209230bSgjelinek 	}
65770209230bSgjelinek 
65780209230bSgjelinek 	handle->zone_dh_cur = cur->next;
65790209230bSgjelinek 	return (Z_OK);
65800209230bSgjelinek }
65810209230bSgjelinek 
65820209230bSgjelinek int
65830209230bSgjelinek zonecfg_getmcapent(zone_dochandle_t handle, struct zone_mcaptab *tabptr)
65840209230bSgjelinek {
65850209230bSgjelinek 	int err;
65860209230bSgjelinek 
65870209230bSgjelinek 	if ((err = zonecfg_setent(handle)) != Z_OK)
65880209230bSgjelinek 		return (err);
65890209230bSgjelinek 
65900209230bSgjelinek 	err = getmcapent_core(handle, tabptr);
65910209230bSgjelinek 
65920209230bSgjelinek 	(void) zonecfg_endent(handle);
65930209230bSgjelinek 
65940209230bSgjelinek 	return (err);
65950209230bSgjelinek }
65960209230bSgjelinek 
65976cfd72c6Sgjelinek /*
65986cfd72c6Sgjelinek  * Get the full tree of pkg/patch metadata in a set of nested AVL trees.
65996cfd72c6Sgjelinek  * pkgs_avl is an AVL tree of pkgs.  Each pkg element contains a
66006cfd72c6Sgjelinek  * zpe_patches_avl member which holds an AVL tree of patches for that pkg.
66016cfd72c6Sgjelinek  * The patch elements have the same zpe_patches_avl member, each of which can
66026cfd72c6Sgjelinek  * hold an AVL tree of patches that are obsoleted by the patch.
66036cfd72c6Sgjelinek  *
66046cfd72c6Sgjelinek  * The zone xml data contains DTD_ELEM_PACKAGE elements, followed by
66056cfd72c6Sgjelinek  * DTD_ELEM_PATCH elements.  The DTD_ELEM_PATCH patch element applies to the
66066cfd72c6Sgjelinek  * DTD_ELEM_PACKAGE that precedes it.  The DTD_ELEM_PATCH element may have
66076cfd72c6Sgjelinek  * child DTD_ELEM_OBSOLETES nodes associated with it.  The DTD_ELEM_PACKAGE
66086cfd72c6Sgjelinek  * really should have had the DTD_ELEM_PATCH elements as children but it
66096cfd72c6Sgjelinek  * was not defined that way initially so we are stuck with the DTD definition
66106cfd72c6Sgjelinek  * now.  However, we can safely assume the ordering for compatibility.
66116cfd72c6Sgjelinek  */
6612ee519a1fSgjelinek int
66136cfd72c6Sgjelinek zonecfg_getpkgdata(zone_dochandle_t handle, uu_avl_pool_t *pkg_pool,
66146cfd72c6Sgjelinek     uu_avl_t *pkgs_avl)
6615ee519a1fSgjelinek {
6616ee519a1fSgjelinek 	xmlNodePtr cur;
66176cfd72c6Sgjelinek 	int res;
66186cfd72c6Sgjelinek 	zone_pkg_entry_t *pkg;
66196cfd72c6Sgjelinek 	char name[MAXNAMELEN];
66206cfd72c6Sgjelinek 	char version[ZONE_PKG_VERSMAX];
6621ee519a1fSgjelinek 
6622ee519a1fSgjelinek 	if (handle == NULL)
6623ee519a1fSgjelinek 		return (Z_INVAL);
6624ee519a1fSgjelinek 
66256cfd72c6Sgjelinek 	if ((res = zonecfg_setent(handle)) != Z_OK)
66266cfd72c6Sgjelinek 		return (res);
6627ee519a1fSgjelinek 
66286cfd72c6Sgjelinek 	if ((cur = handle->zone_dh_cur) == NULL) {
66296cfd72c6Sgjelinek 		res = Z_NO_ENTRY;
66306cfd72c6Sgjelinek 		goto done;
6631ee519a1fSgjelinek 	}
6632ee519a1fSgjelinek 
66336cfd72c6Sgjelinek 	for (; cur != NULL; cur = cur->next) {
66346cfd72c6Sgjelinek 		if (xmlStrcmp(cur->name, DTD_ELEM_PACKAGE) == 0) {
66356cfd72c6Sgjelinek 			uu_avl_index_t where;
66366cfd72c6Sgjelinek 
66376cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_NAME, name,
66386cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
66396cfd72c6Sgjelinek 				goto done;
66406cfd72c6Sgjelinek 
66416cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_VERSION, version,
66426cfd72c6Sgjelinek 			    sizeof (version))) != Z_OK)
66436cfd72c6Sgjelinek 				goto done;
66446cfd72c6Sgjelinek 
66456cfd72c6Sgjelinek 			if ((pkg = (zone_pkg_entry_t *)
66466cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
66476cfd72c6Sgjelinek 				res = Z_NOMEM;
66486cfd72c6Sgjelinek 				goto done;
6649ee519a1fSgjelinek 			}
6650ee519a1fSgjelinek 
66516cfd72c6Sgjelinek 			if ((pkg->zpe_name = strdup(name)) == NULL) {
66526cfd72c6Sgjelinek 				free(pkg);
66536cfd72c6Sgjelinek 				res = Z_NOMEM;
66546cfd72c6Sgjelinek 				goto done;
6655ee519a1fSgjelinek 			}
6656ee519a1fSgjelinek 
66576cfd72c6Sgjelinek 			if ((pkg->zpe_vers = strdup(version)) == NULL) {
66586cfd72c6Sgjelinek 				free(pkg->zpe_name);
66596cfd72c6Sgjelinek 				free(pkg);
66606cfd72c6Sgjelinek 				res = Z_NOMEM;
66616cfd72c6Sgjelinek 				goto done;
6662ee519a1fSgjelinek 			}
6663ee519a1fSgjelinek 
66646cfd72c6Sgjelinek 			pkg->zpe_patches_avl = NULL;
66656cfd72c6Sgjelinek 
66666cfd72c6Sgjelinek 			uu_avl_node_init(pkg, &pkg->zpe_entry, pkg_pool);
66676cfd72c6Sgjelinek 			if (uu_avl_find(pkgs_avl, pkg, NULL, &where) != NULL) {
66686cfd72c6Sgjelinek 				free(pkg->zpe_name);
66696cfd72c6Sgjelinek 				free(pkg->zpe_vers);
66706cfd72c6Sgjelinek 				free(pkg);
66716cfd72c6Sgjelinek 			} else {
66726cfd72c6Sgjelinek 				uu_avl_insert(pkgs_avl, pkg, where);
6673ee519a1fSgjelinek 			}
6674ee519a1fSgjelinek 
66756cfd72c6Sgjelinek 		} else if (xmlStrcmp(cur->name, DTD_ELEM_PATCH) == 0) {
66766cfd72c6Sgjelinek 			zone_pkg_entry_t *patch;
66776cfd72c6Sgjelinek 			uu_avl_index_t where;
66786cfd72c6Sgjelinek 			char *p;
66796cfd72c6Sgjelinek 			char *dashp = NULL;
66806cfd72c6Sgjelinek 			xmlNodePtr child;
66816cfd72c6Sgjelinek 
66826cfd72c6Sgjelinek 			if ((res = fetchprop(cur, DTD_ATTR_ID, name,
66836cfd72c6Sgjelinek 			    sizeof (name))) != Z_OK)
66846cfd72c6Sgjelinek 				goto done;
66856cfd72c6Sgjelinek 
66866cfd72c6Sgjelinek 			if ((patch = (zone_pkg_entry_t *)
66876cfd72c6Sgjelinek 			    malloc(sizeof (zone_pkg_entry_t))) == NULL) {
66886cfd72c6Sgjelinek 				res = Z_NOMEM;
66896cfd72c6Sgjelinek 				goto done;
6690ee519a1fSgjelinek 			}
6691ee519a1fSgjelinek 
66926cfd72c6Sgjelinek 			if ((p = strchr(name, '-')) != NULL) {
66936cfd72c6Sgjelinek 				dashp = p;
66946cfd72c6Sgjelinek 				*p++ = '\0';
66956cfd72c6Sgjelinek 			} else {
66966cfd72c6Sgjelinek 				p = "";
6697ee519a1fSgjelinek 			}
6698ee519a1fSgjelinek 
66996cfd72c6Sgjelinek 			if ((patch->zpe_name = strdup(name)) == NULL) {
67006cfd72c6Sgjelinek 				free(patch);
67016cfd72c6Sgjelinek 				res = Z_NOMEM;
67026cfd72c6Sgjelinek 				goto done;
6703ee519a1fSgjelinek 			}
6704ee519a1fSgjelinek 
67056cfd72c6Sgjelinek 			if ((patch->zpe_vers = strdup(p)) == NULL) {
67066cfd72c6Sgjelinek 				free(patch->zpe_name);
67076cfd72c6Sgjelinek 				free(patch);
67086cfd72c6Sgjelinek 				res = Z_NOMEM;
67096cfd72c6Sgjelinek 				goto done;
6710ee519a1fSgjelinek 			}
6711ee519a1fSgjelinek 
67126cfd72c6Sgjelinek 			if (dashp != NULL)
67136cfd72c6Sgjelinek 				*dashp = '-';
67146cfd72c6Sgjelinek 
67156cfd72c6Sgjelinek 			patch->zpe_patches_avl = NULL;
67166cfd72c6Sgjelinek 
67176cfd72c6Sgjelinek 			if (pkg->zpe_patches_avl == NULL) {
67186cfd72c6Sgjelinek 				pkg->zpe_patches_avl = uu_avl_create(pkg_pool,
67196cfd72c6Sgjelinek 				    NULL, UU_DEFAULT);
67206cfd72c6Sgjelinek 				if (pkg->zpe_patches_avl == NULL) {
67216cfd72c6Sgjelinek 					free(patch->zpe_name);
67226cfd72c6Sgjelinek 					free(patch->zpe_vers);
67236cfd72c6Sgjelinek 					free(patch);
67246cfd72c6Sgjelinek 					res = Z_NOMEM;
67256cfd72c6Sgjelinek 					goto done;
67266cfd72c6Sgjelinek 				}
67276cfd72c6Sgjelinek 			}
67286cfd72c6Sgjelinek 
67296cfd72c6Sgjelinek 			uu_avl_node_init(patch, &patch->zpe_entry, pkg_pool);
67306cfd72c6Sgjelinek 			if (uu_avl_find(pkg->zpe_patches_avl, patch, NULL,
67316cfd72c6Sgjelinek 			    &where) != NULL) {
67326cfd72c6Sgjelinek 				free(patch->zpe_name);
67336cfd72c6Sgjelinek 				free(patch->zpe_vers);
67346cfd72c6Sgjelinek 				free(patch);
67356cfd72c6Sgjelinek 			} else {
67366cfd72c6Sgjelinek 				uu_avl_insert(pkg->zpe_patches_avl, patch,
67376cfd72c6Sgjelinek 				    where);
67386cfd72c6Sgjelinek 			}
67396cfd72c6Sgjelinek 
67406cfd72c6Sgjelinek 			/* Add any patches this patch obsoletes. */
67416cfd72c6Sgjelinek 			for (child = cur->xmlChildrenNode; child != NULL;
67426cfd72c6Sgjelinek 			    child = child->next) {
67436cfd72c6Sgjelinek 				zone_pkg_entry_t *obs;
67446cfd72c6Sgjelinek 
67456cfd72c6Sgjelinek 				if (xmlStrcmp(child->name, DTD_ELEM_OBSOLETES)
67466cfd72c6Sgjelinek 				    != 0)
67476cfd72c6Sgjelinek 					continue;
67486cfd72c6Sgjelinek 
67496cfd72c6Sgjelinek 				if ((res = fetchprop(child, DTD_ATTR_ID,
67506cfd72c6Sgjelinek 				    name, sizeof (name))) != Z_OK)
67516cfd72c6Sgjelinek 					goto done;
67526cfd72c6Sgjelinek 
67536cfd72c6Sgjelinek 				if ((obs = (zone_pkg_entry_t *)malloc(
67546cfd72c6Sgjelinek 				    sizeof (zone_pkg_entry_t))) == NULL) {
67556cfd72c6Sgjelinek 					res = Z_NOMEM;
67566cfd72c6Sgjelinek 					goto done;
67576cfd72c6Sgjelinek 				}
67586cfd72c6Sgjelinek 
67596cfd72c6Sgjelinek 				if ((obs->zpe_name = strdup(name)) == NULL) {
67606cfd72c6Sgjelinek 					free(obs);
67616cfd72c6Sgjelinek 					res = Z_NOMEM;
67626cfd72c6Sgjelinek 					goto done;
67636cfd72c6Sgjelinek 				}
67646cfd72c6Sgjelinek 				/*
67656cfd72c6Sgjelinek 				 * The version doesn't matter for obsoleted
67666cfd72c6Sgjelinek 				 * patches.
67676cfd72c6Sgjelinek 				 */
67686cfd72c6Sgjelinek 				obs->zpe_vers = NULL;
67696cfd72c6Sgjelinek 				obs->zpe_patches_avl = NULL;
67706cfd72c6Sgjelinek 
67716cfd72c6Sgjelinek 				/*
67726cfd72c6Sgjelinek 				 * If this is the first obsolete patch, add an
67736cfd72c6Sgjelinek 				 * AVL tree to the parent patch element.
67746cfd72c6Sgjelinek 				 */
67756cfd72c6Sgjelinek 				if (patch->zpe_patches_avl == NULL) {
67766cfd72c6Sgjelinek 					patch->zpe_patches_avl =
67776cfd72c6Sgjelinek 					    uu_avl_create(pkg_pool, NULL,
67786cfd72c6Sgjelinek 					    UU_DEFAULT);
67796cfd72c6Sgjelinek 					if (patch->zpe_patches_avl == NULL) {
67806cfd72c6Sgjelinek 						free(obs->zpe_name);
67816cfd72c6Sgjelinek 						free(obs);
67826cfd72c6Sgjelinek 						res = Z_NOMEM;
67836cfd72c6Sgjelinek 						goto done;
67846cfd72c6Sgjelinek 					}
67856cfd72c6Sgjelinek 				}
67866cfd72c6Sgjelinek 
67876cfd72c6Sgjelinek 				/* Insert obsolete patch into the AVL tree. */
67886cfd72c6Sgjelinek 				uu_avl_node_init(obs, &obs->zpe_entry,
67896cfd72c6Sgjelinek 				    pkg_pool);
67906cfd72c6Sgjelinek 				if (uu_avl_find(patch->zpe_patches_avl, obs,
67916cfd72c6Sgjelinek 				    NULL, &where) != NULL) {
67926cfd72c6Sgjelinek 					free(obs->zpe_name);
67936cfd72c6Sgjelinek 					free(obs);
67946cfd72c6Sgjelinek 				} else {
67956cfd72c6Sgjelinek 					uu_avl_insert(patch->zpe_patches_avl,
67966cfd72c6Sgjelinek 					    obs, where);
67976cfd72c6Sgjelinek 				}
67986cfd72c6Sgjelinek 			}
67996cfd72c6Sgjelinek 		}
68006cfd72c6Sgjelinek 	}
68016cfd72c6Sgjelinek 
68026cfd72c6Sgjelinek done:
68036cfd72c6Sgjelinek 	(void) zonecfg_endent(handle);
68046cfd72c6Sgjelinek 	return (res);
6805ee519a1fSgjelinek }
6806ee519a1fSgjelinek 
6807ee519a1fSgjelinek int
6808ee519a1fSgjelinek zonecfg_setdevperment(zone_dochandle_t handle)
6809ee519a1fSgjelinek {
6810ee519a1fSgjelinek 	return (zonecfg_setent(handle));
6811ee519a1fSgjelinek }
6812ee519a1fSgjelinek 
6813ee519a1fSgjelinek int
6814ee519a1fSgjelinek zonecfg_getdevperment(zone_dochandle_t handle, struct zone_devpermtab *tabptr)
6815ee519a1fSgjelinek {
6816ee519a1fSgjelinek 	xmlNodePtr cur;
6817ee519a1fSgjelinek 	int err;
6818ee519a1fSgjelinek 	char buf[128];
6819ee519a1fSgjelinek 
6820ee519a1fSgjelinek 	tabptr->zone_devperm_acl = NULL;
6821ee519a1fSgjelinek 
6822ee519a1fSgjelinek 	if (handle == NULL)
6823ee519a1fSgjelinek 		return (Z_INVAL);
6824ee519a1fSgjelinek 
6825ee519a1fSgjelinek 	if ((cur = handle->zone_dh_cur) == NULL)
6826ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6827ee519a1fSgjelinek 
6828ee519a1fSgjelinek 	for (; cur != NULL; cur = cur->next)
6829ee519a1fSgjelinek 		if (!xmlStrcmp(cur->name, DTD_ELEM_DEV_PERM))
6830ee519a1fSgjelinek 			break;
6831ee519a1fSgjelinek 	if (cur == NULL) {
6832ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6833ee519a1fSgjelinek 		return (Z_NO_ENTRY);
6834ee519a1fSgjelinek 	}
6835ee519a1fSgjelinek 
6836ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_NAME, tabptr->zone_devperm_name,
6837ee519a1fSgjelinek 	    sizeof (tabptr->zone_devperm_name))) != Z_OK) {
6838ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6839ee519a1fSgjelinek 		return (err);
6840ee519a1fSgjelinek 	}
6841ee519a1fSgjelinek 
6842ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_UID, buf, sizeof (buf))) != Z_OK) {
6843ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6844ee519a1fSgjelinek 		return (err);
6845ee519a1fSgjelinek 	}
6846ee519a1fSgjelinek 	tabptr->zone_devperm_uid = (uid_t)atol(buf);
6847ee519a1fSgjelinek 
6848ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_GID, buf, sizeof (buf))) != Z_OK) {
6849ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6850ee519a1fSgjelinek 		return (err);
6851ee519a1fSgjelinek 	}
6852ee519a1fSgjelinek 	tabptr->zone_devperm_gid = (gid_t)atol(buf);
6853ee519a1fSgjelinek 
6854ee519a1fSgjelinek 	if ((err = fetchprop(cur, DTD_ATTR_MODE, buf, sizeof (buf))) != Z_OK) {
6855ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6856ee519a1fSgjelinek 		return (err);
6857ee519a1fSgjelinek 	}
6858ee519a1fSgjelinek 	tabptr->zone_devperm_mode = (mode_t)strtol(buf, (char **)NULL, 8);
6859ee519a1fSgjelinek 
6860ee519a1fSgjelinek 	if ((err = fetch_alloc_prop(cur, DTD_ATTR_ACL,
6861ee519a1fSgjelinek 	    &(tabptr->zone_devperm_acl))) != Z_OK) {
6862ee519a1fSgjelinek 		handle->zone_dh_cur = handle->zone_dh_top;
6863ee519a1fSgjelinek 		return (err);
6864ee519a1fSgjelinek 	}
6865ee519a1fSgjelinek 
6866ee519a1fSgjelinek 	handle->zone_dh_cur = cur->next;
6867ee519a1fSgjelinek 	return (Z_OK);
6868ee519a1fSgjelinek }
6869ee519a1fSgjelinek 
6870ee519a1fSgjelinek int
6871ee519a1fSgjelinek zonecfg_enddevperment(zone_dochandle_t handle)
6872ee519a1fSgjelinek {
6873ee519a1fSgjelinek 	return (zonecfg_endent(handle));
6874ee519a1fSgjelinek }
6875ee519a1fSgjelinek 
6876ff17c8bfSgjelinek /* PRINTFLIKE1 */
6877ff17c8bfSgjelinek static void
6878ff17c8bfSgjelinek zerror(const char *zone_name, const char *fmt, ...)
687939935be5Sgjelinek {
6880ff17c8bfSgjelinek 	va_list alist;
688139935be5Sgjelinek 
6882ff17c8bfSgjelinek 	va_start(alist, fmt);
6883ff17c8bfSgjelinek 	(void) fprintf(stderr, "zone '%s': ", zone_name);
6884ff17c8bfSgjelinek 	(void) vfprintf(stderr, fmt, alist);
6885ff17c8bfSgjelinek 	(void) fprintf(stderr, "\n");
6886ff17c8bfSgjelinek 	va_end(alist);
6887ee519a1fSgjelinek }
6888ee519a1fSgjelinek 
6889ee519a1fSgjelinek static void
6890ff17c8bfSgjelinek zperror(const char *str)
6891ee519a1fSgjelinek {
6892ff17c8bfSgjelinek 	(void) fprintf(stderr, "%s: %s\n", str, strerror(errno));
6893ee519a1fSgjelinek }
6894ee519a1fSgjelinek 
6895ee519a1fSgjelinek /*
6896ff17c8bfSgjelinek  * The following three routines implement a simple locking mechanism to
6897ff17c8bfSgjelinek  * ensure that only one instance of zoneadm at a time is able to manipulate
6898ff17c8bfSgjelinek  * a given zone.  The lock is built on top of an fcntl(2) lock of
6899ff17c8bfSgjelinek  * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock.  If a zoneadm instance
6900ff17c8bfSgjelinek  * can grab that lock, it is allowed to manipulate the zone.
6901ff17c8bfSgjelinek  *
6902ff17c8bfSgjelinek  * Since zoneadm may call external applications which in turn invoke
6903ff17c8bfSgjelinek  * zoneadm again, we introduce the notion of "lock inheritance".  Any
6904ff17c8bfSgjelinek  * instance of zoneadm that has another instance in its ancestry is assumed
6905ff17c8bfSgjelinek  * to be acting on behalf of the original zoneadm, and is thus allowed to
6906ff17c8bfSgjelinek  * manipulate its zone.
6907ff17c8bfSgjelinek  *
6908ff17c8bfSgjelinek  * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment
6909ff17c8bfSgjelinek  * variable.  When zoneadm is granted a lock on its zone, this environment
6910ff17c8bfSgjelinek  * variable is set to 1.  When it releases the lock, the variable is set to
6911ff17c8bfSgjelinek  * 0.  Since a child process inherits its parent's environment, checking
6912ff17c8bfSgjelinek  * the state of this variable indicates whether or not any ancestor owns
6913ff17c8bfSgjelinek  * the lock.
691439935be5Sgjelinek  */
6915ff17c8bfSgjelinek void
6916ff17c8bfSgjelinek zonecfg_init_lock_file(const char *zone_name, char **lock_env)
691739935be5Sgjelinek {
6918ff17c8bfSgjelinek 	*lock_env = getenv(LOCK_ENV_VAR);
6919ff17c8bfSgjelinek 	if (*lock_env == NULL) {
6920ff17c8bfSgjelinek 		if (putenv(zoneadm_lock_not_held) != 0) {
6921ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not set env: %s"),
6922ff17c8bfSgjelinek 			    strerror(errno));
6923ff17c8bfSgjelinek 			exit(1);
6924ff17c8bfSgjelinek 		}
692539935be5Sgjelinek 	} else {
6926ff17c8bfSgjelinek 		if (atoi(*lock_env) == 1)
6927ff17c8bfSgjelinek 			zone_lock_cnt = 1;
692839935be5Sgjelinek 	}
692939935be5Sgjelinek }
693039935be5Sgjelinek 
6931ff17c8bfSgjelinek void
6932ff17c8bfSgjelinek zonecfg_release_lock_file(const char *zone_name, int lockfd)
6933ff17c8bfSgjelinek {
6934ff17c8bfSgjelinek 	/*
6935ff17c8bfSgjelinek 	 * If we are cleaning up from a failed attempt to lock the zone for
6936ff17c8bfSgjelinek 	 * the first time, we might have a zone_lock_cnt of 0.  In that
6937ff17c8bfSgjelinek 	 * error case, we don't want to do anything but close the lock
6938ff17c8bfSgjelinek 	 * file.
6939ff17c8bfSgjelinek 	 */
6940ff17c8bfSgjelinek 	assert(zone_lock_cnt >= 0);
6941ff17c8bfSgjelinek 	if (zone_lock_cnt > 0) {
6942ff17c8bfSgjelinek 		assert(getenv(LOCK_ENV_VAR) != NULL);
6943ff17c8bfSgjelinek 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
6944ff17c8bfSgjelinek 		if (--zone_lock_cnt > 0) {
6945ff17c8bfSgjelinek 			assert(lockfd == -1);
6946ff17c8bfSgjelinek 			return;
694739935be5Sgjelinek 		}
6948ff17c8bfSgjelinek 		if (putenv(zoneadm_lock_not_held) != 0) {
6949ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not set env: %s"),
6950ff17c8bfSgjelinek 			    strerror(errno));
6951ff17c8bfSgjelinek 			exit(1);
6952ff17c8bfSgjelinek 		}
6953ff17c8bfSgjelinek 	}
6954ff17c8bfSgjelinek 	assert(lockfd >= 0);
6955ff17c8bfSgjelinek 	(void) close(lockfd);
695639935be5Sgjelinek }
695739935be5Sgjelinek 
6958ff17c8bfSgjelinek int
6959ff17c8bfSgjelinek zonecfg_grab_lock_file(const char *zone_name, int *lockfd)
6960ff17c8bfSgjelinek {
6961ff17c8bfSgjelinek 	char pathbuf[PATH_MAX];
6962ff17c8bfSgjelinek 	struct flock flock;
6963ff17c8bfSgjelinek 
6964ff17c8bfSgjelinek 	/*
6965ff17c8bfSgjelinek 	 * If we already have the lock, we can skip this expensive song
6966ff17c8bfSgjelinek 	 * and dance.
6967ff17c8bfSgjelinek 	 */
6968ff17c8bfSgjelinek 	assert(zone_lock_cnt >= 0);
6969ff17c8bfSgjelinek 	assert(getenv(LOCK_ENV_VAR) != NULL);
6970ff17c8bfSgjelinek 	if (zone_lock_cnt > 0) {
6971ff17c8bfSgjelinek 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
6972ff17c8bfSgjelinek 		zone_lock_cnt++;
6973ff17c8bfSgjelinek 		*lockfd = -1;
697439935be5Sgjelinek 		return (Z_OK);
697539935be5Sgjelinek 	}
6976ff17c8bfSgjelinek 	assert(getenv(LOCK_ENV_VAR) != NULL);
6977ff17c8bfSgjelinek 	assert(atoi(getenv(LOCK_ENV_VAR)) == 0);
697839935be5Sgjelinek 
6979ff17c8bfSgjelinek 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
6980ff17c8bfSgjelinek 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
6981ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
698239935be5Sgjelinek 		return (-1);
698339935be5Sgjelinek 	}
6984ff17c8bfSgjelinek 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
6985ff17c8bfSgjelinek 		zerror(zone_name, gettext("could not mkdir %s: %s"), pathbuf,
6986ff17c8bfSgjelinek 		    strerror(errno));
6987ff17c8bfSgjelinek 		return (-1);
6988ff17c8bfSgjelinek 	}
6989ff17c8bfSgjelinek 	(void) chmod(pathbuf, S_IRWXU);
699039935be5Sgjelinek 
699139935be5Sgjelinek 	/*
6992ff17c8bfSgjelinek 	 * One of these lock files is created for each zone (when needed).
6993ff17c8bfSgjelinek 	 * The lock files are not cleaned up (except on system reboot),
6994ff17c8bfSgjelinek 	 * but since there is only one per zone, there is no resource
6995ff17c8bfSgjelinek 	 * starvation issue.
699639935be5Sgjelinek 	 */
6997ff17c8bfSgjelinek 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
6998ff17c8bfSgjelinek 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
6999ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
7000ff17c8bfSgjelinek 		return (-1);
7001ff17c8bfSgjelinek 	}
7002ff17c8bfSgjelinek 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
7003ff17c8bfSgjelinek 		zerror(zone_name, gettext("could not open %s: %s"), pathbuf,
7004ff17c8bfSgjelinek 		    strerror(errno));
7005ff17c8bfSgjelinek 		return (-1);
7006ff17c8bfSgjelinek 	}
7007ff17c8bfSgjelinek 	/*
7008ff17c8bfSgjelinek 	 * Lock the file to synchronize with other zoneadmds
7009ff17c8bfSgjelinek 	 */
7010ff17c8bfSgjelinek 	flock.l_type = F_WRLCK;
7011ff17c8bfSgjelinek 	flock.l_whence = SEEK_SET;
7012ff17c8bfSgjelinek 	flock.l_start = (off_t)0;
7013ff17c8bfSgjelinek 	flock.l_len = (off_t)0;
7014ff17c8bfSgjelinek 	if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) ||
7015ff17c8bfSgjelinek 	    (putenv(zoneadm_lock_held) != 0)) {
7016ff17c8bfSgjelinek 		zerror(zone_name, gettext("unable to lock %s: %s"), pathbuf,
7017ff17c8bfSgjelinek 		    strerror(errno));
7018ff17c8bfSgjelinek 		zonecfg_release_lock_file(zone_name, *lockfd);
7019ff17c8bfSgjelinek 		return (-1);
7020ff17c8bfSgjelinek 	}
7021ff17c8bfSgjelinek 	zone_lock_cnt = 1;
7022ff17c8bfSgjelinek 	return (Z_OK);
7023ff17c8bfSgjelinek }
7024ff17c8bfSgjelinek 
7025ff17c8bfSgjelinek static boolean_t
7026ff17c8bfSgjelinek get_doorname(const char *zone_name, char *buffer)
702739935be5Sgjelinek {
7028ff17c8bfSgjelinek 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
7029ff17c8bfSgjelinek 	    zonecfg_get_root(), zone_name) < PATH_MAX);
7030e66285fdSgjelinek }
7031ee519a1fSgjelinek 
7032ee519a1fSgjelinek /*
7033ff17c8bfSgjelinek  * system daemons are not audited.  For the global zone, this occurs
7034ff17c8bfSgjelinek  * "naturally" since init is started with the default audit
7035ff17c8bfSgjelinek  * characteristics.  Since zoneadmd is a system daemon and it starts
7036ff17c8bfSgjelinek  * init for a zone, it is necessary to clear out the audit
7037ff17c8bfSgjelinek  * characteristics inherited from whomever started zoneadmd.  This is
7038ff17c8bfSgjelinek  * indicated by the audit id, which is set from the ruid parameter of
7039ff17c8bfSgjelinek  * adt_set_user(), below.
7040ee519a1fSgjelinek  */
7041ff17c8bfSgjelinek 
7042ff17c8bfSgjelinek static void
7043ff17c8bfSgjelinek prepare_audit_context(const char *zone_name)
7044ff17c8bfSgjelinek {
7045ff17c8bfSgjelinek 	adt_session_data_t	*ah;
7046ff17c8bfSgjelinek 	char			*failure = gettext("audit failure: %s");
7047ff17c8bfSgjelinek 
7048ff17c8bfSgjelinek 	if (adt_start_session(&ah, NULL, 0)) {
7049ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7050ff17c8bfSgjelinek 		return;
7051ff17c8bfSgjelinek 	}
7052ff17c8bfSgjelinek 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
7053ff17c8bfSgjelinek 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
7054ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7055ff17c8bfSgjelinek 		(void) adt_end_session(ah);
7056ff17c8bfSgjelinek 		return;
7057ff17c8bfSgjelinek 	}
7058ff17c8bfSgjelinek 	if (adt_set_proc(ah))
7059ff17c8bfSgjelinek 		zerror(zone_name, failure, strerror(errno));
7060ff17c8bfSgjelinek 
7061ff17c8bfSgjelinek 	(void) adt_end_session(ah);
7062ff17c8bfSgjelinek }
7063ff17c8bfSgjelinek 
7064ff17c8bfSgjelinek static int
7065ff17c8bfSgjelinek start_zoneadmd(const char *zone_name, boolean_t lock)
7066ff17c8bfSgjelinek {
7067ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7068ff17c8bfSgjelinek 	pid_t child_pid;
7069ff17c8bfSgjelinek 	int error = -1;
7070ff17c8bfSgjelinek 	int doorfd, lockfd;
7071ff17c8bfSgjelinek 	struct door_info info;
7072ff17c8bfSgjelinek 
7073ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath))
7074ff17c8bfSgjelinek 		return (-1);
7075ff17c8bfSgjelinek 
7076ff17c8bfSgjelinek 	if (lock)
7077ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(zone_name, &lockfd) != Z_OK)
7078ff17c8bfSgjelinek 			return (-1);
7079ff17c8bfSgjelinek 
7080ff17c8bfSgjelinek 	/*
7081ff17c8bfSgjelinek 	 * Now that we have the lock, re-confirm that the daemon is
7082ff17c8bfSgjelinek 	 * *not* up and working fine.  If it is still down, we have a green
7083ff17c8bfSgjelinek 	 * light to start it.
7084ff17c8bfSgjelinek 	 */
7085ff17c8bfSgjelinek 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7086ff17c8bfSgjelinek 		if (errno != ENOENT) {
7087ff17c8bfSgjelinek 			zperror(doorpath);
7088ff17c8bfSgjelinek 			goto out;
7089ff17c8bfSgjelinek 		}
7090ff17c8bfSgjelinek 	} else {
7091ff17c8bfSgjelinek 		if (door_info(doorfd, &info) == 0 &&
7092ff17c8bfSgjelinek 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
7093ff17c8bfSgjelinek 			error = Z_OK;
7094ff17c8bfSgjelinek 			(void) close(doorfd);
7095ff17c8bfSgjelinek 			goto out;
7096ff17c8bfSgjelinek 		}
7097ff17c8bfSgjelinek 		(void) close(doorfd);
7098ff17c8bfSgjelinek 	}
7099ff17c8bfSgjelinek 
7100ff17c8bfSgjelinek 	if ((child_pid = fork()) == -1) {
7101ff17c8bfSgjelinek 		zperror(gettext("could not fork"));
7102ff17c8bfSgjelinek 		goto out;
7103ff17c8bfSgjelinek 	}
7104ff17c8bfSgjelinek 
7105ff17c8bfSgjelinek 	if (child_pid == 0) {
7106ff17c8bfSgjelinek 		const char *argv[6], **ap;
7107ff17c8bfSgjelinek 
7108ff17c8bfSgjelinek 		/* child process */
7109ff17c8bfSgjelinek 		prepare_audit_context(zone_name);
7110ff17c8bfSgjelinek 
7111ff17c8bfSgjelinek 		ap = argv;
7112ff17c8bfSgjelinek 		*ap++ = "zoneadmd";
7113ff17c8bfSgjelinek 		*ap++ = "-z";
7114ff17c8bfSgjelinek 		*ap++ = zone_name;
7115ff17c8bfSgjelinek 		if (zonecfg_in_alt_root()) {
7116ff17c8bfSgjelinek 			*ap++ = "-R";
7117ff17c8bfSgjelinek 			*ap++ = zonecfg_get_root();
7118ff17c8bfSgjelinek 		}
7119ff17c8bfSgjelinek 		*ap = NULL;
7120ff17c8bfSgjelinek 
7121ff17c8bfSgjelinek 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
7122ff17c8bfSgjelinek 		/*
7123ff17c8bfSgjelinek 		 * TRANSLATION_NOTE
7124ff17c8bfSgjelinek 		 * zoneadmd is a literal that should not be translated.
7125ff17c8bfSgjelinek 		 */
7126ff17c8bfSgjelinek 		zperror(gettext("could not exec zoneadmd"));
7127ff17c8bfSgjelinek 		_exit(1);
7128ff17c8bfSgjelinek 	} else {
7129ff17c8bfSgjelinek 		/* parent process */
7130ff17c8bfSgjelinek 		pid_t retval;
7131ff17c8bfSgjelinek 		int pstatus = 0;
7132ee519a1fSgjelinek 
7133ee519a1fSgjelinek 		do {
7134ff17c8bfSgjelinek 			retval = waitpid(child_pid, &pstatus, 0);
7135ff17c8bfSgjelinek 		} while (retval != child_pid);
7136ff17c8bfSgjelinek 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
7137ff17c8bfSgjelinek 		    WEXITSTATUS(pstatus) != 0)) {
7138ff17c8bfSgjelinek 			zerror(zone_name, gettext("could not start %s"),
7139ff17c8bfSgjelinek 			    "zoneadmd");
7140ff17c8bfSgjelinek 			goto out;
71416cfd72c6Sgjelinek 		}
71426cfd72c6Sgjelinek 	}
7143ff17c8bfSgjelinek 	error = Z_OK;
7144ff17c8bfSgjelinek out:
7145ff17c8bfSgjelinek 	if (lock)
7146ff17c8bfSgjelinek 		zonecfg_release_lock_file(zone_name, lockfd);
7147ff17c8bfSgjelinek 	return (error);
7148ee519a1fSgjelinek }
7149ee519a1fSgjelinek 
7150ee519a1fSgjelinek int
7151ff17c8bfSgjelinek zonecfg_ping_zoneadmd(const char *zone_name)
7152ee519a1fSgjelinek {
7153ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7154ff17c8bfSgjelinek 	int doorfd;
7155ff17c8bfSgjelinek 	struct door_info info;
7156ee519a1fSgjelinek 
7157ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath))
7158ff17c8bfSgjelinek 		return (-1);
7159ee519a1fSgjelinek 
7160ff17c8bfSgjelinek 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7161ff17c8bfSgjelinek 		return (-1);
7162ff17c8bfSgjelinek 	}
7163ff17c8bfSgjelinek 	if (door_info(doorfd, &info) == 0 &&
7164ff17c8bfSgjelinek 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
7165ff17c8bfSgjelinek 		(void) close(doorfd);
7166ff17c8bfSgjelinek 		return (Z_OK);
7167ff17c8bfSgjelinek 	}
7168ff17c8bfSgjelinek 	(void) close(doorfd);
7169ff17c8bfSgjelinek 	return (-1);
7170ff17c8bfSgjelinek }
7171ee519a1fSgjelinek 
7172ff17c8bfSgjelinek int
7173ff17c8bfSgjelinek zonecfg_call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg, char *locale,
7174ff17c8bfSgjelinek     boolean_t lock)
7175ff17c8bfSgjelinek {
7176ff17c8bfSgjelinek 	char doorpath[PATH_MAX];
7177ff17c8bfSgjelinek 	int doorfd, result;
7178ff17c8bfSgjelinek 	door_arg_t darg;
7179ff17c8bfSgjelinek 
7180ff17c8bfSgjelinek 	zoneid_t zoneid;
7181ff17c8bfSgjelinek 	uint64_t uniqid = 0;
7182ff17c8bfSgjelinek 
7183ff17c8bfSgjelinek 	zone_cmd_rval_t *rvalp;
7184ff17c8bfSgjelinek 	size_t rlen;
7185ff17c8bfSgjelinek 	char *cp, *errbuf;
7186ff17c8bfSgjelinek 
7187ff17c8bfSgjelinek 	rlen = getpagesize();
7188ff17c8bfSgjelinek 	if ((rvalp = malloc(rlen)) == NULL) {
7189ff17c8bfSgjelinek 		zerror(zone_name, gettext("failed to allocate %lu bytes: %s"),
7190ff17c8bfSgjelinek 		    rlen, strerror(errno));
7191ff17c8bfSgjelinek 		return (-1);
7192ff17c8bfSgjelinek 	}
7193ff17c8bfSgjelinek 
7194ff17c8bfSgjelinek 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
7195ff17c8bfSgjelinek 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
7196ff17c8bfSgjelinek 		    sizeof (uniqid));
7197ff17c8bfSgjelinek 	}
7198ff17c8bfSgjelinek 	arg->uniqid = uniqid;
7199ff17c8bfSgjelinek 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
7200ff17c8bfSgjelinek 	if (!get_doorname(zone_name, doorpath)) {
7201ff17c8bfSgjelinek 		zerror(zone_name, gettext("alternate root path is too long"));
7202ff17c8bfSgjelinek 		free(rvalp);
7203ff17c8bfSgjelinek 		return (-1);
7204ff17c8bfSgjelinek 	}
7205ff17c8bfSgjelinek 
7206ff17c8bfSgjelinek 	/*
7207ff17c8bfSgjelinek 	 * Loop trying to start zoneadmd; if something goes seriously
7208ff17c8bfSgjelinek 	 * wrong we break out and fail.
7209ff17c8bfSgjelinek 	 */
7210ff17c8bfSgjelinek 	for (;;) {
7211ff17c8bfSgjelinek 		if (start_zoneadmd(zone_name, lock) != Z_OK)
7212ff17c8bfSgjelinek 			break;
7213ff17c8bfSgjelinek 
7214ff17c8bfSgjelinek 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
7215ff17c8bfSgjelinek 			zperror(gettext("failed to open zone door"));
7216ff17c8bfSgjelinek 			break;
7217ff17c8bfSgjelinek 		}
7218ff17c8bfSgjelinek 
7219ff17c8bfSgjelinek 		darg.data_ptr = (char *)arg;
7220ff17c8bfSgjelinek 		darg.data_size = sizeof (*arg);
7221ff17c8bfSgjelinek 		darg.desc_ptr = NULL;
7222ff17c8bfSgjelinek 		darg.desc_num = 0;
7223ff17c8bfSgjelinek 		darg.rbuf = (char *)rvalp;
7224ff17c8bfSgjelinek 		darg.rsize = rlen;
7225ff17c8bfSgjelinek 		if (door_call(doorfd, &darg) != 0) {
7226ff17c8bfSgjelinek 			(void) close(doorfd);
7227ff17c8bfSgjelinek 			/*
7228ff17c8bfSgjelinek 			 * We'll get EBADF if the door has been revoked.
7229ff17c8bfSgjelinek 			 */
7230ff17c8bfSgjelinek 			if (errno != EBADF) {
7231ff17c8bfSgjelinek 				zperror(gettext("door_call failed"));
7232ff17c8bfSgjelinek 				break;
7233ff17c8bfSgjelinek 			}
7234ff17c8bfSgjelinek 			continue;	/* take another lap */
7235ff17c8bfSgjelinek 		}
7236ff17c8bfSgjelinek 		(void) close(doorfd);
7237ff17c8bfSgjelinek 
7238ff17c8bfSgjelinek 		if (darg.data_size == 0) {
7239ff17c8bfSgjelinek 			/* Door server is going away; kick it again. */
7240ff17c8bfSgjelinek 			continue;
7241ff17c8bfSgjelinek 		}
7242ff17c8bfSgjelinek 
7243ff17c8bfSgjelinek 		errbuf = rvalp->errbuf;
7244ff17c8bfSgjelinek 		while (*errbuf != '\0') {
7245ff17c8bfSgjelinek 			/*
7246ff17c8bfSgjelinek 			 * Remove any newlines since zerror()
7247ff17c8bfSgjelinek 			 * will append one automatically.
7248ff17c8bfSgjelinek 			 */
7249ff17c8bfSgjelinek 			cp = strchr(errbuf, '\n');
7250ff17c8bfSgjelinek 			if (cp != NULL)
7251ff17c8bfSgjelinek 				*cp = '\0';
7252ff17c8bfSgjelinek 			zerror(zone_name, "%s", errbuf);
7253ff17c8bfSgjelinek 			if (cp == NULL)
7254ff17c8bfSgjelinek 				break;
7255ff17c8bfSgjelinek 			errbuf = cp + 1;
7256ff17c8bfSgjelinek 		}
7257ff17c8bfSgjelinek 		result = rvalp->rval == 0 ? 0 : -1;
7258ff17c8bfSgjelinek 		free(rvalp);
7259ff17c8bfSgjelinek 		return (result);
7260ff17c8bfSgjelinek 	}
7261ff17c8bfSgjelinek 
7262ff17c8bfSgjelinek 	free(rvalp);
7263ff17c8bfSgjelinek 	return (-1);
7264ee519a1fSgjelinek }
7265