xref: /illumos-gate/usr/src/cmd/zoneadm/zoneadm.c (revision bafa7067034145d6897f548d5995b7b10ad8bc9b)
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>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #include <pool.h>
797c478bd9Sstevel@tonic-gate #include <sys/pool.h>
807c478bd9Sstevel@tonic-gate 
810b5de56dSgjelinek #include "zoneadm.h"
820b5de56dSgjelinek 
837c478bd9Sstevel@tonic-gate #define	MAXARGS	8
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */
867c478bd9Sstevel@tonic-gate typedef struct zone_entry {
877c478bd9Sstevel@tonic-gate 	zoneid_t	zid;
887c478bd9Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
897c478bd9Sstevel@tonic-gate 	char		*zstate_str;
907c478bd9Sstevel@tonic-gate 	zone_state_t	zstate_num;
919acbbeafSnn35248 	char		zbrand[MAXNAMELEN];
927c478bd9Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
93555afedfScarlsonj 	char		zuuid[UUID_PRINTABLE_STRING_LENGTH];
947c478bd9Sstevel@tonic-gate } zone_entry_t;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static zone_entry_t *zents;
977c478bd9Sstevel@tonic-gate static size_t nzents;
989acbbeafSnn35248 static boolean_t is_native_zone = B_TRUE;
997c478bd9Sstevel@tonic-gate 
1001390a385Sgjelinek #define	LOOPBACK_IF	"lo0"
1011390a385Sgjelinek #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
1021390a385Sgjelinek 
1031390a385Sgjelinek struct net_if {
1041390a385Sgjelinek 	char	*name;
1051390a385Sgjelinek 	int	af;
1061390a385Sgjelinek };
1071390a385Sgjelinek 
1087c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
1097c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1107c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate struct cmd {
1137c478bd9Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1147c478bd9Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1157c478bd9Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1167c478bd9Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate };
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #define	SHELP_HELP	"help"
1213f2f09c1Sdp #define	SHELP_BOOT	"boot [-- boot_arguments]"
1227c478bd9Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1237c478bd9Sstevel@tonic-gate #define	SHELP_READY	"ready"
1243f2f09c1Sdp #define	SHELP_REBOOT	"reboot [-- boot_arguments]"
1257c478bd9Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1267c478bd9Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
1279acbbeafSnn35248 #define	SHELP_INSTALL	"install [-x nodataset] [brand-specific args]"
1287c478bd9Sstevel@tonic-gate #define	SHELP_UNINSTALL	"uninstall [-F]"
1290b5de56dSgjelinek #define	SHELP_CLONE	"clone [-m method] [-s <ZFS snapshot>] zonename"
130865e09a4Sgjelinek #define	SHELP_MOVE	"move zonepath"
1318cd327d5Sgjelinek #define	SHELP_DETACH	"detach [-n]"
1328cd327d5Sgjelinek #define	SHELP_ATTACH	"attach [-F] [-n <path>]"
133555afedfScarlsonj #define	SHELP_MARK	"mark incomplete"
1347c478bd9Sstevel@tonic-gate 
1359acbbeafSnn35248 #define	EXEC_PREFIX	"exec "
1369acbbeafSnn35248 #define	EXEC_LEN	(strlen(EXEC_PREFIX))
1379acbbeafSnn35248 #define	RMCOMMAND	"/usr/bin/rm -rf"
1389acbbeafSnn35248 
1399acbbeafSnn35248 static int cleanup_zonepath(char *, boolean_t);
1409acbbeafSnn35248 
1417c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1427c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1437c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1447c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1457c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1467c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1477c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1487c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1497c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
150108322fbScarlsonj static int mount_func(int argc, char *argv[]);
151108322fbScarlsonj static int unmount_func(int argc, char *argv[]);
152865e09a4Sgjelinek static int clone_func(int argc, char *argv[]);
153865e09a4Sgjelinek static int move_func(int argc, char *argv[]);
154ee519a1fSgjelinek static int detach_func(int argc, char *argv[]);
155ee519a1fSgjelinek static int attach_func(int argc, char *argv[]);
156555afedfScarlsonj static int mark_func(int argc, char *argv[]);
1577c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1589acbbeafSnn35248     boolean_t unsafe_when_running, boolean_t force);
1597c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd);
1607c478bd9Sstevel@tonic-gate static int verify_details(int);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = {
1637c478bd9Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1647c478bd9Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1657c478bd9Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1667c478bd9Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1677c478bd9Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1687c478bd9Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1697c478bd9Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1707c478bd9Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1717c478bd9Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
172108322fbScarlsonj 	    uninstall_func },
173865e09a4Sgjelinek 	/* mount and unmount are private commands for admin/install */
174108322fbScarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
175865e09a4Sgjelinek 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
176865e09a4Sgjelinek 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
177ee519a1fSgjelinek 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
178ee519a1fSgjelinek 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
179555afedfScarlsonj 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func },
180555afedfScarlsonj 	{ CMD_MARK,		"mark",		SHELP_MARK,	mark_func }
1817c478bd9Sstevel@tonic-gate };
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /* global variables */
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
1867c478bd9Sstevel@tonic-gate static char *execname;
1879acbbeafSnn35248 static char target_brand[MAXNAMELEN];
1887c478bd9Sstevel@tonic-gate static char *locale;
1890b5de56dSgjelinek char *target_zone;
190555afedfScarlsonj static char *target_uuid;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /* used in do_subproc() and signal handler */
1937c478bd9Sstevel@tonic-gate static volatile boolean_t child_killed;
1949acbbeafSnn35248 static int do_subproc_cnt = 0;
1959acbbeafSnn35248 
1969acbbeafSnn35248 /*
1979acbbeafSnn35248  * Used to indicate whether this zoneadm instance has another zoneadm
1989acbbeafSnn35248  * instance in its ancestry.
1999acbbeafSnn35248  */
2009acbbeafSnn35248 static boolean_t zoneadm_is_nested = B_FALSE;
2019acbbeafSnn35248 
2029acbbeafSnn35248 /* used to track nested zone-lock operations */
2039acbbeafSnn35248 static int zone_lock_cnt = 0;
2049acbbeafSnn35248 
2059acbbeafSnn35248 /* used to communicate lock status to children */
2069acbbeafSnn35248 #define	LOCK_ENV_VAR	"_ZONEADM_LOCK_HELD"
2079acbbeafSnn35248 static char zoneadm_lock_held[] = LOCK_ENV_VAR"=1";
2089acbbeafSnn35248 static char zoneadm_lock_not_held[] = LOCK_ENV_VAR"=0";
2097c478bd9Sstevel@tonic-gate 
2100b5de56dSgjelinek char *
2117c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2147c478bd9Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
2187c478bd9Sstevel@tonic-gate static char *
2197c478bd9Sstevel@tonic-gate long_help(int cmd_num)
2207c478bd9Sstevel@tonic-gate {
2217e362f58Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2227c478bd9Sstevel@tonic-gate 	switch (cmd_num) {
2237c478bd9Sstevel@tonic-gate 	case CMD_HELP:
2247c478bd9Sstevel@tonic-gate 		return (gettext("Print usage message."));
2257c478bd9Sstevel@tonic-gate 	case CMD_BOOT:
2263f2f09c1Sdp 		return (gettext("Activates (boots) specified zone.  See "
2273f2f09c1Sdp 		    "zoneadm(1m) for valid boot\n\targuments."));
2287c478bd9Sstevel@tonic-gate 	case CMD_HALT:
2299e518655Sgjelinek 		return (gettext("Halts specified zone, bypassing shutdown "
2309e518655Sgjelinek 		    "scripts and removing runtime\n\tresources of the zone."));
2317c478bd9Sstevel@tonic-gate 	case CMD_READY:
2329e518655Sgjelinek 		return (gettext("Prepares a zone for running applications but "
2339e518655Sgjelinek 		    "does not start any user\n\tprocesses in the zone."));
2347c478bd9Sstevel@tonic-gate 	case CMD_REBOOT:
2359e518655Sgjelinek 		return (gettext("Restarts the zone (equivalent to a halt / "
2363f2f09c1Sdp 		    "boot sequence).\n\tFails if the zone is not active.  "
2373f2f09c1Sdp 		    "See zoneadm(1m) for valid boot\n\targuments."));
2387c478bd9Sstevel@tonic-gate 	case CMD_LIST:
2397c478bd9Sstevel@tonic-gate 		return (gettext("Lists the current zones, or a "
2407c478bd9Sstevel@tonic-gate 		    "specific zone if indicated.  By default,\n\tall "
2417c478bd9Sstevel@tonic-gate 		    "running zones are listed, though this can be "
2427c478bd9Sstevel@tonic-gate 		    "expanded to all\n\tinstalled zones with the -i "
2437c478bd9Sstevel@tonic-gate 		    "option or all configured zones with the\n\t-c "
244555afedfScarlsonj 		    "option.  When used with the general -z <zone> and/or -u "
245555afedfScarlsonj 		    "<uuid-match>\n\toptions, lists only the specified "
246555afedfScarlsonj 		    "matching zone, but lists it\n\tregardless of its state, "
247555afedfScarlsonj 		    "and the -i and -c options are disallowed.  The\n\t-v "
248555afedfScarlsonj 		    "option can be used to display verbose information: zone "
249555afedfScarlsonj 		    "name, id,\n\tcurrent state, root directory and options.  "
250555afedfScarlsonj 		    "The -p option can be used\n\tto request machine-parsable "
251555afedfScarlsonj 		    "output.  The -v and -p options are mutually\n\texclusive."
252555afedfScarlsonj 		    "  If neither -v nor -p is used, just the zone name is "
253555afedfScarlsonj 		    "listed."));
2547c478bd9Sstevel@tonic-gate 	case CMD_VERIFY:
2557c478bd9Sstevel@tonic-gate 		return (gettext("Check to make sure the configuration "
2567c478bd9Sstevel@tonic-gate 		    "can safely be instantiated\n\ton the machine: "
2577c478bd9Sstevel@tonic-gate 		    "physical network interfaces exist, etc."));
2587c478bd9Sstevel@tonic-gate 	case CMD_INSTALL:
2590b5de56dSgjelinek 		return (gettext("Install the configuration on to the system.  "
2600b5de56dSgjelinek 		    "The -x nodataset option\n\tcan be used to prevent the "
2610b5de56dSgjelinek 		    "creation of a new ZFS file system for the\n\tzone "
2629acbbeafSnn35248 		    "(assuming the zonepath is within a ZFS file system).\n\t"
2639acbbeafSnn35248 		    "All other arguments are passed to the brand installation "
2649acbbeafSnn35248 		    "function;\n\tsee brand(4) for more information."));
2657c478bd9Sstevel@tonic-gate 	case CMD_UNINSTALL:
2669e518655Sgjelinek 		return (gettext("Uninstall the configuration from the system.  "
2679e518655Sgjelinek 		    "The -F flag can be used\n\tto force the action."));
268865e09a4Sgjelinek 	case CMD_CLONE:
2690b5de56dSgjelinek 		return (gettext("Clone the installation of another zone.  "
2700b5de56dSgjelinek 		    "The -m option can be used to\n\tspecify 'copy' which "
2710b5de56dSgjelinek 		    "forces a copy of the source zone.  The -s option\n\t"
2720b5de56dSgjelinek 		    "can be used to specify the name of a ZFS snapshot "
2730b5de56dSgjelinek 		    "that was taken from\n\ta previous clone command.  The "
2740b5de56dSgjelinek 		    "snapshot will be used as the source\n\tinstead of "
2750b5de56dSgjelinek 		    "creating a new ZFS snapshot."));
276865e09a4Sgjelinek 	case CMD_MOVE:
277865e09a4Sgjelinek 		return (gettext("Move the zone to a new zonepath."));
2789e518655Sgjelinek 	case CMD_DETACH:
2799e518655Sgjelinek 		return (gettext("Detach the zone from the system. The zone "
2809e518655Sgjelinek 		    "state is changed to\n\t'configured' (but the files under "
2819e518655Sgjelinek 		    "the zonepath are untouched).\n\tThe zone can subsequently "
2829e518655Sgjelinek 		    "be attached, or can be moved to another\n\tsystem and "
2838cd327d5Sgjelinek 		    "attached there.  The -n option can be used to specify\n\t"
2848cd327d5Sgjelinek 		    "'no-execute' mode.  When -n is used, the information "
2858cd327d5Sgjelinek 		    "needed to attach\n\tthe zone is sent to standard output "
2868cd327d5Sgjelinek 		    "but the zone is not actually\n\tdetached."));
2879e518655Sgjelinek 	case CMD_ATTACH:
2889e518655Sgjelinek 		return (gettext("Attach the zone to the system.  The zone "
2899e518655Sgjelinek 		    "state must be 'configured'\n\tprior to attach; upon "
2909e518655Sgjelinek 		    "successful completion, the zone state will be\n\t"
2919e518655Sgjelinek 		    "'installed'.  The system software on the current "
2929e518655Sgjelinek 		    "system must be\n\tcompatible with the software on the "
2939e518655Sgjelinek 		    "zone's original system.\n\tSpecify -F to force the attach "
2948cd327d5Sgjelinek 		    "and skip software compatibility tests.\n\tThe -n option "
2958cd327d5Sgjelinek 		    "can be used to specify 'no-execute' mode.  When -n is\n\t"
2968cd327d5Sgjelinek 		    "used, the information needed to attach the zone is read "
2978cd327d5Sgjelinek 		    "from the\n\tspecified path and the configuration is only "
2988cd327d5Sgjelinek 		    "validated.  The path can\n\tbe '-' to specify standard "
2998cd327d5Sgjelinek 		    "input."));
300555afedfScarlsonj 	case CMD_MARK:
301555afedfScarlsonj 		return (gettext("Set the state of the zone.  This can be used "
302555afedfScarlsonj 		    "to force the zone\n\tstate to 'incomplete' "
303555afedfScarlsonj 		    "administratively if some activity has rendered\n\tthe "
304555afedfScarlsonj 		    "zone permanently unusable.  The only valid state that "
305555afedfScarlsonj 		    "may be\n\tspecified is 'incomplete'."));
306108322fbScarlsonj 	default:
307108322fbScarlsonj 		return ("");
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3107e362f58Scomay 	return (NULL);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
3157c478bd9Sstevel@tonic-gate  * unexpected errors.
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate static int
3197c478bd9Sstevel@tonic-gate usage(boolean_t explicit)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	int i;
3227c478bd9Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
325555afedfScarlsonj 	(void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
326555afedfScarlsonj 	    execname);
327555afedfScarlsonj 	(void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
3287c478bd9Sstevel@tonic-gate 	    gettext("subcommand"));
3297c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
3307c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
331108322fbScarlsonj 		if (cmdtab[i].short_usage == NULL)
332108322fbScarlsonj 			continue;
3337c478bd9Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
3347c478bd9Sstevel@tonic-gate 		if (explicit)
3357c478bd9Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 	if (!explicit)
3387c478bd9Sstevel@tonic-gate 		(void) fputs("\n", fd);
3397c478bd9Sstevel@tonic-gate 	return (Z_USAGE);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate static void
3437c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
3467c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
3517c478bd9Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
3527c478bd9Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate 
3550b5de56dSgjelinek void
3567c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
3597c478bd9Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
3647c478bd9Sstevel@tonic-gate  * supplied zone name after the executable.
3657c478bd9Sstevel@tonic-gate  *
3667c478bd9Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
3677c478bd9Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
3687c478bd9Sstevel@tonic-gate  * like zperror() above.
3697c478bd9Sstevel@tonic-gate  */
3707c478bd9Sstevel@tonic-gate 
3710b5de56dSgjelinek void
3727c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3757c478bd9Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
3790b5de56dSgjelinek void
3807c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	va_list alist;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
3857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3867c478bd9Sstevel@tonic-gate 	if (target_zone != NULL)
3877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3887c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3897c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3907c478bd9Sstevel@tonic-gate 	va_end(alist);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate static void *
3947c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (r == NULL) {
3997c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
4007c478bd9Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
4017c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 	return (r);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate static void
4077c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	assert(!(verbose && parsable));
4127c478bd9Sstevel@tonic-gate 	if (firsttime && verbose) {
4137c478bd9Sstevel@tonic-gate 		firsttime = B_FALSE;
4149acbbeafSnn35248 		(void) printf("%*s %-16s %-14s %-30s %-10s\n", ZONEID_WIDTH,
4159acbbeafSnn35248 		    "ID", "NAME", "STATUS", "PATH", "BRAND");
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 	if (!verbose) {
418555afedfScarlsonj 		char *cp, *clim;
419555afedfScarlsonj 
4207c478bd9Sstevel@tonic-gate 		if (!parsable) {
4217c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
4227c478bd9Sstevel@tonic-gate 			return;
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4257c478bd9Sstevel@tonic-gate 			(void) printf("-");
4267c478bd9Sstevel@tonic-gate 		else
4277c478bd9Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
428555afedfScarlsonj 		(void) printf(":%s:%s:", zent->zname, zent->zstate_str);
429555afedfScarlsonj 		cp = zent->zroot;
430555afedfScarlsonj 		while ((clim = strchr(cp, ':')) != NULL) {
431555afedfScarlsonj 			(void) printf("%.*s\\:", clim - cp, cp);
432555afedfScarlsonj 			cp = clim + 1;
433555afedfScarlsonj 		}
4349acbbeafSnn35248 		(void) printf("%s:%s:%s\n", cp, zent->zuuid, zent->zbrand);
4357c478bd9Sstevel@tonic-gate 		return;
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
4387c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4397c478bd9Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
4407c478bd9Sstevel@tonic-gate 		else
4417c478bd9Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
4429acbbeafSnn35248 		(void) printf(" %-16s %-14s %-30s %-10s\n", zent->zname,
4439acbbeafSnn35248 		    zent->zstate_str, zent->zroot, zent->zbrand);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate static int
448108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
4497c478bd9Sstevel@tonic-gate {
45045916cd2Sjpk 	char root[MAXPATHLEN], *cp;
4517c478bd9Sstevel@tonic-gate 	int err;
452555afedfScarlsonj 	uuid_t uuid;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
4557c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
4569acbbeafSnn35248 	(void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
4577c478bd9Sstevel@tonic-gate 	zent->zstate_str = "???";
4587c478bd9Sstevel@tonic-gate 
459108322fbScarlsonj 	zent->zid = zid;
4607c478bd9Sstevel@tonic-gate 
461555afedfScarlsonj 	if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
462555afedfScarlsonj 	    !uuid_is_null(uuid))
463555afedfScarlsonj 		uuid_unparse(uuid, zent->zuuid);
464555afedfScarlsonj 	else
465555afedfScarlsonj 		zent->zuuid[0] = '\0';
466555afedfScarlsonj 
46745916cd2Sjpk 	/*
46845916cd2Sjpk 	 * For labeled zones which query the zone path of lower-level
46945916cd2Sjpk 	 * zones, the path needs to be adjusted to drop the final
47045916cd2Sjpk 	 * "/root" component. This adjusted path is then useful
47145916cd2Sjpk 	 * for reading down any exported directories from the
47245916cd2Sjpk 	 * lower-level zone.
47345916cd2Sjpk 	 */
47445916cd2Sjpk 	if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
47545916cd2Sjpk 		if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
47645916cd2Sjpk 		    sizeof (zent->zroot)) == -1) {
47745916cd2Sjpk 			zperror2(zent->zname,
47845916cd2Sjpk 			    gettext("could not get zone path."));
47945916cd2Sjpk 			return (Z_ERR);
48045916cd2Sjpk 		}
48145916cd2Sjpk 		cp = zent->zroot + strlen(zent->zroot) - 5;
48245916cd2Sjpk 		if (cp > zent->zroot && strcmp(cp, "/root") == 0)
48345916cd2Sjpk 			*cp = 0;
48445916cd2Sjpk 	} else {
48545916cd2Sjpk 		if ((err = zone_get_zonepath(zent->zname, root,
48645916cd2Sjpk 		    sizeof (root))) != Z_OK) {
4877c478bd9Sstevel@tonic-gate 			errno = err;
48845916cd2Sjpk 			zperror2(zent->zname,
48945916cd2Sjpk 			    gettext("could not get zone path."));
4907c478bd9Sstevel@tonic-gate 			return (Z_ERR);
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 		(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
49345916cd2Sjpk 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
4967c478bd9Sstevel@tonic-gate 		errno = err;
4977c478bd9Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
4987c478bd9Sstevel@tonic-gate 		return (Z_ERR);
4997c478bd9Sstevel@tonic-gate 	}
5007c478bd9Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
501*bafa7067Snn35248 
502*bafa7067Snn35248 	/*
503*bafa7067Snn35248 	 * A zone's brand is only available in the .xml file describing it,
504*bafa7067Snn35248 	 * which is only visible to the global zone.  This causes
505*bafa7067Snn35248 	 * zone_get_brand() to fail when called from within a non-global
506*bafa7067Snn35248 	 * zone.  Fortunately we only do this on labeled systems, where we
507*bafa7067Snn35248 	 * know all zones are native.
508*bafa7067Snn35248 	 */
509*bafa7067Snn35248 	if (getzoneid() != GLOBAL_ZONEID) {
510*bafa7067Snn35248 		assert(is_system_labeled() != 0);
511*bafa7067Snn35248 		(void) strlcpy(zent->zbrand, NATIVE_BRAND_NAME,
512*bafa7067Snn35248 		    sizeof (zent->zbrand));
513*bafa7067Snn35248 	} else if (zone_get_brand(zent->zname, zent->zbrand,
5149acbbeafSnn35248 	    sizeof (zent->zbrand)) != Z_OK) {
5159acbbeafSnn35248 		zperror2(zent->zname, gettext("could not get brand name"));
5169acbbeafSnn35248 		return (Z_ERR);
5179acbbeafSnn35248 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	return (Z_OK);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
5247c478bd9Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
5257c478bd9Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
5267c478bd9Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
5277c478bd9Sstevel@tonic-gate  * return immediately to save work.
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate static int
531108322fbScarlsonj fetch_zents(void)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	zoneid_t *zids = NULL;
5347c478bd9Sstevel@tonic-gate 	uint_t nzents_saved;
535108322fbScarlsonj 	int i, retv;
536108322fbScarlsonj 	FILE *fp;
537108322fbScarlsonj 	boolean_t inaltroot;
538108322fbScarlsonj 	zone_entry_t *zentp;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	if (nzents > 0)
5417c478bd9Sstevel@tonic-gate 		return (Z_OK);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
5447c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
5457c478bd9Sstevel@tonic-gate 		return (Z_ERR);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate again:
5497c478bd9Sstevel@tonic-gate 	if (nzents == 0)
5507c478bd9Sstevel@tonic-gate 		return (Z_OK);
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
5537c478bd9Sstevel@tonic-gate 	nzents_saved = nzents;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
5567c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
5577c478bd9Sstevel@tonic-gate 		free(zids);
5587c478bd9Sstevel@tonic-gate 		return (Z_ERR);
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	if (nzents != nzents_saved) {
5617c478bd9Sstevel@tonic-gate 		/* list changed, try again */
5627c478bd9Sstevel@tonic-gate 		free(zids);
5637c478bd9Sstevel@tonic-gate 		goto again;
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
5677c478bd9Sstevel@tonic-gate 
568108322fbScarlsonj 	inaltroot = zonecfg_in_alt_root();
569108322fbScarlsonj 	if (inaltroot)
570108322fbScarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
571108322fbScarlsonj 	else
572108322fbScarlsonj 		fp = NULL;
573108322fbScarlsonj 	zentp = zents;
574108322fbScarlsonj 	retv = Z_OK;
5757c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
5767c478bd9Sstevel@tonic-gate 		char name[ZONENAME_MAX];
577108322fbScarlsonj 		char altname[ZONENAME_MAX];
5787c478bd9Sstevel@tonic-gate 
579108322fbScarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
5807c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to get zone name"), B_FALSE);
581108322fbScarlsonj 			retv = Z_ERR;
582108322fbScarlsonj 			continue;
5837c478bd9Sstevel@tonic-gate 		}
584108322fbScarlsonj 		if (zonecfg_is_scratch(name)) {
585108322fbScarlsonj 			/* Ignore scratch zones by default */
586108322fbScarlsonj 			if (!inaltroot)
587108322fbScarlsonj 				continue;
588108322fbScarlsonj 			if (fp == NULL ||
589108322fbScarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
590108322fbScarlsonj 			    sizeof (altname), NULL, 0) == -1) {
5915ee84fbdSgjelinek 				zerror(gettext("could not resolve scratch "
592108322fbScarlsonj 				    "zone %s"), name);
593108322fbScarlsonj 				retv = Z_ERR;
594108322fbScarlsonj 				continue;
595108322fbScarlsonj 			}
596108322fbScarlsonj 			(void) strcpy(name, altname);
597108322fbScarlsonj 		} else {
598108322fbScarlsonj 			/* Ignore non-scratch when in an alternate root */
599108322fbScarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
600108322fbScarlsonj 				continue;
601108322fbScarlsonj 		}
602108322fbScarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
603108322fbScarlsonj 			zerror(gettext("failed to get zone data"));
604108322fbScarlsonj 			retv = Z_ERR;
605108322fbScarlsonj 			continue;
606108322fbScarlsonj 		}
607108322fbScarlsonj 		zentp++;
608108322fbScarlsonj 	}
609108322fbScarlsonj 	nzents = zentp - zents;
610108322fbScarlsonj 	if (fp != NULL)
611108322fbScarlsonj 		zonecfg_close_scratch(fp);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	free(zids);
614108322fbScarlsonj 	return (retv);
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
617108322fbScarlsonj static int
6187c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	int i;
6217c478bd9Sstevel@tonic-gate 	zone_entry_t zent;
6227c478bd9Sstevel@tonic-gate 	FILE *cookie;
6237c478bd9Sstevel@tonic-gate 	char *name;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/*
6267c478bd9Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
6277c478bd9Sstevel@tonic-gate 	 * If that is all we need, then return.
6287c478bd9Sstevel@tonic-gate 	 */
629108322fbScarlsonj 	if ((i = fetch_zents()) != Z_OK) {
6307c478bd9Sstevel@tonic-gate 		/*
6317c478bd9Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
6327c478bd9Sstevel@tonic-gate 		 * care of this.
6337c478bd9Sstevel@tonic-gate 		 */
634108322fbScarlsonj 		return (i);
6357c478bd9Sstevel@tonic-gate 	}
636108322fbScarlsonj 	for (i = 0; i < nzents; i++)
6377c478bd9Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
6387c478bd9Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
639108322fbScarlsonj 		return (Z_OK);
6407c478bd9Sstevel@tonic-gate 	/*
6417c478bd9Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
6427c478bd9Sstevel@tonic-gate 	 * any we have already printed.
6437c478bd9Sstevel@tonic-gate 	 */
6447c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
6457c478bd9Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
6467c478bd9Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
6477c478bd9Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
6487c478bd9Sstevel@tonic-gate 				break;
6497c478bd9Sstevel@tonic-gate 		}
6507c478bd9Sstevel@tonic-gate 		if (i < nzents) {
6517c478bd9Sstevel@tonic-gate 			free(name);
6527c478bd9Sstevel@tonic-gate 			continue;
6537c478bd9Sstevel@tonic-gate 		}
654108322fbScarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
6557c478bd9Sstevel@tonic-gate 			free(name);
6567c478bd9Sstevel@tonic-gate 			continue;
6577c478bd9Sstevel@tonic-gate 		}
6587c478bd9Sstevel@tonic-gate 		free(name);
6597c478bd9Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
6607c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
6617c478bd9Sstevel@tonic-gate 	}
6627c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
663108322fbScarlsonj 	return (Z_OK);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate static zone_entry_t *
6677c478bd9Sstevel@tonic-gate lookup_running_zone(char *str)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
6707c478bd9Sstevel@tonic-gate 	char *cp;
6717c478bd9Sstevel@tonic-gate 	int i;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
6747c478bd9Sstevel@tonic-gate 		return (NULL);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6777c478bd9Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
6787c478bd9Sstevel@tonic-gate 			return (&zents[i]);
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 	errno = 0;
6817c478bd9Sstevel@tonic-gate 	zoneid = strtol(str, &cp, 0);
6827c478bd9Sstevel@tonic-gate 	if (zoneid < MIN_ZONEID || zoneid > MAX_ZONEID ||
6837c478bd9Sstevel@tonic-gate 	    errno != 0 || *cp != '\0')
6847c478bd9Sstevel@tonic-gate 		return (NULL);
6857c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6867c478bd9Sstevel@tonic-gate 		if (zoneid == zents[i].zid)
6877c478bd9Sstevel@tonic-gate 			return (&zents[i]);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 	return (NULL);
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
6947c478bd9Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
6957c478bd9Sstevel@tonic-gate  */
6967c478bd9Sstevel@tonic-gate static boolean_t
6977c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	char *str;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
7027c478bd9Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
7037c478bd9Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
7047c478bd9Sstevel@tonic-gate 	/*
7057c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
7067c478bd9Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
7077c478bd9Sstevel@tonic-gate 	 * either:
7087c478bd9Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
7097c478bd9Sstevel@tonic-gate 	 * or
7107c478bd9Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
7117c478bd9Sstevel@tonic-gate 	 */
7127c478bd9Sstevel@tonic-gate 	switch (bit) {
7137c478bd9Sstevel@tonic-gate 	case S_IRUSR:
7147c478bd9Sstevel@tonic-gate 		str = gettext("owner readable");
7157c478bd9Sstevel@tonic-gate 		break;
7167c478bd9Sstevel@tonic-gate 	case S_IWUSR:
7177c478bd9Sstevel@tonic-gate 		str = gettext("owner writable");
7187c478bd9Sstevel@tonic-gate 		break;
7197c478bd9Sstevel@tonic-gate 	case S_IXUSR:
7207c478bd9Sstevel@tonic-gate 		str = gettext("owner executable");
7217c478bd9Sstevel@tonic-gate 		break;
7227c478bd9Sstevel@tonic-gate 	case S_IRGRP:
7237c478bd9Sstevel@tonic-gate 		str = gettext("group readable");
7247c478bd9Sstevel@tonic-gate 		break;
7257c478bd9Sstevel@tonic-gate 	case S_IWGRP:
7267c478bd9Sstevel@tonic-gate 		str = gettext("group writable");
7277c478bd9Sstevel@tonic-gate 		break;
7287c478bd9Sstevel@tonic-gate 	case S_IXGRP:
7297c478bd9Sstevel@tonic-gate 		str = gettext("group executable");
7307c478bd9Sstevel@tonic-gate 		break;
7317c478bd9Sstevel@tonic-gate 	case S_IROTH:
7327c478bd9Sstevel@tonic-gate 		str = gettext("world readable");
7337c478bd9Sstevel@tonic-gate 		break;
7347c478bd9Sstevel@tonic-gate 	case S_IWOTH:
7357c478bd9Sstevel@tonic-gate 		str = gettext("world writable");
7367c478bd9Sstevel@tonic-gate 		break;
7377c478bd9Sstevel@tonic-gate 	case S_IXOTH:
7387c478bd9Sstevel@tonic-gate 		str = gettext("world executable");
7397c478bd9Sstevel@tonic-gate 		break;
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
7427c478bd9Sstevel@tonic-gate 		/*
7437c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
7447c478bd9Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
7457c478bd9Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
7467c478bd9Sstevel@tonic-gate 		 * strings from above.
7477c478bd9Sstevel@tonic-gate 		 */
7487c478bd9Sstevel@tonic-gate 		/*
7497c478bd9Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
7507c478bd9Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
7517c478bd9Sstevel@tonic-gate 		 */
7527c478bd9Sstevel@tonic-gate 		if (on) {
7537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
7547c478bd9Sstevel@tonic-gate 			    file, str);
7557c478bd9Sstevel@tonic-gate 		} else {
7567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
7577c478bd9Sstevel@tonic-gate 			    file, str);
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 		return (B_TRUE);
7607c478bd9Sstevel@tonic-gate 	}
7617c478bd9Sstevel@tonic-gate 	return (B_FALSE);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
7667c478bd9Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
7677c478bd9Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
7687c478bd9Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
7697c478bd9Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
7707c478bd9Sstevel@tonic-gate  */
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate static int
7737c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path)
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
7767c478bd9Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
7777c478bd9Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
7787c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
7797c478bd9Sstevel@tonic-gate 	int res, err;
7807c478bd9Sstevel@tonic-gate 	FILE *cookie;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
7837c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
7847c478bd9Sstevel@tonic-gate 		/* Skip zones which are not installed. */
7857c478bd9Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
7867c478bd9Sstevel@tonic-gate 			free(ze);
7877c478bd9Sstevel@tonic-gate 			continue;
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
7907c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
7917c478bd9Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
7927c478bd9Sstevel@tonic-gate 			free(ze);
7937c478bd9Sstevel@tonic-gate 			continue;
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
7967c478bd9Sstevel@tonic-gate 			/* old index file without path, fall back */
7977c478bd9Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
7987c478bd9Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
7997c478bd9Sstevel@tonic-gate 				errno = err;
8007c478bd9Sstevel@tonic-gate 				zperror2(ze->zone_name,
8017c478bd9Sstevel@tonic-gate 				    gettext("could not get zone path"));
8027c478bd9Sstevel@tonic-gate 				free(ze);
8037c478bd9Sstevel@tonic-gate 				continue;
8047c478bd9Sstevel@tonic-gate 			}
8057c478bd9Sstevel@tonic-gate 		}
806108322fbScarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
807108322fbScarlsonj 		    zonecfg_get_root(), ze->zone_path);
808108322fbScarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
8097c478bd9Sstevel@tonic-gate 		if (res == -1) {
8107c478bd9Sstevel@tonic-gate 			if (errno != ENOENT) {
811108322fbScarlsonj 				zperror(path_copy, B_FALSE);
8127c478bd9Sstevel@tonic-gate 				free(ze);
8137c478bd9Sstevel@tonic-gate 				return (Z_ERR);
8147c478bd9Sstevel@tonic-gate 			}
8157c478bd9Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
8167c478bd9Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
817108322fbScarlsonj 			    "zonepath", path_copy);
8187c478bd9Sstevel@tonic-gate 			free(ze);
8197c478bd9Sstevel@tonic-gate 			continue;
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 		rpath[res] = '\0';
8227c478bd9Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
8237c478bd9Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
8247c478bd9Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
8257c478bd9Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
8265ee84fbdSgjelinek 			/*
8275ee84fbdSgjelinek 			 * TRANSLATION_NOTE
8285ee84fbdSgjelinek 			 * zonepath is a literal that should not be translated.
8295ee84fbdSgjelinek 			 */
8307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
8317c478bd9Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
8327c478bd9Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
8337c478bd9Sstevel@tonic-gate 			free(ze);
8347c478bd9Sstevel@tonic-gate 			return (Z_ERR);
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 		free(ze);
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
8397c478bd9Sstevel@tonic-gate 	return (Z_OK);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate static int
8437c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	int res;			/* result of last library/system call */
8467c478bd9Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
8477c478bd9Sstevel@tonic-gate 	struct stat stbuf;
8483f2f09c1Sdp 	struct statvfs64 vfsbuf;
8497c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
8507c478bd9Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
8517c478bd9Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
8527c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
8537c478bd9Sstevel@tonic-gate 	zone_state_t state;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (path[0] != '/') {
8567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
8577c478bd9Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
8587c478bd9Sstevel@tonic-gate 		return (Z_ERR);
8597c478bd9Sstevel@tonic-gate 	}
8607c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
8617c478bd9Sstevel@tonic-gate 		if ((errno != ENOENT) ||
862865e09a4Sgjelinek 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
863865e09a4Sgjelinek 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
8647c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
8657c478bd9Sstevel@tonic-gate 			return (Z_ERR);
8667c478bd9Sstevel@tonic-gate 		}
8677c478bd9Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
8685ee84fbdSgjelinek 			/*
8695ee84fbdSgjelinek 			 * TRANSLATION_NOTE
8705ee84fbdSgjelinek 			 * zoneadm is a literal that should not be translated.
8715ee84fbdSgjelinek 			 */
8727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
8735ee84fbdSgjelinek 			    "exist, so it could not be verified.\nWhen "
8745ee84fbdSgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
8755ee84fbdSgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
8765ee84fbdSgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
8775ee84fbdSgjelinek 			    "other-writable\nor\n%s overlaps with any other "
8787c478bd9Sstevel@tonic-gate 			    "installed zones.\n"), path,
8797c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
8807c478bd9Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
8817c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
8827c478bd9Sstevel@tonic-gate 			return (Z_OK);
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 		/*
8857c478bd9Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
8867c478bd9Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
8877c478bd9Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
8887c478bd9Sstevel@tonic-gate 		 */
8897c478bd9Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
8907c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
8917c478bd9Sstevel@tonic-gate 			return (Z_ERR);
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 		/*
8947c478bd9Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
8957c478bd9Sstevel@tonic-gate 		 * as well continue the verify procedure.
8967c478bd9Sstevel@tonic-gate 		 */
8977c478bd9Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
8987c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
8997c478bd9Sstevel@tonic-gate 		/*
9007c478bd9Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
9017c478bd9Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
9027c478bd9Sstevel@tonic-gate 		 * only recurse once.
9037c478bd9Sstevel@tonic-gate 		 */
904865e09a4Sgjelinek 		return (validate_zonepath(path, cmd_num));
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
9077c478bd9Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
9087c478bd9Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
9097c478bd9Sstevel@tonic-gate 		zperror(path, B_TRUE);
9107c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
9137c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
9147c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
9177c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
9187c478bd9Sstevel@tonic-gate 		    rpath);
9197c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 	if ((strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) ||
9227c478bd9Sstevel@tonic-gate 	    (strcmp(stbuf.st_fstype, MNTTYPE_XMEMFS) == 0)) {
9237c478bd9Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
9240b5de56dSgjelinek 		    "file system.\n"), rpath);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
9277c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9287c478bd9Sstevel@tonic-gate 	/*
9297c478bd9Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
9307c478bd9Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
9317c478bd9Sstevel@tonic-gate 	 * to be fixed up front.
9327c478bd9Sstevel@tonic-gate 	 */
9337c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
9347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
9357c478bd9Sstevel@tonic-gate 		    rpath);
9367c478bd9Sstevel@tonic-gate 		err = B_TRUE;
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
9397c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
9407c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
9417c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
9427c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
9437c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
9447c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
9457c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
9467c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
9497c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
9507c478bd9Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
9517c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	rppath[res] = '\0';
9547c478bd9Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
9557c478bd9Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
9567c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 	/* theoretically impossible */
9597c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
9607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
9617c478bd9Sstevel@tonic-gate 		    rppath);
9627c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
9657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
9667c478bd9Sstevel@tonic-gate 		    rppath);
9677c478bd9Sstevel@tonic-gate 		err = B_TRUE;
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
9707c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
9717c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
9727c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
9737c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
9747c478bd9Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
9757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
9767c478bd9Sstevel@tonic-gate 		    rppath);
9777c478bd9Sstevel@tonic-gate 		err = B_TRUE;
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 
9803f2f09c1Sdp 	if (statvfs64(rpath, &vfsbuf) != 0) {
9817c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
9827c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9837c478bd9Sstevel@tonic-gate 	}
984b5abaf04Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
9855ee84fbdSgjelinek 		/*
9865ee84fbdSgjelinek 		 * TRANSLATION_NOTE
9875ee84fbdSgjelinek 		 * Zonepath and NFS are literals that should not be translated.
9885ee84fbdSgjelinek 		 */
9895ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
9900b5de56dSgjelinek 		    "mounted file system.\n"
9910b5de56dSgjelinek 		    "\tA local file system must be used.\n"), rpath);
992b5abaf04Sgjelinek 		return (Z_ERR);
993b5abaf04Sgjelinek 	}
994b5abaf04Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
9955ee84fbdSgjelinek 		/*
9965ee84fbdSgjelinek 		 * TRANSLATION_NOTE
9975ee84fbdSgjelinek 		 * Zonepath and nosuid are literals that should not be
9985ee84fbdSgjelinek 		 * translated.
9995ee84fbdSgjelinek 		 */
1000b5abaf04Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
10010b5de56dSgjelinek 		    "file system.\n"), rpath);
10027c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
10067c478bd9Sstevel@tonic-gate 		errno = res;
10077c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
10087c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
10127c478bd9Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
1013ee519a1fSgjelinek 	 * However, the root path is expected to be there if the zone is
1014ee519a1fSgjelinek 	 * detached.
10157c478bd9Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
10167c478bd9Sstevel@tonic-gate 	 */
1017ee519a1fSgjelinek 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
10187c478bd9Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
10197c478bd9Sstevel@tonic-gate 		    sizeof (rootpath)) {
10205ee84fbdSgjelinek 			/*
10215ee84fbdSgjelinek 			 * TRANSLATION_NOTE
10225ee84fbdSgjelinek 			 * Zonepath is a literal that should not be translated.
10235ee84fbdSgjelinek 			 */
10247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10257c478bd9Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
10267c478bd9Sstevel@tonic-gate 			return (Z_ERR);
10277c478bd9Sstevel@tonic-gate 		}
10287c478bd9Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
1029ee519a1fSgjelinek 			if (zonecfg_detached(rpath))
1030ee519a1fSgjelinek 				(void) fprintf(stderr,
1031ee519a1fSgjelinek 				    gettext("Cannot %s detached "
1032ee519a1fSgjelinek 				    "zone.\nUse attach or remove %s "
1033ee519a1fSgjelinek 				    "directory.\n"), cmd_to_str(cmd_num),
1034ee519a1fSgjelinek 				    rpath);
1035ee519a1fSgjelinek 			else
1036ee519a1fSgjelinek 				(void) fprintf(stderr,
1037ee519a1fSgjelinek 				    gettext("Rootpath %s exists; "
1038ee519a1fSgjelinek 				    "remove or move aside prior to %s.\n"),
1039ee519a1fSgjelinek 				    rootpath, cmd_to_str(cmd_num));
10407c478bd9Sstevel@tonic-gate 			return (Z_ERR);
10417c478bd9Sstevel@tonic-gate 		}
10427c478bd9Sstevel@tonic-gate 	}
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10479acbbeafSnn35248 /*
10489acbbeafSnn35248  * The following two routines implement a simple locking mechanism to
10499acbbeafSnn35248  * ensure that only one instance of zoneadm at a time is able to manipulate
10509acbbeafSnn35248  * a given zone.  The lock is built on top of an fcntl(2) lock of
10519acbbeafSnn35248  * [<altroot>]/var/run/zones/<zonename>.zoneadm.lock.  If a zoneadm instance
10529acbbeafSnn35248  * can grab that lock, it is allowed to manipulate the zone.
10539acbbeafSnn35248  *
10549acbbeafSnn35248  * Since zoneadm may call external applications which in turn invoke
10559acbbeafSnn35248  * zoneadm again, we introduce the notion of "lock inheritance".  Any
10569acbbeafSnn35248  * instance of zoneadm that has another instance in its ancestry is assumed
10579acbbeafSnn35248  * to be acting on behalf of the original zoneadm, and is thus allowed to
10589acbbeafSnn35248  * manipulate its zone.
10599acbbeafSnn35248  *
10609acbbeafSnn35248  * This inheritance is implemented via the _ZONEADM_LOCK_HELD environment
10619acbbeafSnn35248  * variable.  When zoneadm is granted a lock on its zone, this environment
10629acbbeafSnn35248  * variable is set to 1.  When it releases the lock, the variable is set to
10639acbbeafSnn35248  * 0.  Since a child process inherits its parent's environment, checking
10649acbbeafSnn35248  * the state of this variable indicates whether or not any ancestor owns
10659acbbeafSnn35248  * the lock.
10669acbbeafSnn35248  */
10677c478bd9Sstevel@tonic-gate static void
10687c478bd9Sstevel@tonic-gate release_lock_file(int lockfd)
10697c478bd9Sstevel@tonic-gate {
10709acbbeafSnn35248 	/*
10719acbbeafSnn35248 	 * If we are cleaning up from a failed attempt to lock the zone for
10729acbbeafSnn35248 	 * the first time, we might have a zone_lock_cnt of 0.  In that
10739acbbeafSnn35248 	 * error case, we don't want to do anything but close the lock
10749acbbeafSnn35248 	 * file.
10759acbbeafSnn35248 	 */
10769acbbeafSnn35248 	assert(zone_lock_cnt >= 0);
10779acbbeafSnn35248 	if (zone_lock_cnt > 0) {
10789acbbeafSnn35248 		assert(getenv(LOCK_ENV_VAR) != NULL);
10799acbbeafSnn35248 		assert(atoi(getenv(LOCK_ENV_VAR)) == 1);
10809acbbeafSnn35248 		if (--zone_lock_cnt > 0) {
10819acbbeafSnn35248 			assert(lockfd == -1);
10829acbbeafSnn35248 			return;
10839acbbeafSnn35248 		}
10849acbbeafSnn35248 		if (putenv(zoneadm_lock_not_held) != 0) {
10859acbbeafSnn35248 			zperror(target_zone, B_TRUE);
10869acbbeafSnn35248 			exit(Z_ERR);
10879acbbeafSnn35248 		}
10889acbbeafSnn35248 	}
10899acbbeafSnn35248 	assert(lockfd >= 0);
10907c478bd9Sstevel@tonic-gate 	(void) close(lockfd);
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate static int
10947c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
10957c478bd9Sstevel@tonic-gate {
10967c478bd9Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
10977c478bd9Sstevel@tonic-gate 	struct flock flock;
10987c478bd9Sstevel@tonic-gate 
10999acbbeafSnn35248 	/*
11009acbbeafSnn35248 	 * If we already have the lock, we can skip this expensive song
11019acbbeafSnn35248 	 * and dance.
11029acbbeafSnn35248 	 */
11039acbbeafSnn35248 	if (zone_lock_cnt > 0) {
11049acbbeafSnn35248 		zone_lock_cnt++;
11059acbbeafSnn35248 		*lockfd = -1;
11069acbbeafSnn35248 		return (Z_OK);
11079acbbeafSnn35248 	}
11089acbbeafSnn35248 	assert(getenv(LOCK_ENV_VAR) != NULL);
11099acbbeafSnn35248 	assert(atoi(getenv(LOCK_ENV_VAR)) == 0);
11109acbbeafSnn35248 
1111108322fbScarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s", zonecfg_get_root(),
1112108322fbScarlsonj 	    ZONES_TMPDIR) >= sizeof (pathbuf)) {
1113108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
1114108322fbScarlsonj 		return (Z_ERR);
1115108322fbScarlsonj 	}
1116108322fbScarlsonj 	if (mkdir(pathbuf, S_IRWXU) < 0 && errno != EEXIST) {
1117108322fbScarlsonj 		zerror(gettext("could not mkdir %s: %s"), pathbuf,
11187c478bd9Sstevel@tonic-gate 		    strerror(errno));
11197c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11207c478bd9Sstevel@tonic-gate 	}
1121108322fbScarlsonj 	(void) chmod(pathbuf, S_IRWXU);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	/*
11247c478bd9Sstevel@tonic-gate 	 * One of these lock files is created for each zone (when needed).
11257c478bd9Sstevel@tonic-gate 	 * The lock files are not cleaned up (except on system reboot),
11267c478bd9Sstevel@tonic-gate 	 * but since there is only one per zone, there is no resource
11277c478bd9Sstevel@tonic-gate 	 * starvation issue.
11287c478bd9Sstevel@tonic-gate 	 */
1129108322fbScarlsonj 	if (snprintf(pathbuf, sizeof (pathbuf), "%s%s/%s.zoneadm.lock",
1130108322fbScarlsonj 	    zonecfg_get_root(), ZONES_TMPDIR, zone_name) >= sizeof (pathbuf)) {
1131108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
1132108322fbScarlsonj 		return (Z_ERR);
1133108322fbScarlsonj 	}
11347c478bd9Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
11357c478bd9Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
11367c478bd9Sstevel@tonic-gate 		    strerror(errno));
11377c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11387c478bd9Sstevel@tonic-gate 	}
11397c478bd9Sstevel@tonic-gate 	/*
11407c478bd9Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
11417c478bd9Sstevel@tonic-gate 	 */
11427c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
11437c478bd9Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
11447c478bd9Sstevel@tonic-gate 	flock.l_start = (off_t)0;
11457c478bd9Sstevel@tonic-gate 	flock.l_len = (off_t)0;
11469acbbeafSnn35248 	if ((fcntl(*lockfd, F_SETLKW, &flock) < 0) ||
11479acbbeafSnn35248 	    (putenv(zoneadm_lock_held) != 0)) {
11487c478bd9Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
11497c478bd9Sstevel@tonic-gate 		    strerror(errno));
11507c478bd9Sstevel@tonic-gate 		release_lock_file(*lockfd);
11517c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11527c478bd9Sstevel@tonic-gate 	}
11539acbbeafSnn35248 	zone_lock_cnt = 1;
11547c478bd9Sstevel@tonic-gate 	return (Z_OK);
11557c478bd9Sstevel@tonic-gate }
11567c478bd9Sstevel@tonic-gate 
1157108322fbScarlsonj static boolean_t
11587c478bd9Sstevel@tonic-gate get_doorname(const char *zone_name, char *buffer)
11597c478bd9Sstevel@tonic-gate {
1160108322fbScarlsonj 	return (snprintf(buffer, PATH_MAX, "%s" ZONE_DOOR_PATH,
1161108322fbScarlsonj 	    zonecfg_get_root(), zone_name) < PATH_MAX);
11627c478bd9Sstevel@tonic-gate }
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate /*
11657c478bd9Sstevel@tonic-gate  * system daemons are not audited.  For the global zone, this occurs
11667c478bd9Sstevel@tonic-gate  * "naturally" since init is started with the default audit
11677c478bd9Sstevel@tonic-gate  * characteristics.  Since zoneadmd is a system daemon and it starts
11687c478bd9Sstevel@tonic-gate  * init for a zone, it is necessary to clear out the audit
11697c478bd9Sstevel@tonic-gate  * characteristics inherited from whomever started zoneadmd.  This is
11707c478bd9Sstevel@tonic-gate  * indicated by the audit id, which is set from the ruid parameter of
11717c478bd9Sstevel@tonic-gate  * adt_set_user(), below.
11727c478bd9Sstevel@tonic-gate  */
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate static void
11757c478bd9Sstevel@tonic-gate prepare_audit_context()
11767c478bd9Sstevel@tonic-gate {
11777c478bd9Sstevel@tonic-gate 	adt_session_data_t	*ah;
11787c478bd9Sstevel@tonic-gate 	char			*failure = gettext("audit failure: %s");
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	if (adt_start_session(&ah, NULL, 0)) {
11817c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
11827c478bd9Sstevel@tonic-gate 		return;
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 	if (adt_set_user(ah, ADT_NO_AUDIT, ADT_NO_AUDIT,
11857c478bd9Sstevel@tonic-gate 	    ADT_NO_AUDIT, ADT_NO_AUDIT, NULL, ADT_NEW)) {
11867c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
11877c478bd9Sstevel@tonic-gate 		(void) adt_end_session(ah);
11887c478bd9Sstevel@tonic-gate 		return;
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 	if (adt_set_proc(ah))
11917c478bd9Sstevel@tonic-gate 		zerror(failure, strerror(errno));
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	(void) adt_end_session(ah);
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate static int
11977c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
12007c478bd9Sstevel@tonic-gate 	pid_t child_pid;
12017c478bd9Sstevel@tonic-gate 	int error = Z_ERR;
12027c478bd9Sstevel@tonic-gate 	int doorfd, lockfd;
12037c478bd9Sstevel@tonic-gate 	struct door_info info;
12047c478bd9Sstevel@tonic-gate 
1205108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath))
1206108322fbScarlsonj 		return (Z_ERR);
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
12097c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	/*
12127c478bd9Sstevel@tonic-gate 	 * Now that we have the lock, re-confirm that the daemon is
12137c478bd9Sstevel@tonic-gate 	 * *not* up and working fine.  If it is still down, we have a green
12147c478bd9Sstevel@tonic-gate 	 * light to start it.
12157c478bd9Sstevel@tonic-gate 	 */
12167c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
12177c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
12187c478bd9Sstevel@tonic-gate 			zperror(doorpath, B_FALSE);
12197c478bd9Sstevel@tonic-gate 			goto out;
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 	} else {
12227c478bd9Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
12237c478bd9Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
12247c478bd9Sstevel@tonic-gate 			error = Z_OK;
12257c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
12267c478bd9Sstevel@tonic-gate 			goto out;
12277c478bd9Sstevel@tonic-gate 		}
12287c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
12297c478bd9Sstevel@tonic-gate 	}
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
12327c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"), B_FALSE);
12337c478bd9Sstevel@tonic-gate 		goto out;
12347c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
1235108322fbScarlsonj 		const char *argv[6], **ap;
12367c478bd9Sstevel@tonic-gate 
1237108322fbScarlsonj 		/* child process */
12387c478bd9Sstevel@tonic-gate 		prepare_audit_context();
12397c478bd9Sstevel@tonic-gate 
1240108322fbScarlsonj 		ap = argv;
1241108322fbScarlsonj 		*ap++ = "zoneadmd";
1242108322fbScarlsonj 		*ap++ = "-z";
1243108322fbScarlsonj 		*ap++ = zone_name;
1244108322fbScarlsonj 		if (zonecfg_in_alt_root()) {
1245108322fbScarlsonj 			*ap++ = "-R";
1246108322fbScarlsonj 			*ap++ = zonecfg_get_root();
1247108322fbScarlsonj 		}
1248108322fbScarlsonj 		*ap = NULL;
1249108322fbScarlsonj 
1250108322fbScarlsonj 		(void) execv("/usr/lib/zones/zoneadmd", (char * const *)argv);
12515ee84fbdSgjelinek 		/*
12525ee84fbdSgjelinek 		 * TRANSLATION_NOTE
12535ee84fbdSgjelinek 		 * zoneadmd is a literal that should not be translated.
12545ee84fbdSgjelinek 		 */
12557c478bd9Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"), B_FALSE);
12567c478bd9Sstevel@tonic-gate 		_exit(Z_ERR);
12577c478bd9Sstevel@tonic-gate 	} else {
12587c478bd9Sstevel@tonic-gate 		/* parent process */
12597c478bd9Sstevel@tonic-gate 		pid_t retval;
12607c478bd9Sstevel@tonic-gate 		int pstatus = 0;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 		do {
12637c478bd9Sstevel@tonic-gate 			retval = waitpid(child_pid, &pstatus, 0);
12647c478bd9Sstevel@tonic-gate 		} while (retval != child_pid);
12657c478bd9Sstevel@tonic-gate 		if (WIFSIGNALED(pstatus) || (WIFEXITED(pstatus) &&
12667c478bd9Sstevel@tonic-gate 		    WEXITSTATUS(pstatus) != 0)) {
12677c478bd9Sstevel@tonic-gate 			zerror(gettext("could not start %s"), "zoneadmd");
12687c478bd9Sstevel@tonic-gate 			goto out;
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 	error = Z_OK;
12727c478bd9Sstevel@tonic-gate out:
12737c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
12747c478bd9Sstevel@tonic-gate 	return (error);
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate static int
12787c478bd9Sstevel@tonic-gate ping_zoneadmd(const char *zone_name)
12797c478bd9Sstevel@tonic-gate {
12807c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
12817c478bd9Sstevel@tonic-gate 	int doorfd;
12827c478bd9Sstevel@tonic-gate 	struct door_info info;
12837c478bd9Sstevel@tonic-gate 
1284108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath))
1285108322fbScarlsonj 		return (Z_ERR);
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
12887c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 	if (door_info(doorfd, &info) == 0 &&
12917c478bd9Sstevel@tonic-gate 	    ((info.di_attributes & DOOR_REVOKED) == 0)) {
12927c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
12937c478bd9Sstevel@tonic-gate 		return (Z_OK);
12947c478bd9Sstevel@tonic-gate 	}
12957c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
12967c478bd9Sstevel@tonic-gate 	return (Z_ERR);
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate static int
13007c478bd9Sstevel@tonic-gate call_zoneadmd(const char *zone_name, zone_cmd_arg_t *arg)
13017c478bd9Sstevel@tonic-gate {
13027c478bd9Sstevel@tonic-gate 	char doorpath[PATH_MAX];
13037c478bd9Sstevel@tonic-gate 	int doorfd, result;
13047c478bd9Sstevel@tonic-gate 	door_arg_t darg;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
13077c478bd9Sstevel@tonic-gate 	uint64_t uniqid = 0;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	zone_cmd_rval_t *rvalp;
13107c478bd9Sstevel@tonic-gate 	size_t rlen;
13117c478bd9Sstevel@tonic-gate 	char *cp, *errbuf;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	rlen = getpagesize();
13147c478bd9Sstevel@tonic-gate 	if ((rvalp = malloc(rlen)) == NULL) {
13157c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rlen,
13167c478bd9Sstevel@tonic-gate 		    strerror(errno));
13177c478bd9Sstevel@tonic-gate 		return (-1);
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if ((zoneid = getzoneidbyname(zone_name)) != ZONE_ID_UNDEFINED) {
13217c478bd9Sstevel@tonic-gate 		(void) zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
13227c478bd9Sstevel@tonic-gate 		    sizeof (uniqid));
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 	arg->uniqid = uniqid;
13257c478bd9Sstevel@tonic-gate 	(void) strlcpy(arg->locale, locale, sizeof (arg->locale));
1326108322fbScarlsonj 	if (!get_doorname(zone_name, doorpath)) {
1327108322fbScarlsonj 		zerror(gettext("alternate root path is too long"));
1328108322fbScarlsonj 		free(rvalp);
1329108322fbScarlsonj 		return (-1);
1330108322fbScarlsonj 	}
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	/*
13337c478bd9Sstevel@tonic-gate 	 * Loop trying to start zoneadmd; if something goes seriously
13347c478bd9Sstevel@tonic-gate 	 * wrong we break out and fail.
13357c478bd9Sstevel@tonic-gate 	 */
13367c478bd9Sstevel@tonic-gate 	for (;;) {
13377c478bd9Sstevel@tonic-gate 		if (start_zoneadmd(zone_name) != Z_OK)
13387c478bd9Sstevel@tonic-gate 			break;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 		if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
13417c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to open zone door"), B_FALSE);
13427c478bd9Sstevel@tonic-gate 			break;
13437c478bd9Sstevel@tonic-gate 		}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 		darg.data_ptr = (char *)arg;
13467c478bd9Sstevel@tonic-gate 		darg.data_size = sizeof (*arg);
13477c478bd9Sstevel@tonic-gate 		darg.desc_ptr = NULL;
13487c478bd9Sstevel@tonic-gate 		darg.desc_num = 0;
13497c478bd9Sstevel@tonic-gate 		darg.rbuf = (char *)rvalp;
13507c478bd9Sstevel@tonic-gate 		darg.rsize = rlen;
13517c478bd9Sstevel@tonic-gate 		if (door_call(doorfd, &darg) != 0) {
13527c478bd9Sstevel@tonic-gate 			(void) close(doorfd);
13537c478bd9Sstevel@tonic-gate 			/*
13547c478bd9Sstevel@tonic-gate 			 * We'll get EBADF if the door has been revoked.
13557c478bd9Sstevel@tonic-gate 			 */
13567c478bd9Sstevel@tonic-gate 			if (errno != EBADF) {
13577c478bd9Sstevel@tonic-gate 				zperror(gettext("door_call failed"), B_FALSE);
13587c478bd9Sstevel@tonic-gate 				break;
13597c478bd9Sstevel@tonic-gate 			}
13607c478bd9Sstevel@tonic-gate 			continue;	/* take another lap */
13617c478bd9Sstevel@tonic-gate 		}
13627c478bd9Sstevel@tonic-gate 		(void) close(doorfd);
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 		if (darg.data_size == 0) {
13657c478bd9Sstevel@tonic-gate 			/* Door server is going away; kick it again. */
13667c478bd9Sstevel@tonic-gate 			continue;
13677c478bd9Sstevel@tonic-gate 		}
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 		errbuf = rvalp->errbuf;
13707c478bd9Sstevel@tonic-gate 		while (*errbuf != '\0') {
13717c478bd9Sstevel@tonic-gate 			/*
13727c478bd9Sstevel@tonic-gate 			 * Remove any newlines since zerror()
13737c478bd9Sstevel@tonic-gate 			 * will append one automatically.
13747c478bd9Sstevel@tonic-gate 			 */
13757c478bd9Sstevel@tonic-gate 			cp = strchr(errbuf, '\n');
13767c478bd9Sstevel@tonic-gate 			if (cp != NULL)
13777c478bd9Sstevel@tonic-gate 				*cp = '\0';
13787c478bd9Sstevel@tonic-gate 			zerror("%s", errbuf);
13797c478bd9Sstevel@tonic-gate 			if (cp == NULL)
13807c478bd9Sstevel@tonic-gate 				break;
13817c478bd9Sstevel@tonic-gate 			errbuf = cp + 1;
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 		result = rvalp->rval == 0 ? 0 : -1;
13847c478bd9Sstevel@tonic-gate 		free(rvalp);
13857c478bd9Sstevel@tonic-gate 		return (result);
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	free(rvalp);
13897c478bd9Sstevel@tonic-gate 	return (-1);
13907c478bd9Sstevel@tonic-gate }
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate static int
13937c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[])
13947c478bd9Sstevel@tonic-gate {
13957c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
13967c478bd9Sstevel@tonic-gate 	int arg;
13977c478bd9Sstevel@tonic-gate 
1398108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1399108322fbScarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1400108322fbScarlsonj 		return (Z_ERR);
1401108322fbScarlsonj 	}
1402108322fbScarlsonj 
14037c478bd9Sstevel@tonic-gate 	optind = 0;
14047c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
14057c478bd9Sstevel@tonic-gate 		switch (arg) {
14067c478bd9Sstevel@tonic-gate 		case '?':
14077c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
14087c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14097c478bd9Sstevel@tonic-gate 		default:
14107c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
14117c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
14127c478bd9Sstevel@tonic-gate 		}
14137c478bd9Sstevel@tonic-gate 	}
14147c478bd9Sstevel@tonic-gate 	if (argc > optind) {
14157c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
14167c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
14177c478bd9Sstevel@tonic-gate 	}
14189acbbeafSnn35248 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
14199acbbeafSnn35248 	    != Z_OK)
14207c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14217c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_READY) != Z_OK)
14227c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_READY;
14257c478bd9Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
14267c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
14277c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14287c478bd9Sstevel@tonic-gate 	}
14297c478bd9Sstevel@tonic-gate 	return (Z_OK);
14307c478bd9Sstevel@tonic-gate }
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate static int
14337c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[])
14347c478bd9Sstevel@tonic-gate {
14357c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
14369acbbeafSnn35248 	boolean_t force = B_FALSE;
14377c478bd9Sstevel@tonic-gate 	int arg;
14387c478bd9Sstevel@tonic-gate 
1439108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1440108322fbScarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1441108322fbScarlsonj 		return (Z_ERR);
1442108322fbScarlsonj 	}
1443108322fbScarlsonj 
14447c478bd9Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	/*
14473f2f09c1Sdp 	 * The following getopt processes arguments to zone boot; that
14483f2f09c1Sdp 	 * is to say, the [here] portion of the argument string:
14493f2f09c1Sdp 	 *
14503f2f09c1Sdp 	 *	zoneadm -z myzone boot [here] -- -v -m verbose
14513f2f09c1Sdp 	 *
14523f2f09c1Sdp 	 * Where [here] can either be nothing, -? (in which case we bail
14539acbbeafSnn35248 	 * and print usage), -f (a private option to indicate that the
14549acbbeafSnn35248 	 * boot operation should be 'forced'), or -s.  Support for -s is
14559acbbeafSnn35248 	 * vestigal and obsolete, but is retained because it was a
14569acbbeafSnn35248 	 * documented interface and there are known consumers including
14579acbbeafSnn35248 	 * admin/install; the proper way to specify boot arguments like -s
14589acbbeafSnn35248 	 * is:
14593f2f09c1Sdp 	 *
14603f2f09c1Sdp 	 *	zoneadm -z myzone boot -- -s -v -m verbose.
14617c478bd9Sstevel@tonic-gate 	 */
14627c478bd9Sstevel@tonic-gate 	optind = 0;
14639acbbeafSnn35248 	while ((arg = getopt(argc, argv, "?fs")) != EOF) {
14647c478bd9Sstevel@tonic-gate 		switch (arg) {
14657c478bd9Sstevel@tonic-gate 		case '?':
14667c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
14677c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
14687c478bd9Sstevel@tonic-gate 		case 's':
14697c478bd9Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
14707c478bd9Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
14717c478bd9Sstevel@tonic-gate 			break;
14729acbbeafSnn35248 		case 'f':
14739acbbeafSnn35248 			force = B_TRUE;
14749acbbeafSnn35248 			break;
14757c478bd9Sstevel@tonic-gate 		default:
14767c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
14777c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
14787c478bd9Sstevel@tonic-gate 		}
14797c478bd9Sstevel@tonic-gate 	}
14803f2f09c1Sdp 
14813f2f09c1Sdp 	for (; optind < argc; optind++) {
14823f2f09c1Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
14833f2f09c1Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
14843f2f09c1Sdp 			zerror(gettext("Boot argument list too long"));
14853f2f09c1Sdp 			return (Z_ERR);
14867c478bd9Sstevel@tonic-gate 		}
14873f2f09c1Sdp 		if (optind < argc - 1)
14883f2f09c1Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
14893f2f09c1Sdp 			    sizeof (zarg.bootbuf)) {
14903f2f09c1Sdp 				zerror(gettext("Boot argument list too long"));
14913f2f09c1Sdp 				return (Z_ERR);
14923f2f09c1Sdp 			}
14933f2f09c1Sdp 	}
14949acbbeafSnn35248 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
14959acbbeafSnn35248 	    != Z_OK)
14967c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14977c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_BOOT) != Z_OK)
14987c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14999acbbeafSnn35248 	zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
15007c478bd9Sstevel@tonic-gate 	if (call_zoneadmd(target_zone, &zarg) != 0) {
15017c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
15027c478bd9Sstevel@tonic-gate 		return (Z_ERR);
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 	return (Z_OK);
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate static void
15087c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
15097c478bd9Sstevel@tonic-gate {
15107c478bd9Sstevel@tonic-gate 	ssize_t result;
1511555afedfScarlsonj 	uuid_t uuid;
1512555afedfScarlsonj 	FILE *fp;
1513555afedfScarlsonj 
1514555afedfScarlsonj 	(void) memset(zeptr, 0, sizeof (*zeptr));
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	zeptr->zid = zid;
1517555afedfScarlsonj 
15187c478bd9Sstevel@tonic-gate 	/*
15197c478bd9Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
15207c478bd9Sstevel@tonic-gate 	 * we can be assured that it will succeed.
15217c478bd9Sstevel@tonic-gate 	 */
15227c478bd9Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
15237c478bd9Sstevel@tonic-gate 	assert(result >= 0);
1524555afedfScarlsonj 	if (zonecfg_is_scratch(zeptr->zname) &&
1525555afedfScarlsonj 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
1526555afedfScarlsonj 		(void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
1527555afedfScarlsonj 		    sizeof (zeptr->zname), NULL, 0);
1528555afedfScarlsonj 		zonecfg_close_scratch(fp);
1529555afedfScarlsonj 	}
1530555afedfScarlsonj 
1531555afedfScarlsonj 	if (is_system_labeled()) {
153245916cd2Sjpk 		(void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
153345916cd2Sjpk 		    sizeof (zeptr->zroot));
15349acbbeafSnn35248 		(void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
15359acbbeafSnn35248 			    sizeof (zeptr->zbrand));
1536555afedfScarlsonj 	} else {
1537555afedfScarlsonj 		(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
15389acbbeafSnn35248 		(void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
15399acbbeafSnn35248 		    sizeof (zeptr->zbrand));
154045916cd2Sjpk 	}
1541555afedfScarlsonj 
15427c478bd9Sstevel@tonic-gate 	zeptr->zstate_str = "running";
1543555afedfScarlsonj 	if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
1544555afedfScarlsonj 	    !uuid_is_null(uuid))
1545555afedfScarlsonj 		uuid_unparse(uuid, zeptr->zuuid);
15467c478bd9Sstevel@tonic-gate }
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate static int
15497c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[])
15507c478bd9Sstevel@tonic-gate {
15517c478bd9Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1552108322fbScarlsonj 	int arg, retv;
15537c478bd9Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
15547c478bd9Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
15557c478bd9Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	if (target_zone == NULL) {
15587c478bd9Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
15597c478bd9Sstevel@tonic-gate 		optind = 0;
15607c478bd9Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
15617c478bd9Sstevel@tonic-gate 			switch (arg) {
15627c478bd9Sstevel@tonic-gate 			case '?':
15637c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
15647c478bd9Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
1565475d78a4Sjonb 				/*
1566475d78a4Sjonb 				 * The 'i' and 'c' options are not mutually
1567475d78a4Sjonb 				 * exclusive so if 'c' is given, then min_state
1568475d78a4Sjonb 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1569475d78a4Sjonb 				 * the lowest possible state.  If 'i' is given,
1570475d78a4Sjonb 				 * then min_state is set to be the lowest state
1571475d78a4Sjonb 				 * so far.
1572475d78a4Sjonb 				 */
15737c478bd9Sstevel@tonic-gate 			case 'c':
15747c478bd9Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
15757c478bd9Sstevel@tonic-gate 				break;
15767c478bd9Sstevel@tonic-gate 			case 'i':
1577475d78a4Sjonb 				min_state = min(ZONE_STATE_INSTALLED,
1578475d78a4Sjonb 				    min_state);
1579475d78a4Sjonb 
15807c478bd9Sstevel@tonic-gate 				break;
15817c478bd9Sstevel@tonic-gate 			case 'p':
15827c478bd9Sstevel@tonic-gate 				parsable = B_TRUE;
15837c478bd9Sstevel@tonic-gate 				break;
15847c478bd9Sstevel@tonic-gate 			case 'v':
15857c478bd9Sstevel@tonic-gate 				verbose = B_TRUE;
15867c478bd9Sstevel@tonic-gate 				break;
15877c478bd9Sstevel@tonic-gate 			default:
15887c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
15897c478bd9Sstevel@tonic-gate 				return (Z_USAGE);
15907c478bd9Sstevel@tonic-gate 			}
15917c478bd9Sstevel@tonic-gate 		}
15927c478bd9Sstevel@tonic-gate 		if (parsable && verbose) {
15937c478bd9Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
15947c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
15957c478bd9Sstevel@tonic-gate 			return (Z_ERR);
15967c478bd9Sstevel@tonic-gate 		}
159745916cd2Sjpk 		if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1598108322fbScarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
15997c478bd9Sstevel@tonic-gate 		} else {
16007c478bd9Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
16019acbbeafSnn35248 			retv = Z_OK;
16027c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
16037c478bd9Sstevel@tonic-gate 		}
1604108322fbScarlsonj 		return (retv);
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	/*
16087c478bd9Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
16097c478bd9Sstevel@tonic-gate 	 */
16107c478bd9Sstevel@tonic-gate 	optind = 0;
16117c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
16127c478bd9Sstevel@tonic-gate 		switch (arg) {
16137c478bd9Sstevel@tonic-gate 		case '?':
16147c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
16157c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
16167c478bd9Sstevel@tonic-gate 		case 'p':
16177c478bd9Sstevel@tonic-gate 			parsable = B_TRUE;
16187c478bd9Sstevel@tonic-gate 			break;
16197c478bd9Sstevel@tonic-gate 		case 'v':
16207c478bd9Sstevel@tonic-gate 			verbose = B_TRUE;
16217c478bd9Sstevel@tonic-gate 			break;
16227c478bd9Sstevel@tonic-gate 		default:
16237c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
16247c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
16257c478bd9Sstevel@tonic-gate 		}
16267c478bd9Sstevel@tonic-gate 	}
16277c478bd9Sstevel@tonic-gate 	if (parsable && verbose) {
16287c478bd9Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
16297c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
16307c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16317c478bd9Sstevel@tonic-gate 	}
16327c478bd9Sstevel@tonic-gate 	if (argc > optind) {
16337c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
16347c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate 	if (zone_id != GLOBAL_ZONEID) {
16377c478bd9Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
16387c478bd9Sstevel@tonic-gate 		/*
16397c478bd9Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
16407c478bd9Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
16417c478bd9Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
16427c478bd9Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
16437c478bd9Sstevel@tonic-gate 		 */
16447c478bd9Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
16457c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
16467c478bd9Sstevel@tonic-gate 		output = B_TRUE;
16477c478bd9Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
16487c478bd9Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
16497c478bd9Sstevel@tonic-gate 		output = B_TRUE;
1650108322fbScarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1651108322fbScarlsonj 	    &zent) == Z_OK) {
16527c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
16537c478bd9Sstevel@tonic-gate 		output = B_TRUE;
16547c478bd9Sstevel@tonic-gate 	}
16557c478bd9Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate static void
16597c478bd9Sstevel@tonic-gate sigterm(int sig)
16607c478bd9Sstevel@tonic-gate {
16617c478bd9Sstevel@tonic-gate 	/*
16627c478bd9Sstevel@tonic-gate 	 * Ignore SIG{INT,TERM}, so we don't end up in an infinite loop,
16637c478bd9Sstevel@tonic-gate 	 * then propagate the signal to our process group.
16647c478bd9Sstevel@tonic-gate 	 */
16659acbbeafSnn35248 	assert(sig == SIGINT || sig == SIGTERM);
16667c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_IGN);
16677c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_IGN);
16687c478bd9Sstevel@tonic-gate 	(void) kill(0, sig);
16697c478bd9Sstevel@tonic-gate 	child_killed = B_TRUE;
16707c478bd9Sstevel@tonic-gate }
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate static int
16737c478bd9Sstevel@tonic-gate do_subproc(char *cmdbuf)
16747c478bd9Sstevel@tonic-gate {
16757c478bd9Sstevel@tonic-gate 	char inbuf[1024];	/* arbitrary large amount */
16767c478bd9Sstevel@tonic-gate 	FILE *file;
16777c478bd9Sstevel@tonic-gate 
16789acbbeafSnn35248 	do_subproc_cnt++;
16797c478bd9Sstevel@tonic-gate 	child_killed = B_FALSE;
16807c478bd9Sstevel@tonic-gate 	/*
16817c478bd9Sstevel@tonic-gate 	 * We use popen(3c) to launch child processes for [un]install;
16827c478bd9Sstevel@tonic-gate 	 * this library call does not return a PID, so we have to kill
16837c478bd9Sstevel@tonic-gate 	 * the whole process group.  To avoid killing our parent, we
16847c478bd9Sstevel@tonic-gate 	 * become a process group leader here.  But doing so can wreak
16857c478bd9Sstevel@tonic-gate 	 * havoc with reading from stdin when launched by a non-job-control
16867c478bd9Sstevel@tonic-gate 	 * shell, so we close stdin and reopen it as /dev/null first.
16877c478bd9Sstevel@tonic-gate 	 */
16887c478bd9Sstevel@tonic-gate 	(void) close(STDIN_FILENO);
16899acbbeafSnn35248 	(void) openat(STDIN_FILENO, "/dev/null", O_RDONLY);
16909acbbeafSnn35248 	if (!zoneadm_is_nested)
16917c478bd9Sstevel@tonic-gate 		(void) setpgid(0, 0);
16927c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, sigterm);
16937c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, sigterm);
16947c478bd9Sstevel@tonic-gate 	file = popen(cmdbuf, "r");
16957c478bd9Sstevel@tonic-gate 	for (;;) {
16967c478bd9Sstevel@tonic-gate 		if (child_killed || fgets(inbuf, sizeof (inbuf), file) == NULL)
16977c478bd9Sstevel@tonic-gate 			break;
16987c478bd9Sstevel@tonic-gate 		(void) fputs(inbuf, stdout);
16997c478bd9Sstevel@tonic-gate 	}
17007c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, SIG_DFL);
17017c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, SIG_DFL);
17027c478bd9Sstevel@tonic-gate 	return (pclose(file));
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate static int
17069acbbeafSnn35248 do_subproc_interactive(char *cmdbuf)
17079acbbeafSnn35248 {
17089acbbeafSnn35248 	void (*saveint)(int);
17099acbbeafSnn35248 	void (*saveterm)(int);
17109acbbeafSnn35248 	void (*savequit)(int);
17119acbbeafSnn35248 	void (*savehup)(int);
17129acbbeafSnn35248 	int pid, child, status;
17139acbbeafSnn35248 
17149acbbeafSnn35248 	/*
17159acbbeafSnn35248 	 * do_subproc() links stdin to /dev/null, which would break any
17169acbbeafSnn35248 	 * interactive subprocess we try to launch here.  Similarly, we
17179acbbeafSnn35248 	 * can't have been launched as a subprocess ourselves.
17189acbbeafSnn35248 	 */
17199acbbeafSnn35248 	assert(do_subproc_cnt == 0 && !zoneadm_is_nested);
17209acbbeafSnn35248 
17219acbbeafSnn35248 	if ((child = vfork()) == 0) {
17229acbbeafSnn35248 		(void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
17239acbbeafSnn35248 	}
17249acbbeafSnn35248 
17259acbbeafSnn35248 	if (child == -1)
17269acbbeafSnn35248 		return (-1);
17279acbbeafSnn35248 
17289acbbeafSnn35248 	saveint = sigset(SIGINT, SIG_IGN);
17299acbbeafSnn35248 	saveterm = sigset(SIGTERM, SIG_IGN);
17309acbbeafSnn35248 	savequit = sigset(SIGQUIT, SIG_IGN);
17319acbbeafSnn35248 	savehup = sigset(SIGHUP, SIG_IGN);
17329acbbeafSnn35248 
17339acbbeafSnn35248 	while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
17349acbbeafSnn35248 		;
17359acbbeafSnn35248 
17369acbbeafSnn35248 	(void) sigset(SIGINT, saveint);
17379acbbeafSnn35248 	(void) sigset(SIGTERM, saveterm);
17389acbbeafSnn35248 	(void) sigset(SIGQUIT, savequit);
17399acbbeafSnn35248 	(void) sigset(SIGHUP, savehup);
17409acbbeafSnn35248 
17419acbbeafSnn35248 	return (pid == -1 ? -1 : status);
17429acbbeafSnn35248 }
17439acbbeafSnn35248 
17449acbbeafSnn35248 static int
17459acbbeafSnn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
17467c478bd9Sstevel@tonic-gate {
17477c478bd9Sstevel@tonic-gate 	if (WIFEXITED(status)) {
17487c478bd9Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
17497c478bd9Sstevel@tonic-gate 
17509acbbeafSnn35248 		if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
17517c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' failed with exit code %d."), cmd,
17527c478bd9Sstevel@tonic-gate 			    exit_code);
17539acbbeafSnn35248 
17549acbbeafSnn35248 		return (exit_code);
17557c478bd9Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
17567c478bd9Sstevel@tonic-gate 		int signal = WTERMSIG(status);
17577c478bd9Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
17607c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
17617c478bd9Sstevel@tonic-gate 			    sigstr);
17627c478bd9Sstevel@tonic-gate 		} else {
17637c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
17647c478bd9Sstevel@tonic-gate 			    cmd);
17657c478bd9Sstevel@tonic-gate 		}
17667c478bd9Sstevel@tonic-gate 	} else {
17677c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
17687c478bd9Sstevel@tonic-gate 	}
17699acbbeafSnn35248 
17709acbbeafSnn35248 	/*
17719acbbeafSnn35248 	 * Assume a subprocess that died due to a signal or an unknown error
17729acbbeafSnn35248 	 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
17739acbbeafSnn35248 	 * user will likely need to do some manual cleanup.
17749acbbeafSnn35248 	 */
17759acbbeafSnn35248 	return (ZONE_SUBPROC_FATAL);
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate /*
17797c478bd9Sstevel@tonic-gate  * Various sanity checks; make sure:
17807c478bd9Sstevel@tonic-gate  * 1. We're in the global zone.
17817c478bd9Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
17827c478bd9Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
17837c478bd9Sstevel@tonic-gate  *    "SUNW".
17847c478bd9Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
17857c478bd9Sstevel@tonic-gate  *     zone, the name service knows about it.
17867c478bd9Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
17877c478bd9Sstevel@tonic-gate  *     not already running (or ready).
17887c478bd9Sstevel@tonic-gate  */
17897c478bd9Sstevel@tonic-gate static int
17907c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
17919acbbeafSnn35248     boolean_t unsafe_when_running, boolean_t force)
17927c478bd9Sstevel@tonic-gate {
17937c478bd9Sstevel@tonic-gate 	zone_entry_t *zent;
17947c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
17959acbbeafSnn35248 	zone_state_t state, min_state;
1796108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
1797108322fbScarlsonj 	FILE *fp;
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
1800ffbafc53Scomay 		switch (cmd_num) {
1801ffbafc53Scomay 		case CMD_HALT:
1802ffbafc53Scomay 			zerror(gettext("use %s to %s this zone."), "halt(1M)",
18037c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num));
1804ffbafc53Scomay 			break;
1805ffbafc53Scomay 		case CMD_REBOOT:
1806ffbafc53Scomay 			zerror(gettext("use %s to %s this zone."),
1807ffbafc53Scomay 			    "reboot(1M)", cmd_to_str(cmd_num));
1808ffbafc53Scomay 			break;
1809ffbafc53Scomay 		default:
1810ffbafc53Scomay 			zerror(gettext("must be in the global zone to %s a "
1811ffbafc53Scomay 			    "zone."), cmd_to_str(cmd_num));
1812ffbafc53Scomay 			break;
1813ffbafc53Scomay 		}
18147c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18157c478bd9Sstevel@tonic-gate 	}
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
18187c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
18197c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18207c478bd9Sstevel@tonic-gate 	}
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
18237c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
18247c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
18257c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
18297c478bd9Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
18307c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
18317c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
18327c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18337c478bd9Sstevel@tonic-gate 	}
18347c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
18377c478bd9Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
18387c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18397c478bd9Sstevel@tonic-gate 	}
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
18427c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
18437c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
18447c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18457c478bd9Sstevel@tonic-gate 	}
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
18487c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
18497c478bd9Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
18507c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18517c478bd9Sstevel@tonic-gate 	}
18527c478bd9Sstevel@tonic-gate 
18539acbbeafSnn35248 	if (!is_native_zone && cmd_num == CMD_MOUNT) {
18549acbbeafSnn35248 		zerror(gettext("%s operation is invalid for branded zones."),
18559acbbeafSnn35248 		    cmd_to_str(cmd_num));
18569acbbeafSnn35248 			return (Z_ERR);
18579acbbeafSnn35248 	}
18589acbbeafSnn35248 
1859108322fbScarlsonj 	if (!zonecfg_in_alt_root()) {
1860108322fbScarlsonj 		zent = lookup_running_zone(zone);
1861108322fbScarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1862108322fbScarlsonj 		zent = NULL;
1863108322fbScarlsonj 	} else {
1864108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1865108322fbScarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1866108322fbScarlsonj 			zent = lookup_running_zone(kernzone);
1867108322fbScarlsonj 		else
1868108322fbScarlsonj 			zent = NULL;
1869108322fbScarlsonj 		zonecfg_close_scratch(fp);
1870108322fbScarlsonj 	}
1871108322fbScarlsonj 
18727c478bd9Sstevel@tonic-gate 	/*
18737c478bd9Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
18747c478bd9Sstevel@tonic-gate 	 */
18759acbbeafSnn35248 	if (running && !force) {
18767c478bd9Sstevel@tonic-gate 		if (zent == NULL) {
18777c478bd9Sstevel@tonic-gate 			zerror(gettext("not running"));
18787c478bd9Sstevel@tonic-gate 			return (Z_ERR);
18797c478bd9Sstevel@tonic-gate 		}
18807c478bd9Sstevel@tonic-gate 	} else {
18817c478bd9Sstevel@tonic-gate 		int err;
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
18847c478bd9Sstevel@tonic-gate 			/* check whether the zone is ready or running */
18857c478bd9Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
18867c478bd9Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
18877c478bd9Sstevel@tonic-gate 				errno = err;
18887c478bd9Sstevel@tonic-gate 				zperror2(zent->zname,
18897c478bd9Sstevel@tonic-gate 				    gettext("could not get state"));
18907c478bd9Sstevel@tonic-gate 				/* can't tell, so hedge */
18917c478bd9Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
18927c478bd9Sstevel@tonic-gate 			} else {
18937c478bd9Sstevel@tonic-gate 				zent->zstate_str =
18947c478bd9Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
18957c478bd9Sstevel@tonic-gate 			}
18967c478bd9Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
18977c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
18987c478bd9Sstevel@tonic-gate 			return (Z_ERR);
18997c478bd9Sstevel@tonic-gate 		}
19007c478bd9Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
19017c478bd9Sstevel@tonic-gate 			errno = err;
19027c478bd9Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
19037c478bd9Sstevel@tonic-gate 			return (Z_ERR);
19047c478bd9Sstevel@tonic-gate 		}
19057c478bd9Sstevel@tonic-gate 		switch (cmd_num) {
19067c478bd9Sstevel@tonic-gate 		case CMD_UNINSTALL:
19077c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
19087c478bd9Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
19097c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
19107c478bd9Sstevel@tonic-gate 				return (Z_ERR);
19117c478bd9Sstevel@tonic-gate 			}
19127c478bd9Sstevel@tonic-gate 			break;
1913ee519a1fSgjelinek 		case CMD_ATTACH:
1914865e09a4Sgjelinek 		case CMD_CLONE:
19157c478bd9Sstevel@tonic-gate 		case CMD_INSTALL:
19167c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
19177c478bd9Sstevel@tonic-gate 				zerror(gettext("is already %s."),
19187c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
19197c478bd9Sstevel@tonic-gate 				return (Z_ERR);
19207c478bd9Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
19217c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
19227c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
19237c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
19247c478bd9Sstevel@tonic-gate 				return (Z_ERR);
19257c478bd9Sstevel@tonic-gate 			}
19267c478bd9Sstevel@tonic-gate 			break;
1927ee519a1fSgjelinek 		case CMD_DETACH:
1928865e09a4Sgjelinek 		case CMD_MOVE:
19297c478bd9Sstevel@tonic-gate 		case CMD_READY:
19307c478bd9Sstevel@tonic-gate 		case CMD_BOOT:
1931108322fbScarlsonj 		case CMD_MOUNT:
1932555afedfScarlsonj 		case CMD_MARK:
19339acbbeafSnn35248 			if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
19349acbbeafSnn35248 			    force)
19359acbbeafSnn35248 				min_state = ZONE_STATE_INCOMPLETE;
19369acbbeafSnn35248 			else
19379acbbeafSnn35248 				min_state = ZONE_STATE_INSTALLED;
19389acbbeafSnn35248 
19399acbbeafSnn35248 			if (force && cmd_num == CMD_BOOT && is_native_zone) {
19409acbbeafSnn35248 				zerror(gettext("Only branded zones may be "
19419acbbeafSnn35248 				    "force-booted."));
19429acbbeafSnn35248 				return (Z_ERR);
19439acbbeafSnn35248 			}
19449acbbeafSnn35248 
19459acbbeafSnn35248 			if (state < min_state) {
19467c478bd9Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
19479acbbeafSnn35248 				    zone_state_str(min_state),
19487c478bd9Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
19497c478bd9Sstevel@tonic-gate 				return (Z_ERR);
19507c478bd9Sstevel@tonic-gate 			}
19517c478bd9Sstevel@tonic-gate 			break;
19527c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
19537c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
19547c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
19557c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
19567c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
19577c478bd9Sstevel@tonic-gate 				return (Z_ERR);
19587c478bd9Sstevel@tonic-gate 			}
19597c478bd9Sstevel@tonic-gate 			break;
1960108322fbScarlsonj 		case CMD_UNMOUNT:
1961108322fbScarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1962108322fbScarlsonj 				zerror(gettext("must be %s before %s."),
1963108322fbScarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1964108322fbScarlsonj 				    cmd_to_str(cmd_num));
1965108322fbScarlsonj 				return (Z_ERR);
1966108322fbScarlsonj 			}
1967108322fbScarlsonj 			break;
19687c478bd9Sstevel@tonic-gate 		}
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 	return (Z_OK);
19717c478bd9Sstevel@tonic-gate }
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate static int
19747c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[])
19757c478bd9Sstevel@tonic-gate {
19767c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
19777c478bd9Sstevel@tonic-gate 	int arg;
19787c478bd9Sstevel@tonic-gate 
1979108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1980108322fbScarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1981108322fbScarlsonj 		return (Z_ERR);
1982108322fbScarlsonj 	}
1983108322fbScarlsonj 
19847c478bd9Sstevel@tonic-gate 	optind = 0;
19857c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
19867c478bd9Sstevel@tonic-gate 		switch (arg) {
19877c478bd9Sstevel@tonic-gate 		case '?':
19887c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
19897c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
19907c478bd9Sstevel@tonic-gate 		default:
19917c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
19927c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
19937c478bd9Sstevel@tonic-gate 		}
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate 	if (argc > optind) {
19967c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
19977c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
19987c478bd9Sstevel@tonic-gate 	}
19997c478bd9Sstevel@tonic-gate 	/*
20007c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
20017c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
20027c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
20037c478bd9Sstevel@tonic-gate 	 */
20049acbbeafSnn35248 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
20059acbbeafSnn35248 	    != Z_OK)
20067c478bd9Sstevel@tonic-gate 		return (Z_ERR);
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
20097c478bd9Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
20107c478bd9Sstevel@tonic-gate }
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate static int
20137c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[])
20147c478bd9Sstevel@tonic-gate {
20157c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
20167c478bd9Sstevel@tonic-gate 	int arg;
20177c478bd9Sstevel@tonic-gate 
2018108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2019108322fbScarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
2020108322fbScarlsonj 		return (Z_ERR);
2021108322fbScarlsonj 	}
2022108322fbScarlsonj 
20237c478bd9Sstevel@tonic-gate 	optind = 0;
20247c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
20257c478bd9Sstevel@tonic-gate 		switch (arg) {
20267c478bd9Sstevel@tonic-gate 		case '?':
20277c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
20287c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
20297c478bd9Sstevel@tonic-gate 		default:
20307c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
20317c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 	}
20343f2f09c1Sdp 
20353f2f09c1Sdp 	zarg.bootbuf[0] = '\0';
20363f2f09c1Sdp 	for (; optind < argc; optind++) {
20373f2f09c1Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
20383f2f09c1Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
20393f2f09c1Sdp 			zerror(gettext("Boot argument list too long"));
20403f2f09c1Sdp 			return (Z_ERR);
20417c478bd9Sstevel@tonic-gate 		}
20423f2f09c1Sdp 		if (optind < argc - 1)
20433f2f09c1Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
20443f2f09c1Sdp 			    sizeof (zarg.bootbuf)) {
20453f2f09c1Sdp 				zerror(gettext("Boot argument list too long"));
20463f2f09c1Sdp 				return (Z_ERR);
20473f2f09c1Sdp 			}
20483f2f09c1Sdp 	}
20493f2f09c1Sdp 
20503f2f09c1Sdp 
20517c478bd9Sstevel@tonic-gate 	/*
20527c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
20537c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
20547c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
20557c478bd9Sstevel@tonic-gate 	 */
20569acbbeafSnn35248 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
20579acbbeafSnn35248 	    != Z_OK)
20587c478bd9Sstevel@tonic-gate 		return (Z_ERR);
2059b5abaf04Sgjelinek 	if (verify_details(CMD_REBOOT) != Z_OK)
2060b5abaf04Sgjelinek 		return (Z_ERR);
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
20637c478bd9Sstevel@tonic-gate 	return ((call_zoneadmd(target_zone, &zarg) == 0) ? Z_OK : Z_ERR);
20647c478bd9Sstevel@tonic-gate }
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate static int
20679acbbeafSnn35248 verify_brand(zone_dochandle_t handle)
20689acbbeafSnn35248 {
20699acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
20709acbbeafSnn35248 	int err;
20719acbbeafSnn35248 	char zonepath[MAXPATHLEN];
2072123807fbSedp 	brand_handle_t bh = NULL;
20739acbbeafSnn35248 	int status;
20749acbbeafSnn35248 
20759acbbeafSnn35248 	/*
20769acbbeafSnn35248 	 * Fetch the verify command from the brand configuration.
20779acbbeafSnn35248 	 * "exec" the command so that the returned status is that of
20789acbbeafSnn35248 	 * the command and not the shell.
20799acbbeafSnn35248 	 */
20809acbbeafSnn35248 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
20819acbbeafSnn35248 	    Z_OK) {
20829acbbeafSnn35248 		errno = err;
20839acbbeafSnn35248 		zperror(cmd_to_str(CMD_VERIFY), B_TRUE);
20849acbbeafSnn35248 		return (Z_ERR);
20859acbbeafSnn35248 	}
2086123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
20879acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
20889acbbeafSnn35248 		return (Z_ERR);
20899acbbeafSnn35248 	}
20909acbbeafSnn35248 
20919acbbeafSnn35248 	/*
20929acbbeafSnn35248 	 * If the brand has its own verification routine, execute it now.
20939acbbeafSnn35248 	 */
20949acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
2095123807fbSedp 	err = brand_get_verify_adm(bh, target_zone, zonepath,
20969acbbeafSnn35248 	    cmdbuf + EXEC_LEN, sizeof (cmdbuf) - EXEC_LEN, 0, NULL);
2097123807fbSedp 	brand_close(bh);
20989acbbeafSnn35248 	if (err == 0 && strlen(cmdbuf) > EXEC_LEN) {
20999acbbeafSnn35248 		status = do_subproc_interactive(cmdbuf);
21009acbbeafSnn35248 		err = subproc_status(gettext("brand-specific verification"),
21019acbbeafSnn35248 		    status, B_FALSE);
21029acbbeafSnn35248 		return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
21039acbbeafSnn35248 	}
21049acbbeafSnn35248 
21059acbbeafSnn35248 	return ((err == Z_OK) ? Z_OK : Z_BRAND_ERROR);
21069acbbeafSnn35248 }
21079acbbeafSnn35248 
21089acbbeafSnn35248 static int
21097c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
21107c478bd9Sstevel@tonic-gate {
21117c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
21127c478bd9Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
21137c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk;
21147c478bd9Sstevel@tonic-gate 	int error = Z_INVAL;
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
21177c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
21187c478bd9Sstevel@tonic-gate 		    strerror(errno));
21197c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
21207c478bd9Sstevel@tonic-gate 	}
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
21237c478bd9Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
21247c478bd9Sstevel@tonic-gate 		free(rctlblk);
21257c478bd9Sstevel@tonic-gate 		return (error);
21267c478bd9Sstevel@tonic-gate 	}
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
21297c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
21307c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
21317c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
21347c478bd9Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
21357c478bd9Sstevel@tonic-gate 			    "'%s'."),  name);
21367c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
21377c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
21387c478bd9Sstevel@tonic-gate 			continue;
21397c478bd9Sstevel@tonic-gate 		}
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
21427c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
21437c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
21447c478bd9Sstevel@tonic-gate 			    != Z_OK) {
21457c478bd9Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
21467c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
21477c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
21487c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
21497c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
21507c478bd9Sstevel@tonic-gate 				goto out;
21517c478bd9Sstevel@tonic-gate 			}
21527c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
21537c478bd9Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
21547c478bd9Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
21557c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
21567c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
21577c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
21587c478bd9Sstevel@tonic-gate 				    name);
21597c478bd9Sstevel@tonic-gate 				goto out;
21607c478bd9Sstevel@tonic-gate 			}
21617c478bd9Sstevel@tonic-gate 		}
21627c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
21637c478bd9Sstevel@tonic-gate 	}
21647c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
21657c478bd9Sstevel@tonic-gate 	error = Z_OK;
21667c478bd9Sstevel@tonic-gate out:
21677c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
21687c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
21697c478bd9Sstevel@tonic-gate 	free(rctlblk);
21707c478bd9Sstevel@tonic-gate 	return (error);
21717c478bd9Sstevel@tonic-gate }
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate static int
21747c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
21757c478bd9Sstevel@tonic-gate {
21767c478bd9Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
21777c478bd9Sstevel@tonic-gate 	pool_conf_t *poolconf;
21787c478bd9Sstevel@tonic-gate 	pool_t *pool;
21797c478bd9Sstevel@tonic-gate 	int status;
21807c478bd9Sstevel@tonic-gate 	int error;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 	/*
21837c478bd9Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
21847c478bd9Sstevel@tonic-gate 	 */
21857c478bd9Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
21867c478bd9Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
21877c478bd9Sstevel@tonic-gate 		/*
21887c478bd9Sstevel@tonic-gate 		 * No pool specified.
21897c478bd9Sstevel@tonic-gate 		 */
21907c478bd9Sstevel@tonic-gate 		return (0);
21917c478bd9Sstevel@tonic-gate 	}
21927c478bd9Sstevel@tonic-gate 	if (error != Z_OK) {
21937c478bd9Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
21947c478bd9Sstevel@tonic-gate 		    "configuration"), B_TRUE);
21957c478bd9Sstevel@tonic-gate 		return (error);
21967c478bd9Sstevel@tonic-gate 	}
21977c478bd9Sstevel@tonic-gate 	/*
21987c478bd9Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
21997c478bd9Sstevel@tonic-gate 	 */
22007c478bd9Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
22017c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
22027c478bd9Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
22037c478bd9Sstevel@tonic-gate 		return (Z_OK);
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate 	/*
22067c478bd9Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
22077c478bd9Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
22087c478bd9Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
22097c478bd9Sstevel@tonic-gate 	 * hence they are not considered errors.
22107c478bd9Sstevel@tonic-gate 	 */
22117c478bd9Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
22127c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
22137c478bd9Sstevel@tonic-gate 		    "using default pool"));
22147c478bd9Sstevel@tonic-gate 		return (Z_OK);
22157c478bd9Sstevel@tonic-gate 	}
22167c478bd9Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
22177c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
22187c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
22197c478bd9Sstevel@tonic-gate 		    "using default pool"));
22207c478bd9Sstevel@tonic-gate 		pool_conf_free(poolconf);
22217c478bd9Sstevel@tonic-gate 		return (Z_OK);
22227c478bd9Sstevel@tonic-gate 	}
22237c478bd9Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
22247c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
22257c478bd9Sstevel@tonic-gate 	pool_conf_free(poolconf);
22267c478bd9Sstevel@tonic-gate 	if (pool == NULL) {
22277c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
22287c478bd9Sstevel@tonic-gate 		    "using default pool"), poolname);
22297c478bd9Sstevel@tonic-gate 	}
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	return (Z_OK);
22327c478bd9Sstevel@tonic-gate }
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate static int
2235b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle)
2236b5abaf04Sgjelinek {
2237b5abaf04Sgjelinek 	int return_code = Z_OK;
2238b5abaf04Sgjelinek 	struct zone_fstab fstab;
2239b5abaf04Sgjelinek 	struct stat st;
2240b5abaf04Sgjelinek 	char specdir[MAXPATHLEN];
2241b5abaf04Sgjelinek 
2242b5abaf04Sgjelinek 	if (zonecfg_setipdent(handle) != Z_OK) {
22435ee84fbdSgjelinek 		/*
22445ee84fbdSgjelinek 		 * TRANSLATION_NOTE
22455ee84fbdSgjelinek 		 * inherit-pkg-dirs is a literal that should not be translated.
22465ee84fbdSgjelinek 		 */
22475ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("could not verify "
2248b5abaf04Sgjelinek 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
2249b5abaf04Sgjelinek 		return (Z_ERR);
2250b5abaf04Sgjelinek 	}
2251b5abaf04Sgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
2252b5abaf04Sgjelinek 		/*
2253b5abaf04Sgjelinek 		 * Verify fs_dir exists.
2254b5abaf04Sgjelinek 		 */
2255b5abaf04Sgjelinek 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
2256b5abaf04Sgjelinek 		    zonecfg_get_root(), fstab.zone_fs_dir);
2257b5abaf04Sgjelinek 		if (stat(specdir, &st) != 0) {
22585ee84fbdSgjelinek 			/*
22595ee84fbdSgjelinek 			 * TRANSLATION_NOTE
22605ee84fbdSgjelinek 			 * inherit-pkg-dir is a literal that should not be
22615ee84fbdSgjelinek 			 * translated.
22625ee84fbdSgjelinek 			 */
22635ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2264b5abaf04Sgjelinek 			    "inherit-pkg-dir %s: %s\n"),
2265b5abaf04Sgjelinek 			    fstab.zone_fs_dir, strerror(errno));
2266b5abaf04Sgjelinek 			return_code = Z_ERR;
2267b5abaf04Sgjelinek 		}
2268b5abaf04Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
22695ee84fbdSgjelinek 			/*
22705ee84fbdSgjelinek 			 * TRANSLATION_NOTE
22715ee84fbdSgjelinek 			 * inherit-pkg-dir and NFS are literals that should
22725ee84fbdSgjelinek 			 * not be translated.
22735ee84fbdSgjelinek 			 */
2274b5abaf04Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
22750b5de56dSgjelinek 			    "inherit-pkg-dir %s: NFS mounted file system.\n"
22760b5de56dSgjelinek 			    "\tA local file system must be used.\n"),
2277b5abaf04Sgjelinek 			    fstab.zone_fs_dir);
2278b5abaf04Sgjelinek 			return_code = Z_ERR;
2279b5abaf04Sgjelinek 		}
2280b5abaf04Sgjelinek 	}
2281b5abaf04Sgjelinek 	(void) zonecfg_endipdent(handle);
2282b5abaf04Sgjelinek 
2283b5abaf04Sgjelinek 	return (return_code);
2284b5abaf04Sgjelinek }
2285b5abaf04Sgjelinek 
228620c8013fSlling /*
228720c8013fSlling  * Verify that the special device/file system exists and is valid.
228820c8013fSlling  */
228920c8013fSlling static int
229020c8013fSlling verify_fs_special(struct zone_fstab *fstab)
229120c8013fSlling {
229220c8013fSlling 	struct stat st;
229320c8013fSlling 
22946cc813faSgjelinek 	/*
22956cc813faSgjelinek 	 * This validation is really intended for standard zone administration.
22966cc813faSgjelinek 	 * If we are in a mini-root or some other upgrade situation where
22976cc813faSgjelinek 	 * we are using the scratch zone, just by-pass this.
22986cc813faSgjelinek 	 */
22996cc813faSgjelinek 	if (zonecfg_in_alt_root())
23006cc813faSgjelinek 		return (Z_OK);
23016cc813faSgjelinek 
230220c8013fSlling 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
230320c8013fSlling 		return (verify_fs_zfs(fstab));
230420c8013fSlling 
230520c8013fSlling 	if (stat(fstab->zone_fs_special, &st) != 0) {
23065c358068Slling 		(void) fprintf(stderr, gettext("could not verify fs "
230720c8013fSlling 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
230820c8013fSlling 		    fstab->zone_fs_special, strerror(errno));
230920c8013fSlling 		return (Z_ERR);
231020c8013fSlling 	}
231120c8013fSlling 
231220c8013fSlling 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
231320c8013fSlling 		/*
231420c8013fSlling 		 * TRANSLATION_NOTE
231520c8013fSlling 		 * fs and NFS are literals that should
231620c8013fSlling 		 * not be translated.
231720c8013fSlling 		 */
231820c8013fSlling 		(void) fprintf(stderr, gettext("cannot verify "
23190b5de56dSgjelinek 		    "fs %s: NFS mounted file system.\n"
23200b5de56dSgjelinek 		    "\tA local file system must be used.\n"),
232120c8013fSlling 		    fstab->zone_fs_special);
232220c8013fSlling 		return (Z_ERR);
232320c8013fSlling 	}
232420c8013fSlling 
232520c8013fSlling 	return (Z_OK);
232620c8013fSlling }
232720c8013fSlling 
2328b5abaf04Sgjelinek static int
23297c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
23307c478bd9Sstevel@tonic-gate {
23317c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
23327c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
23337c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
23347c478bd9Sstevel@tonic-gate 	struct stat st;
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	/*
23377c478bd9Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
23387c478bd9Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
23397c478bd9Sstevel@tonic-gate 	 * verified for regular file systems below.
23407c478bd9Sstevel@tonic-gate 	 *
23417c478bd9Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
23427c478bd9Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
23437c478bd9Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
23447c478bd9Sstevel@tonic-gate 	 */
23457c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
23460b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify file systems: "
23477c478bd9Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
23487c478bd9Sstevel@tonic-gate 		return (Z_ERR);
23497c478bd9Sstevel@tonic-gate 	}
23507c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
23517c478bd9Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
23527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
23537c478bd9Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
23547c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
23557c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
23567c478bd9Sstevel@tonic-gate 			goto next_fs;
23577c478bd9Sstevel@tonic-gate 		}
23587c478bd9Sstevel@tonic-gate 		/*
23597c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
23607c478bd9Sstevel@tonic-gate 		 */
23617c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
23627c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
23637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
23647c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
23657c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
23667c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
23677c478bd9Sstevel@tonic-gate 			goto next_fs;
23687c478bd9Sstevel@tonic-gate 		}
23697c478bd9Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
23705ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
23715ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
23727c478bd9Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
23737c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
23747c478bd9Sstevel@tonic-gate 			goto next_fs;
23757c478bd9Sstevel@tonic-gate 		}
23767c478bd9Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
23775ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
23785ee84fbdSgjelinek 			    "%s: %s is not a regular file\n"),
23795ee84fbdSgjelinek 			    fstab.zone_fs_dir, cmdbuf);
23807c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
23817c478bd9Sstevel@tonic-gate 			goto next_fs;
23827c478bd9Sstevel@tonic-gate 		}
23837c478bd9Sstevel@tonic-gate 		/*
23847c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/fsck exists iff zone_fs_raw is
23857c478bd9Sstevel@tonic-gate 		 * set.
23867c478bd9Sstevel@tonic-gate 		 */
23877c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
23887c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
23897c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
23907c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
23917c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
23927c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
23937c478bd9Sstevel@tonic-gate 			goto next_fs;
23947c478bd9Sstevel@tonic-gate 		}
23957c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] == '\0' && stat(cmdbuf, &st) == 0) {
23965ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
23975ee84fbdSgjelinek 			    "%s: must specify 'raw' device for %s "
23980b5de56dSgjelinek 			    "file systems\n"),
23997c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
24007c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24017c478bd9Sstevel@tonic-gate 			goto next_fs;
24027c478bd9Sstevel@tonic-gate 		}
24037c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
24047c478bd9Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
24057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
24067c478bd9Sstevel@tonic-gate 			    "'raw' device specified but "
24077c478bd9Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
24087c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
24097c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
24107c478bd9Sstevel@tonic-gate 			goto next_fs;
24117c478bd9Sstevel@tonic-gate 		}
241220c8013fSlling 
241320c8013fSlling 		/* Verify fs_special. */
241420c8013fSlling 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2415b5abaf04Sgjelinek 			goto next_fs;
241620c8013fSlling 
241720c8013fSlling 		/* Verify fs_raw. */
2418b5abaf04Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2419b5abaf04Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
24205ee84fbdSgjelinek 			/*
24215ee84fbdSgjelinek 			 * TRANSLATION_NOTE
24225ee84fbdSgjelinek 			 * fs is a literal that should not be translated.
24235ee84fbdSgjelinek 			 */
24245ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
24255ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2426b5abaf04Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2427b5abaf04Sgjelinek 			return_code = Z_ERR;
2428b5abaf04Sgjelinek 			goto next_fs;
2429b5abaf04Sgjelinek 		}
24307c478bd9Sstevel@tonic-gate next_fs:
24317c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
24327c478bd9Sstevel@tonic-gate 	}
24337c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 	return (return_code);
24367c478bd9Sstevel@tonic-gate }
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate static int
2439ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle)
2440ffbafc53Scomay {
2441ffbafc53Scomay 	char *privname = NULL;
2442ffbafc53Scomay 	int err;
2443ffbafc53Scomay 	priv_set_t *privs;
2444ffbafc53Scomay 
2445ffbafc53Scomay 	if ((privs = priv_allocset()) == NULL) {
2446ffbafc53Scomay 		zperror(gettext("failed to allocate privilege set"), B_FALSE);
2447ffbafc53Scomay 		return (Z_NOMEM);
2448ffbafc53Scomay 	}
2449ffbafc53Scomay 	err = zonecfg_get_privset(handle, privs, &privname);
2450ffbafc53Scomay 	switch (err) {
2451ffbafc53Scomay 	case Z_OK:
2452ffbafc53Scomay 		break;
2453ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2454ffbafc53Scomay 		(void) fprintf(stderr, gettext("privilege \"%s\" is not "
2455ffbafc53Scomay 		    "permitted within the zone's privilege set\n"), privname);
2456ffbafc53Scomay 		break;
2457ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2458ffbafc53Scomay 		(void) fprintf(stderr, gettext("required privilege \"%s\" is "
2459ffbafc53Scomay 		    "missing from the zone's privilege set\n"), privname);
2460ffbafc53Scomay 		break;
2461ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2462ffbafc53Scomay 		(void) fprintf(stderr, gettext("unknown privilege \"%s\" "
2463ffbafc53Scomay 		    "specified in the zone's privilege set\n"), privname);
2464ffbafc53Scomay 		break;
2465ffbafc53Scomay 	default:
2466ffbafc53Scomay 		zperror(
2467ffbafc53Scomay 		    gettext("failed to determine the zone's privilege set"),
2468ffbafc53Scomay 		    B_TRUE);
2469ffbafc53Scomay 		break;
2470ffbafc53Scomay 	}
2471ffbafc53Scomay 	free(privname);
2472ffbafc53Scomay 	priv_freeset(privs);
2473ffbafc53Scomay 	return (err);
2474ffbafc53Scomay }
2475ffbafc53Scomay 
24761390a385Sgjelinek static void
24771390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list)
24781390a385Sgjelinek {
24791390a385Sgjelinek 	int		i;
24801390a385Sgjelinek 
24811390a385Sgjelinek 	for (i = 0; i < if_cnt; i++) {
24821390a385Sgjelinek 		free(if_list[i]->name);
24831390a385Sgjelinek 		free(if_list[i]);
24841390a385Sgjelinek 	}
24851390a385Sgjelinek 	free(if_list);
24861390a385Sgjelinek }
24871390a385Sgjelinek 
24881390a385Sgjelinek /*
24891390a385Sgjelinek  * Get a list of the network interfaces, along with their address families,
24901390a385Sgjelinek  * that are plumbed in the global zone.  See if_tcp(7p) for a description
24911390a385Sgjelinek  * of the ioctls used here.
24921390a385Sgjelinek  */
24931390a385Sgjelinek static int
24941390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list)
24951390a385Sgjelinek {
24961390a385Sgjelinek 	int		s;
24971390a385Sgjelinek 	int		i;
24981390a385Sgjelinek 	int		res = Z_OK;
24991390a385Sgjelinek 	int		space_needed;
25001390a385Sgjelinek 	int		cnt = 0;
25011390a385Sgjelinek 	struct		lifnum if_num;
25021390a385Sgjelinek 	struct		lifconf if_conf;
25031390a385Sgjelinek 	struct		lifreq *if_reqp;
25041390a385Sgjelinek 	char		*if_buf;
25051390a385Sgjelinek 	struct net_if	**local_ifs = NULL;
25061390a385Sgjelinek 
25071390a385Sgjelinek 	*if_cnt = 0;
25081390a385Sgjelinek 	*if_list = NULL;
25091390a385Sgjelinek 
25101390a385Sgjelinek 	if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
25111390a385Sgjelinek 		return (Z_ERR);
25121390a385Sgjelinek 
25131390a385Sgjelinek 	/*
25141390a385Sgjelinek 	 * Come back here in the unlikely event that the number of interfaces
25151390a385Sgjelinek 	 * increases between the time we get the count and the time we do the
25161390a385Sgjelinek 	 * SIOCGLIFCONF ioctl.
25171390a385Sgjelinek 	 */
25181390a385Sgjelinek retry:
25191390a385Sgjelinek 	/* Get the number of interfaces. */
25201390a385Sgjelinek 	if_num.lifn_family = AF_UNSPEC;
25211390a385Sgjelinek 	if_num.lifn_flags = LIFC_NOXMIT;
25221390a385Sgjelinek 	if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
25231390a385Sgjelinek 		(void) close(s);
25241390a385Sgjelinek 		return (Z_ERR);
25251390a385Sgjelinek 	}
25261390a385Sgjelinek 
25271390a385Sgjelinek 	/* Get the interface configuration list. */
25281390a385Sgjelinek 	space_needed = if_num.lifn_count * sizeof (struct lifreq);
25291390a385Sgjelinek 	if ((if_buf = malloc(space_needed)) == NULL) {
25301390a385Sgjelinek 		(void) close(s);
25311390a385Sgjelinek 		return (Z_ERR);
25321390a385Sgjelinek 	}
25331390a385Sgjelinek 	if_conf.lifc_family = AF_UNSPEC;
25341390a385Sgjelinek 	if_conf.lifc_flags = LIFC_NOXMIT;
25351390a385Sgjelinek 	if_conf.lifc_len = space_needed;
25361390a385Sgjelinek 	if_conf.lifc_buf = if_buf;
25371390a385Sgjelinek 	if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
25381390a385Sgjelinek 		free(if_buf);
25391390a385Sgjelinek 		/*
25401390a385Sgjelinek 		 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
25411390a385Sgjelinek 		 * too small.  In this case go back and get the new if cnt.
25421390a385Sgjelinek 		 */
25431390a385Sgjelinek 		if (errno == EINVAL)
25441390a385Sgjelinek 			goto retry;
25451390a385Sgjelinek 
25461390a385Sgjelinek 		(void) close(s);
25471390a385Sgjelinek 		return (Z_ERR);
25481390a385Sgjelinek 	}
25491390a385Sgjelinek 	(void) close(s);
25501390a385Sgjelinek 
25511390a385Sgjelinek 	/* Get the name and address family for each interface. */
25521390a385Sgjelinek 	if_reqp = if_conf.lifc_req;
25531390a385Sgjelinek 	for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
25541390a385Sgjelinek 		struct net_if	**p;
25551390a385Sgjelinek 		struct lifreq	req;
25561390a385Sgjelinek 
25571390a385Sgjelinek 		if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
25581390a385Sgjelinek 			if_reqp++;
25591390a385Sgjelinek 			continue;
25601390a385Sgjelinek 		}
25611390a385Sgjelinek 
25621390a385Sgjelinek 		if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
25631390a385Sgjelinek 		    SOCK_DGRAM, 0)) == -1) {
25641390a385Sgjelinek 			res = Z_ERR;
25651390a385Sgjelinek 			break;
25661390a385Sgjelinek 		}
25671390a385Sgjelinek 
25681390a385Sgjelinek 		(void) strncpy(req.lifr_name, if_reqp->lifr_name,
25691390a385Sgjelinek 		    sizeof (req.lifr_name));
25701390a385Sgjelinek 		if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
25711390a385Sgjelinek 			(void) close(s);
25721390a385Sgjelinek 			if_reqp++;
25731390a385Sgjelinek 			continue;
25741390a385Sgjelinek 		}
25751390a385Sgjelinek 
25761390a385Sgjelinek 		if ((p = (struct net_if **)realloc(local_ifs,
25771390a385Sgjelinek 		    sizeof (struct net_if *) * (cnt + 1))) == NULL) {
25781390a385Sgjelinek 			res = Z_ERR;
25791390a385Sgjelinek 			break;
25801390a385Sgjelinek 		}
25811390a385Sgjelinek 		local_ifs = p;
25821390a385Sgjelinek 
25831390a385Sgjelinek 		if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
25841390a385Sgjelinek 			res = Z_ERR;
25851390a385Sgjelinek 			break;
25861390a385Sgjelinek 		}
25871390a385Sgjelinek 
25881390a385Sgjelinek 		if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
25891390a385Sgjelinek 		    == NULL) {
25901390a385Sgjelinek 			free(local_ifs[cnt]);
25911390a385Sgjelinek 			res = Z_ERR;
25921390a385Sgjelinek 			break;
25931390a385Sgjelinek 		}
25941390a385Sgjelinek 		local_ifs[cnt]->af = req.lifr_addr.ss_family;
25951390a385Sgjelinek 		cnt++;
25961390a385Sgjelinek 
25971390a385Sgjelinek 		(void) close(s);
25981390a385Sgjelinek 		if_reqp++;
25991390a385Sgjelinek 	}
26001390a385Sgjelinek 
26011390a385Sgjelinek 	free(if_buf);
26021390a385Sgjelinek 
26031390a385Sgjelinek 	if (res != Z_OK) {
26041390a385Sgjelinek 		free_local_netifs(cnt, local_ifs);
26051390a385Sgjelinek 	} else {
26061390a385Sgjelinek 		*if_cnt = cnt;
26071390a385Sgjelinek 		*if_list = local_ifs;
26081390a385Sgjelinek 	}
26091390a385Sgjelinek 
26101390a385Sgjelinek 	return (res);
26111390a385Sgjelinek }
26121390a385Sgjelinek 
26131390a385Sgjelinek static char *
26141390a385Sgjelinek af2str(int af)
26151390a385Sgjelinek {
26161390a385Sgjelinek 	switch (af) {
26171390a385Sgjelinek 	case AF_INET:
26181390a385Sgjelinek 		return ("IPv4");
26191390a385Sgjelinek 	case AF_INET6:
26201390a385Sgjelinek 		return ("IPv6");
26211390a385Sgjelinek 	default:
26221390a385Sgjelinek 		return ("Unknown");
26231390a385Sgjelinek 	}
26241390a385Sgjelinek }
26251390a385Sgjelinek 
26261390a385Sgjelinek /*
26271390a385Sgjelinek  * Cross check the network interface name and address family with the
26281390a385Sgjelinek  * interfaces that are set up in the global zone so that we can print the
26291390a385Sgjelinek  * appropriate error message.
26301390a385Sgjelinek  */
26311390a385Sgjelinek static void
26321390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg)
26331390a385Sgjelinek {
26341390a385Sgjelinek 	int		i;
26351390a385Sgjelinek 	int		local_if_cnt = 0;
26361390a385Sgjelinek 	struct net_if	**local_ifs = NULL;
26371390a385Sgjelinek 	boolean_t	found_if = B_FALSE;
26381390a385Sgjelinek 	boolean_t	found_af = B_FALSE;
26391390a385Sgjelinek 
26401390a385Sgjelinek 	if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
26411390a385Sgjelinek 		(void) fprintf(stderr,
26421390a385Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
26431390a385Sgjelinek 		    "net", "address", addr, "physical", phys, msg);
26441390a385Sgjelinek 		return;
26451390a385Sgjelinek 	}
26461390a385Sgjelinek 
26471390a385Sgjelinek 	for (i = 0; i < local_if_cnt; i++) {
26481390a385Sgjelinek 		if (strcmp(phys, local_ifs[i]->name) == 0) {
26491390a385Sgjelinek 			found_if = B_TRUE;
26501390a385Sgjelinek 			if (af == local_ifs[i]->af) {
26511390a385Sgjelinek 				found_af = B_TRUE;
26521390a385Sgjelinek 				break;
26531390a385Sgjelinek 			}
26541390a385Sgjelinek 		}
26551390a385Sgjelinek 	}
26561390a385Sgjelinek 
26571390a385Sgjelinek 	free_local_netifs(local_if_cnt, local_ifs);
26581390a385Sgjelinek 
26591390a385Sgjelinek 	if (!found_if) {
26601390a385Sgjelinek 		(void) fprintf(stderr,
26611390a385Sgjelinek 		    gettext("could not verify %s %s=%s\n\t"
26621390a385Sgjelinek 		    "network interface %s is not plumbed in the global zone\n"),
26631390a385Sgjelinek 		    "net", "physical", phys, phys);
26641390a385Sgjelinek 		return;
26651390a385Sgjelinek 	}
26661390a385Sgjelinek 
26671390a385Sgjelinek 	/*
26681390a385Sgjelinek 	 * Print this error if we were unable to find the address family
26691390a385Sgjelinek 	 * for this interface.  If the af variable is not initialized to
26701390a385Sgjelinek 	 * to something meaningful by the caller (not AF_UNSPEC) then we
26711390a385Sgjelinek 	 * also skip this message since it wouldn't be informative.
26721390a385Sgjelinek 	 */
26731390a385Sgjelinek 	if (!found_af && af != AF_UNSPEC) {
26741390a385Sgjelinek 		(void) fprintf(stderr,
26751390a385Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
26761390a385Sgjelinek 		    "family is not configured on this interface in the\n\t"
26771390a385Sgjelinek 		    "global zone\n"),
26781390a385Sgjelinek 		    "net", "address", addr, "physical", phys, af2str(af));
26791390a385Sgjelinek 		return;
26801390a385Sgjelinek 	}
26811390a385Sgjelinek 
26821390a385Sgjelinek 	(void) fprintf(stderr,
26831390a385Sgjelinek 	    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
26841390a385Sgjelinek 	    "net", "address", addr, "physical", phys, msg);
26851390a385Sgjelinek }
26861390a385Sgjelinek 
2687ffbafc53Scomay static int
26888cd327d5Sgjelinek verify_handle(int cmd_num, zone_dochandle_t handle)
26897c478bd9Sstevel@tonic-gate {
26907c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
26917c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
26927c478bd9Sstevel@tonic-gate 	int err;
2693108322fbScarlsonj 	boolean_t in_alt_root;
26947c478bd9Sstevel@tonic-gate 
2695108322fbScarlsonj 	in_alt_root = zonecfg_in_alt_root();
2696108322fbScarlsonj 	if (in_alt_root)
2697108322fbScarlsonj 		goto no_net;
2698108322fbScarlsonj 
26997c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
27007c478bd9Sstevel@tonic-gate 		errno = err;
27017c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
27027c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
27037c478bd9Sstevel@tonic-gate 		return (Z_ERR);
27047c478bd9Sstevel@tonic-gate 	}
27057c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
27067c478bd9Sstevel@tonic-gate 		struct lifreq lifr;
27071390a385Sgjelinek 		sa_family_t af = AF_UNSPEC;
27087c478bd9Sstevel@tonic-gate 		int so, res;
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate 		/* skip any loopback interfaces */
27117c478bd9Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
27127c478bd9Sstevel@tonic-gate 			continue;
27137c478bd9Sstevel@tonic-gate 		if ((res = zonecfg_valid_net_address(nwiftab.zone_nwif_address,
27147c478bd9Sstevel@tonic-gate 		    &lifr)) != Z_OK) {
27151390a385Sgjelinek 			print_net_err(nwiftab.zone_nwif_physical,
27161390a385Sgjelinek 			    nwiftab.zone_nwif_address, af,
27171390a385Sgjelinek 			    zonecfg_strerror(res));
27187c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
27197c478bd9Sstevel@tonic-gate 			continue;
27207c478bd9Sstevel@tonic-gate 		}
27217c478bd9Sstevel@tonic-gate 		af = lifr.lifr_addr.ss_family;
27227c478bd9Sstevel@tonic-gate 		(void) memset(&lifr, 0, sizeof (lifr));
27237c478bd9Sstevel@tonic-gate 		(void) strlcpy(lifr.lifr_name, nwiftab.zone_nwif_physical,
27247c478bd9Sstevel@tonic-gate 		    sizeof (lifr.lifr_name));
27257c478bd9Sstevel@tonic-gate 		lifr.lifr_addr.ss_family = af;
27267c478bd9Sstevel@tonic-gate 		if ((so = socket(af, SOCK_DGRAM, 0)) < 0) {
27277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("could not verify %s "
27287c478bd9Sstevel@tonic-gate 			    "%s=%s %s=%s: could not get socket: %s\n"), "net",
27297c478bd9Sstevel@tonic-gate 			    "address", nwiftab.zone_nwif_address, "physical",
27307c478bd9Sstevel@tonic-gate 			    nwiftab.zone_nwif_physical, strerror(errno));
27317c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
27327c478bd9Sstevel@tonic-gate 			continue;
27337c478bd9Sstevel@tonic-gate 		}
27341390a385Sgjelinek 		if (ioctl(so, SIOCGLIFFLAGS, &lifr) < 0) {
273522321485Svp157776 			/*
273622321485Svp157776 			 * The interface failed to come up.  We continue on
273722321485Svp157776 			 * anyway for the sake of consistency: a zone is not
273822321485Svp157776 			 * shut down if the interface fails any time after
273922321485Svp157776 			 * boot, nor does the global zone fail to boot if an
274022321485Svp157776 			 * interface fails.
274122321485Svp157776 			 */
274222321485Svp157776 			(void) fprintf(stderr,
274322321485Svp157776 			    gettext("WARNING: skipping interface '%s' which "
274422321485Svp157776 			    "may not be present/plumbed in the global zone.\n"),
274522321485Svp157776 			    nwiftab.zone_nwif_physical);
274622321485Svp157776 
27477c478bd9Sstevel@tonic-gate 		}
27487c478bd9Sstevel@tonic-gate 		(void) close(so);
27497c478bd9Sstevel@tonic-gate 	}
27507c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2751108322fbScarlsonj no_net:
27527c478bd9Sstevel@tonic-gate 
2753e7f3c547Sgjelinek 	/* verify that lofs has not been excluded from the kernel */
27548cd327d5Sgjelinek 	if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
27558cd327d5Sgjelinek 	    cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
27568cd327d5Sgjelinek 	    modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
2757e7f3c547Sgjelinek 		if (errno == ENXIO)
2758e7f3c547Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2759e7f3c547Sgjelinek 			    "lofs(7FS): possibly excluded in /etc/system\n"));
2760e7f3c547Sgjelinek 		else
2761e7f3c547Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2762e7f3c547Sgjelinek 			    "lofs(7FS): %s\n"), strerror(errno));
2763e7f3c547Sgjelinek 		return_code = Z_ERR;
2764e7f3c547Sgjelinek 	}
2765e7f3c547Sgjelinek 
27667c478bd9Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
27677c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2768b5abaf04Sgjelinek 	if (verify_ipd(handle) != Z_OK)
2769b5abaf04Sgjelinek 		return_code = Z_ERR;
2770108322fbScarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
27717c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2772108322fbScarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
27737c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
27749acbbeafSnn35248 	if (!in_alt_root && verify_brand(handle) != Z_OK)
27759acbbeafSnn35248 		return_code = Z_ERR;
2776fa9e4066Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2777fa9e4066Sahrens 		return_code = Z_ERR;
2778ffbafc53Scomay 
2779ffbafc53Scomay 	/*
2780ffbafc53Scomay 	 * As the "mount" command is used for patching/upgrading of zones
2781ffbafc53Scomay 	 * or other maintenance processes, the zone's privilege set is not
2782ffbafc53Scomay 	 * checked in this case.  Instead, the default, safe set of
2783ffbafc53Scomay 	 * privileges will be used when this zone is created in the
2784ffbafc53Scomay 	 * kernel.
2785ffbafc53Scomay 	 */
2786ffbafc53Scomay 	if (!in_alt_root && cmd_num != CMD_MOUNT &&
2787ffbafc53Scomay 	    verify_limitpriv(handle) != Z_OK)
2788ffbafc53Scomay 		return_code = Z_ERR;
27898cd327d5Sgjelinek 
27908cd327d5Sgjelinek 	return (return_code);
27918cd327d5Sgjelinek }
27928cd327d5Sgjelinek 
27938cd327d5Sgjelinek static int
27948cd327d5Sgjelinek verify_details(int cmd_num)
27958cd327d5Sgjelinek {
27968cd327d5Sgjelinek 	zone_dochandle_t handle;
27978cd327d5Sgjelinek 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
27988cd327d5Sgjelinek 	int return_code = Z_OK;
27998cd327d5Sgjelinek 	int err;
28008cd327d5Sgjelinek 
28018cd327d5Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
28028cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
28038cd327d5Sgjelinek 		return (Z_ERR);
28048cd327d5Sgjelinek 	}
28058cd327d5Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
28068cd327d5Sgjelinek 		errno = err;
28078cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
28088cd327d5Sgjelinek 		zonecfg_fini_handle(handle);
28098cd327d5Sgjelinek 		return (Z_ERR);
28108cd327d5Sgjelinek 	}
28118cd327d5Sgjelinek 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
28128cd327d5Sgjelinek 	    Z_OK) {
28138cd327d5Sgjelinek 		errno = err;
28148cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
28158cd327d5Sgjelinek 		zonecfg_fini_handle(handle);
28168cd327d5Sgjelinek 		return (Z_ERR);
28178cd327d5Sgjelinek 	}
28188cd327d5Sgjelinek 	/*
28198cd327d5Sgjelinek 	 * zonecfg_get_zonepath() gets its data from the XML repository.
28208cd327d5Sgjelinek 	 * Verify this against the index file, which is checked first by
28218cd327d5Sgjelinek 	 * zone_get_zonepath().  If they don't match, bail out.
28228cd327d5Sgjelinek 	 */
28238cd327d5Sgjelinek 	if ((err = zone_get_zonepath(target_zone, checkpath,
28248cd327d5Sgjelinek 	    sizeof (checkpath))) != Z_OK) {
28258cd327d5Sgjelinek 		errno = err;
28268cd327d5Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
28278cd327d5Sgjelinek 		return (Z_ERR);
28288cd327d5Sgjelinek 	}
28298cd327d5Sgjelinek 	if (strcmp(zonepath, checkpath) != 0) {
28308cd327d5Sgjelinek 		/*
28318cd327d5Sgjelinek 		 * TRANSLATION_NOTE
28328cd327d5Sgjelinek 		 * XML and zonepath are literals that should not be translated.
28338cd327d5Sgjelinek 		 */
28348cd327d5Sgjelinek 		(void) fprintf(stderr, gettext("The XML repository has "
28358cd327d5Sgjelinek 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
28368cd327d5Sgjelinek 		    "These must match, so fix the incorrect entry.\n"),
28378cd327d5Sgjelinek 		    zonepath, checkpath);
28388cd327d5Sgjelinek 		return (Z_ERR);
28398cd327d5Sgjelinek 	}
28408cd327d5Sgjelinek 	if (validate_zonepath(zonepath, cmd_num) != Z_OK) {
28418cd327d5Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
28428cd327d5Sgjelinek 		    "because of the above errors.\n"), zonepath);
28438cd327d5Sgjelinek 		return_code = Z_ERR;
28448cd327d5Sgjelinek 	}
28458cd327d5Sgjelinek 
28468cd327d5Sgjelinek 	if (verify_handle(cmd_num, handle) != Z_OK)
28478cd327d5Sgjelinek 		return_code = Z_ERR;
28488cd327d5Sgjelinek 
28497c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
28507c478bd9Sstevel@tonic-gate 	if (return_code == Z_ERR)
28517c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
28527c478bd9Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
28537c478bd9Sstevel@tonic-gate 		    execname, target_zone);
28547c478bd9Sstevel@tonic-gate 	return (return_code);
28557c478bd9Sstevel@tonic-gate }
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate static int
28587c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[])
28597c478bd9Sstevel@tonic-gate {
28607c478bd9Sstevel@tonic-gate 	int arg;
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate 	optind = 0;
28637c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
28647c478bd9Sstevel@tonic-gate 		switch (arg) {
28657c478bd9Sstevel@tonic-gate 		case '?':
28667c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28677c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
28687c478bd9Sstevel@tonic-gate 		default:
28697c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28707c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
28717c478bd9Sstevel@tonic-gate 		}
28727c478bd9Sstevel@tonic-gate 	}
28737c478bd9Sstevel@tonic-gate 	if (argc > optind) {
28747c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
28757c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
28767c478bd9Sstevel@tonic-gate 	}
28779acbbeafSnn35248 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
28789acbbeafSnn35248 	    != Z_OK)
28797c478bd9Sstevel@tonic-gate 		return (Z_ERR);
28807c478bd9Sstevel@tonic-gate 	return (verify_details(CMD_VERIFY));
28817c478bd9Sstevel@tonic-gate }
28827c478bd9Sstevel@tonic-gate 
28839acbbeafSnn35248 static int
28849acbbeafSnn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize)
28859acbbeafSnn35248 {
28869acbbeafSnn35248 	char optstring[4];
28879acbbeafSnn35248 
28889acbbeafSnn35248 	if (opt > 0)
28899acbbeafSnn35248 		(void) sprintf(optstring, " -%c", opt);
28909acbbeafSnn35248 	else
28919acbbeafSnn35248 		(void) strcpy(optstring, " ");
28929acbbeafSnn35248 
28939acbbeafSnn35248 	if ((strlcat(buf, optstring, bufsize) > bufsize))
28949acbbeafSnn35248 		return (Z_ERR);
28959acbbeafSnn35248 	if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
28969acbbeafSnn35248 		return (Z_ERR);
28979acbbeafSnn35248 	return (Z_OK);
28989acbbeafSnn35248 }
28997c478bd9Sstevel@tonic-gate 
29007c478bd9Sstevel@tonic-gate static int
29017c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[])
29027c478bd9Sstevel@tonic-gate {
29039acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
29047c478bd9Sstevel@tonic-gate 	int lockfd;
29059acbbeafSnn35248 	int arg, err, subproc_err;
29067c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
2907123807fbSedp 	brand_handle_t bh = NULL;
29087c478bd9Sstevel@tonic-gate 	int status;
29090b5de56dSgjelinek 	boolean_t nodataset = B_FALSE;
29109acbbeafSnn35248 	char opts[128];
29119acbbeafSnn35248 
29129acbbeafSnn35248 	if (target_zone == NULL) {
29139acbbeafSnn35248 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
29149acbbeafSnn35248 		return (Z_USAGE);
29159acbbeafSnn35248 	}
29167c478bd9Sstevel@tonic-gate 
2917108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2918108322fbScarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2919108322fbScarlsonj 		return (Z_ERR);
2920108322fbScarlsonj 	}
2921108322fbScarlsonj 
29229acbbeafSnn35248 	if ((err = zone_get_zonepath(target_zone, zonepath,
29239acbbeafSnn35248 	    sizeof (zonepath))) != Z_OK) {
29249acbbeafSnn35248 		errno = err;
29259acbbeafSnn35248 		zperror2(target_zone, gettext("could not get zone path"));
29269acbbeafSnn35248 		return (Z_ERR);
29279acbbeafSnn35248 	}
29289acbbeafSnn35248 
29299acbbeafSnn35248 	/* Fetch the install command from the brand configuration.  */
2930123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
29319acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
29329acbbeafSnn35248 		return (Z_ERR);
29339acbbeafSnn35248 	}
29349acbbeafSnn35248 
29359acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
2936123807fbSedp 	if (brand_get_install(bh, target_zone, zonepath, cmdbuf + EXEC_LEN,
29379acbbeafSnn35248 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) {
29389acbbeafSnn35248 		zerror("invalid brand configuration: missing install resource");
2939123807fbSedp 		brand_close(bh);
29409acbbeafSnn35248 		return (Z_ERR);
29419acbbeafSnn35248 	}
29429acbbeafSnn35248 
29439acbbeafSnn35248 	(void) strcpy(opts, "?x:");
29449acbbeafSnn35248 	if (!is_native_zone) {
29459acbbeafSnn35248 		/*
29469acbbeafSnn35248 		 * Fetch the list of recognized command-line options from
29479acbbeafSnn35248 		 * the brand configuration file.
29489acbbeafSnn35248 		 */
2949123807fbSedp 		if (brand_get_installopts(bh, opts + strlen(opts),
29509acbbeafSnn35248 		    sizeof (opts) - strlen(opts)) != 0) {
29519acbbeafSnn35248 			zerror("invalid brand configuration: missing "
29529acbbeafSnn35248 			    "install options resource");
2953123807fbSedp 			brand_close(bh);
29549acbbeafSnn35248 			return (Z_ERR);
29559acbbeafSnn35248 		}
29569acbbeafSnn35248 	}
2957123807fbSedp 	brand_close(bh);
29589acbbeafSnn35248 
29597c478bd9Sstevel@tonic-gate 	optind = 0;
29609acbbeafSnn35248 	while ((arg = getopt(argc, argv, opts)) != EOF) {
29617c478bd9Sstevel@tonic-gate 		switch (arg) {
29627c478bd9Sstevel@tonic-gate 		case '?':
29637c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
29647c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
29650b5de56dSgjelinek 		case 'x':
29660b5de56dSgjelinek 			if (strcmp(optarg, "nodataset") != 0) {
29670b5de56dSgjelinek 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
29680b5de56dSgjelinek 				return (Z_USAGE);
29690b5de56dSgjelinek 			}
29700b5de56dSgjelinek 			nodataset = B_TRUE;
29710b5de56dSgjelinek 			break;
29727c478bd9Sstevel@tonic-gate 		default:
29739acbbeafSnn35248 			if (is_native_zone) {
29747c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
29757c478bd9Sstevel@tonic-gate 				return (Z_USAGE);
29767c478bd9Sstevel@tonic-gate 			}
29779acbbeafSnn35248 
29789acbbeafSnn35248 			/*
29799acbbeafSnn35248 			 * This option isn't for zoneadm, so append it to
29809acbbeafSnn35248 			 * the command line passed to the brand-specific
29819acbbeafSnn35248 			 * install routine.
29829acbbeafSnn35248 			 */
29839acbbeafSnn35248 			if (addopt(cmdbuf, optopt, optarg,
29849acbbeafSnn35248 			    sizeof (cmdbuf)) != Z_OK) {
29859acbbeafSnn35248 				zerror("Install command line too long");
29869acbbeafSnn35248 				return (Z_ERR);
29877c478bd9Sstevel@tonic-gate 			}
29889acbbeafSnn35248 			break;
29897c478bd9Sstevel@tonic-gate 		}
29909acbbeafSnn35248 	}
29919acbbeafSnn35248 
29929acbbeafSnn35248 	if (!is_native_zone) {
29939acbbeafSnn35248 		for (; optind < argc; optind++) {
29949acbbeafSnn35248 			if (addopt(cmdbuf, 0, argv[optind],
29959acbbeafSnn35248 			    sizeof (cmdbuf)) != Z_OK) {
29969acbbeafSnn35248 				zerror("Install command line too long");
29979acbbeafSnn35248 				return (Z_ERR);
29989acbbeafSnn35248 			}
29999acbbeafSnn35248 		}
30009acbbeafSnn35248 	}
30019acbbeafSnn35248 
30029acbbeafSnn35248 	if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE, B_FALSE)
30039acbbeafSnn35248 	    != Z_OK)
30047c478bd9Sstevel@tonic-gate 		return (Z_ERR);
30057c478bd9Sstevel@tonic-gate 	if (verify_details(CMD_INSTALL) != Z_OK)
30067c478bd9Sstevel@tonic-gate 		return (Z_ERR);
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
30097c478bd9Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
3010865e09a4Sgjelinek 		    "zoneadm");
30117c478bd9Sstevel@tonic-gate 		return (Z_ERR);
30127c478bd9Sstevel@tonic-gate 	}
30137c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
30147c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
30157c478bd9Sstevel@tonic-gate 		errno = err;
30167c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
30177c478bd9Sstevel@tonic-gate 		goto done;
30187c478bd9Sstevel@tonic-gate 	}
30197c478bd9Sstevel@tonic-gate 
30209acbbeafSnn35248 	if (!nodataset)
30219acbbeafSnn35248 		create_zfs_zonepath(zonepath);
30229acbbeafSnn35248 
30237c478bd9Sstevel@tonic-gate 	/*
30247c478bd9Sstevel@tonic-gate 	 * According to the Application Packaging Developer's Guide, a
30257c478bd9Sstevel@tonic-gate 	 * "checkinstall" script when included in a package is executed as
30267c478bd9Sstevel@tonic-gate 	 * the user "install", if such a user exists, or by the user
30277c478bd9Sstevel@tonic-gate 	 * "nobody".  In order to support this dubious behavior, the path
30287c478bd9Sstevel@tonic-gate 	 * to the zone being constructed is opened up during the life of
30297c478bd9Sstevel@tonic-gate 	 * the command laying down the zone's root file system.  Once this
30307c478bd9Sstevel@tonic-gate 	 * has completed, regardless of whether it was successful, the
30317c478bd9Sstevel@tonic-gate 	 * path to the zone is again restricted.
30327c478bd9Sstevel@tonic-gate 	 */
30337c478bd9Sstevel@tonic-gate 	if (chmod(zonepath, DEFAULT_DIR_MODE) != 0) {
30347c478bd9Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
30357c478bd9Sstevel@tonic-gate 		err = Z_ERR;
30367c478bd9Sstevel@tonic-gate 		goto done;
30377c478bd9Sstevel@tonic-gate 	}
30387c478bd9Sstevel@tonic-gate 
30399acbbeafSnn35248 	if (is_native_zone)
30407c478bd9Sstevel@tonic-gate 		status = do_subproc(cmdbuf);
30419acbbeafSnn35248 	else
30429acbbeafSnn35248 		status = do_subproc_interactive(cmdbuf);
30439acbbeafSnn35248 
30447c478bd9Sstevel@tonic-gate 	if (chmod(zonepath, S_IRWXU) != 0) {
30457c478bd9Sstevel@tonic-gate 		zperror(zonepath, B_FALSE);
30467c478bd9Sstevel@tonic-gate 		err = Z_ERR;
30477c478bd9Sstevel@tonic-gate 		goto done;
30487c478bd9Sstevel@tonic-gate 	}
30499acbbeafSnn35248 	if ((subproc_err =
30509acbbeafSnn35248 	    subproc_status(gettext("brand-specific installation"), status,
30519acbbeafSnn35248 	    B_FALSE)) != ZONE_SUBPROC_OK) {
30529acbbeafSnn35248 		err = Z_ERR;
30537c478bd9Sstevel@tonic-gate 		goto done;
30549acbbeafSnn35248 	}
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
30577c478bd9Sstevel@tonic-gate 		errno = err;
30587c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
30597c478bd9Sstevel@tonic-gate 		goto done;
30607c478bd9Sstevel@tonic-gate 	}
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate done:
30639acbbeafSnn35248 	/*
30649acbbeafSnn35248 	 * If the install script exited with ZONE_SUBPROC_USAGE or
30659acbbeafSnn35248 	 * ZONE_SUBPROC_NOTCOMPLETE, try to clean up the zone and leave the
30669acbbeafSnn35248 	 * zone in the CONFIGURED state so that another install can be
30679acbbeafSnn35248 	 * attempted without requiring an uninstall first.
30689acbbeafSnn35248 	 */
30699acbbeafSnn35248 	if ((subproc_err == ZONE_SUBPROC_USAGE) ||
30709acbbeafSnn35248 	    (subproc_err == ZONE_SUBPROC_NOTCOMPLETE)) {
30719acbbeafSnn35248 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
30729acbbeafSnn35248 			errno = err;
30739acbbeafSnn35248 			zperror2(target_zone,
30749acbbeafSnn35248 			    gettext("cleaning up zonepath failed"));
30759acbbeafSnn35248 		} else if ((err = zone_set_state(target_zone,
30769acbbeafSnn35248 		    ZONE_STATE_CONFIGURED)) != Z_OK) {
30779acbbeafSnn35248 			errno = err;
30789acbbeafSnn35248 			zperror2(target_zone, gettext("could not set state"));
30799acbbeafSnn35248 		}
30809acbbeafSnn35248 	}
30819acbbeafSnn35248 
30827c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
30837c478bd9Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
30847c478bd9Sstevel@tonic-gate }
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate /*
3087865e09a4Sgjelinek  * Check that the inherited pkg dirs are the same for the clone and its source.
3088865e09a4Sgjelinek  * The easiest way to do that is check that the list of ipds is the same
3089865e09a4Sgjelinek  * by matching each one against the other.  This algorithm should be fine since
3090865e09a4Sgjelinek  * the list of ipds should not be that long.
3091865e09a4Sgjelinek  */
3092865e09a4Sgjelinek static int
3093865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone,
3094865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3095865e09a4Sgjelinek {
3096865e09a4Sgjelinek 	int err;
3097865e09a4Sgjelinek 	int res = Z_OK;
3098865e09a4Sgjelinek 	int s_cnt = 0;
3099865e09a4Sgjelinek 	int t_cnt = 0;
3100865e09a4Sgjelinek 	struct zone_fstab s_fstab;
3101865e09a4Sgjelinek 	struct zone_fstab t_fstab;
3102865e09a4Sgjelinek 
3103865e09a4Sgjelinek 	/*
3104865e09a4Sgjelinek 	 * First check the source of the clone against the target.
3105865e09a4Sgjelinek 	 */
3106865e09a4Sgjelinek 	if ((err = zonecfg_setipdent(s_handle)) != Z_OK) {
3107865e09a4Sgjelinek 		errno = err;
3108865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not enumerate "
3109865e09a4Sgjelinek 		    "inherit-pkg-dirs"));
3110865e09a4Sgjelinek 		return (Z_ERR);
3111865e09a4Sgjelinek 	}
3112865e09a4Sgjelinek 
3113865e09a4Sgjelinek 	while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) {
3114865e09a4Sgjelinek 		boolean_t match = B_FALSE;
3115865e09a4Sgjelinek 
3116865e09a4Sgjelinek 		s_cnt++;
3117865e09a4Sgjelinek 
3118865e09a4Sgjelinek 		if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
3119865e09a4Sgjelinek 			errno = err;
3120865e09a4Sgjelinek 			zperror2(target_zone, gettext("could not enumerate "
3121865e09a4Sgjelinek 			    "inherit-pkg-dirs"));
3122865e09a4Sgjelinek 			(void) zonecfg_endipdent(s_handle);
3123865e09a4Sgjelinek 			return (Z_ERR);
3124865e09a4Sgjelinek 		}
3125865e09a4Sgjelinek 
3126865e09a4Sgjelinek 		while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) {
3127865e09a4Sgjelinek 			if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir)
3128865e09a4Sgjelinek 			    == 0) {
3129865e09a4Sgjelinek 				match = B_TRUE;
3130865e09a4Sgjelinek 				break;
3131865e09a4Sgjelinek 			}
3132865e09a4Sgjelinek 		}
3133865e09a4Sgjelinek 		(void) zonecfg_endipdent(t_handle);
3134865e09a4Sgjelinek 
3135865e09a4Sgjelinek 		if (!match) {
3136865e09a4Sgjelinek 			(void) fprintf(stderr, gettext("inherit-pkg-dir "
3137865e09a4Sgjelinek 			    "'%s' is not configured in zone %s.\n"),
3138865e09a4Sgjelinek 			    s_fstab.zone_fs_dir, target_zone);
3139865e09a4Sgjelinek 			res = Z_ERR;
3140865e09a4Sgjelinek 		}
3141865e09a4Sgjelinek 	}
3142865e09a4Sgjelinek 
3143865e09a4Sgjelinek 	(void) zonecfg_endipdent(s_handle);
3144865e09a4Sgjelinek 
3145865e09a4Sgjelinek 	/* skip the next check if we already have errors */
3146865e09a4Sgjelinek 	if (res == Z_ERR)
3147865e09a4Sgjelinek 		return (res);
3148865e09a4Sgjelinek 
3149865e09a4Sgjelinek 	/*
3150865e09a4Sgjelinek 	 * Now check the number of ipds in the target so we can verify
3151865e09a4Sgjelinek 	 * that the source is not a subset of the target.
3152865e09a4Sgjelinek 	 */
3153865e09a4Sgjelinek 	if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
3154865e09a4Sgjelinek 		errno = err;
3155865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not enumerate "
3156865e09a4Sgjelinek 		    "inherit-pkg-dirs"));
3157865e09a4Sgjelinek 		return (Z_ERR);
3158865e09a4Sgjelinek 	}
3159865e09a4Sgjelinek 
3160865e09a4Sgjelinek 	while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK)
3161865e09a4Sgjelinek 		t_cnt++;
3162865e09a4Sgjelinek 
3163865e09a4Sgjelinek 	(void) zonecfg_endipdent(t_handle);
3164865e09a4Sgjelinek 
3165865e09a4Sgjelinek 	if (t_cnt != s_cnt) {
3166865e09a4Sgjelinek 		(void) fprintf(stderr, gettext("Zone %s is configured "
3167865e09a4Sgjelinek 		    "with inherit-pkg-dirs that are not configured in zone "
3168865e09a4Sgjelinek 		    "%s.\n"), target_zone, source_zone);
3169865e09a4Sgjelinek 		res = Z_ERR;
3170865e09a4Sgjelinek 	}
3171865e09a4Sgjelinek 
3172865e09a4Sgjelinek 	return (res);
3173865e09a4Sgjelinek }
3174865e09a4Sgjelinek 
3175865e09a4Sgjelinek static void
3176865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
3177865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3178865e09a4Sgjelinek {
3179865e09a4Sgjelinek 	int err;
3180865e09a4Sgjelinek 	struct zone_devtab s_devtab;
3181865e09a4Sgjelinek 	struct zone_devtab t_devtab;
3182865e09a4Sgjelinek 
3183865e09a4Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
3184865e09a4Sgjelinek 		errno = err;
3185865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
3186865e09a4Sgjelinek 		return;
3187865e09a4Sgjelinek 	}
3188865e09a4Sgjelinek 
3189865e09a4Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
3190865e09a4Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
3191865e09a4Sgjelinek 			errno = err;
3192865e09a4Sgjelinek 			zperror2(source_zone,
3193865e09a4Sgjelinek 			    gettext("could not enumerate devices"));
3194865e09a4Sgjelinek 			(void) zonecfg_enddevent(t_handle);
3195865e09a4Sgjelinek 			return;
3196865e09a4Sgjelinek 		}
3197865e09a4Sgjelinek 
3198865e09a4Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
3199865e09a4Sgjelinek 			/*
3200865e09a4Sgjelinek 			 * Use fnmatch to catch the case where wildcards
3201865e09a4Sgjelinek 			 * were used in one zone and the other has an
3202865e09a4Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
3203865e09a4Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
3204865e09a4Sgjelinek 			 */
3205865e09a4Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
3206865e09a4Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
3207865e09a4Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
3208865e09a4Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
3209865e09a4Sgjelinek 				(void) fprintf(stderr,
3210865e09a4Sgjelinek 				    gettext("WARNING: device '%s' "
3211865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3212865e09a4Sgjelinek 				    t_devtab.zone_dev_match);
3213865e09a4Sgjelinek 				break;
3214865e09a4Sgjelinek 			}
3215865e09a4Sgjelinek 		}
3216865e09a4Sgjelinek 		(void) zonecfg_enddevent(s_handle);
3217865e09a4Sgjelinek 	}
3218865e09a4Sgjelinek 
3219865e09a4Sgjelinek 	(void) zonecfg_enddevent(t_handle);
3220865e09a4Sgjelinek }
3221865e09a4Sgjelinek 
3222865e09a4Sgjelinek /*
3223865e09a4Sgjelinek  * Check if the specified mount option (opt) is contained within the
3224865e09a4Sgjelinek  * options string.
3225865e09a4Sgjelinek  */
3226865e09a4Sgjelinek static boolean_t
3227865e09a4Sgjelinek opt_match(char *opt, char *options)
3228865e09a4Sgjelinek {
3229865e09a4Sgjelinek 	char *p;
3230865e09a4Sgjelinek 	char *lastp;
3231865e09a4Sgjelinek 
3232865e09a4Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
3233865e09a4Sgjelinek 		if (strcmp(p, opt) == 0)
3234865e09a4Sgjelinek 			return (B_TRUE);
3235865e09a4Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
3236865e09a4Sgjelinek 			if (strcmp(p, opt) == 0)
3237865e09a4Sgjelinek 				return (B_TRUE);
3238865e09a4Sgjelinek 		}
3239865e09a4Sgjelinek 	}
3240865e09a4Sgjelinek 
3241865e09a4Sgjelinek 	return (B_FALSE);
3242865e09a4Sgjelinek }
3243865e09a4Sgjelinek 
32440b5de56dSgjelinek #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
3245865e09a4Sgjelinek 	"in both zones.\n"
3246865e09a4Sgjelinek 
3247865e09a4Sgjelinek static void
3248865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
3249865e09a4Sgjelinek {
3250865e09a4Sgjelinek 	/*
3251865e09a4Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
3252865e09a4Sgjelinek 	 * either is rw since this will effect the other zone.
3253865e09a4Sgjelinek 	 */
3254865e09a4Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
3255865e09a4Sgjelinek 		zone_fsopt_t *optp;
3256865e09a4Sgjelinek 
3257865e09a4Sgjelinek 		/* The default is rw so no options means rw */
3258865e09a4Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
3259865e09a4Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
3260865e09a4Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
3261865e09a4Sgjelinek 			    t_fstab->zone_fs_special);
3262865e09a4Sgjelinek 			return;
3263865e09a4Sgjelinek 		}
3264865e09a4Sgjelinek 
3265865e09a4Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
3266865e09a4Sgjelinek 		    optp = optp->zone_fsopt_next) {
3267865e09a4Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
3268865e09a4Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
3269865e09a4Sgjelinek 				    s_fstab->zone_fs_special);
3270865e09a4Sgjelinek 				return;
3271865e09a4Sgjelinek 			}
3272865e09a4Sgjelinek 		}
3273865e09a4Sgjelinek 
3274865e09a4Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
3275865e09a4Sgjelinek 		    optp = optp->zone_fsopt_next) {
3276865e09a4Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
3277865e09a4Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
3278865e09a4Sgjelinek 				    t_fstab->zone_fs_special);
3279865e09a4Sgjelinek 				return;
3280865e09a4Sgjelinek 			}
3281865e09a4Sgjelinek 		}
3282865e09a4Sgjelinek 
3283865e09a4Sgjelinek 		return;
3284865e09a4Sgjelinek 	}
3285865e09a4Sgjelinek 
3286865e09a4Sgjelinek 	/*
3287865e09a4Sgjelinek 	 * TRANSLATION_NOTE
32880b5de56dSgjelinek 	 * The first variable is the file system type and the second is
32890b5de56dSgjelinek 	 * the file system special device.  For example,
32900b5de56dSgjelinek 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
3291865e09a4Sgjelinek 	 */
32920b5de56dSgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
3293865e09a4Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
3294865e09a4Sgjelinek 	    t_fstab->zone_fs_special);
3295865e09a4Sgjelinek }
3296865e09a4Sgjelinek 
3297865e09a4Sgjelinek static void
3298865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
3299865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3300865e09a4Sgjelinek {
3301865e09a4Sgjelinek 	int err;
3302865e09a4Sgjelinek 	struct zone_fstab s_fstab;
3303865e09a4Sgjelinek 	struct zone_fstab t_fstab;
3304865e09a4Sgjelinek 
3305865e09a4Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
3306865e09a4Sgjelinek 		errno = err;
3307865e09a4Sgjelinek 		zperror2(target_zone,
33080b5de56dSgjelinek 		    gettext("could not enumerate file systems"));
3309865e09a4Sgjelinek 		return;
3310865e09a4Sgjelinek 	}
3311865e09a4Sgjelinek 
3312865e09a4Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
3313865e09a4Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
3314865e09a4Sgjelinek 			errno = err;
3315865e09a4Sgjelinek 			zperror2(source_zone,
33160b5de56dSgjelinek 			    gettext("could not enumerate file systems"));
3317865e09a4Sgjelinek 			(void) zonecfg_endfsent(t_handle);
3318865e09a4Sgjelinek 			return;
3319865e09a4Sgjelinek 		}
3320865e09a4Sgjelinek 
3321865e09a4Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
3322865e09a4Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
3323865e09a4Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
3324865e09a4Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
3325865e09a4Sgjelinek 				break;
3326865e09a4Sgjelinek 			}
3327865e09a4Sgjelinek 		}
3328865e09a4Sgjelinek 		(void) zonecfg_endfsent(s_handle);
3329865e09a4Sgjelinek 	}
3330865e09a4Sgjelinek 
3331865e09a4Sgjelinek 	(void) zonecfg_endfsent(t_handle);
3332865e09a4Sgjelinek }
3333865e09a4Sgjelinek 
3334865e09a4Sgjelinek /*
3335865e09a4Sgjelinek  * We don't catch the case where you used the same IP address but
3336865e09a4Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
3337865e09a4Sgjelinek  * However, we're not going to worry about that but we will check for
3338865e09a4Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
3339865e09a4Sgjelinek  * and handle that case as a match.
3340865e09a4Sgjelinek  */
3341865e09a4Sgjelinek static void
3342865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
3343865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3344865e09a4Sgjelinek {
3345865e09a4Sgjelinek 	int err;
3346865e09a4Sgjelinek 	struct zone_nwiftab s_nwiftab;
3347865e09a4Sgjelinek 	struct zone_nwiftab t_nwiftab;
3348865e09a4Sgjelinek 
3349865e09a4Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
3350865e09a4Sgjelinek 		errno = err;
3351865e09a4Sgjelinek 		zperror2(target_zone,
3352865e09a4Sgjelinek 		    gettext("could not enumerate network interfaces"));
3353865e09a4Sgjelinek 		return;
3354865e09a4Sgjelinek 	}
3355865e09a4Sgjelinek 
3356865e09a4Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
3357865e09a4Sgjelinek 		char *p;
3358865e09a4Sgjelinek 
3359865e09a4Sgjelinek 		/* remove an (optional) netmask from the address */
3360865e09a4Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
3361865e09a4Sgjelinek 			*p = '\0';
3362865e09a4Sgjelinek 
3363865e09a4Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
3364865e09a4Sgjelinek 			errno = err;
3365865e09a4Sgjelinek 			zperror2(source_zone,
3366865e09a4Sgjelinek 			    gettext("could not enumerate network interfaces"));
3367865e09a4Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
3368865e09a4Sgjelinek 			return;
3369865e09a4Sgjelinek 		}
3370865e09a4Sgjelinek 
3371865e09a4Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
3372865e09a4Sgjelinek 			/* remove an (optional) netmask from the address */
3373865e09a4Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
3374865e09a4Sgjelinek 			    != NULL)
3375865e09a4Sgjelinek 				*p = '\0';
3376865e09a4Sgjelinek 
3377865e09a4Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
3378865e09a4Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
3379865e09a4Sgjelinek 				(void) fprintf(stderr,
3380865e09a4Sgjelinek 				    gettext("WARNING: network address '%s' "
3381865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3382865e09a4Sgjelinek 				    t_nwiftab.zone_nwif_address);
3383865e09a4Sgjelinek 				break;
3384865e09a4Sgjelinek 			}
3385865e09a4Sgjelinek 		}
3386865e09a4Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
3387865e09a4Sgjelinek 	}
3388865e09a4Sgjelinek 
3389865e09a4Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
3390865e09a4Sgjelinek }
3391865e09a4Sgjelinek 
3392865e09a4Sgjelinek static void
33939acbbeafSnn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source,
33949acbbeafSnn35248 	zone_dochandle_t t_handle, char *target)
3395865e09a4Sgjelinek {
3396865e09a4Sgjelinek 	int err;
3397865e09a4Sgjelinek 	struct zone_dstab s_dstab;
3398865e09a4Sgjelinek 	struct zone_dstab t_dstab;
3399865e09a4Sgjelinek 
3400865e09a4Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
3401865e09a4Sgjelinek 		errno = err;
34029acbbeafSnn35248 		zperror2(target, gettext("could not enumerate datasets"));
3403865e09a4Sgjelinek 		return;
3404865e09a4Sgjelinek 	}
3405865e09a4Sgjelinek 
3406865e09a4Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
3407865e09a4Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
3408865e09a4Sgjelinek 			errno = err;
34099acbbeafSnn35248 			zperror2(source,
3410865e09a4Sgjelinek 			    gettext("could not enumerate datasets"));
3411865e09a4Sgjelinek 			(void) zonecfg_enddsent(t_handle);
3412865e09a4Sgjelinek 			return;
3413865e09a4Sgjelinek 		}
3414865e09a4Sgjelinek 
3415865e09a4Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
3416865e09a4Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
3417865e09a4Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
34189acbbeafSnn35248 				target_zone = source;
34199acbbeafSnn35248 				zerror(gettext("WARNING: dataset '%s' "
3420865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3421865e09a4Sgjelinek 				    t_dstab.zone_dataset_name);
3422865e09a4Sgjelinek 				break;
3423865e09a4Sgjelinek 			}
3424865e09a4Sgjelinek 		}
3425865e09a4Sgjelinek 		(void) zonecfg_enddsent(s_handle);
3426865e09a4Sgjelinek 	}
3427865e09a4Sgjelinek 
3428865e09a4Sgjelinek 	(void) zonecfg_enddsent(t_handle);
3429865e09a4Sgjelinek }
3430865e09a4Sgjelinek 
34319acbbeafSnn35248 /*
34329acbbeafSnn35248  * Check that the clone and its source have the same brand type.
34339acbbeafSnn35248  */
34349acbbeafSnn35248 static int
34359acbbeafSnn35248 valid_brand_clone(char *source_zone, char *target_zone)
34369acbbeafSnn35248 {
3437123807fbSedp 	brand_handle_t bh;
34389acbbeafSnn35248 	char source_brand[MAXNAMELEN];
34399acbbeafSnn35248 
34409acbbeafSnn35248 	if ((zone_get_brand(source_zone, source_brand,
34419acbbeafSnn35248 	    sizeof (source_brand))) != Z_OK) {
34429acbbeafSnn35248 		(void) fprintf(stderr, "%s: zone '%s': %s\n",
34439acbbeafSnn35248 		    execname, source_zone, gettext("missing or invalid brand"));
34449acbbeafSnn35248 		return (Z_ERR);
34459acbbeafSnn35248 	}
34469acbbeafSnn35248 
34479acbbeafSnn35248 	if (strcmp(source_brand, target_brand) != NULL) {
34489acbbeafSnn35248 		(void) fprintf(stderr,
34499acbbeafSnn35248 		    gettext("%s: Zones '%s' and '%s' have different brand "
34509acbbeafSnn35248 		    "types.\n"), execname, source_zone, target_zone);
34519acbbeafSnn35248 		return (Z_ERR);
34529acbbeafSnn35248 	}
34539acbbeafSnn35248 
3454123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
34559acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
34569acbbeafSnn35248 		return (Z_ERR);
34579acbbeafSnn35248 	}
3458123807fbSedp 	brand_close(bh);
34599acbbeafSnn35248 	return (Z_OK);
34609acbbeafSnn35248 }
34619acbbeafSnn35248 
3462865e09a4Sgjelinek static int
3463865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone)
3464865e09a4Sgjelinek {
3465865e09a4Sgjelinek 	int err = Z_OK;
3466865e09a4Sgjelinek 	zone_dochandle_t s_handle;
3467865e09a4Sgjelinek 	zone_dochandle_t t_handle;
3468865e09a4Sgjelinek 
3469865e09a4Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
3470865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3471865e09a4Sgjelinek 		return (Z_ERR);
3472865e09a4Sgjelinek 	}
3473865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
3474865e09a4Sgjelinek 		errno = err;
3475865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3476865e09a4Sgjelinek 		zonecfg_fini_handle(t_handle);
3477865e09a4Sgjelinek 		return (Z_ERR);
3478865e09a4Sgjelinek 	}
3479865e09a4Sgjelinek 
3480865e09a4Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
3481865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3482865e09a4Sgjelinek 		zonecfg_fini_handle(t_handle);
3483865e09a4Sgjelinek 		return (Z_ERR);
3484865e09a4Sgjelinek 	}
3485865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
3486865e09a4Sgjelinek 		errno = err;
3487865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3488865e09a4Sgjelinek 		goto done;
3489865e09a4Sgjelinek 	}
3490865e09a4Sgjelinek 
34919acbbeafSnn35248 	/* verify new zone has same brand type */
34929acbbeafSnn35248 	err = valid_brand_clone(source_zone, target_zone);
34939acbbeafSnn35248 	if (err != Z_OK)
34949acbbeafSnn35248 		goto done;
34959acbbeafSnn35248 
3496865e09a4Sgjelinek 	/* verify new zone has same inherit-pkg-dirs */
3497865e09a4Sgjelinek 	err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone);
3498865e09a4Sgjelinek 
3499865e09a4Sgjelinek 	/* warn about imported fs's which are the same */
3500865e09a4Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
3501865e09a4Sgjelinek 
3502865e09a4Sgjelinek 	/* warn about imported IP addresses which are the same */
3503865e09a4Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
3504865e09a4Sgjelinek 
3505865e09a4Sgjelinek 	/* warn about imported devices which are the same */
3506865e09a4Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
3507865e09a4Sgjelinek 
3508865e09a4Sgjelinek 	/* warn about imported datasets which are the same */
3509865e09a4Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
3510865e09a4Sgjelinek 
3511865e09a4Sgjelinek done:
3512865e09a4Sgjelinek 	zonecfg_fini_handle(t_handle);
3513865e09a4Sgjelinek 	zonecfg_fini_handle(s_handle);
3514865e09a4Sgjelinek 
3515865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3516865e09a4Sgjelinek }
3517865e09a4Sgjelinek 
3518865e09a4Sgjelinek static int
3519865e09a4Sgjelinek copy_zone(char *src, char *dst)
3520865e09a4Sgjelinek {
3521865e09a4Sgjelinek 	boolean_t out_null = B_FALSE;
3522865e09a4Sgjelinek 	int status;
3523865e09a4Sgjelinek 	char *outfile;
3524865e09a4Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
3525865e09a4Sgjelinek 
3526865e09a4Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
3527865e09a4Sgjelinek 		outfile = "/dev/null";
3528865e09a4Sgjelinek 		out_null = B_TRUE;
3529865e09a4Sgjelinek 	}
3530865e09a4Sgjelinek 
35310b5de56dSgjelinek 	/*
35320b5de56dSgjelinek 	 * Use find to get the list of files to copy.  We need to skip
35330b5de56dSgjelinek 	 * files of type "socket" since cpio can't handle those but that
35340b5de56dSgjelinek 	 * should be ok since the app will recreate the socket when it runs.
35350b5de56dSgjelinek 	 * We also need to filter out anything under the .zfs subdir.  Since
35360b5de56dSgjelinek 	 * find is running depth-first, we need the extra egrep to filter .zfs.
35370b5de56dSgjelinek 	 */
3538865e09a4Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
35390b5de56dSgjelinek 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
354007b574eeSgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3541865e09a4Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3542865e09a4Sgjelinek 	    src, dst, outfile);
3543865e09a4Sgjelinek 
3544865e09a4Sgjelinek 	status = do_subproc(cmdbuf);
3545865e09a4Sgjelinek 
35469acbbeafSnn35248 	if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
3547865e09a4Sgjelinek 		if (!out_null)
3548865e09a4Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
3549865e09a4Sgjelinek 			    "More information can be found in %s\n"), outfile);
35509acbbeafSnn35248 		return (Z_ERR);
3551865e09a4Sgjelinek 	}
3552865e09a4Sgjelinek 
3553865e09a4Sgjelinek 	if (!out_null)
3554865e09a4Sgjelinek 		(void) unlink(outfile);
3555865e09a4Sgjelinek 
3556865e09a4Sgjelinek 	return (Z_OK);
3557865e09a4Sgjelinek }
3558865e09a4Sgjelinek 
3559865e09a4Sgjelinek static int
35609acbbeafSnn35248 zone_postclone(char *zonepath)
3561865e09a4Sgjelinek {
35629acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
3563865e09a4Sgjelinek 	int status;
3564123807fbSedp 	brand_handle_t bh;
35659acbbeafSnn35248 	int err = Z_OK;
35665cd08338Sgjelinek 
35675cd08338Sgjelinek 	/*
35689acbbeafSnn35248 	 * Fetch the post-clone command, if any, from the brand
35699acbbeafSnn35248 	 * configuration.
357045916cd2Sjpk 	 */
3571123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
35729acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
35735cd08338Sgjelinek 		return (Z_ERR);
3574865e09a4Sgjelinek 	}
35759acbbeafSnn35248 	(void) strcpy(cmdbuf, EXEC_PREFIX);
3576123807fbSedp 	err = brand_get_postclone(bh, target_zone, zonepath, cmdbuf + EXEC_LEN,
35779acbbeafSnn35248 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL);
3578123807fbSedp 	brand_close(bh);
3579865e09a4Sgjelinek 
35809acbbeafSnn35248 	if (err == 0 && strlen(cmdbuf) > EXEC_LEN) {
35815cd08338Sgjelinek 		status = do_subproc(cmdbuf);
35829acbbeafSnn35248 		if ((err = subproc_status("postclone", status, B_FALSE))
35839acbbeafSnn35248 		    != ZONE_SUBPROC_OK) {
35849acbbeafSnn35248 			zerror(gettext("post-clone configuration failed."));
35859acbbeafSnn35248 			err = Z_ERR;
35869acbbeafSnn35248 		}
3587865e09a4Sgjelinek 	}
3588865e09a4Sgjelinek 
35899acbbeafSnn35248 	return (err);
35905cd08338Sgjelinek }
3591865e09a4Sgjelinek 
3592865e09a4Sgjelinek /* ARGSUSED */
35930b5de56dSgjelinek static int
3594865e09a4Sgjelinek zfm_print(const char *p, void *r) {
3595865e09a4Sgjelinek 	zerror("  %s\n", p);
3596865e09a4Sgjelinek 	return (0);
3597865e09a4Sgjelinek }
3598865e09a4Sgjelinek 
35990b5de56dSgjelinek int
36000b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath)
36010b5de56dSgjelinek {
36020b5de56dSgjelinek 	int err;
36030b5de56dSgjelinek 
36040b5de56dSgjelinek 	/* Don't clone the zone if anything is still mounted there */
36050b5de56dSgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
36060b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
36070b5de56dSgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
36080b5de56dSgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
36090b5de56dSgjelinek 		return (Z_ERR);
36100b5de56dSgjelinek 	}
36110b5de56dSgjelinek 
36120b5de56dSgjelinek 	/*
36130b5de56dSgjelinek 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
36140b5de56dSgjelinek 	 * care if this works or not since we always have the default behavior
36150b5de56dSgjelinek 	 * of a simple directory for the zonepath.
36160b5de56dSgjelinek 	 */
36170b5de56dSgjelinek 	create_zfs_zonepath(zonepath);
36180b5de56dSgjelinek 
36190b5de56dSgjelinek 	(void) printf(gettext("Copying %s..."), source_zonepath);
36200b5de56dSgjelinek 	(void) fflush(stdout);
36210b5de56dSgjelinek 
36220b5de56dSgjelinek 	err = copy_zone(source_zonepath, zonepath);
36230b5de56dSgjelinek 
36240b5de56dSgjelinek 	(void) printf("\n");
36250b5de56dSgjelinek 
36260b5de56dSgjelinek 	return (err);
36270b5de56dSgjelinek }
36280b5de56dSgjelinek 
3629865e09a4Sgjelinek static int
3630865e09a4Sgjelinek clone_func(int argc, char *argv[])
3631865e09a4Sgjelinek {
3632865e09a4Sgjelinek 	char *source_zone = NULL;
3633865e09a4Sgjelinek 	int lockfd;
3634865e09a4Sgjelinek 	int err, arg;
3635865e09a4Sgjelinek 	char zonepath[MAXPATHLEN];
3636865e09a4Sgjelinek 	char source_zonepath[MAXPATHLEN];
3637865e09a4Sgjelinek 	zone_state_t state;
3638865e09a4Sgjelinek 	zone_entry_t *zent;
36390b5de56dSgjelinek 	char *method = NULL;
36400b5de56dSgjelinek 	char *snapshot = NULL;
3641865e09a4Sgjelinek 
3642865e09a4Sgjelinek 	if (zonecfg_in_alt_root()) {
3643865e09a4Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
3644865e09a4Sgjelinek 		return (Z_ERR);
3645865e09a4Sgjelinek 	}
3646865e09a4Sgjelinek 
3647865e09a4Sgjelinek 	optind = 0;
36480b5de56dSgjelinek 	if ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
3649865e09a4Sgjelinek 		switch (arg) {
3650865e09a4Sgjelinek 		case '?':
3651865e09a4Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
3652865e09a4Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
3653865e09a4Sgjelinek 		case 'm':
3654865e09a4Sgjelinek 			method = optarg;
3655865e09a4Sgjelinek 			break;
36560b5de56dSgjelinek 		case 's':
36570b5de56dSgjelinek 			snapshot = optarg;
36580b5de56dSgjelinek 			break;
3659865e09a4Sgjelinek 		default:
3660865e09a4Sgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
3661865e09a4Sgjelinek 			return (Z_USAGE);
3662865e09a4Sgjelinek 		}
3663865e09a4Sgjelinek 	}
36640b5de56dSgjelinek 	if (argc != (optind + 1) ||
36650b5de56dSgjelinek 	    (method != NULL && strcmp(method, "copy") != 0)) {
3666865e09a4Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
3667865e09a4Sgjelinek 		return (Z_USAGE);
3668865e09a4Sgjelinek 	}
3669865e09a4Sgjelinek 	source_zone = argv[optind];
36709acbbeafSnn35248 	if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE, B_FALSE)
36719acbbeafSnn35248 	    != Z_OK)
3672865e09a4Sgjelinek 		return (Z_ERR);
3673865e09a4Sgjelinek 	if (verify_details(CMD_CLONE) != Z_OK)
3674865e09a4Sgjelinek 		return (Z_ERR);
3675865e09a4Sgjelinek 
3676865e09a4Sgjelinek 	/*
3677865e09a4Sgjelinek 	 * We also need to do some extra validation on the source zone.
3678865e09a4Sgjelinek 	 */
3679865e09a4Sgjelinek 	if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
3680865e09a4Sgjelinek 		zerror(gettext("%s operation is invalid for the global zone."),
3681865e09a4Sgjelinek 		    cmd_to_str(CMD_CLONE));
3682865e09a4Sgjelinek 		return (Z_ERR);
3683865e09a4Sgjelinek 	}
3684865e09a4Sgjelinek 
3685865e09a4Sgjelinek 	if (strncmp(source_zone, "SUNW", 4) == 0) {
3686865e09a4Sgjelinek 		zerror(gettext("%s operation is invalid for zones starting "
3687865e09a4Sgjelinek 		    "with SUNW."), cmd_to_str(CMD_CLONE));
3688865e09a4Sgjelinek 		return (Z_ERR);
3689865e09a4Sgjelinek 	}
3690865e09a4Sgjelinek 
3691865e09a4Sgjelinek 	zent = lookup_running_zone(source_zone);
3692865e09a4Sgjelinek 	if (zent != NULL) {
3693865e09a4Sgjelinek 		/* check whether the zone is ready or running */
3694865e09a4Sgjelinek 		if ((err = zone_get_state(zent->zname, &zent->zstate_num))
3695865e09a4Sgjelinek 		    != Z_OK) {
3696865e09a4Sgjelinek 			errno = err;
3697865e09a4Sgjelinek 			zperror2(zent->zname, gettext("could not get state"));
3698865e09a4Sgjelinek 			/* can't tell, so hedge */
3699865e09a4Sgjelinek 			zent->zstate_str = "ready/running";
3700865e09a4Sgjelinek 		} else {
3701865e09a4Sgjelinek 			zent->zstate_str = zone_state_str(zent->zstate_num);
3702865e09a4Sgjelinek 		}
3703865e09a4Sgjelinek 		zerror(gettext("%s operation is invalid for %s zones."),
3704865e09a4Sgjelinek 		    cmd_to_str(CMD_CLONE), zent->zstate_str);
3705865e09a4Sgjelinek 		return (Z_ERR);
3706865e09a4Sgjelinek 	}
3707865e09a4Sgjelinek 
3708865e09a4Sgjelinek 	if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
3709865e09a4Sgjelinek 		errno = err;
3710865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not get state"));
3711865e09a4Sgjelinek 		return (Z_ERR);
3712865e09a4Sgjelinek 	}
3713865e09a4Sgjelinek 	if (state != ZONE_STATE_INSTALLED) {
3714865e09a4Sgjelinek 		(void) fprintf(stderr,
3715865e09a4Sgjelinek 		    gettext("%s: zone %s is %s; %s is required.\n"),
3716865e09a4Sgjelinek 		    execname, source_zone, zone_state_str(state),
3717865e09a4Sgjelinek 		    zone_state_str(ZONE_STATE_INSTALLED));
3718865e09a4Sgjelinek 		return (Z_ERR);
3719865e09a4Sgjelinek 	}
3720865e09a4Sgjelinek 
3721865e09a4Sgjelinek 	/*
3722865e09a4Sgjelinek 	 * The source zone checks out ok, continue with the clone.
3723865e09a4Sgjelinek 	 */
3724865e09a4Sgjelinek 
3725865e09a4Sgjelinek 	if (validate_clone(source_zone, target_zone) != Z_OK)
3726865e09a4Sgjelinek 		return (Z_ERR);
3727865e09a4Sgjelinek 
3728865e09a4Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
3729865e09a4Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
3730865e09a4Sgjelinek 		    "zoneadm");
3731865e09a4Sgjelinek 		return (Z_ERR);
3732865e09a4Sgjelinek 	}
3733865e09a4Sgjelinek 
3734865e09a4Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
3735865e09a4Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
3736865e09a4Sgjelinek 		errno = err;
3737865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
3738865e09a4Sgjelinek 		goto done;
3739865e09a4Sgjelinek 	}
3740865e09a4Sgjelinek 
3741865e09a4Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3742865e09a4Sgjelinek 	    != Z_OK) {
3743865e09a4Sgjelinek 		errno = err;
3744865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
3745865e09a4Sgjelinek 		goto done;
3746865e09a4Sgjelinek 	}
3747865e09a4Sgjelinek 
3748865e09a4Sgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
3749865e09a4Sgjelinek 	    != Z_OK) {
3750865e09a4Sgjelinek 		errno = err;
3751865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not set state"));
3752865e09a4Sgjelinek 		goto done;
3753865e09a4Sgjelinek 	}
3754865e09a4Sgjelinek 
37550b5de56dSgjelinek 	if (snapshot != NULL) {
37560b5de56dSgjelinek 		err = clone_snapshot_zfs(snapshot, zonepath);
37570b5de56dSgjelinek 	} else {
37580b5de56dSgjelinek 		/*
37590b5de56dSgjelinek 		 * We always copy the clone unless the source is ZFS and a
37600b5de56dSgjelinek 		 * ZFS clone worked.  We fallback to copying if the ZFS clone
37610b5de56dSgjelinek 		 * fails for some reason.
37620b5de56dSgjelinek 		 */
37630b5de56dSgjelinek 		err = Z_ERR;
37640b5de56dSgjelinek 		if (method == NULL && is_zonepath_zfs(source_zonepath))
37650b5de56dSgjelinek 			err = clone_zfs(source_zone, source_zonepath, zonepath);
3766865e09a4Sgjelinek 
37675cd08338Sgjelinek 		if (err != Z_OK)
37680b5de56dSgjelinek 			err = clone_copy(source_zonepath, zonepath);
37690b5de56dSgjelinek 	}
3770865e09a4Sgjelinek 
37719acbbeafSnn35248 	/*
37729acbbeafSnn35248 	 * Trusted Extensions requires that cloned zones use the same sysid
37739acbbeafSnn35248 	 * configuration, so it is not appropriate to perform any
37749acbbeafSnn35248 	 * post-clone reconfiguration.
37759acbbeafSnn35248 	 */
37769acbbeafSnn35248 	if ((err == Z_OK) && !is_system_labeled())
37779acbbeafSnn35248 		err = zone_postclone(zonepath);
3778865e09a4Sgjelinek 
3779865e09a4Sgjelinek done:
37809acbbeafSnn35248 	/*
37819acbbeafSnn35248 	 * If everything went well, we mark the zone as installed.
37829acbbeafSnn35248 	 */
37839acbbeafSnn35248 	if (err == Z_OK) {
37849acbbeafSnn35248 		err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
37859acbbeafSnn35248 		if (err != Z_OK) {
37869acbbeafSnn35248 			errno = err;
37879acbbeafSnn35248 			zperror2(target_zone, gettext("could not set state"));
37889acbbeafSnn35248 		}
37899acbbeafSnn35248 	}
3790865e09a4Sgjelinek 	release_lock_file(lockfd);
3791865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3792865e09a4Sgjelinek }
3793865e09a4Sgjelinek 
379407b574eeSgjelinek /*
37950b5de56dSgjelinek  * Used when removing a zonepath after uninstalling or cleaning up after
37960b5de56dSgjelinek  * the move subcommand.  This handles a zonepath that has non-standard
37970b5de56dSgjelinek  * contents so that we will only cleanup the stuff we know about and leave
37980b5de56dSgjelinek  * any user data alone.
379907b574eeSgjelinek  *
38000b5de56dSgjelinek  * If the "all" parameter is true then we should remove the whole zonepath
38010b5de56dSgjelinek  * even if it has non-standard files/directories in it.  This can be used when
38020b5de56dSgjelinek  * we need to cleanup after moving the zonepath across file systems.
38030b5de56dSgjelinek  *
38040b5de56dSgjelinek  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
38050b5de56dSgjelinek  * and not the shell.
380607b574eeSgjelinek  */
380707b574eeSgjelinek static int
38080b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all)
380907b574eeSgjelinek {
381007b574eeSgjelinek 	int		status;
38110b5de56dSgjelinek 	int		i;
38120b5de56dSgjelinek 	boolean_t	non_std = B_FALSE;
38130b5de56dSgjelinek 	struct dirent	*dp;
38140b5de56dSgjelinek 	DIR		*dirp;
38150b5de56dSgjelinek 	char		*std_entries[] = {"dev", "lu", "root", NULL};
38160b5de56dSgjelinek 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
38170b5de56dSgjelinek 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
381807b574eeSgjelinek 
381907b574eeSgjelinek 	/*
38200b5de56dSgjelinek 	 * We shouldn't need these checks but lets be paranoid since we
38210b5de56dSgjelinek 	 * could blow away the whole system here if we got the wrong zonepath.
382207b574eeSgjelinek 	 */
38230b5de56dSgjelinek 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
38240b5de56dSgjelinek 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
38250b5de56dSgjelinek 		return (Z_INVAL);
38260b5de56dSgjelinek 	}
38270b5de56dSgjelinek 
382807b574eeSgjelinek 	/*
38290b5de56dSgjelinek 	 * If the dirpath is already gone (maybe it was manually removed) then
38300b5de56dSgjelinek 	 * we just return Z_OK so that the cleanup is successful.
383107b574eeSgjelinek 	 */
38320b5de56dSgjelinek 	if ((dirp = opendir(zonepath)) == NULL)
38330b5de56dSgjelinek 		return (Z_OK);
38340b5de56dSgjelinek 
38350b5de56dSgjelinek 	/*
38360b5de56dSgjelinek 	 * Look through the zonepath directory to see if there are any
38370b5de56dSgjelinek 	 * non-standard files/dirs.  Also skip .zfs since that might be
38380b5de56dSgjelinek 	 * there but we'll handle ZFS file systems as a special case.
38390b5de56dSgjelinek 	 */
38400b5de56dSgjelinek 	while ((dp = readdir(dirp)) != NULL) {
38410b5de56dSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
38420b5de56dSgjelinek 		    strcmp(dp->d_name, "..") == 0 ||
38430b5de56dSgjelinek 		    strcmp(dp->d_name, ".zfs") == 0)
38440b5de56dSgjelinek 			continue;
38450b5de56dSgjelinek 
38460b5de56dSgjelinek 		for (i = 0; std_entries[i] != NULL; i++)
38470b5de56dSgjelinek 			if (strcmp(dp->d_name, std_entries[i]) == 0)
38480b5de56dSgjelinek 				break;
38490b5de56dSgjelinek 
38500b5de56dSgjelinek 		if (std_entries[i] == NULL)
38510b5de56dSgjelinek 			non_std = B_TRUE;
38520b5de56dSgjelinek 	}
38530b5de56dSgjelinek 	(void) closedir(dirp);
38540b5de56dSgjelinek 
38550b5de56dSgjelinek 	if (!all && non_std) {
38560b5de56dSgjelinek 		/*
38570b5de56dSgjelinek 		 * There are extra, non-standard directories/files in the
38580b5de56dSgjelinek 		 * zonepath so we don't want to remove the zonepath.  We
38590b5de56dSgjelinek 		 * just want to remove the standard directories and leave
38600b5de56dSgjelinek 		 * the user data alone.
38610b5de56dSgjelinek 		 */
38620b5de56dSgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
38630b5de56dSgjelinek 
38640b5de56dSgjelinek 		for (i = 0; std_entries[i] != NULL; i++) {
38650b5de56dSgjelinek 			char tmpbuf[MAXPATHLEN];
38660b5de56dSgjelinek 
38670b5de56dSgjelinek 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
38680b5de56dSgjelinek 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
38690b5de56dSgjelinek 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
38700b5de56dSgjelinek 			    sizeof (cmdbuf)) {
38710b5de56dSgjelinek 				(void) fprintf(stderr,
38720b5de56dSgjelinek 				    gettext("path is too long\n"));
38730b5de56dSgjelinek 				return (Z_INVAL);
38740b5de56dSgjelinek 			}
387507b574eeSgjelinek 		}
387607b574eeSgjelinek 
387707b574eeSgjelinek 		status = do_subproc(cmdbuf);
387807b574eeSgjelinek 
38790b5de56dSgjelinek 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
38800b5de56dSgjelinek 		    "remove %s\nbecause it contains additional user data.  "
38810b5de56dSgjelinek 		    "Only the standard directory\nentries have been "
38820b5de56dSgjelinek 		    "removed.\n"),
38830b5de56dSgjelinek 		    zonepath);
388407b574eeSgjelinek 
38859acbbeafSnn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
38869acbbeafSnn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
38870b5de56dSgjelinek 	}
38880b5de56dSgjelinek 
38890b5de56dSgjelinek 	/*
38900b5de56dSgjelinek 	 * There is nothing unexpected in the zonepath, try to get rid of the
38910b5de56dSgjelinek 	 * whole zonepath directory.
38920b5de56dSgjelinek 	 *
38930b5de56dSgjelinek 	 * If the zonepath is its own zfs file system, try to destroy the
38940b5de56dSgjelinek 	 * file system.  If that fails for some reason (e.g. it has clones)
38950b5de56dSgjelinek 	 * then we'll just remove the contents of the zonepath.
38960b5de56dSgjelinek 	 */
38970b5de56dSgjelinek 	if (is_zonepath_zfs(zonepath)) {
38980b5de56dSgjelinek 		if (destroy_zfs(zonepath) == Z_OK)
38990b5de56dSgjelinek 			return (Z_OK);
39000b5de56dSgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
39010b5de56dSgjelinek 		    " %s/*", zonepath);
39020b5de56dSgjelinek 		status = do_subproc(cmdbuf);
39039acbbeafSnn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39049acbbeafSnn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39050b5de56dSgjelinek 	}
39060b5de56dSgjelinek 
39070b5de56dSgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
39080b5de56dSgjelinek 	    zonepath);
39090b5de56dSgjelinek 	status = do_subproc(cmdbuf);
39109acbbeafSnn35248 
39119acbbeafSnn35248 	return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
39129acbbeafSnn35248 	    ? Z_OK : Z_ERR);
391307b574eeSgjelinek }
391407b574eeSgjelinek 
3915865e09a4Sgjelinek static int
3916865e09a4Sgjelinek move_func(int argc, char *argv[])
3917865e09a4Sgjelinek {
3918865e09a4Sgjelinek 	char *new_zonepath = NULL;
3919865e09a4Sgjelinek 	int lockfd;
3920865e09a4Sgjelinek 	int err, arg;
3921865e09a4Sgjelinek 	char zonepath[MAXPATHLEN];
3922865e09a4Sgjelinek 	zone_dochandle_t handle;
3923865e09a4Sgjelinek 	boolean_t fast;
39240b5de56dSgjelinek 	boolean_t is_zfs = B_FALSE;
39250b5de56dSgjelinek 	struct dirent *dp;
39260b5de56dSgjelinek 	DIR *dirp;
39270b5de56dSgjelinek 	boolean_t empty = B_TRUE;
3928865e09a4Sgjelinek 	boolean_t revert;
3929865e09a4Sgjelinek 	struct stat zonepath_buf;
3930865e09a4Sgjelinek 	struct stat new_zonepath_buf;
3931865e09a4Sgjelinek 
3932865e09a4Sgjelinek 	if (zonecfg_in_alt_root()) {
3933865e09a4Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
3934865e09a4Sgjelinek 		return (Z_ERR);
3935865e09a4Sgjelinek 	}
3936865e09a4Sgjelinek 
3937865e09a4Sgjelinek 	optind = 0;
3938865e09a4Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
3939865e09a4Sgjelinek 		switch (arg) {
3940865e09a4Sgjelinek 		case '?':
3941865e09a4Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
3942865e09a4Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
3943865e09a4Sgjelinek 		default:
3944865e09a4Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
3945865e09a4Sgjelinek 			return (Z_USAGE);
3946865e09a4Sgjelinek 		}
3947865e09a4Sgjelinek 	}
3948865e09a4Sgjelinek 	if (argc != (optind + 1)) {
3949865e09a4Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
3950865e09a4Sgjelinek 		return (Z_USAGE);
3951865e09a4Sgjelinek 	}
3952865e09a4Sgjelinek 	new_zonepath = argv[optind];
39539acbbeafSnn35248 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
39549acbbeafSnn35248 	    != Z_OK)
3955865e09a4Sgjelinek 		return (Z_ERR);
3956865e09a4Sgjelinek 	if (verify_details(CMD_MOVE) != Z_OK)
3957865e09a4Sgjelinek 		return (Z_ERR);
3958865e09a4Sgjelinek 
3959865e09a4Sgjelinek 	/*
3960865e09a4Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
3961865e09a4Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
39620b5de56dSgjelinek 	 * stat to see if we are doing a cross file system move or not.
3963865e09a4Sgjelinek 	 */
3964865e09a4Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
3965865e09a4Sgjelinek 		return (Z_ERR);
3966865e09a4Sgjelinek 
3967865e09a4Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3968865e09a4Sgjelinek 	    != Z_OK) {
3969865e09a4Sgjelinek 		errno = err;
3970865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
3971865e09a4Sgjelinek 		return (Z_ERR);
3972865e09a4Sgjelinek 	}
3973865e09a4Sgjelinek 
3974865e09a4Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
3975865e09a4Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
3976865e09a4Sgjelinek 		return (Z_ERR);
3977865e09a4Sgjelinek 	}
3978865e09a4Sgjelinek 
3979865e09a4Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
3980865e09a4Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
3981865e09a4Sgjelinek 		return (Z_ERR);
3982865e09a4Sgjelinek 	}
3983865e09a4Sgjelinek 
39840b5de56dSgjelinek 	/*
39850b5de56dSgjelinek 	 * Check if the destination directory is empty.
39860b5de56dSgjelinek 	 */
39870b5de56dSgjelinek 	if ((dirp = opendir(new_zonepath)) == NULL) {
39880b5de56dSgjelinek 		zperror(gettext("could not open new zone path"), B_FALSE);
39890b5de56dSgjelinek 		return (Z_ERR);
39900b5de56dSgjelinek 	}
39910b5de56dSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
39920b5de56dSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
39930b5de56dSgjelinek 		    strcmp(dp->d_name, "..") == 0)
39940b5de56dSgjelinek 			continue;
39950b5de56dSgjelinek 		empty = B_FALSE;
39960b5de56dSgjelinek 		break;
39970b5de56dSgjelinek 	}
39980b5de56dSgjelinek 	(void) closedir(dirp);
39990b5de56dSgjelinek 
40000b5de56dSgjelinek 	/* Error if there is anything in the destination directory. */
40010b5de56dSgjelinek 	if (!empty) {
40020b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not move zone to %s: "
40030b5de56dSgjelinek 		    "directory not empty\n"), new_zonepath);
40040b5de56dSgjelinek 		return (Z_ERR);
40050b5de56dSgjelinek 	}
40060b5de56dSgjelinek 
4007865e09a4Sgjelinek 	/* Don't move the zone if anything is still mounted there */
4008865e09a4Sgjelinek 	if (zonecfg_find_mounts(zonepath, NULL, NULL)) {
40090b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
4010865e09a4Sgjelinek 		    "subdirectories of %s.\n"), zonepath);
4011865e09a4Sgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4012865e09a4Sgjelinek 		return (Z_ERR);
4013865e09a4Sgjelinek 	}
4014865e09a4Sgjelinek 
4015865e09a4Sgjelinek 	/*
4016865e09a4Sgjelinek 	 * Check if we are moving in the same file system and can do a fast
4017865e09a4Sgjelinek 	 * move or if we are crossing file systems and have to copy the data.
4018865e09a4Sgjelinek 	 */
4019865e09a4Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
4020865e09a4Sgjelinek 
4021865e09a4Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
4022865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
4023865e09a4Sgjelinek 		return (Z_ERR);
4024865e09a4Sgjelinek 	}
4025865e09a4Sgjelinek 
4026865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4027865e09a4Sgjelinek 		errno = err;
4028865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
4029865e09a4Sgjelinek 		zonecfg_fini_handle(handle);
4030865e09a4Sgjelinek 		return (Z_ERR);
4031865e09a4Sgjelinek 	}
4032865e09a4Sgjelinek 
4033865e09a4Sgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
4034865e09a4Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4035865e09a4Sgjelinek 		    "zoneadm");
4036865e09a4Sgjelinek 		zonecfg_fini_handle(handle);
4037865e09a4Sgjelinek 		return (Z_ERR);
4038865e09a4Sgjelinek 	}
4039865e09a4Sgjelinek 
4040865e09a4Sgjelinek 	/*
40410b5de56dSgjelinek 	 * We're making some file system changes now so we have to clean up
40420b5de56dSgjelinek 	 * the file system before we are done.  This will either clean up the
4043865e09a4Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
4044865e09a4Sgjelinek 	 * old zonepath if everything is ok.
4045865e09a4Sgjelinek 	 */
4046865e09a4Sgjelinek 	revert = B_TRUE;
4047865e09a4Sgjelinek 
40480b5de56dSgjelinek 	if (is_zonepath_zfs(zonepath) &&
40490b5de56dSgjelinek 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
40500b5de56dSgjelinek 		is_zfs = B_TRUE;
40510b5de56dSgjelinek 
40520b5de56dSgjelinek 	} else if (fast) {
4053865e09a4Sgjelinek 		/* same file system, use rename for a quick move */
4054865e09a4Sgjelinek 
4055865e09a4Sgjelinek 		/*
4056865e09a4Sgjelinek 		 * Remove the new_zonepath directory that got created above
4057865e09a4Sgjelinek 		 * during the validation.  It gets in the way of the rename.
4058865e09a4Sgjelinek 		 */
4059865e09a4Sgjelinek 		if (rmdir(new_zonepath) != 0) {
4060865e09a4Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
4061865e09a4Sgjelinek 			    B_FALSE);
4062865e09a4Sgjelinek 			zonecfg_fini_handle(handle);
4063865e09a4Sgjelinek 			release_lock_file(lockfd);
4064865e09a4Sgjelinek 			return (Z_ERR);
4065865e09a4Sgjelinek 		}
4066865e09a4Sgjelinek 
4067865e09a4Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
4068865e09a4Sgjelinek 			/*
4069865e09a4Sgjelinek 			 * If this fails we don't need to do all of the
4070865e09a4Sgjelinek 			 * cleanup that happens for the rest of the code
4071865e09a4Sgjelinek 			 * so just return from this error.
4072865e09a4Sgjelinek 			 */
4073865e09a4Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
4074865e09a4Sgjelinek 			zonecfg_fini_handle(handle);
4075865e09a4Sgjelinek 			release_lock_file(lockfd);
4076865e09a4Sgjelinek 			return (Z_ERR);
4077865e09a4Sgjelinek 		}
4078865e09a4Sgjelinek 
4079865e09a4Sgjelinek 	} else {
40800b5de56dSgjelinek 		/*
40810b5de56dSgjelinek 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
40820b5de56dSgjelinek 		 * we don't care if this works or not since we always have the
40830b5de56dSgjelinek 		 * default behavior of a simple directory for the zonepath.
40840b5de56dSgjelinek 		 */
40850b5de56dSgjelinek 		create_zfs_zonepath(new_zonepath);
40860b5de56dSgjelinek 
4087865e09a4Sgjelinek 		(void) printf(gettext(
40880b5de56dSgjelinek 		    "Moving across file systems; copying zonepath %s..."),
4089865e09a4Sgjelinek 		    zonepath);
4090865e09a4Sgjelinek 		(void) fflush(stdout);
4091865e09a4Sgjelinek 
4092865e09a4Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
4093865e09a4Sgjelinek 
4094865e09a4Sgjelinek 		(void) printf("\n");
4095865e09a4Sgjelinek 		if (err != Z_OK)
4096865e09a4Sgjelinek 			goto done;
4097865e09a4Sgjelinek 	}
4098865e09a4Sgjelinek 
4099865e09a4Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
4100865e09a4Sgjelinek 		errno = err;
4101865e09a4Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
4102865e09a4Sgjelinek 		goto done;
4103865e09a4Sgjelinek 	}
4104865e09a4Sgjelinek 
4105865e09a4Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
4106865e09a4Sgjelinek 		errno = err;
4107865e09a4Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
4108865e09a4Sgjelinek 		goto done;
4109865e09a4Sgjelinek 	}
4110865e09a4Sgjelinek 
4111865e09a4Sgjelinek 	revert = B_FALSE;
4112865e09a4Sgjelinek 
4113865e09a4Sgjelinek done:
4114865e09a4Sgjelinek 	zonecfg_fini_handle(handle);
4115865e09a4Sgjelinek 	release_lock_file(lockfd);
4116865e09a4Sgjelinek 
4117865e09a4Sgjelinek 	/*
41180b5de56dSgjelinek 	 * Clean up the file system based on how things went.  We either
4119865e09a4Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
4120865e09a4Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
4121865e09a4Sgjelinek 	 */
4122865e09a4Sgjelinek 	if (revert) {
4123865e09a4Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
41240b5de56dSgjelinek 		if (is_zfs) {
41250b5de56dSgjelinek 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
41260b5de56dSgjelinek 				(void) fprintf(stderr, gettext("could not "
41270b5de56dSgjelinek 				    "restore zonepath, the zfs mountpoint is "
41280b5de56dSgjelinek 				    "set as:\n%s\n"), new_zonepath);
41290b5de56dSgjelinek 				/*
41300b5de56dSgjelinek 				 * err is already != Z_OK since we're reverting
41310b5de56dSgjelinek 				 */
41320b5de56dSgjelinek 			}
41330b5de56dSgjelinek 
41340b5de56dSgjelinek 		} else if (fast) {
4135865e09a4Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
4136865e09a4Sgjelinek 				zperror(gettext("could not restore zonepath"),
4137865e09a4Sgjelinek 				    B_FALSE);
4138865e09a4Sgjelinek 				/*
4139865e09a4Sgjelinek 				 * err is already != Z_OK since we're reverting
4140865e09a4Sgjelinek 				 */
4141865e09a4Sgjelinek 			}
4142865e09a4Sgjelinek 		} else {
4143865e09a4Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
4144865e09a4Sgjelinek 			    new_zonepath);
4145865e09a4Sgjelinek 			(void) fflush(stdout);
41460b5de56dSgjelinek 			err = cleanup_zonepath(new_zonepath, B_TRUE);
4147865e09a4Sgjelinek 			(void) printf("\n");
4148865e09a4Sgjelinek 
414907b574eeSgjelinek 			if (err != Z_OK) {
4150865e09a4Sgjelinek 				errno = err;
4151865e09a4Sgjelinek 				zperror(gettext("could not remove new "
4152865e09a4Sgjelinek 				    "zonepath"), B_TRUE);
4153865e09a4Sgjelinek 			} else {
4154865e09a4Sgjelinek 				/*
4155865e09a4Sgjelinek 				 * Because we're reverting we know the mainline
4156865e09a4Sgjelinek 				 * code failed but we just reused the err
4157865e09a4Sgjelinek 				 * variable so we reset it back to Z_ERR.
4158865e09a4Sgjelinek 				 */
4159865e09a4Sgjelinek 				err = Z_ERR;
4160865e09a4Sgjelinek 			}
4161865e09a4Sgjelinek 		}
4162865e09a4Sgjelinek 
4163865e09a4Sgjelinek 	} else {
4164865e09a4Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
41650b5de56dSgjelinek 		if (!is_zfs && !fast) {
4166865e09a4Sgjelinek 			(void) printf(
4167865e09a4Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
4168865e09a4Sgjelinek 			(void) fflush(stdout);
41690b5de56dSgjelinek 			err = cleanup_zonepath(zonepath, B_TRUE);
4170865e09a4Sgjelinek 			(void) printf("\n");
4171865e09a4Sgjelinek 
417207b574eeSgjelinek 			if (err != Z_OK) {
4173865e09a4Sgjelinek 				errno = err;
4174865e09a4Sgjelinek 				zperror(gettext("could not remove zonepath"),
4175865e09a4Sgjelinek 				    B_TRUE);
4176865e09a4Sgjelinek 			}
4177865e09a4Sgjelinek 		}
4178865e09a4Sgjelinek 	}
4179865e09a4Sgjelinek 
4180865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4181865e09a4Sgjelinek }
4182865e09a4Sgjelinek 
4183ee519a1fSgjelinek static int
4184ee519a1fSgjelinek detach_func(int argc, char *argv[])
4185ee519a1fSgjelinek {
4186ee519a1fSgjelinek 	int lockfd;
4187ee519a1fSgjelinek 	int err, arg;
4188ee519a1fSgjelinek 	char zonepath[MAXPATHLEN];
4189ee519a1fSgjelinek 	zone_dochandle_t handle;
41908cd327d5Sgjelinek 	boolean_t execute = B_TRUE;
4191ee519a1fSgjelinek 
4192ee519a1fSgjelinek 	if (zonecfg_in_alt_root()) {
4193ee519a1fSgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
4194ee519a1fSgjelinek 		return (Z_ERR);
4195ee519a1fSgjelinek 	}
4196ee519a1fSgjelinek 
4197ee519a1fSgjelinek 	optind = 0;
41988cd327d5Sgjelinek 	if ((arg = getopt(argc, argv, "?n")) != EOF) {
4199ee519a1fSgjelinek 		switch (arg) {
4200ee519a1fSgjelinek 		case '?':
4201ee519a1fSgjelinek 			sub_usage(SHELP_DETACH, CMD_DETACH);
4202ee519a1fSgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
42038cd327d5Sgjelinek 		case 'n':
42048cd327d5Sgjelinek 			execute = B_FALSE;
42058cd327d5Sgjelinek 			break;
4206ee519a1fSgjelinek 		default:
4207ee519a1fSgjelinek 			sub_usage(SHELP_DETACH, CMD_DETACH);
4208ee519a1fSgjelinek 			return (Z_USAGE);
4209ee519a1fSgjelinek 		}
4210ee519a1fSgjelinek 	}
42119acbbeafSnn35248 
42128cd327d5Sgjelinek 	if (execute) {
42139acbbeafSnn35248 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
42149acbbeafSnn35248 		    B_FALSE) != Z_OK)
4215ee519a1fSgjelinek 			return (Z_ERR);
4216ee519a1fSgjelinek 		if (verify_details(CMD_DETACH) != Z_OK)
4217ee519a1fSgjelinek 			return (Z_ERR);
42188cd327d5Sgjelinek 	} else {
42198cd327d5Sgjelinek 		/*
42208cd327d5Sgjelinek 		 * We want a dry-run to work for a non-privileged user so we
42218cd327d5Sgjelinek 		 * only do minimal validation.
42228cd327d5Sgjelinek 		 */
42238cd327d5Sgjelinek 		if (getzoneid() != GLOBAL_ZONEID) {
42248cd327d5Sgjelinek 			zerror(gettext("must be in the global zone to %s a "
42258cd327d5Sgjelinek 			    "zone."), cmd_to_str(CMD_DETACH));
42268cd327d5Sgjelinek 			return (Z_ERR);
42278cd327d5Sgjelinek 		}
42288cd327d5Sgjelinek 
42298cd327d5Sgjelinek 		if (target_zone == NULL) {
42308cd327d5Sgjelinek 			zerror(gettext("no zone specified"));
42318cd327d5Sgjelinek 			return (Z_ERR);
42328cd327d5Sgjelinek 		}
42338cd327d5Sgjelinek 
42348cd327d5Sgjelinek 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
42358cd327d5Sgjelinek 			zerror(gettext("%s operation is invalid for the "
42368cd327d5Sgjelinek 			    "global zone."), cmd_to_str(CMD_DETACH));
42378cd327d5Sgjelinek 			return (Z_ERR);
42388cd327d5Sgjelinek 		}
42398cd327d5Sgjelinek 	}
4240ee519a1fSgjelinek 
4241ee519a1fSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4242ee519a1fSgjelinek 	    != Z_OK) {
4243ee519a1fSgjelinek 		errno = err;
4244ee519a1fSgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
4245ee519a1fSgjelinek 		return (Z_ERR);
4246ee519a1fSgjelinek 	}
4247ee519a1fSgjelinek 
4248ee519a1fSgjelinek 	/* Don't detach the zone if anything is still mounted there */
42498cd327d5Sgjelinek 	if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
42500b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
4251ee519a1fSgjelinek 		    "subdirectories of %s.\n"), zonepath);
4252ee519a1fSgjelinek 		(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4253ee519a1fSgjelinek 		return (Z_ERR);
4254ee519a1fSgjelinek 	}
4255ee519a1fSgjelinek 
4256ee519a1fSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
4257ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4258ee519a1fSgjelinek 		return (Z_ERR);
4259ee519a1fSgjelinek 	}
4260ee519a1fSgjelinek 
4261ee519a1fSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4262ee519a1fSgjelinek 		errno = err;
4263ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4264ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
4265ee519a1fSgjelinek 		return (Z_ERR);
4266ee519a1fSgjelinek 	}
4267ee519a1fSgjelinek 
42688cd327d5Sgjelinek 	if (execute && grab_lock_file(target_zone, &lockfd) != Z_OK) {
4269ee519a1fSgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4270ee519a1fSgjelinek 		    "zoneadm");
4271ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
4272ee519a1fSgjelinek 		return (Z_ERR);
4273ee519a1fSgjelinek 	}
4274ee519a1fSgjelinek 
4275ee519a1fSgjelinek 	if ((err = zonecfg_get_detach_info(handle, B_TRUE)) != Z_OK) {
4276ee519a1fSgjelinek 		errno = err;
4277ee519a1fSgjelinek 		zperror(gettext("getting the detach information failed"),
4278ee519a1fSgjelinek 		    B_TRUE);
4279ee519a1fSgjelinek 		goto done;
4280ee519a1fSgjelinek 	}
4281ee519a1fSgjelinek 
42828cd327d5Sgjelinek 	if ((err = zonecfg_detach_save(handle, (execute ? 0 : ZONE_DRY_RUN)))
42838cd327d5Sgjelinek 	    != Z_OK) {
4284ee519a1fSgjelinek 		errno = err;
4285ee519a1fSgjelinek 		zperror(gettext("saving the detach manifest failed"), B_TRUE);
4286ee519a1fSgjelinek 		goto done;
4287ee519a1fSgjelinek 	}
4288ee519a1fSgjelinek 
42898cd327d5Sgjelinek 	/*
42908cd327d5Sgjelinek 	 * Set the zone state back to configured unless we are running with the
42918cd327d5Sgjelinek 	 * no-execute option.
42928cd327d5Sgjelinek 	 */
42938cd327d5Sgjelinek 	if (execute && (err = zone_set_state(target_zone,
42948cd327d5Sgjelinek 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
4295ee519a1fSgjelinek 		errno = err;
4296ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4297ee519a1fSgjelinek 	}
4298ee519a1fSgjelinek 
4299ee519a1fSgjelinek done:
4300ee519a1fSgjelinek 	zonecfg_fini_handle(handle);
43018cd327d5Sgjelinek 	if (execute)
4302ee519a1fSgjelinek 		release_lock_file(lockfd);
4303ee519a1fSgjelinek 
4304ee519a1fSgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4305ee519a1fSgjelinek }
4306ee519a1fSgjelinek 
4307ee519a1fSgjelinek /*
4308ee519a1fSgjelinek  * During attach we go through and fix up the /dev entries for the zone
4309ee519a1fSgjelinek  * we are attaching.  In order to regenerate /dev with the correct devices,
4310ee519a1fSgjelinek  * the old /dev will be removed, the zone readied (which generates a new
4311ee519a1fSgjelinek  * /dev) then halted, then we use the info from the manifest to update
4312ee519a1fSgjelinek  * the modes, owners, etc. on the new /dev.
4313ee519a1fSgjelinek  */
4314ee519a1fSgjelinek static int
4315ee519a1fSgjelinek dev_fix(zone_dochandle_t handle)
4316ee519a1fSgjelinek {
4317ee519a1fSgjelinek 	int			res;
4318ee519a1fSgjelinek 	int			err;
4319ee519a1fSgjelinek 	int			status;
4320ee519a1fSgjelinek 	struct zone_devpermtab	devtab;
4321ee519a1fSgjelinek 	zone_cmd_arg_t		zarg;
4322ee519a1fSgjelinek 	char			devpath[MAXPATHLEN];
4323ee519a1fSgjelinek 				/* 6: "exec " and " " */
4324ee519a1fSgjelinek 	char			cmdbuf[sizeof (RMCOMMAND) + MAXPATHLEN + 6];
4325ee519a1fSgjelinek 
4326ee519a1fSgjelinek 	if ((res = zonecfg_get_zonepath(handle, devpath, sizeof (devpath)))
4327ee519a1fSgjelinek 	    != Z_OK)
4328ee519a1fSgjelinek 		return (res);
4329ee519a1fSgjelinek 
4330ee519a1fSgjelinek 	if (strlcat(devpath, "/dev", sizeof (devpath)) >= sizeof (devpath))
4331ee519a1fSgjelinek 		return (Z_TOO_BIG);
4332ee519a1fSgjelinek 
4333ee519a1fSgjelinek 	/*
4334ee519a1fSgjelinek 	 * "exec" the command so that the returned status is that of
4335ee519a1fSgjelinek 	 * RMCOMMAND and not the shell.
4336ee519a1fSgjelinek 	 */
43379acbbeafSnn35248 	(void) snprintf(cmdbuf, sizeof (cmdbuf), EXEC_PREFIX RMCOMMAND " %s",
4338ee519a1fSgjelinek 	    devpath);
4339ee519a1fSgjelinek 	status = do_subproc(cmdbuf);
43409acbbeafSnn35248 	if ((err = subproc_status(RMCOMMAND, status, B_TRUE)) !=
43419acbbeafSnn35248 	    ZONE_SUBPROC_OK) {
4342ee519a1fSgjelinek 		(void) fprintf(stderr,
4343ee519a1fSgjelinek 		    gettext("could not remove existing /dev\n"));
4344ee519a1fSgjelinek 		return (Z_ERR);
4345ee519a1fSgjelinek 	}
4346ee519a1fSgjelinek 
4347ee519a1fSgjelinek 	/* In order to ready the zone, it must be in the installed state */
4348ee519a1fSgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4349ee519a1fSgjelinek 		errno = err;
4350ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4351ee519a1fSgjelinek 		return (Z_ERR);
4352ee519a1fSgjelinek 	}
4353ee519a1fSgjelinek 
4354ee519a1fSgjelinek 	/* We have to ready the zone to regen the dev tree */
4355ee519a1fSgjelinek 	zarg.cmd = Z_READY;
4356ee519a1fSgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4357ee519a1fSgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
4358ee519a1fSgjelinek 		return (Z_ERR);
4359ee519a1fSgjelinek 	}
4360ee519a1fSgjelinek 
4361ee519a1fSgjelinek 	zarg.cmd = Z_HALT;
4362ee519a1fSgjelinek 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4363ee519a1fSgjelinek 		zerror(gettext("call to %s failed"), "zoneadmd");
4364ee519a1fSgjelinek 		return (Z_ERR);
4365ee519a1fSgjelinek 	}
4366ee519a1fSgjelinek 
4367ee519a1fSgjelinek 	if (zonecfg_setdevperment(handle) != Z_OK) {
4368ee519a1fSgjelinek 		(void) fprintf(stderr,
4369ee519a1fSgjelinek 		    gettext("unable to enumerate device entries\n"));
4370ee519a1fSgjelinek 		return (Z_ERR);
4371ee519a1fSgjelinek 	}
4372ee519a1fSgjelinek 
4373ee519a1fSgjelinek 	while (zonecfg_getdevperment(handle, &devtab) == Z_OK) {
4374ee519a1fSgjelinek 		int err;
4375ee519a1fSgjelinek 
4376ee519a1fSgjelinek 		if ((err = zonecfg_devperms_apply(handle,
4377ee519a1fSgjelinek 		    devtab.zone_devperm_name, devtab.zone_devperm_uid,
4378ee519a1fSgjelinek 		    devtab.zone_devperm_gid, devtab.zone_devperm_mode,
4379ee519a1fSgjelinek 		    devtab.zone_devperm_acl)) != Z_OK && err != Z_INVAL)
4380ee519a1fSgjelinek 			(void) fprintf(stderr, gettext("error updating device "
4381ee519a1fSgjelinek 			    "%s: %s\n"), devtab.zone_devperm_name,
4382ee519a1fSgjelinek 			    zonecfg_strerror(err));
4383ee519a1fSgjelinek 
4384ee519a1fSgjelinek 		free(devtab.zone_devperm_acl);
4385ee519a1fSgjelinek 	}
4386ee519a1fSgjelinek 
4387ee519a1fSgjelinek 	(void) zonecfg_enddevperment(handle);
4388ee519a1fSgjelinek 
4389ee519a1fSgjelinek 	return (Z_OK);
4390ee519a1fSgjelinek }
4391ee519a1fSgjelinek 
43928cd327d5Sgjelinek /*
43938cd327d5Sgjelinek  * Validate attaching a zone but don't actually do the work.  The zone
43948cd327d5Sgjelinek  * does not have to exist, so there is some complexity getting a new zone
43958cd327d5Sgjelinek  * configuration set up so that we can perform the validation.  This is
43968cd327d5Sgjelinek  * handled within zonecfg_attach_manifest() which returns two handles; one
43978cd327d5Sgjelinek  * for the the full configuration to validate (rem_handle) and the other
43988cd327d5Sgjelinek  * (local_handle) containing only the zone configuration derived from the
43998cd327d5Sgjelinek  * manifest.
44008cd327d5Sgjelinek  */
44018cd327d5Sgjelinek static int
44028cd327d5Sgjelinek dryrun_attach(char *manifest_path)
44038cd327d5Sgjelinek {
44048cd327d5Sgjelinek 	int fd;
44058cd327d5Sgjelinek 	int err;
44068cd327d5Sgjelinek 	int res;
44078cd327d5Sgjelinek 	zone_dochandle_t local_handle;
44088cd327d5Sgjelinek 	zone_dochandle_t rem_handle = NULL;
44098cd327d5Sgjelinek 
44108cd327d5Sgjelinek 	if (strcmp(manifest_path, "-") == 0) {
44118cd327d5Sgjelinek 		fd = 0;
44128cd327d5Sgjelinek 	} else if ((fd = open(manifest_path, O_RDONLY)) < 0) {
44138cd327d5Sgjelinek 		zperror(gettext("could not open manifest path"), B_FALSE);
44148cd327d5Sgjelinek 		return (Z_ERR);
44158cd327d5Sgjelinek 	}
44168cd327d5Sgjelinek 
44178cd327d5Sgjelinek 	if ((local_handle = zonecfg_init_handle()) == NULL) {
44188cd327d5Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
44198cd327d5Sgjelinek 		res = Z_ERR;
44208cd327d5Sgjelinek 		goto done;
44218cd327d5Sgjelinek 	}
44228cd327d5Sgjelinek 
44238cd327d5Sgjelinek 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
44248cd327d5Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
44258cd327d5Sgjelinek 		res = Z_ERR;
44268cd327d5Sgjelinek 		goto done;
44278cd327d5Sgjelinek 	}
44288cd327d5Sgjelinek 
44298cd327d5Sgjelinek 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
44308cd327d5Sgjelinek 	    != Z_OK) {
44318cd327d5Sgjelinek 		if (err == Z_INVALID_DOCUMENT)
44328cd327d5Sgjelinek 			zerror(gettext("Cannot attach to an earlier release "
44338cd327d5Sgjelinek 			    "of the operating system"));
44348cd327d5Sgjelinek 		else
44358cd327d5Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
44368cd327d5Sgjelinek 		res = Z_ERR;
44378cd327d5Sgjelinek 		goto done;
44388cd327d5Sgjelinek 	}
44398cd327d5Sgjelinek 
44408cd327d5Sgjelinek 	res = verify_handle(CMD_ATTACH, local_handle);
44418cd327d5Sgjelinek 
44428cd327d5Sgjelinek 	/* Get the detach information for the locally defined zone. */
44438cd327d5Sgjelinek 	if ((err = zonecfg_get_detach_info(local_handle, B_FALSE)) != Z_OK) {
44448cd327d5Sgjelinek 		errno = err;
44458cd327d5Sgjelinek 		zperror(gettext("getting the attach information failed"),
44468cd327d5Sgjelinek 		    B_TRUE);
44478cd327d5Sgjelinek 		res = Z_ERR;
44488cd327d5Sgjelinek 	} else {
44498cd327d5Sgjelinek 		/* sw_cmp prints error msgs as necessary */
44508cd327d5Sgjelinek 		if (sw_cmp(local_handle, rem_handle, SW_CMP_NONE) != Z_OK)
44518cd327d5Sgjelinek 			res = Z_ERR;
44528cd327d5Sgjelinek 	}
44538cd327d5Sgjelinek 
44548cd327d5Sgjelinek done:
44558cd327d5Sgjelinek 	if (strcmp(manifest_path, "-") != 0)
44568cd327d5Sgjelinek 		(void) close(fd);
44578cd327d5Sgjelinek 
44588cd327d5Sgjelinek 	zonecfg_fini_handle(local_handle);
44598cd327d5Sgjelinek 	zonecfg_fini_handle(rem_handle);
44608cd327d5Sgjelinek 
44618cd327d5Sgjelinek 	return ((res == Z_OK) ? Z_OK : Z_ERR);
44628cd327d5Sgjelinek }
44638cd327d5Sgjelinek 
4464ee519a1fSgjelinek static int
4465ee519a1fSgjelinek attach_func(int argc, char *argv[])
4466ee519a1fSgjelinek {
4467ee519a1fSgjelinek 	int lockfd;
4468ee519a1fSgjelinek 	int err, arg;
4469ee519a1fSgjelinek 	boolean_t force = B_FALSE;
4470ee519a1fSgjelinek 	zone_dochandle_t handle;
4471ee519a1fSgjelinek 	zone_dochandle_t athandle = NULL;
4472ee519a1fSgjelinek 	char zonepath[MAXPATHLEN];
44739acbbeafSnn35248 	char brand[MAXNAMELEN], atbrand[MAXNAMELEN];
44748cd327d5Sgjelinek 	boolean_t execute = B_TRUE;
44758cd327d5Sgjelinek 	char *manifest_path;
4476ee519a1fSgjelinek 
4477ee519a1fSgjelinek 	if (zonecfg_in_alt_root()) {
4478ee519a1fSgjelinek 		zerror(gettext("cannot attach zone in alternate root"));
4479ee519a1fSgjelinek 		return (Z_ERR);
4480ee519a1fSgjelinek 	}
4481ee519a1fSgjelinek 
4482ee519a1fSgjelinek 	optind = 0;
44838cd327d5Sgjelinek 	if ((arg = getopt(argc, argv, "?Fn:")) != EOF) {
4484ee519a1fSgjelinek 		switch (arg) {
4485ee519a1fSgjelinek 		case '?':
4486ee519a1fSgjelinek 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
4487ee519a1fSgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
4488ee519a1fSgjelinek 		case 'F':
4489ee519a1fSgjelinek 			force = B_TRUE;
4490ee519a1fSgjelinek 			break;
44918cd327d5Sgjelinek 		case 'n':
44928cd327d5Sgjelinek 			execute = B_FALSE;
44938cd327d5Sgjelinek 			manifest_path = optarg;
44948cd327d5Sgjelinek 			break;
4495ee519a1fSgjelinek 		default:
4496ee519a1fSgjelinek 			sub_usage(SHELP_ATTACH, CMD_ATTACH);
4497ee519a1fSgjelinek 			return (Z_USAGE);
4498ee519a1fSgjelinek 		}
4499ee519a1fSgjelinek 	}
45008cd327d5Sgjelinek 
45018cd327d5Sgjelinek 	/*
45028cd327d5Sgjelinek 	 * If the no-execute option was specified, we need to branch down
45038cd327d5Sgjelinek 	 * a completely different path since there is no zone required to be
45048cd327d5Sgjelinek 	 * configured for this option.
45058cd327d5Sgjelinek 	 */
45068cd327d5Sgjelinek 	if (!execute)
45078cd327d5Sgjelinek 		return (dryrun_attach(manifest_path));
45088cd327d5Sgjelinek 
45099acbbeafSnn35248 	if (sanity_check(target_zone, CMD_ATTACH, B_FALSE, B_TRUE, B_FALSE)
45109acbbeafSnn35248 	    != Z_OK)
4511ee519a1fSgjelinek 		return (Z_ERR);
4512ee519a1fSgjelinek 	if (verify_details(CMD_ATTACH) != Z_OK)
4513ee519a1fSgjelinek 		return (Z_ERR);
4514ee519a1fSgjelinek 
4515ee519a1fSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4516ee519a1fSgjelinek 	    != Z_OK) {
4517ee519a1fSgjelinek 		errno = err;
4518ee519a1fSgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
4519ee519a1fSgjelinek 		return (Z_ERR);
4520ee519a1fSgjelinek 	}
4521ee519a1fSgjelinek 
4522ee519a1fSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
4523ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4524ee519a1fSgjelinek 		return (Z_ERR);
4525ee519a1fSgjelinek 	}
4526ee519a1fSgjelinek 
4527ee519a1fSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4528ee519a1fSgjelinek 		errno = err;
4529ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4530ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
4531ee519a1fSgjelinek 		return (Z_ERR);
4532ee519a1fSgjelinek 	}
4533ee519a1fSgjelinek 
4534ee519a1fSgjelinek 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
4535ee519a1fSgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4536ee519a1fSgjelinek 		    "zoneadm");
4537ee519a1fSgjelinek 		zonecfg_fini_handle(handle);
4538ee519a1fSgjelinek 		return (Z_ERR);
4539ee519a1fSgjelinek 	}
4540ee519a1fSgjelinek 
4541ee519a1fSgjelinek 	if (force)
4542ee519a1fSgjelinek 		goto forced;
4543ee519a1fSgjelinek 
4544ee519a1fSgjelinek 	if ((athandle = zonecfg_init_handle()) == NULL) {
4545ee519a1fSgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4546ee519a1fSgjelinek 		goto done;
4547ee519a1fSgjelinek 	}
4548ee519a1fSgjelinek 
4549ee519a1fSgjelinek 	if ((err = zonecfg_get_attach_handle(zonepath, target_zone, B_TRUE,
4550ee519a1fSgjelinek 	    athandle)) != Z_OK) {
4551ee519a1fSgjelinek 		if (err == Z_NO_ZONE)
4552ee519a1fSgjelinek 			zerror(gettext("Not a detached zone"));
4553ee519a1fSgjelinek 		else if (err == Z_INVALID_DOCUMENT)
4554ee519a1fSgjelinek 			zerror(gettext("Cannot attach to an earlier release "
4555ee519a1fSgjelinek 			    "of the operating system"));
4556ee519a1fSgjelinek 		else
4557ee519a1fSgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4558ee519a1fSgjelinek 		goto done;
4559ee519a1fSgjelinek 	}
4560ee519a1fSgjelinek 
4561ee519a1fSgjelinek 	/* Get the detach information for the locally defined zone. */
4562ee519a1fSgjelinek 	if ((err = zonecfg_get_detach_info(handle, B_FALSE)) != Z_OK) {
4563ee519a1fSgjelinek 		errno = err;
4564ee519a1fSgjelinek 		zperror(gettext("getting the attach information failed"),
4565ee519a1fSgjelinek 		    B_TRUE);
4566ee519a1fSgjelinek 		goto done;
4567ee519a1fSgjelinek 	}
4568ee519a1fSgjelinek 
45699acbbeafSnn35248 	/*
45709acbbeafSnn35248 	 * Ensure that the detached and locally defined zones are both of
45719acbbeafSnn35248 	 * the same brand.
45729acbbeafSnn35248 	 */
45739acbbeafSnn35248 	if ((zonecfg_get_brand(handle, brand, sizeof (brand)) != 0) ||
45749acbbeafSnn35248 	    (zonecfg_get_brand(athandle, atbrand, sizeof (atbrand)) != 0)) {
45759acbbeafSnn35248 		err = Z_ERR;
45769acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
45779acbbeafSnn35248 		goto done;
45789acbbeafSnn35248 	}
45799acbbeafSnn35248 
45809acbbeafSnn35248 	if (strcmp(atbrand, brand) != NULL) {
45819acbbeafSnn35248 		err = Z_ERR;
45829acbbeafSnn35248 		zerror(gettext("Trying to attach a '%s' zone to a '%s' "
45839acbbeafSnn35248 		    "configuration."), atbrand, brand);
45849acbbeafSnn35248 		goto done;
45859acbbeafSnn35248 	}
45869acbbeafSnn35248 
4587ee519a1fSgjelinek 	/* sw_cmp prints error msgs as necessary */
45880b5de56dSgjelinek 	if ((err = sw_cmp(handle, athandle, SW_CMP_NONE)) != Z_OK)
4589ee519a1fSgjelinek 		goto done;
4590ee519a1fSgjelinek 
4591ee519a1fSgjelinek 	if ((err = dev_fix(athandle)) != Z_OK)
4592ee519a1fSgjelinek 		goto done;
4593ee519a1fSgjelinek 
4594ee519a1fSgjelinek forced:
4595ee519a1fSgjelinek 
4596ee519a1fSgjelinek 	zonecfg_rm_detached(handle, force);
4597ee519a1fSgjelinek 
4598ee519a1fSgjelinek 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4599ee519a1fSgjelinek 		errno = err;
4600ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4601ee519a1fSgjelinek 	}
4602ee519a1fSgjelinek 
4603ee519a1fSgjelinek done:
4604ee519a1fSgjelinek 	zonecfg_fini_handle(handle);
4605ee519a1fSgjelinek 	release_lock_file(lockfd);
4606ee519a1fSgjelinek 	if (athandle != NULL)
4607ee519a1fSgjelinek 		zonecfg_fini_handle(athandle);
4608ee519a1fSgjelinek 
4609ee519a1fSgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4610ee519a1fSgjelinek }
4611ee519a1fSgjelinek 
4612865e09a4Sgjelinek /*
46137c478bd9Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
46147c478bd9Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
46157c478bd9Sstevel@tonic-gate  */
46167c478bd9Sstevel@tonic-gate 
46177c478bd9Sstevel@tonic-gate static int
46187c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
46197c478bd9Sstevel@tonic-gate {
46207c478bd9Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
46217c478bd9Sstevel@tonic-gate 
46227c478bd9Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
46237c478bd9Sstevel@tonic-gate 		return (-1);
46247c478bd9Sstevel@tonic-gate 	for (;;) {
46257c478bd9Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
46267c478bd9Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
46277c478bd9Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
46287c478bd9Sstevel@tonic-gate 		    line[0] == '\n')
46297c478bd9Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
46307c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
46317c478bd9Sstevel@tonic-gate 			return (1);
46327c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
46337c478bd9Sstevel@tonic-gate 			return (0);
46347c478bd9Sstevel@tonic-gate 	}
46357c478bd9Sstevel@tonic-gate }
46367c478bd9Sstevel@tonic-gate 
46377c478bd9Sstevel@tonic-gate static int
46387c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
46397c478bd9Sstevel@tonic-gate {
46407c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
46410b5de56dSgjelinek 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
46427c478bd9Sstevel@tonic-gate 	boolean_t force = B_FALSE;
46437c478bd9Sstevel@tonic-gate 	int lockfd, answer;
46447c478bd9Sstevel@tonic-gate 	int err, arg;
46457c478bd9Sstevel@tonic-gate 
4646108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4647108322fbScarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
4648108322fbScarlsonj 		return (Z_ERR);
4649108322fbScarlsonj 	}
4650108322fbScarlsonj 
46517c478bd9Sstevel@tonic-gate 	optind = 0;
46527c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
46537c478bd9Sstevel@tonic-gate 		switch (arg) {
46547c478bd9Sstevel@tonic-gate 		case '?':
46557c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
46567c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
46577c478bd9Sstevel@tonic-gate 		case 'F':
46587c478bd9Sstevel@tonic-gate 			force = B_TRUE;
46597c478bd9Sstevel@tonic-gate 			break;
46607c478bd9Sstevel@tonic-gate 		default:
46617c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
46627c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
46637c478bd9Sstevel@tonic-gate 		}
46647c478bd9Sstevel@tonic-gate 	}
46657c478bd9Sstevel@tonic-gate 	if (argc > optind) {
46667c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
46677c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
46687c478bd9Sstevel@tonic-gate 	}
46697c478bd9Sstevel@tonic-gate 
46709acbbeafSnn35248 	if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE, B_FALSE)
46719acbbeafSnn35248 	    != Z_OK)
46727c478bd9Sstevel@tonic-gate 		return (Z_ERR);
46737c478bd9Sstevel@tonic-gate 
46747c478bd9Sstevel@tonic-gate 	if (!force) {
46757c478bd9Sstevel@tonic-gate 		(void) snprintf(line, sizeof (line),
46767c478bd9Sstevel@tonic-gate 		    gettext("Are you sure you want to %s zone %s"),
46777c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_UNINSTALL), target_zone);
46787c478bd9Sstevel@tonic-gate 		if ((answer = ask_yesno(B_FALSE, line)) == 0) {
46797c478bd9Sstevel@tonic-gate 			return (Z_OK);
46807c478bd9Sstevel@tonic-gate 		} else if (answer == -1) {
46817c478bd9Sstevel@tonic-gate 			zerror(gettext("Input not from terminal and -F "
46827c478bd9Sstevel@tonic-gate 			    "not specified: %s not done."),
46837c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL));
46847c478bd9Sstevel@tonic-gate 			return (Z_ERR);
46857c478bd9Sstevel@tonic-gate 		}
46867c478bd9Sstevel@tonic-gate 	}
46877c478bd9Sstevel@tonic-gate 
46880b5de56dSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath,
46890b5de56dSgjelinek 	    sizeof (zonepath))) != Z_OK) {
46907c478bd9Sstevel@tonic-gate 		errno = err;
46917c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
46927c478bd9Sstevel@tonic-gate 		return (Z_ERR);
46937c478bd9Sstevel@tonic-gate 	}
46947c478bd9Sstevel@tonic-gate 	if ((err = zone_get_rootpath(target_zone, rootpath,
46957c478bd9Sstevel@tonic-gate 	    sizeof (rootpath))) != Z_OK) {
46967c478bd9Sstevel@tonic-gate 		errno = err;
46977c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get root path"));
46987c478bd9Sstevel@tonic-gate 		return (Z_ERR);
46997c478bd9Sstevel@tonic-gate 	}
47007c478bd9Sstevel@tonic-gate 
47017c478bd9Sstevel@tonic-gate 	/*
47027c478bd9Sstevel@tonic-gate 	 * If there seems to be a zoneadmd running for this zone, call it
47037c478bd9Sstevel@tonic-gate 	 * to tell it that an uninstall is happening; if all goes well it
47047c478bd9Sstevel@tonic-gate 	 * will then shut itself down.
47057c478bd9Sstevel@tonic-gate 	 */
47067c478bd9Sstevel@tonic-gate 	if (ping_zoneadmd(target_zone) == Z_OK) {
47077c478bd9Sstevel@tonic-gate 		zone_cmd_arg_t zarg;
47087c478bd9Sstevel@tonic-gate 		zarg.cmd = Z_NOTE_UNINSTALLING;
47097c478bd9Sstevel@tonic-gate 		/* we don't care too much if this fails... just plow on */
47107c478bd9Sstevel@tonic-gate 		(void) call_zoneadmd(target_zone, &zarg);
47117c478bd9Sstevel@tonic-gate 	}
47127c478bd9Sstevel@tonic-gate 
47137c478bd9Sstevel@tonic-gate 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
47147c478bd9Sstevel@tonic-gate 		zerror(gettext("another %s may have an operation in progress."),
4715865e09a4Sgjelinek 		    "zoneadm");
47167c478bd9Sstevel@tonic-gate 		return (Z_ERR);
47177c478bd9Sstevel@tonic-gate 	}
47187c478bd9Sstevel@tonic-gate 
47197c478bd9Sstevel@tonic-gate 	/* Don't uninstall the zone if anything is mounted there */
47207c478bd9Sstevel@tonic-gate 	err = zonecfg_find_mounts(rootpath, NULL, NULL);
47217c478bd9Sstevel@tonic-gate 	if (err) {
47220b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
47237c478bd9Sstevel@tonic-gate 		    "subdirectories of %s.\n"), rootpath);
47247c478bd9Sstevel@tonic-gate 		(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
47257c478bd9Sstevel@tonic-gate 		return (Z_ERR);
47267c478bd9Sstevel@tonic-gate 	}
47277c478bd9Sstevel@tonic-gate 
47287c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
47297c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
47307c478bd9Sstevel@tonic-gate 		errno = err;
47317c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
47327c478bd9Sstevel@tonic-gate 		goto bad;
47337c478bd9Sstevel@tonic-gate 	}
47347c478bd9Sstevel@tonic-gate 
47350b5de56dSgjelinek 	if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
47360b5de56dSgjelinek 		errno = err;
47370b5de56dSgjelinek 		zperror2(target_zone, gettext("cleaning up zonepath failed"));
47387c478bd9Sstevel@tonic-gate 		goto bad;
47390b5de56dSgjelinek 	}
47400b5de56dSgjelinek 
47417c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
47427c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
47437c478bd9Sstevel@tonic-gate 		errno = err;
47447c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
47457c478bd9Sstevel@tonic-gate 	}
47467c478bd9Sstevel@tonic-gate bad:
47477c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
47487c478bd9Sstevel@tonic-gate 	return (err);
47497c478bd9Sstevel@tonic-gate }
47507c478bd9Sstevel@tonic-gate 
4751108322fbScarlsonj /* ARGSUSED */
4752108322fbScarlsonj static int
4753108322fbScarlsonj mount_func(int argc, char *argv[])
4754108322fbScarlsonj {
4755108322fbScarlsonj 	zone_cmd_arg_t zarg;
47569acbbeafSnn35248 	boolean_t force = B_FALSE;
47579acbbeafSnn35248 	int arg;
4758108322fbScarlsonj 
47599acbbeafSnn35248 	/*
47609acbbeafSnn35248 	 * The only supported subargument to the "mount" subcommand is
47619acbbeafSnn35248 	 * "-f", which forces us to mount a zone in the INCOMPLETE state.
47629acbbeafSnn35248 	 */
47639acbbeafSnn35248 	optind = 0;
47649acbbeafSnn35248 	if ((arg = getopt(argc, argv, "f")) != EOF) {
47659acbbeafSnn35248 		switch (arg) {
47669acbbeafSnn35248 		case 'f':
47679acbbeafSnn35248 			force = B_TRUE;
47689acbbeafSnn35248 			break;
47699acbbeafSnn35248 		default:
4770108322fbScarlsonj 			return (Z_USAGE);
47719acbbeafSnn35248 		}
47729acbbeafSnn35248 	}
47739acbbeafSnn35248 	if (argc > optind)
47749acbbeafSnn35248 		return (Z_USAGE);
47759acbbeafSnn35248 
47769acbbeafSnn35248 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
47779acbbeafSnn35248 	    != Z_OK)
4778108322fbScarlsonj 		return (Z_ERR);
4779108322fbScarlsonj 	if (verify_details(CMD_MOUNT) != Z_OK)
4780108322fbScarlsonj 		return (Z_ERR);
4781108322fbScarlsonj 
47829acbbeafSnn35248 	zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
4783108322fbScarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4784108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
4785108322fbScarlsonj 		return (Z_ERR);
4786108322fbScarlsonj 	}
4787108322fbScarlsonj 	return (Z_OK);
4788108322fbScarlsonj }
4789108322fbScarlsonj 
4790108322fbScarlsonj /* ARGSUSED */
4791108322fbScarlsonj static int
4792108322fbScarlsonj unmount_func(int argc, char *argv[])
4793108322fbScarlsonj {
4794108322fbScarlsonj 	zone_cmd_arg_t zarg;
4795108322fbScarlsonj 
4796108322fbScarlsonj 	if (argc > 0)
4797108322fbScarlsonj 		return (Z_USAGE);
47989acbbeafSnn35248 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
47999acbbeafSnn35248 	    != Z_OK)
4800108322fbScarlsonj 		return (Z_ERR);
4801108322fbScarlsonj 
4802108322fbScarlsonj 	zarg.cmd = Z_UNMOUNT;
4803108322fbScarlsonj 	if (call_zoneadmd(target_zone, &zarg) != 0) {
4804108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
4805108322fbScarlsonj 		return (Z_ERR);
4806108322fbScarlsonj 	}
4807108322fbScarlsonj 	return (Z_OK);
4808108322fbScarlsonj }
4809108322fbScarlsonj 
48107c478bd9Sstevel@tonic-gate static int
4811555afedfScarlsonj mark_func(int argc, char *argv[])
4812555afedfScarlsonj {
4813555afedfScarlsonj 	int err, lockfd;
4814555afedfScarlsonj 
4815555afedfScarlsonj 	if (argc != 1 || strcmp(argv[0], "incomplete") != 0)
4816555afedfScarlsonj 		return (Z_USAGE);
48179acbbeafSnn35248 	if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_FALSE, B_FALSE)
48189acbbeafSnn35248 	    != Z_OK)
4819555afedfScarlsonj 		return (Z_ERR);
4820555afedfScarlsonj 
4821555afedfScarlsonj 	if (grab_lock_file(target_zone, &lockfd) != Z_OK) {
4822555afedfScarlsonj 		zerror(gettext("another %s may have an operation in progress."),
4823555afedfScarlsonj 		    "zoneadm");
4824555afedfScarlsonj 		return (Z_ERR);
4825555afedfScarlsonj 	}
4826555afedfScarlsonj 
4827555afedfScarlsonj 	err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
4828555afedfScarlsonj 	if (err != Z_OK) {
4829555afedfScarlsonj 		errno = err;
4830555afedfScarlsonj 		zperror2(target_zone, gettext("could not set state"));
4831555afedfScarlsonj 	}
4832555afedfScarlsonj 	release_lock_file(lockfd);
4833555afedfScarlsonj 
4834555afedfScarlsonj 	return (err);
4835555afedfScarlsonj }
4836555afedfScarlsonj 
4837555afedfScarlsonj static int
48387c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[])
48397c478bd9Sstevel@tonic-gate {
48407c478bd9Sstevel@tonic-gate 	int arg, cmd_num;
48417c478bd9Sstevel@tonic-gate 
48427c478bd9Sstevel@tonic-gate 	if (argc == 0) {
48437c478bd9Sstevel@tonic-gate 		(void) usage(B_TRUE);
48447c478bd9Sstevel@tonic-gate 		return (Z_OK);
48457c478bd9Sstevel@tonic-gate 	}
48467c478bd9Sstevel@tonic-gate 	optind = 0;
48477c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
48487c478bd9Sstevel@tonic-gate 		switch (arg) {
48497c478bd9Sstevel@tonic-gate 		case '?':
48507c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
48517c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
48527c478bd9Sstevel@tonic-gate 		default:
48537c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
48547c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
48557c478bd9Sstevel@tonic-gate 		}
48567c478bd9Sstevel@tonic-gate 	}
48577c478bd9Sstevel@tonic-gate 	while (optind < argc) {
4858394a25e2Scarlsonj 		/* Private commands have NULL short_usage; omit them */
4859394a25e2Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
4860394a25e2Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
48617c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
48627c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
48637c478bd9Sstevel@tonic-gate 		}
48647c478bd9Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
48657c478bd9Sstevel@tonic-gate 		optind++;
48667c478bd9Sstevel@tonic-gate 	}
48677c478bd9Sstevel@tonic-gate 	return (Z_OK);
48687c478bd9Sstevel@tonic-gate }
48697c478bd9Sstevel@tonic-gate 
48707c478bd9Sstevel@tonic-gate /*
48717c478bd9Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
48727c478bd9Sstevel@tonic-gate  */
48737c478bd9Sstevel@tonic-gate 
48747c478bd9Sstevel@tonic-gate static int
48757c478bd9Sstevel@tonic-gate cmd_match(char *cmd)
48767c478bd9Sstevel@tonic-gate {
48777c478bd9Sstevel@tonic-gate 	int i;
48787c478bd9Sstevel@tonic-gate 
48797c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
48807c478bd9Sstevel@tonic-gate 		/* return only if there is an exact match */
48817c478bd9Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
48827c478bd9Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
48837c478bd9Sstevel@tonic-gate 	}
48847c478bd9Sstevel@tonic-gate 	return (-1);
48857c478bd9Sstevel@tonic-gate }
48867c478bd9Sstevel@tonic-gate 
48877c478bd9Sstevel@tonic-gate static int
48887c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
48897c478bd9Sstevel@tonic-gate {
48907c478bd9Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
48917c478bd9Sstevel@tonic-gate 
48927c478bd9Sstevel@tonic-gate 	if (i < 0)
48937c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
48947c478bd9Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
48957c478bd9Sstevel@tonic-gate }
48967c478bd9Sstevel@tonic-gate 
48977c478bd9Sstevel@tonic-gate static char *
48987c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
48997c478bd9Sstevel@tonic-gate {
49007c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
49017c478bd9Sstevel@tonic-gate 
49027c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
49037c478bd9Sstevel@tonic-gate 	for (;;) {
49047c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
49057c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
49067c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
49077c478bd9Sstevel@tonic-gate 			break;
49087c478bd9Sstevel@tonic-gate 		} else {
49097c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
49107c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
49117c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
49127c478bd9Sstevel@tonic-gate 				continue;
49137c478bd9Sstevel@tonic-gate 			}
49147c478bd9Sstevel@tonic-gate 			break;
49157c478bd9Sstevel@tonic-gate 		}
49167c478bd9Sstevel@tonic-gate 	}
49177c478bd9Sstevel@tonic-gate 	return (execbasename);
49187c478bd9Sstevel@tonic-gate }
49197c478bd9Sstevel@tonic-gate 
49207c478bd9Sstevel@tonic-gate int
49217c478bd9Sstevel@tonic-gate main(int argc, char **argv)
49227c478bd9Sstevel@tonic-gate {
49237c478bd9Sstevel@tonic-gate 	int arg;
49247c478bd9Sstevel@tonic-gate 	zoneid_t zid;
4925108322fbScarlsonj 	struct stat st;
49269acbbeafSnn35248 	char *zone_lock_env;
49279acbbeafSnn35248 	int err;
49287c478bd9Sstevel@tonic-gate 
49297c478bd9Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
49307c478bd9Sstevel@tonic-gate 		locale = "C";
49317c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
49327c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
49337c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
49347c478bd9Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
49357c478bd9Sstevel@tonic-gate 	target_zone = NULL;
49367c478bd9Sstevel@tonic-gate 	if (chdir("/") != 0) {
49377c478bd9Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
49387c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
49397c478bd9Sstevel@tonic-gate 	}
49407c478bd9Sstevel@tonic-gate 
494199653d4eSeschrock 	if (init_zfs() != Z_OK)
494299653d4eSeschrock 		exit(Z_ERR);
494399653d4eSeschrock 
4944555afedfScarlsonj 	while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
49457c478bd9Sstevel@tonic-gate 		switch (arg) {
49467c478bd9Sstevel@tonic-gate 		case '?':
49477c478bd9Sstevel@tonic-gate 			return (usage(B_TRUE));
4948555afedfScarlsonj 		case 'u':
4949555afedfScarlsonj 			target_uuid = optarg;
4950555afedfScarlsonj 			break;
49517c478bd9Sstevel@tonic-gate 		case 'z':
49527c478bd9Sstevel@tonic-gate 			target_zone = optarg;
49537c478bd9Sstevel@tonic-gate 			break;
4954108322fbScarlsonj 		case 'R':	/* private option for admin/install use */
4955108322fbScarlsonj 			if (*optarg != '/') {
4956108322fbScarlsonj 				zerror(gettext("root path must be absolute."));
4957108322fbScarlsonj 				exit(Z_ERR);
4958108322fbScarlsonj 			}
4959108322fbScarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
4960108322fbScarlsonj 				zerror(
4961108322fbScarlsonj 				    gettext("root path must be a directory."));
4962108322fbScarlsonj 				exit(Z_ERR);
4963108322fbScarlsonj 			}
4964108322fbScarlsonj 			zonecfg_set_root(optarg);
4965108322fbScarlsonj 			break;
49667c478bd9Sstevel@tonic-gate 		default:
49677c478bd9Sstevel@tonic-gate 			return (usage(B_FALSE));
49687c478bd9Sstevel@tonic-gate 		}
49697c478bd9Sstevel@tonic-gate 	}
49707c478bd9Sstevel@tonic-gate 
49717c478bd9Sstevel@tonic-gate 	if (optind >= argc)
49727c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
4973555afedfScarlsonj 
4974555afedfScarlsonj 	if (target_uuid != NULL && *target_uuid != '\0') {
4975555afedfScarlsonj 		uuid_t uuid;
4976555afedfScarlsonj 		static char newtarget[ZONENAME_MAX];
4977555afedfScarlsonj 
4978555afedfScarlsonj 		if (uuid_parse(target_uuid, uuid) == -1) {
4979555afedfScarlsonj 			zerror(gettext("illegal UUID value specified"));
4980555afedfScarlsonj 			exit(Z_ERR);
4981555afedfScarlsonj 		}
4982555afedfScarlsonj 		if (zonecfg_get_name_by_uuid(uuid, newtarget,
4983555afedfScarlsonj 		    sizeof (newtarget)) == Z_OK)
4984555afedfScarlsonj 			target_zone = newtarget;
4985555afedfScarlsonj 	}
4986555afedfScarlsonj 
49877c478bd9Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
49887c478bd9Sstevel@tonic-gate 		errno = Z_NO_ZONE;
49897c478bd9Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
49907c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
49917c478bd9Sstevel@tonic-gate 	}
49929acbbeafSnn35248 
49939acbbeafSnn35248 	/*
49949acbbeafSnn35248 	 * See if we have inherited the right to manipulate this zone from
49959acbbeafSnn35248 	 * a zoneadm instance in our ancestry.  If so, set zone_lock_cnt to
49969acbbeafSnn35248 	 * indicate it.  If not, make that explicit in our environment.
49979acbbeafSnn35248 	 */
49989acbbeafSnn35248 	zone_lock_env = getenv(LOCK_ENV_VAR);
49999acbbeafSnn35248 	if (zone_lock_env == NULL) {
50009acbbeafSnn35248 		if (putenv(zoneadm_lock_not_held) != 0) {
50019acbbeafSnn35248 			zperror(target_zone, B_TRUE);
50029acbbeafSnn35248 			exit(Z_ERR);
50039acbbeafSnn35248 		}
50049acbbeafSnn35248 	} else {
50059acbbeafSnn35248 		zoneadm_is_nested = B_TRUE;
50069acbbeafSnn35248 		if (atoi(zone_lock_env) == 1)
50079acbbeafSnn35248 			zone_lock_cnt = 1;
50089acbbeafSnn35248 	}
50099acbbeafSnn35248 
50109acbbeafSnn35248 	/*
50119acbbeafSnn35248 	 * If we are going to be operating on a single zone, retrieve its
50129acbbeafSnn35248 	 * brand type and determine whether it is native or not.
50139acbbeafSnn35248 	 */
50149acbbeafSnn35248 	if ((target_zone != NULL) &&
50159acbbeafSnn35248 	    (strcmp(target_zone, GLOBAL_ZONENAME) != NULL)) {
50169acbbeafSnn35248 		if (zone_get_brand(target_zone, target_brand,
50179acbbeafSnn35248 		    sizeof (target_brand)) != Z_OK) {
50189acbbeafSnn35248 			zerror(gettext("missing or invalid brand"));
50199acbbeafSnn35248 			exit(Z_ERR);
50209acbbeafSnn35248 		}
50219acbbeafSnn35248 		is_native_zone = (strcmp(target_brand, NATIVE_BRAND_NAME) == 0);
50229acbbeafSnn35248 	}
50239acbbeafSnn35248 
50249acbbeafSnn35248 	err = parse_and_run(argc - optind, &argv[optind]);
50259acbbeafSnn35248 
50269acbbeafSnn35248 	return (err);
50277c478bd9Sstevel@tonic-gate }
5028