xref: /illumos-gate/usr/src/cmd/zonecfg/zonecfg.c (revision 3042b8b580251031a2cf1fe9a2b8787a497664c8)
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
5fb03efaaSdp  * Common Development and Distribution License (the "License").
6fb03efaaSdp  * 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  */
217e362f58Scomay 
227c478bd9Sstevel@tonic-gate /*
23*3042b8b5Sbatschul  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * zonecfg is a lex/yacc based command interpreter used to manage zone
297c478bd9Sstevel@tonic-gate  * configurations.  The lexer (see zonecfg_lex.l) builds up tokens, which
307c478bd9Sstevel@tonic-gate  * the grammar (see zonecfg_grammar.y) builds up into commands, some of
317c478bd9Sstevel@tonic-gate  * which takes resources and/or properties as arguments.  See the block
327c478bd9Sstevel@tonic-gate  * comments near the end of zonecfg_grammar.y for how the data structures
337c478bd9Sstevel@tonic-gate  * which keep track of these resources and properties are built up.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * The resource/property data structures are inserted into a command
367c478bd9Sstevel@tonic-gate  * structure (see zonecfg.h), which also keeps track of command names,
377c478bd9Sstevel@tonic-gate  * miscellaneous arguments, and function handlers.  The grammar selects
387c478bd9Sstevel@tonic-gate  * the appropriate function handler, each of which takes a pointer to a
397c478bd9Sstevel@tonic-gate  * command structure as its sole argument, and invokes it.  The grammar
407c478bd9Sstevel@tonic-gate  * itself is "entered" (a la the Matrix) by yyparse(), which is called
417c478bd9Sstevel@tonic-gate  * from read_input(), our main driving function.  That in turn is called
427c478bd9Sstevel@tonic-gate  * by one of do_interactive(), cmd_file() or one_command_at_a_time(), each
437c478bd9Sstevel@tonic-gate  * of which is called from main() depending on how the program was invoked.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * The rest of this module consists of the various function handlers and
467c478bd9Sstevel@tonic-gate  * their helper functions.  Some of these functions, particularly the
477c478bd9Sstevel@tonic-gate  * X_to_str() functions, which maps command, resource and property numbers
487c478bd9Sstevel@tonic-gate  * to strings, are used quite liberally, as doing so results in a better
497c478bd9Sstevel@tonic-gate  * program w/rt I18N, reducing the need for translation notes.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
537c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
547c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <errno.h>
579acbbeafSnn #include <fcntl.h>
587c478bd9Sstevel@tonic-gate #include <strings.h>
597c478bd9Sstevel@tonic-gate #include <unistd.h>
607c478bd9Sstevel@tonic-gate #include <ctype.h>
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate #include <assert.h>
637c478bd9Sstevel@tonic-gate #include <sys/stat.h>
647c478bd9Sstevel@tonic-gate #include <zone.h>
657c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
667c478bd9Sstevel@tonic-gate #include <netdb.h>
677c478bd9Sstevel@tonic-gate #include <locale.h>
687c478bd9Sstevel@tonic-gate #include <libintl.h>
697c478bd9Sstevel@tonic-gate #include <alloca.h>
707c478bd9Sstevel@tonic-gate #include <signal.h>
719acbbeafSnn #include <wait.h>
727c478bd9Sstevel@tonic-gate #include <libtecla.h>
73fa9e4066Sahrens #include <libzfs.h>
749acbbeafSnn #include <sys/brand.h>
759acbbeafSnn #include <libbrand.h>
765679c89fSjv #include <sys/systeminfo.h>
77c9f134eaSjv #include <libdladm.h>
78c9f134eaSjv #include <libinetutil.h>
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
817c478bd9Sstevel@tonic-gate #include "zonecfg.h"
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
847c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	PAGER	"/usr/bin/more"
889acbbeafSnn #define	EXEC_PREFIX	"exec "
899acbbeafSnn #define	EXEC_LEN	(strlen(EXEC_PREFIX))
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate struct help {
927c478bd9Sstevel@tonic-gate 	uint_t	cmd_num;
937c478bd9Sstevel@tonic-gate 	char	*cmd_name;
947c478bd9Sstevel@tonic-gate 	uint_t	flags;
957c478bd9Sstevel@tonic-gate 	char	*short_usage;
967c478bd9Sstevel@tonic-gate };
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate extern int yyparse(void);
997c478bd9Sstevel@tonic-gate extern int lex_lineno;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #define	MAX_LINE_LEN	1024
1027c478bd9Sstevel@tonic-gate #define	MAX_CMD_HIST	1024
1039acbbeafSnn #define	MAX_CMD_LEN	1024
1047c478bd9Sstevel@tonic-gate 
1050209230bSgjelinek #define	ONE_MB		1048576
1060209230bSgjelinek 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Each SHELP_ should be a simple string.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #define	SHELP_ADD	"add <resource-type>\n\t(global scope)\n" \
1127c478bd9Sstevel@tonic-gate 	"add <property-name> <property-value>\n\t(resource scope)"
1137c478bd9Sstevel@tonic-gate #define	SHELP_CANCEL	"cancel"
1140209230bSgjelinek #define	SHELP_CLEAR	"clear <property-name>"
1157c478bd9Sstevel@tonic-gate #define	SHELP_COMMIT	"commit"
116ee519a1fSgjelinek #define	SHELP_CREATE	"create [-F] [ -a <path> | -b | -t <template> ]"
1177c478bd9Sstevel@tonic-gate #define	SHELP_DELETE	"delete [-F]"
1187c478bd9Sstevel@tonic-gate #define	SHELP_END	"end"
1197c478bd9Sstevel@tonic-gate #define	SHELP_EXIT	"exit [-F]"
1207c478bd9Sstevel@tonic-gate #define	SHELP_EXPORT	"export [-f output-file]"
1217c478bd9Sstevel@tonic-gate #define	SHELP_HELP	"help [commands] [syntax] [usage] [<command-name>]"
1227c478bd9Sstevel@tonic-gate #define	SHELP_INFO	"info [<resource-type> [property-name=property-value]*]"
1230209230bSgjelinek #define	SHELP_REMOVE	"remove [-F] <resource-type> " \
1240209230bSgjelinek 	"[ <property-name>=<property-value> ]*\n" \
1250209230bSgjelinek 	"\t(global scope)\n" \
1260209230bSgjelinek 	"remove <property-name> <property-value>\n" \
1270209230bSgjelinek 	"\t(resource scope)"
1287c478bd9Sstevel@tonic-gate #define	SHELP_REVERT	"revert [-F]"
1297c478bd9Sstevel@tonic-gate #define	SHELP_SELECT	"select <resource-type> { <property-name>=" \
1307c478bd9Sstevel@tonic-gate 	"<property-value> }"
1317c478bd9Sstevel@tonic-gate #define	SHELP_SET	"set <property-name>=<property-value>"
1327c478bd9Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate static struct help helptab[] = {
1357c478bd9Sstevel@tonic-gate 	{ CMD_ADD,	"add",		HELP_RES_PROPS,	SHELP_ADD, },
1367c478bd9Sstevel@tonic-gate 	{ CMD_CANCEL,	"cancel",	0,		SHELP_CANCEL, },
1370209230bSgjelinek 	{ CMD_CLEAR,	"clear",	HELP_PROPS,	SHELP_CLEAR, },
1387c478bd9Sstevel@tonic-gate 	{ CMD_COMMIT,	"commit",	0,		SHELP_COMMIT, },
1397c478bd9Sstevel@tonic-gate 	{ CMD_CREATE,	"create",	0,		SHELP_CREATE, },
1407c478bd9Sstevel@tonic-gate 	{ CMD_DELETE,	"delete",	0,		SHELP_DELETE, },
1417c478bd9Sstevel@tonic-gate 	{ CMD_END,	"end",		0,		SHELP_END, },
1427c478bd9Sstevel@tonic-gate 	{ CMD_EXIT,	"exit",		0,		SHELP_EXIT, },
1437c478bd9Sstevel@tonic-gate 	{ CMD_EXPORT,	"export",	0,		SHELP_EXPORT, },
1447c478bd9Sstevel@tonic-gate 	{ CMD_HELP,	"help",		0,		SHELP_HELP },
1457c478bd9Sstevel@tonic-gate 	{ CMD_INFO,	"info",		HELP_RES_PROPS,	SHELP_INFO, },
1467c478bd9Sstevel@tonic-gate 	{ CMD_REMOVE,	"remove",	HELP_RES_PROPS,	SHELP_REMOVE, },
1477c478bd9Sstevel@tonic-gate 	{ CMD_REVERT,	"revert",	0,		SHELP_REVERT, },
1487c478bd9Sstevel@tonic-gate 	{ CMD_SELECT,	"select",	HELP_RES_PROPS,	SHELP_SELECT, },
1497c478bd9Sstevel@tonic-gate 	{ CMD_SET,	"set",		HELP_PROPS,	SHELP_SET, },
1507c478bd9Sstevel@tonic-gate 	{ CMD_VERIFY,	"verify",	0,		SHELP_VERIFY, },
1517c478bd9Sstevel@tonic-gate 	{ 0 },
1527c478bd9Sstevel@tonic-gate };
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #define	MAX_RT_STRLEN	16
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /* These *must* match the order of the RT_ define's from zonecfg.h */
157c94c1ef0Sjv char *res_types[] = {
1587c478bd9Sstevel@tonic-gate 	"unknown",
159087719fdSdp 	"zonename",
1607c478bd9Sstevel@tonic-gate 	"zonepath",
1617c478bd9Sstevel@tonic-gate 	"autoboot",
1627c478bd9Sstevel@tonic-gate 	"pool",
1637c478bd9Sstevel@tonic-gate 	"fs",
1647c478bd9Sstevel@tonic-gate 	"inherit-pkg-dir",
1657c478bd9Sstevel@tonic-gate 	"net",
1667c478bd9Sstevel@tonic-gate 	"device",
1677c478bd9Sstevel@tonic-gate 	"rctl",
1687c478bd9Sstevel@tonic-gate 	"attr",
169fa9e4066Sahrens 	"dataset",
170ffbafc53Scomay 	"limitpriv",
1713f2f09c1Sdp 	"bootargs",
1729acbbeafSnn 	"brand",
1730209230bSgjelinek 	"dedicated-cpu",
1740209230bSgjelinek 	"capped-memory",
1750209230bSgjelinek 	ALIAS_MAXLWPS,
1760209230bSgjelinek 	ALIAS_MAXSHMMEM,
1770209230bSgjelinek 	ALIAS_MAXSHMIDS,
1780209230bSgjelinek 	ALIAS_MAXMSGIDS,
1790209230bSgjelinek 	ALIAS_MAXSEMIDS,
1800209230bSgjelinek 	ALIAS_SHARES,
1810209230bSgjelinek 	"scheduling-class",
182f4b3ec61Sdh 	"ip-type",
183c97ad5cdSakolb 	"capped-cpu",
1845679c89fSjv 	"hostid",
1857c478bd9Sstevel@tonic-gate 	NULL
1867c478bd9Sstevel@tonic-gate };
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /* These *must* match the order of the PT_ define's from zonecfg.h */
189c94c1ef0Sjv char *prop_types[] = {
1907c478bd9Sstevel@tonic-gate 	"unknown",
191087719fdSdp 	"zonename",
1927c478bd9Sstevel@tonic-gate 	"zonepath",
1937c478bd9Sstevel@tonic-gate 	"autoboot",
1947c478bd9Sstevel@tonic-gate 	"pool",
1957c478bd9Sstevel@tonic-gate 	"dir",
1967c478bd9Sstevel@tonic-gate 	"special",
1977c478bd9Sstevel@tonic-gate 	"type",
1987c478bd9Sstevel@tonic-gate 	"options",
1997c478bd9Sstevel@tonic-gate 	"address",
2007c478bd9Sstevel@tonic-gate 	"physical",
2017c478bd9Sstevel@tonic-gate 	"name",
2027c478bd9Sstevel@tonic-gate 	"value",
2037c478bd9Sstevel@tonic-gate 	"match",
2047c478bd9Sstevel@tonic-gate 	"priv",
2057c478bd9Sstevel@tonic-gate 	"limit",
2067c478bd9Sstevel@tonic-gate 	"action",
2077c478bd9Sstevel@tonic-gate 	"raw",
208ffbafc53Scomay 	"limitpriv",
2093f2f09c1Sdp 	"bootargs",
2109acbbeafSnn 	"brand",
2110209230bSgjelinek 	"ncpus",
2120209230bSgjelinek 	"importance",
2130209230bSgjelinek 	"swap",
2140209230bSgjelinek 	"locked",
2150209230bSgjelinek 	ALIAS_SHARES,
2160209230bSgjelinek 	ALIAS_MAXLWPS,
2170209230bSgjelinek 	ALIAS_MAXSHMMEM,
2180209230bSgjelinek 	ALIAS_MAXSHMIDS,
2190209230bSgjelinek 	ALIAS_MAXMSGIDS,
2200209230bSgjelinek 	ALIAS_MAXSEMIDS,
2210209230bSgjelinek 	ALIAS_MAXLOCKEDMEM,
2220209230bSgjelinek 	ALIAS_MAXSWAP,
2230209230bSgjelinek 	"scheduling-class",
224f4b3ec61Sdh 	"ip-type",
225de860bd9Sgfaden 	"defrouter",
2265679c89fSjv 	"hostid",
2277c478bd9Sstevel@tonic-gate 	NULL
2287c478bd9Sstevel@tonic-gate };
2297c478bd9Sstevel@tonic-gate 
230ffbafc53Scomay /* These *must* match the order of the PROP_VAL_ define's from zonecfg.h */
2317c478bd9Sstevel@tonic-gate static char *prop_val_types[] = {
2327c478bd9Sstevel@tonic-gate 	"simple",
2337c478bd9Sstevel@tonic-gate 	"complex",
2347c478bd9Sstevel@tonic-gate 	"list",
2357c478bd9Sstevel@tonic-gate };
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * The various _cmds[] lists below are for command tab-completion.
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate /*
2427c478bd9Sstevel@tonic-gate  * remove has a space afterwards because it has qualifiers; the other commands
2430209230bSgjelinek  * that have qualifiers (add, select, etc.) don't need a space here because
2447c478bd9Sstevel@tonic-gate  * they have their own _cmds[] lists below.
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate static const char *global_scope_cmds[] = {
2477c478bd9Sstevel@tonic-gate 	"add",
2480209230bSgjelinek 	"clear",
2497c478bd9Sstevel@tonic-gate 	"commit",
2507c478bd9Sstevel@tonic-gate 	"create",
2517c478bd9Sstevel@tonic-gate 	"delete",
2527c478bd9Sstevel@tonic-gate 	"exit",
2537c478bd9Sstevel@tonic-gate 	"export",
2547c478bd9Sstevel@tonic-gate 	"help",
2557c478bd9Sstevel@tonic-gate 	"info",
2567c478bd9Sstevel@tonic-gate 	"remove ",
2577c478bd9Sstevel@tonic-gate 	"revert",
2587c478bd9Sstevel@tonic-gate 	"select",
2597c478bd9Sstevel@tonic-gate 	"set",
2607c478bd9Sstevel@tonic-gate 	"verify",
2617c478bd9Sstevel@tonic-gate 	NULL
2627c478bd9Sstevel@tonic-gate };
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static const char *add_cmds[] = {
2657c478bd9Sstevel@tonic-gate 	"add fs",
2667c478bd9Sstevel@tonic-gate 	"add inherit-pkg-dir",
2677c478bd9Sstevel@tonic-gate 	"add net",
2687c478bd9Sstevel@tonic-gate 	"add device",
2697c478bd9Sstevel@tonic-gate 	"add rctl",
2707c478bd9Sstevel@tonic-gate 	"add attr",
271fa9e4066Sahrens 	"add dataset",
2720209230bSgjelinek 	"add dedicated-cpu",
273c97ad5cdSakolb 	"add capped-cpu",
2740209230bSgjelinek 	"add capped-memory",
2750209230bSgjelinek 	NULL
2760209230bSgjelinek };
2770209230bSgjelinek 
2780209230bSgjelinek static const char *clear_cmds[] = {
2790209230bSgjelinek 	"clear autoboot",
2800209230bSgjelinek 	"clear pool",
2810209230bSgjelinek 	"clear limitpriv",
2820209230bSgjelinek 	"clear bootargs",
2830209230bSgjelinek 	"clear scheduling-class",
284f4b3ec61Sdh 	"clear ip-type",
2850209230bSgjelinek 	"clear " ALIAS_MAXLWPS,
2860209230bSgjelinek 	"clear " ALIAS_MAXSHMMEM,
2870209230bSgjelinek 	"clear " ALIAS_MAXSHMIDS,
2880209230bSgjelinek 	"clear " ALIAS_MAXMSGIDS,
2890209230bSgjelinek 	"clear " ALIAS_MAXSEMIDS,
2900209230bSgjelinek 	"clear " ALIAS_SHARES,
2917c478bd9Sstevel@tonic-gate 	NULL
2927c478bd9Sstevel@tonic-gate };
2937c478bd9Sstevel@tonic-gate 
2949e7542f4Sdp static const char *remove_cmds[] = {
2959e7542f4Sdp 	"remove fs ",
2969e7542f4Sdp 	"remove inherit-pkg-dir ",
2979e7542f4Sdp 	"remove net ",
2989e7542f4Sdp 	"remove device ",
2999e7542f4Sdp 	"remove rctl ",
3009e7542f4Sdp 	"remove attr ",
3019e7542f4Sdp 	"remove dataset ",
3020209230bSgjelinek 	"remove dedicated-cpu ",
303c97ad5cdSakolb 	"remove capped-cpu ",
3040209230bSgjelinek 	"remove capped-memory ",
3059e7542f4Sdp 	NULL
3069e7542f4Sdp };
3079e7542f4Sdp 
3087c478bd9Sstevel@tonic-gate static const char *select_cmds[] = {
309087719fdSdp 	"select fs ",
310087719fdSdp 	"select inherit-pkg-dir ",
311087719fdSdp 	"select net ",
312087719fdSdp 	"select device ",
313087719fdSdp 	"select rctl ",
314087719fdSdp 	"select attr ",
315fa9e4066Sahrens 	"select dataset ",
3160209230bSgjelinek 	"select dedicated-cpu",
317c97ad5cdSakolb 	"select capped-cpu",
3180209230bSgjelinek 	"select capped-memory",
3197c478bd9Sstevel@tonic-gate 	NULL
3207c478bd9Sstevel@tonic-gate };
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate static const char *set_cmds[] = {
323087719fdSdp 	"set zonename=",
324087719fdSdp 	"set zonepath=",
3259acbbeafSnn 	"set brand=",
326087719fdSdp 	"set autoboot=",
327087719fdSdp 	"set pool=",
328ffbafc53Scomay 	"set limitpriv=",
3293f2f09c1Sdp 	"set bootargs=",
3300209230bSgjelinek 	"set scheduling-class=",
331f4b3ec61Sdh 	"set ip-type=",
3320209230bSgjelinek 	"set " ALIAS_MAXLWPS "=",
3330209230bSgjelinek 	"set " ALIAS_MAXSHMMEM "=",
3340209230bSgjelinek 	"set " ALIAS_MAXSHMIDS "=",
3350209230bSgjelinek 	"set " ALIAS_MAXMSGIDS "=",
3360209230bSgjelinek 	"set " ALIAS_MAXSEMIDS "=",
3370209230bSgjelinek 	"set " ALIAS_SHARES "=",
3385679c89fSjv 	"set hostid=",
3397c478bd9Sstevel@tonic-gate 	NULL
3407c478bd9Sstevel@tonic-gate };
3417c478bd9Sstevel@tonic-gate 
3429e7542f4Sdp static const char *info_cmds[] = {
3439e7542f4Sdp 	"info fs ",
3449e7542f4Sdp 	"info inherit-pkg-dir ",
3459e7542f4Sdp 	"info net ",
3469e7542f4Sdp 	"info device ",
3479e7542f4Sdp 	"info rctl ",
3489e7542f4Sdp 	"info attr ",
3499e7542f4Sdp 	"info dataset ",
3500209230bSgjelinek 	"info capped-memory",
3510209230bSgjelinek 	"info dedicated-cpu",
352c97ad5cdSakolb 	"info capped-cpu",
3539e7542f4Sdp 	"info zonename",
3549e7542f4Sdp 	"info zonepath",
3559e7542f4Sdp 	"info autoboot",
3569e7542f4Sdp 	"info pool",
3579e7542f4Sdp 	"info limitpriv",
3589e7542f4Sdp 	"info bootargs",
3590209230bSgjelinek 	"info brand",
3600209230bSgjelinek 	"info scheduling-class",
361f4b3ec61Sdh 	"info ip-type",
3620209230bSgjelinek 	"info max-lwps",
3630209230bSgjelinek 	"info max-shm-memory",
3640209230bSgjelinek 	"info max-shm-ids",
3650209230bSgjelinek 	"info max-msg-ids",
3660209230bSgjelinek 	"info max-sem-ids",
3670209230bSgjelinek 	"info cpu-shares",
3685679c89fSjv 	"info hostid",
3699e7542f4Sdp 	NULL
3709e7542f4Sdp };
3719e7542f4Sdp 
3727c478bd9Sstevel@tonic-gate static const char *fs_res_scope_cmds[] = {
3737c478bd9Sstevel@tonic-gate 	"add options ",
3747c478bd9Sstevel@tonic-gate 	"cancel",
3757c478bd9Sstevel@tonic-gate 	"end",
3767c478bd9Sstevel@tonic-gate 	"exit",
3777c478bd9Sstevel@tonic-gate 	"help",
3787c478bd9Sstevel@tonic-gate 	"info",
379ffbafc53Scomay 	"remove options ",
3807c478bd9Sstevel@tonic-gate 	"set dir=",
3817c478bd9Sstevel@tonic-gate 	"set raw=",
3827c478bd9Sstevel@tonic-gate 	"set special=",
3837c478bd9Sstevel@tonic-gate 	"set type=",
3840209230bSgjelinek 	"clear raw",
3857c478bd9Sstevel@tonic-gate 	NULL
3867c478bd9Sstevel@tonic-gate };
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate static const char *net_res_scope_cmds[] = {
3897c478bd9Sstevel@tonic-gate 	"cancel",
3907c478bd9Sstevel@tonic-gate 	"end",
3917c478bd9Sstevel@tonic-gate 	"exit",
3927c478bd9Sstevel@tonic-gate 	"help",
3937c478bd9Sstevel@tonic-gate 	"info",
3947c478bd9Sstevel@tonic-gate 	"set address=",
3957c478bd9Sstevel@tonic-gate 	"set physical=",
3967c478bd9Sstevel@tonic-gate 	NULL
3977c478bd9Sstevel@tonic-gate };
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static const char *ipd_res_scope_cmds[] = {
4007c478bd9Sstevel@tonic-gate 	"cancel",
4017c478bd9Sstevel@tonic-gate 	"end",
4027c478bd9Sstevel@tonic-gate 	"exit",
4037c478bd9Sstevel@tonic-gate 	"help",
4047c478bd9Sstevel@tonic-gate 	"info",
4057c478bd9Sstevel@tonic-gate 	"set dir=",
4067c478bd9Sstevel@tonic-gate 	NULL
4077c478bd9Sstevel@tonic-gate };
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate static const char *device_res_scope_cmds[] = {
4107c478bd9Sstevel@tonic-gate 	"cancel",
4117c478bd9Sstevel@tonic-gate 	"end",
4127c478bd9Sstevel@tonic-gate 	"exit",
4137c478bd9Sstevel@tonic-gate 	"help",
4147c478bd9Sstevel@tonic-gate 	"info",
4157c478bd9Sstevel@tonic-gate 	"set match=",
4167c478bd9Sstevel@tonic-gate 	NULL
4177c478bd9Sstevel@tonic-gate };
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate static const char *attr_res_scope_cmds[] = {
4207c478bd9Sstevel@tonic-gate 	"cancel",
4217c478bd9Sstevel@tonic-gate 	"end",
4227c478bd9Sstevel@tonic-gate 	"exit",
4237c478bd9Sstevel@tonic-gate 	"help",
4247c478bd9Sstevel@tonic-gate 	"info",
4257c478bd9Sstevel@tonic-gate 	"set name=",
4267c478bd9Sstevel@tonic-gate 	"set type=",
4277c478bd9Sstevel@tonic-gate 	"set value=",
4287c478bd9Sstevel@tonic-gate 	NULL
4297c478bd9Sstevel@tonic-gate };
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate static const char *rctl_res_scope_cmds[] = {
4327c478bd9Sstevel@tonic-gate 	"add value ",
4337c478bd9Sstevel@tonic-gate 	"cancel",
4347c478bd9Sstevel@tonic-gate 	"end",
4357c478bd9Sstevel@tonic-gate 	"exit",
4367c478bd9Sstevel@tonic-gate 	"help",
4377c478bd9Sstevel@tonic-gate 	"info",
438ffbafc53Scomay 	"remove value ",
4397c478bd9Sstevel@tonic-gate 	"set name=",
4407c478bd9Sstevel@tonic-gate 	NULL
4417c478bd9Sstevel@tonic-gate };
4427c478bd9Sstevel@tonic-gate 
443fa9e4066Sahrens static const char *dataset_res_scope_cmds[] = {
444fa9e4066Sahrens 	"cancel",
445fa9e4066Sahrens 	"end",
446fa9e4066Sahrens 	"exit",
447fa9e4066Sahrens 	"help",
448fa9e4066Sahrens 	"info",
449fa9e4066Sahrens 	"set name=",
450fa9e4066Sahrens 	NULL
451fa9e4066Sahrens };
452fa9e4066Sahrens 
4530209230bSgjelinek static const char *pset_res_scope_cmds[] = {
4540209230bSgjelinek 	"cancel",
4550209230bSgjelinek 	"end",
4560209230bSgjelinek 	"exit",
4570209230bSgjelinek 	"help",
4580209230bSgjelinek 	"info",
4590209230bSgjelinek 	"set ncpus=",
4600209230bSgjelinek 	"set importance=",
4610209230bSgjelinek 	"clear importance",
4620209230bSgjelinek 	NULL
4630209230bSgjelinek };
4640209230bSgjelinek 
465c97ad5cdSakolb static const char *pcap_res_scope_cmds[] = {
466c97ad5cdSakolb 	"cancel",
467c97ad5cdSakolb 	"end",
468c97ad5cdSakolb 	"exit",
469c97ad5cdSakolb 	"help",
470c97ad5cdSakolb 	"info",
471c97ad5cdSakolb 	"set ncpus=",
472c97ad5cdSakolb 	NULL
473c97ad5cdSakolb };
474c97ad5cdSakolb 
4750209230bSgjelinek static const char *mcap_res_scope_cmds[] = {
4760209230bSgjelinek 	"cancel",
4770209230bSgjelinek 	"end",
4780209230bSgjelinek 	"exit",
4790209230bSgjelinek 	"help",
4800209230bSgjelinek 	"info",
4810209230bSgjelinek 	"set physical=",
4820209230bSgjelinek 	"set swap=",
4830209230bSgjelinek 	"set locked=",
4840209230bSgjelinek 	"clear physical",
4850209230bSgjelinek 	"clear swap",
4860209230bSgjelinek 	"clear locked",
4870209230bSgjelinek 	NULL
4880209230bSgjelinek };
4890209230bSgjelinek 
4907c478bd9Sstevel@tonic-gate /* Global variables */
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
4937c478bd9Sstevel@tonic-gate static char *execname;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /* set in main(), used all over the place */
4967c478bd9Sstevel@tonic-gate static zone_dochandle_t handle;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate /* used all over the place */
499087719fdSdp static char zone[ZONENAME_MAX];
500087719fdSdp static char revert_zone[ZONENAME_MAX];
5017c478bd9Sstevel@tonic-gate 
5029acbbeafSnn /* global brand operations */
503123807fbSedp static brand_handle_t brand;
5049acbbeafSnn 
5057c478bd9Sstevel@tonic-gate /* set in modifying functions, checked in read_input() */
506bbec428eSgjelinek static boolean_t need_to_commit = B_FALSE;
507bbec428eSgjelinek boolean_t saw_error;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /* set in yacc parser, checked in read_input() */
510bbec428eSgjelinek boolean_t newline_terminated;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /* set in main(), checked in lex error handler */
513bbec428eSgjelinek boolean_t cmd_file_mode;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate /* set in exit_func(), checked in read_input() */
516bbec428eSgjelinek static boolean_t time_to_exit = B_FALSE, force_exit = B_FALSE;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate /* used in short_usage() and zerr() */
5197c478bd9Sstevel@tonic-gate static char *cmd_file_name = NULL;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /* checked in read_input() and other places */
522bbec428eSgjelinek static boolean_t ok_to_prompt = B_FALSE;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate /* set and checked in initialize() */
525bbec428eSgjelinek static boolean_t got_handle = B_FALSE;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /* initialized in do_interactive(), checked in initialize() */
528bbec428eSgjelinek static boolean_t interactive_mode;
5297c478bd9Sstevel@tonic-gate 
5300209230bSgjelinek /* set if configuring the global zone */
531bbec428eSgjelinek static boolean_t global_zone = B_FALSE;
5320209230bSgjelinek 
5337c478bd9Sstevel@tonic-gate /* set in main(), checked in multiple places */
534bbec428eSgjelinek static boolean_t read_only_mode;
5357c478bd9Sstevel@tonic-gate 
536bbec428eSgjelinek /* scope is outer/global or inner/resource */
537bbec428eSgjelinek static boolean_t global_scope = B_TRUE;
5387c478bd9Sstevel@tonic-gate static int resource_scope;	/* should be in the RT_ list from zonecfg.h */
5397c478bd9Sstevel@tonic-gate static int end_op = -1;		/* operation on end is either add or modify */
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate int num_prop_vals;		/* for grammar */
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * These are for keeping track of resources as they are specified as part of
5457c478bd9Sstevel@tonic-gate  * the multi-step process.  They should be initialized by add_resource() or
5467c478bd9Sstevel@tonic-gate  * select_func() and filled in by add_property() or set_func().
5477c478bd9Sstevel@tonic-gate  */
5487c478bd9Sstevel@tonic-gate static struct zone_fstab	old_fstab, in_progress_fstab;
5497c478bd9Sstevel@tonic-gate static struct zone_fstab	old_ipdtab, in_progress_ipdtab;
5507c478bd9Sstevel@tonic-gate static struct zone_nwiftab	old_nwiftab, in_progress_nwiftab;
5517c478bd9Sstevel@tonic-gate static struct zone_devtab	old_devtab, in_progress_devtab;
5527c478bd9Sstevel@tonic-gate static struct zone_rctltab	old_rctltab, in_progress_rctltab;
5537c478bd9Sstevel@tonic-gate static struct zone_attrtab	old_attrtab, in_progress_attrtab;
554fa9e4066Sahrens static struct zone_dstab	old_dstab, in_progress_dstab;
5550209230bSgjelinek static struct zone_psettab	old_psettab, in_progress_psettab;
5560209230bSgjelinek static struct zone_mcaptab	old_mcaptab, in_progress_mcaptab;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate static GetLine *gl;	/* The gl_get_line() resource object */
5597c478bd9Sstevel@tonic-gate 
5600209230bSgjelinek static void bytes_to_units(char *str, char *buf, int bufsize);
5610209230bSgjelinek 
5627c478bd9Sstevel@tonic-gate /* Functions begin here */
5637c478bd9Sstevel@tonic-gate 
564bbec428eSgjelinek static boolean_t
5657c478bd9Sstevel@tonic-gate initial_match(const char *line1, const char *line2, int word_end)
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate 	if (word_end <= 0)
568bbec428eSgjelinek 		return (B_TRUE);
5697c478bd9Sstevel@tonic-gate 	return (strncmp(line1, line2, word_end) == 0);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate static int
5737c478bd9Sstevel@tonic-gate add_stuff(WordCompletion *cpl, const char *line1, const char **list,
5747c478bd9Sstevel@tonic-gate     int word_end)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	int i, err;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	for (i = 0; list[i] != NULL; i++) {
5797c478bd9Sstevel@tonic-gate 		if (initial_match(line1, list[i], word_end)) {
5807c478bd9Sstevel@tonic-gate 			err = cpl_add_completion(cpl, line1, 0, word_end,
5817c478bd9Sstevel@tonic-gate 			    list[i] + word_end, "", "");
5827c478bd9Sstevel@tonic-gate 			if (err != 0)
5837c478bd9Sstevel@tonic-gate 				return (err);
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	return (0);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate static
5907c478bd9Sstevel@tonic-gate /* ARGSUSED */
5917c478bd9Sstevel@tonic-gate CPL_MATCH_FN(cmd_cpl_fn)
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	if (global_scope) {
5947c478bd9Sstevel@tonic-gate 		/*
5957c478bd9Sstevel@tonic-gate 		 * The MAX/MIN tests below are to make sure we have at least
5967c478bd9Sstevel@tonic-gate 		 * enough characters to distinguish from other prefixes (MAX)
5977c478bd9Sstevel@tonic-gate 		 * but only check MIN(what we have, what we're checking).
5987c478bd9Sstevel@tonic-gate 		 */
5997c478bd9Sstevel@tonic-gate 		if (strncmp(line, "add ", MAX(MIN(word_end, 4), 1)) == 0)
6007c478bd9Sstevel@tonic-gate 			return (add_stuff(cpl, line, add_cmds, word_end));
6010209230bSgjelinek 		if (strncmp(line, "clear ", MAX(MIN(word_end, 6), 2)) == 0)
6020209230bSgjelinek 			return (add_stuff(cpl, line, clear_cmds, word_end));
6037c478bd9Sstevel@tonic-gate 		if (strncmp(line, "select ", MAX(MIN(word_end, 7), 3)) == 0)
6047c478bd9Sstevel@tonic-gate 			return (add_stuff(cpl, line, select_cmds, word_end));
6057c478bd9Sstevel@tonic-gate 		if (strncmp(line, "set ", MAX(MIN(word_end, 4), 3)) == 0)
6067c478bd9Sstevel@tonic-gate 			return (add_stuff(cpl, line, set_cmds, word_end));
6079e7542f4Sdp 		if (strncmp(line, "remove ", MAX(MIN(word_end, 7), 1)) == 0)
6089e7542f4Sdp 			return (add_stuff(cpl, line, remove_cmds, word_end));
6099e7542f4Sdp 		if (strncmp(line, "info ", MAX(MIN(word_end, 5), 1)) == 0)
6109e7542f4Sdp 			return (add_stuff(cpl, line, info_cmds, word_end));
6117c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, global_scope_cmds, word_end));
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 	switch (resource_scope) {
6147c478bd9Sstevel@tonic-gate 	case RT_FS:
6157c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, fs_res_scope_cmds, word_end));
6167c478bd9Sstevel@tonic-gate 	case RT_IPD:
6177c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, ipd_res_scope_cmds, word_end));
6187c478bd9Sstevel@tonic-gate 	case RT_NET:
6197c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, net_res_scope_cmds, word_end));
6207c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
6217c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, device_res_scope_cmds, word_end));
6227c478bd9Sstevel@tonic-gate 	case RT_RCTL:
6237c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, rctl_res_scope_cmds, word_end));
6247c478bd9Sstevel@tonic-gate 	case RT_ATTR:
6257c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, attr_res_scope_cmds, word_end));
626fa9e4066Sahrens 	case RT_DATASET:
627fa9e4066Sahrens 		return (add_stuff(cpl, line, dataset_res_scope_cmds, word_end));
6280209230bSgjelinek 	case RT_DCPU:
6290209230bSgjelinek 		return (add_stuff(cpl, line, pset_res_scope_cmds, word_end));
630c97ad5cdSakolb 	case RT_PCAP:
631c97ad5cdSakolb 		return (add_stuff(cpl, line, pcap_res_scope_cmds, word_end));
6320209230bSgjelinek 	case RT_MCAP:
6330209230bSgjelinek 		return (add_stuff(cpl, line, mcap_res_scope_cmds, word_end));
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	return (0);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * For the main CMD_func() functions below, several of them call getopt()
6407c478bd9Sstevel@tonic-gate  * then check optind against argc to make sure an extra parameter was not
6417c478bd9Sstevel@tonic-gate  * passed in.  The reason this is not caught in the grammar is that the
6427c478bd9Sstevel@tonic-gate  * grammar just checks for a miscellaneous TOKEN, which is *expected* to
6437c478bd9Sstevel@tonic-gate  * be "-F" (for example), but could be anything.  So (for example) this
6447c478bd9Sstevel@tonic-gate  * check will prevent "create bogus".
6457c478bd9Sstevel@tonic-gate  */
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate cmd_t *
6487c478bd9Sstevel@tonic-gate alloc_cmd(void)
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	return (calloc(1, sizeof (cmd_t)));
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate void
6547c478bd9Sstevel@tonic-gate free_cmd(cmd_t *cmd)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	int i;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_EQ_PROP_PAIRS; i++)
6597c478bd9Sstevel@tonic-gate 		if (cmd->cmd_property_ptr[i] != NULL) {
6607c478bd9Sstevel@tonic-gate 			property_value_ptr_t pp = cmd->cmd_property_ptr[i];
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 			switch (pp->pv_type) {
6637c478bd9Sstevel@tonic-gate 			case PROP_VAL_SIMPLE:
6647c478bd9Sstevel@tonic-gate 				free(pp->pv_simple);
6657c478bd9Sstevel@tonic-gate 				break;
6667c478bd9Sstevel@tonic-gate 			case PROP_VAL_COMPLEX:
6677c478bd9Sstevel@tonic-gate 				free_complex(pp->pv_complex);
6687c478bd9Sstevel@tonic-gate 				break;
6697c478bd9Sstevel@tonic-gate 			case PROP_VAL_LIST:
6707c478bd9Sstevel@tonic-gate 				free_list(pp->pv_list);
6717c478bd9Sstevel@tonic-gate 				break;
6727c478bd9Sstevel@tonic-gate 			}
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_argc; i++)
6757c478bd9Sstevel@tonic-gate 		free(cmd->cmd_argv[i]);
6767c478bd9Sstevel@tonic-gate 	free(cmd);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate complex_property_ptr_t
6807c478bd9Sstevel@tonic-gate alloc_complex(void)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	return (calloc(1, sizeof (complex_property_t)));
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate void
6867c478bd9Sstevel@tonic-gate free_complex(complex_property_ptr_t complex)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate 	if (complex == NULL)
6897c478bd9Sstevel@tonic-gate 		return;
6907c478bd9Sstevel@tonic-gate 	free_complex(complex->cp_next);
6917c478bd9Sstevel@tonic-gate 	if (complex->cp_value != NULL)
6927c478bd9Sstevel@tonic-gate 		free(complex->cp_value);
6937c478bd9Sstevel@tonic-gate 	free(complex);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate list_property_ptr_t
6977c478bd9Sstevel@tonic-gate alloc_list(void)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	return (calloc(1, sizeof (list_property_t)));
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate void
7037c478bd9Sstevel@tonic-gate free_list(list_property_ptr_t list)
7047c478bd9Sstevel@tonic-gate {
7057c478bd9Sstevel@tonic-gate 	if (list == NULL)
7067c478bd9Sstevel@tonic-gate 		return;
7077c478bd9Sstevel@tonic-gate 	if (list->lp_simple != NULL)
7087c478bd9Sstevel@tonic-gate 		free(list->lp_simple);
7097c478bd9Sstevel@tonic-gate 	free_complex(list->lp_complex);
7107c478bd9Sstevel@tonic-gate 	free_list(list->lp_next);
7117c478bd9Sstevel@tonic-gate 	free(list);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate void
7157c478bd9Sstevel@tonic-gate free_outer_list(list_property_ptr_t list)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate 	if (list == NULL)
7187c478bd9Sstevel@tonic-gate 		return;
7197c478bd9Sstevel@tonic-gate 	free_outer_list(list->lp_next);
7207c478bd9Sstevel@tonic-gate 	free(list);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate static struct zone_rctlvaltab *
7247c478bd9Sstevel@tonic-gate alloc_rctlvaltab(void)
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate 	return (calloc(1, sizeof (struct zone_rctlvaltab)));
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate static char *
7307c478bd9Sstevel@tonic-gate rt_to_str(int res_type)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate 	assert(res_type >= RT_MIN && res_type <= RT_MAX);
7337c478bd9Sstevel@tonic-gate 	return (res_types[res_type]);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate static char *
7377c478bd9Sstevel@tonic-gate pt_to_str(int prop_type)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	assert(prop_type >= PT_MIN && prop_type <= PT_MAX);
7407c478bd9Sstevel@tonic-gate 	return (prop_types[prop_type]);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate static char *
7447c478bd9Sstevel@tonic-gate pvt_to_str(int pv_type)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	assert(pv_type >= PROP_VAL_MIN && pv_type <= PROP_VAL_MAX);
7477c478bd9Sstevel@tonic-gate 	return (prop_val_types[pv_type]);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate static char *
7517c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
7547c478bd9Sstevel@tonic-gate 	return (helptab[cmd_num].cmd_name);
7557c478bd9Sstevel@tonic-gate }
7567c478bd9Sstevel@tonic-gate 
757*3042b8b5Sbatschul /* PRINTFLIKE1 */
758*3042b8b5Sbatschul static void
759*3042b8b5Sbatschul zerr(const char *fmt, ...)
760*3042b8b5Sbatschul {
761*3042b8b5Sbatschul 	va_list alist;
762*3042b8b5Sbatschul 	static int last_lineno;
763*3042b8b5Sbatschul 
764*3042b8b5Sbatschul 	/* lex_lineno has already been incremented in the lexer; compensate */
765*3042b8b5Sbatschul 	if (cmd_file_mode && lex_lineno > last_lineno) {
766*3042b8b5Sbatschul 		if (strcmp(cmd_file_name, "-") == 0)
767*3042b8b5Sbatschul 			(void) fprintf(stderr, gettext("On line %d:\n"),
768*3042b8b5Sbatschul 			    lex_lineno - 1);
769*3042b8b5Sbatschul 		else
770*3042b8b5Sbatschul 			(void) fprintf(stderr, gettext("On line %d of %s:\n"),
771*3042b8b5Sbatschul 			    lex_lineno - 1, cmd_file_name);
772*3042b8b5Sbatschul 		last_lineno = lex_lineno;
773*3042b8b5Sbatschul 	}
774*3042b8b5Sbatschul 	va_start(alist, fmt);
775*3042b8b5Sbatschul 	(void) vfprintf(stderr, fmt, alist);
776*3042b8b5Sbatschul 	(void) fprintf(stderr, "\n");
777*3042b8b5Sbatschul 	va_end(alist);
778*3042b8b5Sbatschul }
779*3042b8b5Sbatschul 
7807c478bd9Sstevel@tonic-gate /*
7817c478bd9Sstevel@tonic-gate  * This is a separate function rather than a set of define's because of the
7827c478bd9Sstevel@tonic-gate  * gettext() wrapping.
7837c478bd9Sstevel@tonic-gate  */
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate /*
7867c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE
7877c478bd9Sstevel@tonic-gate  * Each string below should have \t follow \n whenever needed; the
7887c478bd9Sstevel@tonic-gate  * initial \t and the terminal \n will be provided by the calling function.
7897c478bd9Sstevel@tonic-gate  */
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate static char *
7927c478bd9Sstevel@tonic-gate long_help(int cmd_num)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	static char line[1024];	/* arbitrary large amount */
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
7977c478bd9Sstevel@tonic-gate 	switch (cmd_num) {
7987c478bd9Sstevel@tonic-gate 		case CMD_HELP:
7997c478bd9Sstevel@tonic-gate 			return (gettext("Prints help message."));
8007c478bd9Sstevel@tonic-gate 		case CMD_CREATE:
8017c478bd9Sstevel@tonic-gate 			(void) snprintf(line, sizeof (line),
8027c478bd9Sstevel@tonic-gate 			    gettext("Creates a configuration for the "
8037c478bd9Sstevel@tonic-gate 			    "specified zone.  %s should be\n\tused to "
8047c478bd9Sstevel@tonic-gate 			    "begin configuring a new zone.  If overwriting an "
8057c478bd9Sstevel@tonic-gate 			    "existing\n\tconfiguration, the -F flag can be "
8067c478bd9Sstevel@tonic-gate 			    "used to force the action.  If\n\t-t template is "
8077c478bd9Sstevel@tonic-gate 			    "given, creates a configuration identical to the\n"
8087c478bd9Sstevel@tonic-gate 			    "\tspecified template, except that the zone name "
8099e518655Sgjelinek 			    "is changed from\n\ttemplate to zonename.  '%s -a' "
8109e518655Sgjelinek 			    "creates a configuration from a\n\tdetached "
8119e518655Sgjelinek 			    "zonepath.  '%s -b' results in a blank "
8129e518655Sgjelinek 			    "configuration.\n\t'%s' with no arguments applies "
8139e518655Sgjelinek 			    "the Sun default settings."),
8147c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_CREATE), cmd_to_str(CMD_CREATE),
8159e518655Sgjelinek 			    cmd_to_str(CMD_CREATE), cmd_to_str(CMD_CREATE));
8167c478bd9Sstevel@tonic-gate 			return (line);
8177c478bd9Sstevel@tonic-gate 		case CMD_EXIT:
8187c478bd9Sstevel@tonic-gate 			return (gettext("Exits the program.  The -F flag can "
8197c478bd9Sstevel@tonic-gate 			    "be used to force the action."));
8207c478bd9Sstevel@tonic-gate 		case CMD_EXPORT:
8217c478bd9Sstevel@tonic-gate 			return (gettext("Prints configuration to standard "
8227c478bd9Sstevel@tonic-gate 			    "output, or to output-file if\n\tspecified, in "
8237c478bd9Sstevel@tonic-gate 			    "a form suitable for use in a command-file."));
8247c478bd9Sstevel@tonic-gate 		case CMD_ADD:
8257c478bd9Sstevel@tonic-gate 			return (gettext("Add specified resource to "
8267c478bd9Sstevel@tonic-gate 			    "configuration."));
8277c478bd9Sstevel@tonic-gate 		case CMD_DELETE:
8287c478bd9Sstevel@tonic-gate 			return (gettext("Deletes the specified zone.  The -F "
8297c478bd9Sstevel@tonic-gate 			    "flag can be used to force the\n\taction."));
8307c478bd9Sstevel@tonic-gate 		case CMD_REMOVE:
8317c478bd9Sstevel@tonic-gate 			return (gettext("Remove specified resource from "
8320209230bSgjelinek 			    "configuration.  The -F flag can be used\n\tto "
8330209230bSgjelinek 			    "force the action."));
8347c478bd9Sstevel@tonic-gate 		case CMD_SELECT:
8357c478bd9Sstevel@tonic-gate 			(void) snprintf(line, sizeof (line),
8367c478bd9Sstevel@tonic-gate 			    gettext("Selects a resource to modify.  "
8377c478bd9Sstevel@tonic-gate 			    "Resource modification is completed\n\twith the "
8387c478bd9Sstevel@tonic-gate 			    "command \"%s\".  The property name/value pairs "
8397c478bd9Sstevel@tonic-gate 			    "must uniquely\n\tidentify a resource.  Note that "
8407c478bd9Sstevel@tonic-gate 			    "the curly braces ('{', '}') mean one\n\tor more "
8417c478bd9Sstevel@tonic-gate 			    "of whatever is between them."),
8427c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_END));
8437c478bd9Sstevel@tonic-gate 			return (line);
8447c478bd9Sstevel@tonic-gate 		case CMD_SET:
8457c478bd9Sstevel@tonic-gate 			return (gettext("Sets property values."));
8460209230bSgjelinek 		case CMD_CLEAR:
8470209230bSgjelinek 			return (gettext("Clears property values."));
8487c478bd9Sstevel@tonic-gate 		case CMD_INFO:
8497c478bd9Sstevel@tonic-gate 			return (gettext("Displays information about the "
8507c478bd9Sstevel@tonic-gate 			    "current configuration.  If resource\n\ttype is "
8517c478bd9Sstevel@tonic-gate 			    "specified, displays only information about "
8527c478bd9Sstevel@tonic-gate 			    "resources of\n\tthe relevant type.  If resource "
8537c478bd9Sstevel@tonic-gate 			    "id is specified, displays only\n\tinformation "
8547c478bd9Sstevel@tonic-gate 			    "about that resource."));
8557c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
8567c478bd9Sstevel@tonic-gate 			return (gettext("Verifies current configuration "
8577c478bd9Sstevel@tonic-gate 			    "for correctness (some resource types\n\thave "
8587c478bd9Sstevel@tonic-gate 			    "required properties)."));
8597c478bd9Sstevel@tonic-gate 		case CMD_COMMIT:
8607c478bd9Sstevel@tonic-gate 			(void) snprintf(line, sizeof (line),
8617c478bd9Sstevel@tonic-gate 			    gettext("Commits current configuration.  "
8627c478bd9Sstevel@tonic-gate 			    "Configuration must be committed to\n\tbe used by "
8637c478bd9Sstevel@tonic-gate 			    "%s.  Until the configuration is committed, "
8647c478bd9Sstevel@tonic-gate 			    "changes \n\tcan be removed with the %s "
8657c478bd9Sstevel@tonic-gate 			    "command.  This operation is\n\tattempted "
8667c478bd9Sstevel@tonic-gate 			    "automatically upon completion of a %s "
8677c478bd9Sstevel@tonic-gate 			    "session."), "zoneadm", cmd_to_str(CMD_REVERT),
8687c478bd9Sstevel@tonic-gate 			    "zonecfg");
8697c478bd9Sstevel@tonic-gate 			return (line);
8707c478bd9Sstevel@tonic-gate 		case CMD_REVERT:
8717c478bd9Sstevel@tonic-gate 			return (gettext("Reverts configuration back to the "
8727c478bd9Sstevel@tonic-gate 			    "last committed state.  The -F flag\n\tcan be "
8737c478bd9Sstevel@tonic-gate 			    "used to force the action."));
8747c478bd9Sstevel@tonic-gate 		case CMD_CANCEL:
8757c478bd9Sstevel@tonic-gate 			return (gettext("Cancels resource/property "
8767c478bd9Sstevel@tonic-gate 			    "specification."));
8777c478bd9Sstevel@tonic-gate 		case CMD_END:
8787c478bd9Sstevel@tonic-gate 			return (gettext("Ends resource/property "
8797c478bd9Sstevel@tonic-gate 			    "specification."));
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8827e362f58Scomay 	return (NULL);
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate /*
8867c478bd9Sstevel@tonic-gate  * Called with verbose TRUE when help is explicitly requested, FALSE for
8877c478bd9Sstevel@tonic-gate  * unexpected errors.
8887c478bd9Sstevel@tonic-gate  */
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate void
891bbec428eSgjelinek usage(boolean_t verbose, uint_t flags)
8927c478bd9Sstevel@tonic-gate {
893*3042b8b5Sbatschul 	FILE *fp = verbose ? stdout : stderr;
894*3042b8b5Sbatschul 	FILE *newfp;
895bbec428eSgjelinek 	boolean_t need_to_close = B_FALSE;
8967c478bd9Sstevel@tonic-gate 	char *pager;
8977c478bd9Sstevel@tonic-gate 	int i;
898*3042b8b5Sbatschul 	struct stat statbuf;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	/* don't page error output */
9017c478bd9Sstevel@tonic-gate 	if (verbose && interactive_mode) {
9027c478bd9Sstevel@tonic-gate 		if ((pager = getenv("PAGER")) == NULL)
9037c478bd9Sstevel@tonic-gate 			pager = PAGER;
904*3042b8b5Sbatschul 
905*3042b8b5Sbatschul 		if (stat(pager, &statbuf) == 0) {
906*3042b8b5Sbatschul 			if ((newfp = popen(pager, "w")) != NULL) {
907*3042b8b5Sbatschul 				need_to_close = B_TRUE;
908*3042b8b5Sbatschul 				fp = newfp;
909*3042b8b5Sbatschul 			}
910*3042b8b5Sbatschul 		} else {
911*3042b8b5Sbatschul 			zerr(gettext("PAGER %s does not exist (%s)."),
912*3042b8b5Sbatschul 			    pager, strerror(errno));
9137c478bd9Sstevel@tonic-gate 		}
9147c478bd9Sstevel@tonic-gate 	}
915*3042b8b5Sbatschul 
9167c478bd9Sstevel@tonic-gate 	if (flags & HELP_META) {
9177c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("More help is available for the "
9187c478bd9Sstevel@tonic-gate 		    "following:\n"));
9197c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\n\tcommands ('%s commands')\n",
9207c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_HELP));
9217c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\tsyntax ('%s syntax')\n",
9227c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_HELP));
9237c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\tusage ('%s usage')\n\n",
9247c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_HELP));
9257c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("You may also obtain help on any "
9267c478bd9Sstevel@tonic-gate 		    "command by typing '%s <command-name>.'\n"),
9277c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_HELP));
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 	if (flags & HELP_RES_SCOPE) {
9307c478bd9Sstevel@tonic-gate 		switch (resource_scope) {
9317c478bd9Sstevel@tonic-gate 		case RT_FS:
9327c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
9337c478bd9Sstevel@tonic-gate 			    "used to configure a file-system.\n"),
9347c478bd9Sstevel@tonic-gate 			    rt_to_str(resource_scope));
9357c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
9367c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9377c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_DIR), gettext("<path>"));
9387c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9397c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_SPECIAL), gettext("<path>"));
9407c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9417c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_RAW), gettext("<raw-device>"));
9427c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9437c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_TYPE), gettext("<file-system type>"));
9447c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s %s\n", cmd_to_str(CMD_ADD),
9457c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_OPTIONS),
9467c478bd9Sstevel@tonic-gate 			    gettext("<file-system options>"));
947ffbafc53Scomay 			(void) fprintf(fp, "\t%s %s %s\n",
948ffbafc53Scomay 			    cmd_to_str(CMD_REMOVE), pt_to_str(PT_OPTIONS),
949ffbafc53Scomay 			    gettext("<file-system options>"));
9507c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Consult the file-system "
9517c478bd9Sstevel@tonic-gate 			    "specific manual page, such as mount_ufs(1M), "
9527c478bd9Sstevel@tonic-gate 			    "for\ndetails about file-system options.  Note "
9537c478bd9Sstevel@tonic-gate 			    "that any file-system options with an\nembedded "
9547c478bd9Sstevel@tonic-gate 			    "'=' character must be enclosed in double quotes, "
9557c478bd9Sstevel@tonic-gate 			    /*CSTYLED*/
9567c478bd9Sstevel@tonic-gate 			    "such as \"%s=5\".\n"), MNTOPT_RETRY);
9577c478bd9Sstevel@tonic-gate 			break;
9587c478bd9Sstevel@tonic-gate 		case RT_IPD:
9597c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
9607c478bd9Sstevel@tonic-gate 			    "used to configure a directory\ninherited from the "
9617c478bd9Sstevel@tonic-gate 			    "global zone into a non-global zone in read-only "
9627c478bd9Sstevel@tonic-gate 			    "mode.\n"), rt_to_str(resource_scope));
9637c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
9647c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9657c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_DIR), gettext("<path>"));
9667c478bd9Sstevel@tonic-gate 			break;
9677c478bd9Sstevel@tonic-gate 		case RT_NET:
9687c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
9697c478bd9Sstevel@tonic-gate 			    "used to configure a network interface.\n"),
9707c478bd9Sstevel@tonic-gate 			    rt_to_str(resource_scope));
9717c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
9727c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9737c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_ADDRESS), gettext("<IP-address>"));
9747c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9757c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_PHYSICAL), gettext("<interface>"));
9767c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("See ifconfig(1M) for "
9777c478bd9Sstevel@tonic-gate 			    "details of the <interface> string.\n"));
978de860bd9Sgfaden 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
979de860bd9Sgfaden 			    pt_to_str(PT_DEFROUTER), gettext("<IP-address>"));
980de860bd9Sgfaden 			(void) fprintf(fp, gettext("%s %s and %s %s are valid "
981de860bd9Sgfaden 			    "if the %s property is set to %s, otherwise they "
982de860bd9Sgfaden 			    "must not be set.\n"),
983f4b3ec61Sdh 			    cmd_to_str(CMD_SET), pt_to_str(PT_ADDRESS),
984de860bd9Sgfaden 			    cmd_to_str(CMD_SET), pt_to_str(PT_DEFROUTER),
985f4b3ec61Sdh 			    pt_to_str(PT_IPTYPE), "shared");
9867c478bd9Sstevel@tonic-gate 			break;
9877c478bd9Sstevel@tonic-gate 		case RT_DEVICE:
9887c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
9897c478bd9Sstevel@tonic-gate 			    "used to configure a device node.\n"),
9907c478bd9Sstevel@tonic-gate 			    rt_to_str(resource_scope));
9917c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
9927c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9937c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_MATCH), gettext("<device-path>"));
9947c478bd9Sstevel@tonic-gate 			break;
9957c478bd9Sstevel@tonic-gate 		case RT_RCTL:
9967c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
9977c478bd9Sstevel@tonic-gate 			    "used to configure a resource control.\n"),
9987c478bd9Sstevel@tonic-gate 			    rt_to_str(resource_scope));
9997c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
10007c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10017c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_NAME), gettext("<string>"));
10027c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s (%s=%s,%s=%s,%s=%s)\n",
10037c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_ADD), pt_to_str(PT_VALUE),
10047c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_PRIV), gettext("<priv-value>"),
10057c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_LIMIT), gettext("<number>"),
10067c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_ACTION), gettext("<action-value>"));
1007ffbafc53Scomay 			(void) fprintf(fp, "\t%s %s (%s=%s,%s=%s,%s=%s)\n",
1008ffbafc53Scomay 			    cmd_to_str(CMD_REMOVE), pt_to_str(PT_VALUE),
1009ffbafc53Scomay 			    pt_to_str(PT_PRIV), gettext("<priv-value>"),
1010ffbafc53Scomay 			    pt_to_str(PT_LIMIT), gettext("<number>"),
1011ffbafc53Scomay 			    pt_to_str(PT_ACTION), gettext("<action-value>"));
10127c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%s\n\t%s := privileged\n"
10137c478bd9Sstevel@tonic-gate 			    "\t%s := none | deny\n", gettext("Where"),
10147c478bd9Sstevel@tonic-gate 			    gettext("<priv-value>"), gettext("<action-value>"));
10157c478bd9Sstevel@tonic-gate 			break;
10167c478bd9Sstevel@tonic-gate 		case RT_ATTR:
10177c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
10187c478bd9Sstevel@tonic-gate 			    "used to configure a generic attribute.\n"),
10197c478bd9Sstevel@tonic-gate 			    rt_to_str(resource_scope));
10207c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
10217c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10227c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_NAME), gettext("<name>"));
10237c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=boolean\n",
10247c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_SET), pt_to_str(PT_TYPE));
10257c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=true | false\n",
10267c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_SET), pt_to_str(PT_VALUE));
10277c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("or\n"));
10287c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=int\n", cmd_to_str(CMD_SET),
10297c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_TYPE));
10307c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10317c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_VALUE), gettext("<integer>"));
10327c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("or\n"));
10337c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=string\n",
10347c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_SET), pt_to_str(PT_TYPE));
10357c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10367c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_VALUE), gettext("<string>"));
10377c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("or\n"));
10387c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=uint\n",
10397c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_SET), pt_to_str(PT_TYPE));
10407c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10417c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_VALUE), gettext("<unsigned integer>"));
10427c478bd9Sstevel@tonic-gate 			break;
1043fa9e4066Sahrens 		case RT_DATASET:
1044fa9e4066Sahrens 			(void) fprintf(fp, gettext("The '%s' resource scope is "
1045fa9e4066Sahrens 			    "used to export ZFS datasets.\n"),
1046fa9e4066Sahrens 			    rt_to_str(resource_scope));
1047fa9e4066Sahrens 			(void) fprintf(fp, gettext("Valid commands:\n"));
1048fa9e4066Sahrens 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
1049fa9e4066Sahrens 			    pt_to_str(PT_NAME), gettext("<name>"));
1050fa9e4066Sahrens 			break;
10510209230bSgjelinek 		case RT_DCPU:
10520209230bSgjelinek 			(void) fprintf(fp, gettext("The '%s' resource scope "
10530209230bSgjelinek 			    "configures the 'pools' facility to dedicate\na "
10540209230bSgjelinek 			    "subset of the system's processors to this zone "
10550209230bSgjelinek 			    "while it is running.\n"),
10560209230bSgjelinek 			    rt_to_str(resource_scope));
10570209230bSgjelinek 			(void) fprintf(fp, gettext("Valid commands:\n"));
10580209230bSgjelinek 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10590209230bSgjelinek 			    pt_to_str(PT_NCPUS),
10600209230bSgjelinek 			    gettext("<unsigned integer | range>"));
10610209230bSgjelinek 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10620209230bSgjelinek 			    pt_to_str(PT_IMPORTANCE),
10630209230bSgjelinek 			    gettext("<unsigned integer>"));
10640209230bSgjelinek 			break;
1065c97ad5cdSakolb 		case RT_PCAP:
1066c97ad5cdSakolb 			(void) fprintf(fp, gettext("The '%s' resource scope is "
1067c97ad5cdSakolb 			    "used to set an upper limit (a cap) on the\n"
1068c97ad5cdSakolb 			    "percentage of CPU that can be used by this zone.  "
1069c97ad5cdSakolb 			    "A '%s' value of 1\ncorresponds to one cpu.  The "
1070c97ad5cdSakolb 			    "value can be set higher than 1, up to the total\n"
1071c97ad5cdSakolb 			    "number of CPUs on the system.  The value can "
1072c97ad5cdSakolb 			    "also be less than 1,\nrepresenting a fraction of "
1073c97ad5cdSakolb 			    "a cpu.\n"),
1074c97ad5cdSakolb 			    rt_to_str(resource_scope), pt_to_str(PT_NCPUS));
1075c97ad5cdSakolb 			(void) fprintf(fp, gettext("Valid commands:\n"));
1076c97ad5cdSakolb 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
1077c97ad5cdSakolb 			    pt_to_str(PT_NCPUS), gettext("<unsigned decimal>"));
1078c97ad5cdSakolb 			break;
10790209230bSgjelinek 		case RT_MCAP:
10800209230bSgjelinek 			(void) fprintf(fp, gettext("The '%s' resource scope is "
10810209230bSgjelinek 			    "used to set an upper limit (a cap) on the\n"
10820209230bSgjelinek 			    "amount of physical memory, swap space and locked "
10830209230bSgjelinek 			    "memory that can be used by\nthis zone.\n"),
10840209230bSgjelinek 			    rt_to_str(resource_scope));
10850209230bSgjelinek 			(void) fprintf(fp, gettext("Valid commands:\n"));
10860209230bSgjelinek 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10870209230bSgjelinek 			    pt_to_str(PT_PHYSICAL),
10880209230bSgjelinek 			    gettext("<qualified unsigned decimal>"));
10890209230bSgjelinek 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10900209230bSgjelinek 			    pt_to_str(PT_SWAP),
10910209230bSgjelinek 			    gettext("<qualified unsigned decimal>"));
10920209230bSgjelinek 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10930209230bSgjelinek 			    pt_to_str(PT_LOCKED),
10940209230bSgjelinek 			    gettext("<qualified unsigned decimal>"));
10950209230bSgjelinek 			break;
10967c478bd9Sstevel@tonic-gate 		}
10977c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("And from any resource scope, you "
10987c478bd9Sstevel@tonic-gate 		    "can:\n"));
10997c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", cmd_to_str(CMD_END),
11007c478bd9Sstevel@tonic-gate 		    gettext("(to conclude this operation)"));
11017c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", cmd_to_str(CMD_CANCEL),
11027c478bd9Sstevel@tonic-gate 		    gettext("(to cancel this operation)"));
11037c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", cmd_to_str(CMD_EXIT),
11047c478bd9Sstevel@tonic-gate 		    gettext("(to exit the zonecfg utility)"));
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 	if (flags & HELP_USAGE) {
11077c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s:\t%s %s\n", gettext("usage"),
11087c478bd9Sstevel@tonic-gate 		    execname, cmd_to_str(CMD_HELP));
11097c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s -z <zone>\t\t\t(%s)\n",
11107c478bd9Sstevel@tonic-gate 		    execname, gettext("interactive"));
11117c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s -z <zone> <command>\n", execname);
11127c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s -z <zone> -f <command-file>\n",
11137c478bd9Sstevel@tonic-gate 		    execname);
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 	if (flags & HELP_SUBCMDS) {
11167c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s:\n\n", gettext("Commands"));
11177c478bd9Sstevel@tonic-gate 		for (i = 0; i <= CMD_MAX; i++) {
11187c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%s\n", helptab[i].short_usage);
11197c478bd9Sstevel@tonic-gate 			if (verbose)
11207c478bd9Sstevel@tonic-gate 				(void) fprintf(fp, "\t%s\n\n", long_help(i));
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 	if (flags & HELP_SYNTAX) {
11247c478bd9Sstevel@tonic-gate 		if (!verbose)
11257c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\n");
11267c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "<zone> := [A-Za-z0-9][A-Za-z0-9_.-]*\n");
11277c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("\t(except the reserved words "
11287c478bd9Sstevel@tonic-gate 		    "'%s' and anything starting with '%s')\n"), "global",
11297c478bd9Sstevel@tonic-gate 		    "SUNW");
11307c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11317c478bd9Sstevel@tonic-gate 		    gettext("\tName must be less than %d characters.\n"),
11327c478bd9Sstevel@tonic-gate 		    ZONENAME_MAX);
11337c478bd9Sstevel@tonic-gate 		if (verbose)
11347c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\n");
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 	if (flags & HELP_NETADDR) {
11377c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("\n<net-addr> :="));
11387c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11397c478bd9Sstevel@tonic-gate 		    gettext("\t<IPv4-address>[/<IPv4-prefix-length>] |\n"));
11407c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11417c478bd9Sstevel@tonic-gate 		    gettext("\t\t<IPv6-address>/<IPv6-prefix-length> |\n"));
11427c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11437c478bd9Sstevel@tonic-gate 		    gettext("\t\t<hostname>[/<IPv4-prefix-length>]\n"));
11447c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("See inet(3SOCKET) for IPv4 and "
11457c478bd9Sstevel@tonic-gate 		    "IPv6 address syntax.\n"));
11467c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("<IPv4-prefix-length> := [0-32]\n"));
11477c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11487c478bd9Sstevel@tonic-gate 		    gettext("<IPv6-prefix-length> := [0-128]\n"));
11497c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11507c478bd9Sstevel@tonic-gate 		    gettext("<hostname> := [A-Za-z0-9][A-Za-z0-9-.]*\n"));
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 	if (flags & HELP_RESOURCES) {
11539e7542f4Sdp 		(void) fprintf(fp, "<%s> := %s | %s | %s | %s | %s | %s |\n\t"
1154c97ad5cdSakolb 		    "%s | %s | %s | %s\n\n",
11557c478bd9Sstevel@tonic-gate 		    gettext("resource type"), rt_to_str(RT_FS),
11567c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_IPD), rt_to_str(RT_NET), rt_to_str(RT_DEVICE),
11579e7542f4Sdp 		    rt_to_str(RT_RCTL), rt_to_str(RT_ATTR),
11580209230bSgjelinek 		    rt_to_str(RT_DATASET), rt_to_str(RT_DCPU),
1159c97ad5cdSakolb 		    rt_to_str(RT_PCAP), rt_to_str(RT_MCAP));
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 	if (flags & HELP_PROPS) {
11627c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("For resource type ... there are "
11637c478bd9Sstevel@tonic-gate 		    "property types ...:\n"));
1164087719fdSdp 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
1165087719fdSdp 		    pt_to_str(PT_ZONENAME));
11667c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11677c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_ZONEPATH));
11689acbbeafSnn 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11699acbbeafSnn 		    pt_to_str(PT_BRAND));
11707c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11717c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_AUTOBOOT));
11723f2f09c1Sdp 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11733f2f09c1Sdp 		    pt_to_str(PT_BOOTARGS));
11747c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11757c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_POOL));
1176ffbafc53Scomay 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
1177ffbafc53Scomay 		    pt_to_str(PT_LIMITPRIV));
11780209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11790209230bSgjelinek 		    pt_to_str(PT_SCHED));
1180f4b3ec61Sdh 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
1181f4b3ec61Sdh 		    pt_to_str(PT_IPTYPE));
11825679c89fSjv 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11835679c89fSjv 		    pt_to_str(PT_HOSTID));
11840209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11850209230bSgjelinek 		    pt_to_str(PT_MAXLWPS));
11860209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11870209230bSgjelinek 		    pt_to_str(PT_MAXSHMMEM));
11880209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11890209230bSgjelinek 		    pt_to_str(PT_MAXSHMIDS));
11900209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11910209230bSgjelinek 		    pt_to_str(PT_MAXMSGIDS));
11920209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11930209230bSgjelinek 		    pt_to_str(PT_MAXSEMIDS));
11940209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11950209230bSgjelinek 		    pt_to_str(PT_SHARES));
11967c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t\t%s, %s, %s, %s\n", rt_to_str(RT_FS),
11977c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_DIR), pt_to_str(PT_SPECIAL),
11987c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_RAW), pt_to_str(PT_TYPE),
11997c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_OPTIONS));
12007c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", rt_to_str(RT_IPD),
12017c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_DIR));
1202de860bd9Sgfaden 		(void) fprintf(fp, "\t%s\t\t%s, %s, %s\n", rt_to_str(RT_NET),
1203de860bd9Sgfaden 		    pt_to_str(PT_ADDRESS), pt_to_str(PT_PHYSICAL),
1204de860bd9Sgfaden 		    pt_to_str(PT_DEFROUTER));
12057c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t\t%s\n", rt_to_str(RT_DEVICE),
12067c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_MATCH));
12077c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t\t%s, %s\n", rt_to_str(RT_RCTL),
12087c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_NAME), pt_to_str(PT_VALUE));
12097c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t\t%s, %s, %s\n", rt_to_str(RT_ATTR),
12107c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_NAME), pt_to_str(PT_TYPE),
12117c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_VALUE));
1212fa9e4066Sahrens 		(void) fprintf(fp, "\t%s\t\t%s\n", rt_to_str(RT_DATASET),
1213fa9e4066Sahrens 		    pt_to_str(PT_NAME));
12140209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s, %s\n", rt_to_str(RT_DCPU),
12150209230bSgjelinek 		    pt_to_str(PT_NCPUS), pt_to_str(PT_IMPORTANCE));
1216c97ad5cdSakolb 		(void) fprintf(fp, "\t%s\t%s\n", rt_to_str(RT_PCAP),
1217c97ad5cdSakolb 		    pt_to_str(PT_NCPUS));
12180209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s, %s, %s\n", rt_to_str(RT_MCAP),
12190209230bSgjelinek 		    pt_to_str(PT_PHYSICAL), pt_to_str(PT_SWAP),
12200209230bSgjelinek 		    pt_to_str(PT_LOCKED));
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 	if (need_to_close)
12237c478bd9Sstevel@tonic-gate 		(void) pclose(fp);
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate static void
1227bbec428eSgjelinek zone_perror(char *prefix, int err, boolean_t set_saw)
12287c478bd9Sstevel@tonic-gate {
12297c478bd9Sstevel@tonic-gate 	zerr("%s: %s", prefix, zonecfg_strerror(err));
12307c478bd9Sstevel@tonic-gate 	if (set_saw)
1231bbec428eSgjelinek 		saw_error = B_TRUE;
12327c478bd9Sstevel@tonic-gate }
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate /*
12357c478bd9Sstevel@tonic-gate  * zone_perror() expects a single string, but for remove and select
12367c478bd9Sstevel@tonic-gate  * we have both the command and the resource type, so this wrapper
12377c478bd9Sstevel@tonic-gate  * function serves the same purpose in a slightly different way.
12387c478bd9Sstevel@tonic-gate  */
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate static void
1241bbec428eSgjelinek z_cmd_rt_perror(int cmd_num, int res_num, int err, boolean_t set_saw)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate 	zerr("%s %s: %s", cmd_to_str(cmd_num), rt_to_str(res_num),
12447c478bd9Sstevel@tonic-gate 	    zonecfg_strerror(err));
12457c478bd9Sstevel@tonic-gate 	if (set_saw)
1246bbec428eSgjelinek 		saw_error = B_TRUE;
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate /* returns Z_OK if successful, Z_foo from <libzonecfg.h> otherwise */
12507c478bd9Sstevel@tonic-gate static int
1251bbec428eSgjelinek initialize(boolean_t handle_expected)
12527c478bd9Sstevel@tonic-gate {
12537c478bd9Sstevel@tonic-gate 	int err;
12549acbbeafSnn 	char brandname[MAXNAMELEN];
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) != Z_OK) {
12577c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_get_handle(zone, handle)) == Z_OK) {
1258bbec428eSgjelinek 			got_handle = B_TRUE;
12599acbbeafSnn 			if (zonecfg_get_brand(handle, brandname,
12609acbbeafSnn 			    sizeof (brandname)) != Z_OK) {
12619acbbeafSnn 				zerr("Zone %s is inconsistent: missing "
12629acbbeafSnn 				    "brand attribute", zone);
12639acbbeafSnn 				exit(Z_ERR);
12649acbbeafSnn 			}
12659acbbeafSnn 			if ((brand = brand_open(brandname)) == NULL) {
12669acbbeafSnn 				zerr("Zone %s uses non-existent brand \"%s\"."
12679acbbeafSnn 				    "  Unable to continue", zone, brandname);
12689acbbeafSnn 				exit(Z_ERR);
12699acbbeafSnn 			}
12700209230bSgjelinek 		} else if (global_zone && err == Z_NO_ZONE && !got_handle &&
12710209230bSgjelinek 		    !read_only_mode) {
12720209230bSgjelinek 			/*
12730209230bSgjelinek 			 * We implicitly create the global zone config if it
12740209230bSgjelinek 			 * doesn't exist.
12750209230bSgjelinek 			 */
12760209230bSgjelinek 			zone_dochandle_t tmphandle;
12770209230bSgjelinek 
12780209230bSgjelinek 			if ((tmphandle = zonecfg_init_handle()) == NULL) {
1279bbec428eSgjelinek 				zone_perror(execname, Z_NOMEM, B_TRUE);
12800209230bSgjelinek 				exit(Z_ERR);
12810209230bSgjelinek 			}
12820209230bSgjelinek 
12830209230bSgjelinek 			err = zonecfg_get_template_handle("SUNWblank", zone,
12840209230bSgjelinek 			    tmphandle);
12850209230bSgjelinek 
12860209230bSgjelinek 			if (err != Z_OK) {
12870209230bSgjelinek 				zonecfg_fini_handle(tmphandle);
1288bbec428eSgjelinek 				zone_perror("SUNWblank", err, B_TRUE);
12890209230bSgjelinek 				return (err);
12900209230bSgjelinek 			}
12910209230bSgjelinek 
1292bbec428eSgjelinek 			need_to_commit = B_TRUE;
12930209230bSgjelinek 			zonecfg_fini_handle(handle);
12940209230bSgjelinek 			handle = tmphandle;
1295bbec428eSgjelinek 			got_handle = B_TRUE;
12960209230bSgjelinek 
12977c478bd9Sstevel@tonic-gate 		} else {
12987c478bd9Sstevel@tonic-gate 			zone_perror(zone, err, handle_expected || got_handle);
12997c478bd9Sstevel@tonic-gate 			if (err == Z_NO_ZONE && !got_handle &&
13007c478bd9Sstevel@tonic-gate 			    interactive_mode && !read_only_mode)
13017c478bd9Sstevel@tonic-gate 				(void) printf(gettext("Use '%s' to begin "
13027c478bd9Sstevel@tonic-gate 				    "configuring a new zone.\n"),
13037c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_CREATE));
13047c478bd9Sstevel@tonic-gate 			return (err);
13057c478bd9Sstevel@tonic-gate 		}
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 	return (Z_OK);
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate 
1310bbec428eSgjelinek static boolean_t
1311087719fdSdp state_atleast(zone_state_t state)
1312087719fdSdp {
1313087719fdSdp 	zone_state_t state_num;
1314087719fdSdp 	int err;
1315087719fdSdp 
1316087719fdSdp 	if ((err = zone_get_state(zone, &state_num)) != Z_OK) {
1317087719fdSdp 		/* all states are greater than "non-existent" */
1318087719fdSdp 		if (err == Z_NO_ZONE)
1319087719fdSdp 			return (B_FALSE);
1320087719fdSdp 		zerr(gettext("Unexpectedly failed to determine state "
1321087719fdSdp 		    "of zone %s: %s"), zone, zonecfg_strerror(err));
1322087719fdSdp 		exit(Z_ERR);
1323087719fdSdp 	}
1324087719fdSdp 	return (state_num >= state);
1325087719fdSdp }
1326087719fdSdp 
13277c478bd9Sstevel@tonic-gate /*
13287c478bd9Sstevel@tonic-gate  * short_usage() is for bad syntax: getopt() issues, too many arguments, etc.
13297c478bd9Sstevel@tonic-gate  */
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate void
13327c478bd9Sstevel@tonic-gate short_usage(int command)
13337c478bd9Sstevel@tonic-gate {
13347c478bd9Sstevel@tonic-gate 	/* lex_lineno has already been incremented in the lexer; compensate */
13357c478bd9Sstevel@tonic-gate 	if (cmd_file_mode) {
13367c478bd9Sstevel@tonic-gate 		if (strcmp(cmd_file_name, "-") == 0)
13377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13387c478bd9Sstevel@tonic-gate 			    gettext("syntax error on line %d\n"),
13397c478bd9Sstevel@tonic-gate 			    lex_lineno - 1);
13407c478bd9Sstevel@tonic-gate 		else
13417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13427c478bd9Sstevel@tonic-gate 			    gettext("syntax error on line %d of %s\n"),
13437c478bd9Sstevel@tonic-gate 			    lex_lineno - 1, cmd_file_name);
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\n%s\n", gettext("usage"),
13467c478bd9Sstevel@tonic-gate 	    helptab[command].short_usage);
1347bbec428eSgjelinek 	saw_error = B_TRUE;
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate /*
13517c478bd9Sstevel@tonic-gate  * long_usage() is for bad semantics: e.g., wrong property type for a given
13527c478bd9Sstevel@tonic-gate  * resource type.  It is also used by longer_usage() below.
13537c478bd9Sstevel@tonic-gate  */
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate void
1356bbec428eSgjelinek long_usage(uint_t cmd_num, boolean_t set_saw)
13577c478bd9Sstevel@tonic-gate {
13587c478bd9Sstevel@tonic-gate 	(void) fprintf(set_saw ? stderr : stdout, "%s:\n%s\n", gettext("usage"),
13597c478bd9Sstevel@tonic-gate 	    helptab[cmd_num].short_usage);
13607c478bd9Sstevel@tonic-gate 	(void) fprintf(set_saw ? stderr : stdout, "\t%s\n", long_help(cmd_num));
13617c478bd9Sstevel@tonic-gate 	if (set_saw)
1362bbec428eSgjelinek 		saw_error = B_TRUE;
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate /*
13667c478bd9Sstevel@tonic-gate  * longer_usage() is for 'help foo' and 'foo -?': call long_usage() and also
13677c478bd9Sstevel@tonic-gate  * any extra usage() flags as appropriate for whatever command.
13687c478bd9Sstevel@tonic-gate  */
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate void
13717c478bd9Sstevel@tonic-gate longer_usage(uint_t cmd_num)
13727c478bd9Sstevel@tonic-gate {
1373bbec428eSgjelinek 	long_usage(cmd_num, B_FALSE);
13747c478bd9Sstevel@tonic-gate 	if (helptab[cmd_num].flags != 0) {
13757c478bd9Sstevel@tonic-gate 		(void) printf("\n");
1376bbec428eSgjelinek 		usage(B_TRUE, helptab[cmd_num].flags);
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate /*
13817c478bd9Sstevel@tonic-gate  * scope_usage() is simply used when a command is called from the wrong scope.
13827c478bd9Sstevel@tonic-gate  */
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate static void
13857c478bd9Sstevel@tonic-gate scope_usage(uint_t cmd_num)
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate 	zerr(gettext("The %s command only makes sense in the %s scope."),
13887c478bd9Sstevel@tonic-gate 	    cmd_to_str(cmd_num),
13897c478bd9Sstevel@tonic-gate 	    global_scope ?  gettext("resource") : gettext("global"));
1390bbec428eSgjelinek 	saw_error = B_TRUE;
13917c478bd9Sstevel@tonic-gate }
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate /*
1394bbec428eSgjelinek  * On input, B_TRUE => yes, B_FALSE => no.
1395bbec428eSgjelinek  * On return, B_TRUE => 1, B_FALSE => no, could not ask => -1.
13967c478bd9Sstevel@tonic-gate  */
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate static int
1399bbec428eSgjelinek ask_yesno(boolean_t default_answer, const char *question)
14007c478bd9Sstevel@tonic-gate {
14017c478bd9Sstevel@tonic-gate 	char line[64];	/* should be enough to answer yes or no */
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	if (!ok_to_prompt) {
1404bbec428eSgjelinek 		saw_error = B_TRUE;
14057c478bd9Sstevel@tonic-gate 		return (-1);
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 	for (;;) {
1408087719fdSdp 		if (printf("%s (%s)? ", question,
1409087719fdSdp 		    default_answer ? "[y]/n" : "y/[n]") < 0)
1410087719fdSdp 			return (-1);
1411087719fdSdp 		if (fgets(line, sizeof (line), stdin) == NULL)
1412087719fdSdp 			return (-1);
1413087719fdSdp 
1414087719fdSdp 		if (line[0] == '\n')
14157c478bd9Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
14167c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
14177c478bd9Sstevel@tonic-gate 			return (1);
14187c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
14197c478bd9Sstevel@tonic-gate 			return (0);
14207c478bd9Sstevel@tonic-gate 	}
14217c478bd9Sstevel@tonic-gate }
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate /*
14247c478bd9Sstevel@tonic-gate  * Prints warning if zone already exists.
14257c478bd9Sstevel@tonic-gate  * In interactive mode, prompts if we should continue anyway and returns Z_OK
14267c478bd9Sstevel@tonic-gate  * if so, Z_ERR if not.  In non-interactive mode, exits with Z_ERR.
14277c478bd9Sstevel@tonic-gate  *
14287c478bd9Sstevel@tonic-gate  * Note that if a zone exists and its state is >= INSTALLED, an error message
14297c478bd9Sstevel@tonic-gate  * will be printed and this function will return Z_ERR regardless of mode.
14307c478bd9Sstevel@tonic-gate  */
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate static int
1433bbec428eSgjelinek check_if_zone_already_exists(boolean_t force)
14347c478bd9Sstevel@tonic-gate {
14357c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* enough to ask a question */
14367c478bd9Sstevel@tonic-gate 	zone_dochandle_t tmphandle;
14377c478bd9Sstevel@tonic-gate 	int res, answer;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	if ((tmphandle = zonecfg_init_handle()) == NULL) {
1440bbec428eSgjelinek 		zone_perror(execname, Z_NOMEM, B_TRUE);
14417c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 	res = zonecfg_get_handle(zone, tmphandle);
14447c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(tmphandle);
1445087719fdSdp 	if (res != Z_OK)
14467c478bd9Sstevel@tonic-gate 		return (Z_OK);
1447087719fdSdp 
1448087719fdSdp 	if (state_atleast(ZONE_STATE_INSTALLED)) {
14497c478bd9Sstevel@tonic-gate 		zerr(gettext("Zone %s already installed; %s not allowed."),
14507c478bd9Sstevel@tonic-gate 		    zone, cmd_to_str(CMD_CREATE));
14517c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14527c478bd9Sstevel@tonic-gate 	}
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	if (force) {
14557c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Zone %s already exists; overwriting.\n"),
14567c478bd9Sstevel@tonic-gate 		    zone);
14577c478bd9Sstevel@tonic-gate 		return (Z_OK);
14587c478bd9Sstevel@tonic-gate 	}
14597c478bd9Sstevel@tonic-gate 	(void) snprintf(line, sizeof (line),
14607c478bd9Sstevel@tonic-gate 	    gettext("Zone %s already exists; %s anyway"), zone,
14617c478bd9Sstevel@tonic-gate 	    cmd_to_str(CMD_CREATE));
1462bbec428eSgjelinek 	if ((answer = ask_yesno(B_FALSE, line)) == -1) {
14637c478bd9Sstevel@tonic-gate 		zerr(gettext("Zone exists, input not from terminal and -F not "
14647c478bd9Sstevel@tonic-gate 		    "specified:\n%s command ignored, exiting."),
14657c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_CREATE));
14667c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate 	return (answer == 1 ? Z_OK : Z_ERR);
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate 
1471bbec428eSgjelinek static boolean_t
14727c478bd9Sstevel@tonic-gate zone_is_read_only(int cmd_num)
14737c478bd9Sstevel@tonic-gate {
14747c478bd9Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
14757c478bd9Sstevel@tonic-gate 		zerr(gettext("%s: zones beginning with SUNW are read-only."),
14767c478bd9Sstevel@tonic-gate 		    zone);
1477bbec428eSgjelinek 		saw_error = B_TRUE;
1478bbec428eSgjelinek 		return (B_TRUE);
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate 	if (read_only_mode) {
14817c478bd9Sstevel@tonic-gate 		zerr(gettext("%s: cannot %s in read-only mode."), zone,
14827c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
1483bbec428eSgjelinek 		saw_error = B_TRUE;
1484bbec428eSgjelinek 		return (B_TRUE);
14857c478bd9Sstevel@tonic-gate 	}
1486bbec428eSgjelinek 	return (B_FALSE);
14877c478bd9Sstevel@tonic-gate }
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate /*
14907c478bd9Sstevel@tonic-gate  * Create a new configuration.
14917c478bd9Sstevel@tonic-gate  */
14927c478bd9Sstevel@tonic-gate void
14937c478bd9Sstevel@tonic-gate create_func(cmd_t *cmd)
14947c478bd9Sstevel@tonic-gate {
14957c478bd9Sstevel@tonic-gate 	int err, arg;
14967c478bd9Sstevel@tonic-gate 	char zone_template[ZONENAME_MAX];
1497ee519a1fSgjelinek 	char attach_path[MAXPATHLEN];
14987c478bd9Sstevel@tonic-gate 	zone_dochandle_t tmphandle;
1499bbec428eSgjelinek 	boolean_t force = B_FALSE;
1500bbec428eSgjelinek 	boolean_t attach = B_FALSE;
1501bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	/* This is the default if no arguments are given. */
15067c478bd9Sstevel@tonic-gate 	(void) strlcpy(zone_template, "SUNWdefault", sizeof (zone_template));
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	optind = 0;
15099acbbeafSnn 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?a:bFt:"))
15109acbbeafSnn 	    != EOF) {
15117c478bd9Sstevel@tonic-gate 		switch (arg) {
15127c478bd9Sstevel@tonic-gate 		case '?':
15137c478bd9Sstevel@tonic-gate 			if (optopt == '?')
15147c478bd9Sstevel@tonic-gate 				longer_usage(CMD_CREATE);
15157c478bd9Sstevel@tonic-gate 			else
15167c478bd9Sstevel@tonic-gate 				short_usage(CMD_CREATE);
1517bbec428eSgjelinek 			arg_err = B_TRUE;
15187ec75eb8Sgjelinek 			break;
1519ee519a1fSgjelinek 		case 'a':
1520ee519a1fSgjelinek 			(void) strlcpy(attach_path, optarg,
1521ee519a1fSgjelinek 			    sizeof (attach_path));
1522bbec428eSgjelinek 			attach = B_TRUE;
1523ee519a1fSgjelinek 			break;
15247c478bd9Sstevel@tonic-gate 		case 'b':
15257c478bd9Sstevel@tonic-gate 			(void) strlcpy(zone_template, "SUNWblank",
15267c478bd9Sstevel@tonic-gate 			    sizeof (zone_template));
15277c478bd9Sstevel@tonic-gate 			break;
15287c478bd9Sstevel@tonic-gate 		case 'F':
1529bbec428eSgjelinek 			force = B_TRUE;
15307c478bd9Sstevel@tonic-gate 			break;
15317c478bd9Sstevel@tonic-gate 		case 't':
15327c478bd9Sstevel@tonic-gate 			(void) strlcpy(zone_template, optarg,
15337c478bd9Sstevel@tonic-gate 			    sizeof (zone_template));
15347c478bd9Sstevel@tonic-gate 			break;
15357c478bd9Sstevel@tonic-gate 		default:
15367c478bd9Sstevel@tonic-gate 			short_usage(CMD_CREATE);
1537bbec428eSgjelinek 			arg_err = B_TRUE;
15387ec75eb8Sgjelinek 			break;
15397c478bd9Sstevel@tonic-gate 		}
15407c478bd9Sstevel@tonic-gate 	}
15417ec75eb8Sgjelinek 	if (arg_err)
15427ec75eb8Sgjelinek 		return;
15437ec75eb8Sgjelinek 
15447c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
15457c478bd9Sstevel@tonic-gate 		short_usage(CMD_CREATE);
15467c478bd9Sstevel@tonic-gate 		return;
15477c478bd9Sstevel@tonic-gate 	}
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_CREATE))
15507c478bd9Sstevel@tonic-gate 		return;
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	if (check_if_zone_already_exists(force) != Z_OK)
15537c478bd9Sstevel@tonic-gate 		return;
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	/*
15567c478bd9Sstevel@tonic-gate 	 * Get a temporary handle first.  If that fails, the old handle
15577c478bd9Sstevel@tonic-gate 	 * will not be lost.  Then finish whichever one we don't need,
15587c478bd9Sstevel@tonic-gate 	 * to avoid leaks.  Then get the handle for zone_template, and
15597c478bd9Sstevel@tonic-gate 	 * set the name to zone: this "copy, rename" method is how
15607c478bd9Sstevel@tonic-gate 	 * create -[b|t] works.
15617c478bd9Sstevel@tonic-gate 	 */
15627c478bd9Sstevel@tonic-gate 	if ((tmphandle = zonecfg_init_handle()) == NULL) {
1563bbec428eSgjelinek 		zone_perror(execname, Z_NOMEM, B_TRUE);
15647c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
15657c478bd9Sstevel@tonic-gate 	}
1566ee519a1fSgjelinek 
1567ee519a1fSgjelinek 	if (attach)
156816ab8c7bSgjelinek 		err = zonecfg_get_attach_handle(attach_path, ZONE_DETACHED,
156916ab8c7bSgjelinek 		    zone, B_FALSE, tmphandle);
1570ee519a1fSgjelinek 	else
1571ee519a1fSgjelinek 		err = zonecfg_get_template_handle(zone_template, zone,
1572ee519a1fSgjelinek 		    tmphandle);
1573ee519a1fSgjelinek 
1574ee519a1fSgjelinek 	if (err != Z_OK) {
15757c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(tmphandle);
1576ee519a1fSgjelinek 		if (attach && err == Z_NO_ZONE)
1577ee519a1fSgjelinek 			(void) fprintf(stderr, gettext("invalid path to "
1578ee519a1fSgjelinek 			    "detached zone\n"));
1579ee519a1fSgjelinek 		else if (attach && err == Z_INVALID_DOCUMENT)
1580ee519a1fSgjelinek 			(void) fprintf(stderr, gettext("Cannot attach to an "
1581ee519a1fSgjelinek 			    "earlier release of the operating system\n"));
1582ee519a1fSgjelinek 		else
1583bbec428eSgjelinek 			zone_perror(zone_template, err, B_TRUE);
15847c478bd9Sstevel@tonic-gate 		return;
15857c478bd9Sstevel@tonic-gate 	}
1586087719fdSdp 
1587bbec428eSgjelinek 	need_to_commit = B_TRUE;
15887c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
15897c478bd9Sstevel@tonic-gate 	handle = tmphandle;
1590bbec428eSgjelinek 	got_handle = B_TRUE;
15917c478bd9Sstevel@tonic-gate }
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate /*
15947c478bd9Sstevel@tonic-gate  * This malloc()'s memory, which must be freed by the caller.
15957c478bd9Sstevel@tonic-gate  */
15967c478bd9Sstevel@tonic-gate static char *
15977c478bd9Sstevel@tonic-gate quoteit(char *instr)
15987c478bd9Sstevel@tonic-gate {
15997c478bd9Sstevel@tonic-gate 	char *outstr;
16007c478bd9Sstevel@tonic-gate 	size_t outstrsize = strlen(instr) + 3;	/* 2 quotes + '\0' */
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	if ((outstr = malloc(outstrsize)) == NULL) {
1603bbec428eSgjelinek 		zone_perror(zone, Z_NOMEM, B_FALSE);
16047c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate 	if (strchr(instr, ' ') == NULL) {
16077c478bd9Sstevel@tonic-gate 		(void) strlcpy(outstr, instr, outstrsize);
16087c478bd9Sstevel@tonic-gate 		return (outstr);
16097c478bd9Sstevel@tonic-gate 	}
16107c478bd9Sstevel@tonic-gate 	(void) snprintf(outstr, outstrsize, "\"%s\"", instr);
16117c478bd9Sstevel@tonic-gate 	return (outstr);
16127c478bd9Sstevel@tonic-gate }
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate static void
16157c478bd9Sstevel@tonic-gate export_prop(FILE *of, int prop_num, char *prop_id)
16167c478bd9Sstevel@tonic-gate {
16177c478bd9Sstevel@tonic-gate 	char *quote_str;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	if (strlen(prop_id) == 0)
16207c478bd9Sstevel@tonic-gate 		return;
16217c478bd9Sstevel@tonic-gate 	quote_str = quoteit(prop_id);
16227c478bd9Sstevel@tonic-gate 	(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
16237c478bd9Sstevel@tonic-gate 	    pt_to_str(prop_num), quote_str);
16247c478bd9Sstevel@tonic-gate 	free(quote_str);
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate void
16287c478bd9Sstevel@tonic-gate export_func(cmd_t *cmd)
16297c478bd9Sstevel@tonic-gate {
16307c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
16317c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
16327c478bd9Sstevel@tonic-gate 	struct zone_devtab devtab;
16337c478bd9Sstevel@tonic-gate 	struct zone_attrtab attrtab;
16347c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
1635fa9e4066Sahrens 	struct zone_dstab dstab;
16360209230bSgjelinek 	struct zone_psettab psettab;
16370209230bSgjelinek 	struct zone_mcaptab mcaptab;
16387c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
16397c478bd9Sstevel@tonic-gate 	int err, arg;
16407c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN], outfile[MAXPATHLEN], pool[MAXNAMELEN];
16413f2f09c1Sdp 	char bootargs[BOOTARGS_MAX];
16420209230bSgjelinek 	char sched[MAXNAMELEN];
16439acbbeafSnn 	char brand[MAXNAMELEN];
16445679c89fSjv 	char hostidp[HW_HOSTID_LEN];
1645ffbafc53Scomay 	char *limitpriv;
16467c478bd9Sstevel@tonic-gate 	FILE *of;
16477c478bd9Sstevel@tonic-gate 	boolean_t autoboot;
1648f4b3ec61Sdh 	zone_iptype_t iptype;
1649bbec428eSgjelinek 	boolean_t need_to_close = B_FALSE;
1650bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	outfile[0] = '\0';
16557c478bd9Sstevel@tonic-gate 	optind = 0;
16567c478bd9Sstevel@tonic-gate 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?f:")) != EOF) {
16577c478bd9Sstevel@tonic-gate 		switch (arg) {
16587c478bd9Sstevel@tonic-gate 		case '?':
16597c478bd9Sstevel@tonic-gate 			if (optopt == '?')
16607c478bd9Sstevel@tonic-gate 				longer_usage(CMD_EXPORT);
16617c478bd9Sstevel@tonic-gate 			else
16627c478bd9Sstevel@tonic-gate 				short_usage(CMD_EXPORT);
1663bbec428eSgjelinek 			arg_err = B_TRUE;
16647ec75eb8Sgjelinek 			break;
16657c478bd9Sstevel@tonic-gate 		case 'f':
16667c478bd9Sstevel@tonic-gate 			(void) strlcpy(outfile, optarg, sizeof (outfile));
16677c478bd9Sstevel@tonic-gate 			break;
16687c478bd9Sstevel@tonic-gate 		default:
16697c478bd9Sstevel@tonic-gate 			short_usage(CMD_EXPORT);
1670bbec428eSgjelinek 			arg_err = B_TRUE;
16717ec75eb8Sgjelinek 			break;
16727c478bd9Sstevel@tonic-gate 		}
16737c478bd9Sstevel@tonic-gate 	}
16747ec75eb8Sgjelinek 	if (arg_err)
16757ec75eb8Sgjelinek 		return;
16767ec75eb8Sgjelinek 
16777c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
16787c478bd9Sstevel@tonic-gate 		short_usage(CMD_EXPORT);
16797c478bd9Sstevel@tonic-gate 		return;
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate 	if (strlen(outfile) == 0) {
16827c478bd9Sstevel@tonic-gate 		of = stdout;
16837c478bd9Sstevel@tonic-gate 	} else {
16847c478bd9Sstevel@tonic-gate 		if ((of = fopen(outfile, "w")) == NULL) {
16857c478bd9Sstevel@tonic-gate 			zerr(gettext("opening file %s: %s"),
16867c478bd9Sstevel@tonic-gate 			    outfile, strerror(errno));
16877c478bd9Sstevel@tonic-gate 			goto done;
16887c478bd9Sstevel@tonic-gate 		}
16897c478bd9Sstevel@tonic-gate 		setbuf(of, NULL);
1690bbec428eSgjelinek 		need_to_close = B_TRUE;
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 
1693bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
16947c478bd9Sstevel@tonic-gate 		goto done;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	(void) fprintf(of, "%s -b\n", cmd_to_str(CMD_CREATE));
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	if (zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath)) == Z_OK &&
16997c478bd9Sstevel@tonic-gate 	    strlen(zonepath) > 0)
17007c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
17017c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_ZONEPATH), zonepath);
17027c478bd9Sstevel@tonic-gate 
17039acbbeafSnn 	if ((zone_get_brand(zone, brand, sizeof (brand)) == Z_OK) &&
17049acbbeafSnn 	    (strcmp(brand, NATIVE_BRAND_NAME) != 0))
17059acbbeafSnn 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
17069acbbeafSnn 		    pt_to_str(PT_BRAND), brand);
17079acbbeafSnn 
17087c478bd9Sstevel@tonic-gate 	if (zonecfg_get_autoboot(handle, &autoboot) == Z_OK)
17097c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
17107c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_AUTOBOOT), autoboot ? "true" : "false");
17117c478bd9Sstevel@tonic-gate 
17123f2f09c1Sdp 	if (zonecfg_get_bootargs(handle, bootargs, sizeof (bootargs)) == Z_OK &&
17133f2f09c1Sdp 	    strlen(bootargs) > 0) {
17143f2f09c1Sdp 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
17153f2f09c1Sdp 		    pt_to_str(PT_BOOTARGS), bootargs);
17163f2f09c1Sdp 	}
17173f2f09c1Sdp 
17187c478bd9Sstevel@tonic-gate 	if (zonecfg_get_pool(handle, pool, sizeof (pool)) == Z_OK &&
17197c478bd9Sstevel@tonic-gate 	    strlen(pool) > 0)
17207c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
17217c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_POOL), pool);
17227c478bd9Sstevel@tonic-gate 
1723ffbafc53Scomay 	if (zonecfg_get_limitpriv(handle, &limitpriv) == Z_OK &&
1724ffbafc53Scomay 	    strlen(limitpriv) > 0) {
1725ffbafc53Scomay 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
1726ffbafc53Scomay 		    pt_to_str(PT_LIMITPRIV), limitpriv);
1727ffbafc53Scomay 		free(limitpriv);
1728ffbafc53Scomay 	}
1729ffbafc53Scomay 
17300209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
17310209230bSgjelinek 	    strlen(sched) > 0)
17320209230bSgjelinek 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
17330209230bSgjelinek 		    pt_to_str(PT_SCHED), sched);
17343f2f09c1Sdp 
1735f4b3ec61Sdh 	if (zonecfg_get_iptype(handle, &iptype) == Z_OK) {
1736f4b3ec61Sdh 		switch (iptype) {
1737f4b3ec61Sdh 		case ZS_SHARED:
1738f4b3ec61Sdh 			(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
1739f4b3ec61Sdh 			    pt_to_str(PT_IPTYPE), "shared");
1740f4b3ec61Sdh 			break;
1741f4b3ec61Sdh 		case ZS_EXCLUSIVE:
1742f4b3ec61Sdh 			(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
1743f4b3ec61Sdh 			    pt_to_str(PT_IPTYPE), "exclusive");
1744f4b3ec61Sdh 			break;
1745f4b3ec61Sdh 		}
1746f4b3ec61Sdh 	}
1747f4b3ec61Sdh 
17485679c89fSjv 	if (zonecfg_get_hostid(handle, hostidp, sizeof (hostidp)) == Z_OK) {
17495679c89fSjv 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
17505679c89fSjv 		    pt_to_str(PT_HOSTID), hostidp);
17515679c89fSjv 	}
17525679c89fSjv 
17537c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setipdent(handle)) != Z_OK) {
1754bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
17557c478bd9Sstevel@tonic-gate 		goto done;
17567c478bd9Sstevel@tonic-gate 	}
17577c478bd9Sstevel@tonic-gate 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
17587c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
17597c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_IPD));
17607c478bd9Sstevel@tonic-gate 		export_prop(of, PT_DIR, fstab.zone_fs_dir);
17617c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate 	(void) zonecfg_endipdent(handle);
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setfsent(handle)) != Z_OK) {
1766bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
17677c478bd9Sstevel@tonic-gate 		goto done;
17687c478bd9Sstevel@tonic-gate 	}
17697c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
17707c478bd9Sstevel@tonic-gate 		zone_fsopt_t *optptr;
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
17737c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_FS));
17747c478bd9Sstevel@tonic-gate 		export_prop(of, PT_DIR, fstab.zone_fs_dir);
17757c478bd9Sstevel@tonic-gate 		export_prop(of, PT_SPECIAL, fstab.zone_fs_special);
17767c478bd9Sstevel@tonic-gate 		export_prop(of, PT_RAW, fstab.zone_fs_raw);
17777c478bd9Sstevel@tonic-gate 		export_prop(of, PT_TYPE, fstab.zone_fs_type);
17787c478bd9Sstevel@tonic-gate 		for (optptr = fstab.zone_fs_options; optptr != NULL;
17797c478bd9Sstevel@tonic-gate 		    optptr = optptr->zone_fsopt_next) {
17807c478bd9Sstevel@tonic-gate 			/*
17817c478bd9Sstevel@tonic-gate 			 * Simple property values with embedded equal signs
17827c478bd9Sstevel@tonic-gate 			 * need to be quoted to prevent the lexer from
17837c478bd9Sstevel@tonic-gate 			 * mis-parsing them as complex name=value pairs.
17847c478bd9Sstevel@tonic-gate 			 */
17857c478bd9Sstevel@tonic-gate 			if (strchr(optptr->zone_fsopt_opt, '='))
17867c478bd9Sstevel@tonic-gate 				(void) fprintf(of, "%s %s \"%s\"\n",
17877c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_ADD),
17887c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_OPTIONS),
17897c478bd9Sstevel@tonic-gate 				    optptr->zone_fsopt_opt);
17907c478bd9Sstevel@tonic-gate 			else
17917c478bd9Sstevel@tonic-gate 				(void) fprintf(of, "%s %s %s\n",
17927c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_ADD),
17937c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_OPTIONS),
17947c478bd9Sstevel@tonic-gate 				    optptr->zone_fsopt_opt);
17957c478bd9Sstevel@tonic-gate 		}
17967c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
17977c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
1802bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
18037c478bd9Sstevel@tonic-gate 		goto done;
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
18067c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
18077c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_NET));
18087c478bd9Sstevel@tonic-gate 		export_prop(of, PT_ADDRESS, nwiftab.zone_nwif_address);
18097c478bd9Sstevel@tonic-gate 		export_prop(of, PT_PHYSICAL, nwiftab.zone_nwif_physical);
1810de860bd9Sgfaden 		export_prop(of, PT_DEFROUTER, nwiftab.zone_nwif_defrouter);
18117c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setdevent(handle)) != Z_OK) {
1816bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
18177c478bd9Sstevel@tonic-gate 		goto done;
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 	while (zonecfg_getdevent(handle, &devtab) == Z_OK) {
18207c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
18217c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_DEVICE));
18227c478bd9Sstevel@tonic-gate 		export_prop(of, PT_MATCH, devtab.zone_dev_match);
18237c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18247c478bd9Sstevel@tonic-gate 	}
18257c478bd9Sstevel@tonic-gate 	(void) zonecfg_enddevent(handle);
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
1828bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
18297c478bd9Sstevel@tonic-gate 		goto done;
18307c478bd9Sstevel@tonic-gate 	}
18317c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
18327c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s rctl\n", cmd_to_str(CMD_ADD));
18337c478bd9Sstevel@tonic-gate 		export_prop(of, PT_NAME, rctltab.zone_rctl_name);
18347c478bd9Sstevel@tonic-gate 		for (valptr = rctltab.zone_rctl_valptr; valptr != NULL;
18357c478bd9Sstevel@tonic-gate 		    valptr = valptr->zone_rctlval_next) {
18367c478bd9Sstevel@tonic-gate 			fprintf(of, "%s %s (%s=%s,%s=%s,%s=%s)\n",
18377c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_ADD), pt_to_str(PT_VALUE),
18387c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_PRIV), valptr->zone_rctlval_priv,
18397c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_LIMIT), valptr->zone_rctlval_limit,
18407c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_ACTION), valptr->zone_rctlval_action);
18417c478bd9Sstevel@tonic-gate 		}
18427c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18437c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
18447c478bd9Sstevel@tonic-gate 	}
18457c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setattrent(handle)) != Z_OK) {
1848bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
18497c478bd9Sstevel@tonic-gate 		goto done;
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 	while (zonecfg_getattrent(handle, &attrtab) == Z_OK) {
18527c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
18537c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR));
18547c478bd9Sstevel@tonic-gate 		export_prop(of, PT_NAME, attrtab.zone_attr_name);
18557c478bd9Sstevel@tonic-gate 		export_prop(of, PT_TYPE, attrtab.zone_attr_type);
18567c478bd9Sstevel@tonic-gate 		export_prop(of, PT_VALUE, attrtab.zone_attr_value);
18577c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate 	(void) zonecfg_endattrent(handle);
18607c478bd9Sstevel@tonic-gate 
1861fa9e4066Sahrens 	if ((err = zonecfg_setdsent(handle)) != Z_OK) {
1862bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
1863fa9e4066Sahrens 		goto done;
1864fa9e4066Sahrens 	}
1865fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
1866fa9e4066Sahrens 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
1867fa9e4066Sahrens 		    rt_to_str(RT_DATASET));
1868fa9e4066Sahrens 		export_prop(of, PT_NAME, dstab.zone_dataset_name);
1869fa9e4066Sahrens 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
1870fa9e4066Sahrens 	}
1871fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
1872fa9e4066Sahrens 
18730209230bSgjelinek 	if (zonecfg_getpsetent(handle, &psettab) == Z_OK) {
18740209230bSgjelinek 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
18750209230bSgjelinek 		    rt_to_str(RT_DCPU));
18760209230bSgjelinek 		if (strcmp(psettab.zone_ncpu_min, psettab.zone_ncpu_max) == 0)
18770209230bSgjelinek 			(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
18780209230bSgjelinek 			    pt_to_str(PT_NCPUS), psettab.zone_ncpu_max);
18790209230bSgjelinek 		else
18800209230bSgjelinek 			(void) fprintf(of, "%s %s=%s-%s\n", cmd_to_str(CMD_SET),
18810209230bSgjelinek 			    pt_to_str(PT_NCPUS), psettab.zone_ncpu_min,
18820209230bSgjelinek 			    psettab.zone_ncpu_max);
18830209230bSgjelinek 		if (psettab.zone_importance[0] != '\0')
18840209230bSgjelinek 			(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
18850209230bSgjelinek 			    pt_to_str(PT_IMPORTANCE), psettab.zone_importance);
18860209230bSgjelinek 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18870209230bSgjelinek 	}
18880209230bSgjelinek 
18890209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcaptab) == Z_OK) {
18900209230bSgjelinek 		char buf[128];
18910209230bSgjelinek 
18920209230bSgjelinek 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
18930209230bSgjelinek 		    rt_to_str(RT_MCAP));
18940209230bSgjelinek 		bytes_to_units(mcaptab.zone_physmem_cap, buf, sizeof (buf));
18950209230bSgjelinek 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
18960209230bSgjelinek 		    pt_to_str(PT_PHYSICAL), buf);
18970209230bSgjelinek 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18980209230bSgjelinek 	}
18990209230bSgjelinek 
1900c97ad5cdSakolb 	/*
1901c97ad5cdSakolb 	 * There is nothing to export for pcap since this resource is just
1902c97ad5cdSakolb 	 * a container for an rctl alias.
1903c97ad5cdSakolb 	 */
1904c97ad5cdSakolb 
19057c478bd9Sstevel@tonic-gate done:
19067c478bd9Sstevel@tonic-gate 	if (need_to_close)
19077c478bd9Sstevel@tonic-gate 		(void) fclose(of);
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate void
19117c478bd9Sstevel@tonic-gate exit_func(cmd_t *cmd)
19127c478bd9Sstevel@tonic-gate {
19137c478bd9Sstevel@tonic-gate 	int arg, answer;
1914bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 	optind = 0;
19177c478bd9Sstevel@tonic-gate 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?F")) != EOF) {
19187c478bd9Sstevel@tonic-gate 		switch (arg) {
19197c478bd9Sstevel@tonic-gate 		case '?':
19207c478bd9Sstevel@tonic-gate 			longer_usage(CMD_EXIT);
1921bbec428eSgjelinek 			arg_err = B_TRUE;
19227ec75eb8Sgjelinek 			break;
19237c478bd9Sstevel@tonic-gate 		case 'F':
1924bbec428eSgjelinek 			force_exit = B_TRUE;
19257c478bd9Sstevel@tonic-gate 			break;
19267c478bd9Sstevel@tonic-gate 		default:
19277c478bd9Sstevel@tonic-gate 			short_usage(CMD_EXIT);
1928bbec428eSgjelinek 			arg_err = B_TRUE;
19297ec75eb8Sgjelinek 			break;
19307c478bd9Sstevel@tonic-gate 		}
19317c478bd9Sstevel@tonic-gate 	}
19327ec75eb8Sgjelinek 	if (arg_err)
19337ec75eb8Sgjelinek 		return;
19347ec75eb8Sgjelinek 
19357c478bd9Sstevel@tonic-gate 	if (optind < cmd->cmd_argc) {
19367c478bd9Sstevel@tonic-gate 		short_usage(CMD_EXIT);
19377c478bd9Sstevel@tonic-gate 		return;
19387c478bd9Sstevel@tonic-gate 	}
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	if (global_scope || force_exit) {
1941bbec428eSgjelinek 		time_to_exit = B_TRUE;
19427c478bd9Sstevel@tonic-gate 		return;
19437c478bd9Sstevel@tonic-gate 	}
19447c478bd9Sstevel@tonic-gate 
1945bbec428eSgjelinek 	answer = ask_yesno(B_FALSE, "Resource incomplete; really quit");
19467c478bd9Sstevel@tonic-gate 	if (answer == -1) {
19477c478bd9Sstevel@tonic-gate 		zerr(gettext("Resource incomplete, input "
19487c478bd9Sstevel@tonic-gate 		    "not from terminal and -F not specified:\n%s command "
19497c478bd9Sstevel@tonic-gate 		    "ignored, but exiting anyway."), cmd_to_str(CMD_EXIT));
19507c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
19517c478bd9Sstevel@tonic-gate 	} else if (answer == 1) {
1952bbec428eSgjelinek 		time_to_exit = B_TRUE;
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 	/* (answer == 0) => just return */
19557c478bd9Sstevel@tonic-gate }
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate static int
19587c478bd9Sstevel@tonic-gate validate_zonepath_syntax(char *path)
19597c478bd9Sstevel@tonic-gate {
19607c478bd9Sstevel@tonic-gate 	if (path[0] != '/') {
19617c478bd9Sstevel@tonic-gate 		zerr(gettext("%s is not an absolute path."), path);
19627c478bd9Sstevel@tonic-gate 		return (Z_ERR);
19637c478bd9Sstevel@tonic-gate 	}
19647c478bd9Sstevel@tonic-gate 	if (strcmp(path, "/") == 0) {
19657c478bd9Sstevel@tonic-gate 		zerr(gettext("/ is not allowed as a %s."),
19667c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_ZONEPATH));
19677c478bd9Sstevel@tonic-gate 		return (Z_ERR);
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 	return (Z_OK);
19707c478bd9Sstevel@tonic-gate }
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate static void
19737c478bd9Sstevel@tonic-gate add_resource(cmd_t *cmd)
19747c478bd9Sstevel@tonic-gate {
19757c478bd9Sstevel@tonic-gate 	int type;
19760209230bSgjelinek 	struct zone_psettab tmp_psettab;
19770209230bSgjelinek 	struct zone_mcaptab tmp_mcaptab;
1978c97ad5cdSakolb 	uint64_t tmp;
19790209230bSgjelinek 	uint64_t tmp_mcap;
19800209230bSgjelinek 	char pool[MAXNAMELEN];
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	if ((type = cmd->cmd_res_type) == RT_UNKNOWN) {
1983bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
19847c478bd9Sstevel@tonic-gate 		goto bad;
19857c478bd9Sstevel@tonic-gate 	}
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	switch (type) {
19887c478bd9Sstevel@tonic-gate 	case RT_FS:
19897c478bd9Sstevel@tonic-gate 		bzero(&in_progress_fstab, sizeof (in_progress_fstab));
19907c478bd9Sstevel@tonic-gate 		return;
19917c478bd9Sstevel@tonic-gate 	case RT_IPD:
1992087719fdSdp 		if (state_atleast(ZONE_STATE_INSTALLED)) {
19937c478bd9Sstevel@tonic-gate 			zerr(gettext("Zone %s already installed; %s %s not "
19947c478bd9Sstevel@tonic-gate 			    "allowed."), zone, cmd_to_str(CMD_ADD),
19957c478bd9Sstevel@tonic-gate 			    rt_to_str(RT_IPD));
19967c478bd9Sstevel@tonic-gate 			goto bad;
19977c478bd9Sstevel@tonic-gate 		}
19987c478bd9Sstevel@tonic-gate 		bzero(&in_progress_ipdtab, sizeof (in_progress_ipdtab));
19997c478bd9Sstevel@tonic-gate 		return;
20007c478bd9Sstevel@tonic-gate 	case RT_NET:
20017c478bd9Sstevel@tonic-gate 		bzero(&in_progress_nwiftab, sizeof (in_progress_nwiftab));
20027c478bd9Sstevel@tonic-gate 		return;
20037c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
20047c478bd9Sstevel@tonic-gate 		bzero(&in_progress_devtab, sizeof (in_progress_devtab));
20057c478bd9Sstevel@tonic-gate 		return;
20067c478bd9Sstevel@tonic-gate 	case RT_RCTL:
20070209230bSgjelinek 		if (global_zone)
20080209230bSgjelinek 			zerr(gettext("WARNING: Setting a global zone resource "
20090209230bSgjelinek 			    "control too low could deny\nservice "
20100209230bSgjelinek 			    "to even the root user; "
20110209230bSgjelinek 			    "this could render the system impossible\n"
20120209230bSgjelinek 			    "to administer.  Please use caution."));
20137c478bd9Sstevel@tonic-gate 		bzero(&in_progress_rctltab, sizeof (in_progress_rctltab));
20147c478bd9Sstevel@tonic-gate 		return;
20157c478bd9Sstevel@tonic-gate 	case RT_ATTR:
20167c478bd9Sstevel@tonic-gate 		bzero(&in_progress_attrtab, sizeof (in_progress_attrtab));
20177c478bd9Sstevel@tonic-gate 		return;
2018fa9e4066Sahrens 	case RT_DATASET:
2019fa9e4066Sahrens 		bzero(&in_progress_dstab, sizeof (in_progress_dstab));
2020fa9e4066Sahrens 		return;
20210209230bSgjelinek 	case RT_DCPU:
2022c97ad5cdSakolb 		/* Make sure there isn't already a cpu-set or cpu-cap entry. */
20230209230bSgjelinek 		if (zonecfg_lookup_pset(handle, &tmp_psettab) == Z_OK) {
20240209230bSgjelinek 			zerr(gettext("The %s resource already exists."),
20250209230bSgjelinek 			    rt_to_str(RT_DCPU));
20260209230bSgjelinek 			goto bad;
20270209230bSgjelinek 		}
2028c97ad5cdSakolb 		if (zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &tmp) !=
2029c97ad5cdSakolb 		    Z_NO_ENTRY) {
2030c97ad5cdSakolb 			zerr(gettext("The %s resource already exists."),
2031c97ad5cdSakolb 			    rt_to_str(RT_PCAP));
2032c97ad5cdSakolb 			goto bad;
2033c97ad5cdSakolb 		}
20340209230bSgjelinek 
20350209230bSgjelinek 		/* Make sure the pool property isn't set. */
20360209230bSgjelinek 		if (zonecfg_get_pool(handle, pool, sizeof (pool)) == Z_OK &&
20370209230bSgjelinek 		    strlen(pool) > 0) {
20380209230bSgjelinek 			zerr(gettext("The %s property is already set.  "
20390209230bSgjelinek 			    "A persistent pool is incompatible with\nthe %s "
20400209230bSgjelinek 			    "resource."),
20410209230bSgjelinek 			    pt_to_str(PT_POOL), rt_to_str(RT_DCPU));
20420209230bSgjelinek 			goto bad;
20430209230bSgjelinek 		}
20440209230bSgjelinek 
20450209230bSgjelinek 		bzero(&in_progress_psettab, sizeof (in_progress_psettab));
20460209230bSgjelinek 		return;
2047c97ad5cdSakolb 	case RT_PCAP:
2048c97ad5cdSakolb 		/*
2049c97ad5cdSakolb 		 * Make sure there isn't already a cpu-set or incompatible
2050c97ad5cdSakolb 		 * cpu-cap rctls.
2051c97ad5cdSakolb 		 */
2052c97ad5cdSakolb 		if (zonecfg_lookup_pset(handle, &tmp_psettab) == Z_OK) {
2053c97ad5cdSakolb 			zerr(gettext("The %s resource already exists."),
2054c97ad5cdSakolb 			    rt_to_str(RT_DCPU));
2055c97ad5cdSakolb 			goto bad;
2056c97ad5cdSakolb 		}
2057c97ad5cdSakolb 
2058c97ad5cdSakolb 		switch (zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &tmp)) {
2059c97ad5cdSakolb 		case Z_ALIAS_DISALLOW:
2060c97ad5cdSakolb 			zone_perror(rt_to_str(RT_PCAP), Z_ALIAS_DISALLOW,
2061bbec428eSgjelinek 			    B_FALSE);
2062c97ad5cdSakolb 			goto bad;
2063c97ad5cdSakolb 
2064c97ad5cdSakolb 		case Z_OK:
2065c97ad5cdSakolb 			zerr(gettext("The %s resource already exists."),
2066c97ad5cdSakolb 			    rt_to_str(RT_PCAP));
2067c97ad5cdSakolb 			goto bad;
2068c97ad5cdSakolb 
2069c97ad5cdSakolb 		default:
2070c97ad5cdSakolb 			break;
2071c97ad5cdSakolb 		}
2072c97ad5cdSakolb 		return;
20730209230bSgjelinek 	case RT_MCAP:
20740209230bSgjelinek 		/*
20750209230bSgjelinek 		 * Make sure there isn't already a mem-cap entry or max-swap
20760209230bSgjelinek 		 * or max-locked rctl.
20770209230bSgjelinek 		 */
20780209230bSgjelinek 		if (zonecfg_lookup_mcap(handle, &tmp_mcaptab) == Z_OK ||
20790209230bSgjelinek 		    zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP, &tmp_mcap)
20800209230bSgjelinek 		    == Z_OK ||
20810209230bSgjelinek 		    zonecfg_get_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM,
20820209230bSgjelinek 		    &tmp_mcap) == Z_OK) {
20830209230bSgjelinek 			zerr(gettext("The %s resource or a related resource "
20840209230bSgjelinek 			    "control already exists."), rt_to_str(RT_MCAP));
20850209230bSgjelinek 			goto bad;
20860209230bSgjelinek 		}
20870209230bSgjelinek 		if (global_zone)
20880209230bSgjelinek 			zerr(gettext("WARNING: Setting a global zone memory "
20890209230bSgjelinek 			    "cap too low could deny\nservice "
20900209230bSgjelinek 			    "to even the root user; "
20910209230bSgjelinek 			    "this could render the system impossible\n"
20920209230bSgjelinek 			    "to administer.  Please use caution."));
20930209230bSgjelinek 		bzero(&in_progress_mcaptab, sizeof (in_progress_mcaptab));
20940209230bSgjelinek 		return;
20957c478bd9Sstevel@tonic-gate 	default:
2096bbec428eSgjelinek 		zone_perror(rt_to_str(type), Z_NO_RESOURCE_TYPE, B_TRUE);
2097bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
2098bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
20997c478bd9Sstevel@tonic-gate 	}
21007c478bd9Sstevel@tonic-gate bad:
2101bbec428eSgjelinek 	global_scope = B_TRUE;
21027c478bd9Sstevel@tonic-gate 	end_op = -1;
21037c478bd9Sstevel@tonic-gate }
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate static void
21067c478bd9Sstevel@tonic-gate do_complex_rctl_val(complex_property_ptr_t cp)
21077c478bd9Sstevel@tonic-gate {
21087c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *rctlvaltab;
21097c478bd9Sstevel@tonic-gate 	complex_property_ptr_t cx;
2110bbec428eSgjelinek 	boolean_t seen_priv = B_FALSE, seen_limit = B_FALSE,
2111bbec428eSgjelinek 	    seen_action = B_FALSE;
21127c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk;
21137c478bd9Sstevel@tonic-gate 	int err;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	if ((rctlvaltab = alloc_rctlvaltab()) == NULL) {
2116bbec428eSgjelinek 		zone_perror(zone, Z_NOMEM, B_TRUE);
21177c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
21187c478bd9Sstevel@tonic-gate 	}
21197c478bd9Sstevel@tonic-gate 	for (cx = cp; cx != NULL; cx = cx->cp_next) {
21207c478bd9Sstevel@tonic-gate 		switch (cx->cp_type) {
21217c478bd9Sstevel@tonic-gate 		case PT_PRIV:
21227c478bd9Sstevel@tonic-gate 			if (seen_priv) {
21237c478bd9Sstevel@tonic-gate 				zerr(gettext("%s already specified"),
21247c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_PRIV));
21257c478bd9Sstevel@tonic-gate 				goto bad;
21267c478bd9Sstevel@tonic-gate 			}
21277c478bd9Sstevel@tonic-gate 			(void) strlcpy(rctlvaltab->zone_rctlval_priv,
21287c478bd9Sstevel@tonic-gate 			    cx->cp_value,
21297c478bd9Sstevel@tonic-gate 			    sizeof (rctlvaltab->zone_rctlval_priv));
2130bbec428eSgjelinek 			seen_priv = B_TRUE;
21317c478bd9Sstevel@tonic-gate 			break;
21327c478bd9Sstevel@tonic-gate 		case PT_LIMIT:
21337c478bd9Sstevel@tonic-gate 			if (seen_limit) {
21347c478bd9Sstevel@tonic-gate 				zerr(gettext("%s already specified"),
21357c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_LIMIT));
21367c478bd9Sstevel@tonic-gate 				goto bad;
21377c478bd9Sstevel@tonic-gate 			}
21387c478bd9Sstevel@tonic-gate 			(void) strlcpy(rctlvaltab->zone_rctlval_limit,
21397c478bd9Sstevel@tonic-gate 			    cx->cp_value,
21407c478bd9Sstevel@tonic-gate 			    sizeof (rctlvaltab->zone_rctlval_limit));
2141bbec428eSgjelinek 			seen_limit = B_TRUE;
21427c478bd9Sstevel@tonic-gate 			break;
21437c478bd9Sstevel@tonic-gate 		case PT_ACTION:
21447c478bd9Sstevel@tonic-gate 			if (seen_action) {
21457c478bd9Sstevel@tonic-gate 				zerr(gettext("%s already specified"),
21467c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_ACTION));
21477c478bd9Sstevel@tonic-gate 				goto bad;
21487c478bd9Sstevel@tonic-gate 			}
21497c478bd9Sstevel@tonic-gate 			(void) strlcpy(rctlvaltab->zone_rctlval_action,
21507c478bd9Sstevel@tonic-gate 			    cx->cp_value,
21517c478bd9Sstevel@tonic-gate 			    sizeof (rctlvaltab->zone_rctlval_action));
2152bbec428eSgjelinek 			seen_action = B_TRUE;
21537c478bd9Sstevel@tonic-gate 			break;
21547c478bd9Sstevel@tonic-gate 		default:
21557c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(PT_VALUE),
2156bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
2157bbec428eSgjelinek 			long_usage(CMD_ADD, B_TRUE);
2158bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
21597c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctlvaltab);
21607c478bd9Sstevel@tonic-gate 			return;
21617c478bd9Sstevel@tonic-gate 		}
21627c478bd9Sstevel@tonic-gate 	}
21637c478bd9Sstevel@tonic-gate 	if (!seen_priv)
21647c478bd9Sstevel@tonic-gate 		zerr(gettext("%s not specified"), pt_to_str(PT_PRIV));
21657c478bd9Sstevel@tonic-gate 	if (!seen_limit)
21667c478bd9Sstevel@tonic-gate 		zerr(gettext("%s not specified"), pt_to_str(PT_LIMIT));
21677c478bd9Sstevel@tonic-gate 	if (!seen_action)
21687c478bd9Sstevel@tonic-gate 		zerr(gettext("%s not specified"), pt_to_str(PT_ACTION));
21697c478bd9Sstevel@tonic-gate 	if (!seen_priv || !seen_limit || !seen_action)
21707c478bd9Sstevel@tonic-gate 		goto bad;
21717c478bd9Sstevel@tonic-gate 	rctlvaltab->zone_rctlval_next = NULL;
21727c478bd9Sstevel@tonic-gate 	rctlblk = alloca(rctlblk_size());
21737c478bd9Sstevel@tonic-gate 	/*
21747c478bd9Sstevel@tonic-gate 	 * Make sure the rctl value looks roughly correct; we won't know if
21757c478bd9Sstevel@tonic-gate 	 * it's truly OK until we verify the configuration on the target
21767c478bd9Sstevel@tonic-gate 	 * system.
21777c478bd9Sstevel@tonic-gate 	 */
21787c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(rctlvaltab, rctlblk) != Z_OK ||
21797c478bd9Sstevel@tonic-gate 	    !zonecfg_valid_rctlblk(rctlblk)) {
21807c478bd9Sstevel@tonic-gate 		zerr(gettext("Invalid %s %s specification"), rt_to_str(RT_RCTL),
21817c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_VALUE));
21827c478bd9Sstevel@tonic-gate 		goto bad;
21837c478bd9Sstevel@tonic-gate 	}
21847c478bd9Sstevel@tonic-gate 	err = zonecfg_add_rctl_value(&in_progress_rctltab, rctlvaltab);
21857c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
2186bbec428eSgjelinek 		zone_perror(pt_to_str(PT_VALUE), err, B_TRUE);
21877c478bd9Sstevel@tonic-gate 	return;
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate bad:
21907c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctlvaltab);
21917c478bd9Sstevel@tonic-gate }
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate static void
21947c478bd9Sstevel@tonic-gate add_property(cmd_t *cmd)
21957c478bd9Sstevel@tonic-gate {
21967c478bd9Sstevel@tonic-gate 	char *prop_id;
21977c478bd9Sstevel@tonic-gate 	int err, res_type, prop_type;
21987c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
21997c478bd9Sstevel@tonic-gate 	list_property_ptr_t l;
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	res_type = resource_scope;
22027c478bd9Sstevel@tonic-gate 	prop_type = cmd->cmd_prop_name[0];
22037c478bd9Sstevel@tonic-gate 	if (res_type == RT_UNKNOWN || prop_type == PT_UNKNOWN) {
2204bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
22057c478bd9Sstevel@tonic-gate 		return;
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	if (cmd->cmd_prop_nv_pairs != 1) {
2209bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
22107c478bd9Sstevel@tonic-gate 		return;
22117c478bd9Sstevel@tonic-gate 	}
22127c478bd9Sstevel@tonic-gate 
2213bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
22147c478bd9Sstevel@tonic-gate 		return;
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	switch (res_type) {
22177c478bd9Sstevel@tonic-gate 	case RT_FS:
22187c478bd9Sstevel@tonic-gate 		if (prop_type != PT_OPTIONS) {
22197c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
2220bbec428eSgjelinek 			    B_TRUE);
2221bbec428eSgjelinek 			long_usage(CMD_ADD, B_TRUE);
2222bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
22237c478bd9Sstevel@tonic-gate 			return;
22247c478bd9Sstevel@tonic-gate 		}
22257c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[0];
22267c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE &&
22277c478bd9Sstevel@tonic-gate 		    pp->pv_type != PROP_VAL_LIST) {
22287c478bd9Sstevel@tonic-gate 			zerr(gettext("A %s or %s value was expected here."),
22297c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_SIMPLE),
22307c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_LIST));
2231bbec428eSgjelinek 			saw_error = B_TRUE;
22327c478bd9Sstevel@tonic-gate 			return;
22337c478bd9Sstevel@tonic-gate 		}
22347c478bd9Sstevel@tonic-gate 		if (pp->pv_type == PROP_VAL_SIMPLE) {
22357c478bd9Sstevel@tonic-gate 			if (pp->pv_simple == NULL) {
2236bbec428eSgjelinek 				long_usage(CMD_ADD, B_TRUE);
22377c478bd9Sstevel@tonic-gate 				return;
22387c478bd9Sstevel@tonic-gate 			}
22397c478bd9Sstevel@tonic-gate 			prop_id = pp->pv_simple;
22407c478bd9Sstevel@tonic-gate 			err = zonecfg_add_fs_option(&in_progress_fstab,
22417c478bd9Sstevel@tonic-gate 			    prop_id);
22427c478bd9Sstevel@tonic-gate 			if (err != Z_OK)
2243bbec428eSgjelinek 				zone_perror(pt_to_str(prop_type), err, B_TRUE);
22447c478bd9Sstevel@tonic-gate 		} else {
22457c478bd9Sstevel@tonic-gate 			list_property_ptr_t list;
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 			for (list = pp->pv_list; list != NULL;
22487c478bd9Sstevel@tonic-gate 			    list = list->lp_next) {
22497c478bd9Sstevel@tonic-gate 				prop_id = list->lp_simple;
22507c478bd9Sstevel@tonic-gate 				if (prop_id == NULL)
22517c478bd9Sstevel@tonic-gate 					break;
22527c478bd9Sstevel@tonic-gate 				err = zonecfg_add_fs_option(
22537c478bd9Sstevel@tonic-gate 				    &in_progress_fstab, prop_id);
22547c478bd9Sstevel@tonic-gate 				if (err != Z_OK)
22557c478bd9Sstevel@tonic-gate 					zone_perror(pt_to_str(prop_type), err,
2256bbec428eSgjelinek 					    B_TRUE);
22577c478bd9Sstevel@tonic-gate 			}
22587c478bd9Sstevel@tonic-gate 		}
22597c478bd9Sstevel@tonic-gate 		return;
22607c478bd9Sstevel@tonic-gate 	case RT_RCTL:
22617c478bd9Sstevel@tonic-gate 		if (prop_type != PT_VALUE) {
22627c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
2263bbec428eSgjelinek 			    B_TRUE);
2264bbec428eSgjelinek 			long_usage(CMD_ADD, B_TRUE);
2265bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
22667c478bd9Sstevel@tonic-gate 			return;
22677c478bd9Sstevel@tonic-gate 		}
22687c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[0];
22697c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_COMPLEX &&
22707c478bd9Sstevel@tonic-gate 		    pp->pv_type != PROP_VAL_LIST) {
22717c478bd9Sstevel@tonic-gate 			zerr(gettext("A %s or %s value was expected here."),
22727c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_COMPLEX),
22737c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_LIST));
2274bbec428eSgjelinek 			saw_error = B_TRUE;
22757c478bd9Sstevel@tonic-gate 			return;
22767c478bd9Sstevel@tonic-gate 		}
22777c478bd9Sstevel@tonic-gate 		if (pp->pv_type == PROP_VAL_COMPLEX) {
22787c478bd9Sstevel@tonic-gate 			do_complex_rctl_val(pp->pv_complex);
22797c478bd9Sstevel@tonic-gate 			return;
22807c478bd9Sstevel@tonic-gate 		}
22817c478bd9Sstevel@tonic-gate 		for (l = pp->pv_list; l != NULL; l = l->lp_next)
22827c478bd9Sstevel@tonic-gate 			do_complex_rctl_val(l->lp_complex);
22837c478bd9Sstevel@tonic-gate 		return;
22847c478bd9Sstevel@tonic-gate 	default:
2285bbec428eSgjelinek 		zone_perror(rt_to_str(res_type), Z_NO_RESOURCE_TYPE, B_TRUE);
2286bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
2287bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
22887c478bd9Sstevel@tonic-gate 		return;
22897c478bd9Sstevel@tonic-gate 	}
22907c478bd9Sstevel@tonic-gate }
22917c478bd9Sstevel@tonic-gate 
22920209230bSgjelinek static boolean_t
22930209230bSgjelinek gz_invalid_resource(int type)
22940209230bSgjelinek {
22950209230bSgjelinek 	return (global_zone && (type == RT_FS || type == RT_IPD ||
22960209230bSgjelinek 	    type == RT_NET || type == RT_DEVICE || type == RT_ATTR ||
22970209230bSgjelinek 	    type == RT_DATASET));
22980209230bSgjelinek }
22990209230bSgjelinek 
23000209230bSgjelinek static boolean_t
23010209230bSgjelinek gz_invalid_rt_property(int type)
23020209230bSgjelinek {
23030209230bSgjelinek 	return (global_zone && (type == RT_ZONENAME || type == RT_ZONEPATH ||
23040209230bSgjelinek 	    type == RT_AUTOBOOT || type == RT_LIMITPRIV ||
2305f4b3ec61Sdh 	    type == RT_BOOTARGS || type == RT_BRAND || type == RT_SCHED ||
23065679c89fSjv 	    type == RT_IPTYPE || type == RT_HOSTID));
23070209230bSgjelinek }
23080209230bSgjelinek 
23090209230bSgjelinek static boolean_t
23100209230bSgjelinek gz_invalid_property(int type)
23110209230bSgjelinek {
23120209230bSgjelinek 	return (global_zone && (type == PT_ZONENAME || type == PT_ZONEPATH ||
23130209230bSgjelinek 	    type == PT_AUTOBOOT || type == PT_LIMITPRIV ||
2314f4b3ec61Sdh 	    type == PT_BOOTARGS || type == PT_BRAND || type == PT_SCHED ||
23155679c89fSjv 	    type == PT_IPTYPE || type == PT_HOSTID));
23160209230bSgjelinek }
23170209230bSgjelinek 
23187c478bd9Sstevel@tonic-gate void
23197c478bd9Sstevel@tonic-gate add_func(cmd_t *cmd)
23207c478bd9Sstevel@tonic-gate {
23217c478bd9Sstevel@tonic-gate 	int arg;
2322bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	optind = 0;
23277ec75eb8Sgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?")) != EOF) {
23287c478bd9Sstevel@tonic-gate 		switch (arg) {
23297c478bd9Sstevel@tonic-gate 		case '?':
23307c478bd9Sstevel@tonic-gate 			longer_usage(CMD_ADD);
2331bbec428eSgjelinek 			arg_err = B_TRUE;
23327ec75eb8Sgjelinek 			break;
23337c478bd9Sstevel@tonic-gate 		default:
23347c478bd9Sstevel@tonic-gate 			short_usage(CMD_ADD);
2335bbec428eSgjelinek 			arg_err = B_TRUE;
23367ec75eb8Sgjelinek 			break;
23377c478bd9Sstevel@tonic-gate 		}
23387c478bd9Sstevel@tonic-gate 	}
23397ec75eb8Sgjelinek 	if (arg_err)
23407ec75eb8Sgjelinek 		return;
23417ec75eb8Sgjelinek 
23427c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
23437c478bd9Sstevel@tonic-gate 		short_usage(CMD_ADD);
23447c478bd9Sstevel@tonic-gate 		return;
23457c478bd9Sstevel@tonic-gate 	}
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_ADD))
23487c478bd9Sstevel@tonic-gate 		return;
23497c478bd9Sstevel@tonic-gate 
2350bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
23517c478bd9Sstevel@tonic-gate 		return;
23527c478bd9Sstevel@tonic-gate 	if (global_scope) {
23530209230bSgjelinek 		if (gz_invalid_resource(cmd->cmd_res_type)) {
23540209230bSgjelinek 			zerr(gettext("Cannot add a %s resource to the "
23550209230bSgjelinek 			    "global zone."), rt_to_str(cmd->cmd_res_type));
2356bbec428eSgjelinek 			saw_error = B_TRUE;
23570209230bSgjelinek 			return;
23580209230bSgjelinek 		}
23590209230bSgjelinek 
2360bbec428eSgjelinek 		global_scope = B_FALSE;
23617c478bd9Sstevel@tonic-gate 		resource_scope = cmd->cmd_res_type;
23627c478bd9Sstevel@tonic-gate 		end_op = CMD_ADD;
23637c478bd9Sstevel@tonic-gate 		add_resource(cmd);
23647c478bd9Sstevel@tonic-gate 	} else
23657c478bd9Sstevel@tonic-gate 		add_property(cmd);
23667c478bd9Sstevel@tonic-gate }
23677c478bd9Sstevel@tonic-gate 
2368087719fdSdp /*
2369087719fdSdp  * This routine has an unusual implementation, because it tries very
2370087719fdSdp  * hard to succeed in the face of a variety of failure modes.
2371087719fdSdp  * The most common and most vexing occurs when the index file and
2372087719fdSdp  * the /etc/zones/<zonename.xml> file are not both present.  In
2373087719fdSdp  * this case, delete must eradicate as much of the zone state as is left
2374087719fdSdp  * so that the user can later create a new zone with the same name.
2375087719fdSdp  */
23767c478bd9Sstevel@tonic-gate void
23777c478bd9Sstevel@tonic-gate delete_func(cmd_t *cmd)
23787c478bd9Sstevel@tonic-gate {
23797c478bd9Sstevel@tonic-gate 	int err, arg, answer;
23807c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* enough to ask a question */
2381bbec428eSgjelinek 	boolean_t force = B_FALSE;
2382bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	optind = 0;
23857c478bd9Sstevel@tonic-gate 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?F")) != EOF) {
23867c478bd9Sstevel@tonic-gate 		switch (arg) {
23877c478bd9Sstevel@tonic-gate 		case '?':
23887c478bd9Sstevel@tonic-gate 			longer_usage(CMD_DELETE);
2389bbec428eSgjelinek 			arg_err = B_TRUE;
23907ec75eb8Sgjelinek 			break;
23917c478bd9Sstevel@tonic-gate 		case 'F':
2392bbec428eSgjelinek 			force = B_TRUE;
23937c478bd9Sstevel@tonic-gate 			break;
23947c478bd9Sstevel@tonic-gate 		default:
23957c478bd9Sstevel@tonic-gate 			short_usage(CMD_DELETE);
2396bbec428eSgjelinek 			arg_err = B_TRUE;
23977ec75eb8Sgjelinek 			break;
23987c478bd9Sstevel@tonic-gate 		}
23997c478bd9Sstevel@tonic-gate 	}
24007ec75eb8Sgjelinek 	if (arg_err)
24017ec75eb8Sgjelinek 		return;
24027ec75eb8Sgjelinek 
24037c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
24047c478bd9Sstevel@tonic-gate 		short_usage(CMD_DELETE);
24057c478bd9Sstevel@tonic-gate 		return;
24067c478bd9Sstevel@tonic-gate 	}
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_DELETE))
24097c478bd9Sstevel@tonic-gate 		return;
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 	if (!force) {
2412087719fdSdp 		/*
2413087719fdSdp 		 * Initialize sets up the global called "handle" and warns the
2414087719fdSdp 		 * user if the zone is not configured.  In force mode, we don't
2415087719fdSdp 		 * trust that evaluation, and hence skip it.  (We don't need the
2416087719fdSdp 		 * handle to be loaded anyway, since zonecfg_destroy is done by
2417087719fdSdp 		 * zonename).  However, we also have to take care to emulate the
2418087719fdSdp 		 * messages spit out by initialize; see below.
2419087719fdSdp 		 */
2420bbec428eSgjelinek 		if (initialize(B_TRUE) != Z_OK)
2421087719fdSdp 			return;
2422087719fdSdp 
24237c478bd9Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
24247c478bd9Sstevel@tonic-gate 		    gettext("Are you sure you want to delete zone %s"), zone);
2425bbec428eSgjelinek 		if ((answer = ask_yesno(B_FALSE, line)) == -1) {
2426087719fdSdp 			zerr(gettext("Input not from terminal and -F not "
2427087719fdSdp 			    "specified:\n%s command ignored, exiting."),
2428087719fdSdp 			    cmd_to_str(CMD_DELETE));
24297c478bd9Sstevel@tonic-gate 			exit(Z_ERR);
24307c478bd9Sstevel@tonic-gate 		}
24317c478bd9Sstevel@tonic-gate 		if (answer != 1)
24327c478bd9Sstevel@tonic-gate 			return;
24337c478bd9Sstevel@tonic-gate 	}
24347c478bd9Sstevel@tonic-gate 
2435087719fdSdp 	if ((err = zonecfg_destroy(zone, force)) != Z_OK) {
2436087719fdSdp 		if ((err == Z_BAD_ZONE_STATE) && !force) {
2437087719fdSdp 			zerr(gettext("Zone %s not in %s state; %s not "
2438087719fdSdp 			    "allowed.  Use -F to force %s."),
2439087719fdSdp 			    zone, zone_state_str(ZONE_STATE_CONFIGURED),
2440087719fdSdp 			    cmd_to_str(CMD_DELETE), cmd_to_str(CMD_DELETE));
2441087719fdSdp 		} else {
2442bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
2443087719fdSdp 		}
24447c478bd9Sstevel@tonic-gate 	}
2445bbec428eSgjelinek 	need_to_commit = B_FALSE;
2446087719fdSdp 
2447087719fdSdp 	/*
2448087719fdSdp 	 * Emulate initialize's messaging; if there wasn't a valid handle to
2449087719fdSdp 	 * begin with, then user had typed delete (or delete -F) multiple
2450087719fdSdp 	 * times.  So we emit a message.
2451087719fdSdp 	 *
2452087719fdSdp 	 * We only do this in the 'force' case because normally, initialize()
2453087719fdSdp 	 * takes care of this for us.
2454087719fdSdp 	 */
2455087719fdSdp 	if (force && zonecfg_check_handle(handle) != Z_OK && interactive_mode)
2456087719fdSdp 		(void) printf(gettext("Use '%s' to begin "
2457087719fdSdp 		    "configuring a new zone.\n"), cmd_to_str(CMD_CREATE));
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	/*
24607c478bd9Sstevel@tonic-gate 	 * Time for a new handle: finish the old one off first
24617c478bd9Sstevel@tonic-gate 	 * then get a new one properly to avoid leaks.
24627c478bd9Sstevel@tonic-gate 	 */
2463087719fdSdp 	if (got_handle) {
2464087719fdSdp 		zonecfg_fini_handle(handle);
2465087719fdSdp 		if ((handle = zonecfg_init_handle()) == NULL) {
2466bbec428eSgjelinek 			zone_perror(execname, Z_NOMEM, B_TRUE);
2467087719fdSdp 			exit(Z_ERR);
2468087719fdSdp 		}
2469087719fdSdp 		if ((err = zonecfg_get_handle(zone, handle)) != Z_OK) {
2470087719fdSdp 			/* If there was no zone before, that's OK */
2471087719fdSdp 			if (err != Z_NO_ZONE)
2472bbec428eSgjelinek 				zone_perror(zone, err, B_TRUE);
2473bbec428eSgjelinek 			got_handle = B_FALSE;
2474087719fdSdp 		}
24757c478bd9Sstevel@tonic-gate 	}
24767c478bd9Sstevel@tonic-gate }
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate static int
2479bbec428eSgjelinek fill_in_fstab(cmd_t *cmd, struct zone_fstab *fstab, boolean_t fill_in_only)
24807c478bd9Sstevel@tonic-gate {
24817c478bd9Sstevel@tonic-gate 	int err, i;
24827c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
24837c478bd9Sstevel@tonic-gate 
2484bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
24857c478bd9Sstevel@tonic-gate 		return (err);
24867c478bd9Sstevel@tonic-gate 
2487e193d1e6Svp 	bzero(fstab, sizeof (*fstab));
24887c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
24897c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
24907c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
24917c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2492bbec428eSgjelinek 			saw_error = B_TRUE;
24937c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
24947c478bd9Sstevel@tonic-gate 		}
24957c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
24967c478bd9Sstevel@tonic-gate 		case PT_DIR:
24977c478bd9Sstevel@tonic-gate 			(void) strlcpy(fstab->zone_fs_dir, pp->pv_simple,
24987c478bd9Sstevel@tonic-gate 			    sizeof (fstab->zone_fs_dir));
24997c478bd9Sstevel@tonic-gate 			break;
25007c478bd9Sstevel@tonic-gate 		case PT_SPECIAL:
25017c478bd9Sstevel@tonic-gate 			(void) strlcpy(fstab->zone_fs_special, pp->pv_simple,
25027c478bd9Sstevel@tonic-gate 			    sizeof (fstab->zone_fs_special));
25037c478bd9Sstevel@tonic-gate 			break;
25047c478bd9Sstevel@tonic-gate 		case PT_RAW:
25057c478bd9Sstevel@tonic-gate 			(void) strlcpy(fstab->zone_fs_raw, pp->pv_simple,
25067c478bd9Sstevel@tonic-gate 			    sizeof (fstab->zone_fs_raw));
25077c478bd9Sstevel@tonic-gate 			break;
25087c478bd9Sstevel@tonic-gate 		case PT_TYPE:
25097c478bd9Sstevel@tonic-gate 			(void) strlcpy(fstab->zone_fs_type, pp->pv_simple,
25107c478bd9Sstevel@tonic-gate 			    sizeof (fstab->zone_fs_type));
25117c478bd9Sstevel@tonic-gate 			break;
25127c478bd9Sstevel@tonic-gate 		default:
25137c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2514bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
25157c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
25167c478bd9Sstevel@tonic-gate 		}
25177c478bd9Sstevel@tonic-gate 	}
25187c478bd9Sstevel@tonic-gate 	if (fill_in_only)
25197c478bd9Sstevel@tonic-gate 		return (Z_OK);
25207c478bd9Sstevel@tonic-gate 	return (zonecfg_lookup_filesystem(handle, fstab));
25217c478bd9Sstevel@tonic-gate }
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate static int
2524bbec428eSgjelinek fill_in_ipdtab(cmd_t *cmd, struct zone_fstab *ipdtab, boolean_t fill_in_only)
25257c478bd9Sstevel@tonic-gate {
25267c478bd9Sstevel@tonic-gate 	int err, i;
25277c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
25287c478bd9Sstevel@tonic-gate 
2529bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
25307c478bd9Sstevel@tonic-gate 		return (err);
25317c478bd9Sstevel@tonic-gate 
2532e193d1e6Svp 	bzero(ipdtab, sizeof (*ipdtab));
25337c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
25347c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
25357c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
25367c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2537bbec428eSgjelinek 			saw_error = B_TRUE;
25387c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
25397c478bd9Sstevel@tonic-gate 		}
25407c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
25417c478bd9Sstevel@tonic-gate 		case PT_DIR:
25427c478bd9Sstevel@tonic-gate 			(void) strlcpy(ipdtab->zone_fs_dir, pp->pv_simple,
25437c478bd9Sstevel@tonic-gate 			    sizeof (ipdtab->zone_fs_dir));
25447c478bd9Sstevel@tonic-gate 			break;
25457c478bd9Sstevel@tonic-gate 		default:
25467c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2547bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
25487c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
25497c478bd9Sstevel@tonic-gate 		}
25507c478bd9Sstevel@tonic-gate 	}
25517c478bd9Sstevel@tonic-gate 	if (fill_in_only)
25527c478bd9Sstevel@tonic-gate 		return (Z_OK);
25537c478bd9Sstevel@tonic-gate 	return (zonecfg_lookup_ipd(handle, ipdtab));
25547c478bd9Sstevel@tonic-gate }
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate static int
2557bbec428eSgjelinek fill_in_nwiftab(cmd_t *cmd, struct zone_nwiftab *nwiftab,
2558bbec428eSgjelinek     boolean_t fill_in_only)
25597c478bd9Sstevel@tonic-gate {
25607c478bd9Sstevel@tonic-gate 	int err, i;
25617c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
25627c478bd9Sstevel@tonic-gate 
2563bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
25647c478bd9Sstevel@tonic-gate 		return (err);
25657c478bd9Sstevel@tonic-gate 
2566e193d1e6Svp 	bzero(nwiftab, sizeof (*nwiftab));
25677c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
25687c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
25697c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
25707c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2571bbec428eSgjelinek 			saw_error = B_TRUE;
25727c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
25737c478bd9Sstevel@tonic-gate 		}
25747c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
25757c478bd9Sstevel@tonic-gate 		case PT_ADDRESS:
25767c478bd9Sstevel@tonic-gate 			(void) strlcpy(nwiftab->zone_nwif_address,
25777c478bd9Sstevel@tonic-gate 			    pp->pv_simple, sizeof (nwiftab->zone_nwif_address));
25787c478bd9Sstevel@tonic-gate 			break;
25797c478bd9Sstevel@tonic-gate 		case PT_PHYSICAL:
25807c478bd9Sstevel@tonic-gate 			(void) strlcpy(nwiftab->zone_nwif_physical,
25817c478bd9Sstevel@tonic-gate 			    pp->pv_simple,
25827c478bd9Sstevel@tonic-gate 			    sizeof (nwiftab->zone_nwif_physical));
25837c478bd9Sstevel@tonic-gate 			break;
2584de860bd9Sgfaden 		case PT_DEFROUTER:
2585de860bd9Sgfaden 			(void) strlcpy(nwiftab->zone_nwif_defrouter,
2586de860bd9Sgfaden 			    pp->pv_simple,
2587de860bd9Sgfaden 			    sizeof (nwiftab->zone_nwif_defrouter));
2588de860bd9Sgfaden 			break;
25897c478bd9Sstevel@tonic-gate 		default:
25907c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2591bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
25927c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
25937c478bd9Sstevel@tonic-gate 		}
25947c478bd9Sstevel@tonic-gate 	}
25957c478bd9Sstevel@tonic-gate 	if (fill_in_only)
25967c478bd9Sstevel@tonic-gate 		return (Z_OK);
25977c478bd9Sstevel@tonic-gate 	err = zonecfg_lookup_nwif(handle, nwiftab);
25987c478bd9Sstevel@tonic-gate 	return (err);
25997c478bd9Sstevel@tonic-gate }
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate static int
2602bbec428eSgjelinek fill_in_devtab(cmd_t *cmd, struct zone_devtab *devtab, boolean_t fill_in_only)
26037c478bd9Sstevel@tonic-gate {
26047c478bd9Sstevel@tonic-gate 	int err, i;
26057c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
26067c478bd9Sstevel@tonic-gate 
2607bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
26087c478bd9Sstevel@tonic-gate 		return (err);
26097c478bd9Sstevel@tonic-gate 
2610e193d1e6Svp 	bzero(devtab, sizeof (*devtab));
26117c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
26127c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
26137c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
26147c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2615bbec428eSgjelinek 			saw_error = B_TRUE;
26167c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
26177c478bd9Sstevel@tonic-gate 		}
26187c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
26197c478bd9Sstevel@tonic-gate 		case PT_MATCH:
26207c478bd9Sstevel@tonic-gate 			(void) strlcpy(devtab->zone_dev_match, pp->pv_simple,
26217c478bd9Sstevel@tonic-gate 			    sizeof (devtab->zone_dev_match));
26227c478bd9Sstevel@tonic-gate 			break;
26237c478bd9Sstevel@tonic-gate 		default:
26247c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2625bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
26267c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
26277c478bd9Sstevel@tonic-gate 		}
26287c478bd9Sstevel@tonic-gate 	}
26297c478bd9Sstevel@tonic-gate 	if (fill_in_only)
26307c478bd9Sstevel@tonic-gate 		return (Z_OK);
26317c478bd9Sstevel@tonic-gate 	err = zonecfg_lookup_dev(handle, devtab);
26327c478bd9Sstevel@tonic-gate 	return (err);
26337c478bd9Sstevel@tonic-gate }
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate static int
2636bbec428eSgjelinek fill_in_rctltab(cmd_t *cmd, struct zone_rctltab *rctltab,
2637bbec428eSgjelinek     boolean_t fill_in_only)
26387c478bd9Sstevel@tonic-gate {
26397c478bd9Sstevel@tonic-gate 	int err, i;
26407c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
26417c478bd9Sstevel@tonic-gate 
2642bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
26437c478bd9Sstevel@tonic-gate 		return (err);
26447c478bd9Sstevel@tonic-gate 
2645e193d1e6Svp 	bzero(rctltab, sizeof (*rctltab));
26467c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
26477c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
26487c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
26497c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2650bbec428eSgjelinek 			saw_error = B_TRUE;
26517c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
26527c478bd9Sstevel@tonic-gate 		}
26537c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
26547c478bd9Sstevel@tonic-gate 		case PT_NAME:
26557c478bd9Sstevel@tonic-gate 			(void) strlcpy(rctltab->zone_rctl_name, pp->pv_simple,
26567c478bd9Sstevel@tonic-gate 			    sizeof (rctltab->zone_rctl_name));
26577c478bd9Sstevel@tonic-gate 			break;
26587c478bd9Sstevel@tonic-gate 		default:
26597c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2660bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
26617c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
26627c478bd9Sstevel@tonic-gate 		}
26637c478bd9Sstevel@tonic-gate 	}
26647c478bd9Sstevel@tonic-gate 	if (fill_in_only)
26657c478bd9Sstevel@tonic-gate 		return (Z_OK);
26667c478bd9Sstevel@tonic-gate 	err = zonecfg_lookup_rctl(handle, rctltab);
26677c478bd9Sstevel@tonic-gate 	return (err);
26687c478bd9Sstevel@tonic-gate }
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate static int
2671bbec428eSgjelinek fill_in_attrtab(cmd_t *cmd, struct zone_attrtab *attrtab,
2672bbec428eSgjelinek     boolean_t fill_in_only)
26737c478bd9Sstevel@tonic-gate {
26747c478bd9Sstevel@tonic-gate 	int err, i;
26757c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
26767c478bd9Sstevel@tonic-gate 
2677bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
26787c478bd9Sstevel@tonic-gate 		return (err);
26797c478bd9Sstevel@tonic-gate 
2680e193d1e6Svp 	bzero(attrtab, sizeof (*attrtab));
26817c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
26827c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
26837c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
26847c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2685bbec428eSgjelinek 			saw_error = B_TRUE;
26867c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
26877c478bd9Sstevel@tonic-gate 		}
26887c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
26897c478bd9Sstevel@tonic-gate 		case PT_NAME:
26907c478bd9Sstevel@tonic-gate 			(void) strlcpy(attrtab->zone_attr_name, pp->pv_simple,
26917c478bd9Sstevel@tonic-gate 			    sizeof (attrtab->zone_attr_name));
26927c478bd9Sstevel@tonic-gate 			break;
26937c478bd9Sstevel@tonic-gate 		case PT_TYPE:
26947c478bd9Sstevel@tonic-gate 			(void) strlcpy(attrtab->zone_attr_type, pp->pv_simple,
26957c478bd9Sstevel@tonic-gate 			    sizeof (attrtab->zone_attr_type));
26967c478bd9Sstevel@tonic-gate 			break;
26977c478bd9Sstevel@tonic-gate 		case PT_VALUE:
26987c478bd9Sstevel@tonic-gate 			(void) strlcpy(attrtab->zone_attr_value, pp->pv_simple,
26997c478bd9Sstevel@tonic-gate 			    sizeof (attrtab->zone_attr_value));
27007c478bd9Sstevel@tonic-gate 			break;
27017c478bd9Sstevel@tonic-gate 		default:
27027c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2703bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
27047c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
27057c478bd9Sstevel@tonic-gate 		}
27067c478bd9Sstevel@tonic-gate 	}
27077c478bd9Sstevel@tonic-gate 	if (fill_in_only)
27087c478bd9Sstevel@tonic-gate 		return (Z_OK);
27097c478bd9Sstevel@tonic-gate 	err = zonecfg_lookup_attr(handle, attrtab);
27107c478bd9Sstevel@tonic-gate 	return (err);
27117c478bd9Sstevel@tonic-gate }
27127c478bd9Sstevel@tonic-gate 
2713fa9e4066Sahrens static int
2714bbec428eSgjelinek fill_in_dstab(cmd_t *cmd, struct zone_dstab *dstab, boolean_t fill_in_only)
2715fa9e4066Sahrens {
2716fa9e4066Sahrens 	int err, i;
2717fa9e4066Sahrens 	property_value_ptr_t pp;
2718fa9e4066Sahrens 
2719bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
2720fa9e4066Sahrens 		return (err);
2721fa9e4066Sahrens 
2722fa9e4066Sahrens 	dstab->zone_dataset_name[0] = '\0';
2723fa9e4066Sahrens 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
2724fa9e4066Sahrens 		pp = cmd->cmd_property_ptr[i];
2725fa9e4066Sahrens 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
2726fa9e4066Sahrens 			zerr(gettext("A simple value was expected here."));
2727bbec428eSgjelinek 			saw_error = B_TRUE;
2728fa9e4066Sahrens 			return (Z_INSUFFICIENT_SPEC);
2729fa9e4066Sahrens 		}
2730fa9e4066Sahrens 		switch (cmd->cmd_prop_name[i]) {
2731fa9e4066Sahrens 		case PT_NAME:
2732fa9e4066Sahrens 			(void) strlcpy(dstab->zone_dataset_name, pp->pv_simple,
2733fa9e4066Sahrens 			    sizeof (dstab->zone_dataset_name));
2734fa9e4066Sahrens 			break;
2735fa9e4066Sahrens 		default:
2736fa9e4066Sahrens 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2737bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
2738fa9e4066Sahrens 			return (Z_INSUFFICIENT_SPEC);
2739fa9e4066Sahrens 		}
2740fa9e4066Sahrens 	}
2741fa9e4066Sahrens 	if (fill_in_only)
2742fa9e4066Sahrens 		return (Z_OK);
2743fa9e4066Sahrens 	return (zonecfg_lookup_ds(handle, dstab));
2744fa9e4066Sahrens }
2745fa9e4066Sahrens 
27467c478bd9Sstevel@tonic-gate static void
27470209230bSgjelinek remove_aliased_rctl(int type, char *name)
27487c478bd9Sstevel@tonic-gate {
27490209230bSgjelinek 	int err;
27500209230bSgjelinek 	uint64_t tmp;
27517c478bd9Sstevel@tonic-gate 
27520209230bSgjelinek 	if ((err = zonecfg_get_aliased_rctl(handle, name, &tmp)) != Z_OK) {
27530209230bSgjelinek 		zerr("%s %s: %s", cmd_to_str(CMD_CLEAR), pt_to_str(type),
27540209230bSgjelinek 		    zonecfg_strerror(err));
2755bbec428eSgjelinek 		saw_error = B_TRUE;
27567c478bd9Sstevel@tonic-gate 		return;
27577c478bd9Sstevel@tonic-gate 	}
27580209230bSgjelinek 	if ((err = zonecfg_rm_aliased_rctl(handle, name)) != Z_OK) {
27590209230bSgjelinek 		zerr("%s %s: %s", cmd_to_str(CMD_CLEAR), pt_to_str(type),
27600209230bSgjelinek 		    zonecfg_strerror(err));
2761bbec428eSgjelinek 		saw_error = B_TRUE;
27620209230bSgjelinek 	} else {
2763bbec428eSgjelinek 		need_to_commit = B_TRUE;
27640209230bSgjelinek 	}
27650209230bSgjelinek }
27667c478bd9Sstevel@tonic-gate 
27670209230bSgjelinek static boolean_t
27680209230bSgjelinek prompt_remove_resource(cmd_t *cmd, char *rsrc)
27690209230bSgjelinek {
27700209230bSgjelinek 	int num;
27710209230bSgjelinek 	int answer;
27720209230bSgjelinek 	int arg;
27730209230bSgjelinek 	boolean_t force = B_FALSE;
27740209230bSgjelinek 	char prompt[128];
2775bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
27760209230bSgjelinek 
27770209230bSgjelinek 	optind = 0;
27780209230bSgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "F")) != EOF) {
27790209230bSgjelinek 		switch (arg) {
27800209230bSgjelinek 		case 'F':
27810209230bSgjelinek 			force = B_TRUE;
27820209230bSgjelinek 			break;
27830209230bSgjelinek 		default:
2784bbec428eSgjelinek 			arg_err = B_TRUE;
27857ec75eb8Sgjelinek 			break;
27860209230bSgjelinek 		}
27870209230bSgjelinek 	}
27887ec75eb8Sgjelinek 	if (arg_err)
27897ec75eb8Sgjelinek 		return (B_FALSE);
27907ec75eb8Sgjelinek 
27910209230bSgjelinek 
27920209230bSgjelinek 	num = zonecfg_num_resources(handle, rsrc);
27930209230bSgjelinek 
27940209230bSgjelinek 	if (num == 0) {
27950209230bSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, cmd->cmd_res_type, Z_NO_ENTRY,
2796bbec428eSgjelinek 		    B_TRUE);
27970209230bSgjelinek 		return (B_FALSE);
27980209230bSgjelinek 	}
27990209230bSgjelinek 	if (num > 1 && !force) {
28000209230bSgjelinek 		if (!interactive_mode) {
28010209230bSgjelinek 			zerr(gettext("There are multiple instances of this "
28020209230bSgjelinek 			    "resource.  Either qualify the resource to\n"
28030209230bSgjelinek 			    "remove a single instance or use the -F option to "
28040209230bSgjelinek 			    "remove all instances."));
2805bbec428eSgjelinek 			saw_error = B_TRUE;
28060209230bSgjelinek 			return (B_FALSE);
28070209230bSgjelinek 		}
28080209230bSgjelinek 		(void) snprintf(prompt, sizeof (prompt), gettext(
28090209230bSgjelinek 		    "Are you sure you want to remove ALL '%s' resources"),
28100209230bSgjelinek 		    rsrc);
2811bbec428eSgjelinek 		answer = ask_yesno(B_FALSE, prompt);
28120209230bSgjelinek 		if (answer == -1) {
28130209230bSgjelinek 			zerr(gettext("Resource incomplete."));
28140209230bSgjelinek 			return (B_FALSE);
28150209230bSgjelinek 		}
28160209230bSgjelinek 		if (answer != 1)
28170209230bSgjelinek 			return (B_FALSE);
28180209230bSgjelinek 	}
28190209230bSgjelinek 	return (B_TRUE);
28200209230bSgjelinek }
28210209230bSgjelinek 
28220209230bSgjelinek static void
28230209230bSgjelinek remove_fs(cmd_t *cmd)
28240209230bSgjelinek {
28250209230bSgjelinek 	int err;
28260209230bSgjelinek 
28270209230bSgjelinek 	/* traditional, qualified fs removal */
28280209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
28290209230bSgjelinek 		struct zone_fstab fstab;
28307c478bd9Sstevel@tonic-gate 
2831bbec428eSgjelinek 		if ((err = fill_in_fstab(cmd, &fstab, B_FALSE)) != Z_OK) {
2832bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_FS, err, B_TRUE);
28337c478bd9Sstevel@tonic-gate 			return;
28347c478bd9Sstevel@tonic-gate 		}
28357c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_delete_filesystem(handle, &fstab)) != Z_OK)
2836bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_FS, err, B_TRUE);
28377c478bd9Sstevel@tonic-gate 		else
2838bbec428eSgjelinek 			need_to_commit = B_TRUE;
28397c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
28407c478bd9Sstevel@tonic-gate 		return;
28410209230bSgjelinek 	}
28420209230bSgjelinek 
28430209230bSgjelinek 	/*
28440209230bSgjelinek 	 * unqualified fs removal.  remove all fs's but prompt if more
28450209230bSgjelinek 	 * than one.
28460209230bSgjelinek 	 */
28470209230bSgjelinek 	if (!prompt_remove_resource(cmd, "fs"))
28480209230bSgjelinek 		return;
28490209230bSgjelinek 
28500209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "fs")) != Z_OK)
2851bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_FS, err, B_TRUE);
28520209230bSgjelinek 	else
2853bbec428eSgjelinek 		need_to_commit = B_TRUE;
28540209230bSgjelinek }
28550209230bSgjelinek 
28560209230bSgjelinek static void
28570209230bSgjelinek remove_ipd(cmd_t *cmd)
28580209230bSgjelinek {
28590209230bSgjelinek 	int err;
28600209230bSgjelinek 
28610209230bSgjelinek 	if (state_atleast(ZONE_STATE_INSTALLED)) {
28620209230bSgjelinek 		zerr(gettext("Zone %s already installed; %s %s not allowed."),
28630209230bSgjelinek 		    zone, cmd_to_str(CMD_REMOVE), rt_to_str(RT_IPD));
28640209230bSgjelinek 		return;
28650209230bSgjelinek 	}
28660209230bSgjelinek 
28670209230bSgjelinek 	/* traditional, qualified ipd removal */
28680209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
28690209230bSgjelinek 		struct zone_fstab fstab;
28700209230bSgjelinek 
2871bbec428eSgjelinek 		if ((err = fill_in_ipdtab(cmd, &fstab, B_FALSE)) != Z_OK) {
2872bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_IPD, err, B_TRUE);
28737c478bd9Sstevel@tonic-gate 			return;
28747c478bd9Sstevel@tonic-gate 		}
28757c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_delete_ipd(handle, &fstab)) != Z_OK)
2876bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_IPD, err, B_TRUE);
28777c478bd9Sstevel@tonic-gate 		else
2878bbec428eSgjelinek 			need_to_commit = B_TRUE;
28797c478bd9Sstevel@tonic-gate 		return;
28800209230bSgjelinek 	}
28810209230bSgjelinek 
28820209230bSgjelinek 	/*
28830209230bSgjelinek 	 * unqualified ipd removal.  remove all ipds but prompt if more
28840209230bSgjelinek 	 * than one.
28850209230bSgjelinek 	 */
28860209230bSgjelinek 	if (!prompt_remove_resource(cmd, "inherit-pkg-dir"))
28870209230bSgjelinek 		return;
28880209230bSgjelinek 
28890209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "inherit-pkg-dir"))
28900209230bSgjelinek 	    != Z_OK)
2891bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_IPD, err, B_TRUE);
28920209230bSgjelinek 	else
2893bbec428eSgjelinek 		need_to_commit = B_TRUE;
28940209230bSgjelinek }
28950209230bSgjelinek 
28960209230bSgjelinek static void
28970209230bSgjelinek remove_net(cmd_t *cmd)
28980209230bSgjelinek {
28990209230bSgjelinek 	int err;
29000209230bSgjelinek 
29010209230bSgjelinek 	/* traditional, qualified net removal */
29020209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
29030209230bSgjelinek 		struct zone_nwiftab nwiftab;
29040209230bSgjelinek 
2905bbec428eSgjelinek 		if ((err = fill_in_nwiftab(cmd, &nwiftab, B_FALSE)) != Z_OK) {
2906bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_NET, err, B_TRUE);
29077c478bd9Sstevel@tonic-gate 			return;
29087c478bd9Sstevel@tonic-gate 		}
29097c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_delete_nwif(handle, &nwiftab)) != Z_OK)
2910bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_NET, err, B_TRUE);
29117c478bd9Sstevel@tonic-gate 		else
2912bbec428eSgjelinek 			need_to_commit = B_TRUE;
29137c478bd9Sstevel@tonic-gate 		return;
29140209230bSgjelinek 	}
29150209230bSgjelinek 
29160209230bSgjelinek 	/*
29170209230bSgjelinek 	 * unqualified net removal.  remove all nets but prompt if more
29180209230bSgjelinek 	 * than one.
29190209230bSgjelinek 	 */
29200209230bSgjelinek 	if (!prompt_remove_resource(cmd, "net"))
29210209230bSgjelinek 		return;
29220209230bSgjelinek 
29230209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "net")) != Z_OK)
2924bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_NET, err, B_TRUE);
29250209230bSgjelinek 	else
2926bbec428eSgjelinek 		need_to_commit = B_TRUE;
29270209230bSgjelinek }
29280209230bSgjelinek 
29290209230bSgjelinek static void
29300209230bSgjelinek remove_device(cmd_t *cmd)
29310209230bSgjelinek {
29320209230bSgjelinek 	int err;
29330209230bSgjelinek 
29340209230bSgjelinek 	/* traditional, qualified device removal */
29350209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
29360209230bSgjelinek 		struct zone_devtab devtab;
29370209230bSgjelinek 
2938bbec428eSgjelinek 		if ((err = fill_in_devtab(cmd, &devtab, B_FALSE)) != Z_OK) {
2939bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_DEVICE, err, B_TRUE);
29407c478bd9Sstevel@tonic-gate 			return;
29417c478bd9Sstevel@tonic-gate 		}
29427c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_delete_dev(handle, &devtab)) != Z_OK)
2943bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_DEVICE, err, B_TRUE);
29447c478bd9Sstevel@tonic-gate 		else
2945bbec428eSgjelinek 			need_to_commit = B_TRUE;
29467c478bd9Sstevel@tonic-gate 		return;
29470209230bSgjelinek 	}
29480209230bSgjelinek 
29490209230bSgjelinek 	/*
29500209230bSgjelinek 	 * unqualified device removal.  remove all devices but prompt if more
29510209230bSgjelinek 	 * than one.
29520209230bSgjelinek 	 */
29530209230bSgjelinek 	if (!prompt_remove_resource(cmd, "device"))
29547c478bd9Sstevel@tonic-gate 		return;
29550209230bSgjelinek 
29560209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "device")) != Z_OK)
2957bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_DEVICE, err, B_TRUE);
29580209230bSgjelinek 	else
2959bbec428eSgjelinek 		need_to_commit = B_TRUE;
29600209230bSgjelinek }
29610209230bSgjelinek 
29620209230bSgjelinek static void
29630209230bSgjelinek remove_attr(cmd_t *cmd)
29640209230bSgjelinek {
29650209230bSgjelinek 	int err;
29660209230bSgjelinek 
29670209230bSgjelinek 	/* traditional, qualified attr removal */
29680209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
29690209230bSgjelinek 		struct zone_attrtab attrtab;
29700209230bSgjelinek 
2971bbec428eSgjelinek 		if ((err = fill_in_attrtab(cmd, &attrtab, B_FALSE)) != Z_OK) {
2972bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_ATTR, err, B_TRUE);
29737c478bd9Sstevel@tonic-gate 			return;
29747c478bd9Sstevel@tonic-gate 		}
29757c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_delete_attr(handle, &attrtab)) != Z_OK)
2976bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_ATTR, err, B_TRUE);
29777c478bd9Sstevel@tonic-gate 		else
2978bbec428eSgjelinek 			need_to_commit = B_TRUE;
29797c478bd9Sstevel@tonic-gate 		return;
29800209230bSgjelinek 	}
29810209230bSgjelinek 
29820209230bSgjelinek 	/*
29830209230bSgjelinek 	 * unqualified attr removal.  remove all attrs but prompt if more
29840209230bSgjelinek 	 * than one.
29850209230bSgjelinek 	 */
29860209230bSgjelinek 	if (!prompt_remove_resource(cmd, "attr"))
29870209230bSgjelinek 		return;
29880209230bSgjelinek 
29890209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "attr")) != Z_OK)
2990bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_ATTR, err, B_TRUE);
29910209230bSgjelinek 	else
2992bbec428eSgjelinek 		need_to_commit = B_TRUE;
29930209230bSgjelinek }
29940209230bSgjelinek 
29950209230bSgjelinek static void
29960209230bSgjelinek remove_dataset(cmd_t *cmd)
29970209230bSgjelinek {
29980209230bSgjelinek 	int err;
29990209230bSgjelinek 
30000209230bSgjelinek 	/* traditional, qualified dataset removal */
30010209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
30020209230bSgjelinek 		struct zone_dstab dstab;
30030209230bSgjelinek 
3004bbec428eSgjelinek 		if ((err = fill_in_dstab(cmd, &dstab, B_FALSE)) != Z_OK) {
3005bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_DATASET, err, B_TRUE);
3006fa9e4066Sahrens 			return;
3007fa9e4066Sahrens 		}
3008fa9e4066Sahrens 		if ((err = zonecfg_delete_ds(handle, &dstab)) != Z_OK)
3009bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_DATASET, err, B_TRUE);
3010fa9e4066Sahrens 		else
3011bbec428eSgjelinek 			need_to_commit = B_TRUE;
3012fa9e4066Sahrens 		return;
30137c478bd9Sstevel@tonic-gate 	}
30140209230bSgjelinek 
30150209230bSgjelinek 	/*
30160209230bSgjelinek 	 * unqualified dataset removal.  remove all datasets but prompt if more
30170209230bSgjelinek 	 * than one.
30180209230bSgjelinek 	 */
30190209230bSgjelinek 	if (!prompt_remove_resource(cmd, "dataset"))
30200209230bSgjelinek 		return;
30210209230bSgjelinek 
30220209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "dataset")) != Z_OK)
3023bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_DATASET, err, B_TRUE);
30240209230bSgjelinek 	else
3025bbec428eSgjelinek 		need_to_commit = B_TRUE;
30260209230bSgjelinek }
30270209230bSgjelinek 
30280209230bSgjelinek static void
30290209230bSgjelinek remove_rctl(cmd_t *cmd)
30300209230bSgjelinek {
30310209230bSgjelinek 	int err;
30320209230bSgjelinek 
30330209230bSgjelinek 	/* traditional, qualified rctl removal */
30340209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
30350209230bSgjelinek 		struct zone_rctltab rctltab;
30360209230bSgjelinek 
3037bbec428eSgjelinek 		if ((err = fill_in_rctltab(cmd, &rctltab, B_FALSE)) != Z_OK) {
3038bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_RCTL, err, B_TRUE);
30390209230bSgjelinek 			return;
30400209230bSgjelinek 		}
30410209230bSgjelinek 		if ((err = zonecfg_delete_rctl(handle, &rctltab)) != Z_OK)
3042bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_RCTL, err, B_TRUE);
30430209230bSgjelinek 		else
3044bbec428eSgjelinek 			need_to_commit = B_TRUE;
30450209230bSgjelinek 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30460209230bSgjelinek 		return;
30470209230bSgjelinek 	}
30480209230bSgjelinek 
30490209230bSgjelinek 	/*
30500209230bSgjelinek 	 * unqualified rctl removal.  remove all rctls but prompt if more
30510209230bSgjelinek 	 * than one.
30520209230bSgjelinek 	 */
30530209230bSgjelinek 	if (!prompt_remove_resource(cmd, "rctl"))
30540209230bSgjelinek 		return;
30550209230bSgjelinek 
30560209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "rctl")) != Z_OK)
3057bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_RCTL, err, B_TRUE);
30580209230bSgjelinek 	else
3059bbec428eSgjelinek 		need_to_commit = B_TRUE;
30600209230bSgjelinek }
30610209230bSgjelinek 
30620209230bSgjelinek static void
30630209230bSgjelinek remove_pset()
30640209230bSgjelinek {
30650209230bSgjelinek 	int err;
30660209230bSgjelinek 	struct zone_psettab psettab;
30670209230bSgjelinek 
30680209230bSgjelinek 	if ((err = zonecfg_lookup_pset(handle, &psettab)) != Z_OK) {
3069bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_DCPU, err, B_TRUE);
30700209230bSgjelinek 		return;
30710209230bSgjelinek 	}
30720209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
3073bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_DCPU, err, B_TRUE);
30740209230bSgjelinek 	else
3075bbec428eSgjelinek 		need_to_commit = B_TRUE;
30760209230bSgjelinek }
30770209230bSgjelinek 
3078c97ad5cdSakolb static void
3079c97ad5cdSakolb remove_pcap()
3080c97ad5cdSakolb {
3081c97ad5cdSakolb 	int err;
3082c97ad5cdSakolb 	uint64_t tmp;
3083c97ad5cdSakolb 
3084c97ad5cdSakolb 	if (zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &tmp) != Z_OK) {
3085c97ad5cdSakolb 		zerr("%s %s: %s", cmd_to_str(CMD_REMOVE), rt_to_str(RT_PCAP),
3086c97ad5cdSakolb 		    zonecfg_strerror(Z_NO_RESOURCE_TYPE));
3087bbec428eSgjelinek 		saw_error = B_TRUE;
3088c97ad5cdSakolb 		return;
3089c97ad5cdSakolb 	}
3090c97ad5cdSakolb 
3091c97ad5cdSakolb 	if ((err = zonecfg_rm_aliased_rctl(handle, ALIAS_CPUCAP)) != Z_OK)
3092bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_PCAP, err, B_TRUE);
3093c97ad5cdSakolb 	else
3094bbec428eSgjelinek 		need_to_commit = B_TRUE;
3095c97ad5cdSakolb }
3096c97ad5cdSakolb 
30970209230bSgjelinek static void
30980209230bSgjelinek remove_mcap()
30990209230bSgjelinek {
31000209230bSgjelinek 	int err, res1, res2, res3;
31010209230bSgjelinek 	uint64_t tmp;
31020209230bSgjelinek 	struct zone_mcaptab mcaptab;
31030209230bSgjelinek 	boolean_t revert = B_FALSE;
31040209230bSgjelinek 
31050209230bSgjelinek 	res1 = zonecfg_lookup_mcap(handle, &mcaptab);
31060209230bSgjelinek 	res2 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP, &tmp);
31070209230bSgjelinek 	res3 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM, &tmp);
31080209230bSgjelinek 
31090209230bSgjelinek 	/* if none of these exist, there is no resource to remove */
31100209230bSgjelinek 	if (res1 != Z_OK && res2 != Z_OK && res3 != Z_OK) {
31110209230bSgjelinek 		zerr("%s %s: %s", cmd_to_str(CMD_REMOVE), rt_to_str(RT_MCAP),
31120209230bSgjelinek 		    zonecfg_strerror(Z_NO_RESOURCE_TYPE));
3113bbec428eSgjelinek 		saw_error = B_TRUE;
31140209230bSgjelinek 		return;
31150209230bSgjelinek 	}
31160209230bSgjelinek 	if (res1 == Z_OK) {
31170209230bSgjelinek 		if ((err = zonecfg_delete_mcap(handle)) != Z_OK) {
3118bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_MCAP, err, B_TRUE);
31190209230bSgjelinek 			revert = B_TRUE;
31200209230bSgjelinek 		} else {
3121bbec428eSgjelinek 			need_to_commit = B_TRUE;
31220209230bSgjelinek 		}
31230209230bSgjelinek 	}
31240209230bSgjelinek 	if (res2 == Z_OK) {
31250209230bSgjelinek 		if ((err = zonecfg_rm_aliased_rctl(handle, ALIAS_MAXSWAP))
31260209230bSgjelinek 		    != Z_OK) {
3127bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_MCAP, err, B_TRUE);
31280209230bSgjelinek 			revert = B_TRUE;
31290209230bSgjelinek 		} else {
3130bbec428eSgjelinek 			need_to_commit = B_TRUE;
31310209230bSgjelinek 		}
31320209230bSgjelinek 	}
31330209230bSgjelinek 	if (res3 == Z_OK) {
31340209230bSgjelinek 		if ((err = zonecfg_rm_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM))
31350209230bSgjelinek 		    != Z_OK) {
3136bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_MCAP, err, B_TRUE);
31370209230bSgjelinek 			revert = B_TRUE;
31380209230bSgjelinek 		} else {
3139bbec428eSgjelinek 			need_to_commit = B_TRUE;
31400209230bSgjelinek 		}
31410209230bSgjelinek 	}
31420209230bSgjelinek 
31430209230bSgjelinek 	if (revert)
3144bbec428eSgjelinek 		need_to_commit = B_FALSE;
31450209230bSgjelinek }
31460209230bSgjelinek 
31470209230bSgjelinek static void
31480209230bSgjelinek remove_resource(cmd_t *cmd)
31490209230bSgjelinek {
31500209230bSgjelinek 	int type;
31510209230bSgjelinek 	int arg;
3152bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
31530209230bSgjelinek 
31540209230bSgjelinek 	if ((type = cmd->cmd_res_type) == RT_UNKNOWN) {
3155bbec428eSgjelinek 		long_usage(CMD_REMOVE, B_TRUE);
31560209230bSgjelinek 		return;
31570209230bSgjelinek 	}
31580209230bSgjelinek 
31590209230bSgjelinek 	optind = 0;
31600209230bSgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?F")) != EOF) {
31610209230bSgjelinek 		switch (arg) {
31620209230bSgjelinek 		case '?':
31630209230bSgjelinek 			longer_usage(CMD_REMOVE);
3164bbec428eSgjelinek 			arg_err = B_TRUE;
31657ec75eb8Sgjelinek 			break;
31660209230bSgjelinek 		case 'F':
31670209230bSgjelinek 			break;
31680209230bSgjelinek 		default:
31690209230bSgjelinek 			short_usage(CMD_REMOVE);
3170bbec428eSgjelinek 			arg_err = B_TRUE;
31717ec75eb8Sgjelinek 			break;
31720209230bSgjelinek 		}
31730209230bSgjelinek 	}
31747ec75eb8Sgjelinek 	if (arg_err)
31757ec75eb8Sgjelinek 		return;
31760209230bSgjelinek 
3177bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
31780209230bSgjelinek 		return;
31790209230bSgjelinek 
31800209230bSgjelinek 	switch (type) {
31810209230bSgjelinek 	case RT_FS:
31820209230bSgjelinek 		remove_fs(cmd);
31830209230bSgjelinek 		return;
31840209230bSgjelinek 	case RT_IPD:
31850209230bSgjelinek 		remove_ipd(cmd);
31860209230bSgjelinek 		return;
31870209230bSgjelinek 	case RT_NET:
31880209230bSgjelinek 		remove_net(cmd);
31890209230bSgjelinek 		return;
31900209230bSgjelinek 	case RT_DEVICE:
31910209230bSgjelinek 		remove_device(cmd);
31920209230bSgjelinek 		return;
31930209230bSgjelinek 	case RT_RCTL:
31940209230bSgjelinek 		remove_rctl(cmd);
31950209230bSgjelinek 		return;
31960209230bSgjelinek 	case RT_ATTR:
31970209230bSgjelinek 		remove_attr(cmd);
31980209230bSgjelinek 		return;
31990209230bSgjelinek 	case RT_DATASET:
32000209230bSgjelinek 		remove_dataset(cmd);
32010209230bSgjelinek 		return;
32020209230bSgjelinek 	case RT_DCPU:
32030209230bSgjelinek 		remove_pset();
32040209230bSgjelinek 		return;
3205c97ad5cdSakolb 	case RT_PCAP:
3206c97ad5cdSakolb 		remove_pcap();
3207c97ad5cdSakolb 		return;
32080209230bSgjelinek 	case RT_MCAP:
32090209230bSgjelinek 		remove_mcap();
32100209230bSgjelinek 		return;
32110209230bSgjelinek 	default:
3212bbec428eSgjelinek 		zone_perror(rt_to_str(type), Z_NO_RESOURCE_TYPE, B_TRUE);
3213bbec428eSgjelinek 		long_usage(CMD_REMOVE, B_TRUE);
3214bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
32150209230bSgjelinek 		return;
32160209230bSgjelinek 	}
32170209230bSgjelinek }
32187c478bd9Sstevel@tonic-gate 
32197c478bd9Sstevel@tonic-gate static void
32207c478bd9Sstevel@tonic-gate remove_property(cmd_t *cmd)
32217c478bd9Sstevel@tonic-gate {
32227c478bd9Sstevel@tonic-gate 	char *prop_id;
32237c478bd9Sstevel@tonic-gate 	int err, res_type, prop_type;
32247c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
32257c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *rctlvaltab;
32267c478bd9Sstevel@tonic-gate 	complex_property_ptr_t cx;
32277c478bd9Sstevel@tonic-gate 
32287c478bd9Sstevel@tonic-gate 	res_type = resource_scope;
32297c478bd9Sstevel@tonic-gate 	prop_type = cmd->cmd_prop_name[0];
32307c478bd9Sstevel@tonic-gate 	if (res_type == RT_UNKNOWN || prop_type == PT_UNKNOWN) {
3231bbec428eSgjelinek 		long_usage(CMD_REMOVE, B_TRUE);
32327c478bd9Sstevel@tonic-gate 		return;
32337c478bd9Sstevel@tonic-gate 	}
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 	if (cmd->cmd_prop_nv_pairs != 1) {
3236bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
32377c478bd9Sstevel@tonic-gate 		return;
32387c478bd9Sstevel@tonic-gate 	}
32397c478bd9Sstevel@tonic-gate 
3240bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
32417c478bd9Sstevel@tonic-gate 		return;
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	switch (res_type) {
32447c478bd9Sstevel@tonic-gate 	case RT_FS:
32457c478bd9Sstevel@tonic-gate 		if (prop_type != PT_OPTIONS) {
32467c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
3247bbec428eSgjelinek 			    B_TRUE);
3248bbec428eSgjelinek 			long_usage(CMD_REMOVE, B_TRUE);
3249bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
32507c478bd9Sstevel@tonic-gate 			return;
32517c478bd9Sstevel@tonic-gate 		}
32527c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[0];
32537c478bd9Sstevel@tonic-gate 		if (pp->pv_type == PROP_VAL_COMPLEX) {
32547c478bd9Sstevel@tonic-gate 			zerr(gettext("A %s or %s value was expected here."),
32557c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_SIMPLE),
32567c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_LIST));
3257bbec428eSgjelinek 			saw_error = B_TRUE;
32587c478bd9Sstevel@tonic-gate 			return;
32597c478bd9Sstevel@tonic-gate 		}
32607c478bd9Sstevel@tonic-gate 		if (pp->pv_type == PROP_VAL_SIMPLE) {
32617c478bd9Sstevel@tonic-gate 			if (pp->pv_simple == NULL) {
3262bbec428eSgjelinek 				long_usage(CMD_ADD, B_TRUE);
32637c478bd9Sstevel@tonic-gate 				return;
32647c478bd9Sstevel@tonic-gate 			}
32657c478bd9Sstevel@tonic-gate 			prop_id = pp->pv_simple;
32667c478bd9Sstevel@tonic-gate 			err = zonecfg_remove_fs_option(&in_progress_fstab,
32677c478bd9Sstevel@tonic-gate 			    prop_id);
32687c478bd9Sstevel@tonic-gate 			if (err != Z_OK)
3269bbec428eSgjelinek 				zone_perror(pt_to_str(prop_type), err, B_TRUE);
32707c478bd9Sstevel@tonic-gate 		} else {
32717c478bd9Sstevel@tonic-gate 			list_property_ptr_t list;
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 			for (list = pp->pv_list; list != NULL;
32747c478bd9Sstevel@tonic-gate 			    list = list->lp_next) {
32757c478bd9Sstevel@tonic-gate 				prop_id = list->lp_simple;
32767c478bd9Sstevel@tonic-gate 				if (prop_id == NULL)
32777c478bd9Sstevel@tonic-gate 					break;
32787c478bd9Sstevel@tonic-gate 				err = zonecfg_remove_fs_option(
32797c478bd9Sstevel@tonic-gate 				    &in_progress_fstab, prop_id);
32807c478bd9Sstevel@tonic-gate 				if (err != Z_OK)
32817c478bd9Sstevel@tonic-gate 					zone_perror(pt_to_str(prop_type), err,
3282bbec428eSgjelinek 					    B_TRUE);
32837c478bd9Sstevel@tonic-gate 			}
32847c478bd9Sstevel@tonic-gate 		}
32857c478bd9Sstevel@tonic-gate 		return;
32867c478bd9Sstevel@tonic-gate 	case RT_RCTL:
32877c478bd9Sstevel@tonic-gate 		if (prop_type != PT_VALUE) {
32887c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
3289bbec428eSgjelinek 			    B_TRUE);
3290bbec428eSgjelinek 			long_usage(CMD_REMOVE, B_TRUE);
3291bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
32927c478bd9Sstevel@tonic-gate 			return;
32937c478bd9Sstevel@tonic-gate 		}
32947c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[0];
32957c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_COMPLEX) {
32967c478bd9Sstevel@tonic-gate 			zerr(gettext("A %s value was expected here."),
32977c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_COMPLEX));
3298bbec428eSgjelinek 			saw_error = B_TRUE;
32997c478bd9Sstevel@tonic-gate 			return;
33007c478bd9Sstevel@tonic-gate 		}
33017c478bd9Sstevel@tonic-gate 		if ((rctlvaltab = alloc_rctlvaltab()) == NULL) {
3302bbec428eSgjelinek 			zone_perror(zone, Z_NOMEM, B_TRUE);
33037c478bd9Sstevel@tonic-gate 			exit(Z_ERR);
33047c478bd9Sstevel@tonic-gate 		}
33057c478bd9Sstevel@tonic-gate 		for (cx = pp->pv_complex; cx != NULL; cx = cx->cp_next) {
33067c478bd9Sstevel@tonic-gate 			switch (cx->cp_type) {
33077c478bd9Sstevel@tonic-gate 			case PT_PRIV:
33087c478bd9Sstevel@tonic-gate 				(void) strlcpy(rctlvaltab->zone_rctlval_priv,
33097c478bd9Sstevel@tonic-gate 				    cx->cp_value,
33107c478bd9Sstevel@tonic-gate 				    sizeof (rctlvaltab->zone_rctlval_priv));
33117c478bd9Sstevel@tonic-gate 				break;
33127c478bd9Sstevel@tonic-gate 			case PT_LIMIT:
33137c478bd9Sstevel@tonic-gate 				(void) strlcpy(rctlvaltab->zone_rctlval_limit,
33147c478bd9Sstevel@tonic-gate 				    cx->cp_value,
33157c478bd9Sstevel@tonic-gate 				    sizeof (rctlvaltab->zone_rctlval_limit));
33167c478bd9Sstevel@tonic-gate 				break;
33177c478bd9Sstevel@tonic-gate 			case PT_ACTION:
33187c478bd9Sstevel@tonic-gate 				(void) strlcpy(rctlvaltab->zone_rctlval_action,
33197c478bd9Sstevel@tonic-gate 				    cx->cp_value,
33207c478bd9Sstevel@tonic-gate 				    sizeof (rctlvaltab->zone_rctlval_action));
33217c478bd9Sstevel@tonic-gate 				break;
33227c478bd9Sstevel@tonic-gate 			default:
33237c478bd9Sstevel@tonic-gate 				zone_perror(pt_to_str(prop_type),
3324bbec428eSgjelinek 				    Z_NO_PROPERTY_TYPE, B_TRUE);
3325bbec428eSgjelinek 				long_usage(CMD_ADD, B_TRUE);
3326bbec428eSgjelinek 				usage(B_FALSE, HELP_PROPS);
33277c478bd9Sstevel@tonic-gate 				zonecfg_free_rctl_value_list(rctlvaltab);
33287c478bd9Sstevel@tonic-gate 				return;
33297c478bd9Sstevel@tonic-gate 			}
33307c478bd9Sstevel@tonic-gate 		}
33317c478bd9Sstevel@tonic-gate 		rctlvaltab->zone_rctlval_next = NULL;
33327c478bd9Sstevel@tonic-gate 		err = zonecfg_remove_rctl_value(&in_progress_rctltab,
33337c478bd9Sstevel@tonic-gate 		    rctlvaltab);
33347c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
3335bbec428eSgjelinek 			zone_perror(pt_to_str(prop_type), err, B_TRUE);
33367c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctlvaltab);
33377c478bd9Sstevel@tonic-gate 		return;
3338de860bd9Sgfaden 	case RT_NET:
3339de860bd9Sgfaden 		if (prop_type != PT_DEFROUTER) {
3340de860bd9Sgfaden 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
3341bbec428eSgjelinek 			    B_TRUE);
3342bbec428eSgjelinek 			long_usage(CMD_REMOVE, B_TRUE);
3343bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
3344de860bd9Sgfaden 			return;
3345de860bd9Sgfaden 		} else {
3346de860bd9Sgfaden 			bzero(&in_progress_nwiftab.zone_nwif_defrouter,
3347de860bd9Sgfaden 			    sizeof (in_progress_nwiftab.zone_nwif_defrouter));
3348de860bd9Sgfaden 			return;
3349de860bd9Sgfaden 		}
33507c478bd9Sstevel@tonic-gate 	default:
3351bbec428eSgjelinek 		zone_perror(rt_to_str(res_type), Z_NO_RESOURCE_TYPE, B_TRUE);
3352bbec428eSgjelinek 		long_usage(CMD_REMOVE, B_TRUE);
3353bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
33547c478bd9Sstevel@tonic-gate 		return;
33557c478bd9Sstevel@tonic-gate 	}
33567c478bd9Sstevel@tonic-gate }
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate void
33597c478bd9Sstevel@tonic-gate remove_func(cmd_t *cmd)
33607c478bd9Sstevel@tonic-gate {
33617c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_REMOVE))
33627c478bd9Sstevel@tonic-gate 		return;
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
33657c478bd9Sstevel@tonic-gate 
33660209230bSgjelinek 	if (global_scope) {
33670209230bSgjelinek 		if (gz_invalid_resource(cmd->cmd_res_type)) {
33680209230bSgjelinek 			zerr(gettext("%s is not a valid resource for the "
33690209230bSgjelinek 			    "global zone."), rt_to_str(cmd->cmd_res_type));
3370bbec428eSgjelinek 			saw_error = B_TRUE;
33710209230bSgjelinek 			return;
33720209230bSgjelinek 		}
33737c478bd9Sstevel@tonic-gate 		remove_resource(cmd);
33740209230bSgjelinek 	} else {
33757c478bd9Sstevel@tonic-gate 		remove_property(cmd);
33760209230bSgjelinek 	}
33770209230bSgjelinek }
33780209230bSgjelinek 
33790209230bSgjelinek static void
33800209230bSgjelinek clear_property(cmd_t *cmd)
33810209230bSgjelinek {
33820209230bSgjelinek 	int res_type, prop_type;
33830209230bSgjelinek 
33840209230bSgjelinek 	res_type = resource_scope;
33850209230bSgjelinek 	prop_type = cmd->cmd_res_type;
33860209230bSgjelinek 	if (res_type == RT_UNKNOWN || prop_type == PT_UNKNOWN) {
3387bbec428eSgjelinek 		long_usage(CMD_CLEAR, B_TRUE);
33880209230bSgjelinek 		return;
33890209230bSgjelinek 	}
33900209230bSgjelinek 
3391bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
33920209230bSgjelinek 		return;
33930209230bSgjelinek 
33940209230bSgjelinek 	switch (res_type) {
33950209230bSgjelinek 	case RT_FS:
33960209230bSgjelinek 		if (prop_type == PT_RAW) {
33970209230bSgjelinek 			in_progress_fstab.zone_fs_raw[0] = '\0';
3398bbec428eSgjelinek 			need_to_commit = B_TRUE;
33990209230bSgjelinek 			return;
34000209230bSgjelinek 		}
34010209230bSgjelinek 		break;
34020209230bSgjelinek 	case RT_DCPU:
34030209230bSgjelinek 		if (prop_type == PT_IMPORTANCE) {
34040209230bSgjelinek 			in_progress_psettab.zone_importance[0] = '\0';
3405bbec428eSgjelinek 			need_to_commit = B_TRUE;
34060209230bSgjelinek 			return;
34070209230bSgjelinek 		}
34080209230bSgjelinek 		break;
34090209230bSgjelinek 	case RT_MCAP:
34100209230bSgjelinek 		switch (prop_type) {
34110209230bSgjelinek 		case PT_PHYSICAL:
34120209230bSgjelinek 			in_progress_mcaptab.zone_physmem_cap[0] = '\0';
3413bbec428eSgjelinek 			need_to_commit = B_TRUE;
34140209230bSgjelinek 			return;
34150209230bSgjelinek 		case PT_SWAP:
34160209230bSgjelinek 			remove_aliased_rctl(PT_SWAP, ALIAS_MAXSWAP);
34170209230bSgjelinek 			return;
34180209230bSgjelinek 		case PT_LOCKED:
34190209230bSgjelinek 			remove_aliased_rctl(PT_LOCKED, ALIAS_MAXLOCKEDMEM);
34200209230bSgjelinek 			return;
34210209230bSgjelinek 		}
34220209230bSgjelinek 		break;
34230209230bSgjelinek 	default:
34240209230bSgjelinek 		break;
34250209230bSgjelinek 	}
34260209230bSgjelinek 
3427bbec428eSgjelinek 	zone_perror(pt_to_str(prop_type), Z_CLEAR_DISALLOW, B_TRUE);
34280209230bSgjelinek }
34290209230bSgjelinek 
34300209230bSgjelinek static void
34310209230bSgjelinek clear_global(cmd_t *cmd)
34320209230bSgjelinek {
34330209230bSgjelinek 	int err, type;
34340209230bSgjelinek 
34350209230bSgjelinek 	if ((type = cmd->cmd_res_type) == RT_UNKNOWN) {
3436bbec428eSgjelinek 		long_usage(CMD_CLEAR, B_TRUE);
34370209230bSgjelinek 		return;
34380209230bSgjelinek 	}
34390209230bSgjelinek 
3440bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
34410209230bSgjelinek 		return;
34420209230bSgjelinek 
34430209230bSgjelinek 	switch (type) {
34440209230bSgjelinek 	case PT_ZONENAME:
34450209230bSgjelinek 		/* FALLTHRU */
34460209230bSgjelinek 	case PT_ZONEPATH:
34470209230bSgjelinek 		/* FALLTHRU */
34480209230bSgjelinek 	case PT_BRAND:
3449bbec428eSgjelinek 		zone_perror(pt_to_str(type), Z_CLEAR_DISALLOW, B_TRUE);
34500209230bSgjelinek 		return;
34510209230bSgjelinek 	case PT_AUTOBOOT:
34520209230bSgjelinek 		/* false is default; we'll treat as equivalent to clearing */
34530209230bSgjelinek 		if ((err = zonecfg_set_autoboot(handle, B_FALSE)) != Z_OK)
3454bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_AUTOBOOT, err, B_TRUE);
34550209230bSgjelinek 		else
3456bbec428eSgjelinek 			need_to_commit = B_TRUE;
34570209230bSgjelinek 		return;
34580209230bSgjelinek 	case PT_POOL:
34590209230bSgjelinek 		if ((err = zonecfg_set_pool(handle, NULL)) != Z_OK)
3460bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_POOL, err, B_TRUE);
34610209230bSgjelinek 		else
3462bbec428eSgjelinek 			need_to_commit = B_TRUE;
34630209230bSgjelinek 		return;
34640209230bSgjelinek 	case PT_LIMITPRIV:
34650209230bSgjelinek 		if ((err = zonecfg_set_limitpriv(handle, NULL)) != Z_OK)
3466bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_LIMITPRIV, err, B_TRUE);
34670209230bSgjelinek 		else
3468bbec428eSgjelinek 			need_to_commit = B_TRUE;
34690209230bSgjelinek 		return;
34700209230bSgjelinek 	case PT_BOOTARGS:
34710209230bSgjelinek 		if ((err = zonecfg_set_bootargs(handle, NULL)) != Z_OK)
3472bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_BOOTARGS, err, B_TRUE);
34730209230bSgjelinek 		else
3474bbec428eSgjelinek 			need_to_commit = B_TRUE;
34750209230bSgjelinek 		return;
34760209230bSgjelinek 	case PT_SCHED:
34770209230bSgjelinek 		if ((err = zonecfg_set_sched(handle, NULL)) != Z_OK)
3478bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_SCHED, err, B_TRUE);
34790209230bSgjelinek 		else
3480bbec428eSgjelinek 			need_to_commit = B_TRUE;
34810209230bSgjelinek 		return;
3482f4b3ec61Sdh 	case PT_IPTYPE:
3483f4b3ec61Sdh 		/* shared is default; we'll treat as equivalent to clearing */
3484f4b3ec61Sdh 		if ((err = zonecfg_set_iptype(handle, ZS_SHARED)) != Z_OK)
3485bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_IPTYPE, err, B_TRUE);
3486f4b3ec61Sdh 		else
3487bbec428eSgjelinek 			need_to_commit = B_TRUE;
3488f4b3ec61Sdh 		return;
34890209230bSgjelinek 	case PT_MAXLWPS:
34900209230bSgjelinek 		remove_aliased_rctl(PT_MAXLWPS, ALIAS_MAXLWPS);
34910209230bSgjelinek 		return;
34920209230bSgjelinek 	case PT_MAXSHMMEM:
34930209230bSgjelinek 		remove_aliased_rctl(PT_MAXSHMMEM, ALIAS_MAXSHMMEM);
34940209230bSgjelinek 		return;
34950209230bSgjelinek 	case PT_MAXSHMIDS:
34960209230bSgjelinek 		remove_aliased_rctl(PT_MAXSHMIDS, ALIAS_MAXSHMIDS);
34970209230bSgjelinek 		return;
34980209230bSgjelinek 	case PT_MAXMSGIDS:
34990209230bSgjelinek 		remove_aliased_rctl(PT_MAXMSGIDS, ALIAS_MAXMSGIDS);
35000209230bSgjelinek 		return;
35010209230bSgjelinek 	case PT_MAXSEMIDS:
35020209230bSgjelinek 		remove_aliased_rctl(PT_MAXSEMIDS, ALIAS_MAXSEMIDS);
35030209230bSgjelinek 		return;
35040209230bSgjelinek 	case PT_SHARES:
35050209230bSgjelinek 		remove_aliased_rctl(PT_SHARES, ALIAS_SHARES);
35060209230bSgjelinek 		return;
35075679c89fSjv 	case PT_HOSTID:
35085679c89fSjv 		if ((err = zonecfg_set_hostid(handle, NULL)) != Z_OK)
35095679c89fSjv 			z_cmd_rt_perror(CMD_CLEAR, RT_HOSTID, err, B_TRUE);
35105679c89fSjv 		else
35115679c89fSjv 			need_to_commit = B_TRUE;
35125679c89fSjv 		return;
35130209230bSgjelinek 	default:
3514bbec428eSgjelinek 		zone_perror(pt_to_str(type), Z_NO_PROPERTY_TYPE, B_TRUE);
3515bbec428eSgjelinek 		long_usage(CMD_CLEAR, B_TRUE);
3516bbec428eSgjelinek 		usage(B_FALSE, HELP_PROPS);
35170209230bSgjelinek 		return;
35180209230bSgjelinek 	}
35190209230bSgjelinek }
35200209230bSgjelinek 
35210209230bSgjelinek void
35220209230bSgjelinek clear_func(cmd_t *cmd)
35230209230bSgjelinek {
35240209230bSgjelinek 	if (zone_is_read_only(CMD_CLEAR))
35250209230bSgjelinek 		return;
35260209230bSgjelinek 
35270209230bSgjelinek 	assert(cmd != NULL);
35280209230bSgjelinek 
35290209230bSgjelinek 	if (global_scope) {
35300209230bSgjelinek 		if (gz_invalid_property(cmd->cmd_res_type)) {
35310209230bSgjelinek 			zerr(gettext("%s is not a valid property for the "
35320209230bSgjelinek 			    "global zone."), pt_to_str(cmd->cmd_res_type));
3533bbec428eSgjelinek 			saw_error = B_TRUE;
35340209230bSgjelinek 			return;
35350209230bSgjelinek 		}
35360209230bSgjelinek 
35370209230bSgjelinek 		clear_global(cmd);
35380209230bSgjelinek 	} else {
35390209230bSgjelinek 		clear_property(cmd);
35400209230bSgjelinek 	}
35417c478bd9Sstevel@tonic-gate }
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate void
35447c478bd9Sstevel@tonic-gate select_func(cmd_t *cmd)
35457c478bd9Sstevel@tonic-gate {
35460209230bSgjelinek 	int type, err, res;
35470209230bSgjelinek 	uint64_t limit;
3548c97ad5cdSakolb 	uint64_t tmp;
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_SELECT))
35517c478bd9Sstevel@tonic-gate 		return;
35527c478bd9Sstevel@tonic-gate 
35537c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate 	if (global_scope) {
3556bbec428eSgjelinek 		global_scope = B_FALSE;
35577c478bd9Sstevel@tonic-gate 		resource_scope = cmd->cmd_res_type;
35587c478bd9Sstevel@tonic-gate 		end_op = CMD_SELECT;
35597c478bd9Sstevel@tonic-gate 	} else {
35607c478bd9Sstevel@tonic-gate 		scope_usage(CMD_SELECT);
35617c478bd9Sstevel@tonic-gate 		return;
35627c478bd9Sstevel@tonic-gate 	}
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 	if ((type = cmd->cmd_res_type) == RT_UNKNOWN) {
3565bbec428eSgjelinek 		long_usage(CMD_SELECT, B_TRUE);
35667c478bd9Sstevel@tonic-gate 		return;
35677c478bd9Sstevel@tonic-gate 	}
35687c478bd9Sstevel@tonic-gate 
3569bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
35707c478bd9Sstevel@tonic-gate 		return;
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 	switch (type) {
35737c478bd9Sstevel@tonic-gate 	case RT_FS:
3574bbec428eSgjelinek 		if ((err = fill_in_fstab(cmd, &old_fstab, B_FALSE)) != Z_OK) {
3575bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_FS, err, B_TRUE);
3576bbec428eSgjelinek 			global_scope = B_TRUE;
35777c478bd9Sstevel@tonic-gate 		}
35787c478bd9Sstevel@tonic-gate 		bcopy(&old_fstab, &in_progress_fstab,
35797c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_fstab));
35807c478bd9Sstevel@tonic-gate 		return;
35817c478bd9Sstevel@tonic-gate 	case RT_IPD:
3582087719fdSdp 		if (state_atleast(ZONE_STATE_INCOMPLETE)) {
35837c478bd9Sstevel@tonic-gate 			zerr(gettext("Zone %s not in %s state; %s %s not "
35847c478bd9Sstevel@tonic-gate 			    "allowed."), zone,
35857c478bd9Sstevel@tonic-gate 			    zone_state_str(ZONE_STATE_CONFIGURED),
35867c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_SELECT), rt_to_str(RT_IPD));
3587bbec428eSgjelinek 			global_scope = B_TRUE;
35887c478bd9Sstevel@tonic-gate 			end_op = -1;
35897c478bd9Sstevel@tonic-gate 			return;
35907c478bd9Sstevel@tonic-gate 		}
3591bbec428eSgjelinek 		if ((err = fill_in_ipdtab(cmd, &old_ipdtab, B_FALSE)) != Z_OK) {
3592bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_IPD, err, B_TRUE);
3593bbec428eSgjelinek 			global_scope = B_TRUE;
35947c478bd9Sstevel@tonic-gate 		}
35957c478bd9Sstevel@tonic-gate 		bcopy(&old_ipdtab, &in_progress_ipdtab,
35967c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_fstab));
35977c478bd9Sstevel@tonic-gate 		return;
35987c478bd9Sstevel@tonic-gate 	case RT_NET:
3599bbec428eSgjelinek 		if ((err = fill_in_nwiftab(cmd, &old_nwiftab, B_FALSE))
3600bbec428eSgjelinek 		    != Z_OK) {
3601bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_NET, err, B_TRUE);
3602bbec428eSgjelinek 			global_scope = B_TRUE;
36037c478bd9Sstevel@tonic-gate 		}
36047c478bd9Sstevel@tonic-gate 		bcopy(&old_nwiftab, &in_progress_nwiftab,
36057c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_nwiftab));
36067c478bd9Sstevel@tonic-gate 		return;
36077c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
3608bbec428eSgjelinek 		if ((err = fill_in_devtab(cmd, &old_devtab, B_FALSE)) != Z_OK) {
3609bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_DEVICE, err, B_TRUE);
3610bbec428eSgjelinek 			global_scope = B_TRUE;
36117c478bd9Sstevel@tonic-gate 		}
36127c478bd9Sstevel@tonic-gate 		bcopy(&old_devtab, &in_progress_devtab,
36137c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_devtab));
36147c478bd9Sstevel@tonic-gate 		return;
36157c478bd9Sstevel@tonic-gate 	case RT_RCTL:
3616bbec428eSgjelinek 		if ((err = fill_in_rctltab(cmd, &old_rctltab, B_FALSE))
3617bbec428eSgjelinek 		    != Z_OK) {
3618bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_RCTL, err, B_TRUE);
3619bbec428eSgjelinek 			global_scope = B_TRUE;
36207c478bd9Sstevel@tonic-gate 		}
36217c478bd9Sstevel@tonic-gate 		bcopy(&old_rctltab, &in_progress_rctltab,
36227c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctltab));
36237c478bd9Sstevel@tonic-gate 		return;
36247c478bd9Sstevel@tonic-gate 	case RT_ATTR:
3625bbec428eSgjelinek 		if ((err = fill_in_attrtab(cmd, &old_attrtab, B_FALSE))
3626bbec428eSgjelinek 		    != Z_OK) {
3627bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_ATTR, err, B_TRUE);
3628bbec428eSgjelinek 			global_scope = B_TRUE;
36297c478bd9Sstevel@tonic-gate 		}
36307c478bd9Sstevel@tonic-gate 		bcopy(&old_attrtab, &in_progress_attrtab,
36317c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_attrtab));
36327c478bd9Sstevel@tonic-gate 		return;
3633fa9e4066Sahrens 	case RT_DATASET:
3634bbec428eSgjelinek 		if ((err = fill_in_dstab(cmd, &old_dstab, B_FALSE)) != Z_OK) {
3635bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_DATASET, err, B_TRUE);
3636bbec428eSgjelinek 			global_scope = B_TRUE;
3637fa9e4066Sahrens 		}
3638fa9e4066Sahrens 		bcopy(&old_dstab, &in_progress_dstab,
3639fa9e4066Sahrens 		    sizeof (struct zone_dstab));
3640fa9e4066Sahrens 		return;
36410209230bSgjelinek 	case RT_DCPU:
36420209230bSgjelinek 		if ((err = zonecfg_lookup_pset(handle, &old_psettab)) != Z_OK) {
3643bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_DCPU, err, B_TRUE);
3644bbec428eSgjelinek 			global_scope = B_TRUE;
36450209230bSgjelinek 		}
36460209230bSgjelinek 		bcopy(&old_psettab, &in_progress_psettab,
36470209230bSgjelinek 		    sizeof (struct zone_psettab));
36480209230bSgjelinek 		return;
3649c97ad5cdSakolb 	case RT_PCAP:
3650c97ad5cdSakolb 		if ((err = zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &tmp))
3651c97ad5cdSakolb 		    != Z_OK) {
3652bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_PCAP, err, B_TRUE);
3653bbec428eSgjelinek 			global_scope = B_TRUE;
3654c97ad5cdSakolb 		}
3655c97ad5cdSakolb 		return;
36560209230bSgjelinek 	case RT_MCAP:
36570209230bSgjelinek 		/* if none of these exist, there is no resource to select */
36580209230bSgjelinek 		if ((res = zonecfg_lookup_mcap(handle, &old_mcaptab)) != Z_OK &&
36590209230bSgjelinek 		    zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP, &limit)
36600209230bSgjelinek 		    != Z_OK &&
36610209230bSgjelinek 		    zonecfg_get_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM, &limit)
36620209230bSgjelinek 		    != Z_OK) {
36630209230bSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_MCAP, Z_NO_RESOURCE_TYPE,
3664bbec428eSgjelinek 			    B_TRUE);
3665bbec428eSgjelinek 			global_scope = B_TRUE;
36660209230bSgjelinek 		}
36670209230bSgjelinek 		if (res == Z_OK)
36680209230bSgjelinek 			bcopy(&old_mcaptab, &in_progress_mcaptab,
36690209230bSgjelinek 			    sizeof (struct zone_mcaptab));
36700209230bSgjelinek 		else
36710209230bSgjelinek 			bzero(&in_progress_mcaptab,
36720209230bSgjelinek 			    sizeof (in_progress_mcaptab));
36730209230bSgjelinek 		return;
36747c478bd9Sstevel@tonic-gate 	default:
3675bbec428eSgjelinek 		zone_perror(rt_to_str(type), Z_NO_RESOURCE_TYPE, B_TRUE);
3676bbec428eSgjelinek 		long_usage(CMD_SELECT, B_TRUE);
3677bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
36787c478bd9Sstevel@tonic-gate 		return;
36797c478bd9Sstevel@tonic-gate 	}
36807c478bd9Sstevel@tonic-gate }
36817c478bd9Sstevel@tonic-gate 
36827c478bd9Sstevel@tonic-gate /*
36837c478bd9Sstevel@tonic-gate  * Network "addresses" can be one of the following forms:
36847c478bd9Sstevel@tonic-gate  *	<IPv4 address>
36857c478bd9Sstevel@tonic-gate  *	<IPv4 address>/<prefix length>
36867c478bd9Sstevel@tonic-gate  *	<IPv6 address>/<prefix length>
36877c478bd9Sstevel@tonic-gate  *	<host name>
36887c478bd9Sstevel@tonic-gate  *	<host name>/<prefix length>
36897c478bd9Sstevel@tonic-gate  * In other words, the "/" followed by a prefix length is allowed but not
36907c478bd9Sstevel@tonic-gate  * required for IPv4 addresses and host names, and required for IPv6 addresses.
36917c478bd9Sstevel@tonic-gate  * If a prefix length is given, it must be in the allowable range: 0 to 32 for
36927c478bd9Sstevel@tonic-gate  * IPv4 addresses and host names, 0 to 128 for IPv6 addresses.
36937c478bd9Sstevel@tonic-gate  * Host names must start with an alpha-numeric character, and all subsequent
36947c478bd9Sstevel@tonic-gate  * characters must be either alpha-numeric or "-".
36957c478bd9Sstevel@tonic-gate  */
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate static int
36987c478bd9Sstevel@tonic-gate validate_net_address_syntax(char *address)
36997c478bd9Sstevel@tonic-gate {
37007c478bd9Sstevel@tonic-gate 	char *slashp, part1[MAXHOSTNAMELEN];
37017c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
37027c478bd9Sstevel@tonic-gate 	struct in_addr in4;
37037c478bd9Sstevel@tonic-gate 	int prefixlen, i;
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate 	/*
37067c478bd9Sstevel@tonic-gate 	 * Copy the part before any '/' into part1 or copy the whole
37077c478bd9Sstevel@tonic-gate 	 * thing if there is no '/'.
37087c478bd9Sstevel@tonic-gate 	 */
37097c478bd9Sstevel@tonic-gate 	if ((slashp = strchr(address, '/')) != NULL) {
37107c478bd9Sstevel@tonic-gate 		*slashp = '\0';
37117c478bd9Sstevel@tonic-gate 		(void) strlcpy(part1, address, sizeof (part1));
37127c478bd9Sstevel@tonic-gate 		*slashp = '/';
37137c478bd9Sstevel@tonic-gate 		prefixlen = atoi(++slashp);
37147c478bd9Sstevel@tonic-gate 	} else {
37157c478bd9Sstevel@tonic-gate 		(void) strlcpy(part1, address, sizeof (part1));
37167c478bd9Sstevel@tonic-gate 	}
37177c478bd9Sstevel@tonic-gate 
37187c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET6, part1, &in6) == 1) {
37197c478bd9Sstevel@tonic-gate 		if (slashp == NULL) {
37207c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: IPv6 addresses "
37217c478bd9Sstevel@tonic-gate 			    "require /prefix-length suffix."), address);
37227c478bd9Sstevel@tonic-gate 			return (Z_ERR);
37237c478bd9Sstevel@tonic-gate 		}
37247c478bd9Sstevel@tonic-gate 		if (prefixlen < 0 || prefixlen > 128) {
37257c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: IPv6 address "
37267c478bd9Sstevel@tonic-gate 			    "prefix lengths must be 0 - 128."), address);
37277c478bd9Sstevel@tonic-gate 			return (Z_ERR);
37287c478bd9Sstevel@tonic-gate 		}
37297c478bd9Sstevel@tonic-gate 		return (Z_OK);
37307c478bd9Sstevel@tonic-gate 	}
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate 	/* At this point, any /prefix must be for IPv4. */
37337c478bd9Sstevel@tonic-gate 	if (slashp != NULL) {
37347c478bd9Sstevel@tonic-gate 		if (prefixlen < 0 || prefixlen > 32) {
37357c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: IPv4 address "
37367c478bd9Sstevel@tonic-gate 			    "prefix lengths must be 0 - 32."), address);
37377c478bd9Sstevel@tonic-gate 			return (Z_ERR);
37387c478bd9Sstevel@tonic-gate 		}
37397c478bd9Sstevel@tonic-gate 	}
37407c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, part1, &in4) == 1)
37417c478bd9Sstevel@tonic-gate 		return (Z_OK);
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 	/* address may also be a host name */
37447c478bd9Sstevel@tonic-gate 	if (!isalnum(part1[0])) {
37457c478bd9Sstevel@tonic-gate 		zerr(gettext("%s: bogus host name or network address syntax"),
37467c478bd9Sstevel@tonic-gate 		    part1);
3747bbec428eSgjelinek 		saw_error = B_TRUE;
3748bbec428eSgjelinek 		usage(B_FALSE, HELP_NETADDR);
37497c478bd9Sstevel@tonic-gate 		return (Z_ERR);
37507c478bd9Sstevel@tonic-gate 	}
37517c478bd9Sstevel@tonic-gate 	for (i = 1; part1[i]; i++)
37527c478bd9Sstevel@tonic-gate 		if (!isalnum(part1[i]) && part1[i] != '-' && part1[i] != '.') {
37537c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: bogus host name or "
37547c478bd9Sstevel@tonic-gate 			    "network address syntax"), part1);
3755bbec428eSgjelinek 			saw_error = B_TRUE;
3756bbec428eSgjelinek 			usage(B_FALSE, HELP_NETADDR);
37577c478bd9Sstevel@tonic-gate 			return (Z_ERR);
37587c478bd9Sstevel@tonic-gate 		}
37597c478bd9Sstevel@tonic-gate 	return (Z_OK);
37607c478bd9Sstevel@tonic-gate }
37617c478bd9Sstevel@tonic-gate 
37627c478bd9Sstevel@tonic-gate static int
3763c9f134eaSjv validate_net_physical_syntax(const char *ifname)
37647c478bd9Sstevel@tonic-gate {
3765c9f134eaSjv 	ifspec_t ifnameprop;
3766c9f134eaSjv 	zone_iptype_t iptype;
3767c9f134eaSjv 
376837b210dcSjv 	if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
3769c9f134eaSjv 		zerr(gettext("zone configuration has an invalid or nonexistent "
3770c9f134eaSjv 		    "ip-type property"));
3771c9f134eaSjv 		return (Z_ERR);
3772c9f134eaSjv 	}
3773c9f134eaSjv 	switch (iptype) {
3774c9f134eaSjv 	case ZS_SHARED:
3775c9f134eaSjv 		if (ifparse_ifspec(ifname, &ifnameprop) == B_FALSE) {
3776c9f134eaSjv 			zerr(gettext("%s: invalid physical interface name"),
3777c9f134eaSjv 			    ifname);
3778c9f134eaSjv 			return (Z_ERR);
3779c9f134eaSjv 		}
3780c9f134eaSjv 		if (ifnameprop.ifsp_lunvalid) {
3781c9f134eaSjv 			zerr(gettext("%s: LUNs not allowed in physical "
3782c9f134eaSjv 			    "interface names"), ifname);
3783c9f134eaSjv 			return (Z_ERR);
3784c9f134eaSjv 		}
3785c9f134eaSjv 		break;
3786c9f134eaSjv 	case ZS_EXCLUSIVE:
3787c9f134eaSjv 		if (dladm_valid_linkname(ifname) == B_FALSE) {
3788c9f134eaSjv 			if (strchr(ifname, ':') != NULL)
3789c9f134eaSjv 				zerr(gettext("%s: physical interface name "
3790c9f134eaSjv 				    "required; logical interface name not "
3791c9f134eaSjv 				    "allowed"), ifname);
3792c9f134eaSjv 			else
3793c9f134eaSjv 				zerr(gettext("%s: invalid physical interface "
3794c9f134eaSjv 				    "name"), ifname);
3795c9f134eaSjv 			return (Z_ERR);
3796c9f134eaSjv 		}
3797c9f134eaSjv 		break;
3798c9f134eaSjv 	}
3799c9f134eaSjv 	return (Z_OK);
38007c478bd9Sstevel@tonic-gate }
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate static boolean_t
38037c478bd9Sstevel@tonic-gate valid_fs_type(const char *type)
38047c478bd9Sstevel@tonic-gate {
38057c478bd9Sstevel@tonic-gate 	/*
38067c478bd9Sstevel@tonic-gate 	 * Is this a valid path component?
38077c478bd9Sstevel@tonic-gate 	 */
38087c478bd9Sstevel@tonic-gate 	if (strlen(type) + 1 > MAXNAMELEN)
38097c478bd9Sstevel@tonic-gate 		return (B_FALSE);
38107c478bd9Sstevel@tonic-gate 	/*
38117c478bd9Sstevel@tonic-gate 	 * Make sure a bad value for "type" doesn't make
38127c478bd9Sstevel@tonic-gate 	 * /usr/lib/fs/<type>/mount turn into something else.
38137c478bd9Sstevel@tonic-gate 	 */
38147c478bd9Sstevel@tonic-gate 	if (strchr(type, '/') != NULL || type[0] == '\0' ||
38157c478bd9Sstevel@tonic-gate 	    strcmp(type, ".") == 0 || strcmp(type, "..") == 0)
3816087719fdSdp 		return (B_FALSE);
38177c478bd9Sstevel@tonic-gate 	/*
38187c478bd9Sstevel@tonic-gate 	 * More detailed verification happens later by zoneadm(1m).
38197c478bd9Sstevel@tonic-gate 	 */
38207c478bd9Sstevel@tonic-gate 	return (B_TRUE);
38217c478bd9Sstevel@tonic-gate }
38227c478bd9Sstevel@tonic-gate 
3823f4b3ec61Sdh static boolean_t
3824f4b3ec61Sdh allow_exclusive()
3825f4b3ec61Sdh {
3826f4b3ec61Sdh 	brand_handle_t	bh;
3827f4b3ec61Sdh 	char		brand[MAXNAMELEN];
3828f4b3ec61Sdh 	boolean_t	ret;
3829f4b3ec61Sdh 
3830f4b3ec61Sdh 	if (zonecfg_get_brand(handle, brand, sizeof (brand)) != Z_OK) {
3831f4b3ec61Sdh 		zerr("%s: %s\n", zone, gettext("could not get zone brand"));
3832f4b3ec61Sdh 		return (B_FALSE);
3833f4b3ec61Sdh 	}
3834f4b3ec61Sdh 	if ((bh = brand_open(brand)) == NULL) {
3835f4b3ec61Sdh 		zerr("%s: %s\n", zone, gettext("unknown brand."));
3836f4b3ec61Sdh 		return (B_FALSE);
3837f4b3ec61Sdh 	}
3838f4b3ec61Sdh 	ret = brand_allow_exclusive_ip(bh);
3839f4b3ec61Sdh 	brand_close(bh);
3840f4b3ec61Sdh 	if (!ret)
3841f4b3ec61Sdh 		zerr(gettext("%s cannot be '%s' when %s is '%s'."),
3842f4b3ec61Sdh 		    pt_to_str(PT_IPTYPE), "exclusive",
3843f4b3ec61Sdh 		    pt_to_str(PT_BRAND), brand);
3844f4b3ec61Sdh 	return (ret);
3845f4b3ec61Sdh }
3846f4b3ec61Sdh 
38470209230bSgjelinek static void
38480209230bSgjelinek set_aliased_rctl(char *alias, int prop_type, char *s)
38490209230bSgjelinek {
38500209230bSgjelinek 	uint64_t limit;
38510209230bSgjelinek 	int err;
38520209230bSgjelinek 	char tmp[128];
38530209230bSgjelinek 
38540209230bSgjelinek 	if (global_zone && strcmp(alias, ALIAS_SHARES) != 0)
38550209230bSgjelinek 		zerr(gettext("WARNING: Setting a global zone resource "
38560209230bSgjelinek 		    "control too low could deny\nservice "
38570209230bSgjelinek 		    "to even the root user; "
38580209230bSgjelinek 		    "this could render the system impossible\n"
38590209230bSgjelinek 		    "to administer.  Please use caution."));
38600209230bSgjelinek 
38610209230bSgjelinek 	/* convert memory based properties */
38620209230bSgjelinek 	if (prop_type == PT_MAXSHMMEM) {
38630209230bSgjelinek 		if (!zonecfg_valid_memlimit(s, &limit)) {
38640209230bSgjelinek 			zerr(gettext("A non-negative number with a required "
38650209230bSgjelinek 			    "scale suffix (K, M, G or T) was expected\nhere."));
3866bbec428eSgjelinek 			saw_error = B_TRUE;
38670209230bSgjelinek 			return;
38680209230bSgjelinek 		}
38690209230bSgjelinek 
38700209230bSgjelinek 		(void) snprintf(tmp, sizeof (tmp), "%llu", limit);
38710209230bSgjelinek 		s = tmp;
38720209230bSgjelinek 	}
38730209230bSgjelinek 
38740209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, alias)) {
3875bbec428eSgjelinek 		zone_perror(pt_to_str(prop_type), Z_ALIAS_DISALLOW, B_FALSE);
3876bbec428eSgjelinek 		saw_error = B_TRUE;
38770209230bSgjelinek 	} else if (!zonecfg_valid_alias_limit(alias, s, &limit)) {
38780209230bSgjelinek 		zerr(gettext("%s property is out of range."),
38790209230bSgjelinek 		    pt_to_str(prop_type));
3880bbec428eSgjelinek 		saw_error = B_TRUE;
38810209230bSgjelinek 	} else if ((err = zonecfg_set_aliased_rctl(handle, alias, limit))
38820209230bSgjelinek 	    != Z_OK) {
3883bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
3884bbec428eSgjelinek 		saw_error = B_TRUE;
38850209230bSgjelinek 	} else {
3886bbec428eSgjelinek 		need_to_commit = B_TRUE;
38870209230bSgjelinek 	}
38880209230bSgjelinek }
38890209230bSgjelinek 
38907c478bd9Sstevel@tonic-gate void
38917c478bd9Sstevel@tonic-gate set_func(cmd_t *cmd)
38927c478bd9Sstevel@tonic-gate {
38937c478bd9Sstevel@tonic-gate 	char *prop_id;
3894555afedfScarlsonj 	int arg, err, res_type, prop_type;
38957c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
38967c478bd9Sstevel@tonic-gate 	boolean_t autoboot;
3897f4b3ec61Sdh 	zone_iptype_t iptype;
3898bbec428eSgjelinek 	boolean_t force_set = B_FALSE;
38990209230bSgjelinek 	size_t physmem_size = sizeof (in_progress_mcaptab.zone_physmem_cap);
39000209230bSgjelinek 	uint64_t mem_cap, mem_limit;
3901c97ad5cdSakolb 	float cap;
3902c97ad5cdSakolb 	char *unitp;
39030209230bSgjelinek 	struct zone_psettab tmp_psettab;
3904bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
39057c478bd9Sstevel@tonic-gate 
39067c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_SET))
39077c478bd9Sstevel@tonic-gate 		return;
39087c478bd9Sstevel@tonic-gate 
39097c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
39107c478bd9Sstevel@tonic-gate 
3911555afedfScarlsonj 	optind = opterr = 0;
3912555afedfScarlsonj 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "F")) != EOF) {
3913555afedfScarlsonj 		switch (arg) {
3914555afedfScarlsonj 		case 'F':
3915bbec428eSgjelinek 			force_set = B_TRUE;
3916555afedfScarlsonj 			break;
3917555afedfScarlsonj 		default:
3918555afedfScarlsonj 			if (optopt == '?')
3919555afedfScarlsonj 				longer_usage(CMD_SET);
3920555afedfScarlsonj 			else
3921555afedfScarlsonj 				short_usage(CMD_SET);
3922bbec428eSgjelinek 			arg_err = B_TRUE;
39237ec75eb8Sgjelinek 			break;
3924555afedfScarlsonj 		}
3925555afedfScarlsonj 	}
39267ec75eb8Sgjelinek 	if (arg_err)
39277ec75eb8Sgjelinek 		return;
3928555afedfScarlsonj 
39297c478bd9Sstevel@tonic-gate 	prop_type = cmd->cmd_prop_name[0];
39307c478bd9Sstevel@tonic-gate 	if (global_scope) {
39310209230bSgjelinek 		if (gz_invalid_property(prop_type)) {
39320209230bSgjelinek 			zerr(gettext("%s is not a valid property for the "
39330209230bSgjelinek 			    "global zone."), pt_to_str(prop_type));
3934bbec428eSgjelinek 			saw_error = B_TRUE;
39350209230bSgjelinek 			return;
39360209230bSgjelinek 		}
39370209230bSgjelinek 
3938087719fdSdp 		if (prop_type == PT_ZONENAME) {
3939087719fdSdp 			res_type = RT_ZONENAME;
3940087719fdSdp 		} else if (prop_type == PT_ZONEPATH) {
39417c478bd9Sstevel@tonic-gate 			res_type = RT_ZONEPATH;
39427c478bd9Sstevel@tonic-gate 		} else if (prop_type == PT_AUTOBOOT) {
39437c478bd9Sstevel@tonic-gate 			res_type = RT_AUTOBOOT;
39449acbbeafSnn 		} else if (prop_type == PT_BRAND) {
39459acbbeafSnn 			res_type = RT_BRAND;
39467c478bd9Sstevel@tonic-gate 		} else if (prop_type == PT_POOL) {
39477c478bd9Sstevel@tonic-gate 			res_type = RT_POOL;
3948ffbafc53Scomay 		} else if (prop_type == PT_LIMITPRIV) {
3949ffbafc53Scomay 			res_type = RT_LIMITPRIV;
39503f2f09c1Sdp 		} else if (prop_type == PT_BOOTARGS) {
39513f2f09c1Sdp 			res_type = RT_BOOTARGS;
39520209230bSgjelinek 		} else if (prop_type == PT_SCHED) {
39530209230bSgjelinek 			res_type = RT_SCHED;
3954f4b3ec61Sdh 		} else if (prop_type == PT_IPTYPE) {
3955f4b3ec61Sdh 			res_type = RT_IPTYPE;
39560209230bSgjelinek 		} else if (prop_type == PT_MAXLWPS) {
39570209230bSgjelinek 			res_type = RT_MAXLWPS;
39580209230bSgjelinek 		} else if (prop_type == PT_MAXSHMMEM) {
39590209230bSgjelinek 			res_type = RT_MAXSHMMEM;
39600209230bSgjelinek 		} else if (prop_type == PT_MAXSHMIDS) {
39610209230bSgjelinek 			res_type = RT_MAXSHMIDS;
39620209230bSgjelinek 		} else if (prop_type == PT_MAXMSGIDS) {
39630209230bSgjelinek 			res_type = RT_MAXMSGIDS;
39640209230bSgjelinek 		} else if (prop_type == PT_MAXSEMIDS) {
39650209230bSgjelinek 			res_type = RT_MAXSEMIDS;
39660209230bSgjelinek 		} else if (prop_type == PT_SHARES) {
39670209230bSgjelinek 			res_type = RT_SHARES;
39685679c89fSjv 		} else if (prop_type == PT_HOSTID) {
39695679c89fSjv 			res_type = RT_HOSTID;
39707c478bd9Sstevel@tonic-gate 		} else {
39717c478bd9Sstevel@tonic-gate 			zerr(gettext("Cannot set a resource-specific property "
39727c478bd9Sstevel@tonic-gate 			    "from the global scope."));
3973bbec428eSgjelinek 			saw_error = B_TRUE;
39747c478bd9Sstevel@tonic-gate 			return;
39757c478bd9Sstevel@tonic-gate 		}
39767c478bd9Sstevel@tonic-gate 	} else {
39777c478bd9Sstevel@tonic-gate 		res_type = resource_scope;
39787c478bd9Sstevel@tonic-gate 	}
39797c478bd9Sstevel@tonic-gate 
3980555afedfScarlsonj 	if (force_set) {
3981555afedfScarlsonj 		if (res_type != RT_ZONEPATH) {
3982555afedfScarlsonj 			zerr(gettext("Only zonepath setting can be forced."));
3983bbec428eSgjelinek 			saw_error = B_TRUE;
3984555afedfScarlsonj 			return;
3985555afedfScarlsonj 		}
3986555afedfScarlsonj 		if (!zonecfg_in_alt_root()) {
3987555afedfScarlsonj 			zerr(gettext("Zonepath is changeable only in an "
3988555afedfScarlsonj 			    "alternate root."));
3989bbec428eSgjelinek 			saw_error = B_TRUE;
3990555afedfScarlsonj 			return;
3991555afedfScarlsonj 		}
3992555afedfScarlsonj 	}
3993555afedfScarlsonj 
39947c478bd9Sstevel@tonic-gate 	pp = cmd->cmd_property_ptr[0];
39957c478bd9Sstevel@tonic-gate 	/*
39967c478bd9Sstevel@tonic-gate 	 * A nasty expression but not that complicated:
39977c478bd9Sstevel@tonic-gate 	 * 1. fs options are simple or list (tested below)
39987c478bd9Sstevel@tonic-gate 	 * 2. rctl value's are complex or list (tested below)
39997c478bd9Sstevel@tonic-gate 	 * Anything else should be simple.
40007c478bd9Sstevel@tonic-gate 	 */
40017c478bd9Sstevel@tonic-gate 	if (!(res_type == RT_FS && prop_type == PT_OPTIONS) &&
40027c478bd9Sstevel@tonic-gate 	    !(res_type == RT_RCTL && prop_type == PT_VALUE) &&
40037c478bd9Sstevel@tonic-gate 	    (pp->pv_type != PROP_VAL_SIMPLE ||
40047c478bd9Sstevel@tonic-gate 	    (prop_id = pp->pv_simple) == NULL)) {
40057c478bd9Sstevel@tonic-gate 		zerr(gettext("A %s value was expected here."),
40067c478bd9Sstevel@tonic-gate 		    pvt_to_str(PROP_VAL_SIMPLE));
4007bbec428eSgjelinek 		saw_error = B_TRUE;
40087c478bd9Sstevel@tonic-gate 		return;
40097c478bd9Sstevel@tonic-gate 	}
40107c478bd9Sstevel@tonic-gate 	if (prop_type == PT_UNKNOWN) {
4011bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
40127c478bd9Sstevel@tonic-gate 		return;
40137c478bd9Sstevel@tonic-gate 	}
40147c478bd9Sstevel@tonic-gate 
4015087719fdSdp 	/*
4016087719fdSdp 	 * Special case: the user can change the zone name prior to 'create';
4017087719fdSdp 	 * if the zone already exists, we fall through letting initialize()
4018087719fdSdp 	 * and the rest of the logic run.
4019087719fdSdp 	 */
4020bbec428eSgjelinek 	if (res_type == RT_ZONENAME && got_handle == B_FALSE &&
4021087719fdSdp 	    !state_atleast(ZONE_STATE_CONFIGURED)) {
4022fb03efaaSdp 		if ((err = zonecfg_validate_zonename(prop_id)) != Z_OK) {
4023bbec428eSgjelinek 			zone_perror(prop_id, err, B_TRUE);
4024bbec428eSgjelinek 			usage(B_FALSE, HELP_SYNTAX);
4025fb03efaaSdp 			return;
4026fb03efaaSdp 		}
4027087719fdSdp 		(void) strlcpy(zone, prop_id, sizeof (zone));
4028087719fdSdp 		return;
4029087719fdSdp 	}
4030087719fdSdp 
4031bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
40327c478bd9Sstevel@tonic-gate 		return;
40337c478bd9Sstevel@tonic-gate 
40347c478bd9Sstevel@tonic-gate 	switch (res_type) {
4035087719fdSdp 	case RT_ZONENAME:
4036087719fdSdp 		if ((err = zonecfg_set_name(handle, prop_id)) != Z_OK) {
4037087719fdSdp 			/*
4038087719fdSdp 			 * Use prop_id instead of 'zone' here, since we're
4039087719fdSdp 			 * reporting a problem about the *new* zonename.
4040087719fdSdp 			 */
4041bbec428eSgjelinek 			zone_perror(prop_id, err, B_TRUE);
4042bbec428eSgjelinek 			usage(B_FALSE, HELP_SYNTAX);
4043087719fdSdp 		} else {
4044bbec428eSgjelinek 			need_to_commit = B_TRUE;
4045087719fdSdp 			(void) strlcpy(zone, prop_id, sizeof (zone));
4046087719fdSdp 		}
4047087719fdSdp 		return;
40487c478bd9Sstevel@tonic-gate 	case RT_ZONEPATH:
4049555afedfScarlsonj 		if (!force_set && state_atleast(ZONE_STATE_INSTALLED)) {
40507c478bd9Sstevel@tonic-gate 			zerr(gettext("Zone %s already installed; %s %s not "
40517c478bd9Sstevel@tonic-gate 			    "allowed."), zone, cmd_to_str(CMD_SET),
40527c478bd9Sstevel@tonic-gate 			    rt_to_str(RT_ZONEPATH));
40537c478bd9Sstevel@tonic-gate 			return;
40547c478bd9Sstevel@tonic-gate 		}
40557c478bd9Sstevel@tonic-gate 		if (validate_zonepath_syntax(prop_id) != Z_OK) {
4056bbec428eSgjelinek 			saw_error = B_TRUE;
40577c478bd9Sstevel@tonic-gate 			return;
40587c478bd9Sstevel@tonic-gate 		}
40597c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_set_zonepath(handle, prop_id)) != Z_OK)
4060bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
40617c478bd9Sstevel@tonic-gate 		else
4062bbec428eSgjelinek 			need_to_commit = B_TRUE;
40637c478bd9Sstevel@tonic-gate 		return;
40649acbbeafSnn 	case RT_BRAND:
40659acbbeafSnn 		if (state_atleast(ZONE_STATE_INSTALLED)) {
40669acbbeafSnn 			zerr(gettext("Zone %s already installed; %s %s not "
40679acbbeafSnn 			    "allowed."), zone, cmd_to_str(CMD_SET),
40689acbbeafSnn 			    rt_to_str(RT_BRAND));
40699acbbeafSnn 			return;
40709acbbeafSnn 		}
40719acbbeafSnn 		if ((err = zonecfg_set_brand(handle, prop_id)) != Z_OK)
4072bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
40739acbbeafSnn 		else
4074bbec428eSgjelinek 			need_to_commit = B_TRUE;
40759acbbeafSnn 		return;
40767c478bd9Sstevel@tonic-gate 	case RT_AUTOBOOT:
40777c478bd9Sstevel@tonic-gate 		if (strcmp(prop_id, "true") == 0) {
40787c478bd9Sstevel@tonic-gate 			autoboot = B_TRUE;
40797c478bd9Sstevel@tonic-gate 		} else if (strcmp(prop_id, "false") == 0) {
40807c478bd9Sstevel@tonic-gate 			autoboot = B_FALSE;
40817c478bd9Sstevel@tonic-gate 		} else {
40827c478bd9Sstevel@tonic-gate 			zerr(gettext("%s value must be '%s' or '%s'."),
40837c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_AUTOBOOT), "true", "false");
4084bbec428eSgjelinek 			saw_error = B_TRUE;
40857c478bd9Sstevel@tonic-gate 			return;
40867c478bd9Sstevel@tonic-gate 		}
40877c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_set_autoboot(handle, autoboot)) != Z_OK)
4088bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
40897c478bd9Sstevel@tonic-gate 		else
4090bbec428eSgjelinek 			need_to_commit = B_TRUE;
40917c478bd9Sstevel@tonic-gate 		return;
40927c478bd9Sstevel@tonic-gate 	case RT_POOL:
40930209230bSgjelinek 		/* don't allow use of the reserved temporary pool names */
40940209230bSgjelinek 		if (strncmp("SUNW", prop_id, 4) == 0) {
40950209230bSgjelinek 			zerr(gettext("pool names starting with SUNW are "
40960209230bSgjelinek 			    "reserved."));
4097bbec428eSgjelinek 			saw_error = B_TRUE;
40980209230bSgjelinek 			return;
40990209230bSgjelinek 		}
41000209230bSgjelinek 
41010209230bSgjelinek 		/* can't set pool if dedicated-cpu exists */
41020209230bSgjelinek 		if (zonecfg_lookup_pset(handle, &tmp_psettab) == Z_OK) {
41030209230bSgjelinek 			zerr(gettext("The %s resource already exists.  "
41040209230bSgjelinek 			    "A persistent pool is incompatible\nwith the %s "
41050209230bSgjelinek 			    "resource."), rt_to_str(RT_DCPU),
41060209230bSgjelinek 			    rt_to_str(RT_DCPU));
4107bbec428eSgjelinek 			saw_error = B_TRUE;
41080209230bSgjelinek 			return;
41090209230bSgjelinek 		}
41100209230bSgjelinek 
41117c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_set_pool(handle, prop_id)) != Z_OK)
4112bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
41137c478bd9Sstevel@tonic-gate 		else
4114bbec428eSgjelinek 			need_to_commit = B_TRUE;
41157c478bd9Sstevel@tonic-gate 		return;
4116ffbafc53Scomay 	case RT_LIMITPRIV:
4117ffbafc53Scomay 		if ((err = zonecfg_set_limitpriv(handle, prop_id)) != Z_OK)
4118bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
4119ffbafc53Scomay 		else
4120bbec428eSgjelinek 			need_to_commit = B_TRUE;
4121ffbafc53Scomay 		return;
41223f2f09c1Sdp 	case RT_BOOTARGS:
41233f2f09c1Sdp 		if ((err = zonecfg_set_bootargs(handle, prop_id)) != Z_OK)
4124bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
41253f2f09c1Sdp 		else
4126bbec428eSgjelinek 			need_to_commit = B_TRUE;
41273f2f09c1Sdp 		return;
41280209230bSgjelinek 	case RT_SCHED:
41290209230bSgjelinek 		if ((err = zonecfg_set_sched(handle, prop_id)) != Z_OK)
4130bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
41310209230bSgjelinek 		else
4132bbec428eSgjelinek 			need_to_commit = B_TRUE;
41330209230bSgjelinek 		return;
4134f4b3ec61Sdh 	case RT_IPTYPE:
4135f4b3ec61Sdh 		if (strcmp(prop_id, "shared") == 0) {
4136f4b3ec61Sdh 			iptype = ZS_SHARED;
4137f4b3ec61Sdh 		} else if (strcmp(prop_id, "exclusive") == 0) {
4138f4b3ec61Sdh 			iptype = ZS_EXCLUSIVE;
4139f4b3ec61Sdh 		} else {
4140f4b3ec61Sdh 			zerr(gettext("%s value must be '%s' or '%s'."),
4141f4b3ec61Sdh 			    pt_to_str(PT_IPTYPE), "shared", "exclusive");
4142bbec428eSgjelinek 			saw_error = B_TRUE;
4143f4b3ec61Sdh 			return;
4144f4b3ec61Sdh 		}
4145f4b3ec61Sdh 		if (iptype == ZS_EXCLUSIVE && !allow_exclusive()) {
4146bbec428eSgjelinek 			saw_error = B_TRUE;
4147f4b3ec61Sdh 			return;
4148f4b3ec61Sdh 		}
4149f4b3ec61Sdh 		if ((err = zonecfg_set_iptype(handle, iptype)) != Z_OK)
4150bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
4151f4b3ec61Sdh 		else
4152bbec428eSgjelinek 			need_to_commit = B_TRUE;
4153f4b3ec61Sdh 		return;
41540209230bSgjelinek 	case RT_MAXLWPS:
41550209230bSgjelinek 		set_aliased_rctl(ALIAS_MAXLWPS, prop_type, prop_id);
41560209230bSgjelinek 		return;
41570209230bSgjelinek 	case RT_MAXSHMMEM:
41580209230bSgjelinek 		set_aliased_rctl(ALIAS_MAXSHMMEM, prop_type, prop_id);
41590209230bSgjelinek 		return;
41600209230bSgjelinek 	case RT_MAXSHMIDS:
41610209230bSgjelinek 		set_aliased_rctl(ALIAS_MAXSHMIDS, prop_type, prop_id);
41620209230bSgjelinek 		return;
41630209230bSgjelinek 	case RT_MAXMSGIDS:
41640209230bSgjelinek 		set_aliased_rctl(ALIAS_MAXMSGIDS, prop_type, prop_id);
41650209230bSgjelinek 		return;
41660209230bSgjelinek 	case RT_MAXSEMIDS:
41670209230bSgjelinek 		set_aliased_rctl(ALIAS_MAXSEMIDS, prop_type, prop_id);
41680209230bSgjelinek 		return;
41690209230bSgjelinek 	case RT_SHARES:
41700209230bSgjelinek 		set_aliased_rctl(ALIAS_SHARES, prop_type, prop_id);
41710209230bSgjelinek 		return;
41725679c89fSjv 	case RT_HOSTID:
41735679c89fSjv 		if ((err = zonecfg_set_hostid(handle, prop_id)) != Z_OK) {
41745679c89fSjv 			if (err == Z_TOO_BIG) {
41755679c89fSjv 				zerr(gettext("hostid string is too large: %s"),
41765679c89fSjv 				    prop_id);
41775679c89fSjv 				saw_error = B_TRUE;
41785679c89fSjv 			} else {
41795679c89fSjv 				zone_perror(pt_to_str(prop_type), err, B_TRUE);
41805679c89fSjv 			}
41815679c89fSjv 			return;
41825679c89fSjv 		}
41835679c89fSjv 		need_to_commit = B_TRUE;
41845679c89fSjv 		return;
41857c478bd9Sstevel@tonic-gate 	case RT_FS:
41867c478bd9Sstevel@tonic-gate 		switch (prop_type) {
41877c478bd9Sstevel@tonic-gate 		case PT_DIR:
41887c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_fstab.zone_fs_dir, prop_id,
41897c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_fstab.zone_fs_dir));
41907c478bd9Sstevel@tonic-gate 			return;
41917c478bd9Sstevel@tonic-gate 		case PT_SPECIAL:
41927c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_fstab.zone_fs_special,
41937c478bd9Sstevel@tonic-gate 			    prop_id,
41947c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_fstab.zone_fs_special));
41957c478bd9Sstevel@tonic-gate 			return;
41967c478bd9Sstevel@tonic-gate 		case PT_RAW:
41977c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_fstab.zone_fs_raw,
41987c478bd9Sstevel@tonic-gate 			    prop_id, sizeof (in_progress_fstab.zone_fs_raw));
41997c478bd9Sstevel@tonic-gate 			return;
42007c478bd9Sstevel@tonic-gate 		case PT_TYPE:
42017c478bd9Sstevel@tonic-gate 			if (!valid_fs_type(prop_id)) {
42027c478bd9Sstevel@tonic-gate 				zerr(gettext("\"%s\" is not a valid %s."),
42037c478bd9Sstevel@tonic-gate 				    prop_id, pt_to_str(PT_TYPE));
4204bbec428eSgjelinek 				saw_error = B_TRUE;
42057c478bd9Sstevel@tonic-gate 				return;
42067c478bd9Sstevel@tonic-gate 			}
42077c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_fstab.zone_fs_type, prop_id,
42087c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_fstab.zone_fs_type));
42097c478bd9Sstevel@tonic-gate 			return;
42107c478bd9Sstevel@tonic-gate 		case PT_OPTIONS:
42117c478bd9Sstevel@tonic-gate 			if (pp->pv_type != PROP_VAL_SIMPLE &&
42127c478bd9Sstevel@tonic-gate 			    pp->pv_type != PROP_VAL_LIST) {
42137c478bd9Sstevel@tonic-gate 				zerr(gettext("A %s or %s value was expected "
42147c478bd9Sstevel@tonic-gate 				    "here."), pvt_to_str(PROP_VAL_SIMPLE),
42157c478bd9Sstevel@tonic-gate 				    pvt_to_str(PROP_VAL_LIST));
4216bbec428eSgjelinek 				saw_error = B_TRUE;
42177c478bd9Sstevel@tonic-gate 				return;
42187c478bd9Sstevel@tonic-gate 			}
42197c478bd9Sstevel@tonic-gate 			zonecfg_free_fs_option_list(
42207c478bd9Sstevel@tonic-gate 			    in_progress_fstab.zone_fs_options);
42217c478bd9Sstevel@tonic-gate 			in_progress_fstab.zone_fs_options = NULL;
42227c478bd9Sstevel@tonic-gate 			if (!(pp->pv_type == PROP_VAL_LIST &&
42237c478bd9Sstevel@tonic-gate 			    pp->pv_list == NULL))
42247c478bd9Sstevel@tonic-gate 				add_property(cmd);
42257c478bd9Sstevel@tonic-gate 			return;
42267c478bd9Sstevel@tonic-gate 		default:
42277c478bd9Sstevel@tonic-gate 			break;
42287c478bd9Sstevel@tonic-gate 		}
4229bbec428eSgjelinek 		zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE, B_TRUE);
4230bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
4231bbec428eSgjelinek 		usage(B_FALSE, HELP_PROPS);
42327c478bd9Sstevel@tonic-gate 		return;
42337c478bd9Sstevel@tonic-gate 	case RT_IPD:
42347c478bd9Sstevel@tonic-gate 		switch (prop_type) {
42357c478bd9Sstevel@tonic-gate 		case PT_DIR:
42367c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_ipdtab.zone_fs_dir, prop_id,
42377c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_ipdtab.zone_fs_dir));
42387c478bd9Sstevel@tonic-gate 			return;
42397c478bd9Sstevel@tonic-gate 		default:
42407c478bd9Sstevel@tonic-gate 			break;
42417c478bd9Sstevel@tonic-gate 		}
4242bbec428eSgjelinek 		zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE, B_TRUE);
4243bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
4244bbec428eSgjelinek 		usage(B_FALSE, HELP_PROPS);
42457c478bd9Sstevel@tonic-gate 		return;
42467c478bd9Sstevel@tonic-gate 	case RT_NET:
42477c478bd9Sstevel@tonic-gate 		switch (prop_type) {
42487c478bd9Sstevel@tonic-gate 		case PT_ADDRESS:
42497c478bd9Sstevel@tonic-gate 			if (validate_net_address_syntax(prop_id) != Z_OK) {
4250bbec428eSgjelinek 				saw_error = B_TRUE;
42517c478bd9Sstevel@tonic-gate 				return;
42527c478bd9Sstevel@tonic-gate 			}
42537c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_nwiftab.zone_nwif_address,
42547c478bd9Sstevel@tonic-gate 			    prop_id,
42557c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_nwiftab.zone_nwif_address));
42567c478bd9Sstevel@tonic-gate 			break;
42577c478bd9Sstevel@tonic-gate 		case PT_PHYSICAL:
42587c478bd9Sstevel@tonic-gate 			if (validate_net_physical_syntax(prop_id) != Z_OK) {
4259bbec428eSgjelinek 				saw_error = B_TRUE;
42607c478bd9Sstevel@tonic-gate 				return;
42617c478bd9Sstevel@tonic-gate 			}
42627c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_nwiftab.zone_nwif_physical,
42637c478bd9Sstevel@tonic-gate 			    prop_id,
42647c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_nwiftab.zone_nwif_physical));
42657c478bd9Sstevel@tonic-gate 			break;
4266de860bd9Sgfaden 		case PT_DEFROUTER:
4267de860bd9Sgfaden 			if (validate_net_address_syntax(prop_id) != Z_OK) {
4268bbec428eSgjelinek 				saw_error = B_TRUE;
4269de860bd9Sgfaden 				return;
4270de860bd9Sgfaden 			}
4271de860bd9Sgfaden 			(void) strlcpy(in_progress_nwiftab.zone_nwif_defrouter,
4272de860bd9Sgfaden 			    prop_id,
4273de860bd9Sgfaden 			    sizeof (in_progress_nwiftab.zone_nwif_defrouter));
4274de860bd9Sgfaden 			break;
42757c478bd9Sstevel@tonic-gate 		default:
42767c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4277bbec428eSgjelinek 			    B_TRUE);
4278bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4279bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
42807c478bd9Sstevel@tonic-gate 			return;
42817c478bd9Sstevel@tonic-gate 		}
42827c478bd9Sstevel@tonic-gate 		return;
42837c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
42847c478bd9Sstevel@tonic-gate 		switch (prop_type) {
42857c478bd9Sstevel@tonic-gate 		case PT_MATCH:
42867c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_devtab.zone_dev_match,
42877c478bd9Sstevel@tonic-gate 			    prop_id,
42887c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_devtab.zone_dev_match));
42897c478bd9Sstevel@tonic-gate 			break;
42907c478bd9Sstevel@tonic-gate 		default:
42917c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4292bbec428eSgjelinek 			    B_TRUE);
4293bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4294bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
42957c478bd9Sstevel@tonic-gate 			return;
42967c478bd9Sstevel@tonic-gate 		}
42977c478bd9Sstevel@tonic-gate 		return;
42987c478bd9Sstevel@tonic-gate 	case RT_RCTL:
42997c478bd9Sstevel@tonic-gate 		switch (prop_type) {
43007c478bd9Sstevel@tonic-gate 		case PT_NAME:
43017c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctlname(prop_id)) {
43027c478bd9Sstevel@tonic-gate 				zerr(gettext("'%s' is not a valid zone %s "
43037c478bd9Sstevel@tonic-gate 				    "name."), prop_id, rt_to_str(RT_RCTL));
43047c478bd9Sstevel@tonic-gate 				return;
43057c478bd9Sstevel@tonic-gate 			}
43067c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_rctltab.zone_rctl_name,
43077c478bd9Sstevel@tonic-gate 			    prop_id,
43087c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_rctltab.zone_rctl_name));
43097c478bd9Sstevel@tonic-gate 			break;
43107c478bd9Sstevel@tonic-gate 		case PT_VALUE:
43117c478bd9Sstevel@tonic-gate 			if (pp->pv_type != PROP_VAL_COMPLEX &&
43127c478bd9Sstevel@tonic-gate 			    pp->pv_type != PROP_VAL_LIST) {
43137c478bd9Sstevel@tonic-gate 				zerr(gettext("A %s or %s value was expected "
43147c478bd9Sstevel@tonic-gate 				    "here."), pvt_to_str(PROP_VAL_COMPLEX),
43157c478bd9Sstevel@tonic-gate 				    pvt_to_str(PROP_VAL_LIST));
4316bbec428eSgjelinek 				saw_error = B_TRUE;
43177c478bd9Sstevel@tonic-gate 				return;
43187c478bd9Sstevel@tonic-gate 			}
43197c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(
43207c478bd9Sstevel@tonic-gate 			    in_progress_rctltab.zone_rctl_valptr);
43217c478bd9Sstevel@tonic-gate 			in_progress_rctltab.zone_rctl_valptr = NULL;
43227c478bd9Sstevel@tonic-gate 			if (!(pp->pv_type == PROP_VAL_LIST &&
43237c478bd9Sstevel@tonic-gate 			    pp->pv_list == NULL))
43247c478bd9Sstevel@tonic-gate 				add_property(cmd);
43257c478bd9Sstevel@tonic-gate 			break;
43267c478bd9Sstevel@tonic-gate 		default:
43277c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4328bbec428eSgjelinek 			    B_TRUE);
4329bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4330bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
43317c478bd9Sstevel@tonic-gate 			return;
43327c478bd9Sstevel@tonic-gate 		}
43337c478bd9Sstevel@tonic-gate 		return;
43347c478bd9Sstevel@tonic-gate 	case RT_ATTR:
43357c478bd9Sstevel@tonic-gate 		switch (prop_type) {
43367c478bd9Sstevel@tonic-gate 		case PT_NAME:
43377c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_attrtab.zone_attr_name,
43387c478bd9Sstevel@tonic-gate 			    prop_id,
43397c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_attrtab.zone_attr_name));
43407c478bd9Sstevel@tonic-gate 			break;
43417c478bd9Sstevel@tonic-gate 		case PT_TYPE:
43427c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_attrtab.zone_attr_type,
43437c478bd9Sstevel@tonic-gate 			    prop_id,
43447c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_attrtab.zone_attr_type));
43457c478bd9Sstevel@tonic-gate 			break;
43467c478bd9Sstevel@tonic-gate 		case PT_VALUE:
43477c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_attrtab.zone_attr_value,
43487c478bd9Sstevel@tonic-gate 			    prop_id,
43497c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_attrtab.zone_attr_value));
43507c478bd9Sstevel@tonic-gate 			break;
43517c478bd9Sstevel@tonic-gate 		default:
43527c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4353bbec428eSgjelinek 			    B_TRUE);
4354bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4355bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
43567c478bd9Sstevel@tonic-gate 			return;
43577c478bd9Sstevel@tonic-gate 		}
43587c478bd9Sstevel@tonic-gate 		return;
4359fa9e4066Sahrens 	case RT_DATASET:
4360fa9e4066Sahrens 		switch (prop_type) {
4361fa9e4066Sahrens 		case PT_NAME:
4362fa9e4066Sahrens 			(void) strlcpy(in_progress_dstab.zone_dataset_name,
4363fa9e4066Sahrens 			    prop_id,
4364fa9e4066Sahrens 			    sizeof (in_progress_dstab.zone_dataset_name));
4365fa9e4066Sahrens 			return;
4366fa9e4066Sahrens 		default:
4367fa9e4066Sahrens 			break;
4368fa9e4066Sahrens 		}
4369bbec428eSgjelinek 		zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE, B_TRUE);
4370bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
4371bbec428eSgjelinek 		usage(B_FALSE, HELP_PROPS);
43720209230bSgjelinek 		return;
43730209230bSgjelinek 	case RT_DCPU:
43740209230bSgjelinek 		switch (prop_type) {
43750209230bSgjelinek 		char *lowp, *highp;
43760209230bSgjelinek 
43770209230bSgjelinek 		case PT_NCPUS:
43780209230bSgjelinek 			lowp = prop_id;
43790209230bSgjelinek 			if ((highp = strchr(prop_id, '-')) != NULL)
43800209230bSgjelinek 				*highp++ = '\0';
43810209230bSgjelinek 			else
43820209230bSgjelinek 				highp = lowp;
43830209230bSgjelinek 
43840209230bSgjelinek 			/* Make sure the input makes sense. */
43850209230bSgjelinek 			if (!zonecfg_valid_ncpus(lowp, highp)) {
43860209230bSgjelinek 				zerr(gettext("%s property is out of range."),
43870209230bSgjelinek 				    pt_to_str(PT_NCPUS));
4388bbec428eSgjelinek 				saw_error = B_TRUE;
43890209230bSgjelinek 				return;
43900209230bSgjelinek 			}
43910209230bSgjelinek 
43920209230bSgjelinek 			(void) strlcpy(
43930209230bSgjelinek 			    in_progress_psettab.zone_ncpu_min, lowp,
43940209230bSgjelinek 			    sizeof (in_progress_psettab.zone_ncpu_min));
43950209230bSgjelinek 			(void) strlcpy(
43960209230bSgjelinek 			    in_progress_psettab.zone_ncpu_max, highp,
43970209230bSgjelinek 			    sizeof (in_progress_psettab.zone_ncpu_max));
43980209230bSgjelinek 			return;
43990209230bSgjelinek 		case PT_IMPORTANCE:
44000209230bSgjelinek 			/* Make sure the value makes sense. */
44010209230bSgjelinek 			if (!zonecfg_valid_importance(prop_id)) {
44020209230bSgjelinek 				zerr(gettext("%s property is out of range."),
44030209230bSgjelinek 				    pt_to_str(PT_IMPORTANCE));
4404bbec428eSgjelinek 				saw_error = B_TRUE;
44050209230bSgjelinek 				return;
44060209230bSgjelinek 			}
44070209230bSgjelinek 
44080209230bSgjelinek 			(void) strlcpy(in_progress_psettab.zone_importance,
44090209230bSgjelinek 			    prop_id,
44100209230bSgjelinek 			    sizeof (in_progress_psettab.zone_importance));
44110209230bSgjelinek 			return;
44120209230bSgjelinek 		default:
44130209230bSgjelinek 			break;
44140209230bSgjelinek 		}
4415bbec428eSgjelinek 		zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE, B_TRUE);
4416bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
4417bbec428eSgjelinek 		usage(B_FALSE, HELP_PROPS);
44180209230bSgjelinek 		return;
4419c97ad5cdSakolb 	case RT_PCAP:
4420c97ad5cdSakolb 		if (prop_type != PT_NCPUS) {
4421c97ad5cdSakolb 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4422bbec428eSgjelinek 			    B_TRUE);
4423bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4424bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
4425c97ad5cdSakolb 			return;
4426c97ad5cdSakolb 		}
4427c97ad5cdSakolb 
4428c97ad5cdSakolb 		/*
4429c97ad5cdSakolb 		 * We already checked that an rctl alias is allowed in
4430c97ad5cdSakolb 		 * the add_resource() function.
4431c97ad5cdSakolb 		 */
4432c97ad5cdSakolb 
4433c97ad5cdSakolb 		if ((cap = strtof(prop_id, &unitp)) <= 0 || *unitp != '\0' ||
4434c97ad5cdSakolb 		    (int)(cap * 100) < 1) {
4435c97ad5cdSakolb 			zerr(gettext("%s property is out of range."),
4436c97ad5cdSakolb 			    pt_to_str(PT_NCPUS));
4437bbec428eSgjelinek 			saw_error = B_TRUE;
4438c97ad5cdSakolb 			return;
4439c97ad5cdSakolb 		}
4440c97ad5cdSakolb 
4441c97ad5cdSakolb 		if ((err = zonecfg_set_aliased_rctl(handle, ALIAS_CPUCAP,
4442c97ad5cdSakolb 		    (int)(cap * 100))) != Z_OK)
4443bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
4444c97ad5cdSakolb 		else
4445bbec428eSgjelinek 			need_to_commit = B_TRUE;
4446c97ad5cdSakolb 		return;
44470209230bSgjelinek 	case RT_MCAP:
44480209230bSgjelinek 		switch (prop_type) {
44490209230bSgjelinek 		case PT_PHYSICAL:
44500209230bSgjelinek 			if (!zonecfg_valid_memlimit(prop_id, &mem_cap)) {
44510209230bSgjelinek 				zerr(gettext("A positive number with a "
44520209230bSgjelinek 				    "required scale suffix (K, M, G or T) was "
44530209230bSgjelinek 				    "expected here."));
4454bbec428eSgjelinek 				saw_error = B_TRUE;
44550209230bSgjelinek 			} else if (mem_cap < ONE_MB) {
44560209230bSgjelinek 				zerr(gettext("%s value is too small.  It must "
44570209230bSgjelinek 				    "be at least 1M."), pt_to_str(PT_PHYSICAL));
4458bbec428eSgjelinek 				saw_error = B_TRUE;
44590209230bSgjelinek 			} else {
44600209230bSgjelinek 				snprintf(in_progress_mcaptab.zone_physmem_cap,
44610209230bSgjelinek 				    physmem_size, "%llu", mem_cap);
44620209230bSgjelinek 			}
44630209230bSgjelinek 			break;
44640209230bSgjelinek 		case PT_SWAP:
44650209230bSgjelinek 			/*
44660209230bSgjelinek 			 * We have to check if an rctl is allowed here since
44670209230bSgjelinek 			 * there might already be a rctl defined that blocks
44680209230bSgjelinek 			 * the alias.
44690209230bSgjelinek 			 */
44700209230bSgjelinek 			if (!zonecfg_aliased_rctl_ok(handle, ALIAS_MAXSWAP)) {
44710209230bSgjelinek 				zone_perror(pt_to_str(PT_MAXSWAP),
4472bbec428eSgjelinek 				    Z_ALIAS_DISALLOW, B_FALSE);
4473bbec428eSgjelinek 				saw_error = B_TRUE;
44740209230bSgjelinek 				return;
44750209230bSgjelinek 			}
44760209230bSgjelinek 
44770209230bSgjelinek 			if (global_zone)
44780209230bSgjelinek 				mem_limit = ONE_MB * 100;
44790209230bSgjelinek 			else
44800209230bSgjelinek 				mem_limit = ONE_MB * 50;
44810209230bSgjelinek 
44820209230bSgjelinek 			if (!zonecfg_valid_memlimit(prop_id, &mem_cap)) {
44830209230bSgjelinek 				zerr(gettext("A positive number with a "
44840209230bSgjelinek 				    "required scale suffix (K, M, G or T) was "
44850209230bSgjelinek 				    "expected here."));
4486bbec428eSgjelinek 				saw_error = B_TRUE;
44870209230bSgjelinek 			} else if (mem_cap < mem_limit) {
44880209230bSgjelinek 				char buf[128];
44890209230bSgjelinek 
44900209230bSgjelinek 				(void) snprintf(buf, sizeof (buf), "%llu",
44910209230bSgjelinek 				    mem_limit);
44920209230bSgjelinek 				bytes_to_units(buf, buf, sizeof (buf));
44930209230bSgjelinek 				zerr(gettext("%s value is too small.  It must "
44940209230bSgjelinek 				    "be at least %s."), pt_to_str(PT_SWAP),
44950209230bSgjelinek 				    buf);
4496bbec428eSgjelinek 				saw_error = B_TRUE;
44970209230bSgjelinek 			} else {
44980209230bSgjelinek 				if ((err = zonecfg_set_aliased_rctl(handle,
44990209230bSgjelinek 				    ALIAS_MAXSWAP, mem_cap)) != Z_OK)
4500bbec428eSgjelinek 					zone_perror(zone, err, B_TRUE);
45010209230bSgjelinek 				else
4502bbec428eSgjelinek 					need_to_commit = B_TRUE;
45030209230bSgjelinek 			}
45040209230bSgjelinek 			break;
45050209230bSgjelinek 		case PT_LOCKED:
45060209230bSgjelinek 			/*
45070209230bSgjelinek 			 * We have to check if an rctl is allowed here since
45080209230bSgjelinek 			 * there might already be a rctl defined that blocks
45090209230bSgjelinek 			 * the alias.
45100209230bSgjelinek 			 */
45110209230bSgjelinek 			if (!zonecfg_aliased_rctl_ok(handle,
45120209230bSgjelinek 			    ALIAS_MAXLOCKEDMEM)) {
45130209230bSgjelinek 				zone_perror(pt_to_str(PT_LOCKED),
4514bbec428eSgjelinek 				    Z_ALIAS_DISALLOW, B_FALSE);
4515bbec428eSgjelinek 				saw_error = B_TRUE;
45160209230bSgjelinek 				return;
45170209230bSgjelinek 			}
45180209230bSgjelinek 
45190209230bSgjelinek 			if (!zonecfg_valid_memlimit(prop_id, &mem_cap)) {
45200209230bSgjelinek 				zerr(gettext("A non-negative number with a "
45210209230bSgjelinek 				    "required scale suffix (K, M, G or T) was "
45220209230bSgjelinek 				    "expected\nhere."));
4523bbec428eSgjelinek 				saw_error = B_TRUE;
45240209230bSgjelinek 			} else {
45250209230bSgjelinek 				if ((err = zonecfg_set_aliased_rctl(handle,
45260209230bSgjelinek 				    ALIAS_MAXLOCKEDMEM, mem_cap)) != Z_OK)
4527bbec428eSgjelinek 					zone_perror(zone, err, B_TRUE);
45280209230bSgjelinek 				else
4529bbec428eSgjelinek 					need_to_commit = B_TRUE;
45300209230bSgjelinek 			}
45310209230bSgjelinek 			break;
45320209230bSgjelinek 		default:
45330209230bSgjelinek 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4534bbec428eSgjelinek 			    B_TRUE);
4535bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4536bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
45370209230bSgjelinek 			return;
45380209230bSgjelinek 		}
45390209230bSgjelinek 
4540fa9e4066Sahrens 		return;
45417c478bd9Sstevel@tonic-gate 	default:
4542bbec428eSgjelinek 		zone_perror(rt_to_str(res_type), Z_NO_RESOURCE_TYPE, B_TRUE);
4543bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
4544bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
45457c478bd9Sstevel@tonic-gate 		return;
45467c478bd9Sstevel@tonic-gate 	}
45477c478bd9Sstevel@tonic-gate }
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate static void
4550bbec428eSgjelinek output_prop(FILE *fp, int pnum, char *pval, boolean_t print_notspec)
45517c478bd9Sstevel@tonic-gate {
45527c478bd9Sstevel@tonic-gate 	char *qstr;
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate 	if (*pval != '\0') {
45557c478bd9Sstevel@tonic-gate 		qstr = quoteit(pval);
45560209230bSgjelinek 		if (pnum == PT_SWAP || pnum == PT_LOCKED)
45570209230bSgjelinek 			(void) fprintf(fp, "\t[%s: %s]\n", pt_to_str(pnum),
45580209230bSgjelinek 			    qstr);
45590209230bSgjelinek 		else
45600209230bSgjelinek 			(void) fprintf(fp, "\t%s: %s\n", pt_to_str(pnum), qstr);
45617c478bd9Sstevel@tonic-gate 		free(qstr);
45627c478bd9Sstevel@tonic-gate 	} else if (print_notspec)
4563087719fdSdp 		(void) fprintf(fp, gettext("\t%s not specified\n"),
4564087719fdSdp 		    pt_to_str(pnum));
4565087719fdSdp }
4566087719fdSdp 
4567087719fdSdp static void
4568087719fdSdp info_zonename(zone_dochandle_t handle, FILE *fp)
4569087719fdSdp {
4570087719fdSdp 	char zonename[ZONENAME_MAX];
4571087719fdSdp 
4572087719fdSdp 	if (zonecfg_get_name(handle, zonename, sizeof (zonename)) == Z_OK)
4573087719fdSdp 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_ZONENAME),
4574087719fdSdp 		    zonename);
4575087719fdSdp 	else
4576087719fdSdp 		(void) fprintf(fp, gettext("%s not specified\n"),
4577087719fdSdp 		    pt_to_str(PT_ZONENAME));
45787c478bd9Sstevel@tonic-gate }
45797c478bd9Sstevel@tonic-gate 
45807c478bd9Sstevel@tonic-gate static void
45817c478bd9Sstevel@tonic-gate info_zonepath(zone_dochandle_t handle, FILE *fp)
45827c478bd9Sstevel@tonic-gate {
45837c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
45847c478bd9Sstevel@tonic-gate 
45857c478bd9Sstevel@tonic-gate 	if (zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath)) == Z_OK)
45867c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_ZONEPATH),
45877c478bd9Sstevel@tonic-gate 		    zonepath);
4588087719fdSdp 	else {
4589087719fdSdp 		(void) fprintf(fp, gettext("%s not specified\n"),
4590087719fdSdp 		    pt_to_str(PT_ZONEPATH));
4591087719fdSdp 	}
45927c478bd9Sstevel@tonic-gate }
45937c478bd9Sstevel@tonic-gate 
45949acbbeafSnn static void
45959acbbeafSnn info_brand(zone_dochandle_t handle, FILE *fp)
45969acbbeafSnn {
45979acbbeafSnn 	char brand[MAXNAMELEN];
45989acbbeafSnn 
45999acbbeafSnn 	if (zonecfg_get_brand(handle, brand, sizeof (brand)) == Z_OK)
46009acbbeafSnn 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_BRAND),
46019acbbeafSnn 		    brand);
46029acbbeafSnn 	else
46039acbbeafSnn 		(void) fprintf(fp, "%s %s\n", pt_to_str(PT_BRAND),
46049acbbeafSnn 		    gettext("not specified"));
46059acbbeafSnn }
46069acbbeafSnn 
46077c478bd9Sstevel@tonic-gate static void
46087c478bd9Sstevel@tonic-gate info_autoboot(zone_dochandle_t handle, FILE *fp)
46097c478bd9Sstevel@tonic-gate {
46107c478bd9Sstevel@tonic-gate 	boolean_t autoboot;
46117c478bd9Sstevel@tonic-gate 	int err;
46127c478bd9Sstevel@tonic-gate 
46137c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_get_autoboot(handle, &autoboot)) == Z_OK)
46147c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_AUTOBOOT),
46157c478bd9Sstevel@tonic-gate 		    autoboot ? "true" : "false");
46167c478bd9Sstevel@tonic-gate 	else
4617bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
46187c478bd9Sstevel@tonic-gate }
46197c478bd9Sstevel@tonic-gate 
46207c478bd9Sstevel@tonic-gate static void
46217c478bd9Sstevel@tonic-gate info_pool(zone_dochandle_t handle, FILE *fp)
46227c478bd9Sstevel@tonic-gate {
46237c478bd9Sstevel@tonic-gate 	char pool[MAXNAMELEN];
46247c478bd9Sstevel@tonic-gate 	int err;
46257c478bd9Sstevel@tonic-gate 
46267c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_get_pool(handle, pool, sizeof (pool))) == Z_OK)
46277c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_POOL), pool);
46287c478bd9Sstevel@tonic-gate 	else
4629bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
46307c478bd9Sstevel@tonic-gate }
46317c478bd9Sstevel@tonic-gate 
4632ffbafc53Scomay static void
4633ffbafc53Scomay info_limitpriv(zone_dochandle_t handle, FILE *fp)
4634ffbafc53Scomay {
4635ffbafc53Scomay 	char *limitpriv;
4636ffbafc53Scomay 	int err;
4637ffbafc53Scomay 
4638ffbafc53Scomay 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) == Z_OK) {
4639ffbafc53Scomay 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_LIMITPRIV),
4640ffbafc53Scomay 		    limitpriv);
4641ffbafc53Scomay 		free(limitpriv);
4642ffbafc53Scomay 	} else {
4643bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
4644ffbafc53Scomay 	}
4645ffbafc53Scomay }
4646ffbafc53Scomay 
46473f2f09c1Sdp static void
46483f2f09c1Sdp info_bootargs(zone_dochandle_t handle, FILE *fp)
46493f2f09c1Sdp {
46503f2f09c1Sdp 	char bootargs[BOOTARGS_MAX];
46513f2f09c1Sdp 	int err;
46523f2f09c1Sdp 
46533f2f09c1Sdp 	if ((err = zonecfg_get_bootargs(handle, bootargs,
46543f2f09c1Sdp 	    sizeof (bootargs))) == Z_OK) {
46553f2f09c1Sdp 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_BOOTARGS),
46563f2f09c1Sdp 		    bootargs);
46573f2f09c1Sdp 	} else {
4658bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
46593f2f09c1Sdp 	}
46603f2f09c1Sdp }
46613f2f09c1Sdp 
46620209230bSgjelinek static void
46630209230bSgjelinek info_sched(zone_dochandle_t handle, FILE *fp)
46640209230bSgjelinek {
46650209230bSgjelinek 	char sched[MAXNAMELEN];
46660209230bSgjelinek 	int err;
46670209230bSgjelinek 
46680209230bSgjelinek 	if ((err = zonecfg_get_sched_class(handle, sched, sizeof (sched)))
46690209230bSgjelinek 	    == Z_OK) {
46700209230bSgjelinek 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_SCHED), sched);
46710209230bSgjelinek 	} else {
4672bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
46730209230bSgjelinek 	}
46740209230bSgjelinek }
46750209230bSgjelinek 
4676f4b3ec61Sdh static void
4677f4b3ec61Sdh info_iptype(zone_dochandle_t handle, FILE *fp)
4678f4b3ec61Sdh {
4679f4b3ec61Sdh 	zone_iptype_t iptype;
4680f4b3ec61Sdh 	int err;
4681f4b3ec61Sdh 
4682f4b3ec61Sdh 	if ((err = zonecfg_get_iptype(handle, &iptype)) == Z_OK) {
4683f4b3ec61Sdh 		switch (iptype) {
4684f4b3ec61Sdh 		case ZS_SHARED:
4685f4b3ec61Sdh 			(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_IPTYPE),
4686f4b3ec61Sdh 			    "shared");
4687f4b3ec61Sdh 			break;
4688f4b3ec61Sdh 		case ZS_EXCLUSIVE:
4689f4b3ec61Sdh 			(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_IPTYPE),
4690f4b3ec61Sdh 			    "exclusive");
4691f4b3ec61Sdh 			break;
4692f4b3ec61Sdh 		}
4693f4b3ec61Sdh 	} else {
4694bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
4695f4b3ec61Sdh 	}
4696f4b3ec61Sdh }
4697f4b3ec61Sdh 
46985679c89fSjv static void
46995679c89fSjv info_hostid(zone_dochandle_t handle, FILE *fp)
47005679c89fSjv {
47015679c89fSjv 	char hostidp[HW_HOSTID_LEN];
47025679c89fSjv 
47035679c89fSjv 	/*
47045679c89fSjv 	 * This will display "hostid: " if there isn't a hostid or an
47055679c89fSjv 	 * error occurs while retrieving the hostid from the configuration
47065679c89fSjv 	 * file.
47075679c89fSjv 	 */
47085679c89fSjv 	if (zonecfg_get_hostid(handle, hostidp, sizeof (hostidp)) != Z_OK)
47095679c89fSjv 		hostidp[0] = '\0';
47105679c89fSjv 	(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_HOSTID), hostidp);
47115679c89fSjv }
47125679c89fSjv 
47137c478bd9Sstevel@tonic-gate static void
47147c478bd9Sstevel@tonic-gate output_fs(FILE *fp, struct zone_fstab *fstab)
47157c478bd9Sstevel@tonic-gate {
47167c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this;
47177c478bd9Sstevel@tonic-gate 
47187c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_FS));
47197c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_DIR, fstab->zone_fs_dir, B_TRUE);
47207c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_SPECIAL, fstab->zone_fs_special, B_TRUE);
47217c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_RAW, fstab->zone_fs_raw, B_TRUE);
47227c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_TYPE, fstab->zone_fs_type, B_TRUE);
47237c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\t%s: [", pt_to_str(PT_OPTIONS));
47247c478bd9Sstevel@tonic-gate 	for (this = fstab->zone_fs_options; this != NULL;
47257c478bd9Sstevel@tonic-gate 	    this = this->zone_fsopt_next) {
47267c478bd9Sstevel@tonic-gate 		if (strchr(this->zone_fsopt_opt, '='))
47277c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\"%s\"", this->zone_fsopt_opt);
47287c478bd9Sstevel@tonic-gate 		else
47297c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%s", this->zone_fsopt_opt);
47307c478bd9Sstevel@tonic-gate 		if (this->zone_fsopt_next != NULL)
47317c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, ",");
47327c478bd9Sstevel@tonic-gate 	}
47337c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "]\n");
47347c478bd9Sstevel@tonic-gate }
47357c478bd9Sstevel@tonic-gate 
47367c478bd9Sstevel@tonic-gate static void
47377c478bd9Sstevel@tonic-gate output_ipd(FILE *fp, struct zone_fstab *ipdtab)
47387c478bd9Sstevel@tonic-gate {
47397c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_IPD));
47407c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_DIR, ipdtab->zone_fs_dir, B_TRUE);
47417c478bd9Sstevel@tonic-gate }
47427c478bd9Sstevel@tonic-gate 
47437c478bd9Sstevel@tonic-gate static void
47447c478bd9Sstevel@tonic-gate info_fs(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
47457c478bd9Sstevel@tonic-gate {
47467c478bd9Sstevel@tonic-gate 	struct zone_fstab lookup, user;
4747bbec428eSgjelinek 	boolean_t output = B_FALSE;
47487c478bd9Sstevel@tonic-gate 
47497c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK)
47507c478bd9Sstevel@tonic-gate 		return;
47517c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &lookup) == Z_OK) {
47527c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
47537c478bd9Sstevel@tonic-gate 			output_fs(fp, &lookup);
47547c478bd9Sstevel@tonic-gate 			goto loopend;
47557c478bd9Sstevel@tonic-gate 		}
4756bbec428eSgjelinek 		if (fill_in_fstab(cmd, &user, B_TRUE) != Z_OK)
47577c478bd9Sstevel@tonic-gate 			goto loopend;
47587c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_fs_dir) > 0 &&
47597c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_fs_dir, lookup.zone_fs_dir) != 0)
47607c478bd9Sstevel@tonic-gate 			goto loopend;	/* no match */
47617c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_fs_special) > 0 &&
47627c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_fs_special, lookup.zone_fs_special) != 0)
47637c478bd9Sstevel@tonic-gate 			goto loopend;	/* no match */
47647c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_fs_type) > 0 &&
47657c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_fs_type, lookup.zone_fs_type) != 0)
47667c478bd9Sstevel@tonic-gate 			goto loopend;	/* no match */
47677c478bd9Sstevel@tonic-gate 		output_fs(fp, &lookup);
4768bbec428eSgjelinek 		output = B_TRUE;
47697c478bd9Sstevel@tonic-gate loopend:
47707c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(lookup.zone_fs_options);
47717c478bd9Sstevel@tonic-gate 	}
47727c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
47737c478bd9Sstevel@tonic-gate 	/*
47747c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
47757c478bd9Sstevel@tonic-gate 	 * nothing to output.
47767c478bd9Sstevel@tonic-gate 	 */
47777c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
47787c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
47797c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_FS));
47807c478bd9Sstevel@tonic-gate }
47817c478bd9Sstevel@tonic-gate 
47827c478bd9Sstevel@tonic-gate static void
47837c478bd9Sstevel@tonic-gate info_ipd(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
47847c478bd9Sstevel@tonic-gate {
47857c478bd9Sstevel@tonic-gate 	struct zone_fstab lookup, user;
4786bbec428eSgjelinek 	boolean_t output = B_FALSE;
47877c478bd9Sstevel@tonic-gate 
47887c478bd9Sstevel@tonic-gate 	if (zonecfg_setipdent(handle) != Z_OK)
47897c478bd9Sstevel@tonic-gate 		return;
47907c478bd9Sstevel@tonic-gate 	while (zonecfg_getipdent(handle, &lookup) == Z_OK) {
47917c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
47927c478bd9Sstevel@tonic-gate 			output_ipd(fp, &lookup);
47937c478bd9Sstevel@tonic-gate 			continue;
47947c478bd9Sstevel@tonic-gate 		}
4795bbec428eSgjelinek 		if (fill_in_ipdtab(cmd, &user, B_TRUE) != Z_OK)
47967c478bd9Sstevel@tonic-gate 			continue;
47977c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_fs_dir) > 0 &&
47987c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_fs_dir, lookup.zone_fs_dir) != 0)
47997c478bd9Sstevel@tonic-gate 			continue;	/* no match */
48007c478bd9Sstevel@tonic-gate 		output_ipd(fp, &lookup);
4801bbec428eSgjelinek 		output = B_TRUE;
48027c478bd9Sstevel@tonic-gate 	}
48037c478bd9Sstevel@tonic-gate 	(void) zonecfg_endipdent(handle);
48047c478bd9Sstevel@tonic-gate 	/*
48057c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
48067c478bd9Sstevel@tonic-gate 	 * nothing to output.
48077c478bd9Sstevel@tonic-gate 	 */
48087c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
48097c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
48107c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_IPD));
48117c478bd9Sstevel@tonic-gate }
48127c478bd9Sstevel@tonic-gate 
48137c478bd9Sstevel@tonic-gate static void
48147c478bd9Sstevel@tonic-gate output_net(FILE *fp, struct zone_nwiftab *nwiftab)
48157c478bd9Sstevel@tonic-gate {
48167c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_NET));
48177c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_ADDRESS, nwiftab->zone_nwif_address, B_TRUE);
48187c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_PHYSICAL, nwiftab->zone_nwif_physical, B_TRUE);
4819de860bd9Sgfaden 	output_prop(fp, PT_DEFROUTER, nwiftab->zone_nwif_defrouter, B_TRUE);
48207c478bd9Sstevel@tonic-gate }
48217c478bd9Sstevel@tonic-gate 
48227c478bd9Sstevel@tonic-gate static void
48237c478bd9Sstevel@tonic-gate info_net(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
48247c478bd9Sstevel@tonic-gate {
48257c478bd9Sstevel@tonic-gate 	struct zone_nwiftab lookup, user;
4826bbec428eSgjelinek 	boolean_t output = B_FALSE;
48277c478bd9Sstevel@tonic-gate 
48287c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) != Z_OK)
48297c478bd9Sstevel@tonic-gate 		return;
48307c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &lookup) == Z_OK) {
48317c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
48327c478bd9Sstevel@tonic-gate 			output_net(fp, &lookup);
48337c478bd9Sstevel@tonic-gate 			continue;
48347c478bd9Sstevel@tonic-gate 		}
4835bbec428eSgjelinek 		if (fill_in_nwiftab(cmd, &user, B_TRUE) != Z_OK)
48367c478bd9Sstevel@tonic-gate 			continue;
48377c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_nwif_physical) > 0 &&
48387c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_nwif_physical,
48397c478bd9Sstevel@tonic-gate 		    lookup.zone_nwif_physical) != 0)
48407c478bd9Sstevel@tonic-gate 			continue;	/* no match */
4841f4b3ec61Sdh 		/* If present make sure it matches */
48427c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_nwif_address) > 0 &&
48437c478bd9Sstevel@tonic-gate 		    !zonecfg_same_net_address(user.zone_nwif_address,
48447c478bd9Sstevel@tonic-gate 		    lookup.zone_nwif_address))
48457c478bd9Sstevel@tonic-gate 			continue;	/* no match */
48467c478bd9Sstevel@tonic-gate 		output_net(fp, &lookup);
4847bbec428eSgjelinek 		output = B_TRUE;
48487c478bd9Sstevel@tonic-gate 	}
48497c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
48507c478bd9Sstevel@tonic-gate 	/*
48517c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
48527c478bd9Sstevel@tonic-gate 	 * nothing to output.
48537c478bd9Sstevel@tonic-gate 	 */
48547c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
48557c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
48567c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_NET));
48577c478bd9Sstevel@tonic-gate }
48587c478bd9Sstevel@tonic-gate 
48597c478bd9Sstevel@tonic-gate static void
48607c478bd9Sstevel@tonic-gate output_dev(FILE *fp, struct zone_devtab *devtab)
48617c478bd9Sstevel@tonic-gate {
486227e6fb21Sdp 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_DEVICE));
48637c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_MATCH, devtab->zone_dev_match, B_TRUE);
48647c478bd9Sstevel@tonic-gate }
48657c478bd9Sstevel@tonic-gate 
48667c478bd9Sstevel@tonic-gate static void
48677c478bd9Sstevel@tonic-gate info_dev(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
48687c478bd9Sstevel@tonic-gate {
48697c478bd9Sstevel@tonic-gate 	struct zone_devtab lookup, user;
4870bbec428eSgjelinek 	boolean_t output = B_FALSE;
48717c478bd9Sstevel@tonic-gate 
48727c478bd9Sstevel@tonic-gate 	if (zonecfg_setdevent(handle) != Z_OK)
48737c478bd9Sstevel@tonic-gate 		return;
48747c478bd9Sstevel@tonic-gate 	while (zonecfg_getdevent(handle, &lookup) == Z_OK) {
48757c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
48767c478bd9Sstevel@tonic-gate 			output_dev(fp, &lookup);
48777c478bd9Sstevel@tonic-gate 			continue;
48787c478bd9Sstevel@tonic-gate 		}
4879bbec428eSgjelinek 		if (fill_in_devtab(cmd, &user, B_TRUE) != Z_OK)
48807c478bd9Sstevel@tonic-gate 			continue;
48817c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_dev_match) > 0 &&
48827c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_dev_match, lookup.zone_dev_match) != 0)
48837c478bd9Sstevel@tonic-gate 			continue;	/* no match */
48847c478bd9Sstevel@tonic-gate 		output_dev(fp, &lookup);
4885bbec428eSgjelinek 		output = B_TRUE;
48867c478bd9Sstevel@tonic-gate 	}
48877c478bd9Sstevel@tonic-gate 	(void) zonecfg_enddevent(handle);
48887c478bd9Sstevel@tonic-gate 	/*
48897c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
48907c478bd9Sstevel@tonic-gate 	 * nothing to output.
48917c478bd9Sstevel@tonic-gate 	 */
48927c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
48937c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
48947c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_DEVICE));
48957c478bd9Sstevel@tonic-gate }
48967c478bd9Sstevel@tonic-gate 
48977c478bd9Sstevel@tonic-gate static void
48987c478bd9Sstevel@tonic-gate output_rctl(FILE *fp, struct zone_rctltab *rctltab)
48997c478bd9Sstevel@tonic-gate {
49007c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
49017c478bd9Sstevel@tonic-gate 
49027c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_RCTL));
49037c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_NAME, rctltab->zone_rctl_name, B_TRUE);
49047c478bd9Sstevel@tonic-gate 	for (valptr = rctltab->zone_rctl_valptr; valptr != NULL;
49057c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
49067c478bd9Sstevel@tonic-gate 		fprintf(fp, "\t%s: (%s=%s,%s=%s,%s=%s)\n",
49077c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_VALUE),
49087c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_PRIV), valptr->zone_rctlval_priv,
49097c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_LIMIT), valptr->zone_rctlval_limit,
49107c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_ACTION), valptr->zone_rctlval_action);
49117c478bd9Sstevel@tonic-gate 	}
49127c478bd9Sstevel@tonic-gate }
49137c478bd9Sstevel@tonic-gate 
49147c478bd9Sstevel@tonic-gate static void
49157c478bd9Sstevel@tonic-gate info_rctl(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
49167c478bd9Sstevel@tonic-gate {
49177c478bd9Sstevel@tonic-gate 	struct zone_rctltab lookup, user;
4918bbec428eSgjelinek 	boolean_t output = B_FALSE;
49197c478bd9Sstevel@tonic-gate 
49207c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK)
49217c478bd9Sstevel@tonic-gate 		return;
49227c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &lookup) == Z_OK) {
49237c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
49247c478bd9Sstevel@tonic-gate 			output_rctl(fp, &lookup);
4925bbec428eSgjelinek 		} else if (fill_in_rctltab(cmd, &user, B_TRUE) == Z_OK &&
49267c478bd9Sstevel@tonic-gate 		    (strlen(user.zone_rctl_name) == 0 ||
49277c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_rctl_name, lookup.zone_rctl_name) == 0)) {
49287c478bd9Sstevel@tonic-gate 			output_rctl(fp, &lookup);
4929bbec428eSgjelinek 			output = B_TRUE;
49307c478bd9Sstevel@tonic-gate 		}
49317c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(lookup.zone_rctl_valptr);
49327c478bd9Sstevel@tonic-gate 	}
49337c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
49347c478bd9Sstevel@tonic-gate 	/*
49357c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
49367c478bd9Sstevel@tonic-gate 	 * nothing to output.
49377c478bd9Sstevel@tonic-gate 	 */
49387c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
49397c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
49407c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_RCTL));
49417c478bd9Sstevel@tonic-gate }
49427c478bd9Sstevel@tonic-gate 
49437c478bd9Sstevel@tonic-gate static void
49447c478bd9Sstevel@tonic-gate output_attr(FILE *fp, struct zone_attrtab *attrtab)
49457c478bd9Sstevel@tonic-gate {
49467c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_ATTR));
49477c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_NAME, attrtab->zone_attr_name, B_TRUE);
49487c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_TYPE, attrtab->zone_attr_type, B_TRUE);
49497c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_VALUE, attrtab->zone_attr_value, B_TRUE);
49507c478bd9Sstevel@tonic-gate }
49517c478bd9Sstevel@tonic-gate 
49527c478bd9Sstevel@tonic-gate static void
49537c478bd9Sstevel@tonic-gate info_attr(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
49547c478bd9Sstevel@tonic-gate {
49557c478bd9Sstevel@tonic-gate 	struct zone_attrtab lookup, user;
4956bbec428eSgjelinek 	boolean_t output = B_FALSE;
49577c478bd9Sstevel@tonic-gate 
49587c478bd9Sstevel@tonic-gate 	if (zonecfg_setattrent(handle) != Z_OK)
49597c478bd9Sstevel@tonic-gate 		return;
49607c478bd9Sstevel@tonic-gate 	while (zonecfg_getattrent(handle, &lookup) == Z_OK) {
49617c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
49627c478bd9Sstevel@tonic-gate 			output_attr(fp, &lookup);
49637c478bd9Sstevel@tonic-gate 			continue;
49647c478bd9Sstevel@tonic-gate 		}
4965bbec428eSgjelinek 		if (fill_in_attrtab(cmd, &user, B_TRUE) != Z_OK)
49667c478bd9Sstevel@tonic-gate 			continue;
49677c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_attr_name) > 0 &&
49687c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_attr_name, lookup.zone_attr_name) != 0)
49697c478bd9Sstevel@tonic-gate 			continue;	/* no match */
49707c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_attr_type) > 0 &&
49717c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_attr_type, lookup.zone_attr_type) != 0)
49727c478bd9Sstevel@tonic-gate 			continue;	/* no match */
49737c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_attr_value) > 0 &&
49747c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_attr_value, lookup.zone_attr_value) != 0)
49757c478bd9Sstevel@tonic-gate 			continue;	/* no match */
49767c478bd9Sstevel@tonic-gate 		output_attr(fp, &lookup);
4977bbec428eSgjelinek 		output = B_TRUE;
49787c478bd9Sstevel@tonic-gate 	}
49797c478bd9Sstevel@tonic-gate 	(void) zonecfg_endattrent(handle);
49807c478bd9Sstevel@tonic-gate 	/*
49817c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
49827c478bd9Sstevel@tonic-gate 	 * nothing to output.
49837c478bd9Sstevel@tonic-gate 	 */
49847c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
49857c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
49867c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR));
49877c478bd9Sstevel@tonic-gate }
49887c478bd9Sstevel@tonic-gate 
4989fa9e4066Sahrens static void
4990fa9e4066Sahrens output_ds(FILE *fp, struct zone_dstab *dstab)
4991fa9e4066Sahrens {
4992fa9e4066Sahrens 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_DATASET));
4993fa9e4066Sahrens 	output_prop(fp, PT_NAME, dstab->zone_dataset_name, B_TRUE);
4994fa9e4066Sahrens }
4995fa9e4066Sahrens 
4996fa9e4066Sahrens static void
4997fa9e4066Sahrens info_ds(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
4998fa9e4066Sahrens {
4999fa9e4066Sahrens 	struct zone_dstab lookup, user;
5000bbec428eSgjelinek 	boolean_t output = B_FALSE;
5001fa9e4066Sahrens 
50020209230bSgjelinek 	if (zonecfg_setdsent(handle) != Z_OK)
5003fa9e4066Sahrens 		return;
5004fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &lookup) == Z_OK) {
5005fa9e4066Sahrens 		if (cmd->cmd_prop_nv_pairs == 0) {
5006fa9e4066Sahrens 			output_ds(fp, &lookup);
5007fa9e4066Sahrens 			continue;
5008fa9e4066Sahrens 		}
5009bbec428eSgjelinek 		if (fill_in_dstab(cmd, &user, B_TRUE) != Z_OK)
5010fa9e4066Sahrens 			continue;
5011fa9e4066Sahrens 		if (strlen(user.zone_dataset_name) > 0 &&
5012fa9e4066Sahrens 		    strcmp(user.zone_dataset_name,
5013fa9e4066Sahrens 		    lookup.zone_dataset_name) != 0)
5014fa9e4066Sahrens 			continue;	/* no match */
5015fa9e4066Sahrens 		output_ds(fp, &lookup);
5016bbec428eSgjelinek 		output = B_TRUE;
5017fa9e4066Sahrens 	}
5018fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
5019fa9e4066Sahrens 	/*
5020fa9e4066Sahrens 	 * If a property n/v pair was specified, warn the user if there was
5021fa9e4066Sahrens 	 * nothing to output.
5022fa9e4066Sahrens 	 */
5023fa9e4066Sahrens 	if (!output && cmd->cmd_prop_nv_pairs > 0)
5024fa9e4066Sahrens 		(void) printf(gettext("No such %s resource.\n"),
5025fa9e4066Sahrens 		    rt_to_str(RT_DATASET));
5026fa9e4066Sahrens }
5027fa9e4066Sahrens 
50280209230bSgjelinek static void
50290209230bSgjelinek output_pset(FILE *fp, struct zone_psettab *psettab)
50300209230bSgjelinek {
50310209230bSgjelinek 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_DCPU));
50320209230bSgjelinek 	if (strcmp(psettab->zone_ncpu_min, psettab->zone_ncpu_max) == 0)
50330209230bSgjelinek 		(void) fprintf(fp, "\t%s: %s\n", pt_to_str(PT_NCPUS),
50340209230bSgjelinek 		    psettab->zone_ncpu_max);
50350209230bSgjelinek 	else
50360209230bSgjelinek 		(void) fprintf(fp, "\t%s: %s-%s\n", pt_to_str(PT_NCPUS),
50370209230bSgjelinek 		    psettab->zone_ncpu_min, psettab->zone_ncpu_max);
50380209230bSgjelinek 	if (psettab->zone_importance[0] != '\0')
50390209230bSgjelinek 		(void) fprintf(fp, "\t%s: %s\n", pt_to_str(PT_IMPORTANCE),
50400209230bSgjelinek 		    psettab->zone_importance);
50410209230bSgjelinek }
50420209230bSgjelinek 
50430209230bSgjelinek static void
50440209230bSgjelinek info_pset(zone_dochandle_t handle, FILE *fp)
50450209230bSgjelinek {
50460209230bSgjelinek 	struct zone_psettab lookup;
50470209230bSgjelinek 
50480209230bSgjelinek 	if (zonecfg_getpsetent(handle, &lookup) == Z_OK)
50490209230bSgjelinek 		output_pset(fp, &lookup);
50500209230bSgjelinek }
50510209230bSgjelinek 
5052c97ad5cdSakolb static void
5053c97ad5cdSakolb output_pcap(FILE *fp)
5054c97ad5cdSakolb {
5055c97ad5cdSakolb 	uint64_t cap;
5056c97ad5cdSakolb 
5057c97ad5cdSakolb 	if (zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &cap) == Z_OK) {
5058c97ad5cdSakolb 		float scaled = (float)cap / 100;
5059c97ad5cdSakolb 		(void) fprintf(fp, "%s:\n", rt_to_str(RT_PCAP));
5060c97ad5cdSakolb 		(void) fprintf(fp, "\t[%s: %.2f]\n", pt_to_str(PT_NCPUS),
5061c97ad5cdSakolb 		    scaled);
5062c97ad5cdSakolb 	}
5063c97ad5cdSakolb }
5064c97ad5cdSakolb 
5065c97ad5cdSakolb static void
5066c97ad5cdSakolb info_pcap(FILE *fp)
5067c97ad5cdSakolb {
5068c97ad5cdSakolb 	output_pcap(fp);
5069c97ad5cdSakolb }
5070c97ad5cdSakolb 
5071c97ad5cdSakolb 
50720209230bSgjelinek static void
50730209230bSgjelinek info_aliased_rctl(zone_dochandle_t handle, FILE *fp, char *alias)
50740209230bSgjelinek {
50750209230bSgjelinek 	uint64_t limit;
50760209230bSgjelinek 
50770209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, alias, &limit) == Z_OK) {
50780209230bSgjelinek 		/* convert memory based properties */
50790209230bSgjelinek 		if (strcmp(alias, ALIAS_MAXSHMMEM) == 0) {
50800209230bSgjelinek 			char buf[128];
50810209230bSgjelinek 
50820209230bSgjelinek 			(void) snprintf(buf, sizeof (buf), "%llu", limit);
50830209230bSgjelinek 			bytes_to_units(buf, buf, sizeof (buf));
50840209230bSgjelinek 			(void) fprintf(fp, "[%s: %s]\n", alias, buf);
50850209230bSgjelinek 			return;
50860209230bSgjelinek 		}
50870209230bSgjelinek 
50880209230bSgjelinek 		(void) fprintf(fp, "[%s: %llu]\n", alias, limit);
50890209230bSgjelinek 	}
50900209230bSgjelinek }
50910209230bSgjelinek 
50920209230bSgjelinek static void
50930209230bSgjelinek bytes_to_units(char *str, char *buf, int bufsize)
50940209230bSgjelinek {
50950209230bSgjelinek 	unsigned long long num;
50960209230bSgjelinek 	unsigned long long save = 0;
50970209230bSgjelinek 	char *units = "BKMGT";
50980209230bSgjelinek 	char *up = units;
50990209230bSgjelinek 
51000209230bSgjelinek 	num = strtoll(str, NULL, 10);
51010209230bSgjelinek 
51020209230bSgjelinek 	if (num < 1024) {
51030209230bSgjelinek 		(void) snprintf(buf, bufsize, "%llu", num);
51040209230bSgjelinek 		return;
51050209230bSgjelinek 	}
51060209230bSgjelinek 
51070209230bSgjelinek 	while ((num >= 1024) && (*up != 'T')) {
51080209230bSgjelinek 		up++; /* next unit of measurement */
51090209230bSgjelinek 		save = num;
51100209230bSgjelinek 		num = (num + 512) >> 10;
51110209230bSgjelinek 	}
51120209230bSgjelinek 
51130209230bSgjelinek 	/* check if we should output a fraction.  snprintf will round for us */
51140209230bSgjelinek 	if (save % 1024 != 0 && ((save >> 10) < 10))
51150209230bSgjelinek 		(void) snprintf(buf, bufsize, "%2.1f%c", ((float)save / 1024),
51160209230bSgjelinek 		    *up);
51170209230bSgjelinek 	else
51180209230bSgjelinek 		(void) snprintf(buf, bufsize, "%llu%c", num, *up);
51190209230bSgjelinek }
51200209230bSgjelinek 
51210209230bSgjelinek static void
51220209230bSgjelinek output_mcap(FILE *fp, struct zone_mcaptab *mcaptab, int showswap,
51230209230bSgjelinek     uint64_t maxswap, int showlocked, uint64_t maxlocked)
51240209230bSgjelinek {
51250209230bSgjelinek 	char buf[128];
51260209230bSgjelinek 
51270209230bSgjelinek 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_MCAP));
51280209230bSgjelinek 	if (mcaptab->zone_physmem_cap[0] != '\0') {
51290209230bSgjelinek 		bytes_to_units(mcaptab->zone_physmem_cap, buf, sizeof (buf));
51300209230bSgjelinek 		output_prop(fp, PT_PHYSICAL, buf, B_TRUE);
51310209230bSgjelinek 	}
51320209230bSgjelinek 
51330209230bSgjelinek 	if (showswap == Z_OK) {
51340209230bSgjelinek 		(void) snprintf(buf, sizeof (buf), "%llu", maxswap);
51350209230bSgjelinek 		bytes_to_units(buf, buf, sizeof (buf));
51360209230bSgjelinek 		output_prop(fp, PT_SWAP, buf, B_TRUE);
51370209230bSgjelinek 	}
51380209230bSgjelinek 
51390209230bSgjelinek 	if (showlocked == Z_OK) {
51400209230bSgjelinek 		(void) snprintf(buf, sizeof (buf), "%llu", maxlocked);
51410209230bSgjelinek 		bytes_to_units(buf, buf, sizeof (buf));
51420209230bSgjelinek 		output_prop(fp, PT_LOCKED, buf, B_TRUE);
51430209230bSgjelinek 	}
51440209230bSgjelinek }
51450209230bSgjelinek 
51460209230bSgjelinek static void
51470209230bSgjelinek info_mcap(zone_dochandle_t handle, FILE *fp)
51480209230bSgjelinek {
51490209230bSgjelinek 	int res1, res2, res3;
51500209230bSgjelinek 	uint64_t swap_limit;
51510209230bSgjelinek 	uint64_t locked_limit;
51520209230bSgjelinek 	struct zone_mcaptab lookup;
51530209230bSgjelinek 
51540209230bSgjelinek 	bzero(&lookup, sizeof (lookup));
51550209230bSgjelinek 	res1 = zonecfg_getmcapent(handle, &lookup);
51560209230bSgjelinek 	res2 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP, &swap_limit);
51570209230bSgjelinek 	res3 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM,
51580209230bSgjelinek 	    &locked_limit);
51590209230bSgjelinek 
51600209230bSgjelinek 	if (res1 == Z_OK || res2 == Z_OK || res3 == Z_OK)
51610209230bSgjelinek 		output_mcap(fp, &lookup, res2, swap_limit, res3, locked_limit);
51620209230bSgjelinek }
51630209230bSgjelinek 
51647c478bd9Sstevel@tonic-gate void
51657c478bd9Sstevel@tonic-gate info_func(cmd_t *cmd)
51667c478bd9Sstevel@tonic-gate {
51677c478bd9Sstevel@tonic-gate 	FILE *fp = stdout;
5168bbec428eSgjelinek 	boolean_t need_to_close = B_FALSE;
51697c478bd9Sstevel@tonic-gate 	char *pager;
51700209230bSgjelinek 	int type;
51710209230bSgjelinek 	int res1, res2;
51720209230bSgjelinek 	uint64_t swap_limit;
51730209230bSgjelinek 	uint64_t locked_limit;
5174*3042b8b5Sbatschul 	struct stat statbuf;
51757c478bd9Sstevel@tonic-gate 
51767c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
51777c478bd9Sstevel@tonic-gate 
5178bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
51797c478bd9Sstevel@tonic-gate 		return;
51807c478bd9Sstevel@tonic-gate 
51817c478bd9Sstevel@tonic-gate 	/* don't page error output */
51827c478bd9Sstevel@tonic-gate 	if (interactive_mode) {
51837c478bd9Sstevel@tonic-gate 		if ((pager = getenv("PAGER")) == NULL)
51847c478bd9Sstevel@tonic-gate 			pager = PAGER;
5185*3042b8b5Sbatschul 
5186*3042b8b5Sbatschul 		if (stat(pager, &statbuf) == 0) {
5187*3042b8b5Sbatschul 			if ((fp = popen(pager, "w")) != NULL)
5188*3042b8b5Sbatschul 				need_to_close = B_TRUE;
5189*3042b8b5Sbatschul 			else
5190*3042b8b5Sbatschul 				fp = stdout;
5191*3042b8b5Sbatschul 		} else {
5192*3042b8b5Sbatschul 			zerr(gettext("PAGER %s does not exist (%s)."),
5193*3042b8b5Sbatschul 			    pager, strerror(errno));
5194*3042b8b5Sbatschul 		}
5195*3042b8b5Sbatschul 
51967c478bd9Sstevel@tonic-gate 		setbuf(fp, NULL);
51977c478bd9Sstevel@tonic-gate 	}
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate 	if (!global_scope) {
52007c478bd9Sstevel@tonic-gate 		switch (resource_scope) {
52017c478bd9Sstevel@tonic-gate 		case RT_FS:
52027c478bd9Sstevel@tonic-gate 			output_fs(fp, &in_progress_fstab);
52037c478bd9Sstevel@tonic-gate 			break;
52047c478bd9Sstevel@tonic-gate 		case RT_IPD:
52057c478bd9Sstevel@tonic-gate 			output_ipd(fp, &in_progress_ipdtab);
52067c478bd9Sstevel@tonic-gate 			break;
52077c478bd9Sstevel@tonic-gate 		case RT_NET:
52087c478bd9Sstevel@tonic-gate 			output_net(fp, &in_progress_nwiftab);
52097c478bd9Sstevel@tonic-gate 			break;
52107c478bd9Sstevel@tonic-gate 		case RT_DEVICE:
52117c478bd9Sstevel@tonic-gate 			output_dev(fp, &in_progress_devtab);
52127c478bd9Sstevel@tonic-gate 			break;
52137c478bd9Sstevel@tonic-gate 		case RT_RCTL:
52147c478bd9Sstevel@tonic-gate 			output_rctl(fp, &in_progress_rctltab);
52157c478bd9Sstevel@tonic-gate 			break;
52167c478bd9Sstevel@tonic-gate 		case RT_ATTR:
52177c478bd9Sstevel@tonic-gate 			output_attr(fp, &in_progress_attrtab);
52187c478bd9Sstevel@tonic-gate 			break;
5219fa9e4066Sahrens 		case RT_DATASET:
5220fa9e4066Sahrens 			output_ds(fp, &in_progress_dstab);
5221fa9e4066Sahrens 			break;
52220209230bSgjelinek 		case RT_DCPU:
52230209230bSgjelinek 			output_pset(fp, &in_progress_psettab);
52240209230bSgjelinek 			break;
5225c97ad5cdSakolb 		case RT_PCAP:
5226c97ad5cdSakolb 			output_pcap(fp);
5227c97ad5cdSakolb 			break;
52280209230bSgjelinek 		case RT_MCAP:
52290209230bSgjelinek 			res1 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP,
52300209230bSgjelinek 			    &swap_limit);
52310209230bSgjelinek 			res2 = zonecfg_get_aliased_rctl(handle,
52320209230bSgjelinek 			    ALIAS_MAXLOCKEDMEM, &locked_limit);
52330209230bSgjelinek 			output_mcap(fp, &in_progress_mcaptab, res1, swap_limit,
52340209230bSgjelinek 			    res2, locked_limit);
52350209230bSgjelinek 			break;
52367c478bd9Sstevel@tonic-gate 		}
52377c478bd9Sstevel@tonic-gate 		goto cleanup;
52387c478bd9Sstevel@tonic-gate 	}
52397c478bd9Sstevel@tonic-gate 
52400209230bSgjelinek 	type = cmd->cmd_res_type;
52410209230bSgjelinek 
52420209230bSgjelinek 	if (gz_invalid_rt_property(type)) {
52430209230bSgjelinek 		zerr(gettext("%s is not a valid property for the global zone."),
52440209230bSgjelinek 		    rt_to_str(type));
52450209230bSgjelinek 		goto cleanup;
52460209230bSgjelinek 	}
52470209230bSgjelinek 
52480209230bSgjelinek 	if (gz_invalid_resource(type)) {
52490209230bSgjelinek 		zerr(gettext("%s is not a valid resource for the global zone."),
52500209230bSgjelinek 		    rt_to_str(type));
52510209230bSgjelinek 		goto cleanup;
52520209230bSgjelinek 	}
52530209230bSgjelinek 
52547c478bd9Sstevel@tonic-gate 	switch (cmd->cmd_res_type) {
52557c478bd9Sstevel@tonic-gate 	case RT_UNKNOWN:
5256087719fdSdp 		info_zonename(handle, fp);
52570209230bSgjelinek 		if (!global_zone) {
52580209230bSgjelinek 			info_zonepath(handle, fp);
52590209230bSgjelinek 			info_brand(handle, fp);
52600209230bSgjelinek 			info_autoboot(handle, fp);
52610209230bSgjelinek 			info_bootargs(handle, fp);
52620209230bSgjelinek 		}
52637c478bd9Sstevel@tonic-gate 		info_pool(handle, fp);
52640209230bSgjelinek 		if (!global_zone) {
52650209230bSgjelinek 			info_limitpriv(handle, fp);
52660209230bSgjelinek 			info_sched(handle, fp);
5267f4b3ec61Sdh 			info_iptype(handle, fp);
52685679c89fSjv 			info_hostid(handle, fp);
52690209230bSgjelinek 		}
52700209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXLWPS);
52710209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSHMMEM);
52720209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSHMIDS);
52730209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXMSGIDS);
52740209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSEMIDS);
52750209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_SHARES);
52760209230bSgjelinek 		if (!global_zone) {
52770209230bSgjelinek 			info_ipd(handle, fp, cmd);
52780209230bSgjelinek 			info_fs(handle, fp, cmd);
52790209230bSgjelinek 			info_net(handle, fp, cmd);
52800209230bSgjelinek 			info_dev(handle, fp, cmd);
52810209230bSgjelinek 		}
52820209230bSgjelinek 		info_pset(handle, fp);
5283c97ad5cdSakolb 		info_pcap(fp);
52840209230bSgjelinek 		info_mcap(handle, fp);
52850209230bSgjelinek 		if (!global_zone) {
52860209230bSgjelinek 			info_attr(handle, fp, cmd);
52870209230bSgjelinek 			info_ds(handle, fp, cmd);
52880209230bSgjelinek 		}
52897c478bd9Sstevel@tonic-gate 		info_rctl(handle, fp, cmd);
52907c478bd9Sstevel@tonic-gate 		break;
5291087719fdSdp 	case RT_ZONENAME:
5292087719fdSdp 		info_zonename(handle, fp);
5293087719fdSdp 		break;
52947c478bd9Sstevel@tonic-gate 	case RT_ZONEPATH:
52957c478bd9Sstevel@tonic-gate 		info_zonepath(handle, fp);
52967c478bd9Sstevel@tonic-gate 		break;
52979acbbeafSnn 	case RT_BRAND:
52989acbbeafSnn 		info_brand(handle, fp);
52999acbbeafSnn 		break;
53007c478bd9Sstevel@tonic-gate 	case RT_AUTOBOOT:
53017c478bd9Sstevel@tonic-gate 		info_autoboot(handle, fp);
53027c478bd9Sstevel@tonic-gate 		break;
53037c478bd9Sstevel@tonic-gate 	case RT_POOL:
53047c478bd9Sstevel@tonic-gate 		info_pool(handle, fp);
53057c478bd9Sstevel@tonic-gate 		break;
5306ffbafc53Scomay 	case RT_LIMITPRIV:
5307ffbafc53Scomay 		info_limitpriv(handle, fp);
5308ffbafc53Scomay 		break;
53093f2f09c1Sdp 	case RT_BOOTARGS:
53103f2f09c1Sdp 		info_bootargs(handle, fp);
53113f2f09c1Sdp 		break;
53120209230bSgjelinek 	case RT_SCHED:
53130209230bSgjelinek 		info_sched(handle, fp);
53140209230bSgjelinek 		break;
5315f4b3ec61Sdh 	case RT_IPTYPE:
5316f4b3ec61Sdh 		info_iptype(handle, fp);
5317f4b3ec61Sdh 		break;
53180209230bSgjelinek 	case RT_MAXLWPS:
53190209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXLWPS);
53200209230bSgjelinek 		break;
53210209230bSgjelinek 	case RT_MAXSHMMEM:
53220209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSHMMEM);
53230209230bSgjelinek 		break;
53240209230bSgjelinek 	case RT_MAXSHMIDS:
53250209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSHMIDS);
53260209230bSgjelinek 		break;
53270209230bSgjelinek 	case RT_MAXMSGIDS:
53280209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXMSGIDS);
53290209230bSgjelinek 		break;
53300209230bSgjelinek 	case RT_MAXSEMIDS:
53310209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSEMIDS);
53320209230bSgjelinek 		break;
53330209230bSgjelinek 	case RT_SHARES:
53340209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_SHARES);
53350209230bSgjelinek 		break;
53367c478bd9Sstevel@tonic-gate 	case RT_FS:
53377c478bd9Sstevel@tonic-gate 		info_fs(handle, fp, cmd);
53387c478bd9Sstevel@tonic-gate 		break;
53397c478bd9Sstevel@tonic-gate 	case RT_IPD:
53407c478bd9Sstevel@tonic-gate 		info_ipd(handle, fp, cmd);
53417c478bd9Sstevel@tonic-gate 		break;
53427c478bd9Sstevel@tonic-gate 	case RT_NET:
53437c478bd9Sstevel@tonic-gate 		info_net(handle, fp, cmd);
53447c478bd9Sstevel@tonic-gate 		break;
53457c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
53467c478bd9Sstevel@tonic-gate 		info_dev(handle, fp, cmd);
53477c478bd9Sstevel@tonic-gate 		break;
53487c478bd9Sstevel@tonic-gate 	case RT_RCTL:
53497c478bd9Sstevel@tonic-gate 		info_rctl(handle, fp, cmd);
53507c478bd9Sstevel@tonic-gate 		break;
53517c478bd9Sstevel@tonic-gate 	case RT_ATTR:
53527c478bd9Sstevel@tonic-gate 		info_attr(handle, fp, cmd);
53537c478bd9Sstevel@tonic-gate 		break;
5354fa9e4066Sahrens 	case RT_DATASET:
5355fa9e4066Sahrens 		info_ds(handle, fp, cmd);
5356fa9e4066Sahrens 		break;
53570209230bSgjelinek 	case RT_DCPU:
53580209230bSgjelinek 		info_pset(handle, fp);
53590209230bSgjelinek 		break;
5360c97ad5cdSakolb 	case RT_PCAP:
5361c97ad5cdSakolb 		info_pcap(fp);
5362c97ad5cdSakolb 		break;
53630209230bSgjelinek 	case RT_MCAP:
53640209230bSgjelinek 		info_mcap(handle, fp);
53650209230bSgjelinek 		break;
53665679c89fSjv 	case RT_HOSTID:
53675679c89fSjv 		info_hostid(handle, fp);
53685679c89fSjv 		break;
53697c478bd9Sstevel@tonic-gate 	default:
53707c478bd9Sstevel@tonic-gate 		zone_perror(rt_to_str(cmd->cmd_res_type), Z_NO_RESOURCE_TYPE,
5371bbec428eSgjelinek 		    B_TRUE);
53727c478bd9Sstevel@tonic-gate 	}
53737c478bd9Sstevel@tonic-gate 
53747c478bd9Sstevel@tonic-gate cleanup:
53757c478bd9Sstevel@tonic-gate 	if (need_to_close)
53767c478bd9Sstevel@tonic-gate 		(void) pclose(fp);
53777c478bd9Sstevel@tonic-gate }
53787c478bd9Sstevel@tonic-gate 
5379087719fdSdp /*
5380087719fdSdp  * Helper function for verify-- checks that a required string property
5381087719fdSdp  * exists.
5382087719fdSdp  */
5383087719fdSdp static void
5384087719fdSdp check_reqd_prop(char *attr, int rt, int pt, int *ret_val)
53857c478bd9Sstevel@tonic-gate {
5386087719fdSdp 	if (strlen(attr) == 0) {
5387087719fdSdp 		zerr(gettext("%s: %s not specified"), rt_to_str(rt),
5388087719fdSdp 		    pt_to_str(pt));
5389bbec428eSgjelinek 		saw_error = B_TRUE;
5390087719fdSdp 		if (*ret_val == Z_OK)
5391087719fdSdp 			*ret_val = Z_REQD_PROPERTY_MISSING;
53927c478bd9Sstevel@tonic-gate 	}
53937c478bd9Sstevel@tonic-gate }
53947c478bd9Sstevel@tonic-gate 
53959acbbeafSnn static int
53969acbbeafSnn do_subproc(char *cmdbuf)
53979acbbeafSnn {
53989acbbeafSnn 	char inbuf[MAX_CMD_LEN];
53999acbbeafSnn 	FILE *file;
54009acbbeafSnn 	int status;
54019acbbeafSnn 
54029acbbeafSnn 	file = popen(cmdbuf, "r");
54039acbbeafSnn 	if (file == NULL) {
54049acbbeafSnn 		zerr(gettext("Could not launch: %s"), cmdbuf);
54059acbbeafSnn 		return (-1);
54069acbbeafSnn 	}
54079acbbeafSnn 
54089acbbeafSnn 	while (fgets(inbuf, sizeof (inbuf), file) != NULL)
54099acbbeafSnn 		fprintf(stderr, "%s", inbuf);
54109acbbeafSnn 	status = pclose(file);
54119acbbeafSnn 
54129acbbeafSnn 	if (WIFSIGNALED(status)) {
54139acbbeafSnn 		zerr(gettext("%s unexpectedly terminated due to signal %d"),
54149acbbeafSnn 		    cmdbuf, WTERMSIG(status));
54159acbbeafSnn 		return (-1);
54169acbbeafSnn 	}
54179acbbeafSnn 	assert(WIFEXITED(status));
54189acbbeafSnn 	return (WEXITSTATUS(status));
54199acbbeafSnn }
54209acbbeafSnn 
54219acbbeafSnn static int
54229acbbeafSnn brand_verify(zone_dochandle_t handle)
54239acbbeafSnn {
54246e65f9afSnn 	char xml_file[32];
54259acbbeafSnn 	char cmdbuf[MAX_CMD_LEN];
5426123807fbSedp 	brand_handle_t bh;
54279acbbeafSnn 	char brand[MAXNAMELEN];
54289acbbeafSnn 	int err;
54299acbbeafSnn 
54309acbbeafSnn 	if (zonecfg_get_brand(handle, brand, sizeof (brand)) != Z_OK) {
54319acbbeafSnn 		zerr("%s: %s\n", zone, gettext("could not get zone brand"));
54329acbbeafSnn 		return (Z_INVALID_DOCUMENT);
54339acbbeafSnn 	}
5434123807fbSedp 	if ((bh = brand_open(brand)) == NULL) {
54359acbbeafSnn 		zerr("%s: %s\n", zone, gettext("unknown brand."));
54369acbbeafSnn 		return (Z_INVALID_DOCUMENT);
54379acbbeafSnn 	}
54389acbbeafSnn 
54399acbbeafSnn 	/*
54409acbbeafSnn 	 * Fetch the verify command, if any, from the brand configuration
54419acbbeafSnn 	 * and build the command line to execute it.
54429acbbeafSnn 	 */
54439acbbeafSnn 	strcpy(cmdbuf, EXEC_PREFIX);
5444123807fbSedp 	err = brand_get_verify_cfg(bh, cmdbuf + EXEC_LEN,
54459acbbeafSnn 	    sizeof (cmdbuf) - (EXEC_LEN + (strlen(xml_file) + 1)));
5446123807fbSedp 	brand_close(bh);
54479acbbeafSnn 	if (err != Z_OK) {
54489acbbeafSnn 		zerr("%s: %s\n", zone,
54499acbbeafSnn 		    gettext("could not get brand verification command"));
54509acbbeafSnn 		return (Z_INVALID_DOCUMENT);
54519acbbeafSnn 	}
54529acbbeafSnn 
54539acbbeafSnn 	/*
54549acbbeafSnn 	 * If the brand doesn't provide a verification routine, we just
54559acbbeafSnn 	 * return success.
54569acbbeafSnn 	 */
54579acbbeafSnn 	if (strlen(cmdbuf) == EXEC_LEN)
54589acbbeafSnn 		return (Z_OK);
54599acbbeafSnn 
54609acbbeafSnn 	/*
54619acbbeafSnn 	 * Dump the current config information for this zone to a file.
54629acbbeafSnn 	 */
54636e65f9afSnn 	strcpy(xml_file, "/tmp/zonecfg_verify.XXXXXX");
54649acbbeafSnn 	if (mkstemp(xml_file) == NULL)
54659acbbeafSnn 		return (Z_TEMP_FILE);
54669acbbeafSnn 	if ((err = zonecfg_verify_save(handle, xml_file)) != Z_OK) {
54679acbbeafSnn 		(void) unlink(xml_file);
54689acbbeafSnn 		return (err);
54699acbbeafSnn 	}
54709acbbeafSnn 
54719acbbeafSnn 	/*
54729acbbeafSnn 	 * Execute the verification command.
54739acbbeafSnn 	 */
54749acbbeafSnn 	if ((strlcat(cmdbuf, " ", MAX_CMD_LEN) >= MAX_CMD_LEN) ||
54759acbbeafSnn 	    (strlcat(cmdbuf, xml_file, MAX_CMD_LEN) >= MAX_CMD_LEN)) {
54769acbbeafSnn 		err = Z_BRAND_ERROR;
54779acbbeafSnn 	} else {
54789acbbeafSnn 		err = do_subproc(cmdbuf);
54799acbbeafSnn 	}
54809acbbeafSnn 
54819acbbeafSnn 	(void) unlink(xml_file);
54829acbbeafSnn 	return ((err == Z_OK) ? Z_OK : Z_BRAND_ERROR);
54839acbbeafSnn }
54849acbbeafSnn 
54857c478bd9Sstevel@tonic-gate /*
54867c478bd9Sstevel@tonic-gate  * See the DTD for which attributes are required for which resources.
54877c478bd9Sstevel@tonic-gate  *
54887c478bd9Sstevel@tonic-gate  * This function can be called by commit_func(), which needs to save things,
54897c478bd9Sstevel@tonic-gate  * in addition to the general call from parse_and_run(), which doesn't need
54907c478bd9Sstevel@tonic-gate  * things saved.  Since the parameters are standardized, we distinguish by
54917c478bd9Sstevel@tonic-gate  * having commit_func() call here with cmd->cmd_arg set to "save" to indicate
54927c478bd9Sstevel@tonic-gate  * that a save is needed.
54937c478bd9Sstevel@tonic-gate  */
54947c478bd9Sstevel@tonic-gate void
54957c478bd9Sstevel@tonic-gate verify_func(cmd_t *cmd)
54967c478bd9Sstevel@tonic-gate {
54977c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
54987c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
54997c478bd9Sstevel@tonic-gate 	struct zone_attrtab attrtab;
55007c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
5501fa9e4066Sahrens 	struct zone_dstab dstab;
55020209230bSgjelinek 	struct zone_psettab psettab;
55037c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
55040209230bSgjelinek 	char sched[MAXNAMELEN];
55059acbbeafSnn 	char brand[MAXNAMELEN];
55065679c89fSjv 	char hostidp[HW_HOSTID_LEN];
55077c478bd9Sstevel@tonic-gate 	int err, ret_val = Z_OK, arg;
5508c97ad5cdSakolb 	int pset_res;
5509bbec428eSgjelinek 	boolean_t save = B_FALSE;
5510bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
5511f4b3ec61Sdh 	zone_iptype_t iptype;
55120209230bSgjelinek 	boolean_t has_cpu_shares = B_FALSE;
5513c97ad5cdSakolb 	boolean_t has_cpu_cap = B_FALSE;
55147c478bd9Sstevel@tonic-gate 
55157c478bd9Sstevel@tonic-gate 	optind = 0;
55167ec75eb8Sgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?")) != EOF) {
55177c478bd9Sstevel@tonic-gate 		switch (arg) {
55187c478bd9Sstevel@tonic-gate 		case '?':
55197c478bd9Sstevel@tonic-gate 			longer_usage(CMD_VERIFY);
5520bbec428eSgjelinek 			arg_err = B_TRUE;
55217ec75eb8Sgjelinek 			break;
55227c478bd9Sstevel@tonic-gate 		default:
55237c478bd9Sstevel@tonic-gate 			short_usage(CMD_VERIFY);
5524bbec428eSgjelinek 			arg_err = B_TRUE;
55257ec75eb8Sgjelinek 			break;
55267c478bd9Sstevel@tonic-gate 		}
55277c478bd9Sstevel@tonic-gate 	}
55287ec75eb8Sgjelinek 	if (arg_err)
55297ec75eb8Sgjelinek 		return;
55307ec75eb8Sgjelinek 
55317c478bd9Sstevel@tonic-gate 	if (optind > cmd->cmd_argc) {
55327c478bd9Sstevel@tonic-gate 		short_usage(CMD_VERIFY);
55337c478bd9Sstevel@tonic-gate 		return;
55347c478bd9Sstevel@tonic-gate 	}
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_VERIFY))
55377c478bd9Sstevel@tonic-gate 		return;
55387c478bd9Sstevel@tonic-gate 
55397c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
55407c478bd9Sstevel@tonic-gate 
55417c478bd9Sstevel@tonic-gate 	if (cmd->cmd_argc > 0 && (strcmp(cmd->cmd_argv[0], "save") == 0))
5542bbec428eSgjelinek 		save = B_TRUE;
5543bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
55447c478bd9Sstevel@tonic-gate 		return;
55457c478bd9Sstevel@tonic-gate 
55460209230bSgjelinek 	if (zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath)) != Z_OK &&
55470209230bSgjelinek 	    !global_zone) {
5548087719fdSdp 		zerr(gettext("%s not specified"), pt_to_str(PT_ZONEPATH));
55497c478bd9Sstevel@tonic-gate 		ret_val = Z_REQD_RESOURCE_MISSING;
5550bbec428eSgjelinek 		saw_error = B_TRUE;
55517c478bd9Sstevel@tonic-gate 	}
55520209230bSgjelinek 	if (strlen(zonepath) == 0 && !global_zone) {
5553087719fdSdp 		zerr(gettext("%s cannot be empty."), pt_to_str(PT_ZONEPATH));
55547c478bd9Sstevel@tonic-gate 		ret_val = Z_REQD_RESOURCE_MISSING;
5555bbec428eSgjelinek 		saw_error = B_TRUE;
55567c478bd9Sstevel@tonic-gate 	}
55577c478bd9Sstevel@tonic-gate 
55589acbbeafSnn 	if ((err = zonecfg_get_brand(handle, brand, sizeof (brand))) != Z_OK) {
5559bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
55609acbbeafSnn 		return;
55619acbbeafSnn 	}
5562a46da47fSEdward Pilatowicz 	if ((err = brand_verify(handle)) != Z_OK) {
5563a46da47fSEdward Pilatowicz 		zone_perror(zone, err, B_TRUE);
5564a46da47fSEdward Pilatowicz 		return;
55659acbbeafSnn 	}
55669acbbeafSnn 
5567f4b3ec61Sdh 	if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
5568f4b3ec61Sdh 		zerr("%s %s", gettext("cannot get"), pt_to_str(PT_IPTYPE));
5569f4b3ec61Sdh 		ret_val = Z_REQD_RESOURCE_MISSING;
5570bbec428eSgjelinek 		saw_error = B_TRUE;
5571f4b3ec61Sdh 	}
55727c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setipdent(handle)) != Z_OK) {
5573bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
55747c478bd9Sstevel@tonic-gate 		return;
55757c478bd9Sstevel@tonic-gate 	}
55767c478bd9Sstevel@tonic-gate 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
5577087719fdSdp 		check_reqd_prop(fstab.zone_fs_dir, RT_IPD, PT_DIR, &ret_val);
55787c478bd9Sstevel@tonic-gate 	}
55797c478bd9Sstevel@tonic-gate 	(void) zonecfg_endipdent(handle);
55807c478bd9Sstevel@tonic-gate 
55815679c89fSjv 	if (zonecfg_get_hostid(handle, hostidp, sizeof (hostidp)) == Z_OK &&
55825679c89fSjv 	    (err = zonecfg_valid_hostid(hostidp)) != Z_OK) {
55835679c89fSjv 		zone_perror(zone, err, B_TRUE);
55845679c89fSjv 		return;
55855679c89fSjv 	}
55865679c89fSjv 
55877c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setfsent(handle)) != Z_OK) {
5588bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
55897c478bd9Sstevel@tonic-gate 		return;
55907c478bd9Sstevel@tonic-gate 	}
55917c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
5592087719fdSdp 		check_reqd_prop(fstab.zone_fs_dir, RT_FS, PT_DIR, &ret_val);
5593087719fdSdp 		check_reqd_prop(fstab.zone_fs_special, RT_FS, PT_SPECIAL,
5594087719fdSdp 		    &ret_val);
5595087719fdSdp 		check_reqd_prop(fstab.zone_fs_type, RT_FS, PT_TYPE, &ret_val);
5596087719fdSdp 
55977c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
55987c478bd9Sstevel@tonic-gate 	}
55997c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
56007c478bd9Sstevel@tonic-gate 
56017c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
5602bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
56037c478bd9Sstevel@tonic-gate 		return;
56047c478bd9Sstevel@tonic-gate 	}
56057c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
5606f4b3ec61Sdh 		/*
5607f4b3ec61Sdh 		 * physical is required in all cases.
5608de860bd9Sgfaden 		 * A shared IP requires an address,
5609de860bd9Sgfaden 		 * and may include a default router, while
5610de860bd9Sgfaden 		 * an exclusive IP must have neither an address
5611de860bd9Sgfaden 		 * nor a default router.
561201b4bc23Sjv 		 * The physical interface name must be valid in all cases.
5613f4b3ec61Sdh 		 */
5614087719fdSdp 		check_reqd_prop(nwiftab.zone_nwif_physical, RT_NET,
5615087719fdSdp 		    PT_PHYSICAL, &ret_val);
561601b4bc23Sjv 		if (validate_net_physical_syntax(nwiftab.zone_nwif_physical) !=
561701b4bc23Sjv 		    Z_OK) {
561801b4bc23Sjv 			saw_error = B_TRUE;
561901b4bc23Sjv 			if (ret_val == Z_OK)
562001b4bc23Sjv 				ret_val = Z_INVAL;
562101b4bc23Sjv 		}
5622f4b3ec61Sdh 
5623f4b3ec61Sdh 		switch (iptype) {
5624f4b3ec61Sdh 		case ZS_SHARED:
5625f4b3ec61Sdh 			check_reqd_prop(nwiftab.zone_nwif_address, RT_NET,
5626f4b3ec61Sdh 			    PT_ADDRESS, &ret_val);
5627f4b3ec61Sdh 			break;
5628f4b3ec61Sdh 		case ZS_EXCLUSIVE:
5629f4b3ec61Sdh 			if (strlen(nwiftab.zone_nwif_address) > 0) {
5630f4b3ec61Sdh 				zerr(gettext("%s: %s cannot be specified "
5631f4b3ec61Sdh 				    "for an exclusive IP type"),
5632f4b3ec61Sdh 				    rt_to_str(RT_NET), pt_to_str(PT_ADDRESS));
5633bbec428eSgjelinek 				saw_error = B_TRUE;
5634f4b3ec61Sdh 				if (ret_val == Z_OK)
5635f4b3ec61Sdh 					ret_val = Z_INVAL;
5636f4b3ec61Sdh 			}
5637de860bd9Sgfaden 			if (strlen(nwiftab.zone_nwif_defrouter) > 0) {
5638de860bd9Sgfaden 				zerr(gettext("%s: %s cannot be specified "
5639de860bd9Sgfaden 				    "for an exclusive IP type"),
5640de860bd9Sgfaden 				    rt_to_str(RT_NET), pt_to_str(PT_DEFROUTER));
5641bbec428eSgjelinek 				saw_error = B_TRUE;
5642de860bd9Sgfaden 				if (ret_val == Z_OK)
5643de860bd9Sgfaden 					ret_val = Z_INVAL;
5644de860bd9Sgfaden 			}
5645f4b3ec61Sdh 			break;
5646f4b3ec61Sdh 		}
56477c478bd9Sstevel@tonic-gate 	}
56487c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
56497c478bd9Sstevel@tonic-gate 
56507c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
5651bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
56527c478bd9Sstevel@tonic-gate 		return;
56537c478bd9Sstevel@tonic-gate 	}
56547c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
5655087719fdSdp 		check_reqd_prop(rctltab.zone_rctl_name, RT_RCTL, PT_NAME,
5656087719fdSdp 		    &ret_val);
5657087719fdSdp 
56580209230bSgjelinek 		if (strcmp(rctltab.zone_rctl_name, "zone.cpu-shares") == 0)
56590209230bSgjelinek 			has_cpu_shares = B_TRUE;
56600209230bSgjelinek 
5661c97ad5cdSakolb 		if (strcmp(rctltab.zone_rctl_name, "zone.cpu-cap") == 0)
5662c97ad5cdSakolb 			has_cpu_cap = B_TRUE;
5663c97ad5cdSakolb 
56647c478bd9Sstevel@tonic-gate 		if (rctltab.zone_rctl_valptr == NULL) {
56657c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: no %s specified"),
56667c478bd9Sstevel@tonic-gate 			    rt_to_str(RT_RCTL), pt_to_str(PT_VALUE));
5667bbec428eSgjelinek 			saw_error = B_TRUE;
56687c478bd9Sstevel@tonic-gate 			if (ret_val == Z_OK)
56697c478bd9Sstevel@tonic-gate 				ret_val = Z_REQD_PROPERTY_MISSING;
56707c478bd9Sstevel@tonic-gate 		} else {
56717c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
56727c478bd9Sstevel@tonic-gate 		}
56737c478bd9Sstevel@tonic-gate 	}
56747c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
56757c478bd9Sstevel@tonic-gate 
5676c97ad5cdSakolb 	if ((pset_res = zonecfg_lookup_pset(handle, &psettab)) == Z_OK &&
5677c97ad5cdSakolb 	    has_cpu_shares) {
56780209230bSgjelinek 		zerr(gettext("%s zone.cpu-shares and %s are incompatible."),
56790209230bSgjelinek 		    rt_to_str(RT_RCTL), rt_to_str(RT_DCPU));
5680bbec428eSgjelinek 		saw_error = B_TRUE;
56810209230bSgjelinek 		if (ret_val == Z_OK)
56820209230bSgjelinek 			ret_val = Z_INCOMPATIBLE;
56830209230bSgjelinek 	}
56840209230bSgjelinek 
56850209230bSgjelinek 	if (has_cpu_shares && zonecfg_get_sched_class(handle, sched,
56860209230bSgjelinek 	    sizeof (sched)) == Z_OK && strlen(sched) > 0 &&
56870209230bSgjelinek 	    strcmp(sched, "FSS") != 0) {
56880209230bSgjelinek 		zerr(gettext("WARNING: %s zone.cpu-shares and %s=%s are "
56890209230bSgjelinek 		    "incompatible"),
56900209230bSgjelinek 		    rt_to_str(RT_RCTL), rt_to_str(RT_SCHED), sched);
5691bbec428eSgjelinek 		saw_error = B_TRUE;
56920209230bSgjelinek 		if (ret_val == Z_OK)
56930209230bSgjelinek 			ret_val = Z_INCOMPATIBLE;
56940209230bSgjelinek 	}
56950209230bSgjelinek 
5696c97ad5cdSakolb 	if (pset_res == Z_OK && has_cpu_cap) {
5697c97ad5cdSakolb 		zerr(gettext("%s zone.cpu-cap and the %s are incompatible."),
5698c97ad5cdSakolb 		    rt_to_str(RT_RCTL), rt_to_str(RT_DCPU));
5699bbec428eSgjelinek 		saw_error = B_TRUE;
5700c97ad5cdSakolb 		if (ret_val == Z_OK)
5701c97ad5cdSakolb 			ret_val = Z_INCOMPATIBLE;
5702c97ad5cdSakolb 	}
5703c97ad5cdSakolb 
57047c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setattrent(handle)) != Z_OK) {
5705bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
57067c478bd9Sstevel@tonic-gate 		return;
57077c478bd9Sstevel@tonic-gate 	}
57087c478bd9Sstevel@tonic-gate 	while (zonecfg_getattrent(handle, &attrtab) == Z_OK) {
5709087719fdSdp 		check_reqd_prop(attrtab.zone_attr_name, RT_ATTR, PT_NAME,
5710087719fdSdp 		    &ret_val);
5711087719fdSdp 		check_reqd_prop(attrtab.zone_attr_type, RT_ATTR, PT_TYPE,
5712087719fdSdp 		    &ret_val);
5713087719fdSdp 		check_reqd_prop(attrtab.zone_attr_value, RT_ATTR, PT_VALUE,
5714087719fdSdp 		    &ret_val);
57157c478bd9Sstevel@tonic-gate 	}
57167c478bd9Sstevel@tonic-gate 	(void) zonecfg_endattrent(handle);
57177c478bd9Sstevel@tonic-gate 
5718fa9e4066Sahrens 	if ((err = zonecfg_setdsent(handle)) != Z_OK) {
5719bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
5720fa9e4066Sahrens 		return;
5721fa9e4066Sahrens 	}
5722fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
5723fa9e4066Sahrens 		if (strlen(dstab.zone_dataset_name) == 0) {
5724fa9e4066Sahrens 			zerr("%s: %s %s", rt_to_str(RT_DATASET),
5725fa9e4066Sahrens 			    pt_to_str(PT_NAME), gettext("not specified"));
5726bbec428eSgjelinek 			saw_error = B_TRUE;
5727fa9e4066Sahrens 			if (ret_val == Z_OK)
5728fa9e4066Sahrens 				ret_val = Z_REQD_PROPERTY_MISSING;
5729fa9e4066Sahrens 		} else if (!zfs_name_valid(dstab.zone_dataset_name,
5730fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) {
5731fa9e4066Sahrens 			zerr("%s: %s %s", rt_to_str(RT_DATASET),
5732fa9e4066Sahrens 			    pt_to_str(PT_NAME), gettext("invalid"));
5733bbec428eSgjelinek 			saw_error = B_TRUE;
5734fa9e4066Sahrens 			if (ret_val == Z_OK)
5735fa9e4066Sahrens 				ret_val = Z_BAD_PROPERTY;
5736fa9e4066Sahrens 		}
5737fa9e4066Sahrens 
5738fa9e4066Sahrens 	}
5739fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
5740fa9e4066Sahrens 
57417c478bd9Sstevel@tonic-gate 	if (!global_scope) {
57427c478bd9Sstevel@tonic-gate 		zerr(gettext("resource specification incomplete"));
5743bbec428eSgjelinek 		saw_error = B_TRUE;
57447c478bd9Sstevel@tonic-gate 		if (ret_val == Z_OK)
57457c478bd9Sstevel@tonic-gate 			ret_val = Z_INSUFFICIENT_SPEC;
57467c478bd9Sstevel@tonic-gate 	}
57477c478bd9Sstevel@tonic-gate 
57487c478bd9Sstevel@tonic-gate 	if (save) {
5749087719fdSdp 		if (ret_val == Z_OK) {
5750087719fdSdp 			if ((ret_val = zonecfg_save(handle)) == Z_OK) {
5751bbec428eSgjelinek 				need_to_commit = B_FALSE;
5752087719fdSdp 				(void) strlcpy(revert_zone, zone,
5753087719fdSdp 				    sizeof (revert_zone));
5754087719fdSdp 			}
5755087719fdSdp 		} else {
5756087719fdSdp 			zerr(gettext("Zone %s failed to verify"), zone);
5757087719fdSdp 		}
57587c478bd9Sstevel@tonic-gate 	}
57597c478bd9Sstevel@tonic-gate 	if (ret_val != Z_OK)
5760bbec428eSgjelinek 		zone_perror(zone, ret_val, B_TRUE);
57617c478bd9Sstevel@tonic-gate }
57627c478bd9Sstevel@tonic-gate 
57637c478bd9Sstevel@tonic-gate void
57647c478bd9Sstevel@tonic-gate cancel_func(cmd_t *cmd)
57657c478bd9Sstevel@tonic-gate {
57667c478bd9Sstevel@tonic-gate 	int arg;
5767bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
57687c478bd9Sstevel@tonic-gate 
57697c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
57707c478bd9Sstevel@tonic-gate 
57717c478bd9Sstevel@tonic-gate 	optind = 0;
57727ec75eb8Sgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?")) != EOF) {
57737c478bd9Sstevel@tonic-gate 		switch (arg) {
57747c478bd9Sstevel@tonic-gate 		case '?':
57757c478bd9Sstevel@tonic-gate 			longer_usage(CMD_CANCEL);
5776bbec428eSgjelinek 			arg_err = B_TRUE;
57777ec75eb8Sgjelinek 			break;
57787c478bd9Sstevel@tonic-gate 		default:
57797c478bd9Sstevel@tonic-gate 			short_usage(CMD_CANCEL);
5780bbec428eSgjelinek 			arg_err = B_TRUE;
57817ec75eb8Sgjelinek 			break;
57827c478bd9Sstevel@tonic-gate 		}
57837c478bd9Sstevel@tonic-gate 	}
57847ec75eb8Sgjelinek 	if (arg_err)
57857ec75eb8Sgjelinek 		return;
57867ec75eb8Sgjelinek 
57877c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
57887c478bd9Sstevel@tonic-gate 		short_usage(CMD_CANCEL);
57897c478bd9Sstevel@tonic-gate 		return;
57907c478bd9Sstevel@tonic-gate 	}
57917c478bd9Sstevel@tonic-gate 
57927c478bd9Sstevel@tonic-gate 	if (global_scope)
57937c478bd9Sstevel@tonic-gate 		scope_usage(CMD_CANCEL);
5794bbec428eSgjelinek 	global_scope = B_TRUE;
57957c478bd9Sstevel@tonic-gate 	zonecfg_free_fs_option_list(in_progress_fstab.zone_fs_options);
57967c478bd9Sstevel@tonic-gate 	bzero(&in_progress_fstab, sizeof (in_progress_fstab));
57977c478bd9Sstevel@tonic-gate 	bzero(&in_progress_nwiftab, sizeof (in_progress_nwiftab));
5798fa9e4066Sahrens 	bzero(&in_progress_ipdtab, sizeof (in_progress_ipdtab));
57997c478bd9Sstevel@tonic-gate 	bzero(&in_progress_devtab, sizeof (in_progress_devtab));
58007c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(in_progress_rctltab.zone_rctl_valptr);
58017c478bd9Sstevel@tonic-gate 	bzero(&in_progress_rctltab, sizeof (in_progress_rctltab));
58027c478bd9Sstevel@tonic-gate 	bzero(&in_progress_attrtab, sizeof (in_progress_attrtab));
5803fa9e4066Sahrens 	bzero(&in_progress_dstab, sizeof (in_progress_dstab));
58047c478bd9Sstevel@tonic-gate }
58057c478bd9Sstevel@tonic-gate 
58067c478bd9Sstevel@tonic-gate static int
58077c478bd9Sstevel@tonic-gate validate_attr_name(char *name)
58087c478bd9Sstevel@tonic-gate {
58097c478bd9Sstevel@tonic-gate 	int i;
58107c478bd9Sstevel@tonic-gate 
58117c478bd9Sstevel@tonic-gate 	if (!isalnum(name[0])) {
58127c478bd9Sstevel@tonic-gate 		zerr(gettext("Invalid %s %s %s: must start with an alpha-"
58137c478bd9Sstevel@tonic-gate 		    "numeric character."), rt_to_str(RT_ATTR),
58147c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_NAME), name);
58157c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
58167c478bd9Sstevel@tonic-gate 	}
58177c478bd9Sstevel@tonic-gate 	for (i = 1; name[i]; i++)
58187c478bd9Sstevel@tonic-gate 		if (!isalnum(name[i]) && name[i] != '-' && name[i] != '.') {
58197c478bd9Sstevel@tonic-gate 			zerr(gettext("Invalid %s %s %s: can only contain "
58207c478bd9Sstevel@tonic-gate 			    "alpha-numeric characters, plus '-' and '.'."),
58217c478bd9Sstevel@tonic-gate 			    rt_to_str(RT_ATTR), pt_to_str(PT_NAME), name);
58227c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
58237c478bd9Sstevel@tonic-gate 		}
58247c478bd9Sstevel@tonic-gate 	return (Z_OK);
58257c478bd9Sstevel@tonic-gate }
58267c478bd9Sstevel@tonic-gate 
58277c478bd9Sstevel@tonic-gate static int
58287c478bd9Sstevel@tonic-gate validate_attr_type_val(struct zone_attrtab *attrtab)
58297c478bd9Sstevel@tonic-gate {
58307c478bd9Sstevel@tonic-gate 	boolean_t boolval;
58317c478bd9Sstevel@tonic-gate 	int64_t intval;
58327c478bd9Sstevel@tonic-gate 	char strval[MAXNAMELEN];
58337c478bd9Sstevel@tonic-gate 	uint64_t uintval;
58347c478bd9Sstevel@tonic-gate 
58357c478bd9Sstevel@tonic-gate 	if (strcmp(attrtab->zone_attr_type, "boolean") == 0) {
58367c478bd9Sstevel@tonic-gate 		if (zonecfg_get_attr_boolean(attrtab, &boolval) == Z_OK)
58377c478bd9Sstevel@tonic-gate 			return (Z_OK);
58387c478bd9Sstevel@tonic-gate 		zerr(gettext("invalid %s value for %s=%s"),
58397c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR), pt_to_str(PT_TYPE), "boolean");
58407c478bd9Sstevel@tonic-gate 		return (Z_ERR);
58417c478bd9Sstevel@tonic-gate 	}
58427c478bd9Sstevel@tonic-gate 
58437c478bd9Sstevel@tonic-gate 	if (strcmp(attrtab->zone_attr_type, "int") == 0) {
58447c478bd9Sstevel@tonic-gate 		if (zonecfg_get_attr_int(attrtab, &intval) == Z_OK)
58457c478bd9Sstevel@tonic-gate 			return (Z_OK);
58467c478bd9Sstevel@tonic-gate 		zerr(gettext("invalid %s value for %s=%s"),
58477c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR), pt_to_str(PT_TYPE), "int");
58487c478bd9Sstevel@tonic-gate 		return (Z_ERR);
58497c478bd9Sstevel@tonic-gate 	}
58507c478bd9Sstevel@tonic-gate 
58517c478bd9Sstevel@tonic-gate 	if (strcmp(attrtab->zone_attr_type, "string") == 0) {
58527c478bd9Sstevel@tonic-gate 		if (zonecfg_get_attr_string(attrtab, strval,
58537c478bd9Sstevel@tonic-gate 		    sizeof (strval)) == Z_OK)
58547c478bd9Sstevel@tonic-gate 			return (Z_OK);
58557c478bd9Sstevel@tonic-gate 		zerr(gettext("invalid %s value for %s=%s"),
58567c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR), pt_to_str(PT_TYPE), "string");
58577c478bd9Sstevel@tonic-gate 		return (Z_ERR);
58587c478bd9Sstevel@tonic-gate 	}
58597c478bd9Sstevel@tonic-gate 
58607c478bd9Sstevel@tonic-gate 	if (strcmp(attrtab->zone_attr_type, "uint") == 0) {
58617c478bd9Sstevel@tonic-gate 		if (zonecfg_get_attr_uint(attrtab, &uintval) == Z_OK)
58627c478bd9Sstevel@tonic-gate 			return (Z_OK);
58637c478bd9Sstevel@tonic-gate 		zerr(gettext("invalid %s value for %s=%s"),
58647c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR), pt_to_str(PT_TYPE), "uint");
58657c478bd9Sstevel@tonic-gate 		return (Z_ERR);
58667c478bd9Sstevel@tonic-gate 	}
58677c478bd9Sstevel@tonic-gate 
58687c478bd9Sstevel@tonic-gate 	zerr(gettext("invalid %s %s '%s'"), rt_to_str(RT_ATTR),
58697c478bd9Sstevel@tonic-gate 	    pt_to_str(PT_TYPE), attrtab->zone_attr_type);
58707c478bd9Sstevel@tonic-gate 	return (Z_ERR);
58717c478bd9Sstevel@tonic-gate }
58727c478bd9Sstevel@tonic-gate 
5873087719fdSdp /*
5874087719fdSdp  * Helper function for end_func-- checks the existence of a given property
5875087719fdSdp  * and emits a message if not specified.
5876087719fdSdp  */
5877087719fdSdp static int
5878bbec428eSgjelinek end_check_reqd(char *attr, int pt, boolean_t *validation_failed)
5879087719fdSdp {
5880087719fdSdp 	if (strlen(attr) == 0) {
5881bbec428eSgjelinek 		*validation_failed = B_TRUE;
5882087719fdSdp 		zerr(gettext("%s not specified"), pt_to_str(pt));
5883087719fdSdp 		return (Z_ERR);
5884087719fdSdp 	}
5885087719fdSdp 	return (Z_OK);
5886087719fdSdp }
5887087719fdSdp 
58887c478bd9Sstevel@tonic-gate void
58897c478bd9Sstevel@tonic-gate end_func(cmd_t *cmd)
58907c478bd9Sstevel@tonic-gate {
5891bbec428eSgjelinek 	boolean_t validation_failed = B_FALSE;
5892bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
58937c478bd9Sstevel@tonic-gate 	struct zone_fstab tmp_fstab;
58947c478bd9Sstevel@tonic-gate 	struct zone_nwiftab tmp_nwiftab;
58957c478bd9Sstevel@tonic-gate 	struct zone_devtab tmp_devtab;
58967c478bd9Sstevel@tonic-gate 	struct zone_rctltab tmp_rctltab;
58977c478bd9Sstevel@tonic-gate 	struct zone_attrtab tmp_attrtab;
5898fa9e4066Sahrens 	struct zone_dstab tmp_dstab;
58990209230bSgjelinek 	int err, arg, res1, res2, res3;
59000209230bSgjelinek 	uint64_t swap_limit;
59010209230bSgjelinek 	uint64_t locked_limit;
5902c97ad5cdSakolb 	uint64_t proc_cap;
59037c478bd9Sstevel@tonic-gate 
59047c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
59057c478bd9Sstevel@tonic-gate 
59067c478bd9Sstevel@tonic-gate 	optind = 0;
59077ec75eb8Sgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?")) != EOF) {
59087c478bd9Sstevel@tonic-gate 		switch (arg) {
59097c478bd9Sstevel@tonic-gate 		case '?':
59107c478bd9Sstevel@tonic-gate 			longer_usage(CMD_END);
5911bbec428eSgjelinek 			arg_err = B_TRUE;
59127ec75eb8Sgjelinek 			break;
59137c478bd9Sstevel@tonic-gate 		default:
59147c478bd9Sstevel@tonic-gate 			short_usage(CMD_END);
5915bbec428eSgjelinek 			arg_err = B_TRUE;
59167ec75eb8Sgjelinek 			break;
59177c478bd9Sstevel@tonic-gate 		}
59187c478bd9Sstevel@tonic-gate 	}
59197ec75eb8Sgjelinek 	if (arg_err)
59207ec75eb8Sgjelinek 		return;
59217ec75eb8Sgjelinek 
59227c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
59237c478bd9Sstevel@tonic-gate 		short_usage(CMD_END);
59247c478bd9Sstevel@tonic-gate 		return;
59257c478bd9Sstevel@tonic-gate 	}
59267c478bd9Sstevel@tonic-gate 
59277c478bd9Sstevel@tonic-gate 	if (global_scope) {
59287c478bd9Sstevel@tonic-gate 		scope_usage(CMD_END);
59297c478bd9Sstevel@tonic-gate 		return;
59307c478bd9Sstevel@tonic-gate 	}
59317c478bd9Sstevel@tonic-gate 
59327c478bd9Sstevel@tonic-gate 	assert(end_op == CMD_ADD || end_op == CMD_SELECT);
59337c478bd9Sstevel@tonic-gate 
59347c478bd9Sstevel@tonic-gate 	switch (resource_scope) {
59357c478bd9Sstevel@tonic-gate 	case RT_FS:
59367c478bd9Sstevel@tonic-gate 		/* First make sure everything was filled in. */
5937087719fdSdp 		if (end_check_reqd(in_progress_fstab.zone_fs_dir,
5938087719fdSdp 		    PT_DIR, &validation_failed) == Z_OK) {
5939087719fdSdp 			if (in_progress_fstab.zone_fs_dir[0] != '/') {
5940087719fdSdp 				zerr(gettext("%s %s is not an absolute path."),
5941087719fdSdp 				    pt_to_str(PT_DIR),
5942087719fdSdp 				    in_progress_fstab.zone_fs_dir);
5943bbec428eSgjelinek 				validation_failed = B_TRUE;
5944087719fdSdp 			}
59457c478bd9Sstevel@tonic-gate 		}
5946087719fdSdp 
5947087719fdSdp 		(void) end_check_reqd(in_progress_fstab.zone_fs_special,
5948087719fdSdp 		    PT_SPECIAL, &validation_failed);
5949087719fdSdp 
59507c478bd9Sstevel@tonic-gate 		if (in_progress_fstab.zone_fs_raw[0] != '\0' &&
59517c478bd9Sstevel@tonic-gate 		    in_progress_fstab.zone_fs_raw[0] != '/') {
5952087719fdSdp 			zerr(gettext("%s %s is not an absolute path."),
5953087719fdSdp 			    pt_to_str(PT_RAW),
5954087719fdSdp 			    in_progress_fstab.zone_fs_raw);
5955bbec428eSgjelinek 			validation_failed = B_TRUE;
59567c478bd9Sstevel@tonic-gate 		}
5957087719fdSdp 
5958087719fdSdp 		(void) end_check_reqd(in_progress_fstab.zone_fs_type, PT_TYPE,
5959087719fdSdp 		    &validation_failed);
5960087719fdSdp 
5961087719fdSdp 		if (validation_failed) {
5962bbec428eSgjelinek 			saw_error = B_TRUE;
59637c478bd9Sstevel@tonic-gate 			return;
5964087719fdSdp 		}
5965087719fdSdp 
59667c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
59677c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
59687c478bd9Sstevel@tonic-gate 			bzero(&tmp_fstab, sizeof (tmp_fstab));
59697c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_fstab.zone_fs_dir,
59707c478bd9Sstevel@tonic-gate 			    in_progress_fstab.zone_fs_dir,
59717c478bd9Sstevel@tonic-gate 			    sizeof (tmp_fstab.zone_fs_dir));
59727c478bd9Sstevel@tonic-gate 			err = zonecfg_lookup_filesystem(handle, &tmp_fstab);
59737c478bd9Sstevel@tonic-gate 			zonecfg_free_fs_option_list(tmp_fstab.zone_fs_options);
59747c478bd9Sstevel@tonic-gate 			if (err == Z_OK) {
59757c478bd9Sstevel@tonic-gate 				zerr(gettext("A %s resource "
59767c478bd9Sstevel@tonic-gate 				    "with the %s '%s' already exists."),
59777c478bd9Sstevel@tonic-gate 				    rt_to_str(RT_FS), pt_to_str(PT_DIR),
59787c478bd9Sstevel@tonic-gate 				    in_progress_fstab.zone_fs_dir);
5979bbec428eSgjelinek 				saw_error = B_TRUE;
59807c478bd9Sstevel@tonic-gate 				return;
59817c478bd9Sstevel@tonic-gate 			}
59827c478bd9Sstevel@tonic-gate 			err = zonecfg_add_filesystem(handle,
59837c478bd9Sstevel@tonic-gate 			    &in_progress_fstab);
59847c478bd9Sstevel@tonic-gate 		} else {
59857c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_filesystem(handle, &old_fstab,
59867c478bd9Sstevel@tonic-gate 			    &in_progress_fstab);
59877c478bd9Sstevel@tonic-gate 		}
59887c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(in_progress_fstab.zone_fs_options);
59897c478bd9Sstevel@tonic-gate 		in_progress_fstab.zone_fs_options = NULL;
59907c478bd9Sstevel@tonic-gate 		break;
5991087719fdSdp 
59927c478bd9Sstevel@tonic-gate 	case RT_IPD:
59937c478bd9Sstevel@tonic-gate 		/* First make sure everything was filled in. */
5994087719fdSdp 		if (end_check_reqd(in_progress_ipdtab.zone_fs_dir, PT_DIR,
5995087719fdSdp 		    &validation_failed) == Z_OK) {
5996087719fdSdp 			if (in_progress_ipdtab.zone_fs_dir[0] != '/') {
5997087719fdSdp 				zerr(gettext("%s %s is not an absolute path."),
5998087719fdSdp 				    pt_to_str(PT_DIR),
5999087719fdSdp 				    in_progress_ipdtab.zone_fs_dir);
6000bbec428eSgjelinek 				validation_failed = B_TRUE;
6001087719fdSdp 			}
60027c478bd9Sstevel@tonic-gate 		}
6003087719fdSdp 		if (validation_failed) {
6004bbec428eSgjelinek 			saw_error = B_TRUE;
60057c478bd9Sstevel@tonic-gate 			return;
6006087719fdSdp 		}
6007087719fdSdp 
60087c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
60097c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
60107c478bd9Sstevel@tonic-gate 			bzero(&tmp_fstab, sizeof (tmp_fstab));
60117c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_fstab.zone_fs_dir,
60127c478bd9Sstevel@tonic-gate 			    in_progress_ipdtab.zone_fs_dir,
60137c478bd9Sstevel@tonic-gate 			    sizeof (tmp_fstab.zone_fs_dir));
60147c478bd9Sstevel@tonic-gate 			err = zonecfg_lookup_ipd(handle, &tmp_fstab);
60157c478bd9Sstevel@tonic-gate 			if (err == Z_OK) {
60167c478bd9Sstevel@tonic-gate 				zerr(gettext("An %s resource "
60177c478bd9Sstevel@tonic-gate 				    "with the %s '%s' already exists."),
60187c478bd9Sstevel@tonic-gate 				    rt_to_str(RT_IPD), pt_to_str(PT_DIR),
60197c478bd9Sstevel@tonic-gate 				    in_progress_ipdtab.zone_fs_dir);
6020bbec428eSgjelinek 				saw_error = B_TRUE;
60217c478bd9Sstevel@tonic-gate 				return;
60227c478bd9Sstevel@tonic-gate 			}
60237c478bd9Sstevel@tonic-gate 			err = zonecfg_add_ipd(handle, &in_progress_ipdtab);
60247c478bd9Sstevel@tonic-gate 		} else {
60257c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_ipd(handle, &old_ipdtab,
60267c478bd9Sstevel@tonic-gate 			    &in_progress_ipdtab);
60277c478bd9Sstevel@tonic-gate 		}
60287c478bd9Sstevel@tonic-gate 		break;
60297c478bd9Sstevel@tonic-gate 	case RT_NET:
6030f4b3ec61Sdh 		/*
6031f4b3ec61Sdh 		 * First make sure everything was filled in.
6032f4b3ec61Sdh 		 * Since we don't know whether IP will be shared
6033f4b3ec61Sdh 		 * or exclusive here, some checks are deferred until
6034f4b3ec61Sdh 		 * the verify command.
6035f4b3ec61Sdh 		 */
6036087719fdSdp 		(void) end_check_reqd(in_progress_nwiftab.zone_nwif_physical,
6037087719fdSdp 		    PT_PHYSICAL, &validation_failed);
6038087719fdSdp 
6039087719fdSdp 		if (validation_failed) {
6040bbec428eSgjelinek 			saw_error = B_TRUE;
60417c478bd9Sstevel@tonic-gate 			return;
6042087719fdSdp 		}
60437c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
60447c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
60457c478bd9Sstevel@tonic-gate 			bzero(&tmp_nwiftab, sizeof (tmp_nwiftab));
6046f4b3ec61Sdh 			(void) strlcpy(tmp_nwiftab.zone_nwif_physical,
6047f4b3ec61Sdh 			    in_progress_nwiftab.zone_nwif_physical,
6048f4b3ec61Sdh 			    sizeof (tmp_nwiftab.zone_nwif_physical));
60497c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_nwiftab.zone_nwif_address,
60507c478bd9Sstevel@tonic-gate 			    in_progress_nwiftab.zone_nwif_address,
60517c478bd9Sstevel@tonic-gate 			    sizeof (tmp_nwiftab.zone_nwif_address));
60527c478bd9Sstevel@tonic-gate 			if (zonecfg_lookup_nwif(handle, &tmp_nwiftab) == Z_OK) {
6053f4b3ec61Sdh 				zerr(gettext("A %s resource with the %s '%s', "
6054f4b3ec61Sdh 				    "and %s '%s' already exists."),
6055f4b3ec61Sdh 				    rt_to_str(RT_NET),
6056f4b3ec61Sdh 				    pt_to_str(PT_PHYSICAL),
6057f4b3ec61Sdh 				    in_progress_nwiftab.zone_nwif_physical,
6058f4b3ec61Sdh 				    pt_to_str(PT_ADDRESS),
60597c478bd9Sstevel@tonic-gate 				    in_progress_nwiftab.zone_nwif_address);
6060bbec428eSgjelinek 				saw_error = B_TRUE;
60617c478bd9Sstevel@tonic-gate 				return;
60627c478bd9Sstevel@tonic-gate 			}
60637c478bd9Sstevel@tonic-gate 			err = zonecfg_add_nwif(handle, &in_progress_nwiftab);
60647c478bd9Sstevel@tonic-gate 		} else {
60657c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_nwif(handle, &old_nwiftab,
60667c478bd9Sstevel@tonic-gate 			    &in_progress_nwiftab);
60677c478bd9Sstevel@tonic-gate 		}
60687c478bd9Sstevel@tonic-gate 		break;
6069087719fdSdp 
60707c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
60717c478bd9Sstevel@tonic-gate 		/* First make sure everything was filled in. */
6072087719fdSdp 		(void) end_check_reqd(in_progress_devtab.zone_dev_match,
6073087719fdSdp 		    PT_MATCH, &validation_failed);
6074087719fdSdp 
6075087719fdSdp 		if (validation_failed) {
6076bbec428eSgjelinek 			saw_error = B_TRUE;
60777c478bd9Sstevel@tonic-gate 			return;
6078087719fdSdp 		}
6079087719fdSdp 
60807c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
60817c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
60827c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_devtab.zone_dev_match,
60837c478bd9Sstevel@tonic-gate 			    in_progress_devtab.zone_dev_match,
60847c478bd9Sstevel@tonic-gate 			    sizeof (tmp_devtab.zone_dev_match));
60857c478bd9Sstevel@tonic-gate 			if (zonecfg_lookup_dev(handle, &tmp_devtab) == Z_OK) {
60867c478bd9Sstevel@tonic-gate 				zerr(gettext("A %s resource with the %s '%s' "
60877c478bd9Sstevel@tonic-gate 				    "already exists."), rt_to_str(RT_DEVICE),
60887c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_MATCH),
60897c478bd9Sstevel@tonic-gate 				    in_progress_devtab.zone_dev_match);
6090bbec428eSgjelinek 				saw_error = B_TRUE;
60917c478bd9Sstevel@tonic-gate 				return;
60927c478bd9Sstevel@tonic-gate 			}
60937c478bd9Sstevel@tonic-gate 			err = zonecfg_add_dev(handle, &in_progress_devtab);
60947c478bd9Sstevel@tonic-gate 		} else {
60957c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_dev(handle, &old_devtab,
60967c478bd9Sstevel@tonic-gate 			    &in_progress_devtab);
60977c478bd9Sstevel@tonic-gate 		}
60987c478bd9Sstevel@tonic-gate 		break;
6099087719fdSdp 
61007c478bd9Sstevel@tonic-gate 	case RT_RCTL:
61017c478bd9Sstevel@tonic-gate 		/* First make sure everything was filled in. */
6102087719fdSdp 		(void) end_check_reqd(in_progress_rctltab.zone_rctl_name,
6103087719fdSdp 		    PT_NAME, &validation_failed);
6104087719fdSdp 
61057c478bd9Sstevel@tonic-gate 		if (in_progress_rctltab.zone_rctl_valptr == NULL) {
61067c478bd9Sstevel@tonic-gate 			zerr(gettext("no %s specified"), pt_to_str(PT_VALUE));
6107bbec428eSgjelinek 			validation_failed = B_TRUE;
61087c478bd9Sstevel@tonic-gate 		}
6109087719fdSdp 
6110087719fdSdp 		if (validation_failed) {
6111bbec428eSgjelinek 			saw_error = B_TRUE;
61127c478bd9Sstevel@tonic-gate 			return;
6113087719fdSdp 		}
6114087719fdSdp 
61157c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
61167c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
61177c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_rctltab.zone_rctl_name,
61187c478bd9Sstevel@tonic-gate 			    in_progress_rctltab.zone_rctl_name,
61197c478bd9Sstevel@tonic-gate 			    sizeof (tmp_rctltab.zone_rctl_name));
61207c478bd9Sstevel@tonic-gate 			tmp_rctltab.zone_rctl_valptr = NULL;
61217c478bd9Sstevel@tonic-gate 			err = zonecfg_lookup_rctl(handle, &tmp_rctltab);
61227c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(
61237c478bd9Sstevel@tonic-gate 			    tmp_rctltab.zone_rctl_valptr);
61247c478bd9Sstevel@tonic-gate 			if (err == Z_OK) {
61257c478bd9Sstevel@tonic-gate 				zerr(gettext("A %s resource "
61267c478bd9Sstevel@tonic-gate 				    "with the %s '%s' already exists."),
61277c478bd9Sstevel@tonic-gate 				    rt_to_str(RT_RCTL), pt_to_str(PT_NAME),
61287c478bd9Sstevel@tonic-gate 				    in_progress_rctltab.zone_rctl_name);
6129bbec428eSgjelinek 				saw_error = B_TRUE;
61307c478bd9Sstevel@tonic-gate 				return;
61317c478bd9Sstevel@tonic-gate 			}
61327c478bd9Sstevel@tonic-gate 			err = zonecfg_add_rctl(handle, &in_progress_rctltab);
61337c478bd9Sstevel@tonic-gate 		} else {
61347c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_rctl(handle, &old_rctltab,
61357c478bd9Sstevel@tonic-gate 			    &in_progress_rctltab);
61367c478bd9Sstevel@tonic-gate 		}
61377c478bd9Sstevel@tonic-gate 		if (err == Z_OK) {
61387c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(
61397c478bd9Sstevel@tonic-gate 			    in_progress_rctltab.zone_rctl_valptr);
61407c478bd9Sstevel@tonic-gate 			in_progress_rctltab.zone_rctl_valptr = NULL;
61417c478bd9Sstevel@tonic-gate 		}
61427c478bd9Sstevel@tonic-gate 		break;
6143087719fdSdp 
61447c478bd9Sstevel@tonic-gate 	case RT_ATTR:
61457c478bd9Sstevel@tonic-gate 		/* First make sure everything was filled in. */
6146087719fdSdp 		(void) end_check_reqd(in_progress_attrtab.zone_attr_name,
6147087719fdSdp 		    PT_NAME, &validation_failed);
6148087719fdSdp 		(void) end_check_reqd(in_progress_attrtab.zone_attr_type,
6149087719fdSdp 		    PT_TYPE, &validation_failed);
6150087719fdSdp 		(void) end_check_reqd(in_progress_attrtab.zone_attr_value,
6151087719fdSdp 		    PT_VALUE, &validation_failed);
6152087719fdSdp 
61537c478bd9Sstevel@tonic-gate 		if (validate_attr_name(in_progress_attrtab.zone_attr_name) !=
6154087719fdSdp 		    Z_OK)
6155bbec428eSgjelinek 			validation_failed = B_TRUE;
6156087719fdSdp 
6157087719fdSdp 		if (validate_attr_type_val(&in_progress_attrtab) != Z_OK)
6158bbec428eSgjelinek 			validation_failed = B_TRUE;
6159087719fdSdp 
6160087719fdSdp 		if (validation_failed) {
6161bbec428eSgjelinek 			saw_error = B_TRUE;
61627c478bd9Sstevel@tonic-gate 			return;
6163087719fdSdp 		}
61647c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
61657c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
61667c478bd9Sstevel@tonic-gate 			bzero(&tmp_attrtab, sizeof (tmp_attrtab));
61677c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_attrtab.zone_attr_name,
61687c478bd9Sstevel@tonic-gate 			    in_progress_attrtab.zone_attr_name,
61697c478bd9Sstevel@tonic-gate 			    sizeof (tmp_attrtab.zone_attr_name));
61707c478bd9Sstevel@tonic-gate 			if (zonecfg_lookup_attr(handle, &tmp_attrtab) == Z_OK) {
61717c478bd9Sstevel@tonic-gate 				zerr(gettext("An %s resource "
61727c478bd9Sstevel@tonic-gate 				    "with the %s '%s' already exists."),
61737c478bd9Sstevel@tonic-gate 				    rt_to_str(RT_ATTR), pt_to_str(PT_NAME),
61747c478bd9Sstevel@tonic-gate 				    in_progress_attrtab.zone_attr_name);
6175bbec428eSgjelinek 				saw_error = B_TRUE;
61767c478bd9Sstevel@tonic-gate 				return;
61777c478bd9Sstevel@tonic-gate 			}
61787c478bd9Sstevel@tonic-gate 			err = zonecfg_add_attr(handle, &in_progress_attrtab);
61797c478bd9Sstevel@tonic-gate 		} else {
61807c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_attr(handle, &old_attrtab,
61817c478bd9Sstevel@tonic-gate 			    &in_progress_attrtab);
61827c478bd9Sstevel@tonic-gate 		}
61837c478bd9Sstevel@tonic-gate 		break;
6184fa9e4066Sahrens 	case RT_DATASET:
6185fa9e4066Sahrens 		/* First make sure everything was filled in. */
6186fa9e4066Sahrens 		if (strlen(in_progress_dstab.zone_dataset_name) == 0) {
6187fa9e4066Sahrens 			zerr("%s %s", pt_to_str(PT_NAME),
6188fa9e4066Sahrens 			    gettext("not specified"));
6189bbec428eSgjelinek 			saw_error = B_TRUE;
6190bbec428eSgjelinek 			validation_failed = B_TRUE;
6191fa9e4066Sahrens 		}
6192fa9e4066Sahrens 		if (validation_failed)
6193fa9e4066Sahrens 			return;
6194fa9e4066Sahrens 		if (end_op == CMD_ADD) {
6195fa9e4066Sahrens 			/* Make sure there isn't already one like this. */
6196fa9e4066Sahrens 			bzero(&tmp_dstab, sizeof (tmp_dstab));
6197fa9e4066Sahrens 			(void) strlcpy(tmp_dstab.zone_dataset_name,
6198fa9e4066Sahrens 			    in_progress_dstab.zone_dataset_name,
6199fa9e4066Sahrens 			    sizeof (tmp_dstab.zone_dataset_name));
6200fa9e4066Sahrens 			err = zonecfg_lookup_ds(handle, &tmp_dstab);
6201fa9e4066Sahrens 			if (err == Z_OK) {
6202fa9e4066Sahrens 				zerr(gettext("A %s resource "
6203fa9e4066Sahrens 				    "with the %s '%s' already exists."),
6204fa9e4066Sahrens 				    rt_to_str(RT_DATASET), pt_to_str(PT_NAME),
6205fa9e4066Sahrens 				    in_progress_dstab.zone_dataset_name);
6206bbec428eSgjelinek 				saw_error = B_TRUE;
6207fa9e4066Sahrens 				return;
6208fa9e4066Sahrens 			}
6209fa9e4066Sahrens 			err = zonecfg_add_ds(handle, &in_progress_dstab);
6210fa9e4066Sahrens 		} else {
6211fa9e4066Sahrens 			err = zonecfg_modify_ds(handle, &old_dstab,
6212fa9e4066Sahrens 			    &in_progress_dstab);
6213fa9e4066Sahrens 		}
6214fa9e4066Sahrens 		break;
62150209230bSgjelinek 	case RT_DCPU:
62160209230bSgjelinek 		/* Make sure everything was filled in. */
62170209230bSgjelinek 		if (end_check_reqd(in_progress_psettab.zone_ncpu_min,
62180209230bSgjelinek 		    PT_NCPUS, &validation_failed) != Z_OK) {
6219bbec428eSgjelinek 			saw_error = B_TRUE;
62200209230bSgjelinek 			return;
62210209230bSgjelinek 		}
62220209230bSgjelinek 
62230209230bSgjelinek 		if (end_op == CMD_ADD) {
62240209230bSgjelinek 			err = zonecfg_add_pset(handle, &in_progress_psettab);
62250209230bSgjelinek 		} else {
62260209230bSgjelinek 			err = zonecfg_modify_pset(handle, &in_progress_psettab);
62270209230bSgjelinek 		}
62280209230bSgjelinek 		break;
6229c97ad5cdSakolb 	case RT_PCAP:
6230c97ad5cdSakolb 		/* Make sure everything was filled in. */
6231c97ad5cdSakolb 		if (zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &proc_cap)
6232c97ad5cdSakolb 		    != Z_OK) {
6233c97ad5cdSakolb 			zerr(gettext("%s not specified"), pt_to_str(PT_NCPUS));
6234bbec428eSgjelinek 			saw_error = B_TRUE;
6235bbec428eSgjelinek 			validation_failed = B_TRUE;
6236c97ad5cdSakolb 			return;
6237c97ad5cdSakolb 		}
6238c97ad5cdSakolb 		err = Z_OK;
6239c97ad5cdSakolb 		break;
62400209230bSgjelinek 	case RT_MCAP:
62410209230bSgjelinek 		/* Make sure everything was filled in. */
62420209230bSgjelinek 		res1 = strlen(in_progress_mcaptab.zone_physmem_cap) == 0 ?
62430209230bSgjelinek 		    Z_ERR : Z_OK;
62440209230bSgjelinek 		res2 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP,
62450209230bSgjelinek 		    &swap_limit);
62460209230bSgjelinek 		res3 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM,
62470209230bSgjelinek 		    &locked_limit);
62480209230bSgjelinek 
62490209230bSgjelinek 		if (res1 != Z_OK && res2 != Z_OK && res3 != Z_OK) {
62500209230bSgjelinek 			zerr(gettext("No property was specified.  One of %s, "
62510209230bSgjelinek 			    "%s or %s is required."), pt_to_str(PT_PHYSICAL),
62520209230bSgjelinek 			    pt_to_str(PT_SWAP), pt_to_str(PT_LOCKED));
6253bbec428eSgjelinek 			saw_error = B_TRUE;
62540209230bSgjelinek 			return;
62550209230bSgjelinek 		}
62560209230bSgjelinek 
62570209230bSgjelinek 		/* if phys & locked are both set, verify locked <= phys */
62580209230bSgjelinek 		if (res1 == Z_OK && res3 == Z_OK) {
62590209230bSgjelinek 			uint64_t phys_limit;
62600209230bSgjelinek 			char *endp;
62610209230bSgjelinek 
62620209230bSgjelinek 			phys_limit = strtoull(
62630209230bSgjelinek 			    in_progress_mcaptab.zone_physmem_cap, &endp, 10);
62640209230bSgjelinek 			if (phys_limit < locked_limit) {
62650209230bSgjelinek 				zerr(gettext("The %s cap must be less than or "
62660209230bSgjelinek 				    "equal to the %s cap."),
62670209230bSgjelinek 				    pt_to_str(PT_LOCKED),
62680209230bSgjelinek 				    pt_to_str(PT_PHYSICAL));
6269bbec428eSgjelinek 				saw_error = B_TRUE;
62700209230bSgjelinek 				return;
62710209230bSgjelinek 			}
62720209230bSgjelinek 		}
62730209230bSgjelinek 
62740209230bSgjelinek 		err = Z_OK;
62750209230bSgjelinek 		if (res1 == Z_OK) {
62760209230bSgjelinek 			/*
62770209230bSgjelinek 			 * We could be ending from either an add operation
62780209230bSgjelinek 			 * or a select operation.  Since all of the properties
62790209230bSgjelinek 			 * within this resource are optional, we always use
62800209230bSgjelinek 			 * modify on the mcap entry.  zonecfg_modify_mcap()
62810209230bSgjelinek 			 * will handle both adding and modifying a memory cap.
62820209230bSgjelinek 			 */
62830209230bSgjelinek 			err = zonecfg_modify_mcap(handle, &in_progress_mcaptab);
62840209230bSgjelinek 		} else if (end_op == CMD_SELECT) {
62850209230bSgjelinek 			/*
62860209230bSgjelinek 			 * If we're ending from a select and the physical
62870209230bSgjelinek 			 * memory cap is empty then the user could have cleared
62880209230bSgjelinek 			 * the physical cap value, so try to delete the entry.
62890209230bSgjelinek 			 */
62900209230bSgjelinek 			(void) zonecfg_delete_mcap(handle);
62910209230bSgjelinek 		}
62920209230bSgjelinek 		break;
62937c478bd9Sstevel@tonic-gate 	default:
62947c478bd9Sstevel@tonic-gate 		zone_perror(rt_to_str(resource_scope), Z_NO_RESOURCE_TYPE,
6295bbec428eSgjelinek 		    B_TRUE);
6296bbec428eSgjelinek 		saw_error = B_TRUE;
62977c478bd9Sstevel@tonic-gate 		return;
62987c478bd9Sstevel@tonic-gate 	}
62997c478bd9Sstevel@tonic-gate 
63007c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
6301bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
63027c478bd9Sstevel@tonic-gate 	} else {
6303bbec428eSgjelinek 		need_to_commit = B_TRUE;
6304bbec428eSgjelinek 		global_scope = B_TRUE;
63057c478bd9Sstevel@tonic-gate 		end_op = -1;
63067c478bd9Sstevel@tonic-gate 	}
63077c478bd9Sstevel@tonic-gate }
63087c478bd9Sstevel@tonic-gate 
63097c478bd9Sstevel@tonic-gate void
63107c478bd9Sstevel@tonic-gate commit_func(cmd_t *cmd)
63117c478bd9Sstevel@tonic-gate {
63127c478bd9Sstevel@tonic-gate 	int arg;
6313bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
63147c478bd9Sstevel@tonic-gate 
63157c478bd9Sstevel@tonic-gate 	optind = 0;
63167ec75eb8Sgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?")) != EOF) {
63177c478bd9Sstevel@tonic-gate 		switch (arg) {
63187c478bd9Sstevel@tonic-gate 		case '?':
63197c478bd9Sstevel@tonic-gate 			longer_usage(CMD_COMMIT);
6320bbec428eSgjelinek 			arg_err = B_TRUE;
63217ec75eb8Sgjelinek 			break;
63227c478bd9Sstevel@tonic-gate 		default:
63237c478bd9Sstevel@tonic-gate 			short_usage(CMD_COMMIT);
6324bbec428eSgjelinek 			arg_err = B_TRUE;
63257ec75eb8Sgjelinek 			break;
63267c478bd9Sstevel@tonic-gate 		}
63277c478bd9Sstevel@tonic-gate 	}
63287ec75eb8Sgjelinek 	if (arg_err)
63297ec75eb8Sgjelinek 		return;
63307ec75eb8Sgjelinek 
63317c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
63327c478bd9Sstevel@tonic-gate 		short_usage(CMD_COMMIT);
63337c478bd9Sstevel@tonic-gate 		return;
63347c478bd9Sstevel@tonic-gate 	}
63357c478bd9Sstevel@tonic-gate 
63367c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_COMMIT))
63377c478bd9Sstevel@tonic-gate 		return;
63387c478bd9Sstevel@tonic-gate 
63397c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
63407c478bd9Sstevel@tonic-gate 
63417c478bd9Sstevel@tonic-gate 	cmd->cmd_argc = 1;
63427c478bd9Sstevel@tonic-gate 	/*
63437c478bd9Sstevel@tonic-gate 	 * cmd_arg normally comes from a strdup() in the lexer, and the
63447c478bd9Sstevel@tonic-gate 	 * whole cmd structure and its (char *) attributes are freed at
63457c478bd9Sstevel@tonic-gate 	 * the completion of each command, so the strdup() below is needed
63467c478bd9Sstevel@tonic-gate 	 * to match this and prevent a core dump from trying to free()
63477c478bd9Sstevel@tonic-gate 	 * something that can't be.
63487c478bd9Sstevel@tonic-gate 	 */
63497c478bd9Sstevel@tonic-gate 	if ((cmd->cmd_argv[0] = strdup("save")) == NULL) {
6350bbec428eSgjelinek 		zone_perror(zone, Z_NOMEM, B_TRUE);
63517c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
63527c478bd9Sstevel@tonic-gate 	}
63537c478bd9Sstevel@tonic-gate 	cmd->cmd_argv[1] = NULL;
63547c478bd9Sstevel@tonic-gate 	verify_func(cmd);
63557c478bd9Sstevel@tonic-gate }
63567c478bd9Sstevel@tonic-gate 
63577c478bd9Sstevel@tonic-gate void
63587c478bd9Sstevel@tonic-gate revert_func(cmd_t *cmd)
63597c478bd9Sstevel@tonic-gate {
63607c478bd9Sstevel@tonic-gate 	char line[128];	/* enough to ask a question */
6361bbec428eSgjelinek 	boolean_t force = B_FALSE;
6362bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
63637c478bd9Sstevel@tonic-gate 	int err, arg, answer;
63647c478bd9Sstevel@tonic-gate 
63657c478bd9Sstevel@tonic-gate 	optind = 0;
63667c478bd9Sstevel@tonic-gate 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?F")) != EOF) {
63677c478bd9Sstevel@tonic-gate 		switch (arg) {
63687c478bd9Sstevel@tonic-gate 		case '?':
63697c478bd9Sstevel@tonic-gate 			longer_usage(CMD_REVERT);
6370bbec428eSgjelinek 			arg_err = B_TRUE;
63717ec75eb8Sgjelinek 			break;
63727c478bd9Sstevel@tonic-gate 		case 'F':
6373bbec428eSgjelinek 			force = B_TRUE;
63747c478bd9Sstevel@tonic-gate 			break;
63757c478bd9Sstevel@tonic-gate 		default:
63767c478bd9Sstevel@tonic-gate 			short_usage(CMD_REVERT);
6377bbec428eSgjelinek 			arg_err = B_TRUE;
63787ec75eb8Sgjelinek 			break;
63797c478bd9Sstevel@tonic-gate 		}
63807c478bd9Sstevel@tonic-gate 	}
63817ec75eb8Sgjelinek 	if (arg_err)
63827ec75eb8Sgjelinek 		return;
63837ec75eb8Sgjelinek 
63847c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
63857c478bd9Sstevel@tonic-gate 		short_usage(CMD_REVERT);
63867c478bd9Sstevel@tonic-gate 		return;
63877c478bd9Sstevel@tonic-gate 	}
63887c478bd9Sstevel@tonic-gate 
63897c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_REVERT))
63907c478bd9Sstevel@tonic-gate 		return;
63917c478bd9Sstevel@tonic-gate 
63925b418297Sjv 	if (!global_scope) {
63935b418297Sjv 		zerr(gettext("You can only use %s in the global scope.\nUse"
63945b418297Sjv 		    " '%s' to cancel changes to a resource specification."),
63955b418297Sjv 		    cmd_to_str(CMD_REVERT), cmd_to_str(CMD_CANCEL));
63965b418297Sjv 		saw_error = B_TRUE;
63975b418297Sjv 		return;
63985b418297Sjv 	}
63995b418297Sjv 
64007c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) != Z_OK) {
64017c478bd9Sstevel@tonic-gate 		zerr(gettext("No changes to revert."));
6402bbec428eSgjelinek 		saw_error = B_TRUE;
64037c478bd9Sstevel@tonic-gate 		return;
64047c478bd9Sstevel@tonic-gate 	}
64057c478bd9Sstevel@tonic-gate 
64067c478bd9Sstevel@tonic-gate 	if (!force) {
64077c478bd9Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
64087c478bd9Sstevel@tonic-gate 		    gettext("Are you sure you want to revert"));
6409bbec428eSgjelinek 		if ((answer = ask_yesno(B_FALSE, line)) == -1) {
64107c478bd9Sstevel@tonic-gate 			zerr(gettext("Input not from terminal and -F not "
64117c478bd9Sstevel@tonic-gate 			    "specified:\n%s command ignored, exiting."),
64127c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_REVERT));
64137c478bd9Sstevel@tonic-gate 			exit(Z_ERR);
64147c478bd9Sstevel@tonic-gate 		}
64157c478bd9Sstevel@tonic-gate 		if (answer != 1)
64167c478bd9Sstevel@tonic-gate 			return;
64177c478bd9Sstevel@tonic-gate 	}
64187c478bd9Sstevel@tonic-gate 
64197c478bd9Sstevel@tonic-gate 	/*
64207c478bd9Sstevel@tonic-gate 	 * Time for a new handle: finish the old one off first
64217c478bd9Sstevel@tonic-gate 	 * then get a new one properly to avoid leaks.
64227c478bd9Sstevel@tonic-gate 	 */
64237c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
64247c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
6425bbec428eSgjelinek 		zone_perror(execname, Z_NOMEM, B_TRUE);
64267c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
64277c478bd9Sstevel@tonic-gate 	}
6428087719fdSdp 	if ((err = zonecfg_get_handle(revert_zone, handle)) != Z_OK) {
6429bbec428eSgjelinek 		saw_error = B_TRUE;
6430bbec428eSgjelinek 		got_handle = B_FALSE;
64317c478bd9Sstevel@tonic-gate 		if (err == Z_NO_ZONE)
64327c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: no such saved zone to revert to."),
6433087719fdSdp 			    revert_zone);
64347c478bd9Sstevel@tonic-gate 		else
6435bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
64367c478bd9Sstevel@tonic-gate 	}
6437087719fdSdp 	(void) strlcpy(zone, revert_zone, sizeof (zone));
64387c478bd9Sstevel@tonic-gate }
64397c478bd9Sstevel@tonic-gate 
64407c478bd9Sstevel@tonic-gate void
64417c478bd9Sstevel@tonic-gate help_func(cmd_t *cmd)
64427c478bd9Sstevel@tonic-gate {
64437c478bd9Sstevel@tonic-gate 	int i;
64447c478bd9Sstevel@tonic-gate 
64457c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
64467c478bd9Sstevel@tonic-gate 
64477c478bd9Sstevel@tonic-gate 	if (cmd->cmd_argc == 0) {
6448bbec428eSgjelinek 		usage(B_TRUE, global_scope ? HELP_SUBCMDS : HELP_RES_SCOPE);
64497c478bd9Sstevel@tonic-gate 		return;
64507c478bd9Sstevel@tonic-gate 	}
64517c478bd9Sstevel@tonic-gate 	if (strcmp(cmd->cmd_argv[0], "usage") == 0) {
6452bbec428eSgjelinek 		usage(B_TRUE, HELP_USAGE);
64537c478bd9Sstevel@tonic-gate 		return;
64547c478bd9Sstevel@tonic-gate 	}
64557c478bd9Sstevel@tonic-gate 	if (strcmp(cmd->cmd_argv[0], "commands") == 0) {
6456bbec428eSgjelinek 		usage(B_TRUE, HELP_SUBCMDS);
64577c478bd9Sstevel@tonic-gate 		return;
64587c478bd9Sstevel@tonic-gate 	}
64597c478bd9Sstevel@tonic-gate 	if (strcmp(cmd->cmd_argv[0], "syntax") == 0) {
6460bbec428eSgjelinek 		usage(B_TRUE, HELP_SYNTAX | HELP_RES_PROPS);
64617c478bd9Sstevel@tonic-gate 		return;
64627c478bd9Sstevel@tonic-gate 	}
64637c478bd9Sstevel@tonic-gate 	if (strcmp(cmd->cmd_argv[0], "-?") == 0) {
64647c478bd9Sstevel@tonic-gate 		longer_usage(CMD_HELP);
64657c478bd9Sstevel@tonic-gate 		return;
64667c478bd9Sstevel@tonic-gate 	}
64677c478bd9Sstevel@tonic-gate 
64687c478bd9Sstevel@tonic-gate 	for (i = 0; i <= CMD_MAX; i++) {
64697c478bd9Sstevel@tonic-gate 		if (strcmp(cmd->cmd_argv[0], cmd_to_str(i)) == 0) {
64707c478bd9Sstevel@tonic-gate 			longer_usage(i);
64717c478bd9Sstevel@tonic-gate 			return;
64727c478bd9Sstevel@tonic-gate 		}
64737c478bd9Sstevel@tonic-gate 	}
64747c478bd9Sstevel@tonic-gate 	/* We do not use zerr() here because we do not want its extra \n. */
64757c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Unknown help subject %s.  "),
64767c478bd9Sstevel@tonic-gate 	    cmd->cmd_argv[0]);
6477bbec428eSgjelinek 	usage(B_FALSE, HELP_META);
64787c478bd9Sstevel@tonic-gate }
64797c478bd9Sstevel@tonic-gate 
64807c478bd9Sstevel@tonic-gate static int
64817c478bd9Sstevel@tonic-gate string_to_yyin(char *string)
64827c478bd9Sstevel@tonic-gate {
64837c478bd9Sstevel@tonic-gate 	if ((yyin = tmpfile()) == NULL) {
6484bbec428eSgjelinek 		zone_perror(execname, Z_TEMP_FILE, B_TRUE);
64857c478bd9Sstevel@tonic-gate 		return (Z_ERR);
64867c478bd9Sstevel@tonic-gate 	}
64877c478bd9Sstevel@tonic-gate 	if (fwrite(string, strlen(string), 1, yyin) != 1) {
6488bbec428eSgjelinek 		zone_perror(execname, Z_TEMP_FILE, B_TRUE);
64897c478bd9Sstevel@tonic-gate 		return (Z_ERR);
64907c478bd9Sstevel@tonic-gate 	}
64917c478bd9Sstevel@tonic-gate 	if (fseek(yyin, 0, SEEK_SET) != 0) {
6492bbec428eSgjelinek 		zone_perror(execname, Z_TEMP_FILE, B_TRUE);
64937c478bd9Sstevel@tonic-gate 		return (Z_ERR);
64947c478bd9Sstevel@tonic-gate 	}
64957c478bd9Sstevel@tonic-gate 	return (Z_OK);
64967c478bd9Sstevel@tonic-gate }
64977c478bd9Sstevel@tonic-gate 
64987c478bd9Sstevel@tonic-gate /* This is the back-end helper function for read_input() below. */
64997c478bd9Sstevel@tonic-gate 
65007c478bd9Sstevel@tonic-gate static int
65017c478bd9Sstevel@tonic-gate cleanup()
65027c478bd9Sstevel@tonic-gate {
65037c478bd9Sstevel@tonic-gate 	int answer;
65047c478bd9Sstevel@tonic-gate 	cmd_t *cmd;
65057c478bd9Sstevel@tonic-gate 
65067c478bd9Sstevel@tonic-gate 	if (!interactive_mode && !cmd_file_mode) {
65077c478bd9Sstevel@tonic-gate 		/*
65087c478bd9Sstevel@tonic-gate 		 * If we're not in interactive mode, and we're not in command
65097c478bd9Sstevel@tonic-gate 		 * file mode, then we must be in commands-from-the-command-line
65107c478bd9Sstevel@tonic-gate 		 * mode.  As such, we can't loop back and ask for more input.
65117c478bd9Sstevel@tonic-gate 		 * It was OK to prompt for such things as whether or not to
65127c478bd9Sstevel@tonic-gate 		 * really delete a zone in the command handler called from
65137c478bd9Sstevel@tonic-gate 		 * yyparse() above, but "really quit?" makes no sense in this
65147c478bd9Sstevel@tonic-gate 		 * context.  So disable prompting.
65157c478bd9Sstevel@tonic-gate 		 */
6516bbec428eSgjelinek 		ok_to_prompt = B_FALSE;
65177c478bd9Sstevel@tonic-gate 	}
65187c478bd9Sstevel@tonic-gate 	if (!global_scope) {
65197c478bd9Sstevel@tonic-gate 		if (!time_to_exit) {
65207c478bd9Sstevel@tonic-gate 			/*
65217c478bd9Sstevel@tonic-gate 			 * Just print a simple error message in the -1 case,
65227c478bd9Sstevel@tonic-gate 			 * since exit_func() already handles that case, and
65237c478bd9Sstevel@tonic-gate 			 * EOF means we are finished anyway.
65247c478bd9Sstevel@tonic-gate 			 */
6525bbec428eSgjelinek 			answer = ask_yesno(B_FALSE,
65267c478bd9Sstevel@tonic-gate 			    gettext("Resource incomplete; really quit"));
65277c478bd9Sstevel@tonic-gate 			if (answer == -1) {
65287c478bd9Sstevel@tonic-gate 				zerr(gettext("Resource incomplete."));
65297c478bd9Sstevel@tonic-gate 				return (Z_ERR);
65307c478bd9Sstevel@tonic-gate 			}
65317c478bd9Sstevel@tonic-gate 			if (answer != 1) {
65327c478bd9Sstevel@tonic-gate 				yyin = stdin;
65337c478bd9Sstevel@tonic-gate 				return (Z_REPEAT);
65347c478bd9Sstevel@tonic-gate 			}
65357c478bd9Sstevel@tonic-gate 		} else {
6536bbec428eSgjelinek 			saw_error = B_TRUE;
65377c478bd9Sstevel@tonic-gate 		}
65387c478bd9Sstevel@tonic-gate 	}
65397c478bd9Sstevel@tonic-gate 	/*
65407c478bd9Sstevel@tonic-gate 	 * Make sure we tried something and that the handle checks
65417c478bd9Sstevel@tonic-gate 	 * out, or we would get a false error trying to commit.
65427c478bd9Sstevel@tonic-gate 	 */
65437c478bd9Sstevel@tonic-gate 	if (need_to_commit && zonecfg_check_handle(handle) == Z_OK) {
65447c478bd9Sstevel@tonic-gate 		if ((cmd = alloc_cmd()) == NULL) {
6545bbec428eSgjelinek 			zone_perror(zone, Z_NOMEM, B_TRUE);
65467c478bd9Sstevel@tonic-gate 			return (Z_ERR);
65477c478bd9Sstevel@tonic-gate 		}
65487c478bd9Sstevel@tonic-gate 		cmd->cmd_argc = 0;
65497c478bd9Sstevel@tonic-gate 		cmd->cmd_argv[0] = NULL;
65507c478bd9Sstevel@tonic-gate 		commit_func(cmd);
65517c478bd9Sstevel@tonic-gate 		free_cmd(cmd);
65527c478bd9Sstevel@tonic-gate 		/*
65537c478bd9Sstevel@tonic-gate 		 * need_to_commit will get set back to FALSE if the
65547c478bd9Sstevel@tonic-gate 		 * configuration is saved successfully.
65557c478bd9Sstevel@tonic-gate 		 */
65567c478bd9Sstevel@tonic-gate 		if (need_to_commit) {
65577c478bd9Sstevel@tonic-gate 			if (force_exit) {
65587c478bd9Sstevel@tonic-gate 				zerr(gettext("Configuration not saved."));
65597c478bd9Sstevel@tonic-gate 				return (Z_ERR);
65607c478bd9Sstevel@tonic-gate 			}
6561bbec428eSgjelinek 			answer = ask_yesno(B_FALSE,
65627c478bd9Sstevel@tonic-gate 			    gettext("Configuration not saved; really quit"));
65637c478bd9Sstevel@tonic-gate 			if (answer == -1) {
65647c478bd9Sstevel@tonic-gate 				zerr(gettext("Configuration not saved."));
65657c478bd9Sstevel@tonic-gate 				return (Z_ERR);
65667c478bd9Sstevel@tonic-gate 			}
65677c478bd9Sstevel@tonic-gate 			if (answer != 1) {
6568bbec428eSgjelinek 				time_to_exit = B_FALSE;
65697c478bd9Sstevel@tonic-gate 				yyin = stdin;
65707c478bd9Sstevel@tonic-gate 				return (Z_REPEAT);
65717c478bd9Sstevel@tonic-gate 			}
65727c478bd9Sstevel@tonic-gate 		}
65737c478bd9Sstevel@tonic-gate 	}
65747c478bd9Sstevel@tonic-gate 	return ((need_to_commit || saw_error) ? Z_ERR : Z_OK);
65757c478bd9Sstevel@tonic-gate }
65767c478bd9Sstevel@tonic-gate 
65777c478bd9Sstevel@tonic-gate /*
65787c478bd9Sstevel@tonic-gate  * read_input() is the driver of this program.  It is a wrapper around
65797c478bd9Sstevel@tonic-gate  * yyparse(), printing appropriate prompts when needed, checking for
65807c478bd9Sstevel@tonic-gate  * exit conditions and reacting appropriately [the latter in its cleanup()
65817c478bd9Sstevel@tonic-gate  * helper function].
65827c478bd9Sstevel@tonic-gate  *
65837c478bd9Sstevel@tonic-gate  * Like most zonecfg functions, it returns Z_OK or Z_ERR, *or* Z_REPEAT
65847c478bd9Sstevel@tonic-gate  * so do_interactive() knows that we are not really done (i.e, we asked
65857c478bd9Sstevel@tonic-gate  * the user if we should really quit and the user said no).
65867c478bd9Sstevel@tonic-gate  */
65877c478bd9Sstevel@tonic-gate static int
65887c478bd9Sstevel@tonic-gate read_input()
65897c478bd9Sstevel@tonic-gate {
6590bbec428eSgjelinek 	boolean_t yyin_is_a_tty = isatty(fileno(yyin));
65917c478bd9Sstevel@tonic-gate 	/*
65927c478bd9Sstevel@tonic-gate 	 * The prompt is "e:z> " or "e:z:r> " where e is execname, z is zone
65937c478bd9Sstevel@tonic-gate 	 * and r is resource_scope: 5 is for the two ":"s + "> " + terminator.
65947c478bd9Sstevel@tonic-gate 	 */
65957c478bd9Sstevel@tonic-gate 	char prompt[MAXPATHLEN + ZONENAME_MAX + MAX_RT_STRLEN + 5], *line;
65967c478bd9Sstevel@tonic-gate 
65977c478bd9Sstevel@tonic-gate 	/* yyin should have been set to the appropriate (FILE *) if not stdin */
6598bbec428eSgjelinek 	newline_terminated = B_TRUE;
65997c478bd9Sstevel@tonic-gate 	for (;;) {
66007c478bd9Sstevel@tonic-gate 		if (yyin_is_a_tty) {
66017c478bd9Sstevel@tonic-gate 			if (newline_terminated) {
66027c478bd9Sstevel@tonic-gate 				if (global_scope)
66037c478bd9Sstevel@tonic-gate 					(void) snprintf(prompt, sizeof (prompt),
66047c478bd9Sstevel@tonic-gate 					    "%s:%s> ", execname, zone);
66057c478bd9Sstevel@tonic-gate 				else
66067c478bd9Sstevel@tonic-gate 					(void) snprintf(prompt, sizeof (prompt),
66077c478bd9Sstevel@tonic-gate 					    "%s:%s:%s> ", execname, zone,
66087c478bd9Sstevel@tonic-gate 					    rt_to_str(resource_scope));
66097c478bd9Sstevel@tonic-gate 			}
66107c478bd9Sstevel@tonic-gate 			/*
66117c478bd9Sstevel@tonic-gate 			 * If the user hits ^C then we want to catch it and
66127c478bd9Sstevel@tonic-gate 			 * start over.  If the user hits EOF then we want to
66137c478bd9Sstevel@tonic-gate 			 * bail out.
66147c478bd9Sstevel@tonic-gate 			 */
66157c478bd9Sstevel@tonic-gate 			line = gl_get_line(gl, prompt, NULL, -1);
66167c478bd9Sstevel@tonic-gate 			if (gl_return_status(gl) == GLR_SIGNAL) {
66177c478bd9Sstevel@tonic-gate 				gl_abandon_line(gl);
66187c478bd9Sstevel@tonic-gate 				continue;
66197c478bd9Sstevel@tonic-gate 			}
66207c478bd9Sstevel@tonic-gate 			if (line == NULL)
66217c478bd9Sstevel@tonic-gate 				break;
66227c478bd9Sstevel@tonic-gate 			(void) string_to_yyin(line);
66237c478bd9Sstevel@tonic-gate 			while (!feof(yyin))
66247c478bd9Sstevel@tonic-gate 				yyparse();
66257c478bd9Sstevel@tonic-gate 		} else {
66267c478bd9Sstevel@tonic-gate 			yyparse();
66277c478bd9Sstevel@tonic-gate 		}
66287c478bd9Sstevel@tonic-gate 		/* Bail out on an error in command file mode. */
66297c478bd9Sstevel@tonic-gate 		if (saw_error && cmd_file_mode && !interactive_mode)
6630bbec428eSgjelinek 			time_to_exit = B_TRUE;
66317c478bd9Sstevel@tonic-gate 		if (time_to_exit || (!yyin_is_a_tty && feof(yyin)))
66327c478bd9Sstevel@tonic-gate 			break;
66337c478bd9Sstevel@tonic-gate 	}
66347c478bd9Sstevel@tonic-gate 	return (cleanup());
66357c478bd9Sstevel@tonic-gate }
66367c478bd9Sstevel@tonic-gate 
66377c478bd9Sstevel@tonic-gate /*
66387c478bd9Sstevel@tonic-gate  * This function is used in the zonecfg-interactive-mode scenario: it just
66397c478bd9Sstevel@tonic-gate  * calls read_input() until we are done.
66407c478bd9Sstevel@tonic-gate  */
66417c478bd9Sstevel@tonic-gate 
66427c478bd9Sstevel@tonic-gate static int
66437c478bd9Sstevel@tonic-gate do_interactive(void)
66447c478bd9Sstevel@tonic-gate {
66457c478bd9Sstevel@tonic-gate 	int err;
66467c478bd9Sstevel@tonic-gate 
6647bbec428eSgjelinek 	interactive_mode = B_TRUE;
66487c478bd9Sstevel@tonic-gate 	if (!read_only_mode) {
66497c478bd9Sstevel@tonic-gate 		/*
66507c478bd9Sstevel@tonic-gate 		 * Try to set things up proactively in interactive mode, so
66517c478bd9Sstevel@tonic-gate 		 * that if the zone in question does not exist yet, we can
66527c478bd9Sstevel@tonic-gate 		 * provide the user with a clue.
66537c478bd9Sstevel@tonic-gate 		 */
6654bbec428eSgjelinek 		(void) initialize(B_FALSE);
66557c478bd9Sstevel@tonic-gate 	}
6656087719fdSdp 	do {
66577c478bd9Sstevel@tonic-gate 		err = read_input();
6658087719fdSdp 	} while (err == Z_REPEAT);
66597c478bd9Sstevel@tonic-gate 	return (err);
66607c478bd9Sstevel@tonic-gate }
66617c478bd9Sstevel@tonic-gate 
66627c478bd9Sstevel@tonic-gate /*
66637c478bd9Sstevel@tonic-gate  * cmd_file is slightly more complicated, as it has to open the command file
66647c478bd9Sstevel@tonic-gate  * and set yyin appropriately.  Once that is done, though, it just calls
66657c478bd9Sstevel@tonic-gate  * read_input(), and only once, since prompting is not possible.
66667c478bd9Sstevel@tonic-gate  */
66677c478bd9Sstevel@tonic-gate 
66687c478bd9Sstevel@tonic-gate static int
66697c478bd9Sstevel@tonic-gate cmd_file(char *file)
66707c478bd9Sstevel@tonic-gate {
66717c478bd9Sstevel@tonic-gate 	FILE *infile;
66727c478bd9Sstevel@tonic-gate 	int err;
66737c478bd9Sstevel@tonic-gate 	struct stat statbuf;
6674bbec428eSgjelinek 	boolean_t using_real_file = (strcmp(file, "-") != 0);
66757c478bd9Sstevel@tonic-gate 
66767c478bd9Sstevel@tonic-gate 	if (using_real_file) {
66777c478bd9Sstevel@tonic-gate 		/*
66787c478bd9Sstevel@tonic-gate 		 * zerr() prints a line number in cmd_file_mode, which we do
66797c478bd9Sstevel@tonic-gate 		 * not want here, so temporarily unset it.
66807c478bd9Sstevel@tonic-gate 		 */
6681bbec428eSgjelinek 		cmd_file_mode = B_FALSE;
66827c478bd9Sstevel@tonic-gate 		if ((infile = fopen(file, "r")) == NULL) {
66837c478bd9Sstevel@tonic-gate 			zerr(gettext("could not open file %s: %s"),
66847c478bd9Sstevel@tonic-gate 			    file, strerror(errno));
66857c478bd9Sstevel@tonic-gate 			return (Z_ERR);
66867c478bd9Sstevel@tonic-gate 		}
66877c478bd9Sstevel@tonic-gate 		if ((err = fstat(fileno(infile), &statbuf)) != 0) {
66887c478bd9Sstevel@tonic-gate 			zerr(gettext("could not stat file %s: %s"),
66897c478bd9Sstevel@tonic-gate 			    file, strerror(errno));
66907c478bd9Sstevel@tonic-gate 			err = Z_ERR;
66917c478bd9Sstevel@tonic-gate 			goto done;
66927c478bd9Sstevel@tonic-gate 		}
66937c478bd9Sstevel@tonic-gate 		if (!S_ISREG(statbuf.st_mode)) {
66947c478bd9Sstevel@tonic-gate 			zerr(gettext("%s is not a regular file."), file);
66957c478bd9Sstevel@tonic-gate 			err = Z_ERR;
66967c478bd9Sstevel@tonic-gate 			goto done;
66977c478bd9Sstevel@tonic-gate 		}
66987c478bd9Sstevel@tonic-gate 		yyin = infile;
6699bbec428eSgjelinek 		cmd_file_mode = B_TRUE;
6700bbec428eSgjelinek 		ok_to_prompt = B_FALSE;
67017c478bd9Sstevel@tonic-gate 	} else {
67027c478bd9Sstevel@tonic-gate 		/*
67037c478bd9Sstevel@tonic-gate 		 * "-f -" is essentially the same as interactive mode,
67047c478bd9Sstevel@tonic-gate 		 * so treat it that way.
67057c478bd9Sstevel@tonic-gate 		 */
6706bbec428eSgjelinek 		interactive_mode = B_TRUE;
67077c478bd9Sstevel@tonic-gate 	}
67087c478bd9Sstevel@tonic-gate 	/* Z_REPEAT is for interactive mode; treat it like Z_ERR here. */
67097c478bd9Sstevel@tonic-gate 	if ((err = read_input()) == Z_REPEAT)
67107c478bd9Sstevel@tonic-gate 		err = Z_ERR;
67117c478bd9Sstevel@tonic-gate done:
67127c478bd9Sstevel@tonic-gate 	if (using_real_file)
67137c478bd9Sstevel@tonic-gate 		(void) fclose(infile);
67147c478bd9Sstevel@tonic-gate 	return (err);
67157c478bd9Sstevel@tonic-gate }
67167c478bd9Sstevel@tonic-gate 
67177c478bd9Sstevel@tonic-gate /*
67187c478bd9Sstevel@tonic-gate  * Since yacc is based on reading from a (FILE *) whereas what we get from
67197c478bd9Sstevel@tonic-gate  * the command line is in argv format, we need to convert when the user
67207c478bd9Sstevel@tonic-gate  * gives us commands directly from the command line.  That is done here by
67217c478bd9Sstevel@tonic-gate  * concatenating the argv list into a space-separated string, writing it
67227c478bd9Sstevel@tonic-gate  * to a temp file, and rewinding the file so yyin can be set to it.  Then
67237c478bd9Sstevel@tonic-gate  * we call read_input(), and only once, since prompting about whether to
67247c478bd9Sstevel@tonic-gate  * continue or quit would make no sense in this context.
67257c478bd9Sstevel@tonic-gate  */
67267c478bd9Sstevel@tonic-gate 
67277c478bd9Sstevel@tonic-gate static int
67287c478bd9Sstevel@tonic-gate one_command_at_a_time(int argc, char *argv[])
67297c478bd9Sstevel@tonic-gate {
67307c478bd9Sstevel@tonic-gate 	char *command;
67317c478bd9Sstevel@tonic-gate 	size_t len = 2; /* terminal \n\0 */
67327c478bd9Sstevel@tonic-gate 	int i, err;
67337c478bd9Sstevel@tonic-gate 
67347c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++)
67357c478bd9Sstevel@tonic-gate 		len += strlen(argv[i]) + 1;
67367c478bd9Sstevel@tonic-gate 	if ((command = malloc(len)) == NULL) {
6737bbec428eSgjelinek 		zone_perror(execname, Z_NOMEM, B_TRUE);
67387c478bd9Sstevel@tonic-gate 		return (Z_ERR);
67397c478bd9Sstevel@tonic-gate 	}
67407c478bd9Sstevel@tonic-gate 	(void) strlcpy(command, argv[0], len);
67417c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
67427c478bd9Sstevel@tonic-gate 		(void) strlcat(command, " ", len);
67437c478bd9Sstevel@tonic-gate 		(void) strlcat(command, argv[i], len);
67447c478bd9Sstevel@tonic-gate 	}
67457c478bd9Sstevel@tonic-gate 	(void) strlcat(command, "\n", len);
67467c478bd9Sstevel@tonic-gate 	err = string_to_yyin(command);
67477c478bd9Sstevel@tonic-gate 	free(command);
67487c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
67497c478bd9Sstevel@tonic-gate 		return (err);
67507c478bd9Sstevel@tonic-gate 	while (!feof(yyin))
67517c478bd9Sstevel@tonic-gate 		yyparse();
67527c478bd9Sstevel@tonic-gate 	return (cleanup());
67537c478bd9Sstevel@tonic-gate }
67547c478bd9Sstevel@tonic-gate 
67557c478bd9Sstevel@tonic-gate static char *
67567c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
67577c478bd9Sstevel@tonic-gate {
67587c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
67597c478bd9Sstevel@tonic-gate 
67607c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
67617c478bd9Sstevel@tonic-gate 	for (;;) {
67627c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
67637c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
67647c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
67657c478bd9Sstevel@tonic-gate 			break;
67667c478bd9Sstevel@tonic-gate 		} else {
67677c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
67687c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
67697c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
67707c478bd9Sstevel@tonic-gate 				continue;
67717c478bd9Sstevel@tonic-gate 			}
67727c478bd9Sstevel@tonic-gate 			break;
67737c478bd9Sstevel@tonic-gate 		}
67747c478bd9Sstevel@tonic-gate 	}
67757c478bd9Sstevel@tonic-gate 	return (execbasename);
67767c478bd9Sstevel@tonic-gate }
67777c478bd9Sstevel@tonic-gate 
67787c478bd9Sstevel@tonic-gate int
67797c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
67807c478bd9Sstevel@tonic-gate {
67817c478bd9Sstevel@tonic-gate 	int err, arg;
6782555afedfScarlsonj 	struct stat st;
67837c478bd9Sstevel@tonic-gate 
67847c478bd9Sstevel@tonic-gate 	/* This must be before anything goes to stdout. */
67857c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
67867c478bd9Sstevel@tonic-gate 
6787bbec428eSgjelinek 	saw_error = B_FALSE;
6788bbec428eSgjelinek 	cmd_file_mode = B_FALSE;
67897c478bd9Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
67907c478bd9Sstevel@tonic-gate 
67917c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
67927c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
67937c478bd9Sstevel@tonic-gate 
67947c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
67957c478bd9Sstevel@tonic-gate 		zerr(gettext("%s can only be run from the global zone."),
67967c478bd9Sstevel@tonic-gate 		    execname);
67977c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
67987c478bd9Sstevel@tonic-gate 	}
67997c478bd9Sstevel@tonic-gate 
68007c478bd9Sstevel@tonic-gate 	if (argc < 2) {
6801bbec428eSgjelinek 		usage(B_FALSE, HELP_USAGE | HELP_SUBCMDS);
68027c478bd9Sstevel@tonic-gate 		exit(Z_USAGE);
68037c478bd9Sstevel@tonic-gate 	}
68047c478bd9Sstevel@tonic-gate 	if (strcmp(argv[1], cmd_to_str(CMD_HELP)) == 0) {
68057c478bd9Sstevel@tonic-gate 		(void) one_command_at_a_time(argc - 1, &(argv[1]));
68067c478bd9Sstevel@tonic-gate 		exit(Z_OK);
68077c478bd9Sstevel@tonic-gate 	}
68087c478bd9Sstevel@tonic-gate 
6809555afedfScarlsonj 	while ((arg = getopt(argc, argv, "?f:R:z:")) != EOF) {
68107c478bd9Sstevel@tonic-gate 		switch (arg) {
68117c478bd9Sstevel@tonic-gate 		case '?':
68127c478bd9Sstevel@tonic-gate 			if (optopt == '?')
6813bbec428eSgjelinek 				usage(B_TRUE, HELP_USAGE | HELP_SUBCMDS);
68147c478bd9Sstevel@tonic-gate 			else
6815bbec428eSgjelinek 				usage(B_FALSE, HELP_USAGE);
68167c478bd9Sstevel@tonic-gate 			exit(Z_USAGE);
68177c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
68187c478bd9Sstevel@tonic-gate 		case 'f':
68197c478bd9Sstevel@tonic-gate 			cmd_file_name = optarg;
6820bbec428eSgjelinek 			cmd_file_mode = B_TRUE;
68217c478bd9Sstevel@tonic-gate 			break;
6822555afedfScarlsonj 		case 'R':
6823555afedfScarlsonj 			if (*optarg != '/') {
6824555afedfScarlsonj 				zerr(gettext("root path must be absolute: %s"),
6825555afedfScarlsonj 				    optarg);
6826555afedfScarlsonj 				exit(Z_USAGE);
6827555afedfScarlsonj 			}
6828555afedfScarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
6829555afedfScarlsonj 				zerr(gettext(
6830555afedfScarlsonj 				    "root path must be a directory: %s"),
6831555afedfScarlsonj 				    optarg);
6832555afedfScarlsonj 				exit(Z_USAGE);
6833555afedfScarlsonj 			}
6834555afedfScarlsonj 			zonecfg_set_root(optarg);
6835555afedfScarlsonj 			break;
68367c478bd9Sstevel@tonic-gate 		case 'z':
68370209230bSgjelinek 			if (strcmp(optarg, GLOBAL_ZONENAME) == 0) {
6838bbec428eSgjelinek 				global_zone = B_TRUE;
68390209230bSgjelinek 			} else if (zonecfg_validate_zonename(optarg) != Z_OK) {
6840bbec428eSgjelinek 				zone_perror(optarg, Z_BOGUS_ZONE_NAME, B_TRUE);
6841bbec428eSgjelinek 				usage(B_FALSE, HELP_SYNTAX);
6842087719fdSdp 				exit(Z_USAGE);
6843087719fdSdp 			}
6844087719fdSdp 			(void) strlcpy(zone, optarg, sizeof (zone));
6845087719fdSdp 			(void) strlcpy(revert_zone, optarg, sizeof (zone));
68467c478bd9Sstevel@tonic-gate 			break;
68477c478bd9Sstevel@tonic-gate 		default:
6848bbec428eSgjelinek 			usage(B_FALSE, HELP_USAGE);
68497c478bd9Sstevel@tonic-gate 			exit(Z_USAGE);
68507c478bd9Sstevel@tonic-gate 		}
68517c478bd9Sstevel@tonic-gate 	}
68527c478bd9Sstevel@tonic-gate 
6853087719fdSdp 	if (optind > argc || strcmp(zone, "") == 0) {
6854bbec428eSgjelinek 		usage(B_FALSE, HELP_USAGE);
68557c478bd9Sstevel@tonic-gate 		exit(Z_USAGE);
68567c478bd9Sstevel@tonic-gate 	}
68577c478bd9Sstevel@tonic-gate 
6858087719fdSdp 	if ((err = zonecfg_access(zone, W_OK)) == Z_OK) {
6859bbec428eSgjelinek 		read_only_mode = B_FALSE;
6860087719fdSdp 	} else if (err == Z_ACCES) {
6861bbec428eSgjelinek 		read_only_mode = B_TRUE;
68627c478bd9Sstevel@tonic-gate 		/* skip this message in one-off from command line mode */
68637c478bd9Sstevel@tonic-gate 		if (optind == argc)
68647c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: you do not "
68657c478bd9Sstevel@tonic-gate 			    "have write access to this zone's configuration "
68667c478bd9Sstevel@tonic-gate 			    "file;\ngoing into read-only mode.\n"));
6867087719fdSdp 	} else {
6868087719fdSdp 		fprintf(stderr, "%s: Could not access zone configuration "
6869087719fdSdp 		    "store: %s\n", execname, zonecfg_strerror(err));
6870087719fdSdp 		exit(Z_ERR);
68717c478bd9Sstevel@tonic-gate 	}
68727c478bd9Sstevel@tonic-gate 
68737c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
6874bbec428eSgjelinek 		zone_perror(execname, Z_NOMEM, B_TRUE);
68757c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
68767c478bd9Sstevel@tonic-gate 	}
68777c478bd9Sstevel@tonic-gate 
68787c478bd9Sstevel@tonic-gate 	/*
68797c478bd9Sstevel@tonic-gate 	 * This may get set back to FALSE again in cmd_file() if cmd_file_name
68807c478bd9Sstevel@tonic-gate 	 * is a "real" file as opposed to "-" (i.e. meaning use stdin).
68817c478bd9Sstevel@tonic-gate 	 */
68827c478bd9Sstevel@tonic-gate 	if (isatty(STDIN_FILENO))
6883bbec428eSgjelinek 		ok_to_prompt = B_TRUE;
68847c478bd9Sstevel@tonic-gate 	if ((gl = new_GetLine(MAX_LINE_LEN, MAX_CMD_HIST)) == NULL)
68857c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
68867c478bd9Sstevel@tonic-gate 	if (gl_customize_completion(gl, NULL, cmd_cpl_fn) != 0)
68877c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
68887c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
68897c478bd9Sstevel@tonic-gate 	if (optind == argc) {
68907c478bd9Sstevel@tonic-gate 		if (!cmd_file_mode)
68917c478bd9Sstevel@tonic-gate 			err = do_interactive();
68927c478bd9Sstevel@tonic-gate 		else
68937c478bd9Sstevel@tonic-gate 			err = cmd_file(cmd_file_name);
68947c478bd9Sstevel@tonic-gate 	} else {
68957c478bd9Sstevel@tonic-gate 		err = one_command_at_a_time(argc - optind, &(argv[optind]));
68967c478bd9Sstevel@tonic-gate 	}
68977c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
68989acbbeafSnn 	if (brand != NULL)
68999acbbeafSnn 		brand_close(brand);
69007c478bd9Sstevel@tonic-gate 	(void) del_GetLine(gl);
69017c478bd9Sstevel@tonic-gate 	return (err);
69027c478bd9Sstevel@tonic-gate }
6903