xref: /titanic_51/usr/src/cmd/zoneadm/zoneadm.c (revision ce28b40ea6e3ce6ebc74a45f03500d7dffe134b1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ee519a1fSgjelinek  * Common Development and Distribution License (the "License").
6ee519a1fSgjelinek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217e362f58Scomay 
227c478bd9Sstevel@tonic-gate /*
23865e09a4Sgjelinek  * Copyright 2006 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  * zoneadm is a command interpreter for zone administration.  It is all in
317c478bd9Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
327c478bd9Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
337c478bd9Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
347c478bd9Sstevel@tonic-gate  * handler functions and their helper functions.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
377c478bd9Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
387c478bd9Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
397c478bd9Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
407c478bd9Sstevel@tonic-gate  * that "foo" need not be translated.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <signal.h>
477c478bd9Sstevel@tonic-gate #include <stdarg.h>
487c478bd9Sstevel@tonic-gate #include <ctype.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #include <wait.h>
527c478bd9Sstevel@tonic-gate #include <zone.h>
537c478bd9Sstevel@tonic-gate #include <priv.h>
547c478bd9Sstevel@tonic-gate #include <locale.h>
557c478bd9Sstevel@tonic-gate #include <libintl.h>
567c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
577c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
589acbbeafSnn35248 #include <sys/brand.h>
597c478bd9Sstevel@tonic-gate #include <sys/param.h>
607c478bd9Sstevel@tonic-gate #include <sys/types.h>
617c478bd9Sstevel@tonic-gate #include <sys/stat.h>
627c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
637c478bd9Sstevel@tonic-gate #include <assert.h>
647c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
657c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
667c478bd9Sstevel@tonic-gate #include <limits.h>
670b5de56dSgjelinek #include <dirent.h>
68555afedfScarlsonj #include <uuid/uuid.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <fcntl.h>
717c478bd9Sstevel@tonic-gate #include <door.h>
727c478bd9Sstevel@tonic-gate #include <macros.h>
737c478bd9Sstevel@tonic-gate #include <libgen.h>
74865e09a4Sgjelinek #include <fnmatch.h>
75e7f3c547Sgjelinek #include <sys/modctl.h>
769acbbeafSnn35248 #include <libbrand.h>
770209230bSgjelinek #include <libscf.h>
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #include <pool.h>
807c478bd9Sstevel@tonic-gate #include <sys/pool.h>
810209230bSgjelinek #include <sys/priocntl.h>
820209230bSgjelinek #include <sys/fsspriocntl.h>
837c478bd9Sstevel@tonic-gate 
840b5de56dSgjelinek #include "zoneadm.h"
850b5de56dSgjelinek 
867c478bd9Sstevel@tonic-gate #define	MAXARGS	8
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */
897c478bd9Sstevel@tonic-gate typedef struct zone_entry {
907c478bd9Sstevel@tonic-gate 	zoneid_t	zid;
917c478bd9Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
927c478bd9Sstevel@tonic-gate 	char		*zstate_str;
937c478bd9Sstevel@tonic-gate 	zone_state_t	zstate_num;
949acbbeafSnn35248 	char		zbrand[MAXNAMELEN];
957c478bd9Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
96555afedfScarlsonj 	char		zuuid[UUID_PRINTABLE_STRING_LENGTH];
977c478bd9Sstevel@tonic-gate } zone_entry_t;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static zone_entry_t *zents;
1007c478bd9Sstevel@tonic-gate static size_t nzents;
1019acbbeafSnn35248 static boolean_t is_native_zone = B_TRUE;
1027c478bd9Sstevel@tonic-gate 
1031390a385Sgjelinek #define	LOOPBACK_IF	"lo0"
1041390a385Sgjelinek #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
1051390a385Sgjelinek 
1061390a385Sgjelinek struct net_if {
1071390a385Sgjelinek 	char	*name;
1081390a385Sgjelinek 	int	af;
1091390a385Sgjelinek };
1101390a385Sgjelinek 
1117c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
1127c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1137c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate struct cmd {
1167c478bd9Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1177c478bd9Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1187c478bd9Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1197c478bd9Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate };
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #define	SHELP_HELP	"help"
1243f2f09c1Sdp #define	SHELP_BOOT	"boot [-- boot_arguments]"
1257c478bd9Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1267c478bd9Sstevel@tonic-gate #define	SHELP_READY	"ready"
1273f2f09c1Sdp #define	SHELP_REBOOT	"reboot [-- boot_arguments]"
1287c478bd9Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1297c478bd9Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1309acbbeafSnn35248 #define	SHELP_INSTALL	"install [-x nodataset] [brand-specific args]"
1317c478bd9Sstevel@tonic-gate #define	SHELP_UNINSTALL	"uninstall [-F]"
1320b5de56dSgjelinek #define	SHELP_CLONE	"clone [-m method] [-s <ZFS snapshot>] zonename"
133865e09a4Sgjelinek #define	SHELP_MOVE	"move zonepath"
1348cd327d5Sgjelinek #define	SHELP_DETACH	"detach [-n]"
1358cd327d5Sgjelinek #define	SHELP_ATTACH	"attach [-F] [-n <path>]"
136555afedfScarlsonj #define	SHELP_MARK	"mark incomplete"
1377c478bd9Sstevel@tonic-gate 
1389acbbeafSnn35248 #define	EXEC_PREFIX	"exec "
1399acbbeafSnn35248 #define	EXEC_LEN	(strlen(EXEC_PREFIX))
1409acbbeafSnn35248 #define	RMCOMMAND	"/usr/bin/rm -rf"
1419acbbeafSnn35248 
1429acbbeafSnn35248 static int cleanup_zonepath(char *, boolean_t);
1439acbbeafSnn35248 
1447c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1457c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1467c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1477c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1487c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1497c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1507c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1517c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1527c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
153108322fbScarlsonj static int mount_func(int argc, char *argv[]);
154108322fbScarlsonj static int unmount_func(int argc, char *argv[]);
155865e09a4Sgjelinek static int clone_func(int argc, char *argv[]);
156865e09a4Sgjelinek static int move_func(int argc, char *argv[]);
157ee519a1fSgjelinek static int detach_func(int argc, char *argv[]);
158ee519a1fSgjelinek static int attach_func(int argc, char *argv[]);
159555afedfScarlsonj static int mark_func(int argc, char *argv[]);
1600209230bSgjelinek static int apply_func(int argc, char *argv[]);
1617c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1629acbbeafSnn35248     boolean_t unsafe_when_running, boolean_t force);
1637c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd);
164*ce28b40eSzt129084 static int verify_details(int, char *argv[]);
165*ce28b40eSzt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]);
166*ce28b40eSzt129084 static int invoke_brand_handler(int, char *argv[]);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = {
1697c478bd9Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1707c478bd9Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1717c478bd9Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1727c478bd9Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1737c478bd9Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1747c478bd9Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1757c478bd9Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1767c478bd9Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1777c478bd9Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
178108322fbScarlsonj 	    uninstall_func },
179865e09a4Sgjelinek 	/* mount and unmount are private commands for admin/install */
180108322fbScarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
181865e09a4Sgjelinek 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
182865e09a4Sgjelinek 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
183ee519a1fSgjelinek 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
184ee519a1fSgjelinek 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
185555afedfScarlsonj 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func },
1860209230bSgjelinek 	{ CMD_MARK,		"mark",		SHELP_MARK,	mark_func },
1870209230bSgjelinek 	{ CMD_APPLY,		"apply",	NULL,		apply_func }
1887c478bd9Sstevel@tonic-gate };
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /* global variables */
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1937c478bd9Sstevel@tonic-gate static char *execname;
1949acbbeafSnn35248 static char target_brand[MAXNAMELEN];
1957c478bd9Sstevel@tonic-gate static char *locale;
1960b5de56dSgjelinek char *target_zone;
197555afedfScarlsonj static char *target_uuid;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */
2007c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed;
2019acbbeafSnn35248 static int do_subproc_cnt = 0;
2029acbbeafSnn35248 
2039acbbeafSnn35248 /*
2049acbbeafSnn35248  * Used to indicate whether this zoneadm instance has another zoneadm
2059acbbeafSnn35248  * instance in its ancestry.
2069acbbeafSnn35248  */
2079acbbeafSnn35248 static boolean_t zoneadm_is_nested = B_FALSE;
2089acbbeafSnn35248 
2099acbbeafSnn35248 /* used to track nested zone-lock operations */
2109acbbeafSnn35248 static int zone_lock_cnt = 0;
2119acbbeafSnn35248 
2129acbbeafSnn35248 /* used to communicate lock status to children */
2139acbbeafSnn35248 #define	LOCK_ENV_VAR	"_ZONEADM_LOCK_HELD"
2149acbbeafSnn35248 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1";
2159acbbeafSnn35248 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0";
2167c478bd9Sstevel@tonic-gate 
2170b5de56dSgjelinek char *
2187c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2217c478bd9Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
2257c478bd9Sstevel@tonic-gate static char *
2267c478bd9Sstevel@tonic-gate long_help(int cmd_num)
2277c478bd9Sstevel@tonic-gate {
2287e362f58Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2297c478bd9Sstevel@tonic-gate 	switch (cmd_num) {
2307c478bd9Sstevel@tonic-gate 	case CMD_HELP:
2317c478bd9Sstevel@tonic-gate 		return (gettext("Print usage message."));
2327c478bd9Sstevel@tonic-gate 	case CMD_BOOT:
2333f2f09c1Sdp 		return (gettext("Activates (boots) specified zone.  See "
2343f2f09c1Sdp 		    "zoneadm(1m) for valid boot\n\targuments."));
2357c478bd9Sstevel@tonic-gate 	case CMD_HALT:
2369e518655Sgjelinek 		return (gettext("Halts specified zone, bypassing shutdown "
2379e518655Sgjelinek 		    "scripts and removing runtime\n\tresources of the zone."));
2387c478bd9Sstevel@tonic-gate 	case CMD_READY:
2399e518655Sgjelinek 		return (gettext("Prepares a zone for running applications but "
2409e518655Sgjelinek 		    "does not start any user\n\tprocesses in the zone."));
2417c478bd9Sstevel@tonic-gate 	case CMD_REBOOT:
2429e518655Sgjelinek 		return (gettext("Restarts the zone (equivalent to a halt / "
2433f2f09c1Sdp 		    "boot sequence).\n\tFails if the zone is not active.  "
2443f2f09c1Sdp 		    "See zoneadm(1m) for valid boot\n\targuments."));
2457c478bd9Sstevel@tonic-gate 	case CMD_LIST:
2467c478bd9Sstevel@tonic-gate 		return (gettext("Lists the current zones, or a "
2477c478bd9Sstevel@tonic-gate 		    "specific zone if indicated.  By default,\n\tall "
2487c478bd9Sstevel@tonic-gate 		    "running zones are listed, though this can be "
2497c478bd9Sstevel@tonic-gate 		    "expanded to all\n\tinstalled zones with the -i "
2507c478bd9Sstevel@tonic-gate 		    "option or all configured zones with the\n\t-c "
251555afedfScarlsonj 		    "option.  When used with the general -z <zone> and/or -u "
252555afedfScarlsonj 		    "<uuid-match>\n\toptions, lists only the specified "
253555afedfScarlsonj 		    "matching zone, but lists it\n\tregardless of its state, "
254555afedfScarlsonj 		    "and the -i and -c options are disallowed.  The\n\t-v "
255555afedfScarlsonj 		    "option can be used to display verbose information: zone "
256555afedfScarlsonj 		    "name, id,\n\tcurrent state, root directory and options.  "
257555afedfScarlsonj 		    "The -p option can be used\n\tto request machine-parsable "
258555afedfScarlsonj 		    "output.  The -v and -p options are mutually\n\texclusive."
259555afedfScarlsonj 		    "  If neither -v nor -p is used, just the zone name is "
260555afedfScarlsonj 		    "listed."));
2617c478bd9Sstevel@tonic-gate 	case CMD_VERIFY:
2627c478bd9Sstevel@tonic-gate 		return (gettext("Check to make sure the configuration "
2637c478bd9Sstevel@tonic-gate 		    "can safely be instantiated\n\ton the machine: "
2647c478bd9Sstevel@tonic-gate 		    "physical network interfaces exist, etc."));
2657c478bd9Sstevel@tonic-gate 	case CMD_INSTALL:
2660b5de56dSgjelinek 		return (gettext("Install the configuration on to the system.  "
2670b5de56dSgjelinek 		    "The -x nodataset option\n\tcan be used to prevent the "
2680b5de56dSgjelinek 		    "creation of a new ZFS file system for the\n\tzone "
2699acbbeafSnn35248 		    "(assuming the zonepath is within a ZFS file system).\n\t"
2709acbbeafSnn35248 		    "All other arguments are passed to the brand installation "
2719acbbeafSnn35248 		    "function;\n\tsee brand(4) for more information."));
2727c478bd9Sstevel@tonic-gate 	case CMD_UNINSTALL:
2739e518655Sgjelinek 		return (gettext("Uninstall the configuration from the system.  "
2749e518655Sgjelinek 		    "The -F flag can be used\n\tto force the action."));
275865e09a4Sgjelinek 	case CMD_CLONE:
2760b5de56dSgjelinek 		return (gettext("Clone the installation of another zone.  "
2770b5de56dSgjelinek 		    "The -m option can be used to\n\tspecify 'copy' which "
2780b5de56dSgjelinek 		    "forces a copy of the source zone.  The -s option\n\t"
2790b5de56dSgjelinek 		    "can be used to specify the name of a ZFS snapshot "
2800b5de56dSgjelinek 		    "that was taken from\n\ta previous clone command.  The "
2810b5de56dSgjelinek 		    "snapshot will be used as the source\n\tinstead of "
2820b5de56dSgjelinek 		    "creating a new ZFS snapshot."));
283865e09a4Sgjelinek 	case CMD_MOVE:
284865e09a4Sgjelinek 		return (gettext("Move the zone to a new zonepath."));
2859e518655Sgjelinek 	case CMD_DETACH:
2869e518655Sgjelinek 		return (gettext("Detach the zone from the system. The zone "
2879e518655Sgjelinek 		    "state is changed to\n\t'configured' (but the files under "
2889e518655Sgjelinek 		    "the zonepath are untouched).\n\tThe zone can subsequently "
2899e518655Sgjelinek 		    "be attached, or can be moved to another\n\tsystem and "
2908cd327d5Sgjelinek 		    "attached there.  The -n option can be used to specify\n\t"
2918cd327d5Sgjelinek 		    "'no-execute' mode.  When -n is used, the information "
2928cd327d5Sgjelinek 		    "needed to attach\n\tthe zone is sent to standard output "
2938cd327d5Sgjelinek 		    "but the zone is not actually\n\tdetached."));
2949e518655Sgjelinek 	case CMD_ATTACH:
2959e518655Sgjelinek 		return (gettext("Attach the zone to the system.  The zone "
2969e518655Sgjelinek 		    "state must be 'configured'\n\tprior to attach; upon "
2979e518655Sgjelinek 		    "successful completion, the zone state will be\n\t"
2989e518655Sgjelinek 		    "'installed'.  The system software on the current "
2999e518655Sgjelinek 		    "system must be\n\tcompatible with the software on the "
3009e518655Sgjelinek 		    "zone's original system.\n\tSpecify -F to force the attach "
3018cd327d5Sgjelinek 		    "and skip software compatibility tests.\n\tThe -n option "
3028cd327d5Sgjelinek 		    "can be used to specify 'no-execute' mode.  When -n is\n\t"
3038cd327d5Sgjelinek 		    "used, the information needed to attach the zone is read "
3048cd327d5Sgjelinek 		    "from the\n\tspecified path and the configuration is only "
3058cd327d5Sgjelinek 		    "validated.  The path can\n\tbe '-' to specify standard "
3068cd327d5Sgjelinek 		    "input."));
307555afedfScarlsonj 	case CMD_MARK:
308555afedfScarlsonj 		return (gettext("Set the state of the zone.  This can be used "
309555afedfScarlsonj 		    "to force the zone\n\tstate to 'incomplete' "
310555afedfScarlsonj 		    "administratively if some activity has rendered\n\tthe "
311555afedfScarlsonj 		    "zone permanently unusable.  The only valid state that "
312555afedfScarlsonj 		    "may be\n\tspecified is 'incomplete'."));
313108322fbScarlsonj 	default:
314108322fbScarlsonj 		return ("");
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3177e362f58Scomay 	return (NULL);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
3227c478bd9Sstevel@tonic-gate  * unexpected errors.
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate static int
3267c478bd9Sstevel@tonic-gate usage(boolean_t explicit)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	int i;
3297c478bd9Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
332555afedfScarlsonj 	(void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
333555afedfScarlsonj 	    execname);
334555afedfScarlsonj 	(void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
3357c478bd9Sstevel@tonic-gate 	    gettext("subcommand"));
3367c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
3377c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
338108322fbScarlsonj 		if (cmdtab[i].short_usage == NULL)
339108322fbScarlsonj 			continue;
3407c478bd9Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
3417c478bd9Sstevel@tonic-gate 		if (explicit)
3427c478bd9Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	if (!explicit)
3457c478bd9Sstevel@tonic-gate 		(void) fputs("\n", fd);
3467c478bd9Sstevel@tonic-gate 	return (Z_USAGE);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate static void
3507c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
3537c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
3587c478bd9Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
3597c478bd9Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate 
3620b5de56dSgjelinek void
3637c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
3667c478bd9Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
3717c478bd9Sstevel@tonic-gate  * supplied zone name after the executable.
3727c478bd9Sstevel@tonic-gate  *
3737c478bd9Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
3747c478bd9Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
3757c478bd9Sstevel@tonic-gate  * like zperror() above.
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate 
3780b5de56dSgjelinek void
3797c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3827c478bd9Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
3860b5de56dSgjelinek void
3877c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	va_list alist;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
3927c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3937c478bd9Sstevel@tonic-gate 	if (target_zone != NULL)
3947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3957c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3967c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3977c478bd9Sstevel@tonic-gate 	va_end(alist);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate static void *
4017c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (r == NULL) {
4067c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
4077c478bd9Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
4087c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	return (r);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate static void
4147c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	assert(!(verbose && parsable));
4197c478bd9Sstevel@tonic-gate 	if (firsttime && verbose) {
4207c478bd9Sstevel@tonic-gate 		firsttime = B_FALSE;
4219acbbeafSnn35248 		(void) printf("%*s %-16s %-14s %-30s %-10s\n", ZONEID_WIDTH,
4229acbbeafSnn35248 		    "ID", "NAME", "STATUS", "PATH", "BRAND");
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	if (!verbose) {
425555afedfScarlsonj 		char *cp, *clim;
426555afedfScarlsonj 
4277c478bd9Sstevel@tonic-gate 		if (!parsable) {
4287c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
4297c478bd9Sstevel@tonic-gate 			return;
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4327c478bd9Sstevel@tonic-gate 			(void) printf("-");
4337c478bd9Sstevel@tonic-gate 		else
4347c478bd9Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
435555afedfScarlsonj 		(void) printf(":%s:%s:", zent->zname, zent->zstate_str);
436555afedfScarlsonj 		cp = zent->zroot;
437555afedfScarlsonj 		while ((clim = strchr(cp, ':')) != NULL) {
438555afedfScarlsonj 			(void) printf("%.*s\\:", clim - cp, cp);
439555afedfScarlsonj 			cp = clim + 1;
440555afedfScarlsonj 		}
4419acbbeafSnn35248 		(void) printf("%s:%s:%s\n", cp, zent->zuuid, zent->zbrand);
4427c478bd9Sstevel@tonic-gate 		return;
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
4457c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4467c478bd9Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
4477c478bd9Sstevel@tonic-gate 		else
4487c478bd9Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
4499acbbeafSnn35248 		(void) printf(" %-16s %-14s %-30s %-10s\n", zent->zname,
4509acbbeafSnn35248 		    zent->zstate_str, zent->zroot, zent->zbrand);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate static int
455108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
4567c478bd9Sstevel@tonic-gate {
45745916cd2Sjpk 	char root[MAXPATHLEN], *cp;
4587c478bd9Sstevel@tonic-gate 	int err;
459555afedfScarlsonj 	uuid_t uuid;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
4627c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
4639acbbeafSnn35248 	(void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
4647c478bd9Sstevel@tonic-gate 	zent->zstate_str = "???";
4657c478bd9Sstevel@tonic-gate 
466108322fbScarlsonj 	zent->zid = zid;
4677c478bd9Sstevel@tonic-gate 
468555afedfScarlsonj 	if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
469555afedfScarlsonj 	    !uuid_is_null(uuid))
470555afedfScarlsonj 		uuid_unparse(uuid, zent->zuuid);
471555afedfScarlsonj 	else
472555afedfScarlsonj 		zent->zuuid[0] = '\0';
473555afedfScarlsonj 
47445916cd2Sjpk 	/*
47545916cd2Sjpk 	 * For labeled zones which query the zone path of lower-level
47645916cd2Sjpk 	 * zones, the path needs to be adjusted to drop the final
47745916cd2Sjpk 	 * "/root" component. This adjusted path is then useful
47845916cd2Sjpk 	 * for reading down any exported directories from the
47945916cd2Sjpk 	 * lower-level zone.
48045916cd2Sjpk 	 */
48145916cd2Sjpk 	if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
48245916cd2Sjpk 		if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
48345916cd2Sjpk 		    sizeof (zent->zroot)) == -1) {
48445916cd2Sjpk 			zperror2(zent->zname,
48545916cd2Sjpk 			    gettext("could not get zone path."));
48645916cd2Sjpk 			return (Z_ERR);
48745916cd2Sjpk 		}
48845916cd2Sjpk 		cp = zent->zroot + strlen(zent->zroot) - 5;
48945916cd2Sjpk 		if (cp > zent->zroot && strcmp(cp, "/root") == 0)
49045916cd2Sjpk 			*cp = 0;
49145916cd2Sjpk 	} else {
49245916cd2Sjpk 		if ((err = zone_get_zonepath(zent->zname, root,
49345916cd2Sjpk 		    sizeof (root))) != Z_OK) {
4947c478bd9Sstevel@tonic-gate 			errno = err;
49545916cd2Sjpk 			zperror2(zent->zname,
49645916cd2Sjpk 			    gettext("could not get zone path."));
4977c478bd9Sstevel@tonic-gate 			return (Z_ERR);
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
50045916cd2Sjpk 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
5037c478bd9Sstevel@tonic-gate 		errno = err;
5047c478bd9Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
5057c478bd9Sstevel@tonic-gate 		return (Z_ERR);
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
508bafa7067Snn35248 
509bafa7067Snn35248 	/*
510bafa7067Snn35248 	 * A zone's brand is only available in the .xml file describing it,
511bafa7067Snn35248 	 * which is only visible to the global zone.  This causes
512bafa7067Snn35248 	 * zone_get_brand() to fail when called from within a non-global
513bafa7067Snn35248 	 * zone.  Fortunately we only do this on labeled systems, where we
514bafa7067Snn35248 	 * know all zones are native.
515bafa7067Snn35248 	 */
516bafa7067Snn35248 	if (getzoneid() != GLOBAL_ZONEID) {
517bafa7067Snn35248 		assert(is_system_labeled() != 0);
518bafa7067Snn35248 		(void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME,
519bafa7067Snn35248 		    sizeof (zent->zbrand));
520bafa7067Snn35248 	} else if (zone_get_brand(zent->zname, zent->zbrand,
5219acbbeafSnn35248 	    sizeof (zent->zbrand)) != Z_OK) {
5229acbbeafSnn35248 		zperror2(zent->zname, gettext("could not get brand name"));
5239acbbeafSnn35248 		return (Z_ERR);
5249acbbeafSnn35248 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	return (Z_OK);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate /*
5307c478bd9Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
5317c478bd9Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
5327c478bd9Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
5337c478bd9Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
5347c478bd9Sstevel@tonic-gate  * return immediately to save work.
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static int
538108322fbScarlsonj fetch_zents(void)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	zoneid_t *zids = NULL;
5417c478bd9Sstevel@tonic-gate 	uint_t nzents_saved;
542108322fbScarlsonj 	int i, retv;
543108322fbScarlsonj 	FILE *fp;
544108322fbScarlsonj 	boolean_t inaltroot;
545108322fbScarlsonj 	zone_entry_t *zentp;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	if (nzents > 0)
5487c478bd9Sstevel@tonic-gate 		return (Z_OK);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
5517c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
5527c478bd9Sstevel@tonic-gate 		return (Z_ERR);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate again:
5567c478bd9Sstevel@tonic-gate 	if (nzents == 0)
5577c478bd9Sstevel@tonic-gate 		return (Z_OK);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
5607c478bd9Sstevel@tonic-gate 	nzents_saved = nzents;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
5637c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
5647c478bd9Sstevel@tonic-gate 		free(zids);
5657c478bd9Sstevel@tonic-gate 		return (Z_ERR);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	if (nzents != nzents_saved) {
5687c478bd9Sstevel@tonic-gate 		/* list changed, try again */
5697c478bd9Sstevel@tonic-gate 		free(zids);
5707c478bd9Sstevel@tonic-gate 		goto again;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
5747c478bd9Sstevel@tonic-gate 
575108322fbScarlsonj 	inaltroot = zonecfg_in_alt_root();
576108322fbScarlsonj 	if (inaltroot)
577108322fbScarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
578108322fbScarlsonj 	else
579108322fbScarlsonj 		fp = NULL;
580108322fbScarlsonj 	zentp = zents;
581108322fbScarlsonj 	retv = Z_OK;
5827c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5837c478bd9Sstevel@tonic-gate 		char name[ZONENAME_MAX];
584108322fbScarlsonj 		char altname[ZONENAME_MAX];
5857c478bd9Sstevel@tonic-gate 
586108322fbScarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
5877c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
588108322fbScarlsonj 			retv = Z_ERR;
589108322fbScarlsonj 			continue;
5907c478bd9Sstevel@tonic-gate 		}
591108322fbScarlsonj 		if (zonecfg_is_scratch(name)) {
592108322fbScarlsonj 			/* Ignore scratch zones by default */
593108322fbScarlsonj 			if (!inaltroot)
594108322fbScarlsonj 				continue;
595108322fbScarlsonj 			if (fp == NULL ||
596108322fbScarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
597108322fbScarlsonj 			    sizeof (altname), NULL, 0) == -1) {
5985ee84fbdSgjelinek 				zerror(gettext("could not resolve scratch "
599108322fbScarlsonj 				    "zone %s"), name);
600108322fbScarlsonj 				retv = Z_ERR;
601108322fbScarlsonj 				continue;
602108322fbScarlsonj 			}
603108322fbScarlsonj 			(void) strcpy(name, altname);
604108322fbScarlsonj 		} else {
605108322fbScarlsonj 			/* Ignore non-scratch when in an alternate root */
606108322fbScarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
607108322fbScarlsonj 				continue;
608108322fbScarlsonj 		}
609108322fbScarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
610108322fbScarlsonj 			zerror(gettext("failed to get zone data"));
611108322fbScarlsonj 			retv = Z_ERR;
612108322fbScarlsonj 			continue;
613108322fbScarlsonj 		}
614108322fbScarlsonj 		zentp++;
615108322fbScarlsonj 	}
616108322fbScarlsonj 	nzents = zentp - zents;
617108322fbScarlsonj 	if (fp != NULL)
618108322fbScarlsonj 		zonecfg_close_scratch(fp);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	free(zids);
621108322fbScarlsonj 	return (retv);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
624108322fbScarlsonj static int
6257c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	int i;
6287c478bd9Sstevel@tonic-gate 	zone_entry_t zent;
6297c478bd9Sstevel@tonic-gate 	FILE *cookie;
6307c478bd9Sstevel@tonic-gate 	char *name;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/*
6337c478bd9Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
6347c478bd9Sstevel@tonic-gate 	 * If that is all we need, then return.
6357c478bd9Sstevel@tonic-gate 	 */
636108322fbScarlsonj 	if ((i = fetch_zents()) != Z_OK) {
6377c478bd9Sstevel@tonic-gate 		/*
6387c478bd9Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
6397c478bd9Sstevel@tonic-gate 		 * care of this.
6407c478bd9Sstevel@tonic-gate 		 */
641108322fbScarlsonj 		return (i);
6427c478bd9Sstevel@tonic-gate 	}
643108322fbScarlsonj 	for (i = 0; i < nzents; i++)
6447c478bd9Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
6457c478bd9Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
646108322fbScarlsonj 		return (Z_OK);
6477c478bd9Sstevel@tonic-gate 	/*
6487c478bd9Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
6497c478bd9Sstevel@tonic-gate 	 * any we have already printed.
6507c478bd9Sstevel@tonic-gate 	 */
6517c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
6527c478bd9Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
6537c478bd9Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
6547c478bd9Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
6557c478bd9Sstevel@tonic-gate 				break;
6567c478bd9Sstevel@tonic-gate 		}
6577c478bd9Sstevel@tonic-gate 		if (i < nzents) {
6587c478bd9Sstevel@tonic-gate 			free(name);
6597c478bd9Sstevel@tonic-gate 			continue;
6607c478bd9Sstevel@tonic-gate 		}
661108322fbScarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
6627c478bd9Sstevel@tonic-gate 			free(name);
6637c478bd9Sstevel@tonic-gate 			continue;
6647c478bd9Sstevel@tonic-gate 		}
6657c478bd9Sstevel@tonic-gate 		free(name);
6667c478bd9Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
6677c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
670108322fbScarlsonj 	return (Z_OK);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate static zone_entry_t *
6747c478bd9Sstevel@tonic-gate lookup_running_zone(char *str)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
6777c478bd9Sstevel@tonic-gate 	char *cp;
6787c478bd9Sstevel@tonic-gate 	int i;
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
6817c478bd9Sstevel@tonic-gate 		return (NULL);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6847c478bd9Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
6857c478bd9Sstevel@tonic-gate 			return (&zents[i]);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	errno = 0;
6887c478bd9Sstevel@tonic-gate 	zoneid = strtol(str, &cp, 0);
6897c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
6907c478bd9Sstevel@tonic-gate 	    errno != 0 || *cp != '\0')
6917c478bd9Sstevel@tonic-gate 		return (NULL);
6927c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6937c478bd9Sstevel@tonic-gate 		if (zoneid == zents[i].zid)
6947c478bd9Sstevel@tonic-gate 			return (&zents[i]);
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 	return (NULL);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate /*
7007c478bd9Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
7017c478bd9Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
7027c478bd9Sstevel@tonic-gate  */
7037c478bd9Sstevel@tonic-gate static boolean_t
7047c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	char *str;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
7097c478bd9Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
7107c478bd9Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
7117c478bd9Sstevel@tonic-gate 	/*
7127c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
7137c478bd9Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
7147c478bd9Sstevel@tonic-gate 	 * either:
7157c478bd9Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
7167c478bd9Sstevel@tonic-gate 	 * or
7177c478bd9Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
7187c478bd9Sstevel@tonic-gate 	 */
7197c478bd9Sstevel@tonic-gate 	switch (bit) {
7207c478bd9Sstevel@tonic-gate 	case S_IRUSR:
7217c478bd9Sstevel@tonic-gate 		str = gettext("owner readable");
7227c478bd9Sstevel@tonic-gate 		break;
7237c478bd9Sstevel@tonic-gate 	case S_IWUSR:
7247c478bd9Sstevel@tonic-gate 		str = gettext("owner writable");
7257c478bd9Sstevel@tonic-gate 		break;
7267c478bd9Sstevel@tonic-gate 	case S_IXUSR:
7277c478bd9Sstevel@tonic-gate 		str = gettext("owner executable");
7287c478bd9Sstevel@tonic-gate 		break;
7297c478bd9Sstevel@tonic-gate 	case S_IRGRP:
7307c478bd9Sstevel@tonic-gate 		str = gettext("group readable");
7317c478bd9Sstevel@tonic-gate 		break;
7327c478bd9Sstevel@tonic-gate 	case S_IWGRP:
7337c478bd9Sstevel@tonic-gate 		str = gettext("group writable");
7347c478bd9Sstevel@tonic-gate 		break;
7357c478bd9Sstevel@tonic-gate 	case S_IXGRP:
7367c478bd9Sstevel@tonic-gate 		str = gettext("group executable");
7377c478bd9Sstevel@tonic-gate 		break;
7387c478bd9Sstevel@tonic-gate 	case S_IROTH:
7397c478bd9Sstevel@tonic-gate 		str = gettext("world readable");
7407c478bd9Sstevel@tonic-gate 		break;
7417c478bd9Sstevel@tonic-gate 	case S_IWOTH:
7427c478bd9Sstevel@tonic-gate 		str = gettext("world writable");
7437c478bd9Sstevel@tonic-gate 		break;
7447c478bd9Sstevel@tonic-gate 	case S_IXOTH:
7457c478bd9Sstevel@tonic-gate 		str = gettext("world executable");
7467c478bd9Sstevel@tonic-gate 		break;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
7497c478bd9Sstevel@tonic-gate 		/*
7507c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
7517c478bd9Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
7527c478bd9Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
7537c478bd9Sstevel@tonic-gate 		 * strings from above.
7547c478bd9Sstevel@tonic-gate 		 */
7557c478bd9Sstevel@tonic-gate 		/*
7567c478bd9Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
7577c478bd9Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
7587c478bd9Sstevel@tonic-gate 		 */
7597c478bd9Sstevel@tonic-gate 		if (on) {
7607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
7617c478bd9Sstevel@tonic-gate 			    file, str);
7627c478bd9Sstevel@tonic-gate 		} else {
7637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
7647c478bd9Sstevel@tonic-gate 			    file, str);
7657c478bd9Sstevel@tonic-gate 		}
7667c478bd9Sstevel@tonic-gate 		return (B_TRUE);
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate 	return (B_FALSE);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate /*
7727c478bd9Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
7737c478bd9Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
7747c478bd9Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
7757c478bd9Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
7767c478bd9Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
7777c478bd9Sstevel@tonic-gate  */
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate static int
7807c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
7837c478bd9Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
7847c478bd9Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
7857c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
7867c478bd9Sstevel@tonic-gate 	int res, err;
7877c478bd9Sstevel@tonic-gate 	FILE *cookie;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
7907c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
7917c478bd9Sstevel@tonic-gate 		/* Skip zones which are not installed. */
7927c478bd9Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
7937c478bd9Sstevel@tonic-gate 			free(ze);
7947c478bd9Sstevel@tonic-gate 			continue;
7957c478bd9Sstevel@tonic-gate 		}
7967c478bd9Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
7977c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
7987c478bd9Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
7997c478bd9Sstevel@tonic-gate 			free(ze);
8007c478bd9Sstevel@tonic-gate 			continue;
8017c478bd9Sstevel@tonic-gate 		}
8027c478bd9Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
8037c478bd9Sstevel@tonic-gate 			/* old index file without path, fall back */
8047c478bd9Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
8057c478bd9Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
8067c478bd9Sstevel@tonic-gate 				errno = err;
8077c478bd9Sstevel@tonic-gate 				zperror2(ze->zone_name,
8087c478bd9Sstevel@tonic-gate 				    gettext("could not get zone path"));
8097c478bd9Sstevel@tonic-gate 				free(ze);
8107c478bd9Sstevel@tonic-gate 				continue;
8117c478bd9Sstevel@tonic-gate 			}
8127c478bd9Sstevel@tonic-gate 		}
813108322fbScarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
814108322fbScarlsonj 		    zonecfg_get_root(), ze->zone_path);
815108322fbScarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
8167c478bd9Sstevel@tonic-gate 		if (res == -1) {
8177c478bd9Sstevel@tonic-gate 			if (errno != ENOENT) {
818108322fbScarlsonj 				zperror(path_copy, B_FALSE);
8197c478bd9Sstevel@tonic-gate 				free(ze);
8207c478bd9Sstevel@tonic-gate 				return (Z_ERR);
8217c478bd9Sstevel@tonic-gate 			}
8227c478bd9Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
8237c478bd9Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
824108322fbScarlsonj 			    "zonepath", path_copy);
8257c478bd9Sstevel@tonic-gate 			free(ze);
8267c478bd9Sstevel@tonic-gate 			continue;
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 		rpath[res] = '\0';
8297c478bd9Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
8307c478bd9Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
8317c478bd9Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
8327c478bd9Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
8335ee84fbdSgjelinek 			/*
8345ee84fbdSgjelinek 			 * TRANSLATION_NOTE
8355ee84fbdSgjelinek 			 * zonepath is a literal that should not be translated.
8365ee84fbdSgjelinek 			 */
8377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
8387c478bd9Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
8397c478bd9Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
8407c478bd9Sstevel@tonic-gate 			free(ze);
8417c478bd9Sstevel@tonic-gate 			return (Z_ERR);
8427c478bd9Sstevel@tonic-gate 		}
8437c478bd9Sstevel@tonic-gate 		free(ze);
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
8467c478bd9Sstevel@tonic-gate 	return (Z_OK);
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate static int
8507c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	int res;			/* result of last library/system call */
8537c478bd9Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
8547c478bd9Sstevel@tonic-gate 	struct stat stbuf;
8553f2f09c1Sdp 	struct statvfs64 vfsbuf;
8567c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
8577c478bd9Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
8587c478bd9Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
8597c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
8607c478bd9Sstevel@tonic-gate 	zone_state_t state;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	if (path[0] != '/') {
8637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8647c478bd9Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
8657c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
8687c478bd9Sstevel@tonic-gate 		if ((errno != ENOENT) ||
869865e09a4Sgjelinek 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
870865e09a4Sgjelinek 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
8717c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
8727c478bd9Sstevel@tonic-gate 			return (Z_ERR);
8737c478bd9Sstevel@tonic-gate 		}
8747c478bd9Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
8755ee84fbdSgjelinek 			/*
8765ee84fbdSgjelinek 			 * TRANSLATION_NOTE
8775ee84fbdSgjelinek 			 * zoneadm is a literal that should not be translated.
8785ee84fbdSgjelinek 			 */
8797c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
8805ee84fbdSgjelinek 			    "exist, so it could not be verified.\nWhen "
8815ee84fbdSgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
8825ee84fbdSgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
8835ee84fbdSgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
8845ee84fbdSgjelinek 			    "other-writable\nor\n%s overlaps with any other "
8857c478bd9Sstevel@tonic-gate 			    "installed zones.\n"), path,
8867c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
8877c478bd9Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
8887c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
8897c478bd9Sstevel@tonic-gate 			return (Z_OK);
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 		/*
8927c478bd9Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
8937c478bd9Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
8947c478bd9Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
8957c478bd9Sstevel@tonic-gate 		 */
8967c478bd9Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
8977c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
8987c478bd9Sstevel@tonic-gate 			return (Z_ERR);
8997c478bd9Sstevel@tonic-gate 		}
9007c478bd9Sstevel@tonic-gate 		/*
9017c478bd9Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
9027c478bd9Sstevel@tonic-gate 		 * as well continue the verify procedure.
9037c478bd9Sstevel@tonic-gate 		 */
9047c478bd9Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
9057c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
9067c478bd9Sstevel@tonic-gate 		/*
9077c478bd9Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
9087c478bd9Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
9097c478bd9Sstevel@tonic-gate 		 * only recurse once.
9107c478bd9Sstevel@tonic-gate 		 */
911865e09a4Sgjelinek 		return (validate_zonepath(path, cmd_num));
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
9147c478bd9Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
9157c478bd9Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
9167c478bd9Sstevel@tonic-gate 		zperror(path, B_TRUE);
9177c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
9207c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
9217c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
9247c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
9257c478bd9Sstevel@tonic-gate 		    rpath);
9267c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 	if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) ||
9297c478bd9Sstevel@tonic-gate 	    (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) {
9307c478bd9Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
9310b5de56dSgjelinek 		    "file system.\n"), rpath);
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
9347c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
9377c478bd9Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
9387c478bd9Sstevel@tonic-gate 	 * to be fixed up front.
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
9417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
9427c478bd9Sstevel@tonic-gate 		    rpath);
9437c478bd9Sstevel@tonic-gate 		err = B_TRUE;
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
9467c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
9477c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
9487c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
9497c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
9507c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
9517c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
9527c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
9537c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
9567c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
9577c478bd9Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
9587c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9597c478bd9Sstevel@tonic-gate 	}
9607c478bd9Sstevel@tonic-gate 	rppath[res] = '\0';
9617c478bd9Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
9627c478bd9Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
9637c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 	/* theoretically impossible */
9667c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
9677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
9687c478bd9Sstevel@tonic-gate 		    rppath);
9697c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9707c478bd9Sstevel@tonic-gate 	}
9717c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
9727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
9737c478bd9Sstevel@tonic-gate 		    rppath);
9747c478bd9Sstevel@tonic-gate 		err = B_TRUE;
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
9777c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
9787c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
9797c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
9807c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
9817c478bd9Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
9827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
9837c478bd9Sstevel@tonic-gate 		    rppath);
9847c478bd9Sstevel@tonic-gate 		err = B_TRUE;
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 
9873f2f09c1Sdp 	if (statvfs64(rpath, &vfsbuf) != 0) {
9887c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
9897c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9907c478bd9Sstevel@tonic-gate 	}
991b5abaf04Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
9925ee84fbdSgjelinek 		/*
9935ee84fbdSgjelinek 		 * TRANSLATION_NOTE
9945ee84fbdSgjelinek 		 * Zonepath and NFS are literals that should not be translated.
9955ee84fbdSgjelinek 		 */
9965ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
9970b5de56dSgjelinek 		    "mounted file system.\n"
9980b5de56dSgjelinek 		    "\tA local file system must be used.\n"), rpath);
999b5abaf04Sgjelinek 		return (Z_ERR);
1000b5abaf04Sgjelinek 	}
1001b5abaf04Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
10025ee84fbdSgjelinek 		/*
10035ee84fbdSgjelinek 		 * TRANSLATION_NOTE
10045ee84fbdSgjelinek 		 * Zonepath and nosuid are literals that should not be
10055ee84fbdSgjelinek 		 * translated.
10065ee84fbdSgjelinek 		 */
1007b5abaf04Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
10080b5de56dSgjelinek 		    "file system.\n"), rpath);
10097c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
10137c478bd9Sstevel@tonic-gate 		errno = res;
10147c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
10157c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 	/*
10187c478bd9Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
10197c478bd9Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
1020ee519a1fSgjelinek 	 * However, the root path is expected to be there if the zone is
1021ee519a1fSgjelinek 	 * detached.
10227c478bd9Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
10237c478bd9Sstevel@tonic-gate 	 */
1024ee519a1fSgjelinek 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
10257c478bd9Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
10267c478bd9Sstevel@tonic-gate 		    sizeof (rootpath)) {
10275ee84fbdSgjelinek 			/*
10285ee84fbdSgjelinek 			 * TRANSLATION_NOTE
10295ee84fbdSgjelinek 			 * Zonepath is a literal that should not be translated.
10305ee84fbdSgjelinek 			 */
10317c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10327c478bd9Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
10337c478bd9Sstevel@tonic-gate 			return (Z_ERR);
10347c478bd9Sstevel@tonic-gate 		}
10357c478bd9Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
1036ee519a1fSgjelinek 			if (zonecfg_detached(rpath))
1037ee519a1fSgjelinek 				(void) fprintf(stderr,
1038ee519a1fSgjelinek 				    gettext("Cannot %s detached "
1039ee519a1fSgjelinek 				    "zone.\nUse attach or remove %s "
1040ee519a1fSgjelinek 				    "directory.\n"), cmd_to_str(cmd_num),
1041ee519a1fSgjelinek 				    rpath);
1042ee519a1fSgjelinek 			else
1043ee519a1fSgjelinek 				(void) fprintf(stderr,
1044ee519a1fSgjelinek 				    gettext("Rootpath %s exists; "
1045ee519a1fSgjelinek 				    "remove or move aside prior to %s.\n"),
1046ee519a1fSgjelinek 				    rootpath, cmd_to_str(cmd_num));
10477c478bd9Sstevel@tonic-gate 			return (Z_ERR);
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate 
10549acbbeafSnn35248 /*
10559acbbeafSnn35248  * The following two routines implement a simple locking mechanism to
10569acbbeafSnn35248  * ensure that only one instance of zoneadm at a time is able to manipulate
10579acbbeafSnn35248  * a given zone.  The lock is built on top of an fcntl(2) lock of
10589acbbeafSnn35248  * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock.  If a zoneadm instance
10599acbbeafSnn35248  * can grab that lock, it is allowed to manipulate the zone.
10609acbbeafSnn35248  *
10619acbbeafSnn35248  * Since zoneadm may call external applications which in turn invoke
10629acbbeafSnn35248  * zoneadm again, we introduce the notion of "lock inheritance".  Any
10639acbbeafSnn35248  * instance of zoneadm that has another instance in its ancestry is assumed
10649acbbeafSnn35248  * to be acting on behalf of the original zoneadm, and is thus allowed to
10659acbbeafSnn35248  * manipulate its zone.
10669acbbeafSnn35248  *
10679acbbeafSnn35248  * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment
10689acbbeafSnn35248  * variable.  When zoneadm is granted a lock on its zone, this environment
10699acbbeafSnn35248  * variable is set to 1.  When it releases the lock, the variable is set to
10709acbbeafSnn35248  * 0.  Since a child process inherits its parent's environment, checking
10719acbbeafSnn35248  * the state of this variable indicates whether or not any ancestor owns
10729acbbeafSnn35248  * the lock.
10739acbbeafSnn35248  */
10747c478bd9Sstevel@tonic-gate static void
10757c478bd9Sstevel@tonic-gate release_lock_file(int lockfd)
10767c478bd9Sstevel@tonic-gate {
10779acbbeafSnn35248 	/*
10789acbbeafSnn35248 	 * If we are cleaning up from a failed attempt to lock the zone for
10799acbbeafSnn35248 	 * the first time, we might have a zone_lock_cnt of 0.  In that
10809acbbeafSnn35248 	 * error case, we don't want to do anything but close the lock
10819acbbeafSnn35248 	 * file.
10829acbbeafSnn35248 	 */
10839acbbeafSnn35248 	assert(zone_lock_cnt >= 0);
10849acbbeafSnn35248 	if (zone_lock_cnt > 0) {
10859acbbeafSnn35248 		assert(getenv(LOCK_ENV_VAR) != NULL);
10869acbbeafSnn35248 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
10879acbbeafSnn35248 		if (--zone_lock_cnt > 0) {
10889acbbeafSnn35248 			assert(lockfd == -1);
10899acbbeafSnn35248 			return;
10909acbbeafSnn35248 		}
10919acbbeafSnn35248 		if (putenv(zoneadm_lock_not_held) != 0) {
10929acbbeafSnn35248 			zperror(target_zone, B_TRUE);
10939acbbeafSnn35248 			exit(Z_ERR);
10949acbbeafSnn35248 		}
10959acbbeafSnn35248 	}
10969acbbeafSnn35248 	assert(lockfd >= 0);
10977c478bd9Sstevel@tonic-gate 	(void) close(lockfd);
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate static int
11017c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
11047c478bd9Sstevel@tonic-gate 	struct flock flock;
11057c478bd9Sstevel@tonic-gate 
11069acbbeafSnn35248 	/*
11079acbbeafSnn35248 	 * If we already have the lock, we can skip this expensive song
11089acbbeafSnn35248 	 * and dance.
11099acbbeafSnn35248 	 */
11109acbbeafSnn35248 	if (zone_lock_cnt > 0) {
11119acbbeafSnn35248 		zone_lock_cnt++;
11129acbbeafSnn35248 		*lockfd = -1;
11139acbbeafSnn35248 		return (Z_OK);
11149acbbeafSnn35248 	}
11159acbbeafSnn35248 	assert(getenv(LOCK_ENV_VAR) != NULL);
11169acbbeafSnn35248 	assert(atoi(getenv(LOCK_ENV_VAR)) == 0);
11179acbbeafSnn35248 
1118108322fbScarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
1119108322fbScarlsonj 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
1120108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
1121108322fbScarlsonj 		return (Z_ERR);
1122108322fbScarlsonj 	}
1123108322fbScarlsonj 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
1124108322fbScarlsonj 		zerror(gettext("could not mkdir %s: %s"), pathbuf,
11257c478bd9Sstevel@tonic-gate 		    strerror(errno));
11267c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11277c478bd9Sstevel@tonic-gate 	}
1128108322fbScarlsonj 	(void) chmod(pathbuf, S_IRWXU);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	/*
11317c478bd9Sstevel@tonic-gate 	 * One of these lock files is created for each zone (when needed).
11327c478bd9Sstevel@tonic-gate 	 * The lock files are not cleaned up (except on system reboot),
11337c478bd9Sstevel@tonic-gate 	 * but since there is only one per zone, there is no resource
11347c478bd9Sstevel@tonic-gate 	 * starvation issue.
11357c478bd9Sstevel@tonic-gate 	 */
1136108322fbScarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
1137108322fbScarlsonj 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
1138108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
1139108322fbScarlsonj 		return (Z_ERR);
1140108322fbScarlsonj 	}
11417c478bd9Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
11427c478bd9Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
11437c478bd9Sstevel@tonic-gate 		    strerror(errno));
11447c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 	/*
11477c478bd9Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
11487c478bd9Sstevel@tonic-gate 	 */
11497c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
11507c478bd9Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
11517c478bd9Sstevel@tonic-gate 	flock.l_start = (off_t)0;
11527c478bd9Sstevel@tonic-gate 	flock.l_len = (off_t)0;
11539acbbeafSnn35248 	if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) ||
11549acbbeafSnn35248 	    (putenv(zoneadm_lock_held) != 0)) {
11557c478bd9Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
11567c478bd9Sstevel@tonic-gate 		    strerror(errno));
11577c478bd9Sstevel@tonic-gate 		release_lock_file(*lockfd);
11587c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11597c478bd9Sstevel@tonic-gate 	}
11609acbbeafSnn35248 	zone_lock_cnt = 1;
11617c478bd9Sstevel@tonic-gate 	return (Z_OK);
11627c478bd9Sstevel@tonic-gate }
11637c478bd9Sstevel@tonic-gate 
1164108322fbScarlsonj static boolean_t
11657c478bd9Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer)
11667c478bd9Sstevel@tonic-gate {
1167108322fbScarlsonj 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
1168108322fbScarlsonj 	    zonecfg_get_root(), zone_name) < PATH_MAX);
11697c478bd9Sstevel@tonic-gate }
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate /*
11727c478bd9Sstevel@tonic-gate  * system daemons are not audited.  For the global zone, this occurs
11737c478bd9Sstevel@tonic-gate  * "naturally" since init is started with the default audit
11747c478bd9Sstevel@tonic-gate  * characteristics.  Since zoneadmd is a system daemon and it starts
11757c478bd9Sstevel@tonic-gate  * init for a zone, it is necessary to clear out the audit
11767c478bd9Sstevel@tonic-gate  * characteristics inherited from whomever started zoneadmd.  This is
11777c478bd9Sstevel@tonic-gate  * indicated by the audit id, which is set from the ruid parameter of
11787c478bd9Sstevel@tonic-gate  * adt_set_user(), below.
11797c478bd9Sstevel@tonic-gate  */
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate static void
11827c478bd9Sstevel@tonic-gate prepare_audit_context()
11837c478bd9Sstevel@tonic-gate {
11847c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah;
11857c478bd9Sstevel@tonic-gate 	char			*failure = gettext("audit failure: %s");
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
11887c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
11897c478bd9Sstevel@tonic-gate 		return;
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
11927c478bd9Sstevel@tonic-gate 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
11937c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
11947c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
11957c478bd9Sstevel@tonic-gate 		return;
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 	if (adt_set_proc(ah))
11987c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	(void) adt_end_session(ah);
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate static int
12047c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
12057c478bd9Sstevel@tonic-gate {
12067c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
12077c478bd9Sstevel@tonic-gate 	pid_t child_pid;
12087c478bd9Sstevel@tonic-gate 	int error = Z_ERR;
12097c478bd9Sstevel@tonic-gate 	int doorfd, lockfd;
12107c478bd9Sstevel@tonic-gate 	struct door_info info;
12117c478bd9Sstevel@tonic-gate 
1212108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath))
1213108322fbScarlsonj 		return (Z_ERR);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
12167c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	/*
12197c478bd9Sstevel@tonic-gate 	 * Now that we have the lock, re-confirm that the daemon is
12207c478bd9Sstevel@tonic-gate 	 * *not* up and working fine.  If it is still down, we have a green
12217c478bd9Sstevel@tonic-gate 	 * light to start it.
12227c478bd9Sstevel@tonic-gate 	 */
12237c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
12247c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
12257c478bd9Sstevel@tonic-gate 			zperror(doorpath, B_FALSE);
12267c478bd9Sstevel@tonic-gate 			goto out;
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 	} else {
12297c478bd9Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
12307c478bd9Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
12317c478bd9Sstevel@tonic-gate 			error = Z_OK;
12327c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
12337c478bd9Sstevel@tonic-gate 			goto out;
12347c478bd9Sstevel@tonic-gate 		}
12357c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
12397c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"), B_FALSE);
12407c478bd9Sstevel@tonic-gate 		goto out;
12417c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
1242108322fbScarlsonj 		const char *argv[6], **ap;
12437c478bd9Sstevel@tonic-gate 
1244108322fbScarlsonj 		/* child process */
12457c478bd9Sstevel@tonic-gate 		prepare_audit_context();
12467c478bd9Sstevel@tonic-gate 
1247108322fbScarlsonj 		ap = argv;
1248108322fbScarlsonj 		*ap++ = "zoneadmd";
1249108322fbScarlsonj 		*ap++ = "-z";
1250108322fbScarlsonj 		*ap++ = zone_name;
1251108322fbScarlsonj 		if (zonecfg_in_alt_root()) {
1252108322fbScarlsonj 			*ap++ = "-R";
1253108322fbScarlsonj 			*ap++ = zonecfg_get_root();
1254108322fbScarlsonj 		}
1255108322fbScarlsonj 		*ap = NULL;
1256108322fbScarlsonj 
1257108322fbScarlsonj 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
12585ee84fbdSgjelinek 		/*
12595ee84fbdSgjelinek 		 * TRANSLATION_NOTE
12605ee84fbdSgjelinek 		 * zoneadmd is a literal that should not be translated.
12615ee84fbdSgjelinek 		 */
12627c478bd9Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
12637c478bd9Sstevel@tonic-gate 		_exit(Z_ERR);
12647c478bd9Sstevel@tonic-gate 	} else {
12657c478bd9Sstevel@tonic-gate 		/* parent process */
12667c478bd9Sstevel@tonic-gate 		pid_t retval;
12677c478bd9Sstevel@tonic-gate 		int pstatus = 0;
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 		do {
12707c478bd9Sstevel@tonic-gate 			retval = waitpid(child_pid, &pstatus, 0);
12717c478bd9Sstevel@tonic-gate 		} while (retval != child_pid);
12727c478bd9Sstevel@tonic-gate 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
12737c478bd9Sstevel@tonic-gate 		    WEXITSTATUS(pstatus) != 0)) {
12747c478bd9Sstevel@tonic-gate 			zerror(gettext("could not start %s"), "zoneadmd");
12757c478bd9Sstevel@tonic-gate 			goto out;
12767c478bd9Sstevel@tonic-gate 		}
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 	error = Z_OK;
12797c478bd9Sstevel@tonic-gate out:
12807c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
12817c478bd9Sstevel@tonic-gate 	return (error);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate static int
12857c478bd9Sstevel@tonic-gate ping_zoneadmd(const char *zone_name)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
12887c478bd9Sstevel@tonic-gate 	int doorfd;
12897c478bd9Sstevel@tonic-gate 	struct door_info info;
12907c478bd9Sstevel@tonic-gate 
1291108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath))
1292108322fbScarlsonj 		return (Z_ERR);
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
12957c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12967c478bd9Sstevel@tonic-gate 	}
12977c478bd9Sstevel@tonic-gate 	if (door_info(doorfd, &info) == 0 &&
12987c478bd9Sstevel@tonic-gate 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
12997c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
13007c478bd9Sstevel@tonic-gate 		return (Z_OK);
13017c478bd9Sstevel@tonic-gate 	}
13027c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
13037c478bd9Sstevel@tonic-gate 	return (Z_ERR);
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate static int
13077c478bd9Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
13087c478bd9Sstevel@tonic-gate {
13097c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
13107c478bd9Sstevel@tonic-gate 	int doorfd, result;
13117c478bd9Sstevel@tonic-gate 	door_arg_t darg;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
13147c478bd9Sstevel@tonic-gate 	uint64_t uniqid = 0;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
13177c478bd9Sstevel@tonic-gate 	size_t rlen;
13187c478bd9Sstevel@tonic-gate 	char *cp, *errbuf;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	rlen = getpagesize();
13217c478bd9Sstevel@tonic-gate 	if ((rvalp = malloc(rlen)) == NULL) {
13227c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
13237c478bd9Sstevel@tonic-gate 		    strerror(errno));
13247c478bd9Sstevel@tonic-gate 		return (-1);
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
13287c478bd9Sstevel@tonic-gate 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
13297c478bd9Sstevel@tonic-gate 		    sizeof (uniqid));
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 	arg->uniqid = uniqid;
13327c478bd9Sstevel@tonic-gate 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
1333108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath)) {
1334108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
1335108322fbScarlsonj 		free(rvalp);
1336108322fbScarlsonj 		return (-1);
1337108322fbScarlsonj 	}
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	/*
13407c478bd9Sstevel@tonic-gate 	 * Loop trying to start zoneadmd; if something goes seriously
13417c478bd9Sstevel@tonic-gate 	 * wrong we break out and fail.
13427c478bd9Sstevel@tonic-gate 	 */
13437c478bd9Sstevel@tonic-gate 	for (;;) {
13447c478bd9Sstevel@tonic-gate 		if (start_zoneadmd(zone_name) != Z_OK)
13457c478bd9Sstevel@tonic-gate 			break;
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
13487c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to open zone door"), B_FALSE);
13497c478bd9Sstevel@tonic-gate 			break;
13507c478bd9Sstevel@tonic-gate 		}
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 		darg.data_ptr = (char *)arg;
13537c478bd9Sstevel@tonic-gate 		darg.data_size = sizeof (*arg);
13547c478bd9Sstevel@tonic-gate 		darg.desc_ptr = NULL;
13557c478bd9Sstevel@tonic-gate 		darg.desc_num = 0;
13567c478bd9Sstevel@tonic-gate 		darg.rbuf = (char *)rvalp;
13577c478bd9Sstevel@tonic-gate 		darg.rsize = rlen;
13587c478bd9Sstevel@tonic-gate 		if (door_call(doorfd, &darg) != 0) {
13597c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
13607c478bd9Sstevel@tonic-gate 			/*
13617c478bd9Sstevel@tonic-gate 			 * We'll get EBADF if the door has been revoked.
13627c478bd9Sstevel@tonic-gate 			 */
13637c478bd9Sstevel@tonic-gate 			if (errno != EBADF) {
13647c478bd9Sstevel@tonic-gate 				zperror(gettext("door_call failed"), B_FALSE);
13657c478bd9Sstevel@tonic-gate 				break;
13667c478bd9Sstevel@tonic-gate 			}
13677c478bd9Sstevel@tonic-gate 			continue;	/* take another lap */
13687c478bd9Sstevel@tonic-gate 		}
13697c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 		if (darg.data_size == 0) {
13727c478bd9Sstevel@tonic-gate 			/* Door server is going away; kick it again. */
13737c478bd9Sstevel@tonic-gate 			continue;
13747c478bd9Sstevel@tonic-gate 		}
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 		errbuf = rvalp->errbuf;
13777c478bd9Sstevel@tonic-gate 		while (*errbuf != '\0') {
13787c478bd9Sstevel@tonic-gate 			/*
13797c478bd9Sstevel@tonic-gate 			 * Remove any newlines since zerror()
13807c478bd9Sstevel@tonic-gate 			 * will append one automatically.
13817c478bd9Sstevel@tonic-gate 			 */
13827c478bd9Sstevel@tonic-gate 			cp = strchr(errbuf, '\n');
13837c478bd9Sstevel@tonic-gate 			if (cp != NULL)
13847c478bd9Sstevel@tonic-gate 				*cp = '\0';
13857c478bd9Sstevel@tonic-gate 			zerror("%s", errbuf);
13867c478bd9Sstevel@tonic-gate 			if (cp == NULL)
13877c478bd9Sstevel@tonic-gate 				break;
13887c478bd9Sstevel@tonic-gate 			errbuf = cp + 1;
13897c478bd9Sstevel@tonic-gate 		}
13907c478bd9Sstevel@tonic-gate 		result = rvalp->rval == 0 ? 0 : -1;
13917c478bd9Sstevel@tonic-gate 		free(rvalp);
13927c478bd9Sstevel@tonic-gate 		return (result);
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	free(rvalp);
13967c478bd9Sstevel@tonic-gate 	return (-1);
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate static int
1400*ce28b40eSzt129084 invoke_brand_handler(int cmd_num, char *argv[])
1401*ce28b40eSzt129084 {
1402*ce28b40eSzt129084 	zone_dochandle_t handle;
1403*ce28b40eSzt129084 	int err;
1404*ce28b40eSzt129084 
1405*ce28b40eSzt129084 	if ((handle = zonecfg_init_handle()) == NULL) {
1406*ce28b40eSzt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
1407*ce28b40eSzt129084 		return (Z_ERR);
1408*ce28b40eSzt129084 	}
1409*ce28b40eSzt129084 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
1410*ce28b40eSzt129084 		errno = err;
1411*ce28b40eSzt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
1412*ce28b40eSzt129084 		zonecfg_fini_handle(handle);
1413*ce28b40eSzt129084 		return (Z_ERR);
1414*ce28b40eSzt129084 	}
1415*ce28b40eSzt129084 	if (verify_brand(handle, cmd_num, argv) != Z_OK) {
1416*ce28b40eSzt129084 		zonecfg_fini_handle(handle);
1417*ce28b40eSzt129084 		return (Z_ERR);
1418*ce28b40eSzt129084 	}
1419*ce28b40eSzt129084 	zonecfg_fini_handle(handle);
1420*ce28b40eSzt129084 	return (Z_OK);
1421*ce28b40eSzt129084 }
1422*ce28b40eSzt129084 
1423*ce28b40eSzt129084 static int
14247c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[])
14257c478bd9Sstevel@tonic-gate {
14267c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
14277c478bd9Sstevel@tonic-gate 	int arg;
14287c478bd9Sstevel@tonic-gate 
1429108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1430108322fbScarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1431108322fbScarlsonj 		return (Z_ERR);
1432108322fbScarlsonj 	}
1433108322fbScarlsonj 
14347c478bd9Sstevel@tonic-gate 	optind = 0;
14357c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
14367c478bd9Sstevel@tonic-gate 		switch (arg) {
14377c478bd9Sstevel@tonic-gate 		case '?':
14387c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
14397c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14407c478bd9Sstevel@tonic-gate 		default:
14417c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
14427c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
14437c478bd9Sstevel@tonic-gate 		}
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 	if (argc > optind) {
14467c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
14477c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
14487c478bd9Sstevel@tonic-gate 	}
14499acbbeafSnn35248 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
14509acbbeafSnn35248 	    != Z_OK)
14517c478bd9Sstevel@tonic-gate 		return (Z_ERR);
1452*ce28b40eSzt129084 	if (verify_details(CMD_READY, argv) != Z_OK)
14537c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_READY;
14567c478bd9Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
14577c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
14587c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 	return (Z_OK);
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate static int
14647c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[])
14657c478bd9Sstevel@tonic-gate {
14667c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
14679acbbeafSnn35248 	boolean_t force = B_FALSE;
14687c478bd9Sstevel@tonic-gate 	int arg;
14697c478bd9Sstevel@tonic-gate 
1470108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1471108322fbScarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1472108322fbScarlsonj 		return (Z_ERR);
1473108322fbScarlsonj 	}
1474108322fbScarlsonj 
14757c478bd9Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	/*
14783f2f09c1Sdp 	 * The following getopt processes arguments to zone boot; that
14793f2f09c1Sdp 	 * is to say, the [here] portion of the argument string:
14803f2f09c1Sdp 	 *
14813f2f09c1Sdp 	 *	zoneadm -z myzone boot [here] -- -v -m verbose
14823f2f09c1Sdp 	 *
14833f2f09c1Sdp 	 * Where [here] can either be nothing, -? (in which case we bail
14849acbbeafSnn35248 	 * and print usage), -f (a private option to indicate that the
14859acbbeafSnn35248 	 * boot operation should be 'forced'), or -s.  Support for -s is
14869acbbeafSnn35248 	 * vestigal and obsolete, but is retained because it was a
14879acbbeafSnn35248 	 * documented interface and there are known consumers including
14889acbbeafSnn35248 	 * admin/install; the proper way to specify boot arguments like -s
14899acbbeafSnn35248 	 * is:
14903f2f09c1Sdp 	 *
14913f2f09c1Sdp 	 *	zoneadm -z myzone boot -- -s -v -m verbose.
14927c478bd9Sstevel@tonic-gate 	 */
14937c478bd9Sstevel@tonic-gate 	optind = 0;
14949acbbeafSnn35248 	while ((arg = getopt(argc, argv, "?fs")) != EOF) {
14957c478bd9Sstevel@tonic-gate 		switch (arg) {
14967c478bd9Sstevel@tonic-gate 		case '?':
14977c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
14987c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14997c478bd9Sstevel@tonic-gate 		case 's':
15007c478bd9Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
15017c478bd9Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
15027c478bd9Sstevel@tonic-gate 			break;
15039acbbeafSnn35248 		case 'f':
15049acbbeafSnn35248 			force = B_TRUE;
15059acbbeafSnn35248 			break;
15067c478bd9Sstevel@tonic-gate 		default:
15077c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
15087c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
15097c478bd9Sstevel@tonic-gate 		}
15107c478bd9Sstevel@tonic-gate 	}
15113f2f09c1Sdp 
15123f2f09c1Sdp 	for (; optind < argc; optind++) {
15133f2f09c1Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
15143f2f09c1Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
15153f2f09c1Sdp 			zerror(gettext("Boot argument list too long"));
15163f2f09c1Sdp 			return (Z_ERR);
15177c478bd9Sstevel@tonic-gate 		}
15183f2f09c1Sdp 		if (optind < argc - 1)
15193f2f09c1Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
15203f2f09c1Sdp 			    sizeof (zarg.bootbuf)) {
15213f2f09c1Sdp 				zerror(gettext("Boot argument list too long"));
15223f2f09c1Sdp 				return (Z_ERR);
15233f2f09c1Sdp 			}
15243f2f09c1Sdp 	}
15259acbbeafSnn35248 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
15269acbbeafSnn35248 	    != Z_OK)
15277c478bd9Sstevel@tonic-gate 		return (Z_ERR);
1528*ce28b40eSzt129084 	if (verify_details(CMD_BOOT, argv) != Z_OK)
15297c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15309acbbeafSnn35248 	zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
15317c478bd9Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
15327c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
15337c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15347c478bd9Sstevel@tonic-gate 	}
15350209230bSgjelinek 
15367c478bd9Sstevel@tonic-gate 	return (Z_OK);
15377c478bd9Sstevel@tonic-gate }
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate static void
15407c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
15417c478bd9Sstevel@tonic-gate {
15427c478bd9Sstevel@tonic-gate 	ssize_t result;
1543555afedfScarlsonj 	uuid_t uuid;
1544555afedfScarlsonj 	FILE *fp;
1545555afedfScarlsonj 
1546555afedfScarlsonj 	(void) memset(zeptr, 0, sizeof (*zeptr));
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	zeptr->zid = zid;
1549555afedfScarlsonj 
15507c478bd9Sstevel@tonic-gate 	/*
15517c478bd9Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
15527c478bd9Sstevel@tonic-gate 	 * we can be assured that it will succeed.
15537c478bd9Sstevel@tonic-gate 	 */
15547c478bd9Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
15557c478bd9Sstevel@tonic-gate 	assert(result >= 0);
1556555afedfScarlsonj 	if (zonecfg_is_scratch(zeptr->zname) &&
1557555afedfScarlsonj 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
1558555afedfScarlsonj 		(void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
1559555afedfScarlsonj 		    sizeof (zeptr->zname), NULL, 0);
1560555afedfScarlsonj 		zonecfg_close_scratch(fp);
1561555afedfScarlsonj 	}
1562555afedfScarlsonj 
1563555afedfScarlsonj 	if (is_system_labeled()) {
156445916cd2Sjpk 		(void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
156545916cd2Sjpk 		    sizeof (zeptr->zroot));
15669acbbeafSnn35248 		(void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
15679acbbeafSnn35248 			    sizeof (zeptr->zbrand));
1568555afedfScarlsonj 	} else {
1569555afedfScarlsonj 		(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
15709acbbeafSnn35248 		(void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
15719acbbeafSnn35248 		    sizeof (zeptr->zbrand));
157245916cd2Sjpk 	}
1573555afedfScarlsonj 
15747c478bd9Sstevel@tonic-gate 	zeptr->zstate_str = "running";
1575555afedfScarlsonj 	if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
1576555afedfScarlsonj 	    !uuid_is_null(uuid))
1577555afedfScarlsonj 		uuid_unparse(uuid, zeptr->zuuid);
15787c478bd9Sstevel@tonic-gate }
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate static int
15817c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[])
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1584108322fbScarlsonj 	int arg, retv;
15857c478bd9Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
15867c478bd9Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
15877c478bd9Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	if (target_zone == NULL) {
15907c478bd9Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
15917c478bd9Sstevel@tonic-gate 		optind = 0;
15927c478bd9Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
15937c478bd9Sstevel@tonic-gate 			switch (arg) {
15947c478bd9Sstevel@tonic-gate 			case '?':
15957c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
15967c478bd9Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
1597475d78a4Sjonb 				/*
1598475d78a4Sjonb 				 * The 'i' and 'c' options are not mutually
1599475d78a4Sjonb 				 * exclusive so if 'c' is given, then min_state
1600475d78a4Sjonb 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1601475d78a4Sjonb 				 * the lowest possible state.  If 'i' is given,
1602475d78a4Sjonb 				 * then min_state is set to be the lowest state
1603475d78a4Sjonb 				 * so far.
1604475d78a4Sjonb 				 */
16057c478bd9Sstevel@tonic-gate 			case 'c':
16067c478bd9Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
16077c478bd9Sstevel@tonic-gate 				break;
16087c478bd9Sstevel@tonic-gate 			case 'i':
1609475d78a4Sjonb 				min_state = min(ZONE_STATE_INSTALLED,
1610475d78a4Sjonb 				    min_state);
1611475d78a4Sjonb 
16127c478bd9Sstevel@tonic-gate 				break;
16137c478bd9Sstevel@tonic-gate 			case 'p':
16147c478bd9Sstevel@tonic-gate 				parsable = B_TRUE;
16157c478bd9Sstevel@tonic-gate 				break;
16167c478bd9Sstevel@tonic-gate 			case 'v':
16177c478bd9Sstevel@tonic-gate 				verbose = B_TRUE;
16187c478bd9Sstevel@tonic-gate 				break;
16197c478bd9Sstevel@tonic-gate 			default:
16207c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
16217c478bd9Sstevel@tonic-gate 				return (Z_USAGE);
16227c478bd9Sstevel@tonic-gate 			}
16237c478bd9Sstevel@tonic-gate 		}
16247c478bd9Sstevel@tonic-gate 		if (parsable && verbose) {
16257c478bd9Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
16267c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
16277c478bd9Sstevel@tonic-gate 			return (Z_ERR);
16287c478bd9Sstevel@tonic-gate 		}
162945916cd2Sjpk 		if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1630108322fbScarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
16317c478bd9Sstevel@tonic-gate 		} else {
16327c478bd9Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
16339acbbeafSnn35248 			retv = Z_OK;
16347c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
16357c478bd9Sstevel@tonic-gate 		}
1636108322fbScarlsonj 		return (retv);
16377c478bd9Sstevel@tonic-gate 	}
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	/*
16407c478bd9Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
16417c478bd9Sstevel@tonic-gate 	 */
16427c478bd9Sstevel@tonic-gate 	optind = 0;
16437c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
16447c478bd9Sstevel@tonic-gate 		switch (arg) {
16457c478bd9Sstevel@tonic-gate 		case '?':
16467c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
16477c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
16487c478bd9Sstevel@tonic-gate 		case 'p':
16497c478bd9Sstevel@tonic-gate 			parsable = B_TRUE;
16507c478bd9Sstevel@tonic-gate 			break;
16517c478bd9Sstevel@tonic-gate 		case 'v':
16527c478bd9Sstevel@tonic-gate 			verbose = B_TRUE;
16537c478bd9Sstevel@tonic-gate 			break;
16547c478bd9Sstevel@tonic-gate 		default:
16557c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
16567c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
16577c478bd9Sstevel@tonic-gate 		}
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 	if (parsable && verbose) {
16607c478bd9Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
16617c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
16627c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16637c478bd9Sstevel@tonic-gate 	}
16647c478bd9Sstevel@tonic-gate 	if (argc > optind) {
16657c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
16667c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 	if (zone_id != GLOBAL_ZONEID) {
16697c478bd9Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
16707c478bd9Sstevel@tonic-gate 		/*
16717c478bd9Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
16727c478bd9Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
16737c478bd9Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
16747c478bd9Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
16757c478bd9Sstevel@tonic-gate 		 */
16767c478bd9Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
16777c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
16787c478bd9Sstevel@tonic-gate 		output = B_TRUE;
16797c478bd9Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
16807c478bd9Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
16817c478bd9Sstevel@tonic-gate 		output = B_TRUE;
1682108322fbScarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1683108322fbScarlsonj 	    &zent) == Z_OK) {
16847c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
16857c478bd9Sstevel@tonic-gate 		output = B_TRUE;
16867c478bd9Sstevel@tonic-gate 	}
1687*ce28b40eSzt129084 
1688*ce28b40eSzt129084 	/*
1689*ce28b40eSzt129084 	 * Invoke brand-specific handler. Note that we do this
1690*ce28b40eSzt129084 	 * only if we're in the global zone, and target_zone is specified.
1691*ce28b40eSzt129084 	 */
1692*ce28b40eSzt129084 	if (zone_id == GLOBAL_ZONEID && target_zone != NULL)
1693*ce28b40eSzt129084 		if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
1694*ce28b40eSzt129084 			return (Z_ERR);
1695*ce28b40eSzt129084 
16967c478bd9Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
16977c478bd9Sstevel@tonic-gate }
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate static void
17007c478bd9Sstevel@tonic-gate sigterm(int sig)
17017c478bd9Sstevel@tonic-gate {
17027c478bd9Sstevel@tonic-gate 	/*
17037c478bd9Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
17047c478bd9Sstevel@tonic-gate 	 * then propagate the signal to our process group.
17057c478bd9Sstevel@tonic-gate 	 */
17069acbbeafSnn35248 	assert(sig == SIGINT || sig == SIGTERM);
17077c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
17087c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
17097c478bd9Sstevel@tonic-gate 	(void) kill(0, sig);
17107c478bd9Sstevel@tonic-gate 	child_killed = B_TRUE;
17117c478bd9Sstevel@tonic-gate }
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate static int
17147c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf)
17157c478bd9Sstevel@tonic-gate {
17167c478bd9Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
17177c478bd9Sstevel@tonic-gate 	FILE *file;
17187c478bd9Sstevel@tonic-gate 
17199acbbeafSnn35248 	do_subproc_cnt++;
17207c478bd9Sstevel@tonic-gate 	child_killed = B_FALSE;
17217c478bd9Sstevel@tonic-gate 	/*
17227c478bd9Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
17237c478bd9Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
17247c478bd9Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
17257c478bd9Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
17267c478bd9Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
17277c478bd9Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
17287c478bd9Sstevel@tonic-gate 	 */
17297c478bd9Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
17309acbbeafSnn35248 	(void) openat(STDIN_FILENO, "/dev/null", O_RDONLY);
17319acbbeafSnn35248 	if (!zoneadm_is_nested)
17327c478bd9Sstevel@tonic-gate 		(void) setpgid(0, 0);
17337c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
17347c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
17357c478bd9Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
17367c478bd9Sstevel@tonic-gate 	for (;;) {
17377c478bd9Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
17387c478bd9Sstevel@tonic-gate 			break;
17397c478bd9Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
17427c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
17437c478bd9Sstevel@tonic-gate 	return (pclose(file));
17447c478bd9Sstevel@tonic-gate }
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate static int
17479acbbeafSnn35248 do_subproc_interactive(char *cmdbuf)
17489acbbeafSnn35248 {
17499acbbeafSnn35248 	void (*saveint)(int);
17509acbbeafSnn35248 	void (*saveterm)(int);
17519acbbeafSnn35248 	void (*savequit)(int);
17529acbbeafSnn35248 	void (*savehup)(int);
17539acbbeafSnn35248 	int pid, child, status;
17549acbbeafSnn35248 
17559acbbeafSnn35248 	/*
17569acbbeafSnn35248 	 * do_subproc() links stdin to /dev/null, which would break any
17579acbbeafSnn35248 	 * interactive subprocess we try to launch here.  Similarly, we
17589acbbeafSnn35248 	 * can't have been launched as a subprocess ourselves.
17599acbbeafSnn35248 	 */
17609acbbeafSnn35248 	assert(do_subproc_cnt == 0 && !zoneadm_is_nested);
17619acbbeafSnn35248 
17629acbbeafSnn35248 	if ((child = vfork()) == 0) {
17639acbbeafSnn35248 		(void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
17649acbbeafSnn35248 	}
17659acbbeafSnn35248 
17669acbbeafSnn35248 	if (child == -1)
17679acbbeafSnn35248 		return (-1);
17689acbbeafSnn35248 
17699acbbeafSnn35248 	saveint = sigset(SIGINT, SIG_IGN);
17709acbbeafSnn35248 	saveterm = sigset(SIGTERM, SIG_IGN);
17719acbbeafSnn35248 	savequit = sigset(SIGQUIT, SIG_IGN);
17729acbbeafSnn35248 	savehup = sigset(SIGHUP, SIG_IGN);
17739acbbeafSnn35248 
17749acbbeafSnn35248 	while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
17759acbbeafSnn35248 		;
17769acbbeafSnn35248 
17779acbbeafSnn35248 	(void) sigset(SIGINT, saveint);
17789acbbeafSnn35248 	(void) sigset(SIGTERM, saveterm);
17799acbbeafSnn35248 	(void) sigset(SIGQUIT, savequit);
17809acbbeafSnn35248 	(void) sigset(SIGHUP, savehup);
17819acbbeafSnn35248 
17829acbbeafSnn35248 	return (pid == -1 ? -1 : status);
17839acbbeafSnn35248 }
17849acbbeafSnn35248 
17859acbbeafSnn35248 static int
17869acbbeafSnn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
17877c478bd9Sstevel@tonic-gate {
17887c478bd9Sstevel@tonic-gate 	if (WIFEXITED(status)) {
17897c478bd9Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
17907c478bd9Sstevel@tonic-gate 
17919acbbeafSnn35248 		if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
17927c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' failed with exit code %d."), cmd,
17937c478bd9Sstevel@tonic-gate 			    exit_code);
17949acbbeafSnn35248 
17959acbbeafSnn35248 		return (exit_code);
17967c478bd9Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
17977c478bd9Sstevel@tonic-gate 		int signal = WTERMSIG(status);
17987c478bd9Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
18017c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
18027c478bd9Sstevel@tonic-gate 			    sigstr);
18037c478bd9Sstevel@tonic-gate 		} else {
18047c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
18057c478bd9Sstevel@tonic-gate 			    cmd);
18067c478bd9Sstevel@tonic-gate 		}
18077c478bd9Sstevel@tonic-gate 	} else {
18087c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
18097c478bd9Sstevel@tonic-gate 	}
18109acbbeafSnn35248 
18119acbbeafSnn35248 	/*
18129acbbeafSnn35248 	 * Assume a subprocess that died due to a signal or an unknown error
18139acbbeafSnn35248 	 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
18149acbbeafSnn35248 	 * user will likely need to do some manual cleanup.
18159acbbeafSnn35248 	 */
18169acbbeafSnn35248 	return (ZONE_SUBPROC_FATAL);
18177c478bd9Sstevel@tonic-gate }
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate /*
18207c478bd9Sstevel@tonic-gate  * Various sanity checks; make sure:
18217c478bd9Sstevel@tonic-gate  * 1. We're in the global zone.
18227c478bd9Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
18237c478bd9Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
18247c478bd9Sstevel@tonic-gate  *    "SUNW".
18257c478bd9Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
18267c478bd9Sstevel@tonic-gate  *     zone, the name service knows about it.
18277c478bd9Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
18287c478bd9Sstevel@tonic-gate  *     not already running (or ready).
18297c478bd9Sstevel@tonic-gate  */
18307c478bd9Sstevel@tonic-gate static int
18317c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
18329acbbeafSnn35248     boolean_t unsafe_when_running, boolean_t force)
18337c478bd9Sstevel@tonic-gate {
18347c478bd9Sstevel@tonic-gate 	zone_entry_t *zent;
18357c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
18369acbbeafSnn35248 	zone_state_t state, min_state;
1837108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
1838108322fbScarlsonj 	FILE *fp;
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
1841ffbafc53Scomay 		switch (cmd_num) {
1842ffbafc53Scomay 		case CMD_HALT:
1843ffbafc53Scomay 			zerror(gettext("use %s to %s this zone."), "halt(1M)",
18447c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num));
1845ffbafc53Scomay 			break;
1846ffbafc53Scomay 		case CMD_REBOOT:
1847ffbafc53Scomay 			zerror(gettext("use %s to %s this zone."),
1848ffbafc53Scomay 			    "reboot(1M)", cmd_to_str(cmd_num));
1849ffbafc53Scomay 			break;
1850ffbafc53Scomay 		default:
1851ffbafc53Scomay 			zerror(gettext("must be in the global zone to %s a "
1852ffbafc53Scomay 			    "zone."), cmd_to_str(cmd_num));
1853ffbafc53Scomay 			break;
1854ffbafc53Scomay 		}
18557c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18567c478bd9Sstevel@tonic-gate 	}
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
18597c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
18607c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
18647c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
18657c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
18667c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
18707c478bd9Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
18717c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
18727c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
18737c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
18787c478bd9Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
18797c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
18837c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
18847c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
18857c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
18897c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
18907c478bd9Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
18917c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate 
18949acbbeafSnn35248 	if (!is_native_zone && cmd_num == CMD_MOUNT) {
18959acbbeafSnn35248 		zerror(gettext("%s operation is invalid for branded zones."),
18969acbbeafSnn35248 		    cmd_to_str(cmd_num));
18979acbbeafSnn35248 			return (Z_ERR);
18989acbbeafSnn35248 	}
18999acbbeafSnn35248 
1900108322fbScarlsonj 	if (!zonecfg_in_alt_root()) {
1901108322fbScarlsonj 		zent = lookup_running_zone(zone);
1902108322fbScarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1903108322fbScarlsonj 		zent = NULL;
1904108322fbScarlsonj 	} else {
1905108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1906108322fbScarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1907108322fbScarlsonj 			zent = lookup_running_zone(kernzone);
1908108322fbScarlsonj 		else
1909108322fbScarlsonj 			zent = NULL;
1910108322fbScarlsonj 		zonecfg_close_scratch(fp);
1911108322fbScarlsonj 	}
1912108322fbScarlsonj 
19137c478bd9Sstevel@tonic-gate 	/*
19147c478bd9Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
19157c478bd9Sstevel@tonic-gate 	 */
19169acbbeafSnn35248 	if (running && !force) {
19177c478bd9Sstevel@tonic-gate 		if (zent == NULL) {
19187c478bd9Sstevel@tonic-gate 			zerror(gettext("not running"));
19197c478bd9Sstevel@tonic-gate 			return (Z_ERR);
19207c478bd9Sstevel@tonic-gate 		}
19217c478bd9Sstevel@tonic-gate 	} else {
19227c478bd9Sstevel@tonic-gate 		int err;
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
19257c478bd9Sstevel@tonic-gate 			/* check whether the zone is ready or running */
19267c478bd9Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
19277c478bd9Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
19287c478bd9Sstevel@tonic-gate 				errno = err;
19297c478bd9Sstevel@tonic-gate 				zperror2(zent->zname,
19307c478bd9Sstevel@tonic-gate 				    gettext("could not get state"));
19317c478bd9Sstevel@tonic-gate 				/* can't tell, so hedge */
19327c478bd9Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
19337c478bd9Sstevel@tonic-gate 			} else {
19347c478bd9Sstevel@tonic-gate 				zent->zstate_str =
19357c478bd9Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
19367c478bd9Sstevel@tonic-gate 			}
19377c478bd9Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
19387c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
19397c478bd9Sstevel@tonic-gate 			return (Z_ERR);
19407c478bd9Sstevel@tonic-gate 		}
19417c478bd9Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
19427c478bd9Sstevel@tonic-gate 			errno = err;
19437c478bd9Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
19447c478bd9Sstevel@tonic-gate 			return (Z_ERR);
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 		switch (cmd_num) {
19477c478bd9Sstevel@tonic-gate 		case CMD_UNINSTALL:
19487c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
19497c478bd9Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
19507c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
19517c478bd9Sstevel@tonic-gate 				return (Z_ERR);
19527c478bd9Sstevel@tonic-gate 			}
19537c478bd9Sstevel@tonic-gate 			break;
1954ee519a1fSgjelinek 		case CMD_ATTACH:
1955865e09a4Sgjelinek 		case CMD_CLONE:
19567c478bd9Sstevel@tonic-gate 		case CMD_INSTALL:
19577c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
19587c478bd9Sstevel@tonic-gate 				zerror(gettext("is already %s."),
19597c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
19607c478bd9Sstevel@tonic-gate 				return (Z_ERR);
19617c478bd9Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
19627c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
19637c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
19647c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
19657c478bd9Sstevel@tonic-gate 				return (Z_ERR);
19667c478bd9Sstevel@tonic-gate 			}
19677c478bd9Sstevel@tonic-gate 			break;
1968ee519a1fSgjelinek 		case CMD_DETACH:
1969865e09a4Sgjelinek 		case CMD_MOVE:
19707c478bd9Sstevel@tonic-gate 		case CMD_READY:
19717c478bd9Sstevel@tonic-gate 		case CMD_BOOT:
1972108322fbScarlsonj 		case CMD_MOUNT:
1973555afedfScarlsonj 		case CMD_MARK:
19749acbbeafSnn35248 			if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
19759acbbeafSnn35248 			    force)
19769acbbeafSnn35248 				min_state = ZONE_STATE_INCOMPLETE;
19779acbbeafSnn35248 			else
19789acbbeafSnn35248 				min_state = ZONE_STATE_INSTALLED;
19799acbbeafSnn35248 
19809acbbeafSnn35248 			if (force && cmd_num == CMD_BOOT && is_native_zone) {
19819acbbeafSnn35248 				zerror(gettext("Only branded zones may be "
19829acbbeafSnn35248 				    "force-booted."));
19839acbbeafSnn35248 				return (Z_ERR);
19849acbbeafSnn35248 			}
19859acbbeafSnn35248 
19869acbbeafSnn35248 			if (state < min_state) {
19877c478bd9Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
19889acbbeafSnn35248 				    zone_state_str(min_state),
19897c478bd9Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
19907c478bd9Sstevel@tonic-gate 				return (Z_ERR);
19917c478bd9Sstevel@tonic-gate 			}
19927c478bd9Sstevel@tonic-gate 			break;
19937c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
19947c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
19957c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
19967c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
19977c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
19987c478bd9Sstevel@tonic-gate 				return (Z_ERR);
19997c478bd9Sstevel@tonic-gate 			}
20007c478bd9Sstevel@tonic-gate 			break;
2001108322fbScarlsonj 		case CMD_UNMOUNT:
2002108322fbScarlsonj 			if (state != ZONE_STATE_MOUNTED) {
2003108322fbScarlsonj 				zerror(gettext("must be %s before %s."),
2004108322fbScarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
2005108322fbScarlsonj 				    cmd_to_str(cmd_num));
2006108322fbScarlsonj 				return (Z_ERR);
2007108322fbScarlsonj 			}
2008108322fbScarlsonj 			break;
20097c478bd9Sstevel@tonic-gate 		}
20107c478bd9Sstevel@tonic-gate 	}
20117c478bd9Sstevel@tonic-gate 	return (Z_OK);
20127c478bd9Sstevel@tonic-gate }
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate static int
20157c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[])
20167c478bd9Sstevel@tonic-gate {
20177c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
20187c478bd9Sstevel@tonic-gate 	int arg;
20197c478bd9Sstevel@tonic-gate 
2020108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2021108322fbScarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
2022108322fbScarlsonj 		return (Z_ERR);
2023108322fbScarlsonj 	}
2024108322fbScarlsonj 
20257c478bd9Sstevel@tonic-gate 	optind = 0;
20267c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
20277c478bd9Sstevel@tonic-gate 		switch (arg) {
20287c478bd9Sstevel@tonic-gate 		case '?':
20297c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
20307c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
20317c478bd9Sstevel@tonic-gate 		default:
20327c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
20337c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
20347c478bd9Sstevel@tonic-gate 		}
20357c478bd9Sstevel@tonic-gate 	}
20367c478bd9Sstevel@tonic-gate 	if (argc > optind) {
20377c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
20387c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
20397c478bd9Sstevel@tonic-gate 	}
20407c478bd9Sstevel@tonic-gate 	/*
20417c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
20427c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
20437c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
20447c478bd9Sstevel@tonic-gate 	 */
20459acbbeafSnn35248 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
20469acbbeafSnn35248 	    != Z_OK)
20477c478bd9Sstevel@tonic-gate 		return (Z_ERR);
20487c478bd9Sstevel@tonic-gate 
2049*ce28b40eSzt129084 	/*
2050*ce28b40eSzt129084 	 * Invoke brand-specific handler.
2051*ce28b40eSzt129084 	 */
2052*ce28b40eSzt129084 	if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
2053*ce28b40eSzt129084 		return (Z_ERR);
2054*ce28b40eSzt129084 
20557c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
20567c478bd9Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
20577c478bd9Sstevel@tonic-gate }
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate static int
20607c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[])
20617c478bd9Sstevel@tonic-gate {
20627c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
20637c478bd9Sstevel@tonic-gate 	int arg;
20647c478bd9Sstevel@tonic-gate 
2065108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2066108322fbScarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
2067108322fbScarlsonj 		return (Z_ERR);
2068108322fbScarlsonj 	}
2069108322fbScarlsonj 
20707c478bd9Sstevel@tonic-gate 	optind = 0;
20717c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
20727c478bd9Sstevel@tonic-gate 		switch (arg) {
20737c478bd9Sstevel@tonic-gate 		case '?':
20747c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
20757c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
20767c478bd9Sstevel@tonic-gate 		default:
20777c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
20787c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
20797c478bd9Sstevel@tonic-gate 		}
20807c478bd9Sstevel@tonic-gate 	}
20813f2f09c1Sdp 
20823f2f09c1Sdp 	zarg.bootbuf[0] = '\0';
20833f2f09c1Sdp 	for (; optind < argc; optind++) {
20843f2f09c1Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
20853f2f09c1Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
20863f2f09c1Sdp 			zerror(gettext("Boot argument list too long"));
20873f2f09c1Sdp 			return (Z_ERR);
20887c478bd9Sstevel@tonic-gate 		}
20893f2f09c1Sdp 		if (optind < argc - 1)
20903f2f09c1Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
20913f2f09c1Sdp 			    sizeof (zarg.bootbuf)) {
20923f2f09c1Sdp 				zerror(gettext("Boot argument list too long"));
20933f2f09c1Sdp 				return (Z_ERR);
20943f2f09c1Sdp 			}
20953f2f09c1Sdp 	}
20963f2f09c1Sdp 
20973f2f09c1Sdp 
20987c478bd9Sstevel@tonic-gate 	/*
20997c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
21007c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
21017c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
21027c478bd9Sstevel@tonic-gate 	 */
21039acbbeafSnn35248 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
21049acbbeafSnn35248 	    != Z_OK)
21057c478bd9Sstevel@tonic-gate 		return (Z_ERR);
2106*ce28b40eSzt129084 	if (verify_details(CMD_REBOOT, argv) != Z_OK)
2107b5abaf04Sgjelinek 		return (Z_ERR);
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
21107c478bd9Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
21117c478bd9Sstevel@tonic-gate }
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate static int
2114*ce28b40eSzt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
21159acbbeafSnn35248 {
21169acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
21179acbbeafSnn35248 	int err;
21189acbbeafSnn35248 	char zonepath[MAXPATHLEN];
2119123807fbSedp 	brand_handle_t bh = NULL;
2120*ce28b40eSzt129084 	int status, i;
21219acbbeafSnn35248 
21229acbbeafSnn35248 	/*
21239acbbeafSnn35248 	 * Fetch the verify command from the brand configuration.
21249acbbeafSnn35248 	 * "exec" the command so that the returned status is that of
21259acbbeafSnn35248 	 * the command and not the shell.
21269acbbeafSnn35248 	 */
21279acbbeafSnn35248 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
21289acbbeafSnn35248 	    Z_OK) {
21299acbbeafSnn35248 		errno = err;
2130*ce28b40eSzt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
21319acbbeafSnn35248 		return (Z_ERR);
21329acbbeafSnn35248 	}
2133123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
21349acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
21359acbbeafSnn35248 		return (Z_ERR);
21369acbbeafSnn35248 	}
21379acbbeafSnn35248 
21389acbbeafSnn35248 	/*
21399acbbeafSnn35248 	 * If the brand has its own verification routine, execute it now.
2140*ce28b40eSzt129084 	 * The verification routine validates the intended zoneadm
2141*ce28b40eSzt129084 	 * operation for the specific brand. The zoneadm subcommand and
2142*ce28b40eSzt129084 	 * all its arguments are passed to the routine.
21439acbbeafSnn35248 	 */
21449acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
2145123807fbSedp 	err = brand_get_verify_adm(bh, target_zone, zonepath,
21469acbbeafSnn35248 	    cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL);
2147123807fbSedp 	brand_close(bh);
2148*ce28b40eSzt129084 	if (err != 0)
2149*ce28b40eSzt129084 		return (Z_BRAND_ERROR);
2150*ce28b40eSzt129084 	if (strlen(cmdbuf) <= EXEC_LEN)
2151*ce28b40eSzt129084 		return (Z_OK);
2152*ce28b40eSzt129084 
2153*ce28b40eSzt129084 	if (strlcat(cmdbuf, cmd_to_str(cmd_num),
2154*ce28b40eSzt129084 	    sizeof (cmdbuf)) >= sizeof (cmdbuf))
2155*ce28b40eSzt129084 		return (Z_ERR);
2156*ce28b40eSzt129084 
2157*ce28b40eSzt129084 	/* Build the argv string */
2158*ce28b40eSzt129084 	i = 0;
2159*ce28b40eSzt129084 	while (argv[i] != NULL) {
2160*ce28b40eSzt129084 		if ((strlcat(cmdbuf, " ",
2161*ce28b40eSzt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
2162*ce28b40eSzt129084 		    (strlcat(cmdbuf, argv[i++],
2163*ce28b40eSzt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)))
2164*ce28b40eSzt129084 			return (Z_ERR);
2165*ce28b40eSzt129084 	}
2166*ce28b40eSzt129084 
21679acbbeafSnn35248 	status = do_subproc_interactive(cmdbuf);
21689acbbeafSnn35248 	err = subproc_status(gettext("brand-specific verification"),
21699acbbeafSnn35248 	    status, B_FALSE);
21709acbbeafSnn35248 
2171*ce28b40eSzt129084 	return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
21729acbbeafSnn35248 }
21739acbbeafSnn35248 
21749acbbeafSnn35248 static int
21757c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
21767c478bd9Sstevel@tonic-gate {
21777c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
21787c478bd9Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
21797c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk;
21807c478bd9Sstevel@tonic-gate 	int error = Z_INVAL;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
21837c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
21847c478bd9Sstevel@tonic-gate 		    strerror(errno));
21857c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
21897c478bd9Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
21907c478bd9Sstevel@tonic-gate 		free(rctlblk);
21917c478bd9Sstevel@tonic-gate 		return (error);
21927c478bd9Sstevel@tonic-gate 	}
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
21957c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
21967c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
21977c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
22007c478bd9Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
22017c478bd9Sstevel@tonic-gate 			    "'%s'."),  name);
22027c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
22037c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
22047c478bd9Sstevel@tonic-gate 			continue;
22057c478bd9Sstevel@tonic-gate 		}
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
22087c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
22097c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
22107c478bd9Sstevel@tonic-gate 			    != Z_OK) {
22117c478bd9Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
22127c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
22137c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
22147c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
22157c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
22167c478bd9Sstevel@tonic-gate 				goto out;
22177c478bd9Sstevel@tonic-gate 			}
22187c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
22197c478bd9Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
22207c478bd9Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
22217c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
22227c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
22237c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
22247c478bd9Sstevel@tonic-gate 				    name);
22257c478bd9Sstevel@tonic-gate 				goto out;
22267c478bd9Sstevel@tonic-gate 			}
22277c478bd9Sstevel@tonic-gate 		}
22287c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
22297c478bd9Sstevel@tonic-gate 	}
22307c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
22317c478bd9Sstevel@tonic-gate 	error = Z_OK;
22327c478bd9Sstevel@tonic-gate out:
22337c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
22347c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
22357c478bd9Sstevel@tonic-gate 	free(rctlblk);
22367c478bd9Sstevel@tonic-gate 	return (error);
22377c478bd9Sstevel@tonic-gate }
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate static int
22407c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
22417c478bd9Sstevel@tonic-gate {
22427c478bd9Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
22437c478bd9Sstevel@tonic-gate 	pool_conf_t *poolconf;
22447c478bd9Sstevel@tonic-gate 	pool_t *pool;
22457c478bd9Sstevel@tonic-gate 	int status;
22467c478bd9Sstevel@tonic-gate 	int error;
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	/*
22497c478bd9Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
22507c478bd9Sstevel@tonic-gate 	 */
22517c478bd9Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
22527c478bd9Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
22537c478bd9Sstevel@tonic-gate 		/*
22547c478bd9Sstevel@tonic-gate 		 * No pool specified.
22557c478bd9Sstevel@tonic-gate 		 */
22567c478bd9Sstevel@tonic-gate 		return (0);
22577c478bd9Sstevel@tonic-gate 	}
22587c478bd9Sstevel@tonic-gate 	if (error != Z_OK) {
22597c478bd9Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
22607c478bd9Sstevel@tonic-gate 		    "configuration"), B_TRUE);
22617c478bd9Sstevel@tonic-gate 		return (error);
22627c478bd9Sstevel@tonic-gate 	}
22637c478bd9Sstevel@tonic-gate 	/*
22647c478bd9Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
22657c478bd9Sstevel@tonic-gate 	 */
22667c478bd9Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
22677c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
22687c478bd9Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
22697c478bd9Sstevel@tonic-gate 		return (Z_OK);
22707c478bd9Sstevel@tonic-gate 	}
22717c478bd9Sstevel@tonic-gate 	/*
22727c478bd9Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
22737c478bd9Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
22747c478bd9Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
22757c478bd9Sstevel@tonic-gate 	 * hence they are not considered errors.
22767c478bd9Sstevel@tonic-gate 	 */
22777c478bd9Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
22787c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
22797c478bd9Sstevel@tonic-gate 		    "using default pool"));
22807c478bd9Sstevel@tonic-gate 		return (Z_OK);
22817c478bd9Sstevel@tonic-gate 	}
22827c478bd9Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
22837c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
22847c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
22857c478bd9Sstevel@tonic-gate 		    "using default pool"));
22867c478bd9Sstevel@tonic-gate 		pool_conf_free(poolconf);
22877c478bd9Sstevel@tonic-gate 		return (Z_OK);
22887c478bd9Sstevel@tonic-gate 	}
22897c478bd9Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
22907c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
22917c478bd9Sstevel@tonic-gate 	pool_conf_free(poolconf);
22927c478bd9Sstevel@tonic-gate 	if (pool == NULL) {
22937c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
22947c478bd9Sstevel@tonic-gate 		    "using default pool"), poolname);
22957c478bd9Sstevel@tonic-gate 	}
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	return (Z_OK);
22987c478bd9Sstevel@tonic-gate }
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate static int
2301b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle)
2302b5abaf04Sgjelinek {
2303b5abaf04Sgjelinek 	int return_code = Z_OK;
2304b5abaf04Sgjelinek 	struct zone_fstab fstab;
2305b5abaf04Sgjelinek 	struct stat st;
2306b5abaf04Sgjelinek 	char specdir[MAXPATHLEN];
2307b5abaf04Sgjelinek 
2308b5abaf04Sgjelinek 	if (zonecfg_setipdent(handle) != Z_OK) {
23095ee84fbdSgjelinek 		/*
23105ee84fbdSgjelinek 		 * TRANSLATION_NOTE
23115ee84fbdSgjelinek 		 * inherit-pkg-dirs is a literal that should not be translated.
23125ee84fbdSgjelinek 		 */
23135ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("could not verify "
2314b5abaf04Sgjelinek 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
2315b5abaf04Sgjelinek 		return (Z_ERR);
2316b5abaf04Sgjelinek 	}
2317b5abaf04Sgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
2318b5abaf04Sgjelinek 		/*
2319b5abaf04Sgjelinek 		 * Verify fs_dir exists.
2320b5abaf04Sgjelinek 		 */
2321b5abaf04Sgjelinek 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
2322b5abaf04Sgjelinek 		    zonecfg_get_root(), fstab.zone_fs_dir);
2323b5abaf04Sgjelinek 		if (stat(specdir, &st) != 0) {
23245ee84fbdSgjelinek 			/*
23255ee84fbdSgjelinek 			 * TRANSLATION_NOTE
23265ee84fbdSgjelinek 			 * inherit-pkg-dir is a literal that should not be
23275ee84fbdSgjelinek 			 * translated.
23285ee84fbdSgjelinek 			 */
23295ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2330b5abaf04Sgjelinek 			    "inherit-pkg-dir %s: %s\n"),
2331b5abaf04Sgjelinek 			    fstab.zone_fs_dir, strerror(errno));
2332b5abaf04Sgjelinek 			return_code = Z_ERR;
2333b5abaf04Sgjelinek 		}
2334b5abaf04Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
23355ee84fbdSgjelinek 			/*
23365ee84fbdSgjelinek 			 * TRANSLATION_NOTE
23375ee84fbdSgjelinek 			 * inherit-pkg-dir and NFS are literals that should
23385ee84fbdSgjelinek 			 * not be translated.
23395ee84fbdSgjelinek 			 */
2340b5abaf04Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
23410b5de56dSgjelinek 			    "inherit-pkg-dir %s: NFS mounted file system.\n"
23420b5de56dSgjelinek 			    "\tA local file system must be used.\n"),
2343b5abaf04Sgjelinek 			    fstab.zone_fs_dir);
2344b5abaf04Sgjelinek 			return_code = Z_ERR;
2345b5abaf04Sgjelinek 		}
2346b5abaf04Sgjelinek 	}
2347b5abaf04Sgjelinek 	(void) zonecfg_endipdent(handle);
2348b5abaf04Sgjelinek 
2349b5abaf04Sgjelinek 	return (return_code);
2350b5abaf04Sgjelinek }
2351b5abaf04Sgjelinek 
235220c8013fSlling /*
235320c8013fSlling  * Verify that the special device/file system exists and is valid.
235420c8013fSlling  */
235520c8013fSlling static int
235620c8013fSlling verify_fs_special(struct zone_fstab *fstab)
235720c8013fSlling {
235820c8013fSlling 	struct stat st;
235920c8013fSlling 
23606cc813faSgjelinek 	/*
23616cc813faSgjelinek 	 * This validation is really intended for standard zone administration.
23626cc813faSgjelinek 	 * If we are in a mini-root or some other upgrade situation where
23636cc813faSgjelinek 	 * we are using the scratch zone, just by-pass this.
23646cc813faSgjelinek 	 */
23656cc813faSgjelinek 	if (zonecfg_in_alt_root())
23666cc813faSgjelinek 		return (Z_OK);
23676cc813faSgjelinek 
236820c8013fSlling 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
236920c8013fSlling 		return (verify_fs_zfs(fstab));
237020c8013fSlling 
237120c8013fSlling 	if (stat(fstab->zone_fs_special, &st) != 0) {
23725c358068Slling 		(void) fprintf(stderr, gettext("could not verify fs "
237320c8013fSlling 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
237420c8013fSlling 		    fstab->zone_fs_special, strerror(errno));
237520c8013fSlling 		return (Z_ERR);
237620c8013fSlling 	}
237720c8013fSlling 
237820c8013fSlling 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
237920c8013fSlling 		/*
238020c8013fSlling 		 * TRANSLATION_NOTE
238120c8013fSlling 		 * fs and NFS are literals that should
238220c8013fSlling 		 * not be translated.
238320c8013fSlling 		 */
238420c8013fSlling 		(void) fprintf(stderr, gettext("cannot verify "
23850b5de56dSgjelinek 		    "fs %s: NFS mounted file system.\n"
23860b5de56dSgjelinek 		    "\tA local file system must be used.\n"),
238720c8013fSlling 		    fstab->zone_fs_special);
238820c8013fSlling 		return (Z_ERR);
238920c8013fSlling 	}
239020c8013fSlling 
239120c8013fSlling 	return (Z_OK);
239220c8013fSlling }
239320c8013fSlling 
2394b5abaf04Sgjelinek static int
23957c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
23967c478bd9Sstevel@tonic-gate {
23977c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
23987c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
23997c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
24007c478bd9Sstevel@tonic-gate 	struct stat st;
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	/*
24037c478bd9Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
24047c478bd9Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
24057c478bd9Sstevel@tonic-gate 	 * verified for regular file systems below.
24067c478bd9Sstevel@tonic-gate 	 *
24077c478bd9Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
24087c478bd9Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
24097c478bd9Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
24107c478bd9Sstevel@tonic-gate 	 */
24117c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
24120b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify file systems: "
24137c478bd9Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
24147c478bd9Sstevel@tonic-gate 		return (Z_ERR);
24157c478bd9Sstevel@tonic-gate 	}
24167c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
24177c478bd9Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
24187c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
24197c478bd9Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
24207c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
24217c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24227c478bd9Sstevel@tonic-gate 			goto next_fs;
24237c478bd9Sstevel@tonic-gate 		}
24247c478bd9Sstevel@tonic-gate 		/*
24257c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
24267c478bd9Sstevel@tonic-gate 		 */
24277c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
24287c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
24297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
24307c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
24317c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
24327c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24337c478bd9Sstevel@tonic-gate 			goto next_fs;
24347c478bd9Sstevel@tonic-gate 		}
24357c478bd9Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
24365ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
24375ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
24387c478bd9Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
24397c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24407c478bd9Sstevel@tonic-gate 			goto next_fs;
24417c478bd9Sstevel@tonic-gate 		}
24427c478bd9Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
24435ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
24445ee84fbdSgjelinek 			    "%s: %s is not a regular file\n"),
24455ee84fbdSgjelinek 			    fstab.zone_fs_dir, cmdbuf);
24467c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24477c478bd9Sstevel@tonic-gate 			goto next_fs;
24487c478bd9Sstevel@tonic-gate 		}
24497c478bd9Sstevel@tonic-gate 		/*
24507c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
24517c478bd9Sstevel@tonic-gate 		 * set.
24527c478bd9Sstevel@tonic-gate 		 */
24537c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
24547c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
24557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
24567c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
24577c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
24587c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24597c478bd9Sstevel@tonic-gate 			goto next_fs;
24607c478bd9Sstevel@tonic-gate 		}
24617c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
24625ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
24635ee84fbdSgjelinek 			    "%s: must specify 'raw' device for %s "
24640b5de56dSgjelinek 			    "file systems\n"),
24657c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
24667c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24677c478bd9Sstevel@tonic-gate 			goto next_fs;
24687c478bd9Sstevel@tonic-gate 		}
24697c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
24707c478bd9Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
24717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
24727c478bd9Sstevel@tonic-gate 			    "'raw' device specified but "
24737c478bd9Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
24747c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
24757c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24767c478bd9Sstevel@tonic-gate 			goto next_fs;
24777c478bd9Sstevel@tonic-gate 		}
247820c8013fSlling 
247920c8013fSlling 		/* Verify fs_special. */
248020c8013fSlling 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2481b5abaf04Sgjelinek 			goto next_fs;
248220c8013fSlling 
248320c8013fSlling 		/* Verify fs_raw. */
2484b5abaf04Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2485b5abaf04Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
24865ee84fbdSgjelinek 			/*
24875ee84fbdSgjelinek 			 * TRANSLATION_NOTE
24885ee84fbdSgjelinek 			 * fs is a literal that should not be translated.
24895ee84fbdSgjelinek 			 */
24905ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
24915ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2492b5abaf04Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2493b5abaf04Sgjelinek 			return_code = Z_ERR;
2494b5abaf04Sgjelinek 			goto next_fs;
2495b5abaf04Sgjelinek 		}
24967c478bd9Sstevel@tonic-gate next_fs:
24977c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
24987c478bd9Sstevel@tonic-gate 	}
24997c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	return (return_code);
25027c478bd9Sstevel@tonic-gate }
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate static int
2505ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle)
2506ffbafc53Scomay {
2507ffbafc53Scomay 	char *privname = NULL;
2508ffbafc53Scomay 	int err;
2509ffbafc53Scomay 	priv_set_t *privs;
2510ffbafc53Scomay 
2511ffbafc53Scomay 	if ((privs = priv_allocset()) == NULL) {
2512ffbafc53Scomay 		zperror(gettext("failed to allocate privilege set"), B_FALSE);
2513ffbafc53Scomay 		return (Z_NOMEM);
2514ffbafc53Scomay 	}
2515ffbafc53Scomay 	err = zonecfg_get_privset(handle, privs, &privname);
2516ffbafc53Scomay 	switch (err) {
2517ffbafc53Scomay 	case Z_OK:
2518ffbafc53Scomay 		break;
2519ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2520ffbafc53Scomay 		(void) fprintf(stderr, gettext("privilege \"%s\" is not "
2521ffbafc53Scomay 		    "permitted within the zone's privilege set\n"), privname);
2522ffbafc53Scomay 		break;
2523ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2524ffbafc53Scomay 		(void) fprintf(stderr, gettext("required privilege \"%s\" is "
2525ffbafc53Scomay 		    "missing from the zone's privilege set\n"), privname);
2526ffbafc53Scomay 		break;
2527ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2528ffbafc53Scomay 		(void) fprintf(stderr, gettext("unknown privilege \"%s\" "
2529ffbafc53Scomay 		    "specified in the zone's privilege set\n"), privname);
2530ffbafc53Scomay 		break;
2531ffbafc53Scomay 	default:
2532ffbafc53Scomay 		zperror(
2533ffbafc53Scomay 		    gettext("failed to determine the zone's privilege set"),
2534ffbafc53Scomay 		    B_TRUE);
2535ffbafc53Scomay 		break;
2536ffbafc53Scomay 	}
2537ffbafc53Scomay 	free(privname);
2538ffbafc53Scomay 	priv_freeset(privs);
2539ffbafc53Scomay 	return (err);
2540ffbafc53Scomay }
2541ffbafc53Scomay 
25421390a385Sgjelinek static void
25431390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list)
25441390a385Sgjelinek {
25451390a385Sgjelinek 	int		i;
25461390a385Sgjelinek 
25471390a385Sgjelinek 	for (i = 0; i < if_cnt; i++) {
25481390a385Sgjelinek 		free(if_list[i]->name);
25491390a385Sgjelinek 		free(if_list[i]);
25501390a385Sgjelinek 	}
25511390a385Sgjelinek 	free(if_list);
25521390a385Sgjelinek }
25531390a385Sgjelinek 
25541390a385Sgjelinek /*
25551390a385Sgjelinek  * Get a list of the network interfaces, along with their address families,
25561390a385Sgjelinek  * that are plumbed in the global zone.  See if_tcp(7p) for a description
25571390a385Sgjelinek  * of the ioctls used here.
25581390a385Sgjelinek  */
25591390a385Sgjelinek static int
25601390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list)
25611390a385Sgjelinek {
25621390a385Sgjelinek 	int		s;
25631390a385Sgjelinek 	int		i;
25641390a385Sgjelinek 	int		res = Z_OK;
25651390a385Sgjelinek 	int		space_needed;
25661390a385Sgjelinek 	int		cnt = 0;
25671390a385Sgjelinek 	struct		lifnum if_num;
25681390a385Sgjelinek 	struct		lifconf if_conf;
25691390a385Sgjelinek 	struct		lifreq *if_reqp;
25701390a385Sgjelinek 	char		*if_buf;
25711390a385Sgjelinek 	struct net_if	**local_ifs = NULL;
25721390a385Sgjelinek 
25731390a385Sgjelinek 	*if_cnt = 0;
25741390a385Sgjelinek 	*if_list = NULL;
25751390a385Sgjelinek 
25761390a385Sgjelinek 	if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
25771390a385Sgjelinek 		return (Z_ERR);
25781390a385Sgjelinek 
25791390a385Sgjelinek 	/*
25801390a385Sgjelinek 	 * Come back here in the unlikely event that the number of interfaces
25811390a385Sgjelinek 	 * increases between the time we get the count and the time we do the
25821390a385Sgjelinek 	 * SIOCGLIFCONF ioctl.
25831390a385Sgjelinek 	 */
25841390a385Sgjelinek retry:
25851390a385Sgjelinek 	/* Get the number of interfaces. */
25861390a385Sgjelinek 	if_num.lifn_family = AF_UNSPEC;
25871390a385Sgjelinek 	if_num.lifn_flags = LIFC_NOXMIT;
25881390a385Sgjelinek 	if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
25891390a385Sgjelinek 		(void) close(s);
25901390a385Sgjelinek 		return (Z_ERR);
25911390a385Sgjelinek 	}
25921390a385Sgjelinek 
25931390a385Sgjelinek 	/* Get the interface configuration list. */
25941390a385Sgjelinek 	space_needed = if_num.lifn_count * sizeof (struct lifreq);
25951390a385Sgjelinek 	if ((if_buf = malloc(space_needed)) == NULL) {
25961390a385Sgjelinek 		(void) close(s);
25971390a385Sgjelinek 		return (Z_ERR);
25981390a385Sgjelinek 	}
25991390a385Sgjelinek 	if_conf.lifc_family = AF_UNSPEC;
26001390a385Sgjelinek 	if_conf.lifc_flags = LIFC_NOXMIT;
26011390a385Sgjelinek 	if_conf.lifc_len = space_needed;
26021390a385Sgjelinek 	if_conf.lifc_buf = if_buf;
26031390a385Sgjelinek 	if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
26041390a385Sgjelinek 		free(if_buf);
26051390a385Sgjelinek 		/*
26061390a385Sgjelinek 		 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
26071390a385Sgjelinek 		 * too small.  In this case go back and get the new if cnt.
26081390a385Sgjelinek 		 */
26091390a385Sgjelinek 		if (errno == EINVAL)
26101390a385Sgjelinek 			goto retry;
26111390a385Sgjelinek 
26121390a385Sgjelinek 		(void) close(s);
26131390a385Sgjelinek 		return (Z_ERR);
26141390a385Sgjelinek 	}
26151390a385Sgjelinek 	(void) close(s);
26161390a385Sgjelinek 
26171390a385Sgjelinek 	/* Get the name and address family for each interface. */
26181390a385Sgjelinek 	if_reqp = if_conf.lifc_req;
26191390a385Sgjelinek 	for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
26201390a385Sgjelinek 		struct net_if	**p;
26211390a385Sgjelinek 		struct lifreq	req;
26221390a385Sgjelinek 
26231390a385Sgjelinek 		if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
26241390a385Sgjelinek 			if_reqp++;
26251390a385Sgjelinek 			continue;
26261390a385Sgjelinek 		}
26271390a385Sgjelinek 
26281390a385Sgjelinek 		if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
26291390a385Sgjelinek 		    SOCK_DGRAM, 0)) == -1) {
26301390a385Sgjelinek 			res = Z_ERR;
26311390a385Sgjelinek 			break;
26321390a385Sgjelinek 		}
26331390a385Sgjelinek 
26341390a385Sgjelinek 		(void) strncpy(req.lifr_name, if_reqp->lifr_name,
26351390a385Sgjelinek 		    sizeof (req.lifr_name));
26361390a385Sgjelinek 		if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
26371390a385Sgjelinek 			(void) close(s);
26381390a385Sgjelinek 			if_reqp++;
26391390a385Sgjelinek 			continue;
26401390a385Sgjelinek 		}
26411390a385Sgjelinek 
26421390a385Sgjelinek 		if ((p = (struct net_if **)realloc(local_ifs,
26431390a385Sgjelinek 		    sizeof (struct net_if *) * (cnt + 1))) == NULL) {
26441390a385Sgjelinek 			res = Z_ERR;
26451390a385Sgjelinek 			break;
26461390a385Sgjelinek 		}
26471390a385Sgjelinek 		local_ifs = p;
26481390a385Sgjelinek 
26491390a385Sgjelinek 		if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
26501390a385Sgjelinek 			res = Z_ERR;
26511390a385Sgjelinek 			break;
26521390a385Sgjelinek 		}
26531390a385Sgjelinek 
26541390a385Sgjelinek 		if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
26551390a385Sgjelinek 		    == NULL) {
26561390a385Sgjelinek 			free(local_ifs[cnt]);
26571390a385Sgjelinek 			res = Z_ERR;
26581390a385Sgjelinek 			break;
26591390a385Sgjelinek 		}
26601390a385Sgjelinek 		local_ifs[cnt]->af = req.lifr_addr.ss_family;
26611390a385Sgjelinek 		cnt++;
26621390a385Sgjelinek 
26631390a385Sgjelinek 		(void) close(s);
26641390a385Sgjelinek 		if_reqp++;
26651390a385Sgjelinek 	}
26661390a385Sgjelinek 
26671390a385Sgjelinek 	free(if_buf);
26681390a385Sgjelinek 
26691390a385Sgjelinek 	if (res != Z_OK) {
26701390a385Sgjelinek 		free_local_netifs(cnt, local_ifs);
26711390a385Sgjelinek 	} else {
26721390a385Sgjelinek 		*if_cnt = cnt;
26731390a385Sgjelinek 		*if_list = local_ifs;
26741390a385Sgjelinek 	}
26751390a385Sgjelinek 
26761390a385Sgjelinek 	return (res);
26771390a385Sgjelinek }
26781390a385Sgjelinek 
26791390a385Sgjelinek static char *
26801390a385Sgjelinek af2str(int af)
26811390a385Sgjelinek {
26821390a385Sgjelinek 	switch (af) {
26831390a385Sgjelinek 	case AF_INET:
26841390a385Sgjelinek 		return ("IPv4");
26851390a385Sgjelinek 	case AF_INET6:
26861390a385Sgjelinek 		return ("IPv6");
26871390a385Sgjelinek 	default:
26881390a385Sgjelinek 		return ("Unknown");
26891390a385Sgjelinek 	}
26901390a385Sgjelinek }
26911390a385Sgjelinek 
26921390a385Sgjelinek /*
26931390a385Sgjelinek  * Cross check the network interface name and address family with the
26941390a385Sgjelinek  * interfaces that are set up in the global zone so that we can print the
26951390a385Sgjelinek  * appropriate error message.
26961390a385Sgjelinek  */
26971390a385Sgjelinek static void
26981390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg)
26991390a385Sgjelinek {
27001390a385Sgjelinek 	int		i;
27011390a385Sgjelinek 	int		local_if_cnt = 0;
27021390a385Sgjelinek 	struct net_if	**local_ifs = NULL;
27031390a385Sgjelinek 	boolean_t	found_if = B_FALSE;
27041390a385Sgjelinek 	boolean_t	found_af = B_FALSE;
27051390a385Sgjelinek 
27061390a385Sgjelinek 	if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
27071390a385Sgjelinek 		(void) fprintf(stderr,
27081390a385Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
27091390a385Sgjelinek 		    "net", "address", addr, "physical", phys, msg);
27101390a385Sgjelinek 		return;
27111390a385Sgjelinek 	}
27121390a385Sgjelinek 
27131390a385Sgjelinek 	for (i = 0; i < local_if_cnt; i++) {
27141390a385Sgjelinek 		if (strcmp(phys, local_ifs[i]->name) == 0) {
27151390a385Sgjelinek 			found_if = B_TRUE;
27161390a385Sgjelinek 			if (af == local_ifs[i]->af) {
27171390a385Sgjelinek 				found_af = B_TRUE;
27181390a385Sgjelinek 				break;
27191390a385Sgjelinek 			}
27201390a385Sgjelinek 		}
27211390a385Sgjelinek 	}
27221390a385Sgjelinek 
27231390a385Sgjelinek 	free_local_netifs(local_if_cnt, local_ifs);
27241390a385Sgjelinek 
27251390a385Sgjelinek 	if (!found_if) {
27261390a385Sgjelinek 		(void) fprintf(stderr,
27271390a385Sgjelinek 		    gettext("could not verify %s %s=%s\n\t"
27281390a385Sgjelinek 		    "network interface %s is not plumbed in the global zone\n"),
27291390a385Sgjelinek 		    "net", "physical", phys, phys);
27301390a385Sgjelinek 		return;
27311390a385Sgjelinek 	}
27321390a385Sgjelinek 
27331390a385Sgjelinek 	/*
27341390a385Sgjelinek 	 * Print this error if we were unable to find the address family
27351390a385Sgjelinek 	 * for this interface.  If the af variable is not initialized to
27361390a385Sgjelinek 	 * to something meaningful by the caller (not AF_UNSPEC) then we
27371390a385Sgjelinek 	 * also skip this message since it wouldn't be informative.
27381390a385Sgjelinek 	 */
27391390a385Sgjelinek 	if (!found_af && af != AF_UNSPEC) {
27401390a385Sgjelinek 		(void) fprintf(stderr,
27411390a385Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
27421390a385Sgjelinek 		    "family is not configured on this interface in the\n\t"
27431390a385Sgjelinek 		    "global zone\n"),
27441390a385Sgjelinek 		    "net", "address", addr, "physical", phys, af2str(af));
27451390a385Sgjelinek 		return;
27461390a385Sgjelinek 	}
27471390a385Sgjelinek 
27481390a385Sgjelinek 	(void) fprintf(stderr,
27491390a385Sgjelinek 	    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
27501390a385Sgjelinek 	    "net", "address", addr, "physical", phys, msg);
27511390a385Sgjelinek }
27521390a385Sgjelinek 
2753ffbafc53Scomay static int
2754*ce28b40eSzt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
27557c478bd9Sstevel@tonic-gate {
27567c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
27577c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
27587c478bd9Sstevel@tonic-gate 	int err;
2759108322fbScarlsonj 	boolean_t in_alt_root;
27607c478bd9Sstevel@tonic-gate 
2761108322fbScarlsonj 	in_alt_root = zonecfg_in_alt_root();
2762108322fbScarlsonj 	if (in_alt_root)
2763108322fbScarlsonj 		goto no_net;
2764108322fbScarlsonj 
27657c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
27667c478bd9Sstevel@tonic-gate 		errno = err;
27677c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
27687c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
27697c478bd9Sstevel@tonic-gate 		return (Z_ERR);
27707c478bd9Sstevel@tonic-gate 	}
27717c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
27727c478bd9Sstevel@tonic-gate 		struct lifreq lifr;
27731390a385Sgjelinek 		sa_family_t af = AF_UNSPEC;
27747c478bd9Sstevel@tonic-gate 		int so, res;
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 		/* skip any loopback interfaces */
27777c478bd9Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
27787c478bd9Sstevel@tonic-gate 			continue;
27797c478bd9Sstevel@tonic-gate 		if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address,
27807c478bd9Sstevel@tonic-gate 		    &lifr)) != Z_OK) {
27811390a385Sgjelinek 			print_net_err(nwiftab.zone_nwif_physical,
27821390a385Sgjelinek 			    nwiftab.zone_nwif_address, af,
27831390a385Sgjelinek 			    zonecfg_strerror(res));
27847c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
27857c478bd9Sstevel@tonic-gate 			continue;
27867c478bd9Sstevel@tonic-gate 		}
27877c478bd9Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family;
27887c478bd9Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
27897c478bd9Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical,
27907c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
27917c478bd9Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = af;
27927c478bd9Sstevel@tonic-gate 		if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
27937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
27947c478bd9Sstevel@tonic-gate 			    "%s=%s %s=%s: could not get socket: %s\n"), "net",
27957c478bd9Sstevel@tonic-gate 			    "address", nwiftab.zone_nwif_address, "physical",
27967c478bd9Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, strerror(errno));
27977c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
27987c478bd9Sstevel@tonic-gate 			continue;
27997c478bd9Sstevel@tonic-gate 		}
28001390a385Sgjelinek 		if (ioctl(so, SIOCGLIFFLAGS, &lifr) < 0) {
280122321485Svp157776 			/*
280222321485Svp157776 			 * The interface failed to come up.  We continue on
280322321485Svp157776 			 * anyway for the sake of consistency: a zone is not
280422321485Svp157776 			 * shut down if the interface fails any time after
280522321485Svp157776 			 * boot, nor does the global zone fail to boot if an
280622321485Svp157776 			 * interface fails.
280722321485Svp157776 			 */
280822321485Svp157776 			(void) fprintf(stderr,
280922321485Svp157776 			    gettext("WARNING: skipping interface '%s' which "
281022321485Svp157776 			    "may not be present/plumbed in the global zone.\n"),
281122321485Svp157776 			    nwiftab.zone_nwif_physical);
281222321485Svp157776 
28137c478bd9Sstevel@tonic-gate 		}
28147c478bd9Sstevel@tonic-gate 		(void) close(so);
28157c478bd9Sstevel@tonic-gate 	}
28167c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2817108322fbScarlsonj no_net:
28187c478bd9Sstevel@tonic-gate 
2819e7f3c547Sgjelinek 	/* verify that lofs has not been excluded from the kernel */
28208cd327d5Sgjelinek 	if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
28218cd327d5Sgjelinek 	    cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
28228cd327d5Sgjelinek 	    modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
2823e7f3c547Sgjelinek 		if (errno == ENXIO)
2824e7f3c547Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2825e7f3c547Sgjelinek 			    "lofs(7FS): possibly excluded in /etc/system\n"));
2826e7f3c547Sgjelinek 		else
2827e7f3c547Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2828e7f3c547Sgjelinek 			    "lofs(7FS): %s\n"), strerror(errno));
2829e7f3c547Sgjelinek 		return_code = Z_ERR;
2830e7f3c547Sgjelinek 	}
2831e7f3c547Sgjelinek 
28327c478bd9Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
28337c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2834b5abaf04Sgjelinek 	if (verify_ipd(handle) != Z_OK)
2835b5abaf04Sgjelinek 		return_code = Z_ERR;
2836108322fbScarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
28377c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2838108322fbScarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
28397c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2840*ce28b40eSzt129084 	if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
28419acbbeafSnn35248 		return_code = Z_ERR;
2842fa9e4066Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2843fa9e4066Sahrens 		return_code = Z_ERR;
2844ffbafc53Scomay 
2845ffbafc53Scomay 	/*
2846ffbafc53Scomay 	 * As the "mount" command is used for patching/upgrading of zones
2847ffbafc53Scomay 	 * or other maintenance processes, the zone's privilege set is not
2848ffbafc53Scomay 	 * checked in this case.  Instead, the default, safe set of
2849ffbafc53Scomay 	 * privileges will be used when this zone is created in the
2850ffbafc53Scomay 	 * kernel.
2851ffbafc53Scomay 	 */
2852ffbafc53Scomay 	if (!in_alt_root && cmd_num != CMD_MOUNT &&
2853ffbafc53Scomay 	    verify_limitpriv(handle) != Z_OK)
2854ffbafc53Scomay 		return_code = Z_ERR;
28558cd327d5Sgjelinek 
28568cd327d5Sgjelinek 	return (return_code);
28578cd327d5Sgjelinek }
28588cd327d5Sgjelinek 
28598cd327d5Sgjelinek static int
2860*ce28b40eSzt129084 verify_details(int cmd_num, char *argv[])
28618cd327d5Sgjelinek {
28628cd327d5Sgjelinek 	zone_dochandle_t handle;
28638cd327d5Sgjelinek 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
28648cd327d5Sgjelinek 	int return_code = Z_OK;
28658cd327d5Sgjelinek 	int err;
28668cd327d5Sgjelinek 
28678cd327d5Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
28688cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
28698cd327d5Sgjelinek 		return (Z_ERR);
28708cd327d5Sgjelinek 	}
28718cd327d5Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
28728cd327d5Sgjelinek 		errno = err;
28738cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
28748cd327d5Sgjelinek 		zonecfg_fini_handle(handle);
28758cd327d5Sgjelinek 		return (Z_ERR);
28768cd327d5Sgjelinek 	}
28778cd327d5Sgjelinek 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
28788cd327d5Sgjelinek 	    Z_OK) {
28798cd327d5Sgjelinek 		errno = err;
28808cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
28818cd327d5Sgjelinek 		zonecfg_fini_handle(handle);
28828cd327d5Sgjelinek 		return (Z_ERR);
28838cd327d5Sgjelinek 	}
28848cd327d5Sgjelinek 	/*
28858cd327d5Sgjelinek 	 * zonecfg_get_zonepath() gets its data from the XML repository.
28868cd327d5Sgjelinek 	 * Verify this against the index file, which is checked first by
28878cd327d5Sgjelinek 	 * zone_get_zonepath().  If they don't match, bail out.
28888cd327d5Sgjelinek 	 */
28898cd327d5Sgjelinek 	if ((err = zone_get_zonepath(target_zone, checkpath,
28908cd327d5Sgjelinek 	    sizeof (checkpath))) != Z_OK) {
28918cd327d5Sgjelinek 		errno = err;
28928cd327d5Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
28938cd327d5Sgjelinek 		return (Z_ERR);
28948cd327d5Sgjelinek 	}
28958cd327d5Sgjelinek 	if (strcmp(zonepath, checkpath) != 0) {
28968cd327d5Sgjelinek 		/*
28978cd327d5Sgjelinek 		 * TRANSLATION_NOTE
28988cd327d5Sgjelinek 		 * XML and zonepath are literals that should not be translated.
28998cd327d5Sgjelinek 		 */
29008cd327d5Sgjelinek 		(void) fprintf(stderr, gettext("The XML repository has "
29018cd327d5Sgjelinek 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
29028cd327d5Sgjelinek 		    "These must match, so fix the incorrect entry.\n"),
29038cd327d5Sgjelinek 		    zonepath, checkpath);
29048cd327d5Sgjelinek 		return (Z_ERR);
29058cd327d5Sgjelinek 	}
29068cd327d5Sgjelinek 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
29078cd327d5Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
29088cd327d5Sgjelinek 		    "because of the above errors.\n"), zonepath);
29098cd327d5Sgjelinek 		return_code = Z_ERR;
29108cd327d5Sgjelinek 	}
29118cd327d5Sgjelinek 
2912*ce28b40eSzt129084 	if (verify_handle(cmd_num, handle, argv) != Z_OK)
29138cd327d5Sgjelinek 		return_code = Z_ERR;
29148cd327d5Sgjelinek 
29157c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
29167c478bd9Sstevel@tonic-gate 	if (return_code == Z_ERR)
29177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
29187c478bd9Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
29197c478bd9Sstevel@tonic-gate 		    execname, target_zone);
29207c478bd9Sstevel@tonic-gate 	return (return_code);
29217c478bd9Sstevel@tonic-gate }
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate static int
29247c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[])
29257c478bd9Sstevel@tonic-gate {
29267c478bd9Sstevel@tonic-gate 	int arg;
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	optind = 0;
29297c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
29307c478bd9Sstevel@tonic-gate 		switch (arg) {
29317c478bd9Sstevel@tonic-gate 		case '?':
29327c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
29337c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
29347c478bd9Sstevel@tonic-gate 		default:
29357c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
29367c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
29377c478bd9Sstevel@tonic-gate 		}
29387c478bd9Sstevel@tonic-gate 	}
29397c478bd9Sstevel@tonic-gate 	if (argc > optind) {
29407c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
29417c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
29427c478bd9Sstevel@tonic-gate 	}
29439acbbeafSnn35248 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
29449acbbeafSnn35248 	    != Z_OK)
29457c478bd9Sstevel@tonic-gate 		return (Z_ERR);
2946*ce28b40eSzt129084 	return (verify_details(CMD_VERIFY, argv));
29477c478bd9Sstevel@tonic-gate }
29487c478bd9Sstevel@tonic-gate 
29499acbbeafSnn35248 static int
29509acbbeafSnn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize)
29519acbbeafSnn35248 {
29529acbbeafSnn35248 	char optstring[4];
29539acbbeafSnn35248 
29549acbbeafSnn35248 	if (opt > 0)
29559acbbeafSnn35248 		(void) sprintf(optstring, " -%c", opt);
29569acbbeafSnn35248 	else
29579acbbeafSnn35248 		(void) strcpy(optstring, " ");
29589acbbeafSnn35248 
29599acbbeafSnn35248 	if ((strlcat(buf, optstring, bufsize) > bufsize))
29609acbbeafSnn35248 		return (Z_ERR);
29619acbbeafSnn35248 	if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
29629acbbeafSnn35248 		return (Z_ERR);
29639acbbeafSnn35248 	return (Z_OK);
29649acbbeafSnn35248 }
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate static int
29677c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[])
29687c478bd9Sstevel@tonic-gate {
29699acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
29707c478bd9Sstevel@tonic-gate 	int lockfd;
29719acbbeafSnn35248 	int arg, err, subproc_err;
29727c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
2973123807fbSedp 	brand_handle_t bh = NULL;
29747c478bd9Sstevel@tonic-gate 	int status;
29750b5de56dSgjelinek 	boolean_t nodataset = B_FALSE;
29769acbbeafSnn35248 	char opts[128];
29779acbbeafSnn35248 
29789acbbeafSnn35248 	if (target_zone == NULL) {
29799acbbeafSnn35248 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
29809acbbeafSnn35248 		return (Z_USAGE);
29819acbbeafSnn35248 	}
29827c478bd9Sstevel@tonic-gate 
2983108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2984108322fbScarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2985108322fbScarlsonj 		return (Z_ERR);
2986108322fbScarlsonj 	}
2987108322fbScarlsonj 
29889acbbeafSnn35248 	if ((err = zone_get_zonepath(target_zone, zonepath,
29899acbbeafSnn35248 	    sizeof (zonepath))) != Z_OK) {
29909acbbeafSnn35248 		errno = err;
29919acbbeafSnn35248 		zperror2(target_zone, gettext("could not get zone path"));
29929acbbeafSnn35248 		return (Z_ERR);
29939acbbeafSnn35248 	}
29949acbbeafSnn35248 
29959acbbeafSnn35248 	/* Fetch the install command from the brand configuration.  */
2996123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
29979acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
29989acbbeafSnn35248 		return (Z_ERR);
29999acbbeafSnn35248 	}
30009acbbeafSnn35248 
30019acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
3002123807fbSedp 	if (brand_get_install(bh, target_zone, zonepath, cmdbuf + EXEC_LEN,
30039acbbeafSnn35248 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) {
30049acbbeafSnn35248 		zerror("invalid brand configuration: missing install resource");
3005123807fbSedp 		brand_close(bh);
30069acbbeafSnn35248 		return (Z_ERR);
30079acbbeafSnn35248 	}
30089acbbeafSnn35248 
30099acbbeafSnn35248 	(void) strcpy(opts, "?x:");
30109acbbeafSnn35248 	if (!is_native_zone) {
30119acbbeafSnn35248 		/*
30129acbbeafSnn35248 		 * Fetch the list of recognized command-line options from
30139acbbeafSnn35248 		 * the brand configuration file.
30149acbbeafSnn35248 		 */
3015123807fbSedp 		if (brand_get_installopts(bh, opts + strlen(opts),
30169acbbeafSnn35248 		    sizeof (opts) - strlen(opts)) != 0) {
30179acbbeafSnn35248 			zerror("invalid brand configuration: missing "
30189acbbeafSnn35248 			    "install options resource");
3019123807fbSedp 			brand_close(bh);
30209acbbeafSnn35248 			return (Z_ERR);
30219acbbeafSnn35248 		}
30229acbbeafSnn35248 	}
3023123807fbSedp 	brand_close(bh);
30249acbbeafSnn35248 
30257c478bd9Sstevel@tonic-gate 	optind = 0;
30269acbbeafSnn35248 	while ((arg = getopt(argc, argv, opts)) != EOF) {
30277c478bd9Sstevel@tonic-gate 		switch (arg) {
30287c478bd9Sstevel@tonic-gate 		case '?':
30297c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
30307c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
30310b5de56dSgjelinek 		case 'x':
30320b5de56dSgjelinek 			if (strcmp(optarg, "nodataset") != 0) {
30330b5de56dSgjelinek 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
30340b5de56dSgjelinek 				return (Z_USAGE);
30350b5de56dSgjelinek 			}
30360b5de56dSgjelinek 			nodataset = B_TRUE;
30370b5de56dSgjelinek 			break;
30387c478bd9Sstevel@tonic-gate 		default:
30399acbbeafSnn35248 			if (is_native_zone) {
30407c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
30417c478bd9Sstevel@tonic-gate 				return (Z_USAGE);
30427c478bd9Sstevel@tonic-gate 			}
30439acbbeafSnn35248 
30449acbbeafSnn35248 			/*
30459acbbeafSnn35248 			 * This option isn't for zoneadm, so append it to
30469acbbeafSnn35248 			 * the command line passed to the brand-specific
30479acbbeafSnn35248 			 * install routine.
30489acbbeafSnn35248 			 */
30499acbbeafSnn35248 			if (addopt(cmdbuf, optopt, optarg,
30509acbbeafSnn35248 			    sizeof (cmdbuf)) != Z_OK) {
30519acbbeafSnn35248 				zerror("Install command line too long");
30529acbbeafSnn35248 				return (Z_ERR);
30537c478bd9Sstevel@tonic-gate 			}
30549acbbeafSnn35248 			break;
30557c478bd9Sstevel@tonic-gate 		}
30569acbbeafSnn35248 	}
30579acbbeafSnn35248 
30589acbbeafSnn35248 	if (!is_native_zone) {
30599acbbeafSnn35248 		for (; optind < argc; optind++) {
30609acbbeafSnn35248 			if (addopt(cmdbuf, 0, argv[optind],
30619acbbeafSnn35248 			    sizeof (cmdbuf)) != Z_OK) {
30629acbbeafSnn35248 				zerror("Install command line too long");
30639acbbeafSnn35248 				return (Z_ERR);
30649acbbeafSnn35248 			}
30659acbbeafSnn35248 		}
30669acbbeafSnn35248 	}
30679acbbeafSnn35248 
30689acbbeafSnn35248 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, B_FALSE)
30699acbbeafSnn35248 	    != Z_OK)
30707c478bd9Sstevel@tonic-gate 		return (Z_ERR);
3071*ce28b40eSzt129084 	if (verify_details(CMD_INSTALL, argv) != Z_OK)
30727c478bd9Sstevel@tonic-gate 		return (Z_ERR);
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
30757c478bd9Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
3076865e09a4Sgjelinek 		    "zoneadm");
30777c478bd9Sstevel@tonic-gate 		return (Z_ERR);
30787c478bd9Sstevel@tonic-gate 	}
30797c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
30807c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
30817c478bd9Sstevel@tonic-gate 		errno = err;
30827c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
30837c478bd9Sstevel@tonic-gate 		goto done;
30847c478bd9Sstevel@tonic-gate 	}
30857c478bd9Sstevel@tonic-gate 
30869acbbeafSnn35248 	if (!nodataset)
30879acbbeafSnn35248 		create_zfs_zonepath(zonepath);
30889acbbeafSnn35248 
30897c478bd9Sstevel@tonic-gate 	/*
30907c478bd9Sstevel@tonic-gate 	 * According to the Application Packaging Developer's Guide, a
30917c478bd9Sstevel@tonic-gate 	 * "checkinstall" script when included in a package is executed as
30927c478bd9Sstevel@tonic-gate 	 * the user "install", if such a user exists, or by the user
30937c478bd9Sstevel@tonic-gate 	 * "nobody".  In order to support this dubious behavior, the path
30947c478bd9Sstevel@tonic-gate 	 * to the zone being constructed is opened up during the life of
30957c478bd9Sstevel@tonic-gate 	 * the command laying down the zone's root file system.  Once this
30967c478bd9Sstevel@tonic-gate 	 * has completed, regardless of whether it was successful, the
30977c478bd9Sstevel@tonic-gate 	 * path to the zone is again restricted.
30987c478bd9Sstevel@tonic-gate 	 */
30997c478bd9Sstevel@tonic-gate 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
31007c478bd9Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
31017c478bd9Sstevel@tonic-gate 		err = Z_ERR;
31027c478bd9Sstevel@tonic-gate 		goto done;
31037c478bd9Sstevel@tonic-gate 	}
31047c478bd9Sstevel@tonic-gate 
31059acbbeafSnn35248 	if (is_native_zone)
31067c478bd9Sstevel@tonic-gate 		status = do_subproc(cmdbuf);
31079acbbeafSnn35248 	else
31089acbbeafSnn35248 		status = do_subproc_interactive(cmdbuf);
31099acbbeafSnn35248 
31107c478bd9Sstevel@tonic-gate 	if (chmod(zonepath, S_IRWXU) != 0) {
31117c478bd9Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
31127c478bd9Sstevel@tonic-gate 		err = Z_ERR;
31137c478bd9Sstevel@tonic-gate 		goto done;
31147c478bd9Sstevel@tonic-gate 	}
31159acbbeafSnn35248 	if ((subproc_err =
31169acbbeafSnn35248 	    subproc_status(gettext("brand-specific installation"), status,
31179acbbeafSnn35248 	    B_FALSE)) != ZONE_SUBPROC_OK) {
31189acbbeafSnn35248 		err = Z_ERR;
31197c478bd9Sstevel@tonic-gate 		goto done;
31209acbbeafSnn35248 	}
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
31237c478bd9Sstevel@tonic-gate 		errno = err;
31247c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
31257c478bd9Sstevel@tonic-gate 		goto done;
31267c478bd9Sstevel@tonic-gate 	}
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate done:
31299acbbeafSnn35248 	/*
31309acbbeafSnn35248 	 * If the install script exited with ZONE_SUBPROC_USAGE or
31319acbbeafSnn35248 	 * ZONE_SUBPROC_NOTCOMPLETE, try to clean up the zone and leave the
31329acbbeafSnn35248 	 * zone in the CONFIGURED state so that another install can be
31339acbbeafSnn35248 	 * attempted without requiring an uninstall first.
31349acbbeafSnn35248 	 */
31359acbbeafSnn35248 	if ((subproc_err == ZONE_SUBPROC_USAGE) ||
31369acbbeafSnn35248 	    (subproc_err == ZONE_SUBPROC_NOTCOMPLETE)) {
31379acbbeafSnn35248 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
31389acbbeafSnn35248 			errno = err;
31399acbbeafSnn35248 			zperror2(target_zone,
31409acbbeafSnn35248 			    gettext("cleaning up zonepath failed"));
31419acbbeafSnn35248 		} else if ((err = zone_set_state(target_zone,
31429acbbeafSnn35248 		    ZONE_STATE_CONFIGURED)) != Z_OK) {
31439acbbeafSnn35248 			errno = err;
31449acbbeafSnn35248 			zperror2(target_zone, gettext("could not set state"));
31459acbbeafSnn35248 		}
31469acbbeafSnn35248 	}
31479acbbeafSnn35248 
31487c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
31497c478bd9Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
31507c478bd9Sstevel@tonic-gate }
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate /*
3153865e09a4Sgjelinek  * Check that the inherited pkg dirs are the same for the clone and its source.
3154865e09a4Sgjelinek  * The easiest way to do that is check that the list of ipds is the same
3155865e09a4Sgjelinek  * by matching each one against the other.  This algorithm should be fine since
3156865e09a4Sgjelinek  * the list of ipds should not be that long.
3157865e09a4Sgjelinek  */
3158865e09a4Sgjelinek static int
3159865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone,
3160865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3161865e09a4Sgjelinek {
3162865e09a4Sgjelinek 	int err;
3163865e09a4Sgjelinek 	int res = Z_OK;
3164865e09a4Sgjelinek 	int s_cnt = 0;
3165865e09a4Sgjelinek 	int t_cnt = 0;
3166865e09a4Sgjelinek 	struct zone_fstab s_fstab;
3167865e09a4Sgjelinek 	struct zone_fstab t_fstab;
3168865e09a4Sgjelinek 
3169865e09a4Sgjelinek 	/*
3170865e09a4Sgjelinek 	 * First check the source of the clone against the target.
3171865e09a4Sgjelinek 	 */
3172865e09a4Sgjelinek 	if ((err = zonecfg_setipdent(s_handle)) != Z_OK) {
3173865e09a4Sgjelinek 		errno = err;
3174865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not enumerate "
3175865e09a4Sgjelinek 		    "inherit-pkg-dirs"));
3176865e09a4Sgjelinek 		return (Z_ERR);
3177865e09a4Sgjelinek 	}
3178865e09a4Sgjelinek 
3179865e09a4Sgjelinek 	while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) {
3180865e09a4Sgjelinek 		boolean_t match = B_FALSE;
3181865e09a4Sgjelinek 
3182865e09a4Sgjelinek 		s_cnt++;
3183865e09a4Sgjelinek 
3184865e09a4Sgjelinek 		if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
3185865e09a4Sgjelinek 			errno = err;
3186865e09a4Sgjelinek 			zperror2(target_zone, gettext("could not enumerate "
3187865e09a4Sgjelinek 			    "inherit-pkg-dirs"));
3188865e09a4Sgjelinek 			(void) zonecfg_endipdent(s_handle);
3189865e09a4Sgjelinek 			return (Z_ERR);
3190865e09a4Sgjelinek 		}
3191865e09a4Sgjelinek 
3192865e09a4Sgjelinek 		while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) {
3193865e09a4Sgjelinek 			if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir)
3194865e09a4Sgjelinek 			    == 0) {
3195865e09a4Sgjelinek 				match = B_TRUE;
3196865e09a4Sgjelinek 				break;
3197865e09a4Sgjelinek 			}
3198865e09a4Sgjelinek 		}
3199865e09a4Sgjelinek 		(void) zonecfg_endipdent(t_handle);
3200865e09a4Sgjelinek 
3201865e09a4Sgjelinek 		if (!match) {
3202865e09a4Sgjelinek 			(void) fprintf(stderr, gettext("inherit-pkg-dir "
3203865e09a4Sgjelinek 			    "'%s' is not configured in zone %s.\n"),
3204865e09a4Sgjelinek 			    s_fstab.zone_fs_dir, target_zone);
3205865e09a4Sgjelinek 			res = Z_ERR;
3206865e09a4Sgjelinek 		}
3207865e09a4Sgjelinek 	}
3208865e09a4Sgjelinek 
3209865e09a4Sgjelinek 	(void) zonecfg_endipdent(s_handle);
3210865e09a4Sgjelinek 
3211865e09a4Sgjelinek 	/* skip the next check if we already have errors */
3212865e09a4Sgjelinek 	if (res == Z_ERR)
3213865e09a4Sgjelinek 		return (res);
3214865e09a4Sgjelinek 
3215865e09a4Sgjelinek 	/*
3216865e09a4Sgjelinek 	 * Now check the number of ipds in the target so we can verify
3217865e09a4Sgjelinek 	 * that the source is not a subset of the target.
3218865e09a4Sgjelinek 	 */
3219865e09a4Sgjelinek 	if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
3220865e09a4Sgjelinek 		errno = err;
3221865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not enumerate "
3222865e09a4Sgjelinek 		    "inherit-pkg-dirs"));
3223865e09a4Sgjelinek 		return (Z_ERR);
3224865e09a4Sgjelinek 	}
3225865e09a4Sgjelinek 
3226865e09a4Sgjelinek 	while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK)
3227865e09a4Sgjelinek 		t_cnt++;
3228865e09a4Sgjelinek 
3229865e09a4Sgjelinek 	(void) zonecfg_endipdent(t_handle);
3230865e09a4Sgjelinek 
3231865e09a4Sgjelinek 	if (t_cnt != s_cnt) {
3232865e09a4Sgjelinek 		(void) fprintf(stderr, gettext("Zone %s is configured "
3233865e09a4Sgjelinek 		    "with inherit-pkg-dirs that are not configured in zone "
3234865e09a4Sgjelinek 		    "%s.\n"), target_zone, source_zone);
3235865e09a4Sgjelinek 		res = Z_ERR;
3236865e09a4Sgjelinek 	}
3237865e09a4Sgjelinek 
3238865e09a4Sgjelinek 	return (res);
3239865e09a4Sgjelinek }
3240865e09a4Sgjelinek 
3241865e09a4Sgjelinek static void
3242865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
3243865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3244865e09a4Sgjelinek {
3245865e09a4Sgjelinek 	int err;
3246865e09a4Sgjelinek 	struct zone_devtab s_devtab;
3247865e09a4Sgjelinek 	struct zone_devtab t_devtab;
3248865e09a4Sgjelinek 
3249865e09a4Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
3250865e09a4Sgjelinek 		errno = err;
3251865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
3252865e09a4Sgjelinek 		return;
3253865e09a4Sgjelinek 	}
3254865e09a4Sgjelinek 
3255865e09a4Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
3256865e09a4Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
3257865e09a4Sgjelinek 			errno = err;
3258865e09a4Sgjelinek 			zperror2(source_zone,
3259865e09a4Sgjelinek 			    gettext("could not enumerate devices"));
3260865e09a4Sgjelinek 			(void) zonecfg_enddevent(t_handle);
3261865e09a4Sgjelinek 			return;
3262865e09a4Sgjelinek 		}
3263865e09a4Sgjelinek 
3264865e09a4Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
3265865e09a4Sgjelinek 			/*
3266865e09a4Sgjelinek 			 * Use fnmatch to catch the case where wildcards
3267865e09a4Sgjelinek 			 * were used in one zone and the other has an
3268865e09a4Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
3269865e09a4Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
3270865e09a4Sgjelinek 			 */
3271865e09a4Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
3272865e09a4Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
3273865e09a4Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
3274865e09a4Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
3275865e09a4Sgjelinek 				(void) fprintf(stderr,
3276865e09a4Sgjelinek 				    gettext("WARNING: device '%s' "
3277865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3278865e09a4Sgjelinek 				    t_devtab.zone_dev_match);
3279865e09a4Sgjelinek 				break;
3280865e09a4Sgjelinek 			}
3281865e09a4Sgjelinek 		}
3282865e09a4Sgjelinek 		(void) zonecfg_enddevent(s_handle);
3283865e09a4Sgjelinek 	}
3284865e09a4Sgjelinek 
3285865e09a4Sgjelinek 	(void) zonecfg_enddevent(t_handle);
3286865e09a4Sgjelinek }
3287865e09a4Sgjelinek 
3288865e09a4Sgjelinek /*
3289865e09a4Sgjelinek  * Check if the specified mount option (opt) is contained within the
3290865e09a4Sgjelinek  * options string.
3291865e09a4Sgjelinek  */
3292865e09a4Sgjelinek static boolean_t
3293865e09a4Sgjelinek opt_match(char *opt, char *options)
3294865e09a4Sgjelinek {
3295865e09a4Sgjelinek 	char *p;
3296865e09a4Sgjelinek 	char *lastp;
3297865e09a4Sgjelinek 
3298865e09a4Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
3299865e09a4Sgjelinek 		if (strcmp(p, opt) == 0)
3300865e09a4Sgjelinek 			return (B_TRUE);
3301865e09a4Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
3302865e09a4Sgjelinek 			if (strcmp(p, opt) == 0)
3303865e09a4Sgjelinek 				return (B_TRUE);
3304865e09a4Sgjelinek 		}
3305865e09a4Sgjelinek 	}
3306865e09a4Sgjelinek 
3307865e09a4Sgjelinek 	return (B_FALSE);
3308865e09a4Sgjelinek }
3309865e09a4Sgjelinek 
33100b5de56dSgjelinek #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
3311865e09a4Sgjelinek 	"in both zones.\n"
3312865e09a4Sgjelinek 
3313865e09a4Sgjelinek static void
3314865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
3315865e09a4Sgjelinek {
3316865e09a4Sgjelinek 	/*
3317865e09a4Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
3318865e09a4Sgjelinek 	 * either is rw since this will effect the other zone.
3319865e09a4Sgjelinek 	 */
3320865e09a4Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
3321865e09a4Sgjelinek 		zone_fsopt_t *optp;
3322865e09a4Sgjelinek 
3323865e09a4Sgjelinek 		/* The default is rw so no options means rw */
3324865e09a4Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
3325865e09a4Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
3326865e09a4Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
3327865e09a4Sgjelinek 			    t_fstab->zone_fs_special);
3328865e09a4Sgjelinek 			return;
3329865e09a4Sgjelinek 		}
3330865e09a4Sgjelinek 
3331865e09a4Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
3332865e09a4Sgjelinek 		    optp = optp->zone_fsopt_next) {
3333865e09a4Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
3334865e09a4Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
3335865e09a4Sgjelinek 				    s_fstab->zone_fs_special);
3336865e09a4Sgjelinek 				return;
3337865e09a4Sgjelinek 			}
3338865e09a4Sgjelinek 		}
3339865e09a4Sgjelinek 
3340865e09a4Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
3341865e09a4Sgjelinek 		    optp = optp->zone_fsopt_next) {
3342865e09a4Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
3343865e09a4Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
3344865e09a4Sgjelinek 				    t_fstab->zone_fs_special);
3345865e09a4Sgjelinek 				return;
3346865e09a4Sgjelinek 			}
3347865e09a4Sgjelinek 		}
3348865e09a4Sgjelinek 
3349865e09a4Sgjelinek 		return;
3350865e09a4Sgjelinek 	}
3351865e09a4Sgjelinek 
3352865e09a4Sgjelinek 	/*
3353865e09a4Sgjelinek 	 * TRANSLATION_NOTE
33540b5de56dSgjelinek 	 * The first variable is the file system type and the second is
33550b5de56dSgjelinek 	 * the file system special device.  For example,
33560b5de56dSgjelinek 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
3357865e09a4Sgjelinek 	 */
33580b5de56dSgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
3359865e09a4Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
3360865e09a4Sgjelinek 	    t_fstab->zone_fs_special);
3361865e09a4Sgjelinek }
3362865e09a4Sgjelinek 
3363865e09a4Sgjelinek static void
3364865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
3365865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3366865e09a4Sgjelinek {
3367865e09a4Sgjelinek 	int err;
3368865e09a4Sgjelinek 	struct zone_fstab s_fstab;
3369865e09a4Sgjelinek 	struct zone_fstab t_fstab;
3370865e09a4Sgjelinek 
3371865e09a4Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
3372865e09a4Sgjelinek 		errno = err;
3373865e09a4Sgjelinek 		zperror2(target_zone,
33740b5de56dSgjelinek 		    gettext("could not enumerate file systems"));
3375865e09a4Sgjelinek 		return;
3376865e09a4Sgjelinek 	}
3377865e09a4Sgjelinek 
3378865e09a4Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
3379865e09a4Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
3380865e09a4Sgjelinek 			errno = err;
3381865e09a4Sgjelinek 			zperror2(source_zone,
33820b5de56dSgjelinek 			    gettext("could not enumerate file systems"));
3383865e09a4Sgjelinek 			(void) zonecfg_endfsent(t_handle);
3384865e09a4Sgjelinek 			return;
3385865e09a4Sgjelinek 		}
3386865e09a4Sgjelinek 
3387865e09a4Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
3388865e09a4Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
3389865e09a4Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
3390865e09a4Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
3391865e09a4Sgjelinek 				break;
3392865e09a4Sgjelinek 			}
3393865e09a4Sgjelinek 		}
3394865e09a4Sgjelinek 		(void) zonecfg_endfsent(s_handle);
3395865e09a4Sgjelinek 	}
3396865e09a4Sgjelinek 
3397865e09a4Sgjelinek 	(void) zonecfg_endfsent(t_handle);
3398865e09a4Sgjelinek }
3399865e09a4Sgjelinek 
3400865e09a4Sgjelinek /*
3401865e09a4Sgjelinek  * We don't catch the case where you used the same IP address but
3402865e09a4Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
3403865e09a4Sgjelinek  * However, we're not going to worry about that but we will check for
3404865e09a4Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
3405865e09a4Sgjelinek  * and handle that case as a match.
3406865e09a4Sgjelinek  */
3407865e09a4Sgjelinek static void
3408865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
3409865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3410865e09a4Sgjelinek {
3411865e09a4Sgjelinek 	int err;
3412865e09a4Sgjelinek 	struct zone_nwiftab s_nwiftab;
3413865e09a4Sgjelinek 	struct zone_nwiftab t_nwiftab;
3414865e09a4Sgjelinek 
3415865e09a4Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
3416865e09a4Sgjelinek 		errno = err;
3417865e09a4Sgjelinek 		zperror2(target_zone,
3418865e09a4Sgjelinek 		    gettext("could not enumerate network interfaces"));
3419865e09a4Sgjelinek 		return;
3420865e09a4Sgjelinek 	}
3421865e09a4Sgjelinek 
3422865e09a4Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
3423865e09a4Sgjelinek 		char *p;
3424865e09a4Sgjelinek 
3425865e09a4Sgjelinek 		/* remove an (optional) netmask from the address */
3426865e09a4Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
3427865e09a4Sgjelinek 			*p = '\0';
3428865e09a4Sgjelinek 
3429865e09a4Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
3430865e09a4Sgjelinek 			errno = err;
3431865e09a4Sgjelinek 			zperror2(source_zone,
3432865e09a4Sgjelinek 			    gettext("could not enumerate network interfaces"));
3433865e09a4Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
3434865e09a4Sgjelinek 			return;
3435865e09a4Sgjelinek 		}
3436865e09a4Sgjelinek 
3437865e09a4Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
3438865e09a4Sgjelinek 			/* remove an (optional) netmask from the address */
3439865e09a4Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
3440865e09a4Sgjelinek 			    != NULL)
3441865e09a4Sgjelinek 				*p = '\0';
3442865e09a4Sgjelinek 
3443865e09a4Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
3444865e09a4Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
3445865e09a4Sgjelinek 				(void) fprintf(stderr,
3446865e09a4Sgjelinek 				    gettext("WARNING: network address '%s' "
3447865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3448865e09a4Sgjelinek 				    t_nwiftab.zone_nwif_address);
3449865e09a4Sgjelinek 				break;
3450865e09a4Sgjelinek 			}
3451865e09a4Sgjelinek 		}
3452865e09a4Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
3453865e09a4Sgjelinek 	}
3454865e09a4Sgjelinek 
3455865e09a4Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
3456865e09a4Sgjelinek }
3457865e09a4Sgjelinek 
3458865e09a4Sgjelinek static void
34599acbbeafSnn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source,
34609acbbeafSnn35248 	zone_dochandle_t t_handle, char *target)
3461865e09a4Sgjelinek {
3462865e09a4Sgjelinek 	int err;
3463865e09a4Sgjelinek 	struct zone_dstab s_dstab;
3464865e09a4Sgjelinek 	struct zone_dstab t_dstab;
3465865e09a4Sgjelinek 
3466865e09a4Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
3467865e09a4Sgjelinek 		errno = err;
34689acbbeafSnn35248 		zperror2(target, gettext("could not enumerate datasets"));
3469865e09a4Sgjelinek 		return;
3470865e09a4Sgjelinek 	}
3471865e09a4Sgjelinek 
3472865e09a4Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
3473865e09a4Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
3474865e09a4Sgjelinek 			errno = err;
34759acbbeafSnn35248 			zperror2(source,
3476865e09a4Sgjelinek 			    gettext("could not enumerate datasets"));
3477865e09a4Sgjelinek 			(void) zonecfg_enddsent(t_handle);
3478865e09a4Sgjelinek 			return;
3479865e09a4Sgjelinek 		}
3480865e09a4Sgjelinek 
3481865e09a4Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
3482865e09a4Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
3483865e09a4Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
34849acbbeafSnn35248 				target_zone = source;
34859acbbeafSnn35248 				zerror(gettext("WARNING: dataset '%s' "
3486865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3487865e09a4Sgjelinek 				    t_dstab.zone_dataset_name);
3488865e09a4Sgjelinek 				break;
3489865e09a4Sgjelinek 			}
3490865e09a4Sgjelinek 		}
3491865e09a4Sgjelinek 		(void) zonecfg_enddsent(s_handle);
3492865e09a4Sgjelinek 	}
3493865e09a4Sgjelinek 
3494865e09a4Sgjelinek 	(void) zonecfg_enddsent(t_handle);
3495865e09a4Sgjelinek }
3496865e09a4Sgjelinek 
34979acbbeafSnn35248 /*
34989acbbeafSnn35248  * Check that the clone and its source have the same brand type.
34999acbbeafSnn35248  */
35009acbbeafSnn35248 static int
35019acbbeafSnn35248 valid_brand_clone(char *source_zone, char *target_zone)
35029acbbeafSnn35248 {
3503123807fbSedp 	brand_handle_t bh;
35049acbbeafSnn35248 	char source_brand[MAXNAMELEN];
35059acbbeafSnn35248 
35069acbbeafSnn35248 	if ((zone_get_brand(source_zone, source_brand,
35079acbbeafSnn35248 	    sizeof (source_brand))) != Z_OK) {
35089acbbeafSnn35248 		(void) fprintf(stderr, "%s: zone '%s': %s\n",
35099acbbeafSnn35248 		    execname, source_zone, gettext("missing or invalid brand"));
35109acbbeafSnn35248 		return (Z_ERR);
35119acbbeafSnn35248 	}
35129acbbeafSnn35248 
35139acbbeafSnn35248 	if (strcmp(source_brand, target_brand) != NULL) {
35149acbbeafSnn35248 		(void) fprintf(stderr,
35159acbbeafSnn35248 		    gettext("%s: Zones '%s' and '%s' have different brand "
35169acbbeafSnn35248 		    "types.\n"), execname, source_zone, target_zone);
35179acbbeafSnn35248 		return (Z_ERR);
35189acbbeafSnn35248 	}
35199acbbeafSnn35248 
3520123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
35219acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
35229acbbeafSnn35248 		return (Z_ERR);
35239acbbeafSnn35248 	}
3524123807fbSedp 	brand_close(bh);
35259acbbeafSnn35248 	return (Z_OK);
35269acbbeafSnn35248 }
35279acbbeafSnn35248 
3528865e09a4Sgjelinek static int
3529865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone)
3530865e09a4Sgjelinek {
3531865e09a4Sgjelinek 	int err = Z_OK;
3532865e09a4Sgjelinek 	zone_dochandle_t s_handle;
3533865e09a4Sgjelinek 	zone_dochandle_t t_handle;
3534865e09a4Sgjelinek 
3535865e09a4Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
3536865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3537865e09a4Sgjelinek 		return (Z_ERR);
3538865e09a4Sgjelinek 	}
3539865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
3540865e09a4Sgjelinek 		errno = err;
3541865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3542865e09a4Sgjelinek 		zonecfg_fini_handle(t_handle);
3543865e09a4Sgjelinek 		return (Z_ERR);
3544865e09a4Sgjelinek 	}
3545865e09a4Sgjelinek 
3546865e09a4Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
3547865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3548865e09a4Sgjelinek 		zonecfg_fini_handle(t_handle);
3549865e09a4Sgjelinek 		return (Z_ERR);
3550865e09a4Sgjelinek 	}
3551865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
3552865e09a4Sgjelinek 		errno = err;
3553865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3554865e09a4Sgjelinek 		goto done;
3555865e09a4Sgjelinek 	}
3556865e09a4Sgjelinek 
35579acbbeafSnn35248 	/* verify new zone has same brand type */
35589acbbeafSnn35248 	err = valid_brand_clone(source_zone, target_zone);
35599acbbeafSnn35248 	if (err != Z_OK)
35609acbbeafSnn35248 		goto done;
35619acbbeafSnn35248 
3562865e09a4Sgjelinek 	/* verify new zone has same inherit-pkg-dirs */
3563865e09a4Sgjelinek 	err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone);
3564865e09a4Sgjelinek 
3565865e09a4Sgjelinek 	/* warn about imported fs's which are the same */
3566865e09a4Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
3567865e09a4Sgjelinek 
3568865e09a4Sgjelinek 	/* warn about imported IP addresses which are the same */
3569865e09a4Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
3570865e09a4Sgjelinek 
3571865e09a4Sgjelinek 	/* warn about imported devices which are the same */
3572865e09a4Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
3573865e09a4Sgjelinek 
3574865e09a4Sgjelinek 	/* warn about imported datasets which are the same */
3575865e09a4Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
3576865e09a4Sgjelinek 
3577865e09a4Sgjelinek done:
3578865e09a4Sgjelinek 	zonecfg_fini_handle(t_handle);
3579865e09a4Sgjelinek 	zonecfg_fini_handle(s_handle);
3580865e09a4Sgjelinek 
3581865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3582865e09a4Sgjelinek }
3583865e09a4Sgjelinek 
3584865e09a4Sgjelinek static int
3585865e09a4Sgjelinek copy_zone(char *src, char *dst)
3586865e09a4Sgjelinek {
3587865e09a4Sgjelinek 	boolean_t out_null = B_FALSE;
3588865e09a4Sgjelinek 	int status;
3589865e09a4Sgjelinek 	char *outfile;
3590865e09a4Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
3591865e09a4Sgjelinek 
3592865e09a4Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
3593865e09a4Sgjelinek 		outfile = "/dev/null";
3594865e09a4Sgjelinek 		out_null = B_TRUE;
3595865e09a4Sgjelinek 	}
3596865e09a4Sgjelinek 
35970b5de56dSgjelinek 	/*
35980b5de56dSgjelinek 	 * Use find to get the list of files to copy.  We need to skip
35990b5de56dSgjelinek 	 * files of type "socket" since cpio can't handle those but that
36000b5de56dSgjelinek 	 * should be ok since the app will recreate the socket when it runs.
36010b5de56dSgjelinek 	 * We also need to filter out anything under the .zfs subdir.  Since
36020b5de56dSgjelinek 	 * find is running depth-first, we need the extra egrep to filter .zfs.
36030b5de56dSgjelinek 	 */
3604865e09a4Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
36050b5de56dSgjelinek 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
360607b574eeSgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3607865e09a4Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3608865e09a4Sgjelinek 	    src, dst, outfile);
3609865e09a4Sgjelinek 
3610865e09a4Sgjelinek 	status = do_subproc(cmdbuf);
3611865e09a4Sgjelinek 
36129acbbeafSnn35248 	if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
3613865e09a4Sgjelinek 		if (!out_null)
3614865e09a4Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
3615865e09a4Sgjelinek 			    "More information can be found in %s\n"), outfile);
36169acbbeafSnn35248 		return (Z_ERR);
3617865e09a4Sgjelinek 	}
3618865e09a4Sgjelinek 
3619865e09a4Sgjelinek 	if (!out_null)
3620865e09a4Sgjelinek 		(void) unlink(outfile);
3621865e09a4Sgjelinek 
3622865e09a4Sgjelinek 	return (Z_OK);
3623865e09a4Sgjelinek }
3624865e09a4Sgjelinek 
3625865e09a4Sgjelinek static int
36269acbbeafSnn35248 zone_postclone(char *zonepath)
3627865e09a4Sgjelinek {
36289acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
3629865e09a4Sgjelinek 	int status;
3630123807fbSedp 	brand_handle_t bh;
36319acbbeafSnn35248 	int err = Z_OK;
36325cd08338Sgjelinek 
36335cd08338Sgjelinek 	/*
36349acbbeafSnn35248 	 * Fetch the post-clone command, if any, from the brand
36359acbbeafSnn35248 	 * configuration.
363645916cd2Sjpk 	 */
3637123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
36389acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
36395cd08338Sgjelinek 		return (Z_ERR);
3640865e09a4Sgjelinek 	}
36419acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
3642123807fbSedp 	err = brand_get_postclone(bh, target_zone, zonepath, cmdbuf + EXEC_LEN,
36439acbbeafSnn35248 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL);
3644123807fbSedp 	brand_close(bh);
3645865e09a4Sgjelinek 
36469acbbeafSnn35248 	if (err == 0 && strlen(cmdbuf) > EXEC_LEN) {
36475cd08338Sgjelinek 		status = do_subproc(cmdbuf);
36489acbbeafSnn35248 		if ((err = subproc_status("postclone", status, B_FALSE))
36499acbbeafSnn35248 		    != ZONE_SUBPROC_OK) {
36509acbbeafSnn35248 			zerror(gettext("post-clone configuration failed."));
36519acbbeafSnn35248 			err = Z_ERR;
36529acbbeafSnn35248 		}
3653865e09a4Sgjelinek 	}
3654865e09a4Sgjelinek 
36559acbbeafSnn35248 	return (err);
36565cd08338Sgjelinek }
3657865e09a4Sgjelinek 
3658865e09a4Sgjelinek /* ARGSUSED */
36590b5de56dSgjelinek static int
3660865e09a4Sgjelinek zfm_print(const char *p, void *r) {
3661865e09a4Sgjelinek 	zerror("  %s\n", p);
3662865e09a4Sgjelinek 	return (0);
3663865e09a4Sgjelinek }
3664865e09a4Sgjelinek 
36650b5de56dSgjelinek int
36660b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath)
36670b5de56dSgjelinek {
36680b5de56dSgjelinek 	int err;
36690b5de56dSgjelinek 
36700b5de56dSgjelinek 	/* Don't clone the zone if anything is still mounted there */
36710b5de56dSgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
36720b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
36730b5de56dSgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
36740b5de56dSgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
36750b5de56dSgjelinek 		return (Z_ERR);
36760b5de56dSgjelinek 	}
36770b5de56dSgjelinek 
36780b5de56dSgjelinek 	/*
36790b5de56dSgjelinek 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
36800b5de56dSgjelinek 	 * care if this works or not since we always have the default behavior
36810b5de56dSgjelinek 	 * of a simple directory for the zonepath.
36820b5de56dSgjelinek 	 */
36830b5de56dSgjelinek 	create_zfs_zonepath(zonepath);
36840b5de56dSgjelinek 
36850b5de56dSgjelinek 	(void) printf(gettext("Copying %s..."), source_zonepath);
36860b5de56dSgjelinek 	(void) fflush(stdout);
36870b5de56dSgjelinek 
36880b5de56dSgjelinek 	err = copy_zone(source_zonepath, zonepath);
36890b5de56dSgjelinek 
36900b5de56dSgjelinek 	(void) printf("\n");
36910b5de56dSgjelinek 
36920b5de56dSgjelinek 	return (err);
36930b5de56dSgjelinek }
36940b5de56dSgjelinek 
3695865e09a4Sgjelinek static int
3696865e09a4Sgjelinek clone_func(int argc, char *argv[])
3697865e09a4Sgjelinek {
3698865e09a4Sgjelinek 	char *source_zone = NULL;
3699865e09a4Sgjelinek 	int lockfd;
3700865e09a4Sgjelinek 	int err, arg;
3701865e09a4Sgjelinek 	char zonepath[MAXPATHLEN];
3702865e09a4Sgjelinek 	char source_zonepath[MAXPATHLEN];
3703865e09a4Sgjelinek 	zone_state_t state;
3704865e09a4Sgjelinek 	zone_entry_t *zent;
37050b5de56dSgjelinek 	char *method = NULL;
37060b5de56dSgjelinek 	char *snapshot = NULL;
3707865e09a4Sgjelinek 
3708865e09a4Sgjelinek 	if (zonecfg_in_alt_root()) {
3709865e09a4Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
3710865e09a4Sgjelinek 		return (Z_ERR);
3711865e09a4Sgjelinek 	}
3712865e09a4Sgjelinek 
3713865e09a4Sgjelinek 	optind = 0;
37140b5de56dSgjelinek 	if ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
3715865e09a4Sgjelinek 		switch (arg) {
3716865e09a4Sgjelinek 		case '?':
3717865e09a4Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
3718865e09a4Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
3719865e09a4Sgjelinek 		case 'm':
3720865e09a4Sgjelinek 			method = optarg;
3721865e09a4Sgjelinek 			break;
37220b5de56dSgjelinek 		case 's':
37230b5de56dSgjelinek 			snapshot = optarg;
37240b5de56dSgjelinek 			break;
3725865e09a4Sgjelinek 		default:
3726865e09a4Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
3727865e09a4Sgjelinek 			return (Z_USAGE);
3728865e09a4Sgjelinek 		}
3729865e09a4Sgjelinek 	}
37300b5de56dSgjelinek 	if (argc != (optind + 1) ||
37310b5de56dSgjelinek 	    (method != NULL && strcmp(method, "copy") != 0)) {
3732865e09a4Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
3733865e09a4Sgjelinek 		return (Z_USAGE);
3734865e09a4Sgjelinek 	}
3735865e09a4Sgjelinek 	source_zone = argv[optind];
37369acbbeafSnn35248 	if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, B_FALSE)
37379acbbeafSnn35248 	    != Z_OK)
3738865e09a4Sgjelinek 		return (Z_ERR);
3739*ce28b40eSzt129084 	if (verify_details(CMD_CLONE, argv) != Z_OK)
3740865e09a4Sgjelinek 		return (Z_ERR);
3741865e09a4Sgjelinek 
3742865e09a4Sgjelinek 	/*
3743865e09a4Sgjelinek 	 * We also need to do some extra validation on the source zone.
3744865e09a4Sgjelinek 	 */
3745865e09a4Sgjelinek 	if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
3746865e09a4Sgjelinek 		zerror(gettext("%s operation is invalid for the global zone."),
3747865e09a4Sgjelinek 		    cmd_to_str(CMD_CLONE));
3748865e09a4Sgjelinek 		return (Z_ERR);
3749865e09a4Sgjelinek 	}
3750865e09a4Sgjelinek 
3751865e09a4Sgjelinek 	if (strncmp(source_zone, "SUNW", 4) == 0) {
3752865e09a4Sgjelinek 		zerror(gettext("%s operation is invalid for zones starting "
3753865e09a4Sgjelinek 		    "with SUNW."), cmd_to_str(CMD_CLONE));
3754865e09a4Sgjelinek 		return (Z_ERR);
3755865e09a4Sgjelinek 	}
3756865e09a4Sgjelinek 
3757865e09a4Sgjelinek 	zent = lookup_running_zone(source_zone);
3758865e09a4Sgjelinek 	if (zent != NULL) {
3759865e09a4Sgjelinek 		/* check whether the zone is ready or running */
3760865e09a4Sgjelinek 		if ((err = zone_get_state(zent->zname, &zent->zstate_num))
3761865e09a4Sgjelinek 		    != Z_OK) {
3762865e09a4Sgjelinek 			errno = err;
3763865e09a4Sgjelinek 			zperror2(zent->zname, gettext("could not get state"));
3764865e09a4Sgjelinek 			/* can't tell, so hedge */
3765865e09a4Sgjelinek 			zent->zstate_str = "ready/running";
3766865e09a4Sgjelinek 		} else {
3767865e09a4Sgjelinek 			zent->zstate_str = zone_state_str(zent->zstate_num);
3768865e09a4Sgjelinek 		}
3769865e09a4Sgjelinek 		zerror(gettext("%s operation is invalid for %s zones."),
3770865e09a4Sgjelinek 		    cmd_to_str(CMD_CLONE), zent->zstate_str);
3771865e09a4Sgjelinek 		return (Z_ERR);
3772865e09a4Sgjelinek 	}
3773865e09a4Sgjelinek 
3774865e09a4Sgjelinek 	if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
3775865e09a4Sgjelinek 		errno = err;
3776865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not get state"));
3777865e09a4Sgjelinek 		return (Z_ERR);
3778865e09a4Sgjelinek 	}
3779865e09a4Sgjelinek 	if (state != ZONE_STATE_INSTALLED) {
3780865e09a4Sgjelinek 		(void) fprintf(stderr,
3781865e09a4Sgjelinek 		    gettext("%s: zone %s is %s; %s is required.\n"),
3782865e09a4Sgjelinek 		    execname, source_zone, zone_state_str(state),
3783865e09a4Sgjelinek 		    zone_state_str(ZONE_STATE_INSTALLED));
3784865e09a4Sgjelinek 		return (Z_ERR);
3785865e09a4Sgjelinek 	}
3786865e09a4Sgjelinek 
3787865e09a4Sgjelinek 	/*
3788865e09a4Sgjelinek 	 * The source zone checks out ok, continue with the clone.
3789865e09a4Sgjelinek 	 */
3790865e09a4Sgjelinek 
3791865e09a4Sgjelinek 	if (validate_clone(source_zone, target_zone) != Z_OK)
3792865e09a4Sgjelinek 		return (Z_ERR);
3793865e09a4Sgjelinek 
3794865e09a4Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
3795865e09a4Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
3796865e09a4Sgjelinek 		    "zoneadm");
3797865e09a4Sgjelinek 		return (Z_ERR);
3798865e09a4Sgjelinek 	}
3799865e09a4Sgjelinek 
3800865e09a4Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
3801865e09a4Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
3802865e09a4Sgjelinek 		errno = err;
3803865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
3804865e09a4Sgjelinek 		goto done;
3805865e09a4Sgjelinek 	}
3806865e09a4Sgjelinek 
3807865e09a4Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3808865e09a4Sgjelinek 	    != Z_OK) {
3809865e09a4Sgjelinek 		errno = err;
3810865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
3811865e09a4Sgjelinek 		goto done;
3812865e09a4Sgjelinek 	}
3813865e09a4Sgjelinek 
3814865e09a4Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
3815865e09a4Sgjelinek 	    != Z_OK) {
3816865e09a4Sgjelinek 		errno = err;
3817865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not set state"));
3818865e09a4Sgjelinek 		goto done;
3819865e09a4Sgjelinek 	}
3820865e09a4Sgjelinek 
38210b5de56dSgjelinek 	if (snapshot != NULL) {
38220b5de56dSgjelinek 		err = clone_snapshot_zfs(snapshot, zonepath);
38230b5de56dSgjelinek 	} else {
38240b5de56dSgjelinek 		/*
38250b5de56dSgjelinek 		 * We always copy the clone unless the source is ZFS and a
38260b5de56dSgjelinek 		 * ZFS clone worked.  We fallback to copying if the ZFS clone
38270b5de56dSgjelinek 		 * fails for some reason.
38280b5de56dSgjelinek 		 */
38290b5de56dSgjelinek 		err = Z_ERR;
38300b5de56dSgjelinek 		if (method == NULL && is_zonepath_zfs(source_zonepath))
38310b5de56dSgjelinek 			err = clone_zfs(source_zone, source_zonepath, zonepath);
3832865e09a4Sgjelinek 
38335cd08338Sgjelinek 		if (err != Z_OK)
38340b5de56dSgjelinek 			err = clone_copy(source_zonepath, zonepath);
38350b5de56dSgjelinek 	}
3836865e09a4Sgjelinek 
38379acbbeafSnn35248 	/*
38389acbbeafSnn35248 	 * Trusted Extensions requires that cloned zones use the same sysid
38399acbbeafSnn35248 	 * configuration, so it is not appropriate to perform any
38409acbbeafSnn35248 	 * post-clone reconfiguration.
38419acbbeafSnn35248 	 */
38429acbbeafSnn35248 	if ((err == Z_OK) && !is_system_labeled())
38439acbbeafSnn35248 		err = zone_postclone(zonepath);
3844865e09a4Sgjelinek 
3845865e09a4Sgjelinek done:
38469acbbeafSnn35248 	/*
38479acbbeafSnn35248 	 * If everything went well, we mark the zone as installed.
38489acbbeafSnn35248 	 */
38499acbbeafSnn35248 	if (err == Z_OK) {
38509acbbeafSnn35248 		err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
38519acbbeafSnn35248 		if (err != Z_OK) {
38529acbbeafSnn35248 			errno = err;
38539acbbeafSnn35248 			zperror2(target_zone, gettext("could not set state"));
38549acbbeafSnn35248 		}
38559acbbeafSnn35248 	}
3856865e09a4Sgjelinek 	release_lock_file(lockfd);
3857865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3858865e09a4Sgjelinek }
3859865e09a4Sgjelinek 
386007b574eeSgjelinek /*
38610b5de56dSgjelinek  * Used when removing a zonepath after uninstalling or cleaning up after
38620b5de56dSgjelinek  * the move subcommand.  This handles a zonepath that has non-standard
38630b5de56dSgjelinek  * contents so that we will only cleanup the stuff we know about and leave
38640b5de56dSgjelinek  * any user data alone.
386507b574eeSgjelinek  *
38660b5de56dSgjelinek  * If the "all" parameter is true then we should remove the whole zonepath
38670b5de56dSgjelinek  * even if it has non-standard files/directories in it.  This can be used when
38680b5de56dSgjelinek  * we need to cleanup after moving the zonepath across file systems.
38690b5de56dSgjelinek  *
38700b5de56dSgjelinek  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
38710b5de56dSgjelinek  * and not the shell.
387207b574eeSgjelinek  */
387307b574eeSgjelinek static int
38740b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all)
387507b574eeSgjelinek {
387607b574eeSgjelinek 	int		status;
38770b5de56dSgjelinek 	int		i;
38780b5de56dSgjelinek 	boolean_t	non_std = B_FALSE;
38790b5de56dSgjelinek 	struct dirent	*dp;
38800b5de56dSgjelinek 	DIR		*dirp;
38810b5de56dSgjelinek 	char		*std_entries[] = {"dev", "lu", "root", NULL};
38820b5de56dSgjelinek 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
38830b5de56dSgjelinek 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
388407b574eeSgjelinek 
388507b574eeSgjelinek 	/*
38860b5de56dSgjelinek 	 * We shouldn't need these checks but lets be paranoid since we
38870b5de56dSgjelinek 	 * could blow away the whole system here if we got the wrong zonepath.
388807b574eeSgjelinek 	 */
38890b5de56dSgjelinek 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
38900b5de56dSgjelinek 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
38910b5de56dSgjelinek 		return (Z_INVAL);
38920b5de56dSgjelinek 	}
38930b5de56dSgjelinek 
389407b574eeSgjelinek 	/*
38950b5de56dSgjelinek 	 * If the dirpath is already gone (maybe it was manually removed) then
38960b5de56dSgjelinek 	 * we just return Z_OK so that the cleanup is successful.
389707b574eeSgjelinek 	 */
38980b5de56dSgjelinek 	if ((dirp = opendir(zonepath)) == NULL)
38990b5de56dSgjelinek 		return (Z_OK);
39000b5de56dSgjelinek 
39010b5de56dSgjelinek 	/*
39020b5de56dSgjelinek 	 * Look through the zonepath directory to see if there are any
39030b5de56dSgjelinek 	 * non-standard files/dirs.  Also skip .zfs since that might be
39040b5de56dSgjelinek 	 * there but we'll handle ZFS file systems as a special case.
39050b5de56dSgjelinek 	 */
39060b5de56dSgjelinek 	while ((dp = readdir(dirp)) != NULL) {
39070b5de56dSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
39080b5de56dSgjelinek 		    strcmp(dp->d_name, "..") == 0 ||
39090b5de56dSgjelinek 		    strcmp(dp->d_name, ".zfs") == 0)
39100b5de56dSgjelinek 			continue;
39110b5de56dSgjelinek 
39120b5de56dSgjelinek 		for (i = 0; std_entries[i] != NULL; i++)
39130b5de56dSgjelinek 			if (strcmp(dp->d_name, std_entries[i]) == 0)
39140b5de56dSgjelinek 				break;
39150b5de56dSgjelinek 
39160b5de56dSgjelinek 		if (std_entries[i] == NULL)
39170b5de56dSgjelinek 			non_std = B_TRUE;
39180b5de56dSgjelinek 	}
39190b5de56dSgjelinek 	(void) closedir(dirp);
39200b5de56dSgjelinek 
39210b5de56dSgjelinek 	if (!all && non_std) {
39220b5de56dSgjelinek 		/*
39230b5de56dSgjelinek 		 * There are extra, non-standard directories/files in the
39240b5de56dSgjelinek 		 * zonepath so we don't want to remove the zonepath.  We
39250b5de56dSgjelinek 		 * just want to remove the standard directories and leave
39260b5de56dSgjelinek 		 * the user data alone.
39270b5de56dSgjelinek 		 */
39280b5de56dSgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
39290b5de56dSgjelinek 
39300b5de56dSgjelinek 		for (i = 0; std_entries[i] != NULL; i++) {
39310b5de56dSgjelinek 			char tmpbuf[MAXPATHLEN];
39320b5de56dSgjelinek 
39330b5de56dSgjelinek 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
39340b5de56dSgjelinek 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
39350b5de56dSgjelinek 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
39360b5de56dSgjelinek 			    sizeof (cmdbuf)) {
39370b5de56dSgjelinek 				(void) fprintf(stderr,
39380b5de56dSgjelinek 				    gettext("path is too long\n"));
39390b5de56dSgjelinek 				return (Z_INVAL);
39400b5de56dSgjelinek 			}
394107b574eeSgjelinek 		}
394207b574eeSgjelinek 
394307b574eeSgjelinek 		status = do_subproc(cmdbuf);
394407b574eeSgjelinek 
39450b5de56dSgjelinek 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
39460b5de56dSgjelinek 		    "remove %s\nbecause it contains additional user data.  "
39470b5de56dSgjelinek 		    "Only the standard directory\nentries have been "
39480b5de56dSgjelinek 		    "removed.\n"),
39490b5de56dSgjelinek 		    zonepath);
395007b574eeSgjelinek 
39519acbbeafSnn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39529acbbeafSnn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39530b5de56dSgjelinek 	}
39540b5de56dSgjelinek 
39550b5de56dSgjelinek 	/*
39560b5de56dSgjelinek 	 * There is nothing unexpected in the zonepath, try to get rid of the
39570b5de56dSgjelinek 	 * whole zonepath directory.
39580b5de56dSgjelinek 	 *
39590b5de56dSgjelinek 	 * If the zonepath is its own zfs file system, try to destroy the
39600b5de56dSgjelinek 	 * file system.  If that fails for some reason (e.g. it has clones)
39610b5de56dSgjelinek 	 * then we'll just remove the contents of the zonepath.
39620b5de56dSgjelinek 	 */
39630b5de56dSgjelinek 	if (is_zonepath_zfs(zonepath)) {
39640b5de56dSgjelinek 		if (destroy_zfs(zonepath) == Z_OK)
39650b5de56dSgjelinek 			return (Z_OK);
39660b5de56dSgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
39670b5de56dSgjelinek 		    " %s/*", zonepath);
39680b5de56dSgjelinek 		status = do_subproc(cmdbuf);
39699acbbeafSnn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39709acbbeafSnn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39710b5de56dSgjelinek 	}
39720b5de56dSgjelinek 
39730b5de56dSgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
39740b5de56dSgjelinek 	    zonepath);
39750b5de56dSgjelinek 	status = do_subproc(cmdbuf);
39769acbbeafSnn35248 
39779acbbeafSnn35248 	return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
39789acbbeafSnn35248 	    ? Z_OK : Z_ERR);
397907b574eeSgjelinek }
398007b574eeSgjelinek 
3981865e09a4Sgjelinek static int
3982865e09a4Sgjelinek move_func(int argc, char *argv[])
3983865e09a4Sgjelinek {
3984865e09a4Sgjelinek 	char *new_zonepath = NULL;
3985865e09a4Sgjelinek 	int lockfd;
3986865e09a4Sgjelinek 	int err, arg;
3987865e09a4Sgjelinek 	char zonepath[MAXPATHLEN];
3988865e09a4Sgjelinek 	zone_dochandle_t handle;
3989865e09a4Sgjelinek 	boolean_t fast;
39900b5de56dSgjelinek 	boolean_t is_zfs = B_FALSE;
39910b5de56dSgjelinek 	struct dirent *dp;
39920b5de56dSgjelinek 	DIR *dirp;
39930b5de56dSgjelinek 	boolean_t empty = B_TRUE;
3994865e09a4Sgjelinek 	boolean_t revert;
3995865e09a4Sgjelinek 	struct stat zonepath_buf;
3996865e09a4Sgjelinek 	struct stat new_zonepath_buf;
3997865e09a4Sgjelinek 
3998865e09a4Sgjelinek 	if (zonecfg_in_alt_root()) {
3999865e09a4Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
4000865e09a4Sgjelinek 		return (Z_ERR);
4001865e09a4Sgjelinek 	}
4002865e09a4Sgjelinek 
4003865e09a4Sgjelinek 	optind = 0;
4004865e09a4Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
4005865e09a4Sgjelinek 		switch (arg) {
4006865e09a4Sgjelinek 		case '?':
4007865e09a4Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
4008865e09a4Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
4009865e09a4Sgjelinek 		default:
4010865e09a4Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
4011865e09a4Sgjelinek 			return (Z_USAGE);
4012865e09a4Sgjelinek 		}
4013865e09a4Sgjelinek 	}
4014865e09a4Sgjelinek 	if (argc != (optind + 1)) {
4015865e09a4Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
4016865e09a4Sgjelinek 		return (Z_USAGE);
4017865e09a4Sgjelinek 	}
4018865e09a4Sgjelinek 	new_zonepath = argv[optind];
40199acbbeafSnn35248 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
40209acbbeafSnn35248 	    != Z_OK)
4021865e09a4Sgjelinek 		return (Z_ERR);
4022*ce28b40eSzt129084 	if (verify_details(CMD_MOVE, argv) != Z_OK)
4023865e09a4Sgjelinek 		return (Z_ERR);
4024865e09a4Sgjelinek 
4025865e09a4Sgjelinek 	/*
4026865e09a4Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
4027865e09a4Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
40280b5de56dSgjelinek 	 * stat to see if we are doing a cross file system move or not.
4029865e09a4Sgjelinek 	 */
4030865e09a4Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
4031865e09a4Sgjelinek 		return (Z_ERR);
4032865e09a4Sgjelinek 
4033865e09a4Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4034865e09a4Sgjelinek 	    != Z_OK) {
4035865e09a4Sgjelinek 		errno = err;
4036865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
4037865e09a4Sgjelinek 		return (Z_ERR);
4038865e09a4Sgjelinek 	}
4039865e09a4Sgjelinek 
4040865e09a4Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
4041865e09a4Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
4042865e09a4Sgjelinek 		return (Z_ERR);
4043865e09a4Sgjelinek 	}
4044865e09a4Sgjelinek 
4045865e09a4Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
4046865e09a4Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
4047865e09a4Sgjelinek 		return (Z_ERR);
4048865e09a4Sgjelinek 	}
4049865e09a4Sgjelinek 
40500b5de56dSgjelinek 	/*
40510b5de56dSgjelinek 	 * Check if the destination directory is empty.
40520b5de56dSgjelinek 	 */
40530b5de56dSgjelinek 	if ((dirp = opendir(new_zonepath)) == NULL) {
40540b5de56dSgjelinek 		zperror(gettext("could not open new zone path"), B_FALSE);
40550b5de56dSgjelinek 		return (Z_ERR);
40560b5de56dSgjelinek 	}
40570b5de56dSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
40580b5de56dSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
40590b5de56dSgjelinek 		    strcmp(dp->d_name, "..") == 0)
40600b5de56dSgjelinek 			continue;
40610b5de56dSgjelinek 		empty = B_FALSE;
40620b5de56dSgjelinek 		break;
40630b5de56dSgjelinek 	}
40640b5de56dSgjelinek 	(void) closedir(dirp);
40650b5de56dSgjelinek 
40660b5de56dSgjelinek 	/* Error if there is anything in the destination directory. */
40670b5de56dSgjelinek 	if (!empty) {
40680b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not move zone to %s: "
40690b5de56dSgjelinek 		    "directory not empty\n"), new_zonepath);
40700b5de56dSgjelinek 		return (Z_ERR);
40710b5de56dSgjelinek 	}
40720b5de56dSgjelinek 
4073865e09a4Sgjelinek 	/* Don't move the zone if anything is still mounted there */
4074865e09a4Sgjelinek 	if (zonecfg_find_mounts(zonepath, NULL, NULL)) {
40750b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
4076865e09a4Sgjelinek 		    "subdirectories of %s.\n"), zonepath);
4077865e09a4Sgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4078865e09a4Sgjelinek 		return (Z_ERR);
4079865e09a4Sgjelinek 	}
4080865e09a4Sgjelinek 
4081865e09a4Sgjelinek 	/*
4082865e09a4Sgjelinek 	 * Check if we are moving in the same file system and can do a fast
4083865e09a4Sgjelinek 	 * move or if we are crossing file systems and have to copy the data.
4084865e09a4Sgjelinek 	 */
4085865e09a4Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
4086865e09a4Sgjelinek 
4087865e09a4Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
4088865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
4089865e09a4Sgjelinek 		return (Z_ERR);
4090865e09a4Sgjelinek 	}
4091865e09a4Sgjelinek 
4092865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4093865e09a4Sgjelinek 		errno = err;
4094865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
4095865e09a4Sgjelinek 		zonecfg_fini_handle(handle);
4096865e09a4Sgjelinek 		return (Z_ERR);
4097865e09a4Sgjelinek 	}
4098865e09a4Sgjelinek 
4099865e09a4Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
4100865e09a4Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4101865e09a4Sgjelinek 		    "zoneadm");
4102865e09a4Sgjelinek 		zonecfg_fini_handle(handle);
4103865e09a4Sgjelinek 		return (Z_ERR);
4104865e09a4Sgjelinek 	}
4105865e09a4Sgjelinek 
4106865e09a4Sgjelinek 	/*
41070b5de56dSgjelinek 	 * We're making some file system changes now so we have to clean up
41080b5de56dSgjelinek 	 * the file system before we are done.  This will either clean up the
4109865e09a4Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
4110865e09a4Sgjelinek 	 * old zonepath if everything is ok.
4111865e09a4Sgjelinek 	 */
4112865e09a4Sgjelinek 	revert = B_TRUE;
4113865e09a4Sgjelinek 
41140b5de56dSgjelinek 	if (is_zonepath_zfs(zonepath) &&
41150b5de56dSgjelinek 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
41160b5de56dSgjelinek 		is_zfs = B_TRUE;
41170b5de56dSgjelinek 
41180b5de56dSgjelinek 	} else if (fast) {
4119865e09a4Sgjelinek 		/* same file system, use rename for a quick move */
4120865e09a4Sgjelinek 
4121865e09a4Sgjelinek 		/*
4122865e09a4Sgjelinek 		 * Remove the new_zonepath directory that got created above
4123865e09a4Sgjelinek 		 * during the validation.  It gets in the way of the rename.
4124865e09a4Sgjelinek 		 */
4125865e09a4Sgjelinek 		if (rmdir(new_zonepath) != 0) {
4126865e09a4Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
4127865e09a4Sgjelinek 			    B_FALSE);
4128865e09a4Sgjelinek 			zonecfg_fini_handle(handle);
4129865e09a4Sgjelinek 			release_lock_file(lockfd);
4130865e09a4Sgjelinek 			return (Z_ERR);
4131865e09a4Sgjelinek 		}
4132865e09a4Sgjelinek 
4133865e09a4Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
4134865e09a4Sgjelinek 			/*
4135865e09a4Sgjelinek 			 * If this fails we don't need to do all of the
4136865e09a4Sgjelinek 			 * cleanup that happens for the rest of the code
4137865e09a4Sgjelinek 			 * so just return from this error.
4138865e09a4Sgjelinek 			 */
4139865e09a4Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
4140865e09a4Sgjelinek 			zonecfg_fini_handle(handle);
4141865e09a4Sgjelinek 			release_lock_file(lockfd);
4142865e09a4Sgjelinek 			return (Z_ERR);
4143865e09a4Sgjelinek 		}
4144865e09a4Sgjelinek 
4145865e09a4Sgjelinek 	} else {
41460b5de56dSgjelinek 		/*
41470b5de56dSgjelinek 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
41480b5de56dSgjelinek 		 * we don't care if this works or not since we always have the
41490b5de56dSgjelinek 		 * default behavior of a simple directory for the zonepath.
41500b5de56dSgjelinek 		 */
41510b5de56dSgjelinek 		create_zfs_zonepath(new_zonepath);
41520b5de56dSgjelinek 
4153865e09a4Sgjelinek 		(void) printf(gettext(
41540b5de56dSgjelinek 		    "Moving across file systems; copying zonepath %s..."),
4155865e09a4Sgjelinek 		    zonepath);
4156865e09a4Sgjelinek 		(void) fflush(stdout);
4157865e09a4Sgjelinek 
4158865e09a4Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
4159865e09a4Sgjelinek 
4160865e09a4Sgjelinek 		(void) printf("\n");
4161865e09a4Sgjelinek 		if (err != Z_OK)
4162865e09a4Sgjelinek 			goto done;
4163865e09a4Sgjelinek 	}
4164865e09a4Sgjelinek 
4165865e09a4Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
4166865e09a4Sgjelinek 		errno = err;
4167865e09a4Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
4168865e09a4Sgjelinek 		goto done;
4169865e09a4Sgjelinek 	}
4170865e09a4Sgjelinek 
4171865e09a4Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
4172865e09a4Sgjelinek 		errno = err;
4173865e09a4Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
4174865e09a4Sgjelinek 		goto done;
4175865e09a4Sgjelinek 	}
4176865e09a4Sgjelinek 
4177865e09a4Sgjelinek 	revert = B_FALSE;
4178865e09a4Sgjelinek 
4179865e09a4Sgjelinek done:
4180865e09a4Sgjelinek 	zonecfg_fini_handle(handle);
4181865e09a4Sgjelinek 	release_lock_file(lockfd);
4182865e09a4Sgjelinek 
4183865e09a4Sgjelinek 	/*
41840b5de56dSgjelinek 	 * Clean up the file system based on how things went.  We either
4185865e09a4Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
4186865e09a4Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
4187865e09a4Sgjelinek 	 */
4188865e09a4Sgjelinek 	if (revert) {
4189865e09a4Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
41900b5de56dSgjelinek 		if (is_zfs) {
41910b5de56dSgjelinek 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
41920b5de56dSgjelinek 				(void) fprintf(stderr, gettext("could not "
41930b5de56dSgjelinek 				    "restore zonepath, the zfs mountpoint is "
41940b5de56dSgjelinek 				    "set as:\n%s\n"), new_zonepath);
41950b5de56dSgjelinek 				/*
41960b5de56dSgjelinek 				 * err is already != Z_OK since we're reverting
41970b5de56dSgjelinek 				 */
41980b5de56dSgjelinek 			}
41990b5de56dSgjelinek 
42000b5de56dSgjelinek 		} else if (fast) {
4201865e09a4Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
4202865e09a4Sgjelinek 				zperror(gettext("could not restore zonepath"),
4203865e09a4Sgjelinek 				    B_FALSE);
4204865e09a4Sgjelinek 				/*
4205865e09a4Sgjelinek 				 * err is already != Z_OK since we're reverting
4206865e09a4Sgjelinek 				 */
4207865e09a4Sgjelinek 			}
4208865e09a4Sgjelinek 		} else {
4209865e09a4Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
4210865e09a4Sgjelinek 			    new_zonepath);
4211865e09a4Sgjelinek 			(void) fflush(stdout);
42120b5de56dSgjelinek 			err = cleanup_zonepath(new_zonepath, B_TRUE);
4213865e09a4Sgjelinek 			(void) printf("\n");
4214865e09a4Sgjelinek 
421507b574eeSgjelinek 			if (err != Z_OK) {
4216865e09a4Sgjelinek 				errno = err;
4217865e09a4Sgjelinek 				zperror(gettext("could not remove new "
4218865e09a4Sgjelinek 				    "zonepath"), B_TRUE);
4219865e09a4Sgjelinek 			} else {
4220865e09a4Sgjelinek 				/*
4221865e09a4Sgjelinek 				 * Because we're reverting we know the mainline
4222865e09a4Sgjelinek 				 * code failed but we just reused the err
4223865e09a4Sgjelinek 				 * variable so we reset it back to Z_ERR.
4224865e09a4Sgjelinek 				 */
4225865e09a4Sgjelinek 				err = Z_ERR;
4226865e09a4Sgjelinek 			}
4227865e09a4Sgjelinek 		}
4228865e09a4Sgjelinek 
4229865e09a4Sgjelinek 	} else {
4230865e09a4Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
42310b5de56dSgjelinek 		if (!is_zfs && !fast) {
4232865e09a4Sgjelinek 			(void) printf(
4233865e09a4Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
4234865e09a4Sgjelinek 			(void) fflush(stdout);
42350b5de56dSgjelinek 			err = cleanup_zonepath(zonepath, B_TRUE);
4236865e09a4Sgjelinek 			(void) printf("\n");
4237865e09a4Sgjelinek 
423807b574eeSgjelinek 			if (err != Z_OK) {
4239865e09a4Sgjelinek 				errno = err;
4240865e09a4Sgjelinek 				zperror(gettext("could not remove zonepath"),
4241865e09a4Sgjelinek 				    B_TRUE);
4242865e09a4Sgjelinek 			}
4243865e09a4Sgjelinek 		}
4244865e09a4Sgjelinek 	}
4245865e09a4Sgjelinek 
4246865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4247865e09a4Sgjelinek }
4248865e09a4Sgjelinek 
4249ee519a1fSgjelinek static int
4250ee519a1fSgjelinek detach_func(int argc, char *argv[])
4251ee519a1fSgjelinek {
4252ee519a1fSgjelinek 	int lockfd;
4253ee519a1fSgjelinek 	int err, arg;
4254ee519a1fSgjelinek 	char zonepath[MAXPATHLEN];
4255ee519a1fSgjelinek 	zone_dochandle_t handle;
42568cd327d5Sgjelinek 	boolean_t execute = B_TRUE;
4257ee519a1fSgjelinek 
4258ee519a1fSgjelinek 	if (zonecfg_in_alt_root()) {
4259ee519a1fSgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
4260ee519a1fSgjelinek 		return (Z_ERR);
4261ee519a1fSgjelinek 	}
4262ee519a1fSgjelinek 
4263ee519a1fSgjelinek 	optind = 0;
42648cd327d5Sgjelinek 	if ((arg = getopt(argc, argv, "?n")) != EOF) {
4265ee519a1fSgjelinek 		switch (arg) {
4266ee519a1fSgjelinek 		case '?':
4267ee519a1fSgjelinek 			sub_usage(SHELP_DETACH, CMD_DETACH);
4268ee519a1fSgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
42698cd327d5Sgjelinek 		case 'n':
42708cd327d5Sgjelinek 			execute = B_FALSE;
42718cd327d5Sgjelinek 			break;
4272ee519a1fSgjelinek 		default:
4273ee519a1fSgjelinek 			sub_usage(SHELP_DETACH, CMD_DETACH);
4274ee519a1fSgjelinek 			return (Z_USAGE);
4275ee519a1fSgjelinek 		}
4276ee519a1fSgjelinek 	}
42779acbbeafSnn35248 
42788cd327d5Sgjelinek 	if (execute) {
42799acbbeafSnn35248 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
42809acbbeafSnn35248 		    B_FALSE) != Z_OK)
4281ee519a1fSgjelinek 			return (Z_ERR);
4282*ce28b40eSzt129084 		if (verify_details(CMD_DETACH, argv) != Z_OK)
4283ee519a1fSgjelinek 			return (Z_ERR);
42848cd327d5Sgjelinek 	} else {
42858cd327d5Sgjelinek 		/*
42868cd327d5Sgjelinek 		 * We want a dry-run to work for a non-privileged user so we
42878cd327d5Sgjelinek 		 * only do minimal validation.
42888cd327d5Sgjelinek 		 */
42898cd327d5Sgjelinek 		if (getzoneid() != GLOBAL_ZONEID) {
42908cd327d5Sgjelinek 			zerror(gettext("must be in the global zone to %s a "
42918cd327d5Sgjelinek 			    "zone."), cmd_to_str(CMD_DETACH));
42928cd327d5Sgjelinek 			return (Z_ERR);
42938cd327d5Sgjelinek 		}
42948cd327d5Sgjelinek 
42958cd327d5Sgjelinek 		if (target_zone == NULL) {
42968cd327d5Sgjelinek 			zerror(gettext("no zone specified"));
42978cd327d5Sgjelinek 			return (Z_ERR);
42988cd327d5Sgjelinek 		}
42998cd327d5Sgjelinek 
43008cd327d5Sgjelinek 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
43018cd327d5Sgjelinek 			zerror(gettext("%s operation is invalid for the "
43028cd327d5Sgjelinek 			    "global zone."), cmd_to_str(CMD_DETACH));
43038cd327d5Sgjelinek 			return (Z_ERR);
43048cd327d5Sgjelinek 		}
43058cd327d5Sgjelinek 	}
4306ee519a1fSgjelinek 
4307ee519a1fSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4308ee519a1fSgjelinek 	    != Z_OK) {
4309ee519a1fSgjelinek 		errno = err;
4310ee519a1fSgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
4311ee519a1fSgjelinek 		return (Z_ERR);
4312ee519a1fSgjelinek 	}
4313ee519a1fSgjelinek 
4314ee519a1fSgjelinek 	/* Don't detach the zone if anything is still mounted there */
43158cd327d5Sgjelinek 	if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
43160b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
4317ee519a1fSgjelinek 		    "subdirectories of %s.\n"), zonepath);
4318ee519a1fSgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4319ee519a1fSgjelinek 		return (Z_ERR);
4320ee519a1fSgjelinek 	}
4321ee519a1fSgjelinek 
4322ee519a1fSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
4323ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4324ee519a1fSgjelinek 		return (Z_ERR);
4325ee519a1fSgjelinek 	}
4326ee519a1fSgjelinek 
4327ee519a1fSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4328ee519a1fSgjelinek 		errno = err;
4329ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4330ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
4331ee519a1fSgjelinek 		return (Z_ERR);
4332ee519a1fSgjelinek 	}
4333ee519a1fSgjelinek 
43348cd327d5Sgjelinek 	if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) {
4335ee519a1fSgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4336ee519a1fSgjelinek 		    "zoneadm");
4337ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
4338ee519a1fSgjelinek 		return (Z_ERR);
4339ee519a1fSgjelinek 	}
4340ee519a1fSgjelinek 
4341ee519a1fSgjelinek 	if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) {
4342ee519a1fSgjelinek 		errno = err;
4343ee519a1fSgjelinek 		zperror(gettext("getting the detach information failed"),
4344ee519a1fSgjelinek 		    B_TRUE);
4345ee519a1fSgjelinek 		goto done;
4346ee519a1fSgjelinek 	}
4347ee519a1fSgjelinek 
43488cd327d5Sgjelinek 	if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN)))
43498cd327d5Sgjelinek 	    != Z_OK) {
4350ee519a1fSgjelinek 		errno = err;
4351ee519a1fSgjelinek 		zperror(gettext("saving the detach manifest failed"), B_TRUE);
4352ee519a1fSgjelinek 		goto done;
4353ee519a1fSgjelinek 	}
4354ee519a1fSgjelinek 
43558cd327d5Sgjelinek 	/*
43568cd327d5Sgjelinek 	 * Set the zone state back to configured unless we are running with the
43578cd327d5Sgjelinek 	 * no-execute option.
43588cd327d5Sgjelinek 	 */
43598cd327d5Sgjelinek 	if (execute && (err = zone_set_state(target_zone,
43608cd327d5Sgjelinek 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
4361ee519a1fSgjelinek 		errno = err;
4362ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4363ee519a1fSgjelinek 	}
4364ee519a1fSgjelinek 
4365ee519a1fSgjelinek done:
4366ee519a1fSgjelinek 	zonecfg_fini_handle(handle);
43678cd327d5Sgjelinek 	if (execute)
4368ee519a1fSgjelinek 		release_lock_file(lockfd);
4369ee519a1fSgjelinek 
4370ee519a1fSgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4371ee519a1fSgjelinek }
4372ee519a1fSgjelinek 
4373ee519a1fSgjelinek /*
4374ee519a1fSgjelinek  * During attach we go through and fix up the /dev entries for the zone
4375ee519a1fSgjelinek  * we are attaching.  In order to regenerate /dev with the correct devices,
4376ee519a1fSgjelinek  * the old /dev will be removed, the zone readied (which generates a new
4377ee519a1fSgjelinek  * /dev) then halted, then we use the info from the manifest to update
4378ee519a1fSgjelinek  * the modes, owners, etc. on the new /dev.
4379ee519a1fSgjelinek  */
4380ee519a1fSgjelinek static int
4381ee519a1fSgjelinek dev_fix(zone_dochandle_t handle)
4382ee519a1fSgjelinek {
4383ee519a1fSgjelinek 	int			res;
4384ee519a1fSgjelinek 	int			err;
4385ee519a1fSgjelinek 	int			status;
4386ee519a1fSgjelinek 	struct zone_devpermtab	devtab;
4387ee519a1fSgjelinek 	zone_cmd_arg_t		zarg;
4388ee519a1fSgjelinek 	char			devpath[MAXPATHLEN];
4389ee519a1fSgjelinek 				/* 6: "exec " and " " */
4390ee519a1fSgjelinek 	char			cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
4391ee519a1fSgjelinek 
4392ee519a1fSgjelinek 	if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath)))
4393ee519a1fSgjelinek 	    != Z_OK)
4394ee519a1fSgjelinek 		return (res);
4395ee519a1fSgjelinek 
4396ee519a1fSgjelinek 	if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath))
4397ee519a1fSgjelinek 		return (Z_TOO_BIG);
4398ee519a1fSgjelinek 
4399ee519a1fSgjelinek 	/*
4400ee519a1fSgjelinek 	 * "exec" the command so that the returned status is that of
4401ee519a1fSgjelinek 	 * RMCOMMAND and not the shell.
4402ee519a1fSgjelinek 	 */
44039acbbeafSnn35248 	(void) snprintf(cmdbuf, sizeof (cmdbuf), EXEC_PREFIX RMCOMMAND " %s",
4404ee519a1fSgjelinek 	    devpath);
4405ee519a1fSgjelinek 	status = do_subproc(cmdbuf);
44069acbbeafSnn35248 	if ((err = subproc_status(RMCOMMAND, status, B_TRUE)) !=
44079acbbeafSnn35248 	    ZONE_SUBPROC_OK) {
4408ee519a1fSgjelinek 		(void) fprintf(stderr,
4409ee519a1fSgjelinek 		    gettext("could not remove existing /dev\n"));
4410ee519a1fSgjelinek 		return (Z_ERR);
4411ee519a1fSgjelinek 	}
4412ee519a1fSgjelinek 
4413ee519a1fSgjelinek 	/* In order to ready the zone, it must be in the installed state */
4414ee519a1fSgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4415ee519a1fSgjelinek 		errno = err;
4416ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4417ee519a1fSgjelinek 		return (Z_ERR);
4418ee519a1fSgjelinek 	}
4419ee519a1fSgjelinek 
4420ee519a1fSgjelinek 	/* We have to ready the zone to regen the dev tree */
4421ee519a1fSgjelinek 	zarg.cmd = Z_READY;
4422ee519a1fSgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4423ee519a1fSgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
44240209230bSgjelinek 		/* attempt to restore zone to configured state */
44250209230bSgjelinek 		(void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
4426ee519a1fSgjelinek 		return (Z_ERR);
4427ee519a1fSgjelinek 	}
4428ee519a1fSgjelinek 
4429ee519a1fSgjelinek 	zarg.cmd = Z_HALT;
4430ee519a1fSgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4431ee519a1fSgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
44320209230bSgjelinek 		/* attempt to restore zone to configured state */
44330209230bSgjelinek 		(void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
4434ee519a1fSgjelinek 		return (Z_ERR);
4435ee519a1fSgjelinek 	}
4436ee519a1fSgjelinek 
44370209230bSgjelinek 	/* attempt to restore zone to configured state */
44380209230bSgjelinek 	(void) zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
44390209230bSgjelinek 
4440ee519a1fSgjelinek 	if (zonecfg_setdevperment(handle) != Z_OK) {
4441ee519a1fSgjelinek 		(void) fprintf(stderr,
4442ee519a1fSgjelinek 		    gettext("unable to enumerate device entries\n"));
4443ee519a1fSgjelinek 		return (Z_ERR);
4444ee519a1fSgjelinek 	}
4445ee519a1fSgjelinek 
4446ee519a1fSgjelinek 	while (zonecfg_getdevperment(handle, &devtab) == Z_OK) {
4447ee519a1fSgjelinek 		int err;
4448ee519a1fSgjelinek 
4449ee519a1fSgjelinek 		if ((err = zonecfg_devperms_apply(handle,
4450ee519a1fSgjelinek 		    devtab.zone_devperm_name, devtab.zone_devperm_uid,
4451ee519a1fSgjelinek 		    devtab.zone_devperm_gid, devtab.zone_devperm_mode,
4452ee519a1fSgjelinek 		    devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL)
4453ee519a1fSgjelinek 			(void) fprintf(stderr, gettext("error updating device "
4454ee519a1fSgjelinek 			    "%s: %s\n"), devtab.zone_devperm_name,
4455ee519a1fSgjelinek 			    zonecfg_strerror(err));
4456ee519a1fSgjelinek 
4457ee519a1fSgjelinek 		free(devtab.zone_devperm_acl);
4458ee519a1fSgjelinek 	}
4459ee519a1fSgjelinek 
4460ee519a1fSgjelinek 	(void) zonecfg_enddevperment(handle);
4461ee519a1fSgjelinek 
4462ee519a1fSgjelinek 	return (Z_OK);
4463ee519a1fSgjelinek }
4464ee519a1fSgjelinek 
44658cd327d5Sgjelinek /*
44668cd327d5Sgjelinek  * Validate attaching a zone but don't actually do the work.  The zone
44678cd327d5Sgjelinek  * does not have to exist, so there is some complexity getting a new zone
44688cd327d5Sgjelinek  * configuration set up so that we can perform the validation.  This is
44698cd327d5Sgjelinek  * handled within zonecfg_attach_manifest() which returns two handles; one
44708cd327d5Sgjelinek  * for the the full configuration to validate (rem_handle) and the other
44718cd327d5Sgjelinek  * (local_handle) containing only the zone configuration derived from the
44728cd327d5Sgjelinek  * manifest.
44738cd327d5Sgjelinek  */
44748cd327d5Sgjelinek static int
4475*ce28b40eSzt129084 dryrun_attach(char *manifest_path, char *argv[])
44768cd327d5Sgjelinek {
44778cd327d5Sgjelinek 	int fd;
44788cd327d5Sgjelinek 	int err;
44798cd327d5Sgjelinek 	int res;
44808cd327d5Sgjelinek 	zone_dochandle_t local_handle;
44818cd327d5Sgjelinek 	zone_dochandle_t rem_handle = NULL;
44828cd327d5Sgjelinek 
44838cd327d5Sgjelinek 	if (strcmp(manifest_path, "-") == 0) {
44848cd327d5Sgjelinek 		fd = 0;
44858cd327d5Sgjelinek 	} else if ((fd = open(manifest_path, O_RDONLY)) < 0) {
44868cd327d5Sgjelinek 		zperror(gettext("could not open manifest path"), B_FALSE);
44878cd327d5Sgjelinek 		return (Z_ERR);
44888cd327d5Sgjelinek 	}
44898cd327d5Sgjelinek 
44908cd327d5Sgjelinek 	if ((local_handle = zonecfg_init_handle()) == NULL) {
44918cd327d5Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
44928cd327d5Sgjelinek 		res = Z_ERR;
44938cd327d5Sgjelinek 		goto done;
44948cd327d5Sgjelinek 	}
44958cd327d5Sgjelinek 
44968cd327d5Sgjelinek 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
44978cd327d5Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
44988cd327d5Sgjelinek 		res = Z_ERR;
44998cd327d5Sgjelinek 		goto done;
45008cd327d5Sgjelinek 	}
45018cd327d5Sgjelinek 
45028cd327d5Sgjelinek 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
45038cd327d5Sgjelinek 	    != Z_OK) {
45048cd327d5Sgjelinek 		if (err == Z_INVALID_DOCUMENT)
45058cd327d5Sgjelinek 			zerror(gettext("Cannot attach to an earlier release "
45068cd327d5Sgjelinek 			    "of the operating system"));
45078cd327d5Sgjelinek 		else
45088cd327d5Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
45098cd327d5Sgjelinek 		res = Z_ERR;
45108cd327d5Sgjelinek 		goto done;
45118cd327d5Sgjelinek 	}
45128cd327d5Sgjelinek 
4513e2482d1aSgjelinek 	/*
4514e2482d1aSgjelinek 	 * Retrieve remote handle brand type and determine whether it is
4515e2482d1aSgjelinek 	 * native or not.
4516e2482d1aSgjelinek 	 */
4517e2482d1aSgjelinek 	if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
4518e2482d1aSgjelinek 	    != Z_OK) {
4519e2482d1aSgjelinek 		zerror(gettext("missing or invalid brand"));
4520e2482d1aSgjelinek 		exit(Z_ERR);
4521e2482d1aSgjelinek 	}
4522e2482d1aSgjelinek 	is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0);
4523e2482d1aSgjelinek 
4524*ce28b40eSzt129084 	res = verify_handle(CMD_ATTACH, local_handle, argv);
45258cd327d5Sgjelinek 
45268cd327d5Sgjelinek 	/* Get the detach information for the locally defined zone. */
45278cd327d5Sgjelinek 	if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) {
45288cd327d5Sgjelinek 		errno = err;
45298cd327d5Sgjelinek 		zperror(gettext("getting the attach information failed"),
45308cd327d5Sgjelinek 		    B_TRUE);
45318cd327d5Sgjelinek 		res = Z_ERR;
45328cd327d5Sgjelinek 	} else {
45338cd327d5Sgjelinek 		/* sw_cmp prints error msgs as necessary */
45348cd327d5Sgjelinek 		if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK)
45358cd327d5Sgjelinek 			res = Z_ERR;
45368cd327d5Sgjelinek 	}
45378cd327d5Sgjelinek 
45388cd327d5Sgjelinek done:
45398cd327d5Sgjelinek 	if (strcmp(manifest_path, "-") != 0)
45408cd327d5Sgjelinek 		(void) close(fd);
45418cd327d5Sgjelinek 
45428cd327d5Sgjelinek 	zonecfg_fini_handle(local_handle);
45438cd327d5Sgjelinek 	zonecfg_fini_handle(rem_handle);
45448cd327d5Sgjelinek 
45458cd327d5Sgjelinek 	return ((res == Z_OK) ? Z_OK : Z_ERR);
45468cd327d5Sgjelinek }
45478cd327d5Sgjelinek 
4548ee519a1fSgjelinek static int
4549ee519a1fSgjelinek attach_func(int argc, char *argv[])
4550ee519a1fSgjelinek {
4551ee519a1fSgjelinek 	int lockfd;
4552ee519a1fSgjelinek 	int err, arg;
4553ee519a1fSgjelinek 	boolean_t force = B_FALSE;
4554ee519a1fSgjelinek 	zone_dochandle_t handle;
4555ee519a1fSgjelinek 	zone_dochandle_t athandle = NULL;
4556ee519a1fSgjelinek 	char zonepath[MAXPATHLEN];
45579acbbeafSnn35248 	char brand[MAXNAMELEN], atbrand[MAXNAMELEN];
45588cd327d5Sgjelinek 	boolean_t execute = B_TRUE;
45598cd327d5Sgjelinek 	char *manifest_path;
4560ee519a1fSgjelinek 
4561ee519a1fSgjelinek 	if (zonecfg_in_alt_root()) {
4562ee519a1fSgjelinek 		zerror(gettext("cannot attach zone in alternate root"));
4563ee519a1fSgjelinek 		return (Z_ERR);
4564ee519a1fSgjelinek 	}
4565ee519a1fSgjelinek 
4566ee519a1fSgjelinek 	optind = 0;
45678cd327d5Sgjelinek 	if ((arg = getopt(argc, argv, "?Fn:")) != EOF) {
4568ee519a1fSgjelinek 		switch (arg) {
4569ee519a1fSgjelinek 		case '?':
4570ee519a1fSgjelinek 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
4571ee519a1fSgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
4572ee519a1fSgjelinek 		case 'F':
4573ee519a1fSgjelinek 			force = B_TRUE;
4574ee519a1fSgjelinek 			break;
45758cd327d5Sgjelinek 		case 'n':
45768cd327d5Sgjelinek 			execute = B_FALSE;
45778cd327d5Sgjelinek 			manifest_path = optarg;
45788cd327d5Sgjelinek 			break;
4579ee519a1fSgjelinek 		default:
4580ee519a1fSgjelinek 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
4581ee519a1fSgjelinek 			return (Z_USAGE);
4582ee519a1fSgjelinek 		}
4583ee519a1fSgjelinek 	}
45848cd327d5Sgjelinek 
45858cd327d5Sgjelinek 	/*
45868cd327d5Sgjelinek 	 * If the no-execute option was specified, we need to branch down
45878cd327d5Sgjelinek 	 * a completely different path since there is no zone required to be
45888cd327d5Sgjelinek 	 * configured for this option.
45898cd327d5Sgjelinek 	 */
45908cd327d5Sgjelinek 	if (!execute)
4591*ce28b40eSzt129084 		return (dryrun_attach(manifest_path, argv));
45928cd327d5Sgjelinek 
45939acbbeafSnn35248 	if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE, B_FALSE)
45949acbbeafSnn35248 	    != Z_OK)
4595ee519a1fSgjelinek 		return (Z_ERR);
4596*ce28b40eSzt129084 	if (verify_details(CMD_ATTACH, argv) != Z_OK)
4597ee519a1fSgjelinek 		return (Z_ERR);
4598ee519a1fSgjelinek 
4599ee519a1fSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4600ee519a1fSgjelinek 	    != Z_OK) {
4601ee519a1fSgjelinek 		errno = err;
4602ee519a1fSgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
4603ee519a1fSgjelinek 		return (Z_ERR);
4604ee519a1fSgjelinek 	}
4605ee519a1fSgjelinek 
4606ee519a1fSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
4607ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4608ee519a1fSgjelinek 		return (Z_ERR);
4609ee519a1fSgjelinek 	}
4610ee519a1fSgjelinek 
4611ee519a1fSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4612ee519a1fSgjelinek 		errno = err;
4613ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4614ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
4615ee519a1fSgjelinek 		return (Z_ERR);
4616ee519a1fSgjelinek 	}
4617ee519a1fSgjelinek 
4618ee519a1fSgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
4619ee519a1fSgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4620ee519a1fSgjelinek 		    "zoneadm");
4621ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
4622ee519a1fSgjelinek 		return (Z_ERR);
4623ee519a1fSgjelinek 	}
4624ee519a1fSgjelinek 
4625ee519a1fSgjelinek 	if (force)
4626ee519a1fSgjelinek 		goto forced;
4627ee519a1fSgjelinek 
4628ee519a1fSgjelinek 	if ((athandle = zonecfg_init_handle()) == NULL) {
4629ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4630ee519a1fSgjelinek 		goto done;
4631ee519a1fSgjelinek 	}
4632ee519a1fSgjelinek 
4633ee519a1fSgjelinek 	if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE,
4634ee519a1fSgjelinek 	    athandle)) != Z_OK) {
4635ee519a1fSgjelinek 		if (err == Z_NO_ZONE)
4636ee519a1fSgjelinek 			zerror(gettext("Not a detached zone"));
4637ee519a1fSgjelinek 		else if (err == Z_INVALID_DOCUMENT)
4638ee519a1fSgjelinek 			zerror(gettext("Cannot attach to an earlier release "
4639ee519a1fSgjelinek 			    "of the operating system"));
4640ee519a1fSgjelinek 		else
4641ee519a1fSgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4642ee519a1fSgjelinek 		goto done;
4643ee519a1fSgjelinek 	}
4644ee519a1fSgjelinek 
4645ee519a1fSgjelinek 	/* Get the detach information for the locally defined zone. */
4646ee519a1fSgjelinek 	if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) {
4647ee519a1fSgjelinek 		errno = err;
4648ee519a1fSgjelinek 		zperror(gettext("getting the attach information failed"),
4649ee519a1fSgjelinek 		    B_TRUE);
4650ee519a1fSgjelinek 		goto done;
4651ee519a1fSgjelinek 	}
4652ee519a1fSgjelinek 
46539acbbeafSnn35248 	/*
46549acbbeafSnn35248 	 * Ensure that the detached and locally defined zones are both of
46559acbbeafSnn35248 	 * the same brand.
46569acbbeafSnn35248 	 */
46579acbbeafSnn35248 	if ((zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) ||
46589acbbeafSnn35248 	    (zonecfg_get_brand(athandle, atbrand, sizeof (atbrand)) != 0)) {
46599acbbeafSnn35248 		err = Z_ERR;
46609acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
46619acbbeafSnn35248 		goto done;
46629acbbeafSnn35248 	}
46639acbbeafSnn35248 
46649acbbeafSnn35248 	if (strcmp(atbrand, brand) != NULL) {
46659acbbeafSnn35248 		err = Z_ERR;
46669acbbeafSnn35248 		zerror(gettext("Trying to attach a '%s' zone to a '%s' "
46679acbbeafSnn35248 		    "configuration."), atbrand, brand);
46689acbbeafSnn35248 		goto done;
46699acbbeafSnn35248 	}
46709acbbeafSnn35248 
4671ee519a1fSgjelinek 	/* sw_cmp prints error msgs as necessary */
46720b5de56dSgjelinek 	if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK)
4673ee519a1fSgjelinek 		goto done;
4674ee519a1fSgjelinek 
4675ee519a1fSgjelinek 	if ((err = dev_fix(athandle)) != Z_OK)
4676ee519a1fSgjelinek 		goto done;
4677ee519a1fSgjelinek 
4678ee519a1fSgjelinek forced:
4679ee519a1fSgjelinek 
4680ee519a1fSgjelinek 	zonecfg_rm_detached(handle, force);
4681ee519a1fSgjelinek 
4682ee519a1fSgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4683ee519a1fSgjelinek 		errno = err;
4684ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4685ee519a1fSgjelinek 	}
4686ee519a1fSgjelinek 
4687ee519a1fSgjelinek done:
4688ee519a1fSgjelinek 	zonecfg_fini_handle(handle);
4689ee519a1fSgjelinek 	release_lock_file(lockfd);
4690ee519a1fSgjelinek 	if (athandle != NULL)
4691ee519a1fSgjelinek 		zonecfg_fini_handle(athandle);
4692ee519a1fSgjelinek 
4693ee519a1fSgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4694ee519a1fSgjelinek }
4695ee519a1fSgjelinek 
4696865e09a4Sgjelinek /*
46977c478bd9Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
46987c478bd9Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
46997c478bd9Sstevel@tonic-gate  */
47007c478bd9Sstevel@tonic-gate 
47017c478bd9Sstevel@tonic-gate static int
47027c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
47037c478bd9Sstevel@tonic-gate {
47047c478bd9Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
47057c478bd9Sstevel@tonic-gate 
47067c478bd9Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
47077c478bd9Sstevel@tonic-gate 		return (-1);
47087c478bd9Sstevel@tonic-gate 	for (;;) {
47097c478bd9Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
47107c478bd9Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
47117c478bd9Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
47127c478bd9Sstevel@tonic-gate 		    line[0] == '\n')
47137c478bd9Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
47147c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
47157c478bd9Sstevel@tonic-gate 			return (1);
47167c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
47177c478bd9Sstevel@tonic-gate 			return (0);
47187c478bd9Sstevel@tonic-gate 	}
47197c478bd9Sstevel@tonic-gate }
47207c478bd9Sstevel@tonic-gate 
47217c478bd9Sstevel@tonic-gate static int
47227c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
47237c478bd9Sstevel@tonic-gate {
47247c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
47250b5de56dSgjelinek 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
47267c478bd9Sstevel@tonic-gate 	boolean_t force = B_FALSE;
47277c478bd9Sstevel@tonic-gate 	int lockfd, answer;
47287c478bd9Sstevel@tonic-gate 	int err, arg;
47297c478bd9Sstevel@tonic-gate 
4730108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4731108322fbScarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
4732108322fbScarlsonj 		return (Z_ERR);
4733108322fbScarlsonj 	}
4734108322fbScarlsonj 
47357c478bd9Sstevel@tonic-gate 	optind = 0;
47367c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
47377c478bd9Sstevel@tonic-gate 		switch (arg) {
47387c478bd9Sstevel@tonic-gate 		case '?':
47397c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
47407c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
47417c478bd9Sstevel@tonic-gate 		case 'F':
47427c478bd9Sstevel@tonic-gate 			force = B_TRUE;
47437c478bd9Sstevel@tonic-gate 			break;
47447c478bd9Sstevel@tonic-gate 		default:
47457c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
47467c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
47477c478bd9Sstevel@tonic-gate 		}
47487c478bd9Sstevel@tonic-gate 	}
47497c478bd9Sstevel@tonic-gate 	if (argc > optind) {
47507c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
47517c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
47527c478bd9Sstevel@tonic-gate 	}
47537c478bd9Sstevel@tonic-gate 
47549acbbeafSnn35248 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, B_FALSE)
47559acbbeafSnn35248 	    != Z_OK)
47567c478bd9Sstevel@tonic-gate 		return (Z_ERR);
47577c478bd9Sstevel@tonic-gate 
4758*ce28b40eSzt129084 	/*
4759*ce28b40eSzt129084 	 * Invoke brand-specific handler.
4760*ce28b40eSzt129084 	 */
4761*ce28b40eSzt129084 	if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
4762*ce28b40eSzt129084 		return (Z_ERR);
4763*ce28b40eSzt129084 
47647c478bd9Sstevel@tonic-gate 	if (!force) {
47657c478bd9Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
47667c478bd9Sstevel@tonic-gate 		    gettext("Are you sure you want to %s zone %s"),
47677c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_UNINSTALL), target_zone);
47687c478bd9Sstevel@tonic-gate 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
47697c478bd9Sstevel@tonic-gate 			return (Z_OK);
47707c478bd9Sstevel@tonic-gate 		} else if (answer == -1) {
47717c478bd9Sstevel@tonic-gate 			zerror(gettext("Input not from terminal and -F "
47727c478bd9Sstevel@tonic-gate 			    "not specified: %s not done."),
47737c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL));
47747c478bd9Sstevel@tonic-gate 			return (Z_ERR);
47757c478bd9Sstevel@tonic-gate 		}
47767c478bd9Sstevel@tonic-gate 	}
47777c478bd9Sstevel@tonic-gate 
47780b5de56dSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath,
47790b5de56dSgjelinek 	    sizeof (zonepath))) != Z_OK) {
47807c478bd9Sstevel@tonic-gate 		errno = err;
47817c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
47827c478bd9Sstevel@tonic-gate 		return (Z_ERR);
47837c478bd9Sstevel@tonic-gate 	}
47847c478bd9Sstevel@tonic-gate 	if ((err = zone_get_rootpath(target_zone, rootpath,
47857c478bd9Sstevel@tonic-gate 	    sizeof (rootpath))) != Z_OK) {
47867c478bd9Sstevel@tonic-gate 		errno = err;
47877c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get root path"));
47887c478bd9Sstevel@tonic-gate 		return (Z_ERR);
47897c478bd9Sstevel@tonic-gate 	}
47907c478bd9Sstevel@tonic-gate 
47917c478bd9Sstevel@tonic-gate 	/*
47927c478bd9Sstevel@tonic-gate 	 * If there seems to be a zoneadmd running for this zone, call it
47937c478bd9Sstevel@tonic-gate 	 * to tell it that an uninstall is happening; if all goes well it
47947c478bd9Sstevel@tonic-gate 	 * will then shut itself down.
47957c478bd9Sstevel@tonic-gate 	 */
47967c478bd9Sstevel@tonic-gate 	if (ping_zoneadmd(target_zone) == Z_OK) {
47977c478bd9Sstevel@tonic-gate 		zone_cmd_arg_t zarg;
47987c478bd9Sstevel@tonic-gate 		zarg.cmd = Z_NOTE_UNINSTALLING;
47997c478bd9Sstevel@tonic-gate 		/* we don't care too much if this fails... just plow on */
48007c478bd9Sstevel@tonic-gate 		(void) call_zoneadmd(target_zone, &zarg);
48017c478bd9Sstevel@tonic-gate 	}
48027c478bd9Sstevel@tonic-gate 
48037c478bd9Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
48047c478bd9Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
4805865e09a4Sgjelinek 		    "zoneadm");
48067c478bd9Sstevel@tonic-gate 		return (Z_ERR);
48077c478bd9Sstevel@tonic-gate 	}
48087c478bd9Sstevel@tonic-gate 
48097c478bd9Sstevel@tonic-gate 	/* Don't uninstall the zone if anything is mounted there */
48107c478bd9Sstevel@tonic-gate 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
48117c478bd9Sstevel@tonic-gate 	if (err) {
48120b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
48137c478bd9Sstevel@tonic-gate 		    "subdirectories of %s.\n"), rootpath);
48147c478bd9Sstevel@tonic-gate 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
48157c478bd9Sstevel@tonic-gate 		return (Z_ERR);
48167c478bd9Sstevel@tonic-gate 	}
48177c478bd9Sstevel@tonic-gate 
48187c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
48197c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
48207c478bd9Sstevel@tonic-gate 		errno = err;
48217c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
48227c478bd9Sstevel@tonic-gate 		goto bad;
48237c478bd9Sstevel@tonic-gate 	}
48247c478bd9Sstevel@tonic-gate 
48250b5de56dSgjelinek 	if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
48260b5de56dSgjelinek 		errno = err;
48270b5de56dSgjelinek 		zperror2(target_zone, gettext("cleaning up zonepath failed"));
48287c478bd9Sstevel@tonic-gate 		goto bad;
48290b5de56dSgjelinek 	}
48300b5de56dSgjelinek 
48317c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
48327c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
48337c478bd9Sstevel@tonic-gate 		errno = err;
48347c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
48357c478bd9Sstevel@tonic-gate 	}
48367c478bd9Sstevel@tonic-gate bad:
48377c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
48387c478bd9Sstevel@tonic-gate 	return (err);
48397c478bd9Sstevel@tonic-gate }
48407c478bd9Sstevel@tonic-gate 
4841108322fbScarlsonj /* ARGSUSED */
4842108322fbScarlsonj static int
4843108322fbScarlsonj mount_func(int argc, char *argv[])
4844108322fbScarlsonj {
4845108322fbScarlsonj 	zone_cmd_arg_t zarg;
48469acbbeafSnn35248 	boolean_t force = B_FALSE;
48479acbbeafSnn35248 	int arg;
4848108322fbScarlsonj 
48499acbbeafSnn35248 	/*
48509acbbeafSnn35248 	 * The only supported subargument to the "mount" subcommand is
48519acbbeafSnn35248 	 * "-f", which forces us to mount a zone in the INCOMPLETE state.
48529acbbeafSnn35248 	 */
48539acbbeafSnn35248 	optind = 0;
48549acbbeafSnn35248 	if ((arg = getopt(argc, argv, "f")) != EOF) {
48559acbbeafSnn35248 		switch (arg) {
48569acbbeafSnn35248 		case 'f':
48579acbbeafSnn35248 			force = B_TRUE;
48589acbbeafSnn35248 			break;
48599acbbeafSnn35248 		default:
4860108322fbScarlsonj 			return (Z_USAGE);
48619acbbeafSnn35248 		}
48629acbbeafSnn35248 	}
48639acbbeafSnn35248 	if (argc > optind)
48649acbbeafSnn35248 		return (Z_USAGE);
48659acbbeafSnn35248 
48669acbbeafSnn35248 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
48679acbbeafSnn35248 	    != Z_OK)
4868108322fbScarlsonj 		return (Z_ERR);
4869*ce28b40eSzt129084 	if (verify_details(CMD_MOUNT, argv) != Z_OK)
4870108322fbScarlsonj 		return (Z_ERR);
4871108322fbScarlsonj 
48729acbbeafSnn35248 	zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
4873108322fbScarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4874108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
4875108322fbScarlsonj 		return (Z_ERR);
4876108322fbScarlsonj 	}
4877108322fbScarlsonj 	return (Z_OK);
4878108322fbScarlsonj }
4879108322fbScarlsonj 
4880108322fbScarlsonj /* ARGSUSED */
4881108322fbScarlsonj static int
4882108322fbScarlsonj unmount_func(int argc, char *argv[])
4883108322fbScarlsonj {
4884108322fbScarlsonj 	zone_cmd_arg_t zarg;
4885108322fbScarlsonj 
4886108322fbScarlsonj 	if (argc > 0)
4887108322fbScarlsonj 		return (Z_USAGE);
48889acbbeafSnn35248 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
48899acbbeafSnn35248 	    != Z_OK)
4890108322fbScarlsonj 		return (Z_ERR);
4891108322fbScarlsonj 
4892108322fbScarlsonj 	zarg.cmd = Z_UNMOUNT;
4893108322fbScarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4894108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
4895108322fbScarlsonj 		return (Z_ERR);
4896108322fbScarlsonj 	}
4897108322fbScarlsonj 	return (Z_OK);
4898108322fbScarlsonj }
4899108322fbScarlsonj 
49007c478bd9Sstevel@tonic-gate static int
4901555afedfScarlsonj mark_func(int argc, char *argv[])
4902555afedfScarlsonj {
4903555afedfScarlsonj 	int err, lockfd;
4904555afedfScarlsonj 
4905555afedfScarlsonj 	if (argc != 1 || strcmp(argv[0], "incomplete") != 0)
4906555afedfScarlsonj 		return (Z_USAGE);
49079acbbeafSnn35248 	if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE)
49089acbbeafSnn35248 	    != Z_OK)
4909555afedfScarlsonj 		return (Z_ERR);
4910555afedfScarlsonj 
4911*ce28b40eSzt129084 	/*
4912*ce28b40eSzt129084 	 * Invoke brand-specific handler.
4913*ce28b40eSzt129084 	 */
4914*ce28b40eSzt129084 	if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
4915*ce28b40eSzt129084 		return (Z_ERR);
4916*ce28b40eSzt129084 
4917555afedfScarlsonj 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
4918555afedfScarlsonj 		zerror(gettext("another %s may have an operation in progress."),
4919555afedfScarlsonj 		    "zoneadm");
4920555afedfScarlsonj 		return (Z_ERR);
4921555afedfScarlsonj 	}
4922555afedfScarlsonj 
4923555afedfScarlsonj 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
4924555afedfScarlsonj 	if (err != Z_OK) {
4925555afedfScarlsonj 		errno = err;
4926555afedfScarlsonj 		zperror2(target_zone, gettext("could not set state"));
4927555afedfScarlsonj 	}
4928555afedfScarlsonj 	release_lock_file(lockfd);
4929555afedfScarlsonj 
4930555afedfScarlsonj 	return (err);
4931555afedfScarlsonj }
4932555afedfScarlsonj 
49330209230bSgjelinek /*
49340209230bSgjelinek  * Check what scheduling class we're running under and print a warning if
49350209230bSgjelinek  * we're not using FSS.
49360209230bSgjelinek  */
49370209230bSgjelinek static int
49380209230bSgjelinek check_sched_fss(zone_dochandle_t handle)
49390209230bSgjelinek {
49400209230bSgjelinek 	char class_name[PC_CLNMSZ];
49410209230bSgjelinek 
49420209230bSgjelinek 	if (zonecfg_get_dflt_sched_class(handle, class_name,
49430209230bSgjelinek 	    sizeof (class_name)) != Z_OK) {
49440209230bSgjelinek 		zerror(gettext("WARNING: unable to determine the zone's "
49450209230bSgjelinek 		    "scheduling class"));
49460209230bSgjelinek 	} else if (strcmp("FSS", class_name) != 0) {
49470209230bSgjelinek 		zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
49480209230bSgjelinek 		    "FSS is not the default scheduling class for this zone.  "
49490209230bSgjelinek 		    "FSS will be\nused for processes in the zone but to get "
49500209230bSgjelinek 		    "the full benefit of FSS,\nit should be the default "
49510209230bSgjelinek 		    "scheduling class.  See dispadmin(1M) for\nmore details."));
49520209230bSgjelinek 		return (Z_SYSTEM);
49530209230bSgjelinek 	}
49540209230bSgjelinek 
49550209230bSgjelinek 	return (Z_OK);
49560209230bSgjelinek }
49570209230bSgjelinek 
49580209230bSgjelinek static int
49590209230bSgjelinek check_cpu_shares_sched(zone_dochandle_t handle)
49600209230bSgjelinek {
49610209230bSgjelinek 	int err;
49620209230bSgjelinek 	int res = Z_OK;
49630209230bSgjelinek 	struct zone_rctltab rctl;
49640209230bSgjelinek 
49650209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
49660209230bSgjelinek 		errno = err;
49670209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
49680209230bSgjelinek 		return (err);
49690209230bSgjelinek 	}
49700209230bSgjelinek 
49710209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
49720209230bSgjelinek 		if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
49730209230bSgjelinek 			if (check_sched_fss(handle) != Z_OK)
49740209230bSgjelinek 				res = Z_SYSTEM;
49750209230bSgjelinek 			break;
49760209230bSgjelinek 		}
49770209230bSgjelinek 	}
49780209230bSgjelinek 
49790209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
49800209230bSgjelinek 
49810209230bSgjelinek 	return (res);
49820209230bSgjelinek }
49830209230bSgjelinek 
49840209230bSgjelinek /*
49850209230bSgjelinek  * This is an undocumented interface which is currently only used to apply
49860209230bSgjelinek  * the global zone resource management settings when the system boots.
49870209230bSgjelinek  * This function does not yet properly handle updating a running system so
49880209230bSgjelinek  * any projects running in the zone would be trashed if this function
49890209230bSgjelinek  * were to run after the zone had booted.  It also does not reset any
49900209230bSgjelinek  * rctl settings that were removed from zonecfg.  There is still work to be
49910209230bSgjelinek  * done before we can properly support dynamically updating the resource
49920209230bSgjelinek  * management settings for a running zone (global or non-global).  Thus, this
49930209230bSgjelinek  * functionality is undocumented for now.
49940209230bSgjelinek  */
49950209230bSgjelinek /* ARGSUSED */
49960209230bSgjelinek static int
49970209230bSgjelinek apply_func(int argc, char *argv[])
49980209230bSgjelinek {
49990209230bSgjelinek 	int err;
50000209230bSgjelinek 	int res = Z_OK;
50010209230bSgjelinek 	priv_set_t *privset;
50020209230bSgjelinek 	zoneid_t zoneid;
50030209230bSgjelinek 	zone_dochandle_t handle;
50040209230bSgjelinek 	struct zone_mcaptab mcap;
50050209230bSgjelinek 	char pool_err[128];
50060209230bSgjelinek 
50070209230bSgjelinek 	zoneid = getzoneid();
50080209230bSgjelinek 
50090209230bSgjelinek 	if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
50100209230bSgjelinek 	    target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
50110209230bSgjelinek 		return (usage(B_FALSE));
50120209230bSgjelinek 
50130209230bSgjelinek 	if ((privset = priv_allocset()) == NULL) {
50140209230bSgjelinek 		zerror(gettext("%s failed"), "priv_allocset");
50150209230bSgjelinek 		return (Z_ERR);
50160209230bSgjelinek 	}
50170209230bSgjelinek 
50180209230bSgjelinek 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
50190209230bSgjelinek 		zerror(gettext("%s failed"), "getppriv");
50200209230bSgjelinek 		priv_freeset(privset);
50210209230bSgjelinek 		return (Z_ERR);
50220209230bSgjelinek 	}
50230209230bSgjelinek 
50240209230bSgjelinek 	if (priv_isfullset(privset) == B_FALSE) {
50250209230bSgjelinek 		(void) usage(B_FALSE);
50260209230bSgjelinek 		priv_freeset(privset);
50270209230bSgjelinek 		return (Z_ERR);
50280209230bSgjelinek 	}
50290209230bSgjelinek 	priv_freeset(privset);
50300209230bSgjelinek 
50310209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
50320209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
50330209230bSgjelinek 		return (Z_ERR);
50340209230bSgjelinek 	}
50350209230bSgjelinek 
50360209230bSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
50370209230bSgjelinek 		errno = err;
50380209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
50390209230bSgjelinek 		zonecfg_fini_handle(handle);
50400209230bSgjelinek 		return (Z_ERR);
50410209230bSgjelinek 	}
50420209230bSgjelinek 
50430209230bSgjelinek 	/* specific error msgs are printed within apply_rctls */
50440209230bSgjelinek 	if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
50450209230bSgjelinek 		errno = err;
50460209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
50470209230bSgjelinek 		res = Z_ERR;
50480209230bSgjelinek 	}
50490209230bSgjelinek 
50500209230bSgjelinek 	if ((err = check_cpu_shares_sched(handle)) != Z_OK)
50510209230bSgjelinek 		res = Z_ERR;
50520209230bSgjelinek 
50530209230bSgjelinek 	/*
50540209230bSgjelinek 	 * The next two blocks of code attempt to set up temporary pools as
50550209230bSgjelinek 	 * well as persistent pools.  In both cases we call the functions
50560209230bSgjelinek 	 * unconditionally.  Within each funtion the code will check if the
50570209230bSgjelinek 	 * zone is actually configured for a temporary pool or persistent pool
50580209230bSgjelinek 	 * and just return if there is nothing to do.
50590209230bSgjelinek 	 */
50600209230bSgjelinek 	if ((err = zonecfg_bind_tmp_pool(handle, zoneid, pool_err,
50610209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
50620209230bSgjelinek 		if (err == Z_POOL || err == Z_POOL_CREATE || err == Z_POOL_BIND)
50630209230bSgjelinek 			zerror("%s: %s", zonecfg_strerror(err), pool_err);
50640209230bSgjelinek 		else
50650209230bSgjelinek 			zerror(gettext("could not bind zone to temporary "
50660209230bSgjelinek 			    "pool: %s"), zonecfg_strerror(err));
50670209230bSgjelinek 		res = Z_ERR;
50680209230bSgjelinek 	}
50690209230bSgjelinek 
50700209230bSgjelinek 	if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
50710209230bSgjelinek 	    sizeof (pool_err))) != Z_OK) {
50720209230bSgjelinek 		if (err == Z_POOL || err == Z_POOL_BIND)
50730209230bSgjelinek 			zerror("%s: %s", zonecfg_strerror(err), pool_err);
50740209230bSgjelinek 		else
50750209230bSgjelinek 			zerror("%s", zonecfg_strerror(err));
50760209230bSgjelinek 	}
50770209230bSgjelinek 
50780209230bSgjelinek 	/*
50790209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
50800209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
50810209230bSgjelinek 	 */
50820209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
50830209230bSgjelinek 		uint64_t num;
50840209230bSgjelinek 		char smf_err[128];
50850209230bSgjelinek 
50860209230bSgjelinek 		num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
50870209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
50880209230bSgjelinek 			zerror(gettext("could not set zone memory cap"));
50890209230bSgjelinek 			res = Z_ERR;
50900209230bSgjelinek 		}
50910209230bSgjelinek 
50920209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
50930209230bSgjelinek 			zerror(gettext("enabling system/rcap service failed: "
50940209230bSgjelinek 			    "%s"), smf_err);
50950209230bSgjelinek 			res = Z_ERR;
50960209230bSgjelinek 		}
50970209230bSgjelinek 	}
50980209230bSgjelinek 
50990209230bSgjelinek 	zonecfg_fini_handle(handle);
51000209230bSgjelinek 
51010209230bSgjelinek 	return (res);
51020209230bSgjelinek }
51030209230bSgjelinek 
5104555afedfScarlsonj static int
51057c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[])
51067c478bd9Sstevel@tonic-gate {
51077c478bd9Sstevel@tonic-gate 	int arg, cmd_num;
51087c478bd9Sstevel@tonic-gate 
51097c478bd9Sstevel@tonic-gate 	if (argc == 0) {
51107c478bd9Sstevel@tonic-gate 		(void) usage(B_TRUE);
51117c478bd9Sstevel@tonic-gate 		return (Z_OK);
51127c478bd9Sstevel@tonic-gate 	}
51137c478bd9Sstevel@tonic-gate 	optind = 0;
51147c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
51157c478bd9Sstevel@tonic-gate 		switch (arg) {
51167c478bd9Sstevel@tonic-gate 		case '?':
51177c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
51187c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
51197c478bd9Sstevel@tonic-gate 		default:
51207c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
51217c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
51227c478bd9Sstevel@tonic-gate 		}
51237c478bd9Sstevel@tonic-gate 	}
51247c478bd9Sstevel@tonic-gate 	while (optind < argc) {
5125394a25e2Scarlsonj 		/* Private commands have NULL short_usage; omit them */
5126394a25e2Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5127394a25e2Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
51287c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
51297c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
51307c478bd9Sstevel@tonic-gate 		}
51317c478bd9Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
51327c478bd9Sstevel@tonic-gate 		optind++;
51337c478bd9Sstevel@tonic-gate 	}
51347c478bd9Sstevel@tonic-gate 	return (Z_OK);
51357c478bd9Sstevel@tonic-gate }
51367c478bd9Sstevel@tonic-gate 
51377c478bd9Sstevel@tonic-gate /*
51387c478bd9Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
51397c478bd9Sstevel@tonic-gate  */
51407c478bd9Sstevel@tonic-gate 
51417c478bd9Sstevel@tonic-gate static int
51427c478bd9Sstevel@tonic-gate cmd_match(char *cmd)
51437c478bd9Sstevel@tonic-gate {
51447c478bd9Sstevel@tonic-gate 	int i;
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
51477c478bd9Sstevel@tonic-gate 		/* return only if there is an exact match */
51487c478bd9Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
51497c478bd9Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
51507c478bd9Sstevel@tonic-gate 	}
51517c478bd9Sstevel@tonic-gate 	return (-1);
51527c478bd9Sstevel@tonic-gate }
51537c478bd9Sstevel@tonic-gate 
51547c478bd9Sstevel@tonic-gate static int
51557c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
51567c478bd9Sstevel@tonic-gate {
51577c478bd9Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
51587c478bd9Sstevel@tonic-gate 
51597c478bd9Sstevel@tonic-gate 	if (i < 0)
51607c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
51617c478bd9Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
51627c478bd9Sstevel@tonic-gate }
51637c478bd9Sstevel@tonic-gate 
51647c478bd9Sstevel@tonic-gate static char *
51657c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
51667c478bd9Sstevel@tonic-gate {
51677c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
51687c478bd9Sstevel@tonic-gate 
51697c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
51707c478bd9Sstevel@tonic-gate 	for (;;) {
51717c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
51727c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
51737c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
51747c478bd9Sstevel@tonic-gate 			break;
51757c478bd9Sstevel@tonic-gate 		} else {
51767c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
51777c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
51787c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
51797c478bd9Sstevel@tonic-gate 				continue;
51807c478bd9Sstevel@tonic-gate 			}
51817c478bd9Sstevel@tonic-gate 			break;
51827c478bd9Sstevel@tonic-gate 		}
51837c478bd9Sstevel@tonic-gate 	}
51847c478bd9Sstevel@tonic-gate 	return (execbasename);
51857c478bd9Sstevel@tonic-gate }
51867c478bd9Sstevel@tonic-gate 
51877c478bd9Sstevel@tonic-gate int
51887c478bd9Sstevel@tonic-gate main(int argc, char **argv)
51897c478bd9Sstevel@tonic-gate {
51907c478bd9Sstevel@tonic-gate 	int arg;
51917c478bd9Sstevel@tonic-gate 	zoneid_t zid;
5192108322fbScarlsonj 	struct stat st;
51939acbbeafSnn35248 	char *zone_lock_env;
51949acbbeafSnn35248 	int err;
51957c478bd9Sstevel@tonic-gate 
51967c478bd9Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
51977c478bd9Sstevel@tonic-gate 		locale = "C";
51987c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
51997c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
52007c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
52017c478bd9Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
52027c478bd9Sstevel@tonic-gate 	target_zone = NULL;
52037c478bd9Sstevel@tonic-gate 	if (chdir("/") != 0) {
52047c478bd9Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
52057c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
52067c478bd9Sstevel@tonic-gate 	}
52077c478bd9Sstevel@tonic-gate 
520899653d4eSeschrock 	if (init_zfs() != Z_OK)
520999653d4eSeschrock 		exit(Z_ERR);
521099653d4eSeschrock 
5211555afedfScarlsonj 	while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
52127c478bd9Sstevel@tonic-gate 		switch (arg) {
52137c478bd9Sstevel@tonic-gate 		case '?':
52147c478bd9Sstevel@tonic-gate 			return (usage(B_TRUE));
5215555afedfScarlsonj 		case 'u':
5216555afedfScarlsonj 			target_uuid = optarg;
5217555afedfScarlsonj 			break;
52187c478bd9Sstevel@tonic-gate 		case 'z':
52197c478bd9Sstevel@tonic-gate 			target_zone = optarg;
52207c478bd9Sstevel@tonic-gate 			break;
5221108322fbScarlsonj 		case 'R':	/* private option for admin/install use */
5222108322fbScarlsonj 			if (*optarg != '/') {
5223108322fbScarlsonj 				zerror(gettext("root path must be absolute."));
5224108322fbScarlsonj 				exit(Z_ERR);
5225108322fbScarlsonj 			}
5226108322fbScarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5227108322fbScarlsonj 				zerror(
5228108322fbScarlsonj 				    gettext("root path must be a directory."));
5229108322fbScarlsonj 				exit(Z_ERR);
5230108322fbScarlsonj 			}
5231108322fbScarlsonj 			zonecfg_set_root(optarg);
5232108322fbScarlsonj 			break;
52337c478bd9Sstevel@tonic-gate 		default:
52347c478bd9Sstevel@tonic-gate 			return (usage(B_FALSE));
52357c478bd9Sstevel@tonic-gate 		}
52367c478bd9Sstevel@tonic-gate 	}
52377c478bd9Sstevel@tonic-gate 
52387c478bd9Sstevel@tonic-gate 	if (optind >= argc)
52397c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
5240555afedfScarlsonj 
5241555afedfScarlsonj 	if (target_uuid != NULL && *target_uuid != '\0') {
5242555afedfScarlsonj 		uuid_t uuid;
5243555afedfScarlsonj 		static char newtarget[ZONENAME_MAX];
5244555afedfScarlsonj 
5245555afedfScarlsonj 		if (uuid_parse(target_uuid, uuid) == -1) {
5246555afedfScarlsonj 			zerror(gettext("illegal UUID value specified"));
5247555afedfScarlsonj 			exit(Z_ERR);
5248555afedfScarlsonj 		}
5249555afedfScarlsonj 		if (zonecfg_get_name_by_uuid(uuid, newtarget,
5250555afedfScarlsonj 		    sizeof (newtarget)) == Z_OK)
5251555afedfScarlsonj 			target_zone = newtarget;
5252555afedfScarlsonj 	}
5253555afedfScarlsonj 
52547c478bd9Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
52557c478bd9Sstevel@tonic-gate 		errno = Z_NO_ZONE;
52567c478bd9Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
52577c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
52587c478bd9Sstevel@tonic-gate 	}
52599acbbeafSnn35248 
52609acbbeafSnn35248 	/*
52619acbbeafSnn35248 	 * See if we have inherited the right to manipulate this zone from
52629acbbeafSnn35248 	 * a zoneadm instance in our ancestry.  If so, set zone_lock_cnt to
52639acbbeafSnn35248 	 * indicate it.  If not, make that explicit in our environment.
52649acbbeafSnn35248 	 */
52659acbbeafSnn35248 	zone_lock_env = getenv(LOCK_ENV_VAR);
52669acbbeafSnn35248 	if (zone_lock_env == NULL) {
52679acbbeafSnn35248 		if (putenv(zoneadm_lock_not_held) != 0) {
52689acbbeafSnn35248 			zperror(target_zone, B_TRUE);
52699acbbeafSnn35248 			exit(Z_ERR);
52709acbbeafSnn35248 		}
52719acbbeafSnn35248 	} else {
52729acbbeafSnn35248 		zoneadm_is_nested = B_TRUE;
52739acbbeafSnn35248 		if (atoi(zone_lock_env) == 1)
52749acbbeafSnn35248 			zone_lock_cnt = 1;
52759acbbeafSnn35248 	}
52769acbbeafSnn35248 
52779acbbeafSnn35248 	/*
52789acbbeafSnn35248 	 * If we are going to be operating on a single zone, retrieve its
52799acbbeafSnn35248 	 * brand type and determine whether it is native or not.
52809acbbeafSnn35248 	 */
52819acbbeafSnn35248 	if ((target_zone != NULL) &&
52829acbbeafSnn35248 	    (strcmp(target_zone, GLOBAL_ZONENAME) != NULL)) {
52839acbbeafSnn35248 		if (zone_get_brand(target_zone, target_brand,
52849acbbeafSnn35248 		    sizeof (target_brand)) != Z_OK) {
52859acbbeafSnn35248 			zerror(gettext("missing or invalid brand"));
52869acbbeafSnn35248 			exit(Z_ERR);
52879acbbeafSnn35248 		}
52889acbbeafSnn35248 		is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0);
52899acbbeafSnn35248 	}
52909acbbeafSnn35248 
52919acbbeafSnn35248 	err = parse_and_run(argc - optind, &argv[optind]);
52929acbbeafSnn35248 
52939acbbeafSnn35248 	return (err);
52947c478bd9Sstevel@tonic-gate }
5295