xref: /titanic_53/usr/src/cmd/zoneadm/zoneadm.c (revision cb8a054b1ab30d5caa746e6c44f29d4c9d3071c1)
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 /*
23ad02e316Sbatschul  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * zoneadm is a command interpreter for zone administration.  It is all in
287c478bd9Sstevel@tonic-gate  * C (i.e., no lex/yacc), and all the argument passing is argc/argv based.
297c478bd9Sstevel@tonic-gate  * main() calls parse_and_run() which calls cmd_match(), then invokes the
307c478bd9Sstevel@tonic-gate  * appropriate command's handler function.  The rest of the program is the
317c478bd9Sstevel@tonic-gate  * handler functions and their helper functions.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * Some of the helper functions are used largely to simplify I18N: reducing
347c478bd9Sstevel@tonic-gate  * the need for translation notes.  This is particularly true of many of
357c478bd9Sstevel@tonic-gate  * the zerror() calls: doing e.g. zerror(gettext("%s failed"), "foo") rather
367c478bd9Sstevel@tonic-gate  * than zerror(gettext("foo failed")) with a translation note indicating
377c478bd9Sstevel@tonic-gate  * that "foo" need not be translated.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <signal.h>
447c478bd9Sstevel@tonic-gate #include <stdarg.h>
457c478bd9Sstevel@tonic-gate #include <ctype.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <string.h>
487c478bd9Sstevel@tonic-gate #include <wait.h>
497c478bd9Sstevel@tonic-gate #include <zone.h>
507c478bd9Sstevel@tonic-gate #include <priv.h>
517c478bd9Sstevel@tonic-gate #include <locale.h>
527c478bd9Sstevel@tonic-gate #include <libintl.h>
537c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
547c478bd9Sstevel@tonic-gate #include <bsm/adt.h>
559acbbeafSnn35248 #include <sys/brand.h>
567c478bd9Sstevel@tonic-gate #include <sys/param.h>
577c478bd9Sstevel@tonic-gate #include <sys/types.h>
587c478bd9Sstevel@tonic-gate #include <sys/stat.h>
597c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
607c478bd9Sstevel@tonic-gate #include <assert.h>
617c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
627c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
637c478bd9Sstevel@tonic-gate #include <limits.h>
640b5de56dSgjelinek #include <dirent.h>
65555afedfScarlsonj #include <uuid/uuid.h>
667c478bd9Sstevel@tonic-gate #include <fcntl.h>
677c478bd9Sstevel@tonic-gate #include <door.h>
687c478bd9Sstevel@tonic-gate #include <macros.h>
697c478bd9Sstevel@tonic-gate #include <libgen.h>
70865e09a4Sgjelinek #include <fnmatch.h>
71e7f3c547Sgjelinek #include <sys/modctl.h>
729acbbeafSnn35248 #include <libbrand.h>
730209230bSgjelinek #include <libscf.h>
747ef01d19Sgjelinek #include <procfs.h>
75d9e728a2Sgjelinek #include <strings.h>
767c478bd9Sstevel@tonic-gate #include <pool.h>
777c478bd9Sstevel@tonic-gate #include <sys/pool.h>
780209230bSgjelinek #include <sys/priocntl.h>
790209230bSgjelinek #include <sys/fsspriocntl.h>
802b24ab6bSSebastien Roy #include <libdladm.h>
812b24ab6bSSebastien Roy #include <libdllink.h>
82*cb8a054bSGlenn Faden #include <pwd.h>
83*cb8a054bSGlenn Faden #include <auth_list.h>
84*cb8a054bSGlenn Faden #include <auth_attr.h>
85*cb8a054bSGlenn Faden #include <secdb.h>
867c478bd9Sstevel@tonic-gate 
870b5de56dSgjelinek #include "zoneadm.h"
880b5de56dSgjelinek 
897c478bd9Sstevel@tonic-gate #define	MAXARGS	8
90*cb8a054bSGlenn Faden #define	SOURCE_ZONE (CMD_MAX + 1)
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /* Reflects kernel zone entries */
937c478bd9Sstevel@tonic-gate typedef struct zone_entry {
947c478bd9Sstevel@tonic-gate 	zoneid_t	zid;
957c478bd9Sstevel@tonic-gate 	char		zname[ZONENAME_MAX];
967c478bd9Sstevel@tonic-gate 	char		*zstate_str;
977c478bd9Sstevel@tonic-gate 	zone_state_t	zstate_num;
989acbbeafSnn35248 	char		zbrand[MAXNAMELEN];
997c478bd9Sstevel@tonic-gate 	char		zroot[MAXPATHLEN];
100555afedfScarlsonj 	char		zuuid[UUID_PRINTABLE_STRING_LENGTH];
101f4b3ec61Sdh155122 	zone_iptype_t	ziptype;
1027c478bd9Sstevel@tonic-gate } zone_entry_t;
1037c478bd9Sstevel@tonic-gate 
10484561e8cStd153743 #define	CLUSTER_BRAND_NAME	"cluster"
10584561e8cStd153743 
1067c478bd9Sstevel@tonic-gate static zone_entry_t *zents;
1077c478bd9Sstevel@tonic-gate static size_t nzents;
1087c478bd9Sstevel@tonic-gate 
1091390a385Sgjelinek #define	LOOPBACK_IF	"lo0"
1101390a385Sgjelinek #define	SOCKET_AF(af)	(((af) == AF_UNSPEC) ? AF_INET : (af))
1111390a385Sgjelinek 
1121390a385Sgjelinek struct net_if {
1131390a385Sgjelinek 	char	*name;
1141390a385Sgjelinek 	int	af;
1151390a385Sgjelinek };
1161390a385Sgjelinek 
1177c478bd9Sstevel@tonic-gate /* 0755 is the default directory mode. */
1187c478bd9Sstevel@tonic-gate #define	DEFAULT_DIR_MODE \
1197c478bd9Sstevel@tonic-gate 	(S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate struct cmd {
1227c478bd9Sstevel@tonic-gate 	uint_t	cmd_num;				/* command number */
1237c478bd9Sstevel@tonic-gate 	char	*cmd_name;				/* command name */
1247c478bd9Sstevel@tonic-gate 	char	*short_usage;				/* short form help */
1257c478bd9Sstevel@tonic-gate 	int	(*handler)(int argc, char *argv[]);	/* function to call */
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate };
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	SHELP_HELP	"help"
1303f2f09c1Sdp #define	SHELP_BOOT	"boot [-- boot_arguments]"
1317c478bd9Sstevel@tonic-gate #define	SHELP_HALT	"halt"
1327c478bd9Sstevel@tonic-gate #define	SHELP_READY	"ready"
1333f2f09c1Sdp #define	SHELP_REBOOT	"reboot [-- boot_arguments]"
1347c478bd9Sstevel@tonic-gate #define	SHELP_LIST	"list [-cipv]"
1357c478bd9Sstevel@tonic-gate #define	SHELP_VERIFY	"verify"
136ad02e316Sbatschul #define	SHELP_INSTALL	"install [brand-specific args]"
137ff17c8bfSgjelinek #define	SHELP_UNINSTALL	"uninstall [-F] [brand-specific args]"
138ff17c8bfSgjelinek #define	SHELP_CLONE	"clone [-m method] [-s <ZFS snapshot>] "\
139ff17c8bfSgjelinek 	"[brand-specific args] zonename"
140865e09a4Sgjelinek #define	SHELP_MOVE	"move zonepath"
141ff17c8bfSgjelinek #define	SHELP_DETACH	"detach [-n] [brand-specific args]"
142ff17c8bfSgjelinek #define	SHELP_ATTACH	"attach [-F] [-n <path>] [brand-specific args]"
143555afedfScarlsonj #define	SHELP_MARK	"mark incomplete"
1447c478bd9Sstevel@tonic-gate 
1459acbbeafSnn35248 #define	EXEC_PREFIX	"exec "
1469acbbeafSnn35248 #define	EXEC_LEN	(strlen(EXEC_PREFIX))
1479acbbeafSnn35248 #define	RMCOMMAND	"/usr/bin/rm -rf"
1489acbbeafSnn35248 
1499acbbeafSnn35248 static int cleanup_zonepath(char *, boolean_t);
1509acbbeafSnn35248 
151f4b3ec61Sdh155122 
1527c478bd9Sstevel@tonic-gate static int help_func(int argc, char *argv[]);
1537c478bd9Sstevel@tonic-gate static int ready_func(int argc, char *argv[]);
1547c478bd9Sstevel@tonic-gate static int boot_func(int argc, char *argv[]);
1557c478bd9Sstevel@tonic-gate static int halt_func(int argc, char *argv[]);
1567c478bd9Sstevel@tonic-gate static int reboot_func(int argc, char *argv[]);
1577c478bd9Sstevel@tonic-gate static int list_func(int argc, char *argv[]);
1587c478bd9Sstevel@tonic-gate static int verify_func(int argc, char *argv[]);
1597c478bd9Sstevel@tonic-gate static int install_func(int argc, char *argv[]);
1607c478bd9Sstevel@tonic-gate static int uninstall_func(int argc, char *argv[]);
161108322fbScarlsonj static int mount_func(int argc, char *argv[]);
162108322fbScarlsonj static int unmount_func(int argc, char *argv[]);
163865e09a4Sgjelinek static int clone_func(int argc, char *argv[]);
164865e09a4Sgjelinek static int move_func(int argc, char *argv[]);
165ee519a1fSgjelinek static int detach_func(int argc, char *argv[]);
166ee519a1fSgjelinek static int attach_func(int argc, char *argv[]);
167555afedfScarlsonj static int mark_func(int argc, char *argv[]);
1680209230bSgjelinek static int apply_func(int argc, char *argv[]);
169fbbfbc6eSjv227347 static int sysboot_func(int argc, char *argv[]);
1707c478bd9Sstevel@tonic-gate static int sanity_check(char *zone, int cmd_num, boolean_t running,
1719acbbeafSnn35248     boolean_t unsafe_when_running, boolean_t force);
1727c478bd9Sstevel@tonic-gate static int cmd_match(char *cmd);
173ce28b40eSzt129084 static int verify_details(int, char *argv[]);
174ce28b40eSzt129084 static int verify_brand(zone_dochandle_t, int, char *argv[]);
175ce28b40eSzt129084 static int invoke_brand_handler(int, char *argv[]);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static struct cmd cmdtab[] = {
1787c478bd9Sstevel@tonic-gate 	{ CMD_HELP,		"help",		SHELP_HELP,	help_func },
1797c478bd9Sstevel@tonic-gate 	{ CMD_BOOT,		"boot",		SHELP_BOOT,	boot_func },
1807c478bd9Sstevel@tonic-gate 	{ CMD_HALT,		"halt",		SHELP_HALT,	halt_func },
1817c478bd9Sstevel@tonic-gate 	{ CMD_READY,		"ready",	SHELP_READY,	ready_func },
1827c478bd9Sstevel@tonic-gate 	{ CMD_REBOOT,		"reboot",	SHELP_REBOOT,	reboot_func },
1837c478bd9Sstevel@tonic-gate 	{ CMD_LIST,		"list",		SHELP_LIST,	list_func },
1847c478bd9Sstevel@tonic-gate 	{ CMD_VERIFY,		"verify",	SHELP_VERIFY,	verify_func },
1857c478bd9Sstevel@tonic-gate 	{ CMD_INSTALL,		"install",	SHELP_INSTALL,	install_func },
1867c478bd9Sstevel@tonic-gate 	{ CMD_UNINSTALL,	"uninstall",	SHELP_UNINSTALL,
187108322fbScarlsonj 	    uninstall_func },
188865e09a4Sgjelinek 	/* mount and unmount are private commands for admin/install */
189108322fbScarlsonj 	{ CMD_MOUNT,		"mount",	NULL,		mount_func },
190865e09a4Sgjelinek 	{ CMD_UNMOUNT,		"unmount",	NULL,		unmount_func },
191865e09a4Sgjelinek 	{ CMD_CLONE,		"clone",	SHELP_CLONE,	clone_func },
192ee519a1fSgjelinek 	{ CMD_MOVE,		"move",		SHELP_MOVE,	move_func },
193ee519a1fSgjelinek 	{ CMD_DETACH,		"detach",	SHELP_DETACH,	detach_func },
194555afedfScarlsonj 	{ CMD_ATTACH,		"attach",	SHELP_ATTACH,	attach_func },
1950209230bSgjelinek 	{ CMD_MARK,		"mark",		SHELP_MARK,	mark_func },
196fbbfbc6eSjv227347 	{ CMD_APPLY,		"apply",	NULL,		apply_func },
197fbbfbc6eSjv227347 	{ CMD_SYSBOOT,		"sysboot",	NULL,		sysboot_func }
1987c478bd9Sstevel@tonic-gate };
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /* global variables */
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /* set early in main(), never modified thereafter, used all over the place */
2037c478bd9Sstevel@tonic-gate static char *execname;
2049acbbeafSnn35248 static char target_brand[MAXNAMELEN];
205e5816e35SEdward Pilatowicz static char default_brand[MAXPATHLEN];
2067c478bd9Sstevel@tonic-gate static char *locale;
2070b5de56dSgjelinek char *target_zone;
208555afedfScarlsonj static char *target_uuid;
209*cb8a054bSGlenn Faden char *username;
2107c478bd9Sstevel@tonic-gate 
2110b5de56dSgjelinek char *
2127c478bd9Sstevel@tonic-gate cmd_to_str(int cmd_num)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2157c478bd9Sstevel@tonic-gate 	return (cmdtab[cmd_num].cmd_name);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /* This is a separate function because of gettext() wrapping. */
2197c478bd9Sstevel@tonic-gate static char *
2207c478bd9Sstevel@tonic-gate long_help(int cmd_num)
2217c478bd9Sstevel@tonic-gate {
2227e362f58Scomay 	assert(cmd_num >= CMD_MIN && cmd_num <= CMD_MAX);
2237c478bd9Sstevel@tonic-gate 	switch (cmd_num) {
2247c478bd9Sstevel@tonic-gate 	case CMD_HELP:
2257c478bd9Sstevel@tonic-gate 		return (gettext("Print usage message."));
2267c478bd9Sstevel@tonic-gate 	case CMD_BOOT:
2273f2f09c1Sdp 		return (gettext("Activates (boots) specified zone.  See "
2283f2f09c1Sdp 		    "zoneadm(1m) for valid boot\n\targuments."));
2297c478bd9Sstevel@tonic-gate 	case CMD_HALT:
2309e518655Sgjelinek 		return (gettext("Halts specified zone, bypassing shutdown "
2319e518655Sgjelinek 		    "scripts and removing runtime\n\tresources of the zone."));
2327c478bd9Sstevel@tonic-gate 	case CMD_READY:
2339e518655Sgjelinek 		return (gettext("Prepares a zone for running applications but "
2349e518655Sgjelinek 		    "does not start any user\n\tprocesses in the zone."));
2357c478bd9Sstevel@tonic-gate 	case CMD_REBOOT:
2369e518655Sgjelinek 		return (gettext("Restarts the zone (equivalent to a halt / "
2373f2f09c1Sdp 		    "boot sequence).\n\tFails if the zone is not active.  "
2383f2f09c1Sdp 		    "See zoneadm(1m) for valid boot\n\targuments."));
2397c478bd9Sstevel@tonic-gate 	case CMD_LIST:
2407c478bd9Sstevel@tonic-gate 		return (gettext("Lists the current zones, or a "
2417c478bd9Sstevel@tonic-gate 		    "specific zone if indicated.  By default,\n\tall "
2427c478bd9Sstevel@tonic-gate 		    "running zones are listed, though this can be "
2437c478bd9Sstevel@tonic-gate 		    "expanded to all\n\tinstalled zones with the -i "
2447c478bd9Sstevel@tonic-gate 		    "option or all configured zones with the\n\t-c "
245555afedfScarlsonj 		    "option.  When used with the general -z <zone> and/or -u "
246555afedfScarlsonj 		    "<uuid-match>\n\toptions, lists only the specified "
247555afedfScarlsonj 		    "matching zone, but lists it\n\tregardless of its state, "
248555afedfScarlsonj 		    "and the -i and -c options are disallowed.  The\n\t-v "
249555afedfScarlsonj 		    "option can be used to display verbose information: zone "
250555afedfScarlsonj 		    "name, id,\n\tcurrent state, root directory and options.  "
251555afedfScarlsonj 		    "The -p option can be used\n\tto request machine-parsable "
252555afedfScarlsonj 		    "output.  The -v and -p options are mutually\n\texclusive."
253555afedfScarlsonj 		    "  If neither -v nor -p is used, just the zone name is "
254555afedfScarlsonj 		    "listed."));
2557c478bd9Sstevel@tonic-gate 	case CMD_VERIFY:
2567c478bd9Sstevel@tonic-gate 		return (gettext("Check to make sure the configuration "
2577c478bd9Sstevel@tonic-gate 		    "can safely be instantiated\n\ton the machine: "
2587c478bd9Sstevel@tonic-gate 		    "physical network interfaces exist, etc."));
2597c478bd9Sstevel@tonic-gate 	case CMD_INSTALL:
2600b5de56dSgjelinek 		return (gettext("Install the configuration on to the system.  "
261ad02e316Sbatschul 		    "All arguments are passed to the brand installation "
262ff17c8bfSgjelinek 		    "function;\n\tsee brands(5) for more information."));
2637c478bd9Sstevel@tonic-gate 	case CMD_UNINSTALL:
2649e518655Sgjelinek 		return (gettext("Uninstall the configuration from the system.  "
265ff17c8bfSgjelinek 		    "The -F flag can be used\n\tto force the action.  All "
266ff17c8bfSgjelinek 		    "other arguments are passed to the brand\n\tuninstall "
267ff17c8bfSgjelinek 		    "function; see brands(5) for more information."));
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 "
275ff17c8bfSgjelinek 		    "creating a new ZFS snapshot.  All other arguments are "
276ff17c8bfSgjelinek 		    "passed\n\tto the brand clone function; see "
277ff17c8bfSgjelinek 		    "brands(5) for more information."));
278865e09a4Sgjelinek 	case CMD_MOVE:
279865e09a4Sgjelinek 		return (gettext("Move the zone to a new zonepath."));
2809e518655Sgjelinek 	case CMD_DETACH:
2819e518655Sgjelinek 		return (gettext("Detach the zone from the system. The zone "
2829e518655Sgjelinek 		    "state is changed to\n\t'configured' (but the files under "
2839e518655Sgjelinek 		    "the zonepath are untouched).\n\tThe zone can subsequently "
2849e518655Sgjelinek 		    "be attached, or can be moved to another\n\tsystem and "
2858cd327d5Sgjelinek 		    "attached there.  The -n option can be used to specify\n\t"
2868cd327d5Sgjelinek 		    "'no-execute' mode.  When -n is used, the information "
2878cd327d5Sgjelinek 		    "needed to attach\n\tthe zone is sent to standard output "
288ff17c8bfSgjelinek 		    "but the zone is not actually\n\tdetached.  All other "
289ff17c8bfSgjelinek 		    "arguments are passed to the brand detach function;\n\tsee "
290ff17c8bfSgjelinek 		    "brands(5) for more information."));
2919e518655Sgjelinek 	case CMD_ATTACH:
2929e518655Sgjelinek 		return (gettext("Attach the zone to the system.  The zone "
2939e518655Sgjelinek 		    "state must be 'configured'\n\tprior to attach; upon "
2949e518655Sgjelinek 		    "successful completion, the zone state will be\n\t"
2959e518655Sgjelinek 		    "'installed'.  The system software on the current "
2969e518655Sgjelinek 		    "system must be\n\tcompatible with the software on the "
297ff17c8bfSgjelinek 		    "zone's original system.\n\tSpecify -F "
2986cfd72c6Sgjelinek 		    "to force the attach and skip software compatibility "
2996cfd72c6Sgjelinek 		    "tests.\n\tThe -n option can be used to specify "
3006cfd72c6Sgjelinek 		    "'no-execute' mode.  When -n is\n\tused, the information "
3016cfd72c6Sgjelinek 		    "needed to attach the zone is read from the\n\tspecified "
3026cfd72c6Sgjelinek 		    "path and the configuration is only validated.  The path "
303ff17c8bfSgjelinek 		    "can\n\tbe '-' to specify standard input.  The -F and -n "
304ff17c8bfSgjelinek 		    "options are mutually\n\texclusive.  All other arguments "
305ff17c8bfSgjelinek 		    "are passed to the brand attach\n\tfunction; see "
306ff17c8bfSgjelinek 		    "brands(5) for more information."));
307555afedfScarlsonj 	case CMD_MARK:
308555afedfScarlsonj 		return (gettext("Set the state of the zone.  This can be used "
309555afedfScarlsonj 		    "to force the zone\n\tstate to 'incomplete' "
310555afedfScarlsonj 		    "administratively if some activity has rendered\n\tthe "
311555afedfScarlsonj 		    "zone permanently unusable.  The only valid state that "
312555afedfScarlsonj 		    "may be\n\tspecified is 'incomplete'."));
313108322fbScarlsonj 	default:
314108322fbScarlsonj 		return ("");
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3177e362f58Scomay 	return (NULL);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * Called with explicit B_TRUE when help is explicitly requested, B_FALSE for
3227c478bd9Sstevel@tonic-gate  * unexpected errors.
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate static int
3267c478bd9Sstevel@tonic-gate usage(boolean_t explicit)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	int i;
3297c478bd9Sstevel@tonic-gate 	FILE *fd = explicit ? stdout : stderr;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "%s:\t%s help\n", gettext("usage"), execname);
332555afedfScarlsonj 	(void) fprintf(fd, "\t%s [-z <zone>] [-u <uuid-match>] list\n",
333555afedfScarlsonj 	    execname);
334555afedfScarlsonj 	(void) fprintf(fd, "\t%s {-z <zone>|-u <uuid-match>} <%s>\n", execname,
3357c478bd9Sstevel@tonic-gate 	    gettext("subcommand"));
3367c478bd9Sstevel@tonic-gate 	(void) fprintf(fd, "\n%s:\n\n", gettext("Subcommands"));
3377c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
338108322fbScarlsonj 		if (cmdtab[i].short_usage == NULL)
339108322fbScarlsonj 			continue;
3407c478bd9Sstevel@tonic-gate 		(void) fprintf(fd, "%s\n", cmdtab[i].short_usage);
3417c478bd9Sstevel@tonic-gate 		if (explicit)
3427c478bd9Sstevel@tonic-gate 			(void) fprintf(fd, "\t%s\n\n", long_help(i));
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 	if (!explicit)
3457c478bd9Sstevel@tonic-gate 		(void) fputs("\n", fd);
3467c478bd9Sstevel@tonic-gate 	return (Z_USAGE);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate static void
3507c478bd9Sstevel@tonic-gate sub_usage(char *short_usage, int cmd_num)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\t%s\n", gettext("usage"), short_usage);
3537c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t%s\n", long_help(cmd_num));
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate  * zperror() is like perror(3c) except that this also prints the executable
3587c478bd9Sstevel@tonic-gate  * name at the start of the message, and takes a boolean indicating whether
3597c478bd9Sstevel@tonic-gate  * to call libc'c strerror() or that from libzonecfg.
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate 
3620b5de56dSgjelinek void
3637c478bd9Sstevel@tonic-gate zperror(const char *str, boolean_t zonecfg_error)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", execname, str,
3667c478bd9Sstevel@tonic-gate 	    zonecfg_error ? zonecfg_strerror(errno) : strerror(errno));
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*
3707c478bd9Sstevel@tonic-gate  * zperror2() is very similar to zperror() above, except it also prints a
3717c478bd9Sstevel@tonic-gate  * supplied zone name after the executable.
3727c478bd9Sstevel@tonic-gate  *
3737c478bd9Sstevel@tonic-gate  * All current consumers of this function want libzonecfg's strerror() rather
3747c478bd9Sstevel@tonic-gate  * than libc's; if this ever changes, this function can be made more generic
3757c478bd9Sstevel@tonic-gate  * like zperror() above.
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate 
3780b5de56dSgjelinek void
3797c478bd9Sstevel@tonic-gate zperror2(const char *zone, const char *str)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s: %s\n", execname, zone, str,
3827c478bd9Sstevel@tonic-gate 	    zonecfg_strerror(errno));
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
3860b5de56dSgjelinek void
3877c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	va_list alist;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
3927c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", execname);
3937c478bd9Sstevel@tonic-gate 	if (target_zone != NULL)
3947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "zone '%s': ", target_zone);
3957c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
3967c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
3977c478bd9Sstevel@tonic-gate 	va_end(alist);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate static void *
4017c478bd9Sstevel@tonic-gate safe_calloc(size_t nelem, size_t elsize)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	void *r = calloc(nelem, elsize);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (r == NULL) {
4067c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"),
4077c478bd9Sstevel@tonic-gate 		    (ulong_t)nelem * elsize, strerror(errno));
4087c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	return (r);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate static void
4147c478bd9Sstevel@tonic-gate zone_print(zone_entry_t *zent, boolean_t verbose, boolean_t parsable)
4157c478bd9Sstevel@tonic-gate {
4167c478bd9Sstevel@tonic-gate 	static boolean_t firsttime = B_TRUE;
417f4b3ec61Sdh155122 	char *ip_type_str;
418f4b3ec61Sdh155122 
4192766118bS 	/* Skip a zone that shutdown while we were collecting data. */
4202766118bS 	if (zent->zname[0] == '\0')
4212766118bS 		return;
4222766118bS 
423f4b3ec61Sdh155122 	if (zent->ziptype == ZS_EXCLUSIVE)
424f4b3ec61Sdh155122 		ip_type_str = "excl";
425f4b3ec61Sdh155122 	else
426f4b3ec61Sdh155122 		ip_type_str = "shared";
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	assert(!(verbose && parsable));
4297c478bd9Sstevel@tonic-gate 	if (firsttime && verbose) {
4307c478bd9Sstevel@tonic-gate 		firsttime = B_FALSE;
431f4b3ec61Sdh155122 		(void) printf("%*s %-16s %-10s %-30s %-8s %-6s\n",
432f4b3ec61Sdh155122 		    ZONEID_WIDTH, "ID", "NAME", "STATUS", "PATH", "BRAND",
433f4b3ec61Sdh155122 		    "IP");
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	if (!verbose) {
436555afedfScarlsonj 		char *cp, *clim;
437555afedfScarlsonj 
4387c478bd9Sstevel@tonic-gate 		if (!parsable) {
4397c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", zent->zname);
4407c478bd9Sstevel@tonic-gate 			return;
4417c478bd9Sstevel@tonic-gate 		}
4427c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4437c478bd9Sstevel@tonic-gate 			(void) printf("-");
4447c478bd9Sstevel@tonic-gate 		else
4457c478bd9Sstevel@tonic-gate 			(void) printf("%lu", zent->zid);
446555afedfScarlsonj 		(void) printf(":%s:%s:", zent->zname, zent->zstate_str);
447555afedfScarlsonj 		cp = zent->zroot;
448555afedfScarlsonj 		while ((clim = strchr(cp, ':')) != NULL) {
449555afedfScarlsonj 			(void) printf("%.*s\\:", clim - cp, cp);
450555afedfScarlsonj 			cp = clim + 1;
451555afedfScarlsonj 		}
452f4b3ec61Sdh155122 		(void) printf("%s:%s:%s:%s\n", cp, zent->zuuid, zent->zbrand,
453f4b3ec61Sdh155122 		    ip_type_str);
4547c478bd9Sstevel@tonic-gate 		return;
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 	if (zent->zstate_str != NULL) {
4577c478bd9Sstevel@tonic-gate 		if (zent->zid == ZONE_ID_UNDEFINED)
4587c478bd9Sstevel@tonic-gate 			(void) printf("%*s", ZONEID_WIDTH, "-");
4597c478bd9Sstevel@tonic-gate 		else
4607c478bd9Sstevel@tonic-gate 			(void) printf("%*lu", ZONEID_WIDTH, zent->zid);
461f4b3ec61Sdh155122 		(void) printf(" %-16s %-10s %-30s %-8s %-6s\n", zent->zname,
462f4b3ec61Sdh155122 		    zent->zstate_str, zent->zroot, zent->zbrand, ip_type_str);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate static int
467108322fbScarlsonj lookup_zone_info(const char *zone_name, zoneid_t zid, zone_entry_t *zent)
4687c478bd9Sstevel@tonic-gate {
46945916cd2Sjpk 	char root[MAXPATHLEN], *cp;
4707c478bd9Sstevel@tonic-gate 	int err;
471555afedfScarlsonj 	uuid_t uuid;
4722766118bS 	zone_dochandle_t handle;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zname, zone_name, sizeof (zent->zname));
4757c478bd9Sstevel@tonic-gate 	(void) strlcpy(zent->zroot, "???", sizeof (zent->zroot));
4769acbbeafSnn35248 	(void) strlcpy(zent->zbrand, "???", sizeof (zent->zbrand));
4777c478bd9Sstevel@tonic-gate 	zent->zstate_str = "???";
4787c478bd9Sstevel@tonic-gate 
479108322fbScarlsonj 	zent->zid = zid;
4807c478bd9Sstevel@tonic-gate 
481555afedfScarlsonj 	if (zonecfg_get_uuid(zone_name, uuid) == Z_OK &&
482555afedfScarlsonj 	    !uuid_is_null(uuid))
483555afedfScarlsonj 		uuid_unparse(uuid, zent->zuuid);
484555afedfScarlsonj 	else
485555afedfScarlsonj 		zent->zuuid[0] = '\0';
486555afedfScarlsonj 
48745916cd2Sjpk 	/*
48845916cd2Sjpk 	 * For labeled zones which query the zone path of lower-level
48945916cd2Sjpk 	 * zones, the path needs to be adjusted to drop the final
49045916cd2Sjpk 	 * "/root" component. This adjusted path is then useful
49145916cd2Sjpk 	 * for reading down any exported directories from the
49245916cd2Sjpk 	 * lower-level zone.
49345916cd2Sjpk 	 */
49445916cd2Sjpk 	if (is_system_labeled() && zent->zid != ZONE_ID_UNDEFINED) {
49545916cd2Sjpk 		if (zone_getattr(zent->zid, ZONE_ATTR_ROOT, zent->zroot,
49645916cd2Sjpk 		    sizeof (zent->zroot)) == -1) {
49745916cd2Sjpk 			zperror2(zent->zname,
49845916cd2Sjpk 			    gettext("could not get zone path."));
49945916cd2Sjpk 			return (Z_ERR);
50045916cd2Sjpk 		}
50145916cd2Sjpk 		cp = zent->zroot + strlen(zent->zroot) - 5;
50245916cd2Sjpk 		if (cp > zent->zroot && strcmp(cp, "/root") == 0)
50345916cd2Sjpk 			*cp = 0;
50445916cd2Sjpk 	} else {
50545916cd2Sjpk 		if ((err = zone_get_zonepath(zent->zname, root,
50645916cd2Sjpk 		    sizeof (root))) != Z_OK) {
5077c478bd9Sstevel@tonic-gate 			errno = err;
50845916cd2Sjpk 			zperror2(zent->zname,
50945916cd2Sjpk 			    gettext("could not get zone path."));
5107c478bd9Sstevel@tonic-gate 			return (Z_ERR);
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 		(void) strlcpy(zent->zroot, root, sizeof (zent->zroot));
51345916cd2Sjpk 	}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	if ((err = zone_get_state(zent->zname, &zent->zstate_num)) != Z_OK) {
5167c478bd9Sstevel@tonic-gate 		errno = err;
5177c478bd9Sstevel@tonic-gate 		zperror2(zent->zname, gettext("could not get state"));
5187c478bd9Sstevel@tonic-gate 		return (Z_ERR);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 	zent->zstate_str = zone_state_str(zent->zstate_num);
521bafa7067Snn35248 
522bafa7067Snn35248 	/*
523bafa7067Snn35248 	 * A zone's brand is only available in the .xml file describing it,
524bafa7067Snn35248 	 * which is only visible to the global zone.  This causes
525bafa7067Snn35248 	 * zone_get_brand() to fail when called from within a non-global
526bafa7067Snn35248 	 * zone.  Fortunately we only do this on labeled systems, where we
527bafa7067Snn35248 	 * know all zones are native.
528bafa7067Snn35248 	 */
529bafa7067Snn35248 	if (getzoneid() != GLOBAL_ZONEID) {
530bafa7067Snn35248 		assert(is_system_labeled() != 0);
531e5816e35SEdward Pilatowicz 		(void) strlcpy(zent->zbrand, default_brand,
532bafa7067Snn35248 		    sizeof (zent->zbrand));
533bafa7067Snn35248 	} else if (zone_get_brand(zent->zname, zent->zbrand,
5349acbbeafSnn35248 	    sizeof (zent->zbrand)) != Z_OK) {
5359acbbeafSnn35248 		zperror2(zent->zname, gettext("could not get brand name"));
5369acbbeafSnn35248 		return (Z_ERR);
5379acbbeafSnn35248 	}
5387c478bd9Sstevel@tonic-gate 
539f4b3ec61Sdh155122 	/*
540f4b3ec61Sdh155122 	 * Get ip type of the zone.
541f4b3ec61Sdh155122 	 * Note for global zone, ZS_SHARED is set always.
542f4b3ec61Sdh155122 	 */
543f4b3ec61Sdh155122 	if (zid == GLOBAL_ZONEID) {
544f4b3ec61Sdh155122 		zent->ziptype = ZS_SHARED;
5452766118bS 		return (Z_OK);
5462766118bS 	}
547f4b3ec61Sdh155122 
5482766118bS 	/*
5492766118bS 	 * There is a race condition where the zone could boot while
5502766118bS 	 * we're walking the index file.  In this case the zone state
5512766118bS 	 * could be seen as running from the call above, but the zoneid
5522766118bS 	 * would be undefined.
5532766118bS 	 *
5542766118bS 	 * There is also a race condition where the zone could shutdown after
5552766118bS 	 * we got its running state above.  This is also not an error and
5562766118bS 	 * we fall back to getting the ziptype from the zone configuration.
5572766118bS 	 */
5582766118bS 	if (zent->zstate_num == ZONE_STATE_RUNNING &&
5592766118bS 	    zid != ZONE_ID_UNDEFINED) {
560f4b3ec61Sdh155122 		ushort_t flags;
561f4b3ec61Sdh155122 
562f4b3ec61Sdh155122 		if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags,
5632766118bS 		    sizeof (flags)) >= 0) {
564f4b3ec61Sdh155122 			if (flags & ZF_NET_EXCL)
565f4b3ec61Sdh155122 				zent->ziptype = ZS_EXCLUSIVE;
566f4b3ec61Sdh155122 			else
567f4b3ec61Sdh155122 				zent->ziptype = ZS_SHARED;
5682766118bS 			return (Z_OK);
5692766118bS 		}
5702766118bS 	}
571f4b3ec61Sdh155122 
572f4b3ec61Sdh155122 	if ((handle = zonecfg_init_handle()) == NULL) {
5732766118bS 		zperror2(zent->zname, gettext("could not init handle"));
574f4b3ec61Sdh155122 		return (Z_ERR);
575f4b3ec61Sdh155122 	}
5762766118bS 	if ((err = zonecfg_get_handle(zent->zname, handle)) != Z_OK) {
5772766118bS 		zperror2(zent->zname, gettext("could not get handle"));
578f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
579f4b3ec61Sdh155122 		return (Z_ERR);
580f4b3ec61Sdh155122 	}
581f4b3ec61Sdh155122 
5822766118bS 	if ((err = zonecfg_get_iptype(handle, &zent->ziptype)) != Z_OK) {
5832766118bS 		zperror2(zent->zname, gettext("could not get ip-type"));
584f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
585f4b3ec61Sdh155122 		return (Z_ERR);
586f4b3ec61Sdh155122 	}
587f4b3ec61Sdh155122 	zonecfg_fini_handle(handle);
588f4b3ec61Sdh155122 
5897c478bd9Sstevel@tonic-gate 	return (Z_OK);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate /*
5937c478bd9Sstevel@tonic-gate  * fetch_zents() calls zone_list(2) to find out how many zones are running
5947c478bd9Sstevel@tonic-gate  * (which is stored in the global nzents), then calls zone_list(2) again
5957c478bd9Sstevel@tonic-gate  * to fetch the list of running zones (stored in the global zents).  This
5967c478bd9Sstevel@tonic-gate  * function may be called multiple times, so if zents is already set, we
5977c478bd9Sstevel@tonic-gate  * return immediately to save work.
5982766118bS  *
5992766118bS  * Note that the data about running zones can change while this function
6002766118bS  * is running, so its possible that the list of zones will have empty slots
6012766118bS  * at the end.
6027c478bd9Sstevel@tonic-gate  */
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate static int
605108322fbScarlsonj fetch_zents(void)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	zoneid_t *zids = NULL;
6087c478bd9Sstevel@tonic-gate 	uint_t nzents_saved;
609108322fbScarlsonj 	int i, retv;
610108322fbScarlsonj 	FILE *fp;
611108322fbScarlsonj 	boolean_t inaltroot;
612108322fbScarlsonj 	zone_entry_t *zentp;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (nzents > 0)
6157c478bd9Sstevel@tonic-gate 		return (Z_OK);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0) {
6187c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zoneid list"), B_FALSE);
6197c478bd9Sstevel@tonic-gate 		return (Z_ERR);
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate again:
6237c478bd9Sstevel@tonic-gate 	if (nzents == 0)
6247c478bd9Sstevel@tonic-gate 		return (Z_OK);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	zids = safe_calloc(nzents, sizeof (zoneid_t));
6277c478bd9Sstevel@tonic-gate 	nzents_saved = nzents;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
6307c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get zone list"), B_FALSE);
6317c478bd9Sstevel@tonic-gate 		free(zids);
6327c478bd9Sstevel@tonic-gate 		return (Z_ERR);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 	if (nzents != nzents_saved) {
6357c478bd9Sstevel@tonic-gate 		/* list changed, try again */
6367c478bd9Sstevel@tonic-gate 		free(zids);
6377c478bd9Sstevel@tonic-gate 		goto again;
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	zents = safe_calloc(nzents, sizeof (zone_entry_t));
6417c478bd9Sstevel@tonic-gate 
642108322fbScarlsonj 	inaltroot = zonecfg_in_alt_root();
643108322fbScarlsonj 	if (inaltroot)
644108322fbScarlsonj 		fp = zonecfg_open_scratch("", B_FALSE);
645108322fbScarlsonj 	else
646108322fbScarlsonj 		fp = NULL;
647108322fbScarlsonj 	zentp = zents;
648108322fbScarlsonj 	retv = Z_OK;
6497c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
6507c478bd9Sstevel@tonic-gate 		char name[ZONENAME_MAX];
651108322fbScarlsonj 		char altname[ZONENAME_MAX];
6527c478bd9Sstevel@tonic-gate 
653108322fbScarlsonj 		if (getzonenamebyid(zids[i], name, sizeof (name)) < 0) {
6542766118bS 			/*
6552766118bS 			 * There is a race condition where the zone may have
6562766118bS 			 * shutdown since we retrieved the number of running
6572766118bS 			 * zones above.  This is not an error, there will be
6582766118bS 			 * an empty slot at the end of the list.
6592766118bS 			 */
660108322fbScarlsonj 			continue;
6617c478bd9Sstevel@tonic-gate 		}
662108322fbScarlsonj 		if (zonecfg_is_scratch(name)) {
663108322fbScarlsonj 			/* Ignore scratch zones by default */
664108322fbScarlsonj 			if (!inaltroot)
665108322fbScarlsonj 				continue;
666108322fbScarlsonj 			if (fp == NULL ||
667108322fbScarlsonj 			    zonecfg_reverse_scratch(fp, name, altname,
668108322fbScarlsonj 			    sizeof (altname), NULL, 0) == -1) {
6695ee84fbdSgjelinek 				zerror(gettext("could not resolve scratch "
670108322fbScarlsonj 				    "zone %s"), name);
671108322fbScarlsonj 				retv = Z_ERR;
672108322fbScarlsonj 				continue;
673108322fbScarlsonj 			}
674108322fbScarlsonj 			(void) strcpy(name, altname);
675108322fbScarlsonj 		} else {
676108322fbScarlsonj 			/* Ignore non-scratch when in an alternate root */
677108322fbScarlsonj 			if (inaltroot && strcmp(name, GLOBAL_ZONENAME) != 0)
678108322fbScarlsonj 				continue;
679108322fbScarlsonj 		}
680108322fbScarlsonj 		if (lookup_zone_info(name, zids[i], zentp) != Z_OK) {
6812766118bS 			/*
6822766118bS 			 * There is a race condition where the zone may have
6832766118bS 			 * shutdown since we retrieved the number of running
6842766118bS 			 * zones above.  This is not an error, there will be
6852766118bS 			 * an empty slot at the end of the list.
6862766118bS 			 */
687108322fbScarlsonj 			continue;
688108322fbScarlsonj 		}
689108322fbScarlsonj 		zentp++;
690108322fbScarlsonj 	}
691108322fbScarlsonj 	nzents = zentp - zents;
692108322fbScarlsonj 	if (fp != NULL)
693108322fbScarlsonj 		zonecfg_close_scratch(fp);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	free(zids);
696108322fbScarlsonj 	return (retv);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
699108322fbScarlsonj static int
7007c478bd9Sstevel@tonic-gate zone_print_list(zone_state_t min_state, boolean_t verbose, boolean_t parsable)
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate 	int i;
7037c478bd9Sstevel@tonic-gate 	zone_entry_t zent;
7047c478bd9Sstevel@tonic-gate 	FILE *cookie;
7057c478bd9Sstevel@tonic-gate 	char *name;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * First get the list of running zones from the kernel and print them.
7097c478bd9Sstevel@tonic-gate 	 * If that is all we need, then return.
7107c478bd9Sstevel@tonic-gate 	 */
711108322fbScarlsonj 	if ((i = fetch_zents()) != Z_OK) {
7127c478bd9Sstevel@tonic-gate 		/*
7137c478bd9Sstevel@tonic-gate 		 * No need for error messages; fetch_zents() has already taken
7147c478bd9Sstevel@tonic-gate 		 * care of this.
7157c478bd9Sstevel@tonic-gate 		 */
716108322fbScarlsonj 		return (i);
7177c478bd9Sstevel@tonic-gate 	}
718108322fbScarlsonj 	for (i = 0; i < nzents; i++)
7197c478bd9Sstevel@tonic-gate 		zone_print(&zents[i], verbose, parsable);
7207c478bd9Sstevel@tonic-gate 	if (min_state >= ZONE_STATE_RUNNING)
721108322fbScarlsonj 		return (Z_OK);
7227c478bd9Sstevel@tonic-gate 	/*
7237c478bd9Sstevel@tonic-gate 	 * Next, get the full list of zones from the configuration, skipping
7247c478bd9Sstevel@tonic-gate 	 * any we have already printed.
7257c478bd9Sstevel@tonic-gate 	 */
7267c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
7277c478bd9Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
7287c478bd9Sstevel@tonic-gate 		for (i = 0; i < nzents; i++) {
7297c478bd9Sstevel@tonic-gate 			if (strcmp(zents[i].zname, name) == 0)
7307c478bd9Sstevel@tonic-gate 				break;
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 		if (i < nzents) {
7337c478bd9Sstevel@tonic-gate 			free(name);
7347c478bd9Sstevel@tonic-gate 			continue;
7357c478bd9Sstevel@tonic-gate 		}
736108322fbScarlsonj 		if (lookup_zone_info(name, ZONE_ID_UNDEFINED, &zent) != Z_OK) {
7377c478bd9Sstevel@tonic-gate 			free(name);
7387c478bd9Sstevel@tonic-gate 			continue;
7397c478bd9Sstevel@tonic-gate 		}
7407c478bd9Sstevel@tonic-gate 		free(name);
7417c478bd9Sstevel@tonic-gate 		if (zent.zstate_num >= min_state)
7427c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
745108322fbScarlsonj 	return (Z_OK);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate 
748c9f134eaSjv227347 /*
749c9f134eaSjv227347  * Retrieve a zone entry by name.  Returns NULL if no such zone exists.
750c9f134eaSjv227347  */
7517c478bd9Sstevel@tonic-gate static zone_entry_t *
752c9f134eaSjv227347 lookup_running_zone(const char *str)
7537c478bd9Sstevel@tonic-gate {
7547c478bd9Sstevel@tonic-gate 	int i;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if (fetch_zents() != Z_OK)
7577c478bd9Sstevel@tonic-gate 		return (NULL);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
7607c478bd9Sstevel@tonic-gate 		if (strcmp(str, zents[i].zname) == 0)
7617c478bd9Sstevel@tonic-gate 			return (&zents[i]);
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 	return (NULL);
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate /*
7677c478bd9Sstevel@tonic-gate  * Check a bit in a mode_t: if on is B_TRUE, that bit should be on; if
7687c478bd9Sstevel@tonic-gate  * B_FALSE, it should be off.  Return B_TRUE if the mode is bad (incorrect).
7697c478bd9Sstevel@tonic-gate  */
7707c478bd9Sstevel@tonic-gate static boolean_t
7717c478bd9Sstevel@tonic-gate bad_mode_bit(mode_t mode, mode_t bit, boolean_t on, char *file)
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate 	char *str;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	assert(bit == S_IRUSR || bit == S_IWUSR || bit == S_IXUSR ||
7767c478bd9Sstevel@tonic-gate 	    bit == S_IRGRP || bit == S_IWGRP || bit == S_IXGRP ||
7777c478bd9Sstevel@tonic-gate 	    bit == S_IROTH || bit == S_IWOTH || bit == S_IXOTH);
7787c478bd9Sstevel@tonic-gate 	/*
7797c478bd9Sstevel@tonic-gate 	 * TRANSLATION_NOTE
7807c478bd9Sstevel@tonic-gate 	 * The strings below will be used as part of a larger message,
7817c478bd9Sstevel@tonic-gate 	 * either:
7827c478bd9Sstevel@tonic-gate 	 * (file name) must be (owner|group|world) (read|writ|execut)able
7837c478bd9Sstevel@tonic-gate 	 * or
7847c478bd9Sstevel@tonic-gate 	 * (file name) must not be (owner|group|world) (read|writ|execut)able
7857c478bd9Sstevel@tonic-gate 	 */
7867c478bd9Sstevel@tonic-gate 	switch (bit) {
7877c478bd9Sstevel@tonic-gate 	case S_IRUSR:
7887c478bd9Sstevel@tonic-gate 		str = gettext("owner readable");
7897c478bd9Sstevel@tonic-gate 		break;
7907c478bd9Sstevel@tonic-gate 	case S_IWUSR:
7917c478bd9Sstevel@tonic-gate 		str = gettext("owner writable");
7927c478bd9Sstevel@tonic-gate 		break;
7937c478bd9Sstevel@tonic-gate 	case S_IXUSR:
7947c478bd9Sstevel@tonic-gate 		str = gettext("owner executable");
7957c478bd9Sstevel@tonic-gate 		break;
7967c478bd9Sstevel@tonic-gate 	case S_IRGRP:
7977c478bd9Sstevel@tonic-gate 		str = gettext("group readable");
7987c478bd9Sstevel@tonic-gate 		break;
7997c478bd9Sstevel@tonic-gate 	case S_IWGRP:
8007c478bd9Sstevel@tonic-gate 		str = gettext("group writable");
8017c478bd9Sstevel@tonic-gate 		break;
8027c478bd9Sstevel@tonic-gate 	case S_IXGRP:
8037c478bd9Sstevel@tonic-gate 		str = gettext("group executable");
8047c478bd9Sstevel@tonic-gate 		break;
8057c478bd9Sstevel@tonic-gate 	case S_IROTH:
8067c478bd9Sstevel@tonic-gate 		str = gettext("world readable");
8077c478bd9Sstevel@tonic-gate 		break;
8087c478bd9Sstevel@tonic-gate 	case S_IWOTH:
8097c478bd9Sstevel@tonic-gate 		str = gettext("world writable");
8107c478bd9Sstevel@tonic-gate 		break;
8117c478bd9Sstevel@tonic-gate 	case S_IXOTH:
8127c478bd9Sstevel@tonic-gate 		str = gettext("world executable");
8137c478bd9Sstevel@tonic-gate 		break;
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 	if ((mode & bit) == (on ? 0 : bit)) {
8167c478bd9Sstevel@tonic-gate 		/*
8177c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
8187c478bd9Sstevel@tonic-gate 		 * The first parameter below is a file name; the second
8197c478bd9Sstevel@tonic-gate 		 * is one of the "(owner|group|world) (read|writ|execut)able"
8207c478bd9Sstevel@tonic-gate 		 * strings from above.
8217c478bd9Sstevel@tonic-gate 		 */
8227c478bd9Sstevel@tonic-gate 		/*
8237c478bd9Sstevel@tonic-gate 		 * The code below could be simplified but not in a way
8247c478bd9Sstevel@tonic-gate 		 * that would easily translate to non-English locales.
8257c478bd9Sstevel@tonic-gate 		 */
8267c478bd9Sstevel@tonic-gate 		if (on) {
8277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must be %s.\n"),
8287c478bd9Sstevel@tonic-gate 			    file, str);
8297c478bd9Sstevel@tonic-gate 		} else {
8307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s must not be %s.\n"),
8317c478bd9Sstevel@tonic-gate 			    file, str);
8327c478bd9Sstevel@tonic-gate 		}
8337c478bd9Sstevel@tonic-gate 		return (B_TRUE);
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 	return (B_FALSE);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate  * We want to make sure that no zone has its zone path as a child node
8407c478bd9Sstevel@tonic-gate  * (in the directory sense) of any other.  We do that by comparing this
8417c478bd9Sstevel@tonic-gate  * zone's path to the path of all other (non-global) zones.  The comparison
8427c478bd9Sstevel@tonic-gate  * in each case is simple: add '/' to the end of the path, then do a
8437c478bd9Sstevel@tonic-gate  * strncmp() of the two paths, using the length of the shorter one.
8447c478bd9Sstevel@tonic-gate  */
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate static int
8477c478bd9Sstevel@tonic-gate crosscheck_zonepaths(char *path)
8487c478bd9Sstevel@tonic-gate {
8497c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
8507c478bd9Sstevel@tonic-gate 	char path_copy[MAXPATHLEN];	/* copy of original path */
8517c478bd9Sstevel@tonic-gate 	char rpath_copy[MAXPATHLEN];	/* copy of original rpath */
8527c478bd9Sstevel@tonic-gate 	struct zoneent *ze;
8537c478bd9Sstevel@tonic-gate 	int res, err;
8547c478bd9Sstevel@tonic-gate 	FILE *cookie;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
8577c478bd9Sstevel@tonic-gate 	while ((ze = getzoneent_private(cookie)) != NULL) {
8587c478bd9Sstevel@tonic-gate 		/* Skip zones which are not installed. */
8597c478bd9Sstevel@tonic-gate 		if (ze->zone_state < ZONE_STATE_INSTALLED) {
8607c478bd9Sstevel@tonic-gate 			free(ze);
8617c478bd9Sstevel@tonic-gate 			continue;
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 		/* Skip the global zone and the current target zone. */
8647c478bd9Sstevel@tonic-gate 		if (strcmp(ze->zone_name, GLOBAL_ZONENAME) == 0 ||
8657c478bd9Sstevel@tonic-gate 		    strcmp(ze->zone_name, target_zone) == 0) {
8667c478bd9Sstevel@tonic-gate 			free(ze);
8677c478bd9Sstevel@tonic-gate 			continue;
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 		if (strlen(ze->zone_path) == 0) {
8707c478bd9Sstevel@tonic-gate 			/* old index file without path, fall back */
8717c478bd9Sstevel@tonic-gate 			if ((err = zone_get_zonepath(ze->zone_name,
8727c478bd9Sstevel@tonic-gate 			    ze->zone_path, sizeof (ze->zone_path))) != Z_OK) {
8737c478bd9Sstevel@tonic-gate 				errno = err;
8747c478bd9Sstevel@tonic-gate 				zperror2(ze->zone_name,
8757c478bd9Sstevel@tonic-gate 				    gettext("could not get zone path"));
8767c478bd9Sstevel@tonic-gate 				free(ze);
8777c478bd9Sstevel@tonic-gate 				continue;
8787c478bd9Sstevel@tonic-gate 			}
8797c478bd9Sstevel@tonic-gate 		}
880108322fbScarlsonj 		(void) snprintf(path_copy, sizeof (path_copy), "%s%s",
881108322fbScarlsonj 		    zonecfg_get_root(), ze->zone_path);
882108322fbScarlsonj 		res = resolvepath(path_copy, rpath, sizeof (rpath));
8837c478bd9Sstevel@tonic-gate 		if (res == -1) {
8847c478bd9Sstevel@tonic-gate 			if (errno != ENOENT) {
885108322fbScarlsonj 				zperror(path_copy, B_FALSE);
8867c478bd9Sstevel@tonic-gate 				free(ze);
8877c478bd9Sstevel@tonic-gate 				return (Z_ERR);
8887c478bd9Sstevel@tonic-gate 			}
8897c478bd9Sstevel@tonic-gate 			(void) printf(gettext("WARNING: zone %s is installed, "
8907c478bd9Sstevel@tonic-gate 			    "but its %s %s does not exist.\n"), ze->zone_name,
891108322fbScarlsonj 			    "zonepath", path_copy);
8927c478bd9Sstevel@tonic-gate 			free(ze);
8937c478bd9Sstevel@tonic-gate 			continue;
8947c478bd9Sstevel@tonic-gate 		}
8957c478bd9Sstevel@tonic-gate 		rpath[res] = '\0';
8967c478bd9Sstevel@tonic-gate 		(void) snprintf(path_copy, sizeof (path_copy), "%s/", path);
8977c478bd9Sstevel@tonic-gate 		(void) snprintf(rpath_copy, sizeof (rpath_copy), "%s/", rpath);
8987c478bd9Sstevel@tonic-gate 		if (strncmp(path_copy, rpath_copy,
8997c478bd9Sstevel@tonic-gate 		    min(strlen(path_copy), strlen(rpath_copy))) == 0) {
9005ee84fbdSgjelinek 			/*
9015ee84fbdSgjelinek 			 * TRANSLATION_NOTE
9025ee84fbdSgjelinek 			 * zonepath is a literal that should not be translated.
9035ee84fbdSgjelinek 			 */
9047c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s zonepath (%s) and "
9057c478bd9Sstevel@tonic-gate 			    "%s zonepath (%s) overlap.\n"),
9067c478bd9Sstevel@tonic-gate 			    target_zone, path, ze->zone_name, rpath);
9077c478bd9Sstevel@tonic-gate 			free(ze);
9087c478bd9Sstevel@tonic-gate 			return (Z_ERR);
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 		free(ze);
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
9137c478bd9Sstevel@tonic-gate 	return (Z_OK);
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate static int
9177c478bd9Sstevel@tonic-gate validate_zonepath(char *path, int cmd_num)
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	int res;			/* result of last library/system call */
9207c478bd9Sstevel@tonic-gate 	boolean_t err = B_FALSE;	/* have we run into an error? */
9217c478bd9Sstevel@tonic-gate 	struct stat stbuf;
9223f2f09c1Sdp 	struct statvfs64 vfsbuf;
9237c478bd9Sstevel@tonic-gate 	char rpath[MAXPATHLEN];		/* resolved path */
9247c478bd9Sstevel@tonic-gate 	char ppath[MAXPATHLEN];		/* parent path */
9257c478bd9Sstevel@tonic-gate 	char rppath[MAXPATHLEN];	/* resolved parent path */
9267c478bd9Sstevel@tonic-gate 	char rootpath[MAXPATHLEN];	/* root path */
9277c478bd9Sstevel@tonic-gate 	zone_state_t state;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	if (path[0] != '/') {
9307c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
9317c478bd9Sstevel@tonic-gate 		    gettext("%s is not an absolute path.\n"), path);
9327c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(path, rpath, sizeof (rpath))) == -1) {
9357c478bd9Sstevel@tonic-gate 		if ((errno != ENOENT) ||
936865e09a4Sgjelinek 		    (cmd_num != CMD_VERIFY && cmd_num != CMD_INSTALL &&
937865e09a4Sgjelinek 		    cmd_num != CMD_CLONE && cmd_num != CMD_MOVE)) {
9387c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
9397c478bd9Sstevel@tonic-gate 			return (Z_ERR);
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 		if (cmd_num == CMD_VERIFY) {
9425ee84fbdSgjelinek 			/*
9435ee84fbdSgjelinek 			 * TRANSLATION_NOTE
9445ee84fbdSgjelinek 			 * zoneadm is a literal that should not be translated.
9455ee84fbdSgjelinek 			 */
9467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("WARNING: %s does not "
9475ee84fbdSgjelinek 			    "exist, so it could not be verified.\nWhen "
9485ee84fbdSgjelinek 			    "'zoneadm %s' is run, '%s' will try to create\n%s, "
9495ee84fbdSgjelinek 			    "and '%s' will be tried again,\nbut the '%s' may "
9505ee84fbdSgjelinek 			    "fail if:\nthe parent directory of %s is group- or "
9515ee84fbdSgjelinek 			    "other-writable\nor\n%s overlaps with any other "
9527c478bd9Sstevel@tonic-gate 			    "installed zones.\n"), path,
9537c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_INSTALL), cmd_to_str(CMD_INSTALL),
9547c478bd9Sstevel@tonic-gate 			    path, cmd_to_str(CMD_VERIFY),
9557c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_VERIFY), path, path);
9567c478bd9Sstevel@tonic-gate 			return (Z_OK);
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 		/*
9597c478bd9Sstevel@tonic-gate 		 * The zonepath is supposed to be mode 700 but its
9607c478bd9Sstevel@tonic-gate 		 * parent(s) 755.  So use 755 on the mkdirp() then
9617c478bd9Sstevel@tonic-gate 		 * chmod() the zonepath itself to 700.
9627c478bd9Sstevel@tonic-gate 		 */
9637c478bd9Sstevel@tonic-gate 		if (mkdirp(path, DEFAULT_DIR_MODE) < 0) {
9647c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
9657c478bd9Sstevel@tonic-gate 			return (Z_ERR);
9667c478bd9Sstevel@tonic-gate 		}
9677c478bd9Sstevel@tonic-gate 		/*
9687c478bd9Sstevel@tonic-gate 		 * If the chmod() fails, report the error, but might
9697c478bd9Sstevel@tonic-gate 		 * as well continue the verify procedure.
9707c478bd9Sstevel@tonic-gate 		 */
9717c478bd9Sstevel@tonic-gate 		if (chmod(path, S_IRWXU) != 0)
9727c478bd9Sstevel@tonic-gate 			zperror(path, B_FALSE);
9737c478bd9Sstevel@tonic-gate 		/*
9747c478bd9Sstevel@tonic-gate 		 * Since the mkdir() succeeded, we should not have to
9757c478bd9Sstevel@tonic-gate 		 * worry about a subsequent ENOENT, thus this should
9767c478bd9Sstevel@tonic-gate 		 * only recurse once.
9777c478bd9Sstevel@tonic-gate 		 */
978865e09a4Sgjelinek 		return (validate_zonepath(path, cmd_num));
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 	rpath[res] = '\0';
9817c478bd9Sstevel@tonic-gate 	if (strcmp(path, rpath) != 0) {
9827c478bd9Sstevel@tonic-gate 		errno = Z_RESOLVED_PATH;
9837c478bd9Sstevel@tonic-gate 		zperror(path, B_TRUE);
9847c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 	if ((res = stat(rpath, &stbuf)) != 0) {
9877c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
9887c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
9917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
9927c478bd9Sstevel@tonic-gate 		    rpath);
9937c478bd9Sstevel@tonic-gate 		return (Z_ERR);
9947c478bd9Sstevel@tonic-gate 	}
9954fceebdfSblakej 	if (strcmp(stbuf.st_fstype, MNTTYPE_TMPFS) == 0) {
9967c478bd9Sstevel@tonic-gate 		(void) printf(gettext("WARNING: %s is on a temporary "
9970b5de56dSgjelinek 		    "file system.\n"), rpath);
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 	if (crosscheck_zonepaths(rpath) != Z_OK)
10007c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10017c478bd9Sstevel@tonic-gate 	/*
10027c478bd9Sstevel@tonic-gate 	 * Try to collect and report as many minor errors as possible
10037c478bd9Sstevel@tonic-gate 	 * before returning, so the user can learn everything that needs
10047c478bd9Sstevel@tonic-gate 	 * to be fixed up front.
10057c478bd9Sstevel@tonic-gate 	 */
10067c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10087c478bd9Sstevel@tonic-gate 		    rpath);
10097c478bd9Sstevel@tonic-gate 		err = B_TRUE;
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rpath);
10127c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rpath);
10137c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rpath);
10147c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRGRP, B_FALSE, rpath);
10157c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rpath);
10167c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXGRP, B_FALSE, rpath);
10177c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IROTH, B_FALSE, rpath);
10187c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rpath);
10197c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXOTH, B_FALSE, rpath);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	(void) snprintf(ppath, sizeof (ppath), "%s/..", path);
10227c478bd9Sstevel@tonic-gate 	if ((res = resolvepath(ppath, rppath, sizeof (rppath))) == -1) {
10237c478bd9Sstevel@tonic-gate 		zperror(ppath, B_FALSE);
10247c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 	rppath[res] = '\0';
10277c478bd9Sstevel@tonic-gate 	if ((res = stat(rppath, &stbuf)) != 0) {
10287c478bd9Sstevel@tonic-gate 		zperror(rppath, B_FALSE);
10297c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 	/* theoretically impossible */
10327c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stbuf.st_mode)) {
10337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not a directory.\n"),
10347c478bd9Sstevel@tonic-gate 		    rppath);
10357c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 	if (stbuf.st_uid != 0) {
10387c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is not owned by root.\n"),
10397c478bd9Sstevel@tonic-gate 		    rppath);
10407c478bd9Sstevel@tonic-gate 		err = B_TRUE;
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IRUSR, B_TRUE, rppath);
10437c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWUSR, B_TRUE, rppath);
10447c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IXUSR, B_TRUE, rppath);
10457c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWGRP, B_FALSE, rppath);
10467c478bd9Sstevel@tonic-gate 	err |= bad_mode_bit(stbuf.st_mode, S_IWOTH, B_FALSE, rppath);
10477c478bd9Sstevel@tonic-gate 	if (strcmp(rpath, rppath) == 0) {
10487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is its own parent.\n"),
10497c478bd9Sstevel@tonic-gate 		    rppath);
10507c478bd9Sstevel@tonic-gate 		err = B_TRUE;
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 
10533f2f09c1Sdp 	if (statvfs64(rpath, &vfsbuf) != 0) {
10547c478bd9Sstevel@tonic-gate 		zperror(rpath, B_FALSE);
10557c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10567c478bd9Sstevel@tonic-gate 	}
1057b5abaf04Sgjelinek 	if (strcmp(vfsbuf.f_basetype, MNTTYPE_NFS) == 0) {
10585ee84fbdSgjelinek 		/*
10595ee84fbdSgjelinek 		 * TRANSLATION_NOTE
10605ee84fbdSgjelinek 		 * Zonepath and NFS are literals that should not be translated.
10615ee84fbdSgjelinek 		 */
10625ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on an NFS "
10630b5de56dSgjelinek 		    "mounted file system.\n"
10640b5de56dSgjelinek 		    "\tA local file system must be used.\n"), rpath);
1065b5abaf04Sgjelinek 		return (Z_ERR);
1066b5abaf04Sgjelinek 	}
1067b5abaf04Sgjelinek 	if (vfsbuf.f_flag & ST_NOSUID) {
10685ee84fbdSgjelinek 		/*
10695ee84fbdSgjelinek 		 * TRANSLATION_NOTE
10705ee84fbdSgjelinek 		 * Zonepath and nosuid are literals that should not be
10715ee84fbdSgjelinek 		 * translated.
10725ee84fbdSgjelinek 		 */
1073b5abaf04Sgjelinek 		(void) fprintf(stderr, gettext("Zonepath %s is on a nosuid "
10740b5de56dSgjelinek 		    "file system.\n"), rpath);
10757c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	if ((res = zone_get_state(target_zone, &state)) != Z_OK) {
10797c478bd9Sstevel@tonic-gate 		errno = res;
10807c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get state"));
10817c478bd9Sstevel@tonic-gate 		return (Z_ERR);
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 	/*
10847c478bd9Sstevel@tonic-gate 	 * The existence of the root path is only bad in the configured state,
10857c478bd9Sstevel@tonic-gate 	 * as it is *supposed* to be there at the installed and later states.
1086ee519a1fSgjelinek 	 * However, the root path is expected to be there if the zone is
1087ee519a1fSgjelinek 	 * detached.
10887c478bd9Sstevel@tonic-gate 	 * State/command mismatches are caught earlier in verify_details().
10897c478bd9Sstevel@tonic-gate 	 */
1090ee519a1fSgjelinek 	if (state == ZONE_STATE_CONFIGURED && cmd_num != CMD_ATTACH) {
10917c478bd9Sstevel@tonic-gate 		if (snprintf(rootpath, sizeof (rootpath), "%s/root", rpath) >=
10927c478bd9Sstevel@tonic-gate 		    sizeof (rootpath)) {
10935ee84fbdSgjelinek 			/*
10945ee84fbdSgjelinek 			 * TRANSLATION_NOTE
10955ee84fbdSgjelinek 			 * Zonepath is a literal that should not be translated.
10965ee84fbdSgjelinek 			 */
10977c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
10987c478bd9Sstevel@tonic-gate 			    gettext("Zonepath %s is too long.\n"), rpath);
10997c478bd9Sstevel@tonic-gate 			return (Z_ERR);
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 		if ((res = stat(rootpath, &stbuf)) == 0) {
1102c4a45622S 			if (zonecfg_detached(rpath)) {
1103ee519a1fSgjelinek 				(void) fprintf(stderr,
1104ee519a1fSgjelinek 				    gettext("Cannot %s detached "
1105ee519a1fSgjelinek 				    "zone.\nUse attach or remove %s "
1106ee519a1fSgjelinek 				    "directory.\n"), cmd_to_str(cmd_num),
1107ee519a1fSgjelinek 				    rpath);
11087c478bd9Sstevel@tonic-gate 				return (Z_ERR);
11097c478bd9Sstevel@tonic-gate 			}
1110c4a45622S 
1111c4a45622S 			/* Not detached, check if it really looks ok. */
1112c4a45622S 
1113c4a45622S 			if (!S_ISDIR(stbuf.st_mode)) {
1114c4a45622S 				(void) fprintf(stderr, gettext("%s is not a "
1115c4a45622S 				    "directory.\n"), rootpath);
1116c4a45622S 				return (Z_ERR);
1117c4a45622S 			}
1118c4a45622S 
1119c4a45622S 			if (stbuf.st_uid != 0) {
1120c4a45622S 				(void) fprintf(stderr, gettext("%s is not "
1121c4a45622S 				    "owned by root.\n"), rootpath);
1122c4a45622S 				return (Z_ERR);
1123c4a45622S 			}
1124c4a45622S 
1125c4a45622S 			if ((stbuf.st_mode & 0777) != 0755) {
1126c4a45622S 				(void) fprintf(stderr, gettext("%s mode is not "
1127c4a45622S 				    "0755.\n"), rootpath);
1128c4a45622S 				return (Z_ERR);
1129c4a45622S 			}
1130c4a45622S 		}
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	return (err ? Z_ERR : Z_OK);
11347c478bd9Sstevel@tonic-gate }
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate static int
1137ce28b40eSzt129084 invoke_brand_handler(int cmd_num, char *argv[])
1138ce28b40eSzt129084 {
1139ce28b40eSzt129084 	zone_dochandle_t handle;
1140ce28b40eSzt129084 	int err;
1141ce28b40eSzt129084 
1142ce28b40eSzt129084 	if ((handle = zonecfg_init_handle()) == NULL) {
1143ce28b40eSzt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
1144ce28b40eSzt129084 		return (Z_ERR);
1145ce28b40eSzt129084 	}
1146ce28b40eSzt129084 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
1147ce28b40eSzt129084 		errno = err;
1148ce28b40eSzt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
1149ce28b40eSzt129084 		zonecfg_fini_handle(handle);
1150ce28b40eSzt129084 		return (Z_ERR);
1151ce28b40eSzt129084 	}
1152ce28b40eSzt129084 	if (verify_brand(handle, cmd_num, argv) != Z_OK) {
1153ce28b40eSzt129084 		zonecfg_fini_handle(handle);
1154ce28b40eSzt129084 		return (Z_ERR);
1155ce28b40eSzt129084 	}
1156ce28b40eSzt129084 	zonecfg_fini_handle(handle);
1157ce28b40eSzt129084 	return (Z_OK);
1158ce28b40eSzt129084 }
1159ce28b40eSzt129084 
1160ce28b40eSzt129084 static int
11617c478bd9Sstevel@tonic-gate ready_func(int argc, char *argv[])
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
11647c478bd9Sstevel@tonic-gate 	int arg;
11657c478bd9Sstevel@tonic-gate 
1166108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1167108322fbScarlsonj 		zerror(gettext("cannot ready zone in alternate root"));
1168108322fbScarlsonj 		return (Z_ERR);
1169108322fbScarlsonj 	}
1170108322fbScarlsonj 
11717c478bd9Sstevel@tonic-gate 	optind = 0;
11727c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
11737c478bd9Sstevel@tonic-gate 		switch (arg) {
11747c478bd9Sstevel@tonic-gate 		case '?':
11757c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11767c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
11777c478bd9Sstevel@tonic-gate 		default:
11787c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_READY, CMD_READY);
11797c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
11807c478bd9Sstevel@tonic-gate 		}
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	if (argc > optind) {
11837c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_READY, CMD_READY);
11847c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
11857c478bd9Sstevel@tonic-gate 	}
11869acbbeafSnn35248 	if (sanity_check(target_zone, CMD_READY, B_FALSE, B_FALSE, B_FALSE)
11879acbbeafSnn35248 	    != Z_OK)
11887c478bd9Sstevel@tonic-gate 		return (Z_ERR);
1189ce28b40eSzt129084 	if (verify_details(CMD_READY, argv) != Z_OK)
11907c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_READY;
1193ff17c8bfSgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
11947c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
11957c478bd9Sstevel@tonic-gate 		return (Z_ERR);
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 	return (Z_OK);
11987c478bd9Sstevel@tonic-gate }
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate static int
12017c478bd9Sstevel@tonic-gate boot_func(int argc, char *argv[])
12027c478bd9Sstevel@tonic-gate {
12037c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
12049acbbeafSnn35248 	boolean_t force = B_FALSE;
12057c478bd9Sstevel@tonic-gate 	int arg;
12067c478bd9Sstevel@tonic-gate 
1207108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1208108322fbScarlsonj 		zerror(gettext("cannot boot zone in alternate root"));
1209108322fbScarlsonj 		return (Z_ERR);
1210108322fbScarlsonj 	}
1211108322fbScarlsonj 
12127c478bd9Sstevel@tonic-gate 	zarg.bootbuf[0] = '\0';
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	/*
12153f2f09c1Sdp 	 * The following getopt processes arguments to zone boot; that
12163f2f09c1Sdp 	 * is to say, the [here] portion of the argument string:
12173f2f09c1Sdp 	 *
12183f2f09c1Sdp 	 *	zoneadm -z myzone boot [here] -- -v -m verbose
12193f2f09c1Sdp 	 *
12203f2f09c1Sdp 	 * Where [here] can either be nothing, -? (in which case we bail
12219acbbeafSnn35248 	 * and print usage), -f (a private option to indicate that the
12229acbbeafSnn35248 	 * boot operation should be 'forced'), or -s.  Support for -s is
12239acbbeafSnn35248 	 * vestigal and obsolete, but is retained because it was a
12249acbbeafSnn35248 	 * documented interface and there are known consumers including
12259acbbeafSnn35248 	 * admin/install; the proper way to specify boot arguments like -s
12269acbbeafSnn35248 	 * is:
12273f2f09c1Sdp 	 *
12283f2f09c1Sdp 	 *	zoneadm -z myzone boot -- -s -v -m verbose.
12297c478bd9Sstevel@tonic-gate 	 */
12307c478bd9Sstevel@tonic-gate 	optind = 0;
12319acbbeafSnn35248 	while ((arg = getopt(argc, argv, "?fs")) != EOF) {
12327c478bd9Sstevel@tonic-gate 		switch (arg) {
12337c478bd9Sstevel@tonic-gate 		case '?':
12347c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12357c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
12367c478bd9Sstevel@tonic-gate 		case 's':
12377c478bd9Sstevel@tonic-gate 			(void) strlcpy(zarg.bootbuf, "-s",
12387c478bd9Sstevel@tonic-gate 			    sizeof (zarg.bootbuf));
12397c478bd9Sstevel@tonic-gate 			break;
12409acbbeafSnn35248 		case 'f':
12419acbbeafSnn35248 			force = B_TRUE;
12429acbbeafSnn35248 			break;
12437c478bd9Sstevel@tonic-gate 		default:
12447c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_BOOT, CMD_BOOT);
12457c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 	}
12483f2f09c1Sdp 
12493f2f09c1Sdp 	for (; optind < argc; optind++) {
12503f2f09c1Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
12513f2f09c1Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
12523f2f09c1Sdp 			zerror(gettext("Boot argument list too long"));
12533f2f09c1Sdp 			return (Z_ERR);
12547c478bd9Sstevel@tonic-gate 		}
12553f2f09c1Sdp 		if (optind < argc - 1)
12563f2f09c1Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
12573f2f09c1Sdp 			    sizeof (zarg.bootbuf)) {
12583f2f09c1Sdp 				zerror(gettext("Boot argument list too long"));
12593f2f09c1Sdp 				return (Z_ERR);
12603f2f09c1Sdp 			}
12613f2f09c1Sdp 	}
12629acbbeafSnn35248 	if (sanity_check(target_zone, CMD_BOOT, B_FALSE, B_FALSE, force)
12639acbbeafSnn35248 	    != Z_OK)
12647c478bd9Sstevel@tonic-gate 		return (Z_ERR);
1265ce28b40eSzt129084 	if (verify_details(CMD_BOOT, argv) != Z_OK)
12667c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12679acbbeafSnn35248 	zarg.cmd = force ? Z_FORCEBOOT : Z_BOOT;
1268ff17c8bfSgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
12697c478bd9Sstevel@tonic-gate 		zerror(gettext("call to %s failed"), "zoneadmd");
12707c478bd9Sstevel@tonic-gate 		return (Z_ERR);
12717c478bd9Sstevel@tonic-gate 	}
12720209230bSgjelinek 
12737c478bd9Sstevel@tonic-gate 	return (Z_OK);
12747c478bd9Sstevel@tonic-gate }
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate static void
12777c478bd9Sstevel@tonic-gate fake_up_local_zone(zoneid_t zid, zone_entry_t *zeptr)
12787c478bd9Sstevel@tonic-gate {
12797c478bd9Sstevel@tonic-gate 	ssize_t result;
1280555afedfScarlsonj 	uuid_t uuid;
1281555afedfScarlsonj 	FILE *fp;
1282f4b3ec61Sdh155122 	ushort_t flags;
1283555afedfScarlsonj 
1284555afedfScarlsonj 	(void) memset(zeptr, 0, sizeof (*zeptr));
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	zeptr->zid = zid;
1287555afedfScarlsonj 
12887c478bd9Sstevel@tonic-gate 	/*
12897c478bd9Sstevel@tonic-gate 	 * Since we're looking up our own (non-global) zone name,
12907c478bd9Sstevel@tonic-gate 	 * we can be assured that it will succeed.
12917c478bd9Sstevel@tonic-gate 	 */
12927c478bd9Sstevel@tonic-gate 	result = getzonenamebyid(zid, zeptr->zname, sizeof (zeptr->zname));
12937c478bd9Sstevel@tonic-gate 	assert(result >= 0);
1294555afedfScarlsonj 	if (zonecfg_is_scratch(zeptr->zname) &&
1295555afedfScarlsonj 	    (fp = zonecfg_open_scratch("", B_FALSE)) != NULL) {
1296555afedfScarlsonj 		(void) zonecfg_reverse_scratch(fp, zeptr->zname, zeptr->zname,
1297555afedfScarlsonj 		    sizeof (zeptr->zname), NULL, 0);
1298555afedfScarlsonj 		zonecfg_close_scratch(fp);
1299555afedfScarlsonj 	}
1300555afedfScarlsonj 
1301555afedfScarlsonj 	if (is_system_labeled()) {
130245916cd2Sjpk 		(void) zone_getattr(zid, ZONE_ATTR_ROOT, zeptr->zroot,
130345916cd2Sjpk 		    sizeof (zeptr->zroot));
13049acbbeafSnn35248 		(void) strlcpy(zeptr->zbrand, NATIVE_BRAND_NAME,
13059acbbeafSnn35248 		    sizeof (zeptr->zbrand));
1306555afedfScarlsonj 	} else {
1307555afedfScarlsonj 		(void) strlcpy(zeptr->zroot, "/", sizeof (zeptr->zroot));
13089acbbeafSnn35248 		(void) zone_getattr(zid, ZONE_ATTR_BRAND, zeptr->zbrand,
13099acbbeafSnn35248 		    sizeof (zeptr->zbrand));
131045916cd2Sjpk 	}
1311555afedfScarlsonj 
13127c478bd9Sstevel@tonic-gate 	zeptr->zstate_str = "running";
1313555afedfScarlsonj 	if (zonecfg_get_uuid(zeptr->zname, uuid) == Z_OK &&
1314555afedfScarlsonj 	    !uuid_is_null(uuid))
1315555afedfScarlsonj 		uuid_unparse(uuid, zeptr->zuuid);
1316f4b3ec61Sdh155122 
1317f4b3ec61Sdh155122 	if (zone_getattr(zid, ZONE_ATTR_FLAGS, &flags, sizeof (flags)) < 0) {
1318f4b3ec61Sdh155122 		zperror2(zeptr->zname, gettext("could not get zone flags"));
1319f4b3ec61Sdh155122 		exit(Z_ERR);
1320f4b3ec61Sdh155122 	}
1321f4b3ec61Sdh155122 	if (flags & ZF_NET_EXCL)
1322f4b3ec61Sdh155122 		zeptr->ziptype = ZS_EXCLUSIVE;
1323f4b3ec61Sdh155122 	else
1324f4b3ec61Sdh155122 		zeptr->ziptype = ZS_SHARED;
13257c478bd9Sstevel@tonic-gate }
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate static int
13287c478bd9Sstevel@tonic-gate list_func(int argc, char *argv[])
13297c478bd9Sstevel@tonic-gate {
13307c478bd9Sstevel@tonic-gate 	zone_entry_t *zentp, zent;
1331108322fbScarlsonj 	int arg, retv;
13327c478bd9Sstevel@tonic-gate 	boolean_t output = B_FALSE, verbose = B_FALSE, parsable = B_FALSE;
13337c478bd9Sstevel@tonic-gate 	zone_state_t min_state = ZONE_STATE_RUNNING;
13347c478bd9Sstevel@tonic-gate 	zoneid_t zone_id = getzoneid();
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	if (target_zone == NULL) {
13377c478bd9Sstevel@tonic-gate 		/* all zones: default view to running but allow override */
13387c478bd9Sstevel@tonic-gate 		optind = 0;
13397c478bd9Sstevel@tonic-gate 		while ((arg = getopt(argc, argv, "?cipv")) != EOF) {
13407c478bd9Sstevel@tonic-gate 			switch (arg) {
13417c478bd9Sstevel@tonic-gate 			case '?':
13427c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13437c478bd9Sstevel@tonic-gate 				return (optopt == '?' ? Z_OK : Z_USAGE);
1344475d78a4Sjonb 				/*
1345475d78a4Sjonb 				 * The 'i' and 'c' options are not mutually
1346475d78a4Sjonb 				 * exclusive so if 'c' is given, then min_state
1347475d78a4Sjonb 				 * is set to 0 (ZONE_STATE_CONFIGURED) which is
1348475d78a4Sjonb 				 * the lowest possible state.  If 'i' is given,
1349475d78a4Sjonb 				 * then min_state is set to be the lowest state
1350475d78a4Sjonb 				 * so far.
1351475d78a4Sjonb 				 */
13527c478bd9Sstevel@tonic-gate 			case 'c':
13537c478bd9Sstevel@tonic-gate 				min_state = ZONE_STATE_CONFIGURED;
13547c478bd9Sstevel@tonic-gate 				break;
13557c478bd9Sstevel@tonic-gate 			case 'i':
1356475d78a4Sjonb 				min_state = min(ZONE_STATE_INSTALLED,
1357475d78a4Sjonb 				    min_state);
1358475d78a4Sjonb 
13597c478bd9Sstevel@tonic-gate 				break;
13607c478bd9Sstevel@tonic-gate 			case 'p':
13617c478bd9Sstevel@tonic-gate 				parsable = B_TRUE;
13627c478bd9Sstevel@tonic-gate 				break;
13637c478bd9Sstevel@tonic-gate 			case 'v':
13647c478bd9Sstevel@tonic-gate 				verbose = B_TRUE;
13657c478bd9Sstevel@tonic-gate 				break;
13667c478bd9Sstevel@tonic-gate 			default:
13677c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_LIST, CMD_LIST);
13687c478bd9Sstevel@tonic-gate 				return (Z_USAGE);
13697c478bd9Sstevel@tonic-gate 			}
13707c478bd9Sstevel@tonic-gate 		}
13717c478bd9Sstevel@tonic-gate 		if (parsable && verbose) {
13727c478bd9Sstevel@tonic-gate 			zerror(gettext("%s -p and -v are mutually exclusive."),
13737c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_LIST));
13747c478bd9Sstevel@tonic-gate 			return (Z_ERR);
13757c478bd9Sstevel@tonic-gate 		}
137645916cd2Sjpk 		if (zone_id == GLOBAL_ZONEID || is_system_labeled()) {
1377108322fbScarlsonj 			retv = zone_print_list(min_state, verbose, parsable);
13787c478bd9Sstevel@tonic-gate 		} else {
13797c478bd9Sstevel@tonic-gate 			fake_up_local_zone(zone_id, &zent);
13809acbbeafSnn35248 			retv = Z_OK;
13817c478bd9Sstevel@tonic-gate 			zone_print(&zent, verbose, parsable);
13827c478bd9Sstevel@tonic-gate 		}
1383108322fbScarlsonj 		return (retv);
13847c478bd9Sstevel@tonic-gate 	}
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	/*
13877c478bd9Sstevel@tonic-gate 	 * Specific target zone: disallow -i/-c suboptions.
13887c478bd9Sstevel@tonic-gate 	 */
13897c478bd9Sstevel@tonic-gate 	optind = 0;
13907c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?pv")) != EOF) {
13917c478bd9Sstevel@tonic-gate 		switch (arg) {
13927c478bd9Sstevel@tonic-gate 		case '?':
13937c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
13947c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
13957c478bd9Sstevel@tonic-gate 		case 'p':
13967c478bd9Sstevel@tonic-gate 			parsable = B_TRUE;
13977c478bd9Sstevel@tonic-gate 			break;
13987c478bd9Sstevel@tonic-gate 		case 'v':
13997c478bd9Sstevel@tonic-gate 			verbose = B_TRUE;
14007c478bd9Sstevel@tonic-gate 			break;
14017c478bd9Sstevel@tonic-gate 		default:
14027c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_LIST, CMD_LIST);
14037c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
14047c478bd9Sstevel@tonic-gate 		}
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 	if (parsable && verbose) {
14077c478bd9Sstevel@tonic-gate 		zerror(gettext("%s -p and -v are mutually exclusive."),
14087c478bd9Sstevel@tonic-gate 		    cmd_to_str(CMD_LIST));
14097c478bd9Sstevel@tonic-gate 		return (Z_ERR);
14107c478bd9Sstevel@tonic-gate 	}
14117c478bd9Sstevel@tonic-gate 	if (argc > optind) {
14127c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_LIST, CMD_LIST);
14137c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
14147c478bd9Sstevel@tonic-gate 	}
14152ec67e04Sgjelinek 	if (zone_id != GLOBAL_ZONEID && !is_system_labeled()) {
14167c478bd9Sstevel@tonic-gate 		fake_up_local_zone(zone_id, &zent);
14177c478bd9Sstevel@tonic-gate 		/*
14187c478bd9Sstevel@tonic-gate 		 * main() will issue a Z_NO_ZONE error if it cannot get an
14197c478bd9Sstevel@tonic-gate 		 * id for target_zone, which in a non-global zone should
14207c478bd9Sstevel@tonic-gate 		 * happen for any zone name except `zonename`.  Thus we
14217c478bd9Sstevel@tonic-gate 		 * assert() that here but don't otherwise check.
14227c478bd9Sstevel@tonic-gate 		 */
14237c478bd9Sstevel@tonic-gate 		assert(strcmp(zent.zname, target_zone) == 0);
14247c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14257c478bd9Sstevel@tonic-gate 		output = B_TRUE;
14267c478bd9Sstevel@tonic-gate 	} else if ((zentp = lookup_running_zone(target_zone)) != NULL) {
14277c478bd9Sstevel@tonic-gate 		zone_print(zentp, verbose, parsable);
14287c478bd9Sstevel@tonic-gate 		output = B_TRUE;
1429108322fbScarlsonj 	} else if (lookup_zone_info(target_zone, ZONE_ID_UNDEFINED,
1430108322fbScarlsonj 	    &zent) == Z_OK) {
14317c478bd9Sstevel@tonic-gate 		zone_print(&zent, verbose, parsable);
14327c478bd9Sstevel@tonic-gate 		output = B_TRUE;
14337c478bd9Sstevel@tonic-gate 	}
1434ce28b40eSzt129084 
1435ce28b40eSzt129084 	/*
1436ce28b40eSzt129084 	 * Invoke brand-specific handler. Note that we do this
1437db628066Sgjelinek 	 * only if we're in the global zone, and target_zone is specified
1438db628066Sgjelinek 	 * and it is not the global zone.
1439ce28b40eSzt129084 	 */
1440db628066Sgjelinek 	if (zone_id == GLOBAL_ZONEID && target_zone != NULL &&
1441db628066Sgjelinek 	    strcmp(target_zone, GLOBAL_ZONENAME) != 0)
1442ce28b40eSzt129084 		if (invoke_brand_handler(CMD_LIST, argv) != Z_OK)
1443ce28b40eSzt129084 			return (Z_ERR);
1444ce28b40eSzt129084 
14457c478bd9Sstevel@tonic-gate 	return (output ? Z_OK : Z_ERR);
14467c478bd9Sstevel@tonic-gate }
14477c478bd9Sstevel@tonic-gate 
1448ff17c8bfSgjelinek int
1449c75cc341S do_subproc(char *cmdbuf)
14509acbbeafSnn35248 {
14519acbbeafSnn35248 	void (*saveint)(int);
14529acbbeafSnn35248 	void (*saveterm)(int);
14539acbbeafSnn35248 	void (*savequit)(int);
14549acbbeafSnn35248 	void (*savehup)(int);
14559acbbeafSnn35248 	int pid, child, status;
14569acbbeafSnn35248 
14579acbbeafSnn35248 	if ((child = vfork()) == 0) {
14589acbbeafSnn35248 		(void) execl("/bin/sh", "sh", "-c", cmdbuf, (char *)NULL);
14599acbbeafSnn35248 	}
14609acbbeafSnn35248 
14619acbbeafSnn35248 	if (child == -1)
14629acbbeafSnn35248 		return (-1);
14639acbbeafSnn35248 
14649acbbeafSnn35248 	saveint = sigset(SIGINT, SIG_IGN);
14659acbbeafSnn35248 	saveterm = sigset(SIGTERM, SIG_IGN);
14669acbbeafSnn35248 	savequit = sigset(SIGQUIT, SIG_IGN);
14679acbbeafSnn35248 	savehup = sigset(SIGHUP, SIG_IGN);
14689acbbeafSnn35248 
14699acbbeafSnn35248 	while ((pid = waitpid(child, &status, 0)) != child && pid != -1)
14709acbbeafSnn35248 		;
14719acbbeafSnn35248 
14729acbbeafSnn35248 	(void) sigset(SIGINT, saveint);
14739acbbeafSnn35248 	(void) sigset(SIGTERM, saveterm);
14749acbbeafSnn35248 	(void) sigset(SIGQUIT, savequit);
14759acbbeafSnn35248 	(void) sigset(SIGHUP, savehup);
14769acbbeafSnn35248 
14779acbbeafSnn35248 	return (pid == -1 ? -1 : status);
14789acbbeafSnn35248 }
14799acbbeafSnn35248 
1480ff17c8bfSgjelinek int
14819acbbeafSnn35248 subproc_status(const char *cmd, int status, boolean_t verbose_failure)
14827c478bd9Sstevel@tonic-gate {
14837c478bd9Sstevel@tonic-gate 	if (WIFEXITED(status)) {
14847c478bd9Sstevel@tonic-gate 		int exit_code = WEXITSTATUS(status);
14857c478bd9Sstevel@tonic-gate 
14869acbbeafSnn35248 		if ((verbose_failure) && (exit_code != ZONE_SUBPROC_OK))
14877c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' failed with exit code %d."), cmd,
14887c478bd9Sstevel@tonic-gate 			    exit_code);
14899acbbeafSnn35248 
14909acbbeafSnn35248 		return (exit_code);
14917c478bd9Sstevel@tonic-gate 	} else if (WIFSIGNALED(status)) {
14927c478bd9Sstevel@tonic-gate 		int signal = WTERMSIG(status);
14937c478bd9Sstevel@tonic-gate 		char sigstr[SIG2STR_MAX];
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 		if (sig2str(signal, sigstr) == 0) {
14967c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by signal SIG%s."), cmd,
14977c478bd9Sstevel@tonic-gate 			    sigstr);
14987c478bd9Sstevel@tonic-gate 		} else {
14997c478bd9Sstevel@tonic-gate 			zerror(gettext("'%s' terminated by an unknown signal."),
15007c478bd9Sstevel@tonic-gate 			    cmd);
15017c478bd9Sstevel@tonic-gate 		}
15027c478bd9Sstevel@tonic-gate 	} else {
15037c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' failed for unknown reasons."), cmd);
15047c478bd9Sstevel@tonic-gate 	}
15059acbbeafSnn35248 
15069acbbeafSnn35248 	/*
15079acbbeafSnn35248 	 * Assume a subprocess that died due to a signal or an unknown error
15089acbbeafSnn35248 	 * should be considered an exit code of ZONE_SUBPROC_FATAL, as the
15099acbbeafSnn35248 	 * user will likely need to do some manual cleanup.
15109acbbeafSnn35248 	 */
15119acbbeafSnn35248 	return (ZONE_SUBPROC_FATAL);
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate 
1514*cb8a054bSGlenn Faden static int
1515*cb8a054bSGlenn Faden auth_check(char *user, char *zone, int cmd_num)
1516*cb8a054bSGlenn Faden {
1517*cb8a054bSGlenn Faden 	char authname[MAXAUTHS];
1518*cb8a054bSGlenn Faden 
1519*cb8a054bSGlenn Faden 	switch (cmd_num) {
1520*cb8a054bSGlenn Faden 	case CMD_LIST:
1521*cb8a054bSGlenn Faden 	case CMD_HELP:
1522*cb8a054bSGlenn Faden 		return (Z_OK);
1523*cb8a054bSGlenn Faden 	case SOURCE_ZONE:
1524*cb8a054bSGlenn Faden 		(void) strlcpy(authname, ZONE_CLONEFROM_AUTH, MAXAUTHS);
1525*cb8a054bSGlenn Faden 		break;
1526*cb8a054bSGlenn Faden 	case CMD_BOOT:
1527*cb8a054bSGlenn Faden 	case CMD_HALT:
1528*cb8a054bSGlenn Faden 	case CMD_READY:
1529*cb8a054bSGlenn Faden 	case CMD_REBOOT:
1530*cb8a054bSGlenn Faden 	case CMD_SYSBOOT:
1531*cb8a054bSGlenn Faden 	case CMD_VERIFY:
1532*cb8a054bSGlenn Faden 	case CMD_INSTALL:
1533*cb8a054bSGlenn Faden 	case CMD_UNINSTALL:
1534*cb8a054bSGlenn Faden 	case CMD_MOUNT:
1535*cb8a054bSGlenn Faden 	case CMD_UNMOUNT:
1536*cb8a054bSGlenn Faden 	case CMD_CLONE:
1537*cb8a054bSGlenn Faden 	case CMD_MOVE:
1538*cb8a054bSGlenn Faden 	case CMD_DETACH:
1539*cb8a054bSGlenn Faden 	case CMD_ATTACH:
1540*cb8a054bSGlenn Faden 	case CMD_MARK:
1541*cb8a054bSGlenn Faden 	case CMD_APPLY:
1542*cb8a054bSGlenn Faden 	default:
1543*cb8a054bSGlenn Faden 		(void) strlcpy(authname, ZONE_MANAGE_AUTH, MAXAUTHS);
1544*cb8a054bSGlenn Faden 		break;
1545*cb8a054bSGlenn Faden 	}
1546*cb8a054bSGlenn Faden 	(void) strlcat(authname, KV_OBJECT, MAXAUTHS);
1547*cb8a054bSGlenn Faden 	(void) strlcat(authname, zone, MAXAUTHS);
1548*cb8a054bSGlenn Faden 	if (chkauthattr(authname, user) == 0) {
1549*cb8a054bSGlenn Faden 		return (Z_ERR);
1550*cb8a054bSGlenn Faden 	} else {
1551*cb8a054bSGlenn Faden 		/*
1552*cb8a054bSGlenn Faden 		 * Some subcommands, e.g. install, run subcommands,
1553*cb8a054bSGlenn Faden 		 * e.g. sysidcfg, that require a real uid of root,
1554*cb8a054bSGlenn Faden 		 *  so switch to root, here.
1555*cb8a054bSGlenn Faden 		 */
1556*cb8a054bSGlenn Faden 		if (setuid(0) == -1) {
1557*cb8a054bSGlenn Faden 			zperror(gettext("insufficient privilege"), B_TRUE);
1558*cb8a054bSGlenn Faden 			return (Z_ERR);
1559*cb8a054bSGlenn Faden 		}
1560*cb8a054bSGlenn Faden 		return (Z_OK);
1561*cb8a054bSGlenn Faden 	}
1562*cb8a054bSGlenn Faden }
1563*cb8a054bSGlenn Faden 
15647c478bd9Sstevel@tonic-gate /*
15657c478bd9Sstevel@tonic-gate  * Various sanity checks; make sure:
15667c478bd9Sstevel@tonic-gate  * 1. We're in the global zone.
15677c478bd9Sstevel@tonic-gate  * 2. The calling user has sufficient privilege.
15687c478bd9Sstevel@tonic-gate  * 3. The target zone is neither the global zone nor anything starting with
15697c478bd9Sstevel@tonic-gate  *    "SUNW".
15707c478bd9Sstevel@tonic-gate  * 4a. If we're looking for a 'not running' (i.e., configured or installed)
15717c478bd9Sstevel@tonic-gate  *     zone, the name service knows about it.
15727c478bd9Sstevel@tonic-gate  * 4b. For some operations which expect a zone not to be running, that it is
15737c478bd9Sstevel@tonic-gate  *     not already running (or ready).
15747c478bd9Sstevel@tonic-gate  */
15757c478bd9Sstevel@tonic-gate static int
15767c478bd9Sstevel@tonic-gate sanity_check(char *zone, int cmd_num, boolean_t running,
15779acbbeafSnn35248     boolean_t unsafe_when_running, boolean_t force)
15787c478bd9Sstevel@tonic-gate {
15797c478bd9Sstevel@tonic-gate 	zone_entry_t *zent;
15807c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
15819acbbeafSnn35248 	zone_state_t state, min_state;
1582108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
1583108322fbScarlsonj 	FILE *fp;
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
1586ffbafc53Scomay 		switch (cmd_num) {
1587ffbafc53Scomay 		case CMD_HALT:
1588ffbafc53Scomay 			zerror(gettext("use %s to %s this zone."), "halt(1M)",
15897c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num));
1590ffbafc53Scomay 			break;
1591ffbafc53Scomay 		case CMD_REBOOT:
1592ffbafc53Scomay 			zerror(gettext("use %s to %s this zone."),
1593ffbafc53Scomay 			    "reboot(1M)", cmd_to_str(cmd_num));
1594ffbafc53Scomay 			break;
1595ffbafc53Scomay 		default:
1596ffbafc53Scomay 			zerror(gettext("must be in the global zone to %s a "
1597ffbafc53Scomay 			    "zone."), cmd_to_str(cmd_num));
1598ffbafc53Scomay 			break;
1599ffbafc53Scomay 		}
16007c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16017c478bd9Sstevel@tonic-gate 	}
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
16047c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "priv_allocset");
16057c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
16097c478bd9Sstevel@tonic-gate 		zerror(gettext("%s failed"), "getppriv");
16107c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
16117c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16127c478bd9Sstevel@tonic-gate 	}
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
16157c478bd9Sstevel@tonic-gate 		zerror(gettext("only a privileged user may %s a zone."),
16167c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16177c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
16187c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16197c478bd9Sstevel@tonic-gate 	}
16207c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	if (zone == NULL) {
16237c478bd9Sstevel@tonic-gate 		zerror(gettext("no zone specified"));
16247c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 
1627*cb8a054bSGlenn Faden 	if (auth_check(username, zone, cmd_num) == Z_ERR) {
1628*cb8a054bSGlenn Faden 		zerror(gettext("User %s is not authorized to %s this zone."),
1629*cb8a054bSGlenn Faden 		    username, cmd_to_str(cmd_num));
1630*cb8a054bSGlenn Faden 		return (Z_ERR);
1631*cb8a054bSGlenn Faden 	}
1632*cb8a054bSGlenn Faden 
16337c478bd9Sstevel@tonic-gate 	if (strcmp(zone, GLOBAL_ZONENAME) == 0) {
16347c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for the global zone."),
16357c478bd9Sstevel@tonic-gate 		    cmd_to_str(cmd_num));
16367c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16377c478bd9Sstevel@tonic-gate 	}
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	if (strncmp(zone, "SUNW", 4) == 0) {
16407c478bd9Sstevel@tonic-gate 		zerror(gettext("%s operation is invalid for zones starting "
16417c478bd9Sstevel@tonic-gate 		    "with SUNW."), cmd_to_str(cmd_num));
16427c478bd9Sstevel@tonic-gate 		return (Z_ERR);
16437c478bd9Sstevel@tonic-gate 	}
16447c478bd9Sstevel@tonic-gate 
1645108322fbScarlsonj 	if (!zonecfg_in_alt_root()) {
1646108322fbScarlsonj 		zent = lookup_running_zone(zone);
1647108322fbScarlsonj 	} else if ((fp = zonecfg_open_scratch("", B_FALSE)) == NULL) {
1648108322fbScarlsonj 		zent = NULL;
1649108322fbScarlsonj 	} else {
1650108322fbScarlsonj 		if (zonecfg_find_scratch(fp, zone, zonecfg_get_root(),
1651108322fbScarlsonj 		    kernzone, sizeof (kernzone)) == 0)
1652108322fbScarlsonj 			zent = lookup_running_zone(kernzone);
1653108322fbScarlsonj 		else
1654108322fbScarlsonj 			zent = NULL;
1655108322fbScarlsonj 		zonecfg_close_scratch(fp);
1656108322fbScarlsonj 	}
1657108322fbScarlsonj 
16587c478bd9Sstevel@tonic-gate 	/*
16597c478bd9Sstevel@tonic-gate 	 * Look up from the kernel for 'running' zones.
16607c478bd9Sstevel@tonic-gate 	 */
16619acbbeafSnn35248 	if (running && !force) {
16627c478bd9Sstevel@tonic-gate 		if (zent == NULL) {
16637c478bd9Sstevel@tonic-gate 			zerror(gettext("not running"));
16647c478bd9Sstevel@tonic-gate 			return (Z_ERR);
16657c478bd9Sstevel@tonic-gate 		}
16667c478bd9Sstevel@tonic-gate 	} else {
16677c478bd9Sstevel@tonic-gate 		int err;
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 		if (unsafe_when_running && zent != NULL) {
16707c478bd9Sstevel@tonic-gate 			/* check whether the zone is ready or running */
16717c478bd9Sstevel@tonic-gate 			if ((err = zone_get_state(zent->zname,
16727c478bd9Sstevel@tonic-gate 			    &zent->zstate_num)) != Z_OK) {
16737c478bd9Sstevel@tonic-gate 				errno = err;
16747c478bd9Sstevel@tonic-gate 				zperror2(zent->zname,
16757c478bd9Sstevel@tonic-gate 				    gettext("could not get state"));
16767c478bd9Sstevel@tonic-gate 				/* can't tell, so hedge */
16777c478bd9Sstevel@tonic-gate 				zent->zstate_str = "ready/running";
16787c478bd9Sstevel@tonic-gate 			} else {
16797c478bd9Sstevel@tonic-gate 				zent->zstate_str =
16807c478bd9Sstevel@tonic-gate 				    zone_state_str(zent->zstate_num);
16817c478bd9Sstevel@tonic-gate 			}
16827c478bd9Sstevel@tonic-gate 			zerror(gettext("%s operation is invalid for %s zones."),
16837c478bd9Sstevel@tonic-gate 			    cmd_to_str(cmd_num), zent->zstate_str);
16847c478bd9Sstevel@tonic-gate 			return (Z_ERR);
16857c478bd9Sstevel@tonic-gate 		}
16867c478bd9Sstevel@tonic-gate 		if ((err = zone_get_state(zone, &state)) != Z_OK) {
16877c478bd9Sstevel@tonic-gate 			errno = err;
16887c478bd9Sstevel@tonic-gate 			zperror2(zone, gettext("could not get state"));
16897c478bd9Sstevel@tonic-gate 			return (Z_ERR);
16907c478bd9Sstevel@tonic-gate 		}
16917c478bd9Sstevel@tonic-gate 		switch (cmd_num) {
16927c478bd9Sstevel@tonic-gate 		case CMD_UNINSTALL:
16937c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_CONFIGURED) {
16947c478bd9Sstevel@tonic-gate 				zerror(gettext("is already in state '%s'."),
16957c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_CONFIGURED));
16967c478bd9Sstevel@tonic-gate 				return (Z_ERR);
16977c478bd9Sstevel@tonic-gate 			}
16987c478bd9Sstevel@tonic-gate 			break;
1699ee519a1fSgjelinek 		case CMD_ATTACH:
1700edfa49ffS 			if (state == ZONE_STATE_INSTALLED) {
1701edfa49ffS 				zerror(gettext("is already %s."),
1702edfa49ffS 				    zone_state_str(ZONE_STATE_INSTALLED));
1703edfa49ffS 				return (Z_ERR);
1704edfa49ffS 			} else if (state == ZONE_STATE_INCOMPLETE && !force) {
1705edfa49ffS 				zerror(gettext("zone is %s; %s required."),
1706edfa49ffS 				    zone_state_str(ZONE_STATE_INCOMPLETE),
1707edfa49ffS 				    cmd_to_str(CMD_UNINSTALL));
1708edfa49ffS 				return (Z_ERR);
1709edfa49ffS 			}
1710edfa49ffS 			break;
1711865e09a4Sgjelinek 		case CMD_CLONE:
17127c478bd9Sstevel@tonic-gate 		case CMD_INSTALL:
17137c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INSTALLED) {
17147c478bd9Sstevel@tonic-gate 				zerror(gettext("is already %s."),
17157c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INSTALLED));
17167c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17177c478bd9Sstevel@tonic-gate 			} else if (state == ZONE_STATE_INCOMPLETE) {
17187c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17197c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17207c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17217c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17227c478bd9Sstevel@tonic-gate 			}
17237c478bd9Sstevel@tonic-gate 			break;
1724ee519a1fSgjelinek 		case CMD_DETACH:
1725865e09a4Sgjelinek 		case CMD_MOVE:
17267c478bd9Sstevel@tonic-gate 		case CMD_READY:
17277c478bd9Sstevel@tonic-gate 		case CMD_BOOT:
1728108322fbScarlsonj 		case CMD_MOUNT:
1729555afedfScarlsonj 		case CMD_MARK:
17309acbbeafSnn35248 			if ((cmd_num == CMD_BOOT || cmd_num == CMD_MOUNT) &&
17319acbbeafSnn35248 			    force)
17329acbbeafSnn35248 				min_state = ZONE_STATE_INCOMPLETE;
1733edfa49ffS 			else if (cmd_num == CMD_MARK)
1734edfa49ffS 				min_state = ZONE_STATE_CONFIGURED;
17359acbbeafSnn35248 			else
17369acbbeafSnn35248 				min_state = ZONE_STATE_INSTALLED;
17379acbbeafSnn35248 
17389acbbeafSnn35248 			if (state < min_state) {
17397c478bd9Sstevel@tonic-gate 				zerror(gettext("must be %s before %s."),
17409acbbeafSnn35248 				    zone_state_str(min_state),
17417c478bd9Sstevel@tonic-gate 				    cmd_to_str(cmd_num));
17427c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17437c478bd9Sstevel@tonic-gate 			}
17447c478bd9Sstevel@tonic-gate 			break;
17457c478bd9Sstevel@tonic-gate 		case CMD_VERIFY:
17467c478bd9Sstevel@tonic-gate 			if (state == ZONE_STATE_INCOMPLETE) {
17477c478bd9Sstevel@tonic-gate 				zerror(gettext("zone is %s; %s required."),
17487c478bd9Sstevel@tonic-gate 				    zone_state_str(ZONE_STATE_INCOMPLETE),
17497c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
17507c478bd9Sstevel@tonic-gate 				return (Z_ERR);
17517c478bd9Sstevel@tonic-gate 			}
17527c478bd9Sstevel@tonic-gate 			break;
1753108322fbScarlsonj 		case CMD_UNMOUNT:
1754108322fbScarlsonj 			if (state != ZONE_STATE_MOUNTED) {
1755108322fbScarlsonj 				zerror(gettext("must be %s before %s."),
1756108322fbScarlsonj 				    zone_state_str(ZONE_STATE_MOUNTED),
1757108322fbScarlsonj 				    cmd_to_str(cmd_num));
1758108322fbScarlsonj 				return (Z_ERR);
1759108322fbScarlsonj 			}
1760108322fbScarlsonj 			break;
1761fbbfbc6eSjv227347 		case CMD_SYSBOOT:
1762fbbfbc6eSjv227347 			if (state != ZONE_STATE_INSTALLED) {
1763fbbfbc6eSjv227347 				zerror(gettext("%s operation is invalid for %s "
1764fbbfbc6eSjv227347 				    "zones."), cmd_to_str(cmd_num),
1765fbbfbc6eSjv227347 				    zone_state_str(state));
1766fbbfbc6eSjv227347 				return (Z_ERR);
1767fbbfbc6eSjv227347 			}
1768fbbfbc6eSjv227347 			break;
17697c478bd9Sstevel@tonic-gate 		}
17707c478bd9Sstevel@tonic-gate 	}
17717c478bd9Sstevel@tonic-gate 	return (Z_OK);
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate static int
17757c478bd9Sstevel@tonic-gate halt_func(int argc, char *argv[])
17767c478bd9Sstevel@tonic-gate {
17777c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
17787c478bd9Sstevel@tonic-gate 	int arg;
17797c478bd9Sstevel@tonic-gate 
1780108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1781108322fbScarlsonj 		zerror(gettext("cannot halt zone in alternate root"));
1782108322fbScarlsonj 		return (Z_ERR);
1783108322fbScarlsonj 	}
1784108322fbScarlsonj 
17857c478bd9Sstevel@tonic-gate 	optind = 0;
17867c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
17877c478bd9Sstevel@tonic-gate 		switch (arg) {
17887c478bd9Sstevel@tonic-gate 		case '?':
17897c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17907c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
17917c478bd9Sstevel@tonic-gate 		default:
17927c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HALT, CMD_HALT);
17937c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
17947c478bd9Sstevel@tonic-gate 		}
17957c478bd9Sstevel@tonic-gate 	}
17967c478bd9Sstevel@tonic-gate 	if (argc > optind) {
17977c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_HALT, CMD_HALT);
17987c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 	/*
18017c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
18027c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
18037c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
18047c478bd9Sstevel@tonic-gate 	 */
18059acbbeafSnn35248 	if (sanity_check(target_zone, CMD_HALT, B_FALSE, B_FALSE, B_FALSE)
18069acbbeafSnn35248 	    != Z_OK)
18077c478bd9Sstevel@tonic-gate 		return (Z_ERR);
18087c478bd9Sstevel@tonic-gate 
1809ce28b40eSzt129084 	/*
1810ce28b40eSzt129084 	 * Invoke brand-specific handler.
1811ce28b40eSzt129084 	 */
1812ce28b40eSzt129084 	if (invoke_brand_handler(CMD_HALT, argv) != Z_OK)
1813ce28b40eSzt129084 		return (Z_ERR);
1814ce28b40eSzt129084 
18157c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_HALT;
1816ff17c8bfSgjelinek 	return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale,
1817ff17c8bfSgjelinek 	    B_TRUE) == 0) ?  Z_OK : Z_ERR);
18187c478bd9Sstevel@tonic-gate }
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate static int
18217c478bd9Sstevel@tonic-gate reboot_func(int argc, char *argv[])
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate 	zone_cmd_arg_t zarg;
18247c478bd9Sstevel@tonic-gate 	int arg;
18257c478bd9Sstevel@tonic-gate 
1826108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1827108322fbScarlsonj 		zerror(gettext("cannot reboot zone in alternate root"));
1828108322fbScarlsonj 		return (Z_ERR);
1829108322fbScarlsonj 	}
1830108322fbScarlsonj 
18317c478bd9Sstevel@tonic-gate 	optind = 0;
18327c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
18337c478bd9Sstevel@tonic-gate 		switch (arg) {
18347c478bd9Sstevel@tonic-gate 		case '?':
18357c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
18367c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
18377c478bd9Sstevel@tonic-gate 		default:
18387c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_REBOOT, CMD_REBOOT);
18397c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
18407c478bd9Sstevel@tonic-gate 		}
18417c478bd9Sstevel@tonic-gate 	}
18423f2f09c1Sdp 
18433f2f09c1Sdp 	zarg.bootbuf[0] = '\0';
18443f2f09c1Sdp 	for (; optind < argc; optind++) {
18453f2f09c1Sdp 		if (strlcat(zarg.bootbuf, argv[optind],
18463f2f09c1Sdp 		    sizeof (zarg.bootbuf)) >= sizeof (zarg.bootbuf)) {
18473f2f09c1Sdp 			zerror(gettext("Boot argument list too long"));
18483f2f09c1Sdp 			return (Z_ERR);
18497c478bd9Sstevel@tonic-gate 		}
18503f2f09c1Sdp 		if (optind < argc - 1)
18513f2f09c1Sdp 			if (strlcat(zarg.bootbuf, " ", sizeof (zarg.bootbuf)) >=
18523f2f09c1Sdp 			    sizeof (zarg.bootbuf)) {
18533f2f09c1Sdp 				zerror(gettext("Boot argument list too long"));
18543f2f09c1Sdp 				return (Z_ERR);
18553f2f09c1Sdp 			}
18563f2f09c1Sdp 	}
18573f2f09c1Sdp 
18583f2f09c1Sdp 
18597c478bd9Sstevel@tonic-gate 	/*
18607c478bd9Sstevel@tonic-gate 	 * zoneadmd should be the one to decide whether or not to proceed,
18617c478bd9Sstevel@tonic-gate 	 * so even though it seems that the fourth parameter below should
18627c478bd9Sstevel@tonic-gate 	 * perhaps be B_TRUE, it really shouldn't be.
18637c478bd9Sstevel@tonic-gate 	 */
18649acbbeafSnn35248 	if (sanity_check(target_zone, CMD_REBOOT, B_TRUE, B_FALSE, B_FALSE)
18659acbbeafSnn35248 	    != Z_OK)
18667c478bd9Sstevel@tonic-gate 		return (Z_ERR);
1867ce28b40eSzt129084 	if (verify_details(CMD_REBOOT, argv) != Z_OK)
1868b5abaf04Sgjelinek 		return (Z_ERR);
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	zarg.cmd = Z_REBOOT;
1871ff17c8bfSgjelinek 	return ((zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) == 0)
1872ff17c8bfSgjelinek 	    ? Z_OK : Z_ERR);
1873ff17c8bfSgjelinek }
1874ff17c8bfSgjelinek 
1875ff17c8bfSgjelinek static int
1876ff17c8bfSgjelinek get_hook(brand_handle_t bh, char *cmd, size_t len, int (*bp)(brand_handle_t,
1877ff17c8bfSgjelinek     const char *, const char *, char *, size_t), char *zonename, char *zonepath)
1878ff17c8bfSgjelinek {
1879ff17c8bfSgjelinek 	if (strlcpy(cmd, EXEC_PREFIX, len) >= len)
1880ff17c8bfSgjelinek 		return (Z_ERR);
1881ff17c8bfSgjelinek 
1882ff17c8bfSgjelinek 	if (bp(bh, zonename, zonepath, cmd + EXEC_LEN, len - EXEC_LEN) != 0)
1883ff17c8bfSgjelinek 		return (Z_ERR);
1884ff17c8bfSgjelinek 
1885ff17c8bfSgjelinek 	if (strlen(cmd) <= EXEC_LEN)
1886ff17c8bfSgjelinek 		cmd[0] = '\0';
1887ff17c8bfSgjelinek 
1888ff17c8bfSgjelinek 	return (Z_OK);
18897c478bd9Sstevel@tonic-gate }
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate static int
1892ce28b40eSzt129084 verify_brand(zone_dochandle_t handle, int cmd_num, char *argv[])
18939acbbeafSnn35248 {
18949acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
18959acbbeafSnn35248 	int err;
18969acbbeafSnn35248 	char zonepath[MAXPATHLEN];
1897123807fbSedp 	brand_handle_t bh = NULL;
1898ce28b40eSzt129084 	int status, i;
18999acbbeafSnn35248 
19009acbbeafSnn35248 	/*
19019acbbeafSnn35248 	 * Fetch the verify command from the brand configuration.
19029acbbeafSnn35248 	 * "exec" the command so that the returned status is that of
19039acbbeafSnn35248 	 * the command and not the shell.
19049acbbeafSnn35248 	 */
1905ff17c8bfSgjelinek 	if (handle == NULL) {
1906ff17c8bfSgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
1907ff17c8bfSgjelinek 	} else if ((err = zonecfg_get_zonepath(handle, zonepath,
1908ff17c8bfSgjelinek 	    sizeof (zonepath))) != Z_OK) {
19099acbbeafSnn35248 		errno = err;
1910ce28b40eSzt129084 		zperror(cmd_to_str(cmd_num), B_TRUE);
19119acbbeafSnn35248 		return (Z_ERR);
19129acbbeafSnn35248 	}
1913123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
19149acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
19159acbbeafSnn35248 		return (Z_ERR);
19169acbbeafSnn35248 	}
19179acbbeafSnn35248 
19189acbbeafSnn35248 	/*
19199acbbeafSnn35248 	 * If the brand has its own verification routine, execute it now.
1920ce28b40eSzt129084 	 * The verification routine validates the intended zoneadm
1921ce28b40eSzt129084 	 * operation for the specific brand. The zoneadm subcommand and
1922ce28b40eSzt129084 	 * all its arguments are passed to the routine.
19239acbbeafSnn35248 	 */
1924ff17c8bfSgjelinek 	err = get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_verify_adm,
1925ff17c8bfSgjelinek 	    target_zone, zonepath);
1926123807fbSedp 	brand_close(bh);
1927ff17c8bfSgjelinek 	if (err != Z_OK)
1928ce28b40eSzt129084 		return (Z_BRAND_ERROR);
1929ff17c8bfSgjelinek 	if (cmdbuf[0] == '\0')
1930ce28b40eSzt129084 		return (Z_OK);
1931ce28b40eSzt129084 
1932ce28b40eSzt129084 	if (strlcat(cmdbuf, cmd_to_str(cmd_num),
1933ce28b40eSzt129084 	    sizeof (cmdbuf)) >= sizeof (cmdbuf))
1934ce28b40eSzt129084 		return (Z_ERR);
1935ce28b40eSzt129084 
1936ce28b40eSzt129084 	/* Build the argv string */
1937ce28b40eSzt129084 	i = 0;
1938ce28b40eSzt129084 	while (argv[i] != NULL) {
1939ce28b40eSzt129084 		if ((strlcat(cmdbuf, " ",
1940ce28b40eSzt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)) ||
1941ce28b40eSzt129084 		    (strlcat(cmdbuf, argv[i++],
1942ce28b40eSzt129084 		    sizeof (cmdbuf)) >= sizeof (cmdbuf)))
1943ce28b40eSzt129084 			return (Z_ERR);
1944ce28b40eSzt129084 	}
1945ce28b40eSzt129084 
1946d9e728a2Sgjelinek 	status = do_subproc(cmdbuf);
19479acbbeafSnn35248 	err = subproc_status(gettext("brand-specific verification"),
19489acbbeafSnn35248 	    status, B_FALSE);
19499acbbeafSnn35248 
1950ce28b40eSzt129084 	return ((err == ZONE_SUBPROC_OK) ? Z_OK : Z_BRAND_ERROR);
19519acbbeafSnn35248 }
19529acbbeafSnn35248 
19539acbbeafSnn35248 static int
19547c478bd9Sstevel@tonic-gate verify_rctls(zone_dochandle_t handle)
19557c478bd9Sstevel@tonic-gate {
19567c478bd9Sstevel@tonic-gate 	struct zone_rctltab rctltab;
19577c478bd9Sstevel@tonic-gate 	size_t rbs = rctlblk_size();
19587c478bd9Sstevel@tonic-gate 	rctlblk_t *rctlblk;
19597c478bd9Sstevel@tonic-gate 	int error = Z_INVAL;
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if ((rctlblk = malloc(rbs)) == NULL) {
19627c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to allocate %lu bytes: %s"), rbs,
19637c478bd9Sstevel@tonic-gate 		    strerror(errno));
19647c478bd9Sstevel@tonic-gate 		return (Z_NOMEM);
19657c478bd9Sstevel@tonic-gate 	}
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	if (zonecfg_setrctlent(handle) != Z_OK) {
19687c478bd9Sstevel@tonic-gate 		zerror(gettext("zonecfg_setrctlent failed"));
19697c478bd9Sstevel@tonic-gate 		free(rctlblk);
19707c478bd9Sstevel@tonic-gate 		return (error);
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
19747c478bd9Sstevel@tonic-gate 	while (zonecfg_getrctlent(handle, &rctltab) == Z_OK) {
19757c478bd9Sstevel@tonic-gate 		struct zone_rctlvaltab *rctlval;
19767c478bd9Sstevel@tonic-gate 		const char *name = rctltab.zone_rctl_name;
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 		if (!zonecfg_is_rctl(name)) {
19797c478bd9Sstevel@tonic-gate 			zerror(gettext("WARNING: Ignoring unrecognized rctl "
19807c478bd9Sstevel@tonic-gate 			    "'%s'."),  name);
19817c478bd9Sstevel@tonic-gate 			zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
19827c478bd9Sstevel@tonic-gate 			rctltab.zone_rctl_valptr = NULL;
19837c478bd9Sstevel@tonic-gate 			continue;
19847c478bd9Sstevel@tonic-gate 		}
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 		for (rctlval = rctltab.zone_rctl_valptr; rctlval != NULL;
19877c478bd9Sstevel@tonic-gate 		    rctlval = rctlval->zone_rctlval_next) {
19887c478bd9Sstevel@tonic-gate 			if (zonecfg_construct_rctlblk(rctlval, rctlblk)
19897c478bd9Sstevel@tonic-gate 			    != Z_OK) {
19907c478bd9Sstevel@tonic-gate 				zerror(gettext("invalid rctl value: "
19917c478bd9Sstevel@tonic-gate 				    "(priv=%s,limit=%s,action%s)"),
19927c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
19937c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
19947c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action);
19957c478bd9Sstevel@tonic-gate 				goto out;
19967c478bd9Sstevel@tonic-gate 			}
19977c478bd9Sstevel@tonic-gate 			if (!zonecfg_valid_rctl(name, rctlblk)) {
19987c478bd9Sstevel@tonic-gate 				zerror(gettext("(priv=%s,limit=%s,action=%s) "
19997c478bd9Sstevel@tonic-gate 				    "is not a valid value for rctl '%s'"),
20007c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_priv,
20017c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_limit,
20027c478bd9Sstevel@tonic-gate 				    rctlval->zone_rctlval_action,
20037c478bd9Sstevel@tonic-gate 				    name);
20047c478bd9Sstevel@tonic-gate 				goto out;
20057c478bd9Sstevel@tonic-gate 			}
20067c478bd9Sstevel@tonic-gate 		}
20077c478bd9Sstevel@tonic-gate 		zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
20087c478bd9Sstevel@tonic-gate 	}
20097c478bd9Sstevel@tonic-gate 	rctltab.zone_rctl_valptr = NULL;
20107c478bd9Sstevel@tonic-gate 	error = Z_OK;
20117c478bd9Sstevel@tonic-gate out:
20127c478bd9Sstevel@tonic-gate 	zonecfg_free_rctl_value_list(rctltab.zone_rctl_valptr);
20137c478bd9Sstevel@tonic-gate 	(void) zonecfg_endrctlent(handle);
20147c478bd9Sstevel@tonic-gate 	free(rctlblk);
20157c478bd9Sstevel@tonic-gate 	return (error);
20167c478bd9Sstevel@tonic-gate }
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate static int
20197c478bd9Sstevel@tonic-gate verify_pool(zone_dochandle_t handle)
20207c478bd9Sstevel@tonic-gate {
20217c478bd9Sstevel@tonic-gate 	char poolname[MAXPATHLEN];
20227c478bd9Sstevel@tonic-gate 	pool_conf_t *poolconf;
20237c478bd9Sstevel@tonic-gate 	pool_t *pool;
20247c478bd9Sstevel@tonic-gate 	int status;
20257c478bd9Sstevel@tonic-gate 	int error;
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	/*
20287c478bd9Sstevel@tonic-gate 	 * This ends up being very similar to the check done in zoneadmd.
20297c478bd9Sstevel@tonic-gate 	 */
20307c478bd9Sstevel@tonic-gate 	error = zonecfg_get_pool(handle, poolname, sizeof (poolname));
20317c478bd9Sstevel@tonic-gate 	if (error == Z_NO_ENTRY || (error == Z_OK && strlen(poolname) == 0)) {
20327c478bd9Sstevel@tonic-gate 		/*
20337c478bd9Sstevel@tonic-gate 		 * No pool specified.
20347c478bd9Sstevel@tonic-gate 		 */
20357c478bd9Sstevel@tonic-gate 		return (0);
20367c478bd9Sstevel@tonic-gate 	}
20377c478bd9Sstevel@tonic-gate 	if (error != Z_OK) {
20387c478bd9Sstevel@tonic-gate 		zperror(gettext("Unable to retrieve pool name from "
20397c478bd9Sstevel@tonic-gate 		    "configuration"), B_TRUE);
20407c478bd9Sstevel@tonic-gate 		return (error);
20417c478bd9Sstevel@tonic-gate 	}
20427c478bd9Sstevel@tonic-gate 	/*
20437c478bd9Sstevel@tonic-gate 	 * Don't do anything if pools aren't enabled.
20447c478bd9Sstevel@tonic-gate 	 */
20457c478bd9Sstevel@tonic-gate 	if (pool_get_status(&status) != PO_SUCCESS || status != POOL_ENABLED) {
20467c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pools facility not active; "
20477c478bd9Sstevel@tonic-gate 		    "zone will not be bound to pool '%s'."), poolname);
20487c478bd9Sstevel@tonic-gate 		return (Z_OK);
20497c478bd9Sstevel@tonic-gate 	}
20507c478bd9Sstevel@tonic-gate 	/*
20517c478bd9Sstevel@tonic-gate 	 * Try to provide a sane error message if the requested pool doesn't
20527c478bd9Sstevel@tonic-gate 	 * exist.  It isn't clear that pools-related failures should
20537c478bd9Sstevel@tonic-gate 	 * necessarily translate to a failure to verify the zone configuration,
20547c478bd9Sstevel@tonic-gate 	 * hence they are not considered errors.
20557c478bd9Sstevel@tonic-gate 	 */
20567c478bd9Sstevel@tonic-gate 	if ((poolconf = pool_conf_alloc()) == NULL) {
20577c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_alloc failed; "
20587c478bd9Sstevel@tonic-gate 		    "using default pool"));
20597c478bd9Sstevel@tonic-gate 		return (Z_OK);
20607c478bd9Sstevel@tonic-gate 	}
20617c478bd9Sstevel@tonic-gate 	if (pool_conf_open(poolconf, pool_dynamic_location(), PO_RDONLY) !=
20627c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
20637c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool_conf_open failed; "
20647c478bd9Sstevel@tonic-gate 		    "using default pool"));
20657c478bd9Sstevel@tonic-gate 		pool_conf_free(poolconf);
20667c478bd9Sstevel@tonic-gate 		return (Z_OK);
20677c478bd9Sstevel@tonic-gate 	}
20687c478bd9Sstevel@tonic-gate 	pool = pool_get_pool(poolconf, poolname);
20697c478bd9Sstevel@tonic-gate 	(void) pool_conf_close(poolconf);
20707c478bd9Sstevel@tonic-gate 	pool_conf_free(poolconf);
20717c478bd9Sstevel@tonic-gate 	if (pool == NULL) {
20727c478bd9Sstevel@tonic-gate 		zerror(gettext("WARNING: pool '%s' not found. "
20737c478bd9Sstevel@tonic-gate 		    "using default pool"), poolname);
20747c478bd9Sstevel@tonic-gate 	}
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 	return (Z_OK);
20777c478bd9Sstevel@tonic-gate }
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate static int
2080b5abaf04Sgjelinek verify_ipd(zone_dochandle_t handle)
2081b5abaf04Sgjelinek {
2082b5abaf04Sgjelinek 	int return_code = Z_OK;
2083b5abaf04Sgjelinek 	struct zone_fstab fstab;
2084b5abaf04Sgjelinek 	struct stat st;
2085b5abaf04Sgjelinek 	char specdir[MAXPATHLEN];
2086b5abaf04Sgjelinek 
2087b5abaf04Sgjelinek 	if (zonecfg_setipdent(handle) != Z_OK) {
20885ee84fbdSgjelinek 		/*
20895ee84fbdSgjelinek 		 * TRANSLATION_NOTE
20905ee84fbdSgjelinek 		 * inherit-pkg-dirs is a literal that should not be translated.
20915ee84fbdSgjelinek 		 */
20925ee84fbdSgjelinek 		(void) fprintf(stderr, gettext("could not verify "
2093b5abaf04Sgjelinek 		    "inherit-pkg-dirs: unable to enumerate mounts\n"));
2094b5abaf04Sgjelinek 		return (Z_ERR);
2095b5abaf04Sgjelinek 	}
2096b5abaf04Sgjelinek 	while (zonecfg_getipdent(handle, &fstab) == Z_OK) {
2097b5abaf04Sgjelinek 		/*
2098b5abaf04Sgjelinek 		 * Verify fs_dir exists.
2099b5abaf04Sgjelinek 		 */
2100b5abaf04Sgjelinek 		(void) snprintf(specdir, sizeof (specdir), "%s%s",
2101b5abaf04Sgjelinek 		    zonecfg_get_root(), fstab.zone_fs_dir);
2102b5abaf04Sgjelinek 		if (stat(specdir, &st) != 0) {
21035ee84fbdSgjelinek 			/*
21045ee84fbdSgjelinek 			 * TRANSLATION_NOTE
21055ee84fbdSgjelinek 			 * inherit-pkg-dir is a literal that should not be
21065ee84fbdSgjelinek 			 * translated.
21075ee84fbdSgjelinek 			 */
21085ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2109b5abaf04Sgjelinek 			    "inherit-pkg-dir %s: %s\n"),
2110b5abaf04Sgjelinek 			    fstab.zone_fs_dir, strerror(errno));
2111b5abaf04Sgjelinek 			return_code = Z_ERR;
2112b5abaf04Sgjelinek 		}
2113b5abaf04Sgjelinek 		if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
21145ee84fbdSgjelinek 			/*
21155ee84fbdSgjelinek 			 * TRANSLATION_NOTE
21165ee84fbdSgjelinek 			 * inherit-pkg-dir and NFS are literals that should
21175ee84fbdSgjelinek 			 * not be translated.
21185ee84fbdSgjelinek 			 */
2119b5abaf04Sgjelinek 			(void) fprintf(stderr, gettext("cannot verify "
21200b5de56dSgjelinek 			    "inherit-pkg-dir %s: NFS mounted file system.\n"
21210b5de56dSgjelinek 			    "\tA local file system must be used.\n"),
2122b5abaf04Sgjelinek 			    fstab.zone_fs_dir);
2123b5abaf04Sgjelinek 			return_code = Z_ERR;
2124b5abaf04Sgjelinek 		}
2125b5abaf04Sgjelinek 	}
2126b5abaf04Sgjelinek 	(void) zonecfg_endipdent(handle);
2127b5abaf04Sgjelinek 
2128b5abaf04Sgjelinek 	return (return_code);
2129b5abaf04Sgjelinek }
2130b5abaf04Sgjelinek 
213120c8013fSlling /*
213220c8013fSlling  * Verify that the special device/file system exists and is valid.
213320c8013fSlling  */
213420c8013fSlling static int
213520c8013fSlling verify_fs_special(struct zone_fstab *fstab)
213620c8013fSlling {
213793239addSjohnlev 	struct stat64 st;
213820c8013fSlling 
21396cc813faSgjelinek 	/*
21406cc813faSgjelinek 	 * This validation is really intended for standard zone administration.
21416cc813faSgjelinek 	 * If we are in a mini-root or some other upgrade situation where
21426cc813faSgjelinek 	 * we are using the scratch zone, just by-pass this.
21436cc813faSgjelinek 	 */
21446cc813faSgjelinek 	if (zonecfg_in_alt_root())
21456cc813faSgjelinek 		return (Z_OK);
21466cc813faSgjelinek 
214720c8013fSlling 	if (strcmp(fstab->zone_fs_type, MNTTYPE_ZFS) == 0)
214820c8013fSlling 		return (verify_fs_zfs(fstab));
214920c8013fSlling 
215093239addSjohnlev 	if (stat64(fstab->zone_fs_special, &st) != 0) {
21515c358068Slling 		(void) fprintf(stderr, gettext("could not verify fs "
215220c8013fSlling 		    "%s: could not access %s: %s\n"), fstab->zone_fs_dir,
215320c8013fSlling 		    fstab->zone_fs_special, strerror(errno));
215420c8013fSlling 		return (Z_ERR);
215520c8013fSlling 	}
215620c8013fSlling 
215720c8013fSlling 	if (strcmp(st.st_fstype, MNTTYPE_NFS) == 0) {
215820c8013fSlling 		/*
215920c8013fSlling 		 * TRANSLATION_NOTE
216020c8013fSlling 		 * fs and NFS are literals that should
216120c8013fSlling 		 * not be translated.
216220c8013fSlling 		 */
216320c8013fSlling 		(void) fprintf(stderr, gettext("cannot verify "
21640b5de56dSgjelinek 		    "fs %s: NFS mounted file system.\n"
21650b5de56dSgjelinek 		    "\tA local file system must be used.\n"),
216620c8013fSlling 		    fstab->zone_fs_special);
216720c8013fSlling 		return (Z_ERR);
216820c8013fSlling 	}
216920c8013fSlling 
217020c8013fSlling 	return (Z_OK);
217120c8013fSlling }
217220c8013fSlling 
2173b5abaf04Sgjelinek static int
217493239addSjohnlev isregfile(const char *path)
217593239addSjohnlev {
217693239addSjohnlev 	struct stat64 st;
217793239addSjohnlev 
217893239addSjohnlev 	if (stat64(path, &st) == -1)
217993239addSjohnlev 		return (-1);
218093239addSjohnlev 
218193239addSjohnlev 	return (S_ISREG(st.st_mode));
218293239addSjohnlev }
218393239addSjohnlev 
218493239addSjohnlev static int
21857c478bd9Sstevel@tonic-gate verify_filesystems(zone_dochandle_t handle)
21867c478bd9Sstevel@tonic-gate {
21877c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
21887c478bd9Sstevel@tonic-gate 	struct zone_fstab fstab;
21897c478bd9Sstevel@tonic-gate 	char cmdbuf[MAXPATHLEN];
21907c478bd9Sstevel@tonic-gate 	struct stat st;
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	/*
21937c478bd9Sstevel@tonic-gate 	 * No need to verify inherit-pkg-dir fs types, as their type is
21947c478bd9Sstevel@tonic-gate 	 * implicitly lofs, which is known.  Therefore, the types are only
21957c478bd9Sstevel@tonic-gate 	 * verified for regular file systems below.
21967c478bd9Sstevel@tonic-gate 	 *
21977c478bd9Sstevel@tonic-gate 	 * Since the actual mount point is not known until the dependent mounts
21987c478bd9Sstevel@tonic-gate 	 * are performed, we don't attempt any path validation here: that will
21997c478bd9Sstevel@tonic-gate 	 * happen later when zoneadmd actually does the mounts.
22007c478bd9Sstevel@tonic-gate 	 */
22017c478bd9Sstevel@tonic-gate 	if (zonecfg_setfsent(handle) != Z_OK) {
22020b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not verify file systems: "
22037c478bd9Sstevel@tonic-gate 		    "unable to enumerate mounts\n"));
22047c478bd9Sstevel@tonic-gate 		return (Z_ERR);
22057c478bd9Sstevel@tonic-gate 	}
22067c478bd9Sstevel@tonic-gate 	while (zonecfg_getfsent(handle, &fstab) == Z_OK) {
22077c478bd9Sstevel@tonic-gate 		if (!zonecfg_valid_fs_type(fstab.zone_fs_type)) {
22087c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22097c478bd9Sstevel@tonic-gate 			    "type %s is not allowed.\n"), fstab.zone_fs_dir,
22107c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
22117c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22127c478bd9Sstevel@tonic-gate 			goto next_fs;
22137c478bd9Sstevel@tonic-gate 		}
22147c478bd9Sstevel@tonic-gate 		/*
22157c478bd9Sstevel@tonic-gate 		 * Verify /usr/lib/fs/<fstype>/mount exists.
22167c478bd9Sstevel@tonic-gate 		 */
22177c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/mount",
22187c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
22197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22207c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
22217c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
22227c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22237c478bd9Sstevel@tonic-gate 			goto next_fs;
22247c478bd9Sstevel@tonic-gate 		}
22257c478bd9Sstevel@tonic-gate 		if (stat(cmdbuf, &st) != 0) {
22265ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
22275ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
22287c478bd9Sstevel@tonic-gate 			    cmdbuf, strerror(errno));
22297c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22307c478bd9Sstevel@tonic-gate 			goto next_fs;
22317c478bd9Sstevel@tonic-gate 		}
22327c478bd9Sstevel@tonic-gate 		if (!S_ISREG(st.st_mode)) {
22335ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
22345ee84fbdSgjelinek 			    "%s: %s is not a regular file\n"),
22355ee84fbdSgjelinek 			    fstab.zone_fs_dir, cmdbuf);
22367c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22377c478bd9Sstevel@tonic-gate 			goto next_fs;
22387c478bd9Sstevel@tonic-gate 		}
22397c478bd9Sstevel@tonic-gate 		/*
224093239addSjohnlev 		 * If zone_fs_raw is set, verify that there's an fsck
224193239addSjohnlev 		 * binary for it.  If zone_fs_raw is not set, and it's
224293239addSjohnlev 		 * not a regular file (lofi mount), and there's an fsck
224393239addSjohnlev 		 * binary for it, complain.
22447c478bd9Sstevel@tonic-gate 		 */
22457c478bd9Sstevel@tonic-gate 		if (snprintf(cmdbuf, sizeof (cmdbuf), "/usr/lib/fs/%s/fsck",
22467c478bd9Sstevel@tonic-gate 		    fstab.zone_fs_type) > sizeof (cmdbuf)) {
22477c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22487c478bd9Sstevel@tonic-gate 			    "type %s is too long.\n"), fstab.zone_fs_dir,
22497c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_type);
22507c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22517c478bd9Sstevel@tonic-gate 			goto next_fs;
22527c478bd9Sstevel@tonic-gate 		}
22537c478bd9Sstevel@tonic-gate 		if (fstab.zone_fs_raw[0] != '\0' &&
22547c478bd9Sstevel@tonic-gate 		    (stat(cmdbuf, &st) != 0 || !S_ISREG(st.st_mode))) {
22557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("cannot verify fs %s: "
22567c478bd9Sstevel@tonic-gate 			    "'raw' device specified but "
22577c478bd9Sstevel@tonic-gate 			    "no fsck executable exists for %s\n"),
22587c478bd9Sstevel@tonic-gate 			    fstab.zone_fs_dir, fstab.zone_fs_type);
22597c478bd9Sstevel@tonic-gate 			return_code = Z_ERR;
22607c478bd9Sstevel@tonic-gate 			goto next_fs;
226193239addSjohnlev 		} else if (fstab.zone_fs_raw[0] == '\0' &&
226293239addSjohnlev 		    stat(cmdbuf, &st) == 0 &&
226393239addSjohnlev 		    isregfile(fstab.zone_fs_special) != 1) {
226493239addSjohnlev 			(void) fprintf(stderr, gettext("could not verify fs "
226593239addSjohnlev 			    "%s: must specify 'raw' device for %s "
226693239addSjohnlev 			    "file systems\n"),
226793239addSjohnlev 			    fstab.zone_fs_dir, fstab.zone_fs_type);
226893239addSjohnlev 			return_code = Z_ERR;
226993239addSjohnlev 			goto next_fs;
22707c478bd9Sstevel@tonic-gate 		}
227120c8013fSlling 
227220c8013fSlling 		/* Verify fs_special. */
227320c8013fSlling 		if ((return_code = verify_fs_special(&fstab)) != Z_OK)
2274b5abaf04Sgjelinek 			goto next_fs;
227520c8013fSlling 
227620c8013fSlling 		/* Verify fs_raw. */
2277b5abaf04Sgjelinek 		if (fstab.zone_fs_raw[0] != '\0' &&
2278b5abaf04Sgjelinek 		    stat(fstab.zone_fs_raw, &st) != 0) {
22795ee84fbdSgjelinek 			/*
22805ee84fbdSgjelinek 			 * TRANSLATION_NOTE
22815ee84fbdSgjelinek 			 * fs is a literal that should not be translated.
22825ee84fbdSgjelinek 			 */
22835ee84fbdSgjelinek 			(void) fprintf(stderr, gettext("could not verify fs "
22845ee84fbdSgjelinek 			    "%s: could not access %s: %s\n"), fstab.zone_fs_dir,
2285b5abaf04Sgjelinek 			    fstab.zone_fs_raw, strerror(errno));
2286b5abaf04Sgjelinek 			return_code = Z_ERR;
2287b5abaf04Sgjelinek 			goto next_fs;
2288b5abaf04Sgjelinek 		}
22897c478bd9Sstevel@tonic-gate next_fs:
22907c478bd9Sstevel@tonic-gate 		zonecfg_free_fs_option_list(fstab.zone_fs_options);
22917c478bd9Sstevel@tonic-gate 	}
22927c478bd9Sstevel@tonic-gate 	(void) zonecfg_endfsent(handle);
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	return (return_code);
22957c478bd9Sstevel@tonic-gate }
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate static int
2298ffbafc53Scomay verify_limitpriv(zone_dochandle_t handle)
2299ffbafc53Scomay {
2300ffbafc53Scomay 	char *privname = NULL;
2301ffbafc53Scomay 	int err;
2302ffbafc53Scomay 	priv_set_t *privs;
2303ffbafc53Scomay 
2304ffbafc53Scomay 	if ((privs = priv_allocset()) == NULL) {
2305ffbafc53Scomay 		zperror(gettext("failed to allocate privilege set"), B_FALSE);
2306ffbafc53Scomay 		return (Z_NOMEM);
2307ffbafc53Scomay 	}
2308ffbafc53Scomay 	err = zonecfg_get_privset(handle, privs, &privname);
2309ffbafc53Scomay 	switch (err) {
2310ffbafc53Scomay 	case Z_OK:
2311ffbafc53Scomay 		break;
2312ffbafc53Scomay 	case Z_PRIV_PROHIBITED:
2313ffbafc53Scomay 		(void) fprintf(stderr, gettext("privilege \"%s\" is not "
2314ffbafc53Scomay 		    "permitted within the zone's privilege set\n"), privname);
2315ffbafc53Scomay 		break;
2316ffbafc53Scomay 	case Z_PRIV_REQUIRED:
2317ffbafc53Scomay 		(void) fprintf(stderr, gettext("required privilege \"%s\" is "
2318ffbafc53Scomay 		    "missing from the zone's privilege set\n"), privname);
2319ffbafc53Scomay 		break;
2320ffbafc53Scomay 	case Z_PRIV_UNKNOWN:
2321ffbafc53Scomay 		(void) fprintf(stderr, gettext("unknown privilege \"%s\" "
2322ffbafc53Scomay 		    "specified in the zone's privilege set\n"), privname);
2323ffbafc53Scomay 		break;
2324ffbafc53Scomay 	default:
2325ffbafc53Scomay 		zperror(
2326ffbafc53Scomay 		    gettext("failed to determine the zone's privilege set"),
2327ffbafc53Scomay 		    B_TRUE);
2328ffbafc53Scomay 		break;
2329ffbafc53Scomay 	}
2330ffbafc53Scomay 	free(privname);
2331ffbafc53Scomay 	priv_freeset(privs);
2332ffbafc53Scomay 	return (err);
2333ffbafc53Scomay }
2334ffbafc53Scomay 
23351390a385Sgjelinek static void
23361390a385Sgjelinek free_local_netifs(int if_cnt, struct net_if **if_list)
23371390a385Sgjelinek {
23381390a385Sgjelinek 	int		i;
23391390a385Sgjelinek 
23401390a385Sgjelinek 	for (i = 0; i < if_cnt; i++) {
23411390a385Sgjelinek 		free(if_list[i]->name);
23421390a385Sgjelinek 		free(if_list[i]);
23431390a385Sgjelinek 	}
23441390a385Sgjelinek 	free(if_list);
23451390a385Sgjelinek }
23461390a385Sgjelinek 
23471390a385Sgjelinek /*
23481390a385Sgjelinek  * Get a list of the network interfaces, along with their address families,
23491390a385Sgjelinek  * that are plumbed in the global zone.  See if_tcp(7p) for a description
23501390a385Sgjelinek  * of the ioctls used here.
23511390a385Sgjelinek  */
23521390a385Sgjelinek static int
23531390a385Sgjelinek get_local_netifs(int *if_cnt, struct net_if ***if_list)
23541390a385Sgjelinek {
23551390a385Sgjelinek 	int		s;
23561390a385Sgjelinek 	int		i;
23571390a385Sgjelinek 	int		res = Z_OK;
23581390a385Sgjelinek 	int		space_needed;
23591390a385Sgjelinek 	int		cnt = 0;
23601390a385Sgjelinek 	struct		lifnum if_num;
23611390a385Sgjelinek 	struct		lifconf if_conf;
23621390a385Sgjelinek 	struct		lifreq *if_reqp;
23631390a385Sgjelinek 	char		*if_buf;
23641390a385Sgjelinek 	struct net_if	**local_ifs = NULL;
23651390a385Sgjelinek 
23661390a385Sgjelinek 	*if_cnt = 0;
23671390a385Sgjelinek 	*if_list = NULL;
23681390a385Sgjelinek 
23691390a385Sgjelinek 	if ((s = socket(SOCKET_AF(AF_INET), SOCK_DGRAM, 0)) < 0)
23701390a385Sgjelinek 		return (Z_ERR);
23711390a385Sgjelinek 
23721390a385Sgjelinek 	/*
23731390a385Sgjelinek 	 * Come back here in the unlikely event that the number of interfaces
23741390a385Sgjelinek 	 * increases between the time we get the count and the time we do the
23751390a385Sgjelinek 	 * SIOCGLIFCONF ioctl.
23761390a385Sgjelinek 	 */
23771390a385Sgjelinek retry:
23781390a385Sgjelinek 	/* Get the number of interfaces. */
23791390a385Sgjelinek 	if_num.lifn_family = AF_UNSPEC;
23801390a385Sgjelinek 	if_num.lifn_flags = LIFC_NOXMIT;
23811390a385Sgjelinek 	if (ioctl(s, SIOCGLIFNUM, &if_num) < 0) {
23821390a385Sgjelinek 		(void) close(s);
23831390a385Sgjelinek 		return (Z_ERR);
23841390a385Sgjelinek 	}
23851390a385Sgjelinek 
23861390a385Sgjelinek 	/* Get the interface configuration list. */
23871390a385Sgjelinek 	space_needed = if_num.lifn_count * sizeof (struct lifreq);
23881390a385Sgjelinek 	if ((if_buf = malloc(space_needed)) == NULL) {
23891390a385Sgjelinek 		(void) close(s);
23901390a385Sgjelinek 		return (Z_ERR);
23911390a385Sgjelinek 	}
23921390a385Sgjelinek 	if_conf.lifc_family = AF_UNSPEC;
23931390a385Sgjelinek 	if_conf.lifc_flags = LIFC_NOXMIT;
23941390a385Sgjelinek 	if_conf.lifc_len = space_needed;
23951390a385Sgjelinek 	if_conf.lifc_buf = if_buf;
23961390a385Sgjelinek 	if (ioctl(s, SIOCGLIFCONF, &if_conf) < 0) {
23971390a385Sgjelinek 		free(if_buf);
23981390a385Sgjelinek 		/*
23991390a385Sgjelinek 		 * SIOCGLIFCONF returns EINVAL if the buffer we passed in is
24001390a385Sgjelinek 		 * too small.  In this case go back and get the new if cnt.
24011390a385Sgjelinek 		 */
24021390a385Sgjelinek 		if (errno == EINVAL)
24031390a385Sgjelinek 			goto retry;
24041390a385Sgjelinek 
24051390a385Sgjelinek 		(void) close(s);
24061390a385Sgjelinek 		return (Z_ERR);
24071390a385Sgjelinek 	}
24081390a385Sgjelinek 	(void) close(s);
24091390a385Sgjelinek 
24101390a385Sgjelinek 	/* Get the name and address family for each interface. */
24111390a385Sgjelinek 	if_reqp = if_conf.lifc_req;
24121390a385Sgjelinek 	for (i = 0; i < (if_conf.lifc_len / sizeof (struct lifreq)); i++) {
24131390a385Sgjelinek 		struct net_if	**p;
24141390a385Sgjelinek 		struct lifreq	req;
24151390a385Sgjelinek 
24161390a385Sgjelinek 		if (strcmp(LOOPBACK_IF, if_reqp->lifr_name) == 0) {
24171390a385Sgjelinek 			if_reqp++;
24181390a385Sgjelinek 			continue;
24191390a385Sgjelinek 		}
24201390a385Sgjelinek 
24211390a385Sgjelinek 		if ((s = socket(SOCKET_AF(if_reqp->lifr_addr.ss_family),
24221390a385Sgjelinek 		    SOCK_DGRAM, 0)) == -1) {
24231390a385Sgjelinek 			res = Z_ERR;
24241390a385Sgjelinek 			break;
24251390a385Sgjelinek 		}
24261390a385Sgjelinek 
24271390a385Sgjelinek 		(void) strncpy(req.lifr_name, if_reqp->lifr_name,
24281390a385Sgjelinek 		    sizeof (req.lifr_name));
24291390a385Sgjelinek 		if (ioctl(s, SIOCGLIFADDR, &req) < 0) {
24301390a385Sgjelinek 			(void) close(s);
24311390a385Sgjelinek 			if_reqp++;
24321390a385Sgjelinek 			continue;
24331390a385Sgjelinek 		}
24341390a385Sgjelinek 
24351390a385Sgjelinek 		if ((p = (struct net_if **)realloc(local_ifs,
24361390a385Sgjelinek 		    sizeof (struct net_if *) * (cnt + 1))) == NULL) {
24371390a385Sgjelinek 			res = Z_ERR;
24381390a385Sgjelinek 			break;
24391390a385Sgjelinek 		}
24401390a385Sgjelinek 		local_ifs = p;
24411390a385Sgjelinek 
24421390a385Sgjelinek 		if ((local_ifs[cnt] = malloc(sizeof (struct net_if))) == NULL) {
24431390a385Sgjelinek 			res = Z_ERR;
24441390a385Sgjelinek 			break;
24451390a385Sgjelinek 		}
24461390a385Sgjelinek 
24471390a385Sgjelinek 		if ((local_ifs[cnt]->name = strdup(if_reqp->lifr_name))
24481390a385Sgjelinek 		    == NULL) {
24491390a385Sgjelinek 			free(local_ifs[cnt]);
24501390a385Sgjelinek 			res = Z_ERR;
24511390a385Sgjelinek 			break;
24521390a385Sgjelinek 		}
24531390a385Sgjelinek 		local_ifs[cnt]->af = req.lifr_addr.ss_family;
24541390a385Sgjelinek 		cnt++;
24551390a385Sgjelinek 
24561390a385Sgjelinek 		(void) close(s);
24571390a385Sgjelinek 		if_reqp++;
24581390a385Sgjelinek 	}
24591390a385Sgjelinek 
24601390a385Sgjelinek 	free(if_buf);
24611390a385Sgjelinek 
24621390a385Sgjelinek 	if (res != Z_OK) {
24631390a385Sgjelinek 		free_local_netifs(cnt, local_ifs);
24641390a385Sgjelinek 	} else {
24651390a385Sgjelinek 		*if_cnt = cnt;
24661390a385Sgjelinek 		*if_list = local_ifs;
24671390a385Sgjelinek 	}
24681390a385Sgjelinek 
24691390a385Sgjelinek 	return (res);
24701390a385Sgjelinek }
24711390a385Sgjelinek 
24721390a385Sgjelinek static char *
24731390a385Sgjelinek af2str(int af)
24741390a385Sgjelinek {
24751390a385Sgjelinek 	switch (af) {
24761390a385Sgjelinek 	case AF_INET:
24771390a385Sgjelinek 		return ("IPv4");
24781390a385Sgjelinek 	case AF_INET6:
24791390a385Sgjelinek 		return ("IPv6");
24801390a385Sgjelinek 	default:
24811390a385Sgjelinek 		return ("Unknown");
24821390a385Sgjelinek 	}
24831390a385Sgjelinek }
24841390a385Sgjelinek 
24851390a385Sgjelinek /*
24861390a385Sgjelinek  * Cross check the network interface name and address family with the
24871390a385Sgjelinek  * interfaces that are set up in the global zone so that we can print the
24881390a385Sgjelinek  * appropriate error message.
24891390a385Sgjelinek  */
24901390a385Sgjelinek static void
24911390a385Sgjelinek print_net_err(char *phys, char *addr, int af, char *msg)
24921390a385Sgjelinek {
24931390a385Sgjelinek 	int		i;
24941390a385Sgjelinek 	int		local_if_cnt = 0;
24951390a385Sgjelinek 	struct net_if	**local_ifs = NULL;
24961390a385Sgjelinek 	boolean_t	found_if = B_FALSE;
24971390a385Sgjelinek 	boolean_t	found_af = B_FALSE;
24981390a385Sgjelinek 
24991390a385Sgjelinek 	if (get_local_netifs(&local_if_cnt, &local_ifs) != Z_OK) {
25001390a385Sgjelinek 		(void) fprintf(stderr,
25011390a385Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
25021390a385Sgjelinek 		    "net", "address", addr, "physical", phys, msg);
25031390a385Sgjelinek 		return;
25041390a385Sgjelinek 	}
25051390a385Sgjelinek 
25061390a385Sgjelinek 	for (i = 0; i < local_if_cnt; i++) {
25071390a385Sgjelinek 		if (strcmp(phys, local_ifs[i]->name) == 0) {
25081390a385Sgjelinek 			found_if = B_TRUE;
25091390a385Sgjelinek 			if (af == local_ifs[i]->af) {
25101390a385Sgjelinek 				found_af = B_TRUE;
25111390a385Sgjelinek 				break;
25121390a385Sgjelinek 			}
25131390a385Sgjelinek 		}
25141390a385Sgjelinek 	}
25151390a385Sgjelinek 
25161390a385Sgjelinek 	free_local_netifs(local_if_cnt, local_ifs);
25171390a385Sgjelinek 
25181390a385Sgjelinek 	if (!found_if) {
25191390a385Sgjelinek 		(void) fprintf(stderr,
25201390a385Sgjelinek 		    gettext("could not verify %s %s=%s\n\t"
25211390a385Sgjelinek 		    "network interface %s is not plumbed in the global zone\n"),
25221390a385Sgjelinek 		    "net", "physical", phys, phys);
25231390a385Sgjelinek 		return;
25241390a385Sgjelinek 	}
25251390a385Sgjelinek 
25261390a385Sgjelinek 	/*
25271390a385Sgjelinek 	 * Print this error if we were unable to find the address family
25281390a385Sgjelinek 	 * for this interface.  If the af variable is not initialized to
25291390a385Sgjelinek 	 * to something meaningful by the caller (not AF_UNSPEC) then we
25301390a385Sgjelinek 	 * also skip this message since it wouldn't be informative.
25311390a385Sgjelinek 	 */
25321390a385Sgjelinek 	if (!found_af && af != AF_UNSPEC) {
25331390a385Sgjelinek 		(void) fprintf(stderr,
25341390a385Sgjelinek 		    gettext("could not verify %s %s=%s %s=%s\n\tthe %s address "
2535f4b3ec61Sdh155122 		    "family is not configured on this network interface in "
2536f4b3ec61Sdh155122 		    "the\n\tglobal zone\n"),
25371390a385Sgjelinek 		    "net", "address", addr, "physical", phys, af2str(af));
25381390a385Sgjelinek 		return;
25391390a385Sgjelinek 	}
25401390a385Sgjelinek 
25411390a385Sgjelinek 	(void) fprintf(stderr,
25421390a385Sgjelinek 	    gettext("could not verify %s %s=%s %s=%s\n\t%s\n"),
25431390a385Sgjelinek 	    "net", "address", addr, "physical", phys, msg);
25441390a385Sgjelinek }
25451390a385Sgjelinek 
2546ffbafc53Scomay static int
2547ce28b40eSzt129084 verify_handle(int cmd_num, zone_dochandle_t handle, char *argv[])
25487c478bd9Sstevel@tonic-gate {
25497c478bd9Sstevel@tonic-gate 	struct zone_nwiftab nwiftab;
25507c478bd9Sstevel@tonic-gate 	int return_code = Z_OK;
25517c478bd9Sstevel@tonic-gate 	int err;
2552108322fbScarlsonj 	boolean_t in_alt_root;
2553f4b3ec61Sdh155122 	zone_iptype_t iptype;
25542b24ab6bSSebastien Roy 	dladm_handle_t dh;
25552b24ab6bSSebastien Roy 	dladm_status_t status;
25562b24ab6bSSebastien Roy 	datalink_id_t linkid;
25572b24ab6bSSebastien Roy 	char errmsg[DLADM_STRSIZE];
25587c478bd9Sstevel@tonic-gate 
2559108322fbScarlsonj 	in_alt_root = zonecfg_in_alt_root();
2560108322fbScarlsonj 	if (in_alt_root)
2561108322fbScarlsonj 		goto no_net;
2562108322fbScarlsonj 
2563f4b3ec61Sdh155122 	if ((err = zonecfg_get_iptype(handle, &iptype)) != Z_OK) {
2564f4b3ec61Sdh155122 		errno = err;
2565f4b3ec61Sdh155122 		zperror(cmd_to_str(cmd_num), B_TRUE);
2566f4b3ec61Sdh155122 		zonecfg_fini_handle(handle);
2567f4b3ec61Sdh155122 		return (Z_ERR);
2568f4b3ec61Sdh155122 	}
25697c478bd9Sstevel@tonic-gate 	if ((err = zonecfg_setnwifent(handle)) != Z_OK) {
25707c478bd9Sstevel@tonic-gate 		errno = err;
25717c478bd9Sstevel@tonic-gate 		zperror(cmd_to_str(cmd_num), B_TRUE);
25727c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(handle);
25737c478bd9Sstevel@tonic-gate 		return (Z_ERR);
25747c478bd9Sstevel@tonic-gate 	}
25757c478bd9Sstevel@tonic-gate 	while (zonecfg_getnwifent(handle, &nwiftab) == Z_OK) {
25767c478bd9Sstevel@tonic-gate 		struct lifreq lifr;
25771390a385Sgjelinek 		sa_family_t af = AF_UNSPEC;
2578f4b3ec61Sdh155122 		char dl_owner_zname[ZONENAME_MAX];
2579f4b3ec61Sdh155122 		zoneid_t dl_owner_zid;
2580f4b3ec61Sdh155122 		zoneid_t target_zid;
2581f4b3ec61Sdh155122 		int res;
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate 		/* skip any loopback interfaces */
25847c478bd9Sstevel@tonic-gate 		if (strcmp(nwiftab.zone_nwif_physical, "lo0") == 0)
25857c478bd9Sstevel@tonic-gate 			continue;
2586f4b3ec61Sdh155122 		switch (iptype) {
2587f4b3ec61Sdh155122 		case ZS_SHARED:
2588f4b3ec61Sdh155122 			if ((res = zonecfg_valid_net_address(
2589f4b3ec61Sdh155122 			    nwiftab.zone_nwif_address, &lifr)) != Z_OK) {
25901390a385Sgjelinek 				print_net_err(nwiftab.zone_nwif_physical,
25911390a385Sgjelinek 				    nwiftab.zone_nwif_address, af,
25921390a385Sgjelinek 				    zonecfg_strerror(res));
25937c478bd9Sstevel@tonic-gate 				return_code = Z_ERR;
25947c478bd9Sstevel@tonic-gate 				continue;
25957c478bd9Sstevel@tonic-gate 			}
25967c478bd9Sstevel@tonic-gate 			af = lifr.lifr_addr.ss_family;
2597f4b3ec61Sdh155122 			if (!zonecfg_ifname_exists(af,
2598f4b3ec61Sdh155122 			    nwiftab.zone_nwif_physical)) {
259922321485Svp157776 				/*
2600f4b3ec61Sdh155122 				 * The interface failed to come up. We continue
2601f4b3ec61Sdh155122 				 * on anyway for the sake of consistency: a
2602f4b3ec61Sdh155122 				 * zone is not shut down if the interface fails
2603f4b3ec61Sdh155122 				 * any time after boot, nor does the global zone
2604f4b3ec61Sdh155122 				 * fail to boot if an interface fails.
260522321485Svp157776 				 */
260622321485Svp157776 				(void) fprintf(stderr,
2607f4b3ec61Sdh155122 				    gettext("WARNING: skipping network "
2608f4b3ec61Sdh155122 				    "interface '%s' which may not be "
2609f4b3ec61Sdh155122 				    "present/plumbed in the global "
2610f4b3ec61Sdh155122 				    "zone.\n"),
261122321485Svp157776 				    nwiftab.zone_nwif_physical);
26127c478bd9Sstevel@tonic-gate 			}
2613f4b3ec61Sdh155122 			break;
2614f4b3ec61Sdh155122 		case ZS_EXCLUSIVE:
2615f4b3ec61Sdh155122 			/* Warning if it exists for either IPv4 or IPv6 */
2616f4b3ec61Sdh155122 
2617f4b3ec61Sdh155122 			if (zonecfg_ifname_exists(AF_INET,
2618f4b3ec61Sdh155122 			    nwiftab.zone_nwif_physical) ||
2619f4b3ec61Sdh155122 			    zonecfg_ifname_exists(AF_INET6,
2620f4b3ec61Sdh155122 			    nwiftab.zone_nwif_physical)) {
2621f4b3ec61Sdh155122 				(void) fprintf(stderr,
2622f4b3ec61Sdh155122 				    gettext("WARNING: skipping network "
2623f4b3ec61Sdh155122 				    "interface '%s' which is used in the "
2624f4b3ec61Sdh155122 				    "global zone.\n"),
2625f4b3ec61Sdh155122 				    nwiftab.zone_nwif_physical);
2626f4b3ec61Sdh155122 				break;
2627f4b3ec61Sdh155122 			}
2628948f2876Sss150715 
2629f4b3ec61Sdh155122 			/*
26302b24ab6bSSebastien Roy 			 * Verify that the datalink exists and that it isn't
26312b24ab6bSSebastien Roy 			 * already assigned to a zone.
2632f4b3ec61Sdh155122 			 */
26332b24ab6bSSebastien Roy 			if ((status = dladm_open(&dh)) == DLADM_STATUS_OK) {
26342b24ab6bSSebastien Roy 				status = dladm_name2info(dh,
26352b24ab6bSSebastien Roy 				    nwiftab.zone_nwif_physical, &linkid, NULL,
26362b24ab6bSSebastien Roy 				    NULL, NULL);
26372b24ab6bSSebastien Roy 				dladm_close(dh);
26382b24ab6bSSebastien Roy 			}
26392b24ab6bSSebastien Roy 			if (status != DLADM_STATUS_OK) {
2640f4b3ec61Sdh155122 				(void) fprintf(stderr,
2641f4b3ec61Sdh155122 				    gettext("WARNING: skipping network "
26422b24ab6bSSebastien Roy 				    "interface '%s': %s\n"),
2643948f2876Sss150715 				    nwiftab.zone_nwif_physical,
26442b24ab6bSSebastien Roy 				    dladm_status2str(status, errmsg));
2645f4b3ec61Sdh155122 				break;
2646f4b3ec61Sdh155122 			}
2647f4b3ec61Sdh155122 			dl_owner_zid = ALL_ZONES;
26482b24ab6bSSebastien Roy 			if (zone_check_datalink(&dl_owner_zid, linkid) != 0)
2649f4b3ec61Sdh155122 				break;
2650f4b3ec61Sdh155122 
2651f4b3ec61Sdh155122 			/*
2652f4b3ec61Sdh155122 			 * If the zone being verified is
2653f4b3ec61Sdh155122 			 * running and owns the interface
2654f4b3ec61Sdh155122 			 */
2655f4b3ec61Sdh155122 			target_zid = getzoneidbyname(target_zone);
2656f4b3ec61Sdh155122 			if (target_zid == dl_owner_zid)
2657f4b3ec61Sdh155122 				break;
2658f4b3ec61Sdh155122 
2659f4b3ec61Sdh155122 			/* Zone id match failed, use name to check */
2660f4b3ec61Sdh155122 			if (getzonenamebyid(dl_owner_zid, dl_owner_zname,
2661f4b3ec61Sdh155122 			    ZONENAME_MAX) < 0) {
2662f4b3ec61Sdh155122 				/* No name, show ID instead */
2663f4b3ec61Sdh155122 				(void) snprintf(dl_owner_zname, ZONENAME_MAX,
2664f4b3ec61Sdh155122 				    "<%d>", dl_owner_zid);
2665f4b3ec61Sdh155122 			} else if (strcmp(dl_owner_zname, target_zone) == 0)
2666f4b3ec61Sdh155122 				break;
2667f4b3ec61Sdh155122 
2668f4b3ec61Sdh155122 			/*
2669f4b3ec61Sdh155122 			 * Note here we only report a warning that
2670f4b3ec61Sdh155122 			 * the interface is already in use by another
2671f4b3ec61Sdh155122 			 * running zone, and the verify process just
2672f4b3ec61Sdh155122 			 * goes on, if the interface is still in use
2673f4b3ec61Sdh155122 			 * when this zone really boots up, zoneadmd
2674f4b3ec61Sdh155122 			 * will find it. If the name of the zone which
2675f4b3ec61Sdh155122 			 * owns this interface cannot be determined,
2676f4b3ec61Sdh155122 			 * then it is not possible to determine if there
2677f4b3ec61Sdh155122 			 * is a conflict so just report it as a warning.
2678f4b3ec61Sdh155122 			 */
2679f4b3ec61Sdh155122 			(void) fprintf(stderr,
2680f4b3ec61Sdh155122 			    gettext("WARNING: skipping network interface "
2681f4b3ec61Sdh155122 			    "'%s' which is used by the non-global zone "
2682f4b3ec61Sdh155122 			    "'%s'.\n"), nwiftab.zone_nwif_physical,
2683f4b3ec61Sdh155122 			    dl_owner_zname);
2684f4b3ec61Sdh155122 			break;
2685f4b3ec61Sdh155122 		}
26867c478bd9Sstevel@tonic-gate 	}
26877c478bd9Sstevel@tonic-gate 	(void) zonecfg_endnwifent(handle);
2688108322fbScarlsonj no_net:
26897c478bd9Sstevel@tonic-gate 
2690e7f3c547Sgjelinek 	/* verify that lofs has not been excluded from the kernel */
26918cd327d5Sgjelinek 	if (!(cmd_num == CMD_DETACH || cmd_num == CMD_ATTACH ||
26928cd327d5Sgjelinek 	    cmd_num == CMD_MOVE || cmd_num == CMD_CLONE) &&
26938cd327d5Sgjelinek 	    modctl(MODLOAD, 1, "fs/lofs", NULL) != 0) {
2694e7f3c547Sgjelinek 		if (errno == ENXIO)
2695e7f3c547Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2696e7f3c547Sgjelinek 			    "lofs(7FS): possibly excluded in /etc/system\n"));
2697e7f3c547Sgjelinek 		else
2698e7f3c547Sgjelinek 			(void) fprintf(stderr, gettext("could not verify "
2699e7f3c547Sgjelinek 			    "lofs(7FS): %s\n"), strerror(errno));
2700e7f3c547Sgjelinek 		return_code = Z_ERR;
2701e7f3c547Sgjelinek 	}
2702e7f3c547Sgjelinek 
27037c478bd9Sstevel@tonic-gate 	if (verify_filesystems(handle) != Z_OK)
27047c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2705b5abaf04Sgjelinek 	if (verify_ipd(handle) != Z_OK)
2706b5abaf04Sgjelinek 		return_code = Z_ERR;
2707108322fbScarlsonj 	if (!in_alt_root && verify_rctls(handle) != Z_OK)
27087c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2709108322fbScarlsonj 	if (!in_alt_root && verify_pool(handle) != Z_OK)
27107c478bd9Sstevel@tonic-gate 		return_code = Z_ERR;
2711ce28b40eSzt129084 	if (!in_alt_root && verify_brand(handle, cmd_num, argv) != Z_OK)
27129acbbeafSnn35248 		return_code = Z_ERR;
2713fa9e4066Sahrens 	if (!in_alt_root && verify_datasets(handle) != Z_OK)
2714fa9e4066Sahrens 		return_code = Z_ERR;
2715ffbafc53Scomay 
2716ffbafc53Scomay 	/*
2717ffbafc53Scomay 	 * As the "mount" command is used for patching/upgrading of zones
2718ffbafc53Scomay 	 * or other maintenance processes, the zone's privilege set is not
2719ffbafc53Scomay 	 * checked in this case.  Instead, the default, safe set of
2720ffbafc53Scomay 	 * privileges will be used when this zone is created in the
2721ffbafc53Scomay 	 * kernel.
2722ffbafc53Scomay 	 */
2723ffbafc53Scomay 	if (!in_alt_root && cmd_num != CMD_MOUNT &&
2724ffbafc53Scomay 	    verify_limitpriv(handle) != Z_OK)
2725ffbafc53Scomay 		return_code = Z_ERR;
27268cd327d5Sgjelinek 
27278cd327d5Sgjelinek 	return (return_code);
27288cd327d5Sgjelinek }
27298cd327d5Sgjelinek 
27308cd327d5Sgjelinek static int
2731ce28b40eSzt129084 verify_details(int cmd_num, char *argv[])
27328cd327d5Sgjelinek {
27338cd327d5Sgjelinek 	zone_dochandle_t handle;
27348cd327d5Sgjelinek 	char zonepath[MAXPATHLEN], checkpath[MAXPATHLEN];
27358cd327d5Sgjelinek 	int return_code = Z_OK;
27368cd327d5Sgjelinek 	int err;
27378cd327d5Sgjelinek 
27388cd327d5Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
27398cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27408cd327d5Sgjelinek 		return (Z_ERR);
27418cd327d5Sgjelinek 	}
27428cd327d5Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
27438cd327d5Sgjelinek 		errno = err;
27448cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27458cd327d5Sgjelinek 		zonecfg_fini_handle(handle);
27468cd327d5Sgjelinek 		return (Z_ERR);
27478cd327d5Sgjelinek 	}
27488cd327d5Sgjelinek 	if ((err = zonecfg_get_zonepath(handle, zonepath, sizeof (zonepath))) !=
27498cd327d5Sgjelinek 	    Z_OK) {
27508cd327d5Sgjelinek 		errno = err;
27518cd327d5Sgjelinek 		zperror(cmd_to_str(cmd_num), B_TRUE);
27528cd327d5Sgjelinek 		zonecfg_fini_handle(handle);
27538cd327d5Sgjelinek 		return (Z_ERR);
27548cd327d5Sgjelinek 	}
27558cd327d5Sgjelinek 	/*
27568cd327d5Sgjelinek 	 * zonecfg_get_zonepath() gets its data from the XML repository.
27578cd327d5Sgjelinek 	 * Verify this against the index file, which is checked first by
27588cd327d5Sgjelinek 	 * zone_get_zonepath().  If they don't match, bail out.
27598cd327d5Sgjelinek 	 */
27608cd327d5Sgjelinek 	if ((err = zone_get_zonepath(target_zone, checkpath,
27618cd327d5Sgjelinek 	    sizeof (checkpath))) != Z_OK) {
27628cd327d5Sgjelinek 		errno = err;
27638cd327d5Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
2764e767a340Sgjelinek 		zonecfg_fini_handle(handle);
27658cd327d5Sgjelinek 		return (Z_ERR);
27668cd327d5Sgjelinek 	}
27678cd327d5Sgjelinek 	if (strcmp(zonepath, checkpath) != 0) {
27688cd327d5Sgjelinek 		/*
27698cd327d5Sgjelinek 		 * TRANSLATION_NOTE
27708cd327d5Sgjelinek 		 * XML and zonepath are literals that should not be translated.
27718cd327d5Sgjelinek 		 */
27728cd327d5Sgjelinek 		(void) fprintf(stderr, gettext("The XML repository has "
27738cd327d5Sgjelinek 		    "zonepath '%s',\nbut the index file has zonepath '%s'.\n"
27748cd327d5Sgjelinek 		    "These must match, so fix the incorrect entry.\n"),
27758cd327d5Sgjelinek 		    zonepath, checkpath);
2776e767a340Sgjelinek 		zonecfg_fini_handle(handle);
27778cd327d5Sgjelinek 		return (Z_ERR);
27788cd327d5Sgjelinek 	}
2779ca733e25S 	if (cmd_num != CMD_ATTACH &&
2780ca733e25S 	    validate_zonepath(zonepath, cmd_num) != Z_OK) {
27818cd327d5Sgjelinek 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
27828cd327d5Sgjelinek 		    "because of the above errors.\n"), zonepath);
27838cd327d5Sgjelinek 		return_code = Z_ERR;
27848cd327d5Sgjelinek 	}
27858cd327d5Sgjelinek 
2786ce28b40eSzt129084 	if (verify_handle(cmd_num, handle, argv) != Z_OK)
27878cd327d5Sgjelinek 		return_code = Z_ERR;
27888cd327d5Sgjelinek 
27897c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(handle);
27907c478bd9Sstevel@tonic-gate 	if (return_code == Z_ERR)
27917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
27927c478bd9Sstevel@tonic-gate 		    gettext("%s: zone %s failed to verify\n"),
27937c478bd9Sstevel@tonic-gate 		    execname, target_zone);
27947c478bd9Sstevel@tonic-gate 	return (return_code);
27957c478bd9Sstevel@tonic-gate }
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate static int
27987c478bd9Sstevel@tonic-gate verify_func(int argc, char *argv[])
27997c478bd9Sstevel@tonic-gate {
28007c478bd9Sstevel@tonic-gate 	int arg;
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	optind = 0;
28037c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
28047c478bd9Sstevel@tonic-gate 		switch (arg) {
28057c478bd9Sstevel@tonic-gate 		case '?':
28067c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28077c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
28087c478bd9Sstevel@tonic-gate 		default:
28097c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_VERIFY, CMD_VERIFY);
28107c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
28117c478bd9Sstevel@tonic-gate 		}
28127c478bd9Sstevel@tonic-gate 	}
28137c478bd9Sstevel@tonic-gate 	if (argc > optind) {
28147c478bd9Sstevel@tonic-gate 		sub_usage(SHELP_VERIFY, CMD_VERIFY);
28157c478bd9Sstevel@tonic-gate 		return (Z_USAGE);
28167c478bd9Sstevel@tonic-gate 	}
28179acbbeafSnn35248 	if (sanity_check(target_zone, CMD_VERIFY, B_FALSE, B_FALSE, B_FALSE)
28189acbbeafSnn35248 	    != Z_OK)
28197c478bd9Sstevel@tonic-gate 		return (Z_ERR);
2820ce28b40eSzt129084 	return (verify_details(CMD_VERIFY, argv));
28217c478bd9Sstevel@tonic-gate }
28227c478bd9Sstevel@tonic-gate 
28239acbbeafSnn35248 static int
2824ff17c8bfSgjelinek addoptions(char *buf, char *argv[], size_t len)
2825ff17c8bfSgjelinek {
2826ff17c8bfSgjelinek 	int i = 0;
2827ff17c8bfSgjelinek 
2828ff17c8bfSgjelinek 	if (buf[0] == '\0')
2829ff17c8bfSgjelinek 		return (Z_OK);
2830ff17c8bfSgjelinek 
2831ff17c8bfSgjelinek 	while (argv[i] != NULL) {
2832ff17c8bfSgjelinek 		if (strlcat(buf, " ", len) >= len ||
2833ff17c8bfSgjelinek 		    strlcat(buf, argv[i++], len) >= len) {
2834ff17c8bfSgjelinek 			zerror("Command line too long");
2835ff17c8bfSgjelinek 			return (Z_ERR);
2836ff17c8bfSgjelinek 		}
2837ff17c8bfSgjelinek 	}
2838ff17c8bfSgjelinek 
2839ff17c8bfSgjelinek 	return (Z_OK);
2840ff17c8bfSgjelinek }
2841ff17c8bfSgjelinek 
2842ff17c8bfSgjelinek static int
28439acbbeafSnn35248 addopt(char *buf, int opt, char *optarg, size_t bufsize)
28449acbbeafSnn35248 {
28459acbbeafSnn35248 	char optstring[4];
28469acbbeafSnn35248 
28479acbbeafSnn35248 	if (opt > 0)
28489acbbeafSnn35248 		(void) sprintf(optstring, " -%c", opt);
28499acbbeafSnn35248 	else
28509acbbeafSnn35248 		(void) strcpy(optstring, " ");
28519acbbeafSnn35248 
28529acbbeafSnn35248 	if ((strlcat(buf, optstring, bufsize) > bufsize))
28539acbbeafSnn35248 		return (Z_ERR);
2854ff17c8bfSgjelinek 
28559acbbeafSnn35248 	if ((optarg != NULL) && (strlcat(buf, optarg, bufsize) > bufsize))
28569acbbeafSnn35248 		return (Z_ERR);
2857ff17c8bfSgjelinek 
28589acbbeafSnn35248 	return (Z_OK);
28599acbbeafSnn35248 }
28607c478bd9Sstevel@tonic-gate 
2861ff17c8bfSgjelinek /* ARGSUSED */
28627c478bd9Sstevel@tonic-gate static int
28637c478bd9Sstevel@tonic-gate install_func(int argc, char *argv[])
28647c478bd9Sstevel@tonic-gate {
28659acbbeafSnn35248 	char cmdbuf[MAXPATHLEN];
28661100f00dSgjelinek 	char postcmdbuf[MAXPATHLEN];
28677c478bd9Sstevel@tonic-gate 	int lockfd;
28689acbbeafSnn35248 	int arg, err, subproc_err;
28697c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
2870123807fbSedp 	brand_handle_t bh = NULL;
28717c478bd9Sstevel@tonic-gate 	int status;
28721100f00dSgjelinek 	boolean_t do_postinstall = B_FALSE;
2873ff17c8bfSgjelinek 	boolean_t brand_help = B_FALSE;
28749acbbeafSnn35248 	char opts[128];
28759acbbeafSnn35248 
28769acbbeafSnn35248 	if (target_zone == NULL) {
28779acbbeafSnn35248 		sub_usage(SHELP_INSTALL, CMD_INSTALL);
28789acbbeafSnn35248 		return (Z_USAGE);
28799acbbeafSnn35248 	}
28807c478bd9Sstevel@tonic-gate 
2881108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
2882108322fbScarlsonj 		zerror(gettext("cannot install zone in alternate root"));
2883108322fbScarlsonj 		return (Z_ERR);
2884108322fbScarlsonj 	}
2885108322fbScarlsonj 
28869acbbeafSnn35248 	if ((err = zone_get_zonepath(target_zone, zonepath,
28879acbbeafSnn35248 	    sizeof (zonepath))) != Z_OK) {
28889acbbeafSnn35248 		errno = err;
28899acbbeafSnn35248 		zperror2(target_zone, gettext("could not get zone path"));
28909acbbeafSnn35248 		return (Z_ERR);
28919acbbeafSnn35248 	}
28929acbbeafSnn35248 
28939acbbeafSnn35248 	/* Fetch the install command from the brand configuration.  */
2894123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
28959acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
28969acbbeafSnn35248 		return (Z_ERR);
28979acbbeafSnn35248 	}
28989acbbeafSnn35248 
2899ff17c8bfSgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_install,
2900ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
29019acbbeafSnn35248 		zerror("invalid brand configuration: missing install resource");
2902123807fbSedp 		brand_close(bh);
29039acbbeafSnn35248 		return (Z_ERR);
29049acbbeafSnn35248 	}
29059acbbeafSnn35248 
2906ff17c8bfSgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postinstall,
2907ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
29081100f00dSgjelinek 		zerror("invalid brand configuration: missing postinstall "
29091100f00dSgjelinek 		    "resource");
29101100f00dSgjelinek 		brand_close(bh);
29111100f00dSgjelinek 		return (Z_ERR);
29121100f00dSgjelinek 	}
29131100f00dSgjelinek 
2914ff17c8bfSgjelinek 	if (postcmdbuf[0] != '\0')
2915ff17c8bfSgjelinek 		do_postinstall = B_TRUE;
2916ff17c8bfSgjelinek 
29179acbbeafSnn35248 	(void) strcpy(opts, "?x:");
29189acbbeafSnn35248 	/*
29199acbbeafSnn35248 	 * Fetch the list of recognized command-line options from
29209acbbeafSnn35248 	 * the brand configuration file.
29219acbbeafSnn35248 	 */
2922123807fbSedp 	if (brand_get_installopts(bh, opts + strlen(opts),
29239acbbeafSnn35248 	    sizeof (opts) - strlen(opts)) != 0) {
29249acbbeafSnn35248 		zerror("invalid brand configuration: missing "
29259acbbeafSnn35248 		    "install options resource");
2926123807fbSedp 		brand_close(bh);
29279acbbeafSnn35248 		return (Z_ERR);
29289acbbeafSnn35248 	}
2929ff17c8bfSgjelinek 
2930123807fbSedp 	brand_close(bh);
29319acbbeafSnn35248 
2932ff17c8bfSgjelinek 	if (cmdbuf[0] == '\0') {
2933ff17c8bfSgjelinek 		zerror("Missing brand install command");
2934ff17c8bfSgjelinek 		return (Z_ERR);
2935ff17c8bfSgjelinek 	}
2936ff17c8bfSgjelinek 
2937ff17c8bfSgjelinek 	/* Check the argv string for args we handle internally */
29387c478bd9Sstevel@tonic-gate 	optind = 0;
2939ff17c8bfSgjelinek 	opterr = 0;
29409acbbeafSnn35248 	while ((arg = getopt(argc, argv, opts)) != EOF) {
29417c478bd9Sstevel@tonic-gate 		switch (arg) {
29427c478bd9Sstevel@tonic-gate 		case '?':
2943ff17c8bfSgjelinek 			if (optopt == '?') {
29447c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_INSTALL, CMD_INSTALL);
2945ff17c8bfSgjelinek 				brand_help = B_TRUE;
29460b5de56dSgjelinek 			}
2947ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
29480b5de56dSgjelinek 			break;
29497c478bd9Sstevel@tonic-gate 		default:
2950ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
2951ff17c8bfSgjelinek 			break;
29527c478bd9Sstevel@tonic-gate 		}
29539acbbeafSnn35248 
29549acbbeafSnn35248 		/*
2955ff17c8bfSgjelinek 		 * Append the option to the command line passed to the
2956ff17c8bfSgjelinek 		 * brand-specific install and postinstall routines.
29579acbbeafSnn35248 		 */
2958ff17c8bfSgjelinek 		if (addopt(cmdbuf, optopt, optarg, sizeof (cmdbuf)) != Z_OK) {
29599acbbeafSnn35248 			zerror("Install command line too long");
29609acbbeafSnn35248 			return (Z_ERR);
29617c478bd9Sstevel@tonic-gate 		}
2962ff17c8bfSgjelinek 		if (addopt(postcmdbuf, optopt, optarg, sizeof (postcmdbuf))
2963ff17c8bfSgjelinek 		    != Z_OK) {
29641100f00dSgjelinek 			zerror("Post-Install command line too long");
29651100f00dSgjelinek 			return (Z_ERR);
29661100f00dSgjelinek 		}
29679acbbeafSnn35248 	}
29689acbbeafSnn35248 
29699acbbeafSnn35248 	for (; optind < argc; optind++) {
2970ff17c8bfSgjelinek 		if (addopt(cmdbuf, 0, argv[optind], sizeof (cmdbuf)) != Z_OK) {
29719acbbeafSnn35248 			zerror("Install command line too long");
29729acbbeafSnn35248 			return (Z_ERR);
29739acbbeafSnn35248 		}
2974ff17c8bfSgjelinek 
2975ff17c8bfSgjelinek 		if (addopt(postcmdbuf, 0, argv[optind], sizeof (postcmdbuf))
2976ff17c8bfSgjelinek 		    != Z_OK) {
29771100f00dSgjelinek 			zerror("Post-Install command line too long");
29781100f00dSgjelinek 			return (Z_ERR);
29791100f00dSgjelinek 		}
29809acbbeafSnn35248 	}
29819acbbeafSnn35248 
2982ff17c8bfSgjelinek 	if (!brand_help) {
2983ff17c8bfSgjelinek 		if (sanity_check(target_zone, CMD_INSTALL, B_FALSE, B_TRUE,
2984ff17c8bfSgjelinek 		    B_FALSE) != Z_OK)
29857c478bd9Sstevel@tonic-gate 			return (Z_ERR);
2986ce28b40eSzt129084 		if (verify_details(CMD_INSTALL, argv) != Z_OK)
29877c478bd9Sstevel@tonic-gate 			return (Z_ERR);
29887c478bd9Sstevel@tonic-gate 
2989ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
2990ff17c8bfSgjelinek 			zerror(gettext("another %s may have an operation in "
2991ff17c8bfSgjelinek 			    "progress."), "zoneadm");
29927c478bd9Sstevel@tonic-gate 			return (Z_ERR);
29937c478bd9Sstevel@tonic-gate 		}
29947c478bd9Sstevel@tonic-gate 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
29957c478bd9Sstevel@tonic-gate 		if (err != Z_OK) {
29967c478bd9Sstevel@tonic-gate 			errno = err;
29977c478bd9Sstevel@tonic-gate 			zperror2(target_zone, gettext("could not set state"));
29987c478bd9Sstevel@tonic-gate 			goto done;
29997c478bd9Sstevel@tonic-gate 		}
30007c478bd9Sstevel@tonic-gate 
30019acbbeafSnn35248 		create_zfs_zonepath(zonepath);
30027c478bd9Sstevel@tonic-gate 	}
30037c478bd9Sstevel@tonic-gate 
3004c75cc341S 	status = do_subproc(cmdbuf);
30059acbbeafSnn35248 	if ((subproc_err =
30069acbbeafSnn35248 	    subproc_status(gettext("brand-specific installation"), status,
30079acbbeafSnn35248 	    B_FALSE)) != ZONE_SUBPROC_OK) {
3008ff17c8bfSgjelinek 		if (subproc_err == ZONE_SUBPROC_USAGE && !brand_help) {
3009ff17c8bfSgjelinek 			sub_usage(SHELP_INSTALL, CMD_INSTALL);
3010ff17c8bfSgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
3011ff17c8bfSgjelinek 			return (Z_ERR);
3012ff17c8bfSgjelinek 		}
3013ad02e316Sbatschul 		errno = subproc_err;
30149acbbeafSnn35248 		err = Z_ERR;
30157c478bd9Sstevel@tonic-gate 		goto done;
30169acbbeafSnn35248 	}
30177c478bd9Sstevel@tonic-gate 
3018ff17c8bfSgjelinek 	if (brand_help)
3019ff17c8bfSgjelinek 		return (Z_OK);
3020ff17c8bfSgjelinek 
30217c478bd9Sstevel@tonic-gate 	if ((err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
30227c478bd9Sstevel@tonic-gate 		errno = err;
30237c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not set state"));
30247c478bd9Sstevel@tonic-gate 		goto done;
30257c478bd9Sstevel@tonic-gate 	}
30267c478bd9Sstevel@tonic-gate 
30271100f00dSgjelinek 	if (do_postinstall) {
30281100f00dSgjelinek 		status = do_subproc(postcmdbuf);
30291100f00dSgjelinek 
30301100f00dSgjelinek 		if ((subproc_err =
30311100f00dSgjelinek 		    subproc_status(gettext("brand-specific post-install"),
30321100f00dSgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
3033ad02e316Sbatschul 			errno = subproc_err;
30341100f00dSgjelinek 			err = Z_ERR;
30351100f00dSgjelinek 			(void) zone_set_state(target_zone,
30361100f00dSgjelinek 			    ZONE_STATE_INCOMPLETE);
30371100f00dSgjelinek 		}
30381100f00dSgjelinek 	}
30391100f00dSgjelinek 
30407c478bd9Sstevel@tonic-gate done:
30419acbbeafSnn35248 	/*
3042ff17c8bfSgjelinek 	 * If the install script exited with ZONE_SUBPROC_NOTCOMPLETE, try to
3043ff17c8bfSgjelinek 	 * clean up the zone and leave the zone in the CONFIGURED state so that
3044ff17c8bfSgjelinek 	 * another install can be attempted without requiring an uninstall
3045ff17c8bfSgjelinek 	 * first.
30469acbbeafSnn35248 	 */
3047ff17c8bfSgjelinek 	if (subproc_err == ZONE_SUBPROC_NOTCOMPLETE) {
3048ad02e316Sbatschul 		int temp_err;
3049ad02e316Sbatschul 
3050ad02e316Sbatschul 		if ((temp_err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
3051ad02e316Sbatschul 			errno = err = temp_err;
30529acbbeafSnn35248 			zperror2(target_zone,
30539acbbeafSnn35248 			    gettext("cleaning up zonepath failed"));
3054ad02e316Sbatschul 		} else if ((temp_err = zone_set_state(target_zone,
30559acbbeafSnn35248 		    ZONE_STATE_CONFIGURED)) != Z_OK) {
3056ad02e316Sbatschul 			errno = err = temp_err;
30579acbbeafSnn35248 			zperror2(target_zone, gettext("could not set state"));
30589acbbeafSnn35248 		}
30599acbbeafSnn35248 	}
30609acbbeafSnn35248 
3061ff17c8bfSgjelinek 	if (!brand_help)
3062ff17c8bfSgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
30637c478bd9Sstevel@tonic-gate 	return ((err == Z_OK) ? Z_OK : Z_ERR);
30647c478bd9Sstevel@tonic-gate }
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate /*
3067865e09a4Sgjelinek  * Check that the inherited pkg dirs are the same for the clone and its source.
3068865e09a4Sgjelinek  * The easiest way to do that is check that the list of ipds is the same
3069865e09a4Sgjelinek  * by matching each one against the other.  This algorithm should be fine since
3070865e09a4Sgjelinek  * the list of ipds should not be that long.
3071865e09a4Sgjelinek  */
3072865e09a4Sgjelinek static int
3073865e09a4Sgjelinek valid_ipd_clone(zone_dochandle_t s_handle, char *source_zone,
3074865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3075865e09a4Sgjelinek {
3076865e09a4Sgjelinek 	int err;
3077865e09a4Sgjelinek 	int res = Z_OK;
3078865e09a4Sgjelinek 	int s_cnt = 0;
3079865e09a4Sgjelinek 	int t_cnt = 0;
3080865e09a4Sgjelinek 	struct zone_fstab s_fstab;
3081865e09a4Sgjelinek 	struct zone_fstab t_fstab;
3082865e09a4Sgjelinek 
3083865e09a4Sgjelinek 	/*
3084865e09a4Sgjelinek 	 * First check the source of the clone against the target.
3085865e09a4Sgjelinek 	 */
3086865e09a4Sgjelinek 	if ((err = zonecfg_setipdent(s_handle)) != Z_OK) {
3087865e09a4Sgjelinek 		errno = err;
3088865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not enumerate "
3089865e09a4Sgjelinek 		    "inherit-pkg-dirs"));
3090865e09a4Sgjelinek 		return (Z_ERR);
3091865e09a4Sgjelinek 	}
3092865e09a4Sgjelinek 
3093865e09a4Sgjelinek 	while (zonecfg_getipdent(s_handle, &s_fstab) == Z_OK) {
3094865e09a4Sgjelinek 		boolean_t match = B_FALSE;
3095865e09a4Sgjelinek 
3096865e09a4Sgjelinek 		s_cnt++;
3097865e09a4Sgjelinek 
3098865e09a4Sgjelinek 		if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
3099865e09a4Sgjelinek 			errno = err;
3100865e09a4Sgjelinek 			zperror2(target_zone, gettext("could not enumerate "
3101865e09a4Sgjelinek 			    "inherit-pkg-dirs"));
3102865e09a4Sgjelinek 			(void) zonecfg_endipdent(s_handle);
3103865e09a4Sgjelinek 			return (Z_ERR);
3104865e09a4Sgjelinek 		}
3105865e09a4Sgjelinek 
3106865e09a4Sgjelinek 		while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK) {
3107865e09a4Sgjelinek 			if (strcmp(s_fstab.zone_fs_dir, t_fstab.zone_fs_dir)
3108865e09a4Sgjelinek 			    == 0) {
3109865e09a4Sgjelinek 				match = B_TRUE;
3110865e09a4Sgjelinek 				break;
3111865e09a4Sgjelinek 			}
3112865e09a4Sgjelinek 		}
3113865e09a4Sgjelinek 		(void) zonecfg_endipdent(t_handle);
3114865e09a4Sgjelinek 
3115865e09a4Sgjelinek 		if (!match) {
3116865e09a4Sgjelinek 			(void) fprintf(stderr, gettext("inherit-pkg-dir "
3117865e09a4Sgjelinek 			    "'%s' is not configured in zone %s.\n"),
3118865e09a4Sgjelinek 			    s_fstab.zone_fs_dir, target_zone);
3119865e09a4Sgjelinek 			res = Z_ERR;
3120865e09a4Sgjelinek 		}
3121865e09a4Sgjelinek 	}
3122865e09a4Sgjelinek 
3123865e09a4Sgjelinek 	(void) zonecfg_endipdent(s_handle);
3124865e09a4Sgjelinek 
3125865e09a4Sgjelinek 	/* skip the next check if we already have errors */
3126865e09a4Sgjelinek 	if (res == Z_ERR)
3127865e09a4Sgjelinek 		return (res);
3128865e09a4Sgjelinek 
3129865e09a4Sgjelinek 	/*
3130865e09a4Sgjelinek 	 * Now check the number of ipds in the target so we can verify
3131865e09a4Sgjelinek 	 * that the source is not a subset of the target.
3132865e09a4Sgjelinek 	 */
3133865e09a4Sgjelinek 	if ((err = zonecfg_setipdent(t_handle)) != Z_OK) {
3134865e09a4Sgjelinek 		errno = err;
3135865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not enumerate "
3136865e09a4Sgjelinek 		    "inherit-pkg-dirs"));
3137865e09a4Sgjelinek 		return (Z_ERR);
3138865e09a4Sgjelinek 	}
3139865e09a4Sgjelinek 
3140865e09a4Sgjelinek 	while (zonecfg_getipdent(t_handle, &t_fstab) == Z_OK)
3141865e09a4Sgjelinek 		t_cnt++;
3142865e09a4Sgjelinek 
3143865e09a4Sgjelinek 	(void) zonecfg_endipdent(t_handle);
3144865e09a4Sgjelinek 
3145865e09a4Sgjelinek 	if (t_cnt != s_cnt) {
3146865e09a4Sgjelinek 		(void) fprintf(stderr, gettext("Zone %s is configured "
3147865e09a4Sgjelinek 		    "with inherit-pkg-dirs that are not configured in zone "
3148865e09a4Sgjelinek 		    "%s.\n"), target_zone, source_zone);
3149865e09a4Sgjelinek 		res = Z_ERR;
3150865e09a4Sgjelinek 	}
3151865e09a4Sgjelinek 
3152865e09a4Sgjelinek 	return (res);
3153865e09a4Sgjelinek }
3154865e09a4Sgjelinek 
3155865e09a4Sgjelinek static void
3156865e09a4Sgjelinek warn_dev_match(zone_dochandle_t s_handle, char *source_zone,
3157865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3158865e09a4Sgjelinek {
3159865e09a4Sgjelinek 	int err;
3160865e09a4Sgjelinek 	struct zone_devtab s_devtab;
3161865e09a4Sgjelinek 	struct zone_devtab t_devtab;
3162865e09a4Sgjelinek 
3163865e09a4Sgjelinek 	if ((err = zonecfg_setdevent(t_handle)) != Z_OK) {
3164865e09a4Sgjelinek 		errno = err;
3165865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not enumerate devices"));
3166865e09a4Sgjelinek 		return;
3167865e09a4Sgjelinek 	}
3168865e09a4Sgjelinek 
3169865e09a4Sgjelinek 	while (zonecfg_getdevent(t_handle, &t_devtab) == Z_OK) {
3170865e09a4Sgjelinek 		if ((err = zonecfg_setdevent(s_handle)) != Z_OK) {
3171865e09a4Sgjelinek 			errno = err;
3172865e09a4Sgjelinek 			zperror2(source_zone,
3173865e09a4Sgjelinek 			    gettext("could not enumerate devices"));
3174865e09a4Sgjelinek 			(void) zonecfg_enddevent(t_handle);
3175865e09a4Sgjelinek 			return;
3176865e09a4Sgjelinek 		}
3177865e09a4Sgjelinek 
3178865e09a4Sgjelinek 		while (zonecfg_getdevent(s_handle, &s_devtab) == Z_OK) {
3179865e09a4Sgjelinek 			/*
3180865e09a4Sgjelinek 			 * Use fnmatch to catch the case where wildcards
3181865e09a4Sgjelinek 			 * were used in one zone and the other has an
3182865e09a4Sgjelinek 			 * explicit entry (e.g. /dev/dsk/c0t0d0s6 vs.
3183865e09a4Sgjelinek 			 * /dev/\*dsk/c0t0d0s6).
3184865e09a4Sgjelinek 			 */
3185865e09a4Sgjelinek 			if (fnmatch(t_devtab.zone_dev_match,
3186865e09a4Sgjelinek 			    s_devtab.zone_dev_match, FNM_PATHNAME) == 0 ||
3187865e09a4Sgjelinek 			    fnmatch(s_devtab.zone_dev_match,
3188865e09a4Sgjelinek 			    t_devtab.zone_dev_match, FNM_PATHNAME) == 0) {
3189865e09a4Sgjelinek 				(void) fprintf(stderr,
3190865e09a4Sgjelinek 				    gettext("WARNING: device '%s' "
3191865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3192865e09a4Sgjelinek 				    t_devtab.zone_dev_match);
3193865e09a4Sgjelinek 				break;
3194865e09a4Sgjelinek 			}
3195865e09a4Sgjelinek 		}
3196865e09a4Sgjelinek 		(void) zonecfg_enddevent(s_handle);
3197865e09a4Sgjelinek 	}
3198865e09a4Sgjelinek 
3199865e09a4Sgjelinek 	(void) zonecfg_enddevent(t_handle);
3200865e09a4Sgjelinek }
3201865e09a4Sgjelinek 
3202865e09a4Sgjelinek /*
3203865e09a4Sgjelinek  * Check if the specified mount option (opt) is contained within the
3204865e09a4Sgjelinek  * options string.
3205865e09a4Sgjelinek  */
3206865e09a4Sgjelinek static boolean_t
3207865e09a4Sgjelinek opt_match(char *opt, char *options)
3208865e09a4Sgjelinek {
3209865e09a4Sgjelinek 	char *p;
3210865e09a4Sgjelinek 	char *lastp;
3211865e09a4Sgjelinek 
3212865e09a4Sgjelinek 	if ((p = strtok_r(options, ",", &lastp)) != NULL) {
3213865e09a4Sgjelinek 		if (strcmp(p, opt) == 0)
3214865e09a4Sgjelinek 			return (B_TRUE);
3215865e09a4Sgjelinek 		while ((p = strtok_r(NULL, ",", &lastp)) != NULL) {
3216865e09a4Sgjelinek 			if (strcmp(p, opt) == 0)
3217865e09a4Sgjelinek 				return (B_TRUE);
3218865e09a4Sgjelinek 		}
3219865e09a4Sgjelinek 	}
3220865e09a4Sgjelinek 
3221865e09a4Sgjelinek 	return (B_FALSE);
3222865e09a4Sgjelinek }
3223865e09a4Sgjelinek 
32240b5de56dSgjelinek #define	RW_LOFS	"WARNING: read-write lofs file system on '%s' is configured " \
3225865e09a4Sgjelinek 	"in both zones.\n"
3226865e09a4Sgjelinek 
3227865e09a4Sgjelinek static void
3228865e09a4Sgjelinek print_fs_warnings(struct zone_fstab *s_fstab, struct zone_fstab *t_fstab)
3229865e09a4Sgjelinek {
3230865e09a4Sgjelinek 	/*
3231865e09a4Sgjelinek 	 * It is ok to have shared lofs mounted fs but we want to warn if
3232865e09a4Sgjelinek 	 * either is rw since this will effect the other zone.
3233865e09a4Sgjelinek 	 */
3234865e09a4Sgjelinek 	if (strcmp(t_fstab->zone_fs_type, "lofs") == 0) {
3235865e09a4Sgjelinek 		zone_fsopt_t *optp;
3236865e09a4Sgjelinek 
3237865e09a4Sgjelinek 		/* The default is rw so no options means rw */
3238865e09a4Sgjelinek 		if (t_fstab->zone_fs_options == NULL ||
3239865e09a4Sgjelinek 		    s_fstab->zone_fs_options == NULL) {
3240865e09a4Sgjelinek 			(void) fprintf(stderr, gettext(RW_LOFS),
3241865e09a4Sgjelinek 			    t_fstab->zone_fs_special);
3242865e09a4Sgjelinek 			return;
3243865e09a4Sgjelinek 		}
3244865e09a4Sgjelinek 
3245865e09a4Sgjelinek 		for (optp = s_fstab->zone_fs_options; optp != NULL;
3246865e09a4Sgjelinek 		    optp = optp->zone_fsopt_next) {
3247865e09a4Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
3248865e09a4Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
3249865e09a4Sgjelinek 				    s_fstab->zone_fs_special);
3250865e09a4Sgjelinek 				return;
3251865e09a4Sgjelinek 			}
3252865e09a4Sgjelinek 		}
3253865e09a4Sgjelinek 
3254865e09a4Sgjelinek 		for (optp = t_fstab->zone_fs_options; optp != NULL;
3255865e09a4Sgjelinek 		    optp = optp->zone_fsopt_next) {
3256865e09a4Sgjelinek 			if (opt_match("rw", optp->zone_fsopt_opt)) {
3257865e09a4Sgjelinek 				(void) fprintf(stderr, gettext(RW_LOFS),
3258865e09a4Sgjelinek 				    t_fstab->zone_fs_special);
3259865e09a4Sgjelinek 				return;
3260865e09a4Sgjelinek 			}
3261865e09a4Sgjelinek 		}
3262865e09a4Sgjelinek 
3263865e09a4Sgjelinek 		return;
3264865e09a4Sgjelinek 	}
3265865e09a4Sgjelinek 
3266865e09a4Sgjelinek 	/*
3267865e09a4Sgjelinek 	 * TRANSLATION_NOTE
32680b5de56dSgjelinek 	 * The first variable is the file system type and the second is
32690b5de56dSgjelinek 	 * the file system special device.  For example,
32700b5de56dSgjelinek 	 * WARNING: ufs file system on '/dev/dsk/c0t0d0s0' ...
3271865e09a4Sgjelinek 	 */
32720b5de56dSgjelinek 	(void) fprintf(stderr, gettext("WARNING: %s file system on '%s' "
3273865e09a4Sgjelinek 	    "is configured in both zones.\n"), t_fstab->zone_fs_type,
3274865e09a4Sgjelinek 	    t_fstab->zone_fs_special);
3275865e09a4Sgjelinek }
3276865e09a4Sgjelinek 
3277865e09a4Sgjelinek static void
3278865e09a4Sgjelinek warn_fs_match(zone_dochandle_t s_handle, char *source_zone,
3279865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3280865e09a4Sgjelinek {
3281865e09a4Sgjelinek 	int err;
3282865e09a4Sgjelinek 	struct zone_fstab s_fstab;
3283865e09a4Sgjelinek 	struct zone_fstab t_fstab;
3284865e09a4Sgjelinek 
3285865e09a4Sgjelinek 	if ((err = zonecfg_setfsent(t_handle)) != Z_OK) {
3286865e09a4Sgjelinek 		errno = err;
3287865e09a4Sgjelinek 		zperror2(target_zone,
32880b5de56dSgjelinek 		    gettext("could not enumerate file systems"));
3289865e09a4Sgjelinek 		return;
3290865e09a4Sgjelinek 	}
3291865e09a4Sgjelinek 
3292865e09a4Sgjelinek 	while (zonecfg_getfsent(t_handle, &t_fstab) == Z_OK) {
3293865e09a4Sgjelinek 		if ((err = zonecfg_setfsent(s_handle)) != Z_OK) {
3294865e09a4Sgjelinek 			errno = err;
3295865e09a4Sgjelinek 			zperror2(source_zone,
32960b5de56dSgjelinek 			    gettext("could not enumerate file systems"));
3297865e09a4Sgjelinek 			(void) zonecfg_endfsent(t_handle);
3298865e09a4Sgjelinek 			return;
3299865e09a4Sgjelinek 		}
3300865e09a4Sgjelinek 
3301865e09a4Sgjelinek 		while (zonecfg_getfsent(s_handle, &s_fstab) == Z_OK) {
3302865e09a4Sgjelinek 			if (strcmp(t_fstab.zone_fs_special,
3303865e09a4Sgjelinek 			    s_fstab.zone_fs_special) == 0) {
3304865e09a4Sgjelinek 				print_fs_warnings(&s_fstab, &t_fstab);
3305865e09a4Sgjelinek 				break;
3306865e09a4Sgjelinek 			}
3307865e09a4Sgjelinek 		}
3308865e09a4Sgjelinek 		(void) zonecfg_endfsent(s_handle);
3309865e09a4Sgjelinek 	}
3310865e09a4Sgjelinek 
3311865e09a4Sgjelinek 	(void) zonecfg_endfsent(t_handle);
3312865e09a4Sgjelinek }
3313865e09a4Sgjelinek 
3314865e09a4Sgjelinek /*
3315865e09a4Sgjelinek  * We don't catch the case where you used the same IP address but
3316865e09a4Sgjelinek  * it is not an exact string match.  For example, 192.9.0.128 vs. 192.09.0.128.
3317865e09a4Sgjelinek  * However, we're not going to worry about that but we will check for
3318865e09a4Sgjelinek  * a possible netmask on one of the addresses (e.g. 10.0.0.1 and 10.0.0.1/24)
3319865e09a4Sgjelinek  * and handle that case as a match.
3320865e09a4Sgjelinek  */
3321865e09a4Sgjelinek static void
3322865e09a4Sgjelinek warn_ip_match(zone_dochandle_t s_handle, char *source_zone,
3323865e09a4Sgjelinek 	zone_dochandle_t t_handle, char *target_zone)
3324865e09a4Sgjelinek {
3325865e09a4Sgjelinek 	int err;
3326865e09a4Sgjelinek 	struct zone_nwiftab s_nwiftab;
3327865e09a4Sgjelinek 	struct zone_nwiftab t_nwiftab;
3328865e09a4Sgjelinek 
3329865e09a4Sgjelinek 	if ((err = zonecfg_setnwifent(t_handle)) != Z_OK) {
3330865e09a4Sgjelinek 		errno = err;
3331865e09a4Sgjelinek 		zperror2(target_zone,
3332865e09a4Sgjelinek 		    gettext("could not enumerate network interfaces"));
3333865e09a4Sgjelinek 		return;
3334865e09a4Sgjelinek 	}
3335865e09a4Sgjelinek 
3336865e09a4Sgjelinek 	while (zonecfg_getnwifent(t_handle, &t_nwiftab) == Z_OK) {
3337865e09a4Sgjelinek 		char *p;
3338865e09a4Sgjelinek 
3339865e09a4Sgjelinek 		/* remove an (optional) netmask from the address */
3340865e09a4Sgjelinek 		if ((p = strchr(t_nwiftab.zone_nwif_address, '/')) != NULL)
3341865e09a4Sgjelinek 			*p = '\0';
3342865e09a4Sgjelinek 
3343865e09a4Sgjelinek 		if ((err = zonecfg_setnwifent(s_handle)) != Z_OK) {
3344865e09a4Sgjelinek 			errno = err;
3345865e09a4Sgjelinek 			zperror2(source_zone,
3346865e09a4Sgjelinek 			    gettext("could not enumerate network interfaces"));
3347865e09a4Sgjelinek 			(void) zonecfg_endnwifent(t_handle);
3348865e09a4Sgjelinek 			return;
3349865e09a4Sgjelinek 		}
3350865e09a4Sgjelinek 
3351865e09a4Sgjelinek 		while (zonecfg_getnwifent(s_handle, &s_nwiftab) == Z_OK) {
3352865e09a4Sgjelinek 			/* remove an (optional) netmask from the address */
3353865e09a4Sgjelinek 			if ((p = strchr(s_nwiftab.zone_nwif_address, '/'))
3354865e09a4Sgjelinek 			    != NULL)
3355865e09a4Sgjelinek 				*p = '\0';
3356865e09a4Sgjelinek 
3357f4b3ec61Sdh155122 			/* For exclusive-IP zones, address is not specified. */
3358f4b3ec61Sdh155122 			if (strlen(s_nwiftab.zone_nwif_address) == 0)
3359f4b3ec61Sdh155122 				continue;
3360f4b3ec61Sdh155122 
3361865e09a4Sgjelinek 			if (strcmp(t_nwiftab.zone_nwif_address,
3362865e09a4Sgjelinek 			    s_nwiftab.zone_nwif_address) == 0) {
3363865e09a4Sgjelinek 				(void) fprintf(stderr,
3364865e09a4Sgjelinek 				    gettext("WARNING: network address '%s' "
3365865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3366865e09a4Sgjelinek 				    t_nwiftab.zone_nwif_address);
3367865e09a4Sgjelinek 				break;
3368865e09a4Sgjelinek 			}
3369865e09a4Sgjelinek 		}
3370865e09a4Sgjelinek 		(void) zonecfg_endnwifent(s_handle);
3371865e09a4Sgjelinek 	}
3372865e09a4Sgjelinek 
3373865e09a4Sgjelinek 	(void) zonecfg_endnwifent(t_handle);
3374865e09a4Sgjelinek }
3375865e09a4Sgjelinek 
3376865e09a4Sgjelinek static void
33779acbbeafSnn35248 warn_dataset_match(zone_dochandle_t s_handle, char *source,
33789acbbeafSnn35248 	zone_dochandle_t t_handle, char *target)
3379865e09a4Sgjelinek {
3380865e09a4Sgjelinek 	int err;
3381865e09a4Sgjelinek 	struct zone_dstab s_dstab;
3382865e09a4Sgjelinek 	struct zone_dstab t_dstab;
3383865e09a4Sgjelinek 
3384865e09a4Sgjelinek 	if ((err = zonecfg_setdsent(t_handle)) != Z_OK) {
3385865e09a4Sgjelinek 		errno = err;
33869acbbeafSnn35248 		zperror2(target, gettext("could not enumerate datasets"));
3387865e09a4Sgjelinek 		return;
3388865e09a4Sgjelinek 	}
3389865e09a4Sgjelinek 
3390865e09a4Sgjelinek 	while (zonecfg_getdsent(t_handle, &t_dstab) == Z_OK) {
3391865e09a4Sgjelinek 		if ((err = zonecfg_setdsent(s_handle)) != Z_OK) {
3392865e09a4Sgjelinek 			errno = err;
33939acbbeafSnn35248 			zperror2(source,
3394865e09a4Sgjelinek 			    gettext("could not enumerate datasets"));
3395865e09a4Sgjelinek 			(void) zonecfg_enddsent(t_handle);
3396865e09a4Sgjelinek 			return;
3397865e09a4Sgjelinek 		}
3398865e09a4Sgjelinek 
3399865e09a4Sgjelinek 		while (zonecfg_getdsent(s_handle, &s_dstab) == Z_OK) {
3400865e09a4Sgjelinek 			if (strcmp(t_dstab.zone_dataset_name,
3401865e09a4Sgjelinek 			    s_dstab.zone_dataset_name) == 0) {
34029acbbeafSnn35248 				target_zone = source;
34039acbbeafSnn35248 				zerror(gettext("WARNING: dataset '%s' "
3404865e09a4Sgjelinek 				    "is configured in both zones.\n"),
3405865e09a4Sgjelinek 				    t_dstab.zone_dataset_name);
3406865e09a4Sgjelinek 				break;
3407865e09a4Sgjelinek 			}
3408865e09a4Sgjelinek 		}
3409865e09a4Sgjelinek 		(void) zonecfg_enddsent(s_handle);
3410865e09a4Sgjelinek 	}
3411865e09a4Sgjelinek 
3412865e09a4Sgjelinek 	(void) zonecfg_enddsent(t_handle);
3413865e09a4Sgjelinek }
3414865e09a4Sgjelinek 
34159acbbeafSnn35248 /*
34169acbbeafSnn35248  * Check that the clone and its source have the same brand type.
34179acbbeafSnn35248  */
34189acbbeafSnn35248 static int
34199acbbeafSnn35248 valid_brand_clone(char *source_zone, char *target_zone)
34209acbbeafSnn35248 {
3421123807fbSedp 	brand_handle_t bh;
34229acbbeafSnn35248 	char source_brand[MAXNAMELEN];
34239acbbeafSnn35248 
34249acbbeafSnn35248 	if ((zone_get_brand(source_zone, source_brand,
34259acbbeafSnn35248 	    sizeof (source_brand))) != Z_OK) {
34269acbbeafSnn35248 		(void) fprintf(stderr, "%s: zone '%s': %s\n",
34279acbbeafSnn35248 		    execname, source_zone, gettext("missing or invalid brand"));
34289acbbeafSnn35248 		return (Z_ERR);
34299acbbeafSnn35248 	}
34309acbbeafSnn35248 
3431ad02e316Sbatschul 	if (strcmp(source_brand, target_brand) != 0) {
34329acbbeafSnn35248 		(void) fprintf(stderr,
34339acbbeafSnn35248 		    gettext("%s: Zones '%s' and '%s' have different brand "
34349acbbeafSnn35248 		    "types.\n"), execname, source_zone, target_zone);
34359acbbeafSnn35248 		return (Z_ERR);
34369acbbeafSnn35248 	}
34379acbbeafSnn35248 
3438123807fbSedp 	if ((bh = brand_open(target_brand)) == NULL) {
34399acbbeafSnn35248 		zerror(gettext("missing or invalid brand"));
34409acbbeafSnn35248 		return (Z_ERR);
34419acbbeafSnn35248 	}
3442123807fbSedp 	brand_close(bh);
34439acbbeafSnn35248 	return (Z_OK);
34449acbbeafSnn35248 }
34459acbbeafSnn35248 
3446865e09a4Sgjelinek static int
3447865e09a4Sgjelinek validate_clone(char *source_zone, char *target_zone)
3448865e09a4Sgjelinek {
3449865e09a4Sgjelinek 	int err = Z_OK;
3450865e09a4Sgjelinek 	zone_dochandle_t s_handle;
3451865e09a4Sgjelinek 	zone_dochandle_t t_handle;
3452865e09a4Sgjelinek 
3453865e09a4Sgjelinek 	if ((t_handle = zonecfg_init_handle()) == NULL) {
3454865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3455865e09a4Sgjelinek 		return (Z_ERR);
3456865e09a4Sgjelinek 	}
3457865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, t_handle)) != Z_OK) {
3458865e09a4Sgjelinek 		errno = err;
3459865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3460865e09a4Sgjelinek 		zonecfg_fini_handle(t_handle);
3461865e09a4Sgjelinek 		return (Z_ERR);
3462865e09a4Sgjelinek 	}
3463865e09a4Sgjelinek 
3464865e09a4Sgjelinek 	if ((s_handle = zonecfg_init_handle()) == NULL) {
3465865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3466865e09a4Sgjelinek 		zonecfg_fini_handle(t_handle);
3467865e09a4Sgjelinek 		return (Z_ERR);
3468865e09a4Sgjelinek 	}
3469865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(source_zone, s_handle)) != Z_OK) {
3470865e09a4Sgjelinek 		errno = err;
3471865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_CLONE), B_TRUE);
3472865e09a4Sgjelinek 		goto done;
3473865e09a4Sgjelinek 	}
3474865e09a4Sgjelinek 
34759acbbeafSnn35248 	/* verify new zone has same brand type */
34769acbbeafSnn35248 	err = valid_brand_clone(source_zone, target_zone);
34779acbbeafSnn35248 	if (err != Z_OK)
34789acbbeafSnn35248 		goto done;
34799acbbeafSnn35248 
3480865e09a4Sgjelinek 	/* verify new zone has same inherit-pkg-dirs */
3481865e09a4Sgjelinek 	err = valid_ipd_clone(s_handle, source_zone, t_handle, target_zone);
3482865e09a4Sgjelinek 
3483865e09a4Sgjelinek 	/* warn about imported fs's which are the same */
3484865e09a4Sgjelinek 	warn_fs_match(s_handle, source_zone, t_handle, target_zone);
3485865e09a4Sgjelinek 
3486865e09a4Sgjelinek 	/* warn about imported IP addresses which are the same */
3487865e09a4Sgjelinek 	warn_ip_match(s_handle, source_zone, t_handle, target_zone);
3488865e09a4Sgjelinek 
3489865e09a4Sgjelinek 	/* warn about imported devices which are the same */
3490865e09a4Sgjelinek 	warn_dev_match(s_handle, source_zone, t_handle, target_zone);
3491865e09a4Sgjelinek 
3492865e09a4Sgjelinek 	/* warn about imported datasets which are the same */
3493865e09a4Sgjelinek 	warn_dataset_match(s_handle, source_zone, t_handle, target_zone);
3494865e09a4Sgjelinek 
3495865e09a4Sgjelinek done:
3496865e09a4Sgjelinek 	zonecfg_fini_handle(t_handle);
3497865e09a4Sgjelinek 	zonecfg_fini_handle(s_handle);
3498865e09a4Sgjelinek 
3499865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3500865e09a4Sgjelinek }
3501865e09a4Sgjelinek 
3502865e09a4Sgjelinek static int
3503865e09a4Sgjelinek copy_zone(char *src, char *dst)
3504865e09a4Sgjelinek {
3505865e09a4Sgjelinek 	boolean_t out_null = B_FALSE;
3506865e09a4Sgjelinek 	int status;
3507865e09a4Sgjelinek 	char *outfile;
3508865e09a4Sgjelinek 	char cmdbuf[MAXPATHLEN * 2 + 128];
3509865e09a4Sgjelinek 
3510865e09a4Sgjelinek 	if ((outfile = tempnam("/var/log", "zone")) == NULL) {
3511865e09a4Sgjelinek 		outfile = "/dev/null";
3512865e09a4Sgjelinek 		out_null = B_TRUE;
3513865e09a4Sgjelinek 	}
3514865e09a4Sgjelinek 
35150b5de56dSgjelinek 	/*
35160b5de56dSgjelinek 	 * Use find to get the list of files to copy.  We need to skip
35170b5de56dSgjelinek 	 * files of type "socket" since cpio can't handle those but that
35180b5de56dSgjelinek 	 * should be ok since the app will recreate the socket when it runs.
35190b5de56dSgjelinek 	 * We also need to filter out anything under the .zfs subdir.  Since
35200b5de56dSgjelinek 	 * find is running depth-first, we need the extra egrep to filter .zfs.
35210b5de56dSgjelinek 	 */
3522865e09a4Sgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf),
35230b5de56dSgjelinek 	    "cd %s && /usr/bin/find . -type s -prune -o -depth -print | "
352407b574eeSgjelinek 	    "/usr/bin/egrep -v '^\\./\\.zfs$|^\\./\\.zfs/' | "
3525865e09a4Sgjelinek 	    "/usr/bin/cpio -pdmuP@ %s > %s 2>&1",
3526865e09a4Sgjelinek 	    src, dst, outfile);
3527865e09a4Sgjelinek 
3528865e09a4Sgjelinek 	status = do_subproc(cmdbuf);
3529865e09a4Sgjelinek 
35309acbbeafSnn35248 	if (subproc_status("copy", status, B_TRUE) != ZONE_SUBPROC_OK) {
3531865e09a4Sgjelinek 		if (!out_null)
3532865e09a4Sgjelinek 			(void) fprintf(stderr, gettext("\nThe copy failed.\n"
3533865e09a4Sgjelinek 			    "More information can be found in %s\n"), outfile);
35349acbbeafSnn35248 		return (Z_ERR);
3535865e09a4Sgjelinek 	}
3536865e09a4Sgjelinek 
3537865e09a4Sgjelinek 	if (!out_null)
3538865e09a4Sgjelinek 		(void) unlink(outfile);
3539865e09a4Sgjelinek 
3540865e09a4Sgjelinek 	return (Z_OK);
3541865e09a4Sgjelinek }
3542865e09a4Sgjelinek 
3543865e09a4Sgjelinek /* ARGSUSED */
35440094b373Sjv227347 int
35450094b373Sjv227347 zfm_print(const struct mnttab *p, void *r) {
35460094b373Sjv227347 	zerror("  %s\n", p->mnt_mountp);
3547865e09a4Sgjelinek 	return (0);
3548865e09a4Sgjelinek }
3549865e09a4Sgjelinek 
35500b5de56dSgjelinek int
35510b5de56dSgjelinek clone_copy(char *source_zonepath, char *zonepath)
35520b5de56dSgjelinek {
35530b5de56dSgjelinek 	int err;
35540b5de56dSgjelinek 
35550b5de56dSgjelinek 	/* Don't clone the zone if anything is still mounted there */
35560b5de56dSgjelinek 	if (zonecfg_find_mounts(source_zonepath, NULL, NULL)) {
35570b5de56dSgjelinek 		zerror(gettext("These file systems are mounted on "
35580b5de56dSgjelinek 		    "subdirectories of %s.\n"), source_zonepath);
35590b5de56dSgjelinek 		(void) zonecfg_find_mounts(source_zonepath, zfm_print, NULL);
35600b5de56dSgjelinek 		return (Z_ERR);
35610b5de56dSgjelinek 	}
35620b5de56dSgjelinek 
35630b5de56dSgjelinek 	/*
35640b5de56dSgjelinek 	 * Attempt to create a ZFS fs for the zonepath.  As usual, we don't
35650b5de56dSgjelinek 	 * care if this works or not since we always have the default behavior
35660b5de56dSgjelinek 	 * of a simple directory for the zonepath.
35670b5de56dSgjelinek 	 */
35680b5de56dSgjelinek 	create_zfs_zonepath(zonepath);
35690b5de56dSgjelinek 
35700b5de56dSgjelinek 	(void) printf(gettext("Copying %s..."), source_zonepath);
35710b5de56dSgjelinek 	(void) fflush(stdout);
35720b5de56dSgjelinek 
35730b5de56dSgjelinek 	err = copy_zone(source_zonepath, zonepath);
35740b5de56dSgjelinek 
35750b5de56dSgjelinek 	(void) printf("\n");
35760b5de56dSgjelinek 
35770b5de56dSgjelinek 	return (err);
35780b5de56dSgjelinek }
35790b5de56dSgjelinek 
3580865e09a4Sgjelinek static int
3581865e09a4Sgjelinek clone_func(int argc, char *argv[])
3582865e09a4Sgjelinek {
3583865e09a4Sgjelinek 	char *source_zone = NULL;
3584865e09a4Sgjelinek 	int lockfd;
3585865e09a4Sgjelinek 	int err, arg;
3586865e09a4Sgjelinek 	char zonepath[MAXPATHLEN];
3587865e09a4Sgjelinek 	char source_zonepath[MAXPATHLEN];
3588865e09a4Sgjelinek 	zone_state_t state;
3589865e09a4Sgjelinek 	zone_entry_t *zent;
35900b5de56dSgjelinek 	char *method = NULL;
35910b5de56dSgjelinek 	char *snapshot = NULL;
3592ff17c8bfSgjelinek 	char cmdbuf[MAXPATHLEN];
3593ff17c8bfSgjelinek 	char postcmdbuf[MAXPATHLEN];
3594ff17c8bfSgjelinek 	char presnapbuf[MAXPATHLEN];
3595ff17c8bfSgjelinek 	char postsnapbuf[MAXPATHLEN];
3596ff17c8bfSgjelinek 	char validsnapbuf[MAXPATHLEN];
3597ff17c8bfSgjelinek 	brand_handle_t bh = NULL;
3598ff17c8bfSgjelinek 	int status;
3599ff17c8bfSgjelinek 	boolean_t brand_help = B_FALSE;
3600865e09a4Sgjelinek 
3601865e09a4Sgjelinek 	if (zonecfg_in_alt_root()) {
3602865e09a4Sgjelinek 		zerror(gettext("cannot clone zone in alternate root"));
3603865e09a4Sgjelinek 		return (Z_ERR);
3604865e09a4Sgjelinek 	}
3605865e09a4Sgjelinek 
3606ff17c8bfSgjelinek 	/* Check the argv string for args we handle internally */
3607865e09a4Sgjelinek 	optind = 0;
3608ff17c8bfSgjelinek 	opterr = 0;
3609ff17c8bfSgjelinek 	while ((arg = getopt(argc, argv, "?m:s:")) != EOF) {
3610865e09a4Sgjelinek 		switch (arg) {
3611865e09a4Sgjelinek 		case '?':
3612ff17c8bfSgjelinek 			if (optopt == '?') {
3613865e09a4Sgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
3614ff17c8bfSgjelinek 				brand_help = B_TRUE;
3615ff17c8bfSgjelinek 			}
3616ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
3617ff17c8bfSgjelinek 			break;
3618865e09a4Sgjelinek 		case 'm':
3619865e09a4Sgjelinek 			method = optarg;
3620865e09a4Sgjelinek 			break;
36210b5de56dSgjelinek 		case 's':
36220b5de56dSgjelinek 			snapshot = optarg;
36230b5de56dSgjelinek 			break;
3624865e09a4Sgjelinek 		default:
3625ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
3626ff17c8bfSgjelinek 			break;
3627ff17c8bfSgjelinek 		}
3628ff17c8bfSgjelinek 	}
3629ff17c8bfSgjelinek 
3630ff17c8bfSgjelinek 	if (argc != (optind + 1)) {
3631865e09a4Sgjelinek 		sub_usage(SHELP_CLONE, CMD_CLONE);
3632865e09a4Sgjelinek 		return (Z_USAGE);
3633865e09a4Sgjelinek 	}
3634ff17c8bfSgjelinek 
3635865e09a4Sgjelinek 	source_zone = argv[optind];
3636ff17c8bfSgjelinek 
3637ff17c8bfSgjelinek 	if (!brand_help) {
3638ff17c8bfSgjelinek 		if (sanity_check(target_zone, CMD_CLONE, B_FALSE, B_TRUE,
3639ff17c8bfSgjelinek 		    B_FALSE) != Z_OK)
3640865e09a4Sgjelinek 			return (Z_ERR);
3641ce28b40eSzt129084 		if (verify_details(CMD_CLONE, argv) != Z_OK)
3642865e09a4Sgjelinek 			return (Z_ERR);
3643865e09a4Sgjelinek 
3644865e09a4Sgjelinek 		/*
3645865e09a4Sgjelinek 		 * We also need to do some extra validation on the source zone.
3646865e09a4Sgjelinek 		 */
3647865e09a4Sgjelinek 		if (strcmp(source_zone, GLOBAL_ZONENAME) == 0) {
3648ff17c8bfSgjelinek 			zerror(gettext("%s operation is invalid for the "
3649ff17c8bfSgjelinek 			    "global zone."), cmd_to_str(CMD_CLONE));
3650865e09a4Sgjelinek 			return (Z_ERR);
3651865e09a4Sgjelinek 		}
3652865e09a4Sgjelinek 
3653865e09a4Sgjelinek 		if (strncmp(source_zone, "SUNW", 4) == 0) {
3654ff17c8bfSgjelinek 			zerror(gettext("%s operation is invalid for zones "
3655ff17c8bfSgjelinek 			    "starting with SUNW."), cmd_to_str(CMD_CLONE));
3656865e09a4Sgjelinek 			return (Z_ERR);
3657865e09a4Sgjelinek 		}
3658865e09a4Sgjelinek 
3659*cb8a054bSGlenn Faden 		if (auth_check(username, source_zone, SOURCE_ZONE) == Z_ERR) {
3660*cb8a054bSGlenn Faden 			zerror(gettext("%s operation is invalid because "
3661*cb8a054bSGlenn Faden 			    "user is not authorized to read source zone."),
3662*cb8a054bSGlenn Faden 			    cmd_to_str(CMD_CLONE));
3663*cb8a054bSGlenn Faden 			return (Z_ERR);
3664*cb8a054bSGlenn Faden 		}
3665*cb8a054bSGlenn Faden 
3666865e09a4Sgjelinek 		zent = lookup_running_zone(source_zone);
3667865e09a4Sgjelinek 		if (zent != NULL) {
3668865e09a4Sgjelinek 			/* check whether the zone is ready or running */
3669ff17c8bfSgjelinek 			if ((err = zone_get_state(zent->zname,
3670ff17c8bfSgjelinek 			    &zent->zstate_num)) != Z_OK) {
3671865e09a4Sgjelinek 				errno = err;
3672ff17c8bfSgjelinek 				zperror2(zent->zname, gettext("could not get "
3673ff17c8bfSgjelinek 				    "state"));
3674865e09a4Sgjelinek 				/* can't tell, so hedge */
3675865e09a4Sgjelinek 				zent->zstate_str = "ready/running";
3676865e09a4Sgjelinek 			} else {
3677ff17c8bfSgjelinek 				zent->zstate_str =
3678ff17c8bfSgjelinek 				    zone_state_str(zent->zstate_num);
3679865e09a4Sgjelinek 			}
3680865e09a4Sgjelinek 			zerror(gettext("%s operation is invalid for %s zones."),
3681865e09a4Sgjelinek 			    cmd_to_str(CMD_CLONE), zent->zstate_str);
3682865e09a4Sgjelinek 			return (Z_ERR);
3683865e09a4Sgjelinek 		}
3684865e09a4Sgjelinek 
3685865e09a4Sgjelinek 		if ((err = zone_get_state(source_zone, &state)) != Z_OK) {
3686865e09a4Sgjelinek 			errno = err;
3687865e09a4Sgjelinek 			zperror2(source_zone, gettext("could not get state"));
3688865e09a4Sgjelinek 			return (Z_ERR);
3689865e09a4Sgjelinek 		}
3690865e09a4Sgjelinek 		if (state != ZONE_STATE_INSTALLED) {
3691865e09a4Sgjelinek 			(void) fprintf(stderr,
3692865e09a4Sgjelinek 			    gettext("%s: zone %s is %s; %s is required.\n"),
3693865e09a4Sgjelinek 			    execname, source_zone, zone_state_str(state),
3694865e09a4Sgjelinek 			    zone_state_str(ZONE_STATE_INSTALLED));
3695865e09a4Sgjelinek 			return (Z_ERR);
3696865e09a4Sgjelinek 		}
3697865e09a4Sgjelinek 
3698865e09a4Sgjelinek 		/*
3699865e09a4Sgjelinek 		 * The source zone checks out ok, continue with the clone.
3700865e09a4Sgjelinek 		 */
3701865e09a4Sgjelinek 
3702865e09a4Sgjelinek 		if (validate_clone(source_zone, target_zone) != Z_OK)
3703865e09a4Sgjelinek 			return (Z_ERR);
3704865e09a4Sgjelinek 
3705ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
3706ff17c8bfSgjelinek 			zerror(gettext("another %s may have an operation in "
3707ff17c8bfSgjelinek 			    "progress."), "zoneadm");
3708865e09a4Sgjelinek 			return (Z_ERR);
3709865e09a4Sgjelinek 		}
3710ff17c8bfSgjelinek 	}
3711865e09a4Sgjelinek 
3712865e09a4Sgjelinek 	if ((err = zone_get_zonepath(source_zone, source_zonepath,
3713865e09a4Sgjelinek 	    sizeof (source_zonepath))) != Z_OK) {
3714865e09a4Sgjelinek 		errno = err;
3715865e09a4Sgjelinek 		zperror2(source_zone, gettext("could not get zone path"));
3716865e09a4Sgjelinek 		goto done;
3717865e09a4Sgjelinek 	}
3718865e09a4Sgjelinek 
3719865e09a4Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
3720865e09a4Sgjelinek 	    != Z_OK) {
3721865e09a4Sgjelinek 		errno = err;
3722865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
3723865e09a4Sgjelinek 		goto done;
3724865e09a4Sgjelinek 	}
3725865e09a4Sgjelinek 
3726ff17c8bfSgjelinek 	/*
3727ff17c8bfSgjelinek 	 * Fetch the clone and postclone hooks from the brand configuration.
3728ff17c8bfSgjelinek 	 */
3729ff17c8bfSgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
3730ff17c8bfSgjelinek 		zerror(gettext("missing or invalid brand"));
3731ff17c8bfSgjelinek 		err = Z_ERR;
3732ff17c8bfSgjelinek 		goto done;
3733ff17c8bfSgjelinek 	}
3734ff17c8bfSgjelinek 
3735ff17c8bfSgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_clone, target_zone,
3736ff17c8bfSgjelinek 	    zonepath) != Z_OK) {
3737ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing clone resource");
3738ff17c8bfSgjelinek 		brand_close(bh);
3739ff17c8bfSgjelinek 		err = Z_ERR;
3740ff17c8bfSgjelinek 		goto done;
3741ff17c8bfSgjelinek 	}
3742ff17c8bfSgjelinek 
3743ff17c8bfSgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postclone,
3744ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
3745ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing postclone "
3746ff17c8bfSgjelinek 		    "resource");
3747ff17c8bfSgjelinek 		brand_close(bh);
3748ff17c8bfSgjelinek 		err = Z_ERR;
3749ff17c8bfSgjelinek 		goto done;
3750ff17c8bfSgjelinek 	}
3751ff17c8bfSgjelinek 
3752ff17c8bfSgjelinek 	if (get_hook(bh, presnapbuf, sizeof (presnapbuf), brand_get_presnap,
3753ff17c8bfSgjelinek 	    source_zone, source_zonepath) != Z_OK) {
3754ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing presnap "
3755ff17c8bfSgjelinek 		    "resource");
3756ff17c8bfSgjelinek 		brand_close(bh);
3757ff17c8bfSgjelinek 		err = Z_ERR;
3758ff17c8bfSgjelinek 		goto done;
3759ff17c8bfSgjelinek 	}
3760ff17c8bfSgjelinek 
3761ff17c8bfSgjelinek 	if (get_hook(bh, postsnapbuf, sizeof (postsnapbuf), brand_get_postsnap,
3762ff17c8bfSgjelinek 	    source_zone, source_zonepath) != Z_OK) {
3763ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing postsnap "
3764ff17c8bfSgjelinek 		    "resource");
3765ff17c8bfSgjelinek 		brand_close(bh);
3766ff17c8bfSgjelinek 		err = Z_ERR;
3767ff17c8bfSgjelinek 		goto done;
3768ff17c8bfSgjelinek 	}
3769ff17c8bfSgjelinek 
3770ff17c8bfSgjelinek 	if (get_hook(bh, validsnapbuf, sizeof (validsnapbuf),
3771ff17c8bfSgjelinek 	    brand_get_validatesnap, target_zone, zonepath) != Z_OK) {
3772ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing validatesnap "
3773ff17c8bfSgjelinek 		    "resource");
3774ff17c8bfSgjelinek 		brand_close(bh);
3775ff17c8bfSgjelinek 		err = Z_ERR;
3776ff17c8bfSgjelinek 		goto done;
3777ff17c8bfSgjelinek 	}
3778ff17c8bfSgjelinek 	brand_close(bh);
3779ff17c8bfSgjelinek 
3780ff17c8bfSgjelinek 	/* Append all options to clone hook. */
3781ff17c8bfSgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK) {
3782ff17c8bfSgjelinek 		err = Z_ERR;
3783ff17c8bfSgjelinek 		goto done;
3784ff17c8bfSgjelinek 	}
3785ff17c8bfSgjelinek 
3786ff17c8bfSgjelinek 	/* Append all options to postclone hook. */
3787ff17c8bfSgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK) {
3788ff17c8bfSgjelinek 		err = Z_ERR;
3789ff17c8bfSgjelinek 		goto done;
3790ff17c8bfSgjelinek 	}
3791ff17c8bfSgjelinek 
3792ff17c8bfSgjelinek 	if (!brand_help) {
3793865e09a4Sgjelinek 		if ((err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE))
3794865e09a4Sgjelinek 		    != Z_OK) {
3795865e09a4Sgjelinek 			errno = err;
3796865e09a4Sgjelinek 			zperror2(target_zone, gettext("could not set state"));
3797865e09a4Sgjelinek 			goto done;
3798865e09a4Sgjelinek 		}
3799ff17c8bfSgjelinek 	}
3800ff17c8bfSgjelinek 
3801ff17c8bfSgjelinek 	/*
3802ff17c8bfSgjelinek 	 * The clone hook is optional.  If it exists, use the hook for
3803ff17c8bfSgjelinek 	 * cloning, otherwise use the built-in clone support
3804ff17c8bfSgjelinek 	 */
3805ff17c8bfSgjelinek 	if (cmdbuf[0] != '\0') {
3806ff17c8bfSgjelinek 		/* Run the clone hook */
3807c75cc341S 		status = do_subproc(cmdbuf);
3808ff17c8bfSgjelinek 		if ((status = subproc_status(gettext("brand-specific clone"),
3809ff17c8bfSgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
3810ff17c8bfSgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
3811ff17c8bfSgjelinek 				sub_usage(SHELP_CLONE, CMD_CLONE);
3812ff17c8bfSgjelinek 			err = Z_ERR;
3813ff17c8bfSgjelinek 			goto done;
3814ff17c8bfSgjelinek 		}
3815ff17c8bfSgjelinek 
3816ff17c8bfSgjelinek 		if (brand_help)
3817ff17c8bfSgjelinek 			return (Z_OK);
3818ff17c8bfSgjelinek 
3819ff17c8bfSgjelinek 	} else {
3820ff17c8bfSgjelinek 		/* If just help, we're done since there is no brand help. */
3821ff17c8bfSgjelinek 		if (brand_help)
3822ff17c8bfSgjelinek 			return (Z_OK);
3823ff17c8bfSgjelinek 
3824ff17c8bfSgjelinek 		/* Run the built-in clone support. */
3825ff17c8bfSgjelinek 
3826ff17c8bfSgjelinek 		/* The only explicit built-in method is "copy". */
3827ff17c8bfSgjelinek 		if (method != NULL && strcmp(method, "copy") != 0) {
3828ff17c8bfSgjelinek 			sub_usage(SHELP_CLONE, CMD_CLONE);
3829ff17c8bfSgjelinek 			err = Z_USAGE;
3830ff17c8bfSgjelinek 			goto done;
3831ff17c8bfSgjelinek 		}
3832865e09a4Sgjelinek 
38330b5de56dSgjelinek 		if (snapshot != NULL) {
3834ff17c8bfSgjelinek 			err = clone_snapshot_zfs(snapshot, zonepath,
3835ff17c8bfSgjelinek 			    validsnapbuf);
38360b5de56dSgjelinek 		} else {
38370b5de56dSgjelinek 			/*
3838ff17c8bfSgjelinek 			 * We always copy the clone unless the source is ZFS
3839ff17c8bfSgjelinek 			 * and a ZFS clone worked.  We fallback to copying if
3840ff17c8bfSgjelinek 			 * the ZFS clone fails for some reason.
38410b5de56dSgjelinek 			 */
38420b5de56dSgjelinek 			err = Z_ERR;
38430b5de56dSgjelinek 			if (method == NULL && is_zonepath_zfs(source_zonepath))
3844ff17c8bfSgjelinek 				err = clone_zfs(source_zonepath, zonepath,
3845ff17c8bfSgjelinek 				    presnapbuf, postsnapbuf);
3846865e09a4Sgjelinek 
38475cd08338Sgjelinek 			if (err != Z_OK)
38480b5de56dSgjelinek 				err = clone_copy(source_zonepath, zonepath);
38490b5de56dSgjelinek 		}
3850ff17c8bfSgjelinek 	}
3851865e09a4Sgjelinek 
3852ff17c8bfSgjelinek 	if (err == Z_OK && postcmdbuf[0] != '\0') {
3853ff17c8bfSgjelinek 		status = do_subproc(postcmdbuf);
3854ff17c8bfSgjelinek 		if ((err = subproc_status("postclone", status, B_FALSE))
3855ff17c8bfSgjelinek 		    != ZONE_SUBPROC_OK) {
3856ff17c8bfSgjelinek 			zerror(gettext("post-clone configuration failed."));
3857ff17c8bfSgjelinek 			err = Z_ERR;
3858ff17c8bfSgjelinek 		}
3859ff17c8bfSgjelinek 	}
3860865e09a4Sgjelinek 
3861865e09a4Sgjelinek done:
38629acbbeafSnn35248 	/*
38639acbbeafSnn35248 	 * If everything went well, we mark the zone as installed.
38649acbbeafSnn35248 	 */
38659acbbeafSnn35248 	if (err == Z_OK) {
38669acbbeafSnn35248 		err = zone_set_state(target_zone, ZONE_STATE_INSTALLED);
38679acbbeafSnn35248 		if (err != Z_OK) {
38689acbbeafSnn35248 			errno = err;
38699acbbeafSnn35248 			zperror2(target_zone, gettext("could not set state"));
38709acbbeafSnn35248 		}
38719acbbeafSnn35248 	}
3872ff17c8bfSgjelinek 	if (!brand_help)
3873ff17c8bfSgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
3874865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
3875865e09a4Sgjelinek }
3876865e09a4Sgjelinek 
387707b574eeSgjelinek /*
38780b5de56dSgjelinek  * Used when removing a zonepath after uninstalling or cleaning up after
38790b5de56dSgjelinek  * the move subcommand.  This handles a zonepath that has non-standard
38800b5de56dSgjelinek  * contents so that we will only cleanup the stuff we know about and leave
38810b5de56dSgjelinek  * any user data alone.
388207b574eeSgjelinek  *
38830b5de56dSgjelinek  * If the "all" parameter is true then we should remove the whole zonepath
38840b5de56dSgjelinek  * even if it has non-standard files/directories in it.  This can be used when
38850b5de56dSgjelinek  * we need to cleanup after moving the zonepath across file systems.
38860b5de56dSgjelinek  *
38870b5de56dSgjelinek  * We "exec" the RMCOMMAND so that the returned status is that of RMCOMMAND
38880b5de56dSgjelinek  * and not the shell.
388907b574eeSgjelinek  */
389007b574eeSgjelinek static int
38910b5de56dSgjelinek cleanup_zonepath(char *zonepath, boolean_t all)
389207b574eeSgjelinek {
389307b574eeSgjelinek 	int		status;
38940b5de56dSgjelinek 	int		i;
38950b5de56dSgjelinek 	boolean_t	non_std = B_FALSE;
38960b5de56dSgjelinek 	struct dirent	*dp;
38970b5de56dSgjelinek 	DIR		*dirp;
3898d9e728a2Sgjelinek 			/*
3899d9e728a2Sgjelinek 			 * The SUNWattached.xml file is expected since it might
3900d9e728a2Sgjelinek 			 * exist if the zone was force-attached after a
3901d9e728a2Sgjelinek 			 * migration.
3902d9e728a2Sgjelinek 			 */
3903d9e728a2Sgjelinek 	char		*std_entries[] = {"dev", "lu", "root",
3904d9e728a2Sgjelinek 			    "SUNWattached.xml", NULL};
39050b5de56dSgjelinek 			/* (MAXPATHLEN * 3) is for the 3 std_entries dirs */
39060b5de56dSgjelinek 	char		cmdbuf[sizeof (RMCOMMAND) + (MAXPATHLEN * 3) + 64];
390707b574eeSgjelinek 
390807b574eeSgjelinek 	/*
39090b5de56dSgjelinek 	 * We shouldn't need these checks but lets be paranoid since we
39100b5de56dSgjelinek 	 * could blow away the whole system here if we got the wrong zonepath.
391107b574eeSgjelinek 	 */
39120b5de56dSgjelinek 	if (*zonepath == NULL || strcmp(zonepath, "/") == 0) {
39130b5de56dSgjelinek 		(void) fprintf(stderr, "invalid zonepath '%s'\n", zonepath);
39140b5de56dSgjelinek 		return (Z_INVAL);
39150b5de56dSgjelinek 	}
39160b5de56dSgjelinek 
391707b574eeSgjelinek 	/*
39180b5de56dSgjelinek 	 * If the dirpath is already gone (maybe it was manually removed) then
39190b5de56dSgjelinek 	 * we just return Z_OK so that the cleanup is successful.
392007b574eeSgjelinek 	 */
39210b5de56dSgjelinek 	if ((dirp = opendir(zonepath)) == NULL)
39220b5de56dSgjelinek 		return (Z_OK);
39230b5de56dSgjelinek 
39240b5de56dSgjelinek 	/*
39250b5de56dSgjelinek 	 * Look through the zonepath directory to see if there are any
39260b5de56dSgjelinek 	 * non-standard files/dirs.  Also skip .zfs since that might be
39270b5de56dSgjelinek 	 * there but we'll handle ZFS file systems as a special case.
39280b5de56dSgjelinek 	 */
39290b5de56dSgjelinek 	while ((dp = readdir(dirp)) != NULL) {
39300b5de56dSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
39310b5de56dSgjelinek 		    strcmp(dp->d_name, "..") == 0 ||
39320b5de56dSgjelinek 		    strcmp(dp->d_name, ".zfs") == 0)
39330b5de56dSgjelinek 			continue;
39340b5de56dSgjelinek 
39350b5de56dSgjelinek 		for (i = 0; std_entries[i] != NULL; i++)
39360b5de56dSgjelinek 			if (strcmp(dp->d_name, std_entries[i]) == 0)
39370b5de56dSgjelinek 				break;
39380b5de56dSgjelinek 
39390b5de56dSgjelinek 		if (std_entries[i] == NULL)
39400b5de56dSgjelinek 			non_std = B_TRUE;
39410b5de56dSgjelinek 	}
39420b5de56dSgjelinek 	(void) closedir(dirp);
39430b5de56dSgjelinek 
39440b5de56dSgjelinek 	if (!all && non_std) {
39450b5de56dSgjelinek 		/*
39460b5de56dSgjelinek 		 * There are extra, non-standard directories/files in the
39470b5de56dSgjelinek 		 * zonepath so we don't want to remove the zonepath.  We
39480b5de56dSgjelinek 		 * just want to remove the standard directories and leave
39490b5de56dSgjelinek 		 * the user data alone.
39500b5de56dSgjelinek 		 */
39510b5de56dSgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND);
39520b5de56dSgjelinek 
39530b5de56dSgjelinek 		for (i = 0; std_entries[i] != NULL; i++) {
39540b5de56dSgjelinek 			char tmpbuf[MAXPATHLEN];
39550b5de56dSgjelinek 
39560b5de56dSgjelinek 			if (snprintf(tmpbuf, sizeof (tmpbuf), " %s/%s",
39570b5de56dSgjelinek 			    zonepath, std_entries[i]) >= sizeof (tmpbuf) ||
39580b5de56dSgjelinek 			    strlcat(cmdbuf, tmpbuf, sizeof (cmdbuf)) >=
39590b5de56dSgjelinek 			    sizeof (cmdbuf)) {
39600b5de56dSgjelinek 				(void) fprintf(stderr,
39610b5de56dSgjelinek 				    gettext("path is too long\n"));
39620b5de56dSgjelinek 				return (Z_INVAL);
39630b5de56dSgjelinek 			}
396407b574eeSgjelinek 		}
396507b574eeSgjelinek 
396607b574eeSgjelinek 		status = do_subproc(cmdbuf);
396707b574eeSgjelinek 
39680b5de56dSgjelinek 		(void) fprintf(stderr, gettext("WARNING: Unable to completely "
39690b5de56dSgjelinek 		    "remove %s\nbecause it contains additional user data.  "
39700b5de56dSgjelinek 		    "Only the standard directory\nentries have been "
39710b5de56dSgjelinek 		    "removed.\n"),
39720b5de56dSgjelinek 		    zonepath);
397307b574eeSgjelinek 
39749acbbeafSnn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39759acbbeafSnn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39760b5de56dSgjelinek 	}
39770b5de56dSgjelinek 
39780b5de56dSgjelinek 	/*
39790b5de56dSgjelinek 	 * There is nothing unexpected in the zonepath, try to get rid of the
39800b5de56dSgjelinek 	 * whole zonepath directory.
39810b5de56dSgjelinek 	 *
39820b5de56dSgjelinek 	 * If the zonepath is its own zfs file system, try to destroy the
39830b5de56dSgjelinek 	 * file system.  If that fails for some reason (e.g. it has clones)
39840b5de56dSgjelinek 	 * then we'll just remove the contents of the zonepath.
39850b5de56dSgjelinek 	 */
39860b5de56dSgjelinek 	if (is_zonepath_zfs(zonepath)) {
39870b5de56dSgjelinek 		if (destroy_zfs(zonepath) == Z_OK)
39880b5de56dSgjelinek 			return (Z_OK);
39890b5de56dSgjelinek 		(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND
39900b5de56dSgjelinek 		    " %s/*", zonepath);
39910b5de56dSgjelinek 		status = do_subproc(cmdbuf);
39929acbbeafSnn35248 		return ((subproc_status(RMCOMMAND, status, B_TRUE) ==
39939acbbeafSnn35248 		    ZONE_SUBPROC_OK) ? Z_OK : Z_ERR);
39940b5de56dSgjelinek 	}
39950b5de56dSgjelinek 
39960b5de56dSgjelinek 	(void) snprintf(cmdbuf, sizeof (cmdbuf), "exec " RMCOMMAND " %s",
39970b5de56dSgjelinek 	    zonepath);
39980b5de56dSgjelinek 	status = do_subproc(cmdbuf);
39999acbbeafSnn35248 
40009acbbeafSnn35248 	return ((subproc_status(RMCOMMAND, status, B_TRUE) == ZONE_SUBPROC_OK)
40019acbbeafSnn35248 	    ? Z_OK : Z_ERR);
400207b574eeSgjelinek }
400307b574eeSgjelinek 
4004865e09a4Sgjelinek static int
4005865e09a4Sgjelinek move_func(int argc, char *argv[])
4006865e09a4Sgjelinek {
4007865e09a4Sgjelinek 	char *new_zonepath = NULL;
4008865e09a4Sgjelinek 	int lockfd;
4009865e09a4Sgjelinek 	int err, arg;
4010865e09a4Sgjelinek 	char zonepath[MAXPATHLEN];
4011865e09a4Sgjelinek 	zone_dochandle_t handle;
4012865e09a4Sgjelinek 	boolean_t fast;
40130b5de56dSgjelinek 	boolean_t is_zfs = B_FALSE;
40140094b373Sjv227347 	boolean_t root_fs_mounted = B_FALSE;
40150b5de56dSgjelinek 	struct dirent *dp;
40160b5de56dSgjelinek 	DIR *dirp;
40170b5de56dSgjelinek 	boolean_t empty = B_TRUE;
4018865e09a4Sgjelinek 	boolean_t revert;
4019865e09a4Sgjelinek 	struct stat zonepath_buf;
4020865e09a4Sgjelinek 	struct stat new_zonepath_buf;
40210094b373Sjv227347 	zone_mounts_t mounts;
4022865e09a4Sgjelinek 
4023865e09a4Sgjelinek 	if (zonecfg_in_alt_root()) {
4024865e09a4Sgjelinek 		zerror(gettext("cannot move zone in alternate root"));
4025865e09a4Sgjelinek 		return (Z_ERR);
4026865e09a4Sgjelinek 	}
4027865e09a4Sgjelinek 
4028865e09a4Sgjelinek 	optind = 0;
4029865e09a4Sgjelinek 	if ((arg = getopt(argc, argv, "?")) != EOF) {
4030865e09a4Sgjelinek 		switch (arg) {
4031865e09a4Sgjelinek 		case '?':
4032865e09a4Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
4033865e09a4Sgjelinek 			return (optopt == '?' ? Z_OK : Z_USAGE);
4034865e09a4Sgjelinek 		default:
4035865e09a4Sgjelinek 			sub_usage(SHELP_MOVE, CMD_MOVE);
4036865e09a4Sgjelinek 			return (Z_USAGE);
4037865e09a4Sgjelinek 		}
4038865e09a4Sgjelinek 	}
4039865e09a4Sgjelinek 	if (argc != (optind + 1)) {
4040865e09a4Sgjelinek 		sub_usage(SHELP_MOVE, CMD_MOVE);
4041865e09a4Sgjelinek 		return (Z_USAGE);
4042865e09a4Sgjelinek 	}
4043865e09a4Sgjelinek 	new_zonepath = argv[optind];
40449acbbeafSnn35248 	if (sanity_check(target_zone, CMD_MOVE, B_FALSE, B_TRUE, B_FALSE)
40459acbbeafSnn35248 	    != Z_OK)
4046865e09a4Sgjelinek 		return (Z_ERR);
4047ce28b40eSzt129084 	if (verify_details(CMD_MOVE, argv) != Z_OK)
4048865e09a4Sgjelinek 		return (Z_ERR);
4049865e09a4Sgjelinek 
4050865e09a4Sgjelinek 	/*
4051865e09a4Sgjelinek 	 * Check out the new zonepath.  This has the side effect of creating
4052865e09a4Sgjelinek 	 * a directory for the new zonepath.  We depend on this later when we
40530b5de56dSgjelinek 	 * stat to see if we are doing a cross file system move or not.
4054865e09a4Sgjelinek 	 */
4055865e09a4Sgjelinek 	if (validate_zonepath(new_zonepath, CMD_MOVE) != Z_OK)
4056865e09a4Sgjelinek 		return (Z_ERR);
4057865e09a4Sgjelinek 
4058865e09a4Sgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4059865e09a4Sgjelinek 	    != Z_OK) {
4060865e09a4Sgjelinek 		errno = err;
4061865e09a4Sgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
4062865e09a4Sgjelinek 		return (Z_ERR);
4063865e09a4Sgjelinek 	}
4064865e09a4Sgjelinek 
4065865e09a4Sgjelinek 	if (stat(zonepath, &zonepath_buf) == -1) {
4066865e09a4Sgjelinek 		zperror(gettext("could not stat zone path"), B_FALSE);
4067865e09a4Sgjelinek 		return (Z_ERR);
4068865e09a4Sgjelinek 	}
4069865e09a4Sgjelinek 
4070865e09a4Sgjelinek 	if (stat(new_zonepath, &new_zonepath_buf) == -1) {
4071865e09a4Sgjelinek 		zperror(gettext("could not stat new zone path"), B_FALSE);
4072865e09a4Sgjelinek 		return (Z_ERR);
4073865e09a4Sgjelinek 	}
4074865e09a4Sgjelinek 
40750b5de56dSgjelinek 	/*
40760b5de56dSgjelinek 	 * Check if the destination directory is empty.
40770b5de56dSgjelinek 	 */
40780b5de56dSgjelinek 	if ((dirp = opendir(new_zonepath)) == NULL) {
40790b5de56dSgjelinek 		zperror(gettext("could not open new zone path"), B_FALSE);
40800b5de56dSgjelinek 		return (Z_ERR);
40810b5de56dSgjelinek 	}
40820b5de56dSgjelinek 	while ((dp = readdir(dirp)) != (struct dirent *)0) {
40830b5de56dSgjelinek 		if (strcmp(dp->d_name, ".") == 0 ||
40840b5de56dSgjelinek 		    strcmp(dp->d_name, "..") == 0)
40850b5de56dSgjelinek 			continue;
40860b5de56dSgjelinek 		empty = B_FALSE;
40870b5de56dSgjelinek 		break;
40880b5de56dSgjelinek 	}
40890b5de56dSgjelinek 	(void) closedir(dirp);
40900b5de56dSgjelinek 
40910b5de56dSgjelinek 	/* Error if there is anything in the destination directory. */
40920b5de56dSgjelinek 	if (!empty) {
40930b5de56dSgjelinek 		(void) fprintf(stderr, gettext("could not move zone to %s: "
40940b5de56dSgjelinek 		    "directory not empty\n"), new_zonepath);
40950b5de56dSgjelinek 		return (Z_ERR);
40960b5de56dSgjelinek 	}
40970b5de56dSgjelinek 
40980094b373Sjv227347 	/*
40990094b373Sjv227347 	 * Collect information about mounts within the zone's zonepath.
41000094b373Sjv227347 	 * Overlay mounts on the zone's root directory are erroneous.
41010094b373Sjv227347 	 * Bail if we encounter any unexpected mounts.
41020094b373Sjv227347 	 */
41030094b373Sjv227347 	if (zone_mounts_init(&mounts, zonepath) != 0)
4104865e09a4Sgjelinek 		return (Z_ERR);
41050094b373Sjv227347 	if (mounts.num_root_overlay_mounts != 0) {
41060094b373Sjv227347 		zerror(gettext("%d overlay mount(s) detected on %s/root."),
41070094b373Sjv227347 		    mounts.num_root_overlay_mounts, zonepath);
41080094b373Sjv227347 		goto err_and_mounts_destroy;
4109865e09a4Sgjelinek 	}
41100094b373Sjv227347 	if (mounts.num_unexpected_mounts != 0)
41110094b373Sjv227347 		goto err_and_mounts_destroy;
4112865e09a4Sgjelinek 
4113865e09a4Sgjelinek 	/*
4114865e09a4Sgjelinek 	 * Check if we are moving in the same file system and can do a fast
4115865e09a4Sgjelinek 	 * move or if we are crossing file systems and have to copy the data.
4116865e09a4Sgjelinek 	 */
4117865e09a4Sgjelinek 	fast = (zonepath_buf.st_dev == new_zonepath_buf.st_dev);
4118865e09a4Sgjelinek 
4119865e09a4Sgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
4120865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
41210094b373Sjv227347 		goto err_and_mounts_destroy;
4122865e09a4Sgjelinek 	}
4123865e09a4Sgjelinek 
4124865e09a4Sgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4125865e09a4Sgjelinek 		errno = err;
4126865e09a4Sgjelinek 		zperror(cmd_to_str(CMD_MOVE), B_TRUE);
41270094b373Sjv227347 		goto err_and_fini_handle;
4128865e09a4Sgjelinek 	}
4129865e09a4Sgjelinek 
4130ff17c8bfSgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4131865e09a4Sgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4132865e09a4Sgjelinek 		    "zoneadm");
41330094b373Sjv227347 		goto err_and_fini_handle;
4134865e09a4Sgjelinek 	}
4135865e09a4Sgjelinek 
4136865e09a4Sgjelinek 	/*
41370094b373Sjv227347 	 * Unmount the zone's root filesystem before we move the zone's
41380094b373Sjv227347 	 * zonepath.
41390094b373Sjv227347 	 */
41400094b373Sjv227347 	if (zone_unmount_rootfs(&mounts, zonepath, B_FALSE) != 0)
41410094b373Sjv227347 		goto err_and_rele_lockfile;
41420094b373Sjv227347 
41430094b373Sjv227347 	/*
41440b5de56dSgjelinek 	 * We're making some file system changes now so we have to clean up
41450b5de56dSgjelinek 	 * the file system before we are done.  This will either clean up the
4146865e09a4Sgjelinek 	 * new zonepath if the zonecfg update failed or it will clean up the
4147865e09a4Sgjelinek 	 * old zonepath if everything is ok.
4148865e09a4Sgjelinek 	 */
4149865e09a4Sgjelinek 	revert = B_TRUE;
4150865e09a4Sgjelinek 
41510b5de56dSgjelinek 	if (is_zonepath_zfs(zonepath) &&
41520b5de56dSgjelinek 	    move_zfs(zonepath, new_zonepath) != Z_ERR) {
41530b5de56dSgjelinek 		is_zfs = B_TRUE;
41540b5de56dSgjelinek 
41550b5de56dSgjelinek 	} else if (fast) {
4156865e09a4Sgjelinek 		/* same file system, use rename for a quick move */
4157865e09a4Sgjelinek 
4158865e09a4Sgjelinek 		/*
4159865e09a4Sgjelinek 		 * Remove the new_zonepath directory that got created above
4160865e09a4Sgjelinek 		 * during the validation.  It gets in the way of the rename.
4161865e09a4Sgjelinek 		 */
4162865e09a4Sgjelinek 		if (rmdir(new_zonepath) != 0) {
4163865e09a4Sgjelinek 			zperror(gettext("could not rmdir new zone path"),
4164865e09a4Sgjelinek 			    B_FALSE);
41650094b373Sjv227347 			(void) zone_mount_rootfs(&mounts, zonepath);
41660094b373Sjv227347 			goto err_and_rele_lockfile;
4167865e09a4Sgjelinek 		}
4168865e09a4Sgjelinek 
4169865e09a4Sgjelinek 		if (rename(zonepath, new_zonepath) != 0) {
4170865e09a4Sgjelinek 			/*
4171865e09a4Sgjelinek 			 * If this fails we don't need to do all of the
4172865e09a4Sgjelinek 			 * cleanup that happens for the rest of the code
4173865e09a4Sgjelinek 			 * so just return from this error.
4174865e09a4Sgjelinek 			 */
4175865e09a4Sgjelinek 			zperror(gettext("could not move zone"), B_FALSE);
41760094b373Sjv227347 			(void) zone_mount_rootfs(&mounts, zonepath);
41770094b373Sjv227347 			goto err_and_rele_lockfile;
4178865e09a4Sgjelinek 		}
4179865e09a4Sgjelinek 
4180865e09a4Sgjelinek 	} else {
41810b5de56dSgjelinek 		/*
41820b5de56dSgjelinek 		 * Attempt to create a ZFS fs for the new zonepath.  As usual,
41830b5de56dSgjelinek 		 * we don't care if this works or not since we always have the
41840b5de56dSgjelinek 		 * default behavior of a simple directory for the zonepath.
41850b5de56dSgjelinek 		 */
41860b5de56dSgjelinek 		create_zfs_zonepath(new_zonepath);
41870b5de56dSgjelinek 
4188865e09a4Sgjelinek 		(void) printf(gettext(
41890b5de56dSgjelinek 		    "Moving across file systems; copying zonepath %s..."),
4190865e09a4Sgjelinek 		    zonepath);
4191865e09a4Sgjelinek 		(void) fflush(stdout);
4192865e09a4Sgjelinek 
4193865e09a4Sgjelinek 		err = copy_zone(zonepath, new_zonepath);
4194865e09a4Sgjelinek 
4195865e09a4Sgjelinek 		(void) printf("\n");
4196865e09a4Sgjelinek 		if (err != Z_OK)
4197865e09a4Sgjelinek 			goto done;
4198865e09a4Sgjelinek 	}
4199865e09a4Sgjelinek 
42000094b373Sjv227347 	/*
42010094b373Sjv227347 	 * Mount the zone's root filesystem in the new zonepath if there was
42020094b373Sjv227347 	 * a root mount prior to the move.
42030094b373Sjv227347 	 */
42040094b373Sjv227347 	if (zone_mount_rootfs(&mounts, new_zonepath) != 0) {
42050094b373Sjv227347 		err = Z_ERR;
42060094b373Sjv227347 		goto done;
42070094b373Sjv227347 	}
42080094b373Sjv227347 	root_fs_mounted = B_TRUE;
42090094b373Sjv227347 
4210865e09a4Sgjelinek 	if ((err = zonecfg_set_zonepath(handle, new_zonepath)) != Z_OK) {
4211865e09a4Sgjelinek 		errno = err;
4212865e09a4Sgjelinek 		zperror(gettext("could not set new zonepath"), B_TRUE);
4213865e09a4Sgjelinek 		goto done;
4214865e09a4Sgjelinek 	}
4215865e09a4Sgjelinek 
4216865e09a4Sgjelinek 	if ((err = zonecfg_save(handle)) != Z_OK) {
4217865e09a4Sgjelinek 		errno = err;
4218865e09a4Sgjelinek 		zperror(gettext("zonecfg save failed"), B_TRUE);
4219865e09a4Sgjelinek 		goto done;
4220865e09a4Sgjelinek 	}
4221865e09a4Sgjelinek 
4222865e09a4Sgjelinek 	revert = B_FALSE;
4223865e09a4Sgjelinek 
4224865e09a4Sgjelinek done:
4225865e09a4Sgjelinek 	zonecfg_fini_handle(handle);
4226ff17c8bfSgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
4227865e09a4Sgjelinek 
4228865e09a4Sgjelinek 	/*
42290b5de56dSgjelinek 	 * Clean up the file system based on how things went.  We either
4230865e09a4Sgjelinek 	 * clean up the new zonepath if the operation failed for some reason
4231865e09a4Sgjelinek 	 * or we clean up the old zonepath if everything is ok.
4232865e09a4Sgjelinek 	 */
4233865e09a4Sgjelinek 	if (revert) {
42340094b373Sjv227347 		/*
42350094b373Sjv227347 		 * Check for the unlikely scenario in which the zone's
42360094b373Sjv227347 		 * zonepath and its root file system moved but libzonecfg
42370094b373Sjv227347 		 * couldn't save the new zonepath to the zone's configuration
42380094b373Sjv227347 		 * file.  The mounted root filesystem must be unmounted before
42390094b373Sjv227347 		 * zoneadm restores the zone's zonepath.
42400094b373Sjv227347 		 */
42410094b373Sjv227347 		if (root_fs_mounted && zone_unmount_rootfs(&mounts,
42420094b373Sjv227347 		    new_zonepath, B_TRUE) != 0) {
42430094b373Sjv227347 			/*
42440094b373Sjv227347 			 * We can't forcibly unmount the zone's root file system
42450094b373Sjv227347 			 * from the new zonepath.  Bail!
42460094b373Sjv227347 			 */
42470094b373Sjv227347 			zerror(gettext("fatal error: cannot unmount %s/root\n"),
42480094b373Sjv227347 			    new_zonepath);
42490094b373Sjv227347 			goto err_and_mounts_destroy;
42500094b373Sjv227347 		}
42510094b373Sjv227347 
4252865e09a4Sgjelinek 		/* The zonecfg update failed, cleanup the new zonepath. */
42530b5de56dSgjelinek 		if (is_zfs) {
42540b5de56dSgjelinek 			if (move_zfs(new_zonepath, zonepath) == Z_ERR) {
42550b5de56dSgjelinek 				(void) fprintf(stderr, gettext("could not "
42560b5de56dSgjelinek 				    "restore zonepath, the zfs mountpoint is "
42570b5de56dSgjelinek 				    "set as:\n%s\n"), new_zonepath);
42580b5de56dSgjelinek 				/*
42590b5de56dSgjelinek 				 * err is already != Z_OK since we're reverting
42600b5de56dSgjelinek 				 */
42610094b373Sjv227347 			} else {
42620094b373Sjv227347 				(void) zone_mount_rootfs(&mounts, zonepath);
42630b5de56dSgjelinek 			}
42640b5de56dSgjelinek 		} else if (fast) {
4265865e09a4Sgjelinek 			if (rename(new_zonepath, zonepath) != 0) {
4266865e09a4Sgjelinek 				zperror(gettext("could not restore zonepath"),
4267865e09a4Sgjelinek 				    B_FALSE);
4268865e09a4Sgjelinek 				/*
4269865e09a4Sgjelinek 				 * err is already != Z_OK since we're reverting
4270865e09a4Sgjelinek 				 */
42710094b373Sjv227347 			} else {
42720094b373Sjv227347 				(void) zone_mount_rootfs(&mounts, zonepath);
4273865e09a4Sgjelinek 			}
4274865e09a4Sgjelinek 		} else {
4275865e09a4Sgjelinek 			(void) printf(gettext("Cleaning up zonepath %s..."),
4276865e09a4Sgjelinek 			    new_zonepath);
4277865e09a4Sgjelinek 			(void) fflush(stdout);
42780b5de56dSgjelinek 			err = cleanup_zonepath(new_zonepath, B_TRUE);
4279865e09a4Sgjelinek 			(void) printf("\n");
4280865e09a4Sgjelinek 
428107b574eeSgjelinek 			if (err != Z_OK) {
4282865e09a4Sgjelinek 				errno = err;
4283865e09a4Sgjelinek 				zperror(gettext("could not remove new "
4284865e09a4Sgjelinek 				    "zonepath"), B_TRUE);
4285865e09a4Sgjelinek 			} else {
4286865e09a4Sgjelinek 				/*
4287865e09a4Sgjelinek 				 * Because we're reverting we know the mainline
4288865e09a4Sgjelinek 				 * code failed but we just reused the err
4289865e09a4Sgjelinek 				 * variable so we reset it back to Z_ERR.
4290865e09a4Sgjelinek 				 */
4291865e09a4Sgjelinek 				err = Z_ERR;
4292865e09a4Sgjelinek 			}
4293865e09a4Sgjelinek 
42940094b373Sjv227347 			(void) zone_mount_rootfs(&mounts, zonepath);
42950094b373Sjv227347 		}
4296865e09a4Sgjelinek 	} else {
4297865e09a4Sgjelinek 		/* The move was successful, cleanup the old zonepath. */
42980b5de56dSgjelinek 		if (!is_zfs && !fast) {
4299865e09a4Sgjelinek 			(void) printf(
4300865e09a4Sgjelinek 			    gettext("Cleaning up zonepath %s..."), zonepath);
4301865e09a4Sgjelinek 			(void) fflush(stdout);
43020b5de56dSgjelinek 			err = cleanup_zonepath(zonepath, B_TRUE);
4303865e09a4Sgjelinek 			(void) printf("\n");
4304865e09a4Sgjelinek 
430507b574eeSgjelinek 			if (err != Z_OK) {
4306865e09a4Sgjelinek 				errno = err;
4307865e09a4Sgjelinek 				zperror(gettext("could not remove zonepath"),
4308865e09a4Sgjelinek 				    B_TRUE);
4309865e09a4Sgjelinek 			}
4310865e09a4Sgjelinek 		}
4311865e09a4Sgjelinek 	}
4312865e09a4Sgjelinek 
43130094b373Sjv227347 	zone_mounts_destroy(&mounts);
4314865e09a4Sgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
43150094b373Sjv227347 
43160094b373Sjv227347 err_and_rele_lockfile:
43170094b373Sjv227347 	zonecfg_release_lock_file(target_zone, lockfd);
43180094b373Sjv227347 err_and_fini_handle:
43190094b373Sjv227347 	zonecfg_fini_handle(handle);
43200094b373Sjv227347 err_and_mounts_destroy:
43210094b373Sjv227347 	zone_mounts_destroy(&mounts);
43220094b373Sjv227347 	return (Z_ERR);
4323865e09a4Sgjelinek }
4324865e09a4Sgjelinek 
4325ff17c8bfSgjelinek /* ARGSUSED */
4326ee519a1fSgjelinek static int
4327ee519a1fSgjelinek detach_func(int argc, char *argv[])
4328ee519a1fSgjelinek {
432971443f5aS 	int lockfd = -1;
4330ee519a1fSgjelinek 	int err, arg;
4331ee519a1fSgjelinek 	char zonepath[MAXPATHLEN];
433237774979Sgjelinek 	char cmdbuf[MAXPATHLEN];
4333ff17c8bfSgjelinek 	char precmdbuf[MAXPATHLEN];
43348cd327d5Sgjelinek 	boolean_t execute = B_TRUE;
4335ff17c8bfSgjelinek 	boolean_t brand_help = B_FALSE;
433637774979Sgjelinek 	brand_handle_t bh = NULL;
4337ff17c8bfSgjelinek 	int status;
4338ee519a1fSgjelinek 
4339ee519a1fSgjelinek 	if (zonecfg_in_alt_root()) {
4340ee519a1fSgjelinek 		zerror(gettext("cannot detach zone in alternate root"));
4341ee519a1fSgjelinek 		return (Z_ERR);
4342ee519a1fSgjelinek 	}
4343ee519a1fSgjelinek 
4344ff17c8bfSgjelinek 	/* Check the argv string for args we handle internally */
4345ee519a1fSgjelinek 	optind = 0;
4346ff17c8bfSgjelinek 	opterr = 0;
4347ff17c8bfSgjelinek 	while ((arg = getopt(argc, argv, "?n")) != EOF) {
4348ee519a1fSgjelinek 		switch (arg) {
4349ee519a1fSgjelinek 		case '?':
4350ff17c8bfSgjelinek 			if (optopt == '?') {
4351ee519a1fSgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
4352ff17c8bfSgjelinek 				brand_help = B_TRUE;
4353ff17c8bfSgjelinek 			}
4354ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4355ff17c8bfSgjelinek 			break;
43568cd327d5Sgjelinek 		case 'n':
43578cd327d5Sgjelinek 			execute = B_FALSE;
43588cd327d5Sgjelinek 			break;
4359ee519a1fSgjelinek 		default:
4360ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4361ff17c8bfSgjelinek 			break;
4362ee519a1fSgjelinek 		}
4363ee519a1fSgjelinek 	}
43649acbbeafSnn35248 
4365ff17c8bfSgjelinek 	if (brand_help)
4366ff17c8bfSgjelinek 		execute = B_FALSE;
4367ff17c8bfSgjelinek 
43688cd327d5Sgjelinek 	if (execute) {
43699acbbeafSnn35248 		if (sanity_check(target_zone, CMD_DETACH, B_FALSE, B_TRUE,
43709acbbeafSnn35248 		    B_FALSE) != Z_OK)
4371ee519a1fSgjelinek 			return (Z_ERR);
4372ce28b40eSzt129084 		if (verify_details(CMD_DETACH, argv) != Z_OK)
4373ee519a1fSgjelinek 			return (Z_ERR);
43748cd327d5Sgjelinek 	} else {
43758cd327d5Sgjelinek 		/*
43768cd327d5Sgjelinek 		 * We want a dry-run to work for a non-privileged user so we
43778cd327d5Sgjelinek 		 * only do minimal validation.
43788cd327d5Sgjelinek 		 */
43798cd327d5Sgjelinek 		if (target_zone == NULL) {
43808cd327d5Sgjelinek 			zerror(gettext("no zone specified"));
43818cd327d5Sgjelinek 			return (Z_ERR);
43828cd327d5Sgjelinek 		}
43838cd327d5Sgjelinek 
43848cd327d5Sgjelinek 		if (strcmp(target_zone, GLOBAL_ZONENAME) == 0) {
43858cd327d5Sgjelinek 			zerror(gettext("%s operation is invalid for the "
43868cd327d5Sgjelinek 			    "global zone."), cmd_to_str(CMD_DETACH));
43878cd327d5Sgjelinek 			return (Z_ERR);
43888cd327d5Sgjelinek 		}
43898cd327d5Sgjelinek 	}
4390ee519a1fSgjelinek 
4391ee519a1fSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath, sizeof (zonepath)))
4392ee519a1fSgjelinek 	    != Z_OK) {
4393ee519a1fSgjelinek 		errno = err;
4394ee519a1fSgjelinek 		zperror2(target_zone, gettext("could not get zone path"));
4395ee519a1fSgjelinek 		return (Z_ERR);
4396ee519a1fSgjelinek 	}
4397ee519a1fSgjelinek 
4398ff17c8bfSgjelinek 	/* Fetch the detach and predetach hooks from the brand configuration. */
439937774979Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
440037774979Sgjelinek 		zerror(gettext("missing or invalid brand"));
440137774979Sgjelinek 		return (Z_ERR);
440237774979Sgjelinek 	}
440337774979Sgjelinek 
4404ff17c8bfSgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_detach, target_zone,
4405ff17c8bfSgjelinek 	    zonepath) != Z_OK) {
4406ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing detach resource");
4407ff17c8bfSgjelinek 		brand_close(bh);
4408ff17c8bfSgjelinek 		return (Z_ERR);
4409ff17c8bfSgjelinek 	}
4410ff17c8bfSgjelinek 
4411ff17c8bfSgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_predetach,
4412ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
441337774979Sgjelinek 		zerror("invalid brand configuration: missing predetach "
441437774979Sgjelinek 		    "resource");
441537774979Sgjelinek 		brand_close(bh);
441637774979Sgjelinek 		return (Z_ERR);
441737774979Sgjelinek 	}
441837774979Sgjelinek 	brand_close(bh);
441937774979Sgjelinek 
4420ff17c8bfSgjelinek 	/* Append all options to predetach hook. */
4421ff17c8bfSgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
442237774979Sgjelinek 		return (Z_ERR);
442337774979Sgjelinek 
4424ff17c8bfSgjelinek 	/* Append all options to detach hook. */
4425ff17c8bfSgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
442637774979Sgjelinek 		return (Z_ERR);
442737774979Sgjelinek 
4428ff17c8bfSgjelinek 	if (execute && zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4429ee519a1fSgjelinek 		zerror(gettext("another %s may have an operation in progress."),
4430ee519a1fSgjelinek 		    "zoneadm");
4431ee519a1fSgjelinek 		return (Z_ERR);
4432ee519a1fSgjelinek 	}
4433ee519a1fSgjelinek 
4434ff17c8bfSgjelinek 	/* If we have a brand predetach hook, run it. */
4435ff17c8bfSgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
4436ff17c8bfSgjelinek 		status = do_subproc(precmdbuf);
4437ff17c8bfSgjelinek 		if (subproc_status(gettext("brand-specific predetach"),
4438ff17c8bfSgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
4439ff17c8bfSgjelinek 
444071443f5aS 			if (execute) {
444171443f5aS 				assert(lockfd >= 0);
4442ff17c8bfSgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
444371443f5aS 				lockfd = -1;
444471443f5aS 			}
444571443f5aS 
444671443f5aS 			assert(lockfd == -1);
4447ff17c8bfSgjelinek 			return (Z_ERR);
4448ff17c8bfSgjelinek 		}
4449ff17c8bfSgjelinek 	}
4450ff17c8bfSgjelinek 
4451ff17c8bfSgjelinek 	if (cmdbuf[0] != '\0') {
4452ff17c8bfSgjelinek 		/* Run the detach hook */
4453c75cc341S 		status = do_subproc(cmdbuf);
4454ff17c8bfSgjelinek 		if ((status = subproc_status(gettext("brand-specific detach"),
4455ff17c8bfSgjelinek 		    status, B_FALSE)) != ZONE_SUBPROC_OK) {
4456ff17c8bfSgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
4457ff17c8bfSgjelinek 				sub_usage(SHELP_DETACH, CMD_DETACH);
4458ff17c8bfSgjelinek 
445971443f5aS 			if (execute) {
446071443f5aS 				assert(lockfd >= 0);
4461ff17c8bfSgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
446271443f5aS 				lockfd = -1;
446371443f5aS 			}
4464ff17c8bfSgjelinek 
446571443f5aS 			assert(lockfd == -1);
4466ff17c8bfSgjelinek 			return (Z_ERR);
4467ff17c8bfSgjelinek 		}
4468ff17c8bfSgjelinek 
4469ff17c8bfSgjelinek 	} else {
447071443f5aS 		zone_dochandle_t handle;
447171443f5aS 
4472ff17c8bfSgjelinek 		/* If just help, we're done since there is no brand help. */
447371443f5aS 		if (brand_help) {
447471443f5aS 			assert(lockfd == -1);
4475ff17c8bfSgjelinek 			return (Z_OK);
447671443f5aS 		}
4477ff17c8bfSgjelinek 
4478ff17c8bfSgjelinek 		/*
4479ff17c8bfSgjelinek 		 * Run the built-in detach support.  Just generate a simple
4480ff17c8bfSgjelinek 		 * zone definition XML file and detach.
4481ff17c8bfSgjelinek 		 */
4482ff17c8bfSgjelinek 
4483ff17c8bfSgjelinek 		/* Don't detach the zone if anything is still mounted there */
4484ff17c8bfSgjelinek 		if (execute && zonecfg_find_mounts(zonepath, NULL, NULL)) {
4485ff17c8bfSgjelinek 			(void) fprintf(stderr, gettext("These file systems are "
4486ff17c8bfSgjelinek 			    "mounted on subdirectories of %s.\n"), zonepath);
4487ff17c8bfSgjelinek 			(void) zonecfg_find_mounts(zonepath, zfm_print, NULL);
4488ff17c8bfSgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
4489ff17c8bfSgjelinek 			goto done;
4490ff17c8bfSgjelinek 		}
4491ff17c8bfSgjelinek 
4492ff17c8bfSgjelinek 		if ((handle = zonecfg_init_handle()) == NULL) {
4493ff17c8bfSgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4494ff17c8bfSgjelinek 			err = ZONE_SUBPROC_NOTCOMPLETE;
4495ff17c8bfSgjelinek 			goto done;
4496ff17c8bfSgjelinek 		}
4497ff17c8bfSgjelinek 
4498ff17c8bfSgjelinek 		if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
4499ee519a1fSgjelinek 			errno = err;
4500ff17c8bfSgjelinek 			zperror(cmd_to_str(CMD_DETACH), B_TRUE);
4501ff17c8bfSgjelinek 
450271443f5aS 		} else if ((err = zonecfg_detach_save(handle,
4503ff17c8bfSgjelinek 		    (execute ? 0 : ZONE_DRY_RUN))) != Z_OK) {
4504ff17c8bfSgjelinek 			errno = err;
4505ff17c8bfSgjelinek 			zperror(gettext("saving the detach manifest failed"),
4506ee519a1fSgjelinek 			    B_TRUE);
4507ee519a1fSgjelinek 		}
4508ee519a1fSgjelinek 
4509ff17c8bfSgjelinek 		zonecfg_fini_handle(handle);
451071443f5aS 		if (err != Z_OK)
451171443f5aS 			goto done;
4512ee519a1fSgjelinek 	}
4513ee519a1fSgjelinek 
45148cd327d5Sgjelinek 	/*
45158cd327d5Sgjelinek 	 * Set the zone state back to configured unless we are running with the
45168cd327d5Sgjelinek 	 * no-execute option.
45178cd327d5Sgjelinek 	 */
45188cd327d5Sgjelinek 	if (execute && (err = zone_set_state(target_zone,
45198cd327d5Sgjelinek 	    ZONE_STATE_CONFIGURED)) != Z_OK) {
4520ee519a1fSgjelinek 		errno = err;
4521ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4522ee519a1fSgjelinek 	}
4523ee519a1fSgjelinek 
4524ee519a1fSgjelinek done:
452571443f5aS 	if (execute) {
452671443f5aS 		assert(lockfd >= 0);
4527ff17c8bfSgjelinek 		zonecfg_release_lock_file(target_zone, lockfd);
452871443f5aS 		lockfd = -1;
452971443f5aS 	}
4530ee519a1fSgjelinek 
453171443f5aS 	assert(lockfd == -1);
4532ee519a1fSgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4533ee519a1fSgjelinek }
4534ee519a1fSgjelinek 
4535ee519a1fSgjelinek /*
4536ff17c8bfSgjelinek  * Determine the brand when doing a dry-run attach.  The zone does not have to
4537ff17c8bfSgjelinek  * exist, so we have to read the incoming manifest to determine the zone's
4538ff17c8bfSgjelinek  * brand.
4539ff17c8bfSgjelinek  *
4540ff17c8bfSgjelinek  * Because the manifest has to be processed twice; once to determine the brand
4541ff17c8bfSgjelinek  * and once to do the brand-specific attach logic, we always read it into a tmp
4542ff17c8bfSgjelinek  * file.  This handles the manifest coming from stdin or a regular file.  The
4543ff17c8bfSgjelinek  * tmpname parameter returns the name of the temporary file that the manifest
4544ff17c8bfSgjelinek  * was read into.
4545ee519a1fSgjelinek  */
4546ee519a1fSgjelinek static int
4547ff17c8bfSgjelinek dryrun_get_brand(char *manifest_path, char *tmpname, int size)
45488cd327d5Sgjelinek {
45498cd327d5Sgjelinek 	int fd;
45508cd327d5Sgjelinek 	int err;
4551ff17c8bfSgjelinek 	int res = Z_OK;
45528cd327d5Sgjelinek 	zone_dochandle_t local_handle;
45538cd327d5Sgjelinek 	zone_dochandle_t rem_handle = NULL;
4554ff17c8bfSgjelinek 	int len;
4555ff17c8bfSgjelinek 	int ofd;
4556ff17c8bfSgjelinek 	char buf[512];
45578cd327d5Sgjelinek 
45588cd327d5Sgjelinek 	if (strcmp(manifest_path, "-") == 0) {
4559ff17c8bfSgjelinek 		fd = STDIN_FILENO;
4560ff17c8bfSgjelinek 	} else {
4561ff17c8bfSgjelinek 		if ((fd = open(manifest_path, O_RDONLY)) < 0) {
4562ff17c8bfSgjelinek 			if (getcwd(buf, sizeof (buf)) == NULL)
4563ff17c8bfSgjelinek 				(void) strlcpy(buf, "/", sizeof (buf));
4564ff17c8bfSgjelinek 			zerror(gettext("could not open manifest path %s%s: %s"),
4565ff17c8bfSgjelinek 			    (*manifest_path == '/' ? "" : buf), manifest_path,
4566ff17c8bfSgjelinek 			    strerror(errno));
4567ff17c8bfSgjelinek 			return (Z_ERR);
4568ff17c8bfSgjelinek 		}
4569ff17c8bfSgjelinek 	}
4570ff17c8bfSgjelinek 
4571ff17c8bfSgjelinek 	(void) snprintf(tmpname, size, "/var/run/zone.%d", getpid());
4572ff17c8bfSgjelinek 
4573ff17c8bfSgjelinek 	if ((ofd = open(tmpname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
4574ff17c8bfSgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
4575ff17c8bfSgjelinek 		(void) close(fd);
4576ff17c8bfSgjelinek 		return (Z_ERR);
4577ff17c8bfSgjelinek 	}
4578ff17c8bfSgjelinek 
4579ff17c8bfSgjelinek 	while ((len = read(fd, buf, sizeof (buf))) > 0) {
4580ff17c8bfSgjelinek 		if (write(ofd, buf, len) == -1) {
4581ff17c8bfSgjelinek 			zperror(gettext("could not save manifest"), B_FALSE);
4582ff17c8bfSgjelinek 			(void) close(ofd);
4583ff17c8bfSgjelinek 			(void) close(fd);
4584ff17c8bfSgjelinek 			return (Z_ERR);
4585ff17c8bfSgjelinek 		}
4586ff17c8bfSgjelinek 	}
4587ff17c8bfSgjelinek 
4588ff17c8bfSgjelinek 	if (close(ofd) != 0) {
4589ff17c8bfSgjelinek 		zperror(gettext("could not save manifest"), B_FALSE);
4590ff17c8bfSgjelinek 		(void) close(fd);
4591ff17c8bfSgjelinek 		return (Z_ERR);
4592ff17c8bfSgjelinek 	}
4593ff17c8bfSgjelinek 
4594ff17c8bfSgjelinek 	(void) close(fd);
4595ff17c8bfSgjelinek 
4596ff17c8bfSgjelinek 	if ((fd = open(tmpname, O_RDONLY)) < 0) {
45978cd327d5Sgjelinek 		zperror(gettext("could not open manifest path"), B_FALSE);
45988cd327d5Sgjelinek 		return (Z_ERR);
45998cd327d5Sgjelinek 	}
46008cd327d5Sgjelinek 
46018cd327d5Sgjelinek 	if ((local_handle = zonecfg_init_handle()) == NULL) {
46028cd327d5Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
46038cd327d5Sgjelinek 		res = Z_ERR;
46048cd327d5Sgjelinek 		goto done;
46058cd327d5Sgjelinek 	}
46068cd327d5Sgjelinek 
46078cd327d5Sgjelinek 	if ((rem_handle = zonecfg_init_handle()) == NULL) {
46088cd327d5Sgjelinek 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
46098cd327d5Sgjelinek 		res = Z_ERR;
46108cd327d5Sgjelinek 		goto done;
46118cd327d5Sgjelinek 	}
46128cd327d5Sgjelinek 
46138cd327d5Sgjelinek 	if ((err = zonecfg_attach_manifest(fd, local_handle, rem_handle))
46148cd327d5Sgjelinek 	    != Z_OK) {
46158cd327d5Sgjelinek 		res = Z_ERR;
4616d9e728a2Sgjelinek 
4617d9e728a2Sgjelinek 		if (err == Z_INVALID_DOCUMENT) {
4618d9e728a2Sgjelinek 			struct stat st;
4619d9e728a2Sgjelinek 			char buf[6];
4620d9e728a2Sgjelinek 
4621d9e728a2Sgjelinek 			if (strcmp(manifest_path, "-") == 0) {
4622d9e728a2Sgjelinek 				zerror(gettext("Input is not a valid XML "
4623d9e728a2Sgjelinek 				    "file"));
4624d9e728a2Sgjelinek 				goto done;
4625d9e728a2Sgjelinek 			}
4626d9e728a2Sgjelinek 
4627d9e728a2Sgjelinek 			if (fstat(fd, &st) == -1 || !S_ISREG(st.st_mode)) {
4628d9e728a2Sgjelinek 				zerror(gettext("%s is not an XML file"),
4629d9e728a2Sgjelinek 				    manifest_path);
4630d9e728a2Sgjelinek 				goto done;
4631d9e728a2Sgjelinek 			}
4632d9e728a2Sgjelinek 
4633d9e728a2Sgjelinek 			bzero(buf, sizeof (buf));
4634d9e728a2Sgjelinek 			(void) lseek(fd, 0L, SEEK_SET);
4635d9e728a2Sgjelinek 			if (read(fd, buf, sizeof (buf) - 1) < 0 ||
4636d9e728a2Sgjelinek 			    strncmp(buf, "<?xml", 5) != 0)
4637d9e728a2Sgjelinek 				zerror(gettext("%s is not an XML file"),
4638d9e728a2Sgjelinek 				    manifest_path);
4639d9e728a2Sgjelinek 			else
4640d9e728a2Sgjelinek 				zerror(gettext("Cannot attach to an earlier "
4641d9e728a2Sgjelinek 				    "release of the operating system"));
4642d9e728a2Sgjelinek 		} else {
4643d9e728a2Sgjelinek 			zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
4644d9e728a2Sgjelinek 		}
46458cd327d5Sgjelinek 		goto done;
46468cd327d5Sgjelinek 	}
46478cd327d5Sgjelinek 
4648ff17c8bfSgjelinek 	/* Retrieve remote handle brand type. */
4649e2482d1aSgjelinek 	if (zonecfg_get_brand(rem_handle, target_brand, sizeof (target_brand))
4650e2482d1aSgjelinek 	    != Z_OK) {
4651e2482d1aSgjelinek 		zerror(gettext("missing or invalid brand"));
4652e2482d1aSgjelinek 		exit(Z_ERR);
4653e2482d1aSgjelinek 	}
46548cd327d5Sgjelinek 
46558cd327d5Sgjelinek done:
46568cd327d5Sgjelinek 	zonecfg_fini_handle(local_handle);
46578cd327d5Sgjelinek 	zonecfg_fini_handle(rem_handle);
4658ff17c8bfSgjelinek 	(void) close(fd);
46598cd327d5Sgjelinek 
46608cd327d5Sgjelinek 	return ((res == Z_OK) ? Z_OK : Z_ERR);
46618cd327d5Sgjelinek }
46628cd327d5Sgjelinek 
46636cfd72c6Sgjelinek /* ARGSUSED */
4664ee519a1fSgjelinek static int
4665ee519a1fSgjelinek attach_func(int argc, char *argv[])
4666ee519a1fSgjelinek {
466771443f5aS 	int lockfd = -1;
4668ee519a1fSgjelinek 	int err, arg;
4669ee519a1fSgjelinek 	boolean_t force = B_FALSE;
4670ee519a1fSgjelinek 	zone_dochandle_t handle;
4671ee519a1fSgjelinek 	char zonepath[MAXPATHLEN];
467237774979Sgjelinek 	char cmdbuf[MAXPATHLEN];
4673ff17c8bfSgjelinek 	char postcmdbuf[MAXPATHLEN];
46748cd327d5Sgjelinek 	boolean_t execute = B_TRUE;
4675ff17c8bfSgjelinek 	boolean_t brand_help = B_FALSE;
46768cd327d5Sgjelinek 	char *manifest_path;
4677ff17c8bfSgjelinek 	char tmpmanifest[80];
4678ff17c8bfSgjelinek 	int manifest_pos;
467937774979Sgjelinek 	brand_handle_t bh = NULL;
4680ff17c8bfSgjelinek 	int status;
4681edfa49ffS 	int last_index = 0;
4682edfa49ffS 	int offset;
4683edfa49ffS 	char *up;
4684edfa49ffS 	boolean_t forced_update = B_FALSE;
4685ee519a1fSgjelinek 
4686ee519a1fSgjelinek 	if (zonecfg_in_alt_root()) {
4687ee519a1fSgjelinek 		zerror(gettext("cannot attach zone in alternate root"));
4688ee519a1fSgjelinek 		return (Z_ERR);
4689ee519a1fSgjelinek 	}
4690ee519a1fSgjelinek 
4691ff17c8bfSgjelinek 	/* Check the argv string for args we handle internally */
4692ee519a1fSgjelinek 	optind = 0;
4693ff17c8bfSgjelinek 	opterr = 0;
4694edfa49ffS 	while ((arg = getopt(argc, argv, "?Fn:U")) != EOF) {
4695ee519a1fSgjelinek 		switch (arg) {
4696ee519a1fSgjelinek 		case '?':
4697ff17c8bfSgjelinek 			if (optopt == '?') {
4698ee519a1fSgjelinek 				sub_usage(SHELP_ATTACH, CMD_ATTACH);
4699ff17c8bfSgjelinek 				brand_help = B_TRUE;
4700ff17c8bfSgjelinek 			}
4701ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4702ff17c8bfSgjelinek 			break;
4703ee519a1fSgjelinek 		case 'F':
4704ee519a1fSgjelinek 			force = B_TRUE;
4705ee519a1fSgjelinek 			break;
47068cd327d5Sgjelinek 		case 'n':
47078cd327d5Sgjelinek 			execute = B_FALSE;
47088cd327d5Sgjelinek 			manifest_path = optarg;
4709ff17c8bfSgjelinek 			manifest_pos = optind - 1;
47106cfd72c6Sgjelinek 			break;
4711edfa49ffS 		case 'U':
4712edfa49ffS 			/*
4713edfa49ffS 			 * Undocumented 'force update' option for p2v update on
4714edfa49ffS 			 * attach when zone is in the incomplete state.  Change
4715edfa49ffS 			 * the option back to 'u' and set forced_update flag.
4716edfa49ffS 			 */
4717edfa49ffS 			if (optind == last_index)
4718edfa49ffS 				offset = optind;
4719edfa49ffS 			else
4720edfa49ffS 				offset = optind - 1;
4721edfa49ffS 			if ((up = index(argv[offset], 'U')) != NULL)
4722edfa49ffS 				*up = 'u';
4723edfa49ffS 			forced_update = B_TRUE;
4724edfa49ffS 			break;
4725ee519a1fSgjelinek 		default:
4726ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4727ff17c8bfSgjelinek 			break;
4728ee519a1fSgjelinek 		}
4729edfa49ffS 		last_index = optind;
4730ee519a1fSgjelinek 	}
47318cd327d5Sgjelinek 
4732ff17c8bfSgjelinek 	if (brand_help) {
4733ff17c8bfSgjelinek 		force = B_FALSE;
4734ff17c8bfSgjelinek 		execute = B_TRUE;
4735ff17c8bfSgjelinek 	}
4736ff17c8bfSgjelinek 
4737ff17c8bfSgjelinek 	/* dry-run and force flags are mutually exclusive */
4738ff17c8bfSgjelinek 	if (!execute && force) {
4739ff17c8bfSgjelinek 		zerror(gettext("-F and -n flags are mutually exclusive"));
47406cfd72c6Sgjelinek 		return (Z_ERR);
47416cfd72c6Sgjelinek 	}
47426cfd72c6Sgjelinek 
47438cd327d5Sgjelinek 	/*
4744ff17c8bfSgjelinek 	 * If the no-execute option was specified, we don't do validation and
4745ff17c8bfSgjelinek 	 * need to figure out the brand, since there is no zone required to be
47468cd327d5Sgjelinek 	 * configured for this option.
47478cd327d5Sgjelinek 	 */
4748ff17c8bfSgjelinek 	if (execute) {
4749ff17c8bfSgjelinek 		if (!brand_help) {
4750ff17c8bfSgjelinek 			if (sanity_check(target_zone, CMD_ATTACH, B_FALSE,
4751edfa49ffS 			    B_TRUE, forced_update) != Z_OK)
4752ee519a1fSgjelinek 				return (Z_ERR);
4753ce28b40eSzt129084 			if (verify_details(CMD_ATTACH, argv) != Z_OK)
4754ee519a1fSgjelinek 				return (Z_ERR);
4755ff17c8bfSgjelinek 		}
4756ee519a1fSgjelinek 
4757ff17c8bfSgjelinek 		if ((err = zone_get_zonepath(target_zone, zonepath,
4758ff17c8bfSgjelinek 		    sizeof (zonepath))) != Z_OK) {
4759ee519a1fSgjelinek 			errno = err;
4760ff17c8bfSgjelinek 			zperror2(target_zone,
4761ff17c8bfSgjelinek 			    gettext("could not get zone path"));
4762ee519a1fSgjelinek 			return (Z_ERR);
4763ee519a1fSgjelinek 		}
4764ff17c8bfSgjelinek 	} else {
4765ff17c8bfSgjelinek 		if (dryrun_get_brand(manifest_path, tmpmanifest,
4766ff17c8bfSgjelinek 		    sizeof (tmpmanifest)) != Z_OK)
4767ff17c8bfSgjelinek 			return (Z_ERR);
4768ff17c8bfSgjelinek 
4769ff17c8bfSgjelinek 		argv[manifest_pos] = tmpmanifest;
4770ff17c8bfSgjelinek 		target_zone = "-";
4771ff17c8bfSgjelinek 		(void) strlcpy(zonepath, "-", sizeof (zonepath));
4772ff17c8bfSgjelinek 
4773ff17c8bfSgjelinek 		/* Run the brand's verify_adm hook. */
4774ff17c8bfSgjelinek 		if (verify_brand(NULL, CMD_ATTACH, argv) != Z_OK)
4775ff17c8bfSgjelinek 			return (Z_ERR);
4776ff17c8bfSgjelinek 	}
4777ff17c8bfSgjelinek 
4778ff17c8bfSgjelinek 	/*
4779ff17c8bfSgjelinek 	 * Fetch the attach and postattach hooks from the brand configuration.
4780ff17c8bfSgjelinek 	 */
478137774979Sgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
478237774979Sgjelinek 		zerror(gettext("missing or invalid brand"));
478337774979Sgjelinek 		return (Z_ERR);
478437774979Sgjelinek 	}
478537774979Sgjelinek 
4786ff17c8bfSgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_attach, target_zone,
4787ff17c8bfSgjelinek 	    zonepath) != Z_OK) {
4788ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing attach resource");
4789ff17c8bfSgjelinek 		brand_close(bh);
4790ff17c8bfSgjelinek 		return (Z_ERR);
4791ff17c8bfSgjelinek 	}
4792ff17c8bfSgjelinek 
4793ff17c8bfSgjelinek 	if (get_hook(bh, postcmdbuf, sizeof (postcmdbuf), brand_get_postattach,
4794ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
479537774979Sgjelinek 		zerror("invalid brand configuration: missing postattach "
479637774979Sgjelinek 		    "resource");
479737774979Sgjelinek 		brand_close(bh);
479837774979Sgjelinek 		return (Z_ERR);
479937774979Sgjelinek 	}
480037774979Sgjelinek 	brand_close(bh);
480137774979Sgjelinek 
4802ff17c8bfSgjelinek 	/* Append all options to attach hook. */
4803ff17c8bfSgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
480437774979Sgjelinek 		return (Z_ERR);
480537774979Sgjelinek 
4806ff17c8bfSgjelinek 	/* Append all options to postattach hook. */
4807ff17c8bfSgjelinek 	if (addoptions(postcmdbuf, argv, sizeof (postcmdbuf)) != Z_OK)
4808ff17c8bfSgjelinek 		return (Z_ERR);
4809ff17c8bfSgjelinek 
4810ff17c8bfSgjelinek 	if (execute && !brand_help) {
4811ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
4812ff17c8bfSgjelinek 			zerror(gettext("another %s may have an operation in "
4813ff17c8bfSgjelinek 			    "progress."), "zoneadm");
4814ee519a1fSgjelinek 			return (Z_ERR);
4815ee519a1fSgjelinek 		}
4816ff17c8bfSgjelinek 	}
4817ee519a1fSgjelinek 
481871443f5aS 	if (!force) {
481971443f5aS 		/*
482071443f5aS 		 * Not a force-attach, so we need to actually do the work.
482171443f5aS 		 */
4822ff17c8bfSgjelinek 		if (cmdbuf[0] != '\0') {
4823ff17c8bfSgjelinek 			/* Run the attach hook */
4824edfa49ffS 			status = do_subproc(cmdbuf);
482571443f5aS 			if ((status = subproc_status(gettext("brand-specific "
482671443f5aS 			    "attach"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
4827ff17c8bfSgjelinek 				if (status == ZONE_SUBPROC_USAGE && !brand_help)
4828ff17c8bfSgjelinek 					sub_usage(SHELP_ATTACH, CMD_ATTACH);
4829ff17c8bfSgjelinek 
483071443f5aS 				if (execute && !brand_help) {
4831edfa49ffS 					assert(zonecfg_lock_file_held(&lockfd));
483271443f5aS 					zonecfg_release_lock_file(target_zone,
483371443f5aS 					    lockfd);
483471443f5aS 					lockfd = -1;
4835ee519a1fSgjelinek 				}
4836ee519a1fSgjelinek 
483771443f5aS 				assert(lockfd == -1);
483871443f5aS 				return (Z_ERR);
483971443f5aS 			}
4840ee519a1fSgjelinek 		}
4841ee519a1fSgjelinek 
48429acbbeafSnn35248 		/*
4843ff17c8bfSgjelinek 		 * Else run the built-in attach support.
4844ff17c8bfSgjelinek 		 * This is a no-op since there is nothing to validate.
48459acbbeafSnn35248 		 */
4846ff17c8bfSgjelinek 
4847ff17c8bfSgjelinek 		/* If dry-run or help, then we're done. */
4848ff17c8bfSgjelinek 		if (!execute || brand_help) {
4849ff17c8bfSgjelinek 			if (!execute)
4850ff17c8bfSgjelinek 				(void) unlink(tmpmanifest);
485171443f5aS 			assert(lockfd == -1);
4852ff17c8bfSgjelinek 			return (Z_OK);
48539acbbeafSnn35248 		}
485471443f5aS 	}
48559acbbeafSnn35248 
4856ca733e25S 	/* Now we can validate that the zonepath exists. */
4857ca733e25S 	if (validate_zonepath(zonepath, CMD_ATTACH) != Z_OK) {
4858ca733e25S 		(void) fprintf(stderr, gettext("could not verify zonepath %s "
4859ca733e25S 		    "because of the above errors.\n"), zonepath);
4860ca733e25S 
4861ca733e25S 		assert(zonecfg_lock_file_held(&lockfd));
4862ca733e25S 		zonecfg_release_lock_file(target_zone, lockfd);
4863ca733e25S 		return (Z_ERR);
4864ca733e25S 	}
4865ca733e25S 
486671443f5aS 	if ((handle = zonecfg_init_handle()) == NULL) {
486771443f5aS 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
486871443f5aS 		err = Z_ERR;
486971443f5aS 	} else if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
487071443f5aS 		errno = err;
487171443f5aS 		zperror(cmd_to_str(CMD_ATTACH), B_TRUE);
487271443f5aS 		zonecfg_fini_handle(handle);
487371443f5aS 	} else {
4874ee519a1fSgjelinek 		zonecfg_rm_detached(handle, force);
487571443f5aS 		zonecfg_fini_handle(handle);
487671443f5aS 	}
4877ee519a1fSgjelinek 
487871443f5aS 	if (err == Z_OK &&
487971443f5aS 	    (err = zone_set_state(target_zone, ZONE_STATE_INSTALLED)) != Z_OK) {
4880ee519a1fSgjelinek 		errno = err;
4881ee519a1fSgjelinek 		zperror(gettext("could not reset state"), B_TRUE);
4882ee519a1fSgjelinek 	}
4883ee519a1fSgjelinek 
4884edfa49ffS 	assert(zonecfg_lock_file_held(&lockfd));
4885ff17c8bfSgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
488671443f5aS 	lockfd = -1;
4887ee519a1fSgjelinek 
488837774979Sgjelinek 	/* If we have a brand postattach hook, run it. */
4889ff17c8bfSgjelinek 	if (err == Z_OK && !force && postcmdbuf[0] != '\0') {
4890ff17c8bfSgjelinek 		status = do_subproc(postcmdbuf);
489137774979Sgjelinek 		if (subproc_status(gettext("brand-specific postattach"),
489237774979Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
489337774979Sgjelinek 			if ((err = zone_set_state(target_zone,
489437774979Sgjelinek 			    ZONE_STATE_CONFIGURED)) != Z_OK) {
489537774979Sgjelinek 				errno = err;
489637774979Sgjelinek 				zperror(gettext("could not reset state"),
489737774979Sgjelinek 				    B_TRUE);
489837774979Sgjelinek 			}
489937774979Sgjelinek 		}
490037774979Sgjelinek 	}
490137774979Sgjelinek 
490271443f5aS 	assert(lockfd == -1);
4903ee519a1fSgjelinek 	return ((err == Z_OK) ? Z_OK : Z_ERR);
4904ee519a1fSgjelinek }
4905ee519a1fSgjelinek 
4906865e09a4Sgjelinek /*
49077c478bd9Sstevel@tonic-gate  * On input, TRUE => yes, FALSE => no.
49087c478bd9Sstevel@tonic-gate  * On return, TRUE => 1, FALSE => 0, could not ask => -1.
49097c478bd9Sstevel@tonic-gate  */
49107c478bd9Sstevel@tonic-gate 
49117c478bd9Sstevel@tonic-gate static int
49127c478bd9Sstevel@tonic-gate ask_yesno(boolean_t default_answer, const char *question)
49137c478bd9Sstevel@tonic-gate {
49147c478bd9Sstevel@tonic-gate 	char line[64];	/* should be large enough to answer yes or no */
49157c478bd9Sstevel@tonic-gate 
49167c478bd9Sstevel@tonic-gate 	if (!isatty(STDIN_FILENO))
49177c478bd9Sstevel@tonic-gate 		return (-1);
49187c478bd9Sstevel@tonic-gate 	for (;;) {
49197c478bd9Sstevel@tonic-gate 		(void) printf("%s (%s)? ", question,
49207c478bd9Sstevel@tonic-gate 		    default_answer ? "[y]/n" : "y/[n]");
49217c478bd9Sstevel@tonic-gate 		if (fgets(line, sizeof (line), stdin) == NULL ||
49227c478bd9Sstevel@tonic-gate 		    line[0] == '\n')
49237c478bd9Sstevel@tonic-gate 			return (default_answer ? 1 : 0);
49247c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'y')
49257c478bd9Sstevel@tonic-gate 			return (1);
49267c478bd9Sstevel@tonic-gate 		if (tolower(line[0]) == 'n')
49277c478bd9Sstevel@tonic-gate 			return (0);
49287c478bd9Sstevel@tonic-gate 	}
49297c478bd9Sstevel@tonic-gate }
49307c478bd9Sstevel@tonic-gate 
4931ff17c8bfSgjelinek /* ARGSUSED */
49327c478bd9Sstevel@tonic-gate static int
49337c478bd9Sstevel@tonic-gate uninstall_func(int argc, char *argv[])
49347c478bd9Sstevel@tonic-gate {
49357c478bd9Sstevel@tonic-gate 	char line[ZONENAME_MAX + 128];	/* Enough for "Are you sure ..." */
49360b5de56dSgjelinek 	char rootpath[MAXPATHLEN], zonepath[MAXPATHLEN];
493737774979Sgjelinek 	char cmdbuf[MAXPATHLEN];
4938ff17c8bfSgjelinek 	char precmdbuf[MAXPATHLEN];
49397c478bd9Sstevel@tonic-gate 	boolean_t force = B_FALSE;
49407c478bd9Sstevel@tonic-gate 	int lockfd, answer;
49417c478bd9Sstevel@tonic-gate 	int err, arg;
4942ff17c8bfSgjelinek 	boolean_t brand_help = B_FALSE;
494337774979Sgjelinek 	brand_handle_t bh = NULL;
4944ff17c8bfSgjelinek 	int status;
49457c478bd9Sstevel@tonic-gate 
4946108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
4947108322fbScarlsonj 		zerror(gettext("cannot uninstall zone in alternate root"));
4948108322fbScarlsonj 		return (Z_ERR);
4949108322fbScarlsonj 	}
4950108322fbScarlsonj 
4951ff17c8bfSgjelinek 	/* Check the argv string for args we handle internally */
49527c478bd9Sstevel@tonic-gate 	optind = 0;
4953ff17c8bfSgjelinek 	opterr = 0;
49547c478bd9Sstevel@tonic-gate 	while ((arg = getopt(argc, argv, "?F")) != EOF) {
49557c478bd9Sstevel@tonic-gate 		switch (arg) {
49567c478bd9Sstevel@tonic-gate 		case '?':
4957ff17c8bfSgjelinek 			if (optopt == '?') {
49587c478bd9Sstevel@tonic-gate 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
4959ff17c8bfSgjelinek 				brand_help = B_TRUE;
4960ff17c8bfSgjelinek 			}
4961ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4962ff17c8bfSgjelinek 			break;
49637c478bd9Sstevel@tonic-gate 		case 'F':
49647c478bd9Sstevel@tonic-gate 			force = B_TRUE;
49657c478bd9Sstevel@tonic-gate 			break;
49667c478bd9Sstevel@tonic-gate 		default:
4967ff17c8bfSgjelinek 			/* Ignore unknown options - may be brand specific. */
4968ff17c8bfSgjelinek 			break;
49697c478bd9Sstevel@tonic-gate 		}
49707c478bd9Sstevel@tonic-gate 	}
49717c478bd9Sstevel@tonic-gate 
4972ff17c8bfSgjelinek 	if (!brand_help) {
4973ff17c8bfSgjelinek 		if (sanity_check(target_zone, CMD_UNINSTALL, B_FALSE, B_TRUE,
4974ff17c8bfSgjelinek 		    B_FALSE) != Z_OK)
49757c478bd9Sstevel@tonic-gate 			return (Z_ERR);
49767c478bd9Sstevel@tonic-gate 
4977ce28b40eSzt129084 		/*
4978ce28b40eSzt129084 		 * Invoke brand-specific handler.
4979ce28b40eSzt129084 		 */
4980ce28b40eSzt129084 		if (invoke_brand_handler(CMD_UNINSTALL, argv) != Z_OK)
4981ce28b40eSzt129084 			return (Z_ERR);
4982ce28b40eSzt129084 
49837c478bd9Sstevel@tonic-gate 		if (!force) {
49847c478bd9Sstevel@tonic-gate 			(void) snprintf(line, sizeof (line),
49857c478bd9Sstevel@tonic-gate 			    gettext("Are you sure you want to %s zone %s"),
49867c478bd9Sstevel@tonic-gate 			    cmd_to_str(CMD_UNINSTALL), target_zone);
49877c478bd9Sstevel@tonic-gate 			if ((answer = ask_yesno(B_FALSE, line)) == 0) {
49887c478bd9Sstevel@tonic-gate 				return (Z_OK);
49897c478bd9Sstevel@tonic-gate 			} else if (answer == -1) {
49907c478bd9Sstevel@tonic-gate 				zerror(gettext("Input not from terminal and -F "
49917c478bd9Sstevel@tonic-gate 				    "not specified: %s not done."),
49927c478bd9Sstevel@tonic-gate 				    cmd_to_str(CMD_UNINSTALL));
49937c478bd9Sstevel@tonic-gate 				return (Z_ERR);
49947c478bd9Sstevel@tonic-gate 			}
49957c478bd9Sstevel@tonic-gate 		}
4996ff17c8bfSgjelinek 	}
49977c478bd9Sstevel@tonic-gate 
49980b5de56dSgjelinek 	if ((err = zone_get_zonepath(target_zone, zonepath,
49990b5de56dSgjelinek 	    sizeof (zonepath))) != Z_OK) {
50007c478bd9Sstevel@tonic-gate 		errno = err;
50017c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not get zone path"));
50027c478bd9Sstevel@tonic-gate 		return (Z_ERR);
50037c478bd9Sstevel@tonic-gate 	}
5004ff17c8bfSgjelinek 
5005ff17c8bfSgjelinek 	/*
5006ff17c8bfSgjelinek 	 * Fetch the uninstall and preuninstall hooks from the brand
5007ff17c8bfSgjelinek 	 * configuration.
5008ff17c8bfSgjelinek 	 */
5009ff17c8bfSgjelinek 	if ((bh = brand_open(target_brand)) == NULL) {
5010ff17c8bfSgjelinek 		zerror(gettext("missing or invalid brand"));
5011ff17c8bfSgjelinek 		return (Z_ERR);
5012ff17c8bfSgjelinek 	}
5013ff17c8bfSgjelinek 
5014ff17c8bfSgjelinek 	if (get_hook(bh, cmdbuf, sizeof (cmdbuf), brand_get_uninstall,
5015ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
5016ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing uninstall "
5017ff17c8bfSgjelinek 		    "resource");
5018ff17c8bfSgjelinek 		brand_close(bh);
5019ff17c8bfSgjelinek 		return (Z_ERR);
5020ff17c8bfSgjelinek 	}
5021ff17c8bfSgjelinek 
5022ff17c8bfSgjelinek 	if (get_hook(bh, precmdbuf, sizeof (precmdbuf), brand_get_preuninstall,
5023ff17c8bfSgjelinek 	    target_zone, zonepath) != Z_OK) {
5024ff17c8bfSgjelinek 		zerror("invalid brand configuration: missing preuninstall "
5025ff17c8bfSgjelinek 		    "resource");
5026ff17c8bfSgjelinek 		brand_close(bh);
5027ff17c8bfSgjelinek 		return (Z_ERR);
5028ff17c8bfSgjelinek 	}
5029ff17c8bfSgjelinek 	brand_close(bh);
5030ff17c8bfSgjelinek 
5031ff17c8bfSgjelinek 	/* Append all options to preuninstall hook. */
5032ff17c8bfSgjelinek 	if (addoptions(precmdbuf, argv, sizeof (precmdbuf)) != Z_OK)
5033ff17c8bfSgjelinek 		return (Z_ERR);
5034ff17c8bfSgjelinek 
5035ff17c8bfSgjelinek 	/* Append all options to uninstall hook. */
5036ff17c8bfSgjelinek 	if (addoptions(cmdbuf, argv, sizeof (cmdbuf)) != Z_OK)
5037ff17c8bfSgjelinek 		return (Z_ERR);
5038ff17c8bfSgjelinek 
5039ff17c8bfSgjelinek 	if (!brand_help) {
50407c478bd9Sstevel@tonic-gate 		if ((err = zone_get_rootpath(target_zone, rootpath,
50417c478bd9Sstevel@tonic-gate 		    sizeof (rootpath))) != Z_OK) {
50427c478bd9Sstevel@tonic-gate 			errno = err;
5043ff17c8bfSgjelinek 			zperror2(target_zone, gettext("could not get root "
5044ff17c8bfSgjelinek 			    "path"));
50457c478bd9Sstevel@tonic-gate 			return (Z_ERR);
50467c478bd9Sstevel@tonic-gate 		}
50477c478bd9Sstevel@tonic-gate 
50487c478bd9Sstevel@tonic-gate 		/*
5049ff17c8bfSgjelinek 		 * If there seems to be a zoneadmd running for this zone, call
5050ff17c8bfSgjelinek 		 * it to tell it that an uninstall is happening; if all goes
5051ff17c8bfSgjelinek 		 * well it will then shut itself down.
50527c478bd9Sstevel@tonic-gate 		 */
5053ff17c8bfSgjelinek 		if (zonecfg_ping_zoneadmd(target_zone) == Z_OK) {
50547c478bd9Sstevel@tonic-gate 			zone_cmd_arg_t zarg;
50557c478bd9Sstevel@tonic-gate 			zarg.cmd = Z_NOTE_UNINSTALLING;
5056ff17c8bfSgjelinek 			/* we don't care too much if this fails, just plow on */
5057ff17c8bfSgjelinek 			(void) zonecfg_call_zoneadmd(target_zone, &zarg, locale,
5058ff17c8bfSgjelinek 			    B_TRUE);
50597c478bd9Sstevel@tonic-gate 		}
50607c478bd9Sstevel@tonic-gate 
5061ff17c8bfSgjelinek 		if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
5062ff17c8bfSgjelinek 			zerror(gettext("another %s may have an operation in "
5063ff17c8bfSgjelinek 			    "progress."), "zoneadm");
50647c478bd9Sstevel@tonic-gate 			return (Z_ERR);
50657c478bd9Sstevel@tonic-gate 		}
50667c478bd9Sstevel@tonic-gate 
50677c478bd9Sstevel@tonic-gate 		/* Don't uninstall the zone if anything is mounted there */
50687c478bd9Sstevel@tonic-gate 		err = zonecfg_find_mounts(rootpath, NULL, NULL);
50697c478bd9Sstevel@tonic-gate 		if (err) {
50700b5de56dSgjelinek 			zerror(gettext("These file systems are mounted on "
50717c478bd9Sstevel@tonic-gate 			    "subdirectories of %s.\n"), rootpath);
50727c478bd9Sstevel@tonic-gate 			(void) zonecfg_find_mounts(rootpath, zfm_print, NULL);
5073ff17c8bfSgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
50747c478bd9Sstevel@tonic-gate 			return (Z_ERR);
50757c478bd9Sstevel@tonic-gate 		}
507637774979Sgjelinek 	}
507737774979Sgjelinek 
5078ff17c8bfSgjelinek 	/* If we have a brand preuninstall hook, run it. */
5079ff17c8bfSgjelinek 	if (!brand_help && precmdbuf[0] != '\0') {
5080ab5dfd5eS 		status = do_subproc(precmdbuf);
508137774979Sgjelinek 		if (subproc_status(gettext("brand-specific preuninstall"),
508237774979Sgjelinek 		    status, B_FALSE) != ZONE_SUBPROC_OK) {
5083ff17c8bfSgjelinek 			zonecfg_release_lock_file(target_zone, lockfd);
508437774979Sgjelinek 			return (Z_ERR);
508537774979Sgjelinek 		}
508637774979Sgjelinek 	}
508737774979Sgjelinek 
5088ff17c8bfSgjelinek 	if (!brand_help) {
50897c478bd9Sstevel@tonic-gate 		err = zone_set_state(target_zone, ZONE_STATE_INCOMPLETE);
50907c478bd9Sstevel@tonic-gate 		if (err != Z_OK) {
50917c478bd9Sstevel@tonic-gate 			errno = err;
50927c478bd9Sstevel@tonic-gate 			zperror2(target_zone, gettext("could not set state"));
50937c478bd9Sstevel@tonic-gate 			goto bad;
50947c478bd9Sstevel@tonic-gate 		}
5095ff17c8bfSgjelinek 	}
50967c478bd9Sstevel@tonic-gate 
5097ff17c8bfSgjelinek 	/*
5098ff17c8bfSgjelinek 	 * If there is a brand uninstall hook, use it, otherwise use the
5099ff17c8bfSgjelinek 	 * built-in uninstall code.
5100ff17c8bfSgjelinek 	 */
5101ff17c8bfSgjelinek 	if (cmdbuf[0] != '\0') {
5102ff17c8bfSgjelinek 		/* Run the uninstall hook */
5103c75cc341S 		status = do_subproc(cmdbuf);
5104ff17c8bfSgjelinek 		if ((status = subproc_status(gettext("brand-specific "
5105ff17c8bfSgjelinek 		    "uninstall"), status, B_FALSE)) != ZONE_SUBPROC_OK) {
5106ff17c8bfSgjelinek 			if (status == ZONE_SUBPROC_USAGE && !brand_help)
5107ff17c8bfSgjelinek 				sub_usage(SHELP_UNINSTALL, CMD_UNINSTALL);
5108ff17c8bfSgjelinek 			if (!brand_help)
5109ff17c8bfSgjelinek 				zonecfg_release_lock_file(target_zone, lockfd);
5110ff17c8bfSgjelinek 			return (Z_ERR);
5111ff17c8bfSgjelinek 		}
5112ff17c8bfSgjelinek 
5113ff17c8bfSgjelinek 		if (brand_help)
5114ff17c8bfSgjelinek 			return (Z_OK);
5115ff17c8bfSgjelinek 	} else {
5116ff17c8bfSgjelinek 		/* If just help, we're done since there is no brand help. */
5117ff17c8bfSgjelinek 		if (brand_help)
5118ff17c8bfSgjelinek 			return (Z_OK);
5119ff17c8bfSgjelinek 
5120ff17c8bfSgjelinek 		/* Run the built-in uninstall support. */
51210b5de56dSgjelinek 		if ((err = cleanup_zonepath(zonepath, B_FALSE)) != Z_OK) {
51220b5de56dSgjelinek 			errno = err;
5123ff17c8bfSgjelinek 			zperror2(target_zone, gettext("cleaning up zonepath "
5124ff17c8bfSgjelinek 			    "failed"));
51257c478bd9Sstevel@tonic-gate 			goto bad;
51260b5de56dSgjelinek 		}
5127ff17c8bfSgjelinek 	}
51280b5de56dSgjelinek 
51297c478bd9Sstevel@tonic-gate 	err = zone_set_state(target_zone, ZONE_STATE_CONFIGURED);
51307c478bd9Sstevel@tonic-gate 	if (err != Z_OK) {
51317c478bd9Sstevel@tonic-gate 		errno = err;
51327c478bd9Sstevel@tonic-gate 		zperror2(target_zone, gettext("could not reset state"));
51337c478bd9Sstevel@tonic-gate 	}
51347c478bd9Sstevel@tonic-gate bad:
5135ff17c8bfSgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
51367c478bd9Sstevel@tonic-gate 	return (err);
51377c478bd9Sstevel@tonic-gate }
51387c478bd9Sstevel@tonic-gate 
5139108322fbScarlsonj /* ARGSUSED */
5140108322fbScarlsonj static int
5141108322fbScarlsonj mount_func(int argc, char *argv[])
5142108322fbScarlsonj {
5143108322fbScarlsonj 	zone_cmd_arg_t zarg;
51449acbbeafSnn35248 	boolean_t force = B_FALSE;
51459acbbeafSnn35248 	int arg;
5146108322fbScarlsonj 
51479acbbeafSnn35248 	/*
51489acbbeafSnn35248 	 * The only supported subargument to the "mount" subcommand is
51499acbbeafSnn35248 	 * "-f", which forces us to mount a zone in the INCOMPLETE state.
51509acbbeafSnn35248 	 */
51519acbbeafSnn35248 	optind = 0;
51529acbbeafSnn35248 	if ((arg = getopt(argc, argv, "f")) != EOF) {
51539acbbeafSnn35248 		switch (arg) {
51549acbbeafSnn35248 		case 'f':
51559acbbeafSnn35248 			force = B_TRUE;
51569acbbeafSnn35248 			break;
51579acbbeafSnn35248 		default:
5158108322fbScarlsonj 			return (Z_USAGE);
51599acbbeafSnn35248 		}
51609acbbeafSnn35248 	}
51619acbbeafSnn35248 	if (argc > optind)
51629acbbeafSnn35248 		return (Z_USAGE);
51639acbbeafSnn35248 
51649acbbeafSnn35248 	if (sanity_check(target_zone, CMD_MOUNT, B_FALSE, B_FALSE, force)
51659acbbeafSnn35248 	    != Z_OK)
5166108322fbScarlsonj 		return (Z_ERR);
5167ce28b40eSzt129084 	if (verify_details(CMD_MOUNT, argv) != Z_OK)
5168108322fbScarlsonj 		return (Z_ERR);
5169108322fbScarlsonj 
51709acbbeafSnn35248 	zarg.cmd = force ? Z_FORCEMOUNT : Z_MOUNT;
51716cfd72c6Sgjelinek 	zarg.bootbuf[0] = '\0';
5172ff17c8bfSgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5173108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5174108322fbScarlsonj 		return (Z_ERR);
5175108322fbScarlsonj 	}
5176108322fbScarlsonj 	return (Z_OK);
5177108322fbScarlsonj }
5178108322fbScarlsonj 
5179108322fbScarlsonj /* ARGSUSED */
5180108322fbScarlsonj static int
5181108322fbScarlsonj unmount_func(int argc, char *argv[])
5182108322fbScarlsonj {
5183108322fbScarlsonj 	zone_cmd_arg_t zarg;
5184108322fbScarlsonj 
5185108322fbScarlsonj 	if (argc > 0)
5186108322fbScarlsonj 		return (Z_USAGE);
51879acbbeafSnn35248 	if (sanity_check(target_zone, CMD_UNMOUNT, B_FALSE, B_FALSE, B_FALSE)
51889acbbeafSnn35248 	    != Z_OK)
5189108322fbScarlsonj 		return (Z_ERR);
5190108322fbScarlsonj 
5191108322fbScarlsonj 	zarg.cmd = Z_UNMOUNT;
5192ff17c8bfSgjelinek 	if (zonecfg_call_zoneadmd(target_zone, &zarg, locale, B_TRUE) != 0) {
5193108322fbScarlsonj 		zerror(gettext("call to %s failed"), "zoneadmd");
5194108322fbScarlsonj 		return (Z_ERR);
5195108322fbScarlsonj 	}
5196108322fbScarlsonj 	return (Z_OK);
5197108322fbScarlsonj }
5198108322fbScarlsonj 
51997c478bd9Sstevel@tonic-gate static int
5200555afedfScarlsonj mark_func(int argc, char *argv[])
5201555afedfScarlsonj {
5202555afedfScarlsonj 	int err, lockfd;
5203edfa49ffS 	int arg;
5204edfa49ffS 	boolean_t force = B_FALSE;
5205edfa49ffS 	int state;
5206555afedfScarlsonj 
5207edfa49ffS 	optind = 0;
5208edfa49ffS 	opterr = 0;
5209edfa49ffS 	while ((arg = getopt(argc, argv, "F")) != EOF) {
5210edfa49ffS 		switch (arg) {
5211edfa49ffS 		case 'F':
5212edfa49ffS 			force = B_TRUE;
5213edfa49ffS 			break;
5214edfa49ffS 		default:
5215555afedfScarlsonj 			return (Z_USAGE);
5216edfa49ffS 		}
5217edfa49ffS 	}
5218edfa49ffS 
5219edfa49ffS 	if (argc != (optind + 1))
5220edfa49ffS 		return (Z_USAGE);
5221edfa49ffS 
5222edfa49ffS 	if (strcmp(argv[optind], "configured") == 0)
5223edfa49ffS 		state = ZONE_STATE_CONFIGURED;
5224edfa49ffS 	else if (strcmp(argv[optind], "incomplete") == 0)
5225edfa49ffS 		state = ZONE_STATE_INCOMPLETE;
5226edfa49ffS 	else if (strcmp(argv[optind], "installed") == 0)
5227edfa49ffS 		state = ZONE_STATE_INSTALLED;
5228edfa49ffS 	else
5229edfa49ffS 		return (Z_USAGE);
5230edfa49ffS 
5231edfa49ffS 	if (state != ZONE_STATE_INCOMPLETE && !force)
5232edfa49ffS 		return (Z_USAGE);
5233edfa49ffS 
5234edfa49ffS 	if (sanity_check(target_zone, CMD_MARK, B_FALSE, B_TRUE, B_FALSE)
52359acbbeafSnn35248 	    != Z_OK)
5236555afedfScarlsonj 		return (Z_ERR);
5237555afedfScarlsonj 
5238ce28b40eSzt129084 	/*
5239ce28b40eSzt129084 	 * Invoke brand-specific handler.
5240ce28b40eSzt129084 	 */
5241ce28b40eSzt129084 	if (invoke_brand_handler(CMD_MARK, argv) != Z_OK)
5242ce28b40eSzt129084 		return (Z_ERR);
5243ce28b40eSzt129084 
5244ff17c8bfSgjelinek 	if (zonecfg_grab_lock_file(target_zone, &lockfd) != Z_OK) {
5245555afedfScarlsonj 		zerror(gettext("another %s may have an operation in progress."),
5246555afedfScarlsonj 		    "zoneadm");
5247555afedfScarlsonj 		return (Z_ERR);
5248555afedfScarlsonj 	}
5249555afedfScarlsonj 
5250edfa49ffS 	err = zone_set_state(target_zone, state);
5251555afedfScarlsonj 	if (err != Z_OK) {
5252555afedfScarlsonj 		errno = err;
5253555afedfScarlsonj 		zperror2(target_zone, gettext("could not set state"));
5254555afedfScarlsonj 	}
5255ff17c8bfSgjelinek 	zonecfg_release_lock_file(target_zone, lockfd);
5256555afedfScarlsonj 
5257555afedfScarlsonj 	return (err);
5258555afedfScarlsonj }
5259555afedfScarlsonj 
52600209230bSgjelinek /*
52610209230bSgjelinek  * Check what scheduling class we're running under and print a warning if
52620209230bSgjelinek  * we're not using FSS.
52630209230bSgjelinek  */
52640209230bSgjelinek static int
52650209230bSgjelinek check_sched_fss(zone_dochandle_t handle)
52660209230bSgjelinek {
52670209230bSgjelinek 	char class_name[PC_CLNMSZ];
52680209230bSgjelinek 
52690209230bSgjelinek 	if (zonecfg_get_dflt_sched_class(handle, class_name,
52700209230bSgjelinek 	    sizeof (class_name)) != Z_OK) {
52710209230bSgjelinek 		zerror(gettext("WARNING: unable to determine the zone's "
52720209230bSgjelinek 		    "scheduling class"));
52730209230bSgjelinek 	} else if (strcmp("FSS", class_name) != 0) {
52740209230bSgjelinek 		zerror(gettext("WARNING: The zone.cpu-shares rctl is set but\n"
52750209230bSgjelinek 		    "FSS is not the default scheduling class for this zone.  "
52760209230bSgjelinek 		    "FSS will be\nused for processes in the zone but to get "
52770209230bSgjelinek 		    "the full benefit of FSS,\nit should be the default "
52780209230bSgjelinek 		    "scheduling class.  See dispadmin(1M) for\nmore details."));
52790209230bSgjelinek 		return (Z_SYSTEM);
52800209230bSgjelinek 	}
52810209230bSgjelinek 
52820209230bSgjelinek 	return (Z_OK);
52830209230bSgjelinek }
52840209230bSgjelinek 
52850209230bSgjelinek static int
52860209230bSgjelinek check_cpu_shares_sched(zone_dochandle_t handle)
52870209230bSgjelinek {
52880209230bSgjelinek 	int err;
52890209230bSgjelinek 	int res = Z_OK;
52900209230bSgjelinek 	struct zone_rctltab rctl;
52910209230bSgjelinek 
52920209230bSgjelinek 	if ((err = zonecfg_setrctlent(handle)) != Z_OK) {
52930209230bSgjelinek 		errno = err;
52940209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
52950209230bSgjelinek 		return (err);
52960209230bSgjelinek 	}
52970209230bSgjelinek 
52980209230bSgjelinek 	while (zonecfg_getrctlent(handle, &rctl) == Z_OK) {
52990209230bSgjelinek 		if (strcmp(rctl.zone_rctl_name, "zone.cpu-shares") == 0) {
53000209230bSgjelinek 			if (check_sched_fss(handle) != Z_OK)
53010209230bSgjelinek 				res = Z_SYSTEM;
53020209230bSgjelinek 			break;
53030209230bSgjelinek 		}
53040209230bSgjelinek 	}
53050209230bSgjelinek 
53060209230bSgjelinek 	(void) zonecfg_endrctlent(handle);
53070209230bSgjelinek 
53080209230bSgjelinek 	return (res);
53090209230bSgjelinek }
53100209230bSgjelinek 
53110209230bSgjelinek /*
53127ef01d19Sgjelinek  * Check if there is a mix of processes running in different pools within the
53137ef01d19Sgjelinek  * zone.  This is currently only going to be called for the global zone from
53147ef01d19Sgjelinek  * apply_func but that could be generalized in the future.
53157ef01d19Sgjelinek  */
53167ef01d19Sgjelinek static boolean_t
53177ef01d19Sgjelinek mixed_pools(zoneid_t zoneid)
53187ef01d19Sgjelinek {
53197ef01d19Sgjelinek 	DIR *dirp;
53207ef01d19Sgjelinek 	dirent_t *dent;
53217ef01d19Sgjelinek 	boolean_t mixed = B_FALSE;
53227ef01d19Sgjelinek 	boolean_t poolid_set = B_FALSE;
53237ef01d19Sgjelinek 	poolid_t last_poolid = 0;
53247ef01d19Sgjelinek 
53257ef01d19Sgjelinek 	if ((dirp = opendir("/proc")) == NULL) {
53267ef01d19Sgjelinek 		zerror(gettext("could not open /proc"));
53277ef01d19Sgjelinek 		return (B_FALSE);
53287ef01d19Sgjelinek 	}
53297ef01d19Sgjelinek 
53307ef01d19Sgjelinek 	while ((dent = readdir(dirp)) != NULL) {
53317ef01d19Sgjelinek 		int procfd;
53327ef01d19Sgjelinek 		psinfo_t ps;
53337ef01d19Sgjelinek 		char procpath[MAXPATHLEN];
53347ef01d19Sgjelinek 
53357ef01d19Sgjelinek 		if (dent->d_name[0] == '.')
53367ef01d19Sgjelinek 			continue;
53377ef01d19Sgjelinek 
53387ef01d19Sgjelinek 		(void) snprintf(procpath, sizeof (procpath), "/proc/%s/psinfo",
53397ef01d19Sgjelinek 		    dent->d_name);
53407ef01d19Sgjelinek 
53417ef01d19Sgjelinek 		if ((procfd = open(procpath, O_RDONLY)) == -1)
53427ef01d19Sgjelinek 			continue;
53437ef01d19Sgjelinek 
53447ef01d19Sgjelinek 		if (read(procfd, &ps, sizeof (ps)) == sizeof (psinfo_t)) {
53457ef01d19Sgjelinek 			/* skip processes in other zones and system processes */
53467ef01d19Sgjelinek 			if (zoneid != ps.pr_zoneid || ps.pr_flag & SSYS) {
53477ef01d19Sgjelinek 				(void) close(procfd);
53487ef01d19Sgjelinek 				continue;
53497ef01d19Sgjelinek 			}
53507ef01d19Sgjelinek 
53517ef01d19Sgjelinek 			if (poolid_set) {
53527ef01d19Sgjelinek 				if (ps.pr_poolid != last_poolid)
53537ef01d19Sgjelinek 					mixed = B_TRUE;
53547ef01d19Sgjelinek 			} else {
53557ef01d19Sgjelinek 				last_poolid = ps.pr_poolid;
53567ef01d19Sgjelinek 				poolid_set = B_TRUE;
53577ef01d19Sgjelinek 			}
53587ef01d19Sgjelinek 		}
53597ef01d19Sgjelinek 
53607ef01d19Sgjelinek 		(void) close(procfd);
53617ef01d19Sgjelinek 
53627ef01d19Sgjelinek 		if (mixed)
53637ef01d19Sgjelinek 			break;
53647ef01d19Sgjelinek 	}
53657ef01d19Sgjelinek 
53667ef01d19Sgjelinek 	(void) closedir(dirp);
53677ef01d19Sgjelinek 
53687ef01d19Sgjelinek 	return (mixed);
53697ef01d19Sgjelinek }
53707ef01d19Sgjelinek 
53717ef01d19Sgjelinek /*
53727ef01d19Sgjelinek  * Check if a persistent or temporary pool is configured for the zone.
53737ef01d19Sgjelinek  * This is currently only going to be called for the global zone from
53747ef01d19Sgjelinek  * apply_func but that could be generalized in the future.
53757ef01d19Sgjelinek  */
53767ef01d19Sgjelinek static boolean_t
53777ef01d19Sgjelinek pool_configured(zone_dochandle_t handle)
53787ef01d19Sgjelinek {
53797ef01d19Sgjelinek 	int err1, err2;
53807ef01d19Sgjelinek 	struct zone_psettab pset_tab;
53817ef01d19Sgjelinek 	char poolname[MAXPATHLEN];
53827ef01d19Sgjelinek 
53837ef01d19Sgjelinek 	err1 = zonecfg_lookup_pset(handle, &pset_tab);
53847ef01d19Sgjelinek 	err2 = zonecfg_get_pool(handle, poolname, sizeof (poolname));
53857ef01d19Sgjelinek 
53867ef01d19Sgjelinek 	if (err1 == Z_NO_ENTRY &&
53877ef01d19Sgjelinek 	    (err2 == Z_NO_ENTRY || (err2 == Z_OK && strlen(poolname) == 0)))
53887ef01d19Sgjelinek 		return (B_FALSE);
53897ef01d19Sgjelinek 
53907ef01d19Sgjelinek 	return (B_TRUE);
53917ef01d19Sgjelinek }
53927ef01d19Sgjelinek 
53937ef01d19Sgjelinek /*
53940209230bSgjelinek  * This is an undocumented interface which is currently only used to apply
53950209230bSgjelinek  * the global zone resource management settings when the system boots.
53960209230bSgjelinek  * This function does not yet properly handle updating a running system so
53970209230bSgjelinek  * any projects running in the zone would be trashed if this function
53980209230bSgjelinek  * were to run after the zone had booted.  It also does not reset any
53990209230bSgjelinek  * rctl settings that were removed from zonecfg.  There is still work to be
54000209230bSgjelinek  * done before we can properly support dynamically updating the resource
54010209230bSgjelinek  * management settings for a running zone (global or non-global).  Thus, this
54020209230bSgjelinek  * functionality is undocumented for now.
54030209230bSgjelinek  */
54040209230bSgjelinek /* ARGSUSED */
54050209230bSgjelinek static int
54060209230bSgjelinek apply_func(int argc, char *argv[])
54070209230bSgjelinek {
54080209230bSgjelinek 	int err;
54090209230bSgjelinek 	int res = Z_OK;
54100209230bSgjelinek 	priv_set_t *privset;
54110209230bSgjelinek 	zoneid_t zoneid;
54120209230bSgjelinek 	zone_dochandle_t handle;
54130209230bSgjelinek 	struct zone_mcaptab mcap;
54140209230bSgjelinek 	char pool_err[128];
54150209230bSgjelinek 
54160209230bSgjelinek 	zoneid = getzoneid();
54170209230bSgjelinek 
54180209230bSgjelinek 	if (zonecfg_in_alt_root() || zoneid != GLOBAL_ZONEID ||
54190209230bSgjelinek 	    target_zone == NULL || strcmp(target_zone, GLOBAL_ZONENAME) != 0)
54200209230bSgjelinek 		return (usage(B_FALSE));
54210209230bSgjelinek 
54220209230bSgjelinek 	if ((privset = priv_allocset()) == NULL) {
54230209230bSgjelinek 		zerror(gettext("%s failed"), "priv_allocset");
54240209230bSgjelinek 		return (Z_ERR);
54250209230bSgjelinek 	}
54260209230bSgjelinek 
54270209230bSgjelinek 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
54280209230bSgjelinek 		zerror(gettext("%s failed"), "getppriv");
54290209230bSgjelinek 		priv_freeset(privset);
54300209230bSgjelinek 		return (Z_ERR);
54310209230bSgjelinek 	}
54320209230bSgjelinek 
54330209230bSgjelinek 	if (priv_isfullset(privset) == B_FALSE) {
54340209230bSgjelinek 		(void) usage(B_FALSE);
54350209230bSgjelinek 		priv_freeset(privset);
54360209230bSgjelinek 		return (Z_ERR);
54370209230bSgjelinek 	}
54380209230bSgjelinek 	priv_freeset(privset);
54390209230bSgjelinek 
54400209230bSgjelinek 	if ((handle = zonecfg_init_handle()) == NULL) {
54410209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
54420209230bSgjelinek 		return (Z_ERR);
54430209230bSgjelinek 	}
54440209230bSgjelinek 
54450209230bSgjelinek 	if ((err = zonecfg_get_handle(target_zone, handle)) != Z_OK) {
54460209230bSgjelinek 		errno = err;
54470209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
54480209230bSgjelinek 		zonecfg_fini_handle(handle);
54490209230bSgjelinek 		return (Z_ERR);
54500209230bSgjelinek 	}
54510209230bSgjelinek 
54520209230bSgjelinek 	/* specific error msgs are printed within apply_rctls */
54530209230bSgjelinek 	if ((err = zonecfg_apply_rctls(target_zone, handle)) != Z_OK) {
54540209230bSgjelinek 		errno = err;
54550209230bSgjelinek 		zperror(cmd_to_str(CMD_APPLY), B_TRUE);
54560209230bSgjelinek 		res = Z_ERR;
54570209230bSgjelinek 	}
54580209230bSgjelinek 
54590209230bSgjelinek 	if ((err = check_cpu_shares_sched(handle)) != Z_OK)
54600209230bSgjelinek 		res = Z_ERR;
54610209230bSgjelinek 
54627ef01d19Sgjelinek 	if (pool_configured(handle)) {
54637ef01d19Sgjelinek 		if (mixed_pools(zoneid)) {
54647ef01d19Sgjelinek 			zerror(gettext("Zone is using multiple resource "
54657ef01d19Sgjelinek 			    "pools.  The pool\nconfiguration cannot be "
54667ef01d19Sgjelinek 			    "applied without rebooting."));
54677ef01d19Sgjelinek 			res = Z_ERR;
54687ef01d19Sgjelinek 		} else {
54697ef01d19Sgjelinek 
54700209230bSgjelinek 			/*
54717ef01d19Sgjelinek 			 * The next two blocks of code attempt to set up
54727ef01d19Sgjelinek 			 * temporary pools as well as persistent pools.  In
54737ef01d19Sgjelinek 			 * both cases we call the functions unconditionally.
54747ef01d19Sgjelinek 			 * Within each funtion the code will check if the zone
54757ef01d19Sgjelinek 			 * is actually configured for a temporary pool or
54767ef01d19Sgjelinek 			 * persistent pool and just return if there is nothing
54777ef01d19Sgjelinek 			 * to do.
54780209230bSgjelinek 			 */
54797ef01d19Sgjelinek 			if ((err = zonecfg_bind_tmp_pool(handle, zoneid,
54807ef01d19Sgjelinek 			    pool_err, sizeof (pool_err))) != Z_OK) {
54817ef01d19Sgjelinek 				if (err == Z_POOL || err == Z_POOL_CREATE ||
54827ef01d19Sgjelinek 				    err == Z_POOL_BIND)
54837ef01d19Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
54847ef01d19Sgjelinek 					    pool_err);
54850209230bSgjelinek 				else
54867ef01d19Sgjelinek 					zerror(gettext("could not bind zone to "
54877ef01d19Sgjelinek 					    "temporary pool: %s"),
54887ef01d19Sgjelinek 					    zonecfg_strerror(err));
54890209230bSgjelinek 				res = Z_ERR;
54900209230bSgjelinek 			}
54910209230bSgjelinek 
54920209230bSgjelinek 			if ((err = zonecfg_bind_pool(handle, zoneid, pool_err,
54930209230bSgjelinek 			    sizeof (pool_err))) != Z_OK) {
54940209230bSgjelinek 				if (err == Z_POOL || err == Z_POOL_BIND)
54957ef01d19Sgjelinek 					zerror("%s: %s", zonecfg_strerror(err),
54967ef01d19Sgjelinek 					    pool_err);
54970209230bSgjelinek 				else
54980209230bSgjelinek 					zerror("%s", zonecfg_strerror(err));
54990209230bSgjelinek 			}
55007ef01d19Sgjelinek 		}
55017ef01d19Sgjelinek 	}
55020209230bSgjelinek 
55030209230bSgjelinek 	/*
55040209230bSgjelinek 	 * If a memory cap is configured, set the cap in the kernel using
55050209230bSgjelinek 	 * zone_setattr() and make sure the rcapd SMF service is enabled.
55060209230bSgjelinek 	 */
55070209230bSgjelinek 	if (zonecfg_getmcapent(handle, &mcap) == Z_OK) {
55080209230bSgjelinek 		uint64_t num;
55090209230bSgjelinek 		char smf_err[128];
55100209230bSgjelinek 
55110209230bSgjelinek 		num = (uint64_t)strtoll(mcap.zone_physmem_cap, NULL, 10);
55120209230bSgjelinek 		if (zone_setattr(zoneid, ZONE_ATTR_PHYS_MCAP, &num, 0) == -1) {
55130209230bSgjelinek 			zerror(gettext("could not set zone memory cap"));
55140209230bSgjelinek 			res = Z_ERR;
55150209230bSgjelinek 		}
55160209230bSgjelinek 
55170209230bSgjelinek 		if (zonecfg_enable_rcapd(smf_err, sizeof (smf_err)) != Z_OK) {
55180209230bSgjelinek 			zerror(gettext("enabling system/rcap service failed: "
55190209230bSgjelinek 			    "%s"), smf_err);
55200209230bSgjelinek 			res = Z_ERR;
55210209230bSgjelinek 		}
55220209230bSgjelinek 	}
55230209230bSgjelinek 
55240209230bSgjelinek 	zonecfg_fini_handle(handle);
55250209230bSgjelinek 
55260209230bSgjelinek 	return (res);
55270209230bSgjelinek }
55280209230bSgjelinek 
5529fbbfbc6eSjv227347 /*
5530fbbfbc6eSjv227347  * This is an undocumented interface that is invoked by the zones SMF service
5531fbbfbc6eSjv227347  * for installed zones that won't automatically boot.
5532fbbfbc6eSjv227347  */
5533fbbfbc6eSjv227347 /* ARGSUSED */
5534fbbfbc6eSjv227347 static int
5535fbbfbc6eSjv227347 sysboot_func(int argc, char *argv[])
5536fbbfbc6eSjv227347 {
5537fbbfbc6eSjv227347 	int err;
5538fbbfbc6eSjv227347 	zone_dochandle_t zone_handle;
5539fbbfbc6eSjv227347 	brand_handle_t brand_handle;
5540fbbfbc6eSjv227347 	char cmdbuf[MAXPATHLEN];
5541fbbfbc6eSjv227347 	char zonepath[MAXPATHLEN];
5542fbbfbc6eSjv227347 
5543fbbfbc6eSjv227347 	/*
5544fbbfbc6eSjv227347 	 * This subcommand can only be executed in the global zone on non-global
5545fbbfbc6eSjv227347 	 * zones.
5546fbbfbc6eSjv227347 	 */
5547fbbfbc6eSjv227347 	if (zonecfg_in_alt_root())
5548fbbfbc6eSjv227347 		return (usage(B_FALSE));
5549fbbfbc6eSjv227347 	if (sanity_check(target_zone, CMD_SYSBOOT, B_FALSE, B_TRUE, B_FALSE) !=
5550fbbfbc6eSjv227347 	    Z_OK)
5551fbbfbc6eSjv227347 		return (Z_ERR);
5552fbbfbc6eSjv227347 
5553fbbfbc6eSjv227347 	/*
5554fbbfbc6eSjv227347 	 * Fetch the sysboot hook from the target zone's brand.
5555fbbfbc6eSjv227347 	 */
5556fbbfbc6eSjv227347 	if ((zone_handle = zonecfg_init_handle()) == NULL) {
5557fbbfbc6eSjv227347 		zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5558fbbfbc6eSjv227347 		return (Z_ERR);
5559fbbfbc6eSjv227347 	}
5560fbbfbc6eSjv227347 	if ((err = zonecfg_get_handle(target_zone, zone_handle)) != Z_OK) {
5561fbbfbc6eSjv227347 		errno = err;
5562fbbfbc6eSjv227347 		zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5563fbbfbc6eSjv227347 		zonecfg_fini_handle(zone_handle);
5564fbbfbc6eSjv227347 		return (Z_ERR);
5565fbbfbc6eSjv227347 	}
5566fbbfbc6eSjv227347 	if ((err = zonecfg_get_zonepath(zone_handle, zonepath,
5567fbbfbc6eSjv227347 	    sizeof (zonepath))) != Z_OK) {
5568fbbfbc6eSjv227347 		errno = err;
5569fbbfbc6eSjv227347 		zperror(cmd_to_str(CMD_SYSBOOT), B_TRUE);
5570fbbfbc6eSjv227347 		zonecfg_fini_handle(zone_handle);
5571fbbfbc6eSjv227347 		return (Z_ERR);
5572fbbfbc6eSjv227347 	}
5573fbbfbc6eSjv227347 	if ((brand_handle = brand_open(target_brand)) == NULL) {
5574fbbfbc6eSjv227347 		zerror(gettext("missing or invalid brand during %s operation: "
5575fbbfbc6eSjv227347 		    "%s"), cmd_to_str(CMD_SYSBOOT), target_brand);
5576fbbfbc6eSjv227347 		zonecfg_fini_handle(zone_handle);
5577fbbfbc6eSjv227347 		return (Z_ERR);
5578fbbfbc6eSjv227347 	}
5579fbbfbc6eSjv227347 	err = get_hook(brand_handle, cmdbuf, sizeof (cmdbuf), brand_get_sysboot,
5580fbbfbc6eSjv227347 	    target_zone, zonepath);
5581fbbfbc6eSjv227347 	brand_close(brand_handle);
5582fbbfbc6eSjv227347 	zonecfg_fini_handle(zone_handle);
5583fbbfbc6eSjv227347 	if (err != Z_OK) {
5584fbbfbc6eSjv227347 		zerror(gettext("unable to get brand hook from brand %s for %s "
5585fbbfbc6eSjv227347 		    "operation"), target_brand, cmd_to_str(CMD_SYSBOOT));
5586fbbfbc6eSjv227347 		return (Z_ERR);
5587fbbfbc6eSjv227347 	}
5588fbbfbc6eSjv227347 
5589fbbfbc6eSjv227347 	/*
5590fbbfbc6eSjv227347 	 * If the hook wasn't defined (which is OK), then indicate success and
5591fbbfbc6eSjv227347 	 * return.  Otherwise, execute the hook.
5592fbbfbc6eSjv227347 	 */
5593fbbfbc6eSjv227347 	if (cmdbuf[0] != '\0')
5594fbbfbc6eSjv227347 		return ((subproc_status(gettext("brand sysboot operation"),
5595fbbfbc6eSjv227347 		    do_subproc(cmdbuf), B_FALSE) == ZONE_SUBPROC_OK) ? Z_OK :
5596fbbfbc6eSjv227347 		    Z_BRAND_ERROR);
5597fbbfbc6eSjv227347 	return (Z_OK);
5598fbbfbc6eSjv227347 }
5599fbbfbc6eSjv227347 
5600555afedfScarlsonj static int
56017c478bd9Sstevel@tonic-gate help_func(int argc, char *argv[])
56027c478bd9Sstevel@tonic-gate {
56037c478bd9Sstevel@tonic-gate 	int arg, cmd_num;
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate 	if (argc == 0) {
56067c478bd9Sstevel@tonic-gate 		(void) usage(B_TRUE);
56077c478bd9Sstevel@tonic-gate 		return (Z_OK);
56087c478bd9Sstevel@tonic-gate 	}
56097c478bd9Sstevel@tonic-gate 	optind = 0;
56107c478bd9Sstevel@tonic-gate 	if ((arg = getopt(argc, argv, "?")) != EOF) {
56117c478bd9Sstevel@tonic-gate 		switch (arg) {
56127c478bd9Sstevel@tonic-gate 		case '?':
56137c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
56147c478bd9Sstevel@tonic-gate 			return (optopt == '?' ? Z_OK : Z_USAGE);
56157c478bd9Sstevel@tonic-gate 		default:
56167c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
56177c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
56187c478bd9Sstevel@tonic-gate 		}
56197c478bd9Sstevel@tonic-gate 	}
56207c478bd9Sstevel@tonic-gate 	while (optind < argc) {
5621394a25e2Scarlsonj 		/* Private commands have NULL short_usage; omit them */
5622394a25e2Scarlsonj 		if ((cmd_num = cmd_match(argv[optind])) < 0 ||
5623394a25e2Scarlsonj 		    cmdtab[cmd_num].short_usage == NULL) {
56247c478bd9Sstevel@tonic-gate 			sub_usage(SHELP_HELP, CMD_HELP);
56257c478bd9Sstevel@tonic-gate 			return (Z_USAGE);
56267c478bd9Sstevel@tonic-gate 		}
56277c478bd9Sstevel@tonic-gate 		sub_usage(cmdtab[cmd_num].short_usage, cmd_num);
56287c478bd9Sstevel@tonic-gate 		optind++;
56297c478bd9Sstevel@tonic-gate 	}
56307c478bd9Sstevel@tonic-gate 	return (Z_OK);
56317c478bd9Sstevel@tonic-gate }
56327c478bd9Sstevel@tonic-gate 
56337c478bd9Sstevel@tonic-gate /*
56347c478bd9Sstevel@tonic-gate  * Returns: CMD_MIN thru CMD_MAX on success, -1 on error
56357c478bd9Sstevel@tonic-gate  */
56367c478bd9Sstevel@tonic-gate 
56377c478bd9Sstevel@tonic-gate static int
56387c478bd9Sstevel@tonic-gate cmd_match(char *cmd)
56397c478bd9Sstevel@tonic-gate {
56407c478bd9Sstevel@tonic-gate 	int i;
56417c478bd9Sstevel@tonic-gate 
56427c478bd9Sstevel@tonic-gate 	for (i = CMD_MIN; i <= CMD_MAX; i++) {
56437c478bd9Sstevel@tonic-gate 		/* return only if there is an exact match */
56447c478bd9Sstevel@tonic-gate 		if (strcmp(cmd, cmdtab[i].cmd_name) == 0)
56457c478bd9Sstevel@tonic-gate 			return (cmdtab[i].cmd_num);
56467c478bd9Sstevel@tonic-gate 	}
56477c478bd9Sstevel@tonic-gate 	return (-1);
56487c478bd9Sstevel@tonic-gate }
56497c478bd9Sstevel@tonic-gate 
56507c478bd9Sstevel@tonic-gate static int
56517c478bd9Sstevel@tonic-gate parse_and_run(int argc, char *argv[])
56527c478bd9Sstevel@tonic-gate {
56537c478bd9Sstevel@tonic-gate 	int i = cmd_match(argv[0]);
56547c478bd9Sstevel@tonic-gate 
56557c478bd9Sstevel@tonic-gate 	if (i < 0)
56567c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
56577c478bd9Sstevel@tonic-gate 	return (cmdtab[i].handler(argc - 1, &(argv[1])));
56587c478bd9Sstevel@tonic-gate }
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate static char *
56617c478bd9Sstevel@tonic-gate get_execbasename(char *execfullname)
56627c478bd9Sstevel@tonic-gate {
56637c478bd9Sstevel@tonic-gate 	char *last_slash, *execbasename;
56647c478bd9Sstevel@tonic-gate 
56657c478bd9Sstevel@tonic-gate 	/* guard against '/' at end of command invocation */
56667c478bd9Sstevel@tonic-gate 	for (;;) {
56677c478bd9Sstevel@tonic-gate 		last_slash = strrchr(execfullname, '/');
56687c478bd9Sstevel@tonic-gate 		if (last_slash == NULL) {
56697c478bd9Sstevel@tonic-gate 			execbasename = execfullname;
56707c478bd9Sstevel@tonic-gate 			break;
56717c478bd9Sstevel@tonic-gate 		} else {
56727c478bd9Sstevel@tonic-gate 			execbasename = last_slash + 1;
56737c478bd9Sstevel@tonic-gate 			if (*execbasename == '\0') {
56747c478bd9Sstevel@tonic-gate 				*last_slash = '\0';
56757c478bd9Sstevel@tonic-gate 				continue;
56767c478bd9Sstevel@tonic-gate 			}
56777c478bd9Sstevel@tonic-gate 			break;
56787c478bd9Sstevel@tonic-gate 		}
56797c478bd9Sstevel@tonic-gate 	}
56807c478bd9Sstevel@tonic-gate 	return (execbasename);
56817c478bd9Sstevel@tonic-gate }
56827c478bd9Sstevel@tonic-gate 
5683*cb8a054bSGlenn Faden static char *
5684*cb8a054bSGlenn Faden get_username()
5685*cb8a054bSGlenn Faden {
5686*cb8a054bSGlenn Faden 	uid_t uid;
5687*cb8a054bSGlenn Faden 	struct passwd *nptr;
5688*cb8a054bSGlenn Faden 
5689*cb8a054bSGlenn Faden 
5690*cb8a054bSGlenn Faden 	/*
5691*cb8a054bSGlenn Faden 	 * Authorizations are checked to restrict access based on the
5692*cb8a054bSGlenn Faden 	 * requested operation and zone name, It is assumed that the
5693*cb8a054bSGlenn Faden 	 * program is running with all privileges, but that the real
5694*cb8a054bSGlenn Faden 	 * user ID is that of the user or role on whose behalf we are
5695*cb8a054bSGlenn Faden 	 * operating. So we start by getting the username that will be
5696*cb8a054bSGlenn Faden 	 * used for subsequent authorization checks.
5697*cb8a054bSGlenn Faden 	 */
5698*cb8a054bSGlenn Faden 
5699*cb8a054bSGlenn Faden 	uid = getuid();
5700*cb8a054bSGlenn Faden 	if ((nptr = getpwuid(uid)) == NULL) {
5701*cb8a054bSGlenn Faden 		zerror(gettext("could not get user name."));
5702*cb8a054bSGlenn Faden 		exit(Z_ERR);
5703*cb8a054bSGlenn Faden 	}
5704*cb8a054bSGlenn Faden 	return (nptr->pw_name);
5705*cb8a054bSGlenn Faden }
5706*cb8a054bSGlenn Faden 
57077c478bd9Sstevel@tonic-gate int
57087c478bd9Sstevel@tonic-gate main(int argc, char **argv)
57097c478bd9Sstevel@tonic-gate {
57107c478bd9Sstevel@tonic-gate 	int arg;
57117c478bd9Sstevel@tonic-gate 	zoneid_t zid;
5712108322fbScarlsonj 	struct stat st;
57139acbbeafSnn35248 	char *zone_lock_env;
57149acbbeafSnn35248 	int err;
57157c478bd9Sstevel@tonic-gate 
57167c478bd9Sstevel@tonic-gate 	if ((locale = setlocale(LC_ALL, "")) == NULL)
57177c478bd9Sstevel@tonic-gate 		locale = "C";
57187c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
57197c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
57207c478bd9Sstevel@tonic-gate 	(void) sigset(SIGHUP, SIG_IGN);
57217c478bd9Sstevel@tonic-gate 	execname = get_execbasename(argv[0]);
5722*cb8a054bSGlenn Faden 	username = get_username();
57237c478bd9Sstevel@tonic-gate 	target_zone = NULL;
57247c478bd9Sstevel@tonic-gate 	if (chdir("/") != 0) {
57257c478bd9Sstevel@tonic-gate 		zerror(gettext("could not change directory to /."));
57267c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
57277c478bd9Sstevel@tonic-gate 	}
5728*cb8a054bSGlenn Faden 
5729d0e4f536SSudheer A 	/*
5730d0e4f536SSudheer A 	 * Use the default system mask rather than anything that may have been
5731d0e4f536SSudheer A 	 * set by the caller.
5732d0e4f536SSudheer A 	 */
5733d0e4f536SSudheer A 	(void) umask(CMASK);
57347c478bd9Sstevel@tonic-gate 
573599653d4eSeschrock 	if (init_zfs() != Z_OK)
573699653d4eSeschrock 		exit(Z_ERR);
573799653d4eSeschrock 
5738555afedfScarlsonj 	while ((arg = getopt(argc, argv, "?u:z:R:")) != EOF) {
57397c478bd9Sstevel@tonic-gate 		switch (arg) {
57407c478bd9Sstevel@tonic-gate 		case '?':
57417c478bd9Sstevel@tonic-gate 			return (usage(B_TRUE));
5742555afedfScarlsonj 		case 'u':
5743555afedfScarlsonj 			target_uuid = optarg;
5744555afedfScarlsonj 			break;
57457c478bd9Sstevel@tonic-gate 		case 'z':
57467c478bd9Sstevel@tonic-gate 			target_zone = optarg;
57477c478bd9Sstevel@tonic-gate 			break;
5748108322fbScarlsonj 		case 'R':	/* private option for admin/install use */
5749108322fbScarlsonj 			if (*optarg != '/') {
5750108322fbScarlsonj 				zerror(gettext("root path must be absolute."));
5751108322fbScarlsonj 				exit(Z_ERR);
5752108322fbScarlsonj 			}
5753108322fbScarlsonj 			if (stat(optarg, &st) == -1 || !S_ISDIR(st.st_mode)) {
5754108322fbScarlsonj 				zerror(
5755108322fbScarlsonj 				    gettext("root path must be a directory."));
5756108322fbScarlsonj 				exit(Z_ERR);
5757108322fbScarlsonj 			}
5758108322fbScarlsonj 			zonecfg_set_root(optarg);
5759108322fbScarlsonj 			break;
57607c478bd9Sstevel@tonic-gate 		default:
57617c478bd9Sstevel@tonic-gate 			return (usage(B_FALSE));
57627c478bd9Sstevel@tonic-gate 		}
57637c478bd9Sstevel@tonic-gate 	}
57647c478bd9Sstevel@tonic-gate 
57657c478bd9Sstevel@tonic-gate 	if (optind >= argc)
57667c478bd9Sstevel@tonic-gate 		return (usage(B_FALSE));
5767555afedfScarlsonj 
5768555afedfScarlsonj 	if (target_uuid != NULL && *target_uuid != '\0') {
5769555afedfScarlsonj 		uuid_t uuid;
5770555afedfScarlsonj 		static char newtarget[ZONENAME_MAX];
5771555afedfScarlsonj 
5772555afedfScarlsonj 		if (uuid_parse(target_uuid, uuid) == -1) {
5773555afedfScarlsonj 			zerror(gettext("illegal UUID value specified"));
5774555afedfScarlsonj 			exit(Z_ERR);
5775555afedfScarlsonj 		}
5776555afedfScarlsonj 		if (zonecfg_get_name_by_uuid(uuid, newtarget,
5777555afedfScarlsonj 		    sizeof (newtarget)) == Z_OK)
5778555afedfScarlsonj 			target_zone = newtarget;
5779555afedfScarlsonj 	}
5780555afedfScarlsonj 
57817c478bd9Sstevel@tonic-gate 	if (target_zone != NULL && zone_get_id(target_zone, &zid) != 0) {
57827c478bd9Sstevel@tonic-gate 		errno = Z_NO_ZONE;
57837c478bd9Sstevel@tonic-gate 		zperror(target_zone, B_TRUE);
57847c478bd9Sstevel@tonic-gate 		exit(Z_ERR);
57857c478bd9Sstevel@tonic-gate 	}
57869acbbeafSnn35248 
57879acbbeafSnn35248 	/*
57889acbbeafSnn35248 	 * See if we have inherited the right to manipulate this zone from
57899acbbeafSnn35248 	 * a zoneadm instance in our ancestry.  If so, set zone_lock_cnt to
57909acbbeafSnn35248 	 * indicate it.  If not, make that explicit in our environment.
57919acbbeafSnn35248 	 */
5792ff17c8bfSgjelinek 	zonecfg_init_lock_file(target_zone, &zone_lock_env);
57939acbbeafSnn35248 
5794e5816e35SEdward Pilatowicz 	/* Figure out what the system's default brand is */
5795e5816e35SEdward Pilatowicz 	if (zonecfg_default_brand(default_brand,
5796e5816e35SEdward Pilatowicz 	    sizeof (default_brand)) != Z_OK) {
5797e5816e35SEdward Pilatowicz 		zerror(gettext("unable to determine default brand"));
5798e5816e35SEdward Pilatowicz 		return (Z_ERR);
5799e5816e35SEdward Pilatowicz 	}
5800e5816e35SEdward Pilatowicz 
58019acbbeafSnn35248 	/*
58029acbbeafSnn35248 	 * If we are going to be operating on a single zone, retrieve its
58039acbbeafSnn35248 	 * brand type and determine whether it is native or not.
58049acbbeafSnn35248 	 */
58059acbbeafSnn35248 	if ((target_zone != NULL) &&
58062ad45a84Sjv227347 	    (strcmp(target_zone, GLOBAL_ZONENAME) != 0)) {
58079acbbeafSnn35248 		if (zone_get_brand(target_zone, target_brand,
58089acbbeafSnn35248 		    sizeof (target_brand)) != Z_OK) {
58099acbbeafSnn35248 			zerror(gettext("missing or invalid brand"));
58109acbbeafSnn35248 			exit(Z_ERR);
58119acbbeafSnn35248 		}
581262868012SSteve Lawrence 		/*
581362868012SSteve Lawrence 		 * In the alternate root environment, the only supported
581462868012SSteve Lawrence 		 * operations are mount and unmount.  In this case, just treat
581562868012SSteve Lawrence 		 * the zone as native if it is cluster.  Cluster zones can be
581662868012SSteve Lawrence 		 * native for the purpose of LU or upgrade, and the cluster
581762868012SSteve Lawrence 		 * brand may not exist in the miniroot (such as in net install
581862868012SSteve Lawrence 		 * upgrade).
581962868012SSteve Lawrence 		 */
582062868012SSteve Lawrence 		if (strcmp(target_brand, CLUSTER_BRAND_NAME) == 0) {
582162868012SSteve Lawrence 			if (zonecfg_in_alt_root()) {
5822e5816e35SEdward Pilatowicz 				(void) strlcpy(target_brand, default_brand,
582362868012SSteve Lawrence 				    sizeof (target_brand));
582462868012SSteve Lawrence 			}
582562868012SSteve Lawrence 		}
58269acbbeafSnn35248 	}
58279acbbeafSnn35248 
58289acbbeafSnn35248 	err = parse_and_run(argc - optind, &argv[optind]);
58299acbbeafSnn35248 
58309acbbeafSnn35248 	return (err);
58317c478bd9Sstevel@tonic-gate }
5832