xref: /titanic_53/usr/src/cmd/zonecfg/zonecfg.c (revision 16ab8c7b0738497a3fa85cb1afebcfd7844517d8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
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 /*
23de860bd9Sgfaden  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * zonecfg is a lex/yacc based command interpreter used to manage zone
317c478bd9Sstevel@tonic-gate  * configurations.  The lexer (see zonecfg_lex.l) builds up tokens, which
327c478bd9Sstevel@tonic-gate  * the grammar (see zonecfg_grammar.y) builds up into commands, some of
337c478bd9Sstevel@tonic-gate  * which takes resources and/or properties as arguments.  See the block
347c478bd9Sstevel@tonic-gate  * comments near the end of zonecfg_grammar.y for how the data structures
357c478bd9Sstevel@tonic-gate  * which keep track of these resources and properties are built up.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * The resource/property data structures are inserted into a command
387c478bd9Sstevel@tonic-gate  * structure (see zonecfg.h), which also keeps track of command names,
397c478bd9Sstevel@tonic-gate  * miscellaneous arguments, and function handlers.  The grammar selects
407c478bd9Sstevel@tonic-gate  * the appropriate function handler, each of which takes a pointer to a
417c478bd9Sstevel@tonic-gate  * command structure as its sole argument, and invokes it.  The grammar
427c478bd9Sstevel@tonic-gate  * itself is "entered" (a la the Matrix) by yyparse(), which is called
437c478bd9Sstevel@tonic-gate  * from read_input(), our main driving function.  That in turn is called
447c478bd9Sstevel@tonic-gate  * by one of do_interactive(), cmd_file() or one_command_at_a_time(), each
457c478bd9Sstevel@tonic-gate  * of which is called from main() depending on how the program was invoked.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * The rest of this module consists of the various function handlers and
487c478bd9Sstevel@tonic-gate  * their helper functions.  Some of these functions, particularly the
497c478bd9Sstevel@tonic-gate  * X_to_str() functions, which maps command, resource and property numbers
507c478bd9Sstevel@tonic-gate  * to strings, are used quite liberally, as doing so results in a better
517c478bd9Sstevel@tonic-gate  * program w/rt I18N, reducing the need for translation notes.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
557c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
567c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <errno.h>
599acbbeafSnn35248 #include <fcntl.h>
607c478bd9Sstevel@tonic-gate #include <strings.h>
617c478bd9Sstevel@tonic-gate #include <unistd.h>
627c478bd9Sstevel@tonic-gate #include <ctype.h>
637c478bd9Sstevel@tonic-gate #include <stdlib.h>
647c478bd9Sstevel@tonic-gate #include <assert.h>
657c478bd9Sstevel@tonic-gate #include <sys/stat.h>
667c478bd9Sstevel@tonic-gate #include <zone.h>
677c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
687c478bd9Sstevel@tonic-gate #include <netdb.h>
697c478bd9Sstevel@tonic-gate #include <locale.h>
707c478bd9Sstevel@tonic-gate #include <libintl.h>
717c478bd9Sstevel@tonic-gate #include <alloca.h>
727c478bd9Sstevel@tonic-gate #include <signal.h>
739acbbeafSnn35248 #include <wait.h>
747c478bd9Sstevel@tonic-gate #include <libtecla.h>
75fa9e4066Sahrens #include <libzfs.h>
769acbbeafSnn35248 #include <sys/brand.h>
779acbbeafSnn35248 #include <libbrand.h>
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
807c478bd9Sstevel@tonic-gate #include "zonecfg.h"
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
837c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #define	PAGER	"/usr/bin/more"
879acbbeafSnn35248 #define	EXEC_PREFIX	"exec "
889acbbeafSnn35248 #define	EXEC_LEN	(strlen(EXEC_PREFIX))
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate struct help {
917c478bd9Sstevel@tonic-gate 	uint_t	cmd_num;
927c478bd9Sstevel@tonic-gate 	char	*cmd_name;
937c478bd9Sstevel@tonic-gate 	uint_t	flags;
947c478bd9Sstevel@tonic-gate 	char	*short_usage;
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate extern int yyparse(void);
987c478bd9Sstevel@tonic-gate extern int lex_lineno;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #define	MAX_LINE_LEN	1024
1017c478bd9Sstevel@tonic-gate #define	MAX_CMD_HIST	1024
1029acbbeafSnn35248 #define	MAX_CMD_LEN	1024
1037c478bd9Sstevel@tonic-gate 
1040209230bSgjelinek #define	ONE_MB		1048576
1050209230bSgjelinek 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * Each SHELP_ should be a simple string.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define	SHELP_ADD	"add <resource-type>\n\t(global scope)\n" \
1117c478bd9Sstevel@tonic-gate 	"add <property-name> <property-value>\n\t(resource scope)"
1127c478bd9Sstevel@tonic-gate #define	SHELP_CANCEL	"cancel"
1130209230bSgjelinek #define	SHELP_CLEAR	"clear <property-name>"
1147c478bd9Sstevel@tonic-gate #define	SHELP_COMMIT	"commit"
115ee519a1fSgjelinek #define	SHELP_CREATE	"create [-F] [ -a <path> | -b | -t <template> ]"
1167c478bd9Sstevel@tonic-gate #define	SHELP_DELETE	"delete [-F]"
1177c478bd9Sstevel@tonic-gate #define	SHELP_END	"end"
1187c478bd9Sstevel@tonic-gate #define	SHELP_EXIT	"exit [-F]"
1197c478bd9Sstevel@tonic-gate #define	SHELP_EXPORT	"export [-f output-file]"
1207c478bd9Sstevel@tonic-gate #define	SHELP_HELP	"help [commands] [syntax] [usage] [<command-name>]"
1217c478bd9Sstevel@tonic-gate #define	SHELP_INFO	"info [<resource-type> [property-name=property-value]*]"
1220209230bSgjelinek #define	SHELP_REMOVE	"remove [-F] <resource-type> " \
1230209230bSgjelinek 	"[ <property-name>=<property-value> ]*\n" \
1240209230bSgjelinek 	"\t(global scope)\n" \
1250209230bSgjelinek 	"remove <property-name> <property-value>\n" \
1260209230bSgjelinek 	"\t(resource scope)"
1277c478bd9Sstevel@tonic-gate #define	SHELP_REVERT	"revert [-F]"
1287c478bd9Sstevel@tonic-gate #define	SHELP_SELECT	"select <resource-type> { <property-name>=" \
1297c478bd9Sstevel@tonic-gate 	"<property-value> }"
1307c478bd9Sstevel@tonic-gate #define	SHELP_SET	"set <property-name>=<property-value>"
1317c478bd9Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static struct help helptab[] = {
1347c478bd9Sstevel@tonic-gate 	{ CMD_ADD,	"add",		HELP_RES_PROPS,	SHELP_ADD, },
1357c478bd9Sstevel@tonic-gate 	{ CMD_CANCEL,	"cancel",	0,		SHELP_CANCEL, },
1360209230bSgjelinek 	{ CMD_CLEAR,	"clear",	HELP_PROPS,	SHELP_CLEAR, },
1377c478bd9Sstevel@tonic-gate 	{ CMD_COMMIT,	"commit",	0,		SHELP_COMMIT, },
1387c478bd9Sstevel@tonic-gate 	{ CMD_CREATE,	"create",	0,		SHELP_CREATE, },
1397c478bd9Sstevel@tonic-gate 	{ CMD_DELETE,	"delete",	0,		SHELP_DELETE, },
1407c478bd9Sstevel@tonic-gate 	{ CMD_END,	"end",		0,		SHELP_END, },
1417c478bd9Sstevel@tonic-gate 	{ CMD_EXIT,	"exit",		0,		SHELP_EXIT, },
1427c478bd9Sstevel@tonic-gate 	{ CMD_EXPORT,	"export",	0,		SHELP_EXPORT, },
1437c478bd9Sstevel@tonic-gate 	{ CMD_HELP,	"help",		0,		SHELP_HELP },
1447c478bd9Sstevel@tonic-gate 	{ CMD_INFO,	"info",		HELP_RES_PROPS,	SHELP_INFO, },
1457c478bd9Sstevel@tonic-gate 	{ CMD_REMOVE,	"remove",	HELP_RES_PROPS,	SHELP_REMOVE, },
1467c478bd9Sstevel@tonic-gate 	{ CMD_REVERT,	"revert",	0,		SHELP_REVERT, },
1477c478bd9Sstevel@tonic-gate 	{ CMD_SELECT,	"select",	HELP_RES_PROPS,	SHELP_SELECT, },
1487c478bd9Sstevel@tonic-gate 	{ CMD_SET,	"set",		HELP_PROPS,	SHELP_SET, },
1497c478bd9Sstevel@tonic-gate 	{ CMD_VERIFY,	"verify",	0,		SHELP_VERIFY, },
1507c478bd9Sstevel@tonic-gate 	{ 0 },
1517c478bd9Sstevel@tonic-gate };
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate #define	MAX_RT_STRLEN	16
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /* These *must* match the order of the RT_ define's from zonecfg.h */
1567c478bd9Sstevel@tonic-gate static char *res_types[] = {
1577c478bd9Sstevel@tonic-gate 	"unknown",
158087719fdSdp 	"zonename",
1597c478bd9Sstevel@tonic-gate 	"zonepath",
1607c478bd9Sstevel@tonic-gate 	"autoboot",
1617c478bd9Sstevel@tonic-gate 	"pool",
1627c478bd9Sstevel@tonic-gate 	"fs",
1637c478bd9Sstevel@tonic-gate 	"inherit-pkg-dir",
1647c478bd9Sstevel@tonic-gate 	"net",
1657c478bd9Sstevel@tonic-gate 	"device",
1667c478bd9Sstevel@tonic-gate 	"rctl",
1677c478bd9Sstevel@tonic-gate 	"attr",
168fa9e4066Sahrens 	"dataset",
169ffbafc53Scomay 	"limitpriv",
1703f2f09c1Sdp 	"bootargs",
1719acbbeafSnn35248 	"brand",
1720209230bSgjelinek 	"dedicated-cpu",
1730209230bSgjelinek 	"capped-memory",
1740209230bSgjelinek 	ALIAS_MAXLWPS,
1750209230bSgjelinek 	ALIAS_MAXSHMMEM,
1760209230bSgjelinek 	ALIAS_MAXSHMIDS,
1770209230bSgjelinek 	ALIAS_MAXMSGIDS,
1780209230bSgjelinek 	ALIAS_MAXSEMIDS,
1790209230bSgjelinek 	ALIAS_SHARES,
1800209230bSgjelinek 	"scheduling-class",
181f4b3ec61Sdh155122 	"ip-type",
182c97ad5cdSakolb 	"capped-cpu",
1837c478bd9Sstevel@tonic-gate 	NULL
1847c478bd9Sstevel@tonic-gate };
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /* These *must* match the order of the PT_ define's from zonecfg.h */
1877c478bd9Sstevel@tonic-gate static char *prop_types[] = {
1887c478bd9Sstevel@tonic-gate 	"unknown",
189087719fdSdp 	"zonename",
1907c478bd9Sstevel@tonic-gate 	"zonepath",
1917c478bd9Sstevel@tonic-gate 	"autoboot",
1927c478bd9Sstevel@tonic-gate 	"pool",
1937c478bd9Sstevel@tonic-gate 	"dir",
1947c478bd9Sstevel@tonic-gate 	"special",
1957c478bd9Sstevel@tonic-gate 	"type",
1967c478bd9Sstevel@tonic-gate 	"options",
1977c478bd9Sstevel@tonic-gate 	"address",
1987c478bd9Sstevel@tonic-gate 	"physical",
1997c478bd9Sstevel@tonic-gate 	"name",
2007c478bd9Sstevel@tonic-gate 	"value",
2017c478bd9Sstevel@tonic-gate 	"match",
2027c478bd9Sstevel@tonic-gate 	"priv",
2037c478bd9Sstevel@tonic-gate 	"limit",
2047c478bd9Sstevel@tonic-gate 	"action",
2057c478bd9Sstevel@tonic-gate 	"raw",
206ffbafc53Scomay 	"limitpriv",
2073f2f09c1Sdp 	"bootargs",
2089acbbeafSnn35248 	"brand",
2090209230bSgjelinek 	"ncpus",
2100209230bSgjelinek 	"importance",
2110209230bSgjelinek 	"swap",
2120209230bSgjelinek 	"locked",
2130209230bSgjelinek 	ALIAS_SHARES,
2140209230bSgjelinek 	ALIAS_MAXLWPS,
2150209230bSgjelinek 	ALIAS_MAXSHMMEM,
2160209230bSgjelinek 	ALIAS_MAXSHMIDS,
2170209230bSgjelinek 	ALIAS_MAXMSGIDS,
2180209230bSgjelinek 	ALIAS_MAXSEMIDS,
2190209230bSgjelinek 	ALIAS_MAXLOCKEDMEM,
2200209230bSgjelinek 	ALIAS_MAXSWAP,
2210209230bSgjelinek 	"scheduling-class",
222f4b3ec61Sdh155122 	"ip-type",
223de860bd9Sgfaden 	"defrouter",
2247c478bd9Sstevel@tonic-gate 	NULL
2257c478bd9Sstevel@tonic-gate };
2267c478bd9Sstevel@tonic-gate 
227ffbafc53Scomay /* These *must* match the order of the PROP_VAL_ define's from zonecfg.h */
2287c478bd9Sstevel@tonic-gate static char *prop_val_types[] = {
2297c478bd9Sstevel@tonic-gate 	"simple",
2307c478bd9Sstevel@tonic-gate 	"complex",
2317c478bd9Sstevel@tonic-gate 	"list",
2327c478bd9Sstevel@tonic-gate };
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * The various _cmds[] lists below are for command tab-completion.
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * remove has a space afterwards because it has qualifiers; the other commands
2400209230bSgjelinek  * that have qualifiers (add, select, etc.) don't need a space here because
2417c478bd9Sstevel@tonic-gate  * they have their own _cmds[] lists below.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate static const char *global_scope_cmds[] = {
2447c478bd9Sstevel@tonic-gate 	"add",
2450209230bSgjelinek 	"clear",
2467c478bd9Sstevel@tonic-gate 	"commit",
2477c478bd9Sstevel@tonic-gate 	"create",
2487c478bd9Sstevel@tonic-gate 	"delete",
2497c478bd9Sstevel@tonic-gate 	"exit",
2507c478bd9Sstevel@tonic-gate 	"export",
2517c478bd9Sstevel@tonic-gate 	"help",
2527c478bd9Sstevel@tonic-gate 	"info",
2537c478bd9Sstevel@tonic-gate 	"remove ",
2547c478bd9Sstevel@tonic-gate 	"revert",
2557c478bd9Sstevel@tonic-gate 	"select",
2567c478bd9Sstevel@tonic-gate 	"set",
2577c478bd9Sstevel@tonic-gate 	"verify",
2587c478bd9Sstevel@tonic-gate 	NULL
2597c478bd9Sstevel@tonic-gate };
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static const char *add_cmds[] = {
2627c478bd9Sstevel@tonic-gate 	"add fs",
2637c478bd9Sstevel@tonic-gate 	"add inherit-pkg-dir",
2647c478bd9Sstevel@tonic-gate 	"add net",
2657c478bd9Sstevel@tonic-gate 	"add device",
2667c478bd9Sstevel@tonic-gate 	"add rctl",
2677c478bd9Sstevel@tonic-gate 	"add attr",
268fa9e4066Sahrens 	"add dataset",
2690209230bSgjelinek 	"add dedicated-cpu",
270c97ad5cdSakolb 	"add capped-cpu",
2710209230bSgjelinek 	"add capped-memory",
2720209230bSgjelinek 	NULL
2730209230bSgjelinek };
2740209230bSgjelinek 
2750209230bSgjelinek static const char *clear_cmds[] = {
2760209230bSgjelinek 	"clear autoboot",
2770209230bSgjelinek 	"clear pool",
2780209230bSgjelinek 	"clear limitpriv",
2790209230bSgjelinek 	"clear bootargs",
2800209230bSgjelinek 	"clear scheduling-class",
281f4b3ec61Sdh155122 	"clear ip-type",
2820209230bSgjelinek 	"clear " ALIAS_MAXLWPS,
2830209230bSgjelinek 	"clear " ALIAS_MAXSHMMEM,
2840209230bSgjelinek 	"clear " ALIAS_MAXSHMIDS,
2850209230bSgjelinek 	"clear " ALIAS_MAXMSGIDS,
2860209230bSgjelinek 	"clear " ALIAS_MAXSEMIDS,
2870209230bSgjelinek 	"clear " ALIAS_SHARES,
2887c478bd9Sstevel@tonic-gate 	NULL
2897c478bd9Sstevel@tonic-gate };
2907c478bd9Sstevel@tonic-gate 
2919e7542f4Sdp static const char *remove_cmds[] = {
2929e7542f4Sdp 	"remove fs ",
2939e7542f4Sdp 	"remove inherit-pkg-dir ",
2949e7542f4Sdp 	"remove net ",
2959e7542f4Sdp 	"remove device ",
2969e7542f4Sdp 	"remove rctl ",
2979e7542f4Sdp 	"remove attr ",
2989e7542f4Sdp 	"remove dataset ",
2990209230bSgjelinek 	"remove dedicated-cpu ",
300c97ad5cdSakolb 	"remove capped-cpu ",
3010209230bSgjelinek 	"remove capped-memory ",
3029e7542f4Sdp 	NULL
3039e7542f4Sdp };
3049e7542f4Sdp 
3057c478bd9Sstevel@tonic-gate static const char *select_cmds[] = {
3067c478bd9Sstevel@tonic-gate 	"select fs ",
3077c478bd9Sstevel@tonic-gate 	"select inherit-pkg-dir ",
3087c478bd9Sstevel@tonic-gate 	"select net ",
3097c478bd9Sstevel@tonic-gate 	"select device ",
3107c478bd9Sstevel@tonic-gate 	"select rctl ",
3117c478bd9Sstevel@tonic-gate 	"select attr ",
312fa9e4066Sahrens 	"select dataset ",
3130209230bSgjelinek 	"select dedicated-cpu",
314c97ad5cdSakolb 	"select capped-cpu",
3150209230bSgjelinek 	"select capped-memory",
3167c478bd9Sstevel@tonic-gate 	NULL
3177c478bd9Sstevel@tonic-gate };
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate static const char *set_cmds[] = {
320087719fdSdp 	"set zonename=",
321087719fdSdp 	"set zonepath=",
3229acbbeafSnn35248 	"set brand=",
323087719fdSdp 	"set autoboot=",
324087719fdSdp 	"set pool=",
325ffbafc53Scomay 	"set limitpriv=",
3263f2f09c1Sdp 	"set bootargs=",
3270209230bSgjelinek 	"set scheduling-class=",
328f4b3ec61Sdh155122 	"set ip-type=",
3290209230bSgjelinek 	"set " ALIAS_MAXLWPS "=",
3300209230bSgjelinek 	"set " ALIAS_MAXSHMMEM "=",
3310209230bSgjelinek 	"set " ALIAS_MAXSHMIDS "=",
3320209230bSgjelinek 	"set " ALIAS_MAXMSGIDS "=",
3330209230bSgjelinek 	"set " ALIAS_MAXSEMIDS "=",
3340209230bSgjelinek 	"set " ALIAS_SHARES "=",
3357c478bd9Sstevel@tonic-gate 	NULL
3367c478bd9Sstevel@tonic-gate };
3377c478bd9Sstevel@tonic-gate 
3389e7542f4Sdp static const char *info_cmds[] = {
3399e7542f4Sdp 	"info fs ",
3409e7542f4Sdp 	"info inherit-pkg-dir ",
3419e7542f4Sdp 	"info net ",
3429e7542f4Sdp 	"info device ",
3439e7542f4Sdp 	"info rctl ",
3449e7542f4Sdp 	"info attr ",
3459e7542f4Sdp 	"info dataset ",
3460209230bSgjelinek 	"info capped-memory",
3470209230bSgjelinek 	"info dedicated-cpu",
348c97ad5cdSakolb 	"info capped-cpu",
3499e7542f4Sdp 	"info zonename",
3509e7542f4Sdp 	"info zonepath",
3519e7542f4Sdp 	"info autoboot",
3529e7542f4Sdp 	"info pool",
3539e7542f4Sdp 	"info limitpriv",
3549e7542f4Sdp 	"info bootargs",
3550209230bSgjelinek 	"info brand",
3560209230bSgjelinek 	"info scheduling-class",
357f4b3ec61Sdh155122 	"info ip-type",
3580209230bSgjelinek 	"info max-lwps",
3590209230bSgjelinek 	"info max-shm-memory",
3600209230bSgjelinek 	"info max-shm-ids",
3610209230bSgjelinek 	"info max-msg-ids",
3620209230bSgjelinek 	"info max-sem-ids",
3630209230bSgjelinek 	"info cpu-shares",
3649e7542f4Sdp 	NULL
3659e7542f4Sdp };
3669e7542f4Sdp 
3677c478bd9Sstevel@tonic-gate static const char *fs_res_scope_cmds[] = {
3687c478bd9Sstevel@tonic-gate 	"add options ",
3697c478bd9Sstevel@tonic-gate 	"cancel",
3707c478bd9Sstevel@tonic-gate 	"end",
3717c478bd9Sstevel@tonic-gate 	"exit",
3727c478bd9Sstevel@tonic-gate 	"help",
3737c478bd9Sstevel@tonic-gate 	"info",
374ffbafc53Scomay 	"remove options ",
3757c478bd9Sstevel@tonic-gate 	"set dir=",
3767c478bd9Sstevel@tonic-gate 	"set raw=",
3777c478bd9Sstevel@tonic-gate 	"set special=",
3787c478bd9Sstevel@tonic-gate 	"set type=",
3790209230bSgjelinek 	"clear raw",
3807c478bd9Sstevel@tonic-gate 	NULL
3817c478bd9Sstevel@tonic-gate };
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static const char *net_res_scope_cmds[] = {
3847c478bd9Sstevel@tonic-gate 	"cancel",
3857c478bd9Sstevel@tonic-gate 	"end",
3867c478bd9Sstevel@tonic-gate 	"exit",
3877c478bd9Sstevel@tonic-gate 	"help",
3887c478bd9Sstevel@tonic-gate 	"info",
3897c478bd9Sstevel@tonic-gate 	"set address=",
3907c478bd9Sstevel@tonic-gate 	"set physical=",
3917c478bd9Sstevel@tonic-gate 	NULL
3927c478bd9Sstevel@tonic-gate };
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static const char *ipd_res_scope_cmds[] = {
3957c478bd9Sstevel@tonic-gate 	"cancel",
3967c478bd9Sstevel@tonic-gate 	"end",
3977c478bd9Sstevel@tonic-gate 	"exit",
3987c478bd9Sstevel@tonic-gate 	"help",
3997c478bd9Sstevel@tonic-gate 	"info",
4007c478bd9Sstevel@tonic-gate 	"set dir=",
4017c478bd9Sstevel@tonic-gate 	NULL
4027c478bd9Sstevel@tonic-gate };
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate static const char *device_res_scope_cmds[] = {
4057c478bd9Sstevel@tonic-gate 	"cancel",
4067c478bd9Sstevel@tonic-gate 	"end",
4077c478bd9Sstevel@tonic-gate 	"exit",
4087c478bd9Sstevel@tonic-gate 	"help",
4097c478bd9Sstevel@tonic-gate 	"info",
4107c478bd9Sstevel@tonic-gate 	"set match=",
4117c478bd9Sstevel@tonic-gate 	NULL
4127c478bd9Sstevel@tonic-gate };
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate static const char *attr_res_scope_cmds[] = {
4157c478bd9Sstevel@tonic-gate 	"cancel",
4167c478bd9Sstevel@tonic-gate 	"end",
4177c478bd9Sstevel@tonic-gate 	"exit",
4187c478bd9Sstevel@tonic-gate 	"help",
4197c478bd9Sstevel@tonic-gate 	"info",
4207c478bd9Sstevel@tonic-gate 	"set name=",
4217c478bd9Sstevel@tonic-gate 	"set type=",
4227c478bd9Sstevel@tonic-gate 	"set value=",
4237c478bd9Sstevel@tonic-gate 	NULL
4247c478bd9Sstevel@tonic-gate };
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate static const char *rctl_res_scope_cmds[] = {
4277c478bd9Sstevel@tonic-gate 	"add value ",
4287c478bd9Sstevel@tonic-gate 	"cancel",
4297c478bd9Sstevel@tonic-gate 	"end",
4307c478bd9Sstevel@tonic-gate 	"exit",
4317c478bd9Sstevel@tonic-gate 	"help",
4327c478bd9Sstevel@tonic-gate 	"info",
433ffbafc53Scomay 	"remove value ",
4347c478bd9Sstevel@tonic-gate 	"set name=",
4357c478bd9Sstevel@tonic-gate 	NULL
4367c478bd9Sstevel@tonic-gate };
4377c478bd9Sstevel@tonic-gate 
438fa9e4066Sahrens static const char *dataset_res_scope_cmds[] = {
439fa9e4066Sahrens 	"cancel",
440fa9e4066Sahrens 	"end",
441fa9e4066Sahrens 	"exit",
442fa9e4066Sahrens 	"help",
443fa9e4066Sahrens 	"info",
444fa9e4066Sahrens 	"set name=",
445fa9e4066Sahrens 	NULL
446fa9e4066Sahrens };
447fa9e4066Sahrens 
4480209230bSgjelinek static const char *pset_res_scope_cmds[] = {
4490209230bSgjelinek 	"cancel",
4500209230bSgjelinek 	"end",
4510209230bSgjelinek 	"exit",
4520209230bSgjelinek 	"help",
4530209230bSgjelinek 	"info",
4540209230bSgjelinek 	"set ncpus=",
4550209230bSgjelinek 	"set importance=",
4560209230bSgjelinek 	"clear importance",
4570209230bSgjelinek 	NULL
4580209230bSgjelinek };
4590209230bSgjelinek 
460c97ad5cdSakolb static const char *pcap_res_scope_cmds[] = {
461c97ad5cdSakolb 	"cancel",
462c97ad5cdSakolb 	"end",
463c97ad5cdSakolb 	"exit",
464c97ad5cdSakolb 	"help",
465c97ad5cdSakolb 	"info",
466c97ad5cdSakolb 	"set ncpus=",
467c97ad5cdSakolb 	NULL
468c97ad5cdSakolb };
469c97ad5cdSakolb 
4700209230bSgjelinek static const char *mcap_res_scope_cmds[] = {
4710209230bSgjelinek 	"cancel",
4720209230bSgjelinek 	"end",
4730209230bSgjelinek 	"exit",
4740209230bSgjelinek 	"help",
4750209230bSgjelinek 	"info",
4760209230bSgjelinek 	"set physical=",
4770209230bSgjelinek 	"set swap=",
4780209230bSgjelinek 	"set locked=",
4790209230bSgjelinek 	"clear physical",
4800209230bSgjelinek 	"clear swap",
4810209230bSgjelinek 	"clear locked",
4820209230bSgjelinek 	NULL
4830209230bSgjelinek };
4840209230bSgjelinek 
4857c478bd9Sstevel@tonic-gate /* Global variables */
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
4887c478bd9Sstevel@tonic-gate static char *execname;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate /* set in main(), used all over the place */
4917c478bd9Sstevel@tonic-gate static zone_dochandle_t handle;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /* used all over the place */
494087719fdSdp static char zone[ZONENAME_MAX];
495087719fdSdp static char revert_zone[ZONENAME_MAX];
4967c478bd9Sstevel@tonic-gate 
4979acbbeafSnn35248 /* global brand operations */
498123807fbSedp static brand_handle_t brand;
4999acbbeafSnn35248 
5007c478bd9Sstevel@tonic-gate /* set in modifying functions, checked in read_input() */
501bbec428eSgjelinek static boolean_t need_to_commit = B_FALSE;
502bbec428eSgjelinek boolean_t saw_error;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /* set in yacc parser, checked in read_input() */
505bbec428eSgjelinek boolean_t newline_terminated;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /* set in main(), checked in lex error handler */
508bbec428eSgjelinek boolean_t cmd_file_mode;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate /* set in exit_func(), checked in read_input() */
511bbec428eSgjelinek static boolean_t time_to_exit = B_FALSE, force_exit = B_FALSE;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /* used in short_usage() and zerr() */
5147c478bd9Sstevel@tonic-gate static char *cmd_file_name = NULL;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate /* checked in read_input() and other places */
517bbec428eSgjelinek static boolean_t ok_to_prompt = B_FALSE;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate /* set and checked in initialize() */
520bbec428eSgjelinek static boolean_t got_handle = B_FALSE;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate /* initialized in do_interactive(), checked in initialize() */
523bbec428eSgjelinek static boolean_t interactive_mode;
5247c478bd9Sstevel@tonic-gate 
5250209230bSgjelinek /* set if configuring the global zone */
526bbec428eSgjelinek static boolean_t global_zone = B_FALSE;
5270209230bSgjelinek 
5287c478bd9Sstevel@tonic-gate /* set in main(), checked in multiple places */
529bbec428eSgjelinek static boolean_t read_only_mode;
5307c478bd9Sstevel@tonic-gate 
531bbec428eSgjelinek /* scope is outer/global or inner/resource */
532bbec428eSgjelinek static boolean_t global_scope = B_TRUE;
5337c478bd9Sstevel@tonic-gate static int resource_scope;	/* should be in the RT_ list from zonecfg.h */
5347c478bd9Sstevel@tonic-gate static int end_op = -1;		/* operation on end is either add or modify */
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate int num_prop_vals;		/* for grammar */
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  * These are for keeping track of resources as they are specified as part of
5407c478bd9Sstevel@tonic-gate  * the multi-step process.  They should be initialized by add_resource() or
5417c478bd9Sstevel@tonic-gate  * select_func() and filled in by add_property() or set_func().
5427c478bd9Sstevel@tonic-gate  */
5437c478bd9Sstevel@tonic-gate static struct zone_fstab	old_fstab, in_progress_fstab;
5447c478bd9Sstevel@tonic-gate static struct zone_fstab	old_ipdtab, in_progress_ipdtab;
5457c478bd9Sstevel@tonic-gate static struct zone_nwiftab	old_nwiftab, in_progress_nwiftab;
5467c478bd9Sstevel@tonic-gate static struct zone_devtab	old_devtab, in_progress_devtab;
5477c478bd9Sstevel@tonic-gate static struct zone_rctltab	old_rctltab, in_progress_rctltab;
5487c478bd9Sstevel@tonic-gate static struct zone_attrtab	old_attrtab, in_progress_attrtab;
549fa9e4066Sahrens static struct zone_dstab	old_dstab, in_progress_dstab;
5500209230bSgjelinek static struct zone_psettab	old_psettab, in_progress_psettab;
5510209230bSgjelinek static struct zone_mcaptab	old_mcaptab, in_progress_mcaptab;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate static GetLine *gl;	/* The gl_get_line() resource object */
5547c478bd9Sstevel@tonic-gate 
5550209230bSgjelinek static void bytes_to_units(char *str, char *buf, int bufsize);
5560209230bSgjelinek 
5577c478bd9Sstevel@tonic-gate /* Functions begin here */
5587c478bd9Sstevel@tonic-gate 
559bbec428eSgjelinek static boolean_t
5607c478bd9Sstevel@tonic-gate initial_match(const char *line1, const char *line2, int word_end)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	if (word_end <= 0)
563bbec428eSgjelinek 		return (B_TRUE);
5647c478bd9Sstevel@tonic-gate 	return (strncmp(line1, line2, word_end) == 0);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate static int
5687c478bd9Sstevel@tonic-gate add_stuff(WordCompletion *cpl, const char *line1, const char **list,
5697c478bd9Sstevel@tonic-gate     int word_end)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	int i, err;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	for (i = 0; list[i] != NULL; i++) {
5747c478bd9Sstevel@tonic-gate 		if (initial_match(line1, list[i], word_end)) {
5757c478bd9Sstevel@tonic-gate 			err = cpl_add_completion(cpl, line1, 0, word_end,
5767c478bd9Sstevel@tonic-gate 			    list[i] + word_end, "", "");
5777c478bd9Sstevel@tonic-gate 			if (err != 0)
5787c478bd9Sstevel@tonic-gate 				return (err);
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 	return (0);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate static
5857c478bd9Sstevel@tonic-gate /* ARGSUSED */
5867c478bd9Sstevel@tonic-gate CPL_MATCH_FN(cmd_cpl_fn)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	if (global_scope) {
5897c478bd9Sstevel@tonic-gate 		/*
5907c478bd9Sstevel@tonic-gate 		 * The MAX/MIN tests below are to make sure we have at least
5917c478bd9Sstevel@tonic-gate 		 * enough characters to distinguish from other prefixes (MAX)
5927c478bd9Sstevel@tonic-gate 		 * but only check MIN(what we have, what we're checking).
5937c478bd9Sstevel@tonic-gate 		 */
5947c478bd9Sstevel@tonic-gate 		if (strncmp(line, "add ", MAX(MIN(word_end, 4), 1)) == 0)
5957c478bd9Sstevel@tonic-gate 			return (add_stuff(cpl, line, add_cmds, word_end));
5960209230bSgjelinek 		if (strncmp(line, "clear ", MAX(MIN(word_end, 6), 2)) == 0)
5970209230bSgjelinek 			return (add_stuff(cpl, line, clear_cmds, word_end));
5987c478bd9Sstevel@tonic-gate 		if (strncmp(line, "select ", MAX(MIN(word_end, 7), 3)) == 0)
5997c478bd9Sstevel@tonic-gate 			return (add_stuff(cpl, line, select_cmds, word_end));
6007c478bd9Sstevel@tonic-gate 		if (strncmp(line, "set ", MAX(MIN(word_end, 4), 3)) == 0)
6017c478bd9Sstevel@tonic-gate 			return (add_stuff(cpl, line, set_cmds, word_end));
6029e7542f4Sdp 		if (strncmp(line, "remove ", MAX(MIN(word_end, 7), 1)) == 0)
6039e7542f4Sdp 			return (add_stuff(cpl, line, remove_cmds, word_end));
6049e7542f4Sdp 		if (strncmp(line, "info ", MAX(MIN(word_end, 5), 1)) == 0)
6059e7542f4Sdp 			return (add_stuff(cpl, line, info_cmds, word_end));
6067c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, global_scope_cmds, word_end));
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 	switch (resource_scope) {
6097c478bd9Sstevel@tonic-gate 	case RT_FS:
6107c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, fs_res_scope_cmds, word_end));
6117c478bd9Sstevel@tonic-gate 	case RT_IPD:
6127c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, ipd_res_scope_cmds, word_end));
6137c478bd9Sstevel@tonic-gate 	case RT_NET:
6147c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, net_res_scope_cmds, word_end));
6157c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
6167c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, device_res_scope_cmds, word_end));
6177c478bd9Sstevel@tonic-gate 	case RT_RCTL:
6187c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, rctl_res_scope_cmds, word_end));
6197c478bd9Sstevel@tonic-gate 	case RT_ATTR:
6207c478bd9Sstevel@tonic-gate 		return (add_stuff(cpl, line, attr_res_scope_cmds, word_end));
621fa9e4066Sahrens 	case RT_DATASET:
622fa9e4066Sahrens 		return (add_stuff(cpl, line, dataset_res_scope_cmds, word_end));
6230209230bSgjelinek 	case RT_DCPU:
6240209230bSgjelinek 		return (add_stuff(cpl, line, pset_res_scope_cmds, word_end));
625c97ad5cdSakolb 	case RT_PCAP:
626c97ad5cdSakolb 		return (add_stuff(cpl, line, pcap_res_scope_cmds, word_end));
6270209230bSgjelinek 	case RT_MCAP:
6280209230bSgjelinek 		return (add_stuff(cpl, line, mcap_res_scope_cmds, word_end));
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 	return (0);
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate /*
6347c478bd9Sstevel@tonic-gate  * For the main CMD_func() functions below, several of them call getopt()
6357c478bd9Sstevel@tonic-gate  * then check optind against argc to make sure an extra parameter was not
6367c478bd9Sstevel@tonic-gate  * passed in.  The reason this is not caught in the grammar is that the
6377c478bd9Sstevel@tonic-gate  * grammar just checks for a miscellaneous TOKEN, which is *expected* to
6387c478bd9Sstevel@tonic-gate  * be "-F" (for example), but could be anything.  So (for example) this
6397c478bd9Sstevel@tonic-gate  * check will prevent "create bogus".
6407c478bd9Sstevel@tonic-gate  */
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate cmd_t *
6437c478bd9Sstevel@tonic-gate alloc_cmd(void)
6447c478bd9Sstevel@tonic-gate {
6457c478bd9Sstevel@tonic-gate 	return (calloc(1, sizeof (cmd_t)));
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate void
6497c478bd9Sstevel@tonic-gate free_cmd(cmd_t *cmd)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	int i;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	for (i = 0; i < MAX_EQ_PROP_PAIRS; i++)
6547c478bd9Sstevel@tonic-gate 		if (cmd->cmd_property_ptr[i] != NULL) {
6557c478bd9Sstevel@tonic-gate 			property_value_ptr_t pp = cmd->cmd_property_ptr[i];
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 			switch (pp->pv_type) {
6587c478bd9Sstevel@tonic-gate 			case PROP_VAL_SIMPLE:
6597c478bd9Sstevel@tonic-gate 				free(pp->pv_simple);
6607c478bd9Sstevel@tonic-gate 				break;
6617c478bd9Sstevel@tonic-gate 			case PROP_VAL_COMPLEX:
6627c478bd9Sstevel@tonic-gate 				free_complex(pp->pv_complex);
6637c478bd9Sstevel@tonic-gate 				break;
6647c478bd9Sstevel@tonic-gate 			case PROP_VAL_LIST:
6657c478bd9Sstevel@tonic-gate 				free_list(pp->pv_list);
6667c478bd9Sstevel@tonic-gate 				break;
6677c478bd9Sstevel@tonic-gate 			}
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_argc; i++)
6707c478bd9Sstevel@tonic-gate 		free(cmd->cmd_argv[i]);
6717c478bd9Sstevel@tonic-gate 	free(cmd);
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate complex_property_ptr_t
6757c478bd9Sstevel@tonic-gate alloc_complex(void)
6767c478bd9Sstevel@tonic-gate {
6777c478bd9Sstevel@tonic-gate 	return (calloc(1, sizeof (complex_property_t)));
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate void
6817c478bd9Sstevel@tonic-gate free_complex(complex_property_ptr_t complex)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	if (complex == NULL)
6847c478bd9Sstevel@tonic-gate 		return;
6857c478bd9Sstevel@tonic-gate 	free_complex(complex->cp_next);
6867c478bd9Sstevel@tonic-gate 	if (complex->cp_value != NULL)
6877c478bd9Sstevel@tonic-gate 		free(complex->cp_value);
6887c478bd9Sstevel@tonic-gate 	free(complex);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate list_property_ptr_t
6927c478bd9Sstevel@tonic-gate alloc_list(void)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	return (calloc(1, sizeof (list_property_t)));
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate void
6987c478bd9Sstevel@tonic-gate free_list(list_property_ptr_t list)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	if (list == NULL)
7017c478bd9Sstevel@tonic-gate 		return;
7027c478bd9Sstevel@tonic-gate 	if (list->lp_simple != NULL)
7037c478bd9Sstevel@tonic-gate 		free(list->lp_simple);
7047c478bd9Sstevel@tonic-gate 	free_complex(list->lp_complex);
7057c478bd9Sstevel@tonic-gate 	free_list(list->lp_next);
7067c478bd9Sstevel@tonic-gate 	free(list);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate void
7107c478bd9Sstevel@tonic-gate free_outer_list(list_property_ptr_t list)
7117c478bd9Sstevel@tonic-gate {
7127c478bd9Sstevel@tonic-gate 	if (list == NULL)
7137c478bd9Sstevel@tonic-gate 		return;
7147c478bd9Sstevel@tonic-gate 	free_outer_list(list->lp_next);
7157c478bd9Sstevel@tonic-gate 	free(list);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate static struct zone_rctlvaltab *
7197c478bd9Sstevel@tonic-gate alloc_rctlvaltab(void)
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate 	return (calloc(1, sizeof (struct zone_rctlvaltab)));
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate static char *
7257c478bd9Sstevel@tonic-gate rt_to_str(int res_type)
7267c478bd9Sstevel@tonic-gate {
7277c478bd9Sstevel@tonic-gate 	assert(res_type >= RT_MIN && res_type <= RT_MAX);
7287c478bd9Sstevel@tonic-gate 	return (res_types[res_type]);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate static char *
7327c478bd9Sstevel@tonic-gate pt_to_str(int prop_type)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	assert(prop_type >= PT_MIN && prop_type <= PT_MAX);
7357c478bd9Sstevel@tonic-gate 	return (prop_types[prop_type]);
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate static char *
7397c478bd9Sstevel@tonic-gate pvt_to_str(int pv_type)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	assert(pv_type >= PROP_VAL_MIN && pv_type <= PROP_VAL_MAX);
7427c478bd9Sstevel@tonic-gate 	return (prop_val_types[pv_type]);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate static char *
7467c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
7497c478bd9Sstevel@tonic-gate 	return (helptab[cmd_num].cmd_name);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * This is a separate function rather than a set of define's because of the
7547c478bd9Sstevel@tonic-gate  * gettext() wrapping.
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate /*
7587c478bd9Sstevel@tonic-gate  * TRANSLATION_NOTE
7597c478bd9Sstevel@tonic-gate  * Each string below should have \t follow \n whenever needed; the
7607c478bd9Sstevel@tonic-gate  * initial \t and the terminal \n will be provided by the calling function.
7617c478bd9Sstevel@tonic-gate  */
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate static char *
7647c478bd9Sstevel@tonic-gate long_help(int cmd_num)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	static char line[1024];	/* arbitrary large amount */
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
7697c478bd9Sstevel@tonic-gate 	switch (cmd_num) {
7707c478bd9Sstevel@tonic-gate 		case CMD_HELP:
7717c478bd9Sstevel@tonic-gate 			return (gettext("Prints help message."));
7727c478bd9Sstevel@tonic-gate 		case CMD_CREATE:
7737c478bd9Sstevel@tonic-gate 			(void) snprintf(line, sizeof (line),
7747c478bd9Sstevel@tonic-gate 			    gettext("Creates a configuration for the "
7757c478bd9Sstevel@tonic-gate 			    "specified zone.  %s should be\n\tused to "
7767c478bd9Sstevel@tonic-gate 			    "begin configuring a new zone.  If overwriting an "
7777c478bd9Sstevel@tonic-gate 			    "existing\n\tconfiguration, the -F flag can be "
7787c478bd9Sstevel@tonic-gate 			    "used to force the action.  If\n\t-t template is "
7797c478bd9Sstevel@tonic-gate 			    "given, creates a configuration identical to the\n"
7807c478bd9Sstevel@tonic-gate 			    "\tspecified template, except that the zone name "
7819e518655Sgjelinek 			    "is changed from\n\ttemplate to zonename.  '%s -a' "
7829e518655Sgjelinek 			    "creates a configuration from a\n\tdetached "
7839e518655Sgjelinek 			    "zonepath.  '%s -b' results in a blank "
7849e518655Sgjelinek 			    "configuration.\n\t'%s' with no arguments applies "
7859e518655Sgjelinek 			    "the Sun default settings."),
7867c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_CREATE), cmd_to_str(CMD_CREATE),
7879e518655Sgjelinek 			    cmd_to_str(CMD_CREATE), cmd_to_str(CMD_CREATE));
7887c478bd9Sstevel@tonic-gate 			return (line);
7897c478bd9Sstevel@tonic-gate 		case CMD_EXIT:
7907c478bd9Sstevel@tonic-gate 			return (gettext("Exits the program.  The -F flag can "
7917c478bd9Sstevel@tonic-gate 			    "be used to force the action."));
7927c478bd9Sstevel@tonic-gate 		case CMD_EXPORT:
7937c478bd9Sstevel@tonic-gate 			return (gettext("Prints configuration to standard "
7947c478bd9Sstevel@tonic-gate 			    "output, or to output-file if\n\tspecified, in "
7957c478bd9Sstevel@tonic-gate 			    "a form suitable for use in a command-file."));
7967c478bd9Sstevel@tonic-gate 		case CMD_ADD:
7977c478bd9Sstevel@tonic-gate 			return (gettext("Add specified resource to "
7987c478bd9Sstevel@tonic-gate 			    "configuration."));
7997c478bd9Sstevel@tonic-gate 		case CMD_DELETE:
8007c478bd9Sstevel@tonic-gate 			return (gettext("Deletes the specified zone.  The -F "
8017c478bd9Sstevel@tonic-gate 			    "flag can be used to force the\n\taction."));
8027c478bd9Sstevel@tonic-gate 		case CMD_REMOVE:
8037c478bd9Sstevel@tonic-gate 			return (gettext("Remove specified resource from "
8040209230bSgjelinek 			    "configuration.  The -F flag can be used\n\tto "
8050209230bSgjelinek 			    "force the action."));
8067c478bd9Sstevel@tonic-gate 		case CMD_SELECT:
8077c478bd9Sstevel@tonic-gate 			(void) snprintf(line, sizeof (line),
8087c478bd9Sstevel@tonic-gate 			    gettext("Selects a resource to modify.  "
8097c478bd9Sstevel@tonic-gate 			    "Resource modification is completed\n\twith the "
8107c478bd9Sstevel@tonic-gate 			    "command \"%s\".  The property name/value pairs "
8117c478bd9Sstevel@tonic-gate 			    "must uniquely\n\tidentify a resource.  Note that "
8127c478bd9Sstevel@tonic-gate 			    "the curly braces ('{', '}') mean one\n\tor more "
8137c478bd9Sstevel@tonic-gate 			    "of whatever is between them."),
8147c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_END));
8157c478bd9Sstevel@tonic-gate 			return (line);
8167c478bd9Sstevel@tonic-gate 		case CMD_SET:
8177c478bd9Sstevel@tonic-gate 			return (gettext("Sets property values."));
8180209230bSgjelinek 		case CMD_CLEAR:
8190209230bSgjelinek 			return (gettext("Clears property values."));
8207c478bd9Sstevel@tonic-gate 		case CMD_INFO:
8217c478bd9Sstevel@tonic-gate 			return (gettext("Displays information about the "
8227c478bd9Sstevel@tonic-gate 			    "current configuration.  If resource\n\ttype is "
8237c478bd9Sstevel@tonic-gate 			    "specified, displays only information about "
8247c478bd9Sstevel@tonic-gate 			    "resources of\n\tthe relevant type.  If resource "
8257c478bd9Sstevel@tonic-gate 			    "id is specified, displays only\n\tinformation "
8267c478bd9Sstevel@tonic-gate 			    "about that resource."));
8277c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
8287c478bd9Sstevel@tonic-gate 			return (gettext("Verifies current configuration "
8297c478bd9Sstevel@tonic-gate 			    "for correctness (some resource types\n\thave "
8307c478bd9Sstevel@tonic-gate 			    "required properties)."));
8317c478bd9Sstevel@tonic-gate 		case CMD_COMMIT:
8327c478bd9Sstevel@tonic-gate 			(void) snprintf(line, sizeof (line),
8337c478bd9Sstevel@tonic-gate 			    gettext("Commits current configuration.  "
8347c478bd9Sstevel@tonic-gate 			    "Configuration must be committed to\n\tbe used by "
8357c478bd9Sstevel@tonic-gate 			    "%s.  Until the configuration is committed, "
8367c478bd9Sstevel@tonic-gate 			    "changes \n\tcan be removed with the %s "
8377c478bd9Sstevel@tonic-gate 			    "command.  This operation is\n\tattempted "
8387c478bd9Sstevel@tonic-gate 			    "automatically upon completion of a %s "
8397c478bd9Sstevel@tonic-gate 			    "session."), "zoneadm", cmd_to_str(CMD_REVERT),
8407c478bd9Sstevel@tonic-gate 			    "zonecfg");
8417c478bd9Sstevel@tonic-gate 			return (line);
8427c478bd9Sstevel@tonic-gate 		case CMD_REVERT:
8437c478bd9Sstevel@tonic-gate 			return (gettext("Reverts configuration back to the "
8447c478bd9Sstevel@tonic-gate 			    "last committed state.  The -F flag\n\tcan be "
8457c478bd9Sstevel@tonic-gate 			    "used to force the action."));
8467c478bd9Sstevel@tonic-gate 		case CMD_CANCEL:
8477c478bd9Sstevel@tonic-gate 			return (gettext("Cancels resource/property "
8487c478bd9Sstevel@tonic-gate 			    "specification."));
8497c478bd9Sstevel@tonic-gate 		case CMD_END:
8507c478bd9Sstevel@tonic-gate 			return (gettext("Ends resource/property "
8517c478bd9Sstevel@tonic-gate 			    "specification."));
8527c478bd9Sstevel@tonic-gate 	}
8537c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8547e362f58Scomay 	return (NULL);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate /*
8587c478bd9Sstevel@tonic-gate  * Called with verbose TRUE when help is explicitly requested, FALSE for
8597c478bd9Sstevel@tonic-gate  * unexpected errors.
8607c478bd9Sstevel@tonic-gate  */
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate void
863bbec428eSgjelinek usage(boolean_t verbose, uint_t flags)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate 	FILE *fp = verbose ? stdout : stderr, *newfp;
866bbec428eSgjelinek 	boolean_t need_to_close = B_FALSE;
8677c478bd9Sstevel@tonic-gate 	char *pager;
8687c478bd9Sstevel@tonic-gate 	int i;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	/* don't page error output */
8717c478bd9Sstevel@tonic-gate 	if (verbose && interactive_mode) {
8727c478bd9Sstevel@tonic-gate 		if ((pager = getenv("PAGER")) == NULL)
8737c478bd9Sstevel@tonic-gate 			pager = PAGER;
8747c478bd9Sstevel@tonic-gate 		if ((newfp = popen(pager, "w")) != NULL) {
875bbec428eSgjelinek 			need_to_close = B_TRUE;
8767c478bd9Sstevel@tonic-gate 			fp = newfp;
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 	if (flags & HELP_META) {
8807c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("More help is available for the "
8817c478bd9Sstevel@tonic-gate 		    "following:\n"));
8827c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\n\tcommands ('%s commands')\n",
8837c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_HELP));
8847c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\tsyntax ('%s syntax')\n",
8857c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_HELP));
8867c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\tusage ('%s usage')\n\n",
8877c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_HELP));
8887c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("You may also obtain help on any "
8897c478bd9Sstevel@tonic-gate 		    "command by typing '%s <command-name>.'\n"),
8907c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_HELP));
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 	if (flags & HELP_RES_SCOPE) {
8937c478bd9Sstevel@tonic-gate 		switch (resource_scope) {
8947c478bd9Sstevel@tonic-gate 		case RT_FS:
8957c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
8967c478bd9Sstevel@tonic-gate 			    "used to configure a file-system.\n"),
8977c478bd9Sstevel@tonic-gate 			    rt_to_str(resource_scope));
8987c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
8997c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9007c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_DIR), gettext("<path>"));
9017c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9027c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_SPECIAL), gettext("<path>"));
9037c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9047c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_RAW), gettext("<raw-device>"));
9057c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9067c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_TYPE), gettext("<file-system type>"));
9077c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s %s\n", cmd_to_str(CMD_ADD),
9087c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_OPTIONS),
9097c478bd9Sstevel@tonic-gate 			    gettext("<file-system options>"));
910ffbafc53Scomay 			(void) fprintf(fp, "\t%s %s %s\n",
911ffbafc53Scomay 			    cmd_to_str(CMD_REMOVE), pt_to_str(PT_OPTIONS),
912ffbafc53Scomay 			    gettext("<file-system options>"));
9137c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Consult the file-system "
9147c478bd9Sstevel@tonic-gate 			    "specific manual page, such as mount_ufs(1M), "
9157c478bd9Sstevel@tonic-gate 			    "for\ndetails about file-system options.  Note "
9167c478bd9Sstevel@tonic-gate 			    "that any file-system options with an\nembedded "
9177c478bd9Sstevel@tonic-gate 			    "'=' character must be enclosed in double quotes, "
9187c478bd9Sstevel@tonic-gate 			    /*CSTYLED*/
9197c478bd9Sstevel@tonic-gate 			    "such as \"%s=5\".\n"), MNTOPT_RETRY);
9207c478bd9Sstevel@tonic-gate 			break;
9217c478bd9Sstevel@tonic-gate 		case RT_IPD:
9227c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
9237c478bd9Sstevel@tonic-gate 			    "used to configure a directory\ninherited from the "
9247c478bd9Sstevel@tonic-gate 			    "global zone into a non-global zone in read-only "
9257c478bd9Sstevel@tonic-gate 			    "mode.\n"), rt_to_str(resource_scope));
9267c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
9277c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9287c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_DIR), gettext("<path>"));
9297c478bd9Sstevel@tonic-gate 			break;
9307c478bd9Sstevel@tonic-gate 		case RT_NET:
9317c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
9327c478bd9Sstevel@tonic-gate 			    "used to configure a network interface.\n"),
9337c478bd9Sstevel@tonic-gate 			    rt_to_str(resource_scope));
9347c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
9357c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9367c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_ADDRESS), gettext("<IP-address>"));
9377c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9387c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_PHYSICAL), gettext("<interface>"));
9397c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("See ifconfig(1M) for "
9407c478bd9Sstevel@tonic-gate 			    "details of the <interface> string.\n"));
941de860bd9Sgfaden 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
942de860bd9Sgfaden 			    pt_to_str(PT_DEFROUTER), gettext("<IP-address>"));
943de860bd9Sgfaden 			(void) fprintf(fp, gettext("%s %s and %s %s are valid "
944de860bd9Sgfaden 			    "if the %s property is set to %s, otherwise they "
945de860bd9Sgfaden 			    "must not be set.\n"),
946f4b3ec61Sdh155122 			    cmd_to_str(CMD_SET), pt_to_str(PT_ADDRESS),
947de860bd9Sgfaden 			    cmd_to_str(CMD_SET), pt_to_str(PT_DEFROUTER),
948f4b3ec61Sdh155122 			    pt_to_str(PT_IPTYPE), "shared");
9497c478bd9Sstevel@tonic-gate 			break;
9507c478bd9Sstevel@tonic-gate 		case RT_DEVICE:
9517c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
9527c478bd9Sstevel@tonic-gate 			    "used to configure a device node.\n"),
9537c478bd9Sstevel@tonic-gate 			    rt_to_str(resource_scope));
9547c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
9557c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9567c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_MATCH), gettext("<device-path>"));
9577c478bd9Sstevel@tonic-gate 			break;
9587c478bd9Sstevel@tonic-gate 		case RT_RCTL:
9597c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
9607c478bd9Sstevel@tonic-gate 			    "used to configure a resource control.\n"),
9617c478bd9Sstevel@tonic-gate 			    rt_to_str(resource_scope));
9627c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
9637c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9647c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_NAME), gettext("<string>"));
9657c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s (%s=%s,%s=%s,%s=%s)\n",
9667c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_ADD), pt_to_str(PT_VALUE),
9677c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_PRIV), gettext("<priv-value>"),
9687c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_LIMIT), gettext("<number>"),
9697c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_ACTION), gettext("<action-value>"));
970ffbafc53Scomay 			(void) fprintf(fp, "\t%s %s (%s=%s,%s=%s,%s=%s)\n",
971ffbafc53Scomay 			    cmd_to_str(CMD_REMOVE), pt_to_str(PT_VALUE),
972ffbafc53Scomay 			    pt_to_str(PT_PRIV), gettext("<priv-value>"),
973ffbafc53Scomay 			    pt_to_str(PT_LIMIT), gettext("<number>"),
974ffbafc53Scomay 			    pt_to_str(PT_ACTION), gettext("<action-value>"));
9757c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%s\n\t%s := privileged\n"
9767c478bd9Sstevel@tonic-gate 			    "\t%s := none | deny\n", gettext("Where"),
9777c478bd9Sstevel@tonic-gate 			    gettext("<priv-value>"), gettext("<action-value>"));
9787c478bd9Sstevel@tonic-gate 			break;
9797c478bd9Sstevel@tonic-gate 		case RT_ATTR:
9807c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("The '%s' resource scope is "
9817c478bd9Sstevel@tonic-gate 			    "used to configure a generic attribute.\n"),
9827c478bd9Sstevel@tonic-gate 			    rt_to_str(resource_scope));
9837c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("Valid commands:\n"));
9847c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9857c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_NAME), gettext("<name>"));
9867c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=boolean\n",
9877c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_SET), pt_to_str(PT_TYPE));
9887c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=true | false\n",
9897c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_SET), pt_to_str(PT_VALUE));
9907c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("or\n"));
9917c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=int\n", cmd_to_str(CMD_SET),
9927c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_TYPE));
9937c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9947c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_VALUE), gettext("<integer>"));
9957c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("or\n"));
9967c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=string\n",
9977c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_SET), pt_to_str(PT_TYPE));
9987c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
9997c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_VALUE), gettext("<string>"));
10007c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, gettext("or\n"));
10017c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=uint\n",
10027c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_SET), pt_to_str(PT_TYPE));
10037c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10047c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_VALUE), gettext("<unsigned integer>"));
10057c478bd9Sstevel@tonic-gate 			break;
1006fa9e4066Sahrens 		case RT_DATASET:
1007fa9e4066Sahrens 			(void) fprintf(fp, gettext("The '%s' resource scope is "
1008fa9e4066Sahrens 			    "used to export ZFS datasets.\n"),
1009fa9e4066Sahrens 			    rt_to_str(resource_scope));
1010fa9e4066Sahrens 			(void) fprintf(fp, gettext("Valid commands:\n"));
1011fa9e4066Sahrens 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
1012fa9e4066Sahrens 			    pt_to_str(PT_NAME), gettext("<name>"));
1013fa9e4066Sahrens 			break;
10140209230bSgjelinek 		case RT_DCPU:
10150209230bSgjelinek 			(void) fprintf(fp, gettext("The '%s' resource scope "
10160209230bSgjelinek 			    "configures the 'pools' facility to dedicate\na "
10170209230bSgjelinek 			    "subset of the system's processors to this zone "
10180209230bSgjelinek 			    "while it is running.\n"),
10190209230bSgjelinek 			    rt_to_str(resource_scope));
10200209230bSgjelinek 			(void) fprintf(fp, gettext("Valid commands:\n"));
10210209230bSgjelinek 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10220209230bSgjelinek 			    pt_to_str(PT_NCPUS),
10230209230bSgjelinek 			    gettext("<unsigned integer | range>"));
10240209230bSgjelinek 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10250209230bSgjelinek 			    pt_to_str(PT_IMPORTANCE),
10260209230bSgjelinek 			    gettext("<unsigned integer>"));
10270209230bSgjelinek 			break;
1028c97ad5cdSakolb 		case RT_PCAP:
1029c97ad5cdSakolb 			(void) fprintf(fp, gettext("The '%s' resource scope is "
1030c97ad5cdSakolb 			    "used to set an upper limit (a cap) on the\n"
1031c97ad5cdSakolb 			    "percentage of CPU that can be used by this zone.  "
1032c97ad5cdSakolb 			    "A '%s' value of 1\ncorresponds to one cpu.  The "
1033c97ad5cdSakolb 			    "value can be set higher than 1, up to the total\n"
1034c97ad5cdSakolb 			    "number of CPUs on the system.  The value can "
1035c97ad5cdSakolb 			    "also be less than 1,\nrepresenting a fraction of "
1036c97ad5cdSakolb 			    "a cpu.\n"),
1037c97ad5cdSakolb 			    rt_to_str(resource_scope), pt_to_str(PT_NCPUS));
1038c97ad5cdSakolb 			(void) fprintf(fp, gettext("Valid commands:\n"));
1039c97ad5cdSakolb 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
1040c97ad5cdSakolb 			    pt_to_str(PT_NCPUS), gettext("<unsigned decimal>"));
1041c97ad5cdSakolb 			break;
10420209230bSgjelinek 		case RT_MCAP:
10430209230bSgjelinek 			(void) fprintf(fp, gettext("The '%s' resource scope is "
10440209230bSgjelinek 			    "used to set an upper limit (a cap) on the\n"
10450209230bSgjelinek 			    "amount of physical memory, swap space and locked "
10460209230bSgjelinek 			    "memory that can be used by\nthis zone.\n"),
10470209230bSgjelinek 			    rt_to_str(resource_scope));
10480209230bSgjelinek 			(void) fprintf(fp, gettext("Valid commands:\n"));
10490209230bSgjelinek 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10500209230bSgjelinek 			    pt_to_str(PT_PHYSICAL),
10510209230bSgjelinek 			    gettext("<qualified unsigned decimal>"));
10520209230bSgjelinek 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10530209230bSgjelinek 			    pt_to_str(PT_SWAP),
10540209230bSgjelinek 			    gettext("<qualified unsigned decimal>"));
10550209230bSgjelinek 			(void) fprintf(fp, "\t%s %s=%s\n", cmd_to_str(CMD_SET),
10560209230bSgjelinek 			    pt_to_str(PT_LOCKED),
10570209230bSgjelinek 			    gettext("<qualified unsigned decimal>"));
10580209230bSgjelinek 			break;
10597c478bd9Sstevel@tonic-gate 		}
10607c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("And from any resource scope, you "
10617c478bd9Sstevel@tonic-gate 		    "can:\n"));
10627c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", cmd_to_str(CMD_END),
10637c478bd9Sstevel@tonic-gate 		    gettext("(to conclude this operation)"));
10647c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", cmd_to_str(CMD_CANCEL),
10657c478bd9Sstevel@tonic-gate 		    gettext("(to cancel this operation)"));
10667c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", cmd_to_str(CMD_EXIT),
10677c478bd9Sstevel@tonic-gate 		    gettext("(to exit the zonecfg utility)"));
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 	if (flags & HELP_USAGE) {
10707c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s:\t%s %s\n", gettext("usage"),
10717c478bd9Sstevel@tonic-gate 		    execname, cmd_to_str(CMD_HELP));
10727c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s -z <zone>\t\t\t(%s)\n",
10737c478bd9Sstevel@tonic-gate 		    execname, gettext("interactive"));
10747c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s -z <zone> <command>\n", execname);
10757c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s -z <zone> -f <command-file>\n",
10767c478bd9Sstevel@tonic-gate 		    execname);
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 	if (flags & HELP_SUBCMDS) {
10797c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s:\n\n", gettext("Commands"));
10807c478bd9Sstevel@tonic-gate 		for (i = 0; i <= CMD_MAX; i++) {
10817c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%s\n", helptab[i].short_usage);
10827c478bd9Sstevel@tonic-gate 			if (verbose)
10837c478bd9Sstevel@tonic-gate 				(void) fprintf(fp, "\t%s\n\n", long_help(i));
10847c478bd9Sstevel@tonic-gate 		}
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 	if (flags & HELP_SYNTAX) {
10877c478bd9Sstevel@tonic-gate 		if (!verbose)
10887c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\n");
10897c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "<zone> := [A-Za-z0-9][A-Za-z0-9_.-]*\n");
10907c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("\t(except the reserved words "
10917c478bd9Sstevel@tonic-gate 		    "'%s' and anything starting with '%s')\n"), "global",
10927c478bd9Sstevel@tonic-gate 		    "SUNW");
10937c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
10947c478bd9Sstevel@tonic-gate 		    gettext("\tName must be less than %d characters.\n"),
10957c478bd9Sstevel@tonic-gate 		    ZONENAME_MAX);
10967c478bd9Sstevel@tonic-gate 		if (verbose)
10977c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\n");
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate 	if (flags & HELP_NETADDR) {
11007c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("\n<net-addr> :="));
11017c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11027c478bd9Sstevel@tonic-gate 		    gettext("\t<IPv4-address>[/<IPv4-prefix-length>] |\n"));
11037c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11047c478bd9Sstevel@tonic-gate 		    gettext("\t\t<IPv6-address>/<IPv6-prefix-length> |\n"));
11057c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11067c478bd9Sstevel@tonic-gate 		    gettext("\t\t<hostname>[/<IPv4-prefix-length>]\n"));
11077c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("See inet(3SOCKET) for IPv4 and "
11087c478bd9Sstevel@tonic-gate 		    "IPv6 address syntax.\n"));
11097c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("<IPv4-prefix-length> := [0-32]\n"));
11107c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11117c478bd9Sstevel@tonic-gate 		    gettext("<IPv6-prefix-length> := [0-128]\n"));
11127c478bd9Sstevel@tonic-gate 		(void) fprintf(fp,
11137c478bd9Sstevel@tonic-gate 		    gettext("<hostname> := [A-Za-z0-9][A-Za-z0-9-.]*\n"));
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 	if (flags & HELP_RESOURCES) {
11169e7542f4Sdp 		(void) fprintf(fp, "<%s> := %s | %s | %s | %s | %s | %s |\n\t"
1117c97ad5cdSakolb 		    "%s | %s | %s | %s\n\n",
11187c478bd9Sstevel@tonic-gate 		    gettext("resource type"), rt_to_str(RT_FS),
11197c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_IPD), rt_to_str(RT_NET), rt_to_str(RT_DEVICE),
11209e7542f4Sdp 		    rt_to_str(RT_RCTL), rt_to_str(RT_ATTR),
11210209230bSgjelinek 		    rt_to_str(RT_DATASET), rt_to_str(RT_DCPU),
1122c97ad5cdSakolb 		    rt_to_str(RT_PCAP), rt_to_str(RT_MCAP));
11237c478bd9Sstevel@tonic-gate 	}
11247c478bd9Sstevel@tonic-gate 	if (flags & HELP_PROPS) {
11257c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, gettext("For resource type ... there are "
11267c478bd9Sstevel@tonic-gate 		    "property types ...:\n"));
11277c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
1128087719fdSdp 		    pt_to_str(PT_ZONENAME));
1129087719fdSdp 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11307c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_ZONEPATH));
11317c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11329acbbeafSnn35248 		    pt_to_str(PT_BRAND));
11339acbbeafSnn35248 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11347c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_AUTOBOOT));
11357c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11363f2f09c1Sdp 		    pt_to_str(PT_BOOTARGS));
11373f2f09c1Sdp 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11387c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_POOL));
1139ffbafc53Scomay 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
1140ffbafc53Scomay 		    pt_to_str(PT_LIMITPRIV));
11410209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11420209230bSgjelinek 		    pt_to_str(PT_SCHED));
11430209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
1144f4b3ec61Sdh155122 		    pt_to_str(PT_IPTYPE));
1145f4b3ec61Sdh155122 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11460209230bSgjelinek 		    pt_to_str(PT_MAXLWPS));
11470209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11480209230bSgjelinek 		    pt_to_str(PT_MAXSHMMEM));
11490209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11500209230bSgjelinek 		    pt_to_str(PT_MAXSHMIDS));
11510209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11520209230bSgjelinek 		    pt_to_str(PT_MAXMSGIDS));
11530209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11540209230bSgjelinek 		    pt_to_str(PT_MAXSEMIDS));
11550209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s\n", gettext("(global)"),
11560209230bSgjelinek 		    pt_to_str(PT_SHARES));
11577c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t\t%s, %s, %s, %s\n", rt_to_str(RT_FS),
11587c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_DIR), pt_to_str(PT_SPECIAL),
11597c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_RAW), pt_to_str(PT_TYPE),
11607c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_OPTIONS));
11617c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t%s\n", rt_to_str(RT_IPD),
11627c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_DIR));
1163de860bd9Sgfaden 		(void) fprintf(fp, "\t%s\t\t%s, %s, %s\n", rt_to_str(RT_NET),
1164de860bd9Sgfaden 		    pt_to_str(PT_ADDRESS), pt_to_str(PT_PHYSICAL),
1165de860bd9Sgfaden 		    pt_to_str(PT_DEFROUTER));
11667c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t\t%s\n", rt_to_str(RT_DEVICE),
11677c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_MATCH));
11687c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t\t%s, %s\n", rt_to_str(RT_RCTL),
11697c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_NAME), pt_to_str(PT_VALUE));
11707c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "\t%s\t\t%s, %s, %s\n", rt_to_str(RT_ATTR),
11717c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_NAME), pt_to_str(PT_TYPE),
11727c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_VALUE));
1173fa9e4066Sahrens 		(void) fprintf(fp, "\t%s\t\t%s\n", rt_to_str(RT_DATASET),
1174fa9e4066Sahrens 		    pt_to_str(PT_NAME));
11750209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s, %s\n", rt_to_str(RT_DCPU),
11760209230bSgjelinek 		    pt_to_str(PT_NCPUS), pt_to_str(PT_IMPORTANCE));
1177c97ad5cdSakolb 		(void) fprintf(fp, "\t%s\t%s\n", rt_to_str(RT_PCAP),
1178c97ad5cdSakolb 		    pt_to_str(PT_NCPUS));
11790209230bSgjelinek 		(void) fprintf(fp, "\t%s\t%s, %s, %s\n", rt_to_str(RT_MCAP),
11800209230bSgjelinek 		    pt_to_str(PT_PHYSICAL), pt_to_str(PT_SWAP),
11810209230bSgjelinek 		    pt_to_str(PT_LOCKED));
11827c478bd9Sstevel@tonic-gate 	}
11837c478bd9Sstevel@tonic-gate 	if (need_to_close)
11847c478bd9Sstevel@tonic-gate 		(void) pclose(fp);
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
11887c478bd9Sstevel@tonic-gate static void
11897c478bd9Sstevel@tonic-gate zerr(const char *fmt, ...)
11907c478bd9Sstevel@tonic-gate {
11917c478bd9Sstevel@tonic-gate 	va_list alist;
11927c478bd9Sstevel@tonic-gate 	static int last_lineno;
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	/* lex_lineno has already been incremented in the lexer; compensate */
11957c478bd9Sstevel@tonic-gate 	if (cmd_file_mode && lex_lineno > last_lineno) {
11967c478bd9Sstevel@tonic-gate 		if (strcmp(cmd_file_name, "-") == 0)
11977c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("On line %d:\n"),
11987c478bd9Sstevel@tonic-gate 			    lex_lineno - 1);
11997c478bd9Sstevel@tonic-gate 		else
12007c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("On line %d of %s:\n"),
12017c478bd9Sstevel@tonic-gate 			    lex_lineno - 1, cmd_file_name);
12027c478bd9Sstevel@tonic-gate 		last_lineno = lex_lineno;
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
12057c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
12067c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
12077c478bd9Sstevel@tonic-gate 	va_end(alist);
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate static void
1211bbec428eSgjelinek zone_perror(char *prefix, int err, boolean_t set_saw)
12127c478bd9Sstevel@tonic-gate {
12137c478bd9Sstevel@tonic-gate 	zerr("%s: %s", prefix, zonecfg_strerror(err));
12147c478bd9Sstevel@tonic-gate 	if (set_saw)
1215bbec428eSgjelinek 		saw_error = B_TRUE;
12167c478bd9Sstevel@tonic-gate }
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate /*
12197c478bd9Sstevel@tonic-gate  * zone_perror() expects a single string, but for remove and select
12207c478bd9Sstevel@tonic-gate  * we have both the command and the resource type, so this wrapper
12217c478bd9Sstevel@tonic-gate  * function serves the same purpose in a slightly different way.
12227c478bd9Sstevel@tonic-gate  */
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate static void
1225bbec428eSgjelinek z_cmd_rt_perror(int cmd_num, int res_num, int err, boolean_t set_saw)
12267c478bd9Sstevel@tonic-gate {
12277c478bd9Sstevel@tonic-gate 	zerr("%s %s: %s", cmd_to_str(cmd_num), rt_to_str(res_num),
12287c478bd9Sstevel@tonic-gate 	    zonecfg_strerror(err));
12297c478bd9Sstevel@tonic-gate 	if (set_saw)
1230bbec428eSgjelinek 		saw_error = B_TRUE;
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate /* returns Z_OK if successful, Z_foo from <libzonecfg.h> otherwise */
12347c478bd9Sstevel@tonic-gate static int
1235bbec428eSgjelinek initialize(boolean_t handle_expected)
12367c478bd9Sstevel@tonic-gate {
12377c478bd9Sstevel@tonic-gate 	int err;
12389acbbeafSnn35248 	char brandname[MAXNAMELEN];
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) != Z_OK) {
12417c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_get_handle(zone, handle)) == Z_OK) {
1242bbec428eSgjelinek 			got_handle = B_TRUE;
12439acbbeafSnn35248 			if (zonecfg_get_brand(handle, brandname,
12449acbbeafSnn35248 			    sizeof (brandname)) != Z_OK) {
12459acbbeafSnn35248 				zerr("Zone %s is inconsistent: missing "
12469acbbeafSnn35248 				    "brand attribute", zone);
12479acbbeafSnn35248 				exit(Z_ERR);
12489acbbeafSnn35248 			}
12499acbbeafSnn35248 			if ((brand = brand_open(brandname)) == NULL) {
12509acbbeafSnn35248 				zerr("Zone %s uses non-existent brand \"%s\"."
12519acbbeafSnn35248 				    "  Unable to continue", zone, brandname);
12529acbbeafSnn35248 				exit(Z_ERR);
12539acbbeafSnn35248 			}
12540209230bSgjelinek 		} else if (global_zone && err == Z_NO_ZONE && !got_handle &&
12550209230bSgjelinek 		    !read_only_mode) {
12560209230bSgjelinek 			/*
12570209230bSgjelinek 			 * We implicitly create the global zone config if it
12580209230bSgjelinek 			 * doesn't exist.
12590209230bSgjelinek 			 */
12600209230bSgjelinek 			zone_dochandle_t tmphandle;
12610209230bSgjelinek 
12620209230bSgjelinek 			if ((tmphandle = zonecfg_init_handle()) == NULL) {
1263bbec428eSgjelinek 				zone_perror(execname, Z_NOMEM, B_TRUE);
12640209230bSgjelinek 				exit(Z_ERR);
12650209230bSgjelinek 			}
12660209230bSgjelinek 
12670209230bSgjelinek 			err = zonecfg_get_template_handle("SUNWblank", zone,
12680209230bSgjelinek 			    tmphandle);
12690209230bSgjelinek 
12700209230bSgjelinek 			if (err != Z_OK) {
12710209230bSgjelinek 				zonecfg_fini_handle(tmphandle);
1272bbec428eSgjelinek 				zone_perror("SUNWblank", err, B_TRUE);
12730209230bSgjelinek 				return (err);
12740209230bSgjelinek 			}
12750209230bSgjelinek 
1276bbec428eSgjelinek 			need_to_commit = B_TRUE;
12770209230bSgjelinek 			zonecfg_fini_handle(handle);
12780209230bSgjelinek 			handle = tmphandle;
1279bbec428eSgjelinek 			got_handle = B_TRUE;
12800209230bSgjelinek 
12817c478bd9Sstevel@tonic-gate 		} else {
12827c478bd9Sstevel@tonic-gate 			zone_perror(zone, err, handle_expected || got_handle);
12837c478bd9Sstevel@tonic-gate 			if (err == Z_NO_ZONE && !got_handle &&
12847c478bd9Sstevel@tonic-gate 			    interactive_mode && !read_only_mode)
12857c478bd9Sstevel@tonic-gate 				(void) printf(gettext("Use '%s' to begin "
12867c478bd9Sstevel@tonic-gate 				    "configuring a new zone.\n"),
12877c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_CREATE));
12887c478bd9Sstevel@tonic-gate 			return (err);
12897c478bd9Sstevel@tonic-gate 		}
12907c478bd9Sstevel@tonic-gate 	}
12917c478bd9Sstevel@tonic-gate 	return (Z_OK);
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
1294bbec428eSgjelinek static boolean_t
1295087719fdSdp state_atleast(zone_state_t state)
1296087719fdSdp {
1297087719fdSdp 	zone_state_t state_num;
1298087719fdSdp 	int err;
1299087719fdSdp 
1300087719fdSdp 	if ((err = zone_get_state(zone, &state_num)) != Z_OK) {
1301087719fdSdp 		/* all states are greater than "non-existent" */
1302087719fdSdp 		if (err == Z_NO_ZONE)
1303087719fdSdp 			return (B_FALSE);
1304087719fdSdp 		zerr(gettext("Unexpectedly failed to determine state "
1305087719fdSdp 		    "of zone %s: %s"), zone, zonecfg_strerror(err));
1306087719fdSdp 		exit(Z_ERR);
1307087719fdSdp 	}
1308087719fdSdp 	return (state_num >= state);
1309087719fdSdp }
1310087719fdSdp 
13117c478bd9Sstevel@tonic-gate /*
13127c478bd9Sstevel@tonic-gate  * short_usage() is for bad syntax: getopt() issues, too many arguments, etc.
13137c478bd9Sstevel@tonic-gate  */
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate void
13167c478bd9Sstevel@tonic-gate short_usage(int command)
13177c478bd9Sstevel@tonic-gate {
13187c478bd9Sstevel@tonic-gate 	/* lex_lineno has already been incremented in the lexer; compensate */
13197c478bd9Sstevel@tonic-gate 	if (cmd_file_mode) {
13207c478bd9Sstevel@tonic-gate 		if (strcmp(cmd_file_name, "-") == 0)
13217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13227c478bd9Sstevel@tonic-gate 			    gettext("syntax error on line %d\n"),
13237c478bd9Sstevel@tonic-gate 			    lex_lineno - 1);
13247c478bd9Sstevel@tonic-gate 		else
13257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13267c478bd9Sstevel@tonic-gate 			    gettext("syntax error on line %d of %s\n"),
13277c478bd9Sstevel@tonic-gate 			    lex_lineno - 1, cmd_file_name);
13287c478bd9Sstevel@tonic-gate 	}
13297c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\n%s\n", gettext("usage"),
13307c478bd9Sstevel@tonic-gate 	    helptab[command].short_usage);
1331bbec428eSgjelinek 	saw_error = B_TRUE;
13327c478bd9Sstevel@tonic-gate }
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate /*
13357c478bd9Sstevel@tonic-gate  * long_usage() is for bad semantics: e.g., wrong property type for a given
13367c478bd9Sstevel@tonic-gate  * resource type.  It is also used by longer_usage() below.
13377c478bd9Sstevel@tonic-gate  */
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate void
1340bbec428eSgjelinek long_usage(uint_t cmd_num, boolean_t set_saw)
13417c478bd9Sstevel@tonic-gate {
13427c478bd9Sstevel@tonic-gate 	(void) fprintf(set_saw ? stderr : stdout, "%s:\n%s\n", gettext("usage"),
13437c478bd9Sstevel@tonic-gate 	    helptab[cmd_num].short_usage);
13447c478bd9Sstevel@tonic-gate 	(void) fprintf(set_saw ? stderr : stdout, "\t%s\n", long_help(cmd_num));
13457c478bd9Sstevel@tonic-gate 	if (set_saw)
1346bbec428eSgjelinek 		saw_error = B_TRUE;
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate /*
13507c478bd9Sstevel@tonic-gate  * longer_usage() is for 'help foo' and 'foo -?': call long_usage() and also
13517c478bd9Sstevel@tonic-gate  * any extra usage() flags as appropriate for whatever command.
13527c478bd9Sstevel@tonic-gate  */
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate void
13557c478bd9Sstevel@tonic-gate longer_usage(uint_t cmd_num)
13567c478bd9Sstevel@tonic-gate {
1357bbec428eSgjelinek 	long_usage(cmd_num, B_FALSE);
13587c478bd9Sstevel@tonic-gate 	if (helptab[cmd_num].flags != 0) {
13597c478bd9Sstevel@tonic-gate 		(void) printf("\n");
1360bbec428eSgjelinek 		usage(B_TRUE, helptab[cmd_num].flags);
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate }
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate /*
13657c478bd9Sstevel@tonic-gate  * scope_usage() is simply used when a command is called from the wrong scope.
13667c478bd9Sstevel@tonic-gate  */
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate static void
13697c478bd9Sstevel@tonic-gate scope_usage(uint_t cmd_num)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	zerr(gettext("The %s command only makes sense in the %s scope."),
13727c478bd9Sstevel@tonic-gate 	    cmd_to_str(cmd_num),
13737c478bd9Sstevel@tonic-gate 	    global_scope ?  gettext("resource") : gettext("global"));
1374bbec428eSgjelinek 	saw_error = B_TRUE;
13757c478bd9Sstevel@tonic-gate }
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate /*
1378bbec428eSgjelinek  * On input, B_TRUE => yes, B_FALSE => no.
1379bbec428eSgjelinek  * On return, B_TRUE => 1, B_FALSE => no, could not ask => -1.
13807c478bd9Sstevel@tonic-gate  */
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate static int
1383bbec428eSgjelinek ask_yesno(boolean_t default_answer, const char *question)
13847c478bd9Sstevel@tonic-gate {
13857c478bd9Sstevel@tonic-gate 	char line[64];	/* should be enough to answer yes or no */
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	if (!ok_to_prompt) {
1388bbec428eSgjelinek 		saw_error = B_TRUE;
13897c478bd9Sstevel@tonic-gate 		return (-1);
13907c478bd9Sstevel@tonic-gate 	}
13917c478bd9Sstevel@tonic-gate 	for (;;) {
1392087719fdSdp 		if (printf("%s (%s)? ", question,
1393087719fdSdp 		    default_answer ? "[y]/n" : "y/[n]") < 0)
1394087719fdSdp 			return (-1);
1395087719fdSdp 		if (fgets(line, sizeof (line), stdin) == NULL)
1396087719fdSdp 			return (-1);
1397087719fdSdp 
1398087719fdSdp 		if (line[0] == '\n')
13997c478bd9Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
14007c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
14017c478bd9Sstevel@tonic-gate 			return (1);
14027c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
14037c478bd9Sstevel@tonic-gate 			return (0);
14047c478bd9Sstevel@tonic-gate 	}
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate /*
14087c478bd9Sstevel@tonic-gate  * Prints warning if zone already exists.
14097c478bd9Sstevel@tonic-gate  * In interactive mode, prompts if we should continue anyway and returns Z_OK
14107c478bd9Sstevel@tonic-gate  * if so, Z_ERR if not.  In non-interactive mode, exits with Z_ERR.
14117c478bd9Sstevel@tonic-gate  *
14127c478bd9Sstevel@tonic-gate  * Note that if a zone exists and its state is >= INSTALLED, an error message
14137c478bd9Sstevel@tonic-gate  * will be printed and this function will return Z_ERR regardless of mode.
14147c478bd9Sstevel@tonic-gate  */
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate static int
1417bbec428eSgjelinek check_if_zone_already_exists(boolean_t force)
14187c478bd9Sstevel@tonic-gate {
14197c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* enough to ask a question */
14207c478bd9Sstevel@tonic-gate 	zone_dochandle_t tmphandle;
14217c478bd9Sstevel@tonic-gate 	int res, answer;
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	if ((tmphandle = zonecfg_init_handle()) == NULL) {
1424bbec428eSgjelinek 		zone_perror(execname, Z_NOMEM, B_TRUE);
14257c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
14267c478bd9Sstevel@tonic-gate 	}
14277c478bd9Sstevel@tonic-gate 	res = zonecfg_get_handle(zone, tmphandle);
14287c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(tmphandle);
1429087719fdSdp 	if (res != Z_OK)
14307c478bd9Sstevel@tonic-gate 		return (Z_OK);
1431087719fdSdp 
1432087719fdSdp 	if (state_atleast(ZONE_STATE_INSTALLED)) {
14337c478bd9Sstevel@tonic-gate 		zerr(gettext("Zone %s already installed; %s not allowed."),
14347c478bd9Sstevel@tonic-gate 		    zone, cmd_to_str(CMD_CREATE));
14357c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	if (force) {
14397c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Zone %s already exists; overwriting.\n"),
14407c478bd9Sstevel@tonic-gate 		    zone);
14417c478bd9Sstevel@tonic-gate 		return (Z_OK);
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 	(void) snprintf(line, sizeof (line),
14447c478bd9Sstevel@tonic-gate 	    gettext("Zone %s already exists; %s anyway"), zone,
14457c478bd9Sstevel@tonic-gate 	    cmd_to_str(CMD_CREATE));
1446bbec428eSgjelinek 	if ((answer = ask_yesno(B_FALSE, line)) == -1) {
14477c478bd9Sstevel@tonic-gate 		zerr(gettext("Zone exists, input not from terminal and -F not "
14487c478bd9Sstevel@tonic-gate 		    "specified:\n%s command ignored, exiting."),
14497c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_CREATE));
14507c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 	return (answer == 1 ? Z_OK : Z_ERR);
14537c478bd9Sstevel@tonic-gate }
14547c478bd9Sstevel@tonic-gate 
1455bbec428eSgjelinek static boolean_t
14567c478bd9Sstevel@tonic-gate zone_is_read_only(int cmd_num)
14577c478bd9Sstevel@tonic-gate {
14587c478bd9Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
14597c478bd9Sstevel@tonic-gate 		zerr(gettext("%s: zones beginning with SUNW are read-only."),
14607c478bd9Sstevel@tonic-gate 		    zone);
1461bbec428eSgjelinek 		saw_error = B_TRUE;
1462bbec428eSgjelinek 		return (B_TRUE);
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 	if (read_only_mode) {
14657c478bd9Sstevel@tonic-gate 		zerr(gettext("%s: cannot %s in read-only mode."), zone,
14667c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
1467bbec428eSgjelinek 		saw_error = B_TRUE;
1468bbec428eSgjelinek 		return (B_TRUE);
14697c478bd9Sstevel@tonic-gate 	}
1470bbec428eSgjelinek 	return (B_FALSE);
14717c478bd9Sstevel@tonic-gate }
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate /*
14747c478bd9Sstevel@tonic-gate  * Create a new configuration.
14757c478bd9Sstevel@tonic-gate  */
14767c478bd9Sstevel@tonic-gate void
14777c478bd9Sstevel@tonic-gate create_func(cmd_t *cmd)
14787c478bd9Sstevel@tonic-gate {
14797c478bd9Sstevel@tonic-gate 	int err, arg;
14807c478bd9Sstevel@tonic-gate 	char zone_template[ZONENAME_MAX];
1481ee519a1fSgjelinek 	char attach_path[MAXPATHLEN];
14827c478bd9Sstevel@tonic-gate 	zone_dochandle_t tmphandle;
1483bbec428eSgjelinek 	boolean_t force = B_FALSE;
1484bbec428eSgjelinek 	boolean_t attach = B_FALSE;
1485bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	/* This is the default if no arguments are given. */
14907c478bd9Sstevel@tonic-gate 	(void) strlcpy(zone_template, "SUNWdefault", sizeof (zone_template));
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	optind = 0;
14939acbbeafSnn35248 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?a:bFt:"))
14949acbbeafSnn35248 	    != EOF) {
14957c478bd9Sstevel@tonic-gate 		switch (arg) {
14967c478bd9Sstevel@tonic-gate 		case '?':
14977c478bd9Sstevel@tonic-gate 			if (optopt == '?')
14987c478bd9Sstevel@tonic-gate 				longer_usage(CMD_CREATE);
14997c478bd9Sstevel@tonic-gate 			else
15007c478bd9Sstevel@tonic-gate 				short_usage(CMD_CREATE);
1501bbec428eSgjelinek 			arg_err = B_TRUE;
15027ec75eb8Sgjelinek 			break;
1503ee519a1fSgjelinek 		case 'a':
1504ee519a1fSgjelinek 			(void) strlcpy(attach_path, optarg,
1505ee519a1fSgjelinek 			    sizeof (attach_path));
1506bbec428eSgjelinek 			attach = B_TRUE;
1507ee519a1fSgjelinek 			break;
15087c478bd9Sstevel@tonic-gate 		case 'b':
15097c478bd9Sstevel@tonic-gate 			(void) strlcpy(zone_template, "SUNWblank",
15107c478bd9Sstevel@tonic-gate 			    sizeof (zone_template));
15117c478bd9Sstevel@tonic-gate 			break;
15127c478bd9Sstevel@tonic-gate 		case 'F':
1513bbec428eSgjelinek 			force = B_TRUE;
15147c478bd9Sstevel@tonic-gate 			break;
15157c478bd9Sstevel@tonic-gate 		case 't':
15167c478bd9Sstevel@tonic-gate 			(void) strlcpy(zone_template, optarg,
15177c478bd9Sstevel@tonic-gate 			    sizeof (zone_template));
15187c478bd9Sstevel@tonic-gate 			break;
15197c478bd9Sstevel@tonic-gate 		default:
15207c478bd9Sstevel@tonic-gate 			short_usage(CMD_CREATE);
1521bbec428eSgjelinek 			arg_err = B_TRUE;
15227ec75eb8Sgjelinek 			break;
15237ec75eb8Sgjelinek 		}
15247ec75eb8Sgjelinek 	}
15257ec75eb8Sgjelinek 	if (arg_err)
15267c478bd9Sstevel@tonic-gate 		return;
15277ec75eb8Sgjelinek 
15287c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
15297c478bd9Sstevel@tonic-gate 		short_usage(CMD_CREATE);
15307c478bd9Sstevel@tonic-gate 		return;
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_CREATE))
15347c478bd9Sstevel@tonic-gate 		return;
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	if (check_if_zone_already_exists(force) != Z_OK)
15377c478bd9Sstevel@tonic-gate 		return;
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	/*
15407c478bd9Sstevel@tonic-gate 	 * Get a temporary handle first.  If that fails, the old handle
15417c478bd9Sstevel@tonic-gate 	 * will not be lost.  Then finish whichever one we don't need,
15427c478bd9Sstevel@tonic-gate 	 * to avoid leaks.  Then get the handle for zone_template, and
15437c478bd9Sstevel@tonic-gate 	 * set the name to zone: this "copy, rename" method is how
15447c478bd9Sstevel@tonic-gate 	 * create -[b|t] works.
15457c478bd9Sstevel@tonic-gate 	 */
15467c478bd9Sstevel@tonic-gate 	if ((tmphandle = zonecfg_init_handle()) == NULL) {
1547bbec428eSgjelinek 		zone_perror(execname, Z_NOMEM, B_TRUE);
15487c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
15497c478bd9Sstevel@tonic-gate 	}
1550ee519a1fSgjelinek 
1551ee519a1fSgjelinek 	if (attach)
1552*16ab8c7bSgjelinek 		err = zonecfg_get_attach_handle(attach_path, ZONE_DETACHED,
1553*16ab8c7bSgjelinek 		    zone, B_FALSE, tmphandle);
1554ee519a1fSgjelinek 	else
1555ee519a1fSgjelinek 		err = zonecfg_get_template_handle(zone_template, zone,
1556ee519a1fSgjelinek 		    tmphandle);
1557ee519a1fSgjelinek 
1558ee519a1fSgjelinek 	if (err != Z_OK) {
15597c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(tmphandle);
1560ee519a1fSgjelinek 		if (attach && err == Z_NO_ZONE)
1561ee519a1fSgjelinek 			(void) fprintf(stderr, gettext("invalid path to "
1562ee519a1fSgjelinek 			    "detached zone\n"));
1563ee519a1fSgjelinek 		else if (attach && err == Z_INVALID_DOCUMENT)
1564ee519a1fSgjelinek 			(void) fprintf(stderr, gettext("Cannot attach to an "
1565ee519a1fSgjelinek 			    "earlier release of the operating system\n"));
1566ee519a1fSgjelinek 		else
1567bbec428eSgjelinek 			zone_perror(zone_template, err, B_TRUE);
15687c478bd9Sstevel@tonic-gate 		return;
15697c478bd9Sstevel@tonic-gate 	}
1570087719fdSdp 
1571bbec428eSgjelinek 	need_to_commit = B_TRUE;
15727c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
15737c478bd9Sstevel@tonic-gate 	handle = tmphandle;
1574bbec428eSgjelinek 	got_handle = B_TRUE;
15757c478bd9Sstevel@tonic-gate }
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate /*
15787c478bd9Sstevel@tonic-gate  * This malloc()'s memory, which must be freed by the caller.
15797c478bd9Sstevel@tonic-gate  */
15807c478bd9Sstevel@tonic-gate static char *
15817c478bd9Sstevel@tonic-gate quoteit(char *instr)
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	char *outstr;
15847c478bd9Sstevel@tonic-gate 	size_t outstrsize = strlen(instr) + 3;	/* 2 quotes + '\0' */
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	if ((outstr = malloc(outstrsize)) == NULL) {
1587bbec428eSgjelinek 		zone_perror(zone, Z_NOMEM, B_FALSE);
15887c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 	if (strchr(instr, ' ') == NULL) {
15917c478bd9Sstevel@tonic-gate 		(void) strlcpy(outstr, instr, outstrsize);
15927c478bd9Sstevel@tonic-gate 		return (outstr);
15937c478bd9Sstevel@tonic-gate 	}
15947c478bd9Sstevel@tonic-gate 	(void) snprintf(outstr, outstrsize, "\"%s\"", instr);
15957c478bd9Sstevel@tonic-gate 	return (outstr);
15967c478bd9Sstevel@tonic-gate }
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate static void
15997c478bd9Sstevel@tonic-gate export_prop(FILE *of, int prop_num, char *prop_id)
16007c478bd9Sstevel@tonic-gate {
16017c478bd9Sstevel@tonic-gate 	char *quote_str;
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	if (strlen(prop_id) == 0)
16047c478bd9Sstevel@tonic-gate 		return;
16057c478bd9Sstevel@tonic-gate 	quote_str = quoteit(prop_id);
16067c478bd9Sstevel@tonic-gate 	(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
16077c478bd9Sstevel@tonic-gate 	    pt_to_str(prop_num), quote_str);
16087c478bd9Sstevel@tonic-gate 	free(quote_str);
16097c478bd9Sstevel@tonic-gate }
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate void
16127c478bd9Sstevel@tonic-gate export_func(cmd_t *cmd)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
16157c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
16167c478bd9Sstevel@tonic-gate 	struct zone_devtab devtab;
16177c478bd9Sstevel@tonic-gate 	struct zone_attrtab attrtab;
16187c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
1619fa9e4066Sahrens 	struct zone_dstab dstab;
16200209230bSgjelinek 	struct zone_psettab psettab;
16210209230bSgjelinek 	struct zone_mcaptab mcaptab;
16227c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
16237c478bd9Sstevel@tonic-gate 	int err, arg;
16247c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN], outfile[MAXPATHLEN], pool[MAXNAMELEN];
16253f2f09c1Sdp 	char bootargs[BOOTARGS_MAX];
16260209230bSgjelinek 	char sched[MAXNAMELEN];
16279acbbeafSnn35248 	char brand[MAXNAMELEN];
1628ffbafc53Scomay 	char *limitpriv;
16297c478bd9Sstevel@tonic-gate 	FILE *of;
16307c478bd9Sstevel@tonic-gate 	boolean_t autoboot;
1631f4b3ec61Sdh155122 	zone_iptype_t iptype;
1632bbec428eSgjelinek 	boolean_t need_to_close = B_FALSE;
1633bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	outfile[0] = '\0';
16387c478bd9Sstevel@tonic-gate 	optind = 0;
16397c478bd9Sstevel@tonic-gate 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?f:")) != EOF) {
16407c478bd9Sstevel@tonic-gate 		switch (arg) {
16417c478bd9Sstevel@tonic-gate 		case '?':
16427c478bd9Sstevel@tonic-gate 			if (optopt == '?')
16437c478bd9Sstevel@tonic-gate 				longer_usage(CMD_EXPORT);
16447c478bd9Sstevel@tonic-gate 			else
16457c478bd9Sstevel@tonic-gate 				short_usage(CMD_EXPORT);
1646bbec428eSgjelinek 			arg_err = B_TRUE;
16477ec75eb8Sgjelinek 			break;
16487c478bd9Sstevel@tonic-gate 		case 'f':
16497c478bd9Sstevel@tonic-gate 			(void) strlcpy(outfile, optarg, sizeof (outfile));
16507c478bd9Sstevel@tonic-gate 			break;
16517c478bd9Sstevel@tonic-gate 		default:
16527c478bd9Sstevel@tonic-gate 			short_usage(CMD_EXPORT);
1653bbec428eSgjelinek 			arg_err = B_TRUE;
16547ec75eb8Sgjelinek 			break;
16557ec75eb8Sgjelinek 		}
16567ec75eb8Sgjelinek 	}
16577ec75eb8Sgjelinek 	if (arg_err)
16587c478bd9Sstevel@tonic-gate 		return;
16597ec75eb8Sgjelinek 
16607c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
16617c478bd9Sstevel@tonic-gate 		short_usage(CMD_EXPORT);
16627c478bd9Sstevel@tonic-gate 		return;
16637c478bd9Sstevel@tonic-gate 	}
16647c478bd9Sstevel@tonic-gate 	if (strlen(outfile) == 0) {
16657c478bd9Sstevel@tonic-gate 		of = stdout;
16667c478bd9Sstevel@tonic-gate 	} else {
16677c478bd9Sstevel@tonic-gate 		if ((of = fopen(outfile, "w")) == NULL) {
16687c478bd9Sstevel@tonic-gate 			zerr(gettext("opening file %s: %s"),
16697c478bd9Sstevel@tonic-gate 			    outfile, strerror(errno));
16707c478bd9Sstevel@tonic-gate 			goto done;
16717c478bd9Sstevel@tonic-gate 		}
16727c478bd9Sstevel@tonic-gate 		setbuf(of, NULL);
1673bbec428eSgjelinek 		need_to_close = B_TRUE;
16747c478bd9Sstevel@tonic-gate 	}
16757c478bd9Sstevel@tonic-gate 
1676bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
16777c478bd9Sstevel@tonic-gate 		goto done;
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	(void) fprintf(of, "%s -b\n", cmd_to_str(CMD_CREATE));
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	if (zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath)) == Z_OK &&
16827c478bd9Sstevel@tonic-gate 	    strlen(zonepath) > 0)
16837c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
16847c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_ZONEPATH), zonepath);
16857c478bd9Sstevel@tonic-gate 
16869acbbeafSnn35248 	if ((zone_get_brand(zone, brand, sizeof (brand)) == Z_OK) &&
16879acbbeafSnn35248 	    (strcmp(brand, NATIVE_BRAND_NAME) != 0))
16889acbbeafSnn35248 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
16899acbbeafSnn35248 		    pt_to_str(PT_BRAND), brand);
16909acbbeafSnn35248 
16917c478bd9Sstevel@tonic-gate 	if (zonecfg_get_autoboot(handle, &autoboot) == Z_OK)
16927c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
16937c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_AUTOBOOT), autoboot ? "true" : "false");
16947c478bd9Sstevel@tonic-gate 
16953f2f09c1Sdp 	if (zonecfg_get_bootargs(handle, bootargs, sizeof (bootargs)) == Z_OK &&
16963f2f09c1Sdp 	    strlen(bootargs) > 0) {
16973f2f09c1Sdp 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
16983f2f09c1Sdp 		    pt_to_str(PT_BOOTARGS), bootargs);
16993f2f09c1Sdp 	}
17003f2f09c1Sdp 
17017c478bd9Sstevel@tonic-gate 	if (zonecfg_get_pool(handle, pool, sizeof (pool)) == Z_OK &&
17027c478bd9Sstevel@tonic-gate 	    strlen(pool) > 0)
17037c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
17047c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_POOL), pool);
17057c478bd9Sstevel@tonic-gate 
1706ffbafc53Scomay 	if (zonecfg_get_limitpriv(handle, &limitpriv) == Z_OK &&
1707ffbafc53Scomay 	    strlen(limitpriv) > 0) {
1708ffbafc53Scomay 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
1709ffbafc53Scomay 		    pt_to_str(PT_LIMITPRIV), limitpriv);
1710ffbafc53Scomay 		free(limitpriv);
1711ffbafc53Scomay 	}
1712ffbafc53Scomay 
17130209230bSgjelinek 	if (zonecfg_get_sched_class(handle, sched, sizeof (sched)) == Z_OK &&
17140209230bSgjelinek 	    strlen(sched) > 0)
17150209230bSgjelinek 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
17160209230bSgjelinek 		    pt_to_str(PT_SCHED), sched);
17173f2f09c1Sdp 
1718f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, &iptype) == Z_OK) {
1719f4b3ec61Sdh155122 		switch (iptype) {
1720f4b3ec61Sdh155122 		case ZS_SHARED:
1721f4b3ec61Sdh155122 			(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
1722f4b3ec61Sdh155122 			    pt_to_str(PT_IPTYPE), "shared");
1723f4b3ec61Sdh155122 			break;
1724f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
1725f4b3ec61Sdh155122 			(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
1726f4b3ec61Sdh155122 			    pt_to_str(PT_IPTYPE), "exclusive");
1727f4b3ec61Sdh155122 			break;
1728f4b3ec61Sdh155122 		}
1729f4b3ec61Sdh155122 	}
1730f4b3ec61Sdh155122 
17317c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setipdent(handle)) != Z_OK) {
1732bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
17337c478bd9Sstevel@tonic-gate 		goto done;
17347c478bd9Sstevel@tonic-gate 	}
17357c478bd9Sstevel@tonic-gate 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
17367c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
17377c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_IPD));
17387c478bd9Sstevel@tonic-gate 		export_prop(of, PT_DIR, fstab.zone_fs_dir);
17397c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 	(void) zonecfg_endipdent(handle);
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setfsent(handle)) != Z_OK) {
1744bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
17457c478bd9Sstevel@tonic-gate 		goto done;
17467c478bd9Sstevel@tonic-gate 	}
17477c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
17487c478bd9Sstevel@tonic-gate 		zone_fsopt_t *optptr;
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
17517c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_FS));
17527c478bd9Sstevel@tonic-gate 		export_prop(of, PT_DIR, fstab.zone_fs_dir);
17537c478bd9Sstevel@tonic-gate 		export_prop(of, PT_SPECIAL, fstab.zone_fs_special);
17547c478bd9Sstevel@tonic-gate 		export_prop(of, PT_RAW, fstab.zone_fs_raw);
17557c478bd9Sstevel@tonic-gate 		export_prop(of, PT_TYPE, fstab.zone_fs_type);
17567c478bd9Sstevel@tonic-gate 		for (optptr = fstab.zone_fs_options; optptr != NULL;
17577c478bd9Sstevel@tonic-gate 		    optptr = optptr->zone_fsopt_next) {
17587c478bd9Sstevel@tonic-gate 			/*
17597c478bd9Sstevel@tonic-gate 			 * Simple property values with embedded equal signs
17607c478bd9Sstevel@tonic-gate 			 * need to be quoted to prevent the lexer from
17617c478bd9Sstevel@tonic-gate 			 * mis-parsing them as complex name=value pairs.
17627c478bd9Sstevel@tonic-gate 			 */
17637c478bd9Sstevel@tonic-gate 			if (strchr(optptr->zone_fsopt_opt, '='))
17647c478bd9Sstevel@tonic-gate 				(void) fprintf(of, "%s %s \"%s\"\n",
17657c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_ADD),
17667c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_OPTIONS),
17677c478bd9Sstevel@tonic-gate 				    optptr->zone_fsopt_opt);
17687c478bd9Sstevel@tonic-gate 			else
17697c478bd9Sstevel@tonic-gate 				(void) fprintf(of, "%s %s %s\n",
17707c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_ADD),
17717c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_OPTIONS),
17727c478bd9Sstevel@tonic-gate 				    optptr->zone_fsopt_opt);
17737c478bd9Sstevel@tonic-gate 		}
17747c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
17757c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
1780bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
17817c478bd9Sstevel@tonic-gate 		goto done;
17827c478bd9Sstevel@tonic-gate 	}
17837c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
17847c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
17857c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_NET));
17867c478bd9Sstevel@tonic-gate 		export_prop(of, PT_ADDRESS, nwiftab.zone_nwif_address);
17877c478bd9Sstevel@tonic-gate 		export_prop(of, PT_PHYSICAL, nwiftab.zone_nwif_physical);
1788de860bd9Sgfaden 		export_prop(of, PT_DEFROUTER, nwiftab.zone_nwif_defrouter);
17897c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
17907c478bd9Sstevel@tonic-gate 	}
17917c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setdevent(handle)) != Z_OK) {
1794bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
17957c478bd9Sstevel@tonic-gate 		goto done;
17967c478bd9Sstevel@tonic-gate 	}
17977c478bd9Sstevel@tonic-gate 	while (zonecfg_getdevent(handle, &devtab) == Z_OK) {
17987c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
17997c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_DEVICE));
18007c478bd9Sstevel@tonic-gate 		export_prop(of, PT_MATCH, devtab.zone_dev_match);
18017c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18027c478bd9Sstevel@tonic-gate 	}
18037c478bd9Sstevel@tonic-gate 	(void) zonecfg_enddevent(handle);
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
1806bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
18077c478bd9Sstevel@tonic-gate 		goto done;
18087c478bd9Sstevel@tonic-gate 	}
18097c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
18107c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s rctl\n", cmd_to_str(CMD_ADD));
18117c478bd9Sstevel@tonic-gate 		export_prop(of, PT_NAME, rctltab.zone_rctl_name);
18127c478bd9Sstevel@tonic-gate 		for (valptr = rctltab.zone_rctl_valptr; valptr != NULL;
18137c478bd9Sstevel@tonic-gate 		    valptr = valptr->zone_rctlval_next) {
18147c478bd9Sstevel@tonic-gate 			fprintf(of, "%s %s (%s=%s,%s=%s,%s=%s)\n",
18157c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_ADD), pt_to_str(PT_VALUE),
18167c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_PRIV), valptr->zone_rctlval_priv,
18177c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_LIMIT), valptr->zone_rctlval_limit,
18187c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_ACTION), valptr->zone_rctlval_action);
18197c478bd9Sstevel@tonic-gate 		}
18207c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18217c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setattrent(handle)) != Z_OK) {
1826bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
18277c478bd9Sstevel@tonic-gate 		goto done;
18287c478bd9Sstevel@tonic-gate 	}
18297c478bd9Sstevel@tonic-gate 	while (zonecfg_getattrent(handle, &attrtab) == Z_OK) {
18307c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
18317c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR));
18327c478bd9Sstevel@tonic-gate 		export_prop(of, PT_NAME, attrtab.zone_attr_name);
18337c478bd9Sstevel@tonic-gate 		export_prop(of, PT_TYPE, attrtab.zone_attr_type);
18347c478bd9Sstevel@tonic-gate 		export_prop(of, PT_VALUE, attrtab.zone_attr_value);
18357c478bd9Sstevel@tonic-gate 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18367c478bd9Sstevel@tonic-gate 	}
18377c478bd9Sstevel@tonic-gate 	(void) zonecfg_endattrent(handle);
18387c478bd9Sstevel@tonic-gate 
1839fa9e4066Sahrens 	if ((err = zonecfg_setdsent(handle)) != Z_OK) {
1840bbec428eSgjelinek 		zone_perror(zone, err, B_FALSE);
1841fa9e4066Sahrens 		goto done;
1842fa9e4066Sahrens 	}
1843fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
1844fa9e4066Sahrens 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
1845fa9e4066Sahrens 		    rt_to_str(RT_DATASET));
1846fa9e4066Sahrens 		export_prop(of, PT_NAME, dstab.zone_dataset_name);
1847fa9e4066Sahrens 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
1848fa9e4066Sahrens 	}
1849fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
1850fa9e4066Sahrens 
18510209230bSgjelinek 	if (zonecfg_getpsetent(handle, &psettab) == Z_OK) {
18520209230bSgjelinek 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
18530209230bSgjelinek 		    rt_to_str(RT_DCPU));
18540209230bSgjelinek 		if (strcmp(psettab.zone_ncpu_min, psettab.zone_ncpu_max) == 0)
18550209230bSgjelinek 			(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
18560209230bSgjelinek 			    pt_to_str(PT_NCPUS), psettab.zone_ncpu_max);
18570209230bSgjelinek 		else
18580209230bSgjelinek 			(void) fprintf(of, "%s %s=%s-%s\n", cmd_to_str(CMD_SET),
18590209230bSgjelinek 			    pt_to_str(PT_NCPUS), psettab.zone_ncpu_min,
18600209230bSgjelinek 			    psettab.zone_ncpu_max);
18610209230bSgjelinek 		if (psettab.zone_importance[0] != '\0')
18620209230bSgjelinek 			(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
18630209230bSgjelinek 			    pt_to_str(PT_IMPORTANCE), psettab.zone_importance);
18640209230bSgjelinek 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18650209230bSgjelinek 	}
18660209230bSgjelinek 
18670209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcaptab) == Z_OK) {
18680209230bSgjelinek 		char buf[128];
18690209230bSgjelinek 
18700209230bSgjelinek 		(void) fprintf(of, "%s %s\n", cmd_to_str(CMD_ADD),
18710209230bSgjelinek 		    rt_to_str(RT_MCAP));
18720209230bSgjelinek 		bytes_to_units(mcaptab.zone_physmem_cap, buf, sizeof (buf));
18730209230bSgjelinek 		(void) fprintf(of, "%s %s=%s\n", cmd_to_str(CMD_SET),
18740209230bSgjelinek 		    pt_to_str(PT_PHYSICAL), buf);
18750209230bSgjelinek 		(void) fprintf(of, "%s\n", cmd_to_str(CMD_END));
18760209230bSgjelinek 	}
18770209230bSgjelinek 
1878c97ad5cdSakolb 	/*
1879c97ad5cdSakolb 	 * There is nothing to export for pcap since this resource is just
1880c97ad5cdSakolb 	 * a container for an rctl alias.
1881c97ad5cdSakolb 	 */
1882c97ad5cdSakolb 
18837c478bd9Sstevel@tonic-gate done:
18847c478bd9Sstevel@tonic-gate 	if (need_to_close)
18857c478bd9Sstevel@tonic-gate 		(void) fclose(of);
18867c478bd9Sstevel@tonic-gate }
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate void
18897c478bd9Sstevel@tonic-gate exit_func(cmd_t *cmd)
18907c478bd9Sstevel@tonic-gate {
18917c478bd9Sstevel@tonic-gate 	int arg, answer;
1892bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	optind = 0;
18957c478bd9Sstevel@tonic-gate 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?F")) != EOF) {
18967c478bd9Sstevel@tonic-gate 		switch (arg) {
18977c478bd9Sstevel@tonic-gate 		case '?':
18987c478bd9Sstevel@tonic-gate 			longer_usage(CMD_EXIT);
1899bbec428eSgjelinek 			arg_err = B_TRUE;
19007ec75eb8Sgjelinek 			break;
19017c478bd9Sstevel@tonic-gate 		case 'F':
1902bbec428eSgjelinek 			force_exit = B_TRUE;
19037c478bd9Sstevel@tonic-gate 			break;
19047c478bd9Sstevel@tonic-gate 		default:
19057c478bd9Sstevel@tonic-gate 			short_usage(CMD_EXIT);
1906bbec428eSgjelinek 			arg_err = B_TRUE;
19077ec75eb8Sgjelinek 			break;
19087ec75eb8Sgjelinek 		}
19097ec75eb8Sgjelinek 	}
19107ec75eb8Sgjelinek 	if (arg_err)
19117c478bd9Sstevel@tonic-gate 		return;
19127ec75eb8Sgjelinek 
19137c478bd9Sstevel@tonic-gate 	if (optind < cmd->cmd_argc) {
19147c478bd9Sstevel@tonic-gate 		short_usage(CMD_EXIT);
19157c478bd9Sstevel@tonic-gate 		return;
19167c478bd9Sstevel@tonic-gate 	}
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 	if (global_scope || force_exit) {
1919bbec428eSgjelinek 		time_to_exit = B_TRUE;
19207c478bd9Sstevel@tonic-gate 		return;
19217c478bd9Sstevel@tonic-gate 	}
19227c478bd9Sstevel@tonic-gate 
1923bbec428eSgjelinek 	answer = ask_yesno(B_FALSE, "Resource incomplete; really quit");
19247c478bd9Sstevel@tonic-gate 	if (answer == -1) {
19257c478bd9Sstevel@tonic-gate 		zerr(gettext("Resource incomplete, input "
19267c478bd9Sstevel@tonic-gate 		    "not from terminal and -F not specified:\n%s command "
19277c478bd9Sstevel@tonic-gate 		    "ignored, but exiting anyway."), cmd_to_str(CMD_EXIT));
19287c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
19297c478bd9Sstevel@tonic-gate 	} else if (answer == 1) {
1930bbec428eSgjelinek 		time_to_exit = B_TRUE;
19317c478bd9Sstevel@tonic-gate 	}
19327c478bd9Sstevel@tonic-gate 	/* (answer == 0) => just return */
19337c478bd9Sstevel@tonic-gate }
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate static int
19367c478bd9Sstevel@tonic-gate validate_zonepath_syntax(char *path)
19377c478bd9Sstevel@tonic-gate {
19387c478bd9Sstevel@tonic-gate 	if (path[0] != '/') {
19397c478bd9Sstevel@tonic-gate 		zerr(gettext("%s is not an absolute path."), path);
19407c478bd9Sstevel@tonic-gate 		return (Z_ERR);
19417c478bd9Sstevel@tonic-gate 	}
19427c478bd9Sstevel@tonic-gate 	if (strcmp(path, "/") == 0) {
19437c478bd9Sstevel@tonic-gate 		zerr(gettext("/ is not allowed as a %s."),
19447c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_ZONEPATH));
19457c478bd9Sstevel@tonic-gate 		return (Z_ERR);
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate 	return (Z_OK);
19487c478bd9Sstevel@tonic-gate }
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate static void
19517c478bd9Sstevel@tonic-gate add_resource(cmd_t *cmd)
19527c478bd9Sstevel@tonic-gate {
19537c478bd9Sstevel@tonic-gate 	int type;
19540209230bSgjelinek 	struct zone_psettab tmp_psettab;
19550209230bSgjelinek 	struct zone_mcaptab tmp_mcaptab;
1956c97ad5cdSakolb 	uint64_t tmp;
19570209230bSgjelinek 	uint64_t tmp_mcap;
19580209230bSgjelinek 	char pool[MAXNAMELEN];
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	if ((type = cmd->cmd_res_type) == RT_UNKNOWN) {
1961bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
19627c478bd9Sstevel@tonic-gate 		goto bad;
19637c478bd9Sstevel@tonic-gate 	}
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 	switch (type) {
19667c478bd9Sstevel@tonic-gate 	case RT_FS:
19677c478bd9Sstevel@tonic-gate 		bzero(&in_progress_fstab, sizeof (in_progress_fstab));
19687c478bd9Sstevel@tonic-gate 		return;
19697c478bd9Sstevel@tonic-gate 	case RT_IPD:
1970087719fdSdp 		if (state_atleast(ZONE_STATE_INSTALLED)) {
19717c478bd9Sstevel@tonic-gate 			zerr(gettext("Zone %s already installed; %s %s not "
19727c478bd9Sstevel@tonic-gate 			    "allowed."), zone, cmd_to_str(CMD_ADD),
19737c478bd9Sstevel@tonic-gate 			    rt_to_str(RT_IPD));
19747c478bd9Sstevel@tonic-gate 			goto bad;
19757c478bd9Sstevel@tonic-gate 		}
19767c478bd9Sstevel@tonic-gate 		bzero(&in_progress_ipdtab, sizeof (in_progress_ipdtab));
19777c478bd9Sstevel@tonic-gate 		return;
19787c478bd9Sstevel@tonic-gate 	case RT_NET:
19797c478bd9Sstevel@tonic-gate 		bzero(&in_progress_nwiftab, sizeof (in_progress_nwiftab));
19807c478bd9Sstevel@tonic-gate 		return;
19817c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
19827c478bd9Sstevel@tonic-gate 		bzero(&in_progress_devtab, sizeof (in_progress_devtab));
19837c478bd9Sstevel@tonic-gate 		return;
19847c478bd9Sstevel@tonic-gate 	case RT_RCTL:
19850209230bSgjelinek 		if (global_zone)
19860209230bSgjelinek 			zerr(gettext("WARNING: Setting a global zone resource "
19870209230bSgjelinek 			    "control too low could deny\nservice "
19880209230bSgjelinek 			    "to even the root user; "
19890209230bSgjelinek 			    "this could render the system impossible\n"
19900209230bSgjelinek 			    "to administer.  Please use caution."));
19917c478bd9Sstevel@tonic-gate 		bzero(&in_progress_rctltab, sizeof (in_progress_rctltab));
19927c478bd9Sstevel@tonic-gate 		return;
19937c478bd9Sstevel@tonic-gate 	case RT_ATTR:
19947c478bd9Sstevel@tonic-gate 		bzero(&in_progress_attrtab, sizeof (in_progress_attrtab));
19957c478bd9Sstevel@tonic-gate 		return;
1996fa9e4066Sahrens 	case RT_DATASET:
1997fa9e4066Sahrens 		bzero(&in_progress_dstab, sizeof (in_progress_dstab));
1998fa9e4066Sahrens 		return;
19990209230bSgjelinek 	case RT_DCPU:
2000c97ad5cdSakolb 		/* Make sure there isn't already a cpu-set or cpu-cap entry. */
20010209230bSgjelinek 		if (zonecfg_lookup_pset(handle, &tmp_psettab) == Z_OK) {
20020209230bSgjelinek 			zerr(gettext("The %s resource already exists."),
20030209230bSgjelinek 			    rt_to_str(RT_DCPU));
20040209230bSgjelinek 			goto bad;
20050209230bSgjelinek 		}
2006c97ad5cdSakolb 		if (zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &tmp) !=
2007c97ad5cdSakolb 		    Z_NO_ENTRY) {
2008c97ad5cdSakolb 			zerr(gettext("The %s resource already exists."),
2009c97ad5cdSakolb 			    rt_to_str(RT_PCAP));
2010c97ad5cdSakolb 			goto bad;
2011c97ad5cdSakolb 		}
20120209230bSgjelinek 
20130209230bSgjelinek 		/* Make sure the pool property isn't set. */
20140209230bSgjelinek 		if (zonecfg_get_pool(handle, pool, sizeof (pool)) == Z_OK &&
20150209230bSgjelinek 		    strlen(pool) > 0) {
20160209230bSgjelinek 			zerr(gettext("The %s property is already set.  "
20170209230bSgjelinek 			    "A persistent pool is incompatible with\nthe %s "
20180209230bSgjelinek 			    "resource."),
20190209230bSgjelinek 			    pt_to_str(PT_POOL), rt_to_str(RT_DCPU));
20200209230bSgjelinek 			goto bad;
20210209230bSgjelinek 		}
20220209230bSgjelinek 
20230209230bSgjelinek 		bzero(&in_progress_psettab, sizeof (in_progress_psettab));
20240209230bSgjelinek 		return;
2025c97ad5cdSakolb 	case RT_PCAP:
2026c97ad5cdSakolb 		/*
2027c97ad5cdSakolb 		 * Make sure there isn't already a cpu-set or incompatible
2028c97ad5cdSakolb 		 * cpu-cap rctls.
2029c97ad5cdSakolb 		 */
2030c97ad5cdSakolb 		if (zonecfg_lookup_pset(handle, &tmp_psettab) == Z_OK) {
2031c97ad5cdSakolb 			zerr(gettext("The %s resource already exists."),
2032c97ad5cdSakolb 			    rt_to_str(RT_DCPU));
2033c97ad5cdSakolb 			goto bad;
2034c97ad5cdSakolb 		}
2035c97ad5cdSakolb 
2036c97ad5cdSakolb 		switch (zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &tmp)) {
2037c97ad5cdSakolb 		case Z_ALIAS_DISALLOW:
2038c97ad5cdSakolb 			zone_perror(rt_to_str(RT_PCAP), Z_ALIAS_DISALLOW,
2039bbec428eSgjelinek 			    B_FALSE);
2040c97ad5cdSakolb 			goto bad;
2041c97ad5cdSakolb 
2042c97ad5cdSakolb 		case Z_OK:
2043c97ad5cdSakolb 			zerr(gettext("The %s resource already exists."),
2044c97ad5cdSakolb 			    rt_to_str(RT_PCAP));
2045c97ad5cdSakolb 			goto bad;
2046c97ad5cdSakolb 
2047c97ad5cdSakolb 		default:
2048c97ad5cdSakolb 			break;
2049c97ad5cdSakolb 		}
2050c97ad5cdSakolb 		return;
20510209230bSgjelinek 	case RT_MCAP:
20520209230bSgjelinek 		/*
20530209230bSgjelinek 		 * Make sure there isn't already a mem-cap entry or max-swap
20540209230bSgjelinek 		 * or max-locked rctl.
20550209230bSgjelinek 		 */
20560209230bSgjelinek 		if (zonecfg_lookup_mcap(handle, &tmp_mcaptab) == Z_OK ||
20570209230bSgjelinek 		    zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP, &tmp_mcap)
20580209230bSgjelinek 		    == Z_OK ||
20590209230bSgjelinek 		    zonecfg_get_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM,
20600209230bSgjelinek 		    &tmp_mcap) == Z_OK) {
20610209230bSgjelinek 			zerr(gettext("The %s resource or a related resource "
20620209230bSgjelinek 			    "control already exists."), rt_to_str(RT_MCAP));
20630209230bSgjelinek 			goto bad;
20640209230bSgjelinek 		}
20650209230bSgjelinek 		if (global_zone)
20660209230bSgjelinek 			zerr(gettext("WARNING: Setting a global zone memory "
20670209230bSgjelinek 			    "cap too low could deny\nservice "
20680209230bSgjelinek 			    "to even the root user; "
20690209230bSgjelinek 			    "this could render the system impossible\n"
20700209230bSgjelinek 			    "to administer.  Please use caution."));
20710209230bSgjelinek 		bzero(&in_progress_mcaptab, sizeof (in_progress_mcaptab));
20720209230bSgjelinek 		return;
20737c478bd9Sstevel@tonic-gate 	default:
2074bbec428eSgjelinek 		zone_perror(rt_to_str(type), Z_NO_RESOURCE_TYPE, B_TRUE);
2075bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
2076bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
20777c478bd9Sstevel@tonic-gate 	}
20787c478bd9Sstevel@tonic-gate bad:
2079bbec428eSgjelinek 	global_scope = B_TRUE;
20807c478bd9Sstevel@tonic-gate 	end_op = -1;
20817c478bd9Sstevel@tonic-gate }
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate static void
20847c478bd9Sstevel@tonic-gate do_complex_rctl_val(complex_property_ptr_t cp)
20857c478bd9Sstevel@tonic-gate {
20867c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *rctlvaltab;
20877c478bd9Sstevel@tonic-gate 	complex_property_ptr_t cx;
2088bbec428eSgjelinek 	boolean_t seen_priv = B_FALSE, seen_limit = B_FALSE,
2089bbec428eSgjelinek 	    seen_action = B_FALSE;
20907c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk;
20917c478bd9Sstevel@tonic-gate 	int err;
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if ((rctlvaltab = alloc_rctlvaltab()) == NULL) {
2094bbec428eSgjelinek 		zone_perror(zone, Z_NOMEM, B_TRUE);
20957c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 	for (cx = cp; cx != NULL; cx = cx->cp_next) {
20987c478bd9Sstevel@tonic-gate 		switch (cx->cp_type) {
20997c478bd9Sstevel@tonic-gate 		case PT_PRIV:
21007c478bd9Sstevel@tonic-gate 			if (seen_priv) {
21017c478bd9Sstevel@tonic-gate 				zerr(gettext("%s already specified"),
21027c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_PRIV));
21037c478bd9Sstevel@tonic-gate 				goto bad;
21047c478bd9Sstevel@tonic-gate 			}
21057c478bd9Sstevel@tonic-gate 			(void) strlcpy(rctlvaltab->zone_rctlval_priv,
21067c478bd9Sstevel@tonic-gate 			    cx->cp_value,
21077c478bd9Sstevel@tonic-gate 			    sizeof (rctlvaltab->zone_rctlval_priv));
2108bbec428eSgjelinek 			seen_priv = B_TRUE;
21097c478bd9Sstevel@tonic-gate 			break;
21107c478bd9Sstevel@tonic-gate 		case PT_LIMIT:
21117c478bd9Sstevel@tonic-gate 			if (seen_limit) {
21127c478bd9Sstevel@tonic-gate 				zerr(gettext("%s already specified"),
21137c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_LIMIT));
21147c478bd9Sstevel@tonic-gate 				goto bad;
21157c478bd9Sstevel@tonic-gate 			}
21167c478bd9Sstevel@tonic-gate 			(void) strlcpy(rctlvaltab->zone_rctlval_limit,
21177c478bd9Sstevel@tonic-gate 			    cx->cp_value,
21187c478bd9Sstevel@tonic-gate 			    sizeof (rctlvaltab->zone_rctlval_limit));
2119bbec428eSgjelinek 			seen_limit = B_TRUE;
21207c478bd9Sstevel@tonic-gate 			break;
21217c478bd9Sstevel@tonic-gate 		case PT_ACTION:
21227c478bd9Sstevel@tonic-gate 			if (seen_action) {
21237c478bd9Sstevel@tonic-gate 				zerr(gettext("%s already specified"),
21247c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_ACTION));
21257c478bd9Sstevel@tonic-gate 				goto bad;
21267c478bd9Sstevel@tonic-gate 			}
21277c478bd9Sstevel@tonic-gate 			(void) strlcpy(rctlvaltab->zone_rctlval_action,
21287c478bd9Sstevel@tonic-gate 			    cx->cp_value,
21297c478bd9Sstevel@tonic-gate 			    sizeof (rctlvaltab->zone_rctlval_action));
2130bbec428eSgjelinek 			seen_action = B_TRUE;
21317c478bd9Sstevel@tonic-gate 			break;
21327c478bd9Sstevel@tonic-gate 		default:
21337c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(PT_VALUE),
2134bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
2135bbec428eSgjelinek 			long_usage(CMD_ADD, B_TRUE);
2136bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
21377c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctlvaltab);
21387c478bd9Sstevel@tonic-gate 			return;
21397c478bd9Sstevel@tonic-gate 		}
21407c478bd9Sstevel@tonic-gate 	}
21417c478bd9Sstevel@tonic-gate 	if (!seen_priv)
21427c478bd9Sstevel@tonic-gate 		zerr(gettext("%s not specified"), pt_to_str(PT_PRIV));
21437c478bd9Sstevel@tonic-gate 	if (!seen_limit)
21447c478bd9Sstevel@tonic-gate 		zerr(gettext("%s not specified"), pt_to_str(PT_LIMIT));
21457c478bd9Sstevel@tonic-gate 	if (!seen_action)
21467c478bd9Sstevel@tonic-gate 		zerr(gettext("%s not specified"), pt_to_str(PT_ACTION));
21477c478bd9Sstevel@tonic-gate 	if (!seen_priv || !seen_limit || !seen_action)
21487c478bd9Sstevel@tonic-gate 		goto bad;
21497c478bd9Sstevel@tonic-gate 	rctlvaltab->zone_rctlval_next = NULL;
21507c478bd9Sstevel@tonic-gate 	rctlblk = alloca(rctlblk_size());
21517c478bd9Sstevel@tonic-gate 	/*
21527c478bd9Sstevel@tonic-gate 	 * Make sure the rctl value looks roughly correct; we won't know if
21537c478bd9Sstevel@tonic-gate 	 * it's truly OK until we verify the configuration on the target
21547c478bd9Sstevel@tonic-gate 	 * system.
21557c478bd9Sstevel@tonic-gate 	 */
21567c478bd9Sstevel@tonic-gate 	if (zonecfg_construct_rctlblk(rctlvaltab, rctlblk) != Z_OK ||
21577c478bd9Sstevel@tonic-gate 	    !zonecfg_valid_rctlblk(rctlblk)) {
21587c478bd9Sstevel@tonic-gate 		zerr(gettext("Invalid %s %s specification"), rt_to_str(RT_RCTL),
21597c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_VALUE));
21607c478bd9Sstevel@tonic-gate 		goto bad;
21617c478bd9Sstevel@tonic-gate 	}
21627c478bd9Sstevel@tonic-gate 	err = zonecfg_add_rctl_value(&in_progress_rctltab, rctlvaltab);
21637c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
2164bbec428eSgjelinek 		zone_perror(pt_to_str(PT_VALUE), err, B_TRUE);
21657c478bd9Sstevel@tonic-gate 	return;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate bad:
21687c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctlvaltab);
21697c478bd9Sstevel@tonic-gate }
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate static void
21727c478bd9Sstevel@tonic-gate add_property(cmd_t *cmd)
21737c478bd9Sstevel@tonic-gate {
21747c478bd9Sstevel@tonic-gate 	char *prop_id;
21757c478bd9Sstevel@tonic-gate 	int err, res_type, prop_type;
21767c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
21777c478bd9Sstevel@tonic-gate 	list_property_ptr_t l;
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	res_type = resource_scope;
21807c478bd9Sstevel@tonic-gate 	prop_type = cmd->cmd_prop_name[0];
21817c478bd9Sstevel@tonic-gate 	if (res_type == RT_UNKNOWN || prop_type == PT_UNKNOWN) {
2182bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
21837c478bd9Sstevel@tonic-gate 		return;
21847c478bd9Sstevel@tonic-gate 	}
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	if (cmd->cmd_prop_nv_pairs != 1) {
2187bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
21887c478bd9Sstevel@tonic-gate 		return;
21897c478bd9Sstevel@tonic-gate 	}
21907c478bd9Sstevel@tonic-gate 
2191bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
21927c478bd9Sstevel@tonic-gate 		return;
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	switch (res_type) {
21957c478bd9Sstevel@tonic-gate 	case RT_FS:
21967c478bd9Sstevel@tonic-gate 		if (prop_type != PT_OPTIONS) {
21977c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
2198bbec428eSgjelinek 			    B_TRUE);
2199bbec428eSgjelinek 			long_usage(CMD_ADD, B_TRUE);
2200bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
22017c478bd9Sstevel@tonic-gate 			return;
22027c478bd9Sstevel@tonic-gate 		}
22037c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[0];
22047c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE &&
22057c478bd9Sstevel@tonic-gate 		    pp->pv_type != PROP_VAL_LIST) {
22067c478bd9Sstevel@tonic-gate 			zerr(gettext("A %s or %s value was expected here."),
22077c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_SIMPLE),
22087c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_LIST));
2209bbec428eSgjelinek 			saw_error = B_TRUE;
22107c478bd9Sstevel@tonic-gate 			return;
22117c478bd9Sstevel@tonic-gate 		}
22127c478bd9Sstevel@tonic-gate 		if (pp->pv_type == PROP_VAL_SIMPLE) {
22137c478bd9Sstevel@tonic-gate 			if (pp->pv_simple == NULL) {
2214bbec428eSgjelinek 				long_usage(CMD_ADD, B_TRUE);
22157c478bd9Sstevel@tonic-gate 				return;
22167c478bd9Sstevel@tonic-gate 			}
22177c478bd9Sstevel@tonic-gate 			prop_id = pp->pv_simple;
22187c478bd9Sstevel@tonic-gate 			err = zonecfg_add_fs_option(&in_progress_fstab,
22197c478bd9Sstevel@tonic-gate 			    prop_id);
22207c478bd9Sstevel@tonic-gate 			if (err != Z_OK)
2221bbec428eSgjelinek 				zone_perror(pt_to_str(prop_type), err, B_TRUE);
22227c478bd9Sstevel@tonic-gate 		} else {
22237c478bd9Sstevel@tonic-gate 			list_property_ptr_t list;
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 			for (list = pp->pv_list; list != NULL;
22267c478bd9Sstevel@tonic-gate 			    list = list->lp_next) {
22277c478bd9Sstevel@tonic-gate 				prop_id = list->lp_simple;
22287c478bd9Sstevel@tonic-gate 				if (prop_id == NULL)
22297c478bd9Sstevel@tonic-gate 					break;
22307c478bd9Sstevel@tonic-gate 				err = zonecfg_add_fs_option(
22317c478bd9Sstevel@tonic-gate 				    &in_progress_fstab, prop_id);
22327c478bd9Sstevel@tonic-gate 				if (err != Z_OK)
22337c478bd9Sstevel@tonic-gate 					zone_perror(pt_to_str(prop_type), err,
2234bbec428eSgjelinek 					    B_TRUE);
22357c478bd9Sstevel@tonic-gate 			}
22367c478bd9Sstevel@tonic-gate 		}
22377c478bd9Sstevel@tonic-gate 		return;
22387c478bd9Sstevel@tonic-gate 	case RT_RCTL:
22397c478bd9Sstevel@tonic-gate 		if (prop_type != PT_VALUE) {
22407c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
2241bbec428eSgjelinek 			    B_TRUE);
2242bbec428eSgjelinek 			long_usage(CMD_ADD, B_TRUE);
2243bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
22447c478bd9Sstevel@tonic-gate 			return;
22457c478bd9Sstevel@tonic-gate 		}
22467c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[0];
22477c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_COMPLEX &&
22487c478bd9Sstevel@tonic-gate 		    pp->pv_type != PROP_VAL_LIST) {
22497c478bd9Sstevel@tonic-gate 			zerr(gettext("A %s or %s value was expected here."),
22507c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_COMPLEX),
22517c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_LIST));
2252bbec428eSgjelinek 			saw_error = B_TRUE;
22537c478bd9Sstevel@tonic-gate 			return;
22547c478bd9Sstevel@tonic-gate 		}
22557c478bd9Sstevel@tonic-gate 		if (pp->pv_type == PROP_VAL_COMPLEX) {
22567c478bd9Sstevel@tonic-gate 			do_complex_rctl_val(pp->pv_complex);
22577c478bd9Sstevel@tonic-gate 			return;
22587c478bd9Sstevel@tonic-gate 		}
22597c478bd9Sstevel@tonic-gate 		for (l = pp->pv_list; l != NULL; l = l->lp_next)
22607c478bd9Sstevel@tonic-gate 			do_complex_rctl_val(l->lp_complex);
22617c478bd9Sstevel@tonic-gate 		return;
22627c478bd9Sstevel@tonic-gate 	default:
2263bbec428eSgjelinek 		zone_perror(rt_to_str(res_type), Z_NO_RESOURCE_TYPE, B_TRUE);
2264bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
2265bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
22667c478bd9Sstevel@tonic-gate 		return;
22677c478bd9Sstevel@tonic-gate 	}
22687c478bd9Sstevel@tonic-gate }
22697c478bd9Sstevel@tonic-gate 
22700209230bSgjelinek static boolean_t
22710209230bSgjelinek gz_invalid_resource(int type)
22720209230bSgjelinek {
22730209230bSgjelinek 	return (global_zone && (type == RT_FS || type == RT_IPD ||
22740209230bSgjelinek 	    type == RT_NET || type == RT_DEVICE || type == RT_ATTR ||
22750209230bSgjelinek 	    type == RT_DATASET));
22760209230bSgjelinek }
22770209230bSgjelinek 
22780209230bSgjelinek static boolean_t
22790209230bSgjelinek gz_invalid_rt_property(int type)
22800209230bSgjelinek {
22810209230bSgjelinek 	return (global_zone && (type == RT_ZONENAME || type == RT_ZONEPATH ||
22820209230bSgjelinek 	    type == RT_AUTOBOOT || type == RT_LIMITPRIV ||
2283f4b3ec61Sdh155122 	    type == RT_BOOTARGS || type == RT_BRAND || type == RT_SCHED ||
2284f4b3ec61Sdh155122 	    type == RT_IPTYPE));
22850209230bSgjelinek }
22860209230bSgjelinek 
22870209230bSgjelinek static boolean_t
22880209230bSgjelinek gz_invalid_property(int type)
22890209230bSgjelinek {
22900209230bSgjelinek 	return (global_zone && (type == PT_ZONENAME || type == PT_ZONEPATH ||
22910209230bSgjelinek 	    type == PT_AUTOBOOT || type == PT_LIMITPRIV ||
2292f4b3ec61Sdh155122 	    type == PT_BOOTARGS || type == PT_BRAND || type == PT_SCHED ||
2293f4b3ec61Sdh155122 	    type == PT_IPTYPE));
22940209230bSgjelinek }
22950209230bSgjelinek 
22967c478bd9Sstevel@tonic-gate void
22977c478bd9Sstevel@tonic-gate add_func(cmd_t *cmd)
22987c478bd9Sstevel@tonic-gate {
22997c478bd9Sstevel@tonic-gate 	int arg;
2300bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 	optind = 0;
23057ec75eb8Sgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?")) != EOF) {
23067c478bd9Sstevel@tonic-gate 		switch (arg) {
23077c478bd9Sstevel@tonic-gate 		case '?':
23087c478bd9Sstevel@tonic-gate 			longer_usage(CMD_ADD);
2309bbec428eSgjelinek 			arg_err = B_TRUE;
23107ec75eb8Sgjelinek 			break;
23117c478bd9Sstevel@tonic-gate 		default:
23127c478bd9Sstevel@tonic-gate 			short_usage(CMD_ADD);
2313bbec428eSgjelinek 			arg_err = B_TRUE;
23147ec75eb8Sgjelinek 			break;
23157ec75eb8Sgjelinek 		}
23167ec75eb8Sgjelinek 	}
23177ec75eb8Sgjelinek 	if (arg_err)
23187c478bd9Sstevel@tonic-gate 		return;
23197ec75eb8Sgjelinek 
23207c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
23217c478bd9Sstevel@tonic-gate 		short_usage(CMD_ADD);
23227c478bd9Sstevel@tonic-gate 		return;
23237c478bd9Sstevel@tonic-gate 	}
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_ADD))
23267c478bd9Sstevel@tonic-gate 		return;
23277c478bd9Sstevel@tonic-gate 
2328bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
23297c478bd9Sstevel@tonic-gate 		return;
23307c478bd9Sstevel@tonic-gate 	if (global_scope) {
23310209230bSgjelinek 		if (gz_invalid_resource(cmd->cmd_res_type)) {
23320209230bSgjelinek 			zerr(gettext("Cannot add a %s resource to the "
23330209230bSgjelinek 			    "global zone."), rt_to_str(cmd->cmd_res_type));
2334bbec428eSgjelinek 			saw_error = B_TRUE;
23350209230bSgjelinek 			return;
23360209230bSgjelinek 		}
23370209230bSgjelinek 
2338bbec428eSgjelinek 		global_scope = B_FALSE;
23397c478bd9Sstevel@tonic-gate 		resource_scope = cmd->cmd_res_type;
23407c478bd9Sstevel@tonic-gate 		end_op = CMD_ADD;
23417c478bd9Sstevel@tonic-gate 		add_resource(cmd);
23427c478bd9Sstevel@tonic-gate 	} else
23437c478bd9Sstevel@tonic-gate 		add_property(cmd);
23447c478bd9Sstevel@tonic-gate }
23457c478bd9Sstevel@tonic-gate 
2346087719fdSdp /*
2347087719fdSdp  * This routine has an unusual implementation, because it tries very
2348087719fdSdp  * hard to succeed in the face of a variety of failure modes.
2349087719fdSdp  * The most common and most vexing occurs when the index file and
2350087719fdSdp  * the /etc/zones/<zonename.xml> file are not both present.  In
2351087719fdSdp  * this case, delete must eradicate as much of the zone state as is left
2352087719fdSdp  * so that the user can later create a new zone with the same name.
2353087719fdSdp  */
23547c478bd9Sstevel@tonic-gate void
23557c478bd9Sstevel@tonic-gate delete_func(cmd_t *cmd)
23567c478bd9Sstevel@tonic-gate {
23577c478bd9Sstevel@tonic-gate 	int err, arg, answer;
23587c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* enough to ask a question */
2359bbec428eSgjelinek 	boolean_t force = B_FALSE;
2360bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	optind = 0;
23637c478bd9Sstevel@tonic-gate 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?F")) != EOF) {
23647c478bd9Sstevel@tonic-gate 		switch (arg) {
23657c478bd9Sstevel@tonic-gate 		case '?':
23667c478bd9Sstevel@tonic-gate 			longer_usage(CMD_DELETE);
2367bbec428eSgjelinek 			arg_err = B_TRUE;
23687ec75eb8Sgjelinek 			break;
23697c478bd9Sstevel@tonic-gate 		case 'F':
2370bbec428eSgjelinek 			force = B_TRUE;
23717c478bd9Sstevel@tonic-gate 			break;
23727c478bd9Sstevel@tonic-gate 		default:
23737c478bd9Sstevel@tonic-gate 			short_usage(CMD_DELETE);
2374bbec428eSgjelinek 			arg_err = B_TRUE;
23757ec75eb8Sgjelinek 			break;
23767ec75eb8Sgjelinek 		}
23777ec75eb8Sgjelinek 	}
23787ec75eb8Sgjelinek 	if (arg_err)
23797c478bd9Sstevel@tonic-gate 		return;
23807ec75eb8Sgjelinek 
23817c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
23827c478bd9Sstevel@tonic-gate 		short_usage(CMD_DELETE);
23837c478bd9Sstevel@tonic-gate 		return;
23847c478bd9Sstevel@tonic-gate 	}
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_DELETE))
23877c478bd9Sstevel@tonic-gate 		return;
23887c478bd9Sstevel@tonic-gate 
2389087719fdSdp 	if (!force) {
2390087719fdSdp 		/*
2391087719fdSdp 		 * Initialize sets up the global called "handle" and warns the
2392087719fdSdp 		 * user if the zone is not configured.  In force mode, we don't
2393087719fdSdp 		 * trust that evaluation, and hence skip it.  (We don't need the
2394087719fdSdp 		 * handle to be loaded anyway, since zonecfg_destroy is done by
2395087719fdSdp 		 * zonename).  However, we also have to take care to emulate the
2396087719fdSdp 		 * messages spit out by initialize; see below.
2397087719fdSdp 		 */
2398bbec428eSgjelinek 		if (initialize(B_TRUE) != Z_OK)
23997c478bd9Sstevel@tonic-gate 			return;
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
24027c478bd9Sstevel@tonic-gate 		    gettext("Are you sure you want to delete zone %s"), zone);
2403bbec428eSgjelinek 		if ((answer = ask_yesno(B_FALSE, line)) == -1) {
2404087719fdSdp 			zerr(gettext("Input not from terminal and -F not "
2405087719fdSdp 			    "specified:\n%s command ignored, exiting."),
2406087719fdSdp 			    cmd_to_str(CMD_DELETE));
24077c478bd9Sstevel@tonic-gate 			exit(Z_ERR);
24087c478bd9Sstevel@tonic-gate 		}
24097c478bd9Sstevel@tonic-gate 		if (answer != 1)
24107c478bd9Sstevel@tonic-gate 			return;
24117c478bd9Sstevel@tonic-gate 	}
24127c478bd9Sstevel@tonic-gate 
2413087719fdSdp 	if ((err = zonecfg_destroy(zone, force)) != Z_OK) {
2414087719fdSdp 		if ((err == Z_BAD_ZONE_STATE) && !force) {
2415087719fdSdp 			zerr(gettext("Zone %s not in %s state; %s not "
2416087719fdSdp 			    "allowed.  Use -F to force %s."),
2417087719fdSdp 			    zone, zone_state_str(ZONE_STATE_CONFIGURED),
2418087719fdSdp 			    cmd_to_str(CMD_DELETE), cmd_to_str(CMD_DELETE));
2419087719fdSdp 		} else {
2420bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
24217c478bd9Sstevel@tonic-gate 		}
2422087719fdSdp 	}
2423bbec428eSgjelinek 	need_to_commit = B_FALSE;
2424087719fdSdp 
2425087719fdSdp 	/*
2426087719fdSdp 	 * Emulate initialize's messaging; if there wasn't a valid handle to
2427087719fdSdp 	 * begin with, then user had typed delete (or delete -F) multiple
2428087719fdSdp 	 * times.  So we emit a message.
2429087719fdSdp 	 *
2430087719fdSdp 	 * We only do this in the 'force' case because normally, initialize()
2431087719fdSdp 	 * takes care of this for us.
2432087719fdSdp 	 */
2433087719fdSdp 	if (force && zonecfg_check_handle(handle) != Z_OK && interactive_mode)
2434087719fdSdp 		(void) printf(gettext("Use '%s' to begin "
2435087719fdSdp 		    "configuring a new zone.\n"), cmd_to_str(CMD_CREATE));
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	/*
24387c478bd9Sstevel@tonic-gate 	 * Time for a new handle: finish the old one off first
24397c478bd9Sstevel@tonic-gate 	 * then get a new one properly to avoid leaks.
24407c478bd9Sstevel@tonic-gate 	 */
2441087719fdSdp 	if (got_handle) {
24427c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
24437c478bd9Sstevel@tonic-gate 		if ((handle = zonecfg_init_handle()) == NULL) {
2444bbec428eSgjelinek 			zone_perror(execname, Z_NOMEM, B_TRUE);
24457c478bd9Sstevel@tonic-gate 			exit(Z_ERR);
24467c478bd9Sstevel@tonic-gate 		}
24477c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_get_handle(zone, handle)) != Z_OK) {
24487c478bd9Sstevel@tonic-gate 			/* If there was no zone before, that's OK */
24497c478bd9Sstevel@tonic-gate 			if (err != Z_NO_ZONE)
2450bbec428eSgjelinek 				zone_perror(zone, err, B_TRUE);
2451bbec428eSgjelinek 			got_handle = B_FALSE;
24527c478bd9Sstevel@tonic-gate 		}
24537c478bd9Sstevel@tonic-gate 	}
2454087719fdSdp }
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate static int
2457bbec428eSgjelinek fill_in_fstab(cmd_t *cmd, struct zone_fstab *fstab, boolean_t fill_in_only)
24587c478bd9Sstevel@tonic-gate {
24597c478bd9Sstevel@tonic-gate 	int err, i;
24607c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
24617c478bd9Sstevel@tonic-gate 
2462bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
24637c478bd9Sstevel@tonic-gate 		return (err);
24647c478bd9Sstevel@tonic-gate 
2465e193d1e6Svp157776 	bzero(fstab, sizeof (*fstab));
24667c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
24677c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
24687c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
24697c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2470bbec428eSgjelinek 			saw_error = B_TRUE;
24717c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
24727c478bd9Sstevel@tonic-gate 		}
24737c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
24747c478bd9Sstevel@tonic-gate 		case PT_DIR:
24757c478bd9Sstevel@tonic-gate 			(void) strlcpy(fstab->zone_fs_dir, pp->pv_simple,
24767c478bd9Sstevel@tonic-gate 			    sizeof (fstab->zone_fs_dir));
24777c478bd9Sstevel@tonic-gate 			break;
24787c478bd9Sstevel@tonic-gate 		case PT_SPECIAL:
24797c478bd9Sstevel@tonic-gate 			(void) strlcpy(fstab->zone_fs_special, pp->pv_simple,
24807c478bd9Sstevel@tonic-gate 			    sizeof (fstab->zone_fs_special));
24817c478bd9Sstevel@tonic-gate 			break;
24827c478bd9Sstevel@tonic-gate 		case PT_RAW:
24837c478bd9Sstevel@tonic-gate 			(void) strlcpy(fstab->zone_fs_raw, pp->pv_simple,
24847c478bd9Sstevel@tonic-gate 			    sizeof (fstab->zone_fs_raw));
24857c478bd9Sstevel@tonic-gate 			break;
24867c478bd9Sstevel@tonic-gate 		case PT_TYPE:
24877c478bd9Sstevel@tonic-gate 			(void) strlcpy(fstab->zone_fs_type, pp->pv_simple,
24887c478bd9Sstevel@tonic-gate 			    sizeof (fstab->zone_fs_type));
24897c478bd9Sstevel@tonic-gate 			break;
24907c478bd9Sstevel@tonic-gate 		default:
24917c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2492bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
24937c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
24947c478bd9Sstevel@tonic-gate 		}
24957c478bd9Sstevel@tonic-gate 	}
24967c478bd9Sstevel@tonic-gate 	if (fill_in_only)
24977c478bd9Sstevel@tonic-gate 		return (Z_OK);
24987c478bd9Sstevel@tonic-gate 	return (zonecfg_lookup_filesystem(handle, fstab));
24997c478bd9Sstevel@tonic-gate }
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate static int
2502bbec428eSgjelinek fill_in_ipdtab(cmd_t *cmd, struct zone_fstab *ipdtab, boolean_t fill_in_only)
25037c478bd9Sstevel@tonic-gate {
25047c478bd9Sstevel@tonic-gate 	int err, i;
25057c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
25067c478bd9Sstevel@tonic-gate 
2507bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
25087c478bd9Sstevel@tonic-gate 		return (err);
25097c478bd9Sstevel@tonic-gate 
2510e193d1e6Svp157776 	bzero(ipdtab, sizeof (*ipdtab));
25117c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
25127c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
25137c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
25147c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2515bbec428eSgjelinek 			saw_error = B_TRUE;
25167c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
25177c478bd9Sstevel@tonic-gate 		}
25187c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
25197c478bd9Sstevel@tonic-gate 		case PT_DIR:
25207c478bd9Sstevel@tonic-gate 			(void) strlcpy(ipdtab->zone_fs_dir, pp->pv_simple,
25217c478bd9Sstevel@tonic-gate 			    sizeof (ipdtab->zone_fs_dir));
25227c478bd9Sstevel@tonic-gate 			break;
25237c478bd9Sstevel@tonic-gate 		default:
25247c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2525bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
25267c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
25277c478bd9Sstevel@tonic-gate 		}
25287c478bd9Sstevel@tonic-gate 	}
25297c478bd9Sstevel@tonic-gate 	if (fill_in_only)
25307c478bd9Sstevel@tonic-gate 		return (Z_OK);
25317c478bd9Sstevel@tonic-gate 	return (zonecfg_lookup_ipd(handle, ipdtab));
25327c478bd9Sstevel@tonic-gate }
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate static int
2535bbec428eSgjelinek fill_in_nwiftab(cmd_t *cmd, struct zone_nwiftab *nwiftab,
2536bbec428eSgjelinek     boolean_t fill_in_only)
25377c478bd9Sstevel@tonic-gate {
25387c478bd9Sstevel@tonic-gate 	int err, i;
25397c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
25407c478bd9Sstevel@tonic-gate 
2541bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
25427c478bd9Sstevel@tonic-gate 		return (err);
25437c478bd9Sstevel@tonic-gate 
2544e193d1e6Svp157776 	bzero(nwiftab, sizeof (*nwiftab));
25457c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
25467c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
25477c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
25487c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2549bbec428eSgjelinek 			saw_error = B_TRUE;
25507c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
25517c478bd9Sstevel@tonic-gate 		}
25527c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
25537c478bd9Sstevel@tonic-gate 		case PT_ADDRESS:
25547c478bd9Sstevel@tonic-gate 			(void) strlcpy(nwiftab->zone_nwif_address,
25557c478bd9Sstevel@tonic-gate 			    pp->pv_simple, sizeof (nwiftab->zone_nwif_address));
25567c478bd9Sstevel@tonic-gate 			break;
25577c478bd9Sstevel@tonic-gate 		case PT_PHYSICAL:
25587c478bd9Sstevel@tonic-gate 			(void) strlcpy(nwiftab->zone_nwif_physical,
25597c478bd9Sstevel@tonic-gate 			    pp->pv_simple,
25607c478bd9Sstevel@tonic-gate 			    sizeof (nwiftab->zone_nwif_physical));
25617c478bd9Sstevel@tonic-gate 			break;
2562de860bd9Sgfaden 		case PT_DEFROUTER:
2563de860bd9Sgfaden 			(void) strlcpy(nwiftab->zone_nwif_defrouter,
2564de860bd9Sgfaden 			    pp->pv_simple,
2565de860bd9Sgfaden 			    sizeof (nwiftab->zone_nwif_defrouter));
2566de860bd9Sgfaden 			break;
25677c478bd9Sstevel@tonic-gate 		default:
25687c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2569bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
25707c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
25717c478bd9Sstevel@tonic-gate 		}
25727c478bd9Sstevel@tonic-gate 	}
25737c478bd9Sstevel@tonic-gate 	if (fill_in_only)
25747c478bd9Sstevel@tonic-gate 		return (Z_OK);
25757c478bd9Sstevel@tonic-gate 	err = zonecfg_lookup_nwif(handle, nwiftab);
25767c478bd9Sstevel@tonic-gate 	return (err);
25777c478bd9Sstevel@tonic-gate }
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate static int
2580bbec428eSgjelinek fill_in_devtab(cmd_t *cmd, struct zone_devtab *devtab, boolean_t fill_in_only)
25817c478bd9Sstevel@tonic-gate {
25827c478bd9Sstevel@tonic-gate 	int err, i;
25837c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
25847c478bd9Sstevel@tonic-gate 
2585bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
25867c478bd9Sstevel@tonic-gate 		return (err);
25877c478bd9Sstevel@tonic-gate 
2588e193d1e6Svp157776 	bzero(devtab, sizeof (*devtab));
25897c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
25907c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
25917c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
25927c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2593bbec428eSgjelinek 			saw_error = B_TRUE;
25947c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
25957c478bd9Sstevel@tonic-gate 		}
25967c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
25977c478bd9Sstevel@tonic-gate 		case PT_MATCH:
25987c478bd9Sstevel@tonic-gate 			(void) strlcpy(devtab->zone_dev_match, pp->pv_simple,
25997c478bd9Sstevel@tonic-gate 			    sizeof (devtab->zone_dev_match));
26007c478bd9Sstevel@tonic-gate 			break;
26017c478bd9Sstevel@tonic-gate 		default:
26027c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2603bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
26047c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
26057c478bd9Sstevel@tonic-gate 		}
26067c478bd9Sstevel@tonic-gate 	}
26077c478bd9Sstevel@tonic-gate 	if (fill_in_only)
26087c478bd9Sstevel@tonic-gate 		return (Z_OK);
26097c478bd9Sstevel@tonic-gate 	err = zonecfg_lookup_dev(handle, devtab);
26107c478bd9Sstevel@tonic-gate 	return (err);
26117c478bd9Sstevel@tonic-gate }
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate static int
2614bbec428eSgjelinek fill_in_rctltab(cmd_t *cmd, struct zone_rctltab *rctltab,
2615bbec428eSgjelinek     boolean_t fill_in_only)
26167c478bd9Sstevel@tonic-gate {
26177c478bd9Sstevel@tonic-gate 	int err, i;
26187c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
26197c478bd9Sstevel@tonic-gate 
2620bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
26217c478bd9Sstevel@tonic-gate 		return (err);
26227c478bd9Sstevel@tonic-gate 
2623e193d1e6Svp157776 	bzero(rctltab, sizeof (*rctltab));
26247c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
26257c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
26267c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
26277c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2628bbec428eSgjelinek 			saw_error = B_TRUE;
26297c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
26307c478bd9Sstevel@tonic-gate 		}
26317c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
26327c478bd9Sstevel@tonic-gate 		case PT_NAME:
26337c478bd9Sstevel@tonic-gate 			(void) strlcpy(rctltab->zone_rctl_name, pp->pv_simple,
26347c478bd9Sstevel@tonic-gate 			    sizeof (rctltab->zone_rctl_name));
26357c478bd9Sstevel@tonic-gate 			break;
26367c478bd9Sstevel@tonic-gate 		default:
26377c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2638bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
26397c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
26407c478bd9Sstevel@tonic-gate 		}
26417c478bd9Sstevel@tonic-gate 	}
26427c478bd9Sstevel@tonic-gate 	if (fill_in_only)
26437c478bd9Sstevel@tonic-gate 		return (Z_OK);
26447c478bd9Sstevel@tonic-gate 	err = zonecfg_lookup_rctl(handle, rctltab);
26457c478bd9Sstevel@tonic-gate 	return (err);
26467c478bd9Sstevel@tonic-gate }
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate static int
2649bbec428eSgjelinek fill_in_attrtab(cmd_t *cmd, struct zone_attrtab *attrtab,
2650bbec428eSgjelinek     boolean_t fill_in_only)
26517c478bd9Sstevel@tonic-gate {
26527c478bd9Sstevel@tonic-gate 	int err, i;
26537c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
26547c478bd9Sstevel@tonic-gate 
2655bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
26567c478bd9Sstevel@tonic-gate 		return (err);
26577c478bd9Sstevel@tonic-gate 
2658e193d1e6Svp157776 	bzero(attrtab, sizeof (*attrtab));
26597c478bd9Sstevel@tonic-gate 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
26607c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[i];
26617c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
26627c478bd9Sstevel@tonic-gate 			zerr(gettext("A simple value was expected here."));
2663bbec428eSgjelinek 			saw_error = B_TRUE;
26647c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
26657c478bd9Sstevel@tonic-gate 		}
26667c478bd9Sstevel@tonic-gate 		switch (cmd->cmd_prop_name[i]) {
26677c478bd9Sstevel@tonic-gate 		case PT_NAME:
26687c478bd9Sstevel@tonic-gate 			(void) strlcpy(attrtab->zone_attr_name, pp->pv_simple,
26697c478bd9Sstevel@tonic-gate 			    sizeof (attrtab->zone_attr_name));
26707c478bd9Sstevel@tonic-gate 			break;
26717c478bd9Sstevel@tonic-gate 		case PT_TYPE:
26727c478bd9Sstevel@tonic-gate 			(void) strlcpy(attrtab->zone_attr_type, pp->pv_simple,
26737c478bd9Sstevel@tonic-gate 			    sizeof (attrtab->zone_attr_type));
26747c478bd9Sstevel@tonic-gate 			break;
26757c478bd9Sstevel@tonic-gate 		case PT_VALUE:
26767c478bd9Sstevel@tonic-gate 			(void) strlcpy(attrtab->zone_attr_value, pp->pv_simple,
26777c478bd9Sstevel@tonic-gate 			    sizeof (attrtab->zone_attr_value));
26787c478bd9Sstevel@tonic-gate 			break;
26797c478bd9Sstevel@tonic-gate 		default:
26807c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2681bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
26827c478bd9Sstevel@tonic-gate 			return (Z_INSUFFICIENT_SPEC);
26837c478bd9Sstevel@tonic-gate 		}
26847c478bd9Sstevel@tonic-gate 	}
26857c478bd9Sstevel@tonic-gate 	if (fill_in_only)
26867c478bd9Sstevel@tonic-gate 		return (Z_OK);
26877c478bd9Sstevel@tonic-gate 	err = zonecfg_lookup_attr(handle, attrtab);
26887c478bd9Sstevel@tonic-gate 	return (err);
26897c478bd9Sstevel@tonic-gate }
26907c478bd9Sstevel@tonic-gate 
2691fa9e4066Sahrens static int
2692bbec428eSgjelinek fill_in_dstab(cmd_t *cmd, struct zone_dstab *dstab, boolean_t fill_in_only)
2693fa9e4066Sahrens {
2694fa9e4066Sahrens 	int err, i;
2695fa9e4066Sahrens 	property_value_ptr_t pp;
2696fa9e4066Sahrens 
2697bbec428eSgjelinek 	if ((err = initialize(B_TRUE)) != Z_OK)
2698fa9e4066Sahrens 		return (err);
2699fa9e4066Sahrens 
2700fa9e4066Sahrens 	dstab->zone_dataset_name[0] = '\0';
2701fa9e4066Sahrens 	for (i = 0; i < cmd->cmd_prop_nv_pairs; i++) {
2702fa9e4066Sahrens 		pp = cmd->cmd_property_ptr[i];
2703fa9e4066Sahrens 		if (pp->pv_type != PROP_VAL_SIMPLE || pp->pv_simple == NULL) {
2704fa9e4066Sahrens 			zerr(gettext("A simple value was expected here."));
2705bbec428eSgjelinek 			saw_error = B_TRUE;
2706fa9e4066Sahrens 			return (Z_INSUFFICIENT_SPEC);
2707fa9e4066Sahrens 		}
2708fa9e4066Sahrens 		switch (cmd->cmd_prop_name[i]) {
2709fa9e4066Sahrens 		case PT_NAME:
2710fa9e4066Sahrens 			(void) strlcpy(dstab->zone_dataset_name, pp->pv_simple,
2711fa9e4066Sahrens 			    sizeof (dstab->zone_dataset_name));
2712fa9e4066Sahrens 			break;
2713fa9e4066Sahrens 		default:
2714fa9e4066Sahrens 			zone_perror(pt_to_str(cmd->cmd_prop_name[i]),
2715bbec428eSgjelinek 			    Z_NO_PROPERTY_TYPE, B_TRUE);
2716fa9e4066Sahrens 			return (Z_INSUFFICIENT_SPEC);
2717fa9e4066Sahrens 		}
2718fa9e4066Sahrens 	}
2719fa9e4066Sahrens 	if (fill_in_only)
2720fa9e4066Sahrens 		return (Z_OK);
2721fa9e4066Sahrens 	return (zonecfg_lookup_ds(handle, dstab));
2722fa9e4066Sahrens }
2723fa9e4066Sahrens 
27247c478bd9Sstevel@tonic-gate static void
27250209230bSgjelinek remove_aliased_rctl(int type, char *name)
27267c478bd9Sstevel@tonic-gate {
27270209230bSgjelinek 	int err;
27280209230bSgjelinek 	uint64_t tmp;
27297c478bd9Sstevel@tonic-gate 
27300209230bSgjelinek 	if ((err = zonecfg_get_aliased_rctl(handle, name, &tmp)) != Z_OK) {
27310209230bSgjelinek 		zerr("%s %s: %s", cmd_to_str(CMD_CLEAR), pt_to_str(type),
27320209230bSgjelinek 		    zonecfg_strerror(err));
2733bbec428eSgjelinek 		saw_error = B_TRUE;
27347c478bd9Sstevel@tonic-gate 		return;
27357c478bd9Sstevel@tonic-gate 	}
27360209230bSgjelinek 	if ((err = zonecfg_rm_aliased_rctl(handle, name)) != Z_OK) {
27370209230bSgjelinek 		zerr("%s %s: %s", cmd_to_str(CMD_CLEAR), pt_to_str(type),
27380209230bSgjelinek 		    zonecfg_strerror(err));
2739bbec428eSgjelinek 		saw_error = B_TRUE;
27400209230bSgjelinek 	} else {
2741bbec428eSgjelinek 		need_to_commit = B_TRUE;
27420209230bSgjelinek 	}
27430209230bSgjelinek }
27447c478bd9Sstevel@tonic-gate 
27450209230bSgjelinek static boolean_t
27460209230bSgjelinek prompt_remove_resource(cmd_t *cmd, char *rsrc)
27470209230bSgjelinek {
27480209230bSgjelinek 	int num;
27490209230bSgjelinek 	int answer;
27500209230bSgjelinek 	int arg;
27510209230bSgjelinek 	boolean_t force = B_FALSE;
27520209230bSgjelinek 	char prompt[128];
2753bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
27547c478bd9Sstevel@tonic-gate 
27550209230bSgjelinek 	optind = 0;
27560209230bSgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "F")) != EOF) {
27570209230bSgjelinek 		switch (arg) {
27580209230bSgjelinek 		case 'F':
27590209230bSgjelinek 			force = B_TRUE;
27600209230bSgjelinek 			break;
27610209230bSgjelinek 		default:
2762bbec428eSgjelinek 			arg_err = B_TRUE;
27637ec75eb8Sgjelinek 			break;
27647ec75eb8Sgjelinek 		}
27657ec75eb8Sgjelinek 	}
27667ec75eb8Sgjelinek 	if (arg_err)
27670209230bSgjelinek 		return (B_FALSE);
27687ec75eb8Sgjelinek 
27690209230bSgjelinek 
27700209230bSgjelinek 	num = zonecfg_num_resources(handle, rsrc);
27710209230bSgjelinek 
27720209230bSgjelinek 	if (num == 0) {
27730209230bSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, cmd->cmd_res_type, Z_NO_ENTRY,
2774bbec428eSgjelinek 		    B_TRUE);
27750209230bSgjelinek 		return (B_FALSE);
27760209230bSgjelinek 	}
27770209230bSgjelinek 	if (num > 1 && !force) {
27780209230bSgjelinek 		if (!interactive_mode) {
27790209230bSgjelinek 			zerr(gettext("There are multiple instances of this "
27800209230bSgjelinek 			    "resource.  Either qualify the resource to\n"
27810209230bSgjelinek 			    "remove a single instance or use the -F option to "
27820209230bSgjelinek 			    "remove all instances."));
2783bbec428eSgjelinek 			saw_error = B_TRUE;
27840209230bSgjelinek 			return (B_FALSE);
27850209230bSgjelinek 		}
27860209230bSgjelinek 		(void) snprintf(prompt, sizeof (prompt), gettext(
27870209230bSgjelinek 		    "Are you sure you want to remove ALL '%s' resources"),
27880209230bSgjelinek 		    rsrc);
2789bbec428eSgjelinek 		answer = ask_yesno(B_FALSE, prompt);
27900209230bSgjelinek 		if (answer == -1) {
27910209230bSgjelinek 			zerr(gettext("Resource incomplete."));
27920209230bSgjelinek 			return (B_FALSE);
27930209230bSgjelinek 		}
27940209230bSgjelinek 		if (answer != 1)
27950209230bSgjelinek 			return (B_FALSE);
27960209230bSgjelinek 	}
27970209230bSgjelinek 	return (B_TRUE);
27980209230bSgjelinek }
27990209230bSgjelinek 
28000209230bSgjelinek static void
28010209230bSgjelinek remove_fs(cmd_t *cmd)
28020209230bSgjelinek {
28030209230bSgjelinek 	int err;
28040209230bSgjelinek 
28050209230bSgjelinek 	/* traditional, qualified fs removal */
28060209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
28070209230bSgjelinek 		struct zone_fstab fstab;
28080209230bSgjelinek 
2809bbec428eSgjelinek 		if ((err = fill_in_fstab(cmd, &fstab, B_FALSE)) != Z_OK) {
2810bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_FS, err, B_TRUE);
28117c478bd9Sstevel@tonic-gate 			return;
28127c478bd9Sstevel@tonic-gate 		}
28137c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_delete_filesystem(handle, &fstab)) != Z_OK)
2814bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_FS, err, B_TRUE);
28157c478bd9Sstevel@tonic-gate 		else
2816bbec428eSgjelinek 			need_to_commit = B_TRUE;
28177c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
28187c478bd9Sstevel@tonic-gate 		return;
28190209230bSgjelinek 	}
28200209230bSgjelinek 
28210209230bSgjelinek 	/*
28220209230bSgjelinek 	 * unqualified fs removal.  remove all fs's but prompt if more
28230209230bSgjelinek 	 * than one.
28240209230bSgjelinek 	 */
28250209230bSgjelinek 	if (!prompt_remove_resource(cmd, "fs"))
28260209230bSgjelinek 		return;
28270209230bSgjelinek 
28280209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "fs")) != Z_OK)
2829bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_FS, err, B_TRUE);
28300209230bSgjelinek 	else
2831bbec428eSgjelinek 		need_to_commit = B_TRUE;
28320209230bSgjelinek }
28330209230bSgjelinek 
28340209230bSgjelinek static void
28350209230bSgjelinek remove_ipd(cmd_t *cmd)
28360209230bSgjelinek {
28370209230bSgjelinek 	int err;
28380209230bSgjelinek 
2839087719fdSdp 	if (state_atleast(ZONE_STATE_INSTALLED)) {
28400209230bSgjelinek 		zerr(gettext("Zone %s already installed; %s %s not allowed."),
28410209230bSgjelinek 		    zone, cmd_to_str(CMD_REMOVE), rt_to_str(RT_IPD));
28427c478bd9Sstevel@tonic-gate 		return;
28437c478bd9Sstevel@tonic-gate 	}
28440209230bSgjelinek 
28450209230bSgjelinek 	/* traditional, qualified ipd removal */
28460209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
28470209230bSgjelinek 		struct zone_fstab fstab;
28480209230bSgjelinek 
2849bbec428eSgjelinek 		if ((err = fill_in_ipdtab(cmd, &fstab, B_FALSE)) != Z_OK) {
2850bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_IPD, err, B_TRUE);
28517c478bd9Sstevel@tonic-gate 			return;
28527c478bd9Sstevel@tonic-gate 		}
28537c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_delete_ipd(handle, &fstab)) != Z_OK)
2854bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_IPD, err, B_TRUE);
28557c478bd9Sstevel@tonic-gate 		else
2856bbec428eSgjelinek 			need_to_commit = B_TRUE;
28577c478bd9Sstevel@tonic-gate 		return;
28580209230bSgjelinek 	}
28590209230bSgjelinek 
28600209230bSgjelinek 	/*
28610209230bSgjelinek 	 * unqualified ipd removal.  remove all ipds but prompt if more
28620209230bSgjelinek 	 * than one.
28630209230bSgjelinek 	 */
28640209230bSgjelinek 	if (!prompt_remove_resource(cmd, "inherit-pkg-dir"))
28650209230bSgjelinek 		return;
28660209230bSgjelinek 
28670209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "inherit-pkg-dir"))
28680209230bSgjelinek 	    != Z_OK)
2869bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_IPD, err, B_TRUE);
28700209230bSgjelinek 	else
2871bbec428eSgjelinek 		need_to_commit = B_TRUE;
28720209230bSgjelinek }
28730209230bSgjelinek 
28740209230bSgjelinek static void
28750209230bSgjelinek remove_net(cmd_t *cmd)
28760209230bSgjelinek {
28770209230bSgjelinek 	int err;
28780209230bSgjelinek 
28790209230bSgjelinek 	/* traditional, qualified net removal */
28800209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
28810209230bSgjelinek 		struct zone_nwiftab nwiftab;
28820209230bSgjelinek 
2883bbec428eSgjelinek 		if ((err = fill_in_nwiftab(cmd, &nwiftab, B_FALSE)) != Z_OK) {
2884bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_NET, err, B_TRUE);
28857c478bd9Sstevel@tonic-gate 			return;
28867c478bd9Sstevel@tonic-gate 		}
28877c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_delete_nwif(handle, &nwiftab)) != Z_OK)
2888bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_NET, err, B_TRUE);
28897c478bd9Sstevel@tonic-gate 		else
2890bbec428eSgjelinek 			need_to_commit = B_TRUE;
28917c478bd9Sstevel@tonic-gate 		return;
28920209230bSgjelinek 	}
28930209230bSgjelinek 
28940209230bSgjelinek 	/*
28950209230bSgjelinek 	 * unqualified net removal.  remove all nets but prompt if more
28960209230bSgjelinek 	 * than one.
28970209230bSgjelinek 	 */
28980209230bSgjelinek 	if (!prompt_remove_resource(cmd, "net"))
28990209230bSgjelinek 		return;
29000209230bSgjelinek 
29010209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "net")) != Z_OK)
2902bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_NET, err, B_TRUE);
29030209230bSgjelinek 	else
2904bbec428eSgjelinek 		need_to_commit = B_TRUE;
29050209230bSgjelinek }
29060209230bSgjelinek 
29070209230bSgjelinek static void
29080209230bSgjelinek remove_device(cmd_t *cmd)
29090209230bSgjelinek {
29100209230bSgjelinek 	int err;
29110209230bSgjelinek 
29120209230bSgjelinek 	/* traditional, qualified device removal */
29130209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
29140209230bSgjelinek 		struct zone_devtab devtab;
29150209230bSgjelinek 
2916bbec428eSgjelinek 		if ((err = fill_in_devtab(cmd, &devtab, B_FALSE)) != Z_OK) {
2917bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_DEVICE, err, B_TRUE);
29187c478bd9Sstevel@tonic-gate 			return;
29197c478bd9Sstevel@tonic-gate 		}
29207c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_delete_dev(handle, &devtab)) != Z_OK)
2921bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_DEVICE, err, B_TRUE);
29227c478bd9Sstevel@tonic-gate 		else
2923bbec428eSgjelinek 			need_to_commit = B_TRUE;
29247c478bd9Sstevel@tonic-gate 		return;
29250209230bSgjelinek 	}
29260209230bSgjelinek 
29270209230bSgjelinek 	/*
29280209230bSgjelinek 	 * unqualified device removal.  remove all devices but prompt if more
29290209230bSgjelinek 	 * than one.
29300209230bSgjelinek 	 */
29310209230bSgjelinek 	if (!prompt_remove_resource(cmd, "device"))
29320209230bSgjelinek 		return;
29330209230bSgjelinek 
29340209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "device")) != Z_OK)
2935bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_DEVICE, err, B_TRUE);
29360209230bSgjelinek 	else
2937bbec428eSgjelinek 		need_to_commit = B_TRUE;
29380209230bSgjelinek }
29390209230bSgjelinek 
29400209230bSgjelinek static void
29410209230bSgjelinek remove_attr(cmd_t *cmd)
29420209230bSgjelinek {
29430209230bSgjelinek 	int err;
29440209230bSgjelinek 
29450209230bSgjelinek 	/* traditional, qualified attr removal */
29460209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
29470209230bSgjelinek 		struct zone_attrtab attrtab;
29480209230bSgjelinek 
2949bbec428eSgjelinek 		if ((err = fill_in_attrtab(cmd, &attrtab, B_FALSE)) != Z_OK) {
2950bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_ATTR, err, B_TRUE);
29510209230bSgjelinek 			return;
29520209230bSgjelinek 		}
29530209230bSgjelinek 		if ((err = zonecfg_delete_attr(handle, &attrtab)) != Z_OK)
2954bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_ATTR, err, B_TRUE);
29550209230bSgjelinek 		else
2956bbec428eSgjelinek 			need_to_commit = B_TRUE;
29570209230bSgjelinek 		return;
29580209230bSgjelinek 	}
29590209230bSgjelinek 
29600209230bSgjelinek 	/*
29610209230bSgjelinek 	 * unqualified attr removal.  remove all attrs but prompt if more
29620209230bSgjelinek 	 * than one.
29630209230bSgjelinek 	 */
29640209230bSgjelinek 	if (!prompt_remove_resource(cmd, "attr"))
29650209230bSgjelinek 		return;
29660209230bSgjelinek 
29670209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "attr")) != Z_OK)
2968bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_ATTR, err, B_TRUE);
29690209230bSgjelinek 	else
2970bbec428eSgjelinek 		need_to_commit = B_TRUE;
29710209230bSgjelinek }
29720209230bSgjelinek 
29730209230bSgjelinek static void
29740209230bSgjelinek remove_dataset(cmd_t *cmd)
29750209230bSgjelinek {
29760209230bSgjelinek 	int err;
29770209230bSgjelinek 
29780209230bSgjelinek 	/* traditional, qualified dataset removal */
29790209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
29800209230bSgjelinek 		struct zone_dstab dstab;
29810209230bSgjelinek 
2982bbec428eSgjelinek 		if ((err = fill_in_dstab(cmd, &dstab, B_FALSE)) != Z_OK) {
2983bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_DATASET, err, B_TRUE);
29840209230bSgjelinek 			return;
29850209230bSgjelinek 		}
29860209230bSgjelinek 		if ((err = zonecfg_delete_ds(handle, &dstab)) != Z_OK)
2987bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_DATASET, err, B_TRUE);
29880209230bSgjelinek 		else
2989bbec428eSgjelinek 			need_to_commit = B_TRUE;
29900209230bSgjelinek 		return;
29910209230bSgjelinek 	}
29920209230bSgjelinek 
29930209230bSgjelinek 	/*
29940209230bSgjelinek 	 * unqualified dataset removal.  remove all datasets but prompt if more
29950209230bSgjelinek 	 * than one.
29960209230bSgjelinek 	 */
29970209230bSgjelinek 	if (!prompt_remove_resource(cmd, "dataset"))
29980209230bSgjelinek 		return;
29990209230bSgjelinek 
30000209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "dataset")) != Z_OK)
3001bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_DATASET, err, B_TRUE);
30020209230bSgjelinek 	else
3003bbec428eSgjelinek 		need_to_commit = B_TRUE;
30040209230bSgjelinek }
30050209230bSgjelinek 
30060209230bSgjelinek static void
30070209230bSgjelinek remove_rctl(cmd_t *cmd)
30080209230bSgjelinek {
30090209230bSgjelinek 	int err;
30100209230bSgjelinek 
30110209230bSgjelinek 	/* traditional, qualified rctl removal */
30120209230bSgjelinek 	if (cmd->cmd_prop_nv_pairs > 0) {
30130209230bSgjelinek 		struct zone_rctltab rctltab;
30140209230bSgjelinek 
3015bbec428eSgjelinek 		if ((err = fill_in_rctltab(cmd, &rctltab, B_FALSE)) != Z_OK) {
3016bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_RCTL, err, B_TRUE);
30177c478bd9Sstevel@tonic-gate 			return;
30187c478bd9Sstevel@tonic-gate 		}
30197c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_delete_rctl(handle, &rctltab)) != Z_OK)
3020bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_RCTL, err, B_TRUE);
30217c478bd9Sstevel@tonic-gate 		else
3022bbec428eSgjelinek 			need_to_commit = B_TRUE;
30237c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
30247c478bd9Sstevel@tonic-gate 		return;
30257c478bd9Sstevel@tonic-gate 	}
30260209230bSgjelinek 
30270209230bSgjelinek 	/*
30280209230bSgjelinek 	 * unqualified rctl removal.  remove all rctls but prompt if more
30290209230bSgjelinek 	 * than one.
30300209230bSgjelinek 	 */
30310209230bSgjelinek 	if (!prompt_remove_resource(cmd, "rctl"))
30320209230bSgjelinek 		return;
30330209230bSgjelinek 
30340209230bSgjelinek 	if ((err = zonecfg_del_all_resources(handle, "rctl")) != Z_OK)
3035bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_RCTL, err, B_TRUE);
30367c478bd9Sstevel@tonic-gate 	else
3037bbec428eSgjelinek 		need_to_commit = B_TRUE;
30380209230bSgjelinek }
30390209230bSgjelinek 
30400209230bSgjelinek static void
30410209230bSgjelinek remove_pset()
30420209230bSgjelinek {
30430209230bSgjelinek 	int err;
30440209230bSgjelinek 	struct zone_psettab psettab;
30450209230bSgjelinek 
30460209230bSgjelinek 	if ((err = zonecfg_lookup_pset(handle, &psettab)) != Z_OK) {
3047bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_DCPU, err, B_TRUE);
30480209230bSgjelinek 		return;
30490209230bSgjelinek 	}
30500209230bSgjelinek 	if ((err = zonecfg_delete_pset(handle)) != Z_OK)
3051bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_DCPU, err, B_TRUE);
30520209230bSgjelinek 	else
3053bbec428eSgjelinek 		need_to_commit = B_TRUE;
30540209230bSgjelinek }
30550209230bSgjelinek 
30560209230bSgjelinek static void
3057c97ad5cdSakolb remove_pcap()
3058c97ad5cdSakolb {
3059c97ad5cdSakolb 	int err;
3060c97ad5cdSakolb 	uint64_t tmp;
3061c97ad5cdSakolb 
3062c97ad5cdSakolb 	if (zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &tmp) != Z_OK) {
3063c97ad5cdSakolb 		zerr("%s %s: %s", cmd_to_str(CMD_REMOVE), rt_to_str(RT_PCAP),
3064c97ad5cdSakolb 		    zonecfg_strerror(Z_NO_RESOURCE_TYPE));
3065bbec428eSgjelinek 		saw_error = B_TRUE;
3066c97ad5cdSakolb 		return;
3067c97ad5cdSakolb 	}
3068c97ad5cdSakolb 
3069c97ad5cdSakolb 	if ((err = zonecfg_rm_aliased_rctl(handle, ALIAS_CPUCAP)) != Z_OK)
3070bbec428eSgjelinek 		z_cmd_rt_perror(CMD_REMOVE, RT_PCAP, err, B_TRUE);
3071c97ad5cdSakolb 	else
3072bbec428eSgjelinek 		need_to_commit = B_TRUE;
3073c97ad5cdSakolb }
3074c97ad5cdSakolb 
3075c97ad5cdSakolb static void
30760209230bSgjelinek remove_mcap()
30770209230bSgjelinek {
30780209230bSgjelinek 	int err, res1, res2, res3;
30790209230bSgjelinek 	uint64_t tmp;
30800209230bSgjelinek 	struct zone_mcaptab mcaptab;
30810209230bSgjelinek 	boolean_t revert = B_FALSE;
30820209230bSgjelinek 
30830209230bSgjelinek 	res1 = zonecfg_lookup_mcap(handle, &mcaptab);
30840209230bSgjelinek 	res2 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP, &tmp);
30850209230bSgjelinek 	res3 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM, &tmp);
30860209230bSgjelinek 
30870209230bSgjelinek 	/* if none of these exist, there is no resource to remove */
30880209230bSgjelinek 	if (res1 != Z_OK && res2 != Z_OK && res3 != Z_OK) {
30890209230bSgjelinek 		zerr("%s %s: %s", cmd_to_str(CMD_REMOVE), rt_to_str(RT_MCAP),
30900209230bSgjelinek 		    zonecfg_strerror(Z_NO_RESOURCE_TYPE));
3091bbec428eSgjelinek 		saw_error = B_TRUE;
30920209230bSgjelinek 		return;
30930209230bSgjelinek 	}
30940209230bSgjelinek 	if (res1 == Z_OK) {
30950209230bSgjelinek 		if ((err = zonecfg_delete_mcap(handle)) != Z_OK) {
3096bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_MCAP, err, B_TRUE);
30970209230bSgjelinek 			revert = B_TRUE;
30980209230bSgjelinek 		} else {
3099bbec428eSgjelinek 			need_to_commit = B_TRUE;
31000209230bSgjelinek 		}
31010209230bSgjelinek 	}
31020209230bSgjelinek 	if (res2 == Z_OK) {
31030209230bSgjelinek 		if ((err = zonecfg_rm_aliased_rctl(handle, ALIAS_MAXSWAP))
31040209230bSgjelinek 		    != Z_OK) {
3105bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_MCAP, err, B_TRUE);
31060209230bSgjelinek 			revert = B_TRUE;
31070209230bSgjelinek 		} else {
3108bbec428eSgjelinek 			need_to_commit = B_TRUE;
31090209230bSgjelinek 		}
31100209230bSgjelinek 	}
31110209230bSgjelinek 	if (res3 == Z_OK) {
31120209230bSgjelinek 		if ((err = zonecfg_rm_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM))
31130209230bSgjelinek 		    != Z_OK) {
3114bbec428eSgjelinek 			z_cmd_rt_perror(CMD_REMOVE, RT_MCAP, err, B_TRUE);
31150209230bSgjelinek 			revert = B_TRUE;
31160209230bSgjelinek 		} else {
3117bbec428eSgjelinek 			need_to_commit = B_TRUE;
31180209230bSgjelinek 		}
31190209230bSgjelinek 	}
31200209230bSgjelinek 
31210209230bSgjelinek 	if (revert)
3122bbec428eSgjelinek 		need_to_commit = B_FALSE;
31230209230bSgjelinek }
31240209230bSgjelinek 
31250209230bSgjelinek static void
31260209230bSgjelinek remove_resource(cmd_t *cmd)
31270209230bSgjelinek {
31280209230bSgjelinek 	int type;
31290209230bSgjelinek 	int arg;
3130bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
31310209230bSgjelinek 
31320209230bSgjelinek 	if ((type = cmd->cmd_res_type) == RT_UNKNOWN) {
3133bbec428eSgjelinek 		long_usage(CMD_REMOVE, B_TRUE);
31340209230bSgjelinek 		return;
31350209230bSgjelinek 	}
31360209230bSgjelinek 
31370209230bSgjelinek 	optind = 0;
31380209230bSgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?F")) != EOF) {
31390209230bSgjelinek 		switch (arg) {
31400209230bSgjelinek 		case '?':
31410209230bSgjelinek 			longer_usage(CMD_REMOVE);
3142bbec428eSgjelinek 			arg_err = B_TRUE;
31437ec75eb8Sgjelinek 			break;
31440209230bSgjelinek 		case 'F':
31450209230bSgjelinek 			break;
31460209230bSgjelinek 		default:
31470209230bSgjelinek 			short_usage(CMD_REMOVE);
3148bbec428eSgjelinek 			arg_err = B_TRUE;
31497ec75eb8Sgjelinek 			break;
31507ec75eb8Sgjelinek 		}
31517ec75eb8Sgjelinek 	}
31527ec75eb8Sgjelinek 	if (arg_err)
31530209230bSgjelinek 		return;
31540209230bSgjelinek 
3155bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
31560209230bSgjelinek 		return;
31570209230bSgjelinek 
31580209230bSgjelinek 	switch (type) {
31590209230bSgjelinek 	case RT_FS:
31600209230bSgjelinek 		remove_fs(cmd);
31610209230bSgjelinek 		return;
31620209230bSgjelinek 	case RT_IPD:
31630209230bSgjelinek 		remove_ipd(cmd);
31640209230bSgjelinek 		return;
31650209230bSgjelinek 	case RT_NET:
31660209230bSgjelinek 		remove_net(cmd);
31670209230bSgjelinek 		return;
31680209230bSgjelinek 	case RT_DEVICE:
31690209230bSgjelinek 		remove_device(cmd);
31700209230bSgjelinek 		return;
31710209230bSgjelinek 	case RT_RCTL:
31720209230bSgjelinek 		remove_rctl(cmd);
31730209230bSgjelinek 		return;
31740209230bSgjelinek 	case RT_ATTR:
31750209230bSgjelinek 		remove_attr(cmd);
31767c478bd9Sstevel@tonic-gate 		return;
3177fa9e4066Sahrens 	case RT_DATASET:
31780209230bSgjelinek 		remove_dataset(cmd);
3179fa9e4066Sahrens 		return;
31800209230bSgjelinek 	case RT_DCPU:
31810209230bSgjelinek 		remove_pset();
31820209230bSgjelinek 		return;
3183c97ad5cdSakolb 	case RT_PCAP:
3184c97ad5cdSakolb 		remove_pcap();
3185c97ad5cdSakolb 		return;
31860209230bSgjelinek 	case RT_MCAP:
31870209230bSgjelinek 		remove_mcap();
3188fa9e4066Sahrens 		return;
31897c478bd9Sstevel@tonic-gate 	default:
3190bbec428eSgjelinek 		zone_perror(rt_to_str(type), Z_NO_RESOURCE_TYPE, B_TRUE);
3191bbec428eSgjelinek 		long_usage(CMD_REMOVE, B_TRUE);
3192bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
31937c478bd9Sstevel@tonic-gate 		return;
31947c478bd9Sstevel@tonic-gate 	}
31957c478bd9Sstevel@tonic-gate }
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate static void
31987c478bd9Sstevel@tonic-gate remove_property(cmd_t *cmd)
31997c478bd9Sstevel@tonic-gate {
32007c478bd9Sstevel@tonic-gate 	char *prop_id;
32017c478bd9Sstevel@tonic-gate 	int err, res_type, prop_type;
32027c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
32037c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *rctlvaltab;
32047c478bd9Sstevel@tonic-gate 	complex_property_ptr_t cx;
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate 	res_type = resource_scope;
32077c478bd9Sstevel@tonic-gate 	prop_type = cmd->cmd_prop_name[0];
32087c478bd9Sstevel@tonic-gate 	if (res_type == RT_UNKNOWN || prop_type == PT_UNKNOWN) {
3209bbec428eSgjelinek 		long_usage(CMD_REMOVE, B_TRUE);
32107c478bd9Sstevel@tonic-gate 		return;
32117c478bd9Sstevel@tonic-gate 	}
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	if (cmd->cmd_prop_nv_pairs != 1) {
3214bbec428eSgjelinek 		long_usage(CMD_ADD, B_TRUE);
32157c478bd9Sstevel@tonic-gate 		return;
32167c478bd9Sstevel@tonic-gate 	}
32177c478bd9Sstevel@tonic-gate 
3218bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
32197c478bd9Sstevel@tonic-gate 		return;
32207c478bd9Sstevel@tonic-gate 
32217c478bd9Sstevel@tonic-gate 	switch (res_type) {
32227c478bd9Sstevel@tonic-gate 	case RT_FS:
32237c478bd9Sstevel@tonic-gate 		if (prop_type != PT_OPTIONS) {
32247c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
3225bbec428eSgjelinek 			    B_TRUE);
3226bbec428eSgjelinek 			long_usage(CMD_REMOVE, B_TRUE);
3227bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
32287c478bd9Sstevel@tonic-gate 			return;
32297c478bd9Sstevel@tonic-gate 		}
32307c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[0];
32317c478bd9Sstevel@tonic-gate 		if (pp->pv_type == PROP_VAL_COMPLEX) {
32327c478bd9Sstevel@tonic-gate 			zerr(gettext("A %s or %s value was expected here."),
32337c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_SIMPLE),
32347c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_LIST));
3235bbec428eSgjelinek 			saw_error = B_TRUE;
32367c478bd9Sstevel@tonic-gate 			return;
32377c478bd9Sstevel@tonic-gate 		}
32387c478bd9Sstevel@tonic-gate 		if (pp->pv_type == PROP_VAL_SIMPLE) {
32397c478bd9Sstevel@tonic-gate 			if (pp->pv_simple == NULL) {
3240bbec428eSgjelinek 				long_usage(CMD_ADD, B_TRUE);
32417c478bd9Sstevel@tonic-gate 				return;
32427c478bd9Sstevel@tonic-gate 			}
32437c478bd9Sstevel@tonic-gate 			prop_id = pp->pv_simple;
32447c478bd9Sstevel@tonic-gate 			err = zonecfg_remove_fs_option(&in_progress_fstab,
32457c478bd9Sstevel@tonic-gate 			    prop_id);
32467c478bd9Sstevel@tonic-gate 			if (err != Z_OK)
3247bbec428eSgjelinek 				zone_perror(pt_to_str(prop_type), err, B_TRUE);
32487c478bd9Sstevel@tonic-gate 		} else {
32497c478bd9Sstevel@tonic-gate 			list_property_ptr_t list;
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 			for (list = pp->pv_list; list != NULL;
32527c478bd9Sstevel@tonic-gate 			    list = list->lp_next) {
32537c478bd9Sstevel@tonic-gate 				prop_id = list->lp_simple;
32547c478bd9Sstevel@tonic-gate 				if (prop_id == NULL)
32557c478bd9Sstevel@tonic-gate 					break;
32567c478bd9Sstevel@tonic-gate 				err = zonecfg_remove_fs_option(
32577c478bd9Sstevel@tonic-gate 				    &in_progress_fstab, prop_id);
32587c478bd9Sstevel@tonic-gate 				if (err != Z_OK)
32597c478bd9Sstevel@tonic-gate 					zone_perror(pt_to_str(prop_type), err,
3260bbec428eSgjelinek 					    B_TRUE);
32617c478bd9Sstevel@tonic-gate 			}
32627c478bd9Sstevel@tonic-gate 		}
32637c478bd9Sstevel@tonic-gate 		return;
32647c478bd9Sstevel@tonic-gate 	case RT_RCTL:
32657c478bd9Sstevel@tonic-gate 		if (prop_type != PT_VALUE) {
32667c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
3267bbec428eSgjelinek 			    B_TRUE);
3268bbec428eSgjelinek 			long_usage(CMD_REMOVE, B_TRUE);
3269bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
32707c478bd9Sstevel@tonic-gate 			return;
32717c478bd9Sstevel@tonic-gate 		}
32727c478bd9Sstevel@tonic-gate 		pp = cmd->cmd_property_ptr[0];
32737c478bd9Sstevel@tonic-gate 		if (pp->pv_type != PROP_VAL_COMPLEX) {
32747c478bd9Sstevel@tonic-gate 			zerr(gettext("A %s value was expected here."),
32757c478bd9Sstevel@tonic-gate 			    pvt_to_str(PROP_VAL_COMPLEX));
3276bbec428eSgjelinek 			saw_error = B_TRUE;
32777c478bd9Sstevel@tonic-gate 			return;
32787c478bd9Sstevel@tonic-gate 		}
32797c478bd9Sstevel@tonic-gate 		if ((rctlvaltab = alloc_rctlvaltab()) == NULL) {
3280bbec428eSgjelinek 			zone_perror(zone, Z_NOMEM, B_TRUE);
32817c478bd9Sstevel@tonic-gate 			exit(Z_ERR);
32827c478bd9Sstevel@tonic-gate 		}
32837c478bd9Sstevel@tonic-gate 		for (cx = pp->pv_complex; cx != NULL; cx = cx->cp_next) {
32847c478bd9Sstevel@tonic-gate 			switch (cx->cp_type) {
32857c478bd9Sstevel@tonic-gate 			case PT_PRIV:
32867c478bd9Sstevel@tonic-gate 				(void) strlcpy(rctlvaltab->zone_rctlval_priv,
32877c478bd9Sstevel@tonic-gate 				    cx->cp_value,
32887c478bd9Sstevel@tonic-gate 				    sizeof (rctlvaltab->zone_rctlval_priv));
32897c478bd9Sstevel@tonic-gate 				break;
32907c478bd9Sstevel@tonic-gate 			case PT_LIMIT:
32917c478bd9Sstevel@tonic-gate 				(void) strlcpy(rctlvaltab->zone_rctlval_limit,
32927c478bd9Sstevel@tonic-gate 				    cx->cp_value,
32937c478bd9Sstevel@tonic-gate 				    sizeof (rctlvaltab->zone_rctlval_limit));
32947c478bd9Sstevel@tonic-gate 				break;
32957c478bd9Sstevel@tonic-gate 			case PT_ACTION:
32967c478bd9Sstevel@tonic-gate 				(void) strlcpy(rctlvaltab->zone_rctlval_action,
32977c478bd9Sstevel@tonic-gate 				    cx->cp_value,
32987c478bd9Sstevel@tonic-gate 				    sizeof (rctlvaltab->zone_rctlval_action));
32997c478bd9Sstevel@tonic-gate 				break;
33007c478bd9Sstevel@tonic-gate 			default:
33017c478bd9Sstevel@tonic-gate 				zone_perror(pt_to_str(prop_type),
3302bbec428eSgjelinek 				    Z_NO_PROPERTY_TYPE, B_TRUE);
3303bbec428eSgjelinek 				long_usage(CMD_ADD, B_TRUE);
3304bbec428eSgjelinek 				usage(B_FALSE, HELP_PROPS);
33057c478bd9Sstevel@tonic-gate 				zonecfg_free_rctl_value_list(rctlvaltab);
33067c478bd9Sstevel@tonic-gate 				return;
33077c478bd9Sstevel@tonic-gate 			}
33087c478bd9Sstevel@tonic-gate 		}
33097c478bd9Sstevel@tonic-gate 		rctlvaltab->zone_rctlval_next = NULL;
33107c478bd9Sstevel@tonic-gate 		err = zonecfg_remove_rctl_value(&in_progress_rctltab,
33117c478bd9Sstevel@tonic-gate 		    rctlvaltab);
33127c478bd9Sstevel@tonic-gate 		if (err != Z_OK)
3313bbec428eSgjelinek 			zone_perror(pt_to_str(prop_type), err, B_TRUE);
33147c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctlvaltab);
33157c478bd9Sstevel@tonic-gate 		return;
3316de860bd9Sgfaden 	case RT_NET:
3317de860bd9Sgfaden 		if (prop_type != PT_DEFROUTER) {
3318de860bd9Sgfaden 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
3319bbec428eSgjelinek 			    B_TRUE);
3320bbec428eSgjelinek 			long_usage(CMD_REMOVE, B_TRUE);
3321bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
3322de860bd9Sgfaden 			return;
3323de860bd9Sgfaden 		} else {
3324de860bd9Sgfaden 			bzero(&in_progress_nwiftab.zone_nwif_defrouter,
3325de860bd9Sgfaden 			    sizeof (in_progress_nwiftab.zone_nwif_defrouter));
3326de860bd9Sgfaden 			return;
3327de860bd9Sgfaden 		}
33287c478bd9Sstevel@tonic-gate 	default:
3329bbec428eSgjelinek 		zone_perror(rt_to_str(res_type), Z_NO_RESOURCE_TYPE, B_TRUE);
3330bbec428eSgjelinek 		long_usage(CMD_REMOVE, B_TRUE);
3331bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
33327c478bd9Sstevel@tonic-gate 		return;
33337c478bd9Sstevel@tonic-gate 	}
33347c478bd9Sstevel@tonic-gate }
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate void
33377c478bd9Sstevel@tonic-gate remove_func(cmd_t *cmd)
33387c478bd9Sstevel@tonic-gate {
33397c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_REMOVE))
33407c478bd9Sstevel@tonic-gate 		return;
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
33437c478bd9Sstevel@tonic-gate 
33440209230bSgjelinek 	if (global_scope) {
33450209230bSgjelinek 		if (gz_invalid_resource(cmd->cmd_res_type)) {
33460209230bSgjelinek 			zerr(gettext("%s is not a valid resource for the "
33470209230bSgjelinek 			    "global zone."), rt_to_str(cmd->cmd_res_type));
3348bbec428eSgjelinek 			saw_error = B_TRUE;
33490209230bSgjelinek 			return;
33500209230bSgjelinek 		}
33517c478bd9Sstevel@tonic-gate 		remove_resource(cmd);
33520209230bSgjelinek 	} else {
33537c478bd9Sstevel@tonic-gate 		remove_property(cmd);
33547c478bd9Sstevel@tonic-gate 	}
33550209230bSgjelinek }
33560209230bSgjelinek 
33570209230bSgjelinek static void
33580209230bSgjelinek clear_property(cmd_t *cmd)
33590209230bSgjelinek {
33600209230bSgjelinek 	int res_type, prop_type;
33610209230bSgjelinek 
33620209230bSgjelinek 	res_type = resource_scope;
33630209230bSgjelinek 	prop_type = cmd->cmd_res_type;
33640209230bSgjelinek 	if (res_type == RT_UNKNOWN || prop_type == PT_UNKNOWN) {
3365bbec428eSgjelinek 		long_usage(CMD_CLEAR, B_TRUE);
33660209230bSgjelinek 		return;
33670209230bSgjelinek 	}
33680209230bSgjelinek 
3369bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
33700209230bSgjelinek 		return;
33710209230bSgjelinek 
33720209230bSgjelinek 	switch (res_type) {
33730209230bSgjelinek 	case RT_FS:
33740209230bSgjelinek 		if (prop_type == PT_RAW) {
33750209230bSgjelinek 			in_progress_fstab.zone_fs_raw[0] = '\0';
3376bbec428eSgjelinek 			need_to_commit = B_TRUE;
33770209230bSgjelinek 			return;
33780209230bSgjelinek 		}
33790209230bSgjelinek 		break;
33800209230bSgjelinek 	case RT_DCPU:
33810209230bSgjelinek 		if (prop_type == PT_IMPORTANCE) {
33820209230bSgjelinek 			in_progress_psettab.zone_importance[0] = '\0';
3383bbec428eSgjelinek 			need_to_commit = B_TRUE;
33840209230bSgjelinek 			return;
33850209230bSgjelinek 		}
33860209230bSgjelinek 		break;
33870209230bSgjelinek 	case RT_MCAP:
33880209230bSgjelinek 		switch (prop_type) {
33890209230bSgjelinek 		case PT_PHYSICAL:
33900209230bSgjelinek 			in_progress_mcaptab.zone_physmem_cap[0] = '\0';
3391bbec428eSgjelinek 			need_to_commit = B_TRUE;
33920209230bSgjelinek 			return;
33930209230bSgjelinek 		case PT_SWAP:
33940209230bSgjelinek 			remove_aliased_rctl(PT_SWAP, ALIAS_MAXSWAP);
33950209230bSgjelinek 			return;
33960209230bSgjelinek 		case PT_LOCKED:
33970209230bSgjelinek 			remove_aliased_rctl(PT_LOCKED, ALIAS_MAXLOCKEDMEM);
33980209230bSgjelinek 			return;
33990209230bSgjelinek 		}
34000209230bSgjelinek 		break;
34010209230bSgjelinek 	default:
34020209230bSgjelinek 		break;
34030209230bSgjelinek 	}
34040209230bSgjelinek 
3405bbec428eSgjelinek 	zone_perror(pt_to_str(prop_type), Z_CLEAR_DISALLOW, B_TRUE);
34060209230bSgjelinek }
34070209230bSgjelinek 
34080209230bSgjelinek static void
34090209230bSgjelinek clear_global(cmd_t *cmd)
34100209230bSgjelinek {
34110209230bSgjelinek 	int err, type;
34120209230bSgjelinek 
34130209230bSgjelinek 	if ((type = cmd->cmd_res_type) == RT_UNKNOWN) {
3414bbec428eSgjelinek 		long_usage(CMD_CLEAR, B_TRUE);
34150209230bSgjelinek 		return;
34160209230bSgjelinek 	}
34170209230bSgjelinek 
3418bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
34190209230bSgjelinek 		return;
34200209230bSgjelinek 
34210209230bSgjelinek 	switch (type) {
34220209230bSgjelinek 	case PT_ZONENAME:
34230209230bSgjelinek 		/* FALLTHRU */
34240209230bSgjelinek 	case PT_ZONEPATH:
34250209230bSgjelinek 		/* FALLTHRU */
34260209230bSgjelinek 	case PT_BRAND:
3427bbec428eSgjelinek 		zone_perror(pt_to_str(type), Z_CLEAR_DISALLOW, B_TRUE);
34280209230bSgjelinek 		return;
34290209230bSgjelinek 	case PT_AUTOBOOT:
34300209230bSgjelinek 		/* false is default; we'll treat as equivalent to clearing */
34310209230bSgjelinek 		if ((err = zonecfg_set_autoboot(handle, B_FALSE)) != Z_OK)
3432bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_AUTOBOOT, err, B_TRUE);
34330209230bSgjelinek 		else
3434bbec428eSgjelinek 			need_to_commit = B_TRUE;
34350209230bSgjelinek 		return;
34360209230bSgjelinek 	case PT_POOL:
34370209230bSgjelinek 		if ((err = zonecfg_set_pool(handle, NULL)) != Z_OK)
3438bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_POOL, err, B_TRUE);
34390209230bSgjelinek 		else
3440bbec428eSgjelinek 			need_to_commit = B_TRUE;
34410209230bSgjelinek 		return;
34420209230bSgjelinek 	case PT_LIMITPRIV:
34430209230bSgjelinek 		if ((err = zonecfg_set_limitpriv(handle, NULL)) != Z_OK)
3444bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_LIMITPRIV, err, B_TRUE);
34450209230bSgjelinek 		else
3446bbec428eSgjelinek 			need_to_commit = B_TRUE;
34470209230bSgjelinek 		return;
34480209230bSgjelinek 	case PT_BOOTARGS:
34490209230bSgjelinek 		if ((err = zonecfg_set_bootargs(handle, NULL)) != Z_OK)
3450bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_BOOTARGS, err, B_TRUE);
34510209230bSgjelinek 		else
3452bbec428eSgjelinek 			need_to_commit = B_TRUE;
34530209230bSgjelinek 		return;
34540209230bSgjelinek 	case PT_SCHED:
34550209230bSgjelinek 		if ((err = zonecfg_set_sched(handle, NULL)) != Z_OK)
3456bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_SCHED, err, B_TRUE);
34570209230bSgjelinek 		else
3458bbec428eSgjelinek 			need_to_commit = B_TRUE;
34590209230bSgjelinek 		return;
3460f4b3ec61Sdh155122 	case PT_IPTYPE:
3461f4b3ec61Sdh155122 		/* shared is default; we'll treat as equivalent to clearing */
3462f4b3ec61Sdh155122 		if ((err = zonecfg_set_iptype(handle, ZS_SHARED)) != Z_OK)
3463bbec428eSgjelinek 			z_cmd_rt_perror(CMD_CLEAR, RT_IPTYPE, err, B_TRUE);
3464f4b3ec61Sdh155122 		else
3465bbec428eSgjelinek 			need_to_commit = B_TRUE;
3466f4b3ec61Sdh155122 		return;
34670209230bSgjelinek 	case PT_MAXLWPS:
34680209230bSgjelinek 		remove_aliased_rctl(PT_MAXLWPS, ALIAS_MAXLWPS);
34690209230bSgjelinek 		return;
34700209230bSgjelinek 	case PT_MAXSHMMEM:
34710209230bSgjelinek 		remove_aliased_rctl(PT_MAXSHMMEM, ALIAS_MAXSHMMEM);
34720209230bSgjelinek 		return;
34730209230bSgjelinek 	case PT_MAXSHMIDS:
34740209230bSgjelinek 		remove_aliased_rctl(PT_MAXSHMIDS, ALIAS_MAXSHMIDS);
34750209230bSgjelinek 		return;
34760209230bSgjelinek 	case PT_MAXMSGIDS:
34770209230bSgjelinek 		remove_aliased_rctl(PT_MAXMSGIDS, ALIAS_MAXMSGIDS);
34780209230bSgjelinek 		return;
34790209230bSgjelinek 	case PT_MAXSEMIDS:
34800209230bSgjelinek 		remove_aliased_rctl(PT_MAXSEMIDS, ALIAS_MAXSEMIDS);
34810209230bSgjelinek 		return;
34820209230bSgjelinek 	case PT_SHARES:
34830209230bSgjelinek 		remove_aliased_rctl(PT_SHARES, ALIAS_SHARES);
34840209230bSgjelinek 		return;
34850209230bSgjelinek 	default:
3486bbec428eSgjelinek 		zone_perror(pt_to_str(type), Z_NO_PROPERTY_TYPE, B_TRUE);
3487bbec428eSgjelinek 		long_usage(CMD_CLEAR, B_TRUE);
3488bbec428eSgjelinek 		usage(B_FALSE, HELP_PROPS);
34890209230bSgjelinek 		return;
34900209230bSgjelinek 	}
34910209230bSgjelinek }
34920209230bSgjelinek 
34930209230bSgjelinek void
34940209230bSgjelinek clear_func(cmd_t *cmd)
34950209230bSgjelinek {
34960209230bSgjelinek 	if (zone_is_read_only(CMD_CLEAR))
34970209230bSgjelinek 		return;
34980209230bSgjelinek 
34990209230bSgjelinek 	assert(cmd != NULL);
35000209230bSgjelinek 
35010209230bSgjelinek 	if (global_scope) {
35020209230bSgjelinek 		if (gz_invalid_property(cmd->cmd_res_type)) {
35030209230bSgjelinek 			zerr(gettext("%s is not a valid property for the "
35040209230bSgjelinek 			    "global zone."), pt_to_str(cmd->cmd_res_type));
3505bbec428eSgjelinek 			saw_error = B_TRUE;
35060209230bSgjelinek 			return;
35070209230bSgjelinek 		}
35080209230bSgjelinek 
35090209230bSgjelinek 		clear_global(cmd);
35100209230bSgjelinek 	} else {
35110209230bSgjelinek 		clear_property(cmd);
35120209230bSgjelinek 	}
35130209230bSgjelinek }
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate void
35167c478bd9Sstevel@tonic-gate select_func(cmd_t *cmd)
35177c478bd9Sstevel@tonic-gate {
35180209230bSgjelinek 	int type, err, res;
35190209230bSgjelinek 	uint64_t limit;
3520c97ad5cdSakolb 	uint64_t tmp;
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_SELECT))
35237c478bd9Sstevel@tonic-gate 		return;
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 	if (global_scope) {
3528bbec428eSgjelinek 		global_scope = B_FALSE;
35297c478bd9Sstevel@tonic-gate 		resource_scope = cmd->cmd_res_type;
35307c478bd9Sstevel@tonic-gate 		end_op = CMD_SELECT;
35317c478bd9Sstevel@tonic-gate 	} else {
35327c478bd9Sstevel@tonic-gate 		scope_usage(CMD_SELECT);
35337c478bd9Sstevel@tonic-gate 		return;
35347c478bd9Sstevel@tonic-gate 	}
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate 	if ((type = cmd->cmd_res_type) == RT_UNKNOWN) {
3537bbec428eSgjelinek 		long_usage(CMD_SELECT, B_TRUE);
35387c478bd9Sstevel@tonic-gate 		return;
35397c478bd9Sstevel@tonic-gate 	}
35407c478bd9Sstevel@tonic-gate 
3541bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
35427c478bd9Sstevel@tonic-gate 		return;
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate 	switch (type) {
35457c478bd9Sstevel@tonic-gate 	case RT_FS:
3546bbec428eSgjelinek 		if ((err = fill_in_fstab(cmd, &old_fstab, B_FALSE)) != Z_OK) {
3547bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_FS, err, B_TRUE);
3548bbec428eSgjelinek 			global_scope = B_TRUE;
35497c478bd9Sstevel@tonic-gate 		}
35507c478bd9Sstevel@tonic-gate 		bcopy(&old_fstab, &in_progress_fstab,
35517c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_fstab));
35527c478bd9Sstevel@tonic-gate 		return;
35537c478bd9Sstevel@tonic-gate 	case RT_IPD:
3554087719fdSdp 		if (state_atleast(ZONE_STATE_INCOMPLETE)) {
35557c478bd9Sstevel@tonic-gate 			zerr(gettext("Zone %s not in %s state; %s %s not "
35567c478bd9Sstevel@tonic-gate 			    "allowed."), zone,
35577c478bd9Sstevel@tonic-gate 			    zone_state_str(ZONE_STATE_CONFIGURED),
35587c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_SELECT), rt_to_str(RT_IPD));
3559bbec428eSgjelinek 			global_scope = B_TRUE;
35607c478bd9Sstevel@tonic-gate 			end_op = -1;
35617c478bd9Sstevel@tonic-gate 			return;
35627c478bd9Sstevel@tonic-gate 		}
3563bbec428eSgjelinek 		if ((err = fill_in_ipdtab(cmd, &old_ipdtab, B_FALSE)) != Z_OK) {
3564bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_IPD, err, B_TRUE);
3565bbec428eSgjelinek 			global_scope = B_TRUE;
35667c478bd9Sstevel@tonic-gate 		}
35677c478bd9Sstevel@tonic-gate 		bcopy(&old_ipdtab, &in_progress_ipdtab,
35687c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_fstab));
35697c478bd9Sstevel@tonic-gate 		return;
35707c478bd9Sstevel@tonic-gate 	case RT_NET:
3571bbec428eSgjelinek 		if ((err = fill_in_nwiftab(cmd, &old_nwiftab, B_FALSE))
3572bbec428eSgjelinek 		    != Z_OK) {
3573bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_NET, err, B_TRUE);
3574bbec428eSgjelinek 			global_scope = B_TRUE;
35757c478bd9Sstevel@tonic-gate 		}
35767c478bd9Sstevel@tonic-gate 		bcopy(&old_nwiftab, &in_progress_nwiftab,
35777c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_nwiftab));
35787c478bd9Sstevel@tonic-gate 		return;
35797c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
3580bbec428eSgjelinek 		if ((err = fill_in_devtab(cmd, &old_devtab, B_FALSE)) != Z_OK) {
3581bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_DEVICE, err, B_TRUE);
3582bbec428eSgjelinek 			global_scope = B_TRUE;
35837c478bd9Sstevel@tonic-gate 		}
35847c478bd9Sstevel@tonic-gate 		bcopy(&old_devtab, &in_progress_devtab,
35857c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_devtab));
35867c478bd9Sstevel@tonic-gate 		return;
35877c478bd9Sstevel@tonic-gate 	case RT_RCTL:
3588bbec428eSgjelinek 		if ((err = fill_in_rctltab(cmd, &old_rctltab, B_FALSE))
3589bbec428eSgjelinek 		    != Z_OK) {
3590bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_RCTL, err, B_TRUE);
3591bbec428eSgjelinek 			global_scope = B_TRUE;
35927c478bd9Sstevel@tonic-gate 		}
35937c478bd9Sstevel@tonic-gate 		bcopy(&old_rctltab, &in_progress_rctltab,
35947c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_rctltab));
35957c478bd9Sstevel@tonic-gate 		return;
35967c478bd9Sstevel@tonic-gate 	case RT_ATTR:
3597bbec428eSgjelinek 		if ((err = fill_in_attrtab(cmd, &old_attrtab, B_FALSE))
3598bbec428eSgjelinek 		    != Z_OK) {
3599bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_ATTR, err, B_TRUE);
3600bbec428eSgjelinek 			global_scope = B_TRUE;
36017c478bd9Sstevel@tonic-gate 		}
36027c478bd9Sstevel@tonic-gate 		bcopy(&old_attrtab, &in_progress_attrtab,
36037c478bd9Sstevel@tonic-gate 		    sizeof (struct zone_attrtab));
36047c478bd9Sstevel@tonic-gate 		return;
3605fa9e4066Sahrens 	case RT_DATASET:
3606bbec428eSgjelinek 		if ((err = fill_in_dstab(cmd, &old_dstab, B_FALSE)) != Z_OK) {
3607bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_DATASET, err, B_TRUE);
3608bbec428eSgjelinek 			global_scope = B_TRUE;
3609fa9e4066Sahrens 		}
3610fa9e4066Sahrens 		bcopy(&old_dstab, &in_progress_dstab,
3611fa9e4066Sahrens 		    sizeof (struct zone_dstab));
3612fa9e4066Sahrens 		return;
36130209230bSgjelinek 	case RT_DCPU:
36140209230bSgjelinek 		if ((err = zonecfg_lookup_pset(handle, &old_psettab)) != Z_OK) {
3615bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_DCPU, err, B_TRUE);
3616bbec428eSgjelinek 			global_scope = B_TRUE;
36170209230bSgjelinek 		}
36180209230bSgjelinek 		bcopy(&old_psettab, &in_progress_psettab,
36190209230bSgjelinek 		    sizeof (struct zone_psettab));
36200209230bSgjelinek 		return;
3621c97ad5cdSakolb 	case RT_PCAP:
3622c97ad5cdSakolb 		if ((err = zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &tmp))
3623c97ad5cdSakolb 		    != Z_OK) {
3624bbec428eSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_PCAP, err, B_TRUE);
3625bbec428eSgjelinek 			global_scope = B_TRUE;
3626c97ad5cdSakolb 		}
3627c97ad5cdSakolb 		return;
36280209230bSgjelinek 	case RT_MCAP:
36290209230bSgjelinek 		/* if none of these exist, there is no resource to select */
36300209230bSgjelinek 		if ((res = zonecfg_lookup_mcap(handle, &old_mcaptab)) != Z_OK &&
36310209230bSgjelinek 		    zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP, &limit)
36320209230bSgjelinek 		    != Z_OK &&
36330209230bSgjelinek 		    zonecfg_get_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM, &limit)
36340209230bSgjelinek 		    != Z_OK) {
36350209230bSgjelinek 			z_cmd_rt_perror(CMD_SELECT, RT_MCAP, Z_NO_RESOURCE_TYPE,
3636bbec428eSgjelinek 			    B_TRUE);
3637bbec428eSgjelinek 			global_scope = B_TRUE;
36380209230bSgjelinek 		}
36390209230bSgjelinek 		if (res == Z_OK)
36400209230bSgjelinek 			bcopy(&old_mcaptab, &in_progress_mcaptab,
36410209230bSgjelinek 			    sizeof (struct zone_mcaptab));
36420209230bSgjelinek 		else
36430209230bSgjelinek 			bzero(&in_progress_mcaptab,
36440209230bSgjelinek 			    sizeof (in_progress_mcaptab));
36450209230bSgjelinek 		return;
36467c478bd9Sstevel@tonic-gate 	default:
3647bbec428eSgjelinek 		zone_perror(rt_to_str(type), Z_NO_RESOURCE_TYPE, B_TRUE);
3648bbec428eSgjelinek 		long_usage(CMD_SELECT, B_TRUE);
3649bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
36507c478bd9Sstevel@tonic-gate 		return;
36517c478bd9Sstevel@tonic-gate 	}
36527c478bd9Sstevel@tonic-gate }
36537c478bd9Sstevel@tonic-gate 
36547c478bd9Sstevel@tonic-gate /*
36557c478bd9Sstevel@tonic-gate  * Network "addresses" can be one of the following forms:
36567c478bd9Sstevel@tonic-gate  *	<IPv4 address>
36577c478bd9Sstevel@tonic-gate  *	<IPv4 address>/<prefix length>
36587c478bd9Sstevel@tonic-gate  *	<IPv6 address>/<prefix length>
36597c478bd9Sstevel@tonic-gate  *	<host name>
36607c478bd9Sstevel@tonic-gate  *	<host name>/<prefix length>
36617c478bd9Sstevel@tonic-gate  * In other words, the "/" followed by a prefix length is allowed but not
36627c478bd9Sstevel@tonic-gate  * required for IPv4 addresses and host names, and required for IPv6 addresses.
36637c478bd9Sstevel@tonic-gate  * If a prefix length is given, it must be in the allowable range: 0 to 32 for
36647c478bd9Sstevel@tonic-gate  * IPv4 addresses and host names, 0 to 128 for IPv6 addresses.
36657c478bd9Sstevel@tonic-gate  * Host names must start with an alpha-numeric character, and all subsequent
36667c478bd9Sstevel@tonic-gate  * characters must be either alpha-numeric or "-".
36677c478bd9Sstevel@tonic-gate  */
36687c478bd9Sstevel@tonic-gate 
36697c478bd9Sstevel@tonic-gate static int
36707c478bd9Sstevel@tonic-gate validate_net_address_syntax(char *address)
36717c478bd9Sstevel@tonic-gate {
36727c478bd9Sstevel@tonic-gate 	char *slashp, part1[MAXHOSTNAMELEN];
36737c478bd9Sstevel@tonic-gate 	struct in6_addr in6;
36747c478bd9Sstevel@tonic-gate 	struct in_addr in4;
36757c478bd9Sstevel@tonic-gate 	int prefixlen, i;
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate 	/*
36787c478bd9Sstevel@tonic-gate 	 * Copy the part before any '/' into part1 or copy the whole
36797c478bd9Sstevel@tonic-gate 	 * thing if there is no '/'.
36807c478bd9Sstevel@tonic-gate 	 */
36817c478bd9Sstevel@tonic-gate 	if ((slashp = strchr(address, '/')) != NULL) {
36827c478bd9Sstevel@tonic-gate 		*slashp = '\0';
36837c478bd9Sstevel@tonic-gate 		(void) strlcpy(part1, address, sizeof (part1));
36847c478bd9Sstevel@tonic-gate 		*slashp = '/';
36857c478bd9Sstevel@tonic-gate 		prefixlen = atoi(++slashp);
36867c478bd9Sstevel@tonic-gate 	} else {
36877c478bd9Sstevel@tonic-gate 		(void) strlcpy(part1, address, sizeof (part1));
36887c478bd9Sstevel@tonic-gate 	}
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET6, part1, &in6) == 1) {
36917c478bd9Sstevel@tonic-gate 		if (slashp == NULL) {
36927c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: IPv6 addresses "
36937c478bd9Sstevel@tonic-gate 			    "require /prefix-length suffix."), address);
36947c478bd9Sstevel@tonic-gate 			return (Z_ERR);
36957c478bd9Sstevel@tonic-gate 		}
36967c478bd9Sstevel@tonic-gate 		if (prefixlen < 0 || prefixlen > 128) {
36977c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: IPv6 address "
36987c478bd9Sstevel@tonic-gate 			    "prefix lengths must be 0 - 128."), address);
36997c478bd9Sstevel@tonic-gate 			return (Z_ERR);
37007c478bd9Sstevel@tonic-gate 		}
37017c478bd9Sstevel@tonic-gate 		return (Z_OK);
37027c478bd9Sstevel@tonic-gate 	}
37037c478bd9Sstevel@tonic-gate 
37047c478bd9Sstevel@tonic-gate 	/* At this point, any /prefix must be for IPv4. */
37057c478bd9Sstevel@tonic-gate 	if (slashp != NULL) {
37067c478bd9Sstevel@tonic-gate 		if (prefixlen < 0 || prefixlen > 32) {
37077c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: IPv4 address "
37087c478bd9Sstevel@tonic-gate 			    "prefix lengths must be 0 - 32."), address);
37097c478bd9Sstevel@tonic-gate 			return (Z_ERR);
37107c478bd9Sstevel@tonic-gate 		}
37117c478bd9Sstevel@tonic-gate 	}
37127c478bd9Sstevel@tonic-gate 	if (inet_pton(AF_INET, part1, &in4) == 1)
37137c478bd9Sstevel@tonic-gate 		return (Z_OK);
37147c478bd9Sstevel@tonic-gate 
37157c478bd9Sstevel@tonic-gate 	/* address may also be a host name */
37167c478bd9Sstevel@tonic-gate 	if (!isalnum(part1[0])) {
37177c478bd9Sstevel@tonic-gate 		zerr(gettext("%s: bogus host name or network address syntax"),
37187c478bd9Sstevel@tonic-gate 		    part1);
3719bbec428eSgjelinek 		saw_error = B_TRUE;
3720bbec428eSgjelinek 		usage(B_FALSE, HELP_NETADDR);
37217c478bd9Sstevel@tonic-gate 		return (Z_ERR);
37227c478bd9Sstevel@tonic-gate 	}
37237c478bd9Sstevel@tonic-gate 	for (i = 1; part1[i]; i++)
37247c478bd9Sstevel@tonic-gate 		if (!isalnum(part1[i]) && part1[i] != '-' && part1[i] != '.') {
37257c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: bogus host name or "
37267c478bd9Sstevel@tonic-gate 			    "network address syntax"), part1);
3727bbec428eSgjelinek 			saw_error = B_TRUE;
3728bbec428eSgjelinek 			usage(B_FALSE, HELP_NETADDR);
37297c478bd9Sstevel@tonic-gate 			return (Z_ERR);
37307c478bd9Sstevel@tonic-gate 		}
37317c478bd9Sstevel@tonic-gate 	return (Z_OK);
37327c478bd9Sstevel@tonic-gate }
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate static int
37357c478bd9Sstevel@tonic-gate validate_net_physical_syntax(char *ifname)
37367c478bd9Sstevel@tonic-gate {
37377c478bd9Sstevel@tonic-gate 	if (strchr(ifname, ':') == NULL)
37387c478bd9Sstevel@tonic-gate 		return (Z_OK);
37397c478bd9Sstevel@tonic-gate 	zerr(gettext("%s: physical interface name required; "
37407c478bd9Sstevel@tonic-gate 	    "logical interface name not allowed"), ifname);
37417c478bd9Sstevel@tonic-gate 	return (Z_ERR);
37427c478bd9Sstevel@tonic-gate }
37437c478bd9Sstevel@tonic-gate 
37447c478bd9Sstevel@tonic-gate static boolean_t
37457c478bd9Sstevel@tonic-gate valid_fs_type(const char *type)
37467c478bd9Sstevel@tonic-gate {
37477c478bd9Sstevel@tonic-gate 	/*
37487c478bd9Sstevel@tonic-gate 	 * Is this a valid path component?
37497c478bd9Sstevel@tonic-gate 	 */
37507c478bd9Sstevel@tonic-gate 	if (strlen(type) + 1 > MAXNAMELEN)
37517c478bd9Sstevel@tonic-gate 		return (B_FALSE);
37527c478bd9Sstevel@tonic-gate 	/*
37537c478bd9Sstevel@tonic-gate 	 * Make sure a bad value for "type" doesn't make
37547c478bd9Sstevel@tonic-gate 	 * /usr/lib/fs/<type>/mount turn into something else.
37557c478bd9Sstevel@tonic-gate 	 */
37567c478bd9Sstevel@tonic-gate 	if (strchr(type, '/') != NULL || type[0] == '\0' ||
37577c478bd9Sstevel@tonic-gate 	    strcmp(type, ".") == 0 || strcmp(type, "..") == 0)
37587c478bd9Sstevel@tonic-gate 		return (B_FALSE);
37597c478bd9Sstevel@tonic-gate 	/*
37607c478bd9Sstevel@tonic-gate 	 * More detailed verification happens later by zoneadm(1m).
37617c478bd9Sstevel@tonic-gate 	 */
37627c478bd9Sstevel@tonic-gate 	return (B_TRUE);
37637c478bd9Sstevel@tonic-gate }
37647c478bd9Sstevel@tonic-gate 
3765f4b3ec61Sdh155122 static boolean_t
3766f4b3ec61Sdh155122 allow_exclusive()
3767f4b3ec61Sdh155122 {
3768f4b3ec61Sdh155122 	brand_handle_t	bh;
3769f4b3ec61Sdh155122 	char		brand[MAXNAMELEN];
3770f4b3ec61Sdh155122 	boolean_t	ret;
3771f4b3ec61Sdh155122 
3772f4b3ec61Sdh155122 	if (zonecfg_get_brand(handle, brand, sizeof (brand)) != Z_OK) {
3773f4b3ec61Sdh155122 		zerr("%s: %s\n", zone, gettext("could not get zone brand"));
3774f4b3ec61Sdh155122 		return (B_FALSE);
3775f4b3ec61Sdh155122 	}
3776f4b3ec61Sdh155122 	if ((bh = brand_open(brand)) == NULL) {
3777f4b3ec61Sdh155122 		zerr("%s: %s\n", zone, gettext("unknown brand."));
3778f4b3ec61Sdh155122 		return (B_FALSE);
3779f4b3ec61Sdh155122 	}
3780f4b3ec61Sdh155122 	ret = brand_allow_exclusive_ip(bh);
3781f4b3ec61Sdh155122 	brand_close(bh);
3782f4b3ec61Sdh155122 	if (!ret)
3783f4b3ec61Sdh155122 		zerr(gettext("%s cannot be '%s' when %s is '%s'."),
3784f4b3ec61Sdh155122 		    pt_to_str(PT_IPTYPE), "exclusive",
3785f4b3ec61Sdh155122 		    pt_to_str(PT_BRAND), brand);
3786f4b3ec61Sdh155122 	return (ret);
3787f4b3ec61Sdh155122 }
3788f4b3ec61Sdh155122 
37890209230bSgjelinek static void
37900209230bSgjelinek set_aliased_rctl(char *alias, int prop_type, char *s)
37910209230bSgjelinek {
37920209230bSgjelinek 	uint64_t limit;
37930209230bSgjelinek 	int err;
37940209230bSgjelinek 	char tmp[128];
37950209230bSgjelinek 
37960209230bSgjelinek 	if (global_zone && strcmp(alias, ALIAS_SHARES) != 0)
37970209230bSgjelinek 		zerr(gettext("WARNING: Setting a global zone resource "
37980209230bSgjelinek 		    "control too low could deny\nservice "
37990209230bSgjelinek 		    "to even the root user; "
38000209230bSgjelinek 		    "this could render the system impossible\n"
38010209230bSgjelinek 		    "to administer.  Please use caution."));
38020209230bSgjelinek 
38030209230bSgjelinek 	/* convert memory based properties */
38040209230bSgjelinek 	if (prop_type == PT_MAXSHMMEM) {
38050209230bSgjelinek 		if (!zonecfg_valid_memlimit(s, &limit)) {
38060209230bSgjelinek 			zerr(gettext("A non-negative number with a required "
38070209230bSgjelinek 			    "scale suffix (K, M, G or T) was expected\nhere."));
3808bbec428eSgjelinek 			saw_error = B_TRUE;
38090209230bSgjelinek 			return;
38100209230bSgjelinek 		}
38110209230bSgjelinek 
38120209230bSgjelinek 		(void) snprintf(tmp, sizeof (tmp), "%llu", limit);
38130209230bSgjelinek 		s = tmp;
38140209230bSgjelinek 	}
38150209230bSgjelinek 
38160209230bSgjelinek 	if (!zonecfg_aliased_rctl_ok(handle, alias)) {
3817bbec428eSgjelinek 		zone_perror(pt_to_str(prop_type), Z_ALIAS_DISALLOW, B_FALSE);
3818bbec428eSgjelinek 		saw_error = B_TRUE;
38190209230bSgjelinek 	} else if (!zonecfg_valid_alias_limit(alias, s, &limit)) {
38200209230bSgjelinek 		zerr(gettext("%s property is out of range."),
38210209230bSgjelinek 		    pt_to_str(prop_type));
3822bbec428eSgjelinek 		saw_error = B_TRUE;
38230209230bSgjelinek 	} else if ((err = zonecfg_set_aliased_rctl(handle, alias, limit))
38240209230bSgjelinek 	    != Z_OK) {
3825bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
3826bbec428eSgjelinek 		saw_error = B_TRUE;
38270209230bSgjelinek 	} else {
3828bbec428eSgjelinek 		need_to_commit = B_TRUE;
38290209230bSgjelinek 	}
38300209230bSgjelinek }
38310209230bSgjelinek 
38327c478bd9Sstevel@tonic-gate void
38337c478bd9Sstevel@tonic-gate set_func(cmd_t *cmd)
38347c478bd9Sstevel@tonic-gate {
38357c478bd9Sstevel@tonic-gate 	char *prop_id;
3836555afedfScarlsonj 	int arg, err, res_type, prop_type;
38377c478bd9Sstevel@tonic-gate 	property_value_ptr_t pp;
38387c478bd9Sstevel@tonic-gate 	boolean_t autoboot;
3839f4b3ec61Sdh155122 	zone_iptype_t iptype;
3840bbec428eSgjelinek 	boolean_t force_set = B_FALSE;
38410209230bSgjelinek 	size_t physmem_size = sizeof (in_progress_mcaptab.zone_physmem_cap);
38420209230bSgjelinek 	uint64_t mem_cap, mem_limit;
3843c97ad5cdSakolb 	float cap;
3844c97ad5cdSakolb 	char *unitp;
38450209230bSgjelinek 	struct zone_psettab tmp_psettab;
3846bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_SET))
38497c478bd9Sstevel@tonic-gate 		return;
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
38527c478bd9Sstevel@tonic-gate 
3853555afedfScarlsonj 	optind = opterr = 0;
3854555afedfScarlsonj 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "F")) != EOF) {
3855555afedfScarlsonj 		switch (arg) {
3856555afedfScarlsonj 		case 'F':
3857bbec428eSgjelinek 			force_set = B_TRUE;
3858555afedfScarlsonj 			break;
3859555afedfScarlsonj 		default:
3860555afedfScarlsonj 			if (optopt == '?')
3861555afedfScarlsonj 				longer_usage(CMD_SET);
3862555afedfScarlsonj 			else
3863555afedfScarlsonj 				short_usage(CMD_SET);
3864bbec428eSgjelinek 			arg_err = B_TRUE;
38657ec75eb8Sgjelinek 			break;
38667ec75eb8Sgjelinek 		}
38677ec75eb8Sgjelinek 	}
38687ec75eb8Sgjelinek 	if (arg_err)
3869555afedfScarlsonj 		return;
3870555afedfScarlsonj 
38717c478bd9Sstevel@tonic-gate 	prop_type = cmd->cmd_prop_name[0];
38727c478bd9Sstevel@tonic-gate 	if (global_scope) {
38730209230bSgjelinek 		if (gz_invalid_property(prop_type)) {
38740209230bSgjelinek 			zerr(gettext("%s is not a valid property for the "
38750209230bSgjelinek 			    "global zone."), pt_to_str(prop_type));
3876bbec428eSgjelinek 			saw_error = B_TRUE;
38770209230bSgjelinek 			return;
38780209230bSgjelinek 		}
38790209230bSgjelinek 
3880087719fdSdp 		if (prop_type == PT_ZONENAME) {
3881087719fdSdp 			res_type = RT_ZONENAME;
3882087719fdSdp 		} else if (prop_type == PT_ZONEPATH) {
38837c478bd9Sstevel@tonic-gate 			res_type = RT_ZONEPATH;
38847c478bd9Sstevel@tonic-gate 		} else if (prop_type == PT_AUTOBOOT) {
38857c478bd9Sstevel@tonic-gate 			res_type = RT_AUTOBOOT;
38869acbbeafSnn35248 		} else if (prop_type == PT_BRAND) {
38879acbbeafSnn35248 			res_type = RT_BRAND;
38887c478bd9Sstevel@tonic-gate 		} else if (prop_type == PT_POOL) {
38897c478bd9Sstevel@tonic-gate 			res_type = RT_POOL;
3890ffbafc53Scomay 		} else if (prop_type == PT_LIMITPRIV) {
3891ffbafc53Scomay 			res_type = RT_LIMITPRIV;
38923f2f09c1Sdp 		} else if (prop_type == PT_BOOTARGS) {
38933f2f09c1Sdp 			res_type = RT_BOOTARGS;
38940209230bSgjelinek 		} else if (prop_type == PT_SCHED) {
38950209230bSgjelinek 			res_type = RT_SCHED;
3896f4b3ec61Sdh155122 		} else if (prop_type == PT_IPTYPE) {
3897f4b3ec61Sdh155122 			res_type = RT_IPTYPE;
38980209230bSgjelinek 		} else if (prop_type == PT_MAXLWPS) {
38990209230bSgjelinek 			res_type = RT_MAXLWPS;
39000209230bSgjelinek 		} else if (prop_type == PT_MAXSHMMEM) {
39010209230bSgjelinek 			res_type = RT_MAXSHMMEM;
39020209230bSgjelinek 		} else if (prop_type == PT_MAXSHMIDS) {
39030209230bSgjelinek 			res_type = RT_MAXSHMIDS;
39040209230bSgjelinek 		} else if (prop_type == PT_MAXMSGIDS) {
39050209230bSgjelinek 			res_type = RT_MAXMSGIDS;
39060209230bSgjelinek 		} else if (prop_type == PT_MAXSEMIDS) {
39070209230bSgjelinek 			res_type = RT_MAXSEMIDS;
39080209230bSgjelinek 		} else if (prop_type == PT_SHARES) {
39090209230bSgjelinek 			res_type = RT_SHARES;
39107c478bd9Sstevel@tonic-gate 		} else {
39117c478bd9Sstevel@tonic-gate 			zerr(gettext("Cannot set a resource-specific property "
39127c478bd9Sstevel@tonic-gate 			    "from the global scope."));
3913bbec428eSgjelinek 			saw_error = B_TRUE;
39147c478bd9Sstevel@tonic-gate 			return;
39157c478bd9Sstevel@tonic-gate 		}
39167c478bd9Sstevel@tonic-gate 	} else {
39177c478bd9Sstevel@tonic-gate 		res_type = resource_scope;
39187c478bd9Sstevel@tonic-gate 	}
39197c478bd9Sstevel@tonic-gate 
3920555afedfScarlsonj 	if (force_set) {
3921555afedfScarlsonj 		if (res_type != RT_ZONEPATH) {
3922555afedfScarlsonj 			zerr(gettext("Only zonepath setting can be forced."));
3923bbec428eSgjelinek 			saw_error = B_TRUE;
3924555afedfScarlsonj 			return;
3925555afedfScarlsonj 		}
3926555afedfScarlsonj 		if (!zonecfg_in_alt_root()) {
3927555afedfScarlsonj 			zerr(gettext("Zonepath is changeable only in an "
3928555afedfScarlsonj 			    "alternate root."));
3929bbec428eSgjelinek 			saw_error = B_TRUE;
3930555afedfScarlsonj 			return;
3931555afedfScarlsonj 		}
3932555afedfScarlsonj 	}
3933555afedfScarlsonj 
39347c478bd9Sstevel@tonic-gate 	pp = cmd->cmd_property_ptr[0];
39357c478bd9Sstevel@tonic-gate 	/*
39367c478bd9Sstevel@tonic-gate 	 * A nasty expression but not that complicated:
39377c478bd9Sstevel@tonic-gate 	 * 1. fs options are simple or list (tested below)
39387c478bd9Sstevel@tonic-gate 	 * 2. rctl value's are complex or list (tested below)
39397c478bd9Sstevel@tonic-gate 	 * Anything else should be simple.
39407c478bd9Sstevel@tonic-gate 	 */
39417c478bd9Sstevel@tonic-gate 	if (!(res_type == RT_FS && prop_type == PT_OPTIONS) &&
39427c478bd9Sstevel@tonic-gate 	    !(res_type == RT_RCTL && prop_type == PT_VALUE) &&
39437c478bd9Sstevel@tonic-gate 	    (pp->pv_type != PROP_VAL_SIMPLE ||
39447c478bd9Sstevel@tonic-gate 	    (prop_id = pp->pv_simple) == NULL)) {
39457c478bd9Sstevel@tonic-gate 		zerr(gettext("A %s value was expected here."),
39467c478bd9Sstevel@tonic-gate 		    pvt_to_str(PROP_VAL_SIMPLE));
3947bbec428eSgjelinek 		saw_error = B_TRUE;
39487c478bd9Sstevel@tonic-gate 		return;
39497c478bd9Sstevel@tonic-gate 	}
39507c478bd9Sstevel@tonic-gate 	if (prop_type == PT_UNKNOWN) {
3951bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
39527c478bd9Sstevel@tonic-gate 		return;
39537c478bd9Sstevel@tonic-gate 	}
39547c478bd9Sstevel@tonic-gate 
3955087719fdSdp 	/*
3956087719fdSdp 	 * Special case: the user can change the zone name prior to 'create';
3957087719fdSdp 	 * if the zone already exists, we fall through letting initialize()
3958087719fdSdp 	 * and the rest of the logic run.
3959087719fdSdp 	 */
3960bbec428eSgjelinek 	if (res_type == RT_ZONENAME && got_handle == B_FALSE &&
3961087719fdSdp 	    !state_atleast(ZONE_STATE_CONFIGURED)) {
3962fb03efaaSdp 		if ((err = zonecfg_validate_zonename(prop_id)) != Z_OK) {
3963bbec428eSgjelinek 			zone_perror(prop_id, err, B_TRUE);
3964bbec428eSgjelinek 			usage(B_FALSE, HELP_SYNTAX);
3965fb03efaaSdp 			return;
3966fb03efaaSdp 		}
3967087719fdSdp 		(void) strlcpy(zone, prop_id, sizeof (zone));
3968087719fdSdp 		return;
3969087719fdSdp 	}
3970087719fdSdp 
3971bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
39727c478bd9Sstevel@tonic-gate 		return;
39737c478bd9Sstevel@tonic-gate 
39747c478bd9Sstevel@tonic-gate 	switch (res_type) {
3975087719fdSdp 	case RT_ZONENAME:
3976087719fdSdp 		if ((err = zonecfg_set_name(handle, prop_id)) != Z_OK) {
3977087719fdSdp 			/*
3978087719fdSdp 			 * Use prop_id instead of 'zone' here, since we're
3979087719fdSdp 			 * reporting a problem about the *new* zonename.
3980087719fdSdp 			 */
3981bbec428eSgjelinek 			zone_perror(prop_id, err, B_TRUE);
3982bbec428eSgjelinek 			usage(B_FALSE, HELP_SYNTAX);
3983087719fdSdp 		} else {
3984bbec428eSgjelinek 			need_to_commit = B_TRUE;
3985087719fdSdp 			(void) strlcpy(zone, prop_id, sizeof (zone));
3986087719fdSdp 		}
3987087719fdSdp 		return;
39887c478bd9Sstevel@tonic-gate 	case RT_ZONEPATH:
3989555afedfScarlsonj 		if (!force_set && state_atleast(ZONE_STATE_INSTALLED)) {
39907c478bd9Sstevel@tonic-gate 			zerr(gettext("Zone %s already installed; %s %s not "
39917c478bd9Sstevel@tonic-gate 			    "allowed."), zone, cmd_to_str(CMD_SET),
39927c478bd9Sstevel@tonic-gate 			    rt_to_str(RT_ZONEPATH));
39937c478bd9Sstevel@tonic-gate 			return;
39947c478bd9Sstevel@tonic-gate 		}
39957c478bd9Sstevel@tonic-gate 		if (validate_zonepath_syntax(prop_id) != Z_OK) {
3996bbec428eSgjelinek 			saw_error = B_TRUE;
39977c478bd9Sstevel@tonic-gate 			return;
39987c478bd9Sstevel@tonic-gate 		}
39997c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_set_zonepath(handle, prop_id)) != Z_OK)
4000bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
40017c478bd9Sstevel@tonic-gate 		else
4002bbec428eSgjelinek 			need_to_commit = B_TRUE;
40037c478bd9Sstevel@tonic-gate 		return;
40049acbbeafSnn35248 	case RT_BRAND:
40059acbbeafSnn35248 		if (state_atleast(ZONE_STATE_INSTALLED)) {
40069acbbeafSnn35248 			zerr(gettext("Zone %s already installed; %s %s not "
40079acbbeafSnn35248 			    "allowed."), zone, cmd_to_str(CMD_SET),
40089acbbeafSnn35248 			    rt_to_str(RT_BRAND));
40099acbbeafSnn35248 			return;
40109acbbeafSnn35248 		}
40119acbbeafSnn35248 		if ((err = zonecfg_set_brand(handle, prop_id)) != Z_OK)
4012bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
40139acbbeafSnn35248 		else
4014bbec428eSgjelinek 			need_to_commit = B_TRUE;
40159acbbeafSnn35248 		return;
40167c478bd9Sstevel@tonic-gate 	case RT_AUTOBOOT:
40177c478bd9Sstevel@tonic-gate 		if (strcmp(prop_id, "true") == 0) {
40187c478bd9Sstevel@tonic-gate 			autoboot = B_TRUE;
40197c478bd9Sstevel@tonic-gate 		} else if (strcmp(prop_id, "false") == 0) {
40207c478bd9Sstevel@tonic-gate 			autoboot = B_FALSE;
40217c478bd9Sstevel@tonic-gate 		} else {
40227c478bd9Sstevel@tonic-gate 			zerr(gettext("%s value must be '%s' or '%s'."),
40237c478bd9Sstevel@tonic-gate 			    pt_to_str(PT_AUTOBOOT), "true", "false");
4024bbec428eSgjelinek 			saw_error = B_TRUE;
40257c478bd9Sstevel@tonic-gate 			return;
40267c478bd9Sstevel@tonic-gate 		}
40277c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_set_autoboot(handle, autoboot)) != Z_OK)
4028bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
40297c478bd9Sstevel@tonic-gate 		else
4030bbec428eSgjelinek 			need_to_commit = B_TRUE;
40317c478bd9Sstevel@tonic-gate 		return;
40327c478bd9Sstevel@tonic-gate 	case RT_POOL:
40330209230bSgjelinek 		/* don't allow use of the reserved temporary pool names */
40340209230bSgjelinek 		if (strncmp("SUNW", prop_id, 4) == 0) {
40350209230bSgjelinek 			zerr(gettext("pool names starting with SUNW are "
40360209230bSgjelinek 			    "reserved."));
4037bbec428eSgjelinek 			saw_error = B_TRUE;
40380209230bSgjelinek 			return;
40390209230bSgjelinek 		}
40400209230bSgjelinek 
40410209230bSgjelinek 		/* can't set pool if dedicated-cpu exists */
40420209230bSgjelinek 		if (zonecfg_lookup_pset(handle, &tmp_psettab) == Z_OK) {
40430209230bSgjelinek 			zerr(gettext("The %s resource already exists.  "
40440209230bSgjelinek 			    "A persistent pool is incompatible\nwith the %s "
40450209230bSgjelinek 			    "resource."), rt_to_str(RT_DCPU),
40460209230bSgjelinek 			    rt_to_str(RT_DCPU));
4047bbec428eSgjelinek 			saw_error = B_TRUE;
40480209230bSgjelinek 			return;
40490209230bSgjelinek 		}
40500209230bSgjelinek 
40517c478bd9Sstevel@tonic-gate 		if ((err = zonecfg_set_pool(handle, prop_id)) != Z_OK)
4052bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
40537c478bd9Sstevel@tonic-gate 		else
4054bbec428eSgjelinek 			need_to_commit = B_TRUE;
40557c478bd9Sstevel@tonic-gate 		return;
4056ffbafc53Scomay 	case RT_LIMITPRIV:
4057ffbafc53Scomay 		if ((err = zonecfg_set_limitpriv(handle, prop_id)) != Z_OK)
4058bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
4059ffbafc53Scomay 		else
4060bbec428eSgjelinek 			need_to_commit = B_TRUE;
4061ffbafc53Scomay 		return;
40623f2f09c1Sdp 	case RT_BOOTARGS:
40633f2f09c1Sdp 		if ((err = zonecfg_set_bootargs(handle, prop_id)) != Z_OK)
4064bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
40653f2f09c1Sdp 		else
4066bbec428eSgjelinek 			need_to_commit = B_TRUE;
40673f2f09c1Sdp 		return;
40680209230bSgjelinek 	case RT_SCHED:
40690209230bSgjelinek 		if ((err = zonecfg_set_sched(handle, prop_id)) != Z_OK)
4070bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
40710209230bSgjelinek 		else
4072bbec428eSgjelinek 			need_to_commit = B_TRUE;
40730209230bSgjelinek 		return;
4074f4b3ec61Sdh155122 	case RT_IPTYPE:
4075f4b3ec61Sdh155122 		if (strcmp(prop_id, "shared") == 0) {
4076f4b3ec61Sdh155122 			iptype = ZS_SHARED;
4077f4b3ec61Sdh155122 		} else if (strcmp(prop_id, "exclusive") == 0) {
4078f4b3ec61Sdh155122 			iptype = ZS_EXCLUSIVE;
4079f4b3ec61Sdh155122 		} else {
4080f4b3ec61Sdh155122 			zerr(gettext("%s value must be '%s' or '%s'."),
4081f4b3ec61Sdh155122 			    pt_to_str(PT_IPTYPE), "shared", "exclusive");
4082bbec428eSgjelinek 			saw_error = B_TRUE;
4083f4b3ec61Sdh155122 			return;
4084f4b3ec61Sdh155122 		}
4085f4b3ec61Sdh155122 		if (iptype == ZS_EXCLUSIVE && !allow_exclusive()) {
4086bbec428eSgjelinek 			saw_error = B_TRUE;
4087f4b3ec61Sdh155122 			return;
4088f4b3ec61Sdh155122 		}
4089f4b3ec61Sdh155122 		if ((err = zonecfg_set_iptype(handle, iptype)) != Z_OK)
4090bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
4091f4b3ec61Sdh155122 		else
4092bbec428eSgjelinek 			need_to_commit = B_TRUE;
4093f4b3ec61Sdh155122 		return;
40940209230bSgjelinek 	case RT_MAXLWPS:
40950209230bSgjelinek 		set_aliased_rctl(ALIAS_MAXLWPS, prop_type, prop_id);
40960209230bSgjelinek 		return;
40970209230bSgjelinek 	case RT_MAXSHMMEM:
40980209230bSgjelinek 		set_aliased_rctl(ALIAS_MAXSHMMEM, prop_type, prop_id);
40990209230bSgjelinek 		return;
41000209230bSgjelinek 	case RT_MAXSHMIDS:
41010209230bSgjelinek 		set_aliased_rctl(ALIAS_MAXSHMIDS, prop_type, prop_id);
41020209230bSgjelinek 		return;
41030209230bSgjelinek 	case RT_MAXMSGIDS:
41040209230bSgjelinek 		set_aliased_rctl(ALIAS_MAXMSGIDS, prop_type, prop_id);
41050209230bSgjelinek 		return;
41060209230bSgjelinek 	case RT_MAXSEMIDS:
41070209230bSgjelinek 		set_aliased_rctl(ALIAS_MAXSEMIDS, prop_type, prop_id);
41080209230bSgjelinek 		return;
41090209230bSgjelinek 	case RT_SHARES:
41100209230bSgjelinek 		set_aliased_rctl(ALIAS_SHARES, prop_type, prop_id);
41110209230bSgjelinek 		return;
41127c478bd9Sstevel@tonic-gate 	case RT_FS:
41137c478bd9Sstevel@tonic-gate 		switch (prop_type) {
41147c478bd9Sstevel@tonic-gate 		case PT_DIR:
41157c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_fstab.zone_fs_dir, prop_id,
41167c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_fstab.zone_fs_dir));
41177c478bd9Sstevel@tonic-gate 			return;
41187c478bd9Sstevel@tonic-gate 		case PT_SPECIAL:
41197c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_fstab.zone_fs_special,
41207c478bd9Sstevel@tonic-gate 			    prop_id,
41217c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_fstab.zone_fs_special));
41227c478bd9Sstevel@tonic-gate 			return;
41237c478bd9Sstevel@tonic-gate 		case PT_RAW:
41247c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_fstab.zone_fs_raw,
41257c478bd9Sstevel@tonic-gate 			    prop_id, sizeof (in_progress_fstab.zone_fs_raw));
41267c478bd9Sstevel@tonic-gate 			return;
41277c478bd9Sstevel@tonic-gate 		case PT_TYPE:
41287c478bd9Sstevel@tonic-gate 			if (!valid_fs_type(prop_id)) {
41297c478bd9Sstevel@tonic-gate 				zerr(gettext("\"%s\" is not a valid %s."),
41307c478bd9Sstevel@tonic-gate 				    prop_id, pt_to_str(PT_TYPE));
4131bbec428eSgjelinek 				saw_error = B_TRUE;
41327c478bd9Sstevel@tonic-gate 				return;
41337c478bd9Sstevel@tonic-gate 			}
41347c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_fstab.zone_fs_type, prop_id,
41357c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_fstab.zone_fs_type));
41367c478bd9Sstevel@tonic-gate 			return;
41377c478bd9Sstevel@tonic-gate 		case PT_OPTIONS:
41387c478bd9Sstevel@tonic-gate 			if (pp->pv_type != PROP_VAL_SIMPLE &&
41397c478bd9Sstevel@tonic-gate 			    pp->pv_type != PROP_VAL_LIST) {
41407c478bd9Sstevel@tonic-gate 				zerr(gettext("A %s or %s value was expected "
41417c478bd9Sstevel@tonic-gate 				    "here."), pvt_to_str(PROP_VAL_SIMPLE),
41427c478bd9Sstevel@tonic-gate 				    pvt_to_str(PROP_VAL_LIST));
4143bbec428eSgjelinek 				saw_error = B_TRUE;
41447c478bd9Sstevel@tonic-gate 				return;
41457c478bd9Sstevel@tonic-gate 			}
41467c478bd9Sstevel@tonic-gate 			zonecfg_free_fs_option_list(
41477c478bd9Sstevel@tonic-gate 			    in_progress_fstab.zone_fs_options);
41487c478bd9Sstevel@tonic-gate 			in_progress_fstab.zone_fs_options = NULL;
41497c478bd9Sstevel@tonic-gate 			if (!(pp->pv_type == PROP_VAL_LIST &&
41507c478bd9Sstevel@tonic-gate 			    pp->pv_list == NULL))
41517c478bd9Sstevel@tonic-gate 				add_property(cmd);
41527c478bd9Sstevel@tonic-gate 			return;
41537c478bd9Sstevel@tonic-gate 		default:
41547c478bd9Sstevel@tonic-gate 			break;
41557c478bd9Sstevel@tonic-gate 		}
4156bbec428eSgjelinek 		zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE, B_TRUE);
4157bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
4158bbec428eSgjelinek 		usage(B_FALSE, HELP_PROPS);
41597c478bd9Sstevel@tonic-gate 		return;
41607c478bd9Sstevel@tonic-gate 	case RT_IPD:
41617c478bd9Sstevel@tonic-gate 		switch (prop_type) {
41627c478bd9Sstevel@tonic-gate 		case PT_DIR:
41637c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_ipdtab.zone_fs_dir, prop_id,
41647c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_ipdtab.zone_fs_dir));
41657c478bd9Sstevel@tonic-gate 			return;
41667c478bd9Sstevel@tonic-gate 		default:
41677c478bd9Sstevel@tonic-gate 			break;
41687c478bd9Sstevel@tonic-gate 		}
4169bbec428eSgjelinek 		zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE, B_TRUE);
4170bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
4171bbec428eSgjelinek 		usage(B_FALSE, HELP_PROPS);
41727c478bd9Sstevel@tonic-gate 		return;
41737c478bd9Sstevel@tonic-gate 	case RT_NET:
41747c478bd9Sstevel@tonic-gate 		switch (prop_type) {
41757c478bd9Sstevel@tonic-gate 		case PT_ADDRESS:
41767c478bd9Sstevel@tonic-gate 			if (validate_net_address_syntax(prop_id) != Z_OK) {
4177bbec428eSgjelinek 				saw_error = B_TRUE;
41787c478bd9Sstevel@tonic-gate 				return;
41797c478bd9Sstevel@tonic-gate 			}
41807c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_nwiftab.zone_nwif_address,
41817c478bd9Sstevel@tonic-gate 			    prop_id,
41827c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_nwiftab.zone_nwif_address));
41837c478bd9Sstevel@tonic-gate 			break;
41847c478bd9Sstevel@tonic-gate 		case PT_PHYSICAL:
41857c478bd9Sstevel@tonic-gate 			if (validate_net_physical_syntax(prop_id) != Z_OK) {
4186bbec428eSgjelinek 				saw_error = B_TRUE;
41877c478bd9Sstevel@tonic-gate 				return;
41887c478bd9Sstevel@tonic-gate 			}
41897c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_nwiftab.zone_nwif_physical,
41907c478bd9Sstevel@tonic-gate 			    prop_id,
41917c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_nwiftab.zone_nwif_physical));
41927c478bd9Sstevel@tonic-gate 			break;
4193de860bd9Sgfaden 		case PT_DEFROUTER:
4194de860bd9Sgfaden 			if (validate_net_address_syntax(prop_id) != Z_OK) {
4195bbec428eSgjelinek 				saw_error = B_TRUE;
4196de860bd9Sgfaden 				return;
4197de860bd9Sgfaden 			}
4198de860bd9Sgfaden 			(void) strlcpy(in_progress_nwiftab.zone_nwif_defrouter,
4199de860bd9Sgfaden 			    prop_id,
4200de860bd9Sgfaden 			    sizeof (in_progress_nwiftab.zone_nwif_defrouter));
4201de860bd9Sgfaden 			break;
42027c478bd9Sstevel@tonic-gate 		default:
42037c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4204bbec428eSgjelinek 			    B_TRUE);
4205bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4206bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
42077c478bd9Sstevel@tonic-gate 			return;
42087c478bd9Sstevel@tonic-gate 		}
42097c478bd9Sstevel@tonic-gate 		return;
42107c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
42117c478bd9Sstevel@tonic-gate 		switch (prop_type) {
42127c478bd9Sstevel@tonic-gate 		case PT_MATCH:
42137c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_devtab.zone_dev_match,
42147c478bd9Sstevel@tonic-gate 			    prop_id,
42157c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_devtab.zone_dev_match));
42167c478bd9Sstevel@tonic-gate 			break;
42177c478bd9Sstevel@tonic-gate 		default:
42187c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4219bbec428eSgjelinek 			    B_TRUE);
4220bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4221bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
42227c478bd9Sstevel@tonic-gate 			return;
42237c478bd9Sstevel@tonic-gate 		}
42247c478bd9Sstevel@tonic-gate 		return;
42257c478bd9Sstevel@tonic-gate 	case RT_RCTL:
42267c478bd9Sstevel@tonic-gate 		switch (prop_type) {
42277c478bd9Sstevel@tonic-gate 		case PT_NAME:
42287c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctlname(prop_id)) {
42297c478bd9Sstevel@tonic-gate 				zerr(gettext("'%s' is not a valid zone %s "
42307c478bd9Sstevel@tonic-gate 				    "name."), prop_id, rt_to_str(RT_RCTL));
42317c478bd9Sstevel@tonic-gate 				return;
42327c478bd9Sstevel@tonic-gate 			}
42337c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_rctltab.zone_rctl_name,
42347c478bd9Sstevel@tonic-gate 			    prop_id,
42357c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_rctltab.zone_rctl_name));
42367c478bd9Sstevel@tonic-gate 			break;
42377c478bd9Sstevel@tonic-gate 		case PT_VALUE:
42387c478bd9Sstevel@tonic-gate 			if (pp->pv_type != PROP_VAL_COMPLEX &&
42397c478bd9Sstevel@tonic-gate 			    pp->pv_type != PROP_VAL_LIST) {
42407c478bd9Sstevel@tonic-gate 				zerr(gettext("A %s or %s value was expected "
42417c478bd9Sstevel@tonic-gate 				    "here."), pvt_to_str(PROP_VAL_COMPLEX),
42427c478bd9Sstevel@tonic-gate 				    pvt_to_str(PROP_VAL_LIST));
4243bbec428eSgjelinek 				saw_error = B_TRUE;
42447c478bd9Sstevel@tonic-gate 				return;
42457c478bd9Sstevel@tonic-gate 			}
42467c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(
42477c478bd9Sstevel@tonic-gate 			    in_progress_rctltab.zone_rctl_valptr);
42487c478bd9Sstevel@tonic-gate 			in_progress_rctltab.zone_rctl_valptr = NULL;
42497c478bd9Sstevel@tonic-gate 			if (!(pp->pv_type == PROP_VAL_LIST &&
42507c478bd9Sstevel@tonic-gate 			    pp->pv_list == NULL))
42517c478bd9Sstevel@tonic-gate 				add_property(cmd);
42527c478bd9Sstevel@tonic-gate 			break;
42537c478bd9Sstevel@tonic-gate 		default:
42547c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4255bbec428eSgjelinek 			    B_TRUE);
4256bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4257bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
42587c478bd9Sstevel@tonic-gate 			return;
42597c478bd9Sstevel@tonic-gate 		}
42607c478bd9Sstevel@tonic-gate 		return;
42617c478bd9Sstevel@tonic-gate 	case RT_ATTR:
42627c478bd9Sstevel@tonic-gate 		switch (prop_type) {
42637c478bd9Sstevel@tonic-gate 		case PT_NAME:
42647c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_attrtab.zone_attr_name,
42657c478bd9Sstevel@tonic-gate 			    prop_id,
42667c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_attrtab.zone_attr_name));
42677c478bd9Sstevel@tonic-gate 			break;
42687c478bd9Sstevel@tonic-gate 		case PT_TYPE:
42697c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_attrtab.zone_attr_type,
42707c478bd9Sstevel@tonic-gate 			    prop_id,
42717c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_attrtab.zone_attr_type));
42727c478bd9Sstevel@tonic-gate 			break;
42737c478bd9Sstevel@tonic-gate 		case PT_VALUE:
42747c478bd9Sstevel@tonic-gate 			(void) strlcpy(in_progress_attrtab.zone_attr_value,
42757c478bd9Sstevel@tonic-gate 			    prop_id,
42767c478bd9Sstevel@tonic-gate 			    sizeof (in_progress_attrtab.zone_attr_value));
42777c478bd9Sstevel@tonic-gate 			break;
42787c478bd9Sstevel@tonic-gate 		default:
42797c478bd9Sstevel@tonic-gate 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4280bbec428eSgjelinek 			    B_TRUE);
4281bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4282bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
42837c478bd9Sstevel@tonic-gate 			return;
42847c478bd9Sstevel@tonic-gate 		}
42857c478bd9Sstevel@tonic-gate 		return;
4286fa9e4066Sahrens 	case RT_DATASET:
4287fa9e4066Sahrens 		switch (prop_type) {
4288fa9e4066Sahrens 		case PT_NAME:
4289fa9e4066Sahrens 			(void) strlcpy(in_progress_dstab.zone_dataset_name,
4290fa9e4066Sahrens 			    prop_id,
4291fa9e4066Sahrens 			    sizeof (in_progress_dstab.zone_dataset_name));
4292fa9e4066Sahrens 			return;
4293fa9e4066Sahrens 		default:
4294fa9e4066Sahrens 			break;
4295fa9e4066Sahrens 		}
4296bbec428eSgjelinek 		zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE, B_TRUE);
4297bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
4298bbec428eSgjelinek 		usage(B_FALSE, HELP_PROPS);
4299fa9e4066Sahrens 		return;
43000209230bSgjelinek 	case RT_DCPU:
43010209230bSgjelinek 		switch (prop_type) {
43020209230bSgjelinek 		char *lowp, *highp;
43030209230bSgjelinek 
43040209230bSgjelinek 		case PT_NCPUS:
43050209230bSgjelinek 			lowp = prop_id;
43060209230bSgjelinek 			if ((highp = strchr(prop_id, '-')) != NULL)
43070209230bSgjelinek 				*highp++ = '\0';
43080209230bSgjelinek 			else
43090209230bSgjelinek 				highp = lowp;
43100209230bSgjelinek 
43110209230bSgjelinek 			/* Make sure the input makes sense. */
43120209230bSgjelinek 			if (!zonecfg_valid_ncpus(lowp, highp)) {
43130209230bSgjelinek 				zerr(gettext("%s property is out of range."),
43140209230bSgjelinek 				    pt_to_str(PT_NCPUS));
4315bbec428eSgjelinek 				saw_error = B_TRUE;
43160209230bSgjelinek 				return;
43170209230bSgjelinek 			}
43180209230bSgjelinek 
43190209230bSgjelinek 			(void) strlcpy(
43200209230bSgjelinek 			    in_progress_psettab.zone_ncpu_min, lowp,
43210209230bSgjelinek 			    sizeof (in_progress_psettab.zone_ncpu_min));
43220209230bSgjelinek 			(void) strlcpy(
43230209230bSgjelinek 			    in_progress_psettab.zone_ncpu_max, highp,
43240209230bSgjelinek 			    sizeof (in_progress_psettab.zone_ncpu_max));
43250209230bSgjelinek 			return;
43260209230bSgjelinek 		case PT_IMPORTANCE:
43270209230bSgjelinek 			/* Make sure the value makes sense. */
43280209230bSgjelinek 			if (!zonecfg_valid_importance(prop_id)) {
43290209230bSgjelinek 				zerr(gettext("%s property is out of range."),
43300209230bSgjelinek 				    pt_to_str(PT_IMPORTANCE));
4331bbec428eSgjelinek 				saw_error = B_TRUE;
43320209230bSgjelinek 				return;
43330209230bSgjelinek 			}
43340209230bSgjelinek 
43350209230bSgjelinek 			(void) strlcpy(in_progress_psettab.zone_importance,
43360209230bSgjelinek 			    prop_id,
43370209230bSgjelinek 			    sizeof (in_progress_psettab.zone_importance));
43380209230bSgjelinek 			return;
43390209230bSgjelinek 		default:
43400209230bSgjelinek 			break;
43410209230bSgjelinek 		}
4342bbec428eSgjelinek 		zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE, B_TRUE);
4343bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
4344bbec428eSgjelinek 		usage(B_FALSE, HELP_PROPS);
43450209230bSgjelinek 		return;
4346c97ad5cdSakolb 	case RT_PCAP:
4347c97ad5cdSakolb 		if (prop_type != PT_NCPUS) {
4348c97ad5cdSakolb 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4349bbec428eSgjelinek 			    B_TRUE);
4350bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4351bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
4352c97ad5cdSakolb 			return;
4353c97ad5cdSakolb 		}
4354c97ad5cdSakolb 
4355c97ad5cdSakolb 		/*
4356c97ad5cdSakolb 		 * We already checked that an rctl alias is allowed in
4357c97ad5cdSakolb 		 * the add_resource() function.
4358c97ad5cdSakolb 		 */
4359c97ad5cdSakolb 
4360c97ad5cdSakolb 		if ((cap = strtof(prop_id, &unitp)) <= 0 || *unitp != '\0' ||
4361c97ad5cdSakolb 		    (int)(cap * 100) < 1) {
4362c97ad5cdSakolb 			zerr(gettext("%s property is out of range."),
4363c97ad5cdSakolb 			    pt_to_str(PT_NCPUS));
4364bbec428eSgjelinek 			saw_error = B_TRUE;
4365c97ad5cdSakolb 			return;
4366c97ad5cdSakolb 		}
4367c97ad5cdSakolb 
4368c97ad5cdSakolb 		if ((err = zonecfg_set_aliased_rctl(handle, ALIAS_CPUCAP,
4369c97ad5cdSakolb 		    (int)(cap * 100))) != Z_OK)
4370bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
4371c97ad5cdSakolb 		else
4372bbec428eSgjelinek 			need_to_commit = B_TRUE;
4373c97ad5cdSakolb 		return;
43740209230bSgjelinek 	case RT_MCAP:
43750209230bSgjelinek 		switch (prop_type) {
43760209230bSgjelinek 		case PT_PHYSICAL:
43770209230bSgjelinek 			if (!zonecfg_valid_memlimit(prop_id, &mem_cap)) {
43780209230bSgjelinek 				zerr(gettext("A positive number with a "
43790209230bSgjelinek 				    "required scale suffix (K, M, G or T) was "
43800209230bSgjelinek 				    "expected here."));
4381bbec428eSgjelinek 				saw_error = B_TRUE;
43820209230bSgjelinek 			} else if (mem_cap < ONE_MB) {
43830209230bSgjelinek 				zerr(gettext("%s value is too small.  It must "
43840209230bSgjelinek 				    "be at least 1M."), pt_to_str(PT_PHYSICAL));
4385bbec428eSgjelinek 				saw_error = B_TRUE;
43860209230bSgjelinek 			} else {
43870209230bSgjelinek 				snprintf(in_progress_mcaptab.zone_physmem_cap,
43880209230bSgjelinek 				    physmem_size, "%llu", mem_cap);
43890209230bSgjelinek 			}
43900209230bSgjelinek 			break;
43910209230bSgjelinek 		case PT_SWAP:
43920209230bSgjelinek 			/*
43930209230bSgjelinek 			 * We have to check if an rctl is allowed here since
43940209230bSgjelinek 			 * there might already be a rctl defined that blocks
43950209230bSgjelinek 			 * the alias.
43960209230bSgjelinek 			 */
43970209230bSgjelinek 			if (!zonecfg_aliased_rctl_ok(handle, ALIAS_MAXSWAP)) {
43980209230bSgjelinek 				zone_perror(pt_to_str(PT_MAXSWAP),
4399bbec428eSgjelinek 				    Z_ALIAS_DISALLOW, B_FALSE);
4400bbec428eSgjelinek 				saw_error = B_TRUE;
44010209230bSgjelinek 				return;
44020209230bSgjelinek 			}
44030209230bSgjelinek 
44040209230bSgjelinek 			if (global_zone)
44050209230bSgjelinek 				mem_limit = ONE_MB * 100;
44060209230bSgjelinek 			else
44070209230bSgjelinek 				mem_limit = ONE_MB * 50;
44080209230bSgjelinek 
44090209230bSgjelinek 			if (!zonecfg_valid_memlimit(prop_id, &mem_cap)) {
44100209230bSgjelinek 				zerr(gettext("A positive number with a "
44110209230bSgjelinek 				    "required scale suffix (K, M, G or T) was "
44120209230bSgjelinek 				    "expected here."));
4413bbec428eSgjelinek 				saw_error = B_TRUE;
44140209230bSgjelinek 			} else if (mem_cap < mem_limit) {
44150209230bSgjelinek 				char buf[128];
44160209230bSgjelinek 
44170209230bSgjelinek 				(void) snprintf(buf, sizeof (buf), "%llu",
44180209230bSgjelinek 				    mem_limit);
44190209230bSgjelinek 				bytes_to_units(buf, buf, sizeof (buf));
44200209230bSgjelinek 				zerr(gettext("%s value is too small.  It must "
44210209230bSgjelinek 				    "be at least %s."), pt_to_str(PT_SWAP),
44220209230bSgjelinek 				    buf);
4423bbec428eSgjelinek 				saw_error = B_TRUE;
44240209230bSgjelinek 			} else {
44250209230bSgjelinek 				if ((err = zonecfg_set_aliased_rctl(handle,
44260209230bSgjelinek 				    ALIAS_MAXSWAP, mem_cap)) != Z_OK)
4427bbec428eSgjelinek 					zone_perror(zone, err, B_TRUE);
44280209230bSgjelinek 				else
4429bbec428eSgjelinek 					need_to_commit = B_TRUE;
44300209230bSgjelinek 			}
44310209230bSgjelinek 			break;
44320209230bSgjelinek 		case PT_LOCKED:
44330209230bSgjelinek 			/*
44340209230bSgjelinek 			 * We have to check if an rctl is allowed here since
44350209230bSgjelinek 			 * there might already be a rctl defined that blocks
44360209230bSgjelinek 			 * the alias.
44370209230bSgjelinek 			 */
44380209230bSgjelinek 			if (!zonecfg_aliased_rctl_ok(handle,
44390209230bSgjelinek 			    ALIAS_MAXLOCKEDMEM)) {
44400209230bSgjelinek 				zone_perror(pt_to_str(PT_LOCKED),
4441bbec428eSgjelinek 				    Z_ALIAS_DISALLOW, B_FALSE);
4442bbec428eSgjelinek 				saw_error = B_TRUE;
44430209230bSgjelinek 				return;
44440209230bSgjelinek 			}
44450209230bSgjelinek 
44460209230bSgjelinek 			if (!zonecfg_valid_memlimit(prop_id, &mem_cap)) {
44470209230bSgjelinek 				zerr(gettext("A non-negative number with a "
44480209230bSgjelinek 				    "required scale suffix (K, M, G or T) was "
44490209230bSgjelinek 				    "expected\nhere."));
4450bbec428eSgjelinek 				saw_error = B_TRUE;
44510209230bSgjelinek 			} else {
44520209230bSgjelinek 				if ((err = zonecfg_set_aliased_rctl(handle,
44530209230bSgjelinek 				    ALIAS_MAXLOCKEDMEM, mem_cap)) != Z_OK)
4454bbec428eSgjelinek 					zone_perror(zone, err, B_TRUE);
44550209230bSgjelinek 				else
4456bbec428eSgjelinek 					need_to_commit = B_TRUE;
44570209230bSgjelinek 			}
44580209230bSgjelinek 			break;
44590209230bSgjelinek 		default:
44600209230bSgjelinek 			zone_perror(pt_to_str(prop_type), Z_NO_PROPERTY_TYPE,
4461bbec428eSgjelinek 			    B_TRUE);
4462bbec428eSgjelinek 			long_usage(CMD_SET, B_TRUE);
4463bbec428eSgjelinek 			usage(B_FALSE, HELP_PROPS);
44640209230bSgjelinek 			return;
44650209230bSgjelinek 		}
44660209230bSgjelinek 
44670209230bSgjelinek 		return;
44687c478bd9Sstevel@tonic-gate 	default:
4469bbec428eSgjelinek 		zone_perror(rt_to_str(res_type), Z_NO_RESOURCE_TYPE, B_TRUE);
4470bbec428eSgjelinek 		long_usage(CMD_SET, B_TRUE);
4471bbec428eSgjelinek 		usage(B_FALSE, HELP_RESOURCES);
44727c478bd9Sstevel@tonic-gate 		return;
44737c478bd9Sstevel@tonic-gate 	}
44747c478bd9Sstevel@tonic-gate }
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate static void
4477bbec428eSgjelinek output_prop(FILE *fp, int pnum, char *pval, boolean_t print_notspec)
44787c478bd9Sstevel@tonic-gate {
44797c478bd9Sstevel@tonic-gate 	char *qstr;
44807c478bd9Sstevel@tonic-gate 
44817c478bd9Sstevel@tonic-gate 	if (*pval != '\0') {
44827c478bd9Sstevel@tonic-gate 		qstr = quoteit(pval);
44830209230bSgjelinek 		if (pnum == PT_SWAP || pnum == PT_LOCKED)
44840209230bSgjelinek 			(void) fprintf(fp, "\t[%s: %s]\n", pt_to_str(pnum),
44850209230bSgjelinek 			    qstr);
44860209230bSgjelinek 		else
44877c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\t%s: %s\n", pt_to_str(pnum), qstr);
44887c478bd9Sstevel@tonic-gate 		free(qstr);
44897c478bd9Sstevel@tonic-gate 	} else if (print_notspec)
4490087719fdSdp 		(void) fprintf(fp, gettext("\t%s not specified\n"),
4491087719fdSdp 		    pt_to_str(pnum));
4492087719fdSdp }
4493087719fdSdp 
4494087719fdSdp static void
4495087719fdSdp info_zonename(zone_dochandle_t handle, FILE *fp)
4496087719fdSdp {
4497087719fdSdp 	char zonename[ZONENAME_MAX];
4498087719fdSdp 
4499087719fdSdp 	if (zonecfg_get_name(handle, zonename, sizeof (zonename)) == Z_OK)
4500087719fdSdp 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_ZONENAME),
4501087719fdSdp 		    zonename);
4502087719fdSdp 	else
4503087719fdSdp 		(void) fprintf(fp, gettext("%s not specified\n"),
4504087719fdSdp 		    pt_to_str(PT_ZONENAME));
45057c478bd9Sstevel@tonic-gate }
45067c478bd9Sstevel@tonic-gate 
45077c478bd9Sstevel@tonic-gate static void
45087c478bd9Sstevel@tonic-gate info_zonepath(zone_dochandle_t handle, FILE *fp)
45097c478bd9Sstevel@tonic-gate {
45107c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate 	if (zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath)) == Z_OK)
45137c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_ZONEPATH),
45147c478bd9Sstevel@tonic-gate 		    zonepath);
4515087719fdSdp 	else {
4516087719fdSdp 		(void) fprintf(fp, gettext("%s not specified\n"),
4517087719fdSdp 		    pt_to_str(PT_ZONEPATH));
4518087719fdSdp 	}
45197c478bd9Sstevel@tonic-gate }
45207c478bd9Sstevel@tonic-gate 
45217c478bd9Sstevel@tonic-gate static void
45229acbbeafSnn35248 info_brand(zone_dochandle_t handle, FILE *fp)
45239acbbeafSnn35248 {
45249acbbeafSnn35248 	char brand[MAXNAMELEN];
45259acbbeafSnn35248 
45269acbbeafSnn35248 	if (zonecfg_get_brand(handle, brand, sizeof (brand)) == Z_OK)
45279acbbeafSnn35248 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_BRAND),
45289acbbeafSnn35248 		    brand);
45299acbbeafSnn35248 	else
45309acbbeafSnn35248 		(void) fprintf(fp, "%s %s\n", pt_to_str(PT_BRAND),
45319acbbeafSnn35248 		    gettext("not specified"));
45329acbbeafSnn35248 }
45339acbbeafSnn35248 
45349acbbeafSnn35248 static void
45357c478bd9Sstevel@tonic-gate info_autoboot(zone_dochandle_t handle, FILE *fp)
45367c478bd9Sstevel@tonic-gate {
45377c478bd9Sstevel@tonic-gate 	boolean_t autoboot;
45387c478bd9Sstevel@tonic-gate 	int err;
45397c478bd9Sstevel@tonic-gate 
45407c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_get_autoboot(handle, &autoboot)) == Z_OK)
45417c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_AUTOBOOT),
45427c478bd9Sstevel@tonic-gate 		    autoboot ? "true" : "false");
45437c478bd9Sstevel@tonic-gate 	else
4544bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
45457c478bd9Sstevel@tonic-gate }
45467c478bd9Sstevel@tonic-gate 
45477c478bd9Sstevel@tonic-gate static void
45487c478bd9Sstevel@tonic-gate info_pool(zone_dochandle_t handle, FILE *fp)
45497c478bd9Sstevel@tonic-gate {
45507c478bd9Sstevel@tonic-gate 	char pool[MAXNAMELEN];
45517c478bd9Sstevel@tonic-gate 	int err;
45527c478bd9Sstevel@tonic-gate 
45537c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_get_pool(handle, pool, sizeof (pool))) == Z_OK)
45547c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_POOL), pool);
45557c478bd9Sstevel@tonic-gate 	else
4556bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
45577c478bd9Sstevel@tonic-gate }
45587c478bd9Sstevel@tonic-gate 
45597c478bd9Sstevel@tonic-gate static void
4560ffbafc53Scomay info_limitpriv(zone_dochandle_t handle, FILE *fp)
4561ffbafc53Scomay {
4562ffbafc53Scomay 	char *limitpriv;
4563ffbafc53Scomay 	int err;
4564ffbafc53Scomay 
4565ffbafc53Scomay 	if ((err = zonecfg_get_limitpriv(handle, &limitpriv)) == Z_OK) {
4566ffbafc53Scomay 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_LIMITPRIV),
4567ffbafc53Scomay 		    limitpriv);
4568ffbafc53Scomay 		free(limitpriv);
4569ffbafc53Scomay 	} else {
4570bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
4571ffbafc53Scomay 	}
4572ffbafc53Scomay }
4573ffbafc53Scomay 
4574ffbafc53Scomay static void
45753f2f09c1Sdp info_bootargs(zone_dochandle_t handle, FILE *fp)
45763f2f09c1Sdp {
45773f2f09c1Sdp 	char bootargs[BOOTARGS_MAX];
45783f2f09c1Sdp 	int err;
45793f2f09c1Sdp 
45803f2f09c1Sdp 	if ((err = zonecfg_get_bootargs(handle, bootargs,
45813f2f09c1Sdp 	    sizeof (bootargs))) == Z_OK) {
45823f2f09c1Sdp 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_BOOTARGS),
45833f2f09c1Sdp 		    bootargs);
45843f2f09c1Sdp 	} else {
4585bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
45863f2f09c1Sdp 	}
45873f2f09c1Sdp }
45883f2f09c1Sdp 
45893f2f09c1Sdp static void
45900209230bSgjelinek info_sched(zone_dochandle_t handle, FILE *fp)
45910209230bSgjelinek {
45920209230bSgjelinek 	char sched[MAXNAMELEN];
45930209230bSgjelinek 	int err;
45940209230bSgjelinek 
45950209230bSgjelinek 	if ((err = zonecfg_get_sched_class(handle, sched, sizeof (sched)))
45960209230bSgjelinek 	    == Z_OK) {
45970209230bSgjelinek 		(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_SCHED), sched);
45980209230bSgjelinek 	} else {
4599bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
46000209230bSgjelinek 	}
46010209230bSgjelinek }
46020209230bSgjelinek 
46030209230bSgjelinek static void
4604f4b3ec61Sdh155122 info_iptype(zone_dochandle_t handle, FILE *fp)
4605f4b3ec61Sdh155122 {
4606f4b3ec61Sdh155122 	zone_iptype_t iptype;
4607f4b3ec61Sdh155122 	int err;
4608f4b3ec61Sdh155122 
4609f4b3ec61Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) == Z_OK) {
4610f4b3ec61Sdh155122 		switch (iptype) {
4611f4b3ec61Sdh155122 		case ZS_SHARED:
4612f4b3ec61Sdh155122 			(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_IPTYPE),
4613f4b3ec61Sdh155122 			    "shared");
4614f4b3ec61Sdh155122 			break;
4615f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
4616f4b3ec61Sdh155122 			(void) fprintf(fp, "%s: %s\n", pt_to_str(PT_IPTYPE),
4617f4b3ec61Sdh155122 			    "exclusive");
4618f4b3ec61Sdh155122 			break;
4619f4b3ec61Sdh155122 		}
4620f4b3ec61Sdh155122 	} else {
4621bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
4622f4b3ec61Sdh155122 	}
4623f4b3ec61Sdh155122 }
4624f4b3ec61Sdh155122 
4625f4b3ec61Sdh155122 static void
46267c478bd9Sstevel@tonic-gate output_fs(FILE *fp, struct zone_fstab *fstab)
46277c478bd9Sstevel@tonic-gate {
46287c478bd9Sstevel@tonic-gate 	zone_fsopt_t *this;
46297c478bd9Sstevel@tonic-gate 
46307c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_FS));
46317c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_DIR, fstab->zone_fs_dir, B_TRUE);
46327c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_SPECIAL, fstab->zone_fs_special, B_TRUE);
46337c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_RAW, fstab->zone_fs_raw, B_TRUE);
46347c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_TYPE, fstab->zone_fs_type, B_TRUE);
46357c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "\t%s: [", pt_to_str(PT_OPTIONS));
46367c478bd9Sstevel@tonic-gate 	for (this = fstab->zone_fs_options; this != NULL;
46377c478bd9Sstevel@tonic-gate 	    this = this->zone_fsopt_next) {
46387c478bd9Sstevel@tonic-gate 		if (strchr(this->zone_fsopt_opt, '='))
46397c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "\"%s\"", this->zone_fsopt_opt);
46407c478bd9Sstevel@tonic-gate 		else
46417c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, "%s", this->zone_fsopt_opt);
46427c478bd9Sstevel@tonic-gate 		if (this->zone_fsopt_next != NULL)
46437c478bd9Sstevel@tonic-gate 			(void) fprintf(fp, ",");
46447c478bd9Sstevel@tonic-gate 	}
46457c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "]\n");
46467c478bd9Sstevel@tonic-gate }
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate static void
46497c478bd9Sstevel@tonic-gate output_ipd(FILE *fp, struct zone_fstab *ipdtab)
46507c478bd9Sstevel@tonic-gate {
46517c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_IPD));
46527c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_DIR, ipdtab->zone_fs_dir, B_TRUE);
46537c478bd9Sstevel@tonic-gate }
46547c478bd9Sstevel@tonic-gate 
46557c478bd9Sstevel@tonic-gate static void
46567c478bd9Sstevel@tonic-gate info_fs(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
46577c478bd9Sstevel@tonic-gate {
46587c478bd9Sstevel@tonic-gate 	struct zone_fstab lookup, user;
4659bbec428eSgjelinek 	boolean_t output = B_FALSE;
46607c478bd9Sstevel@tonic-gate 
46617c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK)
46627c478bd9Sstevel@tonic-gate 		return;
46637c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &lookup) == Z_OK) {
46647c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
46657c478bd9Sstevel@tonic-gate 			output_fs(fp, &lookup);
46667c478bd9Sstevel@tonic-gate 			goto loopend;
46677c478bd9Sstevel@tonic-gate 		}
4668bbec428eSgjelinek 		if (fill_in_fstab(cmd, &user, B_TRUE) != Z_OK)
46697c478bd9Sstevel@tonic-gate 			goto loopend;
46707c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_fs_dir) > 0 &&
46717c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_fs_dir, lookup.zone_fs_dir) != 0)
46727c478bd9Sstevel@tonic-gate 			goto loopend;	/* no match */
46737c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_fs_special) > 0 &&
46747c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_fs_special, lookup.zone_fs_special) != 0)
46757c478bd9Sstevel@tonic-gate 			goto loopend;	/* no match */
46767c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_fs_type) > 0 &&
46777c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_fs_type, lookup.zone_fs_type) != 0)
46787c478bd9Sstevel@tonic-gate 			goto loopend;	/* no match */
46797c478bd9Sstevel@tonic-gate 		output_fs(fp, &lookup);
4680bbec428eSgjelinek 		output = B_TRUE;
46817c478bd9Sstevel@tonic-gate loopend:
46827c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(lookup.zone_fs_options);
46837c478bd9Sstevel@tonic-gate 	}
46847c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
46857c478bd9Sstevel@tonic-gate 	/*
46867c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
46877c478bd9Sstevel@tonic-gate 	 * nothing to output.
46887c478bd9Sstevel@tonic-gate 	 */
46897c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
46907c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
46917c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_FS));
46927c478bd9Sstevel@tonic-gate }
46937c478bd9Sstevel@tonic-gate 
46947c478bd9Sstevel@tonic-gate static void
46957c478bd9Sstevel@tonic-gate info_ipd(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
46967c478bd9Sstevel@tonic-gate {
46977c478bd9Sstevel@tonic-gate 	struct zone_fstab lookup, user;
4698bbec428eSgjelinek 	boolean_t output = B_FALSE;
46997c478bd9Sstevel@tonic-gate 
47007c478bd9Sstevel@tonic-gate 	if (zonecfg_setipdent(handle) != Z_OK)
47017c478bd9Sstevel@tonic-gate 		return;
47027c478bd9Sstevel@tonic-gate 	while (zonecfg_getipdent(handle, &lookup) == Z_OK) {
47037c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
47047c478bd9Sstevel@tonic-gate 			output_ipd(fp, &lookup);
47057c478bd9Sstevel@tonic-gate 			continue;
47067c478bd9Sstevel@tonic-gate 		}
4707bbec428eSgjelinek 		if (fill_in_ipdtab(cmd, &user, B_TRUE) != Z_OK)
47087c478bd9Sstevel@tonic-gate 			continue;
47097c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_fs_dir) > 0 &&
47107c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_fs_dir, lookup.zone_fs_dir) != 0)
47117c478bd9Sstevel@tonic-gate 			continue;	/* no match */
47127c478bd9Sstevel@tonic-gate 		output_ipd(fp, &lookup);
4713bbec428eSgjelinek 		output = B_TRUE;
47147c478bd9Sstevel@tonic-gate 	}
47157c478bd9Sstevel@tonic-gate 	(void) zonecfg_endipdent(handle);
47167c478bd9Sstevel@tonic-gate 	/*
47177c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
47187c478bd9Sstevel@tonic-gate 	 * nothing to output.
47197c478bd9Sstevel@tonic-gate 	 */
47207c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
47217c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
47227c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_IPD));
47237c478bd9Sstevel@tonic-gate }
47247c478bd9Sstevel@tonic-gate 
47257c478bd9Sstevel@tonic-gate static void
47267c478bd9Sstevel@tonic-gate output_net(FILE *fp, struct zone_nwiftab *nwiftab)
47277c478bd9Sstevel@tonic-gate {
47287c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_NET));
47297c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_ADDRESS, nwiftab->zone_nwif_address, B_TRUE);
47307c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_PHYSICAL, nwiftab->zone_nwif_physical, B_TRUE);
4731de860bd9Sgfaden 	output_prop(fp, PT_DEFROUTER, nwiftab->zone_nwif_defrouter, B_TRUE);
47327c478bd9Sstevel@tonic-gate }
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate static void
47357c478bd9Sstevel@tonic-gate info_net(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
47367c478bd9Sstevel@tonic-gate {
47377c478bd9Sstevel@tonic-gate 	struct zone_nwiftab lookup, user;
4738bbec428eSgjelinek 	boolean_t output = B_FALSE;
47397c478bd9Sstevel@tonic-gate 
47407c478bd9Sstevel@tonic-gate 	if (zonecfg_setnwifent(handle) != Z_OK)
47417c478bd9Sstevel@tonic-gate 		return;
47427c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &lookup) == Z_OK) {
47437c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
47447c478bd9Sstevel@tonic-gate 			output_net(fp, &lookup);
47457c478bd9Sstevel@tonic-gate 			continue;
47467c478bd9Sstevel@tonic-gate 		}
4747bbec428eSgjelinek 		if (fill_in_nwiftab(cmd, &user, B_TRUE) != Z_OK)
47487c478bd9Sstevel@tonic-gate 			continue;
47497c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_nwif_physical) > 0 &&
47507c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_nwif_physical,
47517c478bd9Sstevel@tonic-gate 		    lookup.zone_nwif_physical) != 0)
47527c478bd9Sstevel@tonic-gate 			continue;	/* no match */
4753f4b3ec61Sdh155122 		/* If present make sure it matches */
47547c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_nwif_address) > 0 &&
47557c478bd9Sstevel@tonic-gate 		    !zonecfg_same_net_address(user.zone_nwif_address,
47567c478bd9Sstevel@tonic-gate 		    lookup.zone_nwif_address))
47577c478bd9Sstevel@tonic-gate 			continue;	/* no match */
47587c478bd9Sstevel@tonic-gate 		output_net(fp, &lookup);
4759bbec428eSgjelinek 		output = B_TRUE;
47607c478bd9Sstevel@tonic-gate 	}
47617c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
47627c478bd9Sstevel@tonic-gate 	/*
47637c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
47647c478bd9Sstevel@tonic-gate 	 * nothing to output.
47657c478bd9Sstevel@tonic-gate 	 */
47667c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
47677c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
47687c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_NET));
47697c478bd9Sstevel@tonic-gate }
47707c478bd9Sstevel@tonic-gate 
47717c478bd9Sstevel@tonic-gate static void
47727c478bd9Sstevel@tonic-gate output_dev(FILE *fp, struct zone_devtab *devtab)
47737c478bd9Sstevel@tonic-gate {
477427e6fb21Sdp 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_DEVICE));
47757c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_MATCH, devtab->zone_dev_match, B_TRUE);
47767c478bd9Sstevel@tonic-gate }
47777c478bd9Sstevel@tonic-gate 
47787c478bd9Sstevel@tonic-gate static void
47797c478bd9Sstevel@tonic-gate info_dev(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
47807c478bd9Sstevel@tonic-gate {
47817c478bd9Sstevel@tonic-gate 	struct zone_devtab lookup, user;
4782bbec428eSgjelinek 	boolean_t output = B_FALSE;
47837c478bd9Sstevel@tonic-gate 
47847c478bd9Sstevel@tonic-gate 	if (zonecfg_setdevent(handle) != Z_OK)
47857c478bd9Sstevel@tonic-gate 		return;
47867c478bd9Sstevel@tonic-gate 	while (zonecfg_getdevent(handle, &lookup) == Z_OK) {
47877c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
47887c478bd9Sstevel@tonic-gate 			output_dev(fp, &lookup);
47897c478bd9Sstevel@tonic-gate 			continue;
47907c478bd9Sstevel@tonic-gate 		}
4791bbec428eSgjelinek 		if (fill_in_devtab(cmd, &user, B_TRUE) != Z_OK)
47927c478bd9Sstevel@tonic-gate 			continue;
47937c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_dev_match) > 0 &&
47947c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_dev_match, lookup.zone_dev_match) != 0)
47957c478bd9Sstevel@tonic-gate 			continue;	/* no match */
47967c478bd9Sstevel@tonic-gate 		output_dev(fp, &lookup);
4797bbec428eSgjelinek 		output = B_TRUE;
47987c478bd9Sstevel@tonic-gate 	}
47997c478bd9Sstevel@tonic-gate 	(void) zonecfg_enddevent(handle);
48007c478bd9Sstevel@tonic-gate 	/*
48017c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
48027c478bd9Sstevel@tonic-gate 	 * nothing to output.
48037c478bd9Sstevel@tonic-gate 	 */
48047c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
48057c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
48067c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_DEVICE));
48077c478bd9Sstevel@tonic-gate }
48087c478bd9Sstevel@tonic-gate 
48097c478bd9Sstevel@tonic-gate static void
48107c478bd9Sstevel@tonic-gate output_rctl(FILE *fp, struct zone_rctltab *rctltab)
48117c478bd9Sstevel@tonic-gate {
48127c478bd9Sstevel@tonic-gate 	struct zone_rctlvaltab *valptr;
48137c478bd9Sstevel@tonic-gate 
48147c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_RCTL));
48157c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_NAME, rctltab->zone_rctl_name, B_TRUE);
48167c478bd9Sstevel@tonic-gate 	for (valptr = rctltab->zone_rctl_valptr; valptr != NULL;
48177c478bd9Sstevel@tonic-gate 	    valptr = valptr->zone_rctlval_next) {
48187c478bd9Sstevel@tonic-gate 		fprintf(fp, "\t%s: (%s=%s,%s=%s,%s=%s)\n",
48197c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_VALUE),
48207c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_PRIV), valptr->zone_rctlval_priv,
48217c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_LIMIT), valptr->zone_rctlval_limit,
48227c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_ACTION), valptr->zone_rctlval_action);
48237c478bd9Sstevel@tonic-gate 	}
48247c478bd9Sstevel@tonic-gate }
48257c478bd9Sstevel@tonic-gate 
48267c478bd9Sstevel@tonic-gate static void
48277c478bd9Sstevel@tonic-gate info_rctl(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
48287c478bd9Sstevel@tonic-gate {
48297c478bd9Sstevel@tonic-gate 	struct zone_rctltab lookup, user;
4830bbec428eSgjelinek 	boolean_t output = B_FALSE;
48317c478bd9Sstevel@tonic-gate 
48327c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK)
48337c478bd9Sstevel@tonic-gate 		return;
48347c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &lookup) == Z_OK) {
48357c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
48367c478bd9Sstevel@tonic-gate 			output_rctl(fp, &lookup);
4837bbec428eSgjelinek 		} else if (fill_in_rctltab(cmd, &user, B_TRUE) == Z_OK &&
48387c478bd9Sstevel@tonic-gate 		    (strlen(user.zone_rctl_name) == 0 ||
48397c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_rctl_name, lookup.zone_rctl_name) == 0)) {
48407c478bd9Sstevel@tonic-gate 			output_rctl(fp, &lookup);
4841bbec428eSgjelinek 			output = B_TRUE;
48427c478bd9Sstevel@tonic-gate 		}
48437c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(lookup.zone_rctl_valptr);
48447c478bd9Sstevel@tonic-gate 	}
48457c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
48467c478bd9Sstevel@tonic-gate 	/*
48477c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
48487c478bd9Sstevel@tonic-gate 	 * nothing to output.
48497c478bd9Sstevel@tonic-gate 	 */
48507c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
48517c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
48527c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_RCTL));
48537c478bd9Sstevel@tonic-gate }
48547c478bd9Sstevel@tonic-gate 
48557c478bd9Sstevel@tonic-gate static void
48567c478bd9Sstevel@tonic-gate output_attr(FILE *fp, struct zone_attrtab *attrtab)
48577c478bd9Sstevel@tonic-gate {
48587c478bd9Sstevel@tonic-gate 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_ATTR));
48597c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_NAME, attrtab->zone_attr_name, B_TRUE);
48607c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_TYPE, attrtab->zone_attr_type, B_TRUE);
48617c478bd9Sstevel@tonic-gate 	output_prop(fp, PT_VALUE, attrtab->zone_attr_value, B_TRUE);
48627c478bd9Sstevel@tonic-gate }
48637c478bd9Sstevel@tonic-gate 
48647c478bd9Sstevel@tonic-gate static void
48657c478bd9Sstevel@tonic-gate info_attr(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
48667c478bd9Sstevel@tonic-gate {
48677c478bd9Sstevel@tonic-gate 	struct zone_attrtab lookup, user;
4868bbec428eSgjelinek 	boolean_t output = B_FALSE;
48697c478bd9Sstevel@tonic-gate 
48707c478bd9Sstevel@tonic-gate 	if (zonecfg_setattrent(handle) != Z_OK)
48717c478bd9Sstevel@tonic-gate 		return;
48727c478bd9Sstevel@tonic-gate 	while (zonecfg_getattrent(handle, &lookup) == Z_OK) {
48737c478bd9Sstevel@tonic-gate 		if (cmd->cmd_prop_nv_pairs == 0) {
48747c478bd9Sstevel@tonic-gate 			output_attr(fp, &lookup);
48757c478bd9Sstevel@tonic-gate 			continue;
48767c478bd9Sstevel@tonic-gate 		}
4877bbec428eSgjelinek 		if (fill_in_attrtab(cmd, &user, B_TRUE) != Z_OK)
48787c478bd9Sstevel@tonic-gate 			continue;
48797c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_attr_name) > 0 &&
48807c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_attr_name, lookup.zone_attr_name) != 0)
48817c478bd9Sstevel@tonic-gate 			continue;	/* no match */
48827c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_attr_type) > 0 &&
48837c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_attr_type, lookup.zone_attr_type) != 0)
48847c478bd9Sstevel@tonic-gate 			continue;	/* no match */
48857c478bd9Sstevel@tonic-gate 		if (strlen(user.zone_attr_value) > 0 &&
48867c478bd9Sstevel@tonic-gate 		    strcmp(user.zone_attr_value, lookup.zone_attr_value) != 0)
48877c478bd9Sstevel@tonic-gate 			continue;	/* no match */
48887c478bd9Sstevel@tonic-gate 		output_attr(fp, &lookup);
4889bbec428eSgjelinek 		output = B_TRUE;
48907c478bd9Sstevel@tonic-gate 	}
48917c478bd9Sstevel@tonic-gate 	(void) zonecfg_endattrent(handle);
48927c478bd9Sstevel@tonic-gate 	/*
48937c478bd9Sstevel@tonic-gate 	 * If a property n/v pair was specified, warn the user if there was
48947c478bd9Sstevel@tonic-gate 	 * nothing to output.
48957c478bd9Sstevel@tonic-gate 	 */
48967c478bd9Sstevel@tonic-gate 	if (!output && cmd->cmd_prop_nv_pairs > 0)
48977c478bd9Sstevel@tonic-gate 		(void) printf(gettext("No such %s resource.\n"),
48987c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR));
48997c478bd9Sstevel@tonic-gate }
49007c478bd9Sstevel@tonic-gate 
4901fa9e4066Sahrens static void
4902fa9e4066Sahrens output_ds(FILE *fp, struct zone_dstab *dstab)
4903fa9e4066Sahrens {
4904fa9e4066Sahrens 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_DATASET));
4905fa9e4066Sahrens 	output_prop(fp, PT_NAME, dstab->zone_dataset_name, B_TRUE);
4906fa9e4066Sahrens }
4907fa9e4066Sahrens 
4908fa9e4066Sahrens static void
4909fa9e4066Sahrens info_ds(zone_dochandle_t handle, FILE *fp, cmd_t *cmd)
4910fa9e4066Sahrens {
4911fa9e4066Sahrens 	struct zone_dstab lookup, user;
4912bbec428eSgjelinek 	boolean_t output = B_FALSE;
4913fa9e4066Sahrens 
49140209230bSgjelinek 	if (zonecfg_setdsent(handle) != Z_OK)
4915fa9e4066Sahrens 		return;
4916fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &lookup) == Z_OK) {
4917fa9e4066Sahrens 		if (cmd->cmd_prop_nv_pairs == 0) {
4918fa9e4066Sahrens 			output_ds(fp, &lookup);
4919fa9e4066Sahrens 			continue;
4920fa9e4066Sahrens 		}
4921bbec428eSgjelinek 		if (fill_in_dstab(cmd, &user, B_TRUE) != Z_OK)
4922fa9e4066Sahrens 			continue;
4923fa9e4066Sahrens 		if (strlen(user.zone_dataset_name) > 0 &&
4924fa9e4066Sahrens 		    strcmp(user.zone_dataset_name,
4925fa9e4066Sahrens 		    lookup.zone_dataset_name) != 0)
4926fa9e4066Sahrens 			continue;	/* no match */
4927fa9e4066Sahrens 		output_ds(fp, &lookup);
4928bbec428eSgjelinek 		output = B_TRUE;
4929fa9e4066Sahrens 	}
4930fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
4931fa9e4066Sahrens 	/*
4932fa9e4066Sahrens 	 * If a property n/v pair was specified, warn the user if there was
4933fa9e4066Sahrens 	 * nothing to output.
4934fa9e4066Sahrens 	 */
4935fa9e4066Sahrens 	if (!output && cmd->cmd_prop_nv_pairs > 0)
4936fa9e4066Sahrens 		(void) printf(gettext("No such %s resource.\n"),
4937fa9e4066Sahrens 		    rt_to_str(RT_DATASET));
4938fa9e4066Sahrens }
4939fa9e4066Sahrens 
49400209230bSgjelinek static void
49410209230bSgjelinek output_pset(FILE *fp, struct zone_psettab *psettab)
49420209230bSgjelinek {
49430209230bSgjelinek 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_DCPU));
49440209230bSgjelinek 	if (strcmp(psettab->zone_ncpu_min, psettab->zone_ncpu_max) == 0)
49450209230bSgjelinek 		(void) fprintf(fp, "\t%s: %s\n", pt_to_str(PT_NCPUS),
49460209230bSgjelinek 		    psettab->zone_ncpu_max);
49470209230bSgjelinek 	else
49480209230bSgjelinek 		(void) fprintf(fp, "\t%s: %s-%s\n", pt_to_str(PT_NCPUS),
49490209230bSgjelinek 		    psettab->zone_ncpu_min, psettab->zone_ncpu_max);
49500209230bSgjelinek 	if (psettab->zone_importance[0] != '\0')
49510209230bSgjelinek 		(void) fprintf(fp, "\t%s: %s\n", pt_to_str(PT_IMPORTANCE),
49520209230bSgjelinek 		    psettab->zone_importance);
49530209230bSgjelinek }
49540209230bSgjelinek 
49550209230bSgjelinek static void
49560209230bSgjelinek info_pset(zone_dochandle_t handle, FILE *fp)
49570209230bSgjelinek {
49580209230bSgjelinek 	struct zone_psettab lookup;
49590209230bSgjelinek 
49600209230bSgjelinek 	if (zonecfg_getpsetent(handle, &lookup) == Z_OK)
49610209230bSgjelinek 		output_pset(fp, &lookup);
49620209230bSgjelinek }
49630209230bSgjelinek 
49640209230bSgjelinek static void
4965c97ad5cdSakolb output_pcap(FILE *fp)
4966c97ad5cdSakolb {
4967c97ad5cdSakolb 	uint64_t cap;
4968c97ad5cdSakolb 
4969c97ad5cdSakolb 	if (zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &cap) == Z_OK) {
4970c97ad5cdSakolb 		float scaled = (float)cap / 100;
4971c97ad5cdSakolb 		(void) fprintf(fp, "%s:\n", rt_to_str(RT_PCAP));
4972c97ad5cdSakolb 		(void) fprintf(fp, "\t[%s: %.2f]\n", pt_to_str(PT_NCPUS),
4973c97ad5cdSakolb 		    scaled);
4974c97ad5cdSakolb 	}
4975c97ad5cdSakolb }
4976c97ad5cdSakolb 
4977c97ad5cdSakolb static void
4978c97ad5cdSakolb info_pcap(FILE *fp)
4979c97ad5cdSakolb {
4980c97ad5cdSakolb 	output_pcap(fp);
4981c97ad5cdSakolb }
4982c97ad5cdSakolb 
4983c97ad5cdSakolb 
4984c97ad5cdSakolb static void
49850209230bSgjelinek info_aliased_rctl(zone_dochandle_t handle, FILE *fp, char *alias)
49860209230bSgjelinek {
49870209230bSgjelinek 	uint64_t limit;
49880209230bSgjelinek 
49890209230bSgjelinek 	if (zonecfg_get_aliased_rctl(handle, alias, &limit) == Z_OK) {
49900209230bSgjelinek 		/* convert memory based properties */
49910209230bSgjelinek 		if (strcmp(alias, ALIAS_MAXSHMMEM) == 0) {
49920209230bSgjelinek 			char buf[128];
49930209230bSgjelinek 
49940209230bSgjelinek 			(void) snprintf(buf, sizeof (buf), "%llu", limit);
49950209230bSgjelinek 			bytes_to_units(buf, buf, sizeof (buf));
49960209230bSgjelinek 			(void) fprintf(fp, "[%s: %s]\n", alias, buf);
49970209230bSgjelinek 			return;
49980209230bSgjelinek 		}
49990209230bSgjelinek 
50000209230bSgjelinek 		(void) fprintf(fp, "[%s: %llu]\n", alias, limit);
50010209230bSgjelinek 	}
50020209230bSgjelinek }
50030209230bSgjelinek 
50040209230bSgjelinek static void
50050209230bSgjelinek bytes_to_units(char *str, char *buf, int bufsize)
50060209230bSgjelinek {
50070209230bSgjelinek 	unsigned long long num;
50080209230bSgjelinek 	unsigned long long save = 0;
50090209230bSgjelinek 	char *units = "BKMGT";
50100209230bSgjelinek 	char *up = units;
50110209230bSgjelinek 
50120209230bSgjelinek 	num = strtoll(str, NULL, 10);
50130209230bSgjelinek 
50140209230bSgjelinek 	if (num < 1024) {
50150209230bSgjelinek 		(void) snprintf(buf, bufsize, "%llu", num);
50160209230bSgjelinek 		return;
50170209230bSgjelinek 	}
50180209230bSgjelinek 
50190209230bSgjelinek 	while ((num >= 1024) && (*up != 'T')) {
50200209230bSgjelinek 		up++; /* next unit of measurement */
50210209230bSgjelinek 		save = num;
50220209230bSgjelinek 		num = (num + 512) >> 10;
50230209230bSgjelinek 	}
50240209230bSgjelinek 
50250209230bSgjelinek 	/* check if we should output a fraction.  snprintf will round for us */
50260209230bSgjelinek 	if (save % 1024 != 0 && ((save >> 10) < 10))
50270209230bSgjelinek 		(void) snprintf(buf, bufsize, "%2.1f%c", ((float)save / 1024),
50280209230bSgjelinek 		    *up);
50290209230bSgjelinek 	else
50300209230bSgjelinek 		(void) snprintf(buf, bufsize, "%llu%c", num, *up);
50310209230bSgjelinek }
50320209230bSgjelinek 
50330209230bSgjelinek static void
50340209230bSgjelinek output_mcap(FILE *fp, struct zone_mcaptab *mcaptab, int showswap,
50350209230bSgjelinek     uint64_t maxswap, int showlocked, uint64_t maxlocked)
50360209230bSgjelinek {
50370209230bSgjelinek 	char buf[128];
50380209230bSgjelinek 
50390209230bSgjelinek 	(void) fprintf(fp, "%s:\n", rt_to_str(RT_MCAP));
50400209230bSgjelinek 	if (mcaptab->zone_physmem_cap[0] != '\0') {
50410209230bSgjelinek 		bytes_to_units(mcaptab->zone_physmem_cap, buf, sizeof (buf));
50420209230bSgjelinek 		output_prop(fp, PT_PHYSICAL, buf, B_TRUE);
50430209230bSgjelinek 	}
50440209230bSgjelinek 
50450209230bSgjelinek 	if (showswap == Z_OK) {
50460209230bSgjelinek 		(void) snprintf(buf, sizeof (buf), "%llu", maxswap);
50470209230bSgjelinek 		bytes_to_units(buf, buf, sizeof (buf));
50480209230bSgjelinek 		output_prop(fp, PT_SWAP, buf, B_TRUE);
50490209230bSgjelinek 	}
50500209230bSgjelinek 
50510209230bSgjelinek 	if (showlocked == Z_OK) {
50520209230bSgjelinek 		(void) snprintf(buf, sizeof (buf), "%llu", maxlocked);
50530209230bSgjelinek 		bytes_to_units(buf, buf, sizeof (buf));
50540209230bSgjelinek 		output_prop(fp, PT_LOCKED, buf, B_TRUE);
50550209230bSgjelinek 	}
50560209230bSgjelinek }
50570209230bSgjelinek 
50580209230bSgjelinek static void
50590209230bSgjelinek info_mcap(zone_dochandle_t handle, FILE *fp)
50600209230bSgjelinek {
50610209230bSgjelinek 	int res1, res2, res3;
50620209230bSgjelinek 	uint64_t swap_limit;
50630209230bSgjelinek 	uint64_t locked_limit;
50640209230bSgjelinek 	struct zone_mcaptab lookup;
50650209230bSgjelinek 
50660209230bSgjelinek 	bzero(&lookup, sizeof (lookup));
50670209230bSgjelinek 	res1 = zonecfg_getmcapent(handle, &lookup);
50680209230bSgjelinek 	res2 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP, &swap_limit);
50690209230bSgjelinek 	res3 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM,
50700209230bSgjelinek 	    &locked_limit);
50710209230bSgjelinek 
50720209230bSgjelinek 	if (res1 == Z_OK || res2 == Z_OK || res3 == Z_OK)
50730209230bSgjelinek 		output_mcap(fp, &lookup, res2, swap_limit, res3, locked_limit);
50740209230bSgjelinek }
50750209230bSgjelinek 
50767c478bd9Sstevel@tonic-gate void
50777c478bd9Sstevel@tonic-gate info_func(cmd_t *cmd)
50787c478bd9Sstevel@tonic-gate {
50797c478bd9Sstevel@tonic-gate 	FILE *fp = stdout;
5080bbec428eSgjelinek 	boolean_t need_to_close = B_FALSE;
50817c478bd9Sstevel@tonic-gate 	char *pager;
50820209230bSgjelinek 	int type;
50830209230bSgjelinek 	int res1, res2;
50840209230bSgjelinek 	uint64_t swap_limit;
50850209230bSgjelinek 	uint64_t locked_limit;
50867c478bd9Sstevel@tonic-gate 
50877c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
50887c478bd9Sstevel@tonic-gate 
5089bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
50907c478bd9Sstevel@tonic-gate 		return;
50917c478bd9Sstevel@tonic-gate 
50927c478bd9Sstevel@tonic-gate 	/* don't page error output */
50937c478bd9Sstevel@tonic-gate 	if (interactive_mode) {
50947c478bd9Sstevel@tonic-gate 		if ((pager = getenv("PAGER")) == NULL)
50957c478bd9Sstevel@tonic-gate 			pager = PAGER;
50967c478bd9Sstevel@tonic-gate 		if ((fp = popen(pager, "w")) != NULL)
5097bbec428eSgjelinek 			need_to_close = B_TRUE;
50987c478bd9Sstevel@tonic-gate 		setbuf(fp, NULL);
50997c478bd9Sstevel@tonic-gate 	}
51007c478bd9Sstevel@tonic-gate 
51017c478bd9Sstevel@tonic-gate 	if (!global_scope) {
51027c478bd9Sstevel@tonic-gate 		switch (resource_scope) {
51037c478bd9Sstevel@tonic-gate 		case RT_FS:
51047c478bd9Sstevel@tonic-gate 			output_fs(fp, &in_progress_fstab);
51057c478bd9Sstevel@tonic-gate 			break;
51067c478bd9Sstevel@tonic-gate 		case RT_IPD:
51077c478bd9Sstevel@tonic-gate 			output_ipd(fp, &in_progress_ipdtab);
51087c478bd9Sstevel@tonic-gate 			break;
51097c478bd9Sstevel@tonic-gate 		case RT_NET:
51107c478bd9Sstevel@tonic-gate 			output_net(fp, &in_progress_nwiftab);
51117c478bd9Sstevel@tonic-gate 			break;
51127c478bd9Sstevel@tonic-gate 		case RT_DEVICE:
51137c478bd9Sstevel@tonic-gate 			output_dev(fp, &in_progress_devtab);
51147c478bd9Sstevel@tonic-gate 			break;
51157c478bd9Sstevel@tonic-gate 		case RT_RCTL:
51167c478bd9Sstevel@tonic-gate 			output_rctl(fp, &in_progress_rctltab);
51177c478bd9Sstevel@tonic-gate 			break;
51187c478bd9Sstevel@tonic-gate 		case RT_ATTR:
51197c478bd9Sstevel@tonic-gate 			output_attr(fp, &in_progress_attrtab);
51207c478bd9Sstevel@tonic-gate 			break;
5121fa9e4066Sahrens 		case RT_DATASET:
5122fa9e4066Sahrens 			output_ds(fp, &in_progress_dstab);
5123fa9e4066Sahrens 			break;
51240209230bSgjelinek 		case RT_DCPU:
51250209230bSgjelinek 			output_pset(fp, &in_progress_psettab);
51260209230bSgjelinek 			break;
5127c97ad5cdSakolb 		case RT_PCAP:
5128c97ad5cdSakolb 			output_pcap(fp);
5129c97ad5cdSakolb 			break;
51300209230bSgjelinek 		case RT_MCAP:
51310209230bSgjelinek 			res1 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP,
51320209230bSgjelinek 			    &swap_limit);
51330209230bSgjelinek 			res2 = zonecfg_get_aliased_rctl(handle,
51340209230bSgjelinek 			    ALIAS_MAXLOCKEDMEM, &locked_limit);
51350209230bSgjelinek 			output_mcap(fp, &in_progress_mcaptab, res1, swap_limit,
51360209230bSgjelinek 			    res2, locked_limit);
51370209230bSgjelinek 			break;
51387c478bd9Sstevel@tonic-gate 		}
51397c478bd9Sstevel@tonic-gate 		goto cleanup;
51407c478bd9Sstevel@tonic-gate 	}
51417c478bd9Sstevel@tonic-gate 
51420209230bSgjelinek 	type = cmd->cmd_res_type;
51430209230bSgjelinek 
51440209230bSgjelinek 	if (gz_invalid_rt_property(type)) {
51450209230bSgjelinek 		zerr(gettext("%s is not a valid property for the global zone."),
51460209230bSgjelinek 		    rt_to_str(type));
51470209230bSgjelinek 		goto cleanup;
51480209230bSgjelinek 	}
51490209230bSgjelinek 
51500209230bSgjelinek 	if (gz_invalid_resource(type)) {
51510209230bSgjelinek 		zerr(gettext("%s is not a valid resource for the global zone."),
51520209230bSgjelinek 		    rt_to_str(type));
51530209230bSgjelinek 		goto cleanup;
51540209230bSgjelinek 	}
51550209230bSgjelinek 
51567c478bd9Sstevel@tonic-gate 	switch (cmd->cmd_res_type) {
51577c478bd9Sstevel@tonic-gate 	case RT_UNKNOWN:
5158087719fdSdp 		info_zonename(handle, fp);
51590209230bSgjelinek 		if (!global_zone) {
51607c478bd9Sstevel@tonic-gate 			info_zonepath(handle, fp);
51619acbbeafSnn35248 			info_brand(handle, fp);
51627c478bd9Sstevel@tonic-gate 			info_autoboot(handle, fp);
51633f2f09c1Sdp 			info_bootargs(handle, fp);
51640209230bSgjelinek 		}
51657c478bd9Sstevel@tonic-gate 		info_pool(handle, fp);
51660209230bSgjelinek 		if (!global_zone) {
5167ffbafc53Scomay 			info_limitpriv(handle, fp);
51680209230bSgjelinek 			info_sched(handle, fp);
5169f4b3ec61Sdh155122 			info_iptype(handle, fp);
51700209230bSgjelinek 		}
51710209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXLWPS);
51720209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSHMMEM);
51730209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSHMIDS);
51740209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXMSGIDS);
51750209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSEMIDS);
51760209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_SHARES);
51770209230bSgjelinek 		if (!global_zone) {
51787c478bd9Sstevel@tonic-gate 			info_ipd(handle, fp, cmd);
51797c478bd9Sstevel@tonic-gate 			info_fs(handle, fp, cmd);
51807c478bd9Sstevel@tonic-gate 			info_net(handle, fp, cmd);
51817c478bd9Sstevel@tonic-gate 			info_dev(handle, fp, cmd);
51820209230bSgjelinek 		}
51830209230bSgjelinek 		info_pset(handle, fp);
5184c97ad5cdSakolb 		info_pcap(fp);
51850209230bSgjelinek 		info_mcap(handle, fp);
51860209230bSgjelinek 		if (!global_zone) {
51877c478bd9Sstevel@tonic-gate 			info_attr(handle, fp, cmd);
5188fa9e4066Sahrens 			info_ds(handle, fp, cmd);
51890209230bSgjelinek 		}
51900209230bSgjelinek 		info_rctl(handle, fp, cmd);
51917c478bd9Sstevel@tonic-gate 		break;
5192087719fdSdp 	case RT_ZONENAME:
5193087719fdSdp 		info_zonename(handle, fp);
5194087719fdSdp 		break;
51957c478bd9Sstevel@tonic-gate 	case RT_ZONEPATH:
51967c478bd9Sstevel@tonic-gate 		info_zonepath(handle, fp);
51977c478bd9Sstevel@tonic-gate 		break;
51989acbbeafSnn35248 	case RT_BRAND:
51999acbbeafSnn35248 		info_brand(handle, fp);
52009acbbeafSnn35248 		break;
52017c478bd9Sstevel@tonic-gate 	case RT_AUTOBOOT:
52027c478bd9Sstevel@tonic-gate 		info_autoboot(handle, fp);
52037c478bd9Sstevel@tonic-gate 		break;
52047c478bd9Sstevel@tonic-gate 	case RT_POOL:
52057c478bd9Sstevel@tonic-gate 		info_pool(handle, fp);
52067c478bd9Sstevel@tonic-gate 		break;
5207ffbafc53Scomay 	case RT_LIMITPRIV:
5208ffbafc53Scomay 		info_limitpriv(handle, fp);
5209ffbafc53Scomay 		break;
52103f2f09c1Sdp 	case RT_BOOTARGS:
52113f2f09c1Sdp 		info_bootargs(handle, fp);
52123f2f09c1Sdp 		break;
52130209230bSgjelinek 	case RT_SCHED:
52140209230bSgjelinek 		info_sched(handle, fp);
52150209230bSgjelinek 		break;
5216f4b3ec61Sdh155122 	case RT_IPTYPE:
5217f4b3ec61Sdh155122 		info_iptype(handle, fp);
5218f4b3ec61Sdh155122 		break;
52190209230bSgjelinek 	case RT_MAXLWPS:
52200209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXLWPS);
52210209230bSgjelinek 		break;
52220209230bSgjelinek 	case RT_MAXSHMMEM:
52230209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSHMMEM);
52240209230bSgjelinek 		break;
52250209230bSgjelinek 	case RT_MAXSHMIDS:
52260209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSHMIDS);
52270209230bSgjelinek 		break;
52280209230bSgjelinek 	case RT_MAXMSGIDS:
52290209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXMSGIDS);
52300209230bSgjelinek 		break;
52310209230bSgjelinek 	case RT_MAXSEMIDS:
52320209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_MAXSEMIDS);
52330209230bSgjelinek 		break;
52340209230bSgjelinek 	case RT_SHARES:
52350209230bSgjelinek 		info_aliased_rctl(handle, fp, ALIAS_SHARES);
52360209230bSgjelinek 		break;
52377c478bd9Sstevel@tonic-gate 	case RT_FS:
52387c478bd9Sstevel@tonic-gate 		info_fs(handle, fp, cmd);
52397c478bd9Sstevel@tonic-gate 		break;
52407c478bd9Sstevel@tonic-gate 	case RT_IPD:
52417c478bd9Sstevel@tonic-gate 		info_ipd(handle, fp, cmd);
52427c478bd9Sstevel@tonic-gate 		break;
52437c478bd9Sstevel@tonic-gate 	case RT_NET:
52447c478bd9Sstevel@tonic-gate 		info_net(handle, fp, cmd);
52457c478bd9Sstevel@tonic-gate 		break;
52467c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
52477c478bd9Sstevel@tonic-gate 		info_dev(handle, fp, cmd);
52487c478bd9Sstevel@tonic-gate 		break;
52497c478bd9Sstevel@tonic-gate 	case RT_RCTL:
52507c478bd9Sstevel@tonic-gate 		info_rctl(handle, fp, cmd);
52517c478bd9Sstevel@tonic-gate 		break;
52527c478bd9Sstevel@tonic-gate 	case RT_ATTR:
52537c478bd9Sstevel@tonic-gate 		info_attr(handle, fp, cmd);
52547c478bd9Sstevel@tonic-gate 		break;
5255fa9e4066Sahrens 	case RT_DATASET:
5256fa9e4066Sahrens 		info_ds(handle, fp, cmd);
5257fa9e4066Sahrens 		break;
52580209230bSgjelinek 	case RT_DCPU:
52590209230bSgjelinek 		info_pset(handle, fp);
52600209230bSgjelinek 		break;
5261c97ad5cdSakolb 	case RT_PCAP:
5262c97ad5cdSakolb 		info_pcap(fp);
5263c97ad5cdSakolb 		break;
52640209230bSgjelinek 	case RT_MCAP:
52650209230bSgjelinek 		info_mcap(handle, fp);
52660209230bSgjelinek 		break;
52677c478bd9Sstevel@tonic-gate 	default:
52687c478bd9Sstevel@tonic-gate 		zone_perror(rt_to_str(cmd->cmd_res_type), Z_NO_RESOURCE_TYPE,
5269bbec428eSgjelinek 		    B_TRUE);
52707c478bd9Sstevel@tonic-gate 	}
52717c478bd9Sstevel@tonic-gate 
52727c478bd9Sstevel@tonic-gate cleanup:
52737c478bd9Sstevel@tonic-gate 	if (need_to_close)
52747c478bd9Sstevel@tonic-gate 		(void) pclose(fp);
52757c478bd9Sstevel@tonic-gate }
52767c478bd9Sstevel@tonic-gate 
5277087719fdSdp /*
5278087719fdSdp  * Helper function for verify-- checks that a required string property
5279087719fdSdp  * exists.
5280087719fdSdp  */
5281087719fdSdp static void
5282087719fdSdp check_reqd_prop(char *attr, int rt, int pt, int *ret_val)
52837c478bd9Sstevel@tonic-gate {
5284087719fdSdp 	if (strlen(attr) == 0) {
5285087719fdSdp 		zerr(gettext("%s: %s not specified"), rt_to_str(rt),
5286087719fdSdp 		    pt_to_str(pt));
5287bbec428eSgjelinek 		saw_error = B_TRUE;
5288087719fdSdp 		if (*ret_val == Z_OK)
5289087719fdSdp 			*ret_val = Z_REQD_PROPERTY_MISSING;
52907c478bd9Sstevel@tonic-gate 	}
52917c478bd9Sstevel@tonic-gate }
52927c478bd9Sstevel@tonic-gate 
52939acbbeafSnn35248 static int
52949acbbeafSnn35248 do_subproc(char *cmdbuf)
52959acbbeafSnn35248 {
52969acbbeafSnn35248 	char inbuf[MAX_CMD_LEN];
52979acbbeafSnn35248 	FILE *file;
52989acbbeafSnn35248 	int status;
52999acbbeafSnn35248 
53009acbbeafSnn35248 	file = popen(cmdbuf, "r");
53019acbbeafSnn35248 	if (file == NULL) {
53029acbbeafSnn35248 		zerr(gettext("Could not launch: %s"), cmdbuf);
53039acbbeafSnn35248 		return (-1);
53049acbbeafSnn35248 	}
53059acbbeafSnn35248 
53069acbbeafSnn35248 	while (fgets(inbuf, sizeof (inbuf), file) != NULL)
53079acbbeafSnn35248 		fprintf(stderr, "%s", inbuf);
53089acbbeafSnn35248 	status = pclose(file);
53099acbbeafSnn35248 
53109acbbeafSnn35248 	if (WIFSIGNALED(status)) {
53119acbbeafSnn35248 		zerr(gettext("%s unexpectedly terminated due to signal %d"),
53129acbbeafSnn35248 		    cmdbuf, WTERMSIG(status));
53139acbbeafSnn35248 		return (-1);
53149acbbeafSnn35248 	}
53159acbbeafSnn35248 	assert(WIFEXITED(status));
53169acbbeafSnn35248 	return (WEXITSTATUS(status));
53179acbbeafSnn35248 }
53189acbbeafSnn35248 
53199acbbeafSnn35248 static int
53209acbbeafSnn35248 brand_verify(zone_dochandle_t handle)
53219acbbeafSnn35248 {
53226e65f9afSnn35248 	char xml_file[32];
53239acbbeafSnn35248 	char cmdbuf[MAX_CMD_LEN];
5324123807fbSedp 	brand_handle_t bh;
53259acbbeafSnn35248 	char brand[MAXNAMELEN];
53269acbbeafSnn35248 	int err;
53279acbbeafSnn35248 
53289acbbeafSnn35248 	if (zonecfg_get_brand(handle, brand, sizeof (brand)) != Z_OK) {
53299acbbeafSnn35248 		zerr("%s: %s\n", zone, gettext("could not get zone brand"));
53309acbbeafSnn35248 		return (Z_INVALID_DOCUMENT);
53319acbbeafSnn35248 	}
5332123807fbSedp 	if ((bh = brand_open(brand)) == NULL) {
53339acbbeafSnn35248 		zerr("%s: %s\n", zone, gettext("unknown brand."));
53349acbbeafSnn35248 		return (Z_INVALID_DOCUMENT);
53359acbbeafSnn35248 	}
53369acbbeafSnn35248 
53379acbbeafSnn35248 	/*
53389acbbeafSnn35248 	 * Fetch the verify command, if any, from the brand configuration
53399acbbeafSnn35248 	 * and build the command line to execute it.
53409acbbeafSnn35248 	 */
53419acbbeafSnn35248 	strcpy(cmdbuf, EXEC_PREFIX);
5342123807fbSedp 	err = brand_get_verify_cfg(bh, cmdbuf + EXEC_LEN,
53439acbbeafSnn35248 	    sizeof (cmdbuf) - (EXEC_LEN + (strlen(xml_file) + 1)));
5344123807fbSedp 	brand_close(bh);
53459acbbeafSnn35248 	if (err != Z_OK) {
53469acbbeafSnn35248 		zerr("%s: %s\n", zone,
53479acbbeafSnn35248 		    gettext("could not get brand verification command"));
53489acbbeafSnn35248 		return (Z_INVALID_DOCUMENT);
53499acbbeafSnn35248 	}
53509acbbeafSnn35248 
53519acbbeafSnn35248 	/*
53529acbbeafSnn35248 	 * If the brand doesn't provide a verification routine, we just
53539acbbeafSnn35248 	 * return success.
53549acbbeafSnn35248 	 */
53559acbbeafSnn35248 	if (strlen(cmdbuf) == EXEC_LEN)
53569acbbeafSnn35248 		return (Z_OK);
53579acbbeafSnn35248 
53589acbbeafSnn35248 	/*
53599acbbeafSnn35248 	 * Dump the current config information for this zone to a file.
53609acbbeafSnn35248 	 */
53616e65f9afSnn35248 	strcpy(xml_file, "/tmp/zonecfg_verify.XXXXXX");
53629acbbeafSnn35248 	if (mkstemp(xml_file) == NULL)
53639acbbeafSnn35248 		return (Z_TEMP_FILE);
53649acbbeafSnn35248 	if ((err = zonecfg_verify_save(handle, xml_file)) != Z_OK) {
53659acbbeafSnn35248 		(void) unlink(xml_file);
53669acbbeafSnn35248 		return (err);
53679acbbeafSnn35248 	}
53689acbbeafSnn35248 
53699acbbeafSnn35248 	/*
53709acbbeafSnn35248 	 * Execute the verification command.
53719acbbeafSnn35248 	 */
53729acbbeafSnn35248 	if ((strlcat(cmdbuf, " ", MAX_CMD_LEN) >= MAX_CMD_LEN) ||
53739acbbeafSnn35248 	    (strlcat(cmdbuf, xml_file, MAX_CMD_LEN) >= MAX_CMD_LEN)) {
53749acbbeafSnn35248 		err = Z_BRAND_ERROR;
53759acbbeafSnn35248 	} else {
53769acbbeafSnn35248 		err = do_subproc(cmdbuf);
53779acbbeafSnn35248 	}
53789acbbeafSnn35248 
53799acbbeafSnn35248 	(void) unlink(xml_file);
53809acbbeafSnn35248 	return ((err == Z_OK) ? Z_OK : Z_BRAND_ERROR);
53819acbbeafSnn35248 }
53829acbbeafSnn35248 
53837c478bd9Sstevel@tonic-gate /*
53847c478bd9Sstevel@tonic-gate  * See the DTD for which attributes are required for which resources.
53857c478bd9Sstevel@tonic-gate  *
53867c478bd9Sstevel@tonic-gate  * This function can be called by commit_func(), which needs to save things,
53877c478bd9Sstevel@tonic-gate  * in addition to the general call from parse_and_run(), which doesn't need
53887c478bd9Sstevel@tonic-gate  * things saved.  Since the parameters are standardized, we distinguish by
53897c478bd9Sstevel@tonic-gate  * having commit_func() call here with cmd->cmd_arg set to "save" to indicate
53907c478bd9Sstevel@tonic-gate  * that a save is needed.
53917c478bd9Sstevel@tonic-gate  */
53927c478bd9Sstevel@tonic-gate void
53937c478bd9Sstevel@tonic-gate verify_func(cmd_t *cmd)
53947c478bd9Sstevel@tonic-gate {
53957c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
53967c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
53977c478bd9Sstevel@tonic-gate 	struct zone_attrtab attrtab;
53987c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
5399fa9e4066Sahrens 	struct zone_dstab dstab;
54000209230bSgjelinek 	struct zone_psettab psettab;
54017c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
54020209230bSgjelinek 	char sched[MAXNAMELEN];
54039acbbeafSnn35248 	char brand[MAXNAMELEN];
54047c478bd9Sstevel@tonic-gate 	int err, ret_val = Z_OK, arg;
5405c97ad5cdSakolb 	int pset_res;
5406bbec428eSgjelinek 	boolean_t save = B_FALSE;
5407bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
5408f4b3ec61Sdh155122 	zone_iptype_t iptype;
54090209230bSgjelinek 	boolean_t has_cpu_shares = B_FALSE;
5410c97ad5cdSakolb 	boolean_t has_cpu_cap = B_FALSE;
54117c478bd9Sstevel@tonic-gate 
54127c478bd9Sstevel@tonic-gate 	optind = 0;
54137ec75eb8Sgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?")) != EOF) {
54147c478bd9Sstevel@tonic-gate 		switch (arg) {
54157c478bd9Sstevel@tonic-gate 		case '?':
54167c478bd9Sstevel@tonic-gate 			longer_usage(CMD_VERIFY);
5417bbec428eSgjelinek 			arg_err = B_TRUE;
54187ec75eb8Sgjelinek 			break;
54197c478bd9Sstevel@tonic-gate 		default:
54207c478bd9Sstevel@tonic-gate 			short_usage(CMD_VERIFY);
5421bbec428eSgjelinek 			arg_err = B_TRUE;
54227ec75eb8Sgjelinek 			break;
54237ec75eb8Sgjelinek 		}
54247ec75eb8Sgjelinek 	}
54257ec75eb8Sgjelinek 	if (arg_err)
54267c478bd9Sstevel@tonic-gate 		return;
54277ec75eb8Sgjelinek 
54287c478bd9Sstevel@tonic-gate 	if (optind > cmd->cmd_argc) {
54297c478bd9Sstevel@tonic-gate 		short_usage(CMD_VERIFY);
54307c478bd9Sstevel@tonic-gate 		return;
54317c478bd9Sstevel@tonic-gate 	}
54327c478bd9Sstevel@tonic-gate 
54337c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_VERIFY))
54347c478bd9Sstevel@tonic-gate 		return;
54357c478bd9Sstevel@tonic-gate 
54367c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
54377c478bd9Sstevel@tonic-gate 
54387c478bd9Sstevel@tonic-gate 	if (cmd->cmd_argc > 0 && (strcmp(cmd->cmd_argv[0], "save") == 0))
5439bbec428eSgjelinek 		save = B_TRUE;
5440bbec428eSgjelinek 	if (initialize(B_TRUE) != Z_OK)
54417c478bd9Sstevel@tonic-gate 		return;
54427c478bd9Sstevel@tonic-gate 
54430209230bSgjelinek 	if (zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath)) != Z_OK &&
54440209230bSgjelinek 	    !global_zone) {
5445087719fdSdp 		zerr(gettext("%s not specified"), pt_to_str(PT_ZONEPATH));
54467c478bd9Sstevel@tonic-gate 		ret_val = Z_REQD_RESOURCE_MISSING;
5447bbec428eSgjelinek 		saw_error = B_TRUE;
54487c478bd9Sstevel@tonic-gate 	}
54490209230bSgjelinek 	if (strlen(zonepath) == 0 && !global_zone) {
5450087719fdSdp 		zerr(gettext("%s cannot be empty."), pt_to_str(PT_ZONEPATH));
54517c478bd9Sstevel@tonic-gate 		ret_val = Z_REQD_RESOURCE_MISSING;
5452bbec428eSgjelinek 		saw_error = B_TRUE;
54537c478bd9Sstevel@tonic-gate 	}
54547c478bd9Sstevel@tonic-gate 
54559acbbeafSnn35248 	if ((err = zonecfg_get_brand(handle, brand, sizeof (brand))) != Z_OK) {
5456bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
54579acbbeafSnn35248 		return;
54589acbbeafSnn35248 	}
54599acbbeafSnn35248 	if (strcmp(brand, NATIVE_BRAND_NAME) != 0) {
54609acbbeafSnn35248 		if ((err = brand_verify(handle)) != Z_OK) {
5461bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
54629acbbeafSnn35248 			return;
54639acbbeafSnn35248 		}
54649acbbeafSnn35248 	}
54659acbbeafSnn35248 
5466f4b3ec61Sdh155122 	if (zonecfg_get_iptype(handle, &iptype) != Z_OK) {
5467f4b3ec61Sdh155122 		zerr("%s %s", gettext("cannot get"), pt_to_str(PT_IPTYPE));
5468f4b3ec61Sdh155122 		ret_val = Z_REQD_RESOURCE_MISSING;
5469bbec428eSgjelinek 		saw_error = B_TRUE;
5470f4b3ec61Sdh155122 	}
54717c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setipdent(handle)) != Z_OK) {
5472bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
54737c478bd9Sstevel@tonic-gate 		return;
54747c478bd9Sstevel@tonic-gate 	}
54757c478bd9Sstevel@tonic-gate 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
5476087719fdSdp 		check_reqd_prop(fstab.zone_fs_dir, RT_IPD, PT_DIR, &ret_val);
54777c478bd9Sstevel@tonic-gate 	}
54787c478bd9Sstevel@tonic-gate 	(void) zonecfg_endipdent(handle);
54797c478bd9Sstevel@tonic-gate 
54807c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setfsent(handle)) != Z_OK) {
5481bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
54827c478bd9Sstevel@tonic-gate 		return;
54837c478bd9Sstevel@tonic-gate 	}
54847c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
5485087719fdSdp 		check_reqd_prop(fstab.zone_fs_dir, RT_FS, PT_DIR, &ret_val);
5486087719fdSdp 		check_reqd_prop(fstab.zone_fs_special, RT_FS, PT_SPECIAL,
5487087719fdSdp 		    &ret_val);
5488087719fdSdp 		check_reqd_prop(fstab.zone_fs_type, RT_FS, PT_TYPE, &ret_val);
5489087719fdSdp 
54907c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
54917c478bd9Sstevel@tonic-gate 	}
54927c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
54937c478bd9Sstevel@tonic-gate 
54947c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
5495bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
54967c478bd9Sstevel@tonic-gate 		return;
54977c478bd9Sstevel@tonic-gate 	}
54987c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
5499f4b3ec61Sdh155122 		/*
5500f4b3ec61Sdh155122 		 * physical is required in all cases.
5501de860bd9Sgfaden 		 * A shared IP requires an address,
5502de860bd9Sgfaden 		 * and may include a default router, while
5503de860bd9Sgfaden 		 * an exclusive IP must have neither an address
5504de860bd9Sgfaden 		 * nor a default router.
5505f4b3ec61Sdh155122 		 */
5506087719fdSdp 		check_reqd_prop(nwiftab.zone_nwif_physical, RT_NET,
5507087719fdSdp 		    PT_PHYSICAL, &ret_val);
5508f4b3ec61Sdh155122 
5509f4b3ec61Sdh155122 		switch (iptype) {
5510f4b3ec61Sdh155122 		case ZS_SHARED:
5511f4b3ec61Sdh155122 			check_reqd_prop(nwiftab.zone_nwif_address, RT_NET,
5512f4b3ec61Sdh155122 			    PT_ADDRESS, &ret_val);
5513f4b3ec61Sdh155122 			break;
5514f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
5515f4b3ec61Sdh155122 			if (strlen(nwiftab.zone_nwif_address) > 0) {
5516f4b3ec61Sdh155122 				zerr(gettext("%s: %s cannot be specified "
5517f4b3ec61Sdh155122 				    "for an exclusive IP type"),
5518f4b3ec61Sdh155122 				    rt_to_str(RT_NET), pt_to_str(PT_ADDRESS));
5519bbec428eSgjelinek 				saw_error = B_TRUE;
5520f4b3ec61Sdh155122 				if (ret_val == Z_OK)
5521f4b3ec61Sdh155122 					ret_val = Z_INVAL;
5522f4b3ec61Sdh155122 			}
5523de860bd9Sgfaden 			if (strlen(nwiftab.zone_nwif_defrouter) > 0) {
5524de860bd9Sgfaden 				zerr(gettext("%s: %s cannot be specified "
5525de860bd9Sgfaden 				    "for an exclusive IP type"),
5526de860bd9Sgfaden 				    rt_to_str(RT_NET), pt_to_str(PT_DEFROUTER));
5527bbec428eSgjelinek 				saw_error = B_TRUE;
5528de860bd9Sgfaden 				if (ret_val == Z_OK)
5529de860bd9Sgfaden 					ret_val = Z_INVAL;
5530de860bd9Sgfaden 			}
5531f4b3ec61Sdh155122 			break;
5532f4b3ec61Sdh155122 		}
55337c478bd9Sstevel@tonic-gate 	}
55347c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
5537bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
55387c478bd9Sstevel@tonic-gate 		return;
55397c478bd9Sstevel@tonic-gate 	}
55407c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
5541087719fdSdp 		check_reqd_prop(rctltab.zone_rctl_name, RT_RCTL, PT_NAME,
5542087719fdSdp 		    &ret_val);
5543087719fdSdp 
55440209230bSgjelinek 		if (strcmp(rctltab.zone_rctl_name, "zone.cpu-shares") == 0)
55450209230bSgjelinek 			has_cpu_shares = B_TRUE;
55460209230bSgjelinek 
5547c97ad5cdSakolb 		if (strcmp(rctltab.zone_rctl_name, "zone.cpu-cap") == 0)
5548c97ad5cdSakolb 			has_cpu_cap = B_TRUE;
5549c97ad5cdSakolb 
55507c478bd9Sstevel@tonic-gate 		if (rctltab.zone_rctl_valptr == NULL) {
55517c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: no %s specified"),
55527c478bd9Sstevel@tonic-gate 			    rt_to_str(RT_RCTL), pt_to_str(PT_VALUE));
5553bbec428eSgjelinek 			saw_error = B_TRUE;
55547c478bd9Sstevel@tonic-gate 			if (ret_val == Z_OK)
55557c478bd9Sstevel@tonic-gate 				ret_val = Z_REQD_PROPERTY_MISSING;
55567c478bd9Sstevel@tonic-gate 		} else {
55577c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
55587c478bd9Sstevel@tonic-gate 		}
55597c478bd9Sstevel@tonic-gate 	}
55607c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
55617c478bd9Sstevel@tonic-gate 
5562c97ad5cdSakolb 	if ((pset_res = zonecfg_lookup_pset(handle, &psettab)) == Z_OK &&
5563c97ad5cdSakolb 	    has_cpu_shares) {
55640209230bSgjelinek 		zerr(gettext("%s zone.cpu-shares and %s are incompatible."),
55650209230bSgjelinek 		    rt_to_str(RT_RCTL), rt_to_str(RT_DCPU));
5566bbec428eSgjelinek 		saw_error = B_TRUE;
55670209230bSgjelinek 		if (ret_val == Z_OK)
55680209230bSgjelinek 			ret_val = Z_INCOMPATIBLE;
55690209230bSgjelinek 	}
55700209230bSgjelinek 
55710209230bSgjelinek 	if (has_cpu_shares && zonecfg_get_sched_class(handle, sched,
55720209230bSgjelinek 	    sizeof (sched)) == Z_OK && strlen(sched) > 0 &&
55730209230bSgjelinek 	    strcmp(sched, "FSS") != 0) {
55740209230bSgjelinek 		zerr(gettext("WARNING: %s zone.cpu-shares and %s=%s are "
55750209230bSgjelinek 		    "incompatible"),
55760209230bSgjelinek 		    rt_to_str(RT_RCTL), rt_to_str(RT_SCHED), sched);
5577bbec428eSgjelinek 		saw_error = B_TRUE;
55780209230bSgjelinek 		if (ret_val == Z_OK)
55790209230bSgjelinek 			ret_val = Z_INCOMPATIBLE;
55800209230bSgjelinek 	}
55810209230bSgjelinek 
5582c97ad5cdSakolb 	if (pset_res == Z_OK && has_cpu_cap) {
5583c97ad5cdSakolb 		zerr(gettext("%s zone.cpu-cap and the %s are incompatible."),
5584c97ad5cdSakolb 		    rt_to_str(RT_RCTL), rt_to_str(RT_DCPU));
5585bbec428eSgjelinek 		saw_error = B_TRUE;
5586c97ad5cdSakolb 		if (ret_val == Z_OK)
5587c97ad5cdSakolb 			ret_val = Z_INCOMPATIBLE;
5588c97ad5cdSakolb 	}
5589c97ad5cdSakolb 
55907c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setattrent(handle)) != Z_OK) {
5591bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
55927c478bd9Sstevel@tonic-gate 		return;
55937c478bd9Sstevel@tonic-gate 	}
55947c478bd9Sstevel@tonic-gate 	while (zonecfg_getattrent(handle, &attrtab) == Z_OK) {
5595087719fdSdp 		check_reqd_prop(attrtab.zone_attr_name, RT_ATTR, PT_NAME,
5596087719fdSdp 		    &ret_val);
5597087719fdSdp 		check_reqd_prop(attrtab.zone_attr_type, RT_ATTR, PT_TYPE,
5598087719fdSdp 		    &ret_val);
5599087719fdSdp 		check_reqd_prop(attrtab.zone_attr_value, RT_ATTR, PT_VALUE,
5600087719fdSdp 		    &ret_val);
56017c478bd9Sstevel@tonic-gate 	}
56027c478bd9Sstevel@tonic-gate 	(void) zonecfg_endattrent(handle);
56037c478bd9Sstevel@tonic-gate 
5604fa9e4066Sahrens 	if ((err = zonecfg_setdsent(handle)) != Z_OK) {
5605bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
5606fa9e4066Sahrens 		return;
5607fa9e4066Sahrens 	}
5608fa9e4066Sahrens 	while (zonecfg_getdsent(handle, &dstab) == Z_OK) {
5609fa9e4066Sahrens 		if (strlen(dstab.zone_dataset_name) == 0) {
5610fa9e4066Sahrens 			zerr("%s: %s %s", rt_to_str(RT_DATASET),
5611fa9e4066Sahrens 			    pt_to_str(PT_NAME), gettext("not specified"));
5612bbec428eSgjelinek 			saw_error = B_TRUE;
5613fa9e4066Sahrens 			if (ret_val == Z_OK)
5614fa9e4066Sahrens 				ret_val = Z_REQD_PROPERTY_MISSING;
5615fa9e4066Sahrens 		} else if (!zfs_name_valid(dstab.zone_dataset_name,
5616fa9e4066Sahrens 		    ZFS_TYPE_FILESYSTEM)) {
5617fa9e4066Sahrens 			zerr("%s: %s %s", rt_to_str(RT_DATASET),
5618fa9e4066Sahrens 			    pt_to_str(PT_NAME), gettext("invalid"));
5619bbec428eSgjelinek 			saw_error = B_TRUE;
5620fa9e4066Sahrens 			if (ret_val == Z_OK)
5621fa9e4066Sahrens 				ret_val = Z_BAD_PROPERTY;
5622fa9e4066Sahrens 		}
5623fa9e4066Sahrens 
5624fa9e4066Sahrens 	}
5625fa9e4066Sahrens 	(void) zonecfg_enddsent(handle);
5626fa9e4066Sahrens 
56277c478bd9Sstevel@tonic-gate 	if (!global_scope) {
56287c478bd9Sstevel@tonic-gate 		zerr(gettext("resource specification incomplete"));
5629bbec428eSgjelinek 		saw_error = B_TRUE;
56307c478bd9Sstevel@tonic-gate 		if (ret_val == Z_OK)
56317c478bd9Sstevel@tonic-gate 			ret_val = Z_INSUFFICIENT_SPEC;
56327c478bd9Sstevel@tonic-gate 	}
56337c478bd9Sstevel@tonic-gate 
56347c478bd9Sstevel@tonic-gate 	if (save) {
5635087719fdSdp 		if (ret_val == Z_OK) {
5636087719fdSdp 			if ((ret_val = zonecfg_save(handle)) == Z_OK) {
5637bbec428eSgjelinek 				need_to_commit = B_FALSE;
5638087719fdSdp 				(void) strlcpy(revert_zone, zone,
5639087719fdSdp 				    sizeof (revert_zone));
5640087719fdSdp 			}
5641087719fdSdp 		} else {
5642087719fdSdp 			zerr(gettext("Zone %s failed to verify"), zone);
5643087719fdSdp 		}
56447c478bd9Sstevel@tonic-gate 	}
56457c478bd9Sstevel@tonic-gate 	if (ret_val != Z_OK)
5646bbec428eSgjelinek 		zone_perror(zone, ret_val, B_TRUE);
56477c478bd9Sstevel@tonic-gate }
56487c478bd9Sstevel@tonic-gate 
56497c478bd9Sstevel@tonic-gate void
56507c478bd9Sstevel@tonic-gate cancel_func(cmd_t *cmd)
56517c478bd9Sstevel@tonic-gate {
56527c478bd9Sstevel@tonic-gate 	int arg;
5653bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
56547c478bd9Sstevel@tonic-gate 
56557c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
56567c478bd9Sstevel@tonic-gate 
56577c478bd9Sstevel@tonic-gate 	optind = 0;
56587ec75eb8Sgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?")) != EOF) {
56597c478bd9Sstevel@tonic-gate 		switch (arg) {
56607c478bd9Sstevel@tonic-gate 		case '?':
56617c478bd9Sstevel@tonic-gate 			longer_usage(CMD_CANCEL);
5662bbec428eSgjelinek 			arg_err = B_TRUE;
56637ec75eb8Sgjelinek 			break;
56647c478bd9Sstevel@tonic-gate 		default:
56657c478bd9Sstevel@tonic-gate 			short_usage(CMD_CANCEL);
5666bbec428eSgjelinek 			arg_err = B_TRUE;
56677ec75eb8Sgjelinek 			break;
56687ec75eb8Sgjelinek 		}
56697ec75eb8Sgjelinek 	}
56707ec75eb8Sgjelinek 	if (arg_err)
56717c478bd9Sstevel@tonic-gate 		return;
56727ec75eb8Sgjelinek 
56737c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
56747c478bd9Sstevel@tonic-gate 		short_usage(CMD_CANCEL);
56757c478bd9Sstevel@tonic-gate 		return;
56767c478bd9Sstevel@tonic-gate 	}
56777c478bd9Sstevel@tonic-gate 
56787c478bd9Sstevel@tonic-gate 	if (global_scope)
56797c478bd9Sstevel@tonic-gate 		scope_usage(CMD_CANCEL);
5680bbec428eSgjelinek 	global_scope = B_TRUE;
56817c478bd9Sstevel@tonic-gate 	zonecfg_free_fs_option_list(in_progress_fstab.zone_fs_options);
56827c478bd9Sstevel@tonic-gate 	bzero(&in_progress_fstab, sizeof (in_progress_fstab));
56837c478bd9Sstevel@tonic-gate 	bzero(&in_progress_nwiftab, sizeof (in_progress_nwiftab));
5684fa9e4066Sahrens 	bzero(&in_progress_ipdtab, sizeof (in_progress_ipdtab));
56857c478bd9Sstevel@tonic-gate 	bzero(&in_progress_devtab, sizeof (in_progress_devtab));
56867c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(in_progress_rctltab.zone_rctl_valptr);
56877c478bd9Sstevel@tonic-gate 	bzero(&in_progress_rctltab, sizeof (in_progress_rctltab));
56887c478bd9Sstevel@tonic-gate 	bzero(&in_progress_attrtab, sizeof (in_progress_attrtab));
5689fa9e4066Sahrens 	bzero(&in_progress_dstab, sizeof (in_progress_dstab));
56907c478bd9Sstevel@tonic-gate }
56917c478bd9Sstevel@tonic-gate 
56927c478bd9Sstevel@tonic-gate static int
56937c478bd9Sstevel@tonic-gate validate_attr_name(char *name)
56947c478bd9Sstevel@tonic-gate {
56957c478bd9Sstevel@tonic-gate 	int i;
56967c478bd9Sstevel@tonic-gate 
56977c478bd9Sstevel@tonic-gate 	if (!isalnum(name[0])) {
56987c478bd9Sstevel@tonic-gate 		zerr(gettext("Invalid %s %s %s: must start with an alpha-"
56997c478bd9Sstevel@tonic-gate 		    "numeric character."), rt_to_str(RT_ATTR),
57007c478bd9Sstevel@tonic-gate 		    pt_to_str(PT_NAME), name);
57017c478bd9Sstevel@tonic-gate 		return (Z_INVAL);
57027c478bd9Sstevel@tonic-gate 	}
57037c478bd9Sstevel@tonic-gate 	for (i = 1; name[i]; i++)
57047c478bd9Sstevel@tonic-gate 		if (!isalnum(name[i]) && name[i] != '-' && name[i] != '.') {
57057c478bd9Sstevel@tonic-gate 			zerr(gettext("Invalid %s %s %s: can only contain "
57067c478bd9Sstevel@tonic-gate 			    "alpha-numeric characters, plus '-' and '.'."),
57077c478bd9Sstevel@tonic-gate 			    rt_to_str(RT_ATTR), pt_to_str(PT_NAME), name);
57087c478bd9Sstevel@tonic-gate 			return (Z_INVAL);
57097c478bd9Sstevel@tonic-gate 		}
57107c478bd9Sstevel@tonic-gate 	return (Z_OK);
57117c478bd9Sstevel@tonic-gate }
57127c478bd9Sstevel@tonic-gate 
57137c478bd9Sstevel@tonic-gate static int
57147c478bd9Sstevel@tonic-gate validate_attr_type_val(struct zone_attrtab *attrtab)
57157c478bd9Sstevel@tonic-gate {
57167c478bd9Sstevel@tonic-gate 	boolean_t boolval;
57177c478bd9Sstevel@tonic-gate 	int64_t intval;
57187c478bd9Sstevel@tonic-gate 	char strval[MAXNAMELEN];
57197c478bd9Sstevel@tonic-gate 	uint64_t uintval;
57207c478bd9Sstevel@tonic-gate 
57217c478bd9Sstevel@tonic-gate 	if (strcmp(attrtab->zone_attr_type, "boolean") == 0) {
57227c478bd9Sstevel@tonic-gate 		if (zonecfg_get_attr_boolean(attrtab, &boolval) == Z_OK)
57237c478bd9Sstevel@tonic-gate 			return (Z_OK);
57247c478bd9Sstevel@tonic-gate 		zerr(gettext("invalid %s value for %s=%s"),
57257c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR), pt_to_str(PT_TYPE), "boolean");
57267c478bd9Sstevel@tonic-gate 		return (Z_ERR);
57277c478bd9Sstevel@tonic-gate 	}
57287c478bd9Sstevel@tonic-gate 
57297c478bd9Sstevel@tonic-gate 	if (strcmp(attrtab->zone_attr_type, "int") == 0) {
57307c478bd9Sstevel@tonic-gate 		if (zonecfg_get_attr_int(attrtab, &intval) == Z_OK)
57317c478bd9Sstevel@tonic-gate 			return (Z_OK);
57327c478bd9Sstevel@tonic-gate 		zerr(gettext("invalid %s value for %s=%s"),
57337c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR), pt_to_str(PT_TYPE), "int");
57347c478bd9Sstevel@tonic-gate 		return (Z_ERR);
57357c478bd9Sstevel@tonic-gate 	}
57367c478bd9Sstevel@tonic-gate 
57377c478bd9Sstevel@tonic-gate 	if (strcmp(attrtab->zone_attr_type, "string") == 0) {
57387c478bd9Sstevel@tonic-gate 		if (zonecfg_get_attr_string(attrtab, strval,
57397c478bd9Sstevel@tonic-gate 		    sizeof (strval)) == Z_OK)
57407c478bd9Sstevel@tonic-gate 			return (Z_OK);
57417c478bd9Sstevel@tonic-gate 		zerr(gettext("invalid %s value for %s=%s"),
57427c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR), pt_to_str(PT_TYPE), "string");
57437c478bd9Sstevel@tonic-gate 		return (Z_ERR);
57447c478bd9Sstevel@tonic-gate 	}
57457c478bd9Sstevel@tonic-gate 
57467c478bd9Sstevel@tonic-gate 	if (strcmp(attrtab->zone_attr_type, "uint") == 0) {
57477c478bd9Sstevel@tonic-gate 		if (zonecfg_get_attr_uint(attrtab, &uintval) == Z_OK)
57487c478bd9Sstevel@tonic-gate 			return (Z_OK);
57497c478bd9Sstevel@tonic-gate 		zerr(gettext("invalid %s value for %s=%s"),
57507c478bd9Sstevel@tonic-gate 		    rt_to_str(RT_ATTR), pt_to_str(PT_TYPE), "uint");
57517c478bd9Sstevel@tonic-gate 		return (Z_ERR);
57527c478bd9Sstevel@tonic-gate 	}
57537c478bd9Sstevel@tonic-gate 
57547c478bd9Sstevel@tonic-gate 	zerr(gettext("invalid %s %s '%s'"), rt_to_str(RT_ATTR),
57557c478bd9Sstevel@tonic-gate 	    pt_to_str(PT_TYPE), attrtab->zone_attr_type);
57567c478bd9Sstevel@tonic-gate 	return (Z_ERR);
57577c478bd9Sstevel@tonic-gate }
57587c478bd9Sstevel@tonic-gate 
5759087719fdSdp /*
5760087719fdSdp  * Helper function for end_func-- checks the existence of a given property
5761087719fdSdp  * and emits a message if not specified.
5762087719fdSdp  */
5763087719fdSdp static int
5764bbec428eSgjelinek end_check_reqd(char *attr, int pt, boolean_t *validation_failed)
5765087719fdSdp {
5766087719fdSdp 	if (strlen(attr) == 0) {
5767bbec428eSgjelinek 		*validation_failed = B_TRUE;
5768087719fdSdp 		zerr(gettext("%s not specified"), pt_to_str(pt));
5769087719fdSdp 		return (Z_ERR);
5770087719fdSdp 	}
5771087719fdSdp 	return (Z_OK);
5772087719fdSdp }
5773087719fdSdp 
57747c478bd9Sstevel@tonic-gate void
57757c478bd9Sstevel@tonic-gate end_func(cmd_t *cmd)
57767c478bd9Sstevel@tonic-gate {
5777bbec428eSgjelinek 	boolean_t validation_failed = B_FALSE;
5778bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
57797c478bd9Sstevel@tonic-gate 	struct zone_fstab tmp_fstab;
57807c478bd9Sstevel@tonic-gate 	struct zone_nwiftab tmp_nwiftab;
57817c478bd9Sstevel@tonic-gate 	struct zone_devtab tmp_devtab;
57827c478bd9Sstevel@tonic-gate 	struct zone_rctltab tmp_rctltab;
57837c478bd9Sstevel@tonic-gate 	struct zone_attrtab tmp_attrtab;
5784fa9e4066Sahrens 	struct zone_dstab tmp_dstab;
57850209230bSgjelinek 	int err, arg, res1, res2, res3;
57860209230bSgjelinek 	uint64_t swap_limit;
57870209230bSgjelinek 	uint64_t locked_limit;
5788c97ad5cdSakolb 	uint64_t proc_cap;
57897c478bd9Sstevel@tonic-gate 
57907c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
57917c478bd9Sstevel@tonic-gate 
57927c478bd9Sstevel@tonic-gate 	optind = 0;
57937ec75eb8Sgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?")) != EOF) {
57947c478bd9Sstevel@tonic-gate 		switch (arg) {
57957c478bd9Sstevel@tonic-gate 		case '?':
57967c478bd9Sstevel@tonic-gate 			longer_usage(CMD_END);
5797bbec428eSgjelinek 			arg_err = B_TRUE;
57987ec75eb8Sgjelinek 			break;
57997c478bd9Sstevel@tonic-gate 		default:
58007c478bd9Sstevel@tonic-gate 			short_usage(CMD_END);
5801bbec428eSgjelinek 			arg_err = B_TRUE;
58027ec75eb8Sgjelinek 			break;
58037ec75eb8Sgjelinek 		}
58047ec75eb8Sgjelinek 	}
58057ec75eb8Sgjelinek 	if (arg_err)
58067c478bd9Sstevel@tonic-gate 		return;
58077ec75eb8Sgjelinek 
58087c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
58097c478bd9Sstevel@tonic-gate 		short_usage(CMD_END);
58107c478bd9Sstevel@tonic-gate 		return;
58117c478bd9Sstevel@tonic-gate 	}
58127c478bd9Sstevel@tonic-gate 
58137c478bd9Sstevel@tonic-gate 	if (global_scope) {
58147c478bd9Sstevel@tonic-gate 		scope_usage(CMD_END);
58157c478bd9Sstevel@tonic-gate 		return;
58167c478bd9Sstevel@tonic-gate 	}
58177c478bd9Sstevel@tonic-gate 
58187c478bd9Sstevel@tonic-gate 	assert(end_op == CMD_ADD || end_op == CMD_SELECT);
58197c478bd9Sstevel@tonic-gate 
58207c478bd9Sstevel@tonic-gate 	switch (resource_scope) {
58217c478bd9Sstevel@tonic-gate 	case RT_FS:
58227c478bd9Sstevel@tonic-gate 		/* First make sure everything was filled in. */
5823087719fdSdp 		if (end_check_reqd(in_progress_fstab.zone_fs_dir,
5824087719fdSdp 		    PT_DIR, &validation_failed) == Z_OK) {
5825087719fdSdp 			if (in_progress_fstab.zone_fs_dir[0] != '/') {
5826087719fdSdp 				zerr(gettext("%s %s is not an absolute path."),
5827087719fdSdp 				    pt_to_str(PT_DIR),
5828087719fdSdp 				    in_progress_fstab.zone_fs_dir);
5829bbec428eSgjelinek 				validation_failed = B_TRUE;
58307c478bd9Sstevel@tonic-gate 			}
58317c478bd9Sstevel@tonic-gate 		}
5832087719fdSdp 
5833087719fdSdp 		(void) end_check_reqd(in_progress_fstab.zone_fs_special,
5834087719fdSdp 		    PT_SPECIAL, &validation_failed);
5835087719fdSdp 
58367c478bd9Sstevel@tonic-gate 		if (in_progress_fstab.zone_fs_raw[0] != '\0' &&
58377c478bd9Sstevel@tonic-gate 		    in_progress_fstab.zone_fs_raw[0] != '/') {
5838087719fdSdp 			zerr(gettext("%s %s is not an absolute path."),
5839087719fdSdp 			    pt_to_str(PT_RAW),
5840087719fdSdp 			    in_progress_fstab.zone_fs_raw);
5841bbec428eSgjelinek 			validation_failed = B_TRUE;
58427c478bd9Sstevel@tonic-gate 		}
5843087719fdSdp 
5844087719fdSdp 		(void) end_check_reqd(in_progress_fstab.zone_fs_type, PT_TYPE,
5845087719fdSdp 		    &validation_failed);
5846087719fdSdp 
5847087719fdSdp 		if (validation_failed) {
5848bbec428eSgjelinek 			saw_error = B_TRUE;
58497c478bd9Sstevel@tonic-gate 			return;
5850087719fdSdp 		}
5851087719fdSdp 
58527c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
58537c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
58547c478bd9Sstevel@tonic-gate 			bzero(&tmp_fstab, sizeof (tmp_fstab));
58557c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_fstab.zone_fs_dir,
58567c478bd9Sstevel@tonic-gate 			    in_progress_fstab.zone_fs_dir,
58577c478bd9Sstevel@tonic-gate 			    sizeof (tmp_fstab.zone_fs_dir));
58587c478bd9Sstevel@tonic-gate 			err = zonecfg_lookup_filesystem(handle, &tmp_fstab);
58597c478bd9Sstevel@tonic-gate 			zonecfg_free_fs_option_list(tmp_fstab.zone_fs_options);
58607c478bd9Sstevel@tonic-gate 			if (err == Z_OK) {
58617c478bd9Sstevel@tonic-gate 				zerr(gettext("A %s resource "
58627c478bd9Sstevel@tonic-gate 				    "with the %s '%s' already exists."),
58637c478bd9Sstevel@tonic-gate 				    rt_to_str(RT_FS), pt_to_str(PT_DIR),
58647c478bd9Sstevel@tonic-gate 				    in_progress_fstab.zone_fs_dir);
5865bbec428eSgjelinek 				saw_error = B_TRUE;
58667c478bd9Sstevel@tonic-gate 				return;
58677c478bd9Sstevel@tonic-gate 			}
58687c478bd9Sstevel@tonic-gate 			err = zonecfg_add_filesystem(handle,
58697c478bd9Sstevel@tonic-gate 			    &in_progress_fstab);
58707c478bd9Sstevel@tonic-gate 		} else {
58717c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_filesystem(handle, &old_fstab,
58727c478bd9Sstevel@tonic-gate 			    &in_progress_fstab);
58737c478bd9Sstevel@tonic-gate 		}
58747c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(in_progress_fstab.zone_fs_options);
58757c478bd9Sstevel@tonic-gate 		in_progress_fstab.zone_fs_options = NULL;
58767c478bd9Sstevel@tonic-gate 		break;
5877087719fdSdp 
58787c478bd9Sstevel@tonic-gate 	case RT_IPD:
58797c478bd9Sstevel@tonic-gate 		/* First make sure everything was filled in. */
5880087719fdSdp 		if (end_check_reqd(in_progress_ipdtab.zone_fs_dir, PT_DIR,
5881087719fdSdp 		    &validation_failed) == Z_OK) {
5882087719fdSdp 			if (in_progress_ipdtab.zone_fs_dir[0] != '/') {
5883087719fdSdp 				zerr(gettext("%s %s is not an absolute path."),
5884087719fdSdp 				    pt_to_str(PT_DIR),
5885087719fdSdp 				    in_progress_ipdtab.zone_fs_dir);
5886bbec428eSgjelinek 				validation_failed = B_TRUE;
58877c478bd9Sstevel@tonic-gate 			}
5888087719fdSdp 		}
5889087719fdSdp 		if (validation_failed) {
5890bbec428eSgjelinek 			saw_error = B_TRUE;
58917c478bd9Sstevel@tonic-gate 			return;
5892087719fdSdp 		}
5893087719fdSdp 
58947c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
58957c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
58967c478bd9Sstevel@tonic-gate 			bzero(&tmp_fstab, sizeof (tmp_fstab));
58977c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_fstab.zone_fs_dir,
58987c478bd9Sstevel@tonic-gate 			    in_progress_ipdtab.zone_fs_dir,
58997c478bd9Sstevel@tonic-gate 			    sizeof (tmp_fstab.zone_fs_dir));
59007c478bd9Sstevel@tonic-gate 			err = zonecfg_lookup_ipd(handle, &tmp_fstab);
59017c478bd9Sstevel@tonic-gate 			if (err == Z_OK) {
59027c478bd9Sstevel@tonic-gate 				zerr(gettext("An %s resource "
59037c478bd9Sstevel@tonic-gate 				    "with the %s '%s' already exists."),
59047c478bd9Sstevel@tonic-gate 				    rt_to_str(RT_IPD), pt_to_str(PT_DIR),
59057c478bd9Sstevel@tonic-gate 				    in_progress_ipdtab.zone_fs_dir);
5906bbec428eSgjelinek 				saw_error = B_TRUE;
59077c478bd9Sstevel@tonic-gate 				return;
59087c478bd9Sstevel@tonic-gate 			}
59097c478bd9Sstevel@tonic-gate 			err = zonecfg_add_ipd(handle, &in_progress_ipdtab);
59107c478bd9Sstevel@tonic-gate 		} else {
59117c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_ipd(handle, &old_ipdtab,
59127c478bd9Sstevel@tonic-gate 			    &in_progress_ipdtab);
59137c478bd9Sstevel@tonic-gate 		}
59147c478bd9Sstevel@tonic-gate 		break;
59157c478bd9Sstevel@tonic-gate 	case RT_NET:
5916f4b3ec61Sdh155122 		/*
5917f4b3ec61Sdh155122 		 * First make sure everything was filled in.
5918f4b3ec61Sdh155122 		 * Since we don't know whether IP will be shared
5919f4b3ec61Sdh155122 		 * or exclusive here, some checks are deferred until
5920f4b3ec61Sdh155122 		 * the verify command.
5921f4b3ec61Sdh155122 		 */
5922087719fdSdp 		(void) end_check_reqd(in_progress_nwiftab.zone_nwif_physical,
5923087719fdSdp 		    PT_PHYSICAL, &validation_failed);
5924087719fdSdp 
5925087719fdSdp 		if (validation_failed) {
5926bbec428eSgjelinek 			saw_error = B_TRUE;
59277c478bd9Sstevel@tonic-gate 			return;
5928087719fdSdp 		}
59297c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
59307c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
59317c478bd9Sstevel@tonic-gate 			bzero(&tmp_nwiftab, sizeof (tmp_nwiftab));
5932f4b3ec61Sdh155122 			(void) strlcpy(tmp_nwiftab.zone_nwif_physical,
5933f4b3ec61Sdh155122 			    in_progress_nwiftab.zone_nwif_physical,
5934f4b3ec61Sdh155122 			    sizeof (tmp_nwiftab.zone_nwif_physical));
59357c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_nwiftab.zone_nwif_address,
59367c478bd9Sstevel@tonic-gate 			    in_progress_nwiftab.zone_nwif_address,
59377c478bd9Sstevel@tonic-gate 			    sizeof (tmp_nwiftab.zone_nwif_address));
59387c478bd9Sstevel@tonic-gate 			if (zonecfg_lookup_nwif(handle, &tmp_nwiftab) == Z_OK) {
5939f4b3ec61Sdh155122 				zerr(gettext("A %s resource with the %s '%s', "
5940f4b3ec61Sdh155122 				    "and %s '%s' already exists."),
5941f4b3ec61Sdh155122 				    rt_to_str(RT_NET),
5942f4b3ec61Sdh155122 				    pt_to_str(PT_PHYSICAL),
5943f4b3ec61Sdh155122 				    in_progress_nwiftab.zone_nwif_physical,
5944f4b3ec61Sdh155122 				    pt_to_str(PT_ADDRESS),
59457c478bd9Sstevel@tonic-gate 				    in_progress_nwiftab.zone_nwif_address);
5946bbec428eSgjelinek 				saw_error = B_TRUE;
59477c478bd9Sstevel@tonic-gate 				return;
59487c478bd9Sstevel@tonic-gate 			}
59497c478bd9Sstevel@tonic-gate 			err = zonecfg_add_nwif(handle, &in_progress_nwiftab);
59507c478bd9Sstevel@tonic-gate 		} else {
59517c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_nwif(handle, &old_nwiftab,
59527c478bd9Sstevel@tonic-gate 			    &in_progress_nwiftab);
59537c478bd9Sstevel@tonic-gate 		}
59547c478bd9Sstevel@tonic-gate 		break;
5955087719fdSdp 
59567c478bd9Sstevel@tonic-gate 	case RT_DEVICE:
59577c478bd9Sstevel@tonic-gate 		/* First make sure everything was filled in. */
5958087719fdSdp 		(void) end_check_reqd(in_progress_devtab.zone_dev_match,
5959087719fdSdp 		    PT_MATCH, &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 			(void) strlcpy(tmp_devtab.zone_dev_match,
59697c478bd9Sstevel@tonic-gate 			    in_progress_devtab.zone_dev_match,
59707c478bd9Sstevel@tonic-gate 			    sizeof (tmp_devtab.zone_dev_match));
59717c478bd9Sstevel@tonic-gate 			if (zonecfg_lookup_dev(handle, &tmp_devtab) == Z_OK) {
59727c478bd9Sstevel@tonic-gate 				zerr(gettext("A %s resource with the %s '%s' "
59737c478bd9Sstevel@tonic-gate 				    "already exists."), rt_to_str(RT_DEVICE),
59747c478bd9Sstevel@tonic-gate 				    pt_to_str(PT_MATCH),
59757c478bd9Sstevel@tonic-gate 				    in_progress_devtab.zone_dev_match);
5976bbec428eSgjelinek 				saw_error = B_TRUE;
59777c478bd9Sstevel@tonic-gate 				return;
59787c478bd9Sstevel@tonic-gate 			}
59797c478bd9Sstevel@tonic-gate 			err = zonecfg_add_dev(handle, &in_progress_devtab);
59807c478bd9Sstevel@tonic-gate 		} else {
59817c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_dev(handle, &old_devtab,
59827c478bd9Sstevel@tonic-gate 			    &in_progress_devtab);
59837c478bd9Sstevel@tonic-gate 		}
59847c478bd9Sstevel@tonic-gate 		break;
5985087719fdSdp 
59867c478bd9Sstevel@tonic-gate 	case RT_RCTL:
59877c478bd9Sstevel@tonic-gate 		/* First make sure everything was filled in. */
5988087719fdSdp 		(void) end_check_reqd(in_progress_rctltab.zone_rctl_name,
5989087719fdSdp 		    PT_NAME, &validation_failed);
5990087719fdSdp 
59917c478bd9Sstevel@tonic-gate 		if (in_progress_rctltab.zone_rctl_valptr == NULL) {
59927c478bd9Sstevel@tonic-gate 			zerr(gettext("no %s specified"), pt_to_str(PT_VALUE));
5993bbec428eSgjelinek 			validation_failed = B_TRUE;
59947c478bd9Sstevel@tonic-gate 		}
5995087719fdSdp 
5996087719fdSdp 		if (validation_failed) {
5997bbec428eSgjelinek 			saw_error = B_TRUE;
59987c478bd9Sstevel@tonic-gate 			return;
5999087719fdSdp 		}
6000087719fdSdp 
60017c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
60027c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
60037c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_rctltab.zone_rctl_name,
60047c478bd9Sstevel@tonic-gate 			    in_progress_rctltab.zone_rctl_name,
60057c478bd9Sstevel@tonic-gate 			    sizeof (tmp_rctltab.zone_rctl_name));
60067c478bd9Sstevel@tonic-gate 			tmp_rctltab.zone_rctl_valptr = NULL;
60077c478bd9Sstevel@tonic-gate 			err = zonecfg_lookup_rctl(handle, &tmp_rctltab);
60087c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(
60097c478bd9Sstevel@tonic-gate 			    tmp_rctltab.zone_rctl_valptr);
60107c478bd9Sstevel@tonic-gate 			if (err == Z_OK) {
60117c478bd9Sstevel@tonic-gate 				zerr(gettext("A %s resource "
60127c478bd9Sstevel@tonic-gate 				    "with the %s '%s' already exists."),
60137c478bd9Sstevel@tonic-gate 				    rt_to_str(RT_RCTL), pt_to_str(PT_NAME),
60147c478bd9Sstevel@tonic-gate 				    in_progress_rctltab.zone_rctl_name);
6015bbec428eSgjelinek 				saw_error = B_TRUE;
60167c478bd9Sstevel@tonic-gate 				return;
60177c478bd9Sstevel@tonic-gate 			}
60187c478bd9Sstevel@tonic-gate 			err = zonecfg_add_rctl(handle, &in_progress_rctltab);
60197c478bd9Sstevel@tonic-gate 		} else {
60207c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_rctl(handle, &old_rctltab,
60217c478bd9Sstevel@tonic-gate 			    &in_progress_rctltab);
60227c478bd9Sstevel@tonic-gate 		}
60237c478bd9Sstevel@tonic-gate 		if (err == Z_OK) {
60247c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(
60257c478bd9Sstevel@tonic-gate 			    in_progress_rctltab.zone_rctl_valptr);
60267c478bd9Sstevel@tonic-gate 			in_progress_rctltab.zone_rctl_valptr = NULL;
60277c478bd9Sstevel@tonic-gate 		}
60287c478bd9Sstevel@tonic-gate 		break;
6029087719fdSdp 
60307c478bd9Sstevel@tonic-gate 	case RT_ATTR:
60317c478bd9Sstevel@tonic-gate 		/* First make sure everything was filled in. */
6032087719fdSdp 		(void) end_check_reqd(in_progress_attrtab.zone_attr_name,
6033087719fdSdp 		    PT_NAME, &validation_failed);
6034087719fdSdp 		(void) end_check_reqd(in_progress_attrtab.zone_attr_type,
6035087719fdSdp 		    PT_TYPE, &validation_failed);
6036087719fdSdp 		(void) end_check_reqd(in_progress_attrtab.zone_attr_value,
6037087719fdSdp 		    PT_VALUE, &validation_failed);
6038087719fdSdp 
60397c478bd9Sstevel@tonic-gate 		if (validate_attr_name(in_progress_attrtab.zone_attr_name) !=
6040087719fdSdp 		    Z_OK)
6041bbec428eSgjelinek 			validation_failed = B_TRUE;
6042087719fdSdp 
6043087719fdSdp 		if (validate_attr_type_val(&in_progress_attrtab) != Z_OK)
6044bbec428eSgjelinek 			validation_failed = B_TRUE;
6045087719fdSdp 
6046087719fdSdp 		if (validation_failed) {
6047bbec428eSgjelinek 			saw_error = B_TRUE;
60487c478bd9Sstevel@tonic-gate 			return;
6049087719fdSdp 		}
60507c478bd9Sstevel@tonic-gate 		if (end_op == CMD_ADD) {
60517c478bd9Sstevel@tonic-gate 			/* Make sure there isn't already one like this. */
60527c478bd9Sstevel@tonic-gate 			bzero(&tmp_attrtab, sizeof (tmp_attrtab));
60537c478bd9Sstevel@tonic-gate 			(void) strlcpy(tmp_attrtab.zone_attr_name,
60547c478bd9Sstevel@tonic-gate 			    in_progress_attrtab.zone_attr_name,
60557c478bd9Sstevel@tonic-gate 			    sizeof (tmp_attrtab.zone_attr_name));
60567c478bd9Sstevel@tonic-gate 			if (zonecfg_lookup_attr(handle, &tmp_attrtab) == Z_OK) {
60577c478bd9Sstevel@tonic-gate 				zerr(gettext("An %s resource "
60587c478bd9Sstevel@tonic-gate 				    "with the %s '%s' already exists."),
60597c478bd9Sstevel@tonic-gate 				    rt_to_str(RT_ATTR), pt_to_str(PT_NAME),
60607c478bd9Sstevel@tonic-gate 				    in_progress_attrtab.zone_attr_name);
6061bbec428eSgjelinek 				saw_error = B_TRUE;
60627c478bd9Sstevel@tonic-gate 				return;
60637c478bd9Sstevel@tonic-gate 			}
60647c478bd9Sstevel@tonic-gate 			err = zonecfg_add_attr(handle, &in_progress_attrtab);
60657c478bd9Sstevel@tonic-gate 		} else {
60667c478bd9Sstevel@tonic-gate 			err = zonecfg_modify_attr(handle, &old_attrtab,
60677c478bd9Sstevel@tonic-gate 			    &in_progress_attrtab);
60687c478bd9Sstevel@tonic-gate 		}
60697c478bd9Sstevel@tonic-gate 		break;
6070fa9e4066Sahrens 	case RT_DATASET:
6071fa9e4066Sahrens 		/* First make sure everything was filled in. */
6072fa9e4066Sahrens 		if (strlen(in_progress_dstab.zone_dataset_name) == 0) {
6073fa9e4066Sahrens 			zerr("%s %s", pt_to_str(PT_NAME),
6074fa9e4066Sahrens 			    gettext("not specified"));
6075bbec428eSgjelinek 			saw_error = B_TRUE;
6076bbec428eSgjelinek 			validation_failed = B_TRUE;
6077fa9e4066Sahrens 		}
6078fa9e4066Sahrens 		if (validation_failed)
6079fa9e4066Sahrens 			return;
6080fa9e4066Sahrens 		if (end_op == CMD_ADD) {
6081fa9e4066Sahrens 			/* Make sure there isn't already one like this. */
6082fa9e4066Sahrens 			bzero(&tmp_dstab, sizeof (tmp_dstab));
6083fa9e4066Sahrens 			(void) strlcpy(tmp_dstab.zone_dataset_name,
6084fa9e4066Sahrens 			    in_progress_dstab.zone_dataset_name,
6085fa9e4066Sahrens 			    sizeof (tmp_dstab.zone_dataset_name));
6086fa9e4066Sahrens 			err = zonecfg_lookup_ds(handle, &tmp_dstab);
6087fa9e4066Sahrens 			if (err == Z_OK) {
6088fa9e4066Sahrens 				zerr(gettext("A %s resource "
6089fa9e4066Sahrens 				    "with the %s '%s' already exists."),
6090fa9e4066Sahrens 				    rt_to_str(RT_DATASET), pt_to_str(PT_NAME),
6091fa9e4066Sahrens 				    in_progress_dstab.zone_dataset_name);
6092bbec428eSgjelinek 				saw_error = B_TRUE;
6093fa9e4066Sahrens 				return;
6094fa9e4066Sahrens 			}
6095fa9e4066Sahrens 			err = zonecfg_add_ds(handle, &in_progress_dstab);
6096fa9e4066Sahrens 		} else {
6097fa9e4066Sahrens 			err = zonecfg_modify_ds(handle, &old_dstab,
6098fa9e4066Sahrens 			    &in_progress_dstab);
6099fa9e4066Sahrens 		}
6100fa9e4066Sahrens 		break;
61010209230bSgjelinek 	case RT_DCPU:
61020209230bSgjelinek 		/* Make sure everything was filled in. */
61030209230bSgjelinek 		if (end_check_reqd(in_progress_psettab.zone_ncpu_min,
61040209230bSgjelinek 		    PT_NCPUS, &validation_failed) != Z_OK) {
6105bbec428eSgjelinek 			saw_error = B_TRUE;
61060209230bSgjelinek 			return;
61070209230bSgjelinek 		}
61080209230bSgjelinek 
61090209230bSgjelinek 		if (end_op == CMD_ADD) {
61100209230bSgjelinek 			err = zonecfg_add_pset(handle, &in_progress_psettab);
61110209230bSgjelinek 		} else {
61120209230bSgjelinek 			err = zonecfg_modify_pset(handle, &in_progress_psettab);
61130209230bSgjelinek 		}
61140209230bSgjelinek 		break;
6115c97ad5cdSakolb 	case RT_PCAP:
6116c97ad5cdSakolb 		/* Make sure everything was filled in. */
6117c97ad5cdSakolb 		if (zonecfg_get_aliased_rctl(handle, ALIAS_CPUCAP, &proc_cap)
6118c97ad5cdSakolb 		    != Z_OK) {
6119c97ad5cdSakolb 			zerr(gettext("%s not specified"), pt_to_str(PT_NCPUS));
6120bbec428eSgjelinek 			saw_error = B_TRUE;
6121bbec428eSgjelinek 			validation_failed = B_TRUE;
6122c97ad5cdSakolb 			return;
6123c97ad5cdSakolb 		}
6124c97ad5cdSakolb 		err = Z_OK;
6125c97ad5cdSakolb 		break;
61260209230bSgjelinek 	case RT_MCAP:
61270209230bSgjelinek 		/* Make sure everything was filled in. */
61280209230bSgjelinek 		res1 = strlen(in_progress_mcaptab.zone_physmem_cap) == 0 ?
61290209230bSgjelinek 		    Z_ERR : Z_OK;
61300209230bSgjelinek 		res2 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXSWAP,
61310209230bSgjelinek 		    &swap_limit);
61320209230bSgjelinek 		res3 = zonecfg_get_aliased_rctl(handle, ALIAS_MAXLOCKEDMEM,
61330209230bSgjelinek 		    &locked_limit);
61340209230bSgjelinek 
61350209230bSgjelinek 		if (res1 != Z_OK && res2 != Z_OK && res3 != Z_OK) {
61360209230bSgjelinek 			zerr(gettext("No property was specified.  One of %s, "
61370209230bSgjelinek 			    "%s or %s is required."), pt_to_str(PT_PHYSICAL),
61380209230bSgjelinek 			    pt_to_str(PT_SWAP), pt_to_str(PT_LOCKED));
6139bbec428eSgjelinek 			saw_error = B_TRUE;
61400209230bSgjelinek 			return;
61410209230bSgjelinek 		}
61420209230bSgjelinek 
61430209230bSgjelinek 		/* if phys & locked are both set, verify locked <= phys */
61440209230bSgjelinek 		if (res1 == Z_OK && res3 == Z_OK) {
61450209230bSgjelinek 			uint64_t phys_limit;
61460209230bSgjelinek 			char *endp;
61470209230bSgjelinek 
61480209230bSgjelinek 			phys_limit = strtoull(
61490209230bSgjelinek 			    in_progress_mcaptab.zone_physmem_cap, &endp, 10);
61500209230bSgjelinek 			if (phys_limit < locked_limit) {
61510209230bSgjelinek 				zerr(gettext("The %s cap must be less than or "
61520209230bSgjelinek 				    "equal to the %s cap."),
61530209230bSgjelinek 				    pt_to_str(PT_LOCKED),
61540209230bSgjelinek 				    pt_to_str(PT_PHYSICAL));
6155bbec428eSgjelinek 				saw_error = B_TRUE;
61560209230bSgjelinek 				return;
61570209230bSgjelinek 			}
61580209230bSgjelinek 		}
61590209230bSgjelinek 
61600209230bSgjelinek 		err = Z_OK;
61610209230bSgjelinek 		if (res1 == Z_OK) {
61620209230bSgjelinek 			/*
61630209230bSgjelinek 			 * We could be ending from either an add operation
61640209230bSgjelinek 			 * or a select operation.  Since all of the properties
61650209230bSgjelinek 			 * within this resource are optional, we always use
61660209230bSgjelinek 			 * modify on the mcap entry.  zonecfg_modify_mcap()
61670209230bSgjelinek 			 * will handle both adding and modifying a memory cap.
61680209230bSgjelinek 			 */
61690209230bSgjelinek 			err = zonecfg_modify_mcap(handle, &in_progress_mcaptab);
61700209230bSgjelinek 		} else if (end_op == CMD_SELECT) {
61710209230bSgjelinek 			/*
61720209230bSgjelinek 			 * If we're ending from a select and the physical
61730209230bSgjelinek 			 * memory cap is empty then the user could have cleared
61740209230bSgjelinek 			 * the physical cap value, so try to delete the entry.
61750209230bSgjelinek 			 */
61760209230bSgjelinek 			(void) zonecfg_delete_mcap(handle);
61770209230bSgjelinek 		}
61780209230bSgjelinek 		break;
61797c478bd9Sstevel@tonic-gate 	default:
61807c478bd9Sstevel@tonic-gate 		zone_perror(rt_to_str(resource_scope), Z_NO_RESOURCE_TYPE,
6181bbec428eSgjelinek 		    B_TRUE);
6182bbec428eSgjelinek 		saw_error = B_TRUE;
61837c478bd9Sstevel@tonic-gate 		return;
61847c478bd9Sstevel@tonic-gate 	}
61857c478bd9Sstevel@tonic-gate 
61867c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
6187bbec428eSgjelinek 		zone_perror(zone, err, B_TRUE);
61887c478bd9Sstevel@tonic-gate 	} else {
6189bbec428eSgjelinek 		need_to_commit = B_TRUE;
6190bbec428eSgjelinek 		global_scope = B_TRUE;
61917c478bd9Sstevel@tonic-gate 		end_op = -1;
61927c478bd9Sstevel@tonic-gate 	}
61937c478bd9Sstevel@tonic-gate }
61947c478bd9Sstevel@tonic-gate 
61957c478bd9Sstevel@tonic-gate void
61967c478bd9Sstevel@tonic-gate commit_func(cmd_t *cmd)
61977c478bd9Sstevel@tonic-gate {
61987c478bd9Sstevel@tonic-gate 	int arg;
6199bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
62007c478bd9Sstevel@tonic-gate 
62017c478bd9Sstevel@tonic-gate 	optind = 0;
62027ec75eb8Sgjelinek 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?")) != EOF) {
62037c478bd9Sstevel@tonic-gate 		switch (arg) {
62047c478bd9Sstevel@tonic-gate 		case '?':
62057c478bd9Sstevel@tonic-gate 			longer_usage(CMD_COMMIT);
6206bbec428eSgjelinek 			arg_err = B_TRUE;
62077ec75eb8Sgjelinek 			break;
62087c478bd9Sstevel@tonic-gate 		default:
62097c478bd9Sstevel@tonic-gate 			short_usage(CMD_COMMIT);
6210bbec428eSgjelinek 			arg_err = B_TRUE;
62117ec75eb8Sgjelinek 			break;
62127ec75eb8Sgjelinek 		}
62137ec75eb8Sgjelinek 	}
62147ec75eb8Sgjelinek 	if (arg_err)
62157c478bd9Sstevel@tonic-gate 		return;
62167ec75eb8Sgjelinek 
62177c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
62187c478bd9Sstevel@tonic-gate 		short_usage(CMD_COMMIT);
62197c478bd9Sstevel@tonic-gate 		return;
62207c478bd9Sstevel@tonic-gate 	}
62217c478bd9Sstevel@tonic-gate 
62227c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_COMMIT))
62237c478bd9Sstevel@tonic-gate 		return;
62247c478bd9Sstevel@tonic-gate 
62257c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
62267c478bd9Sstevel@tonic-gate 
62277c478bd9Sstevel@tonic-gate 	cmd->cmd_argc = 1;
62287c478bd9Sstevel@tonic-gate 	/*
62297c478bd9Sstevel@tonic-gate 	 * cmd_arg normally comes from a strdup() in the lexer, and the
62307c478bd9Sstevel@tonic-gate 	 * whole cmd structure and its (char *) attributes are freed at
62317c478bd9Sstevel@tonic-gate 	 * the completion of each command, so the strdup() below is needed
62327c478bd9Sstevel@tonic-gate 	 * to match this and prevent a core dump from trying to free()
62337c478bd9Sstevel@tonic-gate 	 * something that can't be.
62347c478bd9Sstevel@tonic-gate 	 */
62357c478bd9Sstevel@tonic-gate 	if ((cmd->cmd_argv[0] = strdup("save")) == NULL) {
6236bbec428eSgjelinek 		zone_perror(zone, Z_NOMEM, B_TRUE);
62377c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
62387c478bd9Sstevel@tonic-gate 	}
62397c478bd9Sstevel@tonic-gate 	cmd->cmd_argv[1] = NULL;
62407c478bd9Sstevel@tonic-gate 	verify_func(cmd);
62417c478bd9Sstevel@tonic-gate }
62427c478bd9Sstevel@tonic-gate 
62437c478bd9Sstevel@tonic-gate void
62447c478bd9Sstevel@tonic-gate revert_func(cmd_t *cmd)
62457c478bd9Sstevel@tonic-gate {
62467c478bd9Sstevel@tonic-gate 	char line[128];	/* enough to ask a question */
6247bbec428eSgjelinek 	boolean_t force = B_FALSE;
6248bbec428eSgjelinek 	boolean_t arg_err = B_FALSE;
62497c478bd9Sstevel@tonic-gate 	int err, arg, answer;
62507c478bd9Sstevel@tonic-gate 
62517c478bd9Sstevel@tonic-gate 	optind = 0;
62527c478bd9Sstevel@tonic-gate 	while ((arg = getopt(cmd->cmd_argc, cmd->cmd_argv, "?F")) != EOF) {
62537c478bd9Sstevel@tonic-gate 		switch (arg) {
62547c478bd9Sstevel@tonic-gate 		case '?':
62557c478bd9Sstevel@tonic-gate 			longer_usage(CMD_REVERT);
6256bbec428eSgjelinek 			arg_err = B_TRUE;
62577ec75eb8Sgjelinek 			break;
62587c478bd9Sstevel@tonic-gate 		case 'F':
6259bbec428eSgjelinek 			force = B_TRUE;
62607c478bd9Sstevel@tonic-gate 			break;
62617c478bd9Sstevel@tonic-gate 		default:
62627c478bd9Sstevel@tonic-gate 			short_usage(CMD_REVERT);
6263bbec428eSgjelinek 			arg_err = B_TRUE;
62647ec75eb8Sgjelinek 			break;
62657ec75eb8Sgjelinek 		}
62667ec75eb8Sgjelinek 	}
62677ec75eb8Sgjelinek 	if (arg_err)
62687c478bd9Sstevel@tonic-gate 		return;
62697ec75eb8Sgjelinek 
62707c478bd9Sstevel@tonic-gate 	if (optind != cmd->cmd_argc) {
62717c478bd9Sstevel@tonic-gate 		short_usage(CMD_REVERT);
62727c478bd9Sstevel@tonic-gate 		return;
62737c478bd9Sstevel@tonic-gate 	}
62747c478bd9Sstevel@tonic-gate 
62757c478bd9Sstevel@tonic-gate 	if (zone_is_read_only(CMD_REVERT))
62767c478bd9Sstevel@tonic-gate 		return;
62777c478bd9Sstevel@tonic-gate 
62787c478bd9Sstevel@tonic-gate 	if (zonecfg_check_handle(handle) != Z_OK) {
62797c478bd9Sstevel@tonic-gate 		zerr(gettext("No changes to revert."));
6280bbec428eSgjelinek 		saw_error = B_TRUE;
62817c478bd9Sstevel@tonic-gate 		return;
62827c478bd9Sstevel@tonic-gate 	}
62837c478bd9Sstevel@tonic-gate 
62847c478bd9Sstevel@tonic-gate 	if (!force) {
62857c478bd9Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
62867c478bd9Sstevel@tonic-gate 		    gettext("Are you sure you want to revert"));
6287bbec428eSgjelinek 		if ((answer = ask_yesno(B_FALSE, line)) == -1) {
62887c478bd9Sstevel@tonic-gate 			zerr(gettext("Input not from terminal and -F not "
62897c478bd9Sstevel@tonic-gate 			    "specified:\n%s command ignored, exiting."),
62907c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_REVERT));
62917c478bd9Sstevel@tonic-gate 			exit(Z_ERR);
62927c478bd9Sstevel@tonic-gate 		}
62937c478bd9Sstevel@tonic-gate 		if (answer != 1)
62947c478bd9Sstevel@tonic-gate 			return;
62957c478bd9Sstevel@tonic-gate 	}
62967c478bd9Sstevel@tonic-gate 
62977c478bd9Sstevel@tonic-gate 	/*
62987c478bd9Sstevel@tonic-gate 	 * Time for a new handle: finish the old one off first
62997c478bd9Sstevel@tonic-gate 	 * then get a new one properly to avoid leaks.
63007c478bd9Sstevel@tonic-gate 	 */
63017c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
63027c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
6303bbec428eSgjelinek 		zone_perror(execname, Z_NOMEM, B_TRUE);
63047c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
63057c478bd9Sstevel@tonic-gate 	}
6306087719fdSdp 	if ((err = zonecfg_get_handle(revert_zone, handle)) != Z_OK) {
6307bbec428eSgjelinek 		saw_error = B_TRUE;
6308bbec428eSgjelinek 		got_handle = B_FALSE;
63097c478bd9Sstevel@tonic-gate 		if (err == Z_NO_ZONE)
63107c478bd9Sstevel@tonic-gate 			zerr(gettext("%s: no such saved zone to revert to."),
6311087719fdSdp 			    revert_zone);
63127c478bd9Sstevel@tonic-gate 		else
6313bbec428eSgjelinek 			zone_perror(zone, err, B_TRUE);
63147c478bd9Sstevel@tonic-gate 	}
6315087719fdSdp 	(void) strlcpy(zone, revert_zone, sizeof (zone));
63167c478bd9Sstevel@tonic-gate }
63177c478bd9Sstevel@tonic-gate 
63187c478bd9Sstevel@tonic-gate void
63197c478bd9Sstevel@tonic-gate help_func(cmd_t *cmd)
63207c478bd9Sstevel@tonic-gate {
63217c478bd9Sstevel@tonic-gate 	int i;
63227c478bd9Sstevel@tonic-gate 
63237c478bd9Sstevel@tonic-gate 	assert(cmd != NULL);
63247c478bd9Sstevel@tonic-gate 
63257c478bd9Sstevel@tonic-gate 	if (cmd->cmd_argc == 0) {
6326bbec428eSgjelinek 		usage(B_TRUE, global_scope ? HELP_SUBCMDS : HELP_RES_SCOPE);
63277c478bd9Sstevel@tonic-gate 		return;
63287c478bd9Sstevel@tonic-gate 	}
63297c478bd9Sstevel@tonic-gate 	if (strcmp(cmd->cmd_argv[0], "usage") == 0) {
6330bbec428eSgjelinek 		usage(B_TRUE, HELP_USAGE);
63317c478bd9Sstevel@tonic-gate 		return;
63327c478bd9Sstevel@tonic-gate 	}
63337c478bd9Sstevel@tonic-gate 	if (strcmp(cmd->cmd_argv[0], "commands") == 0) {
6334bbec428eSgjelinek 		usage(B_TRUE, HELP_SUBCMDS);
63357c478bd9Sstevel@tonic-gate 		return;
63367c478bd9Sstevel@tonic-gate 	}
63377c478bd9Sstevel@tonic-gate 	if (strcmp(cmd->cmd_argv[0], "syntax") == 0) {
6338bbec428eSgjelinek 		usage(B_TRUE, HELP_SYNTAX | HELP_RES_PROPS);
63397c478bd9Sstevel@tonic-gate 		return;
63407c478bd9Sstevel@tonic-gate 	}
63417c478bd9Sstevel@tonic-gate 	if (strcmp(cmd->cmd_argv[0], "-?") == 0) {
63427c478bd9Sstevel@tonic-gate 		longer_usage(CMD_HELP);
63437c478bd9Sstevel@tonic-gate 		return;
63447c478bd9Sstevel@tonic-gate 	}
63457c478bd9Sstevel@tonic-gate 
63467c478bd9Sstevel@tonic-gate 	for (i = 0; i <= CMD_MAX; i++) {
63477c478bd9Sstevel@tonic-gate 		if (strcmp(cmd->cmd_argv[0], cmd_to_str(i)) == 0) {
63487c478bd9Sstevel@tonic-gate 			longer_usage(i);
63497c478bd9Sstevel@tonic-gate 			return;
63507c478bd9Sstevel@tonic-gate 		}
63517c478bd9Sstevel@tonic-gate 	}
63527c478bd9Sstevel@tonic-gate 	/* We do not use zerr() here because we do not want its extra \n. */
63537c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Unknown help subject %s.  "),
63547c478bd9Sstevel@tonic-gate 	    cmd->cmd_argv[0]);
6355bbec428eSgjelinek 	usage(B_FALSE, HELP_META);
63567c478bd9Sstevel@tonic-gate }
63577c478bd9Sstevel@tonic-gate 
63587c478bd9Sstevel@tonic-gate static int
63597c478bd9Sstevel@tonic-gate string_to_yyin(char *string)
63607c478bd9Sstevel@tonic-gate {
63617c478bd9Sstevel@tonic-gate 	if ((yyin = tmpfile()) == NULL) {
6362bbec428eSgjelinek 		zone_perror(execname, Z_TEMP_FILE, B_TRUE);
63637c478bd9Sstevel@tonic-gate 		return (Z_ERR);
63647c478bd9Sstevel@tonic-gate 	}
63657c478bd9Sstevel@tonic-gate 	if (fwrite(string, strlen(string), 1, yyin) != 1) {
6366bbec428eSgjelinek 		zone_perror(execname, Z_TEMP_FILE, B_TRUE);
63677c478bd9Sstevel@tonic-gate 		return (Z_ERR);
63687c478bd9Sstevel@tonic-gate 	}
63697c478bd9Sstevel@tonic-gate 	if (fseek(yyin, 0, SEEK_SET) != 0) {
6370bbec428eSgjelinek 		zone_perror(execname, Z_TEMP_FILE, B_TRUE);
63717c478bd9Sstevel@tonic-gate 		return (Z_ERR);
63727c478bd9Sstevel@tonic-gate 	}
63737c478bd9Sstevel@tonic-gate 	return (Z_OK);
63747c478bd9Sstevel@tonic-gate }
63757c478bd9Sstevel@tonic-gate 
63767c478bd9Sstevel@tonic-gate /* This is the back-end helper function for read_input() below. */
63777c478bd9Sstevel@tonic-gate 
63787c478bd9Sstevel@tonic-gate static int
63797c478bd9Sstevel@tonic-gate cleanup()
63807c478bd9Sstevel@tonic-gate {
63817c478bd9Sstevel@tonic-gate 	int answer;
63827c478bd9Sstevel@tonic-gate 	cmd_t *cmd;
63837c478bd9Sstevel@tonic-gate 
63847c478bd9Sstevel@tonic-gate 	if (!interactive_mode && !cmd_file_mode) {
63857c478bd9Sstevel@tonic-gate 		/*
63867c478bd9Sstevel@tonic-gate 		 * If we're not in interactive mode, and we're not in command
63877c478bd9Sstevel@tonic-gate 		 * file mode, then we must be in commands-from-the-command-line
63887c478bd9Sstevel@tonic-gate 		 * mode.  As such, we can't loop back and ask for more input.
63897c478bd9Sstevel@tonic-gate 		 * It was OK to prompt for such things as whether or not to
63907c478bd9Sstevel@tonic-gate 		 * really delete a zone in the command handler called from
63917c478bd9Sstevel@tonic-gate 		 * yyparse() above, but "really quit?" makes no sense in this
63927c478bd9Sstevel@tonic-gate 		 * context.  So disable prompting.
63937c478bd9Sstevel@tonic-gate 		 */
6394bbec428eSgjelinek 		ok_to_prompt = B_FALSE;
63957c478bd9Sstevel@tonic-gate 	}
63967c478bd9Sstevel@tonic-gate 	if (!global_scope) {
63977c478bd9Sstevel@tonic-gate 		if (!time_to_exit) {
63987c478bd9Sstevel@tonic-gate 			/*
63997c478bd9Sstevel@tonic-gate 			 * Just print a simple error message in the -1 case,
64007c478bd9Sstevel@tonic-gate 			 * since exit_func() already handles that case, and
64017c478bd9Sstevel@tonic-gate 			 * EOF means we are finished anyway.
64027c478bd9Sstevel@tonic-gate 			 */
6403bbec428eSgjelinek 			answer = ask_yesno(B_FALSE,
64047c478bd9Sstevel@tonic-gate 			    gettext("Resource incomplete; really quit"));
64057c478bd9Sstevel@tonic-gate 			if (answer == -1) {
64067c478bd9Sstevel@tonic-gate 				zerr(gettext("Resource incomplete."));
64077c478bd9Sstevel@tonic-gate 				return (Z_ERR);
64087c478bd9Sstevel@tonic-gate 			}
64097c478bd9Sstevel@tonic-gate 			if (answer != 1) {
64107c478bd9Sstevel@tonic-gate 				yyin = stdin;
64117c478bd9Sstevel@tonic-gate 				return (Z_REPEAT);
64127c478bd9Sstevel@tonic-gate 			}
64137c478bd9Sstevel@tonic-gate 		} else {
6414bbec428eSgjelinek 			saw_error = B_TRUE;
64157c478bd9Sstevel@tonic-gate 		}
64167c478bd9Sstevel@tonic-gate 	}
64177c478bd9Sstevel@tonic-gate 	/*
64187c478bd9Sstevel@tonic-gate 	 * Make sure we tried something and that the handle checks
64197c478bd9Sstevel@tonic-gate 	 * out, or we would get a false error trying to commit.
64207c478bd9Sstevel@tonic-gate 	 */
64217c478bd9Sstevel@tonic-gate 	if (need_to_commit && zonecfg_check_handle(handle) == Z_OK) {
64227c478bd9Sstevel@tonic-gate 		if ((cmd = alloc_cmd()) == NULL) {
6423bbec428eSgjelinek 			zone_perror(zone, Z_NOMEM, B_TRUE);
64247c478bd9Sstevel@tonic-gate 			return (Z_ERR);
64257c478bd9Sstevel@tonic-gate 		}
64267c478bd9Sstevel@tonic-gate 		cmd->cmd_argc = 0;
64277c478bd9Sstevel@tonic-gate 		cmd->cmd_argv[0] = NULL;
64287c478bd9Sstevel@tonic-gate 		commit_func(cmd);
64297c478bd9Sstevel@tonic-gate 		free_cmd(cmd);
64307c478bd9Sstevel@tonic-gate 		/*
64317c478bd9Sstevel@tonic-gate 		 * need_to_commit will get set back to FALSE if the
64327c478bd9Sstevel@tonic-gate 		 * configuration is saved successfully.
64337c478bd9Sstevel@tonic-gate 		 */
64347c478bd9Sstevel@tonic-gate 		if (need_to_commit) {
64357c478bd9Sstevel@tonic-gate 			if (force_exit) {
64367c478bd9Sstevel@tonic-gate 				zerr(gettext("Configuration not saved."));
64377c478bd9Sstevel@tonic-gate 				return (Z_ERR);
64387c478bd9Sstevel@tonic-gate 			}
6439bbec428eSgjelinek 			answer = ask_yesno(B_FALSE,
64407c478bd9Sstevel@tonic-gate 			    gettext("Configuration not saved; really quit"));
64417c478bd9Sstevel@tonic-gate 			if (answer == -1) {
64427c478bd9Sstevel@tonic-gate 				zerr(gettext("Configuration not saved."));
64437c478bd9Sstevel@tonic-gate 				return (Z_ERR);
64447c478bd9Sstevel@tonic-gate 			}
64457c478bd9Sstevel@tonic-gate 			if (answer != 1) {
6446bbec428eSgjelinek 				time_to_exit = B_FALSE;
64477c478bd9Sstevel@tonic-gate 				yyin = stdin;
64487c478bd9Sstevel@tonic-gate 				return (Z_REPEAT);
64497c478bd9Sstevel@tonic-gate 			}
64507c478bd9Sstevel@tonic-gate 		}
64517c478bd9Sstevel@tonic-gate 	}
64527c478bd9Sstevel@tonic-gate 	return ((need_to_commit || saw_error) ? Z_ERR : Z_OK);
64537c478bd9Sstevel@tonic-gate }
64547c478bd9Sstevel@tonic-gate 
64557c478bd9Sstevel@tonic-gate /*
64567c478bd9Sstevel@tonic-gate  * read_input() is the driver of this program.  It is a wrapper around
64577c478bd9Sstevel@tonic-gate  * yyparse(), printing appropriate prompts when needed, checking for
64587c478bd9Sstevel@tonic-gate  * exit conditions and reacting appropriately [the latter in its cleanup()
64597c478bd9Sstevel@tonic-gate  * helper function].
64607c478bd9Sstevel@tonic-gate  *
64617c478bd9Sstevel@tonic-gate  * Like most zonecfg functions, it returns Z_OK or Z_ERR, *or* Z_REPEAT
64627c478bd9Sstevel@tonic-gate  * so do_interactive() knows that we are not really done (i.e, we asked
64637c478bd9Sstevel@tonic-gate  * the user if we should really quit and the user said no).
64647c478bd9Sstevel@tonic-gate  */
64657c478bd9Sstevel@tonic-gate static int
64667c478bd9Sstevel@tonic-gate read_input()
64677c478bd9Sstevel@tonic-gate {
6468bbec428eSgjelinek 	boolean_t yyin_is_a_tty = isatty(fileno(yyin));
64697c478bd9Sstevel@tonic-gate 	/*
64707c478bd9Sstevel@tonic-gate 	 * The prompt is "e:z> " or "e:z:r> " where e is execname, z is zone
64717c478bd9Sstevel@tonic-gate 	 * and r is resource_scope: 5 is for the two ":"s + "> " + terminator.
64727c478bd9Sstevel@tonic-gate 	 */
64737c478bd9Sstevel@tonic-gate 	char prompt[MAXPATHLEN + ZONENAME_MAX + MAX_RT_STRLEN + 5], *line;
64747c478bd9Sstevel@tonic-gate 
64757c478bd9Sstevel@tonic-gate 	/* yyin should have been set to the appropriate (FILE *) if not stdin */
6476bbec428eSgjelinek 	newline_terminated = B_TRUE;
64777c478bd9Sstevel@tonic-gate 	for (;;) {
64787c478bd9Sstevel@tonic-gate 		if (yyin_is_a_tty) {
64797c478bd9Sstevel@tonic-gate 			if (newline_terminated) {
64807c478bd9Sstevel@tonic-gate 				if (global_scope)
64817c478bd9Sstevel@tonic-gate 					(void) snprintf(prompt, sizeof (prompt),
64827c478bd9Sstevel@tonic-gate 					    "%s:%s> ", execname, zone);
64837c478bd9Sstevel@tonic-gate 				else
64847c478bd9Sstevel@tonic-gate 					(void) snprintf(prompt, sizeof (prompt),
64857c478bd9Sstevel@tonic-gate 					    "%s:%s:%s> ", execname, zone,
64867c478bd9Sstevel@tonic-gate 					    rt_to_str(resource_scope));
64877c478bd9Sstevel@tonic-gate 			}
64887c478bd9Sstevel@tonic-gate 			/*
64897c478bd9Sstevel@tonic-gate 			 * If the user hits ^C then we want to catch it and
64907c478bd9Sstevel@tonic-gate 			 * start over.  If the user hits EOF then we want to
64917c478bd9Sstevel@tonic-gate 			 * bail out.
64927c478bd9Sstevel@tonic-gate 			 */
64937c478bd9Sstevel@tonic-gate 			line = gl_get_line(gl, prompt, NULL, -1);
64947c478bd9Sstevel@tonic-gate 			if (gl_return_status(gl) == GLR_SIGNAL) {
64957c478bd9Sstevel@tonic-gate 				gl_abandon_line(gl);
64967c478bd9Sstevel@tonic-gate 				continue;
64977c478bd9Sstevel@tonic-gate 			}
64987c478bd9Sstevel@tonic-gate 			if (line == NULL)
64997c478bd9Sstevel@tonic-gate 				break;
65007c478bd9Sstevel@tonic-gate 			(void) string_to_yyin(line);
65017c478bd9Sstevel@tonic-gate 			while (!feof(yyin))
65027c478bd9Sstevel@tonic-gate 				yyparse();
65037c478bd9Sstevel@tonic-gate 		} else {
65047c478bd9Sstevel@tonic-gate 			yyparse();
65057c478bd9Sstevel@tonic-gate 		}
65067c478bd9Sstevel@tonic-gate 		/* Bail out on an error in command file mode. */
65077c478bd9Sstevel@tonic-gate 		if (saw_error && cmd_file_mode && !interactive_mode)
6508bbec428eSgjelinek 			time_to_exit = B_TRUE;
65097c478bd9Sstevel@tonic-gate 		if (time_to_exit || (!yyin_is_a_tty && feof(yyin)))
65107c478bd9Sstevel@tonic-gate 			break;
65117c478bd9Sstevel@tonic-gate 	}
65127c478bd9Sstevel@tonic-gate 	return (cleanup());
65137c478bd9Sstevel@tonic-gate }
65147c478bd9Sstevel@tonic-gate 
65157c478bd9Sstevel@tonic-gate /*
65167c478bd9Sstevel@tonic-gate  * This function is used in the zonecfg-interactive-mode scenario: it just
65177c478bd9Sstevel@tonic-gate  * calls read_input() until we are done.
65187c478bd9Sstevel@tonic-gate  */
65197c478bd9Sstevel@tonic-gate 
65207c478bd9Sstevel@tonic-gate static int
65217c478bd9Sstevel@tonic-gate do_interactive(void)
65227c478bd9Sstevel@tonic-gate {
65237c478bd9Sstevel@tonic-gate 	int err;
65247c478bd9Sstevel@tonic-gate 
6525bbec428eSgjelinek 	interactive_mode = B_TRUE;
65267c478bd9Sstevel@tonic-gate 	if (!read_only_mode) {
65277c478bd9Sstevel@tonic-gate 		/*
65287c478bd9Sstevel@tonic-gate 		 * Try to set things up proactively in interactive mode, so
65297c478bd9Sstevel@tonic-gate 		 * that if the zone in question does not exist yet, we can
65307c478bd9Sstevel@tonic-gate 		 * provide the user with a clue.
65317c478bd9Sstevel@tonic-gate 		 */
6532bbec428eSgjelinek 		(void) initialize(B_FALSE);
65337c478bd9Sstevel@tonic-gate 	}
6534087719fdSdp 	do {
65357c478bd9Sstevel@tonic-gate 		err = read_input();
6536087719fdSdp 	} while (err == Z_REPEAT);
65377c478bd9Sstevel@tonic-gate 	return (err);
65387c478bd9Sstevel@tonic-gate }
65397c478bd9Sstevel@tonic-gate 
65407c478bd9Sstevel@tonic-gate /*
65417c478bd9Sstevel@tonic-gate  * cmd_file is slightly more complicated, as it has to open the command file
65427c478bd9Sstevel@tonic-gate  * and set yyin appropriately.  Once that is done, though, it just calls
65437c478bd9Sstevel@tonic-gate  * read_input(), and only once, since prompting is not possible.
65447c478bd9Sstevel@tonic-gate  */
65457c478bd9Sstevel@tonic-gate 
65467c478bd9Sstevel@tonic-gate static int
65477c478bd9Sstevel@tonic-gate cmd_file(char *file)
65487c478bd9Sstevel@tonic-gate {
65497c478bd9Sstevel@tonic-gate 	FILE *infile;
65507c478bd9Sstevel@tonic-gate 	int err;
65517c478bd9Sstevel@tonic-gate 	struct stat statbuf;
6552bbec428eSgjelinek 	boolean_t using_real_file = (strcmp(file, "-") != 0);
65537c478bd9Sstevel@tonic-gate 
65547c478bd9Sstevel@tonic-gate 	if (using_real_file) {
65557c478bd9Sstevel@tonic-gate 		/*
65567c478bd9Sstevel@tonic-gate 		 * zerr() prints a line number in cmd_file_mode, which we do
65577c478bd9Sstevel@tonic-gate 		 * not want here, so temporarily unset it.
65587c478bd9Sstevel@tonic-gate 		 */
6559bbec428eSgjelinek 		cmd_file_mode = B_FALSE;
65607c478bd9Sstevel@tonic-gate 		if ((infile = fopen(file, "r")) == NULL) {
65617c478bd9Sstevel@tonic-gate 			zerr(gettext("could not open file %s: %s"),
65627c478bd9Sstevel@tonic-gate 			    file, strerror(errno));
65637c478bd9Sstevel@tonic-gate 			return (Z_ERR);
65647c478bd9Sstevel@tonic-gate 		}
65657c478bd9Sstevel@tonic-gate 		if ((err = fstat(fileno(infile), &statbuf)) != 0) {
65667c478bd9Sstevel@tonic-gate 			zerr(gettext("could not stat file %s: %s"),
65677c478bd9Sstevel@tonic-gate 			    file, strerror(errno));
65687c478bd9Sstevel@tonic-gate 			err = Z_ERR;
65697c478bd9Sstevel@tonic-gate 			goto done;
65707c478bd9Sstevel@tonic-gate 		}
65717c478bd9Sstevel@tonic-gate 		if (!S_ISREG(statbuf.st_mode)) {
65727c478bd9Sstevel@tonic-gate 			zerr(gettext("%s is not a regular file."), file);
65737c478bd9Sstevel@tonic-gate 			err = Z_ERR;
65747c478bd9Sstevel@tonic-gate 			goto done;
65757c478bd9Sstevel@tonic-gate 		}
65767c478bd9Sstevel@tonic-gate 		yyin = infile;
6577bbec428eSgjelinek 		cmd_file_mode = B_TRUE;
6578bbec428eSgjelinek 		ok_to_prompt = B_FALSE;
65797c478bd9Sstevel@tonic-gate 	} else {
65807c478bd9Sstevel@tonic-gate 		/*
65817c478bd9Sstevel@tonic-gate 		 * "-f -" is essentially the same as interactive mode,
65827c478bd9Sstevel@tonic-gate 		 * so treat it that way.
65837c478bd9Sstevel@tonic-gate 		 */
6584bbec428eSgjelinek 		interactive_mode = B_TRUE;
65857c478bd9Sstevel@tonic-gate 	}
65867c478bd9Sstevel@tonic-gate 	/* Z_REPEAT is for interactive mode; treat it like Z_ERR here. */
65877c478bd9Sstevel@tonic-gate 	if ((err = read_input()) == Z_REPEAT)
65887c478bd9Sstevel@tonic-gate 		err = Z_ERR;
65897c478bd9Sstevel@tonic-gate done:
65907c478bd9Sstevel@tonic-gate 	if (using_real_file)
65917c478bd9Sstevel@tonic-gate 		(void) fclose(infile);
65927c478bd9Sstevel@tonic-gate 	return (err);
65937c478bd9Sstevel@tonic-gate }
65947c478bd9Sstevel@tonic-gate 
65957c478bd9Sstevel@tonic-gate /*
65967c478bd9Sstevel@tonic-gate  * Since yacc is based on reading from a (FILE *) whereas what we get from
65977c478bd9Sstevel@tonic-gate  * the command line is in argv format, we need to convert when the user
65987c478bd9Sstevel@tonic-gate  * gives us commands directly from the command line.  That is done here by
65997c478bd9Sstevel@tonic-gate  * concatenating the argv list into a space-separated string, writing it
66007c478bd9Sstevel@tonic-gate  * to a temp file, and rewinding the file so yyin can be set to it.  Then
66017c478bd9Sstevel@tonic-gate  * we call read_input(), and only once, since prompting about whether to
66027c478bd9Sstevel@tonic-gate  * continue or quit would make no sense in this context.
66037c478bd9Sstevel@tonic-gate  */
66047c478bd9Sstevel@tonic-gate 
66057c478bd9Sstevel@tonic-gate static int
66067c478bd9Sstevel@tonic-gate one_command_at_a_time(int argc, char *argv[])
66077c478bd9Sstevel@tonic-gate {
66087c478bd9Sstevel@tonic-gate 	char *command;
66097c478bd9Sstevel@tonic-gate 	size_t len = 2; /* terminal \n\0 */
66107c478bd9Sstevel@tonic-gate 	int i, err;
66117c478bd9Sstevel@tonic-gate 
66127c478bd9Sstevel@tonic-gate 	for (i = 0; i < argc; i++)
66137c478bd9Sstevel@tonic-gate 		len += strlen(argv[i]) + 1;
66147c478bd9Sstevel@tonic-gate 	if ((command = malloc(len)) == NULL) {
6615bbec428eSgjelinek 		zone_perror(execname, Z_NOMEM, B_TRUE);
66167c478bd9Sstevel@tonic-gate 		return (Z_ERR);
66177c478bd9Sstevel@tonic-gate 	}
66187c478bd9Sstevel@tonic-gate 	(void) strlcpy(command, argv[0], len);
66197c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
66207c478bd9Sstevel@tonic-gate 		(void) strlcat(command, " ", len);
66217c478bd9Sstevel@tonic-gate 		(void) strlcat(command, argv[i], len);
66227c478bd9Sstevel@tonic-gate 	}
66237c478bd9Sstevel@tonic-gate 	(void) strlcat(command, "\n", len);
66247c478bd9Sstevel@tonic-gate 	err = string_to_yyin(command);
66257c478bd9Sstevel@tonic-gate 	free(command);
66267c478bd9Sstevel@tonic-gate 	if (err != Z_OK)
66277c478bd9Sstevel@tonic-gate 		return (err);
66287c478bd9Sstevel@tonic-gate 	while (!feof(yyin))
66297c478bd9Sstevel@tonic-gate 		yyparse();
66307c478bd9Sstevel@tonic-gate 	return (cleanup());
66317c478bd9Sstevel@tonic-gate }
66327c478bd9Sstevel@tonic-gate 
66337c478bd9Sstevel@tonic-gate static char *
66347c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
66357c478bd9Sstevel@tonic-gate {
66367c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
66377c478bd9Sstevel@tonic-gate 
66387c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
66397c478bd9Sstevel@tonic-gate 	for (;;) {
66407c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
66417c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
66427c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
66437c478bd9Sstevel@tonic-gate 			break;
66447c478bd9Sstevel@tonic-gate 		} else {
66457c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
66467c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
66477c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
66487c478bd9Sstevel@tonic-gate 				continue;
66497c478bd9Sstevel@tonic-gate 			}
66507c478bd9Sstevel@tonic-gate 			break;
66517c478bd9Sstevel@tonic-gate 		}
66527c478bd9Sstevel@tonic-gate 	}
66537c478bd9Sstevel@tonic-gate 	return (execbasename);
66547c478bd9Sstevel@tonic-gate }
66557c478bd9Sstevel@tonic-gate 
66567c478bd9Sstevel@tonic-gate int
66577c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
66587c478bd9Sstevel@tonic-gate {
66597c478bd9Sstevel@tonic-gate 	int err, arg;
6660555afedfScarlsonj 	struct stat st;
66617c478bd9Sstevel@tonic-gate 
66627c478bd9Sstevel@tonic-gate 	/* This must be before anything goes to stdout. */
66637c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
66647c478bd9Sstevel@tonic-gate 
6665bbec428eSgjelinek 	saw_error = B_FALSE;
6666bbec428eSgjelinek 	cmd_file_mode = B_FALSE;
66677c478bd9Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
66687c478bd9Sstevel@tonic-gate 
66697c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
66707c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
66717c478bd9Sstevel@tonic-gate 
66727c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
66737c478bd9Sstevel@tonic-gate 		zerr(gettext("%s can only be run from the global zone."),
66747c478bd9Sstevel@tonic-gate 		    execname);
66757c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
66767c478bd9Sstevel@tonic-gate 	}
66777c478bd9Sstevel@tonic-gate 
66787c478bd9Sstevel@tonic-gate 	if (argc < 2) {
6679bbec428eSgjelinek 		usage(B_FALSE, HELP_USAGE | HELP_SUBCMDS);
66807c478bd9Sstevel@tonic-gate 		exit(Z_USAGE);
66817c478bd9Sstevel@tonic-gate 	}
66827c478bd9Sstevel@tonic-gate 	if (strcmp(argv[1], cmd_to_str(CMD_HELP)) == 0) {
66837c478bd9Sstevel@tonic-gate 		(void) one_command_at_a_time(argc - 1, &(argv[1]));
66847c478bd9Sstevel@tonic-gate 		exit(Z_OK);
66857c478bd9Sstevel@tonic-gate 	}
66867c478bd9Sstevel@tonic-gate 
6687555afedfScarlsonj 	while ((arg = getopt(argc, argv, "?f:R:z:")) != EOF) {
66887c478bd9Sstevel@tonic-gate 		switch (arg) {
66897c478bd9Sstevel@tonic-gate 		case '?':
66907c478bd9Sstevel@tonic-gate 			if (optopt == '?')
6691bbec428eSgjelinek 				usage(B_TRUE, HELP_USAGE | HELP_SUBCMDS);
66927c478bd9Sstevel@tonic-gate 			else
6693bbec428eSgjelinek 				usage(B_FALSE, HELP_USAGE);
66947c478bd9Sstevel@tonic-gate 			exit(Z_USAGE);
66957c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
66967c478bd9Sstevel@tonic-gate 		case 'f':
66977c478bd9Sstevel@tonic-gate 			cmd_file_name = optarg;
6698bbec428eSgjelinek 			cmd_file_mode = B_TRUE;
66997c478bd9Sstevel@tonic-gate 			break;
6700555afedfScarlsonj 		case 'R':
6701555afedfScarlsonj 			if (*optarg != '/') {
6702555afedfScarlsonj 				zerr(gettext("root path must be absolute: %s"),
6703555afedfScarlsonj 				    optarg);
6704555afedfScarlsonj 				exit(Z_USAGE);
6705555afedfScarlsonj 			}
6706555afedfScarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
6707555afedfScarlsonj 				zerr(gettext(
6708555afedfScarlsonj 				    "root path must be a directory: %s"),
6709555afedfScarlsonj 				    optarg);
6710555afedfScarlsonj 				exit(Z_USAGE);
6711555afedfScarlsonj 			}
6712555afedfScarlsonj 			zonecfg_set_root(optarg);
6713555afedfScarlsonj 			break;
67147c478bd9Sstevel@tonic-gate 		case 'z':
67150209230bSgjelinek 			if (strcmp(optarg, GLOBAL_ZONENAME) == 0) {
6716bbec428eSgjelinek 				global_zone = B_TRUE;
67170209230bSgjelinek 			} else if (zonecfg_validate_zonename(optarg) != Z_OK) {
6718bbec428eSgjelinek 				zone_perror(optarg, Z_BOGUS_ZONE_NAME, B_TRUE);
6719bbec428eSgjelinek 				usage(B_FALSE, HELP_SYNTAX);
6720087719fdSdp 				exit(Z_USAGE);
6721087719fdSdp 			}
6722087719fdSdp 			(void) strlcpy(zone, optarg, sizeof (zone));
6723087719fdSdp 			(void) strlcpy(revert_zone, optarg, sizeof (zone));
67247c478bd9Sstevel@tonic-gate 			break;
67257c478bd9Sstevel@tonic-gate 		default:
6726bbec428eSgjelinek 			usage(B_FALSE, HELP_USAGE);
67277c478bd9Sstevel@tonic-gate 			exit(Z_USAGE);
67287c478bd9Sstevel@tonic-gate 		}
67297c478bd9Sstevel@tonic-gate 	}
67307c478bd9Sstevel@tonic-gate 
6731087719fdSdp 	if (optind > argc || strcmp(zone, "") == 0) {
6732bbec428eSgjelinek 		usage(B_FALSE, HELP_USAGE);
67337c478bd9Sstevel@tonic-gate 		exit(Z_USAGE);
67347c478bd9Sstevel@tonic-gate 	}
67357c478bd9Sstevel@tonic-gate 
6736087719fdSdp 	if ((err = zonecfg_access(zone, W_OK)) == Z_OK) {
6737bbec428eSgjelinek 		read_only_mode = B_FALSE;
6738087719fdSdp 	} else if (err == Z_ACCES) {
6739bbec428eSgjelinek 		read_only_mode = B_TRUE;
67407c478bd9Sstevel@tonic-gate 		/* skip this message in one-off from command line mode */
67417c478bd9Sstevel@tonic-gate 		if (optind == argc)
67427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: you do not "
67437c478bd9Sstevel@tonic-gate 			    "have write access to this zone's configuration "
67447c478bd9Sstevel@tonic-gate 			    "file;\ngoing into read-only mode.\n"));
6745087719fdSdp 	} else {
6746087719fdSdp 		fprintf(stderr, "%s: Could not access zone configuration "
6747087719fdSdp 		    "store: %s\n", execname, zonecfg_strerror(err));
6748087719fdSdp 		exit(Z_ERR);
67497c478bd9Sstevel@tonic-gate 	}
67507c478bd9Sstevel@tonic-gate 
67517c478bd9Sstevel@tonic-gate 	if ((handle = zonecfg_init_handle()) == NULL) {
6752bbec428eSgjelinek 		zone_perror(execname, Z_NOMEM, B_TRUE);
67537c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
67547c478bd9Sstevel@tonic-gate 	}
67557c478bd9Sstevel@tonic-gate 
67567c478bd9Sstevel@tonic-gate 	/*
67577c478bd9Sstevel@tonic-gate 	 * This may get set back to FALSE again in cmd_file() if cmd_file_name
67587c478bd9Sstevel@tonic-gate 	 * is a "real" file as opposed to "-" (i.e. meaning use stdin).
67597c478bd9Sstevel@tonic-gate 	 */
67607c478bd9Sstevel@tonic-gate 	if (isatty(STDIN_FILENO))
6761bbec428eSgjelinek 		ok_to_prompt = B_TRUE;
67627c478bd9Sstevel@tonic-gate 	if ((gl = new_GetLine(MAX_LINE_LEN, MAX_CMD_HIST)) == NULL)
67637c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
67647c478bd9Sstevel@tonic-gate 	if (gl_customize_completion(gl, NULL, cmd_cpl_fn) != 0)
67657c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
67667c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
67677c478bd9Sstevel@tonic-gate 	if (optind == argc) {
67687c478bd9Sstevel@tonic-gate 		if (!cmd_file_mode)
67697c478bd9Sstevel@tonic-gate 			err = do_interactive();
67707c478bd9Sstevel@tonic-gate 		else
67717c478bd9Sstevel@tonic-gate 			err = cmd_file(cmd_file_name);
67727c478bd9Sstevel@tonic-gate 	} else {
67737c478bd9Sstevel@tonic-gate 		err = one_command_at_a_time(argc - optind, &(argv[optind]));
67747c478bd9Sstevel@tonic-gate 	}
67757c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
67769acbbeafSnn35248 	if (brand != NULL)
67779acbbeafSnn35248 		brand_close(brand);
67787c478bd9Sstevel@tonic-gate 	(void) del_GetLine(gl);
67797c478bd9Sstevel@tonic-gate 	return (err);
67807c478bd9Sstevel@tonic-gate }
6781